• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "usbfn_dev_mgr.h"
17 #include "hdf_base.h"
18 #include "hdf_log.h"
19 #include "osal_thread.h"
20 #include "osal_time.h"
21 #include "securec.h"
22 #include "usbfn_cfg_mgr.h"
23 #include "usbfn_io_mgr.h"
24 
25 #define HDF_LOG_TAG    usbfn_dev_mgr
26 #define SLEEP_TIME_OUT 100
27 #define SLEEP_TIMES    20
28 
29 static struct DListHead g_devEntry = {0};
30 static uint32_t g_intfCnt = 0;
31 static uint32_t g_epCnt = 1;
32 static uint32_t g_fnCntOld = 0;
GetInterfaceInfo(const struct UsbInterfaceDescriptor * intf,struct UsbFnDeviceMgr * devMgr,uint32_t fnCnt,const struct UsbFnConfiguration * config)33 static void GetInterfaceInfo(const struct UsbInterfaceDescriptor *intf, struct UsbFnDeviceMgr *devMgr, uint32_t fnCnt,
34     const struct UsbFnConfiguration *config)
35 {
36     if (g_fnCntOld != fnCnt) {
37         g_fnCntOld = fnCnt;
38         g_epCnt = 1;
39     }
40     struct UsbFnInterfaceInfo *info = NULL;
41     if (intf->bDescriptorType == USB_DDK_DT_INTERFACE && intf->bNumEndpoints > 0) {
42         if (g_intfCnt >= devMgr->fnDev.numInterfaces) {
43             HDF_LOGE("%{public}s: GetInterfaceInfo failed", __func__);
44             return;
45         }
46         info = &devMgr->interfaceMgr[g_intfCnt].interface.info;
47         info->index = intf->bInterfaceNumber;
48         info->interfaceClass = intf->bInterfaceClass;
49         info->subclass = intf->bInterfaceSubClass;
50         info->protocol = intf->bInterfaceProtocol;
51         info->numPipes = intf->bNumEndpoints;
52         info->configIndex = config->configurationValue;
53 
54         devMgr->interfaceMgr[g_intfCnt].interface.object = &devMgr->fnDev.object;
55         devMgr->interfaceMgr[g_intfCnt].funcMgr = &devMgr->funcMgr[fnCnt];
56         devMgr->interfaceMgr[g_intfCnt].startEpId = (uint8_t)g_epCnt;
57         g_epCnt += intf->bNumEndpoints;
58         g_intfCnt++;
59     }
60 }
61 
CreateInterface(struct UsbFnDeviceDesc * des,struct UsbFnDeviceMgr * devMgr)62 static void CreateInterface(struct UsbFnDeviceDesc *des, struct UsbFnDeviceMgr *devMgr)
63 {
64     uint32_t fnCnt = 0;
65     struct UsbInterfaceDescriptor *intf = NULL;
66     g_intfCnt = 0;
67     g_epCnt = 1;
68     g_fnCntOld = 0;
69 
70     for (uint32_t i = 0; des->configs[i] != NULL; i++) {
71         for (uint32_t j = 0; des->configs[i]->functions[j] != NULL; j++) {
72             if (strncmp(des->configs[i]->functions[j]->funcName, FUNCTION_GENERIC, strlen(FUNCTION_GENERIC))) {
73                 continue;
74             }
75             if (des->configs[i]->functions[j]->enable == false) {
76                 continue;
77             }
78             DListHeadInit(&devMgr->funcMgr[fnCnt].reqEntry);
79             devMgr->funcMgr[fnCnt].object = &devMgr->fnDev.object;
80             int32_t ret = snprintf_s(devMgr->funcMgr[fnCnt].name, MAX_NAMELEN, MAX_NAMELEN - 1, "%s",
81                 des->configs[i]->functions[j]->funcName);
82             if (ret < 0) {
83                 HDF_LOGE("%{public}s: snprintf_s failed", __func__);
84                 return;
85             }
86 
87             for (uint32_t k = 0; des->configs[i]->functions[j]->fsDescriptors[k] != NULL; k++) {
88                 intf = (struct UsbInterfaceDescriptor *)des->configs[i]->functions[j]->fsDescriptors[k];
89                 GetInterfaceInfo(intf, devMgr, fnCnt, des->configs[i]);
90             }
91             fnCnt++;
92         }
93     }
94 }
95 
96 #define MAX_LIST 32
FindEmptyId(void)97 static int32_t FindEmptyId(void)
98 {
99     int32_t devCnt = 1;
100     struct UsbObject *obj = NULL;
101     struct UsbObject *temp = NULL;
102     if (g_devEntry.next != 0 && !DListIsEmpty(&g_devEntry)) {
103         int32_t i;
104         for (i = 1; i < MAX_LIST; i++) {
105             int32_t isUse = 0;
106             DLIST_FOR_EACH_ENTRY_SAFE(obj, temp, &g_devEntry, struct UsbObject, entry) {
107                 if (obj->objectId == i) {
108                     isUse = 1;
109                     break;
110                 }
111             }
112             if (isUse == 0) {
113                 break;
114             }
115         }
116         if (i == MAX_LIST) {
117             HDF_LOGE("%{public}s:%d too much device created", __func__, __LINE__);
118             return -1;
119         }
120         devCnt = i;
121     }
122     return devCnt;
123 }
124 
CreatDev(const char * udcName,struct UsbFnDeviceDesc * des,struct UsbFnDeviceMgr * fnDevMgr)125 static int32_t CreatDev(const char *udcName, struct UsbFnDeviceDesc *des, struct UsbFnDeviceMgr *fnDevMgr)
126 {
127     int32_t ret, devCnt;
128     struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
129     devCnt = FindEmptyId();
130     if (devCnt < 0) {
131         return HDF_ERR_IO;
132     }
133 
134     fnDevMgr->fnDev.object.objectId = devCnt;
135     ret = sprintf_s(fnDevMgr->name, MAX_NAMELEN, "g%d", devCnt);
136     if (ret < 0) {
137         return HDF_ERR_IO;
138     }
139     ret = strcpy_s(fnDevMgr->udcName, MAX_NAMELEN, udcName);
140     if (ret != EOK) {
141         return HDF_ERR_IO;
142     }
143     ret = fnOps->createDevice(udcName, fnDevMgr->name, des);
144     if (ret) {
145         return HDF_ERR_IO;
146     }
147     return 0;
148 }
149 
GetInterfaceNum(struct UsbDescriptorHeader ** intf)150 static uint32_t GetInterfaceNum(struct UsbDescriptorHeader **intf)
151 {
152     uint32_t i;
153     uint32_t num = 0;
154     struct UsbInterfaceDescriptor *interface = NULL;
155     for (i = 0; intf[i] != NULL; i++) {
156         if (intf[i]->bDescriptorType == USB_DDK_DT_INTERFACE) {
157             interface = (struct UsbInterfaceDescriptor *)intf[i];
158             if (interface->bNumEndpoints > 0) {
159                 num++;
160             }
161         }
162     }
163     return num;
164 }
165 
AllocInterfaceAndFuncMgr(struct UsbFnDeviceMgr * fnDevMgr,struct UsbFnDeviceDesc * des)166 static int32_t AllocInterfaceAndFuncMgr(struct UsbFnDeviceMgr *fnDevMgr, struct UsbFnDeviceDesc *des)
167 {
168     uint32_t i, j;
169     for (i = 0; des->configs[i] != NULL; i++) {
170         for (j = 0; des->configs[i]->functions[j] != NULL; j++) {
171             if (strncmp(des->configs[i]->functions[j]->funcName, FUNCTION_GENERIC, strlen(FUNCTION_GENERIC))) {
172                 continue;
173             }
174             if (des->configs[i]->functions[j]->enable == false) {
175                 continue;
176             }
177             fnDevMgr->numFunc++;
178             fnDevMgr->fnDev.numInterfaces += GetInterfaceNum(des->configs[i]->functions[j]->fsDescriptors);
179         }
180     }
181     if (fnDevMgr->fnDev.numInterfaces == 0) {
182         HDF_LOGE("%s functions is null", __func__);
183         return HDF_DEV_ERR_NO_DEVICE;
184     }
185 
186     fnDevMgr->interfaceMgr = UsbFnMemCalloc(fnDevMgr->fnDev.numInterfaces * sizeof(struct UsbFnInterfaceMgr));
187     if (fnDevMgr->interfaceMgr == NULL) {
188         HDF_LOGE("%s UsbFnMemCalloc failed", __func__);
189         return HDF_ERR_IO;
190     }
191 
192     fnDevMgr->funcMgr = UsbFnMemCalloc(fnDevMgr->numFunc * sizeof(struct UsbFnFuncMgr));
193     if (fnDevMgr->funcMgr == NULL) {
194         HDF_LOGE("%s UsbFnMemCalloc failed", __func__);
195         UsbFnMemFree(fnDevMgr->interfaceMgr);
196         return HDF_ERR_IO;
197     }
198     return 0;
199 }
200 
201 static int32_t StartThreadIo(struct UsbFnDeviceMgr *fnDevMgr);
UsbFnMgrDeviceCreate(const char * udcName,struct UsbFnDeviceDesc * des,const struct DeviceResourceNode * node)202 const struct UsbFnDeviceMgr *UsbFnMgrDeviceCreate(
203     const char *udcName, struct UsbFnDeviceDesc *des, const struct DeviceResourceNode *node)
204 {
205     int32_t ret;
206 
207     struct UsbFnDeviceMgr *fnDevMgr = NULL;
208     if (udcName == NULL || des == NULL) {
209         HDF_LOGE("%{public}s invalid param.", __func__);
210         return NULL;
211     }
212 
213     fnDevMgr = UsbFnMemCalloc(sizeof(struct UsbFnDeviceMgr));
214     if (fnDevMgr == NULL) {
215         HDF_LOGE("%{public}s UsbFnMemCalloc failed", __func__);
216         return NULL;
217     }
218 
219     ret = CreatDev(udcName, des, fnDevMgr);
220     if (ret) {
221         HDF_LOGE("%{public}s CreatDev failed", __func__);
222         goto FREE_DEVMGR;
223     }
224     if (g_devEntry.next == 0) {
225         DListHeadInit(&g_devEntry);
226     }
227     DListInsertTail(&fnDevMgr->fnDev.object.entry, &g_devEntry);
228     fnDevMgr->node = node;
229     fnDevMgr->des = des;
230 
231     ret = AllocInterfaceAndFuncMgr(fnDevMgr, des);
232     if (ret == HDF_DEV_ERR_NO_DEVICE) {
233         return fnDevMgr;
234     } else if (ret != 0) {
235         HDF_LOGE("%{public}s AllocInterfaceAndFuncMgr failed", __func__);
236         goto FREE_DEVMGR;
237     }
238 
239     CreateInterface(des, fnDevMgr);
240     fnDevMgr->running = true;
241     ret = StartThreadIo(fnDevMgr);
242     if (ret) {
243         HDF_LOGE("%{public}s: StartThreadIo failed", __func__);
244         goto FREE_INTF_FUNC_MGR;
245     }
246     return fnDevMgr;
247 
248 FREE_INTF_FUNC_MGR:
249     UsbFnMemFree(fnDevMgr->interfaceMgr);
250     UsbFnMemFree(fnDevMgr->funcMgr);
251 FREE_DEVMGR:
252     UsbFnMemFree(fnDevMgr);
253     return NULL;
254 }
255 
UsbFnMgrDeviceRemove(struct UsbFnDevice * fnDevice)256 int32_t UsbFnMgrDeviceRemove(struct UsbFnDevice *fnDevice)
257 {
258     int32_t ret;
259     int32_t i = 0;
260     if (fnDevice == NULL) {
261         return HDF_ERR_INVALID_PARAM;
262     }
263     struct UsbFnDeviceMgr *fnDevMgr = (struct UsbFnDeviceMgr *)fnDevice;
264     struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
265 
266     fnDevMgr->running = false;
267     while (!fnDevMgr->running) {
268         i++;
269         OsalMSleep(SLEEP_TIME_OUT);
270         if (i > SLEEP_TIMES) {
271             HDF_LOGE("%{public}s: wait thread exit timeout", __func__);
272             break;
273         }
274     }
275 
276     ret = OsalThreadDestroy(&fnDevMgr->thread);
277     if (ret != HDF_SUCCESS) {
278         HDF_LOGE("%{public}s:%d OsalThreadDestroy failed, ret = %d", __func__, __LINE__, ret);
279         return ret;
280     }
281     ret = fnOps->delDevice(fnDevMgr->name, fnDevMgr->udcName, fnDevMgr->des);
282     if (ret) {
283         HDF_LOGE("%{public}s:%d UsbFnMgrDeviceRemove failed", __func__, __LINE__);
284         return ret;
285     }
286     DListRemove(&fnDevice->object.entry);
287     UsbFnCfgMgrUnRegisterAllProp();
288 
289     if (fnDevMgr->funcMgr != NULL) {
290         UsbFnMemFree(fnDevMgr->funcMgr);
291         fnDevMgr->funcMgr = NULL;
292     }
293     if (fnDevMgr->interfaceMgr != NULL) {
294         UsbFnMemFree(fnDevMgr->interfaceMgr);
295         fnDevMgr->interfaceMgr = NULL;
296     }
297     if (fnDevMgr->node) {
298         UsbFnCfgMgrFreeUsbFnDeviceDesc(fnDevMgr->des);
299         fnDevMgr->des = NULL;
300     }
301     UsbFnMemFree(fnDevMgr);
302     fnDevMgr = NULL;
303     return 0;
304 }
305 
UsbFnMgrDeviceGet(const char * udcName)306 const struct UsbFnDeviceMgr *UsbFnMgrDeviceGet(const char *udcName)
307 {
308     if (udcName == NULL) {
309         HDF_LOGE("%{public}s invalid param.", __func__);
310         return NULL;
311     }
312     if (g_devEntry.next == 0 || DListIsEmpty(&g_devEntry)) {
313         HDF_LOGE("%{public}s no device created", __func__);
314         return NULL;
315     }
316 
317     struct UsbObject *obj = NULL;
318     struct UsbObject *temp = NULL;
319     DLIST_FOR_EACH_ENTRY_SAFE(obj, temp, &g_devEntry, struct UsbObject, entry) {
320         struct UsbFnDeviceMgr *fnDevMgr = (struct UsbFnDeviceMgr *)obj;
321         if (strcmp(udcName, fnDevMgr->udcName) == 0) {
322             return fnDevMgr;
323         }
324     }
325     return NULL;
326 }
327 
UsbFnMgrDeviceGetState(struct UsbFnDevice * fnDevice,UsbFnDeviceState * devState)328 int32_t UsbFnMgrDeviceGetState(struct UsbFnDevice *fnDevice, UsbFnDeviceState *devState)
329 {
330     if (fnDevice == NULL || devState == NULL) {
331         HDF_LOGE("%{public}s:%d invalid param.", __func__, __LINE__);
332         return HDF_ERR_INVALID_PARAM;
333     }
334     struct UsbFnDeviceMgr *fnDevMgr = (struct UsbFnDeviceMgr *)fnDevice;
335     *devState = fnDevMgr->devState;
336     return 0;
337 }
338 
UsbFnMgrDeviceGetInterface(struct UsbFnDevice * fnDevice,uint8_t interfaceIndex)339 const struct UsbFnInterfaceMgr *UsbFnMgrDeviceGetInterface(struct UsbFnDevice *fnDevice, uint8_t interfaceIndex)
340 {
341     if (fnDevice == NULL) {
342         HDF_LOGE("%{public}s:%d fnDevice is null", __func__, __LINE__);
343         return NULL;
344     }
345     struct UsbFnDeviceMgr *fnDevMgr = (struct UsbFnDeviceMgr *)fnDevice;
346     if (interfaceIndex >= fnDevMgr->fnDev.numInterfaces) {
347         HDF_LOGE("%{public}s:%d invalid param.", __func__, __LINE__);
348         return NULL;
349     }
350     return &(fnDevMgr->interfaceMgr[interfaceIndex]);
351 }
352 
CollectEventHandle(struct UsbFnEventAll * event,struct UsbFnDeviceMgr * devMgr)353 static void CollectEventHandle(struct UsbFnEventAll *event, struct UsbFnDeviceMgr *devMgr)
354 {
355     uint8_t i, j;
356     struct UsbFnFuncMgr *funcMgr = NULL;
357     struct UsbFnInterfaceMgr *intfMgr = NULL;
358     struct UsbHandleMgr *handle = NULL;
359     event->ep0Num = 0;
360     event->epNum = 0;
361     for (i = 0; i < devMgr->numFunc; i++) {
362         funcMgr = devMgr->funcMgr + i;
363 
364         if (funcMgr->fd > 0 && funcMgr->callback != NULL) {
365             event->ep0[event->ep0Num] = funcMgr->fd;
366             event->ep0Event[event->ep0Num].type = USB_EP0_INVALID;
367             event->ep0Num++;
368         }
369     }
370     for (i = 0; i < devMgr->fnDev.numInterfaces; i++) {
371         intfMgr = devMgr->interfaceMgr + i;
372         if (intfMgr == NULL || intfMgr->isOpen == false || intfMgr->handle == NULL) {
373             continue;
374         }
375         handle = intfMgr->handle;
376         for (j = 0; j < handle->numFd; j++) {
377             if (handle->fds[j] <= 0) {
378                 continue;
379             }
380             event->epx[event->epNum] = handle->fds[j];
381             event->reqEvent[event->epNum] = handle->reqEvent[j];
382             event->numEvent[event->epNum] = 0;
383             event->epNum++;
384         }
385     }
386 }
387 
HandleEp0IoEvent(const struct UsbFnFuncMgr * funcMgr,const struct UsbFnReqEvent * reqEvent)388 static void HandleEp0IoEvent(const struct UsbFnFuncMgr *funcMgr, const struct UsbFnReqEvent *reqEvent)
389 {
390     struct ReqList *reqList = NULL;
391     struct ReqList *temp = NULL;
392 
393     DLIST_FOR_EACH_ENTRY_SAFE(reqList, temp, &funcMgr->reqEntry, struct ReqList, entry) {
394         if (reqList->buf == reqEvent->buf) {
395             HDF_LOGD("%{public}s: req.actual = %d", __func__, reqList->req.actual);
396             reqList->req.actual = reqEvent->actual;
397             reqList->req.status = -reqEvent->status;
398             if (reqList->req.complete) {
399                 reqList->req.complete(reqList->pipe, &reqList->req);
400             } else {
401                 HDF_LOGE("no complete callback find");
402             }
403             break;
404         }
405     }
406 }
407 
HandleEp0CtrlEvent(const struct UsbFnFuncMgr * funcMgr,struct UsbFnCtrlEvent * ctrlEvent)408 static void HandleEp0CtrlEvent(const struct UsbFnFuncMgr *funcMgr, struct UsbFnCtrlEvent *ctrlEvent)
409 {
410     struct UsbFnDeviceMgr *devMgr = (struct UsbFnDeviceMgr *)funcMgr->object;
411     struct UsbFnEvent fnEvnet;
412     if (((funcMgr->eventMask) & (1 << ctrlEvent->type)) == 0) {
413         return;
414     }
415     fnEvnet.setup = &ctrlEvent->u.setup;
416     fnEvnet.type = ctrlEvent->type;
417     fnEvnet.context = funcMgr->context;
418     devMgr->devState = ctrlEvent->type;
419     if (funcMgr->callback) {
420         funcMgr->callback(&fnEvnet);
421     } else {
422         HDF_LOGE("%{public}s: no callback find event=%d", __func__, fnEvnet.type);
423     }
424 }
425 
HandleEpsIoEvent(const struct UsbFnReqEvent * reqEvent,const struct UsbHandleMgr * handle)426 static void HandleEpsIoEvent(const struct UsbFnReqEvent *reqEvent, const struct UsbHandleMgr *handle)
427 {
428     struct ReqList *reqList = NULL;
429     struct ReqList *temp = NULL;
430     DLIST_FOR_EACH_ENTRY_SAFE(reqList, temp, &handle->reqEntry, struct ReqList, entry) {
431         if (reqList->buf == reqEvent->buf) {
432             reqList->req.actual = reqEvent->actual;
433             reqList->req.status = -reqEvent->status;
434             if (reqList->req.complete) {
435                 reqList->req.complete(reqList->pipe, &reqList->req);
436             } else {
437                 HDF_LOGE("no complete callback find");
438             }
439             break;
440         }
441     }
442 }
443 
GetFuncMgr(const struct UsbFnDeviceMgr * devMgr,int32_t ep0)444 static struct UsbFnFuncMgr *GetFuncMgr(const struct UsbFnDeviceMgr *devMgr, int32_t ep0)
445 {
446     uint8_t i;
447 
448     if (devMgr == NULL) {
449         HDF_LOGE("%{public}s:%{public}d devMgr is null.", __func__, __LINE__);
450         return NULL;
451     }
452     struct UsbFnFuncMgr *funcMgr = NULL;
453     for (i = 0; i < devMgr->numFunc; i++) {
454         funcMgr = devMgr->funcMgr + i;
455         if (funcMgr->fd == ep0) {
456             break;
457         }
458     }
459     return funcMgr;
460 }
461 
GetHandleMgr(const struct UsbFnDeviceMgr * devMgr,int32_t epx)462 static struct UsbHandleMgr *GetHandleMgr(const struct UsbFnDeviceMgr *devMgr, int32_t epx)
463 {
464     uint8_t i, j;
465     struct UsbHandleMgr *handle = NULL;
466     struct UsbFnInterfaceMgr *intfMgr = NULL;
467 
468     if (devMgr == NULL) {
469         HDF_LOGE("%{public}s:%{public}d devMgr is null.", __func__, __LINE__);
470         return NULL;
471     }
472     for (i = 0; i < devMgr->fnDev.numInterfaces; i++) {
473         intfMgr = devMgr->interfaceMgr + i;
474         if (!intfMgr->isOpen) {
475             continue;
476         }
477         handle = intfMgr->handle;
478         for (j = 0; j < handle->numFd; j++) {
479             if (epx == handle->fds[j]) {
480                 return handle;
481             }
482         }
483     }
484     return handle;
485 }
486 
HandleEp0Event(const struct UsbFnDeviceMgr * devMgr,struct UsbFnEventAll event)487 static void HandleEp0Event(const struct UsbFnDeviceMgr *devMgr, struct UsbFnEventAll event)
488 {
489     uint8_t i;
490     struct UsbFnFuncMgr *funcMgr = NULL;
491     for (i = 0; i < event.ep0Num; i++) {
492         funcMgr = GetFuncMgr(devMgr, event.ep0[i]);
493         if (funcMgr == NULL) {
494             HDF_LOGE("%{public}s:%{public}d GetFuncMgr failed.", __func__, __LINE__);
495             return;
496         }
497         if (event.ep0Event[i].type == USB_EP0_CTRL_EVENT) {
498             HandleEp0CtrlEvent(funcMgr, &event.ep0Event[i].ctrlEvent);
499         } else if (event.ep0Event[i].type == USB_EP0_IO_COMPLETED) {
500             HandleEp0IoEvent(funcMgr, &event.ep0Event[i].reqEvent);
501         }
502     }
503 }
504 
UsbFnEventProcess(void * arg)505 static int32_t UsbFnEventProcess(void *arg)
506 {
507     struct UsbFnDeviceMgr *devMgr = (struct UsbFnDeviceMgr *)arg;
508     struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
509     struct UsbFnEventAll event;
510     struct UsbHandleMgr *handle = NULL;
511     int32_t timeout = SLEEP_TIME_OUT;
512 
513     while (true) {
514         if (devMgr == NULL || !devMgr->running) {
515             break;
516         }
517         if (memset_s(&event, sizeof(event), 0, sizeof(event)) != EOK) {
518             HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
519             return HDF_FAILURE;
520         }
521 
522         CollectEventHandle(&event, devMgr);
523         if (event.ep0Num + event.epNum == 0) {
524             continue;
525         }
526         int32_t ret = fnOps->pollEvent(&event, timeout);
527         if (ret != 0) {
528             if (devMgr == NULL || !devMgr->running) {
529                 break;
530             }
531             OsalMSleep(1);
532             continue;
533         }
534         HandleEp0Event(devMgr, event);
535         for (uint8_t i = 0; i < event.epNum; i++) {
536             handle = GetHandleMgr(devMgr, event.epx[i]);
537             if (handle == NULL) {
538                 continue;
539             }
540             for (uint8_t j = 0; j < event.numEvent[i]; j++) {
541                 HandleEpsIoEvent(&event.reqEvent[i][j], handle);
542             }
543         }
544     }
545     if (devMgr) {
546         devMgr->running = true;
547     }
548     HDF_LOGI("%{public}s, exit\n", __func__);
549     return 0;
550 }
551 
StartThreadIo(struct UsbFnDeviceMgr * fnDevMgr)552 static int32_t StartThreadIo(struct UsbFnDeviceMgr *fnDevMgr)
553 {
554     int32_t ret;
555     struct OsalThreadParam threadCfg;
556     ret = memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg));
557     if (ret != EOK) {
558         HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
559         return ret;
560     }
561     threadCfg.name = "usbfn process";
562     threadCfg.priority = OSAL_THREAD_PRI_LOW;
563     threadCfg.stackSize = HDF_PROCESS_STACK_SIZE;
564 
565     ret = OsalThreadCreate(&fnDevMgr->thread, (OsalThreadEntry)UsbFnEventProcess, (void *)fnDevMgr);
566     if (ret != HDF_SUCCESS) {
567         HDF_LOGE("%{public}s:%d OsalThreadCreate failed, ret = %d ", __func__, __LINE__, ret);
568         return HDF_ERR_DEVICE_BUSY;
569     }
570     HDF_LOGD("%{public}s: Usb device OsalThreadCreate", __func__);
571     ret = OsalThreadStart(&fnDevMgr->thread, &threadCfg);
572     if (ret != HDF_SUCCESS) {
573         HDF_LOGE("%{public}s:%d OsalThreadStart failed, ret = %d ", __func__, __LINE__, ret);
574         return HDF_ERR_DEVICE_BUSY;
575     }
576     return 0;
577 }
578 
UsbFnMgrStartRecvEvent(struct UsbFnInterface * interface,uint32_t eventMask,UsbFnEventCallback callback,void * context)579 int32_t UsbFnMgrStartRecvEvent(
580     struct UsbFnInterface *interface, uint32_t eventMask, UsbFnEventCallback callback, void *context)
581 {
582     struct UsbFnInterfaceMgr *interfaceMgr = (struct UsbFnInterfaceMgr *)interface;
583     struct UsbFnFuncMgr *funcMgr = interfaceMgr->funcMgr;
584     if (funcMgr->callback != NULL) {
585         HDF_LOGE("%{public}s: callback has Register", __func__);
586         return HDF_ERR_INVALID_PARAM;
587     }
588     funcMgr->context = context;
589     funcMgr->eventMask = eventMask;
590     funcMgr->callback = callback;
591     if (funcMgr->fd <= 0) {
592         int32_t ret = OpenEp0AndMapAddr(funcMgr);
593         if (ret != HDF_SUCCESS) {
594             HDF_LOGE("%{public}s: OpenEp0AndMapAddr failed, ret = %d ", __func__, ret);
595             return HDF_ERR_IO;
596         }
597     }
598     return 0;
599 }
600 
UsbFnStopRecvEvent(struct UsbFnInterface * interface)601 int32_t UsbFnStopRecvEvent(struct UsbFnInterface *interface)
602 {
603     int32_t ret;
604     struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
605     struct UsbFnInterfaceMgr *interfaceMgr = (struct UsbFnInterfaceMgr *)interface;
606     struct UsbFnFuncMgr *funcMgr = interfaceMgr->funcMgr;
607     ret = fnOps->queueDel(funcMgr->fd);
608     if (ret) {
609         HDF_LOGE("%{public}s:%d queueDel failed, ret = %d ", __func__, __LINE__, ret);
610         return HDF_ERR_DEVICE_BUSY;
611     }
612     ret = fnOps->closePipe(funcMgr->fd);
613     if (ret) {
614         HDF_LOGE("%{public}s:%d closePipe failed, ret = %d ", __func__, __LINE__, ret);
615         return HDF_ERR_DEVICE_BUSY;
616     }
617     funcMgr->fd = -1;
618     funcMgr->callback = NULL;
619     return 0;
620 }
621