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