blob: 136453773376aaa347de756af06cc57f3be8b424 [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.
Josh Gao0cd3ae12016-09-21 12:37:10 -070022#include <windows.h>
23#include <usb100.h>
24#include <winerror.h>
25
Dan Albert76649012015-02-24 15:51:19 -080026#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <stdio.h>
Christopher Ferris67a7a4a2014-11-06 14:34:24 -080028#include <stdlib.h>
Josh Gao0cd3ae12016-09-21 12:37:10 -070029
30#include <mutex>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080031#include <thread>
Josh Gao0cd3ae12016-09-21 12:37:10 -070032
33#include <adb_api.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
David Pursell5f787ed2016-01-27 08:52:53 -080035#include <android-base/errors.h>
36
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080038#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
40/** Structure usb_handle describes our connection to the usb device via
41 AdbWinApi.dll. This structure is returned from usb_open() routine and
42 is expected in each subsequent call that is accessing the device.
Spencer Lowa5b06b02015-07-22 16:17:07 -070043
44 Most members are protected by usb_lock, except for adb_{read,write}_pipe which
45 rely on AdbWinApi.dll's handle validation and AdbCloseHandle(endpoint)'s
46 ability to break a thread out of pipe IO.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047*/
48struct usb_handle {
49 /// Previous entry in the list of opened usb handles
50 usb_handle *prev;
51
52 /// Next entry in the list of opened usb handles
53 usb_handle *next;
54
55 /// Handle to USB interface
56 ADBAPIHANDLE adb_interface;
57
58 /// Handle to USB read pipe (endpoint)
59 ADBAPIHANDLE adb_read_pipe;
60
61 /// Handle to USB write pipe (endpoint)
62 ADBAPIHANDLE adb_write_pipe;
63
64 /// Interface name
Spencer Lowbb290002015-11-12 20:13:21 -080065 wchar_t* interface_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
67 /// Mask for determining when to use zero length packets
68 unsigned zero_mask;
69};
70
71/// Class ID assigned to the device by androidusb.sys
72static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
73
74/// List of opened usb handles
75static usb_handle handle_list = {
76 .prev = &handle_list,
77 .next = &handle_list,
78};
79
80/// Locker for the list of opened usb handles
Josh Gao0cd3ae12016-09-21 12:37:10 -070081static std::mutex& usb_lock = *new std::mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
83/// Checks if there is opened usb handle in handle_list for this device.
Spencer Lowbb290002015-11-12 20:13:21 -080084int known_device(const wchar_t* dev_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085
86/// Checks if there is opened usb handle in handle_list for this device.
87/// usb_lock mutex must be held before calling this routine.
Spencer Lowbb290002015-11-12 20:13:21 -080088int known_device_locked(const wchar_t* dev_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
90/// Registers opened usb handle (adds it to handle_list).
91int register_new_device(usb_handle* handle);
92
93/// Checks if interface (device) matches certain criteria
94int recognized_device(usb_handle* handle);
95
96/// Enumerates present and available interfaces (devices), opens new ones and
97/// registers usb transport for them.
98void find_devices();
99
Spencer Lowa5b06b02015-07-22 16:17:07 -0700100/// Kicks all USB devices
101static void kick_devices();
102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103/// Entry point for thread that polls (every second) for new usb interfaces.
104/// This routine calls find_devices in infinite loop.
Josh Gaob5fea142016-02-12 14:31:15 -0800105static void device_poll_thread(void*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
107/// Initializes this module
108void usb_init();
109
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110/// Opens usb interface (device) by interface (device) name.
111usb_handle* do_usb_open(const wchar_t* interface_name);
112
113/// Writes data to the opened usb handle
114int usb_write(usb_handle* handle, const void* data, int len);
115
116/// Reads data using the opened usb handle
117int usb_read(usb_handle *handle, void* data, int len);
118
119/// Cleans up opened usb handle
120void usb_cleanup_handle(usb_handle* handle);
121
122/// Cleans up (but don't close) opened usb handle
123void usb_kick(usb_handle* handle);
124
125/// Closes opened usb handle
126int usb_close(usb_handle* handle);
127
Spencer Lowbb290002015-11-12 20:13:21 -0800128int known_device_locked(const wchar_t* dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 usb_handle* usb;
130
131 if (NULL != dev_name) {
132 // Iterate through the list looking for the name match.
133 for(usb = handle_list.next; usb != &handle_list; usb = usb->next) {
134 // In Windows names are not case sensetive!
135 if((NULL != usb->interface_name) &&
Spencer Lowbb290002015-11-12 20:13:21 -0800136 (0 == wcsicmp(usb->interface_name, dev_name))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 return 1;
138 }
139 }
140 }
141
142 return 0;
143}
144
Spencer Lowbb290002015-11-12 20:13:21 -0800145int known_device(const wchar_t* dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 int ret = 0;
147
148 if (NULL != dev_name) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700149 std::lock_guard<std::mutex> lock(usb_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 ret = known_device_locked(dev_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 }
152
153 return ret;
154}
155
156int register_new_device(usb_handle* handle) {
157 if (NULL == handle)
158 return 0;
159
Josh Gao0cd3ae12016-09-21 12:37:10 -0700160 std::lock_guard<std::mutex> lock(usb_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
162 // Check if device is already in the list
163 if (known_device_locked(handle->interface_name)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 return 0;
165 }
166
167 // Not in the list. Add this handle to the list.
168 handle->next = &handle_list;
169 handle->prev = handle_list.prev;
170 handle->prev->next = handle;
171 handle->next->prev = handle;
172
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 return 1;
174}
175
Josh Gaob5fea142016-02-12 14:31:15 -0800176void device_poll_thread(void*) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700177 adb_thread_setname("Device Poll");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700178 D("Created device thread");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800180 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 find_devices();
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800182 std::this_thread::sleep_for(std::chrono::seconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184}
185
Spencer Lowa5b06b02015-07-22 16:17:07 -0700186static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam,
187 LPARAM lParam) {
188 switch (uMsg) {
189 case WM_POWERBROADCAST:
190 switch (wParam) {
191 case PBT_APMRESUMEAUTOMATIC:
192 // Resuming from sleep or hibernation, so kick all existing USB devices
193 // and then allow the device_poll_thread to redetect USB devices from
194 // scratch. If we don't do this, existing USB devices will never respond
195 // to us because they'll be waiting for the connect/auth handshake.
196 D("Received (WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC) notification, "
197 "so kicking all USB devices\n");
198 kick_devices();
199 return TRUE;
200 }
201 }
202 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
203}
204
Josh Gaob5fea142016-02-12 14:31:15 -0800205static void _power_notification_thread(void*) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700206 // This uses a thread with its own window message pump to get power
207 // notifications. If adb runs from a non-interactive service account, this
208 // might not work (not sure). If that happens to not work, we could use
209 // heavyweight WMI APIs to get power notifications. But for the common case
210 // of a developer's interactive session, a window message pump is more
211 // appropriate.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700212 D("Created power notification thread");
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700213 adb_thread_setname("Power Notifier");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700214
215 // Window class names are process specific.
216 static const WCHAR kPowerNotificationWindowClassName[] =
217 L"PowerNotificationWindow";
218
219 // Get the HINSTANCE corresponding to the module that _power_window_proc
220 // is in (the main module).
221 const HINSTANCE instance = GetModuleHandleW(NULL);
222 if (!instance) {
223 // This is such a common API call that this should never fail.
224 fatal("GetModuleHandleW failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800225 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700226 }
227
228 WNDCLASSEXW wndclass;
229 memset(&wndclass, 0, sizeof(wndclass));
230 wndclass.cbSize = sizeof(wndclass);
231 wndclass.lpfnWndProc = _power_window_proc;
232 wndclass.hInstance = instance;
233 wndclass.lpszClassName = kPowerNotificationWindowClassName;
234 if (!RegisterClassExW(&wndclass)) {
235 fatal("RegisterClassExW failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800236 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700237 }
238
239 if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
240 L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0,
241 NULL, NULL, instance, NULL)) {
242 fatal("CreateWindowExW failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800243 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700244 }
245
246 MSG msg;
247 while (GetMessageW(&msg, NULL, 0, 0)) {
248 TranslateMessage(&msg);
249 DispatchMessageW(&msg);
250 }
251
252 // GetMessageW() will return false if a quit message is posted. We don't
253 // do that, but it might be possible for that to occur when logging off or
254 // shutting down. Not a big deal since the whole process will be going away
255 // soon anyway.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700256 D("Power notification thread exiting");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700257}
258
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259void usb_init() {
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700260 if (!adb_thread_create(device_poll_thread, nullptr)) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700261 fatal_errno("cannot create device poll thread");
262 }
263 if (!adb_thread_create(_power_notification_thread, nullptr)) {
264 fatal_errno("cannot create power notification thread");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 }
266}
267
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268usb_handle* do_usb_open(const wchar_t* interface_name) {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700269 unsigned long name_len = 0;
270
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 // Allocate our handle
Spencer Lowa5b06b02015-07-22 16:17:07 -0700272 usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
273 if (NULL == ret) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700274 D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle),
Spencer Lowa5b06b02015-07-22 16:17:07 -0700275 strerror(errno));
276 goto fail;
277 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278
279 // Set linkers back to the handle
280 ret->next = ret;
281 ret->prev = ret;
282
283 // Create interface.
284 ret->adb_interface = AdbCreateInterfaceByName(interface_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 if (NULL == ret->adb_interface) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700286 D("AdbCreateInterfaceByName failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800287 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700288 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 }
290
291 // Open read pipe (endpoint)
292 ret->adb_read_pipe =
293 AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
294 AdbOpenAccessTypeReadWrite,
295 AdbOpenSharingModeReadWrite);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700296 if (NULL == ret->adb_read_pipe) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700297 D("AdbOpenDefaultBulkReadEndpoint failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800298 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700299 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
301
Spencer Lowa5b06b02015-07-22 16:17:07 -0700302 // Open write pipe (endpoint)
303 ret->adb_write_pipe =
304 AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface,
305 AdbOpenAccessTypeReadWrite,
306 AdbOpenSharingModeReadWrite);
307 if (NULL == ret->adb_write_pipe) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700308 D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800309 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700310 goto fail;
311 }
312
313 // Save interface name
314 // First get expected name length
315 AdbGetInterfaceName(ret->adb_interface,
316 NULL,
317 &name_len,
Spencer Lowbb290002015-11-12 20:13:21 -0800318 false);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700319 if (0 == name_len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700320 D("AdbGetInterfaceName returned name length of zero: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800321 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700322 goto fail;
323 }
324
Spencer Lowbb290002015-11-12 20:13:21 -0800325 ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0]));
Spencer Lowa5b06b02015-07-22 16:17:07 -0700326 if (NULL == ret->interface_name) {
Spencer Lowbb290002015-11-12 20:13:21 -0800327 D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno));
Spencer Lowa5b06b02015-07-22 16:17:07 -0700328 goto fail;
329 }
330
331 // Now save the name
332 if (!AdbGetInterfaceName(ret->adb_interface,
333 ret->interface_name,
334 &name_len,
Spencer Lowbb290002015-11-12 20:13:21 -0800335 false)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700336 D("AdbGetInterfaceName failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800337 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700338 goto fail;
339 }
340
341 // We're done at this point
342 return ret;
343
344fail:
345 if (NULL != ret) {
346 usb_cleanup_handle(ret);
347 free(ret);
348 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
350 return NULL;
351}
352
353int usb_write(usb_handle* handle, const void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800354 unsigned long time_out = 5000;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 unsigned long written = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700356 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700358 D("usb_write %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700359 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700360 D("usb_write was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700361 err = EINVAL;
362 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 }
364
Spencer Lowa5b06b02015-07-22 16:17:07 -0700365 // Perform write
366 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
367 (void*)data,
368 (unsigned long)len,
369 &written,
370 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700371 D("AdbWriteEndpointSync failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800372 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700373 err = EIO;
374 goto fail;
375 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
Spencer Lowa5b06b02015-07-22 16:17:07 -0700377 // Make sure that we've written what we were asked to write
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700378 D("usb_write got: %ld, expected: %d", written, len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700379 if (written != (unsigned long)len) {
380 // If this occurs, this code should be changed to repeatedly call
381 // AdbWriteEndpointSync() until all bytes are written.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700382 D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld",
Spencer Lowa5b06b02015-07-22 16:17:07 -0700383 len, written);
384 err = EIO;
385 goto fail;
386 }
387
388 if (handle->zero_mask && (len & handle->zero_mask) == 0) {
389 // Send a zero length packet
390 if (!AdbWriteEndpointSync(handle->adb_write_pipe,
391 (void*)data,
392 0,
393 &written,
394 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700395 D("AdbWriteEndpointSync of zero length packet failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800396 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700397 err = EIO;
398 goto fail;
399 }
400 }
401
402 return 0;
403
404fail:
405 // Any failure should cause us to kick the device instead of leaving it a
406 // zombie state with potential to hang.
407 if (NULL != handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700408 D("Kicking device due to error in usb_write");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700409 usb_kick(handle);
410 }
411
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700412 D("usb_write failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700413 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 return -1;
415}
416
417int usb_read(usb_handle *handle, void* data, int len) {
Jack Ren1c4b7602011-08-12 18:39:04 +0800418 unsigned long time_out = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 unsigned long read = 0;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700420 int err = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700422 D("usb_read %d", len);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700423 if (NULL == handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700424 D("usb_read was passed NULL handle");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700425 err = EINVAL;
426 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 }
428
Spencer Lowa5b06b02015-07-22 16:17:07 -0700429 while (len > 0) {
430 if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
431 time_out)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700432 D("AdbReadEndpointSync failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800433 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700434 err = EIO;
435 goto fail;
436 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700437 D("usb_read got: %ld, expected: %d", read, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438
Spencer Lowa5b06b02015-07-22 16:17:07 -0700439 data = (char *)data + read;
440 len -= read;
441 }
442
443 return 0;
444
445fail:
446 // Any failure should cause us to kick the device instead of leaving it a
447 // zombie state with potential to hang.
448 if (NULL != handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700449 D("Kicking device due to error in usb_read");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700450 usb_kick(handle);
451 }
452
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700453 D("usb_read failed");
Spencer Lowa5b06b02015-07-22 16:17:07 -0700454 errno = err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 return -1;
456}
457
Spencer Lowa5b06b02015-07-22 16:17:07 -0700458// Wrapper around AdbCloseHandle() that logs diagnostics.
459static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
460 if (!AdbCloseHandle(adb_handle)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700461 D("AdbCloseHandle(%p) failed: %s", adb_handle,
David Pursell5f787ed2016-01-27 08:52:53 -0800462 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700463 }
464}
465
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466void usb_cleanup_handle(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700467 D("usb_cleanup_handle");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 if (NULL != handle) {
469 if (NULL != handle->interface_name)
470 free(handle->interface_name);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700471 // AdbCloseHandle(pipe) will break any threads out of pending IO calls and
472 // wait until the pipe no longer uses the interface. Then we can
473 // AdbCloseHandle() the interface.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 if (NULL != handle->adb_write_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700475 _adb_close_handle(handle->adb_write_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 if (NULL != handle->adb_read_pipe)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700477 _adb_close_handle(handle->adb_read_pipe);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 if (NULL != handle->adb_interface)
Spencer Lowa5b06b02015-07-22 16:17:07 -0700479 _adb_close_handle(handle->adb_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480
481 handle->interface_name = NULL;
482 handle->adb_write_pipe = NULL;
483 handle->adb_read_pipe = NULL;
484 handle->adb_interface = NULL;
485 }
486}
487
Spencer Lowa5b06b02015-07-22 16:17:07 -0700488static void usb_kick_locked(usb_handle* handle) {
489 // The reason the lock must be acquired before calling this function is in
490 // case multiple threads are trying to kick the same device at the same time.
491 usb_cleanup_handle(handle);
492}
493
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494void usb_kick(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700495 D("usb_kick");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 if (NULL != handle) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700497 std::lock_guard<std::mutex> lock(usb_lock);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700498 usb_kick_locked(handle);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 } else {
Spencer Lowa5b06b02015-07-22 16:17:07 -0700500 errno = EINVAL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 }
502}
503
504int usb_close(usb_handle* handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700505 D("usb_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506
507 if (NULL != handle) {
508 // Remove handle from the list
Josh Gao0cd3ae12016-09-21 12:37:10 -0700509 {
510 std::lock_guard<std::mutex> lock(usb_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511
Josh Gao0cd3ae12016-09-21 12:37:10 -0700512 if ((handle->next != handle) && (handle->prev != handle)) {
513 handle->next->prev = handle->prev;
514 handle->prev->next = handle->next;
515 handle->prev = handle;
516 handle->next = handle;
517 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 }
519
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 // Cleanup handle
521 usb_cleanup_handle(handle);
522 free(handle);
523 }
524
525 return 0;
526}
527
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528int recognized_device(usb_handle* handle) {
529 if (NULL == handle)
530 return 0;
531
532 // Check vendor and product id first
533 USB_DEVICE_DESCRIPTOR device_desc;
534
535 if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
536 &device_desc)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700537 D("AdbGetUsbDeviceDescriptor failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800538 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 return 0;
540 }
541
542 // Then check interface properties
543 USB_INTERFACE_DESCRIPTOR interf_desc;
544
545 if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
546 &interf_desc)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700547 D("AdbGetUsbInterfaceDescriptor failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800548 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 return 0;
550 }
551
552 // Must have two endpoints
553 if (2 != interf_desc.bNumEndpoints) {
554 return 0;
555 }
556
Josh Gao30186df2016-09-26 21:18:58 -0700557 if (is_adb_interface(interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass,
558 interf_desc.bInterfaceProtocol)) {
559 if (interf_desc.bInterfaceProtocol == 0x01) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560 AdbEndpointInformation endpoint_info;
561 // assuming zero is a valid bulk endpoint ID
562 if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
563 handle->zero_mask = endpoint_info.max_packet_size - 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700564 D("device zero_mask: 0x%x", handle->zero_mask);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700565 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700566 D("AdbGetEndpointInformation failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800567 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 }
569 }
570
571 return 1;
572 }
573
574 return 0;
575}
576
577void find_devices() {
Spencer Lowbb290002015-11-12 20:13:21 -0800578 usb_handle* handle = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579 char entry_buffer[2048];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800580 AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
581 unsigned long entry_buffer_size = sizeof(entry_buffer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582
583 // Enumerate all present and active interfaces.
584 ADBAPIHANDLE enum_handle =
585 AdbEnumInterfaces(usb_class_id, true, true, true);
586
Spencer Lowa5b06b02015-07-22 16:17:07 -0700587 if (NULL == enum_handle) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700588 D("AdbEnumInterfaces failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800589 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 return;
Spencer Lowa5b06b02015-07-22 16:17:07 -0700591 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592
593 while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594 // Lets see if we already have this device in the list
Spencer Lowbb290002015-11-12 20:13:21 -0800595 if (!known_device(next_interface->device_name)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 // This seems to be a new device. Open it!
Spencer Lowbb290002015-11-12 20:13:21 -0800597 handle = do_usb_open(next_interface->device_name);
598 if (NULL != handle) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 // Lets see if this interface (device) belongs to us
600 if (recognized_device(handle)) {
Spencer Lowbb290002015-11-12 20:13:21 -0800601 D("adding a new device %ls", next_interface->device_name);
602
603 // We don't request a wchar_t string from AdbGetSerialNumber() because of a bug in
604 // adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString, bytes_written) where the
605 // last parameter should be (str_len * sizeof(wchar_t)). The bug reads 2 bytes past the
606 // end of a stack buffer in the best case, and in the unlikely case of a long serial
607 // number, it will read 2 bytes past the end of a heap allocation. This doesn't affect the
608 // resulting string, but we should avoid the bad reads in the first place.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 char serial_number[512];
610 unsigned long serial_number_len = sizeof(serial_number);
611 if (AdbGetSerialNumber(handle->adb_interface,
612 serial_number,
613 &serial_number_len,
614 true)) {
615 // Lets make sure that we don't duplicate this device
616 if (register_new_device(handle)) {
Scott Andersone109d262012-04-20 11:21:14 -0700617 register_usb_transport(handle, serial_number, NULL, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 } else {
Spencer Lowbb290002015-11-12 20:13:21 -0800619 D("register_new_device failed for %ls", next_interface->device_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 usb_cleanup_handle(handle);
621 free(handle);
622 }
623 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700624 D("cannot get serial number: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800625 android::base::SystemErrorCodeToString(GetLastError()).c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626 usb_cleanup_handle(handle);
627 free(handle);
628 }
629 } else {
630 usb_cleanup_handle(handle);
631 free(handle);
632 }
633 }
634 }
635
636 entry_buffer_size = sizeof(entry_buffer);
637 }
638
Spencer Lowa5b06b02015-07-22 16:17:07 -0700639 if (GetLastError() != ERROR_NO_MORE_ITEMS) {
640 // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700641 D("AdbNextInterface failed: %s",
David Pursell5f787ed2016-01-27 08:52:53 -0800642 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Lowa5b06b02015-07-22 16:17:07 -0700643 }
644
645 _adb_close_handle(enum_handle);
646}
647
648static void kick_devices() {
649 // Need to acquire lock to safely walk the list which might be modified
650 // by another thread.
Josh Gao0cd3ae12016-09-21 12:37:10 -0700651 std::lock_guard<std::mutex> lock(usb_lock);
Spencer Lowa5b06b02015-07-22 16:17:07 -0700652 for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) {
653 usb_kick_locked(usb);
654 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655}