blob: 8ecca3724dc5933e37577c069c936c1b9fa7958d [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.
Josh Gaob5fea142016-02-12 14:31:15 -0800100static void device_poll_thread(void*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101
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
Josh Gaob5fea142016-02-12 14:31:15 -0800175void device_poll_thread(void*) {
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 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183}
184
Spencer Lowa5b06b02015-07-22 16:17:07 -0700185static 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
Josh Gaob5fea142016-02-12 14:31:15 -0800204static void _power_notification_thread(void*) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700205 // 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.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700211 D("Created power notification thread");
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700212 adb_thread_setname("Power Notifier");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700213
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",
David Pursell5f787ed2016-01-27 08:52:53 -0800224 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700225 }
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",
David Pursell5f787ed2016-01-27 08:52:53 -0800235 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700236 }
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",
David Pursell5f787ed2016-01-27 08:52:53 -0800242 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700243 }
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.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700255 D("Power notification thread exiting");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700256}
257
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258void usb_init() {
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700259 if (!adb_thread_create(device_poll_thread, nullptr)) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700260 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 Projectdd7bc332009-03-03 19:32:55 -0800264 }
265}
266
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267usb_handle* do_usb_open(const wchar_t* interface_name) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700268 unsigned long name_len = 0;
269
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 // Allocate our handle
Spencer Lowa5b06b02015-07-22 16:17:07 -0700271 usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
272 if (NULL == ret) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700273 D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle),
Spencer Lowa5b06b02015-07-22 16:17:07 -0700274 strerror(errno));
275 goto fail;
276 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277
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 Projectdd7bc332009-03-03 19:32:55 -0800284 if (NULL == ret->adb_interface) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700285 D("AdbCreateInterfaceByName failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800286 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700287 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 }
289
290 // Open read pipe (endpoint)
291 ret->adb_read_pipe =
292 AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
293 AdbOpenAccessTypeReadWrite,
294 AdbOpenSharingModeReadWrite);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700295 if (NULL == ret->adb_read_pipe) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700296 D("AdbOpenDefaultBulkReadEndpoint failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800297 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700298 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 }
300
Spencer Lowa5b06b02015-07-22 16:17:07 -0700301 // 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) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700307 D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800308 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700309 goto fail;
310 }
311
312 // Save interface name
313 // First get expected name length
314 AdbGetInterfaceName(ret->adb_interface,
315 NULL,
316 &name_len,
Spencer Lowbb290002015-11-12 20:13:21 -0800317 false);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700318 if (0 == name_len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700319 D("AdbGetInterfaceName returned name length of zero: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800320 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700321 goto fail;
322 }
323
Spencer Lowbb290002015-11-12 20:13:21 -0800324 ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0]));
Spencer Lowa5b06b02015-07-22 16:17:07 -0700325 if (NULL == ret->interface_name) {
Spencer Lowbb290002015-11-12 20:13:21 -0800326 D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno));
Spencer Lowa5b06b02015-07-22 16:17:07 -0700327 goto fail;
328 }
329
330 // Now save the name
331 if (!AdbGetInterfaceName(ret->adb_interface,
332 ret->interface_name,
333 &name_len,
Spencer Lowbb290002015-11-12 20:13:21 -0800334 false)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700335 D("AdbGetInterfaceName failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800336 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700337 goto fail;
338 }
339
340 // We're done at this point
341 return ret;
342
343fail:
344 if (NULL != ret) {
345 usb_cleanup_handle(ret);
346 free(ret);
347 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348
349 return NULL;
350}
351
352int usb_write(usb_handle* handle, const void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800353 unsigned long time_out = 5000;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 unsigned long written = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700355 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700357 D("usb_write %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700358 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700359 D("usb_write was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700360 err = EINVAL;
361 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 }
363
Spencer Lowa5b06b02015-07-22 16:17:07 -0700364 // Perform write
365 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
366 (void*)data,
367 (unsigned long)len,
368 &written,
369 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700370 D("AdbWriteEndpointSync failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800371 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700372 err = EIO;
373 goto fail;
374 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375
Spencer Lowa5b06b02015-07-22 16:17:07 -0700376 // Make sure that we've written what we were asked to write
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700377 D("usb_write got: %ld, expected: %d", written, len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700378 if (written != (unsigned long)len) {
379 // If this occurs, this code should be changed to repeatedly call
380 // AdbWriteEndpointSync() until all bytes are written.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700381 D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700382 len, written);
383 err = EIO;
384 goto fail;
385 }
386
387 if (handle->zero_mask && (len & handle->zero_mask) == 0) {
388 // Send a zero length packet
389 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
390 (void*)data,
391 0,
392 &written,
393 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700394 D("AdbWriteEndpointSync of zero length packet failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800395 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700396 err = EIO;
397 goto fail;
398 }
399 }
400
401 return 0;
402
403fail:
404 // Any failure should cause us to kick the device instead of leaving it a
405 // zombie state with potential to hang.
406 if (NULL != handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700407 D("Kicking device due to error in usb_write");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700408 usb_kick(handle);
409 }
410
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700411 D("usb_write failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700412 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 return -1;
414}
415
416int usb_read(usb_handle *handle, void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800417 unsigned long time_out = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 unsigned long read = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700419 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700421 D("usb_read %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700422 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700423 D("usb_read was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700424 err = EINVAL;
425 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 }
427
Spencer Lowa5b06b02015-07-22 16:17:07 -0700428 while (len > 0) {
429 if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
430 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700431 D("AdbReadEndpointSync failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800432 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700433 err = EIO;
434 goto fail;
435 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700436 D("usb_read got: %ld, expected: %d", read, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437
Spencer Lowa5b06b02015-07-22 16:17:07 -0700438 data = (char *)data + read;
439 len -= read;
440 }
441
442 return 0;
443
444fail:
445 // Any failure should cause us to kick the device instead of leaving it a
446 // zombie state with potential to hang.
447 if (NULL != handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700448 D("Kicking device due to error in usb_read");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700449 usb_kick(handle);
450 }
451
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700452 D("usb_read failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700453 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 return -1;
455}
456
Spencer Lowa5b06b02015-07-22 16:17:07 -0700457// Wrapper around AdbCloseHandle() that logs diagnostics.
458static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
459 if (!AdbCloseHandle(adb_handle)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700460 D("AdbCloseHandle(%p) failed: %s", adb_handle,
David Pursell5f787ed2016-01-27 08:52:53 -0800461 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700462 }
463}
464
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465void usb_cleanup_handle(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700466 D("usb_cleanup_handle");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 if (NULL != handle) {
468 if (NULL != handle->interface_name)
469 free(handle->interface_name);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700470 // AdbCloseHandle(pipe) will break any threads out of pending IO calls and
471 // wait until the pipe no longer uses the interface. Then we can
472 // AdbCloseHandle() the interface.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 if (NULL != handle->adb_write_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700474 _adb_close_handle(handle->adb_write_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 if (NULL != handle->adb_read_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700476 _adb_close_handle(handle->adb_read_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 if (NULL != handle->adb_interface)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700478 _adb_close_handle(handle->adb_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479
480 handle->interface_name = NULL;
481 handle->adb_write_pipe = NULL;
482 handle->adb_read_pipe = NULL;
483 handle->adb_interface = NULL;
484 }
485}
486
Spencer Lowa5b06b02015-07-22 16:17:07 -0700487static void usb_kick_locked(usb_handle* handle) {
488 // The reason the lock must be acquired before calling this function is in
489 // case multiple threads are trying to kick the same device at the same time.
490 usb_cleanup_handle(handle);
491}
492
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493void usb_kick(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700494 D("usb_kick");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 if (NULL != handle) {
496 adb_mutex_lock(&usb_lock);
497
Spencer Lowa5b06b02015-07-22 16:17:07 -0700498 usb_kick_locked(handle);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
500 adb_mutex_unlock(&usb_lock);
501 } else {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700502 errno = EINVAL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 }
504}
505
506int usb_close(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700507 D("usb_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508
509 if (NULL != handle) {
510 // Remove handle from the list
511 adb_mutex_lock(&usb_lock);
512
513 if ((handle->next != handle) && (handle->prev != handle)) {
514 handle->next->prev = handle->prev;
515 handle->prev->next = handle->next;
516 handle->prev = handle;
517 handle->next = handle;
518 }
519
520 adb_mutex_unlock(&usb_lock);
521
522 // Cleanup handle
523 usb_cleanup_handle(handle);
524 free(handle);
525 }
526
527 return 0;
528}
529
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530int recognized_device(usb_handle* handle) {
531 if (NULL == handle)
532 return 0;
533
534 // Check vendor and product id first
535 USB_DEVICE_DESCRIPTOR device_desc;
536
537 if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
538 &device_desc)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700539 D("AdbGetUsbDeviceDescriptor failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800540 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 return 0;
542 }
543
544 // Then check interface properties
545 USB_INTERFACE_DESCRIPTOR interf_desc;
546
547 if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
548 &interf_desc)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700549 D("AdbGetUsbInterfaceDescriptor failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800550 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551 return 0;
552 }
553
554 // Must have two endpoints
555 if (2 != interf_desc.bNumEndpoints) {
556 return 0;
557 }
558
559 if (is_adb_interface(device_desc.idVendor, device_desc.idProduct,
560 interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) {
561
562 if(interf_desc.bInterfaceProtocol == 0x01) {
563 AdbEndpointInformation endpoint_info;
564 // assuming zero is a valid bulk endpoint ID
565 if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
566 handle->zero_mask = endpoint_info.max_packet_size - 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700567 D("device zero_mask: 0x%x", handle->zero_mask);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700568 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700569 D("AdbGetEndpointInformation failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800570 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571 }
572 }
573
574 return 1;
575 }
576
577 return 0;
578}
579
580void find_devices() {
Spencer Lowbb290002015-11-12 20:13:21 -0800581 usb_handle* handle = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582 char entry_buffer[2048];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583 AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
584 unsigned long entry_buffer_size = sizeof(entry_buffer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585
586 // Enumerate all present and active interfaces.
587 ADBAPIHANDLE enum_handle =
588 AdbEnumInterfaces(usb_class_id, true, true, true);
589
Spencer Lowa5b06b02015-07-22 16:17:07 -0700590 if (NULL == enum_handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700591 D("AdbEnumInterfaces failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800592 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 return;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700594 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595
596 while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 // Lets see if we already have this device in the list
Spencer Lowbb290002015-11-12 20:13:21 -0800598 if (!known_device(next_interface->device_name)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 // This seems to be a new device. Open it!
Spencer Lowbb290002015-11-12 20:13:21 -0800600 handle = do_usb_open(next_interface->device_name);
601 if (NULL != handle) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602 // Lets see if this interface (device) belongs to us
603 if (recognized_device(handle)) {
Spencer Lowbb290002015-11-12 20:13:21 -0800604 D("adding a new device %ls", next_interface->device_name);
605
606 // We don't request a wchar_t string from AdbGetSerialNumber() because of a bug in
607 // adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString, bytes_written) where the
608 // last parameter should be (str_len * sizeof(wchar_t)). The bug reads 2 bytes past the
609 // end of a stack buffer in the best case, and in the unlikely case of a long serial
610 // number, it will read 2 bytes past the end of a heap allocation. This doesn't affect the
611 // resulting string, but we should avoid the bad reads in the first place.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 char serial_number[512];
613 unsigned long serial_number_len = sizeof(serial_number);
614 if (AdbGetSerialNumber(handle->adb_interface,
615 serial_number,
616 &serial_number_len,
617 true)) {
618 // Lets make sure that we don't duplicate this device
619 if (register_new_device(handle)) {
Scott Andersone109d262012-04-20 11:21:14 -0700620 register_usb_transport(handle, serial_number, NULL, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621 } else {
Spencer Lowbb290002015-11-12 20:13:21 -0800622 D("register_new_device failed for %ls", next_interface->device_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623 usb_cleanup_handle(handle);
624 free(handle);
625 }
626 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700627 D("cannot get serial number: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800628 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 usb_cleanup_handle(handle);
630 free(handle);
631 }
632 } else {
633 usb_cleanup_handle(handle);
634 free(handle);
635 }
636 }
637 }
638
639 entry_buffer_size = sizeof(entry_buffer);
640 }
641
Spencer Lowa5b06b02015-07-22 16:17:07 -0700642 if (GetLastError() != ERROR_NO_MORE_ITEMS) {
643 // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700644 D("AdbNextInterface failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800645 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700646 }
647
648 _adb_close_handle(enum_handle);
649}
650
651static void kick_devices() {
652 // Need to acquire lock to safely walk the list which might be modified
653 // by another thread.
654 adb_mutex_lock(&usb_lock);
655 for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) {
656 usb_kick_locked(usb);
657 }
658 adb_mutex_unlock(&usb_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659}