diff --git a/usb/gadget/UsbGadget.cpp b/usb/gadget/UsbGadget.cpp index 5c0021fe..361cefd7 100644 --- a/usb/gadget/UsbGadget.cpp +++ b/usb/gadget/UsbGadget.cpp @@ -33,13 +33,55 @@ namespace gadget { namespace V1_2 { namespace implementation { -UsbGadget::UsbGadget() { +UsbGadget::UsbGadget() : mGadgetIrqPath("") { if (access(OS_DESC_PATH, R_OK) != 0) { ALOGE("configfs setup not done yet"); abort(); } } +V1_0::Status UsbGadget::getUsbGadgetIrqPath() { + std::string irqs; + size_t read_pos = 0; + size_t found_pos = 0; + + if (!ReadFileToString(kProcInterruptsPath, &irqs)) { + ALOGE("cannot read all interrupts"); + return Status::ERROR; + } + + while (true) { + found_pos = irqs.find_first_of("\n", read_pos); + if (found_pos == std::string::npos) { + ALOGI("the string of all interrupts is unexpected"); + return Status::ERROR; + } + + std::string single_irq = irqs.substr(read_pos, found_pos - read_pos); + + if (single_irq.find("dwc3", 0) != std::string::npos) { + unsigned int dwc3_irq_number; + size_t dwc3_pos = single_irq.find_first_of(":"); + if (!ParseUint(single_irq.substr(0, dwc3_pos), &dwc3_irq_number)) { + ALOGI("unknown IRQ strings"); + return Status::ERROR; + } + + mGadgetIrqPath = kProcIrqPath + single_irq.substr(0, dwc3_pos) + kSmpAffinityList; + break; + } + + if (found_pos == irqs.npos) { + ALOGI("USB gadget doesn't start"); + return Status::ERROR; + } + + read_pos = found_pos + 1; + } + + return Status::SUCCESS; +} + void currentFunctionsAppliedCallback(bool functionsApplied, void *payload) { UsbGadget *gadget = (UsbGadget *)payload; gadget->mCurrentUsbFunctionsApplied = functionsApplied; @@ -346,6 +388,10 @@ Return UsbGadget::setCurrentUsbFunctions(uint64_t functions, mCurrentUsbFunctions = functions; mCurrentUsbFunctionsApplied = false; + // Get the gadget IRQ number before tearDownGadget() + if (mGadgetIrqPath.empty()) + getUsbGadgetIrqPath(); + // Unlink the gadget and stop the monitor if running. V1_0::Status status = tearDownGadget(); if (status != Status::SUCCESS) { @@ -378,9 +424,15 @@ Return UsbGadget::setCurrentUsbFunctions(uint64_t functions, } if (functions & GadgetFunction::NCM) { - SetProperty("vendor.usb.dwc3_irq", "big"); + if (!mGadgetIrqPath.empty()) { + if (!WriteStringToFile(BIG_CORE, mGadgetIrqPath)) + ALOGI("Cannot move gadget IRQ to big core, path:%s", mGadgetIrqPath.c_str()); + } } else { - SetProperty("vendor.usb.dwc3_irq", "medium"); + if (!mGadgetIrqPath.empty()) { + if (!WriteStringToFile(MEDIUM_CORE, mGadgetIrqPath)) + ALOGI("Cannot move gadget IRQ to medium core, path:%s", mGadgetIrqPath.c_str()); + } } ALOGI("Usb Gadget setcurrent functions called successfully"); diff --git a/usb/gadget/UsbGadget.h b/usb/gadget/UsbGadget.h index 2669f8da..f89476d8 100644 --- a/usb/gadget/UsbGadget.h +++ b/usb/gadget/UsbGadget.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,7 @@ namespace implementation { using ::android::sp; using ::android::base::GetProperty; using ::android::base::SetProperty; +using ::android::base::ParseUint; using ::android::base::unique_fd; using ::android::base::ReadFileToString; using ::android::base::Trim; @@ -71,6 +73,9 @@ using ::android::hardware::usb::gadget::V1_2::GadgetFunction; using ::std::string; constexpr char kGadgetName[] = "11110000.dwc3"; +constexpr char kProcInterruptsPath[] = "/proc/interrupts"; +constexpr char kProcIrqPath[] = "/proc/irq/"; +constexpr char kSmpAffinityList[] = "/smp_affinity_list"; #ifndef UDC_PATH #define UDC_PATH "/sys/class/udc/11110000.dwc3/" #endif @@ -78,11 +83,15 @@ static MonitorFfs monitorFfs(kGadgetName); #define SPEED_PATH UDC_PATH "current_speed" +#define BIG_CORE "6" +#define MEDIUM_CORE "4" + struct UsbGadget : public IUsbGadget { UsbGadget(); // Makes sure that only one request is processed at a time. std::mutex mLockSetCurrentFunction; + std::string mGadgetIrqPath; uint64_t mCurrentUsbFunctions; bool mCurrentUsbFunctionsApplied; UsbSpeed mUsbSpeed; @@ -99,6 +108,7 @@ struct UsbGadget : public IUsbGadget { private: Status tearDownGadget(); + Status getUsbGadgetIrqPath(); Status setupFunctions(uint64_t functions, const sp &callback, uint64_t timeout); };