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