• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "usb_interface_pool.h"
17 #include "linux_adapter.h"
18 #include "usb_io_manage.h"
19 #include "usb_protocol.h"
20 
21 #define HDF_LOG_TAG USB_INTERFACE_POOL
22 
23 static int32_t g_usbRequestObjectId = 0;
IfFreePipeObj(struct UsbPipe * pipeObj)24 static HDF_STATUS IfFreePipeObj(struct UsbPipe *pipeObj)
25 {
26     HDF_STATUS ret = HDF_SUCCESS;
27 
28     if (pipeObj == NULL) {
29         HDF_LOGE("%{public}s:%{public}d pipeObj is null", __func__, __LINE__);
30         return HDF_ERR_INVALID_PARAM;
31     }
32 
33     pipeObj->object.objectId = 0;
34 
35     RawUsbMemFree(pipeObj);
36 
37     return ret;
38 }
39 
IfDestroyPipeObj(const struct UsbSdkInterface * interfaceObj,const struct UsbPipe * pipeObj)40 static HDF_STATUS IfDestroyPipeObj(const struct UsbSdkInterface *interfaceObj, const struct UsbPipe *pipeObj)
41 {
42     HDF_STATUS ret = HDF_SUCCESS;
43     struct UsbPipe *pipePos = NULL;
44     struct UsbPipe *pipeTemp = NULL;
45     bool found = false;
46     bool destroyFlag = false;
47 
48     if (interfaceObj == NULL) {
49         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
50         return HDF_FAILURE;
51     }
52 
53     if (DListIsEmpty((struct DListHead *)&interfaceObj->pipeList)) {
54         HDF_LOGE("%{public}s:%{public}d pipeList is empty", __func__, __LINE__);
55         return HDF_SUCCESS;
56     }
57 
58     if (pipeObj == NULL) {
59         /* Destroys all pipe object */
60         destroyFlag = true;
61     } else {
62         /* Destroys the specified pipe object */
63         destroyFlag = false;
64     }
65 
66     OsalMutexLock((struct OsalMutex *)&interfaceObj->listLock);
67     DLIST_FOR_EACH_ENTRY_SAFE(pipePos, pipeTemp, &interfaceObj->pipeList, struct UsbPipe, object.entry) {
68         if (destroyFlag || pipePos->object.objectId == pipeObj->object.objectId) {
69             found = true;
70             DListRemove(&pipePos->object.entry);
71             ret = IfFreePipeObj(pipePos);
72             if (ret != HDF_SUCCESS) {
73                 HDF_LOGE("%{public}s:%{public}d IfFreePipeObj failed, ret = %d ", __func__, __LINE__, ret);
74                 break;
75             }
76 
77             if (!destroyFlag) {
78                 break;
79             }
80         }
81     }
82     OsalMutexUnlock((struct OsalMutex *)&interfaceObj->listLock);
83 
84     if (!found) {
85         ret = HDF_FAILURE;
86         HDF_LOGE(
87             "%{public}s:%{public}d the pipe object to be destroyed does not exist, ret = %d", __func__, __LINE__, ret);
88     }
89 
90     return ret;
91 }
92 
IfInterfaceObjInit(struct UsbSdkInterface * interfaceObj)93 static void IfInterfaceObjInit(struct UsbSdkInterface *interfaceObj)
94 {
95     DListHeadInit(&interfaceObj->pipeList);
96     OsalMutexInit(&interfaceObj->listLock);
97     OsalAtomicSet(&interfaceObj->refCount, 0);
98     interfaceObj->status = USB_INTERFACE_STATUS_NORMAL;
99 }
100 
IfFreeInterfaceObj(struct UsbSdkInterface * interfaceObj)101 static HDF_STATUS IfFreeInterfaceObj(struct UsbSdkInterface *interfaceObj)
102 {
103     HDF_STATUS ret;
104 
105     if (interfaceObj == NULL) {
106         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
107         return HDF_ERR_INVALID_PARAM;
108     }
109 
110     interfaceObj->interface.object.objectId = 0;
111     interfaceObj->parentObjectId = 0;
112     ret = IfDestroyPipeObj(interfaceObj, NULL);
113     if (ret != HDF_SUCCESS) {
114         HDF_LOGE("%{public}s:%{public}d IfDestroyPipeObj failed", __func__, __LINE__);
115         return ret;
116     }
117     OsalMutexDestroy(&interfaceObj->listLock);
118     interfaceObj->status = USB_INTERFACE_STATUS_NORMAL;
119 
120     RawUsbMemFree(interfaceObj);
121 
122     return ret;
123 }
124 
IfInterfacePoolInit(struct UsbInterfacePool * interfacePool,uint8_t busNum,uint8_t devAddr)125 static HDF_STATUS IfInterfacePoolInit(struct UsbInterfacePool *interfacePool, uint8_t busNum, uint8_t devAddr)
126 {
127     interfacePool->session = NULL;
128     OsalMutexInit(&interfacePool->mutex);
129     DListHeadInit(&interfacePool->interfaceList);
130     OsalMutexInit(&interfacePool->interfaceLock);
131     DListHeadInit(&interfacePool->object.entry);
132     OsalAtomicSet(&interfacePool->refCount, 0);
133     interfacePool->busNum = busNum;
134     interfacePool->devAddr = devAddr;
135     OsalAtomicSet(&interfacePool->ioRefCount, 0);
136     OsalMutexInit(&interfacePool->ioStopLock);
137     OsalMutexLock(&interfacePool->ioStopLock);
138     interfacePool->ioProcessStopStatus = USB_POOL_PROCESS_RUNNING;
139     OsalMutexUnlock(&interfacePool->ioStopLock);
140     interfacePool->device = NULL;
141 
142     /* create submit queue and wait complete queue */
143     return UsbIoCreateQueue(interfacePool);
144 }
145 
IfFreeInterfacePool(struct UsbInterfacePool * interfacePool)146 static HDF_STATUS IfFreeInterfacePool(struct UsbInterfacePool *interfacePool)
147 {
148     HDF_STATUS ret;
149 
150     if (interfacePool == NULL) {
151         HDF_LOGE("%{public}s:%{public}d invalid param interfacePool", __func__, __LINE__);
152         return HDF_ERR_INVALID_PARAM;
153     }
154 
155     ret = UsbIoDestroyQueue(interfacePool);
156     if (ret != HDF_SUCCESS) {
157         HDF_LOGE("%{public}s:%{public}d UsbIoDestroyQueue failed", __func__, __LINE__);
158         return ret;
159     }
160 
161     interfacePool->object.objectId = 0;
162     OsalMutexDestroy(&interfacePool->mutex);
163     ret = UsbIfDestroyInterfaceObj(interfacePool, NULL);
164     if (ret != HDF_SUCCESS) {
165         HDF_LOGE("%{public}s:%{public}d UsbIfDestroyInterfaceObj failed", __func__, __LINE__);
166         return ret;
167     }
168     OsalMutexDestroy(&interfacePool->interfaceLock);
169     interfacePool->busNum = 0;
170     interfacePool->devAddr = 0;
171 
172     RawUsbMemFree(interfacePool);
173 
174     return ret;
175 }
176 
IfDestroyInterfacePool(const struct UsbInterfacePool * interfacePool)177 static HDF_STATUS IfDestroyInterfacePool(const struct UsbInterfacePool *interfacePool)
178 {
179     HDF_STATUS ret = HDF_SUCCESS;
180     struct UsbInterfacePool *interfacePoolPos = NULL;
181     struct UsbInterfacePool *interfacePoolTemp = NULL;
182     struct UsbSession *session = NULL;
183     bool found = false;
184 
185     if (interfacePool == NULL || interfacePool->session == NULL) {
186         HDF_LOGE("%{public}s:%{public}d the interfacePool is null", __func__, __LINE__);
187         return HDF_ERR_INVALID_PARAM;
188     }
189 
190     session = interfacePool->session;
191     if (DListIsEmpty(&session->ifacePoolList)) {
192         HDF_LOGE("%{public}s:%{public}d interface pool list is empty", __func__, __LINE__);
193         return HDF_SUCCESS;
194     }
195 
196     DLIST_FOR_EACH_ENTRY_SAFE(
197         interfacePoolPos, interfacePoolTemp, &session->ifacePoolList, struct UsbInterfacePool, object.entry) {
198         if (interfacePoolPos->object.objectId == interfacePool->object.objectId) {
199             found = true;
200             DListRemove(&interfacePoolPos->object.entry);
201             ret = IfFreeInterfacePool(interfacePoolPos);
202             if (ret != HDF_SUCCESS) {
203                 HDF_LOGE("%{public}s:%{public}d IfFreeInterfacePool failed, ret = %d", __func__, __LINE__, ret);
204                 break;
205             }
206             interfacePoolPos = NULL;
207             break;
208         }
209     }
210 
211     if (!found) {
212         ret = HDF_FAILURE;
213         HDF_LOGE("%{public}s:%{public}d the interfacePool object to be destroyed does not exist", __func__, __LINE__);
214     }
215 
216     return ret;
217 }
218 
IfInterfaceRefCount(const struct UsbSdkInterface * interfaceObj,uint8_t interfaceIndex,bool refCountFlag,bool * claimFlag)219 static void IfInterfaceRefCount(
220     const struct UsbSdkInterface *interfaceObj, uint8_t interfaceIndex, bool refCountFlag, bool *claimFlag)
221 {
222     if (refCountFlag && interfaceIndex != USB_CTRL_INTERFACE_ID) {
223         if (OsalAtomicRead((OsalAtomic *)&interfaceObj->refCount) == 0) {
224             if (claimFlag != NULL) {
225                 *claimFlag = true;
226             }
227         } else {
228             if (claimFlag != NULL) {
229                 *claimFlag = false;
230             }
231         }
232 
233         AdapterAtomicInc((OsalAtomic *)&interfaceObj->refCount);
234     }
235 }
236 
IfFindPipeObj(const struct UsbSdkInterface * interfaceObj,struct UsbPipeQueryPara queryPara)237 static struct UsbPipe *IfFindPipeObj(const struct UsbSdkInterface *interfaceObj, struct UsbPipeQueryPara queryPara)
238 {
239     struct UsbPipe *pipePos = NULL;
240     struct UsbPipe *pipeTemp = NULL;
241     bool findFlag = false;
242 
243     if (interfaceObj == NULL || DListIsEmpty(&interfaceObj->pipeList) ||
244         interfaceObj->status == USB_INTERFACE_STATUS_REMOVE) {
245         HDF_LOGE(
246             "%{public}s:%{public}d interfaceObj is null or status is remove or pipe list is empty", __func__, __LINE__);
247         return NULL;
248     }
249 
250     OsalMutexLock((struct OsalMutex *)&interfaceObj->listLock);
251     DLIST_FOR_EACH_ENTRY_SAFE(pipePos, pipeTemp, &interfaceObj->pipeList, struct UsbPipe, object.entry) {
252         switch (queryPara.type) {
253             case USB_PIPE_INDEX_TYPE:
254                 if (pipePos->info.pipeId == queryPara.pipeId) {
255                     findFlag = true;
256                 }
257                 break;
258             case USB_PIPE_DIRECTION_TYPE:
259                 if (pipePos->info.pipeDirection == queryPara.pipeDirection) {
260                     findFlag = true;
261                 }
262                 break;
263             default:
264                 break;
265         }
266 
267         if (findFlag) {
268             break;
269         }
270     }
271     OsalMutexUnlock((struct OsalMutex *)&interfaceObj->listLock);
272 
273     if (!findFlag) {
274         HDF_LOGE("%{public}s:%{public}d the pipe object to be find does not exist", __func__, __LINE__);
275         return NULL;
276     }
277     return pipePos;
278 }
279 
IfFindInterfaceObj(const struct UsbInterfacePool * interfacePool,struct UsbInterfaceQueryPara queryPara,bool refCountFlag,bool * claimFlag,bool statusFlag)280 static struct UsbSdkInterface *IfFindInterfaceObj(const struct UsbInterfacePool *interfacePool,
281     struct UsbInterfaceQueryPara queryPara, bool refCountFlag, bool *claimFlag, bool statusFlag)
282 {
283     struct UsbSdkInterface *interfacePos = NULL;
284     struct UsbSdkInterface *interfaceTemp = NULL;
285     bool found = false;
286 
287     if (interfacePool == NULL || DListIsEmpty(&interfacePool->interfaceList)) {
288         HDF_LOGE("%{public}s:%{public}d interfacePool is null or interface list is empty", __func__, __LINE__);
289         return NULL;
290     }
291 
292     OsalMutexLock((struct OsalMutex *)&interfacePool->interfaceLock);
293     DLIST_FOR_EACH_ENTRY_SAFE(
294         interfacePos, interfaceTemp, &interfacePool->interfaceList, struct UsbSdkInterface, interface.object.entry) {
295         switch (queryPara.type) {
296             case USB_INTERFACE_INTERFACE_INDEX_TYPE:
297                 if ((interfacePos->interface.info.interfaceIndex == queryPara.interfaceIndex) &&
298                     (interfacePos->interface.info.curAltSetting == interfacePos->altSettingId)) {
299                     found = true;
300                 }
301                 break;
302             case USB_INTERFACE_ALT_SETTINGS_TYPE:
303                 if ((interfacePos->interface.info.interfaceIndex == queryPara.interfaceIndex) &&
304                     (interfacePos->altSettingId == queryPara.altSettingId)) {
305                     found = true;
306                 }
307                 break;
308             default:
309                 break;
310         }
311 
312         if (found) {
313             IfInterfaceRefCount(interfacePos, queryPara.interfaceIndex, refCountFlag, claimFlag);
314             break;
315         }
316     }
317     OsalMutexUnlock((struct OsalMutex *)&interfacePool->interfaceLock);
318 
319     if (!found) {
320         HDF_LOGE("%{public}s:%{public}d the interface object to be find does not exist", __func__, __LINE__);
321         return NULL;
322     }
323 
324     if (statusFlag && interfacePos->status == USB_INTERFACE_STATUS_REMOVE) {
325         HDF_LOGE("%{public}s:%{public}d status = %d error", __func__, __LINE__, interfacePos->status);
326         return NULL;
327     }
328     return interfacePos;
329 }
330 
IfFindInterfacePool(const struct UsbSession * session,struct UsbPoolQueryPara queryPara,bool refCountFlag)331 static struct UsbInterfacePool *IfFindInterfacePool(
332     const struct UsbSession *session, struct UsbPoolQueryPara queryPara, bool refCountFlag)
333 {
334     struct UsbInterfacePool *interfacePoolPos = NULL;
335     struct UsbInterfacePool *interfacePoolTemp = NULL;
336     struct DListHead *ifacePoolList = NULL;
337     bool found = false;
338 
339     if (session == NULL) {
340         HDF_LOGE("%{public}s:%{public}d session is null", __func__, __LINE__);
341         return NULL;
342     }
343 
344     OsalMutexLock((struct OsalMutex *)&session->lock);
345     ifacePoolList = (struct DListHead *)&session->ifacePoolList;
346     if (DListIsEmpty(ifacePoolList) == true) {
347         OsalMutexUnlock((struct OsalMutex *)&session->lock);
348         HDF_LOGE("%{public}s:%{public}d interface pool list is empty", __func__, __LINE__);
349         return NULL;
350     }
351 
352     DLIST_FOR_EACH_ENTRY_SAFE(
353         interfacePoolPos, interfacePoolTemp, ifacePoolList, struct UsbInterfacePool, object.entry) {
354         switch (queryPara.type) {
355             case USB_POOL_NORMAL_TYPE:
356                 if ((interfacePoolPos->busNum == queryPara.busNum) &&
357                     (interfacePoolPos->devAddr == queryPara.usbAddr)) {
358                     found = true;
359                 }
360                 break;
361             case USB_POOL_OBJECT_ID_TYPE:
362                 if (interfacePoolPos->object.objectId == queryPara.objectId) {
363                     found = true;
364                 }
365                 break;
366             default:
367                 break;
368         }
369 
370         if (found) {
371             if (refCountFlag) {
372                 AdapterAtomicInc(&interfacePoolPos->refCount);
373             }
374 
375             break;
376         }
377     }
378     OsalMutexUnlock((struct OsalMutex *)&session->lock);
379 
380     if (!found) {
381         HDF_LOGE("%{public}s:%{public}d the interfacePool object to be find does not exist", __func__, __LINE__);
382         return NULL;
383     }
384 
385     return interfacePoolPos;
386 }
387 
IfGetRequestPipeType(const struct UsbDeviceHandle * devHandle,uint8_t interfaceId,uint8_t pipeId,UsbPipeType * pipeType)388 static int32_t IfGetRequestPipeType(
389     const struct UsbDeviceHandle *devHandle, uint8_t interfaceId, uint8_t pipeId, UsbPipeType *pipeType)
390 {
391     struct UsbInterfacePool *interfacePool = NULL;
392     struct UsbInterfaceQueryPara interfaceQueryPara = {0};
393     struct UsbSdkInterface *interfaceObj = NULL;
394     struct UsbPipeQueryPara pipeQueryPara = {0};
395     struct UsbPipe *pipeObj = NULL;
396 
397     if (pipeType == NULL) {
398         HDF_LOGE("%{public}s:%{public}d pipeType is null", __func__, __LINE__);
399         return HDF_ERR_INVALID_PARAM;
400     }
401 
402     /* Find interfacePool object */
403     interfacePool = (struct UsbInterfacePool *)devHandle->dev->privateObject;
404     if (interfacePool == NULL) {
405         HDF_LOGE("%{public}s:%{public}d get interfacePool failed", __func__, __LINE__);
406         return HDF_ERR_BAD_FD;
407     }
408 
409     /* Find interface object */
410     interfaceQueryPara.type = USB_INTERFACE_INTERFACE_INDEX_TYPE;
411     interfaceQueryPara.interfaceIndex = interfaceId;
412     interfaceObj = IfFindInterfaceObj(interfacePool, interfaceQueryPara, false, NULL, true);
413     if (interfaceObj == NULL) {
414         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
415         return HDF_ERR_BAD_FD;
416     }
417 
418     /* Find pipe object */
419     pipeQueryPara.type = USB_PIPE_INDEX_TYPE;
420     pipeQueryPara.pipeId = pipeId;
421     pipeObj = IfFindPipeObj(interfaceObj, pipeQueryPara);
422     if (pipeObj == NULL) {
423         HDF_LOGE("%{public}s:%{public}d pipeObj is null", __func__, __LINE__);
424         return HDF_ERR_BAD_FD;
425     }
426 
427     *pipeType = pipeObj->info.pipeType;
428 
429     return HDF_SUCCESS;
430 }
431 
IfFillControlRequest(struct UsbHostRequest * hostRequest,const struct UsbDeviceHandle * devHandle,const struct UsbRequestParams * params)432 static int32_t IfFillControlRequest(
433     struct UsbHostRequest *hostRequest, const struct UsbDeviceHandle *devHandle, const struct UsbRequestParams *params)
434 {
435     struct UsbFillRequestData fillRequestData;
436     struct UsbControlRequest ctrlReq = params->ctrlReq;
437     unsigned char *setup = hostRequest->buffer;
438     int32_t ret;
439 
440     ret = UsbProtocalFillControlSetup(setup, &ctrlReq);
441     if (ret != HDF_SUCCESS) {
442         HDF_LOGE("%{public}s:%{public}d UsbControlSetup failed", __func__, __LINE__);
443         return ret;
444     }
445     if (ctrlReq.directon == USB_REQUEST_DIR_TO_DEVICE) {
446         fillRequestData.endPoint = 0;
447         if (ctrlReq.length > 0) {
448             ret = memcpy_s(hostRequest->buffer + USB_RAW_CONTROL_SETUP_SIZE,
449                 USB_RAW_CONTROL_SETUP_SIZE + ctrlReq.length, ctrlReq.buffer, ctrlReq.length);
450             if (ret != EOK) {
451                 HDF_LOGE("%{public}s:%{public}d memcpy_s failed, ctrlReq.length = %{public}u", __func__, __LINE__,
452                     ctrlReq.length);
453                 return ret;
454             }
455         }
456     } else {
457         fillRequestData.endPoint = (((uint8_t)ctrlReq.directon) << USB_DIR_OFFSET);
458     }
459     /* fill control request */
460     fillRequestData.length = USB_RAW_CONTROL_SETUP_SIZE + ctrlReq.length;
461     fillRequestData.userCallback = params->callback;
462     fillRequestData.callback = UsbIoSetRequestCompletionInfo;
463     fillRequestData.userData = params->userData;
464     fillRequestData.timeout = params->timeout;
465 
466     return RawFillControlRequest(hostRequest, devHandle, &fillRequestData);
467 }
468 
IfFillIsoRequest(struct UsbHostRequest * hostRequest,const struct UsbDeviceHandle * devHandle,const struct UsbRequestParams * params)469 static int32_t IfFillIsoRequest(
470     struct UsbHostRequest *hostRequest, const struct UsbDeviceHandle *devHandle, const struct UsbRequestParams *params)
471 {
472     if (devHandle == NULL || params == NULL) {
473         HDF_LOGE("%{public}s: %{public}d invalid param", __func__, __LINE__);
474         return HDF_ERR_INVALID_PARAM;
475     }
476 
477     struct UsbFillRequestData fillRequestData;
478     uint8_t pipeAddress = params->pipeAddress;
479     struct UsbRequestParamsData requestData = params->dataReq;
480     UsbRequestDirection dir = requestData.directon;
481 
482     fillRequestData.endPoint = (((uint8_t)dir) << USB_DIR_OFFSET) | pipeAddress;
483     fillRequestData.buffer = requestData.buffer;
484     fillRequestData.length = requestData.length;
485     fillRequestData.numIsoPackets = requestData.numIsoPackets;
486     fillRequestData.userCallback = params->callback;
487     fillRequestData.callback = UsbIoSetRequestCompletionInfo;
488     fillRequestData.userData = params->userData;
489     fillRequestData.timeout = params->timeout;
490 
491     return RawFillIsoRequest(hostRequest, devHandle, &fillRequestData);
492 }
493 
IfFillBulkRequest(struct UsbHostRequest * hostRequest,const struct UsbDeviceHandle * devHandle,const struct UsbRequestParams * params)494 static int32_t IfFillBulkRequest(
495     struct UsbHostRequest *hostRequest, const struct UsbDeviceHandle *devHandle, const struct UsbRequestParams *params)
496 {
497     struct UsbRequestParamsData requestData = params->dataReq;
498     UsbRequestDirection dir = params->dataReq.directon;
499     uint8_t pipeAddress = params->pipeAddress;
500 
501     if ((params->dataReq.directon == USB_REQUEST_DIR_TO_DEVICE) && (requestData.length > 0)) {
502         int32_t ret = memcpy_s(hostRequest->buffer, hostRequest->bufLen, requestData.buffer, requestData.length);
503         if (ret != EOK) {
504             HDF_LOGE("%{public}s:%{public}d memcpy_s failed", __func__, __LINE__);
505             return HDF_ERR_IO;
506         }
507     }
508     hostRequest->devHandle = (struct UsbDeviceHandle *)devHandle;
509     hostRequest->endPoint = (((uint8_t)dir) << USB_DIR_OFFSET) | pipeAddress;
510     hostRequest->requestType = USB_PIPE_TYPE_BULK;
511     hostRequest->timeout = params->timeout;
512     hostRequest->length = requestData.length;
513     hostRequest->userData = params->userData;
514     hostRequest->callback = UsbIoSetRequestCompletionInfo;
515     hostRequest->userCallback = params->callback;
516 
517     return HDF_SUCCESS;
518 }
519 
IfFillBulkRequestByMmap(struct UsbHostRequest * hostRequest,const struct UsbDeviceHandle * devHandle,const struct UsbRequestParams * params)520 static int32_t IfFillBulkRequestByMmap(
521     struct UsbHostRequest *hostRequest, const struct UsbDeviceHandle *devHandle, const struct UsbRequestParams *params)
522 {
523     struct UsbRequestParamsData requestData = params->dataReq;
524     uint8_t pipeAddress = params->pipeAddress;
525     hostRequest->devHandle = (struct UsbDeviceHandle *)devHandle;
526     hostRequest->endPoint = pipeAddress;
527     hostRequest->requestType = USB_PIPE_TYPE_BULK;
528     hostRequest->timeout = params->timeout;
529     hostRequest->length = requestData.length;
530     hostRequest->userData = params->userData;
531     hostRequest->callback = UsbIoSetRequestCompletionInfo;
532     hostRequest->userCallback = params->callback;
533 
534     return HDF_SUCCESS;
535 }
536 
IfFillInterrupteRequest(struct UsbHostRequest * hostRequest,const struct UsbDeviceHandle * devHandle,const struct UsbRequestParams * params)537 static int32_t IfFillInterrupteRequest(
538     struct UsbHostRequest *hostRequest, const struct UsbDeviceHandle *devHandle, const struct UsbRequestParams *params)
539 {
540     struct UsbFillRequestData fillRequestData;
541     uint8_t pipeAddress = params->pipeAddress;
542     struct UsbRequestParamsData requestData = params->dataReq;
543     UsbRequestDirection dir = requestData.directon;
544 
545     fillRequestData.endPoint = (((uint8_t)dir) << USB_DIR_OFFSET) | pipeAddress;
546     fillRequestData.buffer = requestData.buffer;
547     fillRequestData.length = requestData.length;
548     fillRequestData.userCallback = params->callback;
549     fillRequestData.callback = UsbIoSetRequestCompletionInfo;
550     fillRequestData.userData = params->userData;
551     fillRequestData.timeout = params->timeout;
552 
553     return RawFillInterruptRequest(hostRequest, devHandle, &fillRequestData);
554 }
555 
IfFillInterrupteRequestByMmap(struct UsbHostRequest * hostRequest,const struct UsbDeviceHandle * devHandle,const struct UsbRequestParams * params)556 static int32_t IfFillInterrupteRequestByMmap(
557     struct UsbHostRequest *hostRequest, const struct UsbDeviceHandle *devHandle, const struct UsbRequestParams *params)
558 {
559     struct UsbFillRequestData fillRequestData;
560     uint8_t pipeAddress = params->pipeAddress;
561     struct UsbRequestParamsData requestData = params->dataReq;
562 
563     fillRequestData.endPoint = pipeAddress;
564     fillRequestData.buffer = requestData.buffer;
565     fillRequestData.length = requestData.length;
566     fillRequestData.userCallback = params->callback;
567     fillRequestData.callback = UsbIoSetRequestCompletionInfo;
568     fillRequestData.userData = params->userData;
569     fillRequestData.timeout = params->timeout;
570 
571     return RawFillInterruptRequestByMmap(hostRequest, devHandle, &fillRequestData);
572 }
573 
IfSubmitRequestToQueue(const struct UsbIfRequest * requestObj)574 static int32_t IfSubmitRequestToQueue(const struct UsbIfRequest *requestObj)
575 {
576     int32_t ret;
577     struct UsbHostRequest *hostRequest = NULL;
578     struct UsbInterfacePool *interfacePool = NULL;
579 
580     if (requestObj == NULL) {
581         HDF_LOGE("%{public}s:%{public}d requestObj is null", __func__, __LINE__);
582         return HDF_ERR_INVALID_PARAM;
583     }
584 
585     hostRequest = requestObj->hostRequest;
586     if (hostRequest == NULL || hostRequest->devHandle == NULL || hostRequest->devHandle->dev == NULL) {
587         HDF_LOGE("%{public}s:%{public}d hostRequest is null", __func__, __LINE__);
588         return HDF_ERR_INVALID_PARAM;
589     }
590 
591     interfacePool = (struct UsbInterfacePool *)hostRequest->devHandle->dev->privateObject;
592     if (interfacePool == NULL) {
593         HDF_LOGE("%{public}s:%{public}d get interfacePool failed", __func__, __LINE__);
594         return HDF_ERR_BAD_FD;
595     }
596 
597     ret = UsbIoSendRequest(&interfacePool->submitRequestQueue, hostRequest);
598     if (ret != HDF_SUCCESS) {
599         return ret;
600     }
601 
602     return ret;
603 }
604 
IfFillRequestByPipeType(struct UsbIfRequest * requestObj,UsbPipeType pipeType,struct UsbHostRequest * hostRequest,const struct UsbDeviceHandle * devHandle,const struct UsbRequestParams * params)605 static int32_t IfFillRequestByPipeType(struct UsbIfRequest *requestObj, UsbPipeType pipeType,
606     struct UsbHostRequest *hostRequest, const struct UsbDeviceHandle *devHandle, const struct UsbRequestParams *params)
607 {
608     int32_t ret;
609 
610     switch (pipeType) {
611         case USB_PIPE_TYPE_CONTROL:
612             if (params->requestType != USB_REQUEST_PARAMS_CTRL_TYPE) {
613                 ret = HDF_ERR_INVALID_PARAM;
614                 HDF_LOGE("%{public}s:%d params is not CTRL_TYPE", __func__, __LINE__);
615                 break;
616             }
617 
618             requestObj->request.compInfo.type = USB_REQUEST_TYPE_DEVICE_CONTROL;
619 
620             ret = IfFillControlRequest(hostRequest, devHandle, params);
621             break;
622         case USB_PIPE_TYPE_ISOCHRONOUS:
623             if (params->requestType != USB_REQUEST_PARAMS_DATA_TYPE) {
624                 ret = HDF_ERR_INVALID_PARAM;
625                 HDF_LOGE("%{public}s:%d params is not DATA_TYPE", __func__, __LINE__);
626                 break;
627             }
628 
629             ret = IfFillIsoRequest(hostRequest, devHandle, params);
630             break;
631         case USB_PIPE_TYPE_BULK:
632             if (params->requestType != USB_REQUEST_PARAMS_DATA_TYPE) {
633                 ret = HDF_ERR_INVALID_PARAM;
634                 HDF_LOGE("%{public}s:%d params is not DATA_TYPE", __func__, __LINE__);
635                 break;
636             }
637 
638             ret = IfFillBulkRequest(hostRequest, devHandle, params);
639             break;
640         case USB_PIPE_TYPE_INTERRUPT:
641             if (params->requestType != USB_REQUEST_PARAMS_DATA_TYPE) {
642                 ret = HDF_ERR_INVALID_PARAM;
643                 HDF_LOGE("%{public}s:%d params is not DATA_TYPE", __func__, __LINE__);
644                 break;
645             }
646 
647             ret = IfFillInterrupteRequest(hostRequest, devHandle, params);
648             break;
649         default:
650             ret = HDF_FAILURE;
651             break;
652     }
653 
654     return ret;
655 }
656 
IfFillRequestByPipeTypeByMmap(struct UsbIfRequest * requestObj,UsbPipeType pipeType,struct UsbHostRequest * hostRequest,const struct UsbDeviceHandle * devHandle,const struct UsbRequestParams * params)657 static int32_t IfFillRequestByPipeTypeByMmap(struct UsbIfRequest *requestObj, UsbPipeType pipeType,
658     struct UsbHostRequest *hostRequest, const struct UsbDeviceHandle *devHandle, const struct UsbRequestParams *params)
659 {
660     int32_t ret;
661 
662     switch (pipeType) {
663         case USB_PIPE_TYPE_CONTROL:
664             if (params->requestType != USB_REQUEST_PARAMS_CTRL_TYPE) {
665                 ret = HDF_ERR_INVALID_PARAM;
666                 HDF_LOGE("%{public}s:%d params is not CTRL_TYPE", __func__, __LINE__);
667                 break;
668             }
669 
670             requestObj->request.compInfo.type = USB_REQUEST_TYPE_DEVICE_CONTROL;
671 
672             ret = IfFillControlRequest(hostRequest, devHandle, params);
673             break;
674         case USB_PIPE_TYPE_ISOCHRONOUS:
675             if (params->requestType != USB_REQUEST_PARAMS_DATA_TYPE) {
676                 ret = HDF_ERR_INVALID_PARAM;
677                 HDF_LOGE("%{public}s:%d params is not DATA_TYPE", __func__, __LINE__);
678                 break;
679             }
680 
681             ret = IfFillIsoRequest(hostRequest, devHandle, params);
682             break;
683         case USB_PIPE_TYPE_BULK:
684             if (params->requestType != USB_REQUEST_PARAMS_DATA_TYPE) {
685                 ret = HDF_ERR_INVALID_PARAM;
686                 HDF_LOGE("%{public}s:%d params is not DATA_TYPE", __func__, __LINE__);
687                 break;
688             }
689 
690             ret = IfFillBulkRequestByMmap(hostRequest, devHandle, params);
691             break;
692         case USB_PIPE_TYPE_INTERRUPT:
693             if (params->requestType != USB_REQUEST_PARAMS_DATA_TYPE) {
694                 ret = HDF_ERR_INVALID_PARAM;
695                 HDF_LOGE("%{public}s:%d params is not DATA_TYPE", __func__, __LINE__);
696                 break;
697             }
698 
699             ret = IfFillInterrupteRequestByMmap(hostRequest, devHandle, params);
700             break;
701         default:
702             ret = HDF_FAILURE;
703             break;
704     }
705 
706     return ret;
707 }
708 
IfDestoryDevice(const struct UsbSession * session,const struct UsbInterfacePool * interfacePool,const struct UsbDeviceHandle * devHandle,bool refCountFlag)709 static int32_t IfDestoryDevice(const struct UsbSession *session, const struct UsbInterfacePool *interfacePool,
710     const struct UsbDeviceHandle *devHandle, bool refCountFlag)
711 {
712     int32_t ret;
713 
714     if (session == NULL || interfacePool == NULL || devHandle == NULL) {
715         HDF_LOGE("%{public}s:%{public}d invalid param", __func__, __LINE__);
716         return HDF_ERR_INVALID_PARAM;
717     }
718 
719     OsalMutexLock((struct OsalMutex *)&session->lock);
720     if (refCountFlag) {
721         AdapterAtomicDec((OsalAtomic *)&interfacePool->refCount);
722     }
723 
724     if (OsalAtomicRead((OsalAtomic *)&interfacePool->refCount) > 0) {
725         OsalMutexUnlock((struct OsalMutex *)&session->lock);
726         return HDF_SUCCESS;
727     }
728 
729     ret = IfDestroyInterfacePool(interfacePool);
730     if (ret != HDF_SUCCESS) {
731         HDF_LOGE("%{public}s:%{public}d destroy interface pool failed", __func__, __LINE__);
732         OsalMutexUnlock((struct OsalMutex *)&session->lock);
733         return ret;
734     }
735     OsalMutexUnlock((struct OsalMutex *)&session->lock);
736 
737     ret = RawCloseDevice(devHandle);
738     if (ret != HDF_SUCCESS) {
739         HDF_LOGE("%{public}s:%{public}d close device failed", __func__, __LINE__);
740     }
741 
742     return ret;
743 }
744 
IfGetInterfacePool(struct UsbDeviceHandle ** devHandle,const struct UsbSession * realSession,uint8_t busNum,uint8_t usbAddr)745 static struct UsbInterfacePool *IfGetInterfacePool(
746     struct UsbDeviceHandle **devHandle, const struct UsbSession *realSession, uint8_t busNum, uint8_t usbAddr)
747 {
748     struct UsbPoolQueryPara poolQueryPara;
749     struct UsbInterfacePool *interfacePool = NULL;
750     int32_t ret;
751 
752     *devHandle = RawOpenDevice(realSession, busNum, usbAddr);
753     if (*devHandle == NULL) {
754         HDF_LOGE("%{public}s:%{public}d RawOpenDevice failed", __func__, __LINE__);
755         return NULL;
756     }
757 
758     ret = UsbProtocalParseDescriptor(*devHandle, busNum, usbAddr);
759     if (ret != HDF_SUCCESS) {
760         HDF_LOGE("%{public}s:%{public}d UsbProtocalParseDescriptor failed, ret = %d", __func__, __LINE__, ret);
761         (void)RawCloseDevice(*devHandle);
762         return NULL;
763     }
764 
765     poolQueryPara.type = USB_POOL_NORMAL_TYPE;
766     poolQueryPara.busNum = busNum;
767     poolQueryPara.usbAddr = usbAddr;
768     interfacePool = IfFindInterfacePool(realSession, poolQueryPara, true);
769     if (interfacePool == NULL || interfacePool->device == NULL) {
770         interfacePool = (struct UsbInterfacePool *)((*devHandle)->dev->privateObject);
771         (void)IfDestoryDevice(realSession, interfacePool, *devHandle, false);
772         return NULL;
773     }
774 
775     return interfacePool;
776 }
777 
UsbIfCreatPipeObj(const struct UsbSdkInterface * interfaceObj,struct UsbPipe ** pipeObj)778 int32_t UsbIfCreatPipeObj(const struct UsbSdkInterface *interfaceObj, struct UsbPipe **pipeObj)
779 {
780     struct UsbPipe *pipeObjTemp = NULL;
781     static int32_t idNum = 0;
782 
783     if (interfaceObj == NULL || pipeObj == NULL) {
784         HDF_LOGE("%{public}s:%{public}d interfaceObj or pipeObj is null", __func__, __LINE__);
785         return HDF_ERR_INVALID_PARAM;
786     }
787 
788     pipeObjTemp = (struct UsbPipe *)RawUsbMemCalloc(sizeof(struct UsbPipe));
789     if (pipeObjTemp == NULL) {
790         HDF_LOGE("%{public}s:%{public}d RawUsbMemCalloc failed", __func__, __LINE__);
791         return HDF_ERR_MALLOC_FAIL;
792     }
793 
794     ++idNum;
795     idNum %= INTERFACE_POOL_ID_MAX;
796     pipeObjTemp->object.objectId = idNum;
797     DListHeadInit(&pipeObjTemp->object.entry);
798 
799     OsalMutexLock((struct OsalMutex *)&interfaceObj->listLock);
800     DListInsertTail(&pipeObjTemp->object.entry, (struct DListHead *)&interfaceObj->pipeList);
801     OsalMutexUnlock((struct OsalMutex *)&interfaceObj->listLock);
802 
803     *pipeObj = pipeObjTemp;
804     (*pipeObj)->info.interfaceId = interfaceObj->interface.info.interfaceIndex;
805     return HDF_SUCCESS;
806 }
807 
UsbIfCreatInterfaceObj(const struct UsbInterfacePool * interfacePool,struct UsbSdkInterface ** interfaceObj)808 int32_t UsbIfCreatInterfaceObj(const struct UsbInterfacePool *interfacePool, struct UsbSdkInterface **interfaceObj)
809 {
810     struct UsbSdkInterface *interfaceObjTemp = NULL;
811     static int32_t idNum = 0;
812 
813     if (interfacePool == NULL || interfaceObj == NULL) {
814         HDF_LOGE("%{public}s:%{public}d interfacePool or interfaceObj is null", __func__, __LINE__);
815         return HDF_ERR_INVALID_PARAM;
816     }
817 
818     interfaceObjTemp = (struct UsbSdkInterface *)RawUsbMemCalloc(sizeof(struct UsbSdkInterface));
819     if (interfaceObjTemp == NULL) {
820         HDF_LOGE("%{public}s:%{public}d RawUsbMemCalloc failed", __func__, __LINE__);
821         return HDF_ERR_MALLOC_FAIL;
822     }
823 
824     ++idNum;
825     idNum %= INTERFACE_POOL_ID_MAX;
826     interfaceObjTemp->interface.object.objectId = idNum;
827     DListHeadInit(&interfaceObjTemp->interface.object.entry);
828     IfInterfaceObjInit(interfaceObjTemp);
829     interfaceObjTemp->parentObjectId = interfacePool->object.objectId;
830 
831     OsalMutexLock((struct OsalMutex *)&interfacePool->interfaceLock);
832     DListInsertTail(&interfaceObjTemp->interface.object.entry, (struct DListHead *)&interfacePool->interfaceList);
833     OsalMutexUnlock((struct OsalMutex *)&interfacePool->interfaceLock);
834 
835     *interfaceObj = interfaceObjTemp;
836 
837     return HDF_SUCCESS;
838 }
839 
UsbIfDestroyInterfaceObj(const struct UsbInterfacePool * interfacePool,const struct UsbSdkInterface * interfaceObj)840 HDF_STATUS UsbIfDestroyInterfaceObj(
841     const struct UsbInterfacePool *interfacePool, const struct UsbSdkInterface *interfaceObj)
842 {
843     HDF_STATUS ret = HDF_SUCCESS;
844     struct UsbSdkInterface *interfacePos = NULL;
845     struct UsbSdkInterface *interfaceTemp = NULL;
846     bool found = false;
847     bool destroyFlag = false;
848 
849     if (interfacePool == NULL) {
850         HDF_LOGE("%{public}s:%{public}d interfacePool is null", __func__, __LINE__);
851         return HDF_FAILURE;
852     }
853 
854     if (DListIsEmpty(&interfacePool->interfaceList)) {
855         HDF_LOGE("%{public}s:%{public}d interfaceList is empty ", __func__, __LINE__);
856         return HDF_SUCCESS;
857     }
858 
859     if (interfaceObj == NULL) {
860         /* Destroys all interface object */
861         destroyFlag = true;
862     } else {
863         /* Destroys the specified interface object */
864         destroyFlag = false;
865     }
866 
867     OsalMutexLock((struct OsalMutex *)&interfacePool->interfaceLock);
868     DLIST_FOR_EACH_ENTRY_SAFE(
869         interfacePos, interfaceTemp, &interfacePool->interfaceList, struct UsbSdkInterface, interface.object.entry) {
870         if (destroyFlag || interfacePos->interface.object.objectId == interfaceObj->interface.object.objectId) {
871             found = true;
872             DListRemove(&interfacePos->interface.object.entry);
873             ret = IfFreeInterfaceObj(interfacePos);
874             if (ret != HDF_SUCCESS) {
875                 HDF_LOGE("%{public}s:%{public}d IfFreeInterfaceObj failed, ret = %d", __func__, __LINE__, ret);
876                 break;
877             }
878 
879             if (!destroyFlag) {
880                 break;
881             }
882         }
883     }
884     OsalMutexUnlock((struct OsalMutex *)&interfacePool->interfaceLock);
885 
886     if (!found) {
887         ret = HDF_FAILURE;
888         HDF_LOGE("%{public}s:%{public}d the interface object to be destroyed does not exist", __func__, __LINE__);
889     }
890 
891     return ret;
892 }
893 
UsbIfCreatInterfacePool(const struct UsbSession * session,uint8_t busNum,uint8_t devAddr,struct UsbInterfacePool ** interfacePool)894 int32_t UsbIfCreatInterfacePool(
895     const struct UsbSession *session, uint8_t busNum, uint8_t devAddr, struct UsbInterfacePool **interfacePool)
896 {
897     struct UsbInterfacePool *interfacePoolTemp = NULL;
898     static int32_t idNum = 0;
899 
900     if (interfacePool == NULL) {
901         HDF_LOGE("%{public}s:%{public}d interfacePool is null!", __func__, __LINE__);
902         return HDF_ERR_INVALID_PARAM;
903     }
904 
905     interfacePoolTemp = (struct UsbInterfacePool *)RawUsbMemAlloc(sizeof(struct UsbInterfacePool));
906     if (interfacePoolTemp == NULL) {
907         HDF_LOGE("%{public}s:%{public}d RawUsbMemAlloc failed", __func__, __LINE__);
908         *interfacePool = NULL;
909         return HDF_ERR_MALLOC_FAIL;
910     }
911 
912     ++idNum;
913     idNum %= INTERFACE_POOL_ID_MAX;
914     interfacePoolTemp->object.objectId = idNum;
915     interfacePoolTemp->ioProcessTid = 0;
916 
917     if (IfInterfacePoolInit(interfacePoolTemp, busNum, devAddr) != HDF_SUCCESS) {
918         RawUsbMemFree(interfacePoolTemp);
919         *interfacePool = NULL;
920         return HDF_ERR_IO;
921     }
922     OsalMutexLock((struct OsalMutex *)&session->lock);
923     DListInsertTail(&interfacePoolTemp->object.entry, (struct DListHead *)&session->ifacePoolList);
924     OsalMutexUnlock((struct OsalMutex *)&session->lock);
925 
926     *interfacePool = interfacePoolTemp;
927 
928     return HDF_SUCCESS;
929 }
930 
UsbInitHostSdk(struct UsbSession ** session)931 int32_t UsbInitHostSdk(struct UsbSession **session)
932 {
933     return RawInit(session);
934 }
935 
UsbExitHostSdk(const struct UsbSession * session)936 int32_t UsbExitHostSdk(const struct UsbSession *session)
937 {
938     return RawExit(session);
939 }
940 
SetPoolQueryPara(struct UsbPoolQueryPara * poolQueryPara,uint8_t busNum,uint8_t usbAddr)941 static void SetPoolQueryPara(struct UsbPoolQueryPara *poolQueryPara, uint8_t busNum, uint8_t usbAddr)
942 {
943     poolQueryPara->type = USB_POOL_NORMAL_TYPE;
944     poolQueryPara->busNum = busNum;
945     poolQueryPara->usbAddr = usbAddr;
946 }
947 
UsbGetDeviceMemMapFd(const struct UsbSession * session,uint8_t busNum,uint8_t usbAddr)948 int32_t UsbGetDeviceMemMapFd(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr)
949 {
950     struct UsbPoolQueryPara poolQueryPara = {0};
951     struct UsbSession *realSession = RawGetSession(session);
952 
953     if (realSession == NULL) {
954         HDF_LOGE("%{public}s:%{public}d interfacePoolList is empty", __func__, __LINE__);
955         return HDF_FAILURE;
956     }
957     SetPoolQueryPara(&poolQueryPara, busNum, usbAddr);
958     struct UsbDeviceHandle *devHandle = NULL;
959     struct UsbInterfacePool *interfacePool = IfFindInterfacePool(realSession, poolQueryPara, true);
960     if (interfacePool == NULL || interfacePool->device == NULL) {
961         interfacePool = IfGetInterfacePool(&devHandle, realSession, busNum, usbAddr);
962         if (interfacePool == NULL || interfacePool->device == NULL) {
963             HDF_LOGE("%{public}s:%{public}d interfacePool or interfacePool->device is null", __func__, __LINE__);
964             return HDF_FAILURE;
965         }
966     }
967 
968     return interfacePool->device->devHandle->mmapFd;
969 }
970 
ClaimInterface(const struct UsbSession * session,uint8_t busNum,uint8_t usbAddr,uint8_t interfaceIndex,bool force)971 static struct UsbInterface *ClaimInterface(
972     const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex, bool force)
973 {
974     struct UsbPoolQueryPara poolQueryPara = {0};
975     struct UsbInterfacePool *interfacePool = NULL;
976     struct UsbInterfaceQueryPara interfaceQueryPara = {USB_INTERFACE_INTERFACE_INDEX_TYPE, interfaceIndex, 0};
977     struct UsbSdkInterface *interfaceObj = NULL;
978     struct UsbDeviceHandle *devHandle = NULL;
979     struct UsbSession *realSession = RawGetSession(session);
980     int32_t ret;
981     bool claimFlag = false;
982 
983     if (realSession == NULL) {
984         HDF_LOGE("%{public}s:%{public}d interfacePoolList is empty", __func__, __LINE__);
985         return NULL;
986     }
987     SetPoolQueryPara(&poolQueryPara, busNum, usbAddr);
988 
989     interfacePool = IfFindInterfacePool(realSession, poolQueryPara, true);
990     if (interfacePool == NULL || interfacePool->device == NULL) {
991         interfacePool = IfGetInterfacePool(&devHandle, realSession, busNum, usbAddr);
992         if (interfacePool == NULL || interfacePool->device == NULL) {
993             HDF_LOGE("%{public}s:%{public}d interfacePool or interfacePool->device is null", __func__, __LINE__);
994             return NULL;
995         }
996     }
997 
998     interfaceObj = IfFindInterfaceObj(interfacePool, interfaceQueryPara, true, &claimFlag, true);
999     if (interfaceObj == NULL) {
1000         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
1001         goto ERROR;
1002     }
1003 
1004     if (interfaceIndex != USB_CTRL_INTERFACE_ID && claimFlag) {
1005         OsalMutexLock(&interfacePool->interfaceLock);
1006         devHandle = interfacePool->device->devHandle;
1007         interfaceObj->forceDetachKernelDriver = force;
1008         if (force) {
1009             ret = RawClaimInterfaceForce(devHandle, interfaceIndex);
1010         } else {
1011             ret = RawClaimInterface(devHandle, interfaceIndex);
1012         }
1013         if (ret != HDF_SUCCESS) {
1014             HDF_LOGE("%{public}s:%{public}d RawClaimInterface failed, ret = %d", __func__, __LINE__, ret);
1015             AdapterAtomicDec(&interfaceObj->refCount);
1016             OsalMutexUnlock(&interfacePool->interfaceLock);
1017             goto ERROR;
1018         }
1019         OsalMutexUnlock(&interfacePool->interfaceLock);
1020     }
1021     interfaceObj->session = realSession;
1022 
1023     return (struct UsbInterface *)interfaceObj;
1024 ERROR:
1025     (void)IfDestoryDevice(realSession, interfacePool, devHandle, true);
1026     return NULL;
1027 }
1028 
UsbClaimInterfaceUnforce(const struct UsbSession * session,uint8_t busNum,uint8_t usbAddr,uint8_t interfaceIndex)1029 struct UsbInterface *UsbClaimInterfaceUnforce(
1030     const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex)
1031 {
1032     struct UsbInterface *interfaceObj = ClaimInterface(session, busNum, usbAddr, interfaceIndex, false);
1033     if (interfaceObj == NULL) {
1034         HDF_LOGE("%{public}s: interfaceObj is NULL", __func__);
1035         return NULL;
1036     }
1037 
1038     struct UsbSdkInterface *interfaceSdk = (struct UsbSdkInterface *)interfaceObj;
1039     if (OsalAtomicRead((OsalAtomic *)&interfaceSdk->refCount) > INTERFACE_REFCOUNT_UNFORCE) {
1040         int32_t ret = UsbReleaseInterface(interfaceObj);
1041         if (ret != HDF_SUCCESS) {
1042             HDF_LOGE("%{public}s: UsbReleaseInterface failed!", __func__);
1043         }
1044         return NULL;
1045     }
1046     return interfaceObj;
1047 }
1048 
UsbClaimInterface(const struct UsbSession * session,uint8_t busNum,uint8_t usbAddr,uint8_t interfaceIndex)1049 struct UsbInterface *UsbClaimInterface(
1050     const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex)
1051 {
1052     return ClaimInterface(session, busNum, usbAddr, interfaceIndex, true);
1053 }
1054 
UsbAttachKernelDriver(struct UsbDeviceHandle * devHandle,const struct UsbSdkInterface * interfaceObj)1055 static void UsbAttachKernelDriver(struct UsbDeviceHandle *devHandle, const struct UsbSdkInterface *interfaceObj)
1056 {
1057     if (!interfaceObj->forceDetachKernelDriver) {
1058         HDF_LOGI("%{public}s not force", __func__);
1059         return;
1060     }
1061 
1062     RawAttachKernelDriver(devHandle, interfaceObj->interface.info.interfaceIndex);
1063 }
1064 
UsbReleaseInterface(const struct UsbInterface * interfaceObj)1065 int32_t UsbReleaseInterface(const struct UsbInterface *interfaceObj)
1066 {
1067     struct UsbSdkInterface *interfaceSdk = (struct UsbSdkInterface *)interfaceObj;
1068     if (interfaceSdk == NULL || interfaceSdk->session == NULL) {
1069         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
1070         return HDF_ERR_INVALID_PARAM;
1071     }
1072 
1073     struct UsbPoolQueryPara queryPara;
1074     queryPara.type = USB_POOL_OBJECT_ID_TYPE;
1075     queryPara.objectId = interfaceSdk->parentObjectId;
1076     struct UsbInterfacePool *interfacePool = IfFindInterfacePool(interfaceSdk->session, queryPara, false);
1077     if (interfacePool == NULL || interfacePool->session == NULL) {
1078         HDF_LOGE("%{public}s:%{public}d interfacePool is null", __func__, __LINE__);
1079         return HDF_ERR_BAD_FD;
1080     }
1081 
1082     struct UsbDeviceHandle *devHandle = interfacePool->device->devHandle;
1083     uint8_t interfaceIndex = interfaceSdk->interface.info.interfaceIndex;
1084     OsalMutexLock(&interfacePool->interfaceLock);
1085     if (interfaceIndex != USB_CTRL_INTERFACE_ID && AdapterAtomicDec(&interfaceSdk->refCount) <= 0) {
1086         int32_t ret = RawReleaseInterface(devHandle, interfaceIndex);
1087         if (ret != HDF_SUCCESS && ret != HDF_DEV_ERR_NO_DEVICE) {
1088             HDF_LOGE("%{public}s:%{public}d RawReleaseInterface failed, ret = %d", __func__, __LINE__, ret);
1089             AdapterAtomicInc(&interfaceSdk->refCount);
1090             OsalMutexUnlock(&interfacePool->interfaceLock);
1091             return ret;
1092         }
1093         UsbAttachKernelDriver(devHandle, interfaceSdk);
1094     }
1095     OsalMutexUnlock(&interfacePool->interfaceLock);
1096 
1097     return IfDestoryDevice(interfacePool->session, interfacePool, devHandle, true);
1098 }
1099 
UsbAddOrRemoveInterface(const struct UsbSession * session,uint8_t busNum,uint8_t usbAddr,uint8_t interfaceIndex,UsbInterfaceStatus status)1100 int32_t UsbAddOrRemoveInterface(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr,
1101     uint8_t interfaceIndex, UsbInterfaceStatus status)
1102 {
1103     int32_t ret;
1104     struct UsbInterfaceQueryPara interfaceQueryPara = {0};
1105     struct UsbSdkInterface *interfaceObj = NULL;
1106     struct UsbPoolQueryPara poolQueryPara;
1107     struct UsbInterfacePool *interfacePool = NULL;
1108     enum UsbPnpNotifyServiceCmd cmdType = USB_PNP_NOTIFY_ADD_INTERFACE;
1109     struct UsbPnpAddRemoveInfo infoData = {0};
1110     struct UsbSession *realSession = RawGetSession(session);
1111 
1112     /* Find interfacePool object */
1113     poolQueryPara.type = USB_POOL_NORMAL_TYPE;
1114     poolQueryPara.busNum = busNum;
1115     poolQueryPara.usbAddr = usbAddr;
1116     interfacePool = IfFindInterfacePool(realSession, poolQueryPara, false);
1117     if (interfacePool == NULL) {
1118         HDF_LOGE("%{public}s:%{public}d interfacePool is null", __func__, __LINE__);
1119         return HDF_ERR_BAD_FD;
1120     }
1121 
1122     interfaceQueryPara.type = USB_INTERFACE_INTERFACE_INDEX_TYPE;
1123     interfaceQueryPara.interfaceIndex = interfaceIndex;
1124     interfaceObj = IfFindInterfaceObj(interfacePool, interfaceQueryPara, false, NULL, false);
1125     if (interfaceObj == NULL) {
1126         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
1127         return HDF_ERR_BAD_FD;
1128     }
1129 
1130     if (interfaceObj->status == status) {
1131         HDF_LOGE("%{public}s:%{public}d interfaceObj->status = %d is error", __func__, __LINE__, interfaceObj->status);
1132         return HDF_ERR_INVALID_PARAM;
1133     }
1134 
1135     if (status == USB_INTERFACE_STATUS_ADD) {
1136         cmdType = USB_PNP_NOTIFY_ADD_INTERFACE;
1137     } else if (status == USB_INTERFACE_STATUS_REMOVE) {
1138         cmdType = USB_PNP_NOTIFY_REMOVE_INTERFACE;
1139     } else {
1140         HDF_LOGE("%{public}s:%{public}d status = %d is not define", __func__, __LINE__, status);
1141         return HDF_ERR_INVALID_PARAM;
1142     }
1143 
1144     infoData.devNum = (int32_t)interfacePool->devAddr;
1145     infoData.busNum = (int32_t)interfacePool->busNum;
1146     infoData.interfaceNumber = interfaceObj->interface.info.interfaceIndex;
1147     infoData.interfaceClass = interfaceObj->interface.info.interfaceClass;
1148     infoData.interfaceSubClass = interfaceObj->interface.info.interfaceSubClass;
1149     infoData.interfaceProtocol = interfaceObj->interface.info.interfaceProtocol;
1150     ret = RawInitPnpService(cmdType, infoData);
1151     if (ret == HDF_SUCCESS) {
1152         interfaceObj->status = status;
1153     }
1154 
1155     return ret;
1156 }
1157 
UsbOpenInterface(const struct UsbInterface * interfaceObj)1158 UsbInterfaceHandle *UsbOpenInterface(const struct UsbInterface *interfaceObj)
1159 {
1160     if (interfaceObj == NULL) {
1161         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
1162         return NULL;
1163     }
1164 
1165     struct UsbSdkInterface *interfaceSdk = (struct UsbSdkInterface *)interfaceObj;
1166     if (interfaceSdk->session == NULL || interfaceSdk->status == USB_INTERFACE_STATUS_REMOVE) {
1167         HDF_LOGE("%{public}s:%{public}d interfaceSdk->status = %d is error", __func__, __LINE__, interfaceSdk->status);
1168         return NULL;
1169     }
1170 
1171     struct UsbPoolQueryPara poolQueryPara;
1172     poolQueryPara.type = USB_POOL_OBJECT_ID_TYPE;
1173     poolQueryPara.objectId = interfaceSdk->parentObjectId;
1174     struct UsbInterfacePool *interfacePool = IfFindInterfacePool(interfaceSdk->session, poolQueryPara, false);
1175     if (interfacePool == NULL || interfacePool->device == NULL || interfacePool->device->devHandle == NULL) {
1176         HDF_LOGE("%{public}s:%{public}d interfacePool or interfacePool->device is null", __func__, __LINE__);
1177         return NULL;
1178     }
1179 
1180     OsalMutexLock(&interfacePool->interfaceLock);
1181     struct UsbInterfaceHandleEntity *ifaceHdl = RawUsbMemCalloc(sizeof(struct UsbInterfaceHandleEntity));
1182     if (ifaceHdl == NULL) {
1183         HDF_LOGE("%{public}s:%{public}d RawUsbMemAlloc failed", __func__, __LINE__);
1184         goto OUT;
1185     }
1186     ifaceHdl->devHandle = interfacePool->device->devHandle;
1187     ifaceHdl->interfaceIndex = interfaceSdk->interface.info.interfaceIndex;
1188 
1189     if (OsalAtomicRead(&interfacePool->ioRefCount) == 0) {
1190         HDF_STATUS ret = UsbIoStart(interfacePool);
1191         if (ret != HDF_SUCCESS) {
1192             HDF_LOGE("%{public}s:%{public}d UsbIoStart failed, ret = %d", __func__, __LINE__, ret);
1193             ifaceHdl->devHandle = NULL;
1194             RawUsbMemFree(ifaceHdl);
1195             goto OUT;
1196         }
1197     }
1198     AdapterAtomicInc(&interfaceSdk->refCount);
1199     AdapterAtomicInc(&interfacePool->ioRefCount);
1200     OsalMutexUnlock(&interfacePool->interfaceLock);
1201 
1202     return (UsbInterfaceHandle *)ifaceHdl;
1203 
1204 OUT:
1205     OsalMutexUnlock(&interfacePool->interfaceLock);
1206     return NULL;
1207 }
1208 
GetInterfaceByHandle(const UsbInterfaceHandle * interfaceHandle,struct UsbInterface ** interface)1209 int32_t GetInterfaceByHandle(const UsbInterfaceHandle *interfaceHandle, struct UsbInterface **interface)
1210 {
1211     if (interfaceHandle == NULL) {
1212         HDF_LOGE("%{public}s handle is null", __func__);
1213         return HDF_ERR_INVALID_PARAM;
1214     }
1215 
1216     struct UsbInterfaceHandleEntity *ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1217     if (ifaceHdl->devHandle == NULL || ifaceHdl->devHandle->dev == NULL ||
1218         ifaceHdl->devHandle->dev->privateObject == NULL) {
1219         HDF_LOGE("%{public}s ifaceHdl is null", __func__);
1220         return HDF_ERR_INVALID_PARAM;
1221     }
1222 
1223     struct UsbInterfacePool *interfacePool = (struct UsbInterfacePool *)ifaceHdl->devHandle->dev->privateObject;
1224     /* Find interface object */
1225     struct UsbInterfaceQueryPara interfaceQueryPara = {
1226         .type = USB_INTERFACE_INTERFACE_INDEX_TYPE, .interfaceIndex = ifaceHdl->interfaceIndex};
1227     struct UsbSdkInterface *interfaceObj = IfFindInterfaceObj(interfacePool, interfaceQueryPara, false, NULL, false);
1228     if (interfaceObj == NULL) {
1229         HDF_LOGE("%{public}s interfaceObj is null", __func__);
1230         return HDF_ERR_BAD_FD;
1231     }
1232     *interface = (struct UsbInterface *)interfaceObj;
1233     return HDF_SUCCESS;
1234 }
1235 
UsbCloseInterface(const UsbInterfaceHandle * interfaceHandle)1236 int32_t UsbCloseInterface(const UsbInterfaceHandle *interfaceHandle)
1237 {
1238     HDF_STATUS ret;
1239     struct UsbInterfaceHandleEntity *ifaceHdl = NULL;
1240     struct UsbInterfacePool *interfacePool = NULL;
1241     struct UsbInterfaceQueryPara interfaceQueryPara = {0};
1242     struct UsbSdkInterface *interfaceObj = NULL;
1243 
1244     if (interfaceHandle == NULL) {
1245         HDF_LOGE("%{public}s:%{public}d handle is null", __func__, __LINE__);
1246         return HDF_ERR_INVALID_PARAM;
1247     }
1248 
1249     ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1250     if (ifaceHdl->devHandle == NULL || ifaceHdl->devHandle->dev == NULL ||
1251         ifaceHdl->devHandle->dev->privateObject == NULL) {
1252         HDF_LOGE("%{public}s:%{public}d ifaceHdl is null", __func__, __LINE__);
1253         return HDF_ERR_INVALID_PARAM;
1254     }
1255 
1256     interfacePool = (struct UsbInterfacePool *)ifaceHdl->devHandle->dev->privateObject;
1257     /* Find interface object */
1258     interfaceQueryPara.type = USB_INTERFACE_INTERFACE_INDEX_TYPE;
1259     interfaceQueryPara.interfaceIndex = ifaceHdl->interfaceIndex;
1260     interfaceObj = IfFindInterfaceObj(interfacePool, interfaceQueryPara, false, NULL, false);
1261     if (interfaceObj == NULL) {
1262         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
1263         return HDF_ERR_BAD_FD;
1264     }
1265 
1266     OsalMutexLock(&interfacePool->interfaceLock);
1267     if (AdapterAtomicDec(&interfacePool->ioRefCount) == 0) {
1268         ret = UsbIoStop(interfacePool);
1269         if (ret != HDF_SUCCESS) {
1270             HDF_LOGE("%{public}s:%{public}d UsbIoStop failed, ret = %d", __func__, __LINE__, ret);
1271             goto OUT;
1272         }
1273     }
1274     AdapterAtomicDec(&interfaceObj->refCount);
1275     ifaceHdl->devHandle = NULL;
1276     RawUsbMemFree(ifaceHdl);
1277     OsalMutexUnlock(&interfacePool->interfaceLock);
1278 
1279     return HDF_SUCCESS;
1280 OUT:
1281     AdapterAtomicInc(&interfacePool->ioRefCount);
1282     OsalMutexUnlock(&interfacePool->interfaceLock);
1283     return ret;
1284 }
1285 
UsbSelectInterfaceSetting(const UsbInterfaceHandle * interfaceHandle,uint8_t settingIndex,struct UsbInterface ** interfaceObj)1286 int32_t UsbSelectInterfaceSetting(
1287     const UsbInterfaceHandle *interfaceHandle, uint8_t settingIndex, struct UsbInterface **interfaceObj)
1288 {
1289     struct UsbInterfaceHandleEntity *ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1290     struct UsbInterfacePool *interfacePool = NULL;
1291     struct UsbSdkInterface *interfacePos = NULL;
1292     struct UsbSdkInterface *interfaceTemp = NULL;
1293     struct UsbInterfaceQueryPara interfaceQueryPara = {0};
1294     int32_t ret;
1295 
1296     if (interfaceHandle == NULL || interfaceObj == NULL) {
1297         HDF_LOGE("%{public}s:%{public}d handle is null", __func__, __LINE__);
1298         return HDF_ERR_INVALID_PARAM;
1299     }
1300     interfacePool = (struct UsbInterfacePool *)ifaceHdl->devHandle->dev->privateObject;
1301     if (interfacePool == NULL) {
1302         HDF_LOGE("%{public}s:%{public}d interfacePool is null", __func__, __LINE__);
1303         return HDF_ERR_BAD_FD;
1304     }
1305 
1306     ret = RawSetInterfaceAltsetting(ifaceHdl->devHandle, ifaceHdl->interfaceIndex, settingIndex);
1307     if (ret != HDF_SUCCESS) {
1308         HDF_LOGE("%{public}s:%{public}d RawEnableInterface failed, ret = %d", __func__, __LINE__, ret);
1309         return ret;
1310     }
1311 
1312     OsalMutexLock(&interfacePool->interfaceLock);
1313     DLIST_FOR_EACH_ENTRY_SAFE(
1314         interfacePos, interfaceTemp, &interfacePool->interfaceList, struct UsbSdkInterface, interface.object.entry) {
1315         if (interfacePos->interface.info.interfaceIndex == ifaceHdl->interfaceIndex) {
1316             interfacePos->interface.info.curAltSetting = settingIndex;
1317         }
1318     }
1319     OsalMutexUnlock(&interfacePool->interfaceLock);
1320 
1321     interfaceQueryPara.type = USB_INTERFACE_INTERFACE_INDEX_TYPE;
1322     interfaceQueryPara.interfaceIndex = ifaceHdl->interfaceIndex;
1323     interfaceTemp = IfFindInterfaceObj(interfacePool, interfaceQueryPara, false, NULL, true);
1324     if (interfaceTemp == NULL) {
1325         HDF_LOGE("%{public}s:%{public}d interfaceTemp is null", __func__, __LINE__);
1326         return HDF_FAILURE;
1327     }
1328     interfaceTemp->session = interfacePool->session;
1329 
1330     *interfaceObj = &interfaceTemp->interface;
1331 
1332     return HDF_SUCCESS;
1333 }
1334 
UsbGetInterfaceSetting(const UsbInterfaceHandle * interfaceHandle,uint8_t * settingIndex)1335 int32_t UsbGetInterfaceSetting(const UsbInterfaceHandle *interfaceHandle, uint8_t *settingIndex)
1336 {
1337     if (interfaceHandle == NULL || settingIndex == NULL) {
1338         HDF_LOGE("%{public}s invalid param", __func__);
1339         return HDF_ERR_INVALID_PARAM;
1340     }
1341 
1342     struct UsbInterfaceHandleEntity *handleEntity = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1343     if (handleEntity->devHandle == NULL || handleEntity->devHandle->dev == NULL) {
1344         HDF_LOGE("%{public}s invalid handle entity", __func__);
1345         return HDF_ERR_INVALID_PARAM;
1346     }
1347     struct UsbInterfacePool *interfacePool = (struct UsbInterfacePool *)handleEntity->devHandle->dev->privateObject;
1348     if (interfacePool == NULL) {
1349         HDF_LOGE("%{public}s:%{public}d interfacePool is null", __func__, __LINE__);
1350         return HDF_ERR_BAD_FD;
1351     }
1352 
1353     struct UsbInterfaceQueryPara interfaceQueryPara = {0};
1354     interfaceQueryPara.type = USB_INTERFACE_INTERFACE_INDEX_TYPE;
1355     interfaceQueryPara.interfaceIndex = handleEntity->interfaceIndex;
1356     struct UsbSdkInterface *interfaceTemp = IfFindInterfaceObj(interfacePool, interfaceQueryPara, false, NULL, true);
1357     if (interfaceTemp == NULL) {
1358         HDF_LOGE("%{public}s:%{public}d interfaceTemp is null", __func__, __LINE__);
1359         return HDF_FAILURE;
1360     }
1361     *settingIndex = interfaceTemp->interface.info.curAltSetting;
1362     return HDF_SUCCESS;
1363 }
1364 
UsbGetPipeInfo(const UsbInterfaceHandle * interfaceHandle,uint8_t altSettingIndex,uint8_t pipeId,struct UsbPipeInfo * pipeInfo)1365 int32_t UsbGetPipeInfo(
1366     const UsbInterfaceHandle *interfaceHandle, uint8_t altSettingIndex, uint8_t pipeId, struct UsbPipeInfo *pipeInfo)
1367 {
1368     struct UsbInterfaceHandleEntity *ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1369     struct UsbInterfacePool *interfacePool = NULL;
1370     struct UsbInterfaceQueryPara interfaceQueryPara;
1371     struct UsbSdkInterface *interfaceObj = NULL;
1372     struct UsbPipeQueryPara pipeQueryPara;
1373     struct UsbPipe *pipeObj = NULL;
1374 
1375     if (interfaceHandle == NULL || pipeInfo == NULL || ifaceHdl == NULL || ifaceHdl->devHandle == NULL ||
1376         ifaceHdl->devHandle->dev == NULL) {
1377         HDF_LOGE("%{public}s:%{publid}d invalid parameter", __func__, __LINE__);
1378         return HDF_ERR_INVALID_PARAM;
1379     }
1380 
1381     /* Find interfacePool object */
1382     interfacePool = (struct UsbInterfacePool *)ifaceHdl->devHandle->dev->privateObject;
1383     if (interfacePool == NULL) {
1384         HDF_LOGE("%{public}s:%{public}d interfacePool is null", __func__, __LINE__);
1385         return HDF_ERR_BAD_FD;
1386     }
1387 
1388     /* Find interface object */
1389     interfaceQueryPara.type = USB_INTERFACE_ALT_SETTINGS_TYPE;
1390     interfaceQueryPara.interfaceIndex = ifaceHdl->interfaceIndex;
1391     interfaceQueryPara.altSettingId = altSettingIndex;
1392     interfaceObj = IfFindInterfaceObj(interfacePool, interfaceQueryPara, false, NULL, true);
1393     if (interfaceObj == NULL) {
1394         HDF_LOGE("%{public}s:%{public}d interfaceObj is null", __func__, __LINE__);
1395         return HDF_ERR_BAD_FD;
1396     }
1397 
1398     /* Find pipe object */
1399     pipeQueryPara.type = USB_PIPE_INDEX_TYPE;
1400     pipeQueryPara.pipeId = pipeId;
1401     pipeObj = IfFindPipeObj(interfaceObj, pipeQueryPara);
1402     if (pipeObj == NULL) {
1403         HDF_LOGE("%{public}s:%{public}d pipeObj is null", __func__, __LINE__);
1404         return HDF_ERR_BAD_FD;
1405     }
1406 
1407     *pipeInfo = pipeObj->info;
1408 
1409     return HDF_SUCCESS;
1410 }
1411 
UsbClearInterfaceHalt(const UsbInterfaceHandle * interfaceHandle,uint8_t pipeAddress)1412 int32_t UsbClearInterfaceHalt(const UsbInterfaceHandle *interfaceHandle, uint8_t pipeAddress)
1413 {
1414     struct UsbInterfaceHandleEntity *ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1415 
1416     if (ifaceHdl == NULL || ifaceHdl->devHandle == NULL) {
1417         HDF_LOGE("%{public}s:%{public}d handle is null", __func__, __LINE__);
1418         return HDF_ERR_INVALID_PARAM;
1419     }
1420 
1421     return RawClearHalt(ifaceHdl->devHandle, pipeAddress);
1422 }
1423 
UsbAllocRequest(const UsbInterfaceHandle * interfaceHandle,int32_t isoPackets,int32_t length)1424 struct UsbRequest *UsbAllocRequest(const UsbInterfaceHandle *interfaceHandle, int32_t isoPackets, int32_t length)
1425 {
1426     struct UsbInterfaceHandleEntity *ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1427     struct UsbIfRequest *requestObj = NULL;
1428     struct UsbHostRequest *hostRequest = NULL;
1429 
1430     if (ifaceHdl == NULL || ifaceHdl->devHandle == NULL) {
1431         HDF_LOGE("%{public}s: handle is null", __func__);
1432         return NULL;
1433     }
1434 
1435     requestObj = (struct UsbIfRequest *)RawUsbMemCalloc(sizeof(struct UsbIfRequest));
1436     if (requestObj == NULL) {
1437         HDF_LOGE("%{public}s: RawUsbMemCalloc UsbRequest error", __func__);
1438         return NULL;
1439     }
1440 
1441     hostRequest = RawAllocRequest(ifaceHdl->devHandle, isoPackets, length);
1442     if (hostRequest == NULL) {
1443         HDF_LOGE("%{public}s: RawAllocRequest error", __func__);
1444         RawUsbMemFree(requestObj);
1445         return NULL;
1446     }
1447     hostRequest->devHandle = ifaceHdl->devHandle;
1448 
1449     ++g_usbRequestObjectId;
1450     g_usbRequestObjectId %= MAX_OBJECT_ID;
1451     requestObj->request.object.objectId = g_usbRequestObjectId;
1452     DListHeadInit(&requestObj->request.object.entry);
1453     requestObj->request.compInfo.type = USB_REQUEST_TYPE_INVALID;
1454     requestObj->request.compInfo.buffer = hostRequest->buffer;
1455     requestObj->request.compInfo.length = (uint32_t)hostRequest->length;
1456     requestObj->hostRequest = hostRequest;
1457     requestObj->isSyncReq = false;
1458     hostRequest->privateObj = requestObj;
1459 
1460     return (struct UsbRequest *)requestObj;
1461 }
1462 
UsbAllocRequestByMmap(const UsbInterfaceHandle * interfaceHandle,int32_t isoPackets,int32_t length)1463 struct UsbRequest *UsbAllocRequestByMmap(const UsbInterfaceHandle *interfaceHandle, int32_t isoPackets, int32_t length)
1464 {
1465     struct UsbInterfaceHandleEntity *ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1466     struct UsbIfRequest *requestObj = NULL;
1467     struct UsbHostRequest *hostRequest = NULL;
1468 
1469     if (ifaceHdl == NULL || ifaceHdl->devHandle == NULL) {
1470         HDF_LOGE("%{public}s: handle is null", __func__);
1471         return NULL;
1472     }
1473 
1474     requestObj = (struct UsbIfRequest *)RawUsbMemCalloc(sizeof(struct UsbIfRequest));
1475     if (requestObj == NULL) {
1476         HDF_LOGE("%{public}s: RawUsbMemCalloc UsbRequest error", __func__);
1477         return NULL;
1478     }
1479 
1480     hostRequest = RawAllocRequestByMmap(ifaceHdl->devHandle, isoPackets, length);
1481     if (hostRequest == NULL) {
1482         HDF_LOGE("%{public}s: RawAllocRequest error", __func__);
1483         RawUsbMemFree(requestObj);
1484         return NULL;
1485     }
1486     hostRequest->devHandle = ifaceHdl->devHandle;
1487 
1488     ++g_usbRequestObjectId;
1489     g_usbRequestObjectId %= MAX_OBJECT_ID;
1490     requestObj->request.object.objectId = g_usbRequestObjectId;
1491     DListHeadInit(&requestObj->request.object.entry);
1492     requestObj->request.compInfo.type = USB_REQUEST_TYPE_INVALID;
1493     requestObj->request.compInfo.buffer = hostRequest->buffer;
1494     requestObj->request.compInfo.length = (uint32_t)hostRequest->length;
1495     requestObj->hostRequest = hostRequest;
1496     requestObj->isSyncReq = false;
1497     hostRequest->privateObj = requestObj;
1498 
1499     return (struct UsbRequest *)requestObj;
1500 }
1501 
UsbFreeRequest(const struct UsbRequest * request)1502 int32_t UsbFreeRequest(const struct UsbRequest *request)
1503 {
1504     struct UsbHostRequest *hostRequest = NULL;
1505     struct UsbIfRequest *requestObj = (struct UsbIfRequest *)request;
1506     int32_t ret;
1507 
1508     if (requestObj == NULL) {
1509         HDF_LOGE("%{public}s:%{public}d request is null", __func__, __LINE__);
1510         return HDF_ERR_INVALID_PARAM;
1511     }
1512 
1513     hostRequest = requestObj->hostRequest;
1514     if (hostRequest == NULL) {
1515         HDF_LOGE("%{public}s:%{public}d hostRequest is null", __func__, __LINE__);
1516         return HDF_ERR_INVALID_PARAM;
1517     }
1518 
1519     ret = RawFreeRequest(hostRequest);
1520     if (ret != HDF_SUCCESS) {
1521         HDF_LOGE("%{public}s:%{public}d RawFreeRequest failed", __func__, __LINE__);
1522         return ret;
1523     }
1524 
1525     RawUsbMemFree(requestObj);
1526 
1527     return ret;
1528 }
1529 
UsbFreeRequestByMmap(const struct UsbRequest * request)1530 int32_t UsbFreeRequestByMmap(const struct UsbRequest *request)
1531 {
1532     struct UsbHostRequest *hostRequest = NULL;
1533     struct UsbIfRequest *requestObj = (struct UsbIfRequest *)request;
1534     int32_t ret;
1535 
1536     if (requestObj == NULL) {
1537         HDF_LOGE("%{public}s:%{public}d request is null", __func__, __LINE__);
1538         return HDF_ERR_INVALID_PARAM;
1539     }
1540 
1541     hostRequest = requestObj->hostRequest;
1542     if (hostRequest == NULL) {
1543         HDF_LOGE("%{public}s:%{public}d hostRequest is null", __func__, __LINE__);
1544         return HDF_ERR_INVALID_PARAM;
1545     }
1546 
1547     ret = RawFreeRequestByMmap(hostRequest);
1548     if (ret != HDF_SUCCESS) {
1549         HDF_LOGE("%{public}s:%{public}d RawFreeRequest failed", __func__, __LINE__);
1550         return ret;
1551     }
1552 
1553     RawUsbMemFree(requestObj);
1554 
1555     return ret;
1556 }
1557 
UsbSubmitRequestAsync(const struct UsbRequest * const request)1558 int32_t UsbSubmitRequestAsync(const struct UsbRequest * const request)
1559 {
1560     if (request == NULL) {
1561         HDF_LOGE("%{public}s:%{public}d request is null", __func__, __LINE__);
1562         return HDF_ERR_INVALID_PARAM;
1563     }
1564 
1565     struct UsbIfRequest *requestObj = (struct UsbIfRequest *)request;
1566     requestObj->isSyncReq = false;
1567     if (memset_s((void *)&request->compInfo, sizeof(request->compInfo), 0, sizeof(request->compInfo)) != EOK) {
1568         HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
1569         return HDF_FAILURE;
1570     }
1571     return IfSubmitRequestToQueue(requestObj);
1572 }
1573 
UsbFillRequest(const struct UsbRequest * request,const UsbInterfaceHandle * interfaceHandle,const struct UsbRequestParams * params)1574 int32_t UsbFillRequest(
1575     const struct UsbRequest *request, const UsbInterfaceHandle *interfaceHandle, const struct UsbRequestParams *params)
1576 {
1577     struct UsbInterfaceHandleEntity *ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1578     struct UsbIfRequest *requestObj = (struct UsbIfRequest *)request;
1579     struct UsbHostRequest *hostRequest = NULL;
1580     UsbPipeType pipeType;
1581     UsbRequestDirection directon;
1582     int32_t ret;
1583 
1584     if (requestObj == NULL || params == NULL || ifaceHdl == NULL || ifaceHdl->devHandle == NULL) {
1585         HDF_LOGE("%{public}s:%{public}d params or request is null", __func__, __LINE__);
1586         return HDF_ERR_INVALID_PARAM;
1587     }
1588 
1589     hostRequest = requestObj->hostRequest;
1590     if (hostRequest == NULL) {
1591         HDF_LOGE("%{public}s:%{public}d hostRequest is null", __func__, __LINE__);
1592         return HDF_ERR_INVALID_PARAM;
1593     }
1594 
1595     uint8_t interfaceId = ifaceHdl->interfaceIndex;
1596     if (params->requestType == USB_REQUEST_PARAMS_CTRL_TYPE) {
1597         interfaceId = USB_CTRL_INTERFACE_ID;
1598     }
1599 
1600     ret = IfGetRequestPipeType(ifaceHdl->devHandle, interfaceId, params->pipeId, &pipeType);
1601     if (ret != HDF_SUCCESS) {
1602         HDF_LOGE("%{public}s:%{public}d IfGetRequestPipeType error, ret = %d", __func__, __LINE__, ret);
1603         return ret;
1604     }
1605 
1606     ret = IfFillRequestByPipeType(requestObj, pipeType, hostRequest, ifaceHdl->devHandle, params);
1607     if (params->requestType == USB_REQUEST_PARAMS_DATA_TYPE) {
1608         directon = (params->pipeAddress >> USB_DIR_OFFSET) & 0x01;
1609         if (directon == USB_REQUEST_DIR_TO_DEVICE) {
1610             requestObj->request.compInfo.type = USB_REQUEST_TYPE_PIPE_WRITE;
1611         } else if (directon == USB_REQUEST_DIR_FROM_DEVICE) {
1612             requestObj->request.compInfo.type = USB_REQUEST_TYPE_PIPE_READ;
1613         }
1614     }
1615 
1616     return ret;
1617 }
1618 
UsbFillRequestByMmap(const struct UsbRequest * request,const UsbInterfaceHandle * interfaceHandle,const struct UsbRequestParams * params)1619 int32_t UsbFillRequestByMmap(
1620     const struct UsbRequest *request, const UsbInterfaceHandle *interfaceHandle, const struct UsbRequestParams *params)
1621 {
1622     struct UsbInterfaceHandleEntity *ifaceHdl = (struct UsbInterfaceHandleEntity *)interfaceHandle;
1623     struct UsbIfRequest *requestObj = (struct UsbIfRequest *)request;
1624     struct UsbHostRequest *hostRequest = NULL;
1625     UsbPipeType pipeType;
1626     UsbRequestDirection directon;
1627     int32_t ret;
1628 
1629     if (requestObj == NULL || params == NULL || ifaceHdl == NULL || ifaceHdl->devHandle == NULL) {
1630         HDF_LOGE("%{public}s:%{public}d params or request is null", __func__, __LINE__);
1631         return HDF_ERR_INVALID_PARAM;
1632     }
1633 
1634     hostRequest = requestObj->hostRequest;
1635     if (hostRequest == NULL) {
1636         HDF_LOGE("%{public}s:%{public}d hostRequest is null", __func__, __LINE__);
1637         return HDF_ERR_INVALID_PARAM;
1638     }
1639 
1640     uint8_t interfaceId = ifaceHdl->interfaceIndex;
1641     if (params->requestType == USB_REQUEST_PARAMS_CTRL_TYPE) {
1642         interfaceId = USB_CTRL_INTERFACE_ID;
1643     }
1644 
1645     ret = IfGetRequestPipeType(ifaceHdl->devHandle, interfaceId, params->pipeId, &pipeType);
1646     if (ret != HDF_SUCCESS) {
1647         HDF_LOGE("%{public}s:%{public}d IfGetRequestPipeType error, ret = %d", __func__, __LINE__, ret);
1648         return ret;
1649     }
1650 
1651     ret = IfFillRequestByPipeTypeByMmap(requestObj, pipeType, hostRequest, ifaceHdl->devHandle, params);
1652     if (params->requestType == USB_REQUEST_PARAMS_DATA_TYPE) {
1653         directon = (params->pipeAddress >> USB_DIR_OFFSET) & 0x01;
1654         if (directon == USB_REQUEST_DIR_TO_DEVICE) {
1655             requestObj->request.compInfo.type = USB_REQUEST_TYPE_PIPE_WRITE;
1656         } else if (directon == USB_REQUEST_DIR_FROM_DEVICE) {
1657             requestObj->request.compInfo.type = USB_REQUEST_TYPE_PIPE_READ;
1658         }
1659     }
1660 
1661     return ret;
1662 }
1663 
UsbCancelRequest(const struct UsbRequest * request)1664 int32_t UsbCancelRequest(const struct UsbRequest *request)
1665 {
1666     int32_t ret;
1667     struct UsbHostRequest *hostRequest = NULL;
1668     struct UsbIfRequest *requestObj = (struct UsbIfRequest *)request;
1669 
1670     if (requestObj == NULL || requestObj->hostRequest == NULL) {
1671         HDF_LOGE("%{public}s:%{public}d request is null", __func__, __LINE__);
1672         return HDF_ERR_INVALID_PARAM;
1673     }
1674 
1675     hostRequest = requestObj->hostRequest;
1676     ret = RawCancelRequest(hostRequest);
1677     if (ret != HDF_SUCCESS) {
1678         HDF_LOGE("%{public}s:%{public}d RawCancelRequest failed, ret = %d ", __func__, __LINE__, ret);
1679         return ret;
1680     }
1681 
1682     requestObj->request.compInfo.status = USB_REQUEST_CANCELLED;
1683 
1684     return HDF_SUCCESS;
1685 }
1686 
UsbSubmitRequestSync(const struct UsbRequest * request)1687 int32_t UsbSubmitRequestSync(const struct UsbRequest *request)
1688 {
1689     int32_t ret;
1690     uint32_t waitTime;
1691     struct UsbIfRequest *requestObj = (struct UsbIfRequest *)request;
1692 
1693     if (request == NULL || requestObj->hostRequest == NULL) {
1694         HDF_LOGE("%{public}s:%{public}d request is null", __func__, __LINE__);
1695         return HDF_ERR_INVALID_PARAM;
1696     }
1697 
1698     /* Init request semaphore */
1699     if (OsalSemInit(&requestObj->hostRequest->sem, 0) != HDF_SUCCESS) {
1700         HDF_LOGE("%{public}s:%{public}d OsalSemInit failed!", __func__, __LINE__);
1701         return HDF_ERR_IO;
1702     }
1703     requestObj->request.compInfo.status = USB_REQUEST_COMPLETED;
1704     if (requestObj->hostRequest->timeout == USB_RAW_REQUEST_TIME_ZERO_MS) {
1705         waitTime = HDF_WAIT_FOREVER;
1706     } else {
1707         waitTime = requestObj->hostRequest->timeout;
1708     }
1709 
1710     requestObj->isSyncReq = true;
1711     ret = IfSubmitRequestToQueue(requestObj);
1712     if (ret != HDF_SUCCESS) {
1713         goto OUT;
1714     }
1715 
1716     ret = OsalSemWait(&requestObj->hostRequest->sem, waitTime);
1717     if (ret == HDF_ERR_TIMEOUT) {
1718         UsbCancelRequest(&requestObj->request);
1719         if (OsalSemWait(&requestObj->hostRequest->sem, waitTime) == HDF_ERR_TIMEOUT) {
1720             HDF_LOGE("%{public}s:%{public}d UsbCancelRequest sem wait timeout!", __func__, __LINE__);
1721         }
1722         requestObj->request.compInfo.status = USB_REQUEST_TIMEOUT;
1723     } else if (ret != HDF_SUCCESS) {
1724         HDF_LOGE("%{public}s:%{public}d OsalSemWait failed, ret = %d", __func__, __LINE__, ret);
1725     }
1726 
1727 OUT:
1728     OsalSemDestroy(&requestObj->hostRequest->sem);
1729     return ret;
1730 }
1731 
UsbMemTestTrigger(bool enable)1732 int32_t UsbMemTestTrigger(bool enable)
1733 {
1734     return RawUsbMemTestTrigger(enable);
1735 }
1736