Skip to content

Commit

Permalink
Add USB Gadget HAL 1.2 support
Browse files Browse the repository at this point in the history
Add a new API to query current USB connection speed

Bug: 181938674
Test: Pass USB Gadget V1.2 HIDL tests
Signed-off-by: Albert Wang <[email protected]>
Change-Id: I2cbdb0d73eb5c586524ef4a4083940e07269241b
  • Loading branch information
Albert Wang committed Mar 23, 2021
1 parent b386388 commit c991432
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
3 changes: 2 additions & 1 deletion usb/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cc_binary {
init_rc: ["[email protected]"],
vintf_fragments: [
"[email protected]",
"android.hardware.usb.gadget@1.1-service.sunfish.xml",
"android.hardware.usb.gadget@1.2-service.sunfish.xml",
],
srcs: ["service.cpp", "Usb.cpp", "UsbGadget.cpp"],
shared_libs: [
Expand All @@ -32,6 +32,7 @@ cc_binary {
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"libbase",
"libcutils",
"libhardware",
Expand Down
42 changes: 38 additions & 4 deletions usb/UsbGadget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

#define LOG_TAG "android.hardware.usb.gadget@1.1-service.sunfish"
#define LOG_TAG "android.hardware.usb.gadget@1.2-service.sunfish"

#include "UsbGadget.h"
#include <dirent.h>
Expand All @@ -30,7 +30,7 @@ namespace android {
namespace hardware {
namespace usb {
namespace gadget {
namespace V1_1 {
namespace V1_2 {
namespace implementation {

UsbGadget::UsbGadget() {
Expand All @@ -55,6 +55,40 @@ Return<void> UsbGadget::getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback
return Void();
}

Return<void> UsbGadget::getUsbSpeed(const sp<V1_2::IUsbGadgetCallback> &callback) {
std::string current_speed;
if (ReadFileToString(SPEED_PATH, &current_speed)) {
current_speed = Trim(current_speed);
ALOGI("current USB speed is %s", current_speed.c_str());
if (current_speed == "low-speed")
mUsbSpeed = UsbSpeed::LOWSPEED;
else if (current_speed == "full-speed")
mUsbSpeed = UsbSpeed::FULLSPEED;
else if (current_speed == "high-speed")
mUsbSpeed = UsbSpeed::HIGHSPEED;
else if (current_speed == "super-speed")
mUsbSpeed = UsbSpeed::SUPERSPEED;
else if (current_speed == "super-speed-plus")
mUsbSpeed = UsbSpeed::SUPERSPEED_10Gb;
else if (current_speed == "UNKNOWN")
mUsbSpeed = UsbSpeed::UNKNOWN;
else
mUsbSpeed = UsbSpeed::RESERVED_SPEED;
} else {
ALOGE("Fail to read current speed");
mUsbSpeed = UsbSpeed::UNKNOWN;
}

if (callback) {
Return<void> ret = callback->getUsbSpeedCb(mUsbSpeed);

if (!ret.isOk())
ALOGE("Call to getUsbSpeedCb failed %s", ret.description().c_str());
}

return Void();
}

V1_0::Status UsbGadget::tearDownGadget() {
if (resetGadget() != Status::SUCCESS)
return Status::ERROR;
Expand Down Expand Up @@ -357,7 +391,7 @@ Return<void> UsbGadget::setCurrentUsbFunctions(uint64_t functions,
return Void();
}
} // namespace implementation
} // namespace V1_1
} // namespace V1_2
} // namespace gadget
} // namespace usb
} // namespace hardware
Expand Down
22 changes: 16 additions & 6 deletions usb/UsbGadget.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,8 @@
#include <android-base/file.h>
#include <android-base/properties.h>
#include <android-base/unique_fd.h>
#include <android/hardware/usb/gadget/1.1/IUsbGadget.h>
#include <android-base/strings.h>
#include <android/hardware/usb/gadget/1.2/IUsbGadget.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
#include <pixelusb/UsbGadgetCommon.h>
Expand All @@ -36,14 +37,16 @@ namespace android {
namespace hardware {
namespace usb {
namespace gadget {
namespace V1_1 {
namespace V1_2 {
namespace implementation {

using ::android::sp;
using ::android::base::GetProperty;
using ::android::base::SetProperty;
using ::android::base::unique_fd;
using ::android::base::WriteStringToFile;
using ::android::base::ReadFileToString;
using ::android::base::Trim;
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
Expand All @@ -60,21 +63,26 @@ using ::android::hardware::google::pixel::usb::MonitorFfs;
using ::android::hardware::google::pixel::usb::resetGadget;
using ::android::hardware::google::pixel::usb::setVidPid;
using ::android::hardware::google::pixel::usb::unlinkFunctions;
using ::android::hardware::usb::gadget::V1_0::GadgetFunction;
using ::android::hardware::usb::gadget::V1_0::Status;
using ::android::hardware::usb::gadget::V1_1::IUsbGadget;
using ::android::hardware::usb::gadget::V1_0::IUsbGadgetCallback;
using ::android::hardware::usb::gadget::V1_2::IUsbGadget;
using ::android::hardware::usb::gadget::V1_2::GadgetFunction;
using ::std::string;

constexpr char kGadgetName[] = "a600000.dwc3";
static MonitorFfs monitorFfs(kGadgetName);

#define UDC_PATH "/sys/class/udc/a600000.dwc3/"
#define SPEED_PATH UDC_PATH "current_speed"

struct UsbGadget : public IUsbGadget {
UsbGadget();

// Makes sure that only one request is processed at a time.
std::mutex mLockSetCurrentFunction;
uint64_t mCurrentUsbFunctions;
bool mCurrentUsbFunctionsApplied;
UsbSpeed mUsbSpeed;

Return<void> setCurrentUsbFunctions(uint64_t functions,
const sp<V1_0::IUsbGadgetCallback> &callback,
Expand All @@ -84,14 +92,16 @@ struct UsbGadget : public IUsbGadget {

Return<Status> reset() override;

Return<void> getUsbSpeed(const sp<V1_2::IUsbGadgetCallback> &callback) override;

private:
Status tearDownGadget();
Status setupFunctions(uint64_t functions, const sp<V1_0::IUsbGadgetCallback> &callback,
uint64_t timeout);
};

} // namespace implementation
} // namespace V1_1
} // namespace V1_2
} // namespace gadget
} // namespace usb
} // namespace hardware
Expand Down
2 changes: 1 addition & 1 deletion [email protected][email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<hal format="hidl">
<name>android.hardware.usb.gadget</name>
<transport>hwbinder</transport>
<version>1.1</version>
<version>1.2</version>
<interface>
<name>IUsbGadget</name>
<instance>default</instance>
Expand Down
4 changes: 2 additions & 2 deletions usb/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;

// Generated HIDL files
using android::hardware::usb::gadget::V1_1::IUsbGadget;
using android::hardware::usb::gadget::V1_1::implementation::UsbGadget;
using android::hardware::usb::gadget::V1_2::IUsbGadget;
using android::hardware::usb::gadget::V1_2::implementation::UsbGadget;
using android::hardware::usb::V1_2::IUsb;
using android::hardware::usb::V1_2::implementation::Usb;

Expand Down

0 comments on commit c991432

Please sign in to comment.