blob: d811b2446d868a331b86935e4c78ba654423b373 [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
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
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.
77int 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.
81int known_device_locked(const char* dev_name);
82
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121int 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
138int 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
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,
319 true);
320 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
326 ret->interface_name = (char*)malloc(name_len);
327 if (NULL == ret->interface_name) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700328 D("Could not allocate %lu bytes for interface_name: %s", name_len,
Spencer Lowa5b06b02015-07-22 16:17:07 -0700329 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)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700338 D("AdbGetInterfaceName failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700339 SystemErrorCodeToString(GetLastError()).c_str());
340 goto fail;
341 }
342
343 // We're done at this point
344 return ret;
345
346fail:
347 if (NULL != ret) {
348 usb_cleanup_handle(ret);
349 free(ret);
350 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351
352 return NULL;
353}
354
355int usb_write(usb_handle* handle, const void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800356 unsigned long time_out = 5000;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 unsigned long written = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700358 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700360 D("usb_write %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700361 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700362 D("usb_write was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700363 err = EINVAL;
364 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 }
366
Spencer Lowa5b06b02015-07-22 16:17:07 -0700367 // Perform write
368 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
369 (void*)data,
370 (unsigned long)len,
371 &written,
372 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700373 D("AdbWriteEndpointSync failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700374 SystemErrorCodeToString(GetLastError()).c_str());
375 err = EIO;
376 goto fail;
377 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Spencer Lowa5b06b02015-07-22 16:17:07 -0700379 // Make sure that we've written what we were asked to write
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700380 D("usb_write got: %ld, expected: %d", written, len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700381 if (written != (unsigned long)len) {
382 // If this occurs, this code should be changed to repeatedly call
383 // AdbWriteEndpointSync() until all bytes are written.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700384 D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700385 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)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700397 D("AdbWriteEndpointSync of zero length packet failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700398 SystemErrorCodeToString(GetLastError()).c_str());
399 err = EIO;
400 goto fail;
401 }
402 }
403
404 return 0;
405
406fail:
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) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700410 D("Kicking device due to error in usb_write");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700411 usb_kick(handle);
412 }
413
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700414 D("usb_write failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700415 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 return -1;
417}
418
419int usb_read(usb_handle *handle, void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800420 unsigned long time_out = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 unsigned long read = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700422 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700424 D("usb_read %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700425 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700426 D("usb_read was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700427 err = EINVAL;
428 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 }
430
Spencer Lowa5b06b02015-07-22 16:17:07 -0700431 while (len > 0) {
432 if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
433 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700434 D("AdbReadEndpointSync failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700435 SystemErrorCodeToString(GetLastError()).c_str());
436 err = EIO;
437 goto fail;
438 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700439 D("usb_read got: %ld, expected: %d", read, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
Spencer Lowa5b06b02015-07-22 16:17:07 -0700441 data = (char *)data + read;
442 len -= read;
443 }
444
445 return 0;
446
447fail:
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) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700451 D("Kicking device due to error in usb_read");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700452 usb_kick(handle);
453 }
454
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700455 D("usb_read failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700456 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 return -1;
458}
459
Spencer Lowa5b06b02015-07-22 16:17:07 -0700460// Wrapper around AdbCloseHandle() that logs diagnostics.
461static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
462 if (!AdbCloseHandle(adb_handle)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700463 D("AdbCloseHandle(%p) failed: %s", adb_handle,
Spencer Lowa5b06b02015-07-22 16:17:07 -0700464 SystemErrorCodeToString(GetLastError()).c_str());
465 }
466}
467
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468void usb_cleanup_handle(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700469 D("usb_cleanup_handle");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 if (NULL != handle) {
471 if (NULL != handle->interface_name)
472 free(handle->interface_name);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700473 // 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 Projectdd7bc332009-03-03 19:32:55 -0800476 if (NULL != handle->adb_write_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700477 _adb_close_handle(handle->adb_write_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 if (NULL != handle->adb_read_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700479 _adb_close_handle(handle->adb_read_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 if (NULL != handle->adb_interface)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700481 _adb_close_handle(handle->adb_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482
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 Lowa5b06b02015-07-22 16:17:07 -0700490static 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 Projectdd7bc332009-03-03 19:32:55 -0800496void usb_kick(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700497 D("usb_kick");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 if (NULL != handle) {
499 adb_mutex_lock(&usb_lock);
500
Spencer Lowa5b06b02015-07-22 16:17:07 -0700501 usb_kick_locked(handle);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502
503 adb_mutex_unlock(&usb_lock);
504 } else {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700505 errno = EINVAL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 }
507}
508
509int usb_close(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700510 D("usb_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511
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 Projectdd7bc332009-03-03 19:32:55 -0800533int 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)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700542 D("AdbGetUsbDeviceDescriptor failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700543 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 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)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700552 D("AdbGetUsbInterfaceDescriptor failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700553 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 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;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700570 D("device zero_mask: 0x%x", handle->zero_mask);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700571 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700572 D("AdbGetEndpointInformation failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700573 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574 }
575 }
576
577 return 1;
578 }
579
580 return 0;
581}
582
583void find_devices() {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200584 usb_handle* handle = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585 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 Lowa5b06b02015-07-22 16:17:07 -0700595 if (NULL == enum_handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700596 D("AdbEnumInterfaces failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700597 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598 return;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700599 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600
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)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700620 D("adding a new device %s", interf_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621 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 Andersone109d262012-04-20 11:21:14 -0700629 register_usb_transport(handle, serial_number, NULL, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700631 D("register_new_device failed for %s", interf_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 usb_cleanup_handle(handle);
633 free(handle);
634 }
635 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700636 D("cannot get serial number: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700637 SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 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 Lowa5b06b02015-07-22 16:17:07 -0700651 if (GetLastError() != ERROR_NO_MORE_ITEMS) {
652 // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700653 D("AdbNextInterface failed: %s",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700654 SystemErrorCodeToString(GetLastError()).c_str());
655 }
656
657 _adb_close_handle(enum_handle);
658}
659
660static 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 Projectdd7bc332009-03-03 19:32:55 -0800668}