Merge changes from topic "USB AIDL Migration"

* changes:
  Migrate IUsbGadget to AIDL (Fixed Commnets)
  Migrate IUsbGadget to AIDL
This commit is contained in:
Treehugger Robot 2023-01-12 05:37:41 +00:00 committed by Gerrit Code Review
commit ef0ed10e8f
18 changed files with 1034 additions and 0 deletions

View file

@ -760,6 +760,13 @@
<instance>default</instance> <instance>default</instance>
</interface> </interface>
</hal> </hal>
<hal format="aidl" optional="true">
<name>android.hardware.usb.gadget</name>
<interface>
<name>IUsbGadget</name>
<instance>default</instance>
</interface>
</hal>
<hal format="aidl" optional="true"> <hal format="aidl" optional="true">
<name>android.hardware.vibrator</name> <name>android.hardware.vibrator</name>
<version>1-2</version> <version>1-2</version>

View file

@ -0,0 +1,37 @@
// Copyright (C) 2022 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.
// 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"],
}
aidl_interface {
name: "android.hardware.usb.gadget",
vendor_available: true,
srcs: ["android/hardware/usb/gadget/*.aidl"],
stability: "vintf",
backend: {
cpp: {
enabled: false,
},
java: {
sdk_version: "module_current",
},
},
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (C) 2022 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.
* 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.
*/
///////////////////////////////////////////////////////////////////////////////
// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. //
///////////////////////////////////////////////////////////////////////////////
// This file is a snapshot of an AIDL file. Do not edit it manually. There are
// two cases:
// 1). this is a frozen version file - do not edit this in any case.
// 2). this is a 'current' file. If you make a backwards compatible change to
// the interface (from the latest frozen version), the build system will
// prompt you to update this file with `m <name>-update-api`.
//
// You must not make a backward incompatible change to any AIDL file built
// with the aidl_interface module type with versions property set. The module
// type is used to build AIDL files in a way that they can be used across
// independently updatable components of the system. If a device is shipped
// with such a backward incompatible change, it has a high risk of breaking
// later when a module using the interface is updated, e.g., Mainline modules.
package android.hardware.usb.gadget;
@VintfStability
parcelable GadgetFunction {
const long NONE = 0;
const long ADB = 1;
const long ACCESSORY = 2;
const long MTP = 4;
const long MIDI = 8;
const long PTP = 16;
const long RNDIS = 32;
const long AUDIO_SOURCE = 64;
const long NCM = 1024;
}

View file

@ -0,0 +1,41 @@
/*
* Copyright (C) 2022 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.
* 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.
*/
///////////////////////////////////////////////////////////////////////////////
// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. //
///////////////////////////////////////////////////////////////////////////////
// This file is a snapshot of an AIDL file. Do not edit it manually. There are
// two cases:
// 1). this is a frozen version file - do not edit this in any case.
// 2). this is a 'current' file. If you make a backwards compatible change to
// the interface (from the latest frozen version), the build system will
// prompt you to update this file with `m <name>-update-api`.
//
// You must not make a backward incompatible change to any AIDL file built
// with the aidl_interface module type with versions property set. The module
// type is used to build AIDL files in a way that they can be used across
// independently updatable components of the system. If a device is shipped
// with such a backward incompatible change, it has a high risk of breaking
// later when a module using the interface is updated, e.g., Mainline modules.
package android.hardware.usb.gadget;
@VintfStability
interface IUsbGadget {
oneway void setCurrentUsbFunctions(in long functions, in android.hardware.usb.gadget.IUsbGadgetCallback callback, in long timeoutMs, long transactionId);
oneway void getCurrentUsbFunctions(in android.hardware.usb.gadget.IUsbGadgetCallback callback, long transactionId);
oneway void getUsbSpeed(in android.hardware.usb.gadget.IUsbGadgetCallback callback, long transactionId);
oneway void reset();
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (C) 2022 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.
* 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.
*/
///////////////////////////////////////////////////////////////////////////////
// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. //
///////////////////////////////////////////////////////////////////////////////
// This file is a snapshot of an AIDL file. Do not edit it manually. There are
// two cases:
// 1). this is a frozen version file - do not edit this in any case.
// 2). this is a 'current' file. If you make a backwards compatible change to
// the interface (from the latest frozen version), the build system will
// prompt you to update this file with `m <name>-update-api`.
//
// You must not make a backward incompatible change to any AIDL file built
// with the aidl_interface module type with versions property set. The module
// type is used to build AIDL files in a way that they can be used across
// independently updatable components of the system. If a device is shipped
// with such a backward incompatible change, it has a high risk of breaking
// later when a module using the interface is updated, e.g., Mainline modules.
package android.hardware.usb.gadget;
@VintfStability
interface IUsbGadgetCallback {
oneway void setCurrentUsbFunctionsCb(in long functions, in android.hardware.usb.gadget.Status status, long transactionId);
oneway void getCurrentUsbFunctionsCb(in long functions, in android.hardware.usb.gadget.Status status, long transactionId);
oneway void getUsbSpeedCb(in android.hardware.usb.gadget.UsbSpeed speed, long transactionId);
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (C) 2022 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.
* 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.
*/
///////////////////////////////////////////////////////////////////////////////
// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. //
///////////////////////////////////////////////////////////////////////////////
// This file is a snapshot of an AIDL file. Do not edit it manually. There are
// two cases:
// 1). this is a frozen version file - do not edit this in any case.
// 2). this is a 'current' file. If you make a backwards compatible change to
// the interface (from the latest frozen version), the build system will
// prompt you to update this file with `m <name>-update-api`.
//
// You must not make a backward incompatible change to any AIDL file built
// with the aidl_interface module type with versions property set. The module
// type is used to build AIDL files in a way that they can be used across
// independently updatable components of the system. If a device is shipped
// with such a backward incompatible change, it has a high risk of breaking
// later when a module using the interface is updated, e.g., Mainline modules.
package android.hardware.usb.gadget;
@Backing(type="int") @VintfStability
enum Status {
SUCCESS = 0,
ERROR = 1,
FUNCTIONS_APPLIED = 2,
FUNCTIONS_NOT_APPLIED = 3,
CONFIGURATION_NOT_SUPPORTED = 4,
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (C) 2022 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.
* 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.
*/
///////////////////////////////////////////////////////////////////////////////
// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. //
///////////////////////////////////////////////////////////////////////////////
// This file is a snapshot of an AIDL file. Do not edit it manually. There are
// two cases:
// 1). this is a frozen version file - do not edit this in any case.
// 2). this is a 'current' file. If you make a backwards compatible change to
// the interface (from the latest frozen version), the build system will
// prompt you to update this file with `m <name>-update-api`.
//
// You must not make a backward incompatible change to any AIDL file built
// with the aidl_interface module type with versions property set. The module
// type is used to build AIDL files in a way that they can be used across
// independently updatable components of the system. If a device is shipped
// with such a backward incompatible change, it has a high risk of breaking
// later when a module using the interface is updated, e.g., Mainline modules.
package android.hardware.usb.gadget;
@Backing(type="int") @VintfStability
enum UsbSpeed {
UNKNOWN = 0,
LOWSPEED = 1,
FULLSPEED = 2,
HIGHSPEED = 3,
SUPERSPEED = 4,
SUPERSPEED_10Gb = 5,
SUPERSPEED_20Gb = 6,
}

View file

@ -0,0 +1,57 @@
/*
* Copyright (C) 2022 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.
* 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 android.hardware.usb.gadget;
@VintfStability
parcelable GadgetFunction {
/**
* Removes all the functions and pulls down the gadget.
*/
const long NONE = 0;
/**
* Android Debug Bridge function.
*/
const long ADB = 1;
/**
* Android open accessory protocol function.
*/
const long ACCESSORY = 1 << 1;
/**
* Media Transfer protocol function.
*/
const long MTP = 1 << 2;
/**
* Peripheral mode USB Midi function.
*/
const long MIDI = 1 << 3;
/**
* Picture transfer protocol function.
*/
const long PTP = 1 << 4;
/**
* Tethering function.
*/
const long RNDIS = 1 << 5;
/**
* AOAv2.0 - Audio Source function.
*/
const long AUDIO_SOURCE = 1 << 6;
/**
* NCM - NCM function.
*/
const long NCM = 1 << 10;
}

View file

@ -0,0 +1,65 @@
/*
* Copyright (C) 2022 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.
* 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 android.hardware.usb.gadget;
import android.hardware.usb.gadget.GadgetFunction;
import android.hardware.usb.gadget.IUsbGadgetCallback;
@VintfStability
oneway interface IUsbGadget {
/**
* This function is used to set the current USB gadget configuration.
* Usb gadget needs to be reset if an USB configuration is already.
*
* @param functions The GadgetFunction bitmap. See GadgetFunction for
* the value of each bit.
* @param callback IUsbGadgetCallback::setCurrentUsbFunctionsCb used to
* propagate back the status.
* @param timeoutMs The maximum time (in milliseconds) within which the
* IUsbGadgetCallback needs to be returned.
* @param transactionId ID to be used when invoking the callback.
*
*/
void setCurrentUsbFunctions(in long functions, in IUsbGadgetCallback callback,
in long timeoutMs, long transactionId);
/**
* This function is used to query the USB functions included in the
* current USB configuration.
*
* @param callback IUsbGadgetCallback::getCurrentUsbFunctionsCb used to
* propagate the current functions list.
* @param transactionId ID to be used when invoking the callback.
*/
void getCurrentUsbFunctions(in IUsbGadgetCallback callback, long transactionId);
/**
* The function is used to query current USB speed.
*
* @param callback IUsbGadgetCallback::getUsbSpeedCb used to propagate
* current USB speed.
* @param transactionId ID to be used when invoking the callback.
*/
void getUsbSpeed(in IUsbGadgetCallback callback, long transactionId);
/**
* This function is used to reset USB gadget driver.
* Performs USB data connection reset. The connection will disconnect and
* reconnect.
*/
void reset();
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (C) 2022 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.
* 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 android.hardware.usb.gadget;
import android.hardware.usb.gadget.GadgetFunction;
import android.hardware.usb.gadget.Status;
import android.hardware.usb.gadget.UsbSpeed;
@VintfStability
oneway interface IUsbGadgetCallback {
/**
* Callback function used to propagate the status of configuration
* switch to the caller.
*
* @param functions list of functions defined by GadgetFunction
* included in the current USB gadget composition.
* @param status SUCCESS when the functions are applied.
* FUNCTIONS_NOT_SUPPORTED when the configuration is
* not supported.
* ERROR otherwise.
* @param transactionId ID to be used when invoking the callback.
*/
void setCurrentUsbFunctionsCb(in long functions, in Status status, long transactionId);
/**
* Callback function used to propagate the current USB gadget
* configuration.
* @param functions The GadgetFunction bitmap. See GadgetFunction for
* the value of each bit.
* @param status FUNCTIONS_APPLIED when list of functions have been
* applied.
* FUNCTIONS_NOT_APPLIED when the functions have not
* been applied.
* ERROR otherwise.
* @param transactionId ID to be used when invoking the callback.
*/
void getCurrentUsbFunctionsCb(in long functions, in Status status, long transactionId);
/**
* Used to convey the current USB speed to the caller.
* Must be called either when USB state changes due to USB enumeration or
* when caller requested for USB speed through getUsbSpeed.
*
* @param speed USB Speed defined by UsbSpeed showed current USB
* connection speed.
* @param transactionId ID to be used when invoking the callback.
*/
void getUsbSpeedCb(in UsbSpeed speed, long transactionId);
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (C) 2022 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.
* 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 android.hardware.usb.gadget;
@VintfStability
@Backing(type="int")
enum Status {
SUCCESS = 0,
/**
* Error value when the HAL operation fails for reasons not listed here.
*/
ERROR = 1,
/**
* USB configuration applied successfully.
*/
FUNCTIONS_APPLIED = 2,
/**
* USB confgiuration failed to apply.
*/
FUNCTIONS_NOT_APPLIED = 3,
/**
* USB configuration not supported.
*/
CONFIGURATION_NOT_SUPPORTED = 4,
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (C) 2022 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.
* 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 android.hardware.usb.gadget;
@VintfStability
@Backing(type="int")
enum UsbSpeed {
/**
* UNKNOWN - Not Connected or Unsupported Speed
*/
UNKNOWN = 0,
/**
* USB Low Speed
*/
LOWSPEED = 1,
/**
* USB Full Speed
*/
FULLSPEED = 2,
/**
* USB High Speed
*/
HIGHSPEED = 3,
/**
* USB Super Speed
*/
SUPERSPEED = 4,
/**
* USB Super Speed 10Gbps
*/
SUPERSPEED_10Gb = 5,
/**
* USB Super Speed 20Gbps
*/
SUPERSPEED_20Gb = 6,
}

View file

@ -0,0 +1,47 @@
//
// Copyright (C) 2022 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.
// 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_binary {
name: "android.hardware.usb.gadget-service.example",
relative_install_path: "hw",
init_rc: ["android.hardware.usb.gadget-service.example.rc"],
vintf_fragments: [
"android.hardware.usb.gadget-service.example.xml",
],
vendor: true,
srcs: ["service_gadget.cpp", "UsbGadget.cpp"],
cflags: ["-Wall", "-Werror"],
shared_libs: [
"libbase",
"libbinder",
"libhidlbase",
"liblog",
"libutils",
"libhardware",
"android.hardware.usb.gadget-V1-ndk",
"android.frameworks.stats-V1-ndk",
"libcutils",
"libbinder_ndk",
],
}

View file

@ -0,0 +1,292 @@
/*
* 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.
* 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.
*/
#define LOG_TAG "android.hardware.usb.gadget.aidl-service"
#include "UsbGadget.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/inotify.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <aidl/android/frameworks/stats/IStats.h>
namespace aidl {
namespace android {
namespace hardware {
namespace usb {
namespace gadget {
string enabledPath;
constexpr char kHsi2cPath[] = "/sys/devices/platform/10d50000.hsi2c";
constexpr char kI2CPath[] = "/sys/devices/platform/10d50000.hsi2c/i2c-";
constexpr char kAccessoryLimitCurrent[] = "i2c-max77759tcpc/usb_limit_accessory_current";
constexpr char kAccessoryLimitCurrentEnable[] = "i2c-max77759tcpc/usb_limit_accessory_enable";
UsbGadget::UsbGadget() : mGadgetIrqPath("") {}
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;
}
ScopedAStatus UsbGadget::getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback>& callback,
int64_t in_transactionId) {
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;
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::UNKNOWN;
} else {
ALOGE("Fail to read current speed");
mUsbSpeed = UsbSpeed::UNKNOWN;
}
if (callback) {
ScopedAStatus ret = callback->getUsbSpeedCb(mUsbSpeed, in_transactionId);
if (!ret.isOk()) ALOGE("Call to getUsbSpeedCb failed %s", ret.getDescription().c_str());
}
return ScopedAStatus::ok();
}
Status UsbGadget::tearDownGadget() {
return Status::SUCCESS;
}
ScopedAStatus UsbGadget::reset() {
return ScopedAStatus::ok();
}
Status UsbGadget::setupFunctions(long functions, const shared_ptr<IUsbGadgetCallback>& callback,
uint64_t timeout, int64_t in_transactionId) {
bool ffsEnabled = false;
ALOGI("functions: %ld, timeout: %ld", functions, timeout);
if ((functions & GadgetFunction::ADB) != 0) {
ffsEnabled = true;
}
if ((functions & GadgetFunction::NCM) != 0) {
ALOGI("setCurrentUsbFunctions ncm");
}
// Pull up the gadget right away when there are no ffs functions.
if (!ffsEnabled) {
mCurrentUsbFunctionsApplied = true;
if (callback)
callback->setCurrentUsbFunctionsCb(functions, Status::SUCCESS, in_transactionId);
return Status::SUCCESS;
}
return Status::SUCCESS;
}
Status getI2cBusHelper(string* name) {
DIR* dp;
dp = opendir(kHsi2cPath);
if (dp != NULL) {
struct dirent* ep;
while ((ep = readdir(dp))) {
if (ep->d_type == DT_DIR) {
if (string::npos != string(ep->d_name).find("i2c-")) {
std::strtok(ep->d_name, "-");
*name = std::strtok(NULL, "-");
}
}
}
closedir(dp);
return Status::SUCCESS;
}
ALOGE("Failed to open %s", kHsi2cPath);
return Status::ERROR;
}
ScopedAStatus UsbGadget::setCurrentUsbFunctions(long functions,
const shared_ptr<IUsbGadgetCallback>& callback,
int64_t timeout, int64_t in_transactionId) {
std::unique_lock<std::mutex> lk(mLockSetCurrentFunction);
std::string current_usb_power_operation_mode, current_usb_type;
std::string usb_limit_sink_enable;
string accessoryCurrentLimitEnablePath, accessoryCurrentLimitPath, path;
ALOGI("enter setCurrentUsbFunctions, in_transactionId=%ld , %ld", in_transactionId, timeout);
mCurrentUsbFunctions = functions;
mCurrentUsbFunctionsApplied = false;
getI2cBusHelper(&path);
accessoryCurrentLimitPath = kI2CPath + path + "/" + kAccessoryLimitCurrent;
accessoryCurrentLimitEnablePath = kI2CPath + path + "/" + kAccessoryLimitCurrentEnable;
// Get the gadget IRQ number before tearDownGadget()
if (mGadgetIrqPath.empty()) getUsbGadgetIrqPath();
// Unlink the gadget and stop the monitor if running.
Status status = tearDownGadget();
if (status != Status::SUCCESS) {
goto error;
}
ALOGI("Returned from tearDown gadget");
// Leave the gadget pulled down to give time for the host to sense disconnect.
// usleep(kDisconnectWaitUs);
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.getDescription().c_str());
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Error while calling setCurrentUsbFunctionsCb");
}
status = setupFunctions(functions, callback, timeout, in_transactionId);
if (status != Status::SUCCESS) {
goto error;
}
if (functions & GadgetFunction::NCM) {
if (!mGadgetIrqPath.empty()) {
if (!WriteStringToFile(BIG_CORE, mGadgetIrqPath))
ALOGI("Cannot move gadget IRQ to big core, path:%s", mGadgetIrqPath.c_str());
}
} else {
if (!mGadgetIrqPath.empty()) {
if (!WriteStringToFile(MEDIUM_CORE, mGadgetIrqPath))
ALOGI("Cannot move gadget IRQ to medium core, path:%s", mGadgetIrqPath.c_str());
}
}
if (ReadFileToString(CURRENT_USB_TYPE_PATH, &current_usb_type))
current_usb_type = Trim(current_usb_type);
if (ReadFileToString(CURRENT_USB_POWER_OPERATION_MODE_PATH, &current_usb_power_operation_mode))
current_usb_power_operation_mode = Trim(current_usb_power_operation_mode);
if (functions & GadgetFunction::ACCESSORY && current_usb_type == "Unknown SDP [CDP] DCP" &&
(current_usb_power_operation_mode == "default" ||
current_usb_power_operation_mode == "1.5A")) {
if (!WriteStringToFile("1300000", accessoryCurrentLimitPath)) {
ALOGI("Write 1.3A to limit current fail");
} else {
if (!WriteStringToFile("1", accessoryCurrentLimitEnablePath)) {
ALOGI("Enable limit current fail");
}
}
} else {
if (!WriteStringToFile("0", accessoryCurrentLimitEnablePath))
ALOGI("unvote accessory limit current failed");
}
ALOGI("Usb Gadget setcurrent functions called successfully");
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Usb Gadget setcurrent functions called successfully");
error:
ALOGI("Usb Gadget setcurrent functions failed");
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.getDescription().c_str());
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Error while calling setCurrentUsbFunctionsCb");
}
} // namespace gadget
} // namespace usb
} // namespace hardware
} // namespace android
} // namespace aidl

