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 | |
| 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 | fe582b5 | 2010-02-19 17:45:57 -0500 | [diff] [blame] | 24 | #include <sys/time.h> |
The Android Open Source Project | dd7bc33 | 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 | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 48 | ADB_MUTEX_DEFINE( usb_lock ); |
The Android Open Source Project | dd7bc33 | 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 | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 61 | unsigned writeable; |
The Android Open Source Project | dd7bc33 | 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 | e109d26 | 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 | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 121 | int ifc, int serial_index, unsigned zero_mask); |
The Android Open Source Project | dd7bc33 | 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 | 0927bf9 | 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 | e109d26 | 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 | dd7bc33 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | |
| 141 | busdir = opendir(base); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 142 | if(busdir == 0) return; |
The Android Open Source Project | dd7bc33 | 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 | b596608 | 2011-01-07 23:18:14 -0500 | [diff] [blame] | 153 | unsigned char devdesc[4096]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | unsigned char* bufptr = devdesc; |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 155 | unsigned char* bufend; |
The Android Open Source Project | dd7bc33 | 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 | dd7bc33 | 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); |
Nick Kralevich | 9866a66 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 173 | 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] | 174 | continue; |
| 175 | } |
| 176 | |
| 177 | desclength = adb_read(fd, devdesc, sizeof(devdesc)); |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 178 | bufend = bufptr + desclength; |
The Android Open Source Project | dd7bc33 | 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 | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 182 | D("desclength %zu is too small\n", desclength); |
The Android Open Source Project | dd7bc33 | 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 | 6f703a2 | 2010-09-22 20:09:44 +0200 | [diff] [blame] | 195 | vid = device->idVendor; |
| 196 | pid = device->idProduct; |
The Android Open Source Project | dd7bc33 | 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 | 07e8f7e | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 212 | |
Mike Lockwood | 07e8f7e | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 | |
Mike Lockwood | 07e8f7e | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | |
Mike Lockwood | 07e8f7e | 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 | e109d26 | 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 | 07e8f7e | 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 | 58b01e0 | 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 | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 247 | ep2 = (struct usb_endpoint_descriptor *)bufptr; |
| 248 | bufptr += USB_DT_ENDPOINT_SIZE; |
Ingo Rohloff | 58b01e0 | 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 | 07e8f7e | 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 | e109d26 | 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 | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 304 | interface->bInterfaceNumber, device->iSerialNumber, zero_mask); |
| 305 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 306 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | } else { |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 308 | bufptr += length; |
| 309 | } |
| 310 | } // end of while |
The Android Open Source Project | dd7bc33 | 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 | dd7bc33 | 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 | fe582b5 | 2010-02-19 17:45:57 -0500 | [diff] [blame] | 327 | struct timeval tv; |
| 328 | struct timespec ts; |
The Android Open Source Project | dd7bc33 | 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 | fe582b5 | 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 | dd7bc33 | 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 | |
Nanik Tolaram | b627a0e | 2015-02-18 22:53:37 +1100 | [diff] [blame^] | 382 | D("++ usb_bulk_read ++\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 383 | memset(urb, 0, sizeof(*urb)); |
| 384 | urb->type = USBDEVFS_URB_TYPE_BULK; |
| 385 | urb->endpoint = h->ep_in; |
| 386 | urb->status = -1; |
| 387 | urb->buffer = data; |
| 388 | urb->buffer_length = len; |
| 389 | |
| 390 | |
| 391 | adb_mutex_lock(&h->lock); |
| 392 | if(h->dead) { |
| 393 | res = -1; |
| 394 | goto fail; |
| 395 | } |
| 396 | do { |
| 397 | res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb); |
| 398 | } while((res < 0) && (errno == EINTR)); |
| 399 | |
| 400 | if(res < 0) { |
| 401 | goto fail; |
| 402 | } |
| 403 | |
| 404 | h->urb_in_busy = 1; |
| 405 | for(;;) { |
| 406 | D("[ reap urb - wait ]\n"); |
| 407 | h->reaper_thread = pthread_self(); |
| 408 | adb_mutex_unlock(&h->lock); |
| 409 | res = ioctl(h->desc, USBDEVFS_REAPURB, &out); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 410 | int saved_errno = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 411 | adb_mutex_lock(&h->lock); |
| 412 | h->reaper_thread = 0; |
| 413 | if(h->dead) { |
| 414 | res = -1; |
| 415 | break; |
| 416 | } |
| 417 | if(res < 0) { |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 418 | if(saved_errno == EINTR) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | continue; |
| 420 | } |
| 421 | D("[ reap urb - error ]\n"); |
| 422 | break; |
| 423 | } |
| 424 | D("[ urb @%p status = %d, actual = %d ]\n", |
| 425 | out, out->status, out->actual_length); |
| 426 | |
| 427 | if(out == &h->urb_in) { |
| 428 | D("[ reap urb - IN complete ]\n"); |
| 429 | h->urb_in_busy = 0; |
| 430 | if(urb->status == 0) { |
| 431 | res = urb->actual_length; |
| 432 | } else { |
| 433 | res = -1; |
| 434 | } |
| 435 | break; |
| 436 | } |
| 437 | if(out == &h->urb_out) { |
| 438 | D("[ reap urb - OUT compelete ]\n"); |
| 439 | h->urb_out_busy = 0; |
| 440 | adb_cond_broadcast(&h->notify); |
| 441 | } |
| 442 | } |
| 443 | fail: |
| 444 | adb_mutex_unlock(&h->lock); |
Nanik Tolaram | b627a0e | 2015-02-18 22:53:37 +1100 | [diff] [blame^] | 445 | D("-- usb_bulk_read --\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 446 | return res; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | int usb_write(usb_handle *h, const void *_data, int len) |
| 451 | { |
| 452 | unsigned char *data = (unsigned char*) _data; |
| 453 | int n; |
| 454 | int need_zero = 0; |
| 455 | |
Nanik Tolaram | b627a0e | 2015-02-18 22:53:37 +1100 | [diff] [blame^] | 456 | D("++ usb_write ++\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 457 | if(h->zero_mask) { |
| 458 | /* if we need 0-markers and our transfer |
| 459 | ** is an even multiple of the packet size, |
| 460 | ** we make note of it |
| 461 | */ |
| 462 | if(!(len & h->zero_mask)) { |
| 463 | need_zero = 1; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | while(len > 0) { |
| 468 | int xfer = (len > 4096) ? 4096 : len; |
| 469 | |
| 470 | n = usb_bulk_write(h, data, xfer); |
| 471 | if(n != xfer) { |
| 472 | D("ERROR: n = %d, errno = %d (%s)\n", |
| 473 | n, errno, strerror(errno)); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | len -= xfer; |
| 478 | data += xfer; |
| 479 | } |
| 480 | |
| 481 | if(need_zero){ |
| 482 | n = usb_bulk_write(h, _data, 0); |
| 483 | return n; |
| 484 | } |
| 485 | |
Nanik Tolaram | b627a0e | 2015-02-18 22:53:37 +1100 | [diff] [blame^] | 486 | D("-- usb_write --\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | int usb_read(usb_handle *h, void *_data, int len) |
| 491 | { |
| 492 | unsigned char *data = (unsigned char*) _data; |
| 493 | int n; |
| 494 | |
| 495 | D("++ usb_read ++\n"); |
| 496 | while(len > 0) { |
| 497 | int xfer = (len > 4096) ? 4096 : len; |
| 498 | |
| 499 | D("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname); |
| 500 | n = usb_bulk_read(h, data, xfer); |
| 501 | D("[ usb read %d ] = %d, fname=%s\n", xfer, n, h->fname); |
| 502 | if(n != xfer) { |
| 503 | if((errno == ETIMEDOUT) && (h->desc != -1)) { |
| 504 | D("[ timeout ]\n"); |
| 505 | if(n > 0){ |
| 506 | data += n; |
| 507 | len -= n; |
| 508 | } |
| 509 | continue; |
| 510 | } |
| 511 | D("ERROR: n = %d, errno = %d (%s)\n", |
| 512 | n, errno, strerror(errno)); |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | len -= xfer; |
| 517 | data += xfer; |
| 518 | } |
| 519 | |
| 520 | D("-- usb_read --\n"); |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | void usb_kick(usb_handle *h) |
| 525 | { |
| 526 | D("[ kicking %p (fd = %d) ]\n", h, h->desc); |
| 527 | adb_mutex_lock(&h->lock); |
| 528 | if(h->dead == 0) { |
| 529 | h->dead = 1; |
| 530 | |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 531 | if (h->writeable) { |
| 532 | /* HACK ALERT! |
| 533 | ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB). |
| 534 | ** This is a workaround for that problem. |
| 535 | */ |
| 536 | if (h->reaper_thread) { |
| 537 | pthread_kill(h->reaper_thread, SIGALRM); |
| 538 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 539 | |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 540 | /* cancel any pending transactions |
| 541 | ** these will quietly fail if the txns are not active, |
| 542 | ** but this ensures that a reader blocked on REAPURB |
| 543 | ** will get unblocked |
| 544 | */ |
| 545 | ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_in); |
| 546 | ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_out); |
| 547 | h->urb_in.status = -ENODEV; |
| 548 | h->urb_out.status = -ENODEV; |
| 549 | h->urb_in_busy = 0; |
| 550 | h->urb_out_busy = 0; |
| 551 | adb_cond_broadcast(&h->notify); |
| 552 | } else { |
| 553 | unregister_usb_transport(h); |
| 554 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 555 | } |
| 556 | adb_mutex_unlock(&h->lock); |
| 557 | } |
| 558 | |
| 559 | int usb_close(usb_handle *h) |
| 560 | { |
Nanik Tolaram | b627a0e | 2015-02-18 22:53:37 +1100 | [diff] [blame^] | 561 | D("++ usb close ++\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 562 | adb_mutex_lock(&usb_lock); |
| 563 | h->next->prev = h->prev; |
| 564 | h->prev->next = h->next; |
| 565 | h->prev = 0; |
| 566 | h->next = 0; |
| 567 | |
| 568 | adb_close(h->desc); |
Nanik Tolaram | b627a0e | 2015-02-18 22:53:37 +1100 | [diff] [blame^] | 569 | 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] | 570 | adb_mutex_unlock(&usb_lock); |
| 571 | |
| 572 | free(h); |
| 573 | return 0; |
| 574 | } |
| 575 | |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 576 | 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] | 577 | unsigned char ep_in, unsigned char ep_out, |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 578 | int interface, int serial_index, unsigned zero_mask) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 579 | { |
| 580 | usb_handle* usb = 0; |
| 581 | int n = 0; |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 582 | char serial[256]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 583 | |
| 584 | /* Since Linux will not reassign the device ID (and dev_name) |
| 585 | ** as long as the device is open, we can add to the list here |
| 586 | ** once we open it and remove from the list when we're finally |
| 587 | ** closed and everything will work out fine. |
| 588 | ** |
| 589 | ** If we have a usb_handle on the list 'o handles with a matching |
| 590 | ** name, we have no further work to do. |
| 591 | */ |
| 592 | adb_mutex_lock(&usb_lock); |
| 593 | for(usb = handle_list.next; usb != &handle_list; usb = usb->next){ |
| 594 | if(!strcmp(usb->fname, dev_name)) { |
| 595 | adb_mutex_unlock(&usb_lock); |
| 596 | return; |
| 597 | } |
| 598 | } |
| 599 | adb_mutex_unlock(&usb_lock); |
| 600 | |
| 601 | D("[ usb located new device %s (%d/%d/%d) ]\n", |
| 602 | dev_name, ep_in, ep_out, interface); |
| 603 | usb = calloc(1, sizeof(usb_handle)); |
| 604 | strcpy(usb->fname, dev_name); |
| 605 | usb->ep_in = ep_in; |
| 606 | usb->ep_out = ep_out; |
| 607 | usb->zero_mask = zero_mask; |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 608 | usb->writeable = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 609 | |
| 610 | adb_cond_init(&usb->notify, 0); |
| 611 | adb_mutex_init(&usb->lock, 0); |
| 612 | /* initialize mark to 1 so we don't get garbage collected after the device scan */ |
| 613 | usb->mark = 1; |
| 614 | usb->reaper_thread = 0; |
| 615 | |
Nick Kralevich | 9866a66 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 616 | usb->desc = unix_open(usb->fname, O_RDWR | O_CLOEXEC); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 617 | if(usb->desc < 0) { |
| 618 | /* if we fail, see if have read-only access */ |
Nick Kralevich | 9866a66 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 619 | usb->desc = unix_open(usb->fname, O_RDONLY | O_CLOEXEC); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 620 | if(usb->desc < 0) goto fail; |
| 621 | usb->writeable = 0; |
| 622 | D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc); |
| 623 | } else { |
| 624 | D("[ usb open %s fd = %d]\n", usb->fname, usb->desc); |
| 625 | n = ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface); |
| 626 | if(n != 0) goto fail; |
| 627 | } |
| 628 | |
| 629 | /* read the device's serial number */ |
| 630 | serial[0] = 0; |
| 631 | memset(serial, 0, sizeof(serial)); |
| 632 | if (serial_index) { |
| 633 | struct usbdevfs_ctrltransfer ctrl; |
| 634 | __u16 buffer[128]; |
| 635 | __u16 languages[128]; |
| 636 | int i, result; |
| 637 | int languageCount = 0; |
| 638 | |
| 639 | memset(languages, 0, sizeof(languages)); |
| 640 | memset(&ctrl, 0, sizeof(ctrl)); |
| 641 | |
| 642 | // read list of supported languages |
| 643 | ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE; |
| 644 | ctrl.bRequest = USB_REQ_GET_DESCRIPTOR; |
| 645 | ctrl.wValue = (USB_DT_STRING << 8) | 0; |
| 646 | ctrl.wIndex = 0; |
| 647 | ctrl.wLength = sizeof(languages); |
| 648 | ctrl.data = languages; |
Omari Stephens | 8bbae23 | 2011-05-09 18:45:06 -0700 | [diff] [blame] | 649 | ctrl.timeout = 1000; |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 650 | |
| 651 | result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl); |
| 652 | if (result > 0) |
| 653 | languageCount = (result - 2) / 2; |
| 654 | |
| 655 | for (i = 1; i <= languageCount; i++) { |
| 656 | memset(buffer, 0, sizeof(buffer)); |
| 657 | memset(&ctrl, 0, sizeof(ctrl)); |
| 658 | |
| 659 | ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE; |
| 660 | ctrl.bRequest = USB_REQ_GET_DESCRIPTOR; |
| 661 | ctrl.wValue = (USB_DT_STRING << 8) | serial_index; |
Marcus Comstedt | 6f703a2 | 2010-09-22 20:09:44 +0200 | [diff] [blame] | 662 | ctrl.wIndex = __le16_to_cpu(languages[i]); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 663 | ctrl.wLength = sizeof(buffer); |
| 664 | ctrl.data = buffer; |
Omari Stephens | 8bbae23 | 2011-05-09 18:45:06 -0700 | [diff] [blame] | 665 | ctrl.timeout = 1000; |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 666 | |
| 667 | result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl); |
| 668 | if (result > 0) { |
| 669 | int i; |
| 670 | // skip first word, and copy the rest to the serial string, changing shorts to bytes. |
| 671 | result /= 2; |
| 672 | for (i = 1; i < result; i++) |
Marcus Comstedt | 6f703a2 | 2010-09-22 20:09:44 +0200 | [diff] [blame] | 673 | serial[i - 1] = __le16_to_cpu(buffer[i]); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 674 | serial[i - 1] = 0; |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 679 | |
| 680 | /* add to the end of the active handles */ |
| 681 | adb_mutex_lock(&usb_lock); |
| 682 | usb->next = &handle_list; |
| 683 | usb->prev = handle_list.prev; |
| 684 | usb->prev->next = usb; |
| 685 | usb->next->prev = usb; |
| 686 | adb_mutex_unlock(&usb_lock); |
| 687 | |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 688 | register_usb_transport(usb, serial, devpath, usb->writeable); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 689 | return; |
| 690 | |
| 691 | fail: |
| 692 | D("[ usb open %s error=%d, err_str = %s]\n", |
| 693 | usb->fname, errno, strerror(errno)); |
| 694 | if(usb->desc >= 0) { |
| 695 | adb_close(usb->desc); |
| 696 | } |
| 697 | free(usb); |
| 698 | } |
| 699 | |
| 700 | void* device_poll_thread(void* unused) |
| 701 | { |
| 702 | D("Created device thread\n"); |
| 703 | for(;;) { |
| 704 | /* XXX use inotify */ |
| 705 | find_usb_device("/dev/bus/usb", register_device); |
| 706 | kick_disconnected_devices(); |
| 707 | sleep(1); |
| 708 | } |
| 709 | return NULL; |
| 710 | } |
| 711 | |
| 712 | static void sigalrm_handler(int signo) |
| 713 | { |
| 714 | // don't need to do anything here |
| 715 | } |
| 716 | |
| 717 | void usb_init() |
| 718 | { |
| 719 | adb_thread_t tid; |
| 720 | struct sigaction actions; |
| 721 | |
| 722 | memset(&actions, 0, sizeof(actions)); |
| 723 | sigemptyset(&actions.sa_mask); |
| 724 | actions.sa_flags = 0; |
| 725 | actions.sa_handler = sigalrm_handler; |
| 726 | sigaction(SIGALRM,& actions, NULL); |
| 727 | |
| 728 | if(adb_thread_create(&tid, device_poll_thread, NULL)){ |
| 729 | fatal_errno("cannot create input thread"); |
| 730 | } |
| 731 | } |