The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include <sys/ioctl.h> |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 35 | #include <sys/stat.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <dirent.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <errno.h> |
| 40 | #include <pthread.h> |
| 41 | #include <ctype.h> |
| 42 | |
| 43 | #include <linux/usbdevice_fs.h> |
| 44 | #include <linux/usbdevice_fs.h> |
| 45 | #include <linux/version.h> |
| 46 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) |
| 47 | #include <linux/usb/ch9.h> |
| 48 | #else |
| 49 | #include <linux/usb_ch9.h> |
| 50 | #endif |
| 51 | #include <asm/byteorder.h> |
| 52 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 53 | #include "fastboot.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | #include "usb.h" |
| 55 | |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 56 | #define MAX_RETRIES 5 |
| 57 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 58 | /* Timeout in seconds for usb_wait_for_disconnect. |
| 59 | * It doesn't usually take long for a device to disconnect (almost always |
| 60 | * under 2 seconds) but we'll time out after 3 seconds just in case. |
| 61 | */ |
| 62 | #define WAIT_FOR_DISCONNECT_TIMEOUT 3 |
| 63 | |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 64 | #ifdef TRACE_USB |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | #define DBG1(x...) fprintf(stderr, x) |
| 66 | #define DBG(x...) fprintf(stderr, x) |
| 67 | #else |
| 68 | #define DBG(x...) |
| 69 | #define DBG1(x...) |
| 70 | #endif |
| 71 | |
David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 72 | /* The max bulk size for linux is 16384 which is defined |
| 73 | * in drivers/usb/core/devio.c. |
| 74 | */ |
| 75 | #define MAX_USBFS_BULK_SIZE (16 * 1024) |
| 76 | |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 77 | struct usb_handle |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | { |
| 79 | char fname[64]; |
| 80 | int desc; |
| 81 | unsigned char ep_in; |
| 82 | unsigned char ep_out; |
| 83 | }; |
| 84 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 85 | /* True if name isn't a valid name for a USB device in /sys/bus/usb/devices. |
| 86 | * Device names are made up of numbers, dots, and dashes, e.g., '7-1.5'. |
| 87 | * We reject interfaces (e.g., '7-1.5:1.0') and host controllers (e.g. 'usb1'). |
| 88 | * The name must also start with a digit, to disallow '.' and '..' |
| 89 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | static inline int badname(const char *name) |
| 91 | { |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 92 | if (!isdigit(*name)) |
| 93 | return 1; |
| 94 | while(*++name) { |
| 95 | if(!isdigit(*name) && *name != '.' && *name != '-') |
| 96 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int check(void *_desc, int len, unsigned type, int size) |
| 102 | { |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 103 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)_desc; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 104 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | if(len < size) return -1; |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 106 | if(hdr->bLength < size) return -1; |
| 107 | if(hdr->bLength > len) return -1; |
| 108 | if(hdr->bDescriptorType != type) return -1; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 109 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 113 | static int filter_usb_device(char* sysfs_name, |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 114 | char *ptr, int len, int writable, |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 115 | ifc_match_func callback, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | int *ept_in_id, int *ept_out_id, int *ifc_id) |
| 117 | { |
| 118 | struct usb_device_descriptor *dev; |
| 119 | struct usb_config_descriptor *cfg; |
| 120 | struct usb_interface_descriptor *ifc; |
| 121 | struct usb_endpoint_descriptor *ept; |
| 122 | struct usb_ifc_info info; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 123 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 124 | int in, out; |
| 125 | unsigned i; |
| 126 | unsigned e; |
| 127 | |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 128 | if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE)) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | return -1; |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 130 | dev = (struct usb_device_descriptor *)ptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | len -= dev->bLength; |
| 132 | ptr += dev->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 133 | |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 134 | if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE)) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | return -1; |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 136 | cfg = (struct usb_config_descriptor *)ptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | len -= cfg->bLength; |
| 138 | ptr += cfg->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 139 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | info.dev_vendor = dev->idVendor; |
| 141 | info.dev_product = dev->idProduct; |
| 142 | info.dev_class = dev->bDeviceClass; |
| 143 | info.dev_subclass = dev->bDeviceSubClass; |
| 144 | info.dev_protocol = dev->bDeviceProtocol; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 145 | info.writable = writable; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 146 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 147 | snprintf(info.device_path, sizeof(info.device_path), "usb:%s", sysfs_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 149 | /* Read device serial number (if there is one). |
| 150 | * We read the serial number from sysfs, since it's faster and more |
| 151 | * reliable than issuing a control pipe read, and also won't |
| 152 | * cause problems for devices which don't like getting descriptor |
| 153 | * requests while they're in the middle of flashing. |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 154 | */ |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 155 | info.serial_number[0] = '\0'; |
| 156 | if (dev->iSerialNumber) { |
| 157 | char path[80]; |
| 158 | int fd; |
| 159 | |
| 160 | snprintf(path, sizeof(path), |
| 161 | "/sys/bus/usb/devices/%s/serial", sysfs_name); |
| 162 | path[sizeof(path) - 1] = '\0'; |
| 163 | |
| 164 | fd = open(path, O_RDONLY); |
| 165 | if (fd >= 0) { |
| 166 | int chars_read = read(fd, info.serial_number, |
| 167 | sizeof(info.serial_number) - 1); |
| 168 | close(fd); |
| 169 | |
| 170 | if (chars_read <= 0) |
| 171 | info.serial_number[0] = '\0'; |
| 172 | else if (info.serial_number[chars_read - 1] == '\n') { |
| 173 | // strip trailing newline |
| 174 | info.serial_number[chars_read - 1] = '\0'; |
| 175 | } |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | for(i = 0; i < cfg->bNumInterfaces; i++) { |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 180 | |
| 181 | while (len > 0) { |
| 182 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr; |
| 183 | if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE) == 0) |
| 184 | break; |
| 185 | len -= hdr->bLength; |
| 186 | ptr += hdr->bLength; |
| 187 | } |
| 188 | |
| 189 | if (len <= 0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | return -1; |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 191 | |
| 192 | ifc = (struct usb_interface_descriptor *)ptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | len -= ifc->bLength; |
| 194 | ptr += ifc->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 195 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | in = -1; |
| 197 | out = -1; |
| 198 | info.ifc_class = ifc->bInterfaceClass; |
| 199 | info.ifc_subclass = ifc->bInterfaceSubClass; |
| 200 | info.ifc_protocol = ifc->bInterfaceProtocol; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 201 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 202 | for(e = 0; e < ifc->bNumEndpoints; e++) { |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 203 | while (len > 0) { |
| 204 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr; |
| 205 | if (check(hdr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE) == 0) |
| 206 | break; |
| 207 | len -= hdr->bLength; |
| 208 | ptr += hdr->bLength; |
| 209 | } |
| 210 | if (len < 0) { |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | ept = (struct usb_endpoint_descriptor *)ptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | len -= ept->bLength; |
| 216 | ptr += ept->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 217 | |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 218 | if((ept->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | continue; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 220 | |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 221 | if(ept->bEndpointAddress & USB_ENDPOINT_DIR_MASK) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 222 | in = ept->bEndpointAddress; |
| 223 | } else { |
| 224 | out = ept->bEndpointAddress; |
| 225 | } |
Jack Pham | 1c02213 | 2014-12-05 12:11:20 -0800 | [diff] [blame] | 226 | |
| 227 | // For USB 3.0 devices skip the SS Endpoint Companion descriptor |
| 228 | if (check((struct usb_descriptor_hdr *)ptr, len, |
| 229 | USB_DT_SS_ENDPOINT_COMP, USB_DT_SS_EP_COMP_SIZE) == 0) { |
| 230 | len -= USB_DT_SS_EP_COMP_SIZE; |
| 231 | ptr += USB_DT_SS_EP_COMP_SIZE; |
| 232 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | info.has_bulk_in = (in != -1); |
| 236 | info.has_bulk_out = (out != -1); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 237 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 238 | if(callback(&info) == 0) { |
| 239 | *ept_in_id = in; |
| 240 | *ept_out_id = out; |
| 241 | *ifc_id = ifc->bInterfaceNumber; |
| 242 | return 0; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return -1; |
| 247 | } |
| 248 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 249 | static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node, |
| 250 | char* buf, int bufsize) |
| 251 | { |
| 252 | char path[80]; |
| 253 | int fd, n; |
| 254 | |
| 255 | snprintf(path, sizeof(path), |
| 256 | "/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node); |
| 257 | path[sizeof(path) - 1] = '\0'; |
| 258 | |
| 259 | fd = open(path, O_RDONLY); |
| 260 | if (fd < 0) |
| 261 | return -1; |
| 262 | |
| 263 | n = read(fd, buf, bufsize - 1); |
| 264 | close(fd); |
| 265 | |
| 266 | if (n < 0) |
| 267 | return -1; |
| 268 | |
| 269 | buf[n] = '\0'; |
| 270 | |
| 271 | return n; |
| 272 | } |
| 273 | |
| 274 | static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node) |
| 275 | { |
| 276 | char buf[16]; |
| 277 | int value; |
| 278 | |
| 279 | if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0) |
| 280 | return -1; |
| 281 | |
| 282 | if (sscanf(buf, "%d", &value) != 1) |
| 283 | return -1; |
| 284 | |
| 285 | return value; |
| 286 | } |
| 287 | |
| 288 | /* Given the name of a USB device in sysfs, get the name for the same |
| 289 | * device in devfs. Returns 0 for success, -1 for failure. |
| 290 | */ |
| 291 | static int convert_to_devfs_name(const char* sysfs_name, |
| 292 | char* devname, int devname_size) |
| 293 | { |
| 294 | int busnum, devnum; |
| 295 | |
| 296 | busnum = read_sysfs_number(sysfs_name, "busnum"); |
| 297 | if (busnum < 0) |
| 298 | return -1; |
| 299 | |
| 300 | devnum = read_sysfs_number(sysfs_name, "devnum"); |
| 301 | if (devnum < 0) |
| 302 | return -1; |
| 303 | |
| 304 | snprintf(devname, devname_size, "/dev/bus/usb/%03d/%03d", busnum, devnum); |
| 305 | return 0; |
| 306 | } |
| 307 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 308 | static usb_handle *find_usb_device(const char *base, ifc_match_func callback) |
| 309 | { |
| 310 | usb_handle *usb = 0; |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 311 | char devname[64]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 312 | char desc[1024]; |
| 313 | int n, in, out, ifc; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 314 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 315 | DIR *busdir; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | struct dirent *de; |
| 317 | int fd; |
Elliott Hughes | c500be9 | 2009-10-07 17:24:39 -0700 | [diff] [blame] | 318 | int writable; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 319 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 320 | busdir = opendir(base); |
| 321 | if(busdir == 0) return 0; |
| 322 | |
| 323 | while((de = readdir(busdir)) && (usb == 0)) { |
| 324 | if(badname(de->d_name)) continue; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 325 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 326 | if(!convert_to_devfs_name(de->d_name, devname, sizeof(devname))) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 327 | |
| 328 | // DBG("[ scanning %s ]\n", devname); |
Elliott Hughes | c500be9 | 2009-10-07 17:24:39 -0700 | [diff] [blame] | 329 | writable = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | if((fd = open(devname, O_RDWR)) < 0) { |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 331 | // Check if we have read-only access, so we can give a helpful |
| 332 | // diagnostic like "adb devices" does. |
| 333 | writable = 0; |
| 334 | if((fd = open(devname, O_RDONLY)) < 0) { |
| 335 | continue; |
| 336 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | n = read(fd, desc, sizeof(desc)); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 340 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 341 | if(filter_usb_device(de->d_name, desc, n, writable, callback, |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 342 | &in, &out, &ifc) == 0) { |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 343 | usb = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | strcpy(usb->fname, devname); |
| 345 | usb->ep_in = in; |
| 346 | usb->ep_out = out; |
| 347 | usb->desc = fd; |
| 348 | |
| 349 | n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); |
| 350 | if(n != 0) { |
| 351 | close(fd); |
| 352 | free(usb); |
| 353 | usb = 0; |
| 354 | continue; |
| 355 | } |
| 356 | } else { |
| 357 | close(fd); |
| 358 | } |
| 359 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 360 | } |
| 361 | closedir(busdir); |
| 362 | |
| 363 | return usb; |
| 364 | } |
| 365 | |
| 366 | int usb_write(usb_handle *h, const void *_data, int len) |
| 367 | { |
| 368 | unsigned char *data = (unsigned char*) _data; |
| 369 | unsigned count = 0; |
| 370 | struct usbdevfs_bulktransfer bulk; |
| 371 | int n; |
| 372 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 373 | if(h->ep_out == 0 || h->desc == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | return -1; |
| 375 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 376 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 377 | do { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | int xfer; |
David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 379 | xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 380 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 381 | bulk.ep = h->ep_out; |
| 382 | bulk.len = xfer; |
| 383 | bulk.data = data; |
| 384 | bulk.timeout = 0; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 385 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | n = ioctl(h->desc, USBDEVFS_BULK, &bulk); |
| 387 | if(n != xfer) { |
| 388 | DBG("ERROR: n = %d, errno = %d (%s)\n", |
| 389 | n, errno, strerror(errno)); |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | count += xfer; |
| 394 | len -= xfer; |
| 395 | data += xfer; |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 396 | } while(len > 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 397 | |
| 398 | return count; |
| 399 | } |
| 400 | |
| 401 | int usb_read(usb_handle *h, void *_data, int len) |
| 402 | { |
| 403 | unsigned char *data = (unsigned char*) _data; |
| 404 | unsigned count = 0; |
| 405 | struct usbdevfs_bulktransfer bulk; |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 406 | int n, retry; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 408 | if(h->ep_in == 0 || h->desc == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | return -1; |
| 410 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 411 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 412 | while(len > 0) { |
David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 413 | int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 414 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 415 | bulk.ep = h->ep_in; |
| 416 | bulk.len = xfer; |
| 417 | bulk.data = data; |
| 418 | bulk.timeout = 0; |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 419 | retry = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 420 | |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 421 | do{ |
| 422 | DBG("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname); |
| 423 | n = ioctl(h->desc, USBDEVFS_BULK, &bulk); |
| 424 | DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, h->fname, retry); |
| 425 | |
| 426 | if( n < 0 ) { |
| 427 | DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); |
| 428 | if ( ++retry > MAX_RETRIES ) return -1; |
| 429 | sleep( 1 ); |
| 430 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 431 | } |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 432 | while( n < 0 ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 433 | |
| 434 | count += n; |
| 435 | len -= n; |
| 436 | data += n; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 437 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 438 | if(n < xfer) { |
| 439 | break; |
| 440 | } |
| 441 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 442 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 443 | return count; |
| 444 | } |
| 445 | |
| 446 | void usb_kick(usb_handle *h) |
| 447 | { |
| 448 | int fd; |
| 449 | |
| 450 | fd = h->desc; |
| 451 | h->desc = -1; |
| 452 | if(fd >= 0) { |
| 453 | close(fd); |
| 454 | DBG("[ usb closed %d ]\n", fd); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | int usb_close(usb_handle *h) |
| 459 | { |
| 460 | int fd; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 461 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 462 | fd = h->desc; |
| 463 | h->desc = -1; |
| 464 | if(fd >= 0) { |
| 465 | close(fd); |
| 466 | DBG("[ usb closed %d ]\n", fd); |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | usb_handle *usb_open(ifc_match_func callback) |
| 473 | { |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 474 | return find_usb_device("/sys/bus/usb/devices", callback); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 475 | } |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 476 | |
| 477 | /* Wait for the system to notice the device is gone, so that a subsequent |
| 478 | * fastboot command won't try to access the device before it's rebooted. |
| 479 | * Returns 0 for success, -1 for timeout. |
| 480 | */ |
| 481 | int usb_wait_for_disconnect(usb_handle *usb) |
| 482 | { |
| 483 | double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT; |
| 484 | while (now() < deadline) { |
| 485 | if (access(usb->fname, F_OK)) |
| 486 | return 0; |
| 487 | usleep(50000); |
| 488 | } |
| 489 | return -1; |
| 490 | } |