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