1 /*
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
17 #define TRACE_TAG TRACE_USB
18
19 #include "sysdeps.h"
20
21 #include <winsock2.h> // winsock.h *must* be included before windows.h.
22 #include <adb_api.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <usb100.h>
27 #include <windows.h>
28 #include <winerror.h>
29
30 #include "adb.h"
31 #include "transport.h"
32
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.
36 */
37 struct usb_handle {
38 /// Previous entry in the list of opened usb handles
39 usb_handle *prev;
40
41 /// Next entry in the list of opened usb handles
42 usb_handle *next;
43
44 /// Handle to USB interface
45 ADBAPIHANDLE adb_interface;
46
47 /// Handle to USB read pipe (endpoint)
48 ADBAPIHANDLE adb_read_pipe;
49
50 /// Handle to USB write pipe (endpoint)
51 ADBAPIHANDLE adb_write_pipe;
52
53 /// Interface name
54 char* interface_name;
55
56 /// Mask for determining when to use zero length packets
57 unsigned zero_mask;
58 };
59
60 /// Class ID assigned to the device by androidusb.sys
61 static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
62
63 /// List of opened usb handles
64 static usb_handle handle_list = {
65 .prev = &handle_list,
66 .next = &handle_list,
67 };
68
69 /// Locker for the list of opened usb handles
70 ADB_MUTEX_DEFINE( usb_lock );
71
72 /// Checks if there is opened usb handle in handle_list for this device.
73 int known_device(const char* dev_name);
74
75 /// Checks if there is opened usb handle in handle_list for this device.
76 /// usb_lock mutex must be held before calling this routine.
77 int known_device_locked(const char* dev_name);
78
79 /// Registers opened usb handle (adds it to handle_list).
80 int register_new_device(usb_handle* handle);
81
82 /// Checks if interface (device) matches certain criteria
83 int recognized_device(usb_handle* handle);
84
85 /// Enumerates present and available interfaces (devices), opens new ones and
86 /// registers usb transport for them.
87 void find_devices();
88
89 /// Entry point for thread that polls (every second) for new usb interfaces.
90 /// This routine calls find_devices in infinite loop.
91 void* device_poll_thread(void* unused);
92
93 /// Initializes this module
94 void usb_init();
95
96 /// Cleans up this module
97 void usb_cleanup();
98
99 /// Opens usb interface (device) by interface (device) name.
100 usb_handle* do_usb_open(const wchar_t* interface_name);
101
102 /// Writes data to the opened usb handle
103 int usb_write(usb_handle* handle, const void* data, int len);
104
105 /// Reads data using the opened usb handle
106 int usb_read(usb_handle *handle, void* data, int len);
107
108 /// Cleans up opened usb handle
109 void usb_cleanup_handle(usb_handle* handle);
110
111 /// Cleans up (but don't close) opened usb handle
112 void usb_kick(usb_handle* handle);
113
114 /// Closes opened usb handle
115 int usb_close(usb_handle* handle);
116
117 /// Gets interface (device) name for an opened usb handle
118 const char *usb_name(usb_handle* handle);
119
known_device_locked(const char * dev_name)120 int known_device_locked(const char* dev_name) {
121 usb_handle* usb;
122
123 if (NULL != dev_name) {
124 // Iterate through the list looking for the name match.
125 for(usb = handle_list.next; usb != &handle_list; usb = usb->next) {
126 // In Windows names are not case sensetive!
127 if((NULL != usb->interface_name) &&
128 (0 == stricmp(usb->interface_name, dev_name))) {
129 return 1;
130 }
131 }
132 }
133
134 return 0;
135 }
136
known_device(const char * dev_name)137 int known_device(const char* dev_name) {
138 int ret = 0;
139
140 if (NULL != dev_name) {
141 adb_mutex_lock(&usb_lock);
142 ret = known_device_locked(dev_name);
143 adb_mutex_unlock(&usb_lock);
144 }
145
146 return ret;
147 }
148
register_new_device(usb_handle * handle)149 int register_new_device(usb_handle* handle) {
150 if (NULL == handle)
151 return 0;
152
153 adb_mutex_lock(&usb_lock);
154
155 // Check if device is already in the list
156 if (known_device_locked(handle->interface_name)) {
157 adb_mutex_unlock(&usb_lock);
158 return 0;
159 }
160
161 // Not in the list. Add this handle to the list.
162 handle->next = &handle_list;
163 handle->prev = handle_list.prev;
164 handle->prev->next = handle;
165 handle->next->prev = handle;
166
167 adb_mutex_unlock(&usb_lock);
168
169 return 1;
170 }
171
device_poll_thread(void * unused)172 void* device_poll_thread(void* unused) {
173 D("Created device thread\n");
174
175 while(1) {
176 find_devices();
177 adb_sleep_ms(1000);
178 }
179
180 return NULL;
181 }
182
usb_init()183 void usb_init() {
184 adb_thread_t tid;
185
186 if(adb_thread_create(&tid, device_poll_thread, NULL)) {
187 fatal_errno("cannot create input thread");
188 }
189 }
190
usb_cleanup()191 void usb_cleanup() {
192 }
193
do_usb_open(const wchar_t * interface_name)194 usb_handle* do_usb_open(const wchar_t* interface_name) {
195 // Allocate our handle
196 usb_handle* ret = (usb_handle*)malloc(sizeof(usb_handle));
197 if (NULL == ret)
198 return NULL;
199
200 // Set linkers back to the handle
201 ret->next = ret;
202 ret->prev = ret;
203
204 // Create interface.
205 ret->adb_interface = AdbCreateInterfaceByName(interface_name);
206
207 if (NULL == ret->adb_interface) {
208 free(ret);
209 errno = GetLastError();
210 return NULL;
211 }
212
213 // Open read pipe (endpoint)
214 ret->adb_read_pipe =
215 AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
216 AdbOpenAccessTypeReadWrite,
217 AdbOpenSharingModeReadWrite);
218 if (NULL != ret->adb_read_pipe) {
219 // Open write pipe (endpoint)
220 ret->adb_write_pipe =
221 AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface,
222 AdbOpenAccessTypeReadWrite,
223 AdbOpenSharingModeReadWrite);
224 if (NULL != ret->adb_write_pipe) {
225 // Save interface name
226 unsigned long name_len = 0;
227
228 // First get expected name length
229 AdbGetInterfaceName(ret->adb_interface,
230 NULL,
231 &name_len,
232 true);
233 if (0 != name_len) {
234 ret->interface_name = (char*)malloc(name_len);
235
236 if (NULL != ret->interface_name) {
237 // Now save the name
238 if (AdbGetInterfaceName(ret->adb_interface,
239 ret->interface_name,
240 &name_len,
241 true)) {
242 // We're done at this point
243 return ret;
244 }
245 } else {
246 SetLastError(ERROR_OUTOFMEMORY);
247 }
248 }
249 }
250 }
251
252 // Something went wrong.
253 int saved_errno = GetLastError();
254 usb_cleanup_handle(ret);
255 free(ret);
256 SetLastError(saved_errno);
257
258 return NULL;
259 }
260
usb_write(usb_handle * handle,const void * data,int len)261 int usb_write(usb_handle* handle, const void* data, int len) {
262 unsigned long time_out = 5000;
263 unsigned long written = 0;
264 int ret;
265
266 D("usb_write %d\n", len);
267 if (NULL != handle) {
268 // Perform write
269 ret = AdbWriteEndpointSync(handle->adb_write_pipe,
270 (void*)data,
271 (unsigned long)len,
272 &written,
273 time_out);
274 int saved_errno = GetLastError();
275
276 if (ret) {
277 // Make sure that we've written what we were asked to write
278 D("usb_write got: %ld, expected: %d\n", written, len);
279 if (written == (unsigned long)len) {
280 if(handle->zero_mask && (len & handle->zero_mask) == 0) {
281 // Send a zero length packet
282 AdbWriteEndpointSync(handle->adb_write_pipe,
283 (void*)data,
284 0,
285 &written,
286 time_out);
287 }
288 return 0;
289 }
290 } else {
291 // assume ERROR_INVALID_HANDLE indicates we are disconnected
292 if (saved_errno == ERROR_INVALID_HANDLE)
293 usb_kick(handle);
294 }
295 errno = saved_errno;
296 } else {
297 D("usb_write NULL handle\n");
298 SetLastError(ERROR_INVALID_HANDLE);
299 }
300
301 D("usb_write failed: %d\n", errno);
302
303 return -1;
304 }
305
usb_read(usb_handle * handle,void * data,int len)306 int usb_read(usb_handle *handle, void* data, int len) {
307 unsigned long time_out = 0;
308 unsigned long read = 0;
309 int ret;
310
311 D("usb_read %d\n", len);
312 if (NULL != handle) {
313 while (len > 0) {
314 int xfer = (len > 4096) ? 4096 : len;
315
316 ret = AdbReadEndpointSync(handle->adb_read_pipe,
317 data,
318 (unsigned long)xfer,
319 &read,
320 time_out);
321 int saved_errno = GetLastError();
322 D("usb_write got: %ld, expected: %d, errno: %d\n", read, xfer, saved_errno);
323 if (ret) {
324 data = (char *)data + read;
325 len -= read;
326
327 if (len == 0)
328 return 0;
329 } else {
330 // assume ERROR_INVALID_HANDLE indicates we are disconnected
331 if (saved_errno == ERROR_INVALID_HANDLE)
332 usb_kick(handle);
333 break;
334 }
335 errno = saved_errno;
336 }
337 } else {
338 D("usb_read NULL handle\n");
339 SetLastError(ERROR_INVALID_HANDLE);
340 }
341
342 D("usb_read failed: %d\n", errno);
343
344 return -1;
345 }
346
usb_cleanup_handle(usb_handle * handle)347 void usb_cleanup_handle(usb_handle* handle) {
348 if (NULL != handle) {
349 if (NULL != handle->interface_name)
350 free(handle->interface_name);
351 if (NULL != handle->adb_write_pipe)
352 AdbCloseHandle(handle->adb_write_pipe);
353 if (NULL != handle->adb_read_pipe)
354 AdbCloseHandle(handle->adb_read_pipe);
355 if (NULL != handle->adb_interface)
356 AdbCloseHandle(handle->adb_interface);
357
358 handle->interface_name = NULL;
359 handle->adb_write_pipe = NULL;
360 handle->adb_read_pipe = NULL;
361 handle->adb_interface = NULL;
362 }
363 }
364
usb_kick(usb_handle * handle)365 void usb_kick(usb_handle* handle) {
366 if (NULL != handle) {
367 adb_mutex_lock(&usb_lock);
368
369 usb_cleanup_handle(handle);
370
371 adb_mutex_unlock(&usb_lock);
372 } else {
373 SetLastError(ERROR_INVALID_HANDLE);
374 errno = ERROR_INVALID_HANDLE;
375 }
376 }
377
usb_close(usb_handle * handle)378 int usb_close(usb_handle* handle) {
379 D("usb_close\n");
380
381 if (NULL != handle) {
382 // Remove handle from the list
383 adb_mutex_lock(&usb_lock);
384
385 if ((handle->next != handle) && (handle->prev != handle)) {
386 handle->next->prev = handle->prev;
387 handle->prev->next = handle->next;
388 handle->prev = handle;
389 handle->next = handle;
390 }
391
392 adb_mutex_unlock(&usb_lock);
393
394 // Cleanup handle
395 usb_cleanup_handle(handle);
396 free(handle);
397 }
398
399 return 0;
400 }
401
usb_name(usb_handle * handle)402 const char *usb_name(usb_handle* handle) {
403 if (NULL == handle) {
404 SetLastError(ERROR_INVALID_HANDLE);
405 errno = ERROR_INVALID_HANDLE;
406 return NULL;
407 }
408
409 return (const char*)handle->interface_name;
410 }
411
recognized_device(usb_handle * handle)412 int recognized_device(usb_handle* handle) {
413 if (NULL == handle)
414 return 0;
415
416 // Check vendor and product id first
417 USB_DEVICE_DESCRIPTOR device_desc;
418
419 if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
420 &device_desc)) {
421 return 0;
422 }
423
424 // Then check interface properties
425 USB_INTERFACE_DESCRIPTOR interf_desc;
426
427 if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
428 &interf_desc)) {
429 return 0;
430 }
431
432 // Must have two endpoints
433 if (2 != interf_desc.bNumEndpoints) {
434 return 0;
435 }
436
437 if (is_adb_interface(device_desc.idVendor, device_desc.idProduct,
438 interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) {
439
440 if(interf_desc.bInterfaceProtocol == 0x01) {
441 AdbEndpointInformation endpoint_info;
442 // assuming zero is a valid bulk endpoint ID
443 if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
444 handle->zero_mask = endpoint_info.max_packet_size - 1;
445 }
446 }
447
448 return 1;
449 }
450
451 return 0;
452 }
453
find_devices()454 void find_devices() {
455 usb_handle* handle = NULL;
456 char entry_buffer[2048];
457 char interf_name[2048];
458 AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
459 unsigned long entry_buffer_size = sizeof(entry_buffer);
460 char* copy_name;
461
462 // Enumerate all present and active interfaces.
463 ADBAPIHANDLE enum_handle =
464 AdbEnumInterfaces(usb_class_id, true, true, true);
465
466 if (NULL == enum_handle)
467 return;
468
469 while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
470 // TODO: FIXME - temp hack converting wchar_t into char.
471 // It would be better to change AdbNextInterface so it will return
472 // interface name as single char string.
473 const wchar_t* wchar_name = next_interface->device_name;
474 for(copy_name = interf_name;
475 L'\0' != *wchar_name;
476 wchar_name++, copy_name++) {
477 *copy_name = (char)(*wchar_name);
478 }
479 *copy_name = '\0';
480
481 // Lets see if we already have this device in the list
482 if (!known_device(interf_name)) {
483 // This seems to be a new device. Open it!
484 handle = do_usb_open(next_interface->device_name);
485 if (NULL != handle) {
486 // Lets see if this interface (device) belongs to us
487 if (recognized_device(handle)) {
488 D("adding a new device %s\n", interf_name);
489 char serial_number[512];
490 unsigned long serial_number_len = sizeof(serial_number);
491 if (AdbGetSerialNumber(handle->adb_interface,
492 serial_number,
493 &serial_number_len,
494 true)) {
495 // Lets make sure that we don't duplicate this device
496 if (register_new_device(handle)) {
497 register_usb_transport(handle, serial_number, NULL, 1);
498 } else {
499 D("register_new_device failed for %s\n", interf_name);
500 usb_cleanup_handle(handle);
501 free(handle);
502 }
503 } else {
504 D("cannot get serial number\n");
505 usb_cleanup_handle(handle);
506 free(handle);
507 }
508 } else {
509 usb_cleanup_handle(handle);
510 free(handle);
511 }
512 }
513 }
514
515 entry_buffer_size = sizeof(entry_buffer);
516 }
517
518 AdbCloseHandle(enum_handle);
519 }
520