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