Add USB Gadget HAL 1.2 support

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 <albertccwang@google.com>
Change-Id: Ib908434771854e66d22323b15c02acffe1eff48b
This commit is contained in:
Albert Wang 2021-03-23 17:41:42 +08:00
parent 95e87f3721
commit a36726f4fd
5 changed files with 62 additions and 16 deletions

View File

@ -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.
@ -23,7 +23,7 @@ cc_binary {
init_rc: ["android.hardware.usb@1.2-service.redfin.rc"],
vintf_fragments: [
"android.hardware.usb@1.2-service.redfin.xml",
"android.hardware.usb.gadget@1.1-service.redfin.xml",
"android.hardware.usb.gadget@1.2-service.redfin.xml",
],
srcs: ["service.cpp", "Usb.cpp", "UsbGadget.cpp"],
shared_libs: [
@ -32,6 +32,7 @@ cc_binary {
"android.hardware.usb@1.2",
"android.hardware.usb.gadget@1.0",
"android.hardware.usb.gadget@1.1",
"android.hardware.usb.gadget@1.2",
"libbase",
"libcutils",
"libhardware",

View File

@ -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.
@ -14,7 +14,7 @@
* limitations under the License.
*/
#define LOG_TAG "android.hardware.usb.gadget@1.1-service.redfin"
#define LOG_TAG "android.hardware.usb.gadget@1.2-service.redfin"
#include "UsbGadget.h"
#include <dirent.h>
@ -30,7 +30,7 @@ namespace android {
namespace hardware {
namespace usb {
namespace gadget {
namespace V1_1 {
namespace V1_2 {
namespace implementation {
UsbGadget::UsbGadget() {
@ -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;
@ -357,7 +391,7 @@ error:
return Void();
}
} // namespace implementation
} // namespace V1_1
} // namespace V1_2
} // namespace gadget
} // namespace usb
} // namespace hardware

View File

@ -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.
@ -19,7 +19,9 @@
#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/types.h>
#include <android/hardware/usb/gadget/1.2/IUsbGadget.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
#include <pixelusb/UsbGadgetCommon.h>
@ -36,13 +38,15 @@ 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::Trim;
using ::android::base::unique_fd;
using ::android::base::ReadFileToString;
using ::android::base::WriteStringToFile;
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
@ -60,14 +64,18 @@ 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::IUsbGadgetCallback;
using ::android::hardware::usb::gadget::V1_0::Status;
using ::android::hardware::usb::gadget::V1_1::IUsbGadget;
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();
@ -75,6 +83,7 @@ struct UsbGadget : public IUsbGadget {
std::mutex mLockSetCurrentFunction;
uint64_t mCurrentUsbFunctions;
bool mCurrentUsbFunctionsApplied;
UsbSpeed mUsbSpeed;
Return<void> setCurrentUsbFunctions(uint64_t functions,
const sp<V1_0::IUsbGadgetCallback> &callback,
@ -84,6 +93,8 @@ 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,
@ -91,7 +102,7 @@ private:
};
} // namespace implementation
} // namespace V1_1
} // namespace V1_2
} // namespace gadget
} // namespace usb
} // namespace hardware

View File

@ -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>

View File

@ -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.
@ -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;