blob: 78b7b98059c7e5937ed907e21e02c6e586850e0a [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
12 * the documentation and/or other materials provided with the
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
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
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
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33
34#include <sys/ioctl.h>
35#include <sys/types.h>
36#include <dirent.h>
37#include <fcntl.h>
38#include <errno.h>
39#include <pthread.h>
40#include <ctype.h>
41
42#include <linux/usbdevice_fs.h>
43#include <linux/usbdevice_fs.h>
44#include <linux/version.h>
45#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
46#include <linux/usb/ch9.h>
47#else
48#include <linux/usb_ch9.h>
49#endif
50#include <asm/byteorder.h>
51
52#include "usb.h"
53
Dan Murphyb2de4db2009-08-18 09:41:09 -050054#define MAX_RETRIES 5
55
56#ifdef TRACE_USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057#define DBG1(x...) fprintf(stderr, x)
58#define DBG(x...) fprintf(stderr, x)
59#else
60#define DBG(x...)
61#define DBG1(x...)
62#endif
63
64struct usb_handle
65{
66 char fname[64];
67 int desc;
68 unsigned char ep_in;
69 unsigned char ep_out;
70};
71
72static inline int badname(const char *name)
73{
74 while(*name) {
75 if(!isdigit(*name++)) return 1;
76 }
77 return 0;
78}
79
80static int check(void *_desc, int len, unsigned type, int size)
81{
82 unsigned char *desc = _desc;
83
84 if(len < size) return -1;
85 if(desc[0] < size) return -1;
86 if(desc[0] > len) return -1;
87 if(desc[1] != type) return -1;
88
89 return 0;
90}
91
Elliott Hughesb4add9b2009-10-06 18:07:49 -070092static int filter_usb_device(int fd, char *ptr, int len, int writable,
93 ifc_match_func callback,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 int *ept_in_id, int *ept_out_id, int *ifc_id)
95{
96 struct usb_device_descriptor *dev;
97 struct usb_config_descriptor *cfg;
98 struct usb_interface_descriptor *ifc;
99 struct usb_endpoint_descriptor *ept;
100 struct usb_ifc_info info;
101
102 int in, out;
103 unsigned i;
104 unsigned e;
105
106 if(check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
107 return -1;
108 dev = (void*) ptr;
109 len -= dev->bLength;
110 ptr += dev->bLength;
111
112 if(check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE))
113 return -1;
114 cfg = (void*) ptr;
115 len -= cfg->bLength;
116 ptr += cfg->bLength;
117
118 info.dev_vendor = dev->idVendor;
119 info.dev_product = dev->idProduct;
120 info.dev_class = dev->bDeviceClass;
121 info.dev_subclass = dev->bDeviceSubClass;
122 info.dev_protocol = dev->bDeviceProtocol;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700123 info.writable = writable;
124
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 // read device serial number (if there is one)
126 info.serial_number[0] = 0;
127 if (dev->iSerialNumber) {
128 struct usbdevfs_ctrltransfer ctrl;
129 __u16 buffer[128];
130 int result;
131
132 memset(buffer, 0, sizeof(buffer));
133
134 ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
135 ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
136 ctrl.wValue = (USB_DT_STRING << 8) | dev->iSerialNumber;
137 ctrl.wIndex = 0;
138 ctrl.wLength = sizeof(buffer);
139 ctrl.data = buffer;
Werner Johansson931bdcc2010-06-30 18:42:21 -0700140 ctrl.timeout = 50;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
142 result = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
143 if (result > 0) {
144 int i;
145 // skip first word, and copy the rest to the serial string, changing shorts to bytes.
146 result /= 2;
147 for (i = 1; i < result; i++)
148 info.serial_number[i - 1] = buffer[i];
149 info.serial_number[i - 1] = 0;
150 }
151 }
152
153 for(i = 0; i < cfg->bNumInterfaces; i++) {
154 if(check(ptr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE))
155 return -1;
156 ifc = (void*) ptr;
157 len -= ifc->bLength;
158 ptr += ifc->bLength;
159
160 in = -1;
161 out = -1;
162 info.ifc_class = ifc->bInterfaceClass;
163 info.ifc_subclass = ifc->bInterfaceSubClass;
164 info.ifc_protocol = ifc->bInterfaceProtocol;
165
166 for(e = 0; e < ifc->bNumEndpoints; e++) {
167 if(check(ptr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE))
168 return -1;
169 ept = (void*) ptr;
170 len -= ept->bLength;
171 ptr += ept->bLength;
172
173 if((ept->bmAttributes & 0x03) != 0x02)
174 continue;
175
176 if(ept->bEndpointAddress & 0x80) {
177 in = ept->bEndpointAddress;
178 } else {
179 out = ept->bEndpointAddress;
180 }
181 }
182
183 info.has_bulk_in = (in != -1);
184 info.has_bulk_out = (out != -1);
185
186 if(callback(&info) == 0) {
187 *ept_in_id = in;
188 *ept_out_id = out;
189 *ifc_id = ifc->bInterfaceNumber;
190 return 0;
191 }
192 }
193
194 return -1;
195}
196
197static usb_handle *find_usb_device(const char *base, ifc_match_func callback)
198{
199 usb_handle *usb = 0;
200 char busname[64], devname[64];
201 char desc[1024];
202 int n, in, out, ifc;
203
204 DIR *busdir, *devdir;
205 struct dirent *de;
206 int fd;
Elliott Hughesc500be92009-10-07 17:24:39 -0700207 int writable;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208
209 busdir = opendir(base);
210 if(busdir == 0) return 0;
211
212 while((de = readdir(busdir)) && (usb == 0)) {
213 if(badname(de->d_name)) continue;
214
215 sprintf(busname, "%s/%s", base, de->d_name);
216 devdir = opendir(busname);
217 if(devdir == 0) continue;
218
219// DBG("[ scanning %s ]\n", busname);
220 while((de = readdir(devdir)) && (usb == 0)) {
221
222 if(badname(de->d_name)) continue;
223 sprintf(devname, "%s/%s", busname, de->d_name);
224
225// DBG("[ scanning %s ]\n", devname);
Elliott Hughesc500be92009-10-07 17:24:39 -0700226 writable = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 if((fd = open(devname, O_RDWR)) < 0) {
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700228 // Check if we have read-only access, so we can give a helpful
229 // diagnostic like "adb devices" does.
230 writable = 0;
231 if((fd = open(devname, O_RDONLY)) < 0) {
232 continue;
233 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
235
236 n = read(fd, desc, sizeof(desc));
237
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700238 if(filter_usb_device(fd, desc, n, writable, callback,
239 &in, &out, &ifc) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 usb = calloc(1, sizeof(usb_handle));
241 strcpy(usb->fname, devname);
242 usb->ep_in = in;
243 usb->ep_out = out;
244 usb->desc = fd;
245
246 n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc);
247 if(n != 0) {
248 close(fd);
249 free(usb);
250 usb = 0;
251 continue;
252 }
253 } else {
254 close(fd);
255 }
256 }
257 closedir(devdir);
258 }
259 closedir(busdir);
260
261 return usb;
262}
263
264int usb_write(usb_handle *h, const void *_data, int len)
265{
266 unsigned char *data = (unsigned char*) _data;
267 unsigned count = 0;
268 struct usbdevfs_bulktransfer bulk;
269 int n;
270
271 if(h->ep_out == 0) {
272 return -1;
273 }
274
275 if(len == 0) {
276 bulk.ep = h->ep_out;
277 bulk.len = 0;
278 bulk.data = data;
279 bulk.timeout = 0;
280
281 n = ioctl(h->desc, USBDEVFS_BULK, &bulk);
282 if(n != 0) {
283 fprintf(stderr,"ERROR: n = %d, errno = %d (%s)\n",
284 n, errno, strerror(errno));
285 return -1;
286 }
287 return 0;
288 }
289
290 while(len > 0) {
291 int xfer;
292 xfer = (len > 4096) ? 4096 : len;
293
294 bulk.ep = h->ep_out;
295 bulk.len = xfer;
296 bulk.data = data;
297 bulk.timeout = 0;
298
299 n = ioctl(h->desc, USBDEVFS_BULK, &bulk);
300 if(n != xfer) {
301 DBG("ERROR: n = %d, errno = %d (%s)\n",
302 n, errno, strerror(errno));
303 return -1;
304 }
305
306 count += xfer;
307 len -= xfer;
308 data += xfer;
309 }
310
311 return count;
312}
313
314int usb_read(usb_handle *h, void *_data, int len)
315{
316 unsigned char *data = (unsigned char*) _data;
317 unsigned count = 0;
318 struct usbdevfs_bulktransfer bulk;
Dan Murphyb2de4db2009-08-18 09:41:09 -0500319 int n, retry;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320
321 if(h->ep_in == 0) {
322 return -1;
323 }
324
325 while(len > 0) {
326 int xfer = (len > 4096) ? 4096 : len;
327
328 bulk.ep = h->ep_in;
329 bulk.len = xfer;
330 bulk.data = data;
331 bulk.timeout = 0;
Dan Murphyb2de4db2009-08-18 09:41:09 -0500332 retry = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333
Dan Murphyb2de4db2009-08-18 09:41:09 -0500334 do{
335 DBG("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname);
336 n = ioctl(h->desc, USBDEVFS_BULK, &bulk);
337 DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, h->fname, retry);
338
339 if( n < 0 ) {
340 DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno));
341 if ( ++retry > MAX_RETRIES ) return -1;
342 sleep( 1 );
343 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 }
Dan Murphyb2de4db2009-08-18 09:41:09 -0500345 while( n < 0 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346
347 count += n;
348 len -= n;
349 data += n;
350
351 if(n < xfer) {
352 break;
353 }
354 }
355
356 return count;
357}
358
359void usb_kick(usb_handle *h)
360{
361 int fd;
362
363 fd = h->desc;
364 h->desc = -1;
365 if(fd >= 0) {
366 close(fd);
367 DBG("[ usb closed %d ]\n", fd);
368 }
369}
370
371int usb_close(usb_handle *h)
372{
373 int fd;
374
375 fd = h->desc;
376 h->desc = -1;
377 if(fd >= 0) {
378 close(fd);
379 DBG("[ usb closed %d ]\n", fd);
380 }
381
382 return 0;
383}
384
385usb_handle *usb_open(ifc_match_func callback)
386{
387 return find_usb_device("/dev/bus/usb", callback);
388}