blob: b8e32154f064b9020bc0e3ab006c1289dc314614 [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 Lockwood0dd1aab2015-06-18 13:38:31 -070059// Some devices fail to send string descriptors if we attempt reading > 255 bytes
60#define MAX_STRING_DESCRIPTOR_LENGTH 255
61
Mike Lockwoodc4c00d82011-03-12 12:25:11 -050062// 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 Lockwood30ff2c72010-05-09 16:23:47 -040065
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020066#define MAX_USBFS_WD_COUNT 10
67
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040068struct usb_host_context {
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020069 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 Hendela306ced2013-08-07 19:29:17 +030075 int wddbus;
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040076};
77
Mike Lockwood30ff2c72010-05-09 16:23:47 -040078struct usb_device {
79 char dev_name[64];
Mike Lockwoodec9e7b12011-01-22 09:17:07 -080080 unsigned char desc[4096];
Mike Lockwood30ff2c72010-05-09 16:23:47 -040081 int desc_length;
82 int fd;
83 int writeable;
84};
85
Mike Lockwood30ff2c72010-05-09 16:23:47 -040086static inline int badname(const char *name)
87{
88 while(*name) {
89 if(!isdigit(*name++)) return 1;
90 }
91 return 0;
92}
93
Benoit Goby6cc883c2012-07-31 19:22:06 -070094static 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 Lockwood7a96ba42010-07-01 11:33:41 -0400117/* returns true if one of the callbacks indicates we are done */
118static int find_existing_devices(usb_device_added_cb added_cb,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400119 void *client_data)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400120{
Benoit Goby6cc883c2012-07-31 19:22:06 -0700121 char busname[32];
122 DIR *busdir;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400123 struct dirent *de;
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400124 int done = 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400125
126 busdir = opendir(USB_FS_DIR);
Benoit Gobyf4de0782012-08-01 18:54:02 -0700127 if(busdir == 0) return 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400128
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400129 while ((de = readdir(busdir)) != 0 && !done) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400130 if(badname(de->d_name)) continue;
131
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200132 snprintf(busname, sizeof(busname), USB_FS_DIR "/%s", de->d_name);
Benoit Goby6cc883c2012-07-31 19:22:06 -0700133 done = find_existing_devices_bus(busname, added_cb,
134 client_data);
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200135 } //end of busdir while
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400136 closedir(busdir);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400137
138 return done;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400139}
140
Benoit Gobyf4de0782012-08-01 18:54:02 -0700141static 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 Ranquetdea46b62012-10-23 17:11:44 +0200153 snprintf(path, sizeof(path), USB_FS_DIR "/%03d", i);
Benoit Gobyf4de0782012-08-01 18:54:02 -0700154 ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE);
155 if (ret >= 0)
156 wds[i] = ret;
157 }
158}
159
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400160struct usb_host_context *usb_host_init()
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400161{
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400162 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
176void usb_host_cleanup(struct usb_host_context *context)
177{
178 close(context->fd);
179 free(context);
180}
181
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200182int usb_host_get_fd(struct usb_host_context *context)
183{
184 return context->fd;
185} /* usb_host_get_fd() */
186
187int usb_host_load(struct usb_host_context *context,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400188 usb_device_added_cb added_cb,
189 usb_device_removed_cb removed_cb,
Mike Lockwooda8055192010-07-19 20:15:15 -0400190 usb_discovery_done_cb discovery_done_cb,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400191 void *client_data)
192{
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200193 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 Lockwood30ff2c72010-05-09 16:23:47 -0400199
200 D("Created device discovery thread\n");
201
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400202 /* watch for files added and deleted within USB_FS_DIR */
Ziv Hendela306ced2013-08-07 19:29:17 +0300203 context->wddbus = -1;
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200204 for (i = 0; i < MAX_USBFS_WD_COUNT; i++)
205 context->wds[i] = -1;
Benoit Gobyf4de0782012-08-01 18:54:02 -0700206
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400207 /* watch the root for new subdirectories */
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200208 context->wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE);
209 if (context->wdd < 0) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400210 fprintf(stderr, "inotify_add_watch failed\n");
Mike Lockwoode8849d12010-07-20 16:31:42 -0400211 if (discovery_done_cb)
212 discovery_done_cb(client_data);
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200213 return done;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400214 }
215
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200216 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400217
218 /* check for existing devices first, after we have inotify set up */
Benoit Goby6cc883c2012-07-31 19:22:06 -0700219 done = find_existing_devices(added_cb, client_data);
Mike Lockwooda8055192010-07-19 20:15:15 -0400220 if (discovery_done_cb)
221 done |= discovery_done_cb(client_data);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400222
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200223 return done;
224} /* usb_host_load() */
225
226int 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 Hendelf75ea8d2013-03-24 18:10:52 +0200232 int offset = 0;
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200233 int wd;
234
235 ret = read(context->fd, event_buf, sizeof(event_buf));
236 if (ret >= (int)sizeof(struct inotify_event)) {
Ziv Hendela306ced2013-08-07 19:29:17 +0300237 while (offset < ret && !done) {
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200238 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 Hendela306ced2013-08-07 19:29:17 +0300243 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 Hendelf75ea8d2013-03-24 18:10:52 +0200253 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
254 done = find_existing_devices(context->cb_added, context->data);
Ziv Hendela306ced2013-08-07 19:29:17 +0300255 } else if ((event->mask & IN_DELETE) && !strcmp(event->name, "usb")) {
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200256 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 Huang3c1d7b32014-01-21 14:25:22 +0800269 int local_ret = 0;
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200270 if (event->mask & IN_CREATE) {
Bo Huang3c1d7b32014-01-21 14:25:22 +0800271 local_ret = inotify_add_watch(context->fd, path,
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200272 IN_CREATE | IN_DELETE);
Bo Huang3c1d7b32014-01-21 14:25:22 +0800273 if (local_ret >= 0)
274 context->wds[i] = local_ret;
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200275 done = find_existing_devices_bus(path, context->cb_added,
276 context->data);
277 } else if (event->mask & IN_DELETE) {
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200278 inotify_rm_watch(context->fd, context->wds[i]);
279 context->wds[i] = -1;
Benoit Gobyf4de0782012-08-01 18:54:02 -0700280 }
281 }
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200282 } 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 Lockwood30ff2c72010-05-09 16:23:47 -0400293 }
294 }
295 }
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200296
297 offset += sizeof(struct inotify_event) + event->len;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400298 }
299 }
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200300
301 return done;
302} /* usb_host_read_event() */
303
304void 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 Lockwood30ff2c72010-05-09 16:23:47 -0400319
320struct usb_device *usb_device_open(const char *dev_name)
321{
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800322 int fd, did_retry = 0, writeable = 1;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400323
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800324 D("usb_device_open %s\n", dev_name);
325
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400326retry:
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 Lockwoode15af092010-06-10 10:20:49 -0400331 D("usb_device_open open returned %d errno %d\n", fd, errno);
332 if (fd < 0 && (errno == EACCES || errno == ENOENT) && !did_retry) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400333 /* work around race condition between inotify and permissions management */
334 sleep(1);
335 did_retry = 1;
336 goto retry;
337 }
338
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800339 if (fd < 0)
340 return NULL;
341 writeable = 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400342 D("[ usb open read-only %s fd = %d]\n", dev_name, fd);
343 }
344
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800345 struct usb_device* result = usb_device_new(dev_name, fd);
346 if (result)
347 result->writeable = writeable;
348 return result;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400349}
350
351void usb_device_close(struct usb_device *device)
352{
353 close(device->fd);
354 free(device);
355}
356
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800357struct 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 Lockwoodec9e7b12011-01-22 09:17:07 -0800362 D("usb_device_new %s fd: %d\n", dev_name, fd);
363
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800364 if (lseek(fd, 0, SEEK_SET) != 0)
365 goto failed;
366 length = read(fd, device->desc, sizeof(device->desc));
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800367 D("usb_device_new read returned %d errno %d\n", length, errno);
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800368 if (length < 0)
369 goto failed;
370
Mike Lockwood93aff722010-12-15 12:58:04 -0800371 strncpy(device->dev_name, dev_name, sizeof(device->dev_name) - 1);
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800372 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
378failed:
379 close(fd);
380 free(device);
381 return NULL;
382}
383
384static 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
400int 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 Lockwood30ff2c72010-05-09 16:23:47 -0400407const char* usb_device_get_name(struct usb_device *device)
408{
409 return device->dev_name;
410}
411
Mike Lockwood203f1022010-05-27 10:12:03 -0400412int 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 Lockwood07eb4af2010-07-27 19:05:33 -0400419int 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 Lockwood7d700f82010-12-29 08:47:29 -0500426char* 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 Lockwood30ff2c72010-05-09 16:23:47 -0400435uint16_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
441uint16_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 Lockwood50372072010-12-13 10:15:25 -0800447const 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 Lockwood1b7d9912010-07-24 13:57:21 -0400452char* usb_device_get_string(struct usb_device *device, int id)
453{
454 char string[256];
Mike Lockwood0dd1aab2015-06-18 13:38:31 -0700455 __u16 buffer[MAX_STRING_DESCRIPTOR_LENGTH / sizeof(__u16)];
456 __u16 languages[MAX_STRING_DESCRIPTOR_LENGTH / sizeof(__u16)];
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400457 int i, result;
458 int languageCount = 0;
459
Mike Lockwoodd2e798b2014-01-13 09:54:13 -0800460 if (id == 0) return NULL;
461
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400462 string[0] = 0;
463 memset(languages, 0, sizeof(languages));
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400464
465 // read list of supported languages
Mike Lockwood120b57a2011-01-27 10:46:19 -0800466 result = usb_device_control_transfer(device,
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400467 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
Mike Lockwood120b57a2011-01-27 10:46:19 -0800468 (USB_DT_STRING << 8) | 0, 0, languages, sizeof(languages), 0);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400469 if (result > 0)
470 languageCount = (result - 2) / 2;
471
472 for (i = 1; i <= languageCount; i++) {
473 memset(buffer, 0, sizeof(buffer));
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400474
Mike Lockwood120b57a2011-01-27 10:46:19 -0800475 result = usb_device_control_transfer(device,
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400476 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
Mike Lockwood120b57a2011-01-27 10:46:19 -0800477 (USB_DT_STRING << 8) | id, languages[i], buffer, sizeof(buffer), 0);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400478 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
492char* usb_device_get_manufacturer_name(struct usb_device *device)
493{
494 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
Mike Lockwoodd2e798b2014-01-13 09:54:13 -0800495 return usb_device_get_string(device, desc->iManufacturer);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400496}
497
498char* usb_device_get_product_name(struct usb_device *device)
499{
500 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
Mike Lockwoodd2e798b2014-01-13 09:54:13 -0800501 return usb_device_get_string(device, desc->iProduct);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400502}
503
Mike Lockwoodf68600a2015-04-29 13:04:10 -0700504int 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 Lockwood30ff2c72010-05-09 16:23:47 -0400510char* usb_device_get_serial(struct usb_device *device)
511{
512 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
Mike Lockwoodd2e798b2014-01-13 09:54:13 -0800513 return usb_device_get_string(device, desc->iSerialNumber);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400514}
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 Lockwoodd2e798b2014-01-13 09:54:13 -0800559int usb_device_set_configuration(struct usb_device *device, int configuration)
560{
561 return ioctl(device->fd, USBDEVFS_SETCONFIGURATION, &configuration);
562}
563
564int 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 Lockwood120b57a2011-01-27 10:46:19 -0800574int 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
600int 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 Lockwoodc4c00d82011-03-12 12:25:11 -0500608 // need to limit request size to avoid EINVAL
609 if (length > MAX_USBFS_BUFFER_SIZE)
610 length = MAX_USBFS_BUFFER_SIZE;
611
Mike Lockwood120b57a2011-01-27 10:46:19 -0800612 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 Lockwoode533c5f2011-01-04 20:04:36 -0500620struct usb_request *usb_request_new(struct usb_device *dev,
621 const struct usb_endpoint_descriptor *ep_desc)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400622{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500623 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 Lockwoodb5d68a32011-02-14 08:05:40 -0500647 req->endpoint = urb->endpoint;
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500648 urb->usercontext = req;
649
650 return req;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400651}
652
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500653void usb_request_free(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400654{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500655 free(req->private_data);
656 free(req);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400657}
658
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500659int usb_request_queue(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400660{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500661 struct usbdevfs_urb *urb = (struct usbdevfs_urb*)req->private_data;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400662 int res;
663
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400664 urb->status = -1;
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500665 urb->buffer = req->buffer;
Mike Lockwoodc4c00d82011-03-12 12:25:11 -0500666 // 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 Lockwood30ff2c72010-05-09 16:23:47 -0400671
672 do {
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500673 res = ioctl(req->dev->fd, USBDEVFS_SUBMITURB, urb);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400674 } while((res < 0) && (errno == EINTR));
675
676 return res;
677}
678
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500679struct usb_request *usb_request_wait(struct usb_device *dev)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400680{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500681 struct usbdevfs_urb *urb = NULL;
682 struct usb_request *req = NULL;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400683
684 while (1) {
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500685 int res = ioctl(dev->fd, USBDEVFS_REAPURB, &urb);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400686 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 Lockwoode533c5f2011-01-04 20:04:36 -0500692 return NULL;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400693 } else {
694 D("[ urb @%p status = %d, actual = %d ]\n",
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500695 urb, urb->status, urb->actual_length);
696 req = (struct usb_request*)urb->usercontext;
697 req->actual_length = urb->actual_length;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400698 }
699 break;
700 }
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500701 return req;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400702}
703
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500704int usb_request_cancel(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400705{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500706 struct usbdevfs_urb *urb = ((struct usbdevfs_urb*)req->private_data);
Badhri Jagan Sridharanef4087b2014-08-06 12:34:30 -0700707 return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, urb);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400708}
709