From c6802aa729d11b32a6ca9d4500b8e72fc3686e6f Mon Sep 17 00:00:00 2001 From: Badhri Jagan Sridharan Date: Thu, 1 Nov 2018 12:24:20 -0700 Subject: [PATCH 1/2] Check for preexisting flag before enabling the gadget onRegistration callback gets triggered as soon has the serviceNotification object is registered with registerForNotifications. The preexisting flag in the callback is set if the gadget hal service dies and restarts. Use this to differentiate between the onRegistration call for the registerForNotifications vs actual gadget hal service restart. Do not reset usb gadget if the onRegistration callback is called as a result of registerForNotifications. This will overcome the occasional reset of the usb gadget hence the connection during phone bootup. Bug: 121448199 Test: Verified that gadget reset does not happen Change-Id: I3b94e55289bb3a53eb9e083177fd4d54bec5f4da Merged-In: I3b94e55289bb3a53eb9e083177fd4d54bec5f4da (cherry picked from commit 222ed0fcf04cc6bc064a41c60c5f7213cf724127) --- .../android/server/usb/UsbDeviceManager.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java index 9f8042c844ded..0fb047c86c1db 100644 --- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java +++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java @@ -152,6 +152,7 @@ public class UsbDeviceManager implements ActivityManagerInternal.ScreenObserver private static final int MSG_SET_FUNCTIONS_TIMEOUT = 15; private static final int MSG_GET_CURRENT_USB_FUNCTIONS = 16; private static final int MSG_FUNCTION_SWITCH_TIMEOUT = 17; + private static final int MSG_GADGET_HAL_REGISTERED = 18; private static final int AUDIO_MODE_SOURCE = 1; @@ -1732,10 +1733,16 @@ public class UsbDeviceManager implements ActivityManagerInternal.ScreenObserver protected static final String CTL_STOP = "ctl.stop"; /** - * Adb natvie daemon + * Adb native daemon. */ protected static final String ADBD = "adbd"; + /** + * Gadget HAL fully qualified instance name for registering for ServiceNotification. + */ + protected static final String GADGET_HAL_FQ_NAME = + "android.hardware.usb.gadget@1.0::IUsbGadget"; + protected boolean mCurrentUsbFunctionsRequested; UsbHandlerHal(Looper looper, Context context, UsbDeviceManager deviceManager, @@ -1746,8 +1753,7 @@ public class UsbDeviceManager implements ActivityManagerInternal.ScreenObserver ServiceNotification serviceNotification = new ServiceNotification(); boolean ret = IServiceManager.getService() - .registerForNotifications("android.hardware.usb.gadget@1.0::IUsbGadget", - "", serviceNotification); + .registerForNotifications(GADGET_HAL_FQ_NAME, "", serviceNotification); if (!ret) { Slog.e(TAG, "Failed to register usb gadget service start notification"); return; @@ -1789,20 +1795,12 @@ public class UsbDeviceManager implements ActivityManagerInternal.ScreenObserver @Override public void onRegistration(String fqName, String name, boolean preexisting) { Slog.i(TAG, "Usb gadget hal service started " + fqName + " " + name); - synchronized (mGadgetProxyLock) { - try { - mGadgetProxy = IUsbGadget.getService(); - mGadgetProxy.linkToDeath(new UsbGadgetDeathRecipient(), - USB_GADGET_HAL_DEATH_COOKIE); - if (!mCurrentFunctionsApplied && !mCurrentUsbFunctionsRequested) { - setEnabledFunctions(mCurrentFunctions, false); - } - } catch (NoSuchElementException e) { - Slog.e(TAG, "Usb gadget hal not found", e); - } catch (RemoteException e) { - Slog.e(TAG, "Usb Gadget hal not responding", e); - } + if (!fqName.equals(GADGET_HAL_FQ_NAME)) { + Slog.e(TAG, "fqName does not match"); + return; } + + sendMessage(MSG_GADGET_HAL_REGISTERED, preexisting); } } @@ -1840,6 +1838,23 @@ public class UsbDeviceManager implements ActivityManagerInternal.ScreenObserver setEnabledFunctions(UsbManager.FUNCTION_NONE, !mAdbEnabled); } break; + case MSG_GADGET_HAL_REGISTERED: + boolean preexisting = msg.arg1 == 1; + synchronized (mGadgetProxyLock) { + try { + mGadgetProxy = IUsbGadget.getService(); + mGadgetProxy.linkToDeath(new UsbGadgetDeathRecipient(), + USB_GADGET_HAL_DEATH_COOKIE); + if (!mCurrentFunctionsApplied && !preexisting) { + setEnabledFunctions(mCurrentFunctions, false); + } + } catch (NoSuchElementException e) { + Slog.e(TAG, "Usb gadget hal not found", e); + } catch (RemoteException e) { + Slog.e(TAG, "Usb Gadget hal not responding", e); + } + } + break; default: super.handleMessage(msg); } From ec1e90a786d092359bc6db78f36d8587086e24f2 Mon Sep 17 00:00:00 2001 From: Howard Yen Date: Mon, 26 Nov 2018 15:31:47 +0800 Subject: [PATCH 2/2] Make sure mCurrentUsbFunctionsRequested flag be updated currectly There is a situation that getCurrentUsbFunctions callback function will be invoked before mCurrentUsbFunctionsRequested flag updated, thus, gadget will be reset during boot completed. Make sure mCurrentUsbFunctionsRequested flag be updated before getCurrentUsbFunctions callback function invoked. Bug: 121448199 Test: Verified that gadget reset does not happen Change-Id: Id8b58d9ccd3f62c976d1292575a06554bef622a4 Merged-In: Id8b58d9ccd3f62c976d1292575a06554bef622a4 Signed-off-by: Howard Yen (cherry picked from commit 3b747d3138e32903d75b33150c90858ee463d521) --- services/usb/java/com/android/server/usb/UsbDeviceManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java index 0fb047c86c1db..d9710d9794c80 100644 --- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java +++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java @@ -1764,8 +1764,8 @@ public class UsbDeviceManager implements ActivityManagerInternal.ScreenObserver mGadgetProxy.linkToDeath(new UsbGadgetDeathRecipient(), USB_GADGET_HAL_DEATH_COOKIE); mCurrentFunctions = UsbManager.FUNCTION_NONE; - mGadgetProxy.getCurrentUsbFunctions(new UsbGadgetCallback()); mCurrentUsbFunctionsRequested = true; + mGadgetProxy.getCurrentUsbFunctions(new UsbGadgetCallback()); } String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim(); updateState(state);