| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2010 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
| Mike Lockwood | e15af09 | 2010-06-10 10:20:49 -0400 | [diff] [blame] | 17 | // #define DEBUG 1 | 
 | 18 | #if DEBUG | 
 | 19 |  | 
 | 20 | #ifdef USE_LIBLOG | 
 | 21 | #define LOG_TAG "usbhost" | 
 | 22 | #include "utils/Log.h" | 
 | 23 | #define D LOGD | 
 | 24 | #else | 
 | 25 | #define D printf | 
 | 26 | #endif | 
 | 27 |  | 
 | 28 | #else | 
 | 29 | #define D(...) | 
 | 30 | #endif | 
 | 31 |  | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 32 | #include <stdio.h> | 
 | 33 | #include <stdlib.h> | 
 | 34 | #include <unistd.h> | 
 | 35 | #include <string.h> | 
 | 36 |  | 
 | 37 | #include <sys/ioctl.h> | 
 | 38 | #include <sys/types.h> | 
 | 39 | #include <sys/time.h> | 
 | 40 | #include <sys/inotify.h> | 
 | 41 | #include <dirent.h> | 
 | 42 | #include <fcntl.h> | 
 | 43 | #include <errno.h> | 
 | 44 | #include <ctype.h> | 
 | 45 | #include <pthread.h> | 
 | 46 |  | 
 | 47 | #include <linux/usbdevice_fs.h> | 
 | 48 | #include <linux/version.h> | 
 | 49 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) | 
 | 50 | #include <linux/usb/ch9.h> | 
 | 51 | #else | 
 | 52 | #include <linux/usb_ch9.h> | 
 | 53 | #endif | 
 | 54 | #include <asm/byteorder.h> | 
 | 55 |  | 
 | 56 | #include "usbhost/usbhost.h" | 
 | 57 |  | 
 | 58 | #define USB_FS_DIR "/dev/bus/usb" | 
