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