blob: 6363aa547a6683602b4eee802e7afc45baf97568 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Pomazau5ae3f932012-02-28 07:21:08 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * 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 Pomazau5ae3f932012-02-28 07:21:08 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * 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 Hughes93f65fa2015-07-24 14:09:44 -070029#include <ctype.h>
30#include <dirent.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <pthread.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034#include <stdio.h>
35#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <sys/ioctl.h>
Scott Anderson13081c62012-04-06 12:39:30 -070038#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#include <sys/types.h>
Elliott Hughes93f65fa2015-07-24 14:09:44 -070040#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
42#include <linux/usbdevice_fs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#include <linux/version.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#include <linux/usb/ch9.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
David Pursell0b156632015-10-30 11:22:01 -070047#include <memory>
Elliott Hughes290a2282016-11-14 17:08:47 -080048#include <thread>
David Pursell0b156632015-10-30 11:22:01 -070049
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#include "usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050051#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Elliott Hughes290a2282016-11-14 17:08:47 -080053using namespace std::chrono_literals;
54
Aaron Wisneracf78d42018-07-26 10:56:09 -050055#define MAX_RETRIES 2
Dan Murphyb2de4db2009-08-18 09:41:09 -050056
Mark Wachsler157b0012013-10-02 09:35:38 -040057/* Timeout in seconds for usb_wait_for_disconnect.
58 * It doesn't usually take long for a device to disconnect (almost always
59 * under 2 seconds) but we'll time out after 3 seconds just in case.
60 */
61#define WAIT_FOR_DISCONNECT_TIMEOUT 3
62
Dan Murphyb2de4db2009-08-18 09:41:09 -050063#ifdef TRACE_USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064#define DBG1(x...) fprintf(stderr, x)
65#define DBG(x...) fprintf(stderr, x)
66#else
67#define DBG(x...)
68#define DBG1(x...)
69#endif
70
Elliott Hughes93f65fa2015-07-24 14:09:44 -070071// Kernels before 3.3 have a 16KiB transfer limit. That limit was replaced
72// with a 16MiB global limit in 3.3, but each URB submitted required a
73// contiguous kernel allocation, so you would get ENOMEM if you tried to
74// send something larger than the biggest available contiguous kernel
75// memory region. 256KiB contiguous allocations are generally not reliable
76// on a device kernel that has been running for a while fragmenting its
77// memory, but that shouldn't be a problem for fastboot on the host.
78// In 3.6, the contiguous buffer limit was removed by allocating multiple
79// 16KiB chunks and having the USB driver stitch them back together while
80// transmitting using a scatter-gather list, so 256KiB bulk transfers should
81// be reliable.
82// 256KiB seems to work, but 1MiB bulk transfers lock up my z620 with a 3.13
83// kernel.
David Krause913eb8b2011-03-08 14:10:16 +080084#define MAX_USBFS_BULK_SIZE (16 * 1024)
85
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080086struct usb_handle
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087{
88 char fname[64];
89 int desc;
90 unsigned char ep_in;
91 unsigned char ep_out;
92};
93
Aaron Wisneracf78d42018-07-26 10:56:09 -050094class LinuxUsbTransport : public UsbTransport {
David Pursell0b156632015-10-30 11:22:01 -070095 public:
Aaron Wisneracf78d42018-07-26 10:56:09 -050096 explicit LinuxUsbTransport(std::unique_ptr<usb_handle> handle, uint32_t ms_timeout = 0)
97 : handle_(std::move(handle)), ms_timeout_(ms_timeout) {}
David Anderson03de6452018-09-04 14:32:54 -070098 ~LinuxUsbTransport() override;
David Pursell0b156632015-10-30 11:22:01 -070099
100 ssize_t Read(void* data, size_t len) override;
101 ssize_t Write(const void* data, size_t len) override;
102 int Close() override;
Aaron Wisneracf78d42018-07-26 10:56:09 -0500103 int Reset() override;
David Pursell0b156632015-10-30 11:22:01 -0700104 int WaitForDisconnect() override;
105
106 private:
107 std::unique_ptr<usb_handle> handle_;
Aaron Wisneracf78d42018-07-26 10:56:09 -0500108 const uint32_t ms_timeout_;
David Pursell0b156632015-10-30 11:22:01 -0700109
110 DISALLOW_COPY_AND_ASSIGN(LinuxUsbTransport);
111};
112
Mark Wachslerbd446c72013-09-11 18:23:31 -0400113/* True if name isn't a valid name for a USB device in /sys/bus/usb/devices.
114 * Device names are made up of numbers, dots, and dashes, e.g., '7-1.5'.
115 * We reject interfaces (e.g., '7-1.5:1.0') and host controllers (e.g. 'usb1').
116 * The name must also start with a digit, to disallow '.' and '..'
117 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118static inline int badname(const char *name)
119{
Mark Wachslerbd446c72013-09-11 18:23:31 -0400120 if (!isdigit(*name))
121 return 1;
122 while(*++name) {
123 if(!isdigit(*name) && *name != '.' && *name != '-')
124 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 }
126 return 0;
127}
128
129static int check(void *_desc, int len, unsigned type, int size)
130{
Patrick Tjinaac89db2014-07-11 08:59:01 -0700131 struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)_desc;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800132
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 if(len < size) return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700134 if(hdr->bLength < size) return -1;
135 if(hdr->bLength > len) return -1;
136 if(hdr->bDescriptorType != type) return -1;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 return 0;
139}
140
Mark Wachsler157b0012013-10-02 09:35:38 -0400141static int filter_usb_device(char* sysfs_name,
Mark Wachslerbd446c72013-09-11 18:23:31 -0400142 char *ptr, int len, int writable,
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700143 ifc_match_func callback,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 int *ept_in_id, int *ept_out_id, int *ifc_id)
145{
146 struct usb_device_descriptor *dev;
147 struct usb_config_descriptor *cfg;
148 struct usb_interface_descriptor *ifc;
149 struct usb_endpoint_descriptor *ept;
150 struct usb_ifc_info info;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800151
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 int in, out;
153 unsigned i;
154 unsigned e;
James Hawkins588a2ca2016-02-18 14:52:46 -0800155
Patrick Tjinaac89db2014-07-11 08:59:01 -0700156 if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700158 dev = (struct usb_device_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 len -= dev->bLength;
160 ptr += dev->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800161
Patrick Tjinaac89db2014-07-11 08:59:01 -0700162 if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700164 cfg = (struct usb_config_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 len -= cfg->bLength;
166 ptr += cfg->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800167
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 info.dev_vendor = dev->idVendor;
169 info.dev_product = dev->idProduct;
170 info.dev_class = dev->bDeviceClass;
171 info.dev_subclass = dev->bDeviceSubClass;
172 info.dev_protocol = dev->bDeviceProtocol;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700173 info.writable = writable;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800174
Mark Wachslerbd446c72013-09-11 18:23:31 -0400175 snprintf(info.device_path, sizeof(info.device_path), "usb:%s", sysfs_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Mark Wachslerbd446c72013-09-11 18:23:31 -0400177 /* Read device serial number (if there is one).
178 * We read the serial number from sysfs, since it's faster and more
179 * reliable than issuing a control pipe read, and also won't
180 * cause problems for devices which don't like getting descriptor
181 * requests while they're in the middle of flashing.
Scott Anderson13081c62012-04-06 12:39:30 -0700182 */
Mark Wachslerbd446c72013-09-11 18:23:31 -0400183 info.serial_number[0] = '\0';
184 if (dev->iSerialNumber) {
185 char path[80];
186 int fd;
187
188 snprintf(path, sizeof(path),
189 "/sys/bus/usb/devices/%s/serial", sysfs_name);
190 path[sizeof(path) - 1] = '\0';
191
192 fd = open(path, O_RDONLY);
193 if (fd >= 0) {
194 int chars_read = read(fd, info.serial_number,
195 sizeof(info.serial_number) - 1);
196 close(fd);
197
198 if (chars_read <= 0)
199 info.serial_number[0] = '\0';
200 else if (info.serial_number[chars_read - 1] == '\n') {
201 // strip trailing newline
202 info.serial_number[chars_read - 1] = '\0';
203 }
Scott Anderson13081c62012-04-06 12:39:30 -0700204 }
205 }
206
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 for(i = 0; i < cfg->bNumInterfaces; i++) {
Patrick Tjinaac89db2014-07-11 08:59:01 -0700208
209 while (len > 0) {
210 struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr;
211 if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE) == 0)
212 break;
213 len -= hdr->bLength;
214 ptr += hdr->bLength;
215 }
216
217 if (len <= 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700219
220 ifc = (struct usb_interface_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 len -= ifc->bLength;
222 ptr += ifc->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800223
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 in = -1;
225 out = -1;
226 info.ifc_class = ifc->bInterfaceClass;
227 info.ifc_subclass = ifc->bInterfaceSubClass;
228 info.ifc_protocol = ifc->bInterfaceProtocol;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800229
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 for(e = 0; e < ifc->bNumEndpoints; e++) {
Patrick Tjinaac89db2014-07-11 08:59:01 -0700231 while (len > 0) {
232 struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr;
233 if (check(hdr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE) == 0)
234 break;
235 len -= hdr->bLength;
236 ptr += hdr->bLength;
237 }
238 if (len < 0) {
239 break;
240 }
241
242 ept = (struct usb_endpoint_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 len -= ept->bLength;
244 ptr += ept->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800245
Patrick Tjinaac89db2014-07-11 08:59:01 -0700246 if((ept->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 continue;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800248
Patrick Tjinaac89db2014-07-11 08:59:01 -0700249 if(ept->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 in = ept->bEndpointAddress;
251 } else {
252 out = ept->bEndpointAddress;
253 }
Jack Pham1c022132014-12-05 12:11:20 -0800254
255 // For USB 3.0 devices skip the SS Endpoint Companion descriptor
256 if (check((struct usb_descriptor_hdr *)ptr, len,
257 USB_DT_SS_ENDPOINT_COMP, USB_DT_SS_EP_COMP_SIZE) == 0) {
258 len -= USB_DT_SS_EP_COMP_SIZE;
259 ptr += USB_DT_SS_EP_COMP_SIZE;
260 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 }
262
263 info.has_bulk_in = (in != -1);
264 info.has_bulk_out = (out != -1);
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800265
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 if(callback(&info) == 0) {
267 *ept_in_id = in;
268 *ept_out_id = out;
269 *ifc_id = ifc->bInterfaceNumber;
270 return 0;
271 }
272 }
273
274 return -1;
275}
276
Mark Wachslerbd446c72013-09-11 18:23:31 -0400277static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node,
278 char* buf, int bufsize)
279{
280 char path[80];
281 int fd, n;
282
283 snprintf(path, sizeof(path),
284 "/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node);
285 path[sizeof(path) - 1] = '\0';
286
287 fd = open(path, O_RDONLY);
288 if (fd < 0)
289 return -1;
290
291 n = read(fd, buf, bufsize - 1);
292 close(fd);
293
294 if (n < 0)
295 return -1;
296
297 buf[n] = '\0';
298
299 return n;
300}
301
302static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node)
303{
304 char buf[16];
305 int value;
306
307 if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0)
308 return -1;
309
310 if (sscanf(buf, "%d", &value) != 1)
311 return -1;
312
313 return value;
314}
315
316/* Given the name of a USB device in sysfs, get the name for the same
317 * device in devfs. Returns 0 for success, -1 for failure.
318 */
319static int convert_to_devfs_name(const char* sysfs_name,
320 char* devname, int devname_size)
321{
322 int busnum, devnum;
323
324 busnum = read_sysfs_number(sysfs_name, "busnum");
325 if (busnum < 0)
326 return -1;
327
328 devnum = read_sysfs_number(sysfs_name, "devnum");
329 if (devnum < 0)
330 return -1;
331
332 snprintf(devname, devname_size, "/dev/bus/usb/%03d/%03d", busnum, devnum);
333 return 0;
334}
335
David Pursell0b156632015-10-30 11:22:01 -0700336static std::unique_ptr<usb_handle> find_usb_device(const char* base, ifc_match_func callback)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337{
David Pursell0b156632015-10-30 11:22:01 -0700338 std::unique_ptr<usb_handle> usb;
Mark Wachslerbd446c72013-09-11 18:23:31 -0400339 char devname[64];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 char desc[1024];
341 int n, in, out, ifc;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800342
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 struct dirent *de;
344 int fd;
Elliott Hughesc500be92009-10-07 17:24:39 -0700345 int writable;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800346
James Hawkins588a2ca2016-02-18 14:52:46 -0800347 std::unique_ptr<DIR, decltype(&closedir)> busdir(opendir(base), closedir);
David Pursell0b156632015-10-30 11:22:01 -0700348 if (busdir == 0) return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
James Hawkins588a2ca2016-02-18 14:52:46 -0800350 while ((de = readdir(busdir.get())) && (usb == nullptr)) {
David Pursell0b156632015-10-30 11:22:01 -0700351 if (badname(de->d_name)) continue;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800352
David Pursell0b156632015-10-30 11:22:01 -0700353 if (!convert_to_devfs_name(de->d_name, devname, sizeof(devname))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354
355// DBG("[ scanning %s ]\n", devname);
Elliott Hughesc500be92009-10-07 17:24:39 -0700356 writable = 1;
David Pursell0b156632015-10-30 11:22:01 -0700357 if ((fd = open(devname, O_RDWR)) < 0) {
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700358 // Check if we have read-only access, so we can give a helpful
359 // diagnostic like "adb devices" does.
360 writable = 0;
David Pursell0b156632015-10-30 11:22:01 -0700361 if ((fd = open(devname, O_RDONLY)) < 0) {
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700362 continue;
363 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 }
365
366 n = read(fd, desc, sizeof(desc));
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800367
David Pursell0b156632015-10-30 11:22:01 -0700368 if (filter_usb_device(de->d_name, desc, n, writable, callback, &in, &out, &ifc) == 0) {
369 usb.reset(new usb_handle());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 strcpy(usb->fname, devname);
371 usb->ep_in = in;
372 usb->ep_out = out;
373 usb->desc = fd;
374
375 n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc);
David Pursell0b156632015-10-30 11:22:01 -0700376 if (n != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 close(fd);
David Pursell0b156632015-10-30 11:22:01 -0700378 usb.reset();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 continue;
380 }
381 } else {
382 close(fd);
383 }
384 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386
387 return usb;
388}
389
David Anderson03de6452018-09-04 14:32:54 -0700390LinuxUsbTransport::~LinuxUsbTransport() {
391 Close();
392}
393
David Pursell0b156632015-10-30 11:22:01 -0700394ssize_t LinuxUsbTransport::Write(const void* _data, size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395{
396 unsigned char *data = (unsigned char*) _data;
397 unsigned count = 0;
398 struct usbdevfs_bulktransfer bulk;
399 int n;
400
David Pursell0b156632015-10-30 11:22:01 -0700401 if (handle_->ep_out == 0 || handle_->desc == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 return -1;
403 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800404
Mark Wachsler157b0012013-10-02 09:35:38 -0400405 do {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 int xfer;
David Krause913eb8b2011-03-08 14:10:16 +0800407 xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800408
David Pursell0b156632015-10-30 11:22:01 -0700409 bulk.ep = handle_->ep_out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 bulk.len = xfer;
411 bulk.data = data;
Aaron Wisneracf78d42018-07-26 10:56:09 -0500412 bulk.timeout = ms_timeout_;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800413
David Pursell0b156632015-10-30 11:22:01 -0700414 n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 if(n != xfer) {
416 DBG("ERROR: n = %d, errno = %d (%s)\n",
417 n, errno, strerror(errno));
418 return -1;
419 }
420
421 count += xfer;
422 len -= xfer;
423 data += xfer;
Mark Wachsler157b0012013-10-02 09:35:38 -0400424 } while(len > 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425
426 return count;
427}
428
David Pursell0b156632015-10-30 11:22:01 -0700429ssize_t LinuxUsbTransport::Read(void* _data, size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430{
431 unsigned char *data = (unsigned char*) _data;
432 unsigned count = 0;
433 struct usbdevfs_bulktransfer bulk;
Dan Murphyb2de4db2009-08-18 09:41:09 -0500434 int n, retry;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435
David Pursell0b156632015-10-30 11:22:01 -0700436 if (handle_->ep_in == 0 || handle_->desc == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 return -1;
438 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800439
Elliott Hughes290a2282016-11-14 17:08:47 -0800440 while (len > 0) {
David Krause913eb8b2011-03-08 14:10:16 +0800441 int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800442
David Pursell0b156632015-10-30 11:22:01 -0700443 bulk.ep = handle_->ep_in;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 bulk.len = xfer;
445 bulk.data = data;
Aaron Wisneracf78d42018-07-26 10:56:09 -0500446 bulk.timeout = ms_timeout_;
Dan Murphyb2de4db2009-08-18 09:41:09 -0500447 retry = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448
Elliott Hughes290a2282016-11-14 17:08:47 -0800449 do {
450 DBG("[ usb read %d fd = %d], fname=%s\n", xfer, handle_->desc, handle_->fname);
451 n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk);
452 DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, handle_->fname, retry);
Dan Murphyb2de4db2009-08-18 09:41:09 -0500453
Elliott Hughes290a2282016-11-14 17:08:47 -0800454 if (n < 0) {
455 DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno));
456 if (++retry > MAX_RETRIES) return -1;
Aaron Wisneracf78d42018-07-26 10:56:09 -0500457 std::this_thread::sleep_for(100ms);
Elliott Hughes290a2282016-11-14 17:08:47 -0800458 }
459 } while (n < 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460
461 count += n;
462 len -= n;
463 data += n;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800464
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 if(n < xfer) {
466 break;
467 }
468 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 return count;
471}
472
David Pursell0b156632015-10-30 11:22:01 -0700473int LinuxUsbTransport::Close()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474{
475 int fd;
476
David Pursell0b156632015-10-30 11:22:01 -0700477 fd = handle_->desc;
478 handle_->desc = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 if(fd >= 0) {
480 close(fd);
481 DBG("[ usb closed %d ]\n", fd);
482 }
483
484 return 0;
485}
486
Aaron Wisneracf78d42018-07-26 10:56:09 -0500487int LinuxUsbTransport::Reset() {
488 int ret = 0;
489 // We reset the USB connection
490 if ((ret = ioctl(handle_->desc, USBDEVFS_RESET, 0))) {
491 return ret;
492 }
493
494 return 0;
495}
496
497UsbTransport* usb_open(ifc_match_func callback, uint32_t timeout_ms) {
David Pursell0b156632015-10-30 11:22:01 -0700498 std::unique_ptr<usb_handle> handle = find_usb_device("/sys/bus/usb/devices", callback);
Aaron Wisneracf78d42018-07-26 10:56:09 -0500499 return handle ? new LinuxUsbTransport(std::move(handle), timeout_ms) : nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500}
Mark Wachsler157b0012013-10-02 09:35:38 -0400501
502/* Wait for the system to notice the device is gone, so that a subsequent
503 * fastboot command won't try to access the device before it's rebooted.
504 * Returns 0 for success, -1 for timeout.
505 */
David Pursell0b156632015-10-30 11:22:01 -0700506int LinuxUsbTransport::WaitForDisconnect()
Mark Wachsler157b0012013-10-02 09:35:38 -0400507{
508 double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT;
509 while (now() < deadline) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800510 if (access(handle_->fname, F_OK)) return 0;
511 std::this_thread::sleep_for(50ms);
Mark Wachsler157b0012013-10-02 09:35:38 -0400512 }
513 return -1;
514}