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