| 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 |  | 
| Elliott Hughes | 93f65fa | 2015-07-24 14:09:44 -0700 | [diff] [blame] | 29 | #include <ctype.h> | 
|  | 30 | #include <dirent.h> | 
|  | 31 | #include <errno.h> | 
|  | 32 | #include <fcntl.h> | 
|  | 33 | #include <pthread.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | #include <stdio.h> | 
|  | 35 | #include <stdlib.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include <string.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #include <sys/ioctl.h> | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 38 | #include <sys/stat.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | #include <sys/types.h> | 
| Elliott Hughes | 93f65fa | 2015-07-24 14:09:44 -0700 | [diff] [blame] | 40 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | #include <linux/usbdevice_fs.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | #include <linux/version.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | #include <linux/usb/ch9.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 46 | #include <memory> | 
|  | 47 |  | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 48 | #include "fastboot.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | #include "usb.h" | 
|  | 50 |  | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 51 | #define MAX_RETRIES 5 | 
|  | 52 |  | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 53 | /* Timeout in seconds for usb_wait_for_disconnect. | 
|  | 54 | * It doesn't usually take long for a device to disconnect (almost always | 
|  | 55 | * under 2 seconds) but we'll time out after 3 seconds just in case. | 
|  | 56 | */ | 
|  | 57 | #define WAIT_FOR_DISCONNECT_TIMEOUT  3 | 
|  | 58 |  | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 59 | #ifdef TRACE_USB | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | #define DBG1(x...) fprintf(stderr, x) | 
|  | 61 | #define DBG(x...) fprintf(stderr, x) | 
|  | 62 | #else | 
|  | 63 | #define DBG(x...) | 
|  | 64 | #define DBG1(x...) | 
|  | 65 | #endif | 
|  | 66 |  | 
| Elliott Hughes | 93f65fa | 2015-07-24 14:09:44 -0700 | [diff] [blame] | 67 | // Kernels before 3.3 have a 16KiB transfer limit. That limit was replaced | 
|  | 68 | // with a 16MiB global limit in 3.3, but each URB submitted required a | 
|  | 69 | // contiguous kernel allocation, so you would get ENOMEM if you tried to | 
|  | 70 | // send something larger than the biggest available contiguous kernel | 
|  | 71 | // memory region. 256KiB contiguous allocations are generally not reliable | 
|  | 72 | // on a device kernel that has been running for a while fragmenting its | 
|  | 73 | // memory, but that shouldn't be a problem for fastboot on the host. | 
|  | 74 | // In 3.6, the contiguous buffer limit was removed by allocating multiple | 
|  | 75 | // 16KiB chunks and having the USB driver stitch them back together while | 
|  | 76 | // transmitting using a scatter-gather list, so 256KiB bulk transfers should | 
|  | 77 | // be reliable. | 
|  | 78 | // 256KiB seems to work, but 1MiB bulk transfers lock up my z620 with a 3.13 | 
|  | 79 | // kernel. | 
| David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 80 | #define MAX_USBFS_BULK_SIZE (16 * 1024) | 
|  | 81 |  | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 82 | struct usb_handle | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | { | 
|  | 84 | char fname[64]; | 
|  | 85 | int desc; | 
|  | 86 | unsigned char ep_in; | 
|  | 87 | unsigned char ep_out; | 
|  | 88 | }; | 
|  | 89 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 90 | class LinuxUsbTransport : public Transport { | 
|  | 91 | public: | 
|  | 92 | LinuxUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {} | 
|  | 93 | ~LinuxUsbTransport() override = default; | 
|  | 94 |  | 
|  | 95 | ssize_t Read(void* data, size_t len) override; | 
|  | 96 | ssize_t Write(const void* data, size_t len) override; | 
|  | 97 | int Close() override; | 
|  | 98 | int WaitForDisconnect() override; | 
|  | 99 |  | 
|  | 100 | private: | 
|  | 101 | std::unique_ptr<usb_handle> handle_; | 
|  | 102 |  | 
|  | 103 | DISALLOW_COPY_AND_ASSIGN(LinuxUsbTransport); | 
|  | 104 | }; | 
|  | 105 |  | 
| Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 106 | /* True if name isn't a valid name for a USB device in /sys/bus/usb/devices. | 
|  | 107 | * Device names are made up of numbers, dots, and dashes, e.g., '7-1.5'. | 
|  | 108 | * We reject interfaces (e.g., '7-1.5:1.0') and host controllers (e.g. 'usb1'). | 
|  | 109 | * The name must also start with a digit, to disallow '.' and '..' | 
|  | 110 | */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 | static inline int badname(const char *name) | 
|  | 112 | { | 
| Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 113 | if (!isdigit(*name)) | 
|  | 114 | return 1; | 
|  | 115 | while(*++name) { | 
|  | 116 | if(!isdigit(*name) && *name != '.' && *name != '-') | 
|  | 117 | return 1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | } | 
|  | 119 | return 0; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | static int check(void *_desc, int len, unsigned type, int size) | 
|  | 123 | { | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 124 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)_desc; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 125 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | if(len < size) return -1; | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 127 | if(hdr->bLength < size) return -1; | 
|  | 128 | if(hdr->bLength > len) return -1; | 
|  | 129 | if(hdr->bDescriptorType != type) return -1; | 
| 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 | return 0; | 
|  | 132 | } | 
|  | 133 |  | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 134 | static int filter_usb_device(char* sysfs_name, | 
| Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 135 | char *ptr, int len, int writable, | 
| Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 136 | ifc_match_func callback, | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | int *ept_in_id, int *ept_out_id, int *ifc_id) | 
|  | 138 | { | 
|  | 139 | struct usb_device_descriptor *dev; | 
|  | 140 | struct usb_config_descriptor *cfg; | 
|  | 141 | struct usb_interface_descriptor *ifc; | 
|  | 142 | struct usb_endpoint_descriptor *ept; | 
|  | 143 | struct usb_ifc_info info; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 144 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 | int in, out; | 
|  | 146 | unsigned i; | 
|  | 147 | unsigned e; | 
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 148 |  | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 149 | 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] | 150 | return -1; | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 151 | dev = (struct usb_device_descriptor *)ptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | len -= dev->bLength; | 
|  | 153 | ptr += dev->bLength; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 154 |  | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 155 | 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] | 156 | return -1; | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 157 | cfg = (struct usb_config_descriptor *)ptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 158 | len -= cfg->bLength; | 
|  | 159 | ptr += cfg->bLength; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 160 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | info.dev_vendor = dev->idVendor; | 
|  | 162 | info.dev_product = dev->idProduct; | 
|  | 163 | info.dev_class = dev->bDeviceClass; | 
|  | 164 | info.dev_subclass = dev->bDeviceSubClass; | 
|  | 165 | info.dev_protocol = dev->bDeviceProtocol; | 
| Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 166 | info.writable = writable; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 167 |  | 
| Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 168 | 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] | 169 |  | 
| Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 170 | /* Read device serial number (if there is one). | 
|  | 171 | * We read the serial number from sysfs, since it's faster and more | 
|  | 172 | * reliable than issuing a control pipe read, and also won't | 
|  | 173 | * cause problems for devices which don't like getting descriptor | 
|  | 174 | * requests while they're in the middle of flashing. | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 175 | */ | 
| Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 176 | info.serial_number[0] = '\0'; | 
|  | 177 | if (dev->iSerialNumber) { | 
|  | 178 | char path[80]; | 
|  | 179 | int fd; | 
|  | 180 |  | 
|  | 181 | snprintf(path, sizeof(path), | 
|  | 182 | "/sys/bus/usb/devices/%s/serial", sysfs_name); | 
|  | 183 | path[sizeof(path) - 1] = '\0'; | 
|  | 184 |  | 
|  | 185 | fd = open(path, O_RDONLY); | 
|  | 186 | if (fd >= 0) { | 
|  | 187 | int chars_read = read(fd, info.serial_number, | 
|  | 188 | sizeof(info.serial_number) - 1); | 
|  | 189 | close(fd); | 
|  | 190 |  | 
|  | 191 | if (chars_read <= 0) | 
|  | 192 | info.serial_number[0] = '\0'; | 
|  | 193 | else if (info.serial_number[chars_read - 1] == '\n') { | 
|  | 194 | // strip trailing newline | 
|  | 195 | info.serial_number[chars_read - 1] = '\0'; | 
|  | 196 | } | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 197 | } | 
|  | 198 | } | 
|  | 199 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 | for(i = 0; i < cfg->bNumInterfaces; i++) { | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 201 |  | 
|  | 202 | while (len > 0) { | 
|  | 203 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr; | 
|  | 204 | if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE) == 0) | 
|  | 205 | break; | 
|  | 206 | len -= hdr->bLength; | 
|  | 207 | ptr += hdr->bLength; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | if (len <= 0) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | return -1; | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 212 |  | 
|  | 213 | ifc = (struct usb_interface_descriptor *)ptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | len -= ifc->bLength; | 
|  | 215 | ptr += ifc->bLength; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 216 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 217 | in = -1; | 
|  | 218 | out = -1; | 
|  | 219 | info.ifc_class = ifc->bInterfaceClass; | 
|  | 220 | info.ifc_subclass = ifc->bInterfaceSubClass; | 
|  | 221 | info.ifc_protocol = ifc->bInterfaceProtocol; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 222 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | for(e = 0; e < ifc->bNumEndpoints; e++) { | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 224 | while (len > 0) { | 
|  | 225 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr; | 
|  | 226 | if (check(hdr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE) == 0) | 
|  | 227 | break; | 
|  | 228 | len -= hdr->bLength; | 
|  | 229 | ptr += hdr->bLength; | 
|  | 230 | } | 
|  | 231 | if (len < 0) { | 
|  | 232 | break; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | ept = (struct usb_endpoint_descriptor *)ptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | len -= ept->bLength; | 
|  | 237 | ptr += ept->bLength; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 238 |  | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 239 | 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] | 240 | continue; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 241 |  | 
| Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 242 | if(ept->bEndpointAddress & USB_ENDPOINT_DIR_MASK) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | in = ept->bEndpointAddress; | 
|  | 244 | } else { | 
|  | 245 | out = ept->bEndpointAddress; | 
|  | 246 | } | 
| Jack Pham | 1c02213 | 2014-12-05 12:11:20 -0800 | [diff] [blame] | 247 |  | 
|  | 248 | // For USB 3.0 devices skip the SS Endpoint Companion descriptor | 
|  | 249 | if (check((struct usb_descriptor_hdr *)ptr, len, | 
|  | 250 | USB_DT_SS_ENDPOINT_COMP, USB_DT_SS_EP_COMP_SIZE) == 0) { | 
|  | 251 | len -= USB_DT_SS_EP_COMP_SIZE; | 
|  | 252 | ptr += USB_DT_SS_EP_COMP_SIZE; | 
|  | 253 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 254 | } | 
|  | 255 |  | 
|  | 256 | info.has_bulk_in = (in != -1); | 
|  | 257 | info.has_bulk_out = (out != -1); | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 258 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | if(callback(&info) == 0) { | 
|  | 260 | *ept_in_id = in; | 
|  | 261 | *ept_out_id = out; | 
|  | 262 | *ifc_id = ifc->bInterfaceNumber; | 
|  | 263 | return 0; | 
|  | 264 | } | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | return -1; | 
|  | 268 | } | 
|  | 269 |  | 
| Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 270 | static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node, | 
|  | 271 | char* buf, int bufsize) | 
|  | 272 | { | 
|  | 273 | char path[80]; | 
|  | 274 | int fd, n; | 
|  | 275 |  | 
|  | 276 | snprintf(path, sizeof(path), | 
|  | 277 | "/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node); | 
|  | 278 | path[sizeof(path) - 1] = '\0'; | 
|  | 279 |  | 
|  | 280 | fd = open(path, O_RDONLY); | 
|  | 281 | if (fd < 0) | 
|  | 282 | return -1; | 
|  | 283 |  | 
|  | 284 | n = read(fd, buf, bufsize - 1); | 
|  | 285 | close(fd); | 
|  | 286 |  | 
|  | 287 | if (n < 0) | 
|  | 288 | return -1; | 
|  | 289 |  | 
|  | 290 | buf[n] = '\0'; | 
|  | 291 |  | 
|  | 292 | return n; | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node) | 
|  | 296 | { | 
|  | 297 | char buf[16]; | 
|  | 298 | int value; | 
|  | 299 |  | 
|  | 300 | if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0) | 
|  | 301 | return -1; | 
|  | 302 |  | 
|  | 303 | if (sscanf(buf, "%d", &value) != 1) | 
|  | 304 | return -1; | 
|  | 305 |  | 
|  | 306 | return value; | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | /* Given the name of a USB device in sysfs, get the name for the same | 
|  | 310 | * device in devfs. Returns 0 for success, -1 for failure. | 
|  | 311 | */ | 
|  | 312 | static int convert_to_devfs_name(const char* sysfs_name, | 
|  | 313 | char* devname, int devname_size) | 
|  | 314 | { | 
|  | 315 | int busnum, devnum; | 
|  | 316 |  | 
|  | 317 | busnum = read_sysfs_number(sysfs_name, "busnum"); | 
|  | 318 | if (busnum < 0) | 
|  | 319 | return -1; | 
|  | 320 |  | 
|  | 321 | devnum = read_sysfs_number(sysfs_name, "devnum"); | 
|  | 322 | if (devnum < 0) | 
|  | 323 | return -1; | 
|  | 324 |  | 
|  | 325 | snprintf(devname, devname_size, "/dev/bus/usb/%03d/%03d", busnum, devnum); | 
|  | 326 | return 0; | 
|  | 327 | } | 
|  | 328 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 329 | static std::unique_ptr<usb_handle> find_usb_device(const char* base, ifc_match_func callback) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 331 | std::unique_ptr<usb_handle> usb; | 
| Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 332 | char devname[64]; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | char desc[1024]; | 
|  | 334 | int n, in, out, ifc; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 335 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | struct dirent *de; | 
|  | 337 | int fd; | 
| Elliott Hughes | c500be9 | 2009-10-07 17:24:39 -0700 | [diff] [blame] | 338 | int writable; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 339 |  | 
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 340 | std::unique_ptr<DIR, decltype(&closedir)> busdir(opendir(base), closedir); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 341 | if (busdir == 0) return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 342 |  | 
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 343 | while ((de = readdir(busdir.get())) && (usb == nullptr)) { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 344 | if (badname(de->d_name)) continue; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 345 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 346 | 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] | 347 |  | 
|  | 348 | //            DBG("[ scanning %s ]\n", devname); | 
| Elliott Hughes | c500be9 | 2009-10-07 17:24:39 -0700 | [diff] [blame] | 349 | writable = 1; | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 350 | if ((fd = open(devname, O_RDWR)) < 0) { | 
| Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 351 | // Check if we have read-only access, so we can give a helpful | 
|  | 352 | // diagnostic like "adb devices" does. | 
|  | 353 | writable = 0; | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 354 | if ((fd = open(devname, O_RDONLY)) < 0) { | 
| Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 355 | continue; | 
|  | 356 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | } | 
|  | 358 |  | 
|  | 359 | n = read(fd, desc, sizeof(desc)); | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 360 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 361 | if (filter_usb_device(de->d_name, desc, n, writable, callback, &in, &out, &ifc) == 0) { | 
|  | 362 | usb.reset(new usb_handle()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | strcpy(usb->fname, devname); | 
|  | 364 | usb->ep_in = in; | 
|  | 365 | usb->ep_out = out; | 
|  | 366 | usb->desc = fd; | 
|  | 367 |  | 
|  | 368 | n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 369 | if (n != 0) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 370 | close(fd); | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 371 | usb.reset(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 372 | continue; | 
|  | 373 | } | 
|  | 374 | } else { | 
|  | 375 | close(fd); | 
|  | 376 | } | 
|  | 377 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 |  | 
|  | 380 | return usb; | 
|  | 381 | } | 
|  | 382 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 383 | ssize_t LinuxUsbTransport::Write(const void* _data, size_t len) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 384 | { | 
|  | 385 | unsigned char *data = (unsigned char*) _data; | 
|  | 386 | unsigned count = 0; | 
|  | 387 | struct usbdevfs_bulktransfer bulk; | 
|  | 388 | int n; | 
|  | 389 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 390 | if (handle_->ep_out == 0 || handle_->desc == -1) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | return -1; | 
|  | 392 | } | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 393 |  | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 394 | do { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 395 | int xfer; | 
| David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 396 | xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 397 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 398 | bulk.ep = handle_->ep_out; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 399 | bulk.len = xfer; | 
|  | 400 | bulk.data = data; | 
|  | 401 | bulk.timeout = 0; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 402 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 403 | n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 404 | if(n != xfer) { | 
|  | 405 | DBG("ERROR: n = %d, errno = %d (%s)\n", | 
|  | 406 | n, errno, strerror(errno)); | 
|  | 407 | return -1; | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 | count += xfer; | 
|  | 411 | len -= xfer; | 
|  | 412 | data += xfer; | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 413 | } while(len > 0); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 414 |  | 
|  | 415 | return count; | 
|  | 416 | } | 
|  | 417 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 418 | ssize_t LinuxUsbTransport::Read(void* _data, size_t len) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | { | 
|  | 420 | unsigned char *data = (unsigned char*) _data; | 
|  | 421 | unsigned count = 0; | 
|  | 422 | struct usbdevfs_bulktransfer bulk; | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 423 | int n, retry; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 424 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 425 | if (handle_->ep_in == 0 || handle_->desc == -1) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | return -1; | 
|  | 427 | } | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 428 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | while(len > 0) { | 
| David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 430 | int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 431 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 432 | bulk.ep = handle_->ep_in; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 433 | bulk.len = xfer; | 
|  | 434 | bulk.data = data; | 
|  | 435 | bulk.timeout = 0; | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 436 | retry = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 |  | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 438 | do{ | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 439 | DBG("[ usb read %d fd = %d], fname=%s\n", xfer, handle_->desc, handle_->fname); | 
|  | 440 | n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk); | 
|  | 441 | DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, handle_->fname, retry); | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 442 |  | 
|  | 443 | if( n < 0 ) { | 
|  | 444 | DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); | 
|  | 445 | if ( ++retry > MAX_RETRIES ) return -1; | 
|  | 446 | sleep( 1 ); | 
|  | 447 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | } | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 449 | while( n < 0 ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 450 |  | 
|  | 451 | count += n; | 
|  | 452 | len -= n; | 
|  | 453 | data += n; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 454 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | if(n < xfer) { | 
|  | 456 | break; | 
|  | 457 | } | 
|  | 458 | } | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 459 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 460 | return count; | 
|  | 461 | } | 
|  | 462 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 463 | int LinuxUsbTransport::Close() | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 464 | { | 
|  | 465 | int fd; | 
|  | 466 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 467 | fd = handle_->desc; | 
|  | 468 | handle_->desc = -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | if(fd >= 0) { | 
|  | 470 | close(fd); | 
|  | 471 | DBG("[ usb closed %d ]\n", fd); | 
|  | 472 | } | 
|  | 473 |  | 
|  | 474 | return 0; | 
|  | 475 | } | 
|  | 476 |  | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 477 | Transport* usb_open(ifc_match_func callback) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 478 | { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 479 | std::unique_ptr<usb_handle> handle = find_usb_device("/sys/bus/usb/devices", callback); | 
|  | 480 | return handle ? new LinuxUsbTransport(std::move(handle)) : nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 481 | } | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 482 |  | 
|  | 483 | /* Wait for the system to notice the device is gone, so that a subsequent | 
|  | 484 | * fastboot command won't try to access the device before it's rebooted. | 
|  | 485 | * Returns 0 for success, -1 for timeout. | 
|  | 486 | */ | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 487 | int LinuxUsbTransport::WaitForDisconnect() | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 488 | { | 
|  | 489 | double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT; | 
|  | 490 | while (now() < deadline) { | 
| David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 491 | if (access(handle_->fname, F_OK)) | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 492 | return 0; | 
|  | 493 | usleep(50000); | 
|  | 494 | } | 
|  | 495 | return -1; | 
|  | 496 | } |