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