| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 22 | import android.hardware.IUsbManager; |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 23 | import android.hardware.UsbAccessory; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 24 | import android.hardware.UsbConstants; |
| 25 | import android.hardware.UsbDevice; |
| 26 | import android.hardware.UsbEndpoint; |
| 27 | import android.hardware.UsbInterface; |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 28 | import android.hardware.UsbManager; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 29 | import android.net.Uri; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 30 | import android.os.Bundle; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 33 | import android.os.Parcelable; |
| 34 | import android.os.ParcelFileDescriptor; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 35 | import android.os.UEventObserver; |
| 36 | import android.provider.Settings; |
| 37 | import android.util.Log; |
| 38 | import android.util.Slog; |
| 39 | |
| 40 | import java.io.File; |
| 41 | import java.io.FileNotFoundException; |
| 42 | import java.io.FileReader; |
| 43 | import java.util.ArrayList; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 44 | import java.util.HashMap; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 45 | |
| 46 | /** |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 47 | * UsbService monitors for changes to USB state. |
| 48 | * This includes code for both USB host support (where the android device is the host) |
| 49 | * as well as USB device support (android device is connected to a USB host). |
| 50 | * Accessory mode is a special case of USB device mode, where the android device is |
| 51 | * connected to a USB host that supports the android accessory protocol. |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 52 | */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 53 | class UsbService extends IUsbManager.Stub { |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 54 | private static final String TAG = UsbService.class.getSimpleName(); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 55 | private static final boolean LOG = false; |
| 56 | |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 57 | private static final String USB_CONNECTED_MATCH = |
| 58 | "DEVPATH=/devices/virtual/switch/usb_connected"; |
| 59 | private static final String USB_CONFIGURATION_MATCH = |
| 60 | "DEVPATH=/devices/virtual/switch/usb_configuration"; |
| 61 | private static final String USB_FUNCTIONS_MATCH = |
| 62 | "DEVPATH=/devices/virtual/usb_composite/"; |
| 63 | private static final String USB_CONNECTED_PATH = |
| 64 | "/sys/class/switch/usb_connected/state"; |
| 65 | private static final String USB_CONFIGURATION_PATH = |
| 66 | "/sys/class/switch/usb_configuration/state"; |
| 67 | private static final String USB_COMPOSITE_CLASS_PATH = |
| 68 | "/sys/class/usb_composite"; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 69 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 70 | private static final int MSG_UPDATE_STATE = 0; |
| 71 | private static final int MSG_FUNCTION_ENABLED = 1; |
| 72 | private static final int MSG_FUNCTION_DISABLED = 2; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 73 | |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 74 | // Delay for debouncing USB disconnects. |
| 75 | // We often get rapid connect/disconnect events when enabling USB functions, |
| 76 | // which need debouncing. |
| 77 | private static final int UPDATE_DELAY = 1000; |
| 78 | |
| 79 | // current connected and configuration state |
| 80 | private int mConnected; |
| 81 | private int mConfiguration; |
| 82 | |
| 83 | // last broadcasted connected and configuration state |
| 84 | private int mLastConnected = -1; |
| 85 | private int mLastConfiguration = -1; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 86 | |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 87 | // lists of enabled and disabled USB functions (for USB device mode) |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 88 | private final ArrayList<String> mEnabledFunctions = new ArrayList<String>(); |
| 89 | private final ArrayList<String> mDisabledFunctions = new ArrayList<String>(); |
| 90 | |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 91 | // contains all connected USB devices (for USB host mode) |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 92 | private final HashMap<String,UsbDevice> mDevices = new HashMap<String,UsbDevice>(); |
| 93 | |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 94 | // USB busses to exclude from USB host support |
| 95 | private final String[] mHostBlacklist; |
| 96 | |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 97 | private boolean mSystemReady; |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 98 | |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 99 | private UsbAccessory mCurrentAccessory; |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 100 | // functions to restore after exiting accessory mode |
| 101 | private final ArrayList<String> mAccessoryRestoreFunctions = new ArrayList<String>(); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 102 | |
| 103 | private final Context mContext; |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 104 | private final Object mLock = new Object(); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 105 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 106 | /* |
| 107 | * Handles USB function enable/disable events (device mode) |
| 108 | */ |
| 109 | private final void functionEnabledLocked(String function, boolean enabled) { |
| 110 | boolean enteringAccessoryMode = |
| 111 | (enabled && UsbManager.USB_FUNCTION_ACCESSORY.equals(function)); |
| 112 | |
| 113 | if (enteringAccessoryMode) { |
| 114 | // keep a list of functions to reenable after exiting accessory mode |
| 115 | mAccessoryRestoreFunctions.clear(); |
| 116 | int count = mEnabledFunctions.size(); |
| 117 | for (int i = 0; i < count; i++) { |
| 118 | String f = mEnabledFunctions.get(i); |
| 119 | // RNDIS should not be restored and adb is handled automatically |
| 120 | if (!UsbManager.USB_FUNCTION_RNDIS.equals(f) && |
| 121 | !UsbManager.USB_FUNCTION_ADB.equals(f) && |
| 122 | !UsbManager.USB_FUNCTION_ACCESSORY.equals(f)) { |
| 123 | mAccessoryRestoreFunctions.add(f); |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 124 | } |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 125 | } |
| 126 | } |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 127 | if (enabled) { |
| 128 | if (!mEnabledFunctions.contains(function)) { |
| 129 | mEnabledFunctions.add(function); |
| 130 | } |
| 131 | mDisabledFunctions.remove(function); |
| 132 | } else { |
| 133 | if (!mDisabledFunctions.contains(function)) { |
| 134 | mDisabledFunctions.add(function); |
| 135 | } |
| 136 | mEnabledFunctions.remove(function); |
| 137 | } |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 138 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 139 | if (enteringAccessoryMode) { |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 140 | String[] strings = nativeGetAccessoryStrings(); |
| 141 | if (strings != null) { |
| 142 | Log.d(TAG, "entering USB accessory mode"); |
| 143 | mCurrentAccessory = new UsbAccessory(strings); |
| 144 | Intent intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); |
| 145 | intent.putExtra(UsbManager.EXTRA_ACCESSORY, mCurrentAccessory); |
| 146 | // add strings as separate extras to allow filtering |
| 147 | if (strings[0] != null) { |
| 148 | intent.putExtra(UsbManager.EXTRA_ACCESSORY_MANUFACTURER, strings[0]); |
| 149 | } |
| 150 | if (strings[1] != null) { |
| 151 | intent.putExtra(UsbManager.EXTRA_ACCESSORY_PRODUCT, strings[1]); |
| 152 | } |
| 153 | if (strings[2] != null) { |
| 154 | intent.putExtra(UsbManager.EXTRA_ACCESSORY_TYPE, strings[2]); |
| 155 | } |
| 156 | if (strings[3] != null) { |
| 157 | intent.putExtra(UsbManager.EXTRA_ACCESSORY_VERSION, strings[3]); |
| 158 | } |
| 159 | mContext.sendBroadcast(intent); |
| 160 | } else { |
| 161 | Log.e(TAG, "nativeGetAccessoryStrings failed"); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 166 | /* |
| 167 | * Listens for uevent messages from the kernel to monitor the USB state (device mode) |
| 168 | */ |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 169 | private final UEventObserver mUEventObserver = new UEventObserver() { |
| 170 | @Override |
| 171 | public void onUEvent(UEventObserver.UEvent event) { |
| 172 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 173 | Slog.v(TAG, "USB UEVENT: " + event.toString()); |
| 174 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 175 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 176 | synchronized (mLock) { |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 177 | String name = event.get("SWITCH_NAME"); |
| 178 | String state = event.get("SWITCH_STATE"); |
| 179 | if (name != null && state != null) { |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 180 | try { |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 181 | int intState = Integer.parseInt(state); |
| 182 | if ("usb_connected".equals(name)) { |
| 183 | mConnected = intState; |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 184 | // trigger an Intent broadcast |
| 185 | if (mSystemReady) { |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 186 | // debounce disconnects |
| 187 | update(mConnected == 0); |
| 188 | } |
| 189 | } else if ("usb_configuration".equals(name)) { |
| 190 | mConfiguration = intState; |
| 191 | // trigger an Intent broadcast |
| 192 | if (mSystemReady) { |
| 193 | update(mConnected == 0); |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 194 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 195 | } |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 196 | } catch (NumberFormatException e) { |
| 197 | Slog.e(TAG, "Could not parse switch state from event " + event); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 198 | } |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 199 | } else { |
| 200 | String function = event.get("FUNCTION"); |
| 201 | String enabledStr = event.get("ENABLED"); |
| 202 | if (function != null && enabledStr != null) { |
| 203 | // Note: we do not broadcast a change when a function is enabled or disabled. |
| 204 | // We just record the state change for the next broadcast. |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 205 | int what = ("1".equals(enabledStr) ? |
| 206 | MSG_FUNCTION_ENABLED : MSG_FUNCTION_DISABLED); |
| 207 | Message msg = Message.obtain(mHandler, what); |
| 208 | msg.obj = function; |
| 209 | mHandler.sendMessage(msg); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | public UsbService(Context context) { |
| 217 | mContext = context; |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 218 | mHostBlacklist = context.getResources().getStringArray( |
| 219 | com.android.internal.R.array.config_usbHostBlacklist); |
| 220 | |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 221 | init(); // set initial status |
| 222 | |
| David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 223 | if (mConfiguration >= 0) { |
| 224 | mUEventObserver.startObserving(USB_CONNECTED_MATCH); |
| 225 | mUEventObserver.startObserving(USB_CONFIGURATION_MATCH); |
| 226 | mUEventObserver.startObserving(USB_FUNCTIONS_MATCH); |
| 227 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 228 | } |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 229 | |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 230 | private final void init() { |
| 231 | char[] buffer = new char[1024]; |
| 232 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 233 | // Read initial USB state (device mode) |
| David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 234 | mConfiguration = -1; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 235 | try { |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 236 | FileReader file = new FileReader(USB_CONNECTED_PATH); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 237 | int len = file.read(buffer, 0, 1024); |
| Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 238 | file.close(); |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 239 | mConnected = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 240 | |
| 241 | file = new FileReader(USB_CONFIGURATION_PATH); |
| 242 | len = file.read(buffer, 0, 1024); |
| 243 | file.close(); |
| 244 | mConfiguration = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 245 | |
| 246 | } catch (FileNotFoundException e) { |
| David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 247 | Slog.i(TAG, "This kernel does not have USB configuration switch support"); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 248 | } catch (Exception e) { |
| 249 | Slog.e(TAG, "" , e); |
| 250 | } |
| David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 251 | if (mConfiguration < 0) |
| 252 | return; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 253 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 254 | // Read initial list of enabled and disabled functions (device mode) |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 255 | try { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 256 | File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles(); |
| 257 | for (int i = 0; i < files.length; i++) { |
| 258 | File file = new File(files[i], "enable"); |
| 259 | FileReader reader = new FileReader(file); |
| 260 | int len = reader.read(buffer, 0, 1024); |
| 261 | reader.close(); |
| 262 | int value = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 263 | String functionName = files[i].getName(); |
| 264 | if (value == 1) { |
| 265 | mEnabledFunctions.add(functionName); |
| 266 | } else { |
| 267 | mDisabledFunctions.add(functionName); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } catch (FileNotFoundException e) { |
| 271 | Slog.w(TAG, "This kernel does not have USB composite class support"); |
| 272 | } catch (Exception e) { |
| 273 | Slog.e(TAG, "" , e); |
| 274 | } |
| 275 | } |
| 276 | |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 277 | private boolean isBlackListed(String deviceName) { |
| 278 | int count = mHostBlacklist.length; |
| 279 | for (int i = 0; i < count; i++) { |
| 280 | if (deviceName.startsWith(mHostBlacklist[i])) { |
| 281 | return true; |
| 282 | } |
| 283 | } |
| 284 | return false; |
| 285 | } |
| 286 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 287 | /* returns true if the USB device should not be accessible by applications (host mode) */ |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 288 | private boolean isBlackListed(int clazz, int subClass, int protocol) { |
| 289 | // blacklist hubs |
| 290 | if (clazz == UsbConstants.USB_CLASS_HUB) return true; |
| 291 | |
| 292 | // blacklist HID boot devices (mouse and keyboard) |
| 293 | if (clazz == UsbConstants.USB_CLASS_HID && |
| 294 | subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) { |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | return false; |
| 299 | } |
| 300 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 301 | /* Called from JNI in monitorUsbHostBus() to report new USB devices (host mode) */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 302 | private void usbDeviceAdded(String deviceName, int vendorID, int productID, |
| 303 | int deviceClass, int deviceSubclass, int deviceProtocol, |
| 304 | /* array of quintuples containing id, class, subclass, protocol |
| 305 | and number of endpoints for each interface */ |
| 306 | int[] interfaceValues, |
| 307 | /* array of quadruples containing address, attributes, max packet size |
| 308 | and interval for each endpoint */ |
| 309 | int[] endpointValues) { |
| 310 | |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 311 | if (isBlackListed(deviceName) || |
| 312 | isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) { |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 316 | synchronized (mLock) { |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 317 | if (mDevices.get(deviceName) != null) { |
| 318 | Log.w(TAG, "device already on mDevices list: " + deviceName); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | int numInterfaces = interfaceValues.length / 5; |
| 323 | Parcelable[] interfaces = new UsbInterface[numInterfaces]; |
| 324 | try { |
| 325 | // repackage interfaceValues as an array of UsbInterface |
| 326 | int intf, endp, ival = 0, eval = 0; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 327 | for (intf = 0; intf < numInterfaces; intf++) { |
| 328 | int interfaceId = interfaceValues[ival++]; |
| 329 | int interfaceClass = interfaceValues[ival++]; |
| 330 | int interfaceSubclass = interfaceValues[ival++]; |
| 331 | int interfaceProtocol = interfaceValues[ival++]; |
| 332 | int numEndpoints = interfaceValues[ival++]; |
| 333 | |
| 334 | Parcelable[] endpoints = new UsbEndpoint[numEndpoints]; |
| 335 | for (endp = 0; endp < numEndpoints; endp++) { |
| 336 | int address = endpointValues[eval++]; |
| 337 | int attributes = endpointValues[eval++]; |
| 338 | int maxPacketSize = endpointValues[eval++]; |
| 339 | int interval = endpointValues[eval++]; |
| 340 | endpoints[endp] = new UsbEndpoint(address, attributes, |
| 341 | maxPacketSize, interval); |
| 342 | } |
| 343 | |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 344 | // don't allow if any interfaces are blacklisted |
| 345 | if (isBlackListed(interfaceClass, interfaceSubclass, interfaceProtocol)) { |
| 346 | return; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 347 | } |
| 348 | interfaces[intf] = new UsbInterface(interfaceId, interfaceClass, |
| 349 | interfaceSubclass, interfaceProtocol, endpoints); |
| 350 | } |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 351 | } catch (Exception e) { |
| 352 | // beware of index out of bound exceptions, which might happen if |
| 353 | // a device does not set bNumEndpoints correctly |
| 354 | Log.e(TAG, "error parsing USB descriptors", e); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | UsbDevice device = new UsbDevice(deviceName, vendorID, productID, |
| 359 | deviceClass, deviceSubclass, deviceProtocol, interfaces); |
| 360 | mDevices.put(deviceName, device); |
| 361 | |
| 362 | Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
| 363 | intent.putExtra(UsbManager.EXTRA_DEVICE_NAME, deviceName); |
| 364 | intent.putExtra(UsbManager.EXTRA_VENDOR_ID, vendorID); |
| 365 | intent.putExtra(UsbManager.EXTRA_PRODUCT_ID, productID); |
| 366 | intent.putExtra(UsbManager.EXTRA_DEVICE_CLASS, deviceClass); |
| 367 | intent.putExtra(UsbManager.EXTRA_DEVICE_SUBCLASS, deviceSubclass); |
| 368 | intent.putExtra(UsbManager.EXTRA_DEVICE_PROTOCOL, deviceProtocol); |
| 369 | intent.putExtra(UsbManager.EXTRA_DEVICE, device); |
| 370 | Log.d(TAG, "usbDeviceAdded, sending " + intent); |
| 371 | mContext.sendBroadcast(intent); |
| 372 | } |
| 373 | } |
| 374 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 375 | /* Called from JNI in monitorUsbHostBus to report USB device removal (host mode) */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 376 | private void usbDeviceRemoved(String deviceName) { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 377 | synchronized (mLock) { |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 378 | UsbDevice device = mDevices.remove(deviceName); |
| 379 | if (device != null) { |
| 380 | Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_DETACHED); |
| 381 | intent.putExtra(UsbManager.EXTRA_DEVICE_NAME, deviceName); |
| 382 | Log.d(TAG, "usbDeviceRemoved, sending " + intent); |
| 383 | mContext.sendBroadcast(intent); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 388 | private void initHostSupport() { |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 389 | // Create a thread to call into native code to wait for USB host events. |
| 390 | // This thread will call us back on usbDeviceAdded and usbDeviceRemoved. |
| 391 | Runnable runnable = new Runnable() { |
| 392 | public void run() { |
| 393 | monitorUsbHostBus(); |
| 394 | } |
| 395 | }; |
| 396 | new Thread(null, runnable, "UsbService host thread").start(); |
| Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 397 | } |
| 398 | |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 399 | void systemReady() { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 400 | synchronized (mLock) { |
| Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 401 | if (mContext.getResources().getBoolean( |
| 402 | com.android.internal.R.bool.config_hasUsbHostSupport)) { |
| 403 | // start monitoring for connected USB devices |
| 404 | initHostSupport(); |
| 405 | } |
| 406 | |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 407 | update(false); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 408 | mSystemReady = true; |
| 409 | } |
| 410 | } |
| 411 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 412 | /* |
| 413 | * Sends a message to update the USB connected and configured state (device mode). |
| 414 | * If delayed is true, then we add a small delay in sending the message to debounce |
| 415 | * the USB connection when enabling USB tethering. |
| 416 | */ |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 417 | private final void update(boolean delayed) { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 418 | mHandler.removeMessages(MSG_UPDATE_STATE); |
| 419 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE_STATE, delayed ? UPDATE_DELAY : 0); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 420 | } |
| 421 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 422 | /* Returns a list of all currently attached USB devices (host mdoe) */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 423 | public void getDeviceList(Bundle devices) { |
| 424 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 425 | synchronized (mLock) { |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 426 | for (String name : mDevices.keySet()) { |
| 427 | devices.putParcelable(name, mDevices.get(name)); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 432 | /* Opens the specified USB device (host mode) */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 433 | public ParcelFileDescriptor openDevice(String deviceName) { |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 434 | if (isBlackListed(deviceName)) { |
| 435 | throw new SecurityException("USB device is on a restricted bus"); |
| 436 | } |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 437 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 438 | if (mDevices.get(deviceName) == null) { |
| 439 | // if it is not in mDevices, it either does not exist or is blacklisted |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 440 | throw new IllegalArgumentException( |
| 441 | "device " + deviceName + " does not exist or is restricted"); |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 442 | } |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 443 | return nativeOpenDevice(deviceName); |
| 444 | } |
| 445 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 446 | /* returns the currently attached USB accessory (device mode) */ |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 447 | public UsbAccessory getCurrentAccessory() { |
| 448 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| 449 | return mCurrentAccessory; |
| 450 | } |
| 451 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 452 | /* opens the currently attached USB accessory (device mode) */ |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 453 | public ParcelFileDescriptor openAccessory() { |
| 454 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| 455 | return nativeOpenAccessory(); |
| 456 | } |
| 457 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 458 | /* |
| 459 | * This handler is for deferred handling of events related to device mode and accessories. |
| 460 | */ |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 461 | private final Handler mHandler = new Handler() { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 462 | private void addEnabledFunctionsLocked(Intent intent) { |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 463 | // include state of all USB functions in our extras |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 464 | for (int i = 0; i < mEnabledFunctions.size(); i++) { |
| 465 | intent.putExtra(mEnabledFunctions.get(i), UsbManager.USB_FUNCTION_ENABLED); |
| 466 | } |
| 467 | for (int i = 0; i < mDisabledFunctions.size(); i++) { |
| 468 | intent.putExtra(mDisabledFunctions.get(i), UsbManager.USB_FUNCTION_DISABLED); |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 472 | @Override |
| 473 | public void handleMessage(Message msg) { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 474 | synchronized (mLock) { |
| 475 | switch (msg.what) { |
| 476 | case MSG_UPDATE_STATE: |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 477 | if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) { |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 478 | if (mConnected == 0 && mCurrentAccessory != null) { |
| 479 | // turn off accessory mode when we are disconnected |
| 480 | if (UsbManager.setFunctionEnabled( |
| 481 | UsbManager.USB_FUNCTION_ACCESSORY, false)) { |
| 482 | Log.d(TAG, "exited USB accessory mode"); |
| 483 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 484 | // restore previously enabled functions |
| 485 | for (String function : mAccessoryRestoreFunctions) { |
| 486 | if (UsbManager.setFunctionEnabled(function, true)) { |
| 487 | Log.e(TAG, "could not reenable function " + function); |
| 488 | } |
| 489 | } |
| 490 | mAccessoryRestoreFunctions.clear(); |
| 491 | |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 492 | Intent intent = new Intent( |
| 493 | UsbManager.ACTION_USB_ACCESSORY_DETACHED); |
| 494 | intent.putExtra(UsbManager.EXTRA_ACCESSORY, mCurrentAccessory); |
| 495 | mContext.sendBroadcast(intent); |
| 496 | mCurrentAccessory = null; |
| 497 | |
| 498 | // this will cause an immediate reset of the USB bus, |
| 499 | // so there is no point in sending the |
| 500 | // function disabled broadcast. |
| 501 | return; |
| 502 | } else { |
| 503 | Log.e(TAG, "could not disable USB_FUNCTION_ACCESSORY"); |
| 504 | } |
| 505 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 506 | |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 507 | final ContentResolver cr = mContext.getContentResolver(); |
| 508 | if (Settings.Secure.getInt(cr, |
| 509 | Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { |
| 510 | Slog.i(TAG, "Device not provisioned, skipping USB broadcast"); |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | mLastConnected = mConnected; |
| 515 | mLastConfiguration = mConfiguration; |
| 516 | |
| 517 | // send a sticky broadcast containing current USB state |
| 518 | Intent intent = new Intent(UsbManager.ACTION_USB_STATE); |
| 519 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 520 | intent.putExtra(UsbManager.USB_CONNECTED, mConnected != 0); |
| 521 | intent.putExtra(UsbManager.USB_CONFIGURATION, mConfiguration); |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 522 | addEnabledFunctionsLocked(intent); |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 523 | mContext.sendStickyBroadcast(intent); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 524 | } |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 525 | break; |
| 526 | case MSG_FUNCTION_ENABLED: |
| 527 | case MSG_FUNCTION_DISABLED: |
| 528 | functionEnabledLocked((String)msg.obj, msg.what == MSG_FUNCTION_ENABLED); |
| 529 | break; |
| 530 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | }; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 534 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 535 | // host support |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 536 | private native void monitorUsbHostBus(); |
| 537 | private native ParcelFileDescriptor nativeOpenDevice(String deviceName); |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame^] | 538 | // accessory support |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 539 | private native String[] nativeGetAccessoryStrings(); |
| 540 | private native ParcelFileDescriptor nativeOpenAccessory(); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 541 | } |