usb_linux.c revision c500be926cf07c43aeb828ae86b04b856c6b7c63
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 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 92static int filter_usb_device(int fd, char *ptr, int len, int writable, 93 ifc_match_func callback, 94 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; 123 info.writable = writable; 124 125 // 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; 140 141 result = ioctl(fd, USBDEVFS_CONTROL, &ctrl); 142 if (result > 0) { 143 int i; 144 // skip first word, and copy the rest to the serial string, changing shorts to bytes. 145 result /= 2; 146 for (i = 1; i < result; i++) 147 info.serial_number[i - 1] = buffer[i]; 148 info.serial_number[i - 1] = 0; 149 } 150 } 151 152 for(i = 0; i < cfg->bNumInterfaces; i++) { 153 if(check(ptr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE)) 154 return -1; 155 ifc = (void*) ptr; 156 len -= ifc->bLength; 157 ptr += ifc->bLength; 158 159 in = -1; 160 out = -1; 161 info.ifc_class = ifc->bInterfaceClass; 162 info.ifc_subclass = ifc->bInterfaceSubClass; 163 info.ifc_protocol = ifc->bInterfaceProtocol; 164 165 for(e = 0; e < ifc->bNumEndpoints; e++) { 166 if(check(ptr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE)) 167 return -1; 168 ept = (void*) ptr; 169 len -= ept->bLength; 170 ptr += ept->bLength; 171 172 if((ept->bmAttributes & 0x03) != 0x02) 173 continue; 174 175 if(ept->bEndpointAddress & 0x80) { 176 in = ept->bEndpointAddress; 177 } else { 178 out = ept->bEndpointAddress; 179 } 180 } 181 182 info.has_bulk_in = (in != -1); 183 info.has_bulk_out = (out != -1); 184 185 if(callback(&info) == 0) { 186 *ept_in_id = in; 187 *ept_out_id = out; 188 *ifc_id = ifc->bInterfaceNumber; 189 return 0; 190 } 191 } 192 193 return -1; 194} 195 196static usb_handle *find_usb_device(const char *base, ifc_match_func callback) 197{ 198 usb_handle *usb = 0; 199 char busname[64], devname[64]; 200 char desc[1024]; 201 int n, in, out, ifc; 202 203 DIR *busdir, *devdir; 204 struct dirent *de; 205 int fd; 206 int writable; 207 208 busdir = opendir(base); 209 if(busdir == 0) return 0; 210 211 while((de = readdir(busdir)) && (usb == 0)) { 212 if(badname(de->d_name)) continue; 213 214 sprintf(busname, "%s/%s", base, de->d_name); 215 devdir = opendir(busname); 216 if(devdir == 0) continue; 217 218// DBG("[ scanning %s ]\n", busname); 219 while((de = readdir(devdir)) && (usb == 0)) { 220 221 if(badname(de->d_name)) continue; 222 sprintf(devname, "%s/%s", busname, de->d_name); 223 224// DBG("[ scanning %s ]\n", devname); 225 writable = 1; 226 if((fd = open(devname, O_RDWR)) < 0) { 227 // Check if we have read-only access, so we can give a helpful 228 // diagnostic like "adb devices" does. 229 writable = 0; 230 if((fd = open(devname, O_RDONLY)) < 0) { 231 continue; 232 } 233 } 234 235 n = read(fd, desc, sizeof(desc)); 236 237 if(filter_usb_device(fd, desc, n, writable, callback, 238 &in, &out, &ifc) == 0) { 239 usb = calloc(1, sizeof(usb_handle)); 240 strcpy(usb->fname, devname); 241 usb->ep_in = in; 242 usb->ep_out = out; 243 usb->desc = fd; 244 245 n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); 246 if(n != 0) { 247 close(fd); 248 free(usb); 249 usb = 0; 250 continue; 251 } 252 } else { 253 close(fd); 254 } 255 } 256 closedir(devdir); 257 } 258 closedir(busdir); 259 260 return usb; 261} 262 263int usb_write(usb_handle *h, const void *_data, int len) 264{ 265 unsigned char *data = (unsigned char*) _data; 266 unsigned count = 0; 267 struct usbdevfs_bulktransfer bulk; 268 int n; 269 270 if(h->ep_out == 0) { 271 return -1; 272 } 273 274 if(len == 0) { 275 bulk.ep = h->ep_out; 276 bulk.len = 0; 277 bulk.data = data; 278 bulk.timeout = 0; 279 280 n = ioctl(h->desc, USBDEVFS_BULK, &bulk); 281 if(n != 0) { 282 fprintf(stderr,"ERROR: n = %d, errno = %d (%s)\n", 283 n, errno, strerror(errno)); 284 return -1; 285 } 286 return 0; 287 } 288 289 while(len > 0) { 290 int xfer; 291 xfer = (len > 4096) ? 4096 : len; 292 293 bulk.ep = h->ep_out; 294 bulk.len = xfer; 295 bulk.data = data; 296 bulk.timeout = 0; 297 298 n = ioctl(h->desc, USBDEVFS_BULK, &bulk); 299 if(n != xfer) { 300 DBG("ERROR: n = %d, errno = %d (%s)\n", 301 n, errno, strerror(errno)); 302 return -1; 303 } 304 305 count += xfer; 306 len -= xfer; 307 data += xfer; 308 } 309 310 return count; 311} 312 313int usb_read(usb_handle *h, void *_data, int len) 314{ 315 unsigned char *data = (unsigned char*) _data; 316 unsigned count = 0; 317 struct usbdevfs_bulktransfer bulk; 318 int n, retry; 319 320 if(h->ep_in == 0) { 321 return -1; 322 } 323 324 while(len > 0) { 325 int xfer = (len > 4096) ? 4096 : len; 326 327 bulk.ep = h->ep_in; 328 bulk.len = xfer; 329 bulk.data = data; 330 bulk.timeout = 0; 331 retry = 0; 332 333 do{ 334 DBG("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname); 335 n = ioctl(h->desc, USBDEVFS_BULK, &bulk); 336 DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, h->fname, retry); 337 338 if( n < 0 ) { 339 DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); 340 if ( ++retry > MAX_RETRIES ) return -1; 341 sleep( 1 ); 342 } 343 } 344 while( n < 0 ); 345 346 count += n; 347 len -= n; 348 data += n; 349 350 if(n < xfer) { 351 break; 352 } 353 } 354 355 return count; 356} 357 358void usb_kick(usb_handle *h) 359{ 360 int fd; 361 362 fd = h->desc; 363 h->desc = -1; 364 if(fd >= 0) { 365 close(fd); 366 DBG("[ usb closed %d ]\n", fd); 367 } 368} 369 370int usb_close(usb_handle *h) 371{ 372 int fd; 373 374 fd = h->desc; 375 h->desc = -1; 376 if(fd >= 0) { 377 close(fd); 378 DBG("[ usb closed %d ]\n", fd); 379 } 380 381 return 0; 382} 383 384usb_handle *usb_open(ifc_match_func callback) 385{ 386 return find_usb_device("/dev/bus/usb", callback); 387} 388