The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_USB |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include <winsock2.h> // winsock.h *must* be included before windows.h. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <adb_api.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 23 | #include <errno.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
Christopher Ferris | 054d170 | 2014-11-06 14:34:24 -0800 | [diff] [blame] | 25 | #include <stdlib.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <usb100.h> |
| 27 | #include <windows.h> |
| 28 | #include <winerror.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | #include "adb.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 31 | #include "transport.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | |
| 33 | /** Structure usb_handle describes our connection to the usb device via |
| 34 | AdbWinApi.dll. This structure is returned from usb_open() routine and |
| 35 | is expected in each subsequent call that is accessing the device. |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 36 | |
| 37 | Most members are protected by usb_lock, except for adb_{read,write}_pipe which |
| 38 | rely on AdbWinApi.dll's handle validation and AdbCloseHandle(endpoint)'s |
| 39 | ability to break a thread out of pipe IO. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | */ |
| 41 | struct usb_handle { |
| 42 | /// Previous entry in the list of opened usb handles |
| 43 | usb_handle *prev; |
| 44 | |
| 45 | /// Next entry in the list of opened usb handles |
| 46 | usb_handle *next; |
| 47 | |
| 48 | /// Handle to USB interface |
| 49 | ADBAPIHANDLE adb_interface; |
| 50 | |
| 51 | /// Handle to USB read pipe (endpoint) |
| 52 | ADBAPIHANDLE adb_read_pipe; |
| 53 | |
| 54 | /// Handle to USB write pipe (endpoint) |
| 55 | ADBAPIHANDLE adb_write_pipe; |
| 56 | |
| 57 | /// Interface name |
| 58 | char* interface_name; |
| 59 | |
| 60 | /// Mask for determining when to use zero length packets |
| 61 | unsigned zero_mask; |
| 62 | }; |
| 63 | |
| 64 | /// Class ID assigned to the device by androidusb.sys |
| 65 | static const GUID usb_class_id = ANDROID_USB_CLASS_ID; |
| 66 | |
| 67 | /// List of opened usb handles |
| 68 | static usb_handle handle_list = { |
| 69 | .prev = &handle_list, |
| 70 | .next = &handle_list, |
| 71 | }; |
| 72 | |
| 73 | /// Locker for the list of opened usb handles |
| 74 | ADB_MUTEX_DEFINE( usb_lock ); |
| 75 | |
| 76 | /// Checks if there is opened usb handle in handle_list for this device. |
| 77 | int known_device(const char* dev_name); |
| 78 | |
| 79 | /// Checks if there is opened usb handle in handle_list for this device. |
| 80 | /// usb_lock mutex must be held before calling this routine. |
| 81 | int known_device_locked(const char* dev_name); |
| 82 | |
| 83 | /// Registers opened usb handle (adds it to handle_list). |
| 84 | int register_new_device(usb_handle* handle); |
| 85 | |
| 86 | /// Checks if interface (device) matches certain criteria |
| 87 | int recognized_device(usb_handle* handle); |
| 88 | |
| 89 | /// Enumerates present and available interfaces (devices), opens new ones and |
| 90 | /// registers usb transport for them. |
| 91 | void find_devices(); |
| 92 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 93 | /// Kicks all USB devices |
| 94 | static void kick_devices(); |
| 95 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | /// Entry point for thread that polls (every second) for new usb interfaces. |
| 97 | /// This routine calls find_devices in infinite loop. |
| 98 | void* device_poll_thread(void* unused); |
| 99 | |
| 100 | /// Initializes this module |
| 101 | void usb_init(); |
| 102 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | /// Opens usb interface (device) by interface (device) name. |
| 104 | usb_handle* do_usb_open(const wchar_t* interface_name); |
| 105 | |
| 106 | /// Writes data to the opened usb handle |
| 107 | int usb_write(usb_handle* handle, const void* data, int len); |
| 108 | |
| 109 | /// Reads data using the opened usb handle |
| 110 | int usb_read(usb_handle *handle, void* data, int len); |
| 111 | |
| 112 | /// Cleans up opened usb handle |
| 113 | void usb_cleanup_handle(usb_handle* handle); |
| 114 | |
| 115 | /// Cleans up (but don't close) opened usb handle |
| 116 | void usb_kick(usb_handle* handle); |
| 117 | |
| 118 | /// Closes opened usb handle |
| 119 | int usb_close(usb_handle* handle); |
| 120 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | int known_device_locked(const char* dev_name) { |
| 122 | usb_handle* usb; |
| 123 | |
| 124 | if (NULL != dev_name) { |
| 125 | // Iterate through the list looking for the name match. |
| 126 | for(usb = handle_list.next; usb != &handle_list; usb = usb->next) { |
| 127 | // In Windows names are not case sensetive! |
| 128 | if((NULL != usb->interface_name) && |
| 129 | (0 == stricmp(usb->interface_name, dev_name))) { |
| 130 | return 1; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int known_device(const char* dev_name) { |
| 139 | int ret = 0; |
| 140 | |
| 141 | if (NULL != dev_name) { |
| 142 | adb_mutex_lock(&usb_lock); |
| 143 | ret = known_device_locked(dev_name); |
| 144 | adb_mutex_unlock(&usb_lock); |
| 145 | } |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | int register_new_device(usb_handle* handle) { |
| 151 | if (NULL == handle) |
| 152 | return 0; |
| 153 | |
| 154 | adb_mutex_lock(&usb_lock); |
| 155 | |
| 156 | // Check if device is already in the list |
| 157 | if (known_device_locked(handle->interface_name)) { |
| 158 | adb_mutex_unlock(&usb_lock); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | // Not in the list. Add this handle to the list. |
| 163 | handle->next = &handle_list; |
| 164 | handle->prev = handle_list.prev; |
| 165 | handle->prev->next = handle; |
| 166 | handle->next->prev = handle; |
| 167 | |
| 168 | adb_mutex_unlock(&usb_lock); |
| 169 | |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | void* device_poll_thread(void* unused) { |
| 174 | D("Created device thread\n"); |
| 175 | |
| 176 | while(1) { |
| 177 | find_devices(); |
| 178 | adb_sleep_ms(1000); |
| 179 | } |
| 180 | |
| 181 | return NULL; |
| 182 | } |
| 183 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 184 | static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, |
| 185 | LPARAM lParam) { |
| 186 | switch (uMsg) { |
| 187 | case WM_POWERBROADCAST: |
| 188 | switch (wParam) { |
| 189 | case PBT_APMRESUMEAUTOMATIC: |
| 190 | // Resuming from sleep or hibernation, so kick all existing USB devices |
| 191 | // and then allow the device_poll_thread to redetect USB devices from |
| 192 | // scratch. If we don't do this, existing USB devices will never respond |
| 193 | // to us because they'll be waiting for the connect/auth handshake. |
| 194 | D("Received (WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC) notification, " |
| 195 | "so kicking all USB devices\n"); |
| 196 | kick_devices(); |
| 197 | return TRUE; |
| 198 | } |
| 199 | } |
| 200 | return DefWindowProcW(hwnd, uMsg, wParam, lParam); |
| 201 | } |
| 202 | |
| 203 | static void* _power_notification_thread(void* unused) { |
| 204 | // This uses a thread with its own window message pump to get power |
| 205 | // notifications. If adb runs from a non-interactive service account, this |
| 206 | // might not work (not sure). If that happens to not work, we could use |
| 207 | // heavyweight WMI APIs to get power notifications. But for the common case |
| 208 | // of a developer's interactive session, a window message pump is more |
| 209 | // appropriate. |
| 210 | D("Created power notification thread\n"); |
| 211 | |
| 212 | // Window class names are process specific. |
| 213 | static const WCHAR kPowerNotificationWindowClassName[] = |
| 214 | L"PowerNotificationWindow"; |
| 215 | |
| 216 | // Get the HINSTANCE corresponding to the module that _power_window_proc |
| 217 | // is in (the main module). |
| 218 | const HINSTANCE instance = GetModuleHandleW(NULL); |
| 219 | if (!instance) { |
| 220 | // This is such a common API call that this should never fail. |
| 221 | fatal("GetModuleHandleW failed: %s", |
| 222 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 223 | } |
| 224 | |
| 225 | WNDCLASSEXW wndclass; |
| 226 | memset(&wndclass, 0, sizeof(wndclass)); |
| 227 | wndclass.cbSize = sizeof(wndclass); |
| 228 | wndclass.lpfnWndProc = _power_window_proc; |
| 229 | wndclass.hInstance = instance; |
| 230 | wndclass.lpszClassName = kPowerNotificationWindowClassName; |
| 231 | if (!RegisterClassExW(&wndclass)) { |
| 232 | fatal("RegisterClassExW failed: %s", |
| 233 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 234 | } |
| 235 | |
| 236 | if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName, |
| 237 | L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0, |
| 238 | NULL, NULL, instance, NULL)) { |
| 239 | fatal("CreateWindowExW failed: %s", |
| 240 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 241 | } |
| 242 | |
| 243 | MSG msg; |
| 244 | while (GetMessageW(&msg, NULL, 0, 0)) { |
| 245 | TranslateMessage(&msg); |
| 246 | DispatchMessageW(&msg); |
| 247 | } |
| 248 | |
| 249 | // GetMessageW() will return false if a quit message is posted. We don't |
| 250 | // do that, but it might be possible for that to occur when logging off or |
| 251 | // shutting down. Not a big deal since the whole process will be going away |
| 252 | // soon anyway. |
| 253 | D("Power notification thread exiting\n"); |
| 254 | |
| 255 | return NULL; |
| 256 | } |
| 257 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | void usb_init() { |
Elliott Hughes | f251714 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 259 | if (!adb_thread_create(device_poll_thread, nullptr)) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 260 | fatal_errno("cannot create device poll thread"); |
| 261 | } |
| 262 | if (!adb_thread_create(_power_notification_thread, nullptr)) { |
| 263 | fatal_errno("cannot create power notification thread"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | usb_handle* do_usb_open(const wchar_t* interface_name) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 268 | unsigned long name_len = 0; |
| 269 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | // Allocate our handle |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 271 | usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle)); |
| 272 | if (NULL == ret) { |
| 273 | D("Could not allocate %u bytes for usb_handle: %s\n", sizeof(usb_handle), |
| 274 | strerror(errno)); |
| 275 | goto fail; |
| 276 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 | |
| 278 | // Set linkers back to the handle |
| 279 | ret->next = ret; |
| 280 | ret->prev = ret; |
| 281 | |
| 282 | // Create interface. |
| 283 | ret->adb_interface = AdbCreateInterfaceByName(interface_name); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 284 | if (NULL == ret->adb_interface) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 285 | D("AdbCreateInterfaceByName failed: %s\n", |
| 286 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 287 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Open read pipe (endpoint) |
| 291 | ret->adb_read_pipe = |
| 292 | AdbOpenDefaultBulkReadEndpoint(ret->adb_interface, |
| 293 | AdbOpenAccessTypeReadWrite, |
| 294 | AdbOpenSharingModeReadWrite); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 295 | if (NULL == ret->adb_read_pipe) { |
| 296 | D("AdbOpenDefaultBulkReadEndpoint failed: %s\n", |
| 297 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 298 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 301 | // Open write pipe (endpoint) |
| 302 | ret->adb_write_pipe = |
| 303 | AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface, |
| 304 | AdbOpenAccessTypeReadWrite, |
| 305 | AdbOpenSharingModeReadWrite); |
| 306 | if (NULL == ret->adb_write_pipe) { |
| 307 | D("AdbOpenDefaultBulkWriteEndpoint failed: %s\n", |
| 308 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 309 | goto fail; |
| 310 | } |
| 311 | |
| 312 | // Save interface name |
| 313 | // First get expected name length |
| 314 | AdbGetInterfaceName(ret->adb_interface, |
| 315 | NULL, |
| 316 | &name_len, |
| 317 | true); |
| 318 | if (0 == name_len) { |
| 319 | D("AdbGetInterfaceName returned name length of zero: %s\n", |
| 320 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 321 | goto fail; |
| 322 | } |
| 323 | |
| 324 | ret->interface_name = (char*)malloc(name_len); |
| 325 | if (NULL == ret->interface_name) { |
| 326 | D("Could not allocate %lu bytes for interface_name: %s\n", name_len, |
| 327 | strerror(errno)); |
| 328 | goto fail; |
| 329 | } |
| 330 | |
| 331 | // Now save the name |
| 332 | if (!AdbGetInterfaceName(ret->adb_interface, |
| 333 | ret->interface_name, |
| 334 | &name_len, |
| 335 | true)) { |
| 336 | D("AdbGetInterfaceName failed: %s\n", |
| 337 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 338 | goto fail; |
| 339 | } |
| 340 | |
| 341 | // We're done at this point |
| 342 | return ret; |
| 343 | |
| 344 | fail: |
| 345 | if (NULL != ret) { |
| 346 | usb_cleanup_handle(ret); |
| 347 | free(ret); |
| 348 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 349 | |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | int usb_write(usb_handle* handle, const void* data, int len) { |
Jack Ren | 80f2645 | 2011-08-12 18:39:04 +0800 | [diff] [blame] | 354 | unsigned long time_out = 5000; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 | unsigned long written = 0; |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 356 | int err = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | |
| 358 | D("usb_write %d\n", len); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 359 | if (NULL == handle) { |
| 360 | D("usb_write was passed NULL handle\n"); |
| 361 | err = EINVAL; |
| 362 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 365 | // Perform write |
| 366 | if (!AdbWriteEndpointSync(handle->adb_write_pipe, |
| 367 | (void*)data, |
| 368 | (unsigned long)len, |
| 369 | &written, |
| 370 | time_out)) { |
| 371 | D("AdbWriteEndpointSync failed: %s\n", |
| 372 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 373 | err = EIO; |
| 374 | goto fail; |
| 375 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 376 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 377 | // Make sure that we've written what we were asked to write |
| 378 | D("usb_write got: %ld, expected: %d\n", written, len); |
| 379 | if (written != (unsigned long)len) { |
| 380 | // If this occurs, this code should be changed to repeatedly call |
| 381 | // AdbWriteEndpointSync() until all bytes are written. |
| 382 | D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld\n", |
| 383 | len, written); |
| 384 | err = EIO; |
| 385 | goto fail; |
| 386 | } |
| 387 | |
| 388 | if (handle->zero_mask && (len & handle->zero_mask) == 0) { |
| 389 | // Send a zero length packet |
| 390 | if (!AdbWriteEndpointSync(handle->adb_write_pipe, |
| 391 | (void*)data, |
| 392 | 0, |
| 393 | &written, |
| 394 | time_out)) { |
| 395 | D("AdbWriteEndpointSync of zero length packet failed: %s\n", |
| 396 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 397 | err = EIO; |
| 398 | goto fail; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return 0; |
| 403 | |
| 404 | fail: |
| 405 | // Any failure should cause us to kick the device instead of leaving it a |
| 406 | // zombie state with potential to hang. |
| 407 | if (NULL != handle) { |
| 408 | D("Kicking device due to error in usb_write\n"); |
| 409 | usb_kick(handle); |
| 410 | } |
| 411 | |
| 412 | D("usb_write failed\n"); |
| 413 | errno = err; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | int usb_read(usb_handle *handle, void* data, int len) { |
Jack Ren | 80f2645 | 2011-08-12 18:39:04 +0800 | [diff] [blame] | 418 | unsigned long time_out = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | unsigned long read = 0; |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 420 | int err = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | |
| 422 | D("usb_read %d\n", len); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 423 | if (NULL == handle) { |
| 424 | D("usb_read was passed NULL handle\n"); |
| 425 | err = EINVAL; |
| 426 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 429 | while (len > 0) { |
| 430 | if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read, |
| 431 | time_out)) { |
| 432 | D("AdbReadEndpointSync failed: %s\n", |
| 433 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 434 | err = EIO; |
| 435 | goto fail; |
| 436 | } |
| 437 | D("usb_read got: %ld, expected: %d\n", read, len); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 438 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 439 | data = (char *)data + read; |
| 440 | len -= read; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | |
| 445 | fail: |
| 446 | // Any failure should cause us to kick the device instead of leaving it a |
| 447 | // zombie state with potential to hang. |
| 448 | if (NULL != handle) { |
| 449 | D("Kicking device due to error in usb_read\n"); |
| 450 | usb_kick(handle); |
| 451 | } |
| 452 | |
| 453 | D("usb_read failed\n"); |
| 454 | errno = err; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | return -1; |
| 456 | } |
| 457 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 458 | // Wrapper around AdbCloseHandle() that logs diagnostics. |
| 459 | static void _adb_close_handle(ADBAPIHANDLE adb_handle) { |
| 460 | if (!AdbCloseHandle(adb_handle)) { |
| 461 | D("AdbCloseHandle(%p) failed: %s\n", adb_handle, |
| 462 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 463 | } |
| 464 | } |
| 465 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 466 | void usb_cleanup_handle(usb_handle* handle) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 467 | D("usb_cleanup_handle\n"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 468 | if (NULL != handle) { |
| 469 | if (NULL != handle->interface_name) |
| 470 | free(handle->interface_name); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 471 | // AdbCloseHandle(pipe) will break any threads out of pending IO calls and |
| 472 | // wait until the pipe no longer uses the interface. Then we can |
| 473 | // AdbCloseHandle() the interface. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 474 | if (NULL != handle->adb_write_pipe) |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 475 | _adb_close_handle(handle->adb_write_pipe); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 476 | if (NULL != handle->adb_read_pipe) |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 477 | _adb_close_handle(handle->adb_read_pipe); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 478 | if (NULL != handle->adb_interface) |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 479 | _adb_close_handle(handle->adb_interface); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 480 | |
| 481 | handle->interface_name = NULL; |
| 482 | handle->adb_write_pipe = NULL; |
| 483 | handle->adb_read_pipe = NULL; |
| 484 | handle->adb_interface = NULL; |
| 485 | } |
| 486 | } |
| 487 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 488 | static void usb_kick_locked(usb_handle* handle) { |
| 489 | // The reason the lock must be acquired before calling this function is in |
| 490 | // case multiple threads are trying to kick the same device at the same time. |
| 491 | usb_cleanup_handle(handle); |
| 492 | } |
| 493 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 494 | void usb_kick(usb_handle* handle) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 495 | D("usb_kick\n"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 496 | if (NULL != handle) { |
| 497 | adb_mutex_lock(&usb_lock); |
| 498 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 499 | usb_kick_locked(handle); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | |
| 501 | adb_mutex_unlock(&usb_lock); |
| 502 | } else { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 503 | errno = EINVAL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | int usb_close(usb_handle* handle) { |
| 508 | D("usb_close\n"); |
| 509 | |
| 510 | if (NULL != handle) { |
| 511 | // Remove handle from the list |
| 512 | adb_mutex_lock(&usb_lock); |
| 513 | |
| 514 | if ((handle->next != handle) && (handle->prev != handle)) { |
| 515 | handle->next->prev = handle->prev; |
| 516 | handle->prev->next = handle->next; |
| 517 | handle->prev = handle; |
| 518 | handle->next = handle; |
| 519 | } |
| 520 | |
| 521 | adb_mutex_unlock(&usb_lock); |
| 522 | |
| 523 | // Cleanup handle |
| 524 | usb_cleanup_handle(handle); |
| 525 | free(handle); |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 531 | int recognized_device(usb_handle* handle) { |
| 532 | if (NULL == handle) |
| 533 | return 0; |
| 534 | |
| 535 | // Check vendor and product id first |
| 536 | USB_DEVICE_DESCRIPTOR device_desc; |
| 537 | |
| 538 | if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, |
| 539 | &device_desc)) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 540 | D("AdbGetUsbDeviceDescriptor failed: %s\n", |
| 541 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | // Then check interface properties |
| 546 | USB_INTERFACE_DESCRIPTOR interf_desc; |
| 547 | |
| 548 | if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, |
| 549 | &interf_desc)) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 550 | D("AdbGetUsbInterfaceDescriptor failed: %s\n", |
| 551 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | // Must have two endpoints |
| 556 | if (2 != interf_desc.bNumEndpoints) { |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | if (is_adb_interface(device_desc.idVendor, device_desc.idProduct, |
| 561 | interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) { |
| 562 | |
| 563 | if(interf_desc.bInterfaceProtocol == 0x01) { |
| 564 | AdbEndpointInformation endpoint_info; |
| 565 | // assuming zero is a valid bulk endpoint ID |
| 566 | if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) { |
| 567 | handle->zero_mask = endpoint_info.max_packet_size - 1; |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 568 | D("device zero_mask: 0x%x\n", handle->zero_mask); |
| 569 | } else { |
| 570 | D("AdbGetEndpointInformation failed: %s\n", |
| 571 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | void find_devices() { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 582 | usb_handle* handle = NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 583 | char entry_buffer[2048]; |
| 584 | char interf_name[2048]; |
| 585 | AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); |
| 586 | unsigned long entry_buffer_size = sizeof(entry_buffer); |
| 587 | char* copy_name; |
| 588 | |
| 589 | // Enumerate all present and active interfaces. |
| 590 | ADBAPIHANDLE enum_handle = |
| 591 | AdbEnumInterfaces(usb_class_id, true, true, true); |
| 592 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 593 | if (NULL == enum_handle) { |
| 594 | D("AdbEnumInterfaces failed: %s\n", |
| 595 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 596 | return; |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 597 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | |
| 599 | while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { |
| 600 | // TODO: FIXME - temp hack converting wchar_t into char. |
| 601 | // It would be better to change AdbNextInterface so it will return |
| 602 | // interface name as single char string. |
| 603 | const wchar_t* wchar_name = next_interface->device_name; |
| 604 | for(copy_name = interf_name; |
| 605 | L'\0' != *wchar_name; |
| 606 | wchar_name++, copy_name++) { |
| 607 | *copy_name = (char)(*wchar_name); |
| 608 | } |
| 609 | *copy_name = '\0'; |
| 610 | |
| 611 | // Lets see if we already have this device in the list |
| 612 | if (!known_device(interf_name)) { |
| 613 | // This seems to be a new device. Open it! |
| 614 | handle = do_usb_open(next_interface->device_name); |
| 615 | if (NULL != handle) { |
| 616 | // Lets see if this interface (device) belongs to us |
| 617 | if (recognized_device(handle)) { |
| 618 | D("adding a new device %s\n", interf_name); |
| 619 | char serial_number[512]; |
| 620 | unsigned long serial_number_len = sizeof(serial_number); |
| 621 | if (AdbGetSerialNumber(handle->adb_interface, |
| 622 | serial_number, |
| 623 | &serial_number_len, |
| 624 | true)) { |
| 625 | // Lets make sure that we don't duplicate this device |
| 626 | if (register_new_device(handle)) { |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 627 | register_usb_transport(handle, serial_number, NULL, 1); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 628 | } else { |
| 629 | D("register_new_device failed for %s\n", interf_name); |
| 630 | usb_cleanup_handle(handle); |
| 631 | free(handle); |
| 632 | } |
| 633 | } else { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 634 | D("cannot get serial number: %s\n", |
| 635 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 636 | usb_cleanup_handle(handle); |
| 637 | free(handle); |
| 638 | } |
| 639 | } else { |
| 640 | usb_cleanup_handle(handle); |
| 641 | free(handle); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | entry_buffer_size = sizeof(entry_buffer); |
| 647 | } |
| 648 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 649 | if (GetLastError() != ERROR_NO_MORE_ITEMS) { |
| 650 | // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration. |
| 651 | D("AdbNextInterface failed: %s\n", |
| 652 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 653 | } |
| 654 | |
| 655 | _adb_close_handle(enum_handle); |
| 656 | } |
| 657 | |
| 658 | static void kick_devices() { |
| 659 | // Need to acquire lock to safely walk the list which might be modified |
| 660 | // by another thread. |
| 661 | adb_mutex_lock(&usb_lock); |
| 662 | for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) { |
| 663 | usb_kick_locked(usb); |
| 664 | } |
| 665 | adb_mutex_unlock(&usb_lock); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 666 | } |