View file

@ -0,0 +1,115 @@
/*
* 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.
* 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.
*/
#pragma once
#include <aidl/android/hardware/usb/gadget/BnUsbGadget.h>
#include <aidl/android/hardware/usb/gadget/BnUsbGadgetCallback.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 <android-base/file.h>
#include <android-base/parseint.h>
#include <android-base/properties.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <sched.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <utils/Log.h>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
namespace aidl {
namespace android {
namespace hardware {
namespace usb {
namespace gadget {
using ::aidl::android::hardware::usb::gadget::GadgetFunction;
using ::aidl::android::hardware::usb::gadget::IUsbGadget;
using ::aidl::android::hardware::usb::gadget::IUsbGadgetCallback;
using ::aidl::android::hardware::usb::gadget::Status;
using ::aidl::android::hardware::usb::gadget::UsbSpeed;
using ::android::base::GetProperty;
using ::android::base::ParseUint;
using ::android::base::ReadFileToString;
using ::android::base::SetProperty;
using ::android::base::Trim;
using ::android::base::unique_fd;
using ::android::base::WriteStringToFile;
using ::ndk::ScopedAStatus;
using ::std::shared_ptr;
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
// static MonitorFfs monitorFfs(kGadgetName);
#define SPEED_PATH UDC_PATH "current_speed"
#define BIG_CORE "6"
#define MEDIUM_CORE "4"
#define POWER_SUPPLY_PATH "/sys/class/power_supply/usb/"
#define USB_PORT0_PATH "/sys/class/typec/port0/"
#define CURRENT_MAX_PATH POWER_SUPPLY_PATH "current_max"
#define CURRENT_USB_TYPE_PATH POWER_SUPPLY_PATH "usb_type"
#define CURRENT_USB_POWER_OPERATION_MODE_PATH USB_PORT0_PATH "power_operation_mode"
struct UsbGadget : public BnUsbGadget {
UsbGadget();
// Makes sure that only one request is processed at a time.
std::mutex mLockSetCurrentFunction;
std::string mGadgetIrqPath;
long mCurrentUsbFunctions;
bool mCurrentUsbFunctionsApplied;
UsbSpeed mUsbSpeed;
ScopedAStatus setCurrentUsbFunctions(long functions,
const shared_ptr<IUsbGadgetCallback>& callback,
int64_t timeout, int64_t in_transactionId) override;
ScopedAStatus getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback>& callback,
int64_t in_transactionId) override;
ScopedAStatus reset() override;
ScopedAStatus getUsbSpeed(const shared_ptr<IUsbGadgetCallback>& callback,
int64_t in_transactionId) override;
private:
Status tearDownGadget();
Status getUsbGadgetIrqPath();
Status setupFunctions(long functions, const shared_ptr<IUsbGadgetCallback>& callback,
uint64_t timeout, int64_t in_transactionId);
};
} // namespace gadget
} // namespace usb
} // namespace hardware
} // namespace android
} // namespace aidl

View file

@ -0,0 +1,4 @@
service vendor.usb_gadget_default /vendor/bin/hw/android.hardware.usb.gadget-service.example
class hal
user system
group system

View file

@ -0,0 +1,10 @@
<manifest version="1.0" type="device">
<hal format="aidl">
<name>android.hardware.usb.gadget</name>
<version>1</version>
<interface>
<name>IUsbGadget</name>
<instance>default</instance>
</interface>
</hal>
</manifest>

View file

@ -0,0 +1,35 @@
/*
* Copyright (C) 2022 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.
* 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.
*/
#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include "UsbGadget.h"
/*using android::OK;
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::sp;
using android::status_t;*/
using ::aidl::android::hardware::usb::gadget::UsbGadget;
int main() {
ABinderProcess_setThreadPoolMaxThreadCount(0);
std::shared_ptr<UsbGadget> usbgadget = ndk::SharedRefBase::make<UsbGadget>();
const std::string instance = std::string() + UsbGadget::descriptor + "/default";
binder_status_t status =
AServiceManager_addService(usbgadget->asBinder().get(), instance.c_str());
CHECK(status == STATUS_OK);
ABinderProcess_joinThreadPool();
return -1;
}