usb_linux.c revision b4add9b74525210478bac702d27fdaf9cf7ab18f
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 = 1; 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 if((fd = open(devname, O_RDWR)) < 0) { 226 // Check if we have read-only access, so we can give a helpful 227 // diagnostic like "adb devices" does. 228 writable = 0; 229 if((fd = open(devname, O_RDONLY)) < 0) { 230 continue; 231 } 232 } 233 234 n = read(fd, desc, sizeof(desc)); 235 236 if(filter_usb_device(fd, desc, n, writable, callback, 237 &in, &out, &ifc) == 0) { 238 usb = calloc(1, sizeof(usb_handle)); 239 strcpy(usb->fname, devname); 240 usb->ep_in = in; 241 usb->ep_out = out; 242 usb->desc = fd; 243 244 n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); 245 if(n != 0) { 246 close(fd); 247 free(usb); 248 usb = 0; 249 continue; 250 } 251 } else { 252 close(fd); 253 } 254 } 255 closedir(devdir); 256 } 257 closedir(busdir); 258 259 return usb; 260} 261 262int usb_write(usb_handle *h, const void *_data, int len) 263{ 264 unsigned char *data = (unsigned char*) _data; 265 unsigned count = 0; 266 struct usbdevfs_bulktransfer bulk; 267 int n; 268 269 if(h->ep_out == 0) { 270 return -1; 271 } 272 273 if(len == 0) { 274 bulk.ep = h->ep_out; 275 bulk.len = 0; 276 bulk.data = data; 277 bulk.timeout = 0; 278 279 n = ioctl(h->desc, USBDEVFS_BULK, &bulk); 280 if(n != 0) { 281 fprintf(stderr,"ERROR: n = %d, errno = %d (%s)\n", 282 n, errno, strerror(errno)); 283 return -1; 284 } 285 return 0; 286 } 287 288 while(len > 0) { 289 int xfer; 290 xfer = (len > 4096) ? 4096 : len; 291 292 bulk.ep = h->ep_out; 293 bulk.len = xfer; 294 bulk.data = data; 295 bulk.timeout = 0; 296 297 n = ioctl(h->desc, USBDEVFS_BULK, &bulk); 298 if(n != xfer) { 299 DBG("ERROR: n = %d, errno = %d (%s)\n", 300 n, errno, strerror(errno)); 301 return -1; 302 } 303 304 count += xfer; 305 len -= xfer; 306 data += xfer; 307 } 308 309 return count; 310} 311 312int usb_read(usb_handle *h, void *_data, int len) 313{ 314 unsigned char *data = (unsigned char*) _data; 315 unsigned count = 0; 316 struct usbdevfs_bulktransfer bulk; 317 int n, retry; 318 319 if(h->ep_in == 0) { 320 return -1; 321 } 322 323 while(len > 0) { 324 int xfer = (len > 4096) ? 4096 : len; 325 326 bulk.ep = h->ep_in; 327 bulk.len = xfer; 328 bulk.data = data; 329 bulk.timeout = 0; 330 retry = 0; 331 332 do{ 333 DBG("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname); 334 n = ioctl(h->desc, USBDEVFS_BULK, &bulk); 335 DBG("[ usb read %d ] = %d, fname=%s, Retry %d \n", xfer, n, h->fname, retry); 336 337 if( n < 0 ) { 338 DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno)); 339 if ( ++retry > MAX_RETRIES ) return -1; 340 sleep( 1 ); 341 } 342 } 343 while( n < 0 ); 344 345 count += n; 346 len -= n; 347 data += n; 348 349 if(n < xfer) { 350 break; 351 } 352 } 353 354 return count; 355} 356 357void usb_kick(usb_handle *h) 358{ 359 int fd; 360 361 fd = h->desc; 362 h->desc = -1; 363 if(fd >= 0) { 364 close(fd); 365 DBG("[ usb closed %d ]\n", fd); 366 } 367} 368 369int usb_close(usb_handle *h) 370{ 371 int fd; 372 373 fd = h->desc; 374 h->desc = -1; 375 if(fd >= 0) { 376 close(fd); 377 DBG("[ usb closed %d ]\n", fd); 378 } 379 380 return 0; 381} 382 383usb_handle *usb_open(ifc_match_func callback) 384{ 385 return find_usb_device("/dev/bus/usb", callback); 386} 387