diff options
| -rw-r--r-- | Android.bp | 2 | ||||
| -rw-r--r-- | cmds/svc/src/com/android/commands/svc/UsbCommand.java | 33 | ||||
| -rw-r--r-- | core/api/module-lib-current.txt | 21 | ||||
| -rw-r--r-- | core/java/android/hardware/usb/IUsbManager.aidl | 6 | ||||
| -rw-r--r-- | core/java/android/hardware/usb/UsbManager.java | 239 | ||||
| -rw-r--r-- | services/usb/Android.bp | 1 | ||||
| -rw-r--r-- | services/usb/java/com/android/server/usb/UsbDeviceManager.java | 97 | ||||
| -rw-r--r-- | services/usb/java/com/android/server/usb/UsbService.java | 25 |
8 files changed, 420 insertions, 4 deletions
diff --git a/Android.bp b/Android.bp index 6b39b514a2df..27378d4fcc42 100644 --- a/Android.bp +++ b/Android.bp @@ -515,6 +515,8 @@ java_library { "android.hardware.usb-V1.1-java-constants", "android.hardware.usb-V1.2-java-constants", "android.hardware.usb.gadget-V1.0-java", + "android.hardware.usb.gadget-V1.1-java", + "android.hardware.usb.gadget-V1.2-java", "android.hardware.vibrator-V1.0-java", "android.hardware.vibrator-V1.1-java", "android.hardware.vibrator-V1.2-java", diff --git a/cmds/svc/src/com/android/commands/svc/UsbCommand.java b/cmds/svc/src/com/android/commands/svc/UsbCommand.java index cd751f4e2d20..227b9e24f173 100644 --- a/cmds/svc/src/com/android/commands/svc/UsbCommand.java +++ b/cmds/svc/src/com/android/commands/svc/UsbCommand.java @@ -42,10 +42,20 @@ public class UsbCommand extends Svc.Command { + " Sets the functions which, if the device was charging, become current on" + "screen unlock. If function is blank, turn off this feature.\n" + " svc usb getFunctions\n" - + " Gets the list of currently enabled functions\n" + + " Gets the list of currently enabled functions\n" + + " possible values of [function] are any of 'mtp', 'ptp', 'rndis',\n" + + " 'midi', 'ncm (if supporting gadget hal v1.2)'\n" + " svc usb resetUsbGadget\n" - + " Reset usb gadget\n\n" - + "possible values of [function] are any of 'mtp', 'ptp', 'rndis', 'midi'\n"; + + " Reset usb gadget\n" + + " svc usb getUsbSpeed\n" + + " Gets current USB speed\n" + + " possible values of USB speed are any of 'low speed', 'full speed',\n" + + " 'high speed', 'super speed', 'super speed (10G)',\n" + + " 'super speed (20G)', or higher (future extension)\n" + + " svc usb getGadgetHalVersion\n" + + " Gets current Gadget Hal Version\n" + + " possible values of Hal version are any of 'unknown', 'V1_0', 'V1_1',\n" + + " 'V1_2'\n"; } @Override @@ -84,6 +94,23 @@ public class UsbCommand extends Svc.Command { System.err.println("Error communicating with UsbManager: " + e); } return; + } else if ("getUsbSpeed".equals(args[1])) { + try { + System.err.println( + UsbManager.usbSpeedToBandwidth(usbMgr.getCurrentUsbSpeed())); + } catch (RemoteException e) { + System.err.println("Error communicating with UsbManager: " + e); + } + return; + } else if ("getGadgetHalVersion".equals(args[1])) { + try { + System.err.println( + UsbManager.usbGadgetHalVersionToString( + usbMgr.getGadgetHalVersion())); + } catch (RemoteException e) { + System.err.println("Error communicating with UsbManager: " + e); + } + return; } } System.err.println(longHelp()); diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt index 3617967bfbdf..8a369c8f27c2 100644 --- a/core/api/module-lib-current.txt +++ b/core/api/module-lib-current.txt @@ -43,6 +43,27 @@ package android.content.rollback { } +package android.hardware.usb { + + public class UsbManager { + method @RequiresPermission(android.Manifest.permission.MANAGE_USB) public int getGadgetHalVersion(); + method public int getUsbBandwidth(); + field public static final int GADGET_HAL_NOT_SUPPORTED = -1; // 0xffffffff + field public static final int GADGET_HAL_V1_0 = 10; // 0xa + field public static final int GADGET_HAL_V1_1 = 11; // 0xb + field public static final int GADGET_HAL_V1_2 = 12; // 0xc + field public static final int USB_DATA_TRANSFER_RATE_10G = 10240; // 0x2800 + field public static final int USB_DATA_TRANSFER_RATE_20G = 20480; // 0x5000 + field public static final int USB_DATA_TRANSFER_RATE_40G = 40960; // 0xa000 + field public static final int USB_DATA_TRANSFER_RATE_5G = 5120; // 0x1400 + field public static final int USB_DATA_TRANSFER_RATE_FULL_SPEED = 12; // 0xc + field public static final int USB_DATA_TRANSFER_RATE_HIGH_SPEED = 480; // 0x1e0 + field public static final int USB_DATA_TRANSFER_RATE_LOW_SPEED = 2; // 0x2 + field public static final int USB_DATA_TRANSFER_RATE_UNKNOWN = -1; // 0xffffffff + } + +} + package android.media { public class AudioManager { diff --git a/core/java/android/hardware/usb/IUsbManager.aidl b/core/java/android/hardware/usb/IUsbManager.aidl index e32865ebeced..ca79901c2bbe 100644 --- a/core/java/android/hardware/usb/IUsbManager.aidl +++ b/core/java/android/hardware/usb/IUsbManager.aidl @@ -118,6 +118,12 @@ interface IUsbManager /* Gets the current USB functions. */ long getCurrentFunctions(); + /* Gets the current USB Speed. */ + int getCurrentUsbSpeed(); + + /* Gets the Gadget Hal Version. */ + int getGadgetHalVersion(); + /* Sets the screen unlocked USB function(s), which will be set automatically * when the screen is unlocked. */ diff --git a/core/java/android/hardware/usb/UsbManager.java b/core/java/android/hardware/usb/UsbManager.java index 62a5782cb55d..aac57fe88b17 100644 --- a/core/java/android/hardware/usb/UsbManager.java +++ b/core/java/android/hardware/usb/UsbManager.java @@ -18,6 +18,7 @@ package android.hardware.usb; import android.Manifest; +import android.annotation.IntDef; import android.annotation.LongDef; import android.annotation.NonNull; import android.annotation.Nullable; @@ -34,6 +35,7 @@ import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.hardware.usb.gadget.V1_0.GadgetFunction; +import android.hardware.usb.gadget.V1_2.UsbSpeed; import android.os.Build; import android.os.Bundle; import android.os.ParcelFileDescriptor; @@ -288,6 +290,34 @@ public class UsbManager { public static final String USB_FUNCTION_NCM = "ncm"; /** + * Name of Gadget Hal Not Present; + * + * {@hide} + */ + public static final String GADGET_HAL_UNKNOWN = "unknown"; + + /** + * Name of the USB Gadget Hal Version v1.0; + * + * {@hide} + */ + public static final String GADGET_HAL_VERSION_1_0 = "V1_0"; + + /** + * Name of the USB Gadget Hal Version v1.1; + * + * {@hide} + */ + public static final String GADGET_HAL_VERSION_1_1 = "V1_1"; + + /** + * Name of the USB Gadget Hal Version v1.2; + * + * {@hide} + */ + public static final String GADGET_HAL_VERSION_1_2 = "V1_2"; + + /** * Name of extra for {@link #ACTION_USB_PORT_CHANGED} * containing the {@link UsbPort} object for the port. * @@ -390,6 +420,102 @@ public class UsbManager { public static final String EXTRA_CAN_BE_DEFAULT = "android.hardware.usb.extra.CAN_BE_DEFAULT"; /** + * The Value for USB gadget hal is not presented. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int GADGET_HAL_NOT_SUPPORTED = -1; + + /** + * Value for Gadget Hal Version v1.0. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int GADGET_HAL_V1_0 = 10; + + /** + * Value for Gadget Hal Version v1.1. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int GADGET_HAL_V1_1 = 11; + + /** + * Value for Gadget Hal Version v1.2. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int GADGET_HAL_V1_2 = 12; + + /** + * Value for USB_STATE is not configured. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int USB_DATA_TRANSFER_RATE_UNKNOWN = -1; + + /** + * Value for USB Transfer Rate of Low Speed in Mbps (real value is 1.5Mbps). + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int USB_DATA_TRANSFER_RATE_LOW_SPEED = 2; + + /** + * Value for USB Transfer Rate of Full Speed in Mbps. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int USB_DATA_TRANSFER_RATE_FULL_SPEED = 12; + + /** + * Value for USB Transfer Rate of High Speed in Mbps. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int USB_DATA_TRANSFER_RATE_HIGH_SPEED = 480; + + /** + * Value for USB Transfer Rate of Super Speed in Mbps. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int USB_DATA_TRANSFER_RATE_5G = 5 * 1024; + + /** + * Value for USB Transfer Rate of 10G. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int USB_DATA_TRANSFER_RATE_10G = 10 * 1024; + + /** + * Value for USB Transfer Rate of 20G. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int USB_DATA_TRANSFER_RATE_20G = 20 * 1024; + + /** + * Value for USB Transfer Rate of 40G. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public static final int USB_DATA_TRANSFER_RATE_40G = 40 * 1024; + + /** * Code for the charging usb function. Passed into {@link #setCurrentFunctions(long)} * {@hide} */ @@ -482,6 +608,15 @@ public class UsbManager { }) public @interface UsbFunctionMode {} + /** @hide */ + @IntDef(flag = true, prefix = { "GADGET_HAL_" }, value = { + GADGET_HAL_NOT_SUPPORTED, + GADGET_HAL_V1_0, + GADGET_HAL_V1_1, + GADGET_HAL_V1_2, + }) + public @interface UsbGadgetHalVersion {} + private final Context mContext; private final IUsbManager mService; @@ -894,6 +1029,53 @@ public class UsbManager { } /** + * Get the Current USB Bandwidth. + * <p> + * This function returns the current USB bandwidth through USB Gadget HAL. + * It should be used when Android device is in USB peripheral mode and + * connects to a USB host. If USB state is not configued, API will return + * {@value #USB_DATA_TRANSFER_RATE_UNKNOWN}. In addition, the unit of the + * return value is Mbps. + * </p> + * + * @return The value of currently USB Bandwidth. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public int getUsbBandwidth() { + int usbSpeed; + + try { + usbSpeed = mService.getCurrentUsbSpeed(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + + return usbSpeedToBandwidth(usbSpeed); + } + + /** + * Get the Current Gadget Hal Version. + * <p> + * This function returns the current Gadget Hal Version. + * </p> + * + * @return a integer {@code GADGET_HAL_*} represent hal version. + * + * {@hide} + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + @RequiresPermission(Manifest.permission.MANAGE_USB) + public @UsbGadgetHalVersion int getGadgetHalVersion() { + try { + return mService.getGadgetHalVersion(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** * Resets the USB Gadget. * <p> * Performs USB data stack reset through USB Gadget HAL. @@ -1084,4 +1266,61 @@ public class UsbManager { } return ret; } + + /** + * Converts the given integer of USB speed to corresponding bandwidth. + * + * @return a value of USB bandwidth + * + * {@hide} + */ + public static int usbSpeedToBandwidth(int speed) { + switch (speed) { + case UsbSpeed.USB4_GEN3_40Gb: + return USB_DATA_TRANSFER_RATE_40G; + case UsbSpeed.USB4_GEN3_20Gb: + return USB_DATA_TRANSFER_RATE_20G; + case UsbSpeed.USB4_GEN2_20Gb: + return USB_DATA_TRANSFER_RATE_20G; + case UsbSpeed.USB4_GEN2_10Gb: + return USB_DATA_TRANSFER_RATE_10G; + case UsbSpeed.SUPERSPEED_20Gb: + return USB_DATA_TRANSFER_RATE_20G; + case UsbSpeed.SUPERSPEED_10Gb: + return USB_DATA_TRANSFER_RATE_10G; + case UsbSpeed.SUPERSPEED: + return USB_DATA_TRANSFER_RATE_5G; + case UsbSpeed.HIGHSPEED: + return USB_DATA_TRANSFER_RATE_HIGH_SPEED; + case UsbSpeed.FULLSPEED: + return USB_DATA_TRANSFER_RATE_FULL_SPEED; + case UsbSpeed.LOWSPEED: + return USB_DATA_TRANSFER_RATE_LOW_SPEED; + default: + return USB_DATA_TRANSFER_RATE_UNKNOWN; + } + } + + /** + * Converts the given usb gadgdet hal version to String + * + * @return String representation of Usb Gadget Hal Version + * + * {@hide} + */ + public static @NonNull String usbGadgetHalVersionToString(int version) { + String halVersion; + + if (version == GADGET_HAL_V1_2) { + halVersion = GADGET_HAL_VERSION_1_2; + } else if (version == GADGET_HAL_V1_1) { + halVersion = GADGET_HAL_VERSION_1_1; + } else if (version == GADGET_HAL_V1_0) { + halVersion = GADGET_HAL_VERSION_1_0; + } else { + halVersion = GADGET_HAL_UNKNOWN; + } + + return halVersion; + } } diff --git a/services/usb/Android.bp b/services/usb/Android.bp index 1a23c8c51a4e..aa8bbde8b32a 100644 --- a/services/usb/Android.bp +++ b/services/usb/Android.bp @@ -21,5 +21,6 @@ java_library_static { "android.hardware.usb-V1.2-java", "android.hardware.usb.gadget-V1.0-java", "android.hardware.usb.gadget-V1.1-java", + "android.hardware.usb.gadget-V1.2-java", ], } diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java index 58859e095554..3af88e1c6354 100644 --- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java +++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java @@ -55,8 +55,9 @@ import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; import android.hardware.usb.gadget.V1_0.GadgetFunction; import android.hardware.usb.gadget.V1_0.IUsbGadget; -import android.hardware.usb.gadget.V1_0.IUsbGadgetCallback; import android.hardware.usb.gadget.V1_0.Status; +import android.hardware.usb.gadget.V1_2.IUsbGadgetCallback; +import android.hardware.usb.gadget.V1_2.UsbSpeed; import android.hidl.manager.V1_0.IServiceManager; import android.hidl.manager.V1_0.IServiceNotification; import android.os.BatteryManager; @@ -166,6 +167,8 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser private static final int MSG_RESET_USB_GADGET = 19; private static final int MSG_ACCESSORY_HANDSHAKE_TIMEOUT = 20; private static final int MSG_INCREASE_SENDSTRING_COUNT = 21; + private static final int MSG_UPDATE_USB_SPEED = 22; + private static final int MSG_UPDATE_HAL_VERSION = 23; private static final int AUDIO_MODE_SOURCE = 1; @@ -532,6 +535,8 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser protected SharedPreferences mSettings; protected int mCurrentUser; protected boolean mCurrentUsbFunctionsReceived; + protected int mUsbSpeed; + protected int mCurrentGadgetHalVersion; /** * The persistent property which stores whether adb is enabled or not. @@ -902,6 +907,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser } else { mPendingBootBroadcast = true; } + updateUsbSpeed(); break; case MSG_UPDATE_PORT_STATE: SomeArgs args = (SomeArgs) msg.obj; @@ -1117,6 +1123,26 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser return mCurrentAccessory; } + protected void updateUsbGadgetHalVersion() { + sendMessage(MSG_UPDATE_HAL_VERSION, null); + } + + protected void updateUsbSpeed() { + if (mCurrentGadgetHalVersion < UsbManager.GADGET_HAL_V1_0) { + mUsbSpeed = UsbSpeed.UNKNOWN; + return; + } + + if (mConnected && mConfigured) { + sendMessage(MSG_UPDATE_USB_SPEED, null); + } else { + // clear USB speed due to disconnected + mUsbSpeed = UsbSpeed.UNKNOWN; + } + + return; + } + protected void updateUsbNotification(boolean force) { if (mNotificationManager == null || !mUseUsbNotification || ("0".equals(getSystemProperty("persist.charging.notify", "")))) { @@ -1324,6 +1350,14 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser return mScreenUnlockedFunctions; } + public int getUsbSpeed() { + return mUsbSpeed; + } + + public int getGadgetHalVersion() { + return mCurrentGadgetHalVersion; + } + /** * Dump a functions mask either as proto-enums (if dumping to proto) or a string (if dumping * to a print writer) @@ -1450,6 +1484,9 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser mCurrentFunctions = UsbManager.FUNCTION_NONE; mCurrentUsbFunctionsReceived = true; + mUsbSpeed = UsbSpeed.UNKNOWN; + mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_NOT_SUPPORTED; + String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim(); updateState(state); } catch (Exception e) { @@ -1826,10 +1863,13 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser USB_GADGET_HAL_DEATH_COOKIE); mCurrentFunctions = UsbManager.FUNCTION_NONE; mCurrentUsbFunctionsRequested = true; + mUsbSpeed = UsbSpeed.UNKNOWN; + mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_V1_0; mGadgetProxy.getCurrentUsbFunctions(new UsbGadgetCallback()); } String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim(); updateState(state); + updateUsbGadgetHalVersion(); } catch (NoSuchElementException e) { Slog.e(TAG, "Usb gadget hal not found", e); } catch (RemoteException e) { @@ -1935,6 +1975,48 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser } } break; + case MSG_UPDATE_USB_SPEED: + synchronized (mGadgetProxyLock) { + if (mGadgetProxy == null) { + Slog.e(TAG, "mGadgetProxy is null"); + break; + } + + try { + android.hardware.usb.gadget.V1_2.IUsbGadget gadgetProxy = + android.hardware.usb.gadget.V1_2.IUsbGadget + .castFrom(mGadgetProxy); + if (gadgetProxy != null) { + gadgetProxy.getUsbSpeed(new UsbGadgetCallback()); + } + } catch (RemoteException e) { + Slog.e(TAG, "get UsbSpeed failed", e); + } + } + break; + case MSG_UPDATE_HAL_VERSION: + synchronized (mGadgetProxyLock) { + if (mGadgetProxy == null) { + Slog.e(TAG, "mGadgetProxy is null"); + break; + } + + android.hardware.usb.gadget.V1_2.IUsbGadget gadgetProxy = + android.hardware.usb.gadget.V1_2.IUsbGadget.castFrom(mGadgetProxy); + if (gadgetProxy == null) { + android.hardware.usb.gadget.V1_1.IUsbGadget gadgetProxyV1By1 = + android.hardware.usb.gadget.V1_1.IUsbGadget + .castFrom(mGadgetProxy); + if (gadgetProxyV1By1 == null) { + mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_V1_0; + break; + } + mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_V1_1; + break; + } + mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_V1_2; + } + break; default: super.handleMessage(msg); } @@ -1982,6 +2064,11 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser sendMessage(MSG_GET_CURRENT_USB_FUNCTIONS, functions, status == Status.FUNCTIONS_APPLIED); } + + @Override + public void getUsbSpeedCb(int speed) { + mUsbSpeed = speed; + } } private void setUsbConfig(long config, boolean chargingFunctions) { @@ -2090,6 +2177,14 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser return mHandler.getEnabledFunctions(); } + public int getCurrentUsbSpeed() { + return mHandler.getUsbSpeed(); + } + + public int getGadgetHalVersion() { + return mHandler.getGadgetHalVersion(); + } + /** * Returns a dup of the control file descriptor for the given function. */ diff --git a/services/usb/java/com/android/server/usb/UsbService.java b/services/usb/java/com/android/server/usb/UsbService.java index 444cb5cb89c7..edd4a3874c94 100644 --- a/services/usb/java/com/android/server/usb/UsbService.java +++ b/services/usb/java/com/android/server/usb/UsbService.java @@ -638,6 +638,31 @@ public class UsbService extends IUsbManager.Stub { } @Override + public int getCurrentUsbSpeed() { + Preconditions.checkNotNull(mDeviceManager, "DeviceManager must not be null"); + + final long ident = Binder.clearCallingIdentity(); + try { + return mDeviceManager.getCurrentUsbSpeed(); + } finally { + Binder.restoreCallingIdentity(ident); + } + } + + @Override + public int getGadgetHalVersion() { + mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); + Preconditions.checkNotNull(mDeviceManager, "DeviceManager must not be null"); + + final long ident = Binder.clearCallingIdentity(); + try { + return mDeviceManager.getGadgetHalVersion(); + } finally { + Binder.restoreCallingIdentity(ident); + } + } + + @Override public void resetUsbGadget() { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); Preconditions.checkNotNull(mDeviceManager, "DeviceManager must not be null"); |