blob: 8be393ee7347826258e4232dc05e3e3d019f9c3f [file] [log] [blame]
Mike Lockwood30ff2c72010-05-09 16:23:47 -04001/*
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 Lockwoode15af092010-06-10 10:20:49 -040017// #define DEBUG 1
18#if DEBUG
19
20#ifdef USE_LIBLOG
21#define LOG_TAG "usbhost"
22#include "utils/Log.h"
Steve Block8d66c492011-12-20 16:07:45 +000023#define D ALOGD
Mike Lockwoode15af092010-06-10 10:20:49 -040024#else
25#define D printf
26#endif
27
28#else
29#define D(...)
30#endif
31
Mike Lockwood30ff2c72010-05-09 16:23:47 -040032#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
35#include <string.h>
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020036#include <stddef.h>
Mike Lockwood30ff2c72010-05-09 16:23:47 -040037
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 Lockwood30ff2c72010-05-09 16:23:47 -040049#include <asm/byteorder.h>
50
51#include "usbhost/usbhost.h"
52
Benoit Gobyf4de0782012-08-01 18:54:02 -070053#define DEV_DIR "/dev"
Ziv Hendela306ced2013-08-07 19:29:17 +030054#define DEV_BUS_DIR DEV_DIR "/bus"
55#define USB_FS_DIR DEV_BUS_DIR "/usb"
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020056#define USB_FS_ID_SCANNER USB_FS_DIR "/%d/%d"
57#define USB_FS_ID_FORMAT USB_FS_DIR "/%03d/%03d"
Mike Lockwood30ff2c72010-05-09 16:23:47 -040058
Mike Lockwoodc4c00d82011-03-12 12:25:11 -050059// From drivers/usb/core/devio.c
60// I don't know why this isn't in a kernel header
61#define MAX_USBFS_BUFFER_SIZE 16384
Mike Lockwood30ff2c72010-05-09 16:23:47 -040062
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020063#define MAX_USBFS_WD_COUNT 10
64
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040065struct usb_host_context {
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020066 int fd;
67 usb_device_added_cb cb_added;
68 usb_device_removed_cb cb_removed;
69 void *data;
70 int wds[MAX_USBFS_WD_COUNT];
71 int wdd;
Ziv Hendela306ced2013-08-07 19:29:17 +030072 int wddbus;
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040073};
74
Mike Lockwood30ff2c72010-05-09 16:23:47 -040075struct usb_device {
76 char dev_name[64];
Mike Lockwoodec9e7b12011-01-22 09:17:07 -080077 unsigned char desc[4096];
Mike Lockwood30ff2c72010-05-09 16:23:47 -040078 int desc_length;
79 int fd;
80 int writeable;
81};
82
Mike Lockwood30ff2c72010-05-09 16:23:47 -040083static inline int badname(const char *name)
84{
85 while(*name) {
86 if(!isdigit(*name++)) return 1;
87 }
88 return 0;
89}
90
Benoit Goby6cc883c2012-07-31 19:22:06 -070091static int find_existing_devices_bus(char *busname,
92 usb_device_added_cb added_cb,
93 void *client_data)
94{
95 char devname[32];
96 DIR *devdir;
97 struct dirent *de;
98 int done = 0;
99
100 devdir = opendir(busname);
101 if(devdir == 0) return 0;
102
103 while ((de = readdir(devdir)) && !done) {
104 if(badname(de->d_name)) continue;
105
106 snprintf(devname, sizeof(devname), "%s/%s", busname, de->d_name);
107 done = added_cb(devname, client_data);
108 } // end of devdir while
109 closedir(devdir);
110
111 return done;
112}
113
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400114/* returns true if one of the callbacks indicates we are done */
115static int find_existing_devices(usb_device_added_cb added_cb,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400116 void *client_data)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400117{
Benoit Goby6cc883c2012-07-31 19:22:06 -0700118 char busname[32];
119 DIR *busdir;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400120 struct dirent *de;
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400121 int done = 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400122
123 busdir = opendir(USB_FS_DIR);
Benoit Gobyf4de0782012-08-01 18:54:02 -0700124 if(busdir == 0) return 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400125
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400126 while ((de = readdir(busdir)) != 0 && !done) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400127 if(badname(de->d_name)) continue;
128
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200129 snprintf(busname, sizeof(busname), USB_FS_DIR "/%s", de->d_name);
Benoit Goby6cc883c2012-07-31 19:22:06 -0700130 done = find_existing_devices_bus(busname, added_cb,
131 client_data);
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200132 } //end of busdir while
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400133 closedir(busdir);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400134
135 return done;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400136}
137
Benoit Gobyf4de0782012-08-01 18:54:02 -0700138static void watch_existing_subdirs(struct usb_host_context *context,
139 int *wds, int wd_count)
140{
141 char path[100];
142 int i, ret;
143
144 wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE);
145 if (wds[0] < 0)
146 return;
147
148 /* watch existing subdirectories of USB_FS_DIR */
149 for (i = 1; i < wd_count; i++) {
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200150 snprintf(path, sizeof(path), USB_FS_DIR "/%03d", i);
Benoit Gobyf4de0782012-08-01 18:54:02 -0700151 ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE);
152 if (ret >= 0)
153 wds[i] = ret;
154 }
155}
156
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400157struct usb_host_context *usb_host_init()
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400158{
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400159 struct usb_host_context *context = calloc(1, sizeof(struct usb_host_context));
160 if (!context) {
161 fprintf(stderr, "out of memory in usb_host_context\n");
162 return NULL;
163 }
164 context->fd = inotify_init();
165 if (context->fd < 0) {
166 fprintf(stderr, "inotify_init failed\n");
167 free(context);
168 return NULL;
169 }
170 return context;
171}
172
173void usb_host_cleanup(struct usb_host_context *context)
174{
175 close(context->fd);
176 free(context);
177}
178
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200179int usb_host_get_fd(struct usb_host_context *context)
180{
181 return context->fd;
182} /* usb_host_get_fd() */
183
184int usb_host_load(struct usb_host_context *context,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400185 usb_device_added_cb added_cb,
186 usb_device_removed_cb removed_cb,
Mike Lockwooda8055192010-07-19 20:15:15 -0400187 usb_discovery_done_cb discovery_done_cb,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400188 void *client_data)
189{
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200190 int done = 0;
191 int i;
192
193 context->cb_added = added_cb;
194 context->cb_removed = removed_cb;
195 context->data = client_data;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400196
197 D("Created device discovery thread\n");
198
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400199 /* watch for files added and deleted within USB_FS_DIR */
Ziv Hendela306ced2013-08-07 19:29:17 +0300200 context->wddbus = -1;
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200201 for (i = 0; i < MAX_USBFS_WD_COUNT; i++)
202 context->wds[i] = -1;
Benoit Gobyf4de0782012-08-01 18:54:02 -0700203
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400204 /* watch the root for new subdirectories */
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200205 context->wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE);
206 if (context->wdd < 0) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400207 fprintf(stderr, "inotify_add_watch failed\n");
Mike Lockwoode8849d12010-07-20 16:31:42 -0400208 if (discovery_done_cb)
209 discovery_done_cb(client_data);
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200210 return done;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400211 }
212
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200213 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400214
215 /* check for existing devices first, after we have inotify set up */
Benoit Goby6cc883c2012-07-31 19:22:06 -0700216 done = find_existing_devices(added_cb, client_data);
Mike Lockwooda8055192010-07-19 20:15:15 -0400217 if (discovery_done_cb)
218 done |= discovery_done_cb(client_data);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400219
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200220 return done;
221} /* usb_host_load() */
222
223int usb_host_read_event(struct usb_host_context *context)
224{
225 struct inotify_event* event;
226 char event_buf[512];
227 char path[100];
228 int i, ret, done = 0;
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200229 int offset = 0;
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200230 int wd;
231
232 ret = read(context->fd, event_buf, sizeof(event_buf));
233 if (ret >= (int)sizeof(struct inotify_event)) {
Ziv Hendela306ced2013-08-07 19:29:17 +0300234 while (offset < ret && !done) {
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200235 event = (struct inotify_event*)&event_buf[offset];
236 done = 0;
237 wd = event->wd;
238 if (wd == context->wdd) {
239 if ((event->mask & IN_CREATE) && !strcmp(event->name, "bus")) {
Ziv Hendela306ced2013-08-07 19:29:17 +0300240 context->wddbus = inotify_add_watch(context->fd, DEV_BUS_DIR, IN_CREATE | IN_DELETE);
241 if (context->wddbus < 0) {
242 done = 1;
243 } else {
244 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
245 done = find_existing_devices(context->cb_added, context->data);
246 }
247 }
248 } else if (wd == context->wddbus) {
249 if ((event->mask & IN_CREATE) && !strcmp(event->name, "usb")) {
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200250 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
251 done = find_existing_devices(context->cb_added, context->data);
Ziv Hendela306ced2013-08-07 19:29:17 +0300252 } else if ((event->mask & IN_DELETE) && !strcmp(event->name, "usb")) {
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200253 for (i = 0; i < MAX_USBFS_WD_COUNT; i++) {
254 if (context->wds[i] >= 0) {
255 inotify_rm_watch(context->fd, context->wds[i]);
256 context->wds[i] = -1;
257 }
258 }
259 }
260 } else if (wd == context->wds[0]) {
261 i = atoi(event->name);
262 snprintf(path, sizeof(path), USB_FS_DIR "/%s", event->name);
263 D("%s subdirectory %s: index: %d\n", (event->mask & IN_CREATE) ?
264 "new" : "gone", path, i);
265 if (i > 0 && i < MAX_USBFS_WD_COUNT) {
266 if (event->mask & IN_CREATE) {
267 ret = inotify_add_watch(context->fd, path,
268 IN_CREATE | IN_DELETE);
269 if (ret >= 0)
270 context->wds[i] = ret;
271 done = find_existing_devices_bus(path, context->cb_added,
272 context->data);
273 } else if (event->mask & IN_DELETE) {
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200274 inotify_rm_watch(context->fd, context->wds[i]);
275 context->wds[i] = -1;
Benoit Gobyf4de0782012-08-01 18:54:02 -0700276 }
277 }
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200278 } else {
279 for (i = 1; (i < MAX_USBFS_WD_COUNT) && !done; i++) {
280 if (wd == context->wds[i]) {
281 snprintf(path, sizeof(path), USB_FS_DIR "/%03d/%s", i, event->name);
282 if (event->mask == IN_CREATE) {
283 D("new device %s\n", path);
284 done = context->cb_added(path, context->data);
285 } else if (event->mask == IN_DELETE) {
286 D("gone device %s\n", path);
287 done = context->cb_removed(path, context->data);
288 }
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400289 }
290 }
291 }
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200292
293 offset += sizeof(struct inotify_event) + event->len;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400294 }
295 }
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200296
297 return done;
298} /* usb_host_read_event() */
299
300void usb_host_run(struct usb_host_context *context,
301 usb_device_added_cb added_cb,
302 usb_device_removed_cb removed_cb,
303 usb_discovery_done_cb discovery_done_cb,
304 void *client_data)
305{
306 int done;
307
308 done = usb_host_load(context, added_cb, removed_cb, discovery_done_cb, client_data);
309
310 while (!done) {
311
312 done = usb_host_read_event(context);
313 }
314} /* usb_host_run() */
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400315
316struct usb_device *usb_device_open(const char *dev_name)
317{
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800318 int fd, did_retry = 0, writeable = 1;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400319
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800320 D("usb_device_open %s\n", dev_name);
321
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400322retry:
323 fd = open(dev_name, O_RDWR);
324 if (fd < 0) {
325 /* if we fail, see if have read-only access */
326 fd = open(dev_name, O_RDONLY);
Mike Lockwoode15af092010-06-10 10:20:49 -0400327 D("usb_device_open open returned %d errno %d\n", fd, errno);
328 if (fd < 0 && (errno == EACCES || errno == ENOENT) && !did_retry) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400329 /* work around race condition between inotify and permissions management */
330 sleep(1);
331 did_retry = 1;
332 goto retry;
333 }
334
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800335 if (fd < 0)
336 return NULL;
337 writeable = 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400338 D("[ usb open read-only %s fd = %d]\n", dev_name, fd);
339 }
340
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800341 struct usb_device* result = usb_device_new(dev_name, fd);
342 if (result)
343 result->writeable = writeable;
344 return result;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400345}
346
347void usb_device_close(struct usb_device *device)
348{
349 close(device->fd);
350 free(device);
351}
352
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800353struct usb_device *usb_device_new(const char *dev_name, int fd)
354{
355 struct usb_device *device = calloc(1, sizeof(struct usb_device));
356 int length;
357
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800358 D("usb_device_new %s fd: %d\n", dev_name, fd);
359
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800360 if (lseek(fd, 0, SEEK_SET) != 0)
361 goto failed;
362 length = read(fd, device->desc, sizeof(device->desc));
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800363 D("usb_device_new read returned %d errno %d\n", length, errno);
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800364 if (length < 0)
365 goto failed;
366
Mike Lockwood93aff722010-12-15 12:58:04 -0800367 strncpy(device->dev_name, dev_name, sizeof(device->dev_name) - 1);
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800368 device->fd = fd;
369 device->desc_length = length;
370 // assume we are writeable, since usb_device_get_fd will only return writeable fds
371 device->writeable = 1;
372 return device;
373
374failed:
375 close(fd);
376 free(device);
377 return NULL;
378}
379
380static int usb_device_reopen_writeable(struct usb_device *device)
381{
382 if (device->writeable)
383 return 1;
384
385 int fd = open(device->dev_name, O_RDWR);
386 if (fd >= 0) {
387 close(device->fd);
388 device->fd = fd;
389 device->writeable = 1;
390 return 1;
391 }
392 D("usb_device_reopen_writeable failed errno %d\n", errno);
393 return 0;
394}
395
396int usb_device_get_fd(struct usb_device *device)
397{
398 if (!usb_device_reopen_writeable(device))
399 return -1;
400 return device->fd;
401}
402
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400403const char* usb_device_get_name(struct usb_device *device)
404{
405 return device->dev_name;
406}
407
Mike Lockwood203f1022010-05-27 10:12:03 -0400408int usb_device_get_unique_id(struct usb_device *device)
409{
410 int bus = 0, dev = 0;
411 sscanf(device->dev_name, USB_FS_ID_SCANNER, &bus, &dev);
412 return bus * 1000 + dev;
413}
414
Mike Lockwood07eb4af2010-07-27 19:05:33 -0400415int usb_device_get_unique_id_from_name(const char* name)
416{
417 int bus = 0, dev = 0;
418 sscanf(name, USB_FS_ID_SCANNER, &bus, &dev);
419 return bus * 1000 + dev;
420}
421
Mike Lockwood7d700f82010-12-29 08:47:29 -0500422char* usb_device_get_name_from_unique_id(int id)
423{
424 int bus = id / 1000;
425 int dev = id % 1000;
426 char* result = (char *)calloc(1, strlen(USB_FS_ID_FORMAT));
427 snprintf(result, strlen(USB_FS_ID_FORMAT) - 1, USB_FS_ID_FORMAT, bus, dev);
428 return result;
429}
430
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400431uint16_t usb_device_get_vendor_id(struct usb_device *device)
432{
433 struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;
434 return __le16_to_cpu(desc->idVendor);
435}
436
437uint16_t usb_device_get_product_id(struct usb_device *device)
438{
439 struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;
440 return __le16_to_cpu(desc->idProduct);
441}
442
Mike Lockwood50372072010-12-13 10:15:25 -0800443const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device)
444{
445 return (struct usb_device_descriptor*)device->desc;
446}
447
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400448char* usb_device_get_string(struct usb_device *device, int id)
449{
450 char string[256];
451 __u16 buffer[128];
452 __u16 languages[128];
453 int i, result;
454 int languageCount = 0;
455
456 string[0] = 0;
457 memset(languages, 0, sizeof(languages));
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400458
459 // read list of supported languages
Mike Lockwood120b57a2011-01-27 10:46:19 -0800460 result = usb_device_control_transfer(device,
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400461 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
Mike Lockwood120b57a2011-01-27 10:46:19 -0800462 (USB_DT_STRING << 8) | 0, 0, languages, sizeof(languages), 0);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400463 if (result > 0)
464 languageCount = (result - 2) / 2;
465
466 for (i = 1; i <= languageCount; i++) {
467 memset(buffer, 0, sizeof(buffer));
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400468
Mike Lockwood120b57a2011-01-27 10:46:19 -0800469 result = usb_device_control_transfer(device,
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400470 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
Mike Lockwood120b57a2011-01-27 10:46:19 -0800471 (USB_DT_STRING << 8) | id, languages[i], buffer, sizeof(buffer), 0);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400472 if (result > 0) {
473 int i;
474 // skip first word, and copy the rest to the string, changing shorts to bytes.
475 result /= 2;
476 for (i = 1; i < result; i++)
477 string[i - 1] = buffer[i];
478 string[i - 1] = 0;
479 return strdup(string);
480 }
481 }
482
483 return NULL;
484}
485
486char* usb_device_get_manufacturer_name(struct usb_device *device)
487{
488 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
489
490 if (desc->iManufacturer)
491 return usb_device_get_string(device, desc->iManufacturer);
492 else
493 return NULL;
494}
495
496char* usb_device_get_product_name(struct usb_device *device)
497{
498 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
499
500 if (desc->iProduct)
501 return usb_device_get_string(device, desc->iProduct);
502 else
503 return NULL;
504}
505
506char* usb_device_get_serial(struct usb_device *device)
507{
508 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
509
510 if (desc->iSerialNumber)
511 return usb_device_get_string(device, desc->iSerialNumber);
512 else
513 return NULL;
514}
515
516int usb_device_is_writeable(struct usb_device *device)
517{
518 return device->writeable;
519}
520
521void 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
528struct 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
538int usb_device_claim_interface(struct usb_device *device, unsigned int interface)
539{
540 return ioctl(device->fd, USBDEVFS_CLAIMINTERFACE, &interface);
541}
542
543int usb_device_release_interface(struct usb_device *device, unsigned int interface)
544{
545 return ioctl(device->fd, USBDEVFS_RELEASEINTERFACE, &interface);
546}
547
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800548int 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 Lockwood120b57a2011-01-27 10:46:19 -0800559int usb_device_control_transfer(struct usb_device *device,
560 int requestType,
561 int request,
562 int value,
563 int index,
564 void* buffer,
565 int length,
566 unsigned int timeout)
567{
568 struct usbdevfs_ctrltransfer ctrl;
569
570 // this usually requires read/write permission
571 if (!usb_device_reopen_writeable(device))
572 return -1;
573
574 memset(&ctrl, 0, sizeof(ctrl));
575 ctrl.bRequestType = requestType;
576 ctrl.bRequest = request;
577 ctrl.wValue = value;
578 ctrl.wIndex = index;
579 ctrl.wLength = length;
580 ctrl.data = buffer;
581 ctrl.timeout = timeout;
582 return ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
583}
584
585int usb_device_bulk_transfer(struct usb_device *device,
586 int endpoint,
587 void* buffer,
588 int length,
589 unsigned int timeout)
590{
591 struct usbdevfs_bulktransfer ctrl;
592
Mike Lockwoodc4c00d82011-03-12 12:25:11 -0500593 // need to limit request size to avoid EINVAL
594 if (length > MAX_USBFS_BUFFER_SIZE)
595 length = MAX_USBFS_BUFFER_SIZE;
596
Mike Lockwood120b57a2011-01-27 10:46:19 -0800597 memset(&ctrl, 0, sizeof(ctrl));
598 ctrl.ep = endpoint;
599 ctrl.len = length;
600 ctrl.data = buffer;
601 ctrl.timeout = timeout;
602 return ioctl(device->fd, USBDEVFS_BULK, &ctrl);
603}
604
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500605struct usb_request *usb_request_new(struct usb_device *dev,
606 const struct usb_endpoint_descriptor *ep_desc)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400607{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500608 struct usbdevfs_urb *urb = calloc(1, sizeof(struct usbdevfs_urb));
609 if (!urb)
610 return NULL;
611
612 if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK)
613 urb->type = USBDEVFS_URB_TYPE_BULK;
614 else if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
615 urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
616 else {
617 D("Unsupported endpoint type %d", ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
618 free(urb);
619 return NULL;
620 }
621 urb->endpoint = ep_desc->bEndpointAddress;
622
623 struct usb_request *req = calloc(1, sizeof(struct usb_request));
624 if (!req) {
625 free(urb);
626 return NULL;
627 }
628
629 req->dev = dev;
630 req->max_packet_size = __le16_to_cpu(ep_desc->wMaxPacketSize);
631 req->private_data = urb;
Mike Lockwoodb5d68a32011-02-14 08:05:40 -0500632 req->endpoint = urb->endpoint;
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500633 urb->usercontext = req;
634
635 return req;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400636}
637
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500638void usb_request_free(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400639{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500640 free(req->private_data);
641 free(req);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400642}
643
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500644int usb_request_queue(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400645{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500646 struct usbdevfs_urb *urb = (struct usbdevfs_urb*)req->private_data;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400647 int res;
648
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400649 urb->status = -1;
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500650 urb->buffer = req->buffer;
Mike Lockwoodc4c00d82011-03-12 12:25:11 -0500651 // need to limit request size to avoid EINVAL
652 if (req->buffer_length > MAX_USBFS_BUFFER_SIZE)
653 urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
654 else
655 urb->buffer_length = req->buffer_length;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400656
657 do {
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500658 res = ioctl(req->dev->fd, USBDEVFS_SUBMITURB, urb);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400659 } while((res < 0) && (errno == EINTR));
660
661 return res;
662}
663
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500664struct usb_request *usb_request_wait(struct usb_device *dev)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400665{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500666 struct usbdevfs_urb *urb = NULL;
667 struct usb_request *req = NULL;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400668
669 while (1) {
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500670 int res = ioctl(dev->fd, USBDEVFS_REAPURB, &urb);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400671 D("USBDEVFS_REAPURB returned %d\n", res);
672 if (res < 0) {
673 if(errno == EINTR) {
674 continue;
675 }
676 D("[ reap urb - error ]\n");
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500677 return NULL;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400678 } else {
679 D("[ urb @%p status = %d, actual = %d ]\n",
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500680 urb, urb->status, urb->actual_length);
681 req = (struct usb_request*)urb->usercontext;
682 req->actual_length = urb->actual_length;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400683 }
684 break;
685 }
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500686 return req;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400687}
688
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500689int usb_request_cancel(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400690{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500691 struct usbdevfs_urb *urb = ((struct usbdevfs_urb*)req->private_data);
692 return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, &urb);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400693}
694