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. |
Peter Collingbourne | ab8cc2f | 2024-02-13 15:40:37 -0800 | [diff] [blame^] | 86 | // 128KiB was experimentally found to be enough to saturate the bus at |
| 87 | // SuperSpeed+, so we first try double that for writes. If the operation fails |
| 88 | // due to a lack of contiguous regions (or an ancient kernel), try smaller sizes |
| 89 | // until we find one that works (see LinuxUsbTransport::Write). Reads are less |
| 90 | // performance critical so for now just use a known good size. |
| 91 | #define MAX_USBFS_BULK_WRITE_SIZE (256 * 1024) |
| 92 | #define MAX_USBFS_BULK_READ_SIZE (16 * 1024) |
| 93 | |
| 94 | // This size should pretty much always work (it's compatible with pre-3.3 |
| 95 | // kernels and it's what we used to use historically), so if it doesn't work |
| 96 | // something has gone badly wrong. |
| 97 | #define MIN_USBFS_BULK_WRITE_SIZE (16 * 1024) |
David Krause | 913eb8b | 2011-03-08 14:10:16 +0800 | [diff] [blame] | 98 | |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 99 | struct usb_handle |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | { |
| 101 | char fname[64]; |
| 102 | int desc; |
| 103 | unsigned char ep_in; |
| 104 | unsigned char ep_out; |
| 105 | }; |
| 106 | |
Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 107 | class LinuxUsbTransport : public UsbTransport { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 108 | public: |
Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 109 | explicit LinuxUsbTransport(std::unique_ptr<usb_handle> handle, uint32_t ms_timeout = 0) |
| 110 | : handle_(std::move(handle)), ms_timeout_(ms_timeout) {} |
David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 111 | ~LinuxUsbTransport() override; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 112 | |
| 113 | ssize_t Read(void* data, size_t len) override; |
| 114 | ssize_t Write(const void* data, size_t len) override; |
| 115 | int Close() override; |
Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 116 | int Reset() override; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 117 | int WaitForDisconnect() override; |
| 118 | |
| 119 | private: |
| 120 | std::unique_ptr<usb_handle> handle_; |
Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 121 | const uint32_t ms_timeout_; |
Peter Collingbourne | ab8cc2f | 2024-02-13 15:40:37 -0800 | [diff] [blame^] | 122 | size_t max_usbfs_bulk_write_size_ = MAX_USBFS_BULK_WRITE_SIZE; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(LinuxUsbTransport); |
| 125 | }; |
| 126 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 127 | /* True if name isn't a valid name for a USB device in /sys/bus/usb/devices. |
| 128 | * Device names are made up of numbers, dots, and dashes, e.g., '7-1.5'. |
| 129 | * We reject interfaces (e.g., '7-1.5:1.0') and host controllers (e.g. 'usb1'). |
| 130 | * The name must also start with a digit, to disallow '.' and '..' |
| 131 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | static inline int badname(const char *name) |
| 133 | { |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 134 | if (!isdigit(*name)) |
| 135 | return 1; |
| 136 | while(*++name) { |
| 137 | if(!isdigit(*name) && *name != '.' && *name != '-') |
| 138 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int check(void *_desc, int len, unsigned type, int size) |
| 144 | { |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 145 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)_desc; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 146 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | if(len < size) return -1; |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 148 | if(hdr->bLength < size) return -1; |
| 149 | if(hdr->bLength > len) return -1; |
| 150 | if(hdr->bDescriptorType != type) return -1; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 151 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 155 | static int filter_usb_device(char* sysfs_name, |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 156 | char *ptr, int len, int writable, |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 157 | ifc_match_func callback, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 158 | int *ept_in_id, int *ept_out_id, int *ifc_id) |
| 159 | { |
| 160 | struct usb_device_descriptor *dev; |
| 161 | struct usb_config_descriptor *cfg; |
| 162 | struct usb_interface_descriptor *ifc; |
| 163 | struct usb_endpoint_descriptor *ept; |
| 164 | struct usb_ifc_info info; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 165 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | int in, out; |
| 167 | unsigned i; |
| 168 | unsigned e; |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 169 | |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 170 | 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] | 171 | return -1; |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 172 | dev = (struct usb_device_descriptor *)ptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | len -= dev->bLength; |
| 174 | ptr += dev->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 175 | |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 176 | 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] | 177 | return -1; |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 178 | cfg = (struct usb_config_descriptor *)ptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | len -= cfg->bLength; |
| 180 | ptr += cfg->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 181 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 182 | info.dev_vendor = dev->idVendor; |
| 183 | info.dev_product = dev->idProduct; |
| 184 | info.dev_class = dev->bDeviceClass; |
| 185 | info.dev_subclass = dev->bDeviceSubClass; |
| 186 | info.dev_protocol = dev->bDeviceProtocol; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 187 | info.writable = writable; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 188 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 189 | 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] | 190 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 191 | /* Read device serial number (if there is one). |
| 192 | * We read the serial number from sysfs, since it's faster and more |
| 193 | * reliable than issuing a control pipe read, and also won't |
| 194 | * cause problems for devices which don't like getting descriptor |
| 195 | * requests while they're in the middle of flashing. |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 196 | */ |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 197 | info.serial_number[0] = '\0'; |
| 198 | if (dev->iSerialNumber) { |
| 199 | char path[80]; |
| 200 | int fd; |
| 201 | |
| 202 | snprintf(path, sizeof(path), |
| 203 | "/sys/bus/usb/devices/%s/serial", sysfs_name); |
| 204 | path[sizeof(path) - 1] = '\0'; |
| 205 | |
| 206 | fd = open(path, O_RDONLY); |
| 207 | if (fd >= 0) { |
| 208 | int chars_read = read(fd, info.serial_number, |
| 209 | sizeof(info.serial_number) - 1); |
| 210 | close(fd); |
| 211 | |
| 212 | if (chars_read <= 0) |
| 213 | info.serial_number[0] = '\0'; |
| 214 | else if (info.serial_number[chars_read - 1] == '\n') { |
| 215 | // strip trailing newline |
| 216 | info.serial_number[chars_read - 1] = '\0'; |
| 217 | } |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | for(i = 0; i < cfg->bNumInterfaces; i++) { |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 222 | |
| 223 | while (len > 0) { |
| 224 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr; |
| 225 | if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE) == 0) |
| 226 | break; |
| 227 | len -= hdr->bLength; |
| 228 | ptr += hdr->bLength; |
| 229 | } |
| 230 | |
| 231 | if (len <= 0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 232 | return -1; |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 233 | |
| 234 | ifc = (struct usb_interface_descriptor *)ptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | len -= ifc->bLength; |
| 236 | ptr += ifc->bLength; |
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 | in = -1; |
| 239 | out = -1; |
| 240 | info.ifc_class = ifc->bInterfaceClass; |
| 241 | info.ifc_subclass = ifc->bInterfaceSubClass; |
| 242 | info.ifc_protocol = ifc->bInterfaceProtocol; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 243 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 244 | for(e = 0; e < ifc->bNumEndpoints; e++) { |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 245 | while (len > 0) { |
| 246 | struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr; |
| 247 | if (check(hdr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE) == 0) |
| 248 | break; |
| 249 | len -= hdr->bLength; |
| 250 | ptr += hdr->bLength; |
| 251 | } |
| 252 | if (len < 0) { |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | ept = (struct usb_endpoint_descriptor *)ptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 257 | len -= ept->bLength; |
| 258 | ptr += ept->bLength; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 259 | |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 260 | 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] | 261 | continue; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 262 | |
Patrick Tjin | aac89db | 2014-07-11 08:59:01 -0700 | [diff] [blame] | 263 | if(ept->bEndpointAddress & USB_ENDPOINT_DIR_MASK) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | in = ept->bEndpointAddress; |
| 265 | } else { |
| 266 | out = ept->bEndpointAddress; |
| 267 | } |
Jack Pham | 1c02213 | 2014-12-05 12:11:20 -0800 | [diff] [blame] | 268 | |
| 269 | // For USB 3.0 devices skip the SS Endpoint Companion descriptor |
| 270 | if (check((struct usb_descriptor_hdr *)ptr, len, |
| 271 | USB_DT_SS_ENDPOINT_COMP, USB_DT_SS_EP_COMP_SIZE) == 0) { |
| 272 | len -= USB_DT_SS_EP_COMP_SIZE; |
| 273 | ptr += USB_DT_SS_EP_COMP_SIZE; |
| 274 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | info.has_bulk_in = (in != -1); |
| 278 | info.has_bulk_out = (out != -1); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 279 | |
David Anderson | 4e058ca | 2020-05-28 04:39:37 +0000 | [diff] [blame] | 280 | std::string interface; |
| 281 | auto path = android::base::StringPrintf("/sys/bus/usb/devices/%s/%s:1.%d/interface", |
| 282 | sysfs_name, sysfs_name, ifc->bInterfaceNumber); |
| 283 | if (android::base::ReadFileToString(path, &interface)) { |
| 284 | snprintf(info.interface, sizeof(info.interface), "%s", interface.c_str()); |
| 285 | } |
| 286 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | if(callback(&info) == 0) { |
| 288 | *ept_in_id = in; |
| 289 | *ept_out_id = out; |
| 290 | *ifc_id = ifc->bInterfaceNumber; |
| 291 | return 0; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return -1; |
| 296 | } |
| 297 | |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 298 | static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node, |
| 299 | char* buf, int bufsize) |
| 300 | { |
| 301 | char path[80]; |
| 302 | int fd, n; |
| 303 | |
| 304 | snprintf(path, sizeof(path), |
| 305 | "/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node); |
| 306 | path[sizeof(path) - 1] = '\0'; |
| 307 | |
| 308 | fd = open(path, O_RDONLY); |
| 309 | if (fd < 0) |
| 310 | return -1; |
| 311 | |
| 312 | n = read(fd, buf, bufsize - 1); |
| 313 | close(fd); |
| 314 | |
| 315 | if (n < 0) |
| 316 | return -1; |
| 317 | |
| 318 | buf[n] = '\0'; |
| 319 | |
| 320 | return n; |
| 321 | } |
| 322 | |
| 323 | static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node) |
| 324 | { |
| 325 | char buf[16]; |
| 326 | int value; |
| 327 | |
| 328 | if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0) |
| 329 | return -1; |
| 330 | |
| 331 | if (sscanf(buf, "%d", &value) != 1) |
| 332 | return -1; |
| 333 | |
| 334 | return value; |
| 335 | } |
| 336 | |
| 337 | /* Given the name of a USB device in sysfs, get the name for the same |
| 338 | * device in devfs. Returns 0 for success, -1 for failure. |
| 339 | */ |
| 340 | static int convert_to_devfs_name(const char* sysfs_name, |
| 341 | char* devname, int devname_size) |
| 342 | { |
| 343 | int busnum, devnum; |
| 344 | |
| 345 | busnum = read_sysfs_number(sysfs_name, "busnum"); |
| 346 | if (busnum < 0) |
| 347 | return -1; |
| 348 | |
| 349 | devnum = read_sysfs_number(sysfs_name, "devnum"); |
| 350 | if (devnum < 0) |
| 351 | return -1; |
| 352 | |
| 353 | snprintf(devname, devname_size, "/dev/bus/usb/%03d/%03d", busnum, devnum); |
| 354 | return 0; |
| 355 | } |
| 356 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 357 | 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] | 358 | { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 359 | std::unique_ptr<usb_handle> usb; |
Mark Wachsler | bd446c7 | 2013-09-11 18:23:31 -0400 | [diff] [blame] | 360 | char devname[64]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | char desc[1024]; |
| 362 | int n, in, out, ifc; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 363 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 364 | struct dirent *de; |
| 365 | int fd; |
Elliott Hughes | c500be9 | 2009-10-07 17:24:39 -0700 | [diff] [blame] | 366 | int writable; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 367 | |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 368 | std::unique_ptr<DIR, decltype(&closedir)> busdir(opendir(base), closedir); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 369 | if (busdir == 0) return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 370 | |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 371 | while ((de = readdir(busdir.get())) && (usb == nullptr)) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 372 | if (badname(de->d_name)) continue; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 373 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 374 | 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] | 375 | |
| 376 | // DBG("[ scanning %s ]\n", devname); |
Elliott Hughes | c500be9 | 2009-10-07 17:24:39 -0700 | [diff] [blame] | 377 | writable = 1; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 378 | if ((fd = open(devname, O_RDWR)) < 0) { |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 379 | // Check if we have read-only access, so we can give a helpful |
| 380 | // diagnostic like "adb devices" does. |
| 381 | writable = 0; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 382 | if ((fd = open(devname, O_RDONLY)) < 0) { |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 383 | continue; |
| 384 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | n = read(fd, desc, sizeof(desc)); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 388 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 389 | if (filter_usb_device(de->d_name, desc, n, writable, callback, &in, &out, &ifc) == 0) { |
| 390 | usb.reset(new usb_handle()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | strcpy(usb->fname, devname); |
| 392 | usb->ep_in = in; |
| 393 | usb->ep_out = out; |
| 394 | usb->desc = fd; |
| 395 | |
| 396 | n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 397 | if (n != 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | close(fd); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 399 | usb.reset(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | } else { |
| 403 | close(fd); |
| 404 | } |
| 405 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 406 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | |
| 408 | return usb; |
| 409 | } |
| 410 | |
David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 411 | LinuxUsbTransport::~LinuxUsbTransport() { |
| 412 | Close(); |
| 413 | } |
| 414 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 415 | 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] | 416 | { |
| 417 | unsigned char *data = (unsigned char*) _data; |
| 418 | unsigned count = 0; |
Peter Collingbourne | 8635c01 | 2024-02-05 14:59:44 -0800 | [diff] [blame] | 419 | struct usbdevfs_urb urb[2] = {}; |
| 420 | bool pending[2] = {}; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 422 | if (handle_->ep_out == 0 || handle_->desc == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | return -1; |
| 424 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 425 | |
Peter Collingbourne | 8635c01 | 2024-02-05 14:59:44 -0800 | [diff] [blame] | 426 | auto submit_urb = [&](size_t i) { |
Peter Collingbourne | ab8cc2f | 2024-02-13 15:40:37 -0800 | [diff] [blame^] | 427 | while (true) { |
| 428 | int xfer = (len > max_usbfs_bulk_write_size_) ? max_usbfs_bulk_write_size_ : len; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 429 | |
Peter Collingbourne | ab8cc2f | 2024-02-13 15:40:37 -0800 | [diff] [blame^] | 430 | urb[i].type = USBDEVFS_URB_TYPE_BULK; |
| 431 | urb[i].endpoint = handle_->ep_out; |
| 432 | urb[i].buffer_length = xfer; |
| 433 | urb[i].buffer = data; |
| 434 | urb[i].usercontext = (void *)i; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 435 | |
Peter Collingbourne | ab8cc2f | 2024-02-13 15:40:37 -0800 | [diff] [blame^] | 436 | int n = ioctl(handle_->desc, USBDEVFS_SUBMITURB, &urb[i]); |
| 437 | if (n != 0) { |
| 438 | if (errno == ENOMEM && max_usbfs_bulk_write_size_ > MIN_USBFS_BULK_WRITE_SIZE) { |
| 439 | max_usbfs_bulk_write_size_ /= 2; |
| 440 | continue; |
| 441 | } |
| 442 | DBG("ioctl(USBDEVFS_SUBMITURB) failed\n"); |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | pending[i] = true; |
| 447 | count += xfer; |
| 448 | len -= xfer; |
| 449 | data += xfer; |
| 450 | |
| 451 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | } |
Peter Collingbourne | 8635c01 | 2024-02-05 14:59:44 -0800 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | auto reap_urb = [&](size_t i) { |
| 456 | while (pending[i]) { |
| 457 | struct usbdevfs_urb *urbp; |
| 458 | int res = ioctl(handle_->desc, USBDEVFS_REAPURB, &urbp); |
| 459 | if (res != 0) { |
| 460 | DBG("ioctl(USBDEVFS_REAPURB) failed\n"); |
| 461 | return false; |
| 462 | } |
| 463 | size_t done = (size_t)urbp->usercontext; |
| 464 | if (done >= 2 || !pending[done]) { |
| 465 | DBG("unexpected urb\n"); |
| 466 | return false; |
| 467 | } |
| 468 | if (urbp->status != 0 || urbp->actual_length != urbp->buffer_length) { |
| 469 | DBG("urb returned error\n"); |
| 470 | return false; |
| 471 | } |
| 472 | pending[done] = false; |
| 473 | } |
| 474 | return true; |
| 475 | }; |
| 476 | |
| 477 | if (!submit_urb(0)) { |
| 478 | return -1; |
| 479 | } |
| 480 | while (len > 0) { |
| 481 | if (!submit_urb(1)) { |
| 482 | return -1; |
| 483 | } |
| 484 | if (!reap_urb(0)) { |
| 485 | return -1; |
| 486 | } |
| 487 | if (len <= 0) { |
| 488 | if (!reap_urb(1)) { |
| 489 | return -1; |
| 490 | } |
| 491 | return count; |
| 492 | } |
| 493 | if (!submit_urb(0)) { |
| 494 | return -1; |
| 495 | } |
| 496 | if (!reap_urb(1)) { |
| 497 | return -1; |
| 498 | } |
| 499 | } |
| 500 | if (!reap_urb(0)) { |
| 501 | return -1; |
| 502 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 503 | return count; |
| 504 | } |
| 505 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 506 | ssize_t LinuxUsbTransport::Read(void* _data, size_t len) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 507 | { |
| 508 | unsigned char *data = (unsigned char*) _data; |
| 509 | unsigned count = 0; |
| 510 | struct usbdevfs_bulktransfer bulk; |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 511 | int n, retry; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 512 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 513 | if (handle_->ep_in == 0 || handle_->desc == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 514 | return -1; |
| 515 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 516 | |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 517 | while (len > 0) { |
Peter Collingbourne | ab8cc2f | 2024-02-13 15:40:37 -0800 | [diff] [blame^] | 518 | int xfer = (len > MAX_USBFS_BULK_READ_SIZE) ? MAX_USBFS_BULK_READ_SIZE : len; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 519 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 520 | bulk.ep = handle_->ep_in; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 521 | bulk.len = xfer; |
| 522 | bulk.data = data; |
Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 523 | bulk.timeout = ms_timeout_; |
Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 524 | retry = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 525 | |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 526 | do { |
| 527 | DBG("[ usb read %d fd = %d], fname=%s\n", xfer, handle_->desc, handle_->fname); |
| 528 | n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk); |
| 529 | 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] | 530 | |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 531 | if (n < 0) { |
| 532 | DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); |
| 533 | if (++retry > MAX_RETRIES) return -1; |
Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 534 | std::this_thread::sleep_for(100ms); |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 535 | } |
| 536 | } while (n < 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 537 | |
| 538 | count += n; |
| 539 | len -= n; |
| 540 | data += n; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 541 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 542 | if(n < xfer) { |
| 543 | break; |
| 544 | } |
| 545 | } |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 546 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 547 | return count; |
| 548 | } |
| 549 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 550 | int LinuxUsbTransport::Close() |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 551 | { |
| 552 | int fd; |
| 553 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 554 | fd = handle_->desc; |
| 555 | handle_->desc = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 556 | if(fd >= 0) { |
| 557 | close(fd); |
| 558 | DBG("[ usb closed %d ]\n", fd); |
| 559 | } |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
Aaron Wisner | acf78d4 | 2018-07-26 10:56:09 -0500 | [diff] [blame] | 564 | int LinuxUsbTransport::Reset() { |
| 565 | int ret = 0; |
| 566 | // We reset the USB connection |
| 567 | if ((ret = ioctl(handle_->desc, USBDEVFS_RESET, 0))) { |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
Dmitrii Merkurev | 0b627d9 | 2023-09-03 17:30:46 +0100 | [diff] [blame] | 574 | std::unique_ptr<UsbTransport> usb_open(ifc_match_func callback, uint32_t timeout_ms) { |
| 575 | std::unique_ptr<UsbTransport> result; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 576 | std::unique_ptr<usb_handle> handle = find_usb_device("/sys/bus/usb/devices", callback); |
Dmitrii Merkurev | 0b627d9 | 2023-09-03 17:30:46 +0100 | [diff] [blame] | 577 | |
| 578 | if (handle) { |
| 579 | result = std::make_unique<LinuxUsbTransport>(std::move(handle), timeout_ms); |
| 580 | } |
| 581 | |
| 582 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 583 | } |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 584 | |
| 585 | /* Wait for the system to notice the device is gone, so that a subsequent |
| 586 | * fastboot command won't try to access the device before it's rebooted. |
| 587 | * Returns 0 for success, -1 for timeout. |
| 588 | */ |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 589 | int LinuxUsbTransport::WaitForDisconnect() |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 590 | { |
| 591 | double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT; |
| 592 | while (now() < deadline) { |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 593 | if (access(handle_->fname, F_OK)) return 0; |
| 594 | std::this_thread::sleep_for(50ms); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 595 | } |
| 596 | return -1; |
| 597 | } |