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