1/* 2 * Copyright (c) 2024 Archermind Technology (Nanjing) Co. Ltd. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without modification, 5 * are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, this list of 8 * conditions and the following disclaimer. 9 * 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include <stdio.h> 32#include <unistd.h> 33#include <sys/stat.h> 34#include <sys/ioctl.h> 35 36#include <fcntl.h> 37 38#include "usb_net_host.h" 39#include "hdf_usb_pnp_manage.h" 40#include "cdc_ether.h" 41 42#define HDF_LOG_TAG usb_net_host 43#define USB_NET_SERVICE_NAME "hdf_usb_net_service" 44#define MAX_QUEUE_MEMORY (60 * 1518) 45#define PRINT_LINE_MAX 32 46#define USBNET_QLEN_TIME 5 47#define USBNET_QLEN_DEFAULT 4 48 49uint32_t g_sendToUrbTimes = 0; 50uint32_t g_sendToUrbSuccessTimes = 0; 51uint32_t g_sendToUrbReadTimes = 0; 52 53static int printf_char_buffer(char *buff, int size, bool isPrint) 54{ 55 if (isPrint) { 56 int i = 0; 57 HDF_LOGI("===-harch-=== printf_char_buffer begin"); 58 for (i = 0; i < size; i++) { 59 HDF_LOGI("%{public}02x ", buff[i]); 60 if ((i + 1) % PRINT_LINE_MAX == 0) { 61 HDF_LOGI(""); 62 } 63 } 64 HDF_LOGI("===-harch-=== printf_char_buffer end"); 65 } 66 return 0; 67} 68 69void UsbnetWriteLog(char *buff, int size, int tag) 70{ 71 HARCH_INFO_PRINT("begin"); 72 if (tag) { 73 struct timeval time; 74 gettimeofday(&time, NULL); 75 76 char str[1024] = {0}; 77 snprintf_s(str, sizeof(str), sizeof(str) - 1, "/data/log/%d%06d_%04d.txt", 78 time.tv_sec, time.tv_usec, size); 79 80 FILE *fp = fopen(str, "a+"); 81 if (!fp) { 82 HDF_LOGE("%{public}s: fopen failed", __func__); 83 return; 84 } 85 (void)fwrite(buff, size, 1, fp); 86 (void)fclose(fp); 87 } 88 HARCH_INFO_PRINT("end"); 89} 90 91// net process 92static int32_t UsbnetHostSendBufToNet(struct HdfIoService *serv, uint32_t id, 93 const void *buf, uint32_t length, int32_t *replyData) 94{ 95 HARCH_INFO_PRINT("begin"); 96 int32_t ret = 0; 97 struct HdfSBuf *data = HdfSbufObtainDefaultSize(); 98 if (data == NULL) { 99 HDF_LOGE("fail to obtain sbuf data"); 100 return HDF_FAILURE; 101 } 102 103 struct HdfSBuf *reply = HdfSbufObtainDefaultSize(); 104 if (reply == NULL) { 105 HDF_LOGE("fail to obtain sbuf reply"); 106 ret = HDF_DEV_ERR_NO_MEMORY; 107 goto out; 108 } 109 110 if (!HdfSbufWriteBuffer(data, buf, length)) { 111 HDF_LOGE("fail to write sbuf"); 112 ret = HDF_FAILURE; 113 goto out; 114 } 115 116 ret = serv->dispatcher->Dispatch(&serv->object, id, data, reply); 117 if (ret != HDF_SUCCESS) { 118 HDF_LOGE("fail to send service call"); 119 goto out; 120 } 121 122 if (!HdfSbufReadInt32(reply, replyData)) { 123 HDF_LOGE("fail to get service call reply"); 124 ret = HDF_ERR_INVALID_OBJECT; 125 goto out; 126 } 127 128 HARCH_INFO_PRINT("Get reply is: %{public}d", *replyData); 129out: 130 HdfSbufRecycle(data); 131 HdfSbufRecycle(reply); 132 return ret; 133} 134 135static void UsbnetHostTXComplete(const void *requestArg) 136{ 137 g_sendToUrbSuccessTimes++; 138 HARCH_INFO_PRINT("begin success times = %{public}d", g_sendToUrbSuccessTimes); 139 struct UsbRawRequest *req = (struct UsbRawRequest *)requestArg; 140 if (req == NULL) { 141 HDF_LOGE("%{public}s:%{public}d req is null!", __func__, __LINE__); 142 return; 143 } 144 145 struct UsbHostWb *wb = (struct UsbHostWb *)req->userData; 146 if (wb == NULL) { 147 HDF_LOGE("%{public}s:%{public}d userData(wb) is null!", __func__, __LINE__); 148 return; 149 } 150 151 if (req->status != USB_REQUEST_COMPLETED) { 152 HDF_LOGE("%{public}s: write req failed, status = %{public}d", __func__, req->status); 153 } 154 155 wb->use = 0; 156 HARCH_INFO_PRINT("%{public}s:%{public}d", __func__, __LINE__); 157 return; 158} 159 160 161static int32_t UsbnetHostStartWb(struct UsbnetHost *usbNet, struct UsbHostWb *wb) 162{ 163 HARCH_INFO_PRINT("begin"); 164 struct UsbRawFillRequestData reqData; 165 int32_t ret; 166 167 if ((usbNet == NULL) || 168 (wb == NULL) || 169 (usbNet->dataOutEp == NULL) || 170 (usbNet->devHandle == NULL) || 171 (wb->request == NULL)) { 172 HDF_LOGE("%{public}s:%{public}d", __func__, __LINE__); 173 return HDF_ERR_INVALID_PARAM; 174 } 175 176 HARCH_INFO_PRINT(); 177 usbNet->transmitting++; 178 179 reqData.endPoint = usbNet->dataOutEp->addr; 180 reqData.numIsoPackets = 0; 181 reqData.callback = UsbnetHostTXComplete; 182 reqData.userData = (void *)wb; 183 reqData.timeout = USB_CTRL_SET_TIMEOUT; 184 reqData.buffer = wb->buf; 185 reqData.length = wb->len; 186 187 HARCH_INFO_PRINT(); 188 ret = UsbRawFillBulkRequest(wb->request, usbNet->devHandle, &reqData); 189 if (ret) { 190 HDF_LOGE("%{public}s: FillInterruptRequest failed, ret = %{public}d", __func__, ret); 191 return HDF_FAILURE; 192 } 193 194 HARCH_INFO_PRINT(); 195 ret = UsbRawSubmitRequest(wb->request); 196 if (ret) { 197 HDF_LOGE("UsbRawSubmitRequest failed, ret = %{public}d", ret); 198 wb->use = 0; 199 usbNet->transmitting--; 200 return HDF_FAILURE; 201 } 202 HARCH_INFO_PRINT(); 203 return HDF_SUCCESS; 204} 205 206static int32_t UsbHostWbIsAvail(const struct UsbnetHost *usbNet) 207{ 208 int32_t i; 209 int32_t n = USBNET_NW; 210 211 OsalMutexLock((struct OsalMutex *)&usbNet->writeLock); 212 for (i = 0; i < USBNET_NW; i++) { 213 n -= usbNet->wb[i].use; 214 } 215 OsalMutexUnlock((struct OsalMutex *)&usbNet->writeLock); 216 HARCH_INFO_PRINT("g_sendToUrbTimes = %{public}d, curWbUse = %{public}d", g_sendToUrbTimes, n); 217 return n; 218} 219 220static int32_t UsbHostWbAlloc(const struct UsbnetHost *usbNet) 221{ 222 struct UsbHostWb *wb = NULL; 223 int32_t i; 224 225 for (i = 0; i < USBNET_NW; i++) { 226 wb = (struct UsbHostWb *)&usbNet->wb[i]; 227 if (!wb->use) { 228 wb->use = 1; 229 wb->len = 0; 230 return i; 231 } 232 } 233 return -1; 234} 235 236static int32_t UsbnetHostSnedbufToUrb(struct UsbnetHost *usbNet, struct HdfSBuf *data) 237{ 238 int32_t wbn; 239 int32_t size; 240 unsigned char *buf = NULL; 241 uint32_t bufSize = 0; 242 243 g_sendToUrbTimes++; 244 if (usbNet == NULL) { 245 HDF_LOGE("%{public}s: usbNet is null", __func__); 246 return HDF_ERR_INVALID_PARAM; 247 } 248 249 if (UsbHostWbIsAvail(usbNet)) { 250 wbn = UsbHostWbAlloc(usbNet); 251 if (wbn < 0 || wbn >= USBNET_NW) { 252 HDF_LOGE("%{public}s: UsbHostWbAlloc failed", __func__); 253 return HDF_FAILURE; 254 } 255 } else { 256 HDF_LOGE("%{public}s: no write buf", __func__); 257 return HDF_SUCCESS; 258 } 259 260 struct UsbHostWb *wb = &usbNet->wb[wbn]; 261 if (wb == NULL) { 262 return HDF_FAILURE; 263 } 264 265 int flag = HdfSbufReadBuffer(data, (const void **)(&(buf)), &bufSize); 266 if ((!flag) || buf == NULL) { 267 HDF_LOGE("%{public}s: fail to read infoTable in event data, flag = %{public}d", __func__, flag); 268 return HDF_ERR_INVALID_PARAM; 269 } 270 size = bufSize; 271 HARCH_INFO_PRINT("buf size = %{public}d, usbNet->dataOutEp->maxPacketSize = %{public}d", 272 bufSize, usbNet->dataOutEp->maxPacketSize); 273 274 printf_char_buffer((char *)buf, CPU_TO_LE32(bufSize), false); 275 UsbnetWriteLog((char *)buf, CPU_TO_LE32(bufSize), false); 276 if (usbNet->dataOutEp != NULL) { 277 size = (size > usbNet->dataOutEp->maxPacketSize) ? usbNet->dataOutEp->maxPacketSize : size; 278 if (memcpy_s(wb->buf, usbNet->dataOutEp->maxPacketSize, buf, size) != EOK) { 279 HDF_LOGE("%{public}s: memcpy_s fail", __func__); 280 return HDF_FAILURE; 281 } 282 } 283 284 wb->len = (uint32_t)size; 285 if (UsbnetHostStartWb(usbNet, wb) != HDF_SUCCESS) { 286 HDF_LOGE("%{public}s: UsbnetHostStartWb failed", __func__); 287 return HDF_FAILURE; 288 } 289 return HDF_SUCCESS; 290} 291 292static int32_t UsbnetHostWriteBufAlloc(struct UsbnetHost *usbNet) 293{ 294 struct UsbHostWb *wb = (struct UsbHostWb *)&usbNet->wb[0]; 295 int32_t i; 296 297 for (i = 0; i < USBNET_NW; i++, wb++) { 298 wb->buf = OsalMemCalloc(usbNet->dataOutEp->maxPacketSize); 299 if (!wb->buf) { 300 while (i > 0) { 301 --i; 302 --wb; 303 OsalMemFree(wb->buf); 304 wb->buf = NULL; 305 } 306 return -HDF_ERR_MALLOC_FAIL; 307 } 308 } 309 return HDF_SUCCESS; 310} 311 312static int32_t UsbnetHostAllocWriteRequests(struct UsbnetHost *usbNet) 313{ 314 int32_t i; 315 for (i = 0; i < USBNET_NW; i++) { 316 struct UsbHostWb *snd = &usbNet->wb[i]; 317 snd->request = UsbRawAllocRequest(usbNet->devHandle, 0, usbNet->dataOutEp->maxPacketSize); 318 snd->nNet = usbNet; 319 if (snd->request == NULL) { 320 HDF_LOGE("%{public}s: UsbRawAllocRequest failed", __func__); 321 return HDF_ERR_MALLOC_FAIL; 322 } 323 } 324 return HDF_SUCCESS; 325} 326 327static void UsbnetHostFreeWriteRequests(struct UsbnetHost *usbNet) 328{ 329 int32_t i; 330 struct UsbHostWb *snd = NULL; 331 for (i = 0; i < USBNET_NW; i++) { 332 snd = &usbNet->wb[i]; 333 if (snd->request != NULL) { 334 UsbRawFreeRequest(snd->request); 335 snd->request = NULL; 336 } 337 } 338 return; 339} 340 341static void UsbnetHostWriteBufFree(struct UsbnetHost *usbNet) 342{ 343 struct UsbHostWb *wb = &usbNet->wb[0]; 344 int32_t i; 345 346 for (i = 0; i < USBNET_NW; i++, wb++) { 347 if (wb->buf) { 348 OsalMemFree(wb->buf); 349 wb->buf = NULL; 350 } 351 } 352 return; 353} 354 355static int32_t UsbnetHostNotificationBufferProcess(const struct UsbRawRequest *req, 356 struct UsbnetHost *usbNet, unsigned int currentSize, unsigned int expectedSize) 357{ 358 if (usbNet->nbSize < expectedSize) { 359 if (usbNet->nbSize) { 360 OsalMemFree(usbNet->notificationBuffer); 361 usbNet->nbSize = 0; 362 } 363 unsigned int allocSize = expectedSize; 364 usbNet->notificationBuffer = (uint8_t *)OsalMemCalloc(allocSize); 365 if (!usbNet->notificationBuffer) { 366 return HDF_FAILURE; 367 } 368 usbNet->nbSize = allocSize; 369 } 370 unsigned int copySize = MIN(currentSize, expectedSize - usbNet->nbIndex); 371 int32_t ret = memcpy_s(&usbNet->notificationBuffer[usbNet->nbIndex], 372 usbNet->nbSize - usbNet->nbIndex, req->buffer, copySize); 373 if (ret != EOK) { 374 HDF_LOGE("memcpy_s fail ret=%{public}d", ret); 375 } 376 usbNet->nbIndex += copySize; 377 378 return HDF_SUCCESS; 379} 380 381static void UsbnetHostProcessNotification(const struct UsbnetHost *usbNet, const unsigned char *buf) 382{ 383 (void)usbNet; 384 struct UsbCdcNotification *dr = (struct UsbCdcNotification *)buf; 385 386 switch (dr->bNotificationType) { 387 case USB_DDK_CDC_NOTIFY_NETWORK_CONNECTION: 388 HARCH_INFO_PRINT("%{public}s - network connection: %{public}d\n", __func__, dr->wValue); 389 break; 390 case USB_DDK_CDC_NOTIFY_SERIAL_STATE: 391 HARCH_INFO_PRINT("the serial State change\n"); 392 break; 393 default: 394 HARCH_INFO_PRINT("%{public}s-%{public}d received: index %{public}d len %{public}d\n", 395 __func__, dr->bNotificationType, dr->wIndex, dr->wLength); 396 /* fall-through */ 397 } 398} 399 400static void UsbnetHostReqCallback(const void *requestArg) 401{ 402 struct UsbRawRequest *req = (struct UsbRawRequest *)requestArg; 403 if (req == NULL) { 404 HDF_LOGE("%{public}s:%{public}d req is NULL!", __func__, __LINE__); 405 return; 406 } 407 struct UsbnetHost *usbNet = (struct UsbnetHost *)req->userData; 408 if (usbNet == NULL) { 409 HDF_LOGE("%{public}s:%{public}d userData(usbNet) is NULL!", __func__, __LINE__); 410 return; 411 } 412 struct UsbCdcNotification *dr = (struct UsbCdcNotification *)req->buffer; 413 if (dr == NULL) { 414 HDF_LOGE("%{public}s:%{public}d req->buffer(dr) is NULL!", __func__, __LINE__); 415 return; 416 } 417 unsigned int currentSize = (unsigned int)req->actualLength; 418 unsigned int expectedSize = 0; 419 HARCH_INFO_PRINT("Irqstatus:%{public}d,actualLength:%{public}u\n", req->status, currentSize); 420 if (req->status != USB_REQUEST_COMPLETED) { 421 goto EXIT; 422 } 423 424 if (usbNet->nbIndex) { 425 dr = (struct UsbCdcNotification *)usbNet->notificationBuffer; 426 } 427 428 if (dr != NULL) { 429 expectedSize = sizeof(struct UsbCdcNotification) + LE16_TO_CPU(dr->wLength); 430 } else { 431 HDF_LOGE("%{public}s:%{public}d dr is NULL!", __func__, __LINE__); 432 return; 433 } 434 435 if (currentSize < expectedSize) { 436 if (UsbnetHostNotificationBufferProcess(req, usbNet, currentSize, expectedSize) != HDF_SUCCESS) { 437 goto EXIT; 438 } 439 currentSize = usbNet->nbIndex; 440 } 441 442 if (currentSize >= expectedSize) { 443 UsbnetHostProcessNotification(usbNet, (unsigned char *)dr); 444 usbNet->nbIndex = 0; 445 } 446 447 if (UsbRawSubmitRequest(req) != HDF_SUCCESS) { 448 HDF_LOGE("%{public}s - UsbRawSubmitRequest failed", __func__); 449 } 450EXIT: 451 HARCH_INFO_PRINT("%{public}s:%{public}d exit", __func__, __LINE__); 452} 453 454static int32_t UsbnetHostAllocStatusRequests(struct UsbnetHost *usbNet) 455{ 456 struct UsbRawFillRequestData fillRequestData; 457 uint32_t size = usbNet->statusEp->maxPacketSize; 458 int32_t ret; 459 460 usbNet->statusReq = UsbRawAllocRequest(usbNet->devHandle, 0, size); 461 if (!usbNet->statusReq) { 462 HDF_LOGE("statusReq request fail"); 463 return HDF_ERR_MALLOC_FAIL; 464 } 465 466 fillRequestData.endPoint = usbNet->statusEp->addr; 467 fillRequestData.length = size; 468 fillRequestData.numIsoPackets = 0; 469 fillRequestData.callback = UsbnetHostReqCallback; 470 fillRequestData.userData = (void *)usbNet; 471 fillRequestData.timeout = USB_CTRL_SET_TIMEOUT; 472 473 ret = UsbRawFillInterruptRequest(usbNet->statusReq, usbNet->devHandle, &fillRequestData); 474 if (ret) { 475 HDF_LOGE("%{public}s: FillInterruptRequest failed, ret=%{public}d", __func__, ret); 476 return HDF_FAILURE; 477 } 478 return HDF_SUCCESS; 479} 480 481static void UsbnetHostFreesSatusReqeust(struct UsbnetHost *usbNet) 482{ 483 int32_t ret; 484 if ((usbNet == NULL) || (usbNet->statusReq == NULL)) { 485 HDF_LOGE("%{public}s: usbNet or statusReq is NULL", __func__); 486 return; 487 } 488 489 ret = UsbRawFreeRequest(usbNet->statusReq); 490 if (ret == HDF_SUCCESS) { 491 usbNet->statusReq = NULL; 492 } else { 493 HDF_LOGE("%{public}s: UsbFreestatusReqeust failed, ret=%{public}d", __func__, ret); 494 } 495} 496 497static void UsbnetHostReadBulkCallback(const void *requestArg) 498{ 499 g_sendToUrbReadTimes++; 500 HARCH_INFO_PRINT("begin g_sendToUrbReadTimes = %{public}d", g_sendToUrbReadTimes); 501 502 struct UsbRawRequest *req = (struct UsbRawRequest *)requestArg; 503 if (req == NULL) { 504 HARCH_INFO_PRINT("%{public}s:%{public}d req is NULL!", __func__, __LINE__); 505 HDF_LOGE("%{public}s:%{public}d req is NULL!", __func__, __LINE__); 506 return; 507 } 508 509 struct UsbnetHost *usbNet = (struct UsbnetHost *)req->userData; 510 if (usbNet == NULL) { 511 HARCH_INFO_PRINT("%{public}s:%{public}d request userData is NULL!", __func__, __LINE__); 512 HDF_LOGE("%{public}s:%{public}d request userData is NULL!", __func__, __LINE__); 513 return; 514 } 515 size_t size = (size_t)req->actualLength; 516 517 if (req->status != USB_REQUEST_COMPLETED) { 518 HARCH_INFO_PRINT("%{public}s: the request is failed, status=%{public}d", __func__, req->status); 519 HDF_LOGE("%{public}s: the request is failed, status=%{public}d", __func__, req->status); 520 return; 521 } 522 523 HARCH_INFO_PRINT("Bulk status: %{public}d+size:%{public}zu", req->status, size); 524 printf_char_buffer((char *)req->buffer, CPU_TO_LE32(size), true); 525 HARCH_INFO_PRINT("Bulk status: %{public}d+size:%{public}zu", req->status, size); 526 527 //send readBuf to net begin 528 int32_t reply = 0; 529 OsalMutexLock(&usbNet->sendNetLock); 530 int32_t ret = UsbnetHostSendBufToNet(usbNet->hdfNetIoServ, USB_NET_RECIVE_DATA_FROM_USB, 531 (unsigned char *)req->buffer, CPU_TO_LE32(size), &reply); 532 if (ret != HDF_SUCCESS || reply != HDF_SUCCESS) { 533 HDF_LOGE("%{public}s:%{public}d fail to UsbnetHostSendBufToNet ret = %{public}d, reply = %{public}d!", 534 __func__, __LINE__, ret, reply); 535 } 536 OsalMutexUnlock(&usbNet->sendNetLock); 537 538 //send readBuf to net end 539 if (size == 0) { 540 HARCH_INFO_PRINT("DataFifoWrite"); 541 uint8_t *data = req->buffer; 542 OsalMutexLock(&usbNet->readLock); 543 if (DataFifoIsFull(&usbNet->readFifo)) { 544 DataFifoSkip(&usbNet->readFifo, size); 545 } 546 uint32_t count = DataFifoWrite(&usbNet->readFifo, data, size); 547 if (count != size) { 548 HDF_LOGE("%{public}s: write %{public}u less than expected %{public}zu", __func__, count, size); 549 } 550 OsalMutexUnlock(&usbNet->readLock); 551 } 552 553 if (UsbRawSubmitRequest(req) != HDF_SUCCESS) { 554 HDF_LOGE("%{public}s UsbRawSubmitRequest failed", __func__); 555 } 556} 557 558static int32_t UsbnetHostAllocReadRequests(struct UsbnetHost *usbNet) 559{ 560 struct UsbRawFillRequestData reqData = {}; 561 uint32_t size = usbNet->dataInEp->maxPacketSize; 562 HARCH_INFO_PRINT("read maxPacketSize read num = %{public}d", size); 563 564 for (int32_t i = 0; i < usbNet->readReqNum; i++) { 565 HARCH_INFO_PRINT("UsbRawAllocRequest read num = %{public}d", i); 566 usbNet->readReq[i] = UsbRawAllocRequest(usbNet->devHandle, 0, size); 567 if (!usbNet->readReq[i]) { 568 HDF_LOGE("readReq request failed"); 569 return HDF_ERR_MALLOC_FAIL; 570 } 571 572 reqData.endPoint = usbNet->dataInEp->addr; 573 reqData.numIsoPackets = 0; 574 reqData.callback = UsbnetHostReadBulkCallback; 575 reqData.userData = (void *)usbNet; 576 reqData.timeout = USB_CTRL_SET_TIMEOUT; 577 reqData.length = size; 578 579 HARCH_INFO_PRINT("UsbRawFillBulkRequest read num = %{public}d", i); 580 int32_t ret = UsbRawFillBulkRequest(usbNet->readReq[i], usbNet->devHandle, &reqData); 581 if (ret != HDF_SUCCESS) { 582 HDF_LOGE("%{public}s: FillBulkRequest failed, ret=%{public}d", __func__, ret); 583 return HDF_FAILURE; 584 } 585 } 586 return HDF_SUCCESS; 587} 588 589static void UsbnetHostFreeReadRequests(struct UsbnetHost *usbNet) 590{ 591 if (usbNet == NULL) { 592 HDF_LOGE("%{public}s: usbNet is NULL", __func__); 593 return; 594 } 595 596 int32_t i; 597 for (i = 0; i < usbNet->readReqNum; i++) { 598 if (usbNet->readReq[i]) { 599 UsbRawFreeRequest(usbNet->readReq[i]); 600 usbNet->readReq[i] = NULL; 601 } 602 } 603} 604 605static int32_t UsbnetHostAllocFifo(struct DataFifo *fifo, uint32_t size) 606{ 607 if (!DataFifoIsInitialized(fifo)) { 608 void *data = OsalMemAlloc(size); 609 if (data == NULL) { 610 HDF_LOGE("%{public}s:allocate failed", __func__); 611 return HDF_ERR_MALLOC_FAIL; 612 } 613 DataFifoInit(fifo, size, data); 614 } 615 return HDF_SUCCESS; 616} 617 618static void UsbnetHostFreeFifo(const struct DataFifo *fifo) 619{ 620 if (fifo == NULL) { 621 HDF_LOGE("%{public}s:%{public}d fifo is null", __func__, __LINE__); 622 return; 623 } 624 625 if (fifo->data != NULL) { 626 OsalMemFree((void *)fifo->data); 627 } 628 629 DataFifoInit((struct DataFifo *)fifo, 0, NULL); 630} 631 632static int32_t UsbIoThread(void *data) 633{ 634 int32_t ret; 635 struct UsbnetHost *usbNet = (struct UsbnetHost *)data; 636 HARCH_INFO_PRINT("begin"); 637 while (true) { 638 if (usbNet == NULL) { 639 HDF_LOGE("%{public}s:%{public}d usbNet is null", __func__, __LINE__); 640 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME); 641 continue; 642 } 643 644 if (usbNet->devHandle == NULL) { 645 HDF_LOGE("%{public}s:%{public}d usbNet->devHandle is null", __func__, __LINE__); 646 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME); 647 continue; 648 } 649 650 ret = UsbRawHandleRequests(usbNet->devHandle); 651 if ((ret < 0) || (usbNet->usbIoStatus != USB_RAW_IO_PROCESS_RUNNING)) { 652 HDF_LOGE("%{public}s:%{public}d UsbIoThread failed, usbNet->usbIoStatus =%{public}d ret=%{public}d ", 653 __func__, __LINE__, usbNet->usbIoStatus, ret); 654 break; 655 } 656 } 657 658 OsalMutexLock(&usbNet->usbIoLock); 659 usbNet->usbIoStatus = USB_RAW_IO_PROCESS_STOPED; 660 OsalMutexUnlock(&usbNet->usbIoLock); 661 662 HARCH_INFO_PRINT("end"); 663 return HDF_SUCCESS; 664} 665 666static int32_t UsbStartIo(struct UsbnetHost *usbNet) 667{ 668 struct OsalThreadParam threadCfg = {}; 669 int32_t ret = HDF_SUCCESS; 670 671 HARCH_INFO_PRINT(""); 672 OsalMutexInit(&usbNet->usbIoLock); 673 674 OsalMutexLock(&usbNet->usbIoLock); 675 usbNet->usbIoStatus = USB_RAW_IO_PROCESS_RUNNING; 676 OsalMutexUnlock(&usbNet->usbIoLock); 677 678 /* create Io thread */ 679 (void)memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg)); 680 threadCfg.name = "usb io thread"; 681 threadCfg.priority = OSAL_THREAD_PRI_LOW; 682 threadCfg.stackSize = USB_IO_THREAD_STACK_SIZE; 683 684 ret = OsalThreadCreate(&usbNet->ioThread, (OsalThreadEntry)UsbIoThread, (void *)usbNet); 685 if (ret != HDF_SUCCESS) { 686 HDF_LOGE("%{public}s:%{public}d OsalThreadCreate failed, ret = %{public}d", __func__, __LINE__, ret); 687 return ret; 688 } 689 690 ret = OsalThreadStart(&usbNet->ioThread, &threadCfg); 691 if (ret != HDF_SUCCESS) { 692 HDF_LOGE("%{public}s:%{public}d OsalThreadStart failed, ret = %{public}d", __func__, __LINE__, ret); 693 return ret; 694 } 695 return HDF_SUCCESS; 696} 697 698static void UsbStopIo(struct UsbnetHost *usbNet) 699{ 700 int32_t ret; 701 int32_t i = 0; 702 703 if (usbNet->usbIoStatus != USB_RAW_IO_PROCESS_STOPED) { 704 HARCH_INFO_PRINT("not stopped"); 705 OsalMutexLock(&usbNet->usbIoLock); 706 usbNet->usbIoStatus = USB_RAW_IO_PROCESS_STOP; 707 OsalMutexUnlock(&usbNet->usbIoLock); 708 } else { 709 HARCH_INFO_PRINT("stopped"); 710 } 711 712 while (usbNet->usbIoStatus != USB_RAW_IO_PROCESS_STOPED) { 713 i++; 714 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME); 715 if (i > USB_RAW_IO_STOP_WAIT_MAX_TIME) { 716 HDF_LOGD("%{public}s:%{public}d", __func__, __LINE__); 717 break; 718 } 719 } 720 721 ret = OsalThreadDestroy(&usbNet->ioThread); 722 if (ret != HDF_SUCCESS) { 723 HDF_LOGE("%{public}s:%{public}d OsalThreadDestroy failed, ret = %{public}d", __func__, __LINE__, ret); 724 } 725 726 OsalMutexDestroy(&usbNet->usbIoLock); 727 return; 728} 729 730static int32_t UsbnetHostAlloc(struct UsbnetHost *usbNet) 731{ 732 //1.write request 733 int ret = UsbnetHostWriteBufAlloc(usbNet); 734 if (ret < 0) { 735 HDF_LOGE("%{public}s:%{public}d usbNetWriteBufAlloc failed", __func__, __LINE__); 736 return ret; 737 } 738 739 ret = UsbnetHostAllocWriteRequests(usbNet); 740 if (ret != HDF_SUCCESS) { 741 HDF_LOGE("%{public}s: UsbRawAllocRequest failed", __func__); 742 UsbnetHostWriteBufFree(usbNet); 743 return ret; 744 } 745 746 //2.status request 747 ret = UsbnetHostAllocStatusRequests(usbNet); 748 if (ret != HDF_SUCCESS) { 749 HDF_LOGE("%{public}s: UsbRawAllocRequest failed", __func__); 750 UsbnetHostFreeWriteRequests(usbNet); 751 return ret; 752 } 753 754 //3.read request 755 ret = UsbnetHostAllocReadRequests(usbNet); 756 if (ret != HDF_SUCCESS) { 757 HDF_LOGE("%{public}s: UsbRawAllocRequest failed", __func__); 758 UsbnetHostFreesSatusReqeust(usbNet); 759 return ret; 760 } 761 return ret; 762} 763 764static int32_t UsbnetHostAllocRequests(struct UsbnetHost *usbNet) 765{ 766 if (usbNet->allocFlag == true) { 767 HDF_LOGI("UsbnetHostAllocRequests has been alloced"); 768 return HDF_SUCCESS; 769 } 770 771 int ret = UsbnetHostAlloc(usbNet); 772 if (ret < 0) { 773 HDF_LOGE("%{public}s:%{public}d UsbnetHostAlloc failed", __func__, __LINE__); 774 return ret; 775 } 776 777 ret = UsbStartIo(usbNet); 778 if (ret) { 779 HDF_LOGE("%{public}s:%{public}d UsbAllocReadRequests failed", __func__, __LINE__); 780 goto ERR_ALLOC_READ_REQS; 781 } 782 783 //status begin 784 ret = UsbRawSubmitRequest(usbNet->statusReq); 785 if (ret) { 786 HDF_LOGE("%{public}s:%{public}d UsbRawSubmitRequest failed", __func__, __LINE__); 787 goto ERR_SUBMIT_REQ; 788 } 789 790 //read begin 791 ret = UsbnetHostAllocFifo(&usbNet->readFifo, READ_BUF_SIZE); 792 if (ret != HDF_SUCCESS) { 793 HDF_LOGE("%{public}s: UsbnetHostAllocFifo failed", __func__); 794 goto ERR_SUBMIT_STATUS_REQ; 795 } 796 797 for (int32_t i = 0; i < usbNet->readReqNum; i++) { 798 HARCH_INFO_PRINT("UsbRawSubmitRequest read num = %{public}d", i); 799 ret = UsbRawSubmitRequest(usbNet->readReq[i]); 800 if (ret) { 801 HDF_LOGE("%{public}s: UsbRawSubmitRequest failed, ret=%{public}d ", __func__, ret); 802 goto ERR_SUBMIT_READ_REQ; 803 } 804 } 805 806 usbNet->allocFlag = true; 807 return HDF_SUCCESS; 808 809ERR_SUBMIT_READ_REQ: 810 UsbnetHostFreeFifo(&usbNet->readFifo); 811ERR_SUBMIT_STATUS_REQ: 812 UsbnetHostFreeReadRequests(usbNet); 813ERR_SUBMIT_REQ: 814 UsbStopIo(usbNet); 815ERR_ALLOC_READ_REQS: 816 UsbnetHostFreesSatusReqeust(usbNet); 817 818 return ret; 819} 820 821static void UsbnetHostFreeRequests(struct UsbnetHost *usbNet) 822{ 823 if (usbNet == NULL) { 824 HDF_LOGI("UsbnetHostFreeRequests usbNet is null"); 825 return; 826 } 827 //FIFO 828 OsalMutexLock(&usbNet->readLock); 829 UsbnetHostFreeFifo(&usbNet->readFifo); 830 OsalMutexUnlock(&usbNet->readLock); 831 //read 832 UsbnetHostFreeReadRequests(usbNet); 833 //status 834 UsbnetHostFreesSatusReqeust(usbNet); 835 //write 836 UsbnetHostFreeWriteRequests(usbNet); 837 UsbnetHostWriteBufFree(usbNet); 838 839 return ; 840} 841 842//--------------------------usb get config-------------- 843static void UsbnetHostPrintConfigDescriptor(struct UsbRawConfigDescriptor *tmpConfig) 844{ 845 HARCH_INFO_PRINT("bLength = %{public}d", tmpConfig->configDescriptor.bLength); 846 HARCH_INFO_PRINT("bDescriptorType = %{public}d", tmpConfig->configDescriptor.bDescriptorType); 847 HARCH_INFO_PRINT("wTotalLength = %{public}d", tmpConfig->configDescriptor.wTotalLength); 848 HARCH_INFO_PRINT("bNumInterfaces = %{public}d", tmpConfig->configDescriptor.bNumInterfaces); 849 HARCH_INFO_PRINT("bConfigurationValue = %{public}d", tmpConfig->configDescriptor.bConfigurationValue); 850 HARCH_INFO_PRINT("iConfiguration = %{public}d", tmpConfig->configDescriptor.iConfiguration); 851 HARCH_INFO_PRINT("bMaxPower = %{public}d", tmpConfig->configDescriptor.bMaxPower); 852 853 for (int i = 0; i < tmpConfig->configDescriptor.bNumInterfaces; i++) { 854 HARCH_INFO_PRINT("interface number = %{public}d", i); 855 for (int j = 0; j < tmpConfig->interface[i]->numAltsetting; j++) { 856 HARCH_INFO_PRINT("altsetting number = %{public}d", j); 857 for (int k = 0; k < tmpConfig->interface[i]->altsetting->interfaceDescriptor.bNumEndpoints; k++) { 858 HARCH_INFO_PRINT("bLength = %{public}d", 859 tmpConfig->interface[i]->altsetting[j].endPoint[k].endpointDescriptor.bLength); 860 HARCH_INFO_PRINT("bDescriptorType = %{public}d", 861 tmpConfig->interface[i]->altsetting[j].endPoint[k].endpointDescriptor.bDescriptorType); 862 HARCH_INFO_PRINT("bEndpointAddress = %{public}d", 863 tmpConfig->interface[i]->altsetting[j].endPoint[k].endpointDescriptor.bEndpointAddress); 864 HARCH_INFO_PRINT("bmAttributes = %{public}d", 865 tmpConfig->interface[i]->altsetting[j].endPoint[k].endpointDescriptor.bmAttributes); 866 HARCH_INFO_PRINT("wMaxPacketSize = %{public}d", 867 tmpConfig->interface[i]->altsetting[j].endPoint[k].endpointDescriptor.wMaxPacketSize); 868 HARCH_INFO_PRINT("bInterval = %{public}d", 869 tmpConfig->interface[i]->altsetting[j].endPoint[k].endpointDescriptor.bInterval); 870 HARCH_INFO_PRINT("bRefresh = %{public}d", 871 tmpConfig->interface[i]->altsetting[j].endPoint[k].endpointDescriptor.bRefresh); 872 HARCH_INFO_PRINT("bSynchAddress = %{public}d", 873 tmpConfig->interface[i]->altsetting[j].endPoint[k].endpointDescriptor.bSynchAddress); 874 } 875 } 876 } 877} 878 879static int32_t UsbnetHostGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConfigDescriptor **config) 880{ 881 UsbRawDevice *dev = NULL; 882 int32_t activeConfig = -1; 883 int32_t ret = HDF_SUCCESS; 884 if (devHandle == NULL) { 885 HDF_LOGE("%{public}s:%{public}d devHandle is null", __func__, __LINE__); 886 return HDF_ERR_INVALID_PARAM; 887 } 888 889 ret = UsbRawGetConfiguration(devHandle, &activeConfig); 890 if (ret) { 891 HDF_LOGE("%{public}s:%{public}d UsbRawGetConfiguration failed, ret = %{public}d", __func__, __LINE__, ret); 892 return HDF_FAILURE; 893 } 894 HARCH_INFO_PRINT("activeConfig = %{public}d", activeConfig); 895 dev = UsbRawGetDevice(devHandle); 896 if (dev == NULL) { 897 HDF_LOGE("%{public}s:%{public}d UsbRawGetDevice failed", __func__, __LINE__); 898 return HDF_FAILURE; 899 } 900 901 ret = UsbRawGetConfigDescriptor(dev, activeConfig, config); 902 if (ret) { 903 HDF_LOGE("UsbRawGetConfigDescriptor failed, ret = %{public}d", ret); 904 return HDF_FAILURE; 905 } 906 907 struct UsbRawConfigDescriptor *tmpConfig = (struct UsbRawConfigDescriptor *)*config; 908 UsbnetHostPrintConfigDescriptor(tmpConfig); 909 return HDF_SUCCESS; 910} 911 912/* must be called if hard_mtu or rx_urb_size changed */ 913static void UsbnetHostUpdateMaxQlen(struct UsbnetHost *usbNet) 914{ 915 int32_t ret = UsbRawGetUsbSpeed(usbNet->devHandle); 916 HARCH_INFO_PRINT("speed = %{public}d", ret); 917 if (ret < 0) { 918 HDF_LOGE("%{public}s:%{public}d UsbGetUsbSpeed failed", __func__, __LINE__); 919 ret = HDF_FAILURE; 920 } 921 enum UsbnetHostDeviceSpeed speed = ret; 922 switch (speed) { 923 case USB_SPEED_HIGH: 924 usbNet->rxQlen = MAX_QUEUE_MEMORY / usbNet->net.rxUrbSize; 925 usbNet->txQlen = MAX_QUEUE_MEMORY / usbNet->net.hardMtu; 926 break; 927 case USB_SPEED_SUPER: 928 case USB_SPEED_SUPER_PLUS: 929 /* 930 * Not take default 5ms qlen for super speed HC to 931 * save memory, and iperf tests show 2.5ms qlen can 932 * work well 933 */ 934 usbNet->rxQlen = USBNET_QLEN_TIME * MAX_QUEUE_MEMORY / usbNet->net.rxUrbSize; 935 usbNet->txQlen = USBNET_QLEN_TIME * MAX_QUEUE_MEMORY / usbNet->net.hardMtu; 936 break; 937 default: 938 usbNet->rxQlen = usbNet->txQlen = USBNET_QLEN_DEFAULT; 939 /* fall-through */ 940 } 941 HARCH_INFO_PRINT("usbNet->rxQlen = %{public}d, usbNet->txQlen = %{public}d," 942 "usbNet->rxUrbSize = %{public}d, usbNet->hardMtu = %{public}d", 943 usbNet->rxQlen, usbNet->txQlen, usbNet->net.rxUrbSize, usbNet->net.hardMtu); 944} 945 946static int32_t UsbnetHostInitObject(struct UsbnetHost *usbNet) 947{ 948 int ret = HDF_SUCCESS; 949 //net init 950 usbNet->net.mtu = DEFAULT_MTU; 951 usbNet->net.hardHeaderLen = DEFAULT_NET_HEAD_LEN; 952 usbNet->net.hardMtu = usbNet->net.mtu + usbNet->net.hardHeaderLen; 953 //falgs of usb device 954 if (usbNet->driverInfo->flags) { 955 usbNet->net.usbFlags = usbNet->driverInfo->flags; 956 } 957 958 usbNet->net.isBindDevice = 0; 959 if (usbNet->driverInfo->bind) { 960 usbNet->net.isBindDevice = 1; 961 ret = usbNet->driverInfo->bind(usbNet); 962 if (ret) { 963 HDF_LOGE("%{public}s:%{public}d bind failed", __func__, __LINE__); 964 ret = HDF_FAILURE; 965 return ret; 966 } 967 968 HARCH_INFO_PRINT("net->mtu = %{public}d, dev->hardMtu= %{public}d ,net->hardHeaderLen = %{public}d", 969 usbNet->net.mtu, usbNet->net.hardMtu, usbNet->net.hardHeaderLen); 970 /* maybe the remote can't receive an Ethernet MTU */ 971 if (usbNet->net.mtu > (usbNet->net.hardMtu - usbNet->net.hardHeaderLen)) { 972 usbNet->net.mtu = usbNet->net.hardMtu - usbNet->net.hardHeaderLen; 973 } 974 } 975 976 if (!usbNet->net.rxUrbSize) { 977 usbNet->net.rxUrbSize = usbNet->net.hardMtu; 978 } 979 980 HARCH_INFO_PRINT("rxUrbSize = %{public}d\n", usbNet->net.rxUrbSize); 981 HARCH_INFO_PRINT("net->mtu = %{public}d,dev->hardMtu= %{public}d ,net->hardHeaderLen = %{public}d", 982 usbNet->net.mtu, usbNet->net.hardMtu, usbNet->net.hardHeaderLen); 983 UsbnetHostUpdateMaxQlen(usbNet); 984 usbNet->net.txQlen = usbNet->txQlen; 985 if (usbNet->canDmaSg && 986 !(usbNet->driverInfo->flags & FLAG_SEND_ZLP) && 987 !(usbNet->driverInfo->flags & FLAG_MULTI_PACKET)) { 988 HARCH_INFO_PRINT(); 989 usbNet->paddingPkt = (uint8_t *)OsalMemAlloc(1); 990 if (!usbNet->paddingPkt) { 991 HDF_LOGE("%{public}s:%{public}d OsalMemAlloc failed", __func__, __LINE__); 992 ret = HDF_ERR_MALLOC_FAIL; 993 return ret; 994 } 995 } 996 usbNet->initFlag = true; 997 return ret; 998} 999 1000static int32_t UsbnetHostUsbRawInit(struct UsbnetHost *usbNet) 1001{ 1002 int32_t ret = HDF_SUCCESS; 1003 struct UsbSession *session = NULL; 1004 HARCH_INFO_PRINT("initFlag:%{public}d", usbNet->initFlag); 1005 if (usbNet->initFlag) { 1006 HDF_LOGE("%{public}s:%{public}d: initFlag is true", __func__, __LINE__); 1007 return HDF_SUCCESS; 1008 } 1009 1010 HARCH_INFO_PRINT("busNum:%{public}d, devAddr:%{public}#x", usbNet->busNum, usbNet->devAddr); 1011 //1.session 1012 ret = UsbRawInit(&session); 1013 if (ret) { 1014 HDF_LOGE("%{public}s:%{public}d UsbRawInit failed", __func__, __LINE__); 1015 return HDF_ERR_IO; 1016 } 1017 usbNet->session = session; 1018 //2.handle 1019 UsbRawHandle *devHandle = UsbRawOpenDevice(session, usbNet->busNum, usbNet->devAddr); 1020 if (devHandle == NULL) { 1021 HDF_LOGE("%{public}s:%{public}d UsbRawOpenDevice failed", __func__, __LINE__); 1022 ret = HDF_FAILURE; 1023 goto ERR_OPEN_DEVICE; 1024 } 1025 usbNet->devHandle = devHandle; 1026 //3.get para 1027 HARCH_INFO_PRINT(); 1028 ret = UsbnetHostGetConfigDescriptor(devHandle, &usbNet->config); 1029 if (ret) { 1030 HDF_LOGE("%{public}s:%{public}d UsbGetConfigDescriptor failed", __func__, __LINE__); 1031 ret = HDF_FAILURE; 1032 goto ERR_GET_DESC; 1033 } 1034 ret = UsbnetHostInitObject(usbNet); 1035 if (ret!= HDF_SUCCESS) { 1036 goto ERR_GET_DESC; 1037 } 1038 return ret; 1039ERR_GET_DESC: 1040 (void)UsbRawCloseDevice(devHandle); 1041ERR_OPEN_DEVICE: 1042 UsbRawExit(usbNet->session); 1043 return ret; 1044} 1045 1046static int32_t UsbnetHostUpdateFlags(struct UsbnetHost *usbNet, struct HdfSBuf *data) 1047{ 1048 int32_t* flags = NULL; 1049 uint32_t readSize = 0; 1050 HARCH_INFO_PRINT("begin"); 1051 if (NULL == usbNet || NULL == data) { 1052 HDF_LOGE("param invalid!"); 1053 return HDF_FAILURE; 1054 } 1055 HARCH_INFO_PRINT("before set flags usbNet->flags = %{public}d", usbNet->flags); 1056 if (!HdfSbufReadBuffer(data, (const void **)&flags, &readSize)) { 1057 HDF_LOGE("%{public}s:%{public}d fail to read usbnet flags from usb net adapter", __func__, __LINE__); 1058 return HDF_FAILURE; 1059 } 1060 usbNet->flags = *flags; 1061 HARCH_INFO_PRINT("after set flags usbNet->flags = %{public}d, readSize = %{public}d", usbNet->flags, readSize); 1062 return HDF_SUCCESS; 1063} 1064 1065static void UsbnetHostUpdateHardMtu(struct UsbnetHost *usbNet, struct HdfSBuf *data) 1066{ 1067 uint32_t readSize = 0; 1068 HARCH_INFO_PRINT("begin"); 1069 if (NULL == usbNet || NULL == data) { 1070 HDF_LOGE("param invalid!"); 1071 return; 1072 } 1073 1074 HARCH_INFO_PRINT("before hardMtu = %{public}d, rxUrbSize = %{public}d", usbNet->net.hardMtu, usbNet->net.rxUrbSize); 1075 if (!HdfSbufReadBuffer(data, (const void **)&usbNet->net, &readSize)) { 1076 HDF_LOGE("%{public}s:%{public}d fail to read usbnet hardMtu from usb net adapter", __func__, __LINE__); 1077 return; 1078 } 1079 HARCH_INFO_PRINT("after hardMtu = %{public}d, rxUrbSize = %{public}d, readSize = %{public}d", 1080 usbNet->net.hardMtu, usbNet->net.rxUrbSize, readSize); 1081 return; 1082} 1083 1084static int32_t UsbnetHostOpen(struct UsbnetHost *usbNet, struct HdfSBuf *data) 1085{ 1086 HARCH_INFO_PRINT("begin"); 1087 if (NULL == usbNet || NULL == data) { 1088 HDF_LOGE("param invalid!"); 1089 return HDF_FAILURE; 1090 } 1091 1092 int ret = UsbnetHostUpdateFlags(usbNet, data); 1093 if (HDF_SUCCESS != ret) { 1094 HDF_LOGE("%{public}s: fail to Update Flags", __func__); 1095 return ret; 1096 } 1097 1098 /* 3. update usbnet max qlen */ 1099 UsbnetHostUpdateMaxQlen(usbNet); 1100 usbNet->dataOutEp->maxPacketSize = (usbNet->dataOutEp->maxPacketSize > usbNet->net.rxUrbSize ? 1101 usbNet->dataOutEp->maxPacketSize : usbNet->net.rxUrbSize); 1102 usbNet->dataInEp->maxPacketSize = (usbNet->dataInEp->maxPacketSize > usbNet->net.rxUrbSize ? 1103 usbNet->dataInEp->maxPacketSize : usbNet->net.rxUrbSize); 1104 1105 HARCH_INFO_PRINT("dataOutEp-maxPacketSize = %{public}d", usbNet->dataOutEp->maxPacketSize); 1106 HARCH_INFO_PRINT("dataInEp-maxPacketSize = %{public}d", usbNet->dataInEp->maxPacketSize); 1107 HARCH_INFO_PRINT("read num = %{public}d", usbNet->rxQlen); 1108 HARCH_INFO_PRINT("write num = %{public}d", usbNet->txQlen); 1109 1110 OsalMutexInit(&usbNet->readLock); 1111 OsalMutexInit(&usbNet->writeLock); 1112 OsalMutexInit(&usbNet->sendNetLock); 1113 1114 usbNet->readReqNum = usbNet->rxQlen; 1115 ret = UsbnetHostAllocRequests(usbNet); 1116 if (ret) { 1117 HDF_LOGE("%{public}s:%{public}d UsbnetHostAllocRequests failed", __func__, __LINE__); 1118 return ret; 1119 } 1120 return ret; 1121} 1122 1123static int32_t UsbnetHostClose(struct UsbnetHost *usbNet, struct HdfSBuf *data) 1124{ 1125 HARCH_INFO_PRINT("begin"); 1126 if (NULL == usbNet || NULL == data) { 1127 HDF_LOGE("param invalid!"); 1128 return HDF_FAILURE; 1129 } 1130 1131 int ret = UsbnetHostUpdateFlags(usbNet, data); 1132 if (HDF_SUCCESS != ret) { 1133 HDF_LOGE("%{public}s: fail to Update Flags", __func__); 1134 return ret; 1135 } 1136 1137 UsbnetHostFreeRequests(usbNet); 1138 OsalMutexDestroy(&usbNet->readLock); 1139 OsalMutexDestroy(&usbNet->writeLock); 1140 return HDF_SUCCESS; 1141} 1142 1143static int32_t OnUsbnetHostEventReceived(void *priv, uint32_t id, struct HdfSBuf *data) 1144{ 1145 int32_t ret = HDF_SUCCESS; 1146 struct HdfDeviceObject *device = (struct HdfDeviceObject *)priv; 1147 struct UsbnetHost *usbNet = (struct UsbnetHost *)device->service; 1148 HARCH_INFO_PRINT("begin id = %{public}d", id); 1149 if (usbNet == NULL) { 1150 HDF_LOGE("%{public}s: invalid usbNet", __func__); 1151 return HDF_FAILURE; 1152 } 1153 switch (id) { 1154 case USB_NET_OPEN_USB: 1155 ret = UsbnetHostOpen(usbNet, data); 1156 break; 1157 case USB_NET_SEND_DATA_TO_USB: 1158 HARCH_INFO_PRINT("start send whole times = %{public}d, success Times = %{public}d", 1159 g_sendToUrbTimes, g_sendToUrbSuccessTimes); 1160 ret = UsbnetHostSnedbufToUrb(usbNet, data); 1161 break; 1162 case USB_NET_CLOSE_USB: 1163 ret = UsbnetHostClose(usbNet, data); 1164 break; 1165 case USB_NET_UPDATE_FLAGS: 1166 HARCH_INFO_PRINT(); 1167 UsbnetHostUpdateFlags(usbNet, data); 1168 break; 1169 case USB_NET_UPDATE_MAXQLEN: 1170 HARCH_INFO_PRINT(); 1171 UsbnetHostUpdateHardMtu(usbNet, data); 1172 UsbnetHostUpdateMaxQlen(usbNet); 1173 break; 1174 default: 1175 break; 1176 } 1177 return ret; 1178} 1179 1180static int32_t UsbnetHostRegisterNet(struct UsbnetHost *usbNet) 1181{ 1182 HARCH_INFO_PRINT("begin"); 1183 struct HdfIoService *serv = HdfIoServiceBind(USB_NET_SERVICE_NAME); 1184 if (serv == NULL) { 1185 HDF_LOGE("fail to get service %{public}s", USB_NET_SERVICE_NAME); 1186 return HDF_FAILURE; 1187 } 1188 1189 HARCH_INFO_PRINT("success to get service %{public}s", USB_NET_SERVICE_NAME); 1190 static struct HdfDevEventlistener listener = { 1191 .callBack = OnUsbnetHostEventReceived, 1192 }; 1193 listener.priv = (void *)(usbNet->deviceObject); 1194 1195 HARCH_INFO_PRINT("listener.priv addr = %{public}p", &(listener.priv)); 1196 if (HdfDeviceRegisterEventListener(serv, &listener) != HDF_SUCCESS) { 1197 HDF_LOGE("fail to register event listener"); 1198 return HDF_FAILURE; 1199 } 1200 1201 //send msg to net register net 1202 int32_t reply = 0; 1203 int32_t ret = UsbnetHostSendBufToNet(serv, USB_NET_REGISTER_NET, 1204 (unsigned char *)&(usbNet->net), sizeof(struct UsbnetTransInfo), &reply); 1205 if (ret != HDF_SUCCESS || reply != HDF_SUCCESS) { 1206 HDF_LOGE("%{public}s:%{public}d fail to UsbnetHostSendBufToNet ret = %{public}d, reply = %{public}d!", 1207 __func__, __LINE__, ret, reply); 1208 return HDF_FAILURE; 1209 } 1210 usbNet->hdfNetIoServ = serv; 1211 usbNet->hdfNetListener = &listener; 1212 return HDF_SUCCESS; 1213} 1214 1215int32_t UsbnetHostProbe(struct UsbnetHost *usbNet) 1216{ 1217 int32_t status = HDF_ERR_INVALID_PARAM; 1218 HARCH_INFO_PRINT("begin"); 1219 if (usbNet->deviceObject == NULL || usbNet->driverInfo == NULL) { 1220 HDF_LOGE("%{public}s: invalid param", __func__); 1221 return HDF_ERR_INVALID_PARAM; 1222 } 1223 1224 //usb init 1225 status = UsbnetHostUsbRawInit(usbNet); 1226 if (status != HDF_SUCCESS) { 1227 HDF_LOGE("%{public}s: UsbnetHostUsbRawInit failed", __func__); 1228 return HDF_FAILURE; 1229 } 1230 1231 //register net 1232 status = UsbnetHostRegisterNet(usbNet); 1233 if (status != HDF_SUCCESS) { 1234 HDF_LOGE("%{public}s: UsbnetHostRegisterNet failed", __func__); 1235 return HDF_FAILURE; 1236 } 1237 1238 HARCH_INFO_PRINT("end"); 1239 return status; 1240} 1241 1242// release buf 1243static void UsbReleaseInterfaces(struct UsbnetHost *usbNet) 1244{ 1245 int ret = HDF_SUCCESS; 1246 if ((usbNet == NULL) || (usbNet->devHandle == NULL)) { 1247 HDF_LOGE("%{public}s:%{public}d usbNet is null", __func__, __LINE__); 1248 return; 1249 } 1250 1251 HARCH_INFO_PRINT("usbNet->ctrlIface = %{public}d", usbNet->ctrlIface); 1252 HARCH_INFO_PRINT("usbNet->dataIface = %{public}d", usbNet->dataIface); 1253 if (usbNet->ctrlIface != usbNet->dataIface) { 1254 ret = UsbRawReleaseInterface(usbNet->devHandle, usbNet->ctrlIface); 1255 HARCH_INFO_PRINT("ctrlIface ret = %{public}d", ret); 1256 1257 ret = UsbRawReleaseInterface(usbNet->devHandle, usbNet->dataIface); 1258 HARCH_INFO_PRINT("dataIface ret = %{public}d", ret); 1259 } else { 1260 ret = UsbRawReleaseInterface(usbNet->devHandle, usbNet->ctrlIface); 1261 HARCH_INFO_PRINT("ctrlIface ret = %{public}d", ret); 1262 } 1263 1264 if (usbNet->statusEp) { 1265 OsalMemFree(usbNet->statusEp); 1266 usbNet->statusEp = NULL; 1267 } 1268 1269 if (usbNet->dataInEp) { 1270 OsalMemFree(usbNet->dataInEp); 1271 usbNet->dataInEp = NULL; 1272 } 1273 1274 if (usbNet->dataOutEp) { 1275 OsalMemFree(usbNet->dataOutEp); 1276 usbNet->dataOutEp = NULL; 1277 } 1278} 1279 1280static void UsbnetHostUnRegisterNet(struct UsbnetHost *usbNet) 1281{ 1282 HARCH_INFO_PRINT("begin"); 1283 //send msg to net unregister net 1284 int32_t reply = 0; 1285 int32_t ret = UsbnetHostSendBufToNet(usbNet->hdfNetIoServ, USB_NET_CLOSE_NET, 1286 (unsigned char *)&(usbNet->net), sizeof(struct UsbnetTransInfo), &reply); 1287 if (ret != HDF_SUCCESS || reply != HDF_SUCCESS) { 1288 HDF_LOGE("%{public}s:%{public}d fail to UsbnetHostSendBufToNet ret = %{public}d, reply = %{public}d!", 1289 __func__, __LINE__, ret, reply); 1290 } 1291 1292 // net unregister 1293 if (HdfDeviceUnregisterEventListener(usbNet->hdfNetIoServ, usbNet->hdfNetListener)) { 1294 HDF_LOGE("fail to unregister listener"); 1295 return; 1296 } 1297 HARCH_INFO_PRINT("HdfDeviceUnregisterEventListener"); 1298 HdfIoServiceRecycle(usbNet->hdfNetIoServ); 1299 HARCH_INFO_PRINT("HdfIoServiceRecycle"); 1300 return; 1301} 1302 1303//Sync write cmd 1304int32_t UsbnetHostWriteCmdSync(struct UsbnetHost *usbNet, struct UsbnetHostCmdParam cmdParam) 1305{ 1306 uint8_t cmd = cmdParam.cmd; 1307 uint8_t reqtype = cmdParam.reqtype; 1308 uint16_t value = cmdParam.value; 1309 uint16_t index = cmdParam.index; 1310 const void *data = cmdParam.data; 1311 uint16_t size = cmdParam.size; 1312 HARCH_INFO_PRINT("usbnet_write_cmd cmd=0x%{public}02x reqtype=%{public}02x" 1313 " value=0x%{public}04x index=0x%{public}04x size=%{public}x\n", 1314 cmd, reqtype, value, index, size); 1315 1316 struct UsbControlRequestData ctrlReq = {}; 1317 ctrlReq.requestType = reqtype; 1318 ctrlReq.requestCmd = cmd; 1319 ctrlReq.value = CPU_TO_LE16(value); 1320 ctrlReq.index = CPU_TO_LE16(index); 1321 ctrlReq.data = (unsigned char *)data; 1322 ctrlReq.length = CPU_TO_LE16(size); 1323 ctrlReq.timeout = USB_CTRL_SET_TIMEOUT; 1324 1325 HARCH_INFO_PRINT("usbfs: UsbRawControlMsg data = %{public}x\n", *(ctrlReq.data)); 1326 int32_t ret = UsbRawControlMsg(usbNet->devHandle, &ctrlReq); 1327 HARCH_INFO_PRINT("%{public}d", ret); 1328 if (ret < 0) { 1329 return HDF_FAILURE; 1330 } 1331 1332 HARCH_INFO_PRINT("usbnet_write_cmd cmd=0x%{public}02x reqtype=%{public}02x" 1333 " value=0x%{public}04x index=0x%{public}04x size=%{public}d, data = %{public}x\n", 1334 ctrlReq.requestCmd, ctrlReq.requestType, ctrlReq.value, ctrlReq.index, ctrlReq.length, *(ctrlReq.data)); 1335 return ret; 1336} 1337 1338static void UsbnetHostWriteCmdAsyncFree(struct UsbnetHost *usbNet) 1339{ 1340 if (usbNet->ctrlWriteReqAsync) { 1341 UsbRawFreeRequest(usbNet->ctrlWriteReqAsync); 1342 usbNet->ctrlWriteReqAsync = NULL; 1343 } 1344} 1345 1346static void UsbnetHostReadCmdSyncFree(struct UsbnetHost *usbNet) 1347{ 1348 if (usbNet->ctrlReadReqSync) { 1349 UsbRawFreeRequest(usbNet->ctrlReadReqSync); 1350 usbNet->ctrlReadReqSync = NULL; 1351 } 1352} 1353 1354void UsbnetHostRelease(struct UsbnetHost *usbNet) 1355{ 1356 HARCH_INFO_PRINT("begin"); 1357 int ret = HDF_SUCCESS; 1358 if (usbNet == NULL) { 1359 HDF_LOGE("%{public}s: invalid usbNet", __func__); 1360 return; 1361 } 1362 HARCH_INFO_PRINT("bus:%{public}d+dev:%{public}d", usbNet->busNum, usbNet->devAddr); 1363 //net release 1364 UsbnetHostUnRegisterNet(usbNet); 1365 1366 //usb io stop 1367 UsbStopIo(usbNet); 1368 //usb release 1369 UsbnetHostFreeRequests(usbNet); 1370 //cmd release 1371 UsbnetHostReadCmdSyncFree(usbNet); 1372 UsbnetHostWriteCmdAsyncFree(usbNet); 1373 UsbReleaseInterfaces(usbNet); 1374 if (usbNet->devHandle != NULL) { 1375 HARCH_INFO_PRINT("UsbRawCloseDevice"); 1376 ret = UsbRawCloseDevice(usbNet->devHandle); 1377 HARCH_INFO_PRINT("UsbRawCloseDevice ret = %{public}d", ret); 1378 } 1379 1380 if (usbNet->paddingPkt) { 1381 HARCH_INFO_PRINT("free paddingPkt"); 1382 OsalMemFree(usbNet->paddingPkt); 1383 usbNet->paddingPkt = NULL; 1384 } 1385 1386 if (usbNet->config != NULL) { 1387 HARCH_INFO_PRINT("free Config"); 1388 UsbRawFreeConfigDescriptor(usbNet->config); 1389 usbNet->config = NULL; 1390 } 1391 1392 if (usbNet->session != NULL) { 1393 HARCH_INFO_PRINT("exit session"); 1394 ret = UsbRawExit(usbNet->session); 1395 HARCH_INFO_PRINT("session exit ret = %{public}d", ret); 1396 } 1397 1398 OsalMutexDestroy(&usbNet->readLock); 1399 OsalMutexDestroy(&usbNet->writeLock); 1400 OsalMutexDestroy(&usbNet->sendNetLock); 1401 1402 usbNet->initFlag = false; 1403 usbNet->allocFlag = false; 1404 return; 1405} 1406