diff --git a/usb/ddk/host/include/usb_interface_pool.h b/usb/ddk/host/include/usb_interface_pool.h index 122cbf4b467fe69eac491e96cb068b180fc526b5..356a86585ebe15ac92647b690643dc34fcaf739a 100644 --- a/usb/ddk/host/include/usb_interface_pool.h +++ b/usb/ddk/host/include/usb_interface_pool.h @@ -80,6 +80,7 @@ struct UsbInterfacePool { UsbPoolProcessStatusType ioRecvProcessStopStatus; struct OsalMutex ioStopLock; struct UsbDevice *device; + struct OsalSem ioSem; }; struct UsbPipeQueryPara { diff --git a/usb/ddk/host/src/usb_interface_pool.c b/usb/ddk/host/src/usb_interface_pool.c index 1b1d16d706d1d4b17f068a2f69e6f603da51a7cc..bfb4cf13cdda5a4f050b771ed583177572ba05fb 100644 --- a/usb/ddk/host/src/usb_interface_pool.c +++ b/usb/ddk/host/src/usb_interface_pool.c @@ -1402,15 +1402,18 @@ int32_t UsbCloseCtlProcess(const UsbInterfaceHandle *interfaceHandle) } OsalMutexLock(&interfacePool->interfaceLock); - if (OsalAtomicRead(&interfacePool->ioRefCount) == 1) { + int32_t refCnt = OsalAtomicRead(&interfacePool->ioRefCount); + if (refCnt == 1) { ret = UsbIoRecvProcessStop(interfacePool); if (ret != HDF_SUCCESS) { HDF_LOGE("%{public}s:%{public}d UsbIoStop failed, ret = %{public}d", __func__, __LINE__, ret); OsalMutexUnlock(&interfacePool->interfaceLock); return ret; } + } else { + HDF_LOGD("%{public}s:%{public}d UsbIoStop ref count = %{public}d", __func__, __LINE__, refCnt); } - + OsalMutexUnlock(&interfacePool->interfaceLock); return HDF_SUCCESS; } diff --git a/usb/ddk/host/src/usb_io_manage.c b/usb/ddk/host/src/usb_io_manage.c index 1d2031b958fb6593ab113f81b18add3df87459ad..96dc0932f318191fe0572373dcda58a1d57c54e6 100644 --- a/usb/ddk/host/src/usb_io_manage.c +++ b/usb/ddk/host/src/usb_io_manage.c @@ -85,6 +85,7 @@ static int32_t IoSendProcess(const void *interfacePoolArg) continue; } /* Submit success */ + OsalSemPost(&interfacePool->ioSem); break; } @@ -111,6 +112,7 @@ static int32_t IoAsyncReceiveProcess(const void *interfacePoolArg) HDF_LOGE("%{public}s:%{public}d RawRegisterSignal error", __func__, __LINE__); } + HDF_LOGD("%{public}s, enter recv thread", __func__); while (true) { if (!interfacePool->ioProcessTid) { interfacePool->ioProcessTid = RawGetTid(); @@ -128,12 +130,17 @@ static int32_t IoAsyncReceiveProcess(const void *interfacePoolArg) continue; } + int32_t ret = OsalSemWait(&interfacePool->ioSem, HDF_WAIT_FOREVER); + if (ret != HDF_SUCCESS) { + HDF_LOGE("sem wait failed: %{public}d", ret); + } + if (interfacePool->ioProcessStopStatus != USB_POOL_PROCESS_RUNNING || interfacePool->ioRecvProcessStopStatus != USB_POOL_PROCESS_RUNNING) { break; } - int32_t ret = RawHandleRequest(interfacePool->device->devHandle); + ret = RawHandleRequest(interfacePool->device->devHandle); if (ret < 0) { HDF_LOGE("%{public}s RawHandleRequest failed ret: %{public}d", __func__, ret); OsalMSleep(USB_IO_SLEEP_MS_TIME); @@ -255,6 +262,7 @@ HDF_STATUS UsbIoStart(struct UsbInterfacePool *interfacePool) OsalMutexLock(&interfacePool->ioStopLock); interfacePool->ioProcessStopStatus = USB_POOL_PROCESS_RUNNING; interfacePool->ioRecvProcessStopStatus = USB_POOL_PROCESS_RUNNING; + OsalSemInit(&interfacePool->ioSem, 0); OsalMutexUnlock(&interfacePool->ioStopLock); /* create IoSendProcess thread */ @@ -285,8 +293,7 @@ HDF_STATUS UsbIoStart(struct UsbInterfacePool *interfacePool) threadCfg.priority = OSAL_THREAD_PRI_DEFAULT; threadCfg.stackSize = USB_IO_RECEIVE_PROCESS_STACK_SIZE; - ret = OsalThreadCreate( - &interfacePool->ioAsyncReceiveProcess, (OsalThreadEntry)IoAsyncReceiveProcess, (void *)interfacePool); + ret = OsalThreadCreate(&interfacePool->ioAsyncReceiveProcess, (void *)IoAsyncReceiveProcess, (void *)interfacePool); if (ret != HDF_SUCCESS) { HDF_LOGE("%{public}s:%{public}d OsalThreadCreate failed, ret=%{public}d ", __func__, __LINE__, ret); goto ERR_DESTROY_SEND; @@ -332,6 +339,7 @@ HDF_STATUS UsbIoStop(struct UsbInterfacePool *interfacePool) if ((interfacePool->ioProcessStopStatus != USB_POOL_PROCESS_STOPED)) { OsalMutexLock(&interfacePool->ioStopLock); interfacePool->ioProcessStopStatus = USB_POOL_PROCESS_STOP; + OsalSemPost(&interfacePool->ioSem); OsalSemPost(&interfacePool->submitRequestQueue.sem); OsalMutexUnlock(&interfacePool->ioStopLock); @@ -359,6 +367,8 @@ HDF_STATUS UsbIoStop(struct UsbInterfacePool *interfacePool) ret = OsalThreadDestroy(&interfacePool->ioAsyncReceiveProcess); if (ret != HDF_SUCCESS) { HDF_LOGE("%{public}s:%{public}d OsalThreadDestroy failed, ret=%{public}d ", __func__, __LINE__, ret); + } else { + OsalSemDestroy(&interfacePool->ioSem); } return ret; } diff --git a/usb/hdi_service/src/usb_impl.cpp b/usb/hdi_service/src/usb_impl.cpp index f8e907af4e24ef3698a6c89dff5ade33e2713a3b..bc7892fad4237ed4149a603b53e9e513a96169c9 100644 --- a/usb/hdi_service/src/usb_impl.cpp +++ b/usb/hdi_service/src/usb_impl.cpp @@ -44,7 +44,6 @@ constexpr uint16_t ENGLISH_US_LANGUAGE_ID = 0x409; constexpr uint32_t FUNCTION_VALUE_MAX_LEN = 32; constexpr uint8_t USB_PARAM_REQTYPE = 128; constexpr uint8_t USB_PARAM_STAND_REQTYPE = 0; -constexpr int32_t USB_TRANSFER_TIMEOUT = 1000; namespace OHOS { namespace HDI { namespace Usb { @@ -1099,13 +1098,6 @@ int32_t UsbImpl::CloseDevice(const UsbDev &dev) int32_t ret = 0; if (port->ctrDevHandle != nullptr) { RawUsbCloseCtlProcess(port->ctrDevHandle); - uint8_t activeConfig = 0; - uint16_t length = 1; - UsbControlParams controlParams; - MakeGetActiveUsbControlParams(&controlParams, &activeConfig, length, 0, 0); - UsbControlTransferEx(port, &controlParams, USB_TRANSFER_TIMEOUT); - HDF_LOGD("%{public}s:start closeInterface,busNum: %{public}d, devAddr: %{public}d ", - __func__, dev.busNum, dev.devAddr); ret = UsbCloseInterface(port->ctrDevHandle, true); if (ret != HDF_SUCCESS) { HDF_LOGE("%{public}s:usbCloseInterface ctrDevHandle failed.", __func__);