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