usb: convert gadget hal to aidl

* Based on hardware/interfaces/usb/gadget/aidl/default.
This commit is contained in:
Konsta
2024-11-16 18:10:05 +02:00
parent a91af8e006
commit cab82fc38b
13 changed files with 191 additions and 218 deletions

View File

@@ -253,7 +253,7 @@ PRODUCT_PACKAGES += \
# USB # USB
PRODUCT_PACKAGES += \ PRODUCT_PACKAGES += \
android.hardware.usb-service.example \ android.hardware.usb-service.example \
android.hardware.usb.gadget@1.2-service.rpi android.hardware.usb.gadget-service.rpi
PRODUCT_COPY_FILES += \ PRODUCT_COPY_FILES += \
frameworks/native/data/etc/android.hardware.usb.accessory.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.usb.accessory.xml \ frameworks/native/data/etc/android.hardware.usb.accessory.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.usb.accessory.xml \

View File

@@ -48,7 +48,7 @@
/vendor/bin/suspend_blocker_rpi u:object_r:suspend_blocker_exec:s0 /vendor/bin/suspend_blocker_rpi u:object_r:suspend_blocker_exec:s0
# USB # USB
/vendor/bin/hw/android\.hardware\.usb\.gadget@1\.2-service\.rpi u:object_r:hal_usb_gadget_default_exec:s0 /vendor/bin/hw/android\.hardware\.usb\.gadget-service\.rpi u:object_r:hal_usb_gadget_default_exec:s0
# V4L2 # V4L2
/vendor/bin/hw/android\.hardware\.media\.c2@1\.2-service-v4l2(.*)? u:object_r:mediacodec_exec:s0 /vendor/bin/hw/android\.hardware\.media\.c2@1\.2-service-v4l2(.*)? u:object_r:mediacodec_exec:s0

View File

