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