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