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" |
Steve Block | 8d66c49 | 2011-12-20 16:07:45 +0000 | [diff] [blame] | 23 | #define D ALOGD |
Mike Lockwood | e15af09 | 2010-06-10 10:20:49 -0400 | [diff] [blame] | 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> |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 36 | #include <stddef.h> |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 37 | |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/time.h> |
| 41 | #include <sys/inotify.h> |
| 42 | #include <dirent.h> |
| 43 | #include <fcntl.h> |
| 44 | #include <errno.h> |
| 45 | #include <ctype.h> |
| 46 | #include <pthread.h> |
| 47 | |
| 48 | #include <linux/usbdevice_fs.h> |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 49 | #include <asm/byteorder.h> |
| 50 | |
| 51 | #include "usbhost/usbhost.h" |
| 52 | |
Benoit Goby | f4de078 | 2012-08-01 18:54:02 -0700 | [diff] [blame] | 53 | #define DEV_DIR "/dev" |
Ziv Hendel | a306ced | 2013-08-07 19:29:17 +0300 | [diff] [blame] | 54 | #define DEV_BUS_DIR DEV_DIR "/bus" |
| 55 | #define USB_FS_DIR DEV_BUS_DIR "/usb" |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 56 | #define USB_FS_ID_SCANNER USB_FS_DIR "/%d/%d" |
| 57 | #define USB_FS_ID_FORMAT USB_FS_DIR "/%03d/%03d" |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 58 | |
Mike Lockwood | 0dd1aab | 2015-06-18 13:38:31 -0700 | [diff] [blame] | 59 | // Some devices fail to send string descriptors if we attempt reading > 255 bytes |
| 60 | #define MAX_STRING_DESCRIPTOR_LENGTH 255 |
| 61 | |
Mike Lockwood | c4c00d8 | 2011-03-12 12:25:11 -0500 | [diff] [blame] | 62 | // From drivers/usb/core/devio.c |
| 63 | // I don't know why this isn't in a kernel header |
| 64 | #define MAX_USBFS_BUFFER_SIZE 16384 |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 65 | |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 66 | #define MAX_USBFS_WD_COUNT 10 |
| 67 | |
Mike Lockwood | 6ac3aa1 | 2010-05-25 08:10:02 -0400 | [diff] [blame] | 68 | struct usb_host_context { |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 69 | int fd; |
| 70 | usb_device_added_cb cb_added; |
| 71 | usb_device_removed_cb cb_removed; |
| 72 | void *data; |
| 73 | int wds[MAX_USBFS_WD_COUNT]; |
| 74 | int wdd; |
Ziv Hendel | a306ced | 2013-08-07 19:29:17 +0300 | [diff] [blame] | 75 | int wddbus; |
Mike Lockwood | 6ac3aa1 | 2010-05-25 08:10:02 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 78 | struct usb_device { |
| 79 | char dev_name[64]; |
Mike Lockwood | ec9e7b1 | 2011-01-22 09:17:07 -0800 | [diff] [blame] | 80 | unsigned char desc[4096]; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 81 | int desc_length; |
| 82 | int fd; |
| 83 | int writeable; |
| 84 | }; |
| 85 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 86 | static inline int badname(const char *name) |
| 87 | { |
| 88 | while(*name) { |
| 89 | if(!isdigit(*name++)) return 1; |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
Benoit Goby | 6cc883c | 2012-07-31 19:22:06 -0700 | [diff] [blame] | 94 | static int find_existing_devices_bus(char *busname, |
| 95 | usb_device_added_cb added_cb, |
| 96 | void *client_data) |
| 97 | { |
| 98 | char devname[32]; |
| 99 | DIR *devdir; |
| 100 | struct dirent *de; |
| 101 | int done = 0; |
| 102 | |
| 103 | devdir = opendir(busname); |
| 104 | if(devdir == 0) return 0; |
| 105 | |
| 106 | while ((de = readdir(devdir)) && !done) { |
| 107 | if(badname(de->d_name)) continue; |
| 108 | |
| 109 | snprintf(devname, sizeof(devname), "%s/%s", busname, de->d_name); |
| 110 | done = added_cb(devname, client_data); |
| 111 | } // end of devdir while |
| 112 | closedir(devdir); |
| 113 | |
| 114 | return done; |
| 115 | } |
| 116 | |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 117 | /* returns true if one of the callbacks indicates we are done */ |
| 118 | static int find_existing_devices(usb_device_added_cb added_cb, |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 119 | void *client_data) |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 120 | { |
Benoit Goby | 6cc883c | 2012-07-31 19:22:06 -0700 | [diff] [blame] | 121 | char busname[32]; |
| 122 | DIR *busdir; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 123 | struct dirent *de; |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 124 | int done = 0; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 125 | |
| 126 | busdir = opendir(USB_FS_DIR); |
Benoit Goby | f4de078 | 2012-08-01 18:54:02 -0700 | [diff] [blame] | 127 | if(busdir == 0) return 0; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 128 | |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 129 | while ((de = readdir(busdir)) != 0 && !done) { |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 130 | if(badname(de->d_name)) continue; |
| 131 | |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 132 | snprintf(busname, sizeof(busname), USB_FS_DIR "/%s", de->d_name); |
Benoit Goby | 6cc883c | 2012-07-31 19:22:06 -0700 | [diff] [blame] | 133 | done = find_existing_devices_bus(busname, added_cb, |
| 134 | client_data); |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 135 | } //end of busdir while |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 136 | closedir(busdir); |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 137 | |
| 138 | return done; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Benoit Goby | f4de078 | 2012-08-01 18:54:02 -0700 | [diff] [blame] | 141 | static void watch_existing_subdirs(struct usb_host_context *context, |
| 142 | int *wds, int wd_count) |
| 143 | { |
| 144 | char path[100]; |
| 145 | int i, ret; |
| 146 | |
| 147 | wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE); |
| 148 | if (wds[0] < 0) |
| 149 | return; |
| 150 | |
| 151 | /* watch existing subdirectories of USB_FS_DIR */ |
| 152 | for (i = 1; i < wd_count; i++) { |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 153 | snprintf(path, sizeof(path), USB_FS_DIR "/%03d", i); |
Benoit Goby | f4de078 | 2012-08-01 18:54:02 -0700 | [diff] [blame] | 154 | ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE); |
| 155 | if (ret >= 0) |
| 156 | wds[i] = ret; |
| 157 | } |
| 158 | } |
| 159 | |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 160 | struct usb_host_context *usb_host_init() |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 161 | { |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 162 | struct usb_host_context *context = calloc(1, sizeof(struct usb_host_context)); |
| 163 | if (!context) { |
| 164 | fprintf(stderr, "out of memory in usb_host_context\n"); |
| 165 | return NULL; |
| 166 | } |
| 167 | context->fd = inotify_init(); |
| 168 | if (context->fd < 0) { |
| 169 | fprintf(stderr, "inotify_init failed\n"); |
| 170 | free(context); |
| 171 | return NULL; |
| 172 | } |
| 173 | return context; |
| 174 | } |
| 175 | |
| 176 | void usb_host_cleanup(struct usb_host_context *context) |
| 177 | { |
| 178 | close(context->fd); |
| 179 | free(context); |
| 180 | } |
| 181 | |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 182 | int usb_host_get_fd(struct usb_host_context *context) |
| 183 | { |
| 184 | return context->fd; |
| 185 | } /* usb_host_get_fd() */ |
| 186 | |
| 187 | int usb_host_load(struct usb_host_context *context, |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 188 | usb_device_added_cb added_cb, |
| 189 | usb_device_removed_cb removed_cb, |
Mike Lockwood | a805519 | 2010-07-19 20:15:15 -0400 | [diff] [blame] | 190 | usb_discovery_done_cb discovery_done_cb, |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 191 | void *client_data) |
| 192 | { |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 193 | int done = 0; |
| 194 | int i; |
| 195 | |
| 196 | context->cb_added = added_cb; |
| 197 | context->cb_removed = removed_cb; |
| 198 | context->data = client_data; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 199 | |
| 200 | D("Created device discovery thread\n"); |
| 201 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 202 | /* watch for files added and deleted within USB_FS_DIR */ |
Ziv Hendel | a306ced | 2013-08-07 19:29:17 +0300 | [diff] [blame] | 203 | context->wddbus = -1; |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 204 | for (i = 0; i < MAX_USBFS_WD_COUNT; i++) |
| 205 | context->wds[i] = -1; |
Benoit Goby | f4de078 | 2012-08-01 18:54:02 -0700 | [diff] [blame] | 206 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 207 | /* watch the root for new subdirectories */ |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 208 | context->wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE); |
| 209 | if (context->wdd < 0) { |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 210 | fprintf(stderr, "inotify_add_watch failed\n"); |
Mike Lockwood | e8849d1 | 2010-07-20 16:31:42 -0400 | [diff] [blame] | 211 | if (discovery_done_cb) |
| 212 | discovery_done_cb(client_data); |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 213 | return done; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 216 | watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 217 | |
| 218 | /* check for existing devices first, after we have inotify set up */ |
Benoit Goby | 6cc883c | 2012-07-31 19:22:06 -0700 | [diff] [blame] | 219 | done = find_existing_devices(added_cb, client_data); |
Mike Lockwood | a805519 | 2010-07-19 20:15:15 -0400 | [diff] [blame] | 220 | if (discovery_done_cb) |
| 221 | done |= discovery_done_cb(client_data); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 222 | |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 223 | return done; |
| 224 | } /* usb_host_load() */ |
| 225 | |
| 226 | int usb_host_read_event(struct usb_host_context *context) |
| 227 | { |
| 228 | struct inotify_event* event; |
| 229 | char event_buf[512]; |
| 230 | char path[100]; |
| 231 | int i, ret, done = 0; |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 232 | int offset = 0; |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 233 | int wd; |
| 234 | |
| 235 | ret = read(context->fd, event_buf, sizeof(event_buf)); |
| 236 | if (ret >= (int)sizeof(struct inotify_event)) { |
Ziv Hendel | a306ced | 2013-08-07 19:29:17 +0300 | [diff] [blame] | 237 | while (offset < ret && !done) { |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 238 | event = (struct inotify_event*)&event_buf[offset]; |
| 239 | done = 0; |
| 240 | wd = event->wd; |
| 241 | if (wd == context->wdd) { |
| 242 | if ((event->mask & IN_CREATE) && !strcmp(event->name, "bus")) { |
Ziv Hendel | a306ced | 2013-08-07 19:29:17 +0300 | [diff] [blame] | 243 | context->wddbus = inotify_add_watch(context->fd, DEV_BUS_DIR, IN_CREATE | IN_DELETE); |
| 244 | if (context->wddbus < 0) { |
| 245 | done = 1; |
| 246 | } else { |
| 247 | watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT); |
| 248 | done = find_existing_devices(context->cb_added, context->data); |
| 249 | } |
| 250 | } |
| 251 | } else if (wd == context->wddbus) { |
| 252 | if ((event->mask & IN_CREATE) && !strcmp(event->name, "usb")) { |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 253 | watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT); |
| 254 | done = find_existing_devices(context->cb_added, context->data); |
Ziv Hendel | a306ced | 2013-08-07 19:29:17 +0300 | [diff] [blame] | 255 | } else if ((event->mask & IN_DELETE) && !strcmp(event->name, "usb")) { |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 256 | for (i = 0; i < MAX_USBFS_WD_COUNT; i++) { |
| 257 | if (context->wds[i] >= 0) { |
| 258 | inotify_rm_watch(context->fd, context->wds[i]); |
| 259 | context->wds[i] = -1; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } else if (wd == context->wds[0]) { |
| 264 | i = atoi(event->name); |
| 265 | snprintf(path, sizeof(path), USB_FS_DIR "/%s", event->name); |
| 266 | D("%s subdirectory %s: index: %d\n", (event->mask & IN_CREATE) ? |
| 267 | "new" : "gone", path, i); |
| 268 | if (i > 0 && i < MAX_USBFS_WD_COUNT) { |
Bo Huang | 3c1d7b3 | 2014-01-21 14:25:22 +0800 | [diff] [blame] | 269 | int local_ret = 0; |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 270 | if (event->mask & IN_CREATE) { |
Bo Huang | 3c1d7b3 | 2014-01-21 14:25:22 +0800 | [diff] [blame] | 271 | local_ret = inotify_add_watch(context->fd, path, |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 272 | IN_CREATE | IN_DELETE); |
Bo Huang | 3c1d7b3 | 2014-01-21 14:25:22 +0800 | [diff] [blame] | 273 | if (local_ret >= 0) |
| 274 | context->wds[i] = local_ret; |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 275 | done = find_existing_devices_bus(path, context->cb_added, |
| 276 | context->data); |
| 277 | } else if (event->mask & IN_DELETE) { |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 278 | inotify_rm_watch(context->fd, context->wds[i]); |
| 279 | context->wds[i] = -1; |
Benoit Goby | f4de078 | 2012-08-01 18:54:02 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 282 | } else { |
| 283 | for (i = 1; (i < MAX_USBFS_WD_COUNT) && !done; i++) { |
| 284 | if (wd == context->wds[i]) { |
| 285 | snprintf(path, sizeof(path), USB_FS_DIR "/%03d/%s", i, event->name); |
| 286 | if (event->mask == IN_CREATE) { |
| 287 | D("new device %s\n", path); |
| 288 | done = context->cb_added(path, context->data); |
| 289 | } else if (event->mask == IN_DELETE) { |
| 290 | D("gone device %s\n", path); |
| 291 | done = context->cb_removed(path, context->data); |
| 292 | } |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
Ziv Hendel | f75ea8d | 2013-03-24 18:10:52 +0200 | [diff] [blame] | 296 | |
| 297 | offset += sizeof(struct inotify_event) + event->len; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 298 | } |
| 299 | } |
Guillaume Ranquet | dea46b6 | 2012-10-23 17:11:44 +0200 | [diff] [blame] | 300 | |
| 301 | return done; |
| 302 | } /* usb_host_read_event() */ |
| 303 | |
| 304 | void usb_host_run(struct usb_host_context *context, |
| 305 | usb_device_added_cb added_cb, |
| 306 | usb_device_removed_cb removed_cb, |
| 307 | usb_discovery_done_cb discovery_done_cb, |
| 308 | void *client_data) |
| 309 | { |
| 310 | int done; |
| 311 | |
| 312 | done = usb_host_load(context, added_cb, removed_cb, discovery_done_cb, client_data); |
| 313 | |
| 314 | while (!done) { |
| 315 | |
| 316 | done = usb_host_read_event(context); |
| 317 | } |
| 318 | } /* usb_host_run() */ |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 319 | |
| 320 | struct usb_device *usb_device_open(const char *dev_name) |
| 321 | { |
Mike Lockwood | cd185f2 | 2010-12-12 14:17:02 -0800 | [diff] [blame] | 322 | int fd, did_retry = 0, writeable = 1; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 323 | |
Mike Lockwood | ec9e7b1 | 2011-01-22 09:17:07 -0800 | [diff] [blame] | 324 | D("usb_device_open %s\n", dev_name); |
| 325 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 326 | retry: |
| 327 | fd = open(dev_name, O_RDWR); |
| 328 | if (fd < 0) { |
| 329 | /* if we fail, see if have read-only access */ |
| 330 | fd = open(dev_name, O_RDONLY); |
Mike Lockwood | e15af09 | 2010-06-10 10:20:49 -0400 | [diff] [blame] | 331 | D("usb_device_open open returned %d errno %d\n", fd, errno); |
| 332 | if (fd < 0 && (errno == EACCES || errno == ENOENT) && !did_retry) { |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 333 | /* work around race condition between inotify and permissions management */ |
| 334 | sleep(1); |
| 335 | did_retry = 1; |
| 336 | goto retry; |
| 337 | } |
| 338 | |
Mike Lockwood | cd185f2 | 2010-12-12 14:17:02 -0800 | [diff] [blame] | 339 | if (fd < 0) |
| 340 | return NULL; |
| 341 | writeable = 0; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 342 | D("[ usb open read-only %s fd = %d]\n", dev_name, fd); |
| 343 | } |
| 344 | |
Mike Lockwood | cd185f2 | 2010-12-12 14:17:02 -0800 | [diff] [blame] | 345 | struct usb_device* result = usb_device_new(dev_name, fd); |
| 346 | if (result) |
| 347 | result->writeable = writeable; |
| 348 | return result; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void usb_device_close(struct usb_device *device) |
| 352 | { |
| 353 | close(device->fd); |
| 354 | free(device); |
| 355 | } |
| 356 | |
Mike Lockwood | cd185f2 | 2010-12-12 14:17:02 -0800 | [diff] [blame] | 357 | struct usb_device *usb_device_new(const char *dev_name, int fd) |
| 358 | { |
| 359 | struct usb_device *device = calloc(1, sizeof(struct usb_device)); |
| 360 | int length; |
| 361 | |
Mike Lockwood | ec9e7b1 | 2011-01-22 09:17:07 -0800 | [diff] [blame] | 362 | D("usb_device_new %s fd: %d\n", dev_name, fd); |
| 363 | |
Mike Lockwood | cd185f2 | 2010-12-12 14:17:02 -0800 | [diff] [blame] | 364 | if (lseek(fd, 0, SEEK_SET) != 0) |
| 365 | goto failed; |
| 366 | length = read(fd, device->desc, sizeof(device->desc)); |
Mike Lockwood | ec9e7b1 | 2011-01-22 09:17:07 -0800 | [diff] [blame] | 367 | D("usb_device_new read returned %d errno %d\n", length, errno); |
Mike Lockwood | cd185f2 | 2010-12-12 14:17:02 -0800 | [diff] [blame] | 368 | if (length < 0) |
| 369 | goto failed; |
| 370 | |
Mike Lockwood | 93aff72 | 2010-12-15 12:58:04 -0800 | [diff] [blame] | 371 | strncpy(device->dev_name, dev_name, sizeof(device->dev_name) - 1); |
Mike Lockwood | cd185f2 | 2010-12-12 14:17:02 -0800 | [diff] [blame] | 372 | device->fd = fd; |
| 373 | device->desc_length = length; |
| 374 | // assume we are writeable, since usb_device_get_fd will only return writeable fds |
| 375 | device->writeable = 1; |
| 376 | return device; |
| 377 | |
| 378 | failed: |
| 379 | close(fd); |
| 380 | free(device); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | static int usb_device_reopen_writeable(struct usb_device *device) |
| 385 | { |
| 386 | if (device->writeable) |
| 387 | return 1; |
| 388 | |
| 389 | int fd = open(device->dev_name, O_RDWR); |
| 390 | if (fd >= 0) { |
| 391 | close(device->fd); |
| 392 | device->fd = fd; |
| 393 | device->writeable = 1; |
| 394 | return 1; |
| 395 | } |
| 396 | D("usb_device_reopen_writeable failed errno %d\n", errno); |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | int usb_device_get_fd(struct usb_device *device) |
| 401 | { |
| 402 | if (!usb_device_reopen_writeable(device)) |
| 403 | return -1; |
| 404 | return device->fd; |
| 405 | } |
| 406 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 407 | const char* usb_device_get_name(struct usb_device *device) |
| 408 | { |
| 409 | return device->dev_name; |
| 410 | } |
| 411 | |
Mike Lockwood | 203f102 | 2010-05-27 10:12:03 -0400 | [diff] [blame] | 412 | int usb_device_get_unique_id(struct usb_device *device) |
| 413 | { |
| 414 | int bus = 0, dev = 0; |
| 415 | sscanf(device->dev_name, USB_FS_ID_SCANNER, &bus, &dev); |
| 416 | return bus * 1000 + dev; |
| 417 | } |
| 418 | |
Mike Lockwood | 07eb4af | 2010-07-27 19:05:33 -0400 | [diff] [blame] | 419 | int usb_device_get_unique_id_from_name(const char* name) |
| 420 | { |
| 421 | int bus = 0, dev = 0; |
| 422 | sscanf(name, USB_FS_ID_SCANNER, &bus, &dev); |
| 423 | return bus * 1000 + dev; |
| 424 | } |
| 425 | |
Mike Lockwood | 7d700f8 | 2010-12-29 08:47:29 -0500 | [diff] [blame] | 426 | char* usb_device_get_name_from_unique_id(int id) |
| 427 | { |
| 428 | int bus = id / 1000; |
| 429 | int dev = id % 1000; |
| 430 | char* result = (char *)calloc(1, strlen(USB_FS_ID_FORMAT)); |
| 431 | snprintf(result, strlen(USB_FS_ID_FORMAT) - 1, USB_FS_ID_FORMAT, bus, dev); |
| 432 | return result; |
| 433 | } |
| 434 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 435 | uint16_t usb_device_get_vendor_id(struct usb_device *device) |
| 436 | { |
| 437 | struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc; |
| 438 | return __le16_to_cpu(desc->idVendor); |
| 439 | } |
| 440 | |
| 441 | uint16_t usb_device_get_product_id(struct usb_device *device) |
| 442 | { |
| 443 | struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc; |
| 444 | return __le16_to_cpu(desc->idProduct); |
| 445 | } |
| 446 | |
Mike Lockwood | 5037207 | 2010-12-13 10:15:25 -0800 | [diff] [blame] | 447 | const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device) |
| 448 | { |
| 449 | return (struct usb_device_descriptor*)device->desc; |
| 450 | } |
| 451 | |
Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 452 | char* usb_device_get_string(struct usb_device *device, int id) |
| 453 | { |
| 454 | char string[256]; |
Mike Lockwood | 0dd1aab | 2015-06-18 13:38:31 -0700 | [diff] [blame] | 455 | __u16 buffer[MAX_STRING_DESCRIPTOR_LENGTH / sizeof(__u16)]; |
| 456 | __u16 languages[MAX_STRING_DESCRIPTOR_LENGTH / sizeof(__u16)]; |
Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 457 | int i, result; |
| 458 | int languageCount = 0; |
| 459 | |
Mike Lockwood | d2e798b | 2014-01-13 09:54:13 -0800 | [diff] [blame] | 460 | if (id == 0) return NULL; |
| 461 | |
Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 462 | string[0] = 0; |
| 463 | memset(languages, 0, sizeof(languages)); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 464 | |
| 465 | // read list of supported languages |
Mike Lockwood | 120b57a | 2011-01-27 10:46:19 -0800 | [diff] [blame] | 466 | result = usb_device_control_transfer(device, |
Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 467 | USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR, |
Mike Lockwood | 120b57a | 2011-01-27 10:46:19 -0800 | [diff] [blame] | 468 | (USB_DT_STRING << 8) | 0, 0, languages, sizeof(languages), 0); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 469 | if (result > 0) |
| 470 | languageCount = (result - 2) / 2; |
| 471 | |
| 472 | for (i = 1; i <= languageCount; i++) { |
| 473 | memset(buffer, 0, sizeof(buffer)); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 474 | |
Mike Lockwood | 120b57a | 2011-01-27 10:46:19 -0800 | [diff] [blame] | 475 | result = usb_device_control_transfer(device, |
Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 476 | USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR, |
Mike Lockwood | 120b57a | 2011-01-27 10:46:19 -0800 | [diff] [blame] | 477 | (USB_DT_STRING << 8) | id, languages[i], buffer, sizeof(buffer), 0); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 478 | if (result > 0) { |
| 479 | int i; |
| 480 | // skip first word, and copy the rest to the string, changing shorts to bytes. |
| 481 | result /= 2; |
| 482 | for (i = 1; i < result; i++) |
| 483 | string[i - 1] = buffer[i]; |
| 484 | string[i - 1] = 0; |
| 485 | return strdup(string); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | char* usb_device_get_manufacturer_name(struct usb_device *device) |
| 493 | { |
| 494 | struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; |
Mike Lockwood | d2e798b | 2014-01-13 09:54:13 -0800 | [diff] [blame] | 495 | return usb_device_get_string(device, desc->iManufacturer); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | char* usb_device_get_product_name(struct usb_device *device) |
| 499 | { |
| 500 | struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; |
Mike Lockwood | d2e798b | 2014-01-13 09:54:13 -0800 | [diff] [blame] | 501 | return usb_device_get_string(device, desc->iProduct); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 502 | } |
| 503 | |
Mike Lockwood | f68600a | 2015-04-29 13:04:10 -0700 | [diff] [blame] | 504 | int usb_device_get_version(struct usb_device *device) |
| 505 | { |
| 506 | struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; |
| 507 | return desc->bcdUSB; |
| 508 | } |
| 509 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 510 | char* usb_device_get_serial(struct usb_device *device) |
| 511 | { |
| 512 | struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; |
Mike Lockwood | d2e798b | 2014-01-13 09:54:13 -0800 | [diff] [blame] | 513 | return usb_device_get_string(device, desc->iSerialNumber); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | int usb_device_is_writeable(struct usb_device *device) |
| 517 | { |
| 518 | return device->writeable; |
| 519 | } |
| 520 | |
| 521 | void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter) |
| 522 | { |
| 523 | iter->config = device->desc; |
| 524 | iter->config_end = device->desc + device->desc_length; |
| 525 | iter->curr_desc = device->desc; |
| 526 | } |
| 527 | |
| 528 | struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter) |
| 529 | { |
| 530 | struct usb_descriptor_header* next; |
| 531 | if (iter->curr_desc >= iter->config_end) |
| 532 | return NULL; |
| 533 | next = (struct usb_descriptor_header*)iter->curr_desc; |
| 534 | iter->curr_desc += next->bLength; |
| 535 | return next; |
| 536 | } |
| 537 | |
| 538 | int usb_device_claim_interface(struct usb_device *device, unsigned int interface) |
| 539 | { |
| 540 | return ioctl(device->fd, USBDEVFS_CLAIMINTERFACE, &interface); |
| 541 | } |
| 542 | |
| 543 | int usb_device_release_interface(struct usb_device *device, unsigned int interface) |
| 544 | { |
| 545 | return ioctl(device->fd, USBDEVFS_RELEASEINTERFACE, &interface); |
| 546 | } |
| 547 | |
Mike Lockwood | ec9e7b1 | 2011-01-22 09:17:07 -0800 | [diff] [blame] | 548 | int usb_device_connect_kernel_driver(struct usb_device *device, |
| 549 | unsigned int interface, int connect) |
| 550 | { |
| 551 | struct usbdevfs_ioctl ctl; |
| 552 | |
| 553 | ctl.ifno = interface; |
| 554 | ctl.ioctl_code = (connect ? USBDEVFS_CONNECT : USBDEVFS_DISCONNECT); |
| 555 | ctl.data = NULL; |
| 556 | return ioctl(device->fd, USBDEVFS_IOCTL, &ctl); |
| 557 | } |
| 558 | |
Mike Lockwood | d2e798b | 2014-01-13 09:54:13 -0800 | [diff] [blame] | 559 | int usb_device_set_configuration(struct usb_device *device, int configuration) |
| 560 | { |
| 561 | return ioctl(device->fd, USBDEVFS_SETCONFIGURATION, &configuration); |
| 562 | } |
| 563 | |
| 564 | int usb_device_set_interface(struct usb_device *device, unsigned int interface, |
| 565 | unsigned int alt_setting) |
| 566 | { |
| 567 | struct usbdevfs_setinterface ctl; |
| 568 | |
| 569 | ctl.interface = interface; |
| 570 | ctl.altsetting = alt_setting; |
| 571 | return ioctl(device->fd, USBDEVFS_SETINTERFACE, &ctl); |
| 572 | } |
| 573 | |
Mike Lockwood | 120b57a | 2011-01-27 10:46:19 -0800 | [diff] [blame] | 574 | int usb_device_control_transfer(struct usb_device *device, |
| 575 | int requestType, |
| 576 | int request, |
| 577 | int value, |
| 578 | int index, |
| 579 | void* buffer, |
| 580 | int length, |
| 581 | unsigned int timeout) |
| 582 | { |
| 583 | struct usbdevfs_ctrltransfer ctrl; |
| 584 | |
| 585 | // this usually requires read/write permission |
| 586 | if (!usb_device_reopen_writeable(device)) |
| 587 | return -1; |
| 588 | |
| 589 | memset(&ctrl, 0, sizeof(ctrl)); |
| 590 | ctrl.bRequestType = requestType; |
| 591 | ctrl.bRequest = request; |
| 592 | ctrl.wValue = value; |
| 593 | ctrl.wIndex = index; |
| 594 | ctrl.wLength = length; |
| 595 | ctrl.data = buffer; |
| 596 | ctrl.timeout = timeout; |
| 597 | return ioctl(device->fd, USBDEVFS_CONTROL, &ctrl); |
| 598 | } |
| 599 | |
| 600 | int usb_device_bulk_transfer(struct usb_device *device, |
| 601 | int endpoint, |
| 602 | void* buffer, |
| 603 | int length, |
| 604 | unsigned int timeout) |
| 605 | { |
| 606 | struct usbdevfs_bulktransfer ctrl; |
| 607 | |
Mike Lockwood | c4c00d8 | 2011-03-12 12:25:11 -0500 | [diff] [blame] | 608 | // need to limit request size to avoid EINVAL |
| 609 | if (length > MAX_USBFS_BUFFER_SIZE) |
| 610 | length = MAX_USBFS_BUFFER_SIZE; |
| 611 | |
Mike Lockwood | 120b57a | 2011-01-27 10:46:19 -0800 | [diff] [blame] | 612 | memset(&ctrl, 0, sizeof(ctrl)); |
| 613 | ctrl.ep = endpoint; |
| 614 | ctrl.len = length; |
| 615 | ctrl.data = buffer; |
| 616 | ctrl.timeout = timeout; |
| 617 | return ioctl(device->fd, USBDEVFS_BULK, &ctrl); |
| 618 | } |
| 619 | |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 620 | struct usb_request *usb_request_new(struct usb_device *dev, |
| 621 | const struct usb_endpoint_descriptor *ep_desc) |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 622 | { |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 623 | struct usbdevfs_urb *urb = calloc(1, sizeof(struct usbdevfs_urb)); |
| 624 | if (!urb) |
| 625 | return NULL; |
| 626 | |
| 627 | if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) |
| 628 | urb->type = USBDEVFS_URB_TYPE_BULK; |
| 629 | else if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) |
| 630 | urb->type = USBDEVFS_URB_TYPE_INTERRUPT; |
| 631 | else { |
| 632 | D("Unsupported endpoint type %d", ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK); |
| 633 | free(urb); |
| 634 | return NULL; |
| 635 | } |
| 636 | urb->endpoint = ep_desc->bEndpointAddress; |
| 637 | |
| 638 | struct usb_request *req = calloc(1, sizeof(struct usb_request)); |
| 639 | if (!req) { |
| 640 | free(urb); |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | req->dev = dev; |
| 645 | req->max_packet_size = __le16_to_cpu(ep_desc->wMaxPacketSize); |
| 646 | req->private_data = urb; |
Mike Lockwood | b5d68a3 | 2011-02-14 08:05:40 -0500 | [diff] [blame] | 647 | req->endpoint = urb->endpoint; |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 648 | urb->usercontext = req; |
| 649 | |
| 650 | return req; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 651 | } |
| 652 | |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 653 | void usb_request_free(struct usb_request *req) |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 654 | { |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 655 | free(req->private_data); |
| 656 | free(req); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 657 | } |
| 658 | |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 659 | int usb_request_queue(struct usb_request *req) |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 660 | { |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 661 | struct usbdevfs_urb *urb = (struct usbdevfs_urb*)req->private_data; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 662 | int res; |
| 663 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 664 | urb->status = -1; |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 665 | urb->buffer = req->buffer; |
Mike Lockwood | c4c00d8 | 2011-03-12 12:25:11 -0500 | [diff] [blame] | 666 | // need to limit request size to avoid EINVAL |
| 667 | if (req->buffer_length > MAX_USBFS_BUFFER_SIZE) |
| 668 | urb->buffer_length = MAX_USBFS_BUFFER_SIZE; |
| 669 | else |
| 670 | urb->buffer_length = req->buffer_length; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 671 | |
| 672 | do { |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 673 | res = ioctl(req->dev->fd, USBDEVFS_SUBMITURB, urb); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 674 | } while((res < 0) && (errno == EINTR)); |
| 675 | |
| 676 | return res; |
| 677 | } |
| 678 | |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 679 | struct usb_request *usb_request_wait(struct usb_device *dev) |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 680 | { |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 681 | struct usbdevfs_urb *urb = NULL; |
| 682 | struct usb_request *req = NULL; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 683 | |
| 684 | while (1) { |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 685 | int res = ioctl(dev->fd, USBDEVFS_REAPURB, &urb); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 686 | D("USBDEVFS_REAPURB returned %d\n", res); |
| 687 | if (res < 0) { |
| 688 | if(errno == EINTR) { |
| 689 | continue; |
| 690 | } |
| 691 | D("[ reap urb - error ]\n"); |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 692 | return NULL; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 693 | } else { |
| 694 | D("[ urb @%p status = %d, actual = %d ]\n", |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 695 | urb, urb->status, urb->actual_length); |
| 696 | req = (struct usb_request*)urb->usercontext; |
| 697 | req->actual_length = urb->actual_length; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 698 | } |
| 699 | break; |
| 700 | } |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 701 | return req; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 702 | } |
| 703 | |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 704 | int usb_request_cancel(struct usb_request *req) |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 705 | { |
Mike Lockwood | e533c5f | 2011-01-04 20:04:36 -0500 | [diff] [blame] | 706 | struct usbdevfs_urb *urb = ((struct usbdevfs_urb*)req->private_data); |
Badhri Jagan Sridharan | ef4087b | 2014-08-06 12:34:30 -0700 | [diff] [blame] | 707 | return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, urb); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 708 | } |
| 709 | |