blob: cf78d80bcd39ec40d5f2993ef36bcc41e9182bad [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21
22#include <sys/ioctl.h>
23#include <sys/types.h>
24#include <dirent.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <ctype.h>
28
29#include <linux/usbdevice_fs.h>
30#include <linux/version.h>
31#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
32#include <linux/usb/ch9.h>
33#else
34#include <linux/usb_ch9.h>
35#endif
36#include <asm/byteorder.h>
37
38#include "sysdeps.h"
39
40#define TRACE_TAG TRACE_USB
41#include "adb.h"
42
43
44/* usb scan debugging is waaaay too verbose */
45#define DBGX(x...)
46
47static adb_mutex_t usb_lock = ADB_MUTEX_INITIALIZER;
48
49struct usb_handle
50{
51 usb_handle *prev;
52 usb_handle *next;
53
54 char fname[64];
55 int desc;
56 unsigned char ep_in;
57 unsigned char ep_out;
58
59 unsigned zero_mask;
Mike Lockwood0927bf92009-08-08 12:37:44 -040060 unsigned writeable;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
62 struct usbdevfs_urb urb_in;
63 struct usbdevfs_urb urb_out;
64
65 int urb_in_busy;
66 int urb_out_busy;
67 int dead;
68
69 adb_cond_t notify;
70 adb_mutex_t lock;
71
72 // for garbage collecting disconnected devices
73 int mark;
74
75 // ID of thread currently in REAPURB
76 pthread_t reaper_thread;
77};
78
79static usb_handle handle_list = {
80 .prev = &handle_list,
81 .next = &handle_list,
82};
83
84static int known_device(const char *dev_name)
85{
86 usb_handle *usb;
87
88 adb_mutex_lock(&usb_lock);
89 for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
90 if(!strcmp(usb->fname, dev_name)) {
91 // set mark flag to indicate this device is still alive
92 usb->mark = 1;
93 adb_mutex_unlock(&usb_lock);
94 return 1;
95 }
96 }
97 adb_mutex_unlock(&usb_lock);
98 return 0;
99}
100
101static void kick_disconnected_devices()
102{
103 usb_handle *usb;
104
105 adb_mutex_lock(&usb_lock);
106 // kick any devices in the device list that were not found in the device scan
107 for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
108 if (usb->mark == 0) {
109 usb_kick(usb);
110 } else {
111 usb->mark = 0;
112 }
113 }
114 adb_mutex_unlock(&usb_lock);
115
116}
117
118static void register_device(const char *dev_name, unsigned char ep_in, unsigned char ep_out,
Mike Lockwood0927bf92009-08-08 12:37:44 -0400119 int ifc, int serial_index, unsigned zero_mask);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120
121static inline int badname(const char *name)
122{
123 while(*name) {
124 if(!isdigit(*name++)) return 1;
125 }
126 return 0;
127}
128
Mike Lockwood0927bf92009-08-08 12:37:44 -0400129static void find_usb_device(const char *base,
130 void (*register_device_callback)
131 (const char *, unsigned char, unsigned char, int, int, unsigned))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132{
133 char busname[32], devname[32];
134 unsigned char local_ep_in, local_ep_out;
135 DIR *busdir , *devdir ;
136 struct dirent *de;
137 int fd ;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138
139 busdir = opendir(base);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400140 if(busdir == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
142 while((de = readdir(busdir)) != 0) {
143 if(badname(de->d_name)) continue;
144
145 snprintf(busname, sizeof busname, "%s/%s", base, de->d_name);
146 devdir = opendir(busname);
147 if(devdir == 0) continue;
148
149// DBGX("[ scanning %s ]\n", busname);
150 while((de = readdir(devdir))) {
151 unsigned char devdesc[256];
152 unsigned char* bufptr = devdesc;
153 struct usb_device_descriptor* device;
154 struct usb_config_descriptor* config;
155 struct usb_interface_descriptor* interface;
156 struct usb_endpoint_descriptor *ep1, *ep2;
157 unsigned zero_mask = 0;
158 unsigned vid, pid;
159 int i, interfaces;
160 size_t desclength;
161
162 if(badname(de->d_name)) continue;
163 snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
164
165 if(known_device(devname)) {
166 DBGX("skipping %s\n", devname);
167 continue;
168 }
169
170// DBGX("[ scanning %s ]\n", devname);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400171 if((fd = unix_open(devname, O_RDONLY)) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 continue;
173 }
174
175 desclength = adb_read(fd, devdesc, sizeof(devdesc));
176
177 // should have device and configuration descriptors, and atleast two endpoints
178 if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
179 D("desclength %d is too small\n", desclength);
180 adb_close(fd);
181 continue;
182 }
183
184 device = (struct usb_device_descriptor*)bufptr;
185 bufptr += USB_DT_DEVICE_SIZE;
186
187 if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
188 adb_close(fd);
189 continue;
190 }
191
192 vid = __le16_to_cpu(device->idVendor);
193 pid = __le16_to_cpu(device->idProduct);
194 pid = devdesc[10] | (devdesc[11] << 8);
195 DBGX("[ %s is V:%04x P:%04x ]\n", devname, vid, pid);
196
197 // should have config descriptor next
198 config = (struct usb_config_descriptor *)bufptr;
199 bufptr += USB_DT_CONFIG_SIZE;
200 if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
201 D("usb_config_descriptor not found\n");
202 adb_close(fd);
203 continue;
204 }
205
206 // loop through all the interfaces and look for the ADB interface
207 interfaces = config->bNumInterfaces;
208 for (i = 0; i < interfaces; i++) {
209 if (bufptr + USB_DT_ENDPOINT_SIZE > devdesc + desclength)
210 break;
211
212 interface = (struct usb_interface_descriptor *)bufptr;
213 bufptr += USB_DT_INTERFACE_SIZE;
214 if (interface->bLength != USB_DT_INTERFACE_SIZE ||
215 interface->bDescriptorType != USB_DT_INTERFACE) {
216 D("usb_interface_descriptor not found\n");
217 break;
218 }
219
220 DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
221 "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
222 interface->bInterfaceClass, interface->bInterfaceSubClass,
223 interface->bInterfaceProtocol, interface->bNumEndpoints);
224
225 if (interface->bNumEndpoints == 2 &&
226 is_adb_interface(vid, pid, interface->bInterfaceClass,
227 interface->bInterfaceSubClass, interface->bInterfaceProtocol)) {
228
229 DBGX("looking for bulk endpoints\n");
230 // looks like ADB...
231 ep1 = (struct usb_endpoint_descriptor *)bufptr;
232 bufptr += USB_DT_ENDPOINT_SIZE;
233 ep2 = (struct usb_endpoint_descriptor *)bufptr;
234 bufptr += USB_DT_ENDPOINT_SIZE;
235
236 if (bufptr > devdesc + desclength ||
237 ep1->bLength != USB_DT_ENDPOINT_SIZE ||
238 ep1->bDescriptorType != USB_DT_ENDPOINT ||
239 ep2->bLength != USB_DT_ENDPOINT_SIZE ||
240 ep2->bDescriptorType != USB_DT_ENDPOINT) {
241 D("endpoints not found\n");
242 break;
243 }
244
245 // both endpoints should be bulk
246 if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
247 ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
248 D("bulk endpoints not found\n");
249 continue;
250 }
251
252 /* aproto 01 needs 0 termination */
253 if(interface->bInterfaceProtocol == 0x01) {
254 zero_mask = ep1->wMaxPacketSize - 1;
255 }
256
257 // we have a match. now we just need to figure out which is in and which is out.
258 if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
259 local_ep_in = ep1->bEndpointAddress;
260 local_ep_out = ep2->bEndpointAddress;
261 } else {
262 local_ep_in = ep2->bEndpointAddress;
263 local_ep_out = ep1->bEndpointAddress;
264 }
265
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 register_device_callback(devname, local_ep_in, local_ep_out,
Mike Lockwood0927bf92009-08-08 12:37:44 -0400267 interface->bInterfaceNumber, device->iSerialNumber, zero_mask);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 break;
270 } else {
Xavier Ducrohetde6f62a2009-07-24 14:14:56 -0700271 // seek next interface descriptor
272 if (i < interfaces - 1) {
273 while (bufptr[1] != USB_DT_INTERFACE) {
274 bufptr += bufptr[0];
275 }
276 }
277 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 } // end of for
279
280 adb_close(fd);
281 } // end of devdir while
282 closedir(devdir);
283 } //end of busdir while
284 closedir(busdir);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285}
286
287void usb_cleanup()
288{
289}
290
291static int usb_bulk_write(usb_handle *h, const void *data, int len)
292{
293 struct usbdevfs_urb *urb = &h->urb_out;
294 int res;
295
296 memset(urb, 0, sizeof(*urb));
297 urb->type = USBDEVFS_URB_TYPE_BULK;
298 urb->endpoint = h->ep_out;
299 urb->status = -1;
300 urb->buffer = (void*) data;
301 urb->buffer_length = len;
302
303 D("++ write ++\n");
304
305 adb_mutex_lock(&h->lock);
306 if(h->dead) {
307 res = -1;
308 goto fail;
309 }
310 do {
311 res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
312 } while((res < 0) && (errno == EINTR));
313
314 if(res < 0) {
315 goto fail;
316 }
317
318 res = -1;
319 h->urb_out_busy = 1;
320 for(;;) {
321 adb_cond_wait(&h->notify, &h->lock);
322 if(h->dead) {
323 break;
324 }
325 if(h->urb_out_busy == 0) {
326 if(urb->status == 0) {
327 res = urb->actual_length;
328 }
329 break;
330 }
331 }
332fail:
333 adb_mutex_unlock(&h->lock);
334 D("-- write --\n");
335 return res;
336}
337
338static int usb_bulk_read(usb_handle *h, void *data, int len)
339{
340 struct usbdevfs_urb *urb = &h->urb_in;
341 struct usbdevfs_urb *out = NULL;
342 int res;
343
344 memset(urb, 0, sizeof(*urb));
345 urb->type = USBDEVFS_URB_TYPE_BULK;
346 urb->endpoint = h->ep_in;
347 urb->status = -1;
348 urb->buffer = data;
349 urb->buffer_length = len;
350
351
352 adb_mutex_lock(&h->lock);
353 if(h->dead) {
354 res = -1;
355 goto fail;
356 }
357 do {
358 res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
359 } while((res < 0) && (errno == EINTR));
360
361 if(res < 0) {
362 goto fail;
363 }
364
365 h->urb_in_busy = 1;
366 for(;;) {
367 D("[ reap urb - wait ]\n");
368 h->reaper_thread = pthread_self();
369 adb_mutex_unlock(&h->lock);
370 res = ioctl(h->desc, USBDEVFS_REAPURB, &out);
371 adb_mutex_lock(&h->lock);
372 h->reaper_thread = 0;
373 if(h->dead) {
374 res = -1;
375 break;
376 }
377 if(res < 0) {
378 if(errno == EINTR) {
379 continue;
380 }
381 D("[ reap urb - error ]\n");
382 break;
383 }
384 D("[ urb @%p status = %d, actual = %d ]\n",
385 out, out->status, out->actual_length);
386
387 if(out == &h->urb_in) {
388 D("[ reap urb - IN complete ]\n");
389 h->urb_in_busy = 0;
390 if(urb->status == 0) {
391 res = urb->actual_length;
392 } else {
393 res = -1;
394 }
395 break;
396 }
397 if(out == &h->urb_out) {
398 D("[ reap urb - OUT compelete ]\n");
399 h->urb_out_busy = 0;
400 adb_cond_broadcast(&h->notify);
401 }
402 }
403fail:
404 adb_mutex_unlock(&h->lock);
405 return res;
406}
407
408
409int usb_write(usb_handle *h, const void *_data, int len)
410{
411 unsigned char *data = (unsigned char*) _data;
412 int n;
413 int need_zero = 0;
414
415 if(h->zero_mask) {
416 /* if we need 0-markers and our transfer
417 ** is an even multiple of the packet size,
418 ** we make note of it
419 */
420 if(!(len & h->zero_mask)) {
421 need_zero = 1;
422 }
423 }
424
425 while(len > 0) {
426 int xfer = (len > 4096) ? 4096 : len;
427
428 n = usb_bulk_write(h, data, xfer);
429 if(n != xfer) {
430 D("ERROR: n = %d, errno = %d (%s)\n",
431 n, errno, strerror(errno));
432 return -1;
433 }
434
435 len -= xfer;
436 data += xfer;
437 }
438
439 if(need_zero){
440 n = usb_bulk_write(h, _data, 0);
441 return n;
442 }
443
444 return 0;
445}
446
447int usb_read(usb_handle *h, void *_data, int len)
448{
449 unsigned char *data = (unsigned char*) _data;
450 int n;
451
452 D("++ usb_read ++\n");
453 while(len > 0) {
454 int xfer = (len > 4096) ? 4096 : len;
455
456 D("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname);
457 n = usb_bulk_read(h, data, xfer);
458 D("[ usb read %d ] = %d, fname=%s\n", xfer, n, h->fname);
459 if(n != xfer) {
460 if((errno == ETIMEDOUT) && (h->desc != -1)) {
461 D("[ timeout ]\n");
462 if(n > 0){
463 data += n;
464 len -= n;
465 }
466 continue;
467 }
468 D("ERROR: n = %d, errno = %d (%s)\n",
469 n, errno, strerror(errno));
470 return -1;
471 }
472
473 len -= xfer;
474 data += xfer;
475 }
476
477 D("-- usb_read --\n");
478 return 0;
479}
480
481void usb_kick(usb_handle *h)
482{
483 D("[ kicking %p (fd = %d) ]\n", h, h->desc);
484 adb_mutex_lock(&h->lock);
485 if(h->dead == 0) {
486 h->dead = 1;
487
Mike Lockwood0927bf92009-08-08 12:37:44 -0400488 if (h->writeable) {
489 /* HACK ALERT!
490 ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
491 ** This is a workaround for that problem.
492 */
493 if (h->reaper_thread) {
494 pthread_kill(h->reaper_thread, SIGALRM);
495 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496
Mike Lockwood0927bf92009-08-08 12:37:44 -0400497 /* cancel any pending transactions
498 ** these will quietly fail if the txns are not active,
499 ** but this ensures that a reader blocked on REAPURB
500 ** will get unblocked
501 */
502 ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_in);
503 ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_out);
504 h->urb_in.status = -ENODEV;
505 h->urb_out.status = -ENODEV;
506 h->urb_in_busy = 0;
507 h->urb_out_busy = 0;
508 adb_cond_broadcast(&h->notify);
509 } else {
510 unregister_usb_transport(h);
511 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 }
513 adb_mutex_unlock(&h->lock);
514}
515
516int usb_close(usb_handle *h)
517{
518 D("[ usb close ... ]\n");
519 adb_mutex_lock(&usb_lock);
520 h->next->prev = h->prev;
521 h->prev->next = h->next;
522 h->prev = 0;
523 h->next = 0;
524
525 adb_close(h->desc);
526 D("[ usb closed %p (fd = %d) ]\n", h, h->desc);
527 adb_mutex_unlock(&usb_lock);
528
529 free(h);
530 return 0;
531}
532
533static void register_device(const char *dev_name,
534 unsigned char ep_in, unsigned char ep_out,
Mike Lockwood0927bf92009-08-08 12:37:44 -0400535 int interface, int serial_index, unsigned zero_mask)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536{
537 usb_handle* usb = 0;
538 int n = 0;
Mike Lockwood0927bf92009-08-08 12:37:44 -0400539 char serial[256];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540
541 /* Since Linux will not reassign the device ID (and dev_name)
542 ** as long as the device is open, we can add to the list here
543 ** once we open it and remove from the list when we're finally
544 ** closed and everything will work out fine.
545 **
546 ** If we have a usb_handle on the list 'o handles with a matching
547 ** name, we have no further work to do.
548 */
549 adb_mutex_lock(&usb_lock);
550 for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
551 if(!strcmp(usb->fname, dev_name)) {
552 adb_mutex_unlock(&usb_lock);
553 return;
554 }
555 }
556 adb_mutex_unlock(&usb_lock);
557
558 D("[ usb located new device %s (%d/%d/%d) ]\n",
559 dev_name, ep_in, ep_out, interface);
560 usb = calloc(1, sizeof(usb_handle));
561 strcpy(usb->fname, dev_name);
562 usb->ep_in = ep_in;
563 usb->ep_out = ep_out;
564 usb->zero_mask = zero_mask;
Mike Lockwood0927bf92009-08-08 12:37:44 -0400565 usb->writeable = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566
567 adb_cond_init(&usb->notify, 0);
568 adb_mutex_init(&usb->lock, 0);
569 /* initialize mark to 1 so we don't get garbage collected after the device scan */
570 usb->mark = 1;
571 usb->reaper_thread = 0;
572
573 usb->desc = unix_open(usb->fname, O_RDWR);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400574 if(usb->desc < 0) {
575 /* if we fail, see if have read-only access */
576 usb->desc = unix_open(usb->fname, O_RDONLY);
577 if(usb->desc < 0) goto fail;
578 usb->writeable = 0;
579 D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc);
580 } else {
581 D("[ usb open %s fd = %d]\n", usb->fname, usb->desc);
582 n = ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface);
583 if(n != 0) goto fail;
584 }
585
586 /* read the device's serial number */
587 serial[0] = 0;
588 memset(serial, 0, sizeof(serial));
589 if (serial_index) {
590 struct usbdevfs_ctrltransfer ctrl;
591 __u16 buffer[128];
592 __u16 languages[128];
593 int i, result;
594 int languageCount = 0;
595
596 memset(languages, 0, sizeof(languages));
597 memset(&ctrl, 0, sizeof(ctrl));
598
599 // read list of supported languages
600 ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
601 ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
602 ctrl.wValue = (USB_DT_STRING << 8) | 0;
603 ctrl.wIndex = 0;
604 ctrl.wLength = sizeof(languages);
605 ctrl.data = languages;
606
607 result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
608 if (result > 0)
609 languageCount = (result - 2) / 2;
610
611 for (i = 1; i <= languageCount; i++) {
612 memset(buffer, 0, sizeof(buffer));
613 memset(&ctrl, 0, sizeof(ctrl));
614
615 ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
616 ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
617 ctrl.wValue = (USB_DT_STRING << 8) | serial_index;
618 ctrl.wIndex = languages[i];
619 ctrl.wLength = sizeof(buffer);
620 ctrl.data = buffer;
621
622 result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
623 if (result > 0) {
624 int i;
625 // skip first word, and copy the rest to the serial string, changing shorts to bytes.
626 result /= 2;
627 for (i = 1; i < result; i++)
628 serial[i - 1] = buffer[i];
629 serial[i - 1] = 0;
630 break;
631 }
632 }
633 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634
635 /* add to the end of the active handles */
636 adb_mutex_lock(&usb_lock);
637 usb->next = &handle_list;
638 usb->prev = handle_list.prev;
639 usb->prev->next = usb;
640 usb->next->prev = usb;
641 adb_mutex_unlock(&usb_lock);
642
Mike Lockwood0927bf92009-08-08 12:37:44 -0400643 register_usb_transport(usb, serial, usb->writeable);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644 return;
645
646fail:
647 D("[ usb open %s error=%d, err_str = %s]\n",
648 usb->fname, errno, strerror(errno));
649 if(usb->desc >= 0) {
650 adb_close(usb->desc);
651 }
652 free(usb);
653}
654
655void* device_poll_thread(void* unused)
656{
657 D("Created device thread\n");
658 for(;;) {
659 /* XXX use inotify */
660 find_usb_device("/dev/bus/usb", register_device);
661 kick_disconnected_devices();
662 sleep(1);
663 }
664 return NULL;
665}
666
667static void sigalrm_handler(int signo)
668{
669 // don't need to do anything here
670}
671
672void usb_init()
673{
674 adb_thread_t tid;
675 struct sigaction actions;
676
677 memset(&actions, 0, sizeof(actions));
678 sigemptyset(&actions.sa_mask);
679 actions.sa_flags = 0;
680 actions.sa_handler = sigalrm_handler;
681 sigaction(SIGALRM,& actions, NULL);
682
683 if(adb_thread_create(&tid, device_poll_thread, NULL)){
684 fatal_errno("cannot create input thread");
685 }
686}
687