| 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 | 
 | 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 Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 54 | #define MAX_RETRIES 5 | 
 | 55 |  | 
 | 56 | #ifdef TRACE_USB | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | #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 |  | 
 | 64 | struct usb_handle  | 
 | 65 | { | 
 | 66 |     char fname[64]; | 
 | 67 |     int desc; | 
 | 68 |     unsigned char ep_in; | 
 | 69 |     unsigned char ep_out; | 
 | 70 | }; | 
 | 71 |  | 
 | 72 | static inline int badname(const char *name) | 
 | 73 | { | 
 | 74 |     while(*name) { | 
 | 75 |         if(!isdigit(*name++)) return 1; | 
 | 76 |     } | 
 | 77 |     return 0; | 
 | 78 | } | 
 | 79 |  | 
 | 80 | static 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 |  | 
 | 92 | static int filter_usb_device(int fd, char *ptr, int len, ifc_match_func callback, | 
 | 93 |                              int *ept_in_id, int *ept_out_id, int *ifc_id) | 
 | 94 | { | 
 | 95 |     struct usb_device_descriptor *dev; | 
 | 96 |     struct usb_config_descriptor *cfg; | 
 | 97 |     struct usb_interface_descriptor *ifc; | 
 | 98 |     struct usb_endpoint_descriptor *ept; | 
 | 99 |     struct usb_ifc_info info; | 
 | 100 |      | 
 | 101 |     int in, out; | 
 | 102 |     unsigned i; | 
 | 103 |     unsigned e; | 
 | 104 |      | 
 | 105 |     if(check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE)) | 
 | 106 |         return -1; | 
 | 107 |     dev = (void*) ptr; | 
 | 108 |     len -= dev->bLength; | 
 | 109 |     ptr += dev->bLength; | 
 | 110 |      | 
 | 111 |     if(check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE)) | 
 | 112 |         return -1; | 
 | 113 |     cfg = (void*) ptr; | 
 | 114 |     len -= cfg->bLength; | 
 | 115 |     ptr += cfg->bLength; | 
 | 116 |      | 
 | 117 |     info.dev_vendor = dev->idVendor; | 
 | 118 |     info.dev_product = dev->idProduct; | 
 | 119 |     info.dev_class = dev->bDeviceClass; | 
 | 120 |     info.dev_subclass = dev->bDeviceSubClass; | 
 | 121 |     info.dev_protocol = dev->bDeviceProtocol; | 
 | 122 |  | 
 | 123 |     // read device serial number (if there is one) | 
 | 124 |     info.serial_number[0] = 0; | 
 | 125 |     if (dev->iSerialNumber) { | 
 | 126 |         struct usbdevfs_ctrltransfer  ctrl; | 
 | 127 |         __u16 buffer[128]; | 
 | 128 |         int result; | 
 | 129 |  | 
 | 130 |         memset(buffer, 0, sizeof(buffer)); | 
 | 131 |  | 
 | 132 |         ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE; | 
 | 133 |         ctrl.bRequest = USB_REQ_GET_DESCRIPTOR; | 
 | 134 |         ctrl.wValue = (USB_DT_STRING << 8) | dev->iSerialNumber; | 
 | 135 |         ctrl.wIndex = 0; | 
 | 136 |         ctrl.wLength = sizeof(buffer); | 
 | 137 |         ctrl.data = buffer; | 
 | 138 |  | 
 | 139 |         result = ioctl(fd, USBDEVFS_CONTROL, &ctrl); | 
 | 140 |         if (result > 0) { | 
 | 141 |             int i; | 
 | 142 |             // skip first word, and copy the rest to the serial string, changing shorts to bytes. | 
 | 143 |             result /= 2; | 
 | 144 |             for (i = 1; i < result; i++) | 
 | 145 |                 info.serial_number[i - 1] = buffer[i]; | 
 | 146 |             info.serial_number[i - 1] = 0; | 
 | 147 |         } | 
 | 148 |     } | 
 | 149 |  | 
 | 150 |     for(i = 0; i < cfg->bNumInterfaces; i++) { | 
 | 151 |         if(check(ptr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE)) | 
 | 152 |             return -1; | 
 | 153 |         ifc = (void*) ptr; | 
 | 154 |         len -= ifc->bLength; | 
 | 155 |         ptr += ifc->bLength; | 
 | 156 |          | 
 | 157 |         in = -1; | 
 | 158 |         out = -1; | 
 | 159 |         info.ifc_class = ifc->bInterfaceClass; | 
 | 160 |         info.ifc_subclass = ifc->bInterfaceSubClass; | 
 | 161 |         info.ifc_protocol = ifc->bInterfaceProtocol; | 
 | 162 |          | 
 | 163 |         for(e = 0; e < ifc->bNumEndpoints; e++) { | 
 | 164 |             if(check(ptr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE)) | 
 | 165 |                 return -1; | 
 | 166 |             ept = (void*) ptr; | 
 | 167 |             len -= ept->bLength; | 
 | 168 |             ptr += ept->bLength; | 
 | 169 |      | 
 | 170 |             if((ept->bmAttributes & 0x03) != 0x02) | 
 | 171 |                 continue; | 
 | 172 |              | 
 | 173 |             if(ept->bEndpointAddress & 0x80) { | 
 | 174 |                 in = ept->bEndpointAddress; | 
 | 175 |             } else { | 
 | 176 |                 out = ept->bEndpointAddress; | 
 | 177 |             } | 
 | 178 |         } | 
 | 179 |  | 
 | 180 |         info.has_bulk_in = (in != -1); | 
 | 181 |         info.has_bulk_out = (out != -1); | 
 | 182 |          | 
 | 183 |         if(callback(&info) == 0) { | 
 | 184 |             *ept_in_id = in; | 
 | 185 |             *ept_out_id = out; | 
 | 186 |             *ifc_id = ifc->bInterfaceNumber; | 
 | 187 |             return 0; | 
 | 188 |         } | 
 | 189 |     } | 
 | 190 |  | 
 | 191 |     return -1; | 
 | 192 | } | 
 | 193 |  | 
 | 194 | static usb_handle *find_usb_device(const char *base, ifc_match_func callback) | 
 | 195 | { | 
 | 196 |     usb_handle *usb = 0; | 
 | 197 |     char busname[64], devname[64]; | 
 | 198 |     char desc[1024]; | 
 | 199 |     int n, in, out, ifc; | 
 | 200 |      | 
 | 201 |     DIR *busdir, *devdir; | 
 | 202 |     struct dirent *de; | 
 | 203 |     int fd; | 
 | 204 |      | 
 | 205 |     busdir = opendir(base); | 
 | 206 |     if(busdir == 0) return 0; | 
 | 207 |  | 
 | 208 |     while((de = readdir(busdir)) && (usb == 0)) { | 
 | 209 |         if(badname(de->d_name)) continue; | 
 | 210 |          | 
 | 211 |         sprintf(busname, "%s/%s", base, de->d_name); | 
 | 212 |         devdir = opendir(busname); | 
 | 213 |         if(devdir == 0) continue; | 
 | 214 |          | 
 | 215 | //        DBG("[ scanning %s ]\n", busname); | 
 | 216 |         while((de = readdir(devdir)) && (usb == 0)) { | 
 | 217 |              | 
 | 218 |             if(badname(de->d_name)) continue; | 
 | 219 |             sprintf(devname, "%s/%s", busname, de->d_name); | 
 | 220 |  | 
 | 221 | //            DBG("[ scanning %s ]\n", devname); | 
 | 222 |             if((fd = open(devname, O_RDWR)) < 0) { | 
 | 223 |                 continue; | 
 | 224 |             } | 
 | 225 |  | 
 | 226 |             n = read(fd, desc, sizeof(desc)); | 
 | 227 |              | 
 | 228 |             if(filter_usb_device(fd, desc, n, callback, &in, &out, &ifc) == 0){ | 
 | 229 |                 usb = calloc(1, sizeof(usb_handle)); | 
 | 230 |                 strcpy(usb->fname, devname); | 
 | 231 |                 usb->ep_in = in; | 
 | 232 |                 usb->ep_out = out; | 
 | 233 |                 usb->desc = fd; | 
 | 234 |  | 
 | 235 |                 n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); | 
 | 236 |                 if(n != 0) { | 
 | 237 |                     close(fd); | 
 | 238 |                     free(usb); | 
 | 239 |                     usb = 0; | 
 | 240 |                     continue; | 
 | 241 |                 } | 
 | 242 |             } else { | 
 | 243 |                 close(fd); | 
 | 244 |             } | 
 | 245 |         } | 
 | 246 |         closedir(devdir); | 
 | 247 |     } | 
 | 248 |     closedir(busdir); | 
 | 249 |  | 
 | 250 |     return usb; | 
 | 251 | } | 
 | 252 |  | 
 | 253 | int usb_write(usb_handle *h, const void *_data, int len) | 
 | 254 | { | 
 | 255 |     unsigned char *data = (unsigned char*) _data; | 
 | 256 |     unsigned count = 0; | 
 | 257 |     struct usbdevfs_bulktransfer bulk; | 
 | 258 |     int n; | 
 | 259 |  | 
 | 260 |     if(h->ep_out == 0) { | 
 | 261 |         return -1; | 
 | 262 |     } | 
 | 263 |      | 
 | 264 |     if(len == 0) { | 
 | 265 |         bulk.ep = h->ep_out; | 
 | 266 |         bulk.len = 0; | 
 | 267 |         bulk.data = data; | 
 | 268 |         bulk.timeout = 0; | 
 | 269 |          | 
 | 270 |         n = ioctl(h->desc, USBDEVFS_BULK, &bulk); | 
 | 271 |         if(n != 0) { | 
 | 272 |             fprintf(stderr,"ERROR: n = %d, errno = %d (%s)\n", | 
 | 273 |                     n, errno, strerror(errno)); | 
 | 274 |             return -1; | 
 | 275 |         } | 
 | 276 |         return 0; | 
 | 277 |     } | 
 | 278 |      | 
 | 279 |     while(len > 0) { | 
 | 280 |         int xfer; | 
 | 281 |         xfer = (len > 4096) ? 4096 : len; | 
 | 282 |          | 
 | 283 |         bulk.ep = h->ep_out; | 
 | 284 |         bulk.len = xfer; | 
 | 285 |         bulk.data = data; | 
 | 286 |         bulk.timeout = 0; | 
 | 287 |          | 
 | 288 |         n = ioctl(h->desc, USBDEVFS_BULK, &bulk); | 
 | 289 |         if(n != xfer) { | 
 | 290 |             DBG("ERROR: n = %d, errno = %d (%s)\n", | 
 | 291 |                 n, errno, strerror(errno)); | 
 | 292 |             return -1; | 
 | 293 |         } | 
 | 294 |  | 
 | 295 |         count += xfer; | 
 | 296 |         len -= xfer; | 
 | 297 |         data += xfer; | 
 | 298 |     } | 
 | 299 |  | 
 | 300 |     return count; | 
 | 301 | } | 
 | 302 |  | 
 | 303 | int usb_read(usb_handle *h, void *_data, int len) | 
 | 304 | { | 
 | 305 |     unsigned char *data = (unsigned char*) _data; | 
 | 306 |     unsigned count = 0; | 
 | 307 |     struct usbdevfs_bulktransfer bulk; | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 308 |     int n, retry; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 |  | 
 | 310 |     if(h->ep_in == 0) { | 
 | 311 |         return -1; | 
 | 312 |     } | 
 | 313 |      | 
 | 314 |     while(len > 0) { | 
 | 315 |         int xfer = (len > 4096) ? 4096 : len; | 
 | 316 |          | 
 | 317 |         bulk.ep = h->ep_in; | 
 | 318 |         bulk.len = xfer; | 
 | 319 |         bulk.data = data; | 
 | 320 |         bulk.timeout = 0; | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 321 |         retry = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 322 |  | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 323 |         do{ | 
 | 324 |            DBG("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname); | 
 | 325 |            n = ioctl(h->desc, USBDEVFS_BULK, &bulk); | 
 | 326 |            DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, h->fname, retry); | 
 | 327 |  | 
 | 328 |            if( n < 0 ) { | 
 | 329 |             DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); | 
 | 330 |             if ( ++retry > MAX_RETRIES ) return -1; | 
 | 331 |             sleep( 1 ); | 
 | 332 |            } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 |         } | 
| Dan Murphy | b2de4db | 2009-08-18 09:41:09 -0500 | [diff] [blame] | 334 |         while( n < 0 ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 335 |  | 
 | 336 |         count += n; | 
 | 337 |         len -= n; | 
 | 338 |         data += n; | 
 | 339 |          | 
 | 340 |         if(n < xfer) { | 
 | 341 |             break; | 
 | 342 |         } | 
 | 343 |     } | 
 | 344 |      | 
 | 345 |     return count; | 
 | 346 | } | 
 | 347 |  | 
 | 348 | void usb_kick(usb_handle *h) | 
 | 349 | { | 
 | 350 |     int fd; | 
 | 351 |  | 
 | 352 |     fd = h->desc; | 
 | 353 |     h->desc = -1; | 
 | 354 |     if(fd >= 0) { | 
 | 355 |         close(fd); | 
 | 356 |         DBG("[ usb closed %d ]\n", fd); | 
 | 357 |     } | 
 | 358 | } | 
 | 359 |  | 
 | 360 | int usb_close(usb_handle *h) | 
 | 361 | { | 
 | 362 |     int fd; | 
 | 363 |      | 
 | 364 |     fd = h->desc; | 
 | 365 |     h->desc = -1; | 
 | 366 |     if(fd >= 0) { | 
 | 367 |         close(fd); | 
 | 368 |         DBG("[ usb closed %d ]\n", fd); | 
 | 369 |     } | 
 | 370 |  | 
 | 371 |     return 0; | 
 | 372 | } | 
 | 373 |  | 
 | 374 | usb_handle *usb_open(ifc_match_func callback) | 
 | 375 | { | 
 | 376 |     return find_usb_device("/dev/bus/usb", callback); | 
 | 377 | } | 
 | 378 |  | 
 | 379 |  |