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) { |
Siva Velusamy | 2669cf9 | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 174 | adb_thread_setname("Device Poll"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | D("Created device thread\n"); |
| 176 | |
| 177 | while(1) { |
| 178 | find_devices(); |
| 179 | adb_sleep_ms(1000); |
| 180 | } |
| 181 | |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 185 | static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, |
| 186 | LPARAM lParam) { |
| 187 | switch (uMsg) { |
| 188 | case WM_POWERBROADCAST: |
| 189 | switch (wParam) { |
| 190 | case PBT_APMRESUMEAUTOMATIC: |
| 191 | // Resuming from sleep or hibernation, so kick all existing USB devices |
| 192 | // and then allow the device_poll_thread to redetect USB devices from |
| 193 | // scratch. If we don't do this, existing USB devices will never respond |
| 194 | // to us because they'll be waiting for the connect/auth handshake. |
| 195 | D("Received (WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC) notification, " |
| 196 | "so kicking all USB devices\n"); |
| 197 | kick_devices(); |
| 198 | return TRUE; |
| 199 | } |
| 200 | } |
| 201 | return DefWindowProcW(hwnd, uMsg, wParam, lParam); |
| 202 | } |
| 203 | |
| 204 | static void* _power_notification_thread(void* unused) { |
| 205 | // This uses a thread with its own window message pump to get power |
| 206 | // notifications. If adb runs from a non-interactive service account, this |
| 207 | // might not work (not sure). If that happens to not work, we could use |
| 208 | // heavyweight WMI APIs to get power notifications. But for the common case |
| 209 | // of a developer's interactive session, a window message pump is more |
| 210 | // appropriate. |
| 211 | D("Created power notification thread\n"); |
Siva Velusamy | 2669cf9 | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 212 | adb_thread_setname("Power Notifier"); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 213 | |
| 214 | // Window class names are process specific. |
| 215 | static const WCHAR kPowerNotificationWindowClassName[] = |
| 216 | L"PowerNotificationWindow"; |
| 217 | |
| 218 | // Get the HINSTANCE corresponding to the module that _power_window_proc |
| 219 | // is in (the main module). |
| 220 | const HINSTANCE instance = GetModuleHandleW(NULL); |
| 221 | if (!instance) { |
| 222 | // This is such a common API call that this should never fail. |
| 223 | fatal("GetModuleHandleW failed: %s", |
| 224 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 225 | } |
| 226 | |
| 227 | WNDCLASSEXW wndclass; |
| 228 | memset(&wndclass, 0, sizeof(wndclass)); |
| 229 | wndclass.cbSize = sizeof(wndclass); |
| 230 | wndclass.lpfnWndProc = _power_window_proc; |
| 231 | wndclass.hInstance = instance; |
| 232 | wndclass.lpszClassName = kPowerNotificationWindowClassName; |
| 233 | if (!RegisterClassExW(&wndclass)) { |
| 234 | fatal("RegisterClassExW failed: %s", |
| 235 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 236 | } |
| 237 | |
| 238 | if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName, |
| 239 | L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0, |
| 240 | NULL, NULL, instance, NULL)) { |
| 241 | fatal("CreateWindowExW failed: %s", |
| 242 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 243 | } |
| 244 | |
| 245 | MSG msg; |
| 246 | while (GetMessageW(&msg, NULL, 0, 0)) { |
| 247 | TranslateMessage(&msg); |
| 248 | DispatchMessageW(&msg); |
| 249 | } |
| 250 | |
| 251 | // GetMessageW() will return false if a quit message is posted. We don't |
| 252 | // do that, but it might be possible for that to occur when logging off or |
| 253 | // shutting down. Not a big deal since the whole process will be going away |
| 254 | // soon anyway. |
| 255 | D("Power notification thread exiting\n"); |
| 256 | |
| 257 | return NULL; |
| 258 | } |
| 259 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 260 | void usb_init() { |
Elliott Hughes | f251714 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 261 | if (!adb_thread_create(device_poll_thread, nullptr)) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 262 | fatal_errno("cannot create device poll thread"); |
| 263 | } |
| 264 | if (!adb_thread_create(_power_notification_thread, nullptr)) { |
| 265 | fatal_errno("cannot create power notification thread"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 269 | usb_handle* do_usb_open(const wchar_t* interface_name) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 270 | unsigned long name_len = 0; |
| 271 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 272 | // Allocate our handle |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 273 | usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle)); |
| 274 | if (NULL == ret) { |
| 275 | D("Could not allocate %u bytes for usb_handle: %s\n", sizeof(usb_handle), |
| 276 | strerror(errno)); |
| 277 | goto fail; |
| 278 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 279 | |
| 280 | // Set linkers back to the handle |
| 281 | ret->next = ret; |
| 282 | ret->prev = ret; |
| 283 | |
| 284 | // Create interface. |
| 285 | ret->adb_interface = AdbCreateInterfaceByName(interface_name); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 286 | if (NULL == ret->adb_interface) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 287 | D("AdbCreateInterfaceByName failed: %s\n", |
| 288 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 289 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // Open read pipe (endpoint) |
| 293 | ret->adb_read_pipe = |
| 294 | AdbOpenDefaultBulkReadEndpoint(ret->adb_interface, |
| 295 | AdbOpenAccessTypeReadWrite, |
| 296 | AdbOpenSharingModeReadWrite); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 297 | if (NULL == ret->adb_read_pipe) { |
| 298 | D("AdbOpenDefaultBulkReadEndpoint failed: %s\n", |
| 299 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 300 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 303 | // Open write pipe (endpoint) |
| 304 | ret->adb_write_pipe = |
| 305 | AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface, |
| 306 | AdbOpenAccessTypeReadWrite, |
| 307 | AdbOpenSharingModeReadWrite); |
| 308 | if (NULL == ret->adb_write_pipe) { |
| 309 | D("AdbOpenDefaultBulkWriteEndpoint failed: %s\n", |
| 310 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 311 | goto fail; |
| 312 | } |
| 313 | |
| 314 | // Save interface name |
| 315 | // First get expected name length |
| 316 | AdbGetInterfaceName(ret->adb_interface, |
| 317 | NULL, |
| 318 | &name_len, |
| 319 | true); |
| 320 | if (0 == name_len) { |
| 321 | D("AdbGetInterfaceName returned name length of zero: %s\n", |
| 322 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 323 | goto fail; |
| 324 | } |
| 325 | |
| 326 | ret->interface_name = (char*)malloc(name_len); |
| 327 | if (NULL == ret->interface_name) { |
| 328 | D("Could not allocate %lu bytes for interface_name: %s\n", name_len, |
| 329 | strerror(errno)); |
| 330 | goto fail; |
| 331 | } |
| 332 | |
| 333 | // Now save the name |
| 334 | if (!AdbGetInterfaceName(ret->adb_interface, |
| 335 | ret->interface_name, |
| 336 | &name_len, |
| 337 | true)) { |
| 338 | D("AdbGetInterfaceName failed: %s\n", |
| 339 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 340 | goto fail; |
| 341 | } |
| 342 | |
| 343 | // We're done at this point |
| 344 | return ret; |
| 345 | |
| 346 | fail: |
| 347 | if (NULL != ret) { |
| 348 | usb_cleanup_handle(ret); |
| 349 | free(ret); |
| 350 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 351 | |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | int usb_write(usb_handle* handle, const void* data, int len) { |
Jack Ren | 80f2645 | 2011-08-12 18:39:04 +0800 | [diff] [blame] | 356 | unsigned long time_out = 5000; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | unsigned long written = 0; |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 358 | int err = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | |
| 360 | D("usb_write %d\n", len); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 361 | if (NULL == handle) { |
| 362 | D("usb_write was passed NULL handle\n"); |
| 363 | err = EINVAL; |
| 364 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 367 | // Perform write |
| 368 | if (!AdbWriteEndpointSync(handle->adb_write_pipe, |
| 369 | (void*)data, |
| 370 | (unsigned long)len, |
| 371 | &written, |
| 372 | time_out)) { |
| 373 | D("AdbWriteEndpointSync failed: %s\n", |
| 374 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 375 | err = EIO; |
| 376 | goto fail; |
| 377 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 379 | // Make sure that we've written what we were asked to write |
| 380 | D("usb_write got: %ld, expected: %d\n", written, len); |
| 381 | if (written != (unsigned long)len) { |
| 382 | // If this occurs, this code should be changed to repeatedly call |
| 383 | // AdbWriteEndpointSync() until all bytes are written. |
| 384 | D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld\n", |
| 385 | len, written); |
| 386 | err = EIO; |
| 387 | goto fail; |
| 388 | } |
| 389 | |
| 390 | if (handle->zero_mask && (len & handle->zero_mask) == 0) { |
| 391 | // Send a zero length packet |
| 392 | if (!AdbWriteEndpointSync(handle->adb_write_pipe, |
| 393 | (void*)data, |
| 394 | 0, |
| 395 | &written, |
| 396 | time_out)) { |
| 397 | D("AdbWriteEndpointSync of zero length packet failed: %s\n", |
| 398 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 399 | err = EIO; |
| 400 | goto fail; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | |
| 406 | fail: |
| 407 | // Any failure should cause us to kick the device instead of leaving it a |
| 408 | // zombie state with potential to hang. |
| 409 | if (NULL != handle) { |
| 410 | D("Kicking device due to error in usb_write\n"); |
| 411 | usb_kick(handle); |
| 412 | } |
| 413 | |
| 414 | D("usb_write failed\n"); |
| 415 | errno = err; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 416 | return -1; |
| 417 | } |
| 418 | |
| 419 | int usb_read(usb_handle *handle, void* data, int len) { |
Jack Ren | 80f2645 | 2011-08-12 18:39:04 +0800 | [diff] [blame] | 420 | unsigned long time_out = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | unsigned long read = 0; |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 422 | int err = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | |
| 424 | D("usb_read %d\n", len); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 425 | if (NULL == handle) { |
| 426 | D("usb_read was passed NULL handle\n"); |
| 427 | err = EINVAL; |
| 428 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 431 | while (len > 0) { |
| 432 | if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read, |
| 433 | time_out)) { |
| 434 | D("AdbReadEndpointSync failed: %s\n", |
| 435 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 436 | err = EIO; |
| 437 | goto fail; |
| 438 | } |
| 439 | 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] | 440 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 441 | data = (char *)data + read; |
| 442 | len -= read; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | |
| 447 | fail: |
| 448 | // Any failure should cause us to kick the device instead of leaving it a |
| 449 | // zombie state with potential to hang. |
| 450 | if (NULL != handle) { |
| 451 | D("Kicking device due to error in usb_read\n"); |
| 452 | usb_kick(handle); |
| 453 | } |
| 454 | |
| 455 | D("usb_read failed\n"); |
| 456 | errno = err; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 457 | return -1; |
| 458 | } |
| 459 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 460 | // Wrapper around AdbCloseHandle() that logs diagnostics. |
| 461 | static void _adb_close_handle(ADBAPIHANDLE adb_handle) { |
| 462 | if (!AdbCloseHandle(adb_handle)) { |
| 463 | D("AdbCloseHandle(%p) failed: %s\n", adb_handle, |
| 464 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 465 | } |
| 466 | } |
| 467 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 468 | void usb_cleanup_handle(usb_handle* handle) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 469 | D("usb_cleanup_handle\n"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 470 | if (NULL != handle) { |
| 471 | if (NULL != handle->interface_name) |
| 472 | free(handle->interface_name); |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 473 | // AdbCloseHandle(pipe) will break any threads out of pending IO calls and |
| 474 | // wait until the pipe no longer uses the interface. Then we can |
| 475 | // AdbCloseHandle() the interface. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 476 | if (NULL != handle->adb_write_pipe) |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 477 | _adb_close_handle(handle->adb_write_pipe); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 478 | if (NULL != handle->adb_read_pipe) |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 479 | _adb_close_handle(handle->adb_read_pipe); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 480 | if (NULL != handle->adb_interface) |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 481 | _adb_close_handle(handle->adb_interface); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 482 | |
| 483 | handle->interface_name = NULL; |
| 484 | handle->adb_write_pipe = NULL; |
| 485 | handle->adb_read_pipe = NULL; |
| 486 | handle->adb_interface = NULL; |
| 487 | } |
| 488 | } |
| 489 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 490 | static void usb_kick_locked(usb_handle* handle) { |
| 491 | // The reason the lock must be acquired before calling this function is in |
| 492 | // case multiple threads are trying to kick the same device at the same time. |
| 493 | usb_cleanup_handle(handle); |
| 494 | } |
| 495 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 496 | void usb_kick(usb_handle* handle) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 497 | D("usb_kick\n"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 498 | if (NULL != handle) { |
| 499 | adb_mutex_lock(&usb_lock); |
| 500 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 501 | usb_kick_locked(handle); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 502 | |
| 503 | adb_mutex_unlock(&usb_lock); |
| 504 | } else { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 505 | errno = EINVAL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | int usb_close(usb_handle* handle) { |
| 510 | D("usb_close\n"); |
| 511 | |
| 512 | if (NULL != handle) { |
| 513 | // Remove handle from the list |
| 514 | adb_mutex_lock(&usb_lock); |
| 515 | |
| 516 | if ((handle->next != handle) && (handle->prev != handle)) { |
| 517 | handle->next->prev = handle->prev; |
| 518 | handle->prev->next = handle->next; |
| 519 | handle->prev = handle; |
| 520 | handle->next = handle; |
| 521 | } |
| 522 | |
| 523 | adb_mutex_unlock(&usb_lock); |
| 524 | |
| 525 | // Cleanup handle |
| 526 | usb_cleanup_handle(handle); |
| 527 | free(handle); |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 533 | int recognized_device(usb_handle* handle) { |
| 534 | if (NULL == handle) |
| 535 | return 0; |
| 536 | |
| 537 | // Check vendor and product id first |
| 538 | USB_DEVICE_DESCRIPTOR device_desc; |
| 539 | |
| 540 | if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, |
| 541 | &device_desc)) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 542 | D("AdbGetUsbDeviceDescriptor failed: %s\n", |
| 543 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | // Then check interface properties |
| 548 | USB_INTERFACE_DESCRIPTOR interf_desc; |
| 549 | |
| 550 | if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, |
| 551 | &interf_desc)) { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 552 | D("AdbGetUsbInterfaceDescriptor failed: %s\n", |
| 553 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | // Must have two endpoints |
| 558 | if (2 != interf_desc.bNumEndpoints) { |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | if (is_adb_interface(device_desc.idVendor, device_desc.idProduct, |
| 563 | interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) { |
| 564 | |
| 565 | if(interf_desc.bInterfaceProtocol == 0x01) { |
| 566 | AdbEndpointInformation endpoint_info; |
| 567 | // assuming zero is a valid bulk endpoint ID |
| 568 | if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) { |
| 569 | handle->zero_mask = endpoint_info.max_packet_size - 1; |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 570 | D("device zero_mask: 0x%x\n", handle->zero_mask); |
| 571 | } else { |
| 572 | D("AdbGetEndpointInformation failed: %s\n", |
| 573 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | void find_devices() { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 584 | usb_handle* handle = NULL; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 585 | char entry_buffer[2048]; |
| 586 | char interf_name[2048]; |
| 587 | AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); |
| 588 | unsigned long entry_buffer_size = sizeof(entry_buffer); |
| 589 | char* copy_name; |
| 590 | |
| 591 | // Enumerate all present and active interfaces. |
| 592 | ADBAPIHANDLE enum_handle = |
| 593 | AdbEnumInterfaces(usb_class_id, true, true, true); |
| 594 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 595 | if (NULL == enum_handle) { |
| 596 | D("AdbEnumInterfaces failed: %s\n", |
| 597 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | return; |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 599 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 600 | |
| 601 | while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { |
| 602 | // TODO: FIXME - temp hack converting wchar_t into char. |
| 603 | // It would be better to change AdbNextInterface so it will return |
| 604 | // interface name as single char string. |
| 605 | const wchar_t* wchar_name = next_interface->device_name; |
| 606 | for(copy_name = interf_name; |
| 607 | L'\0' != *wchar_name; |
| 608 | wchar_name++, copy_name++) { |
| 609 | *copy_name = (char)(*wchar_name); |
| 610 | } |
| 611 | *copy_name = '\0'; |
| 612 | |
| 613 | // Lets see if we already have this device in the list |
| 614 | if (!known_device(interf_name)) { |
| 615 | // This seems to be a new device. Open it! |
| 616 | handle = do_usb_open(next_interface->device_name); |
| 617 | if (NULL != handle) { |
| 618 | // Lets see if this interface (device) belongs to us |
| 619 | if (recognized_device(handle)) { |
| 620 | D("adding a new device %s\n", interf_name); |
| 621 | char serial_number[512]; |
| 622 | unsigned long serial_number_len = sizeof(serial_number); |
| 623 | if (AdbGetSerialNumber(handle->adb_interface, |
| 624 | serial_number, |
| 625 | &serial_number_len, |
| 626 | true)) { |
| 627 | // Lets make sure that we don't duplicate this device |
| 628 | if (register_new_device(handle)) { |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 629 | register_usb_transport(handle, serial_number, NULL, 1); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 630 | } else { |
| 631 | D("register_new_device failed for %s\n", interf_name); |
| 632 | usb_cleanup_handle(handle); |
| 633 | free(handle); |
| 634 | } |
| 635 | } else { |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 636 | D("cannot get serial number: %s\n", |
| 637 | SystemErrorCodeToString(GetLastError()).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 638 | usb_cleanup_handle(handle); |
| 639 | free(handle); |
| 640 | } |
| 641 | } else { |
| 642 | usb_cleanup_handle(handle); |
| 643 | free(handle); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | entry_buffer_size = sizeof(entry_buffer); |
| 649 | } |
| 650 | |
Spencer Low | 9f1ba56 | 2015-07-22 16:17:07 -0700 | [diff] [blame] | 651 | if (GetLastError() != ERROR_NO_MORE_ITEMS) { |
| 652 | // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration. |
| 653 | D("AdbNextInterface failed: %s\n", |
| 654 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 655 | } |
| 656 | |
| 657 | _adb_close_handle(enum_handle); |
| 658 | } |
| 659 | |
| 660 | static void kick_devices() { |
| 661 | // Need to acquire lock to safely walk the list which might be modified |
| 662 | // by another thread. |
| 663 | adb_mutex_lock(&usb_lock); |
| 664 | for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) { |
| 665 | usb_kick_locked(usb); |
| 666 | } |
| 667 | adb_mutex_unlock(&usb_lock); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 668 | } |