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 | { |
| 103 | unsigned char *desc = _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; |
| 106 | if(desc[0] < size) return -1; |
| 107 | if(desc[0] > len) return -1; |
| 108 | if(desc[1] != 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 | |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 128 | struct stat st; |
| 129 | int result; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 130 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | if(check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE)) |
| 132 | return -1; |
| 133 | dev = (void*) ptr; |
| 134 | len -= dev->bLength; |
| 135 | ptr += dev->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 136 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | if(check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE)) |
| 138 | return -1; |
| 139 | cfg = (void*) ptr; |
| 140 | len -= cfg->bLength; |
| 141 | ptr += cfg->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 142 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | info.dev_vendor = dev->idVendor; |
| 144 | info.dev_product = dev->idProduct; |
| 145 | info.dev_class = dev->bDeviceClass; |
| 146 | info.dev_subclass = dev->bDeviceSubClass; |
| 147 | info.dev_protocol = dev->bDeviceProtocol; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 148 | info.writable = writable; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 149 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 150 | 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] | 151 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 152 | /* Read device serial number (if there is one). |
| 153 | * We read the serial number from sysfs, since it's faster and more |
| 154 | * reliable than issuing a control pipe read, and also won't |
| 155 | * cause problems for devices which don't like getting descriptor |
| 156 | * requests while they're in the middle of flashing. |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 157 | */ |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 158 | info.serial_number[0] = '\0'; |
| 159 | if (dev->iSerialNumber) { |
| 160 | char path[80]; |
| 161 | int fd; |
| 162 | |
| 163 | snprintf(path, sizeof(path), |
| 164 | "/sys/bus/usb/devices/%s/serial", sysfs_name); |
| 165 | path[sizeof(path) - 1] = '\0'; |
| 166 | |
| 167 | fd = open(path, O_RDONLY); |
| 168 | if (fd >= 0) { |
| 169 | int chars_read = read(fd, info.serial_number, |
| 170 | sizeof(info.serial_number) - 1); |
| 171 | close(fd); |
| 172 | |
| 173 | if (chars_read <= 0) |
| 174 | info.serial_number[0] = '\0'; |
| 175 | else if (info.serial_number[chars_read - 1] == '\n') { |
| 176 | // strip trailing newline |
| 177 | info.serial_number[chars_read - 1] = '\0'; |
| 178 | } |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 182 | for(i = 0; i < cfg->bNumInterfaces; i++) { |
| 183 | if(check(ptr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE)) |
| 184 | return -1; |
| 185 | ifc = (void*) ptr; |
| 186 | len -= ifc->bLength; |
| 187 | ptr += ifc->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 188 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | in = -1; |
| 190 | out = -1; |
| 191 | info.ifc_class = ifc->bInterfaceClass; |
| 192 | info.ifc_subclass = ifc->bInterfaceSubClass; |
| 193 | info.ifc_protocol = ifc->bInterfaceProtocol; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 194 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | for(e = 0; e < ifc->bNumEndpoints; e++) { |
| 196 | if(check(ptr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE)) |
| 197 | return -1; |
| 198 | ept = (void*) ptr; |
| 199 | len -= ept->bLength; |
| 200 | ptr += ept->bLength; |
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 | if((ept->bmAttributes & 0x03) != 0x02) |
| 203 | continue; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 204 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | if(ept->bEndpointAddress & 0x80) { |
| 206 | in = ept->bEndpointAddress; |
| 207 | } else { |
| 208 | out = ept->bEndpointAddress; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | info.has_bulk_in = (in != -1); |
| 213 | info.has_bulk_out = (out != -1); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 214 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | if(callback(&info) == 0) { |
| 216 | *ept_in_id = in; |
| 217 | *ept_out_id = out; |
| 218 | *ifc_id = ifc->bInterfaceNumber; |
| 219 | return 0; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return -1; |
| 224 | } |
| 225 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 226 | static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node, |
| 227 | char* buf, int bufsize) |
| 228 | { |
| 229 | char path[80]; |
| 230 | int fd, n; |
| 231 | |
| 232 | snprintf(path, sizeof(path), |
| 233 | "/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node); |
| 234 | path[sizeof(path) - 1] = '\0'; |
| 235 | |
| 236 | fd = open(path, O_RDONLY); |
| 237 | if (fd < 0) |
| 238 | return -1; |
| 239 | |
| 240 | n = read(fd, buf, bufsize - 1); |
| 241 | close(fd); |
| 242 | |
| 243 | if (n < 0) |
| 244 | return -1; |
| 245 | |
| 246 | buf[n] = '\0'; |
| 247 | |
| 248 | return n; |
| 249 | } |
| 250 | |
| 251 | static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node) |
| 252 | { |
| 253 | char buf[16]; |
| 254 | int value; |
| 255 | |
| 256 | if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0) |
| 257 | return -1; |
| 258 | |
| 259 | if (sscanf(buf, "%d", &value) != 1) |
| 260 | return -1; |
| 261 | |
| 262 | return value; |
| 263 | } |
| 264 | |
| 265 | /* Given the name of a USB device in sysfs, get the name for the same |
| 266 | * device in devfs. Returns 0 for success, -1 for failure. |
| 267 | */ |
| 268 | static int convert_to_devfs_name(const char* sysfs_name, |
| 269 | char* devname, int devname_size) |
| 270 | { |
| 271 | int busnum, devnum; |
| 272 | |
| 273 | busnum = read_sysfs_number(sysfs_name, "busnum"); |
| 274 | if (busnum < 0) |
| 275 | return -1; |
| 276 | |
| 277 | devnum = read_sysfs_number(sysfs_name, "devnum"); |
| 278 | if (devnum < 0) |
| 279 | return -1; |
| 280 | |
| 281 | snprintf(devname, devname_size, "/dev/bus/usb/%03d/%03d", busnum, devnum); |
| 282 | return 0; |
| 283 | } |
| 284 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 285 | static usb_handle *find_usb_device(const char *base, ifc_match_func callback) |
| 286 | { |
| 287 | usb_handle *usb = 0; |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 288 | char devname[64]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | char desc[1024]; |
| 290 | int n, in, out, ifc; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 291 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 292 | DIR *busdir; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 293 | struct dirent *de; |
| 294 | int fd; |
Elliott Hughes | c500be9 | 2009-10-07 17:24:39 -0700 | [diff] [blame] | 295 | int writable; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 296 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 297 | busdir = opendir(base); |
| 298 | if(busdir == 0) return 0; |
| 299 | |
| 300 | while((de = readdir(busdir)) && (usb == 0)) { |
| 301 | if(badname(de->d_name)) continue; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 302 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 303 | 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] | 304 | |
| 305 | // DBG("[ scanning %s ]\n", devname); |
Elliott Hughes | c500be9 | 2009-10-07 17:24:39 -0700 | [diff] [blame] | 306 | writable = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | if((fd = open(devname, O_RDWR)) < 0) { |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 308 | // Check if we have read-only access, so we can give a helpful |
| 309 | // diagnostic like "adb devices" does. |
| 310 | writable = 0; |
| 311 | if((fd = open(devname, O_RDONLY)) < 0) { |
| 312 | continue; |
| 313 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | n = read(fd, desc, sizeof(desc)); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 317 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame^] | 318 | if(filter_usb_device(de->d_name, desc, n, writable, callback, |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 319 | &in, &out, &ifc) == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 320 | usb = calloc(1, sizeof(usb_handle)); |
| 321 | strcpy(usb->fname, devname); |
| 322 | usb->ep_in = in; |
| 323 | usb->ep_out = out; |
| 324 | usb->desc = fd; |
| 325 | |
| 326 | n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); |
| 327 | if(n != 0) { |
| 328 | close(fd); |
| 329 | free(usb); |
| 330 | usb = 0; |
| 331 | continue; |
| 332 | } |
| 333 | } else { |
| 334 | close(fd); |
| 335 | } |
| 336 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 337 | } |
| 338 | closedir(busdir); |
| 339 | |
| 340 | return usb; |
| 341 | } |
| 342 | |
| 343 | int usb_write(usb_handle *h, const void *_data, int len) |
| 344 | { |
| 345 | unsigned char *data = (unsigned char*) _data; |
| 346 | unsigned count = 0; |
| 347 | struct usbdevfs_bulktransfer bulk; |
| 348 | int n; |
| 349 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame^] | 350 | if(h->ep_out == 0 || h->desc == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 351 | return -1; |
| 352 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 353 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame^] | 354 | do { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 | int xfer; |
David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 356 | xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 357 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 358 | bulk.ep = h->ep_out; |
| 359 | bulk.len = xfer; |
| 360 | bulk.data = data; |
| 361 | bulk.timeout = 0; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 362 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | n = ioctl(h->desc, USBDEVFS_BULK, &bulk); |
| 364 | if(n != xfer) { |
| 365 | DBG("ERROR: n = %d, errno = %d (%s)\n", |
| 366 | n, errno, strerror(errno)); |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | count += xfer; |
| 371 | len -= xfer; |
| 372 | data += xfer; |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame^] | 373 | } while(len > 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | |
| 375 | return count; |
| 376 | } |
| 377 | |
| 378 | int usb_read(usb_handle *h, void *_data, int len) |
| 379 | { |
| 380 | unsigned char *data = (unsigned char*) _data; |
| 381 | unsigned count = 0; |
| 382 | struct usbdevfs_bulktransfer bulk; |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 383 | int n, retry; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 384 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame^] | 385 | if(h->ep_in == 0 || h->desc == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | return -1; |
| 387 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 388 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 389 | while(len > 0) { |
David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 390 | int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 391 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 392 | bulk.ep = h->ep_in; |
| 393 | bulk.len = xfer; |
| 394 | bulk.data = data; |
| 395 | bulk.timeout = 0; |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 396 | retry = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 397 | |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 398 | do{ |
| 399 | DBG("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname); |
| 400 | n = ioctl(h->desc, USBDEVFS_BULK, &bulk); |
| 401 | DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, h->fname, retry); |
| 402 | |
| 403 | if( n < 0 ) { |
| 404 | DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); |
| 405 | if ( ++retry > MAX_RETRIES ) return -1; |
| 406 | sleep( 1 ); |
| 407 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | } |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 409 | while( n < 0 ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 410 | |
| 411 | count += n; |
| 412 | len -= n; |
| 413 | data += n; |
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 | if(n < xfer) { |
| 416 | break; |
| 417 | } |
| 418 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 419 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 420 | return count; |
| 421 | } |
| 422 | |
| 423 | void usb_kick(usb_handle *h) |
| 424 | { |
| 425 | int fd; |
| 426 | |
| 427 | fd = h->desc; |
| 428 | h->desc = -1; |
| 429 | if(fd >= 0) { |
| 430 | close(fd); |
| 431 | DBG("[ usb closed %d ]\n", fd); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | int usb_close(usb_handle *h) |
| 436 | { |
| 437 | int fd; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 438 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 | fd = h->desc; |
| 440 | h->desc = -1; |
| 441 | if(fd >= 0) { |
| 442 | close(fd); |
| 443 | DBG("[ usb closed %d ]\n", fd); |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | usb_handle *usb_open(ifc_match_func callback) |
| 450 | { |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 451 | return find_usb_device("/sys/bus/usb/devices", callback); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | } |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame^] | 453 | |
| 454 | /* Wait for the system to notice the device is gone, so that a subsequent |
| 455 | * fastboot command won't try to access the device before it's rebooted. |
| 456 | * Returns 0 for success, -1 for timeout. |
| 457 | */ |
| 458 | int usb_wait_for_disconnect(usb_handle *usb) |
| 459 | { |
| 460 | double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT; |
| 461 | while (now() < deadline) { |
| 462 | if (access(usb->fname, F_OK)) |
| 463 | return 0; |
| 464 | usleep(50000); |
| 465 | } |
| 466 | return -1; |
| 467 | } |