usb: convert gadget hal to aidl
* Based on hardware/interfaces/usb/gadget/aidl/default.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* 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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,7 +15,7 @@
|
||||
* 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 <dirent.h>
|
||||
@@ -27,12 +27,11 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace aidl {
|
||||
namespace android {
|
||||
namespace hardware {
|
||||
namespace usb {
|
||||
namespace gadget {
|
||||
namespace V1_2 {
|
||||
namespace implementation {
|
||||
|
||||
UsbGadget::UsbGadget() {
|
||||
if (access(OS_DESC_PATH, R_OK) != 0) {
|
||||
@@ -46,16 +45,24 @@ void currentFunctionsAppliedCallback(bool functionsApplied, void* payload) {
|
||||
gadget->mCurrentUsbFunctionsApplied = functionsApplied;
|
||||
}
|
||||
|
||||
Return<void> UsbGadget::getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback>& callback) {
|
||||
Return<void> ret = callback->getCurrentUsbFunctionsCb(
|
||||
mCurrentUsbFunctions, mCurrentUsbFunctionsApplied ? Status::FUNCTIONS_APPLIED
|
||||
: Status::FUNCTIONS_NOT_APPLIED);
|
||||
if (!ret.isOk()) ALOGE("Call to getCurrentUsbFunctionsCb failed %s", ret.description().c_str());
|
||||
ScopedAStatus UsbGadget::getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback>& callback,
|
||||
int64_t in_transactionId) {
|
||||
if (callback == nullptr) {
|
||||
return ScopedAStatus::fromExceptionCode(EX_NULL_POINTER);
|
||||
}
|
||||
|
||||
return Void();
|
||||
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();
|
||||
}
|
||||
|
||||
Return<void> UsbGadget::getUsbSpeed(const sp<V1_2::IUsbGadgetCallback>& callback) {
|
||||
ScopedAStatus UsbGadget::getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback,
|
||||
int64_t in_transactionId) {
|
||||
std::string current_speed;
|
||||
if (ReadFileToString(SPEED_PATH, ¤t_speed)) {
|
||||
current_speed = Trim(current_speed);
|
||||
@@ -72,144 +79,150 @@ Return<void> UsbGadget::getUsbSpeed(const sp<V1_2::IUsbGadgetCallback>& callback
|
||||
mUsbSpeed = UsbSpeed::SUPERSPEED_10Gb;
|
||||
else if (current_speed == "UNKNOWN")
|
||||
mUsbSpeed = UsbSpeed::UNKNOWN;
|
||||
else {
|
||||
/**
|
||||
* 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
|
||||
mUsbSpeed = UsbSpeed::UNKNOWN;
|
||||
} else {
|
||||
ALOGE("Fail to read current speed");
|
||||
mUsbSpeed = UsbSpeed::UNKNOWN;
|
||||
}
|
||||
|
||||
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() {
|
||||
if (resetGadget() != V1_0::Status::SUCCESS) return V1_0::Status::ERROR;
|
||||
Status UsbGadget::tearDownGadget() {
|
||||
if (resetGadget() != Status::SUCCESS) return Status::ERROR;
|
||||
|
||||
if (monitorFfs.isMonitorRunning()) {
|
||||
monitorFfs.reset();
|
||||
} else {
|
||||
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)) {
|
||||
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);
|
||||
|
||||
if (!WriteStringToFile(kGadgetName, PULLUP_PATH)) {
|
||||
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) {
|
||||
V1_0::Status ret = V1_0::Status::SUCCESS;
|
||||
static Status validateAndSetVidPid(uint64_t functions) {
|
||||
Status ret = Status::SUCCESS;
|
||||
|
||||
switch (functions) {
|
||||
case static_cast<uint64_t>(V1_2::GadgetFunction::MTP):
|
||||
case GadgetFunction::MTP:
|
||||
ret = setVidPid("0x18d1", "0x4ee1");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::MTP:
|
||||
case GadgetFunction::ADB | GadgetFunction::MTP:
|
||||
ret = setVidPid("0x18d1", "0x4ee2");
|
||||
break;
|
||||
case static_cast<uint64_t>(V1_2::GadgetFunction::RNDIS):
|
||||
case GadgetFunction::RNDIS:
|
||||
ret = setVidPid("0x18d1", "0x4ee3");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::RNDIS:
|
||||
case GadgetFunction::ADB | GadgetFunction::RNDIS:
|
||||
ret = setVidPid("0x18d1", "0x4ee4");
|
||||
break;
|
||||
case static_cast<uint64_t>(V1_2::GadgetFunction::PTP):
|
||||
case GadgetFunction::PTP:
|
||||
ret = setVidPid("0x18d1", "0x4ee5");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::PTP:
|
||||
case GadgetFunction::ADB | GadgetFunction::PTP:
|
||||
ret = setVidPid("0x18d1", "0x4ee6");
|
||||
break;
|
||||
case static_cast<uint64_t>(V1_2::GadgetFunction::ADB):
|
||||
case GadgetFunction::ADB:
|
||||
ret = setVidPid("0x18d1", "0x4ee7");
|
||||
break;
|
||||
case static_cast<uint64_t>(V1_2::GadgetFunction::MIDI):
|
||||
case GadgetFunction::MIDI:
|
||||
ret = setVidPid("0x18d1", "0x4ee8");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::MIDI:
|
||||
case GadgetFunction::ADB | GadgetFunction::MIDI:
|
||||
ret = setVidPid("0x18d1", "0x4ee9");
|
||||
break;
|
||||
case static_cast<uint64_t>(V1_2::GadgetFunction::NCM):
|
||||
case GadgetFunction::NCM:
|
||||
ret = setVidPid("0x18d1", "0x4eeb");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::NCM:
|
||||
case GadgetFunction::ADB | GadgetFunction::NCM:
|
||||
ret = setVidPid("0x18d1", "0x4eec");
|
||||
break;
|
||||
case static_cast<uint64_t>(V1_2::GadgetFunction::ACCESSORY):
|
||||
case GadgetFunction::ACCESSORY:
|
||||
ret = setVidPid("0x18d1", "0x2d00");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::ACCESSORY:
|
||||
case GadgetFunction::ADB | GadgetFunction::ACCESSORY:
|
||||
ret = setVidPid("0x18d1", "0x2d01");
|
||||
break;
|
||||
case static_cast<uint64_t>(V1_2::GadgetFunction::AUDIO_SOURCE):
|
||||
case GadgetFunction::AUDIO_SOURCE:
|
||||
ret = setVidPid("0x18d1", "0x2d02");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::AUDIO_SOURCE:
|
||||
case GadgetFunction::ADB | GadgetFunction::AUDIO_SOURCE:
|
||||
ret = setVidPid("0x18d1", "0x2d03");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ACCESSORY | V1_2::GadgetFunction::AUDIO_SOURCE:
|
||||
case GadgetFunction::ACCESSORY | GadgetFunction::AUDIO_SOURCE:
|
||||
ret = setVidPid("0x18d1", "0x2d04");
|
||||
break;
|
||||
case V1_2::GadgetFunction::ADB | V1_2::GadgetFunction::ACCESSORY |
|
||||
V1_2::GadgetFunction::AUDIO_SOURCE:
|
||||
case GadgetFunction::ADB | GadgetFunction::ACCESSORY |
|
||||
GadgetFunction::AUDIO_SOURCE:
|
||||
ret = setVidPid("0x18d1", "0x2d05");
|
||||
break;
|
||||
default:
|
||||
ALOGE("Combination not supported");
|
||||
ret = V1_0::Status::CONFIGURATION_NOT_SUPPORTED;
|
||||
ret = Status::CONFIGURATION_NOT_SUPPORTED;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
V1_0::Status UsbGadget::setupFunctions(uint64_t functions,
|
||||
const sp<V1_0::IUsbGadgetCallback>& callback,
|
||||
uint64_t timeout) {
|
||||
Status UsbGadget::setupFunctions(long functions,
|
||||
const shared_ptr<IUsbGadgetCallback> &callback, uint64_t timeout,
|
||||
int64_t in_transactionId) {
|
||||
bool ffsEnabled = false;
|
||||
int i = 0;
|
||||
|
||||
if (addGenericAndroidFunctions(&monitorFfs, functions, &ffsEnabled, &i) !=
|
||||
V1_0::Status::SUCCESS)
|
||||
return V1_0::Status::ERROR;
|
||||
Status::SUCCESS)
|
||||
return Status::ERROR;
|
||||
|
||||
if ((functions & V1_2::GadgetFunction::ADB) != 0) {
|
||||
if ((functions & GadgetFunction::ADB) != 0) {
|
||||
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.
|
||||
if (!ffsEnabled) {
|
||||
if (!WriteStringToFile(kGadgetName, PULLUP_PATH)) return V1_0::Status::ERROR;
|
||||
if (!WriteStringToFile(kGadgetName, PULLUP_PATH))
|
||||
return Status::ERROR;
|
||||
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(¤tFunctionsAppliedCallback, this);
|
||||
@@ -222,25 +235,28 @@ V1_0::Status UsbGadget::setupFunctions(uint64_t functions,
|
||||
|
||||
if (callback) {
|
||||
bool pullup = monitorFfs.waitForPullUp(timeout);
|
||||
Return<void> ret = callback->setCurrentUsbFunctionsCb(
|
||||
functions, pullup ? V1_0::Status::SUCCESS : V1_0::Status::ERROR);
|
||||
if (!ret.isOk()) ALOGE("setCurrentUsbFunctionsCb error %s", ret.description().c_str());
|
||||
ScopedAStatus ret = callback->setCurrentUsbFunctionsCb(functions,
|
||||
pullup ? Status::SUCCESS : Status::ERROR,
|
||||
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,
|
||||
const sp<V1_0::IUsbGadgetCallback>& callback,
|
||||
uint64_t timeout) {
|
||||
ScopedAStatus UsbGadget::setCurrentUsbFunctions(int64_t functions,
|
||||
const shared_ptr<IUsbGadgetCallback> &callback,
|
||||
int64_t timeoutMs,
|
||||
int64_t in_transactionId) {
|
||||
std::unique_lock<std::mutex> lk(mLockSetCurrentFunction);
|
||||
|
||||
mCurrentUsbFunctions = functions;
|
||||
mCurrentUsbFunctionsApplied = false;
|
||||
|
||||
// Unlink the gadget and stop the monitor if running.
|
||||
V1_0::Status status = tearDownGadget();
|
||||
if (status != V1_0::Status::SUCCESS) {
|
||||
Status status = tearDownGadget();
|
||||
if (status != Status::SUCCESS) {
|
||||
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.
|
||||
usleep(kDisconnectWaitUs);
|
||||
|
||||
if (functions == static_cast<uint64_t>(V1_2::GadgetFunction::NONE)) {
|
||||
if (callback == NULL) return Void();
|
||||
Return<void> ret = callback->setCurrentUsbFunctionsCb(functions, V1_0::Status::SUCCESS);
|
||||
if (functions == GadgetFunction::NONE) {
|
||||
if (callback == NULL)
|
||||
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
|
||||
-1, "callback == NULL");
|
||||
ScopedAStatus ret = callback->setCurrentUsbFunctionsCb(functions, Status::SUCCESS, in_transactionId);
|
||||
if (!ret.isOk())
|
||||
ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.description().c_str());
|
||||
return Void();
|
||||
ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.getDescription().c_str());
|
||||
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
|
||||
-1, "Error while calling setCurrentUsbFunctionsCb");
|
||||
}
|
||||
|
||||
status = validateAndSetVidPid(functions);
|
||||
|
||||
if (status != V1_0::Status::SUCCESS) {
|
||||
if (status != Status::SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
status = setupFunctions(functions, callback, timeout);
|
||||
if (status != V1_0::Status::SUCCESS) {
|
||||
status = setupFunctions(functions, callback, timeoutMs, in_transactionId);
|
||||
if (status != Status::SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
ALOGI("Usb Gadget setcurrent functions called successfully");
|
||||
return Void();
|
||||
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
|
||||
-1, "Usb Gadget setcurrent functions called successfully");
|
||||
|
||||
error:
|
||||
ALOGI("Usb Gadget setcurrent functions failed");
|
||||
if (callback == NULL) return Void();
|
||||
Return<void> ret = callback->setCurrentUsbFunctionsCb(functions, status);
|
||||
if (callback == NULL)
|
||||
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
|
||||
-1, "Usb Gadget setcurrent functions failed");
|
||||
ScopedAStatus ret = callback->setCurrentUsbFunctionsCb(functions, status, in_transactionId);
|
||||
if (!ret.isOk())
|
||||
ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.description().c_str());
|
||||
return Void();
|
||||
ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.getDescription().c_str());
|
||||
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
|
||||
-1, "Error while calling setCurrentUsbFunctionsCb");
|
||||
}
|
||||
} // namespace implementation
|
||||
} // namespace V1_2
|
||||
} // namespace gadget
|
||||
} // namespace usb
|
||||
} // namespace hardware
|
||||
} // namespace android
|
||||
} // aidl
|
||||
|
Reference in New Issue
Block a user