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