| Mike Lockwood | 203f102 | 2010-05-27 10:12:03 -0400 | [diff] [blame] | 59 | #define USB_FS_ID_SCANNER   "/dev/bus/usb/%d/%d" | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 60 |  | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 61 |  | 
| Mike Lockwood | 6ac3aa1 | 2010-05-25 08:10:02 -0400 | [diff] [blame] | 62 | struct usb_host_context { | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 63 |     int fd; | 
| Mike Lockwood | 6ac3aa1 | 2010-05-25 08:10:02 -0400 | [diff] [blame] | 64 | }; | 
 | 65 |  | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 66 | struct usb_device { | 
 | 67 |     char dev_name[64]; | 
 | 68 |     unsigned char desc[256]; | 
 | 69 |     int desc_length; | 
 | 70 |     int fd; | 
 | 71 |     int writeable; | 
 | 72 | }; | 
 | 73 |  | 
 | 74 | struct usb_endpoint | 
 | 75 | { | 
 | 76 |     struct usb_device *dev; | 
 | 77 |     struct usb_endpoint_descriptor  desc; | 
 | 78 |     struct usbdevfs_urb urb; | 
 | 79 | }; | 
 | 80 |  | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 81 | static inline int badname(const char *name) | 
 | 82 | { | 
 | 83 |     while(*name) { | 
 | 84 |         if(!isdigit(*name++)) return 1; | 
 | 85 |     } | 
 | 86 |     return 0; | 
 | 87 | } | 
 | 88 |  | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 89 | /* returns true if one of the callbacks indicates we are done */ | 
 | 90 | static int find_existing_devices(usb_device_added_cb added_cb, | 
 | 91 |                                   usb_device_removed_cb removed_cb, | 
 | 92 |                                   void *client_data) | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 93 | { | 
 | 94 |     char busname[32], devname[32]; | 
 | 95 |     DIR *busdir , *devdir ; | 
 | 96 |     struct dirent *de; | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 97 |     int done = 0; | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 98 |  | 
 | 99 |     busdir = opendir(USB_FS_DIR); | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 100 |     if(busdir == 0) return 1; | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 101 |  | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 102 |     while ((de = readdir(busdir)) != 0 && !done) { | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 103 |         if(badname(de->d_name)) continue; | 
 | 104 |  | 
 | 105 |         snprintf(busname, sizeof busname, "%s/%s", USB_FS_DIR, de->d_name); | 
 | 106 |         devdir = opendir(busname); | 
 | 107 |         if(devdir == 0) continue; | 
 | 108 |  | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 109 |         while ((de = readdir(devdir)) && !done) { | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 110 |             if(badname(de->d_name)) continue; | 
 | 111 |  | 
 | 112 |             snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name); | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 113 |             done = added_cb(devname, client_data); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 114 |         } // end of devdir while | 
 | 115 |         closedir(devdir); | 
 | 116 |     } //end of busdir while | 
 | 117 |     closedir(busdir); | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 118 |  | 
 | 119 |     return done; | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 120 | } | 
 | 121 |  | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 122 | struct usb_host_context *usb_host_init() | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 123 | { | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 124 |     struct usb_host_context *context = calloc(1, sizeof(struct usb_host_context)); | 
 | 125 |     if (!context) { | 
 | 126 |         fprintf(stderr, "out of memory in usb_host_context\n"); | 
 | 127 |         return NULL; | 
 | 128 |     } | 
 | 129 |     context->fd = inotify_init(); | 
 | 130 |     if (context->fd < 0) { | 
 | 131 |         fprintf(stderr, "inotify_init failed\n"); | 
 | 132 |         free(context); | 
 | 133 |         return NULL; | 
 | 134 |     } | 
 | 135 |     return context; | 
 | 136 | } | 
 | 137 |  | 
 | 138 | void usb_host_cleanup(struct usb_host_context *context) | 
 | 139 | { | 
 | 140 |     close(context->fd); | 
 | 141 |     free(context); | 
 | 142 | } | 
 | 143 |  | 
 | 144 | void usb_host_run(struct usb_host_context *context, | 
 | 145 |                   usb_device_added_cb added_cb, | 
 | 146 |                   usb_device_removed_cb removed_cb, | 
| Mike Lockwood | a805519 | 2010-07-19 20:15:15 -0400 | [diff] [blame] | 147 |                   usb_discovery_done_cb discovery_done_cb, | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 148 |                   void *client_data) | 
 | 149 | { | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 150 |     struct inotify_event* event; | 
 | 151 |     char event_buf[512]; | 
 | 152 |     char path[100]; | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 153 |     int i, ret, done = 0; | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 154 |     int wd, wds[10]; | 
 | 155 |     int wd_count = sizeof(wds) / sizeof(wds[0]); | 
 | 156 |  | 
 | 157 |     D("Created device discovery thread\n"); | 
 | 158 |  | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 159 |     /* watch for files added and deleted within USB_FS_DIR */ | 
 | 160 |     memset(wds, 0, sizeof(wds)); | 
 | 161 |     /* watch the root for new subdirectories */ | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 162 |     wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 163 |     if (wds[0] < 0) { | 
 | 164 |         fprintf(stderr, "inotify_add_watch failed\n"); | 
| Mike Lockwood | e8849d1 | 2010-07-20 16:31:42 -0400 | [diff] [blame] | 165 |         if (discovery_done_cb) | 
 | 166 |             discovery_done_cb(client_data); | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 167 |         return; | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 168 |     } | 
 | 169 |  | 
 | 170 |     /* watch existing subdirectories of USB_FS_DIR */ | 
 | 171 |     for (i = 1; i < wd_count; i++) { | 
 | 172 |         snprintf(path, sizeof(path), "%s/%03d", USB_FS_DIR, i); | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 173 |         ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 174 |         if (ret > 0) | 
 | 175 |             wds[i] = ret; | 
 | 176 |     } | 
 | 177 |  | 
 | 178 |     /* check for existing devices first, after we have inotify set up */ | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 179 |     done = find_existing_devices(added_cb, removed_cb, client_data); | 
| Mike Lockwood | a805519 | 2010-07-19 20:15:15 -0400 | [diff] [blame] | 180 |     if (discovery_done_cb) | 
 | 181 |         done |= discovery_done_cb(client_data); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 182 |  | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 183 |     while (!done) { | 
 | 184 |         ret = read(context->fd, event_buf, sizeof(event_buf)); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 185 |         if (ret >= (int)sizeof(struct inotify_event)) { | 
 | 186 |             event = (struct inotify_event *)event_buf; | 
 | 187 |             wd = event->wd; | 
 | 188 |             if (wd == wds[0]) { | 
 | 189 |                 i = atoi(event->name); | 
 | 190 |                 snprintf(path, sizeof(path), "%s/%s", USB_FS_DIR, event->name); | 
 | 191 |                 D("new subdirectory %s: index: %d\n", path, i); | 
 | 192 |                 if (i > 0 && i < wd_count) { | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 193 |                 ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 194 |                 if (ret > 0) | 
 | 195 |                     wds[i] = ret; | 
 | 196 |                 } | 
 | 197 |             } else { | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 198 |                 for (i = 1; i < wd_count && !done; i++) { | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 199 |                     if (wd == wds[i]) { | 
 | 200 |                         snprintf(path, sizeof(path), "%s/%03d/%s", USB_FS_DIR, i, event->name); | 
 | 201 |                         if (event->mask == IN_CREATE) { | 
 | 202 |                             D("new device %s\n", path); | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 203 |                             done = added_cb(path, client_data); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 204 |                         } else if (event->mask == IN_DELETE) { | 
 | 205 |                             D("gone device %s\n", path); | 
| Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 206 |                             done = removed_cb(path, client_data); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 207 |                         } | 
 | 208 |                     } | 
 | 209 |                 } | 
 | 210 |             } | 
 | 211 |         } | 
 | 212 |     } | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 213 | } | 
 | 214 |  | 
 | 215 | struct usb_device *usb_device_open(const char *dev_name) | 
 | 216 | { | 
 | 217 |     struct usb_device *device = calloc(1, sizeof(struct usb_device)); | 
 | 218 |     int fd, length, did_retry = 0; | 
 | 219 |  | 
 | 220 |     strcpy(device->dev_name, dev_name); | 
 | 221 |     device->writeable = 1; | 
 | 222 |  | 
 | 223 | retry: | 
 | 224 |     fd = open(dev_name, O_RDWR); | 
 | 225 |     if (fd < 0) { | 
 | 226 |         /* if we fail, see if have read-only access */ | 
 | 227 |         fd = open(dev_name, O_RDONLY); | 
| Mike Lockwood | e15af09 | 2010-06-10 10:20:49 -0400 | [diff] [blame] | 228 |         D("usb_device_open open returned %d errno %d\n", fd, errno); | 
 | 229 |         if (fd < 0 && (errno == EACCES || errno == ENOENT) && !did_retry) { | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 230 |             /* work around race condition between inotify and permissions management */ | 
 | 231 |             sleep(1); | 
 | 232 |             did_retry = 1; | 
 | 233 |             goto retry; | 
 | 234 |         } | 
 | 235 |  | 
 | 236 |         if (fd < 0) goto fail; | 
 | 237 |         device->writeable = 0; | 
 | 238 |         D("[ usb open read-only %s fd = %d]\n", dev_name, fd); | 
 | 239 |     } | 
 | 240 |  | 
 | 241 |     length = read(fd, device->desc, sizeof(device->desc)); | 
| Mike Lockwood | e15af09 | 2010-06-10 10:20:49 -0400 | [diff] [blame] | 242 |     D("usb_device_open read returned %d errno %d\n", fd, errno); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 243 |     if (length < 0) | 
 | 244 |         goto fail; | 
 | 245 |  | 
 | 246 |     device->fd = fd; | 
 | 247 |     device->desc_length = length; | 
 | 248 |     return device; | 
 | 249 | fail: | 
 | 250 |     close(fd); | 
 | 251 |     free(device); | 
 | 252 |     return NULL; | 
 | 253 | } | 
 | 254 |  | 
 | 255 | void usb_device_close(struct usb_device *device) | 
 | 256 | { | 
 | 257 |     close(device->fd); | 
 | 258 |     free(device); | 
 | 259 | } | 
 | 260 |  | 
 | 261 | const char* usb_device_get_name(struct usb_device *device) | 
 | 262 | { | 
 | 263 |     return device->dev_name; | 
 | 264 | } | 
 | 265 |  | 
