blob: 7b879073d91d53a7318fc202d8a47c6ba60e5498 [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
Mark Wachsler157b0012013-10-02 09:35:38 -040046#include "fastboot.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include "usb.h"
48
Dan Murphyb2de4db2009-08-18 09:41:09 -050049#define MAX_RETRIES 5
50
Mark Wachsler157b0012013-10-02 09:35:38 -040051/* Timeout in seconds for usb_wait_for_disconnect.
52 * It doesn't usually take long for a device to disconnect (almost always
53 * under 2 seconds) but we'll time out after 3 seconds just in case.
54 */
55#define WAIT_FOR_DISCONNECT_TIMEOUT 3
56
Dan Murphyb2de4db2009-08-18 09:41:09 -050057#ifdef TRACE_USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058#define DBG1(x...) fprintf(stderr, x)
59#define DBG(x...) fprintf(stderr, x)
60#else
61#define DBG(x...)
62#define DBG1(x...)
63#endif
64
Elliott Hughes93f65fa2015-07-24 14:09:44 -070065// Kernels before 3.3 have a 16KiB transfer limit. That limit was replaced
66// with a 16MiB global limit in 3.3, but each URB submitted required a
67// contiguous kernel allocation, so you would get ENOMEM if you tried to
68// send something larger than the biggest available contiguous kernel
69// memory region. 256KiB contiguous allocations are generally not reliable
70// on a device kernel that has been running for a while fragmenting its
71// memory, but that shouldn't be a problem for fastboot on the host.
72// In 3.6, the contiguous buffer limit was removed by allocating multiple
73// 16KiB chunks and having the USB driver stitch them back together while
74// transmitting using a scatter-gather list, so 256KiB bulk transfers should
75// be reliable.
76// 256KiB seems to work, but 1MiB bulk transfers lock up my z620 with a 3.13
77// kernel.
David Krause913eb8b2011-03-08 14:10:16 +080078#define MAX_USBFS_BULK_SIZE (16 * 1024)
79
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080080struct usb_handle
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081{
82 char fname[64];
83 int desc;
84 unsigned char ep_in;
85 unsigned char ep_out;
86};
87
Mark Wachslerbd446c72013-09-11 18:23:31 -040088/* True if name isn't a valid name for a USB device in /sys/bus/usb/devices.
89 * Device names are made up of numbers, dots, and dashes, e.g., '7-1.5'.
90 * We reject interfaces (e.g., '7-1.5:1.0') and host controllers (e.g. 'usb1').
91 * The name must also start with a digit, to disallow '.' and '..'
92 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093static inline int badname(const char *name)
94{
Mark Wachslerbd446c72013-09-11 18:23:31 -040095 if (!isdigit(*name))
96 return 1;
97 while(*++name) {
98 if(!isdigit(*name) && *name != '.' && *name != '-')
99 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 }
101 return 0;
102}
103
104static int check(void *_desc, int len, unsigned type, int size)
105{
Patrick Tjinaac89db2014-07-11 08:59:01 -0700106 struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)_desc;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800107
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 if(len < size) return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700109 if(hdr->bLength < size) return -1;
110 if(hdr->bLength > len) return -1;
111 if(hdr->bDescriptorType != type) return -1;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800112
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 return 0;
114}
115
Mark Wachsler157b0012013-10-02 09:35:38 -0400116static int filter_usb_device(char* sysfs_name,
Mark Wachslerbd446c72013-09-11 18:23:31 -0400117 char *ptr, int len, int writable,
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700118 ifc_match_func callback,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 int *ept_in_id, int *ept_out_id, int *ifc_id)
120{
121 struct usb_device_descriptor *dev;
122 struct usb_config_descriptor *cfg;
123 struct usb_interface_descriptor *ifc;
124 struct usb_endpoint_descriptor *ept;
125 struct usb_ifc_info info;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800126
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 int in, out;
128 unsigned i;
129 unsigned e;
130
Patrick Tjinaac89db2014-07-11 08:59:01 -0700131 if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700133 dev = (struct usb_device_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 len -= dev->bLength;
135 ptr += dev->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800136
Patrick Tjinaac89db2014-07-11 08:59:01 -0700137 if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700139 cfg = (struct usb_config_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 len -= cfg->bLength;
141 ptr += cfg->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800142
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 info.dev_vendor = dev->idVendor;
144 info.dev_product = dev->idProduct;
145 info.dev_class = dev->bDeviceClass;
146 info.dev_subclass = dev->bDeviceSubClass;
147 info.dev_protocol = dev->bDeviceProtocol;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700148 info.writable = writable;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800149
Mark Wachslerbd446c72013-09-11 18:23:31 -0400150 snprintf(info.device_path, sizeof(info.device_path), "usb:%s", sysfs_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151
Mark Wachslerbd446c72013-09-11 18:23:31 -0400152 /* Read device serial number (if there is one).
153 * We read the serial number from sysfs, since it's faster and more
154 * reliable than issuing a control pipe read, and also won't
155 * cause problems for devices which don't like getting descriptor
156 * requests while they're in the middle of flashing.
Scott Anderson13081c62012-04-06 12:39:30 -0700157 */
Mark Wachslerbd446c72013-09-11 18:23:31 -0400158 info.serial_number[0] = '\0';
159 if (dev->iSerialNumber) {
160 char path[80];
161 int fd;
162
163 snprintf(path, sizeof(path),
164 "/sys/bus/usb/devices/%s/serial", sysfs_name);
165 path[sizeof(path) - 1] = '\0';
166
167 fd = open(path, O_RDONLY);
168 if (fd >= 0) {
169 int chars_read = read(fd, info.serial_number,
170 sizeof(info.serial_number) - 1);
171 close(fd);
172
173 if (chars_read <= 0)
174 info.serial_number[0] = '\0';
175 else if (info.serial_number[chars_read - 1] == '\n') {
176 // strip trailing newline
177 info.serial_number[chars_read - 1] = '\0';
178 }
Scott Anderson13081c62012-04-06 12:39:30 -0700179 }
180 }
181
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 for(i = 0; i < cfg->bNumInterfaces; i++) {
Patrick Tjinaac89db2014-07-11 08:59:01 -0700183
184 while (len > 0) {
185 struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr;
186 if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE) == 0)
187 break;
188 len -= hdr->bLength;
189 ptr += hdr->bLength;
190 }
191
192 if (len <= 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 return -1;
Patrick Tjinaac89db2014-07-11 08:59:01 -0700194
195 ifc = (struct usb_interface_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 len -= ifc->bLength;
197 ptr += ifc->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800198
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 in = -1;
200 out = -1;
201 info.ifc_class = ifc->bInterfaceClass;
202 info.ifc_subclass = ifc->bInterfaceSubClass;
203 info.ifc_protocol = ifc->bInterfaceProtocol;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 for(e = 0; e < ifc->bNumEndpoints; e++) {
Patrick Tjinaac89db2014-07-11 08:59:01 -0700206 while (len > 0) {
207 struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr;
208 if (check(hdr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE) == 0)
209 break;
210 len -= hdr->bLength;
211 ptr += hdr->bLength;
212 }
213 if (len < 0) {
214 break;
215 }
216
217 ept = (struct usb_endpoint_descriptor *)ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 len -= ept->bLength;
219 ptr += ept->bLength;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800220
Patrick Tjinaac89db2014-07-11 08:59:01 -0700221 if((ept->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 continue;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800223
Patrick Tjinaac89db2014-07-11 08:59:01 -0700224 if(ept->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 in = ept->bEndpointAddress;
226 } else {
227 out = ept->bEndpointAddress;
228 }
Jack Pham1c022132014-12-05 12:11:20 -0800229
230 // For USB 3.0 devices skip the SS Endpoint Companion descriptor
231 if (check((struct usb_descriptor_hdr *)ptr, len,
232 USB_DT_SS_ENDPOINT_COMP, USB_DT_SS_EP_COMP_SIZE) == 0) {
233 len -= USB_DT_SS_EP_COMP_SIZE;
234 ptr += USB_DT_SS_EP_COMP_SIZE;
235 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 }
237
238 info.has_bulk_in = (in != -1);
239 info.has_bulk_out = (out != -1);
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800240
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 if(callback(&info) == 0) {
242 *ept_in_id = in;
243 *ept_out_id = out;
244 *ifc_id = ifc->bInterfaceNumber;
245 return 0;
246 }
247 }
248
249 return -1;
250}
251
Mark Wachslerbd446c72013-09-11 18:23:31 -0400252static int read_sysfs_string(const char *sysfs_name, const char *sysfs_node,
253 char* buf, int bufsize)
254{
255 char path[80];
256 int fd, n;
257
258 snprintf(path, sizeof(path),
259 "/sys/bus/usb/devices/%s/%s", sysfs_name, sysfs_node);
260 path[sizeof(path) - 1] = '\0';
261
262 fd = open(path, O_RDONLY);
263 if (fd < 0)
264 return -1;
265
266 n = read(fd, buf, bufsize - 1);
267 close(fd);
268
269 if (n < 0)
270 return -1;
271
272 buf[n] = '\0';
273
274 return n;
275}
276
277static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node)
278{
279 char buf[16];
280 int value;
281
282 if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0)
283 return -1;
284
285 if (sscanf(buf, "%d", &value) != 1)
286 return -1;
287
288 return value;
289}
290
291/* Given the name of a USB device in sysfs, get the name for the same
292 * device in devfs. Returns 0 for success, -1 for failure.
293 */
294static int convert_to_devfs_name(const char* sysfs_name,
295 char* devname, int devname_size)
296{
297 int busnum, devnum;
298
299 busnum = read_sysfs_number(sysfs_name, "busnum");
300 if (busnum < 0)
301 return -1;
302
303 devnum = read_sysfs_number(sysfs_name, "devnum");
304 if (devnum < 0)
305 return -1;
306
307 snprintf(devname, devname_size, "/dev/bus/usb/%03d/%03d", busnum, devnum);
308 return 0;
309}
310
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311static usb_handle *find_usb_device(const char *base, ifc_match_func callback)
312{
313 usb_handle *usb = 0;
Mark Wachslerbd446c72013-09-11 18:23:31 -0400314 char devname[64];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 char desc[1024];
316 int n, in, out, ifc;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800317
Mark Wachslerbd446c72013-09-11 18:23:31 -0400318 DIR *busdir;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 struct dirent *de;
320 int fd;
Elliott Hughesc500be92009-10-07 17:24:39 -0700321 int writable;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800322
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 busdir = opendir(base);
324 if(busdir == 0) return 0;
325
326 while((de = readdir(busdir)) && (usb == 0)) {
327 if(badname(de->d_name)) continue;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800328
Mark Wachslerbd446c72013-09-11 18:23:31 -0400329 if(!convert_to_devfs_name(de->d_name, devname, sizeof(devname))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330
331// DBG("[ scanning %s ]\n", devname);
Elliott Hughesc500be92009-10-07 17:24:39 -0700332 writable = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 if((fd = open(devname, O_RDWR)) < 0) {
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700334 // Check if we have read-only access, so we can give a helpful
335 // diagnostic like "adb devices" does.
336 writable = 0;
337 if((fd = open(devname, O_RDONLY)) < 0) {
338 continue;
339 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 }
341
342 n = read(fd, desc, sizeof(desc));
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800343
Mark Wachsler157b0012013-10-02 09:35:38 -0400344 if(filter_usb_device(de->d_name, desc, n, writable, callback,
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700345 &in, &out, &ifc) == 0) {
Elliott Hughesb3748de2015-06-23 20:27:58 -0700346 usb = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 strcpy(usb->fname, devname);
348 usb->ep_in = in;
349 usb->ep_out = out;
350 usb->desc = fd;
351
352 n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc);
353 if(n != 0) {
354 close(fd);
355 free(usb);
356 usb = 0;
357 continue;
358 }
359 } else {
360 close(fd);
361 }
362 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 }
364 closedir(busdir);
365
366 return usb;
367}
368
369int usb_write(usb_handle *h, const void *_data, int len)
370{
371 unsigned char *data = (unsigned char*) _data;
372 unsigned count = 0;
373 struct usbdevfs_bulktransfer bulk;
374 int n;
375
Mark Wachsler157b0012013-10-02 09:35:38 -0400376 if(h->ep_out == 0 || h->desc == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 return -1;
378 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800379
Mark Wachsler157b0012013-10-02 09:35:38 -0400380 do {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 int xfer;
David Krause913eb8b2011-03-08 14:10:16 +0800382 xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800383
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 bulk.ep = h->ep_out;
385 bulk.len = xfer;
386 bulk.data = data;
387 bulk.timeout = 0;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800388
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 n = ioctl(h->desc, USBDEVFS_BULK, &bulk);
390 if(n != xfer) {
391 DBG("ERROR: n = %d, errno = %d (%s)\n",
392 n, errno, strerror(errno));
393 return -1;
394 }
395
396 count += xfer;
397 len -= xfer;
398 data += xfer;
Mark Wachsler157b0012013-10-02 09:35:38 -0400399 } while(len > 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400
401 return count;
402}
403
404int usb_read(usb_handle *h, void *_data, int len)
405{
406 unsigned char *data = (unsigned char*) _data;
407 unsigned count = 0;
408 struct usbdevfs_bulktransfer bulk;
Dan Murphyb2de4db2009-08-18 09:41:09 -0500409 int n, retry;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410
Mark Wachsler157b0012013-10-02 09:35:38 -0400411 if(h->ep_in == 0 || h->desc == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 return -1;
413 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800414
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 while(len > 0) {
David Krause913eb8b2011-03-08 14:10:16 +0800416 int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800417
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 bulk.ep = h->ep_in;
419 bulk.len = xfer;
420 bulk.data = data;
421 bulk.timeout = 0;
Dan Murphyb2de4db2009-08-18 09:41:09 -0500422 retry = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423
Dan Murphyb2de4db2009-08-18 09:41:09 -0500424 do{
425 DBG("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname);
426 n = ioctl(h->desc, USBDEVFS_BULK, &bulk);
427 DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, h->fname, retry);
428
429 if( n < 0 ) {
430 DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno));
431 if ( ++retry > MAX_RETRIES ) return -1;
432 sleep( 1 );
433 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 }
Dan Murphyb2de4db2009-08-18 09:41:09 -0500435 while( n < 0 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436
437 count += n;
438 len -= n;
439 data += n;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 if(n < xfer) {
442 break;
443 }
444 }
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800445
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 return count;
447}
448
449void usb_kick(usb_handle *h)
450{
451 int fd;
452
453 fd = h->desc;
454 h->desc = -1;
455 if(fd >= 0) {
456 close(fd);
457 DBG("[ usb closed %d ]\n", fd);
458 }
459}
460
461int usb_close(usb_handle *h)
462{
463 int fd;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -0800464
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 fd = h->desc;
466 h->desc = -1;
467 if(fd >= 0) {
468 close(fd);
469 DBG("[ usb closed %d ]\n", fd);
470 }
471
472 return 0;
473}
474
475usb_handle *usb_open(ifc_match_func callback)
476{
Mark Wachslerbd446c72013-09-11 18:23:31 -0400477 return find_usb_device("/sys/bus/usb/devices", callback);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478}
Mark Wachsler157b0012013-10-02 09:35:38 -0400479
480/* Wait for the system to notice the device is gone, so that a subsequent
481 * fastboot command won't try to access the device before it's rebooted.
482 * Returns 0 for success, -1 for timeout.
483 */
484int usb_wait_for_disconnect(usb_handle *usb)
485{
486 double deadline = now() + WAIT_FOR_DISCONNECT_TIMEOUT;
487 while (now() < deadline) {
488 if (access(usb->fname, F_OK))
489 return 0;
490 usleep(50000);
491 }
492 return -1;
493}