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