| Mike Lockwood | 203f102 | 2010-05-27 10:12:03 -0400 | [diff] [blame] | 266 | int usb_device_get_unique_id(struct usb_device *device) | 
 | 267 | { | 
 | 268 |     int bus = 0, dev = 0; | 
 | 269 |     sscanf(device->dev_name, USB_FS_ID_SCANNER, &bus, &dev); | 
 | 270 |     return bus * 1000 + dev; | 
 | 271 | } | 
 | 272 |  | 
| Mike Lockwood | 07eb4af | 2010-07-27 19:05:33 -0400 | [diff] [blame] | 273 | int usb_device_get_unique_id_from_name(const char* name) | 
 | 274 | { | 
 | 275 |     int bus = 0, dev = 0; | 
 | 276 |     sscanf(name, USB_FS_ID_SCANNER, &bus, &dev); | 
 | 277 |     return bus * 1000 + dev; | 
 | 278 | } | 
 | 279 |  | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 280 | uint16_t usb_device_get_vendor_id(struct usb_device *device) | 
 | 281 | { | 
 | 282 |     struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc; | 
 | 283 |     return __le16_to_cpu(desc->idVendor); | 
 | 284 | } | 
 | 285 |  | 
 | 286 | uint16_t usb_device_get_product_id(struct usb_device *device) | 
 | 287 | { | 
 | 288 |     struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc; | 
 | 289 |     return __le16_to_cpu(desc->idProduct); | 
 | 290 | } | 
 | 291 |  | 
| Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 292 | int usb_device_send_control(struct usb_device *device, | 
 | 293 |                             int requestType, | 
 | 294 |                             int request, | 
 | 295 |                             int value, | 
 | 296 |                             int index, | 
 | 297 |                             int length, | 
 | 298 |                             void* buffer) | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 299 | { | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 300 |     struct usbdevfs_ctrltransfer  ctrl; | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 301 |  | 
| Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 302 |     // this usually requires read/write permission | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 303 |     if (!device->writeable) { | 
 | 304 |         int fd = open(device->dev_name, O_RDWR); | 
 | 305 |         if (fd > 0) { | 
 | 306 |             close(device->fd); | 
 | 307 |             device->fd = fd; | 
 | 308 |             device->writeable = 1; | 
 | 309 |         } else { | 
| Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 310 |             return -1; | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 311 |         } | 
 | 312 |     } | 
 | 313 |  | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 314 |     memset(&ctrl, 0, sizeof(ctrl)); | 
| Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 315 |     ctrl.bRequestType = requestType; | 
 | 316 |     ctrl.bRequest = request; | 
 | 317 |     ctrl.wValue = value; | 
 | 318 |     ctrl.wIndex = index; | 
 | 319 |     ctrl.wLength = length; | 
 | 320 |     ctrl.data = buffer; | 
 | 321 |     return ioctl(device->fd, USBDEVFS_CONTROL, &ctrl); | 
 | 322 | } | 
 | 323 |  | 
 | 324 | char* usb_device_get_string(struct usb_device *device, int id) | 
 | 325 | { | 
 | 326 |     char string[256]; | 
 | 327 |     __u16 buffer[128]; | 
 | 328 |     __u16 languages[128]; | 
 | 329 |     int i, result; | 
 | 330 |     int languageCount = 0; | 
 | 331 |  | 
 | 332 |     string[0] = 0; | 
 | 333 |     memset(languages, 0, sizeof(languages)); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 334 |  | 
 | 335 |     // read list of supported languages | 
| Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 336 |     result = usb_device_send_control(device, | 
 | 337 |             USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR, | 
 | 338 |             (USB_DT_STRING << 8) | 0, 0, sizeof(languages), languages); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 339 |     if (result > 0) | 
 | 340 |         languageCount = (result - 2) / 2; | 
 | 341 |  | 
 | 342 |     for (i = 1; i <= languageCount; i++) { | 
 | 343 |         memset(buffer, 0, sizeof(buffer)); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 344 |  | 
| Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 345 |         result = usb_device_send_control(device, | 
 | 346 |                 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR, | 
 | 347 |                 (USB_DT_STRING << 8) | id, languages[i], sizeof(buffer), buffer); | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 348 |         if (result > 0) { | 
 | 349 |             int i; | 
 | 350 |             // skip first word, and copy the rest to the string, changing shorts to bytes. | 
 | 351 |             result /= 2; | 
 | 352 |             for (i = 1; i < result; i++) | 
 | 353 |                 string[i - 1] = buffer[i]; | 
 | 354 |             string[i - 1] = 0; | 
 | 355 |             return strdup(string); | 
 | 356 |         } | 
 | 357 |     } | 
 | 358 |  | 
 | 359 |     return NULL; | 
 | 360 | } | 
 | 361 |  | 
 | 362 | char* usb_device_get_manufacturer_name(struct usb_device *device) | 
 | 363 | { | 
 | 364 |     struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; | 
 | 365 |  | 
 | 366 |     if (desc->iManufacturer) | 
 | 367 |         return usb_device_get_string(device, desc->iManufacturer); | 
 | 368 |     else | 
 | 369 |         return NULL; | 
 | 370 | } | 
 | 371 |  | 
 | 372 | char* usb_device_get_product_name(struct usb_device *device) | 
 | 373 | { | 
 | 374 |     struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; | 
 | 375 |  | 
 | 376 |     if (desc->iProduct) | 
 | 377 |         return usb_device_get_string(device, desc->iProduct); | 
 | 378 |     else | 
 | 379 |         return NULL; | 
 | 380 | } | 
 | 381 |  | 
 | 382 | char* usb_device_get_serial(struct usb_device *device) | 
 | 383 | { | 
 | 384 |     struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; | 
 | 385 |  | 
 | 386 |     if (desc->iSerialNumber) | 
 | 387 |         return usb_device_get_string(device, desc->iSerialNumber); | 
 | 388 |     else | 
 | 389 |         return NULL; | 
 | 390 | } | 
 | 391 |  | 
 | 392 | int usb_device_is_writeable(struct usb_device *device) | 
 | 393 | { | 
 | 394 |     return device->writeable; | 
 | 395 | } | 
 | 396 |  | 
 | 397 | void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter) | 
 | 398 | { | 
 | 399 |     iter->config = device->desc; | 
 | 400 |     iter->config_end = device->desc + device->desc_length; | 
 | 401 |     iter->curr_desc = device->desc; | 
 | 402 | } | 
 | 403 |  | 
 | 404 | struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter) | 
 | 405 | { | 
 | 406 |     struct usb_descriptor_header* next; | 
 | 407 |     if (iter->curr_desc >= iter->config_end) | 
 | 408 |         return NULL; | 
 | 409 |     next = (struct usb_descriptor_header*)iter->curr_desc; | 
 | 410 |     iter->curr_desc += next->bLength; | 
 | 411 |     return next; | 
 | 412 | } | 
 | 413 |  | 
 | 414 | int usb_device_claim_interface(struct usb_device *device, unsigned int interface) | 
 | 415 | { | 
 | 416 |     return ioctl(device->fd, USBDEVFS_CLAIMINTERFACE, &interface); | 
 | 417 | } | 
 | 418 |  | 
 | 419 | int usb_device_release_interface(struct usb_device *device, unsigned int interface) | 
 | 420 | { | 
 | 421 |     return ioctl(device->fd, USBDEVFS_RELEASEINTERFACE, &interface); | 
 | 422 | } | 
 | 423 |  | 
 | 424 | struct usb_endpoint *usb_endpoint_open(struct usb_device *dev, | 
 | 425 |         const struct usb_endpoint_descriptor *desc) | 
 | 426 | { | 
 | 427 |     struct usb_endpoint *ep = calloc(1, sizeof(struct usb_endpoint)); | 
 | 428 |     memcpy(&ep->desc, desc, sizeof(ep->desc)); | 
 | 429 |     ep->dev = dev; | 
 | 430 |     return ep; | 
 | 431 | } | 
 | 432 |  | 
 | 433 | void usb_endpoint_close(struct usb_endpoint *ep) | 
 | 434 | { | 
 | 435 |     // cancel IO here? | 
 | 436 |     free(ep); | 
 | 437 | } | 
 | 438 |  | 
 | 439 | int usb_endpoint_queue(struct usb_endpoint *ep, void *data, int len) | 
 | 440 | { | 
 | 441 |     struct usbdevfs_urb *urb = &ep->urb; | 
 | 442 |     int res; | 
 | 443 |  | 
 | 444 |     D("usb_endpoint_queue\n"); | 
 | 445 |     memset(urb, 0, sizeof(*urb)); | 
 | 446 |     urb->type = USBDEVFS_URB_TYPE_BULK; | 
 | 447 |     urb->endpoint = ep->desc.bEndpointAddress; | 
 | 448 |     urb->status = -1; | 
 | 449 |     urb->buffer = data; | 
 | 450 |     urb->buffer_length = len; | 
 | 451 |  | 
 | 452 |     do { | 
 | 453 |         res = ioctl(ep->dev->fd, USBDEVFS_SUBMITURB, urb); | 
 | 454 |     } while((res < 0) && (errno == EINTR)); | 
 | 455 |  | 
 | 456 |     return res; | 
 | 457 | } | 
 | 458 |  | 
 | 459 | int usb_endpoint_wait(struct usb_device *dev, int *out_ep_num) | 
 | 460 | { | 
 | 461 |     struct usbdevfs_urb *out = NULL; | 
 | 462 |     int res; | 
 | 463 |  | 
 | 464 |     while (1) { | 
 | 465 |         res = ioctl(dev->fd, USBDEVFS_REAPURB, &out); | 
 | 466 |         D("USBDEVFS_REAPURB returned %d\n", res); | 
 | 467 |         if (res < 0) { | 
 | 468 |             if(errno == EINTR) { | 
 | 469 |                 continue; | 
 | 470 |             } | 
 | 471 |             D("[ reap urb - error ]\n"); | 
 | 472 |             *out_ep_num = -1; | 
 | 473 |         } else { | 
 | 474 |             D("[ urb @%p status = %d, actual = %d ]\n", | 
 | 475 |                 out, out->status, out->actual_length); | 
 | 476 |             res = out->actual_length; | 
 | 477 |             *out_ep_num = out->endpoint; | 
 | 478 |         } | 
 | 479 |         break; | 
 | 480 |     } | 
 | 481 |     return res; | 
 | 482 | } | 
 | 483 |  | 
 | 484 | int usb_endpoint_cancel(struct usb_endpoint *ep) | 
 | 485 | { | 
 | 486 |     return ioctl(ep->dev->fd, USBDEVFS_DISCARDURB, &ep->urb); | 
 | 487 | } | 
 | 488 |  | 
| Mike Lockwood | 5e567cb | 2010-05-12 08:53:49 -0400 | [diff] [blame] | 489 | struct usb_device *usb_endpoint_get_device(struct usb_endpoint *ep) | 
 | 490 | { | 
 | 491 |     return ep->dev; | 
 | 492 | } | 
 | 493 |  | 
| Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 494 | int usb_endpoint_number(struct usb_endpoint *ep) | 
 | 495 | { | 
 | 496 |     return ep->desc.bEndpointAddress; | 
 | 497 | } | 
 | 498 |  | 
 | 499 | int usb_endpoint_max_packet(struct usb_endpoint *ep) | 
 | 500 | { | 
 | 501 |     return __le16_to_cpu(ep->desc.wMaxPacketSize); | 
 | 502 | } | 
 | 503 |  |