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