@@ -1,23 +1,22 @@
// Copyright (C) 2020 The Android Open Source Project // Copyright (C) 2020 The Android Open Source Project
// Copyright (C) 2023 KonstaKANG // Copyright (C) 2024 KonstaKANG
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
cc_binary { cc_binary {
name: "android.hardware.usb.gadget@1.2-service.rpi", name: "android.hardware.usb.gadget-service.rpi",
defaults: ["hidl_defaults"], defaults: ["hidl_defaults"],
relative_install_path: "hw", relative_install_path: "hw",
init_rc: ["android.hardware.usb.gadget@1.2-service.rpi.rc"], init_rc: ["android.hardware.usb.gadget-service.rpi.rc"],
vintf_fragments: ["android.hardware.usb.gadget@1.2-service.rpi.xml"], vintf_fragments: ["android.hardware.usb.gadget-service.rpi.xml"],
vendor: true, vendor: true,
srcs: [ srcs: [
"service.cpp", "service.cpp",
"UsbGadget.cpp", "UsbGadget.cpp",
], ],
shared_libs: [ shared_libs: [
"android.hardware.usb.gadget@1.0", "android.hardware.usb.gadget-V1-ndk",
"android.hardware.usb.gadget@1.1", "libbinder_ndk",
"android.hardware.usb.gadget@1.2",
"libbase", "libbase",
"libcutils", "libcutils",
"libhardware", "libhardware",
@@ -25,5 +24,5 @@ cc_binary {
"liblog", "liblog",
"libutils", "libutils",
], ],
static_libs: ["libusbconfigfs-2"], static_libs: ["libusbconfigfs-rpi"],
} }

View File

@@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2020 The Android Open Source Project * Copyright (C) 2020 The Android Open Source Project
* Copyright (C) 2023 KonstaKANG * Copyright (C) 2024 KonstaKANG
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
#define LOG_TAG "android.hardware.usb.gadget@1.2-service.rpi" #define LOG_TAG "android.hardware.usb.gadget-service.rpi"
#include "UsbGadget.h" #include "UsbGadget.h"
#include <dirent.h> #include <dirent.h>
@@ -27,12 +27,11 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
namespace aidl {
namespace android { namespace android {
namespace hardware { namespace hardware {
namespace usb { namespace usb {
namespace gadget { namespace gadget {
namespace V1_2 {
namespace implementation {
UsbGadget::UsbGadget() { UsbGadget::UsbGadget() {
if (access(OS_DESC_PATH, R_OK) != 0) { if (access(OS_DESC_PATH, R_OK) != 0) {
@@ -46,16 +45,24 @@ void currentFunctionsAppliedCallback(bool functionsApplied, void* payload) {
gadget->mCurrentUsbFunctionsApplied = functionsApplied; gadget->mCurrentUsbFunctionsApplied = functionsApplied;
} }
Return<void> UsbGadget::getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback>& callback) { ScopedAStatus UsbGadget::getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback>& callback,
Return<void> ret = callback->getCurrentUsbFunctionsCb( int64_t in_transactionId) {
mCurrentUsbFunctions, mCurrentUsbFunctionsApplied ? Status::FUNCTIONS_APPLIED if (callback == nullptr) {
: Status::FUNCTIONS_NOT_APPLIED); return ScopedAStatus::fromExceptionCode(EX_NULL_POINTER);
if (!ret.isOk()) ALOGE("Call to getCurrentUsbFunctionsCb failed %s", ret.description().c_str());
return Void();
} }
Return<void> UsbGadget::getUsbSpeed(const sp<V1_2::IUsbGadgetCallback>& callback) { ScopedAStatus ret = callback->getCurrentUsbFunctionsCb(
mCurrentUsbFunctions,
mCurrentUsbFunctionsApplied ? Status::FUNCTIONS_APPLIED : Status::FUNCTIONS_NOT_APPLIED,
in_transactionId);
if (!ret.isOk())
ALOGE("Call to getCurrentUsbFunctionsCb failed %s", ret.getDescription().c_str());
return ScopedAStatus::ok();
}
ScopedAStatus UsbGadget::getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback,
int64_t in_transactionId) {
std::string current_speed; std::string current_speed;
if (ReadFileToString(SPEED_PATH, &current_speed)) { if (ReadFileToString(SPEED_PATH, &current_speed)) {
current_speed = Trim(current_speed); current_speed = Trim(current_speed);
@@ -72,144 +79,150 @@ Return<void> UsbGadget::getUsbSpeed(const sp<V1_2::IUsbGadgetCallback>& callback
mUsbSpeed = UsbSpeed::SUPERSPEED_10Gb; mUsbSpeed = UsbSpeed::SUPERSPEED_10Gb;
else if (current_speed == "UNKNOWN") else if (current_speed == "UNKNOWN")
mUsbSpeed = UsbSpeed::UNKNOWN; mUsbSpeed = UsbSpeed::UNKNOWN;
else { else
/** mUsbSpeed = UsbSpeed::UNKNOWN;
* This part is used for USB4 or reserved speed.
*
* If reserved speed is detected, it needs to convert to other speeds.
* For example:
* If the bandwidth of new speed is 7G, adding new if
* statement and set mUsbSpeed to SUPERSPEED.
* If the bandwidth of new speed is 80G, adding new if
* statement and set mUsbSpeed to USB4_GEN3_40Gb.
*/
mUsbSpeed = UsbSpeed::RESERVED_SPEED;
}
} else { } else {
ALOGE("Fail to read current speed"); ALOGE("Fail to read current speed");
mUsbSpeed = UsbSpeed::UNKNOWN; mUsbSpeed = UsbSpeed::UNKNOWN;
} }
if (callback) { if (callback) {
Return<void> ret = callback->getUsbSpeedCb(mUsbSpeed); ScopedAStatus ret = callback->getUsbSpeedCb(mUsbSpeed, in_transactionId);
if (!ret.isOk()) ALOGE("Call to getUsbSpeedCb failed %s", ret.description().c_str()); if (!ret.isOk())
ALOGE("Call to getUsbSpeedCb failed %s", ret.getDescription().c_str());
} }
return Void(); return ScopedAStatus::ok();
} }
V1_0::Status UsbGadget::tearDownGadget() { Status UsbGadget::tearDownGadget() {
if (resetGadget() != V1_0::Status::SUCCESS) return V1_0::Status::ERROR; if (resetGadget() != Status::SUCCESS) return Status::ERROR;
if (monitorFfs.isMonitorRunning()) { if (monitorFfs.isMonitorRunning()) {
monitorFfs.reset(); monitorFfs.reset();
} else { } else {
ALOGI("mMonitor not running"); ALOGI("mMonitor not running");
} }
return V1_0::Status::SUCCESS; return Status::SUCCESS;
} }
Return<Status> UsbGadget::reset() { ScopedAStatus UsbGadget::reset(const shared_ptr<IUsbGadgetCallback> &callback,
int64_t in_transactionId) {
if (!WriteStringToFile("none", PULLUP_PATH)) { if (!WriteStringToFile("none", PULLUP_PATH)) {
ALOGI("Gadget cannot be pulled down"); ALOGI("Gadget cannot be pulled down");
return Status::ERROR; if (callback)
callback->resetCb(Status::ERROR, in_transactionId);
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Error while calling resetCb");
} }
usleep(kDisconnectWaitUs); usleep(kDisconnectWaitUs);
if (!WriteStringToFile(kGadgetName, PULLUP_PATH)) { if (!WriteStringToFile(kGadgetName, PULLUP_PATH)) {
ALOGI("Gadget cannot be pulled up"); ALOGI("Gadget cannot be pulled up");
return Status::ERROR; if (callback)
callback->resetCb(Status::ERROR, in_transactionId);
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Error while calling resetCb");
} }
return Status::SUCCESS; if (callback)
callback->resetCb(Status::SUCCESS, in_transactionId);
return ScopedAStatus::ok();
} }
static V1_0::Status validateAndSetVidPid(uint64_t functions) { static Status validateAndSetVidPid(uint64_t functions) {
V1_0::Status ret = V1_0::Status::SUCCESS; Status ret = Status::SUCCESS;
switch (functions) { switch (functions) {
case static_cast<uint64_t>(V1_2::GadgetFunction::MTP): case GadgetFunction::MTP:
ret = setVidPid("0x18d1", "0x4ee1"); ret = setVidPid("0x18d1", "0x4ee1");
break; break;
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::MTP: case GadgetFunction::ADB | GadgetFunction::MTP:
ret = setVidPid("0x18d1", "0x4ee2"); ret = setVidPid("0x18d1", "0x4ee2");
break; break;
case static_cast<uint64_t>(V1_2::GadgetFunction::RNDIS): case GadgetFunction::RNDIS:
ret = setVidPid("0x18d1", "0x4ee3"); ret = setVidPid("0x18d1", "0x4ee3");
break; break;
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::RNDIS: case GadgetFunction::ADB | GadgetFunction::RNDIS:
ret = setVidPid("0x18d1", "0x4ee4"); ret = setVidPid("0x18d1", "0x4ee4");
break; break;
case static_cast<uint64_t>(V1_2::GadgetFunction::PTP): case GadgetFunction::PTP:
ret = setVidPid("0x18d1", "0x4ee5"); ret = setVidPid("0x18d1", "0x4ee5");
break; break;
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::PTP: case GadgetFunction::ADB | GadgetFunction::PTP:
ret = setVidPid("0x18d1", "0x4ee6"); ret = setVidPid("0x18d1", "0x4ee6");
break; break;
case static_cast<uint64_t>(V1_2::GadgetFunction::ADB): case GadgetFunction::ADB:
ret = setVidPid("0x18d1", "0x4ee7"); ret = setVidPid("0x18d1", "0x4ee7");
break; break;
case static_cast<uint64_t>(V1_2::GadgetFunction::MIDI): case GadgetFunction::MIDI:
ret = setVidPid("0x18d1", "0x4ee8"); ret = setVidPid("0x18d1", "0x4ee8");
break; break;
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::MIDI: case GadgetFunction::ADB | GadgetFunction::MIDI:
ret = setVidPid("0x18d1", "0x4ee9"); ret = setVidPid("0x18d1", "0x4ee9");
break; break;
case static_cast<uint64_t>(V1_2::GadgetFunction::NCM): case GadgetFunction::NCM:
ret = setVidPid("0x18d1", "0x4eeb"); ret = setVidPid("0x18d1", "0x4eeb");
break; break;
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::NCM: case GadgetFunction::ADB | GadgetFunction::NCM:
ret = setVidPid("0x18d1", "0x4eec"); ret = setVidPid("0x18d1", "0x4eec");
break; break;
case static_cast<uint64_t>(V1_2::GadgetFunction::ACCESSORY): case GadgetFunction::ACCESSORY:
ret = setVidPid("0x18d1", "0x2d00"); ret = setVidPid("0x18d1", "0x2d00");
break; break;
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::ACCESSORY: case GadgetFunction::ADB | GadgetFunction::ACCESSORY:
ret = setVidPid("0x18d1", "0x2d01"); ret = setVidPid("0x18d1", "0x2d01");
break; break;
case static_cast<uint64_t>(V1_2::GadgetFunction::AUDIO_SOURCE): case GadgetFunction::AUDIO_SOURCE:
ret = setVidPid("0x18d1", "0x2d02"); ret = setVidPid("0x18d1", "0x2d02");
break; break;
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::AUDIO_SOURCE: case GadgetFunction::ADB | GadgetFunction::AUDIO_SOURCE:
ret = setVidPid("0x18d1", "0x2d03"); ret = setVidPid("0x18d1", "0x2d03");
break; break;
case V1_2::GadgetFunction::ACCESSORY | V1_2::GadgetFunction::AUDIO_SOURCE: case GadgetFunction::ACCESSORY | GadgetFunction::AUDIO_SOURCE:
ret = setVidPid("0x18d1", "0x2d04"); ret = setVidPid("0x18d1", "0x2d04");
break; break;
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::ACCESSORY | case GadgetFunction::ADB | GadgetFunction::ACCESSORY |
V1_2::GadgetFunction::AUDIO_SOURCE: GadgetFunction::AUDIO_SOURCE:
ret = setVidPid("0x18d1", "0x2d05"); ret = setVidPid("0x18d1", "0x2d05");
break; break;
default: default:
ALOGE("Combination not supported"); ALOGE("Combination not supported");
ret = V1_0::Status::CONFIGURATION_NOT_SUPPORTED; ret = Status::CONFIGURATION_NOT_SUPPORTED;
} }
return ret; return ret;
} }
V1_0::Status UsbGadget::setupFunctions(uint64_t functions, Status UsbGadget::setupFunctions(long functions,
const sp<V1_0::IUsbGadgetCallback>& callback, const shared_ptr<IUsbGadgetCallback> &callback, uint64_t timeout,
uint64_t timeout) { int64_t in_transactionId) {
bool ffsEnabled = false; bool ffsEnabled = false;
int i = 0; int i = 0;
if (addGenericAndroidFunctions(&monitorFfs, functions, &ffsEnabled, &i) != if (addGenericAndroidFunctions(&monitorFfs, functions, &ffsEnabled, &i) !=
V1_0::Status::SUCCESS) Status::SUCCESS)
return V1_0::Status::ERROR; return Status::ERROR;
if ((functions & V1_2::GadgetFunction::ADB) != 0) { if ((functions & GadgetFunction::ADB) != 0) {
ffsEnabled = true; ffsEnabled = true;
if (addAdb(&monitorFfs, &i) != V1_0::Status::SUCCESS) return V1_0::Status::ERROR; if (addAdb(&monitorFfs, &i) != Status::SUCCESS) return Status::ERROR;
} }
// Pull up the gadget right away when there are no ffs functions. // Pull up the gadget right away when there are no ffs functions.
if (!ffsEnabled) { if (!ffsEnabled) {
if (!WriteStringToFile(kGadgetName, PULLUP_PATH)) return V1_0::Status::ERROR; if (!WriteStringToFile(kGadgetName, PULLUP_PATH))
return Status::ERROR;
mCurrentUsbFunctionsApplied = true; mCurrentUsbFunctionsApplied = true;
if (callback) callback->setCurrentUsbFunctionsCb(functions, V1_0::Status::SUCCESS);
return V1_0::Status::SUCCESS; if (callback)
callback->setCurrentUsbFunctionsCb(functions, Status::SUCCESS, in_transactionId);
return Status::SUCCESS;
} }
monitorFfs.registerFunctionsAppliedCallback(&currentFunctionsAppliedCallback, this); monitorFfs.registerFunctionsAppliedCallback(&currentFunctionsAppliedCallback, this);
@@ -222,25 +235,28 @@ V1_0::Status UsbGadget::setupFunctions(uint64_t functions,
if (callback) { if (callback) {
bool pullup = monitorFfs.waitForPullUp(timeout); bool pullup = monitorFfs.waitForPullUp(timeout);
Return<void> ret = callback->setCurrentUsbFunctionsCb( ScopedAStatus ret = callback->setCurrentUsbFunctionsCb(functions,
functions, pullup ? V1_0::Status::SUCCESS : V1_0::Status::ERROR); pullup ? Status::SUCCESS : Status::ERROR,
if (!ret.isOk()) ALOGE("setCurrentUsbFunctionsCb error %s", ret.description().c_str()); in_transactionId);
if (!ret.isOk())
ALOGE("setCurrentUsbFunctionsCb error %s", ret.getMessage());
} }
return V1_0::Status::SUCCESS; return Status::SUCCESS;
} }
Return<void> UsbGadget::setCurrentUsbFunctions(uint64_t functions, ScopedAStatus UsbGadget::setCurrentUsbFunctions(int64_t functions,
const sp<V1_0::IUsbGadgetCallback>& callback, const shared_ptr<IUsbGadgetCallback> &callback,
uint64_t timeout) { int64_t timeoutMs,
int64_t in_transactionId) {
std::unique_lock<std::mutex> lk(mLockSetCurrentFunction); std::unique_lock<std::mutex> lk(mLockSetCurrentFunction);
mCurrentUsbFunctions = functions; mCurrentUsbFunctions = functions;
mCurrentUsbFunctionsApplied = false; mCurrentUsbFunctionsApplied = false;
// Unlink the gadget and stop the monitor if running. // Unlink the gadget and stop the monitor if running.
V1_0::Status status = tearDownGadget(); Status status = tearDownGadget();
if (status != V1_0::Status::SUCCESS) { if (status != Status::SUCCESS) {
goto error; goto error;
} }
@@ -249,39 +265,45 @@ Return<void> UsbGadget::setCurrentUsbFunctions(uint64_t functions,
// Leave the gadget pulled down to give time for the host to sense disconnect. // Leave the gadget pulled down to give time for the host to sense disconnect.
usleep(kDisconnectWaitUs); usleep(kDisconnectWaitUs);
if (functions == static_cast<uint64_t>(V1_2::GadgetFunction::NONE)) { if (functions == GadgetFunction::NONE) {
if (callback == NULL) return Void(); if (callback == NULL)
Return<void> ret = callback->setCurrentUsbFunctionsCb(functions, V1_0::Status::SUCCESS); return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "callback == NULL");
ScopedAStatus ret = callback->setCurrentUsbFunctionsCb(functions, Status::SUCCESS, in_transactionId);
if (!ret.isOk()) if (!ret.isOk())
ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.description().c_str()); ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.getDescription().c_str());
return Void(); return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Error while calling setCurrentUsbFunctionsCb");
} }
status = validateAndSetVidPid(functions); status = validateAndSetVidPid(functions);
if (status != V1_0::Status::SUCCESS) { if (status != Status::SUCCESS) {
goto error; goto error;
} }
status = setupFunctions(functions, callback, timeout); status = setupFunctions(functions, callback, timeoutMs, in_transactionId);
if (status != V1_0::Status::SUCCESS) { if (status != Status::SUCCESS) {
goto error; goto error;
} }
ALOGI("Usb Gadget setcurrent functions called successfully"); ALOGI("Usb Gadget setcurrent functions called successfully");
return Void(); return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Usb Gadget setcurrent functions called successfully");
error: error:
ALOGI("Usb Gadget setcurrent functions failed"); ALOGI("Usb Gadget setcurrent functions failed");
if (callback == NULL) return Void(); if (callback == NULL)
Return<void> ret = callback->setCurrentUsbFunctionsCb(functions, status); return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Usb Gadget setcurrent functions failed");
ScopedAStatus ret = callback->setCurrentUsbFunctionsCb(functions, status, in_transactionId);
if (!ret.isOk()) if (!ret.isOk())
ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.description().c_str()); ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.getDescription().c_str());
return Void(); return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Error while calling setCurrentUsbFunctionsCb");
} }
} // namespace implementation
} // namespace V1_2
} // namespace gadget } // namespace gadget
} // namespace usb } // namespace usb
} // namespace hardware } // namespace hardware
} // namespace android } // namespace android
} // aidl

View File

@@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2020 The Android Open Source Project * Copyright (C) 2020 The Android Open Source Project
* Copyright (C) 2023 KonstaKANG * Copyright (C) 2024 KonstaKANG
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,18 +15,18 @@
* limitations under the License. * limitations under the License.
*/ */
#ifndef ANDROID_HARDWARE_USB_GADGET_V1_2_USBGADGET_H #pragma once
#define ANDROID_HARDWARE_USB_GADGET_V1_2_USBGADGET_H
#include <UsbGadgetCommon.h> #include <UsbGadgetCommon.h>
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/properties.h> #include <android-base/properties.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <android/hardware/usb/gadget/1.2/IUsbGadget.h> #include <android-base/strings.h>
#include <android/hardware/usb/gadget/1.2/types.h> #include <aidl/android/hardware/usb/gadget/BnUsbGadget.h>
#include <hidl/MQDescriptor.h> #include <aidl/android/hardware/usb/gadget/BnUsbGadgetCallback.h>
#include <hidl/Status.h> #include <aidl/android/hardware/usb/gadget/GadgetFunction.h>
#include <aidl/android/hardware/usb/gadget/IUsbGadget.h>
#include <aidl/android/hardware/usb/gadget/IUsbGadgetCallback.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/eventfd.h> #include <sys/eventfd.h>
#include <utils/Log.h> #include <utils/Log.h>
@@ -36,36 +36,25 @@
#include <string> #include <string>
#include <thread> #include <thread>
namespace aidl {
namespace android { namespace android {
namespace hardware { namespace hardware {
namespace usb { namespace usb {
namespace gadget { namespace gadget {
namespace V1_2 {
namespace implementation {
using ::android::sp; using ::aidl::android::hardware::usb::gadget::GadgetFunction;
using ::aidl::android::hardware::usb::gadget::IUsbGadgetCallback;
using ::aidl::android::hardware::usb::gadget::IUsbGadget;
using ::aidl::android::hardware::usb::gadget::Status;
using ::aidl::android::hardware::usb::gadget::UsbSpeed;
using ::android::base::GetProperty; using ::android::base::GetProperty;
using ::android::base::ReadFileToString;
using ::android::base::SetProperty; using ::android::base::SetProperty;
using ::android::base::Trim;
using ::android::base::unique_fd; using ::android::base::unique_fd;
using ::android::base::ReadFileToString;
using ::android::base::Trim;
using ::android::base::WriteStringToFile; using ::android::base::WriteStringToFile;
using ::android::hardware::hidl_array; using ::ndk::ScopedAStatus;
using ::android::hardware::hidl_memory; using ::std::shared_ptr;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::usb::gadget::addAdb;
using ::android::hardware::usb::gadget::addEpollFd;
using ::android::hardware::usb::gadget::getVendorFunctions;
using ::android::hardware::usb::gadget::kDebug;
using ::android::hardware::usb::gadget::kDisconnectWaitUs;
using ::android::hardware::usb::gadget::linkFunction;
using ::android::hardware::usb::gadget::MonitorFfs;
using ::android::hardware::usb::gadget::resetGadget;
using ::android::hardware::usb::gadget::setVidPid;
using ::android::hardware::usb::gadget::unlinkFunctions;
using ::std::string; using ::std::string;
constexpr char kGadgetName[] = "fe980000.usb"; constexpr char kGadgetName[] = "fe980000.usb";
@@ -74,36 +63,36 @@ static MonitorFfs monitorFfs(kGadgetName);
#define UDC_PATH "/sys/class/udc/fe980000.usb/" #define UDC_PATH "/sys/class/udc/fe980000.usb/"
#define SPEED_PATH UDC_PATH "current_speed" #define SPEED_PATH UDC_PATH "current_speed"
struct UsbGadget : public IUsbGadget { struct UsbGadget : public BnUsbGadget {
UsbGadget(); UsbGadget();
// Makes sure that only one request is processed at a time. // Makes sure that only one request is processed at a time.
std::mutex mLockSetCurrentFunction; std::mutex mLockSetCurrentFunction;
uint64_t mCurrentUsbFunctions; long mCurrentUsbFunctions;
bool mCurrentUsbFunctionsApplied; bool mCurrentUsbFunctionsApplied;
UsbSpeed mUsbSpeed; UsbSpeed mUsbSpeed;
Return<void> setCurrentUsbFunctions(uint64_t functions, ScopedAStatus setCurrentUsbFunctions(int64_t functions,
const sp<V1_0::IUsbGadgetCallback>& callback, const shared_ptr<IUsbGadgetCallback> &callback,
uint64_t timeout) override; int64_t timeoutMs, int64_t in_transactionId) override;
Return<void> getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback>& callback) override; ScopedAStatus getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback> &callback,
int64_t in_transactionId) override;
Return<Status> reset() override; ScopedAStatus reset(const shared_ptr<IUsbGadgetCallback> &callback,
int64_t in_transactionId) override;
Return<void> getUsbSpeed(const sp<V1_2::IUsbGadgetCallback>& callback) override; ScopedAStatus getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback,
int64_t in_transactionId) override;
private: private:
V1_0::Status tearDownGadget(); Status tearDownGadget();
V1_0::Status setupFunctions(uint64_t functions, const sp<V1_0::IUsbGadgetCallback>& callback, Status setupFunctions(long functions, const shared_ptr<IUsbGadgetCallback> &callback,
uint64_t timeout); uint64_t timeout, int64_t in_transactionId);
}; };
} // namespace implementation
} // namespace V1_2
} // namespace gadget } // namespace gadget
} // namespace usb } // namespace usb
} // namespace hardware } // namespace hardware
} // namespace android } // namespace android
} // aidl
#endif // ANDROID_HARDWARE_USB_V1_2_USBGADGET_H

View File

@@ -0,0 +1,4 @@
service vendor.usb_gadget-default /vendor/bin/hw/android.hardware.usb.gadget-service.rpi
class hal
user system
group system shell mtp

View File

@@ -1,8 +1,7 @@
<manifest version="1.0" type="device"> <manifest version="1.0" type="device">
<hal format="hidl"> <hal format="aidl">
<name>android.hardware.usb.gadget</name> <name>android.hardware.usb.gadget</name>
<transport>hwbinder</transport> <version>1</version>
<version>1.2</version>
<interface> <interface>
<name>IUsbGadget</name> <name>IUsbGadget</name>
<instance>default</instance> <instance>default</instance>

View File

@@ -1,7 +0,0 @@
service vendor.usb-gadget-hal-1-2 /vendor/bin/hw/android.hardware.usb.gadget@1.2-service.rpi
interface android.hardware.usb.gadget@1.0::IUsbGadget default
interface android.hardware.usb.gadget@1.1::IUsbGadget default
interface android.hardware.usb.gadget@1.2::IUsbGadget default
class hal
user system
group system shell mtp

View File

@@ -1,30 +1,10 @@
/* // Copyright (C) 2020 The Android Open Source Project
* Copyright (C) 2020 The Android Open Source Project // Copyright (C) 2024 KonstaKANG
* //
* Licensed under the Apache License, Version 2.0 (the "License"); // SPDX-License-Identifier: Apache-2.0
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package {
// See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import
// all of the 'license_kinds' from "hardware_interfaces_license"
// to get the below license kinds:
// SPDX-license-identifier-Apache-2.0
default_applicable_licenses: ["hardware_interfaces_license"],
}
cc_library_static { cc_library_static {
name: "libusbconfigfs-2", name: "libusbconfigfs-rpi",
vendor_available: true, vendor_available: true,
export_include_dirs: ["include"], export_include_dirs: ["include"],
@@ -39,9 +19,7 @@ cc_library_static {
], ],
shared_libs: [ shared_libs: [
"android.hardware.usb.gadget@1.0", "android.hardware.usb.gadget-V1-ndk",
"android.hardware.usb.gadget@1.1",
"android.hardware.usb.gadget@1.2",
"libbase", "libbase",
"libcutils", "libcutils",
"libhidlbase", "libhidlbase",

View File

@@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2020 The Android Open Source Project * Copyright (C) 2020 The Android Open Source Project
* Copyright (C) 2024 KonstaKANG
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,6 +19,7 @@
#include "include/UsbGadgetCommon.h" #include "include/UsbGadgetCommon.h"
namespace aidl {
namespace android { namespace android {
namespace hardware { namespace hardware {
namespace usb { namespace usb {
@@ -267,3 +269,4 @@ void MonitorFfs::registerFunctionsAppliedCallback(void (*callback)(bool function
} // namespace usb } // namespace usb
} // namespace hardware } // namespace hardware
} // namespace android } // namespace android
} // namespace aidl

View File

@@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2020 The Android Open Source Project * Copyright (C) 2020 The Android Open Source Project
* Copyright (C) 2024 KonstaKANG
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,6 +19,7 @@
#include "include/UsbGadgetCommon.h" #include "include/UsbGadgetCommon.h"
namespace aidl {
namespace android { namespace android {
namespace hardware { namespace hardware {
namespace usb { namespace usb {
@@ -205,3 +207,4 @@ Status addAdb(MonitorFfs* monitorFfs, int* functionCount) {
} // namespace usb } // namespace usb
} // namespace hardware } // namespace hardware
} // namespace android } // namespace android
} // namespace aidl

View File

@@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2020 The Android Open Source Project * Copyright (C) 2020 The Android Open Source Project
* Copyright (C) 2024 KonstaKANG
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,8 +22,8 @@
#include <android-base/properties.h> #include <android-base/properties.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <android/hardware/usb/gadget/1.2/IUsbGadget.h> #include <aidl/android/hardware/usb/gadget/IUsbGadget.h>
#include <android/hardware/usb/gadget/1.2/types.h> #include <aidl/android/hardware/usb/gadget/GadgetFunction.h>
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
@@ -41,6 +42,7 @@
#include <string> #include <string>
#include <thread> #include <thread>
namespace aidl {
namespace android { namespace android {
namespace hardware { namespace hardware {
namespace usb { namespace usb {
@@ -79,8 +81,8 @@ using ::android::base::GetProperty;
using ::android::base::SetProperty; using ::android::base::SetProperty;
using ::android::base::unique_fd; using ::android::base::unique_fd;
using ::android::base::WriteStringToFile; using ::android::base::WriteStringToFile;
using ::android::hardware::usb::gadget::V1_0::Status; using ::aidl::android::hardware::usb::gadget::GadgetFunction;
using ::android::hardware::usb::gadget::V1_2::GadgetFunction; using ::aidl::android::hardware::usb::gadget::Status;
using ::std::lock_guard; using ::std::lock_guard;
using ::std::move; using ::std::move;
@@ -176,4 +178,5 @@ Status resetGadget();
} // namespace usb } // namespace usb
} // namespace hardware } // namespace hardware
} // namespace android } // namespace android
} // namespace aidl
#endif #endif

View File

@@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2020 The Android Open Source Project * Copyright (C) 2022 The Android Open Source Project
* Copyright (C) 2023 KonstaKANG * Copyright (C) 2024 KonstaKANG
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -15,39 +15,19 @@
* limitations under the License. * limitations under the License.
*/ */
#define LOG_TAG "android.hardware.usb.gadget@1.2-service.rpi" #include <android-base/logging.h>
#include <android/binder_manager.h>
#include <hidl/HidlTransportSupport.h> #include <android/binder_process.h>
#include "UsbGadget.h" #include "UsbGadget.h"
using android::sp; using ::aidl::android::hardware::usb::gadget::UsbGadget;
// libhwbinder:
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
// Generated HIDL files
using android::hardware::usb::gadget::V1_2::IUsbGadget;
using android::hardware::usb::gadget::V1_2::implementation::UsbGadget;
using android::OK;
using android::status_t;
int main() { int main() {
configureRpcThreadpool(1, true /*callerWillJoin*/); ABinderProcess_setThreadPoolMaxThreadCount(0);
std::shared_ptr<UsbGadget> usbgadget = ndk::SharedRefBase::make<UsbGadget>();
android::sp<IUsbGadget> service = new UsbGadget(); const std::string instance = std::string() + UsbGadget::descriptor + "/default";
binder_status_t status = AServiceManager_addService(usbgadget->asBinder().get(), instance.c_str());
status_t status = service->registerAsService(); CHECK(status == STATUS_OK);
ABinderProcess_joinThreadPool();
if (status != OK) { return -1;
ALOGE("Cannot register USB Gadget HAL service");
return 1;
}
ALOGI("USB Gadget HAL Ready.");
joinRpcThreadpool();
// Under noraml cases, execution will not reach this line.
ALOGI("USB Gadget HAL failed to join thread pool.");
return 1;
} }