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