blob: 8d3501e24f4ba0d8b173d0e8584f88975b286d39 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080031#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
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 Lowa5b06b02015-07-22 16:17:07 -070036
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 Projectdd7bc332009-03-03 19:32:55 -080040*/
41struct 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
Spencer Lowbb290002015-11-12 20:13:21 -080058 wchar_t* interface_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
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
65static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
66
67/// List of opened usb handles
68static usb_handle handle_list = {
69 .prev = &handle_list,
70 .next = &handle_list,
71};
72
73/// Locker for the list of opened usb handles
74ADB_MUTEX_DEFINE( usb_lock );
75
76/// Checks if there is opened usb handle in handle_list for this device.
Spencer Lowbb290002015-11-12 20:13:21 -080077int known_device(const wchar_t* dev_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
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.
Spencer Lowbb290002015-11-12 20:13:21 -080081int known_device_locked(const wchar_t* dev_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
83/// Registers opened usb handle (adds it to handle_list).
84int register_new_device(usb_handle* handle);
85
86/// Checks if interface (device) matches certain criteria
87int recognized_device(usb_handle* handle);
88
89/// Enumerates present and available interfaces (devices), opens new ones and
90/// registers usb transport for them.
91void find_devices();
92
Spencer Lowa5b06b02015-07-22 16:17:07 -070093/// Kicks all USB devices
94static void kick_devices();
95
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096/// Entry point for thread that polls (every second) for new usb interfaces.
97/// This routine calls find_devices in infinite loop.
98void* device_poll_thread(void* unused);
99
100/// Initializes this module
101void usb_init();
102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103/// Opens usb interface (device) by interface (device) name.
104usb_handle* do_usb_open(const wchar_t* interface_name);
105
106/// Writes data to the opened usb handle
107int usb_write(usb_handle* handle, const void* data, int len);
108
109/// Reads data using the opened usb handle
110int usb_read(usb_handle *handle, void* data, int len);
111
112/// Cleans up opened usb handle
113void usb_cleanup_handle(usb_handle* handle);
114
115/// Cleans up (but don't close) opened usb handle
116void usb_kick(usb_handle* handle);
117
118/// Closes opened usb handle
119int usb_close(usb_handle* handle);
120
Spencer Lowbb290002015-11-12 20:13:21 -0800121int known_device_locked(const wchar_t* dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 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) &&
Spencer Lowbb290002015-11-12 20:13:21 -0800129 (0 == wcsicmp(usb->interface_name, dev_name))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 return 1;
131 }
132 }
133 }
134
135 return 0;
136}
137
Spencer Lowbb290002015-11-12 20:13:21 -0800138int known_device(const wchar_t* dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 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
150int 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
173void* device_poll_thread(void* unused) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700174 adb_thread_setname("Device Poll");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700175 D("Created device thread");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
177 while(1) {
178 find_devices();
179 adb_sleep_ms(1000);
180 }
181
182 return NULL;
183}
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
204static 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.
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",
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.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700255 D("Power notification thread exiting");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700256
257 return NULL;
258}
259
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260void usb_init() {
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700261 if (!adb_thread_create(device_poll_thread, nullptr)) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700262 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 Projectdd7bc332009-03-03 19:32:55 -0800266 }
267}
268
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269usb_handle* do_usb_open(const wchar_t* interface_name) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700270 unsigned long name_len = 0;
271
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 // Allocate our handle
Spencer Lowa5b06b02015-07-22 16:17:07 -0700273 usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
274 if (NULL == ret) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700275 D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle),
Spencer Lowa5b06b02015-07-22 16:17:07 -0700276 strerror(errno));
277 goto fail;
278 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279
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 Projectdd7bc332009-03-03 19:32:55 -0800286 if (NULL == ret->adb_interface) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700287 D("AdbCreateInterfaceByName failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700288 SystemErrorCodeToString(GetLastError()).c_str());
289 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 }
291
292 // Open read pipe (endpoint)
293 ret->adb_read_pipe =
294 AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
295 AdbOpenAccessTypeReadWrite,
296 AdbOpenSharingModeReadWrite);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700297 if (NULL == ret->adb_read_pipe) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700298 D("AdbOpenDefaultBulkReadEndpoint failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700299 SystemErrorCodeToString(GetLastError()).c_str());
300 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 }
302
Spencer Lowa5b06b02015-07-22 16:17:07 -0700303 // 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) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700309 D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700310 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,
Spencer Lowbb290002015-11-12 20:13:21 -0800319 false);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700320 if (0 == name_len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700321 D("AdbGetInterfaceName returned name length of zero: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700322 SystemErrorCodeToString(GetLastError()).c_str());
323 goto fail;
324 }
325
Spencer Lowbb290002015-11-12 20:13:21 -0800326 ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0]));
Spencer Lowa5b06b02015-07-22 16:17:07 -0700327 if (NULL == ret->interface_name) {
Spencer Lowbb290002015-11-12 20:13:21 -0800328 D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno));
Spencer Lowa5b06b02015-07-22 16:17:07 -0700329 goto fail;
330 }
331
332 // Now save the name
333 if (!AdbGetInterfaceName(ret->adb_interface,
334 ret->interface_name,
335 &name_len,
Spencer Lowbb290002015-11-12 20:13:21 -0800336 false)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700337 D("AdbGetInterfaceName failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700338 SystemErrorCodeToString(GetLastError()).c_str());
339 goto fail;
340 }
341
342 // We're done at this point
343 return ret;
344
345fail:
346 if (NULL != ret) {
347 usb_cleanup_handle(ret);
348 free(ret);
349 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350
351 return NULL;
352}
353
354int usb_write(usb_handle* handle, const void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800355 unsigned long time_out = 5000;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 unsigned long written = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700357 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700359 D("usb_write %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700360 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700361 D("usb_write was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700362 err = EINVAL;
363 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 }
365
Spencer Lowa5b06b02015-07-22 16:17:07 -0700366 // Perform write
367 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
368 (void*)data,
369 (unsigned long)len,
370 &written,
371 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700372 D("AdbWriteEndpointSync failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700373 SystemErrorCodeToString(GetLastError()).c_str());
374 err = EIO;
375 goto fail;
376 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377
Spencer Lowa5b06b02015-07-22 16:17:07 -0700378 // Make sure that we've written what we were asked to write
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700379 D("usb_write got: %ld, expected: %d", written, len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700380 if (written != (unsigned long)len) {
381 // If this occurs, this code should be changed to repeatedly call
382 // AdbWriteEndpointSync() until all bytes are written.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700383 D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700384 len, written);
385 err = EIO;
386 goto fail;
387 }
388
389 if (handle->zero_mask && (len & handle->zero_mask) == 0) {
390 // Send a zero length packet
391 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
392 (void*)data,
393 0,
394 &written,
395 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700396 D("AdbWriteEndpointSync of zero length packet failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700397 SystemErrorCodeToString(GetLastError()).c_str());
398 err = EIO;
399 goto fail;
400 }
401 }
402
403 return 0;
404
405fail:
406 // Any failure should cause us to kick the device instead of leaving it a
407 // zombie state with potential to hang.
408 if (NULL != handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700409 D("Kicking device due to error in usb_write");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700410 usb_kick(handle);
411 }
412
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700413 D("usb_write failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700414 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 return -1;
416}
417
418int usb_read(usb_handle *handle, void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800419 unsigned long time_out = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 unsigned long read = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700421 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700423 D("usb_read %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700424 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700425 D("usb_read was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700426 err = EINVAL;
427 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 }
429
Spencer Lowa5b06b02015-07-22 16:17:07 -0700430 while (len > 0) {
431 if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
432 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700433 D("AdbReadEndpointSync failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700434 SystemErrorCodeToString(GetLastError()).c_str());
435 err = EIO;
436 goto fail;
437 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700438 D("usb_read got: %ld, expected: %d", read, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439
Spencer Lowa5b06b02015-07-22 16:17:07 -0700440 data = (char *)data + read;
441 len -= read;
442 }
443
444 return 0;
445
446fail:
447 // Any failure should cause us to kick the device instead of leaving it a
448 // zombie state with potential to hang.
449 if (NULL != handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700450 D("Kicking device due to error in usb_read");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700451 usb_kick(handle);
452 }
453
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700454 D("usb_read failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700455 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 return -1;
457}
458
Spencer Lowa5b06b02015-07-22 16:17:07 -0700459// Wrapper around AdbCloseHandle() that logs diagnostics.
460static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
461 if (!AdbCloseHandle(adb_handle)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700462 D("AdbCloseHandle(%p) failed: %s", adb_handle,
Spencer Lowa5b06b02015-07-22 16:17:07 -0700463 SystemErrorCodeToString(GetLastError()).c_str());
464 }
465}
466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467void usb_cleanup_handle(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700468 D("usb_cleanup_handle");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 if (NULL != handle) {
470 if (NULL != handle->interface_name)
471 free(handle->interface_name);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700472 // AdbCloseHandle(pipe) will break any threads out of pending IO calls and
473 // wait until the pipe no longer uses the interface. Then we can
474 // AdbCloseHandle() the interface.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 if (NULL != handle->adb_write_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700476 _adb_close_handle(handle->adb_write_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 if (NULL != handle->adb_read_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700478 _adb_close_handle(handle->adb_read_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 if (NULL != handle->adb_interface)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700480 _adb_close_handle(handle->adb_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481
482 handle->interface_name = NULL;
483 handle->adb_write_pipe = NULL;
484 handle->adb_read_pipe = NULL;
485 handle->adb_interface = NULL;
486 }
487}
488
Spencer Lowa5b06b02015-07-22 16:17:07 -0700489static void usb_kick_locked(usb_handle* handle) {
490 // The reason the lock must be acquired before calling this function is in
491 // case multiple threads are trying to kick the same device at the same time.
492 usb_cleanup_handle(handle);
493}
494
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495void usb_kick(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700496 D("usb_kick");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 if (NULL != handle) {
498 adb_mutex_lock(&usb_lock);
499
Spencer Lowa5b06b02015-07-22 16:17:07 -0700500 usb_kick_locked(handle);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501
502 adb_mutex_unlock(&usb_lock);
503 } else {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700504 errno = EINVAL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 }
506}
507
508int usb_close(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700509 D("usb_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510
511 if (NULL != handle) {
512 // Remove handle from the list
513 adb_mutex_lock(&usb_lock);
514
515 if ((handle->next != handle) && (handle->prev != handle)) {
516 handle->next->prev = handle->prev;
517 handle->prev->next = handle->next;
518 handle->prev = handle;
519 handle->next = handle;
520 }
521
522 adb_mutex_unlock(&usb_lock);
523
524 // Cleanup handle
525 usb_cleanup_handle(handle);
526 free(handle);
527 }
528
529 return 0;
530}
531
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532int recognized_device(usb_handle* handle) {
533 if (NULL == handle)
534 return 0;
535
536 // Check vendor and product id first
537 USB_DEVICE_DESCRIPTOR device_desc;
538
539 if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
540 &device_desc)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700541 D("AdbGetUsbDeviceDescriptor failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700542 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 return 0;
544 }
545
546 // Then check interface properties
547 USB_INTERFACE_DESCRIPTOR interf_desc;
548
549 if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
550 &interf_desc)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700551 D("AdbGetUsbInterfaceDescriptor failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700552 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 return 0;
554 }
555
556 // Must have two endpoints
557 if (2 != interf_desc.bNumEndpoints) {
558 return 0;
559 }
560
561 if (is_adb_interface(device_desc.idVendor, device_desc.idProduct,
562 interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) {
563
564 if(interf_desc.bInterfaceProtocol == 0x01) {
565 AdbEndpointInformation endpoint_info;
566 // assuming zero is a valid bulk endpoint ID
567 if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
568 handle->zero_mask = endpoint_info.max_packet_size - 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700569 D("device zero_mask: 0x%x", handle->zero_mask);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700570 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700571 D("AdbGetEndpointInformation failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700572 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573 }
574 }
575
576 return 1;
577 }
578
579 return 0;
580}
581
582void find_devices() {
Spencer Lowbb290002015-11-12 20:13:21 -0800583 usb_handle* handle = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584 char entry_buffer[2048];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585 AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
586 unsigned long entry_buffer_size = sizeof(entry_buffer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587
588 // Enumerate all present and active interfaces.
589 ADBAPIHANDLE enum_handle =
590 AdbEnumInterfaces(usb_class_id, true, true, true);
591
Spencer Lowa5b06b02015-07-22 16:17:07 -0700592 if (NULL == enum_handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700593 D("AdbEnumInterfaces failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700594 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 return;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700596 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597
598 while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 // Lets see if we already have this device in the list
Spencer Lowbb290002015-11-12 20:13:21 -0800600 if (!known_device(next_interface->device_name)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 // This seems to be a new device. Open it!
Spencer Lowbb290002015-11-12 20:13:21 -0800602 handle = do_usb_open(next_interface->device_name);
603 if (NULL != handle) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 // Lets see if this interface (device) belongs to us
605 if (recognized_device(handle)) {
Spencer Lowbb290002015-11-12 20:13:21 -0800606 D("adding a new device %ls", next_interface->device_name);
607
608 // We don't request a wchar_t string from AdbGetSerialNumber() because of a bug in
609 // adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString, bytes_written) where the
610 // last parameter should be (str_len * sizeof(wchar_t)). The bug reads 2 bytes past the
611 // end of a stack buffer in the best case, and in the unlikely case of a long serial
612 // number, it will read 2 bytes past the end of a heap allocation. This doesn't affect the
613 // resulting string, but we should avoid the bad reads in the first place.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614 char serial_number[512];
615 unsigned long serial_number_len = sizeof(serial_number);
616 if (AdbGetSerialNumber(handle->adb_interface,
617 serial_number,
618 &serial_number_len,
619 true)) {
620 // Lets make sure that we don't duplicate this device
621 if (register_new_device(handle)) {
Scott Andersone109d262012-04-20 11:21:14 -0700622 register_usb_transport(handle, serial_number, NULL, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623 } else {
Spencer Lowbb290002015-11-12 20:13:21 -0800624 D("register_new_device failed for %ls", next_interface->device_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800625 usb_cleanup_handle(handle);
626 free(handle);
627 }
628 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700629 D("cannot get serial number: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700630 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 usb_cleanup_handle(handle);
632 free(handle);
633 }
634 } else {
635 usb_cleanup_handle(handle);
636 free(handle);
637 }
638 }
639 }
640
641 entry_buffer_size = sizeof(entry_buffer);
642 }
643
Spencer Lowa5b06b02015-07-22 16:17:07 -0700644 if (GetLastError() != ERROR_NO_MORE_ITEMS) {
645 // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700646 D("AdbNextInterface failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700647 SystemErrorCodeToString(GetLastError()).c_str());
648 }
649
650 _adb_close_handle(enum_handle);
651}
652
653static void kick_devices() {
654 // Need to acquire lock to safely walk the list which might be modified
655 // by another thread.
656 adb_mutex_lock(&usb_lock);
657 for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) {
658 usb_kick_locked(usb);
659 }
660 adb_mutex_unlock(&usb_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661}