| 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); |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 146 | mContext.sendBroadcast(intent); |
| 147 | } else { |
| 148 | Log.e(TAG, "nativeGetAccessoryStrings failed"); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 153 | /* |
| 154 | * Listens for uevent messages from the kernel to monitor the USB state (device mode) |
| 155 | */ |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 156 | private final UEventObserver mUEventObserver = new UEventObserver() { |
| 157 | @Override |
| 158 | public void onUEvent(UEventObserver.UEvent event) { |
| 159 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 160 | Slog.v(TAG, "USB UEVENT: " + event.toString()); |
| 161 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 162 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 163 | synchronized (mLock) { |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 164 | String name = event.get("SWITCH_NAME"); |
| 165 | String state = event.get("SWITCH_STATE"); |
| 166 | if (name != null && state != null) { |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 167 | try { |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 168 | int intState = Integer.parseInt(state); |
| 169 | if ("usb_connected".equals(name)) { |
| 170 | mConnected = intState; |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 171 | // trigger an Intent broadcast |
| 172 | if (mSystemReady) { |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 173 | // debounce disconnects |
| 174 | update(mConnected == 0); |
| 175 | } |
| 176 | } else if ("usb_configuration".equals(name)) { |
| 177 | mConfiguration = intState; |
| 178 | // trigger an Intent broadcast |
| 179 | if (mSystemReady) { |
| 180 | update(mConnected == 0); |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 181 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 182 | } |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 183 | } catch (NumberFormatException e) { |
| 184 | Slog.e(TAG, "Could not parse switch state from event " + event); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 185 | } |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 186 | } else { |
| 187 | String function = event.get("FUNCTION"); |
| 188 | String enabledStr = event.get("ENABLED"); |
| 189 | if (function != null && enabledStr != null) { |
| 190 | // Note: we do not broadcast a change when a function is enabled or disabled. |
| 191 | // We just record the state change for the next broadcast. |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 192 | int what = ("1".equals(enabledStr) ? |
| 193 | MSG_FUNCTION_ENABLED : MSG_FUNCTION_DISABLED); |
| 194 | Message msg = Message.obtain(mHandler, what); |
| 195 | msg.obj = function; |
| 196 | mHandler.sendMessage(msg); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | public UsbService(Context context) { |
| 204 | mContext = context; |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 205 | mHostBlacklist = context.getResources().getStringArray( |
| 206 | com.android.internal.R.array.config_usbHostBlacklist); |
| 207 | |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 208 | init(); // set initial status |
| 209 | |
| David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 210 | if (mConfiguration >= 0) { |
| 211 | mUEventObserver.startObserving(USB_CONNECTED_MATCH); |
| 212 | mUEventObserver.startObserving(USB_CONFIGURATION_MATCH); |
| 213 | mUEventObserver.startObserving(USB_FUNCTIONS_MATCH); |
| 214 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 215 | } |
| Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 216 | |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 217 | private final void init() { |
| 218 | char[] buffer = new char[1024]; |
| 219 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 220 | // Read initial USB state (device mode) |
| David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 221 | mConfiguration = -1; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 222 | try { |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 223 | FileReader file = new FileReader(USB_CONNECTED_PATH); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 224 | int len = file.read(buffer, 0, 1024); |
| Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 225 | file.close(); |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 226 | mConnected = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 227 | |
| 228 | file = new FileReader(USB_CONFIGURATION_PATH); |
| 229 | len = file.read(buffer, 0, 1024); |
| 230 | file.close(); |
| 231 | mConfiguration = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 232 | |
| 233 | } catch (FileNotFoundException e) { |
| David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 234 | Slog.i(TAG, "This kernel does not have USB configuration switch support"); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 235 | } catch (Exception e) { |
| 236 | Slog.e(TAG, "" , e); |
| 237 | } |
| David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 238 | if (mConfiguration < 0) |
| 239 | return; |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 240 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 241 | // Read initial list of enabled and disabled functions (device mode) |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 242 | try { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 243 | File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles(); |
| 244 | for (int i = 0; i < files.length; i++) { |
| 245 | File file = new File(files[i], "enable"); |
| 246 | FileReader reader = new FileReader(file); |
| 247 | int len = reader.read(buffer, 0, 1024); |
| 248 | reader.close(); |
| 249 | int value = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 250 | String functionName = files[i].getName(); |
| 251 | if (value == 1) { |
| 252 | mEnabledFunctions.add(functionName); |
| 253 | } else { |
| 254 | mDisabledFunctions.add(functionName); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } catch (FileNotFoundException e) { |
| 258 | Slog.w(TAG, "This kernel does not have USB composite class support"); |
| 259 | } catch (Exception e) { |
| 260 | Slog.e(TAG, "" , e); |
| 261 | } |
| 262 | } |
| 263 | |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 264 | private boolean isBlackListed(String deviceName) { |
| 265 | int count = mHostBlacklist.length; |
| 266 | for (int i = 0; i < count; i++) { |
| 267 | if (deviceName.startsWith(mHostBlacklist[i])) { |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 274 | /* 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] | 275 | private boolean isBlackListed(int clazz, int subClass, int protocol) { |
| 276 | // blacklist hubs |
| 277 | if (clazz == UsbConstants.USB_CLASS_HUB) return true; |
| 278 | |
| 279 | // blacklist HID boot devices (mouse and keyboard) |
| 280 | if (clazz == UsbConstants.USB_CLASS_HID && |
| 281 | subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) { |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | return false; |
| 286 | } |
| 287 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 288 | /* Called from JNI in monitorUsbHostBus() to report new USB devices (host mode) */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 289 | private void usbDeviceAdded(String deviceName, int vendorID, int productID, |
| 290 | int deviceClass, int deviceSubclass, int deviceProtocol, |
| 291 | /* array of quintuples containing id, class, subclass, protocol |
| 292 | and number of endpoints for each interface */ |
| 293 | int[] interfaceValues, |
| 294 | /* array of quadruples containing address, attributes, max packet size |
| 295 | and interval for each endpoint */ |
| 296 | int[] endpointValues) { |
| 297 | |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 298 | if (isBlackListed(deviceName) || |
| 299 | isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) { |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 303 | synchronized (mLock) { |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 304 | if (mDevices.get(deviceName) != null) { |
| 305 | Log.w(TAG, "device already on mDevices list: " + deviceName); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | int numInterfaces = interfaceValues.length / 5; |
| 310 | Parcelable[] interfaces = new UsbInterface[numInterfaces]; |
| 311 | try { |
| 312 | // repackage interfaceValues as an array of UsbInterface |
| 313 | int intf, endp, ival = 0, eval = 0; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 314 | for (intf = 0; intf < numInterfaces; intf++) { |
| 315 | int interfaceId = interfaceValues[ival++]; |
| 316 | int interfaceClass = interfaceValues[ival++]; |
| 317 | int interfaceSubclass = interfaceValues[ival++]; |
| 318 | int interfaceProtocol = interfaceValues[ival++]; |
| 319 | int numEndpoints = interfaceValues[ival++]; |
| 320 | |
| 321 | Parcelable[] endpoints = new UsbEndpoint[numEndpoints]; |
| 322 | for (endp = 0; endp < numEndpoints; endp++) { |
| 323 | int address = endpointValues[eval++]; |
| 324 | int attributes = endpointValues[eval++]; |
| 325 | int maxPacketSize = endpointValues[eval++]; |
| 326 | int interval = endpointValues[eval++]; |
| 327 | endpoints[endp] = new UsbEndpoint(address, attributes, |
| 328 | maxPacketSize, interval); |
| 329 | } |
| 330 | |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 331 | // don't allow if any interfaces are blacklisted |
| 332 | if (isBlackListed(interfaceClass, interfaceSubclass, interfaceProtocol)) { |
| 333 | return; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 334 | } |
| 335 | interfaces[intf] = new UsbInterface(interfaceId, interfaceClass, |
| 336 | interfaceSubclass, interfaceProtocol, endpoints); |
| 337 | } |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 338 | } catch (Exception e) { |
| 339 | // beware of index out of bound exceptions, which might happen if |
| 340 | // a device does not set bNumEndpoints correctly |
| 341 | Log.e(TAG, "error parsing USB descriptors", e); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | UsbDevice device = new UsbDevice(deviceName, vendorID, productID, |
| 346 | deviceClass, deviceSubclass, deviceProtocol, interfaces); |
| 347 | mDevices.put(deviceName, device); |
| 348 | |
| 349 | Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 350 | intent.putExtra(UsbManager.EXTRA_DEVICE, device); |
| 351 | Log.d(TAG, "usbDeviceAdded, sending " + intent); |
| 352 | mContext.sendBroadcast(intent); |
| 353 | } |
| 354 | } |
| 355 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 356 | /* Called from JNI in monitorUsbHostBus to report USB device removal (host mode) */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 357 | private void usbDeviceRemoved(String deviceName) { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 358 | synchronized (mLock) { |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 359 | UsbDevice device = mDevices.remove(deviceName); |
| 360 | if (device != null) { |
| 361 | Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_DETACHED); |
| Mike Lockwood | 188d00b | 2011-02-23 13:14:33 -0800 | [diff] [blame] | 362 | intent.putExtra(UsbManager.EXTRA_DEVICE, device); |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 363 | Log.d(TAG, "usbDeviceRemoved, sending " + intent); |
| 364 | mContext.sendBroadcast(intent); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 369 | private void initHostSupport() { |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 370 | // Create a thread to call into native code to wait for USB host events. |
| 371 | // This thread will call us back on usbDeviceAdded and usbDeviceRemoved. |
| 372 | Runnable runnable = new Runnable() { |
| 373 | public void run() { |
| 374 | monitorUsbHostBus(); |
| 375 | } |
| 376 | }; |
| 377 | new Thread(null, runnable, "UsbService host thread").start(); |
| Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 380 | void systemReady() { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 381 | synchronized (mLock) { |
| Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 382 | if (mContext.getResources().getBoolean( |
| 383 | com.android.internal.R.bool.config_hasUsbHostSupport)) { |
| 384 | // start monitoring for connected USB devices |
| 385 | initHostSupport(); |
| 386 | } |
| 387 | |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 388 | update(false); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 389 | mSystemReady = true; |
| 390 | } |
| 391 | } |
| 392 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 393 | /* |
| 394 | * Sends a message to update the USB connected and configured state (device mode). |
| 395 | * If delayed is true, then we add a small delay in sending the message to debounce |
| 396 | * the USB connection when enabling USB tethering. |
| 397 | */ |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 398 | private final void update(boolean delayed) { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 399 | mHandler.removeMessages(MSG_UPDATE_STATE); |
| 400 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE_STATE, delayed ? UPDATE_DELAY : 0); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 401 | } |
| 402 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 403 | /* Returns a list of all currently attached USB devices (host mdoe) */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 404 | public void getDeviceList(Bundle devices) { |
| 405 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 406 | synchronized (mLock) { |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 407 | for (String name : mDevices.keySet()) { |
| 408 | devices.putParcelable(name, mDevices.get(name)); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 413 | /* Opens the specified USB device (host mode) */ |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 414 | public ParcelFileDescriptor openDevice(String deviceName) { |
| Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 415 | if (isBlackListed(deviceName)) { |
| 416 | throw new SecurityException("USB device is on a restricted bus"); |
| 417 | } |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 418 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 419 | if (mDevices.get(deviceName) == null) { |
| 420 | // 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] | 421 | throw new IllegalArgumentException( |
| 422 | "device " + deviceName + " does not exist or is restricted"); |
| Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 423 | } |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 424 | return nativeOpenDevice(deviceName); |
| 425 | } |
| 426 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 427 | /* returns the currently attached USB accessory (device mode) */ |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 428 | public UsbAccessory getCurrentAccessory() { |
| 429 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| 430 | return mCurrentAccessory; |
| 431 | } |
| 432 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 433 | /* opens the currently attached USB accessory (device mode) */ |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 434 | public ParcelFileDescriptor openAccessory() { |
| 435 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| 436 | return nativeOpenAccessory(); |
| 437 | } |
| 438 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 439 | /* |
| 440 | * This handler is for deferred handling of events related to device mode and accessories. |
| 441 | */ |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 442 | private final Handler mHandler = new Handler() { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 443 | private void addEnabledFunctionsLocked(Intent intent) { |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 444 | // include state of all USB functions in our extras |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 445 | for (int i = 0; i < mEnabledFunctions.size(); i++) { |
| 446 | intent.putExtra(mEnabledFunctions.get(i), UsbManager.USB_FUNCTION_ENABLED); |
| 447 | } |
| 448 | for (int i = 0; i < mDisabledFunctions.size(); i++) { |
| 449 | intent.putExtra(mDisabledFunctions.get(i), UsbManager.USB_FUNCTION_DISABLED); |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 453 | @Override |
| 454 | public void handleMessage(Message msg) { |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 455 | synchronized (mLock) { |
| 456 | switch (msg.what) { |
| 457 | case MSG_UPDATE_STATE: |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 458 | if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) { |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 459 | if (mConnected == 0 && mCurrentAccessory != null) { |
| 460 | // turn off accessory mode when we are disconnected |
| 461 | if (UsbManager.setFunctionEnabled( |
| 462 | UsbManager.USB_FUNCTION_ACCESSORY, false)) { |
| 463 | Log.d(TAG, "exited USB accessory mode"); |
| 464 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 465 | // restore previously enabled functions |
| 466 | for (String function : mAccessoryRestoreFunctions) { |
| 467 | if (UsbManager.setFunctionEnabled(function, true)) { |
| 468 | Log.e(TAG, "could not reenable function " + function); |
| 469 | } |
| 470 | } |
| 471 | mAccessoryRestoreFunctions.clear(); |
| 472 | |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 473 | Intent intent = new Intent( |
| 474 | UsbManager.ACTION_USB_ACCESSORY_DETACHED); |
| 475 | intent.putExtra(UsbManager.EXTRA_ACCESSORY, mCurrentAccessory); |
| 476 | mContext.sendBroadcast(intent); |
| 477 | mCurrentAccessory = null; |
| 478 | |
| 479 | // this will cause an immediate reset of the USB bus, |
| 480 | // so there is no point in sending the |
| 481 | // function disabled broadcast. |
| 482 | return; |
| 483 | } else { |
| 484 | Log.e(TAG, "could not disable USB_FUNCTION_ACCESSORY"); |
| 485 | } |
| 486 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 487 | |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 488 | final ContentResolver cr = mContext.getContentResolver(); |
| 489 | if (Settings.Secure.getInt(cr, |
| 490 | Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { |
| 491 | Slog.i(TAG, "Device not provisioned, skipping USB broadcast"); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | mLastConnected = mConnected; |
| 496 | mLastConfiguration = mConfiguration; |
| 497 | |
| 498 | // send a sticky broadcast containing current USB state |
| 499 | Intent intent = new Intent(UsbManager.ACTION_USB_STATE); |
| 500 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 501 | intent.putExtra(UsbManager.USB_CONNECTED, mConnected != 0); |
| 502 | intent.putExtra(UsbManager.USB_CONFIGURATION, mConfiguration); |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 503 | addEnabledFunctionsLocked(intent); |
| Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 504 | mContext.sendStickyBroadcast(intent); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 505 | } |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 506 | break; |
| 507 | case MSG_FUNCTION_ENABLED: |
| 508 | case MSG_FUNCTION_DISABLED: |
| 509 | functionEnabledLocked((String)msg.obj, msg.what == MSG_FUNCTION_ENABLED); |
| 510 | break; |
| 511 | } |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | }; |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 515 | |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 516 | // host support |
| Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 517 | private native void monitorUsbHostBus(); |
| 518 | private native ParcelFileDescriptor nativeOpenDevice(String deviceName); |
| Mike Lockwood | ddc6cad | 2011-02-17 09:21:25 -0500 | [diff] [blame] | 519 | // accessory support |
| Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 520 | private native String[] nativeGetAccessoryStrings(); |
| 521 | private native ParcelFileDescriptor nativeOpenAccessory(); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 522 | } |