blob: e79008f48ac89db8a2e062d2edc893d0376041ff [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albert76649012015-02-24 15:51:19 -080021#include <winsock2.h> // winsock.h *must* be included before windows.h.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <adb_api.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdio.h>
Christopher Ferris67a7a4a2014-11-06 14:34:24 -080025#include <stdlib.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <usb100.h>
27#include <windows.h>
28#include <winerror.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
David Pursell5f787ed2016-01-27 08:52:53 -080030#include <android-base/errors.h>
31
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080033#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35/** Structure usb_handle describes our connection to the usb device via
36 AdbWinApi.dll. This structure is returned from usb_open() routine and
37 is expected in each subsequent call that is accessing the device.
Spencer Lowa5b06b02015-07-22 16:17:07 -070038
39 Most members are protected by usb_lock, except for adb_{read,write}_pipe which
40 rely on AdbWinApi.dll's handle validation and AdbCloseHandle(endpoint)'s
41 ability to break a thread out of pipe IO.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042*/
43struct usb_handle {
44 /// Previous entry in the list of opened usb handles
45 usb_handle *prev;
46
47 /// Next entry in the list of opened usb handles
48 usb_handle *next;
49
50 /// Handle to USB interface
51 ADBAPIHANDLE adb_interface;
52
53 /// Handle to USB read pipe (endpoint)
54 ADBAPIHANDLE adb_read_pipe;
55
56 /// Handle to USB write pipe (endpoint)
57 ADBAPIHANDLE adb_write_pipe;
58
59 /// Interface name
Spencer Lowbb290002015-11-12 20:13:21 -080060 wchar_t* interface_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
62 /// Mask for determining when to use zero length packets
63 unsigned zero_mask;
64};
65
66/// Class ID assigned to the device by androidusb.sys
67static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
68
69/// List of opened usb handles
70static usb_handle handle_list = {
71 .prev = &handle_list,
72 .next = &handle_list,
73};
74
75/// Locker for the list of opened usb handles
76ADB_MUTEX_DEFINE( usb_lock );
77
78/// Checks if there is opened usb handle in handle_list for this device.
Spencer Lowbb290002015-11-12 20:13:21 -080079int known_device(const wchar_t* dev_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
81/// Checks if there is opened usb handle in handle_list for this device.
82/// usb_lock mutex must be held before calling this routine.
Spencer Lowbb290002015-11-12 20:13:21 -080083int known_device_locked(const wchar_t* dev_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084
85/// Registers opened usb handle (adds it to handle_list).
86int register_new_device(usb_handle* handle);
87
88/// Checks if interface (device) matches certain criteria
89int recognized_device(usb_handle* handle);
90
91/// Enumerates present and available interfaces (devices), opens new ones and
92/// registers usb transport for them.
93void find_devices();
94
Spencer Lowa5b06b02015-07-22 16:17:07 -070095/// Kicks all USB devices
96static void kick_devices();
97
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098/// Entry point for thread that polls (every second) for new usb interfaces.
99/// This routine calls find_devices in infinite loop.
100void* device_poll_thread(void* unused);
101
102/// Initializes this module
103void usb_init();
104
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105/// Opens usb interface (device) by interface (device) name.
106usb_handle* do_usb_open(const wchar_t* interface_name);
107
108/// Writes data to the opened usb handle
109int usb_write(usb_handle* handle, const void* data, int len);
110
111/// Reads data using the opened usb handle
112int usb_read(usb_handle *handle, void* data, int len);
113
114/// Cleans up opened usb handle
115void usb_cleanup_handle(usb_handle* handle);
116
117/// Cleans up (but don't close) opened usb handle
118void usb_kick(usb_handle* handle);
119
120/// Closes opened usb handle
121int usb_close(usb_handle* handle);
122
Spencer Lowbb290002015-11-12 20:13:21 -0800123int known_device_locked(const wchar_t* dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 usb_handle* usb;
125
126 if (NULL != dev_name) {
127 // Iterate through the list looking for the name match.
128 for(usb = handle_list.next; usb != &handle_list; usb = usb->next) {
129 // In Windows names are not case sensetive!
130 if((NULL != usb->interface_name) &&
Spencer Lowbb290002015-11-12 20:13:21 -0800131 (0 == wcsicmp(usb->interface_name, dev_name))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 return 1;
133 }
134 }
135 }
136
137 return 0;
138}
139
Spencer Lowbb290002015-11-12 20:13:21 -0800140int known_device(const wchar_t* dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 int ret = 0;
142
143 if (NULL != dev_name) {
144 adb_mutex_lock(&usb_lock);
145 ret = known_device_locked(dev_name);
146 adb_mutex_unlock(&usb_lock);
147 }
148
149 return ret;
150}
151
152int register_new_device(usb_handle* handle) {
153 if (NULL == handle)
154 return 0;
155
156 adb_mutex_lock(&usb_lock);
157
158 // Check if device is already in the list
159 if (known_device_locked(handle->interface_name)) {
160 adb_mutex_unlock(&usb_lock);
161 return 0;
162 }
163
164 // Not in the list. Add this handle to the list.
165 handle->next = &handle_list;
166 handle->prev = handle_list.prev;
167 handle->prev->next = handle;
168 handle->next->prev = handle;
169
170 adb_mutex_unlock(&usb_lock);
171
172 return 1;
173}
174
175void* device_poll_thread(void* unused) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700176 adb_thread_setname("Device Poll");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700177 D("Created device thread");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
179 while(1) {
180 find_devices();
181 adb_sleep_ms(1000);
182 }
183
184 return NULL;
185}
186
Spencer Lowa5b06b02015-07-22 16:17:07 -0700187static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam,
188 LPARAM lParam) {
189 switch (uMsg) {
190 case WM_POWERBROADCAST:
191 switch (wParam) {
192 case PBT_APMRESUMEAUTOMATIC:
193 // Resuming from sleep or hibernation, so kick all existing USB devices
194 // and then allow the device_poll_thread to redetect USB devices from
195 // scratch. If we don't do this, existing USB devices will never respond
196 // to us because they'll be waiting for the connect/auth handshake.
197 D("Received (WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC) notification, "
198 "so kicking all USB devices\n");
199 kick_devices();
200 return TRUE;
201 }
202 }
203 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
204}
205
206static void* _power_notification_thread(void* unused) {
207 // This uses a thread with its own window message pump to get power
208 // notifications. If adb runs from a non-interactive service account, this
209 // might not work (not sure). If that happens to not work, we could use
210 // heavyweight WMI APIs to get power notifications. But for the common case
211 // of a developer's interactive session, a window message pump is more
212 // appropriate.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700213 D("Created power notification thread");
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700214 adb_thread_setname("Power Notifier");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700215
216 // Window class names are process specific.
217 static const WCHAR kPowerNotificationWindowClassName[] =
218 L"PowerNotificationWindow";
219
220 // Get the HINSTANCE corresponding to the module that _power_window_proc
221 // is in (the main module).
222 const HINSTANCE instance = GetModuleHandleW(NULL);
223 if (!instance) {
224 // This is such a common API call that this should never fail.
225 fatal("GetModuleHandleW failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800226 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700227 }
228
229 WNDCLASSEXW wndclass;
230 memset(&wndclass, 0, sizeof(wndclass));
231 wndclass.cbSize = sizeof(wndclass);
232 wndclass.lpfnWndProc = _power_window_proc;
233 wndclass.hInstance = instance;
234 wndclass.lpszClassName = kPowerNotificationWindowClassName;
235 if (!RegisterClassExW(&wndclass)) {
236 fatal("RegisterClassExW failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800237 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700238 }
239
240 if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
241 L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0,
242 NULL, NULL, instance, NULL)) {
243 fatal("CreateWindowExW failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800244 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700245 }
246
247 MSG msg;
248 while (GetMessageW(&msg, NULL, 0, 0)) {
249 TranslateMessage(&msg);
250 DispatchMessageW(&msg);
251 }
252
253 // GetMessageW() will return false if a quit message is posted. We don't
254 // do that, but it might be possible for that to occur when logging off or
255 // shutting down. Not a big deal since the whole process will be going away
256 // soon anyway.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700257 D("Power notification thread exiting");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700258
259 return NULL;
260}
261
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262void usb_init() {
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700263 if (!adb_thread_create(device_poll_thread, nullptr)) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700264 fatal_errno("cannot create device poll thread");
265 }
266 if (!adb_thread_create(_power_notification_thread, nullptr)) {
267 fatal_errno("cannot create power notification thread");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 }
269}
270
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271usb_handle* do_usb_open(const wchar_t* interface_name) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700272 unsigned long name_len = 0;
273
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 // Allocate our handle
Spencer Lowa5b06b02015-07-22 16:17:07 -0700275 usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
276 if (NULL == ret) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700277 D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle),
Spencer Lowa5b06b02015-07-22 16:17:07 -0700278 strerror(errno));
279 goto fail;
280 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281
282 // Set linkers back to the handle
283 ret->next = ret;
284 ret->prev = ret;
285
286 // Create interface.
287 ret->adb_interface = AdbCreateInterfaceByName(interface_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 if (NULL == ret->adb_interface) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700289 D("AdbCreateInterfaceByName failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800290 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700291 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 }
293
294 // Open read pipe (endpoint)
295 ret->adb_read_pipe =
296 AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
297 AdbOpenAccessTypeReadWrite,
298 AdbOpenSharingModeReadWrite);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700299 if (NULL == ret->adb_read_pipe) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700300 D("AdbOpenDefaultBulkReadEndpoint failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800301 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700302 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 }
304
Spencer Lowa5b06b02015-07-22 16:17:07 -0700305 // Open write pipe (endpoint)
306 ret->adb_write_pipe =
307 AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface,
308 AdbOpenAccessTypeReadWrite,
309 AdbOpenSharingModeReadWrite);
310 if (NULL == ret->adb_write_pipe) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700311 D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800312 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700313 goto fail;
314 }
315
316 // Save interface name
317 // First get expected name length
318 AdbGetInterfaceName(ret->adb_interface,
319 NULL,
320 &name_len,
Spencer Lowbb290002015-11-12 20:13:21 -0800321 false);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700322 if (0 == name_len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700323 D("AdbGetInterfaceName returned name length of zero: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800324 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700325 goto fail;
326 }
327
Spencer Lowbb290002015-11-12 20:13:21 -0800328 ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0]));
Spencer Lowa5b06b02015-07-22 16:17:07 -0700329 if (NULL == ret->interface_name) {
Spencer Lowbb290002015-11-12 20:13:21 -0800330 D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno));
Spencer Lowa5b06b02015-07-22 16:17:07 -0700331 goto fail;
332 }
333
334 // Now save the name
335 if (!AdbGetInterfaceName(ret->adb_interface,
336 ret->interface_name,
337 &name_len,
Spencer Lowbb290002015-11-12 20:13:21 -0800338 false)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700339 D("AdbGetInterfaceName failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800340 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700341 goto fail;
342 }
343
344 // We're done at this point
345 return ret;
346
347fail:
348 if (NULL != ret) {
349 usb_cleanup_handle(ret);
350 free(ret);
351 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352
353 return NULL;
354}
355
356int usb_write(usb_handle* handle, const void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800357 unsigned long time_out = 5000;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 unsigned long written = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700359 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700361 D("usb_write %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700362 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700363 D("usb_write was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700364 err = EINVAL;
365 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 }
367
Spencer Lowa5b06b02015-07-22 16:17:07 -0700368 // Perform write
369 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
370 (void*)data,
371 (unsigned long)len,
372 &written,
373 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700374 D("AdbWriteEndpointSync failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800375 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700376 err = EIO;
377 goto fail;
378 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379
Spencer Lowa5b06b02015-07-22 16:17:07 -0700380 // Make sure that we've written what we were asked to write
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700381 D("usb_write got: %ld, expected: %d", written, len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700382 if (written != (unsigned long)len) {
383 // If this occurs, this code should be changed to repeatedly call
384 // AdbWriteEndpointSync() until all bytes are written.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700385 D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700386 len, written);
387 err = EIO;
388 goto fail;
389 }
390
391 if (handle->zero_mask && (len & handle->zero_mask) == 0) {
392 // Send a zero length packet
393 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
394 (void*)data,
395 0,
396 &written,
397 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700398 D("AdbWriteEndpointSync of zero length packet failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800399 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700400 err = EIO;
401 goto fail;
402 }
403 }
404
405 return 0;
406
407fail:
408 // Any failure should cause us to kick the device instead of leaving it a
409 // zombie state with potential to hang.
410 if (NULL != handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700411 D("Kicking device due to error in usb_write");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700412 usb_kick(handle);
413 }
414
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700415 D("usb_write failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700416 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 return -1;
418}
419
420int usb_read(usb_handle *handle, void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800421 unsigned long time_out = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 unsigned long read = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700423 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700425 D("usb_read %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700426 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700427 D("usb_read was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700428 err = EINVAL;
429 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 }
431
Spencer Lowa5b06b02015-07-22 16:17:07 -0700432 while (len > 0) {
433 if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
434 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700435 D("AdbReadEndpointSync failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800436 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700437 err = EIO;
438 goto fail;
439 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700440 D("usb_read got: %ld, expected: %d", read, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441
Spencer Lowa5b06b02015-07-22 16:17:07 -0700442 data = (char *)data + read;
443 len -= read;
444 }
445
446 return 0;
447
448fail:
449 // Any failure should cause us to kick the device instead of leaving it a
450 // zombie state with potential to hang.
451 if (NULL != handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700452 D("Kicking device due to error in usb_read");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700453 usb_kick(handle);
454 }
455
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700456 D("usb_read failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700457 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 return -1;
459}
460
Spencer Lowa5b06b02015-07-22 16:17:07 -0700461// Wrapper around AdbCloseHandle() that logs diagnostics.
462static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
463 if (!AdbCloseHandle(adb_handle)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700464 D("AdbCloseHandle(%p) failed: %s", adb_handle,
David Pursell5f787ed2016-01-27 08:52:53 -0800465 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700466 }
467}
468
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469void usb_cleanup_handle(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700470 D("usb_cleanup_handle");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 if (NULL != handle) {
472 if (NULL != handle->interface_name)
473 free(handle->interface_name);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700474 // AdbCloseHandle(pipe) will break any threads out of pending IO calls and
475 // wait until the pipe no longer uses the interface. Then we can
476 // AdbCloseHandle() the interface.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 if (NULL != handle->adb_write_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700478 _adb_close_handle(handle->adb_write_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 if (NULL != handle->adb_read_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700480 _adb_close_handle(handle->adb_read_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 if (NULL != handle->adb_interface)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700482 _adb_close_handle(handle->adb_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483
484 handle->interface_name = NULL;
485 handle->adb_write_pipe = NULL;
486 handle->adb_read_pipe = NULL;
487 handle->adb_interface = NULL;
488 }
489}
490
Spencer Lowa5b06b02015-07-22 16:17:07 -0700491static void usb_kick_locked(usb_handle* handle) {
492 // The reason the lock must be acquired before calling this function is in
493 // case multiple threads are trying to kick the same device at the same time.
494 usb_cleanup_handle(handle);
495}
496
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497void usb_kick(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700498 D("usb_kick");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 if (NULL != handle) {
500 adb_mutex_lock(&usb_lock);
501
Spencer Lowa5b06b02015-07-22 16:17:07 -0700502 usb_kick_locked(handle);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503
504 adb_mutex_unlock(&usb_lock);
505 } else {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700506 errno = EINVAL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 }
508}
509
510int usb_close(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700511 D("usb_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512
513 if (NULL != handle) {
514 // Remove handle from the list
515 adb_mutex_lock(&usb_lock);
516
517 if ((handle->next != handle) && (handle->prev != handle)) {
518 handle->next->prev = handle->prev;
519 handle->prev->next = handle->next;
520 handle->prev = handle;
521 handle->next = handle;
522 }
523
524 adb_mutex_unlock(&usb_lock);
525
526 // Cleanup handle
527 usb_cleanup_handle(handle);
528 free(handle);
529 }
530
531 return 0;
532}
533
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534int recognized_device(usb_handle* handle) {
535 if (NULL == handle)
536 return 0;
537
538 // Check vendor and product id first
539 USB_DEVICE_DESCRIPTOR device_desc;
540
541 if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
542 &device_desc)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700543 D("AdbGetUsbDeviceDescriptor failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800544 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 return 0;
546 }
547
548 // Then check interface properties
549 USB_INTERFACE_DESCRIPTOR interf_desc;
550
551 if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
552 &interf_desc)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700553 D("AdbGetUsbInterfaceDescriptor failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800554 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800555 return 0;
556 }
557
558 // Must have two endpoints
559 if (2 != interf_desc.bNumEndpoints) {
560 return 0;
561 }
562
563 if (is_adb_interface(device_desc.idVendor, device_desc.idProduct,
564 interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) {
565
566 if(interf_desc.bInterfaceProtocol == 0x01) {
567 AdbEndpointInformation endpoint_info;
568 // assuming zero is a valid bulk endpoint ID
569 if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
570 handle->zero_mask = endpoint_info.max_packet_size - 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700571 D("device zero_mask: 0x%x", handle->zero_mask);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700572 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700573 D("AdbGetEndpointInformation failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800574 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575 }
576 }
577
578 return 1;
579 }
580
581 return 0;
582}
583
584void find_devices() {
Spencer Lowbb290002015-11-12 20:13:21 -0800585 usb_handle* handle = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586 char entry_buffer[2048];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587 AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
588 unsigned long entry_buffer_size = sizeof(entry_buffer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589
590 // Enumerate all present and active interfaces.
591 ADBAPIHANDLE enum_handle =
592 AdbEnumInterfaces(usb_class_id, true, true, true);
593
Spencer Lowa5b06b02015-07-22 16:17:07 -0700594 if (NULL == enum_handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700595 D("AdbEnumInterfaces failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800596 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 return;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700598 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599
600 while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 // Lets see if we already have this device in the list
Spencer Lowbb290002015-11-12 20:13:21 -0800602 if (!known_device(next_interface->device_name)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 // This seems to be a new device. Open it!
Spencer Lowbb290002015-11-12 20:13:21 -0800604 handle = do_usb_open(next_interface->device_name);
605 if (NULL != handle) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 // Lets see if this interface (device) belongs to us
607 if (recognized_device(handle)) {
Spencer Lowbb290002015-11-12 20:13:21 -0800608 D("adding a new device %ls", next_interface->device_name);
609
610 // We don't request a wchar_t string from AdbGetSerialNumber() because of a bug in
611 // adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString, bytes_written) where the
612 // last parameter should be (str_len * sizeof(wchar_t)). The bug reads 2 bytes past the
613 // end of a stack buffer in the best case, and in the unlikely case of a long serial
614 // number, it will read 2 bytes past the end of a heap allocation. This doesn't affect the
615 // resulting string, but we should avoid the bad reads in the first place.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616 char serial_number[512];
617 unsigned long serial_number_len = sizeof(serial_number);
618 if (AdbGetSerialNumber(handle->adb_interface,
619 serial_number,
620 &serial_number_len,
621 true)) {
622 // Lets make sure that we don't duplicate this device
623 if (register_new_device(handle)) {
Scott Andersone109d262012-04-20 11:21:14 -0700624 register_usb_transport(handle, serial_number, NULL, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800625 } else {
Spencer Lowbb290002015-11-12 20:13:21 -0800626 D("register_new_device failed for %ls", next_interface->device_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627 usb_cleanup_handle(handle);
628 free(handle);
629 }
630 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700631 D("cannot get serial number: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800632 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633 usb_cleanup_handle(handle);
634 free(handle);
635 }
636 } else {
637 usb_cleanup_handle(handle);
638 free(handle);
639 }
640 }
641 }
642
643 entry_buffer_size = sizeof(entry_buffer);
644 }
645
Spencer Lowa5b06b02015-07-22 16:17:07 -0700646 if (GetLastError() != ERROR_NO_MORE_ITEMS) {
647 // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700648 D("AdbNextInterface failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800649 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700650 }
651
652 _adb_close_handle(enum_handle);
653}
654
655static void kick_devices() {
656 // Need to acquire lock to safely walk the list which might be modified
657 // by another thread.
658 adb_mutex_lock(&usb_lock);
659 for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) {
660 usb_kick_locked(usb);
661 }
662 adb_mutex_unlock(&usb_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663}