diff --git a/src/daemon/daemon_usb.cpp b/src/daemon/daemon_usb.cpp index b32a8b7207082d37121ad44e03633fb7c3719448..6c51b46e86e2a36edcaacada3aa5b7f20f8feda5 100644 --- a/src/daemon/daemon_usb.cpp +++ b/src/daemon/daemon_usb.cpp @@ -44,14 +44,42 @@ void HdcDaemonUSB::Stop() WRITE_LOG(LOG_DEBUG, "HdcDaemonUSB Stop free main session finish"); } +string HdcDaemonUSB::GetDevPath(const std::string& path) { + DIR *dir = ::opendir(path.c_str()); + if (dir == nullptr) { + WRITE_LOG(LOG_WARN, "%s: cannot open devpath: errno: %d", path.c_str(), errno); + return ""; + } + + string res = USB_FFS_BASE; + string node; + int count = 0; + struct dirent *entry = nullptr; + while ((entry = ::readdir(dir))) { + if (*entry->d_name == '.') { + continue; + } + node = entry->d_name; + ++count; + } + if (count > 1) { + res += "hdc"; + } else { + res += node; + } + ::closedir(dir); + return res; +} + int HdcDaemonUSB::Initial() { // 4.4 Kit Kat |19, 20 |3.10 // after Linux-3.8,kernel switch to the USB Function FS // Implement USB hdc function in user space WRITE_LOG(LOG_DEBUG, "HdcDaemonUSB init"); - if (access(USB_FFS_HDC_EP0, F_OK) != 0) { - WRITE_LOG(LOG_DEBUG, "Just support usb-ffs, must kernel3.8+ and enable usb-ffs, usbmod disable"); + basePath = GetDevPath(USB_FFS_BASE); + if (access((basePath + "/ep0").c_str(), F_OK) != 0) { + WRITE_LOG(LOG_DEBUG, "Only support usb-ffs, make sure kernel3.8+ and usb-ffs enabled, usbmode disabled"); return -1; } const uint16_t usbFfsScanInterval = 1500; @@ -73,29 +101,31 @@ int HdcDaemonUSB::ConnectEPPoint(HUSB hUSB) // which can be found for USB devices. Do not send initialization to the EP0 control port, the USB // device will not be initialized by Host WRITE_LOG(LOG_DEBUG, "Begin send to control(EP0) for usb descriptor init"); - if ((controlEp = open(USB_FFS_HDC_EP0, O_RDWR)) < 0) { - WRITE_LOG(LOG_WARN, "%s: cannot open control endpoint: errno=%d", USB_FFS_HDC_EP0, errno); + string ep0Path = basePath + "/ep0"; + if ((controlEp = open(ep0Path.c_str(), O_RDWR)) < 0) { + WRITE_LOG(LOG_WARN, "%s: cannot open control endpoint: errno=%d", ep0Path.c_str(), errno); break; } if (write(controlEp, &USB_FFS_DESC, sizeof(USB_FFS_DESC)) < 0) { - WRITE_LOG(LOG_WARN, "%s: write ffs configs failed: errno=%d", USB_FFS_HDC_EP0, errno); + WRITE_LOG(LOG_WARN, "%s: write ffs configs failed: errno=%d", ep0Path.c_str(), errno); break; } if (write(controlEp, &USB_FFS_VALUE, sizeof(USB_FFS_VALUE)) < 0) { - WRITE_LOG(LOG_WARN, "%s: write USB_FFS_VALUE failed: errno=%d", USB_FFS_HDC_EP0, errno); + WRITE_LOG(LOG_WARN, "%s: write USB_FFS_VALUE failed: errno=%d", ep0Path.c_str(), errno); break; } // active usbrc,Send USB initialization singal Base::SetHdcProperty("sys.usb.ffs.ready", "1"); WRITE_LOG(LOG_DEBUG, "ConnectEPPoint ctrl init finish, set usb-ffs ready"); } - - if ((hUSB->bulkOut = open(USB_FFS_HDC_OUT, O_RDWR)) < 0) { - WRITE_LOG(LOG_WARN, "%s: cannot open bulk-out ep: errno=%d", USB_FFS_HDC_OUT, errno); + string outPath = basePath + "/ep1"; + if ((hUSB->bulkOut = open(outPath.c_str(), O_RDWR)) < 0) { + WRITE_LOG(LOG_WARN, "%s: cannot open bulk-out ep: errno=%d", outPath.c_str(), errno); break; } - if ((hUSB->bulkIn = open(USB_FFS_HDC_IN, O_RDWR)) < 0) { - WRITE_LOG(LOG_WARN, "%s: cannot open bulk-in ep: errno=%d", USB_FFS_HDC_IN, errno); + string inPath = basePath + "/ep2"; + if ((hUSB->bulkIn = open(inPath.c_str(), O_RDWR)) < 0) { + WRITE_LOG(LOG_WARN, "%s: cannot open bulk-in ep: errno=%d", inPath.c_str(), errno); break; } // cannot open with O_CLOEXEC, must fcntl diff --git a/src/daemon/daemon_usb.h b/src/daemon/daemon_usb.h index a79c04b1c02506a38f969eca081bb516c63c843d..26abfd957feff841351f67dce33b4ac1e885ff23 100644 --- a/src/daemon/daemon_usb.h +++ b/src/daemon/daemon_usb.h @@ -40,6 +40,7 @@ private: int DispatchToWorkThread(const uint32_t sessionId, uint8_t *readBuf, int readBytes); bool AvailablePacket(uint8_t *ioBuf, uint32_t *sessionId); void CloseEndpoint(HUSB hUSB, bool closeCtrlEp = false); + string GetDevPath(const string& path); bool ReadyForWorkThread(HSession hSession); int LoopUSBRead(HUSB hUSB); HSession PrepareNewSession(uint32_t sessionId, uint8_t *pRecvBuf, int recvBytesIO); @@ -48,6 +49,7 @@ private: int CloseBulkEp(bool bulkInOut, int bulkFd, uv_loop_t *loop); HdcUSB usbHandle; + string basePath; // usb device's base path uint32_t currentSessionId = 0; // USB mode,limit only one session std::atomic ref = 0; uv_timer_t checkEP; // server-use diff --git a/src/daemon/usb_ffs.h b/src/daemon/usb_ffs.h index d3909cda77679fc3b6cef2a5a38d76ca12a9f0d1..a520560c91794d52a4116565cfffeafc246cbd6e 100644 --- a/src/daemon/usb_ffs.h +++ b/src/daemon/usb_ffs.h @@ -29,9 +29,7 @@ constexpr auto HDC_SUBCLASS = 0x50; constexpr auto HDC_FSPKT_SIZE_MAX = 64; constexpr auto HDC_HSPKT_SIZE_MAX = 512; constexpr uint16_t HDC_SSPKT_SIZE_MAX = 1024; -constexpr auto USB_FFS_HDC_EP0 = "/dev/usb-ffs/hdc/ep0"; -constexpr auto USB_FFS_HDC_OUT = "/dev/usb-ffs/hdc/ep1"; -constexpr auto USB_FFS_HDC_IN = "/dev/usb-ffs/hdc/ep2"; +constexpr auto USB_FFS_BASE = "/dev/usb-ffs/"; constexpr auto HDC_USBTF_DEV = 0x01; constexpr auto HDC_USBTF_CFG = 0x02; constexpr auto HDC_USBTF_STR = 0x03;