blob: 6db1e27271e12661217aa683a2fdad71d07e0c4b [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
David Pursell0b156632015-10-30 11:22:01 -070046#include <memory>
47
Mark Wachsler157b0012013-10-02 09:35:38 -040048#include "fastboot.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#include "usb.h"
50
Dan Murphyb2de4db2009-08-18 09:41:09 -050051#define MAX_RETRIES 5
52
Mark Wachsler157b0012013-10-02 09:35:38 -040053/* 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 Murphyb2de4db2009-08-18 09:41:09 -050059#ifdef TRACE_USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#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 Hughes93f65fa2015-07-24 14:09:44 -070067// 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 Krause913eb8b2011-03-08 14:10:16 +080080#define MAX_USBFS_BULK_SIZE (16 * 1024)
81
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080082struct usb_handle
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083{
84 char fname[64];
85 int desc;
86 unsigned char ep_in;
87 unsigned char ep_out;
88};
89
David Pursell0b156632015-10-30 11:22:01 -070090class LinuxUsbTransport : public Transport {
91 public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -070092 explicit LinuxUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {}
David Pursell0b156632015-10-30 11:22:01 -070093 ~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 Wachslerbd446c72013-09-11 18:23:31 -0400106/* 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 Projectdd7bc332009-03-03 19:32:55 -0800111static inline int badname(const char *name)
112{
Mark Wachslerbd446c72013-09-11 18:23:31 -0400113 if (!isdigit(*name))
114 return 1;
115 while(*++name) {
116 if(!isdigit(*name) && *name != '.' && *name != '-')
117 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 }
119 return 0;
120}
121
122static int check(void *_desc, int len, unsigned type, int size)
123{
Patrick Tjinaac89db2014-07-11 08:59:01 -0700124 struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)_desc;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800125
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 if(len < size) return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700127 if(hdr->bLength < size) return -1;
128 if(hdr->bLength > len) return -1;
129 if(hdr->bDescriptorType != type) return -1;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800130
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 return 0;
132}
133
Mark Wachsler157b0012013-10-02 09:35:38 -0400134static int filter_usb_device(char* sysfs_name,
Mark Wachslerbd446c72013-09-11 18:23:31 -0400135 char *ptr, int len, int writable,
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700136 ifc_match_func callback,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 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 Pomazau5ae3f932012-02-28 07:21:08 -0800144
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 int in, out;
146 unsigned i;
147 unsigned e;
James Hawkins588a2ca2016-02-18 14:52:46 -0800148
Patrick Tjinaac89db2014-07-11 08:59:01 -0700149 if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700151 dev = (struct usb_device_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 len -= dev->bLength;
153 ptr += dev->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800154
Patrick Tjinaac89db2014-07-11 08:59:01 -0700155 if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700157 cfg = (struct usb_config_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 len -= cfg->bLength;
159 ptr += cfg->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800160
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 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 Hughesb4add9b2009-10-06 18:07:49 -0700166 info.writable = writable;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800167
Mark Wachslerbd446c72013-09-11 18:23:31 -0400168 snprintf(info.device_path, sizeof(info.device_path), "usb:%s", sysfs_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Mark Wachslerbd446c72013-09-11 18:23:31 -0400170 /* 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 Anderson13081c62012-04-06 12:39:30 -0700175 */
Mark Wachslerbd446c72013-09-11 18:23:31 -0400176 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 Anderson13081c62012-04-06 12:39:30 -0700197 }
198 }
199
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 for(i = 0; i < cfg->bNumInterfaces; i++) {
Patrick Tjinaac89db2014-07-11 08:59:01 -0700201
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 Projectdd7bc332009-03-03 19:32:55 -0800211 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700212
213 ifc = (struct usb_interface_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 len -= ifc->bLength;
215 ptr += ifc->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800216
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 in = -1;
218 out = -1;
219 info.ifc_class = ifc->bInterfaceClass;
220 info.ifc_subclass = ifc->bInterfaceSubClass;
221 info.ifc_protocol = ifc->bInterfaceProtocol;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800222
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 for(e = 0; e < ifc->bNumEndpoints; e++) {
Patrick Tjinaac89db2014-07-11 08:59:01 -0700224 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 Projectdd7bc332009-03-03 19:32:55 -0800236 len -= ept->bLength;
237 ptr += ept->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800238
Patrick Tjinaac89db2014-07-11 08:59:01 -0700239 if((ept->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 continue;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800241
Patrick Tjinaac89db2014-07-11 08:59:01 -0700242 if(ept->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 in = ept->bEndpointAddress;
244 } else {
245 out = ept->bEndpointAddress;
246 }
Jack Pham1c022132014-12-05 12:11:20 -0800247
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 Projectdd7bc332009-03-03 19:32:55 -0800254 }
255
256 info.has_bulk_in = (in != -1);
257 info.has_bulk_out = (out != -1);
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800258
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 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 Wachslerbd446c72013-09-11 18:23:31 -0400270static 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
295static 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 */
312static 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 Pursell0b156632015-10-30 11:22:01 -0700329static 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 -0800330{
David Pursell0b156632015-10-30 11:22:01 -0700331 std::unique_ptr<usb_handle> usb;
Mark Wachslerbd446c72013-09-11 18:23:31 -0400332 char devname[64];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 char desc[1024];
334 int n, in, out, ifc;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800335
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 struct dirent *de;
337 int fd;
Elliott Hughesc500be92009-10-07 17:24:39 -0700338 int writable;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800339
James Hawkins588a2ca2016-02-18 14:52:46 -0800340 std::unique_ptr<DIR, decltype(&closedir)> busdir(opendir(base), closedir);
David Pursell0b156632015-10-30 11:22:01 -0700341 if (busdir == 0) return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342
James Hawkins588a2ca2016-02-18 14:52:46 -0800343 while ((de = readdir(busdir.get())) && (usb == nullptr)) {
David Pursell0b156632015-10-30 11:22:01 -0700344 if (badname(de->d_name)) continue;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800345
David Pursell0b156632015-10-30 11:22:01 -0700346 if (!convert_to_devfs_name(de->d_name, devname, sizeof(devname))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347
348// DBG("[ scanning %s ]\n", devname);
Elliott Hughesc500be92009-10-07 17:24:39 -0700349 writable = 1;
David Pursell0b156632015-10-30 11:22:01 -0700350 if ((fd = open(devname, O_RDWR)) < 0) {
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700351 // Check if we have read-only access, so we can give a helpful
352 // diagnostic like "adb devices" does.
353 writable = 0;
David Pursell0b156632015-10-30 11:22:01 -0700354 if ((fd = open(devname, O_RDONLY)) < 0) {
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700355 continue;
356 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 }
358
359 n = read(fd, desc, sizeof(desc));
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800360
David Pursell0b156632015-10-30 11:22:01 -0700361 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 Projectdd7bc332009-03-03 19:32:55 -0800363 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 Pursell0b156632015-10-30 11:22:01 -0700369 if (n != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 close(fd);
David Pursell0b156632015-10-30 11:22:01 -0700371 usb.reset();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 continue;
373 }
374 } else {
375 close(fd);
376 }
377 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379
380 return usb;
381}
382
David Pursell0b156632015-10-30 11:22:01 -0700383ssize_t LinuxUsbTransport::Write(const void* _data, size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384{
385 unsigned char *data = (unsigned char*) _data;
386 unsigned count = 0;
387 struct usbdevfs_bulktransfer bulk;
388 int n;
389
David Pursell0b156632015-10-30 11:22:01 -0700390 if (handle_->ep_out == 0 || handle_->desc == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 return -1;
392 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800393
Mark Wachsler157b0012013-10-02 09:35:38 -0400394 do {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 int xfer;
David Krause913eb8b2011-03-08 14:10:16 +0800396 xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800397
David Pursell0b156632015-10-30 11:22:01 -0700398 bulk.ep = handle_->ep_out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 bulk.len = xfer;
400 bulk.data = data;
401 bulk.timeout = 0;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800402
David Pursell0b156632015-10-30 11:22:01 -0700403 n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 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 Wachsler157b0012013-10-02 09:35:38 -0400413 } while(len > 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414
415 return count;
416}
417
David Pursell0b156632015-10-30 11:22:01 -0700418ssize_t LinuxUsbTransport::Read(void* _data, size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419{
420 unsigned char *data = (unsigned char*) _data;
421 unsigned count = 0;
422 struct usbdevfs_bulktransfer bulk;
Dan Murphyb2de4db2009-08-18 09:41:09 -0500423 int n, retry;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424
David Pursell0b156632015-10-30 11:22:01 -0700425 if (handle_->ep_in == 0 || handle_->desc == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 return -1;
427 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800428
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 while(len > 0) {
David Krause913eb8b2011-03-08 14:10:16 +0800430 int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800431
David Pursell0b156632015-10-30 11:22:01 -0700432 bulk.ep = handle_->ep_in;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 bulk.len = xfer;
434 bulk.data = data;
435 bulk.timeout = 0;
Dan Murphyb2de4db2009-08-18 09:41:09 -0500436 retry = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437
Dan Murphyb2de4db2009-08-18 09:41:09 -0500438 do{
David Pursell0b156632015-10-30 11:22:01 -0700439 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 Murphyb2de4db2009-08-18 09:41:09 -0500442
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 Projectdd7bc332009-03-03 19:32:55 -0800448 }
Dan Murphyb2de4db2009-08-18 09:41:09 -0500449 while( n < 0 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450
451 count += n;
452 len -= n;
453 data += n;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800454
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 if(n < xfer) {
456 break;
457 }
458 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 return count;
461}
462
David Pursell0b156632015-10-30 11:22:01 -0700463int LinuxUsbTransport::Close()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464{
465 int fd;
466
David Pursell0b156632015-10-30 11:22:01 -0700467 fd = handle_->desc;
468 handle_->desc = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 if(fd >= 0) {
470 close(fd);
471 DBG("[ usb closed %d ]\n", fd);
472 }
473
474 return 0;
475}
476
David Pursell0b156632015-10-30 11:22:01 -0700477Transport* usb_open(ifc_match_func callback)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478{
David Pursell0b156632015-10-30 11:22:01 -0700479 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 Projectdd7bc332009-03-03 19:32:55 -0800481}
Mark Wachsler157b0012013-10-02 09:35:38 -0400482
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 Pursell0b156632015-10-30 11:22:01 -0700487int LinuxUsbTransport::WaitForDisconnect()
Mark Wachsler157b0012013-10-02 09:35:38 -0400488{
489 double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT;
490 while (now() < deadline) {
David Pursell0b156632015-10-30 11:22:01 -0700491 if (access(handle_->fname, F_OK))
Mark Wachsler157b0012013-10-02 09:35:38 -0400492 return 0;
493 usleep(50000);
494 }
495 return -1;
496}