The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | #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 | |
| 47 | static adb_mutex_t usb_lock = ADB_MUTEX_INITIALIZER; |
| 48 | |
| 49 | struct 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 Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 60 | unsigned writeable; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | |
| 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 | |
| 79 | static usb_handle handle_list = { |
| 80 | .prev = &handle_list, |
| 81 | .next = &handle_list, |
| 82 | }; |
| 83 | |
| 84 | static 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 | |
| 101 | static 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 | |
| 118 | static void register_device(const char *dev_name, unsigned char ep_in, unsigned char ep_out, |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 119 | int ifc, int serial_index, unsigned zero_mask); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | |
| 121 | static inline int badname(const char *name) |
| 122 | { |
| 123 | while(*name) { |
| 124 | if(!isdigit(*name++)) return 1; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 129 | static 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 Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | { |
| 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 Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | |
| 139 | busdir = opendir(base); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 140 | if(busdir == 0) return; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | |
| 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; |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame^] | 153 | unsigned char* bufend; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | struct usb_device_descriptor* device; |
| 155 | struct usb_config_descriptor* config; |
| 156 | struct usb_interface_descriptor* interface; |
| 157 | struct usb_endpoint_descriptor *ep1, *ep2; |
| 158 | unsigned zero_mask = 0; |
| 159 | unsigned vid, pid; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 171 | if((fd = unix_open(devname, O_RDONLY)) < 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | continue; |
| 173 | } |
| 174 | |
| 175 | desclength = adb_read(fd, devdesc, sizeof(devdesc)); |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame^] | 176 | bufend = bufptr + desclength; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | |
| 178 | // should have device and configuration descriptors, and atleast two endpoints |
| 179 | if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) { |
| 180 | D("desclength %d is too small\n", desclength); |
| 181 | adb_close(fd); |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | device = (struct usb_device_descriptor*)bufptr; |
| 186 | bufptr += USB_DT_DEVICE_SIZE; |
| 187 | |
| 188 | if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) { |
| 189 | adb_close(fd); |
| 190 | continue; |
| 191 | } |
| 192 | |
| 193 | vid = __le16_to_cpu(device->idVendor); |
| 194 | pid = __le16_to_cpu(device->idProduct); |
| 195 | pid = devdesc[10] | (devdesc[11] << 8); |
| 196 | DBGX("[ %s is V:%04x P:%04x ]\n", devname, vid, pid); |
| 197 | |
| 198 | // should have config descriptor next |
| 199 | config = (struct usb_config_descriptor *)bufptr; |
| 200 | bufptr += USB_DT_CONFIG_SIZE; |
| 201 | if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) { |
| 202 | D("usb_config_descriptor not found\n"); |
| 203 | adb_close(fd); |
| 204 | continue; |
| 205 | } |
| 206 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame^] | 207 | // loop through all the descriptors and look for the ADB interface |
| 208 | while (bufptr < bufend) { |
| 209 | unsigned char length = bufptr[0]; |
| 210 | unsigned char type = bufptr[1]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame^] | 212 | if (type == USB_DT_INTERFACE) { |
| 213 | interface = (struct usb_interface_descriptor *)bufptr; |
| 214 | bufptr += length; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame^] | 216 | if (length != USB_DT_INTERFACE_SIZE) { |
| 217 | D("interface descriptor has wrong size\n"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame^] | 221 | DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d," |
| 222 | "bInterfaceProtocol: %d, bNumEndpoints: %d\n", |
| 223 | interface->bInterfaceClass, interface->bInterfaceSubClass, |
| 224 | interface->bInterfaceProtocol, interface->bNumEndpoints); |
| 225 | |
| 226 | if (interface->bNumEndpoints == 2 && |
| 227 | is_adb_interface(vid, pid, interface->bInterfaceClass, |
| 228 | interface->bInterfaceSubClass, interface->bInterfaceProtocol)) { |
| 229 | |
| 230 | DBGX("looking for bulk endpoints\n"); |
| 231 | // looks like ADB... |
| 232 | ep1 = (struct usb_endpoint_descriptor *)bufptr; |
| 233 | bufptr += USB_DT_ENDPOINT_SIZE; |
| 234 | ep2 = (struct usb_endpoint_descriptor *)bufptr; |
| 235 | bufptr += USB_DT_ENDPOINT_SIZE; |
| 236 | |
| 237 | if (bufptr > devdesc + desclength || |
| 238 | ep1->bLength != USB_DT_ENDPOINT_SIZE || |
| 239 | ep1->bDescriptorType != USB_DT_ENDPOINT || |
| 240 | ep2->bLength != USB_DT_ENDPOINT_SIZE || |
| 241 | ep2->bDescriptorType != USB_DT_ENDPOINT) { |
| 242 | D("endpoints not found\n"); |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | // both endpoints should be bulk |
| 247 | if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK || |
| 248 | ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) { |
| 249 | D("bulk endpoints not found\n"); |
| 250 | continue; |
| 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 | |
| 266 | register_device_callback(devname, local_ep_in, local_ep_out, |
| 267 | interface->bInterfaceNumber, device->iSerialNumber, zero_mask); |
| 268 | break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 269 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | } else { |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame^] | 271 | bufptr += length; |
| 272 | } |
| 273 | } // end of while |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 274 | |
| 275 | adb_close(fd); |
| 276 | } // end of devdir while |
| 277 | closedir(devdir); |
| 278 | } //end of busdir while |
| 279 | closedir(busdir); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void usb_cleanup() |
| 283 | { |
| 284 | } |
| 285 | |
| 286 | static int usb_bulk_write(usb_handle *h, const void *data, int len) |
| 287 | { |
| 288 | struct usbdevfs_urb *urb = &h->urb_out; |
| 289 | int res; |
| 290 | |
| 291 | memset(urb, 0, sizeof(*urb)); |
| 292 | urb->type = USBDEVFS_URB_TYPE_BULK; |
| 293 | urb->endpoint = h->ep_out; |
| 294 | urb->status = -1; |
| 295 | urb->buffer = (void*) data; |
| 296 | urb->buffer_length = len; |
| 297 | |
| 298 | D("++ write ++\n"); |
| 299 | |
| 300 | adb_mutex_lock(&h->lock); |
| 301 | if(h->dead) { |
| 302 | res = -1; |
| 303 | goto fail; |
| 304 | } |
| 305 | do { |
| 306 | res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb); |
| 307 | } while((res < 0) && (errno == EINTR)); |
| 308 | |
| 309 | if(res < 0) { |
| 310 | goto fail; |
| 311 | } |
| 312 | |
| 313 | res = -1; |
| 314 | h->urb_out_busy = 1; |
| 315 | for(;;) { |
| 316 | adb_cond_wait(&h->notify, &h->lock); |
| 317 | if(h->dead) { |
| 318 | break; |
| 319 | } |
| 320 | if(h->urb_out_busy == 0) { |
| 321 | if(urb->status == 0) { |
| 322 | res = urb->actual_length; |
| 323 | } |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | fail: |
| 328 | adb_mutex_unlock(&h->lock); |
| 329 | D("-- write --\n"); |
| 330 | return res; |
| 331 | } |
| 332 | |
| 333 | static int usb_bulk_read(usb_handle *h, void *data, int len) |
| 334 | { |
| 335 | struct usbdevfs_urb *urb = &h->urb_in; |
| 336 | struct usbdevfs_urb *out = NULL; |
| 337 | int res; |
| 338 | |
| 339 | memset(urb, 0, sizeof(*urb)); |
| 340 | urb->type = USBDEVFS_URB_TYPE_BULK; |
| 341 | urb->endpoint = h->ep_in; |
| 342 | urb->status = -1; |
| 343 | urb->buffer = data; |
| 344 | urb->buffer_length = len; |
| 345 | |
| 346 | |
| 347 | adb_mutex_lock(&h->lock); |
| 348 | if(h->dead) { |
| 349 | res = -1; |
| 350 | goto fail; |
| 351 | } |
| 352 | do { |
| 353 | res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb); |
| 354 | } while((res < 0) && (errno == EINTR)); |
| 355 | |
| 356 | if(res < 0) { |
| 357 | goto fail; |
| 358 | } |
| 359 | |
| 360 | h->urb_in_busy = 1; |
| 361 | for(;;) { |
| 362 | D("[ reap urb - wait ]\n"); |
| 363 | h->reaper_thread = pthread_self(); |
| 364 | adb_mutex_unlock(&h->lock); |
| 365 | res = ioctl(h->desc, USBDEVFS_REAPURB, &out); |
| 366 | adb_mutex_lock(&h->lock); |
| 367 | h->reaper_thread = 0; |
| 368 | if(h->dead) { |
| 369 | res = -1; |
| 370 | break; |
| 371 | } |
| 372 | if(res < 0) { |
| 373 | if(errno == EINTR) { |
| 374 | continue; |
| 375 | } |
| 376 | D("[ reap urb - error ]\n"); |
| 377 | break; |
| 378 | } |
| 379 | D("[ urb @%p status = %d, actual = %d ]\n", |
| 380 | out, out->status, out->actual_length); |
| 381 | |
| 382 | if(out == &h->urb_in) { |
| 383 | D("[ reap urb - IN complete ]\n"); |
| 384 | h->urb_in_busy = 0; |
| 385 | if(urb->status == 0) { |
| 386 | res = urb->actual_length; |
| 387 | } else { |
| 388 | res = -1; |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | if(out == &h->urb_out) { |
| 393 | D("[ reap urb - OUT compelete ]\n"); |
| 394 | h->urb_out_busy = 0; |
| 395 | adb_cond_broadcast(&h->notify); |
| 396 | } |
| 397 | } |
| 398 | fail: |
| 399 | adb_mutex_unlock(&h->lock); |
| 400 | return res; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | int usb_write(usb_handle *h, const void *_data, int len) |
| 405 | { |
| 406 | unsigned char *data = (unsigned char*) _data; |
| 407 | int n; |
| 408 | int need_zero = 0; |
| 409 | |
| 410 | if(h->zero_mask) { |
| 411 | /* if we need 0-markers and our transfer |
| 412 | ** is an even multiple of the packet size, |
| 413 | ** we make note of it |
| 414 | */ |
| 415 | if(!(len & h->zero_mask)) { |
| 416 | need_zero = 1; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | while(len > 0) { |
| 421 | int xfer = (len > 4096) ? 4096 : len; |
| 422 | |
| 423 | n = usb_bulk_write(h, data, xfer); |
| 424 | if(n != xfer) { |
| 425 | D("ERROR: n = %d, errno = %d (%s)\n", |
| 426 | n, errno, strerror(errno)); |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | len -= xfer; |
| 431 | data += xfer; |
| 432 | } |
| 433 | |
| 434 | if(need_zero){ |
| 435 | n = usb_bulk_write(h, _data, 0); |
| 436 | return n; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | int usb_read(usb_handle *h, void *_data, int len) |
| 443 | { |
| 444 | unsigned char *data = (unsigned char*) _data; |
| 445 | int n; |
| 446 | |
| 447 | D("++ usb_read ++\n"); |
| 448 | while(len > 0) { |
| 449 | int xfer = (len > 4096) ? 4096 : len; |
| 450 | |
| 451 | D("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname); |
| 452 | n = usb_bulk_read(h, data, xfer); |
| 453 | D("[ usb read %d ] = %d, fname=%s\n", xfer, n, h->fname); |
| 454 | if(n != xfer) { |
| 455 | if((errno == ETIMEDOUT) && (h->desc != -1)) { |
| 456 | D("[ timeout ]\n"); |
| 457 | if(n > 0){ |
| 458 | data += n; |
| 459 | len -= n; |
| 460 | } |
| 461 | continue; |
| 462 | } |
| 463 | D("ERROR: n = %d, errno = %d (%s)\n", |
| 464 | n, errno, strerror(errno)); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | len -= xfer; |
| 469 | data += xfer; |
| 470 | } |
| 471 | |
| 472 | D("-- usb_read --\n"); |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | void usb_kick(usb_handle *h) |
| 477 | { |
| 478 | D("[ kicking %p (fd = %d) ]\n", h, h->desc); |
| 479 | adb_mutex_lock(&h->lock); |
| 480 | if(h->dead == 0) { |
| 481 | h->dead = 1; |
| 482 | |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 483 | if (h->writeable) { |
| 484 | /* HACK ALERT! |
| 485 | ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB). |
| 486 | ** This is a workaround for that problem. |
| 487 | */ |
| 488 | if (h->reaper_thread) { |
| 489 | pthread_kill(h->reaper_thread, SIGALRM); |
| 490 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 491 | |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 492 | /* cancel any pending transactions |
| 493 | ** these will quietly fail if the txns are not active, |
| 494 | ** but this ensures that a reader blocked on REAPURB |
| 495 | ** will get unblocked |
| 496 | */ |
| 497 | ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_in); |
| 498 | ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_out); |
| 499 | h->urb_in.status = -ENODEV; |
| 500 | h->urb_out.status = -ENODEV; |
| 501 | h->urb_in_busy = 0; |
| 502 | h->urb_out_busy = 0; |
| 503 | adb_cond_broadcast(&h->notify); |
| 504 | } else { |
| 505 | unregister_usb_transport(h); |
| 506 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 507 | } |
| 508 | adb_mutex_unlock(&h->lock); |
| 509 | } |
| 510 | |
| 511 | int usb_close(usb_handle *h) |
| 512 | { |
| 513 | D("[ usb close ... ]\n"); |
| 514 | adb_mutex_lock(&usb_lock); |
| 515 | h->next->prev = h->prev; |
| 516 | h->prev->next = h->next; |
| 517 | h->prev = 0; |
| 518 | h->next = 0; |
| 519 | |
| 520 | adb_close(h->desc); |
| 521 | D("[ usb closed %p (fd = %d) ]\n", h, h->desc); |
| 522 | adb_mutex_unlock(&usb_lock); |
| 523 | |
| 524 | free(h); |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static void register_device(const char *dev_name, |
| 529 | unsigned char ep_in, unsigned char ep_out, |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 530 | int interface, int serial_index, unsigned zero_mask) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 531 | { |
| 532 | usb_handle* usb = 0; |
| 533 | int n = 0; |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 534 | char serial[256]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 535 | |
| 536 | /* Since Linux will not reassign the device ID (and dev_name) |
| 537 | ** as long as the device is open, we can add to the list here |
| 538 | ** once we open it and remove from the list when we're finally |
| 539 | ** closed and everything will work out fine. |
| 540 | ** |
| 541 | ** If we have a usb_handle on the list 'o handles with a matching |
| 542 | ** name, we have no further work to do. |
| 543 | */ |
| 544 | adb_mutex_lock(&usb_lock); |
| 545 | for(usb = handle_list.next; usb != &handle_list; usb = usb->next){ |
| 546 | if(!strcmp(usb->fname, dev_name)) { |
| 547 | adb_mutex_unlock(&usb_lock); |
| 548 | return; |
| 549 | } |
| 550 | } |
| 551 | adb_mutex_unlock(&usb_lock); |
| 552 | |
| 553 | D("[ usb located new device %s (%d/%d/%d) ]\n", |
| 554 | dev_name, ep_in, ep_out, interface); |
| 555 | usb = calloc(1, sizeof(usb_handle)); |
| 556 | strcpy(usb->fname, dev_name); |
| 557 | usb->ep_in = ep_in; |
| 558 | usb->ep_out = ep_out; |
| 559 | usb->zero_mask = zero_mask; |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 560 | usb->writeable = 1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 561 | |
| 562 | adb_cond_init(&usb->notify, 0); |
| 563 | adb_mutex_init(&usb->lock, 0); |
| 564 | /* initialize mark to 1 so we don't get garbage collected after the device scan */ |
| 565 | usb->mark = 1; |
| 566 | usb->reaper_thread = 0; |
| 567 | |
| 568 | usb->desc = unix_open(usb->fname, O_RDWR); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 569 | if(usb->desc < 0) { |
| 570 | /* if we fail, see if have read-only access */ |
| 571 | usb->desc = unix_open(usb->fname, O_RDONLY); |
| 572 | if(usb->desc < 0) goto fail; |
| 573 | usb->writeable = 0; |
| 574 | D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc); |
| 575 | } else { |
| 576 | D("[ usb open %s fd = %d]\n", usb->fname, usb->desc); |
| 577 | n = ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface); |
| 578 | if(n != 0) goto fail; |
| 579 | } |
| 580 | |
| 581 | /* read the device's serial number */ |
| 582 | serial[0] = 0; |
| 583 | memset(serial, 0, sizeof(serial)); |
| 584 | if (serial_index) { |
| 585 | struct usbdevfs_ctrltransfer ctrl; |
| 586 | __u16 buffer[128]; |
| 587 | __u16 languages[128]; |
| 588 | int i, result; |
| 589 | int languageCount = 0; |
| 590 | |
| 591 | memset(languages, 0, sizeof(languages)); |
| 592 | memset(&ctrl, 0, sizeof(ctrl)); |
| 593 | |
| 594 | // read list of supported languages |
| 595 | ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE; |
| 596 | ctrl.bRequest = USB_REQ_GET_DESCRIPTOR; |
| 597 | ctrl.wValue = (USB_DT_STRING << 8) | 0; |
| 598 | ctrl.wIndex = 0; |
| 599 | ctrl.wLength = sizeof(languages); |
| 600 | ctrl.data = languages; |
| 601 | |
| 602 | result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl); |
| 603 | if (result > 0) |
| 604 | languageCount = (result - 2) / 2; |
| 605 | |
| 606 | for (i = 1; i <= languageCount; i++) { |
| 607 | memset(buffer, 0, sizeof(buffer)); |
| 608 | memset(&ctrl, 0, sizeof(ctrl)); |
| 609 | |
| 610 | ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE; |
| 611 | ctrl.bRequest = USB_REQ_GET_DESCRIPTOR; |
| 612 | ctrl.wValue = (USB_DT_STRING << 8) | serial_index; |
| 613 | ctrl.wIndex = languages[i]; |
| 614 | ctrl.wLength = sizeof(buffer); |
| 615 | ctrl.data = buffer; |
| 616 | |
| 617 | result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl); |
| 618 | if (result > 0) { |
| 619 | int i; |
| 620 | // skip first word, and copy the rest to the serial string, changing shorts to bytes. |
| 621 | result /= 2; |
| 622 | for (i = 1; i < result; i++) |
| 623 | serial[i - 1] = buffer[i]; |
| 624 | serial[i - 1] = 0; |
| 625 | break; |
| 626 | } |
| 627 | } |
| 628 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 629 | |
| 630 | /* add to the end of the active handles */ |
| 631 | adb_mutex_lock(&usb_lock); |
| 632 | usb->next = &handle_list; |
| 633 | usb->prev = handle_list.prev; |
| 634 | usb->prev->next = usb; |
| 635 | usb->next->prev = usb; |
| 636 | adb_mutex_unlock(&usb_lock); |
| 637 | |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 638 | register_usb_transport(usb, serial, usb->writeable); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 639 | return; |
| 640 | |
| 641 | fail: |
| 642 | D("[ usb open %s error=%d, err_str = %s]\n", |
| 643 | usb->fname, errno, strerror(errno)); |
| 644 | if(usb->desc >= 0) { |
| 645 | adb_close(usb->desc); |
| 646 | } |
| 647 | free(usb); |
| 648 | } |
| 649 | |
| 650 | void* device_poll_thread(void* unused) |
| 651 | { |
| 652 | D("Created device thread\n"); |
| 653 | for(;;) { |
| 654 | /* XXX use inotify */ |
| 655 | find_usb_device("/dev/bus/usb", register_device); |
| 656 | kick_disconnected_devices(); |
| 657 | sleep(1); |
| 658 | } |
| 659 | return NULL; |
| 660 | } |
| 661 | |
| 662 | static void sigalrm_handler(int signo) |
| 663 | { |
| 664 | // don't need to do anything here |
| 665 | } |
| 666 | |
| 667 | void usb_init() |
| 668 | { |
| 669 | adb_thread_t tid; |
| 670 | struct sigaction actions; |
| 671 | |
| 672 | memset(&actions, 0, sizeof(actions)); |
| 673 | sigemptyset(&actions.sa_mask); |
| 674 | actions.sa_flags = 0; |
| 675 | actions.sa_handler = sigalrm_handler; |
| 676 | sigaction(SIGALRM,& actions, NULL); |
| 677 | |
| 678 | if(adb_thread_create(&tid, device_poll_thread, NULL)){ |
| 679 | fatal_errno("cannot create input thread"); |
| 680 | } |
| 681 | } |
| 682 | |