blob: 44b878d2c143def242be9d67c987b0ee9005be5c [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
Philip P. Moltmann36952852016-10-17 16:57:43 -070017#ifndef _GNU_SOURCE
18#define _GNU_SOURCE
19#endif
20
Mike Lockwoode15af092010-06-10 10:20:49 -040021// #define DEBUG 1
22#if DEBUG
23
24#ifdef USE_LIBLOG
25#define LOG_TAG "usbhost"
Dan Willemsena5c60172017-04-20 08:32:35 -070026#include "log/log.h"
Steve Block8d66c492011-12-20 16:07:45 +000027#define D ALOGD
Mike Lockwoode15af092010-06-10 10:20:49 -040028#else
29#define D printf
30#endif
31
32#else
33#define D(...)
34#endif
35
Mike Lockwood30ff2c72010-05-09 16:23:47 -040036#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39#include <string.h>
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020040#include <stddef.h>
Mike Lockwood30ff2c72010-05-09 16:23:47 -040041
42#include <sys/ioctl.h>
43#include <sys/types.h>
44#include <sys/time.h>
45#include <sys/inotify.h>
46#include <dirent.h>
47#include <fcntl.h>
48#include <errno.h>
49#include <ctype.h>
Philip P. Moltmann36952852016-10-17 16:57:43 -070050#include <poll.h>
Mike Lockwood30ff2c72010-05-09 16:23:47 -040051#include <pthread.h>
52
53#include <linux/usbdevice_fs.h>
Mike Lockwood30ff2c72010-05-09 16:23:47 -040054#include <asm/byteorder.h>
55
56#include "usbhost/usbhost.h"
57
Benoit Gobyf4de0782012-08-01 18:54:02 -070058#define DEV_DIR "/dev"
Ziv Hendela306ced2013-08-07 19:29:17 +030059#define DEV_BUS_DIR DEV_DIR "/bus"
60#define USB_FS_DIR DEV_BUS_DIR "/usb"
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020061#define USB_FS_ID_SCANNER USB_FS_DIR "/%d/%d"
62#define USB_FS_ID_FORMAT USB_FS_DIR "/%03d/%03d"
Mike Lockwood30ff2c72010-05-09 16:23:47 -040063
Mike Lockwood0dd1aab2015-06-18 13:38:31 -070064// Some devices fail to send string descriptors if we attempt reading > 255 bytes
65#define MAX_STRING_DESCRIPTOR_LENGTH 255
66
Mike Lockwoodc4c00d82011-03-12 12:25:11 -050067// From drivers/usb/core/devio.c
68// I don't know why this isn't in a kernel header
69#define MAX_USBFS_BUFFER_SIZE 16384
Mike Lockwood30ff2c72010-05-09 16:23:47 -040070
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020071#define MAX_USBFS_WD_COUNT 10
72
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040073struct usb_host_context {
Guillaume Ranquetdea46b62012-10-23 17:11:44 +020074 int fd;
75 usb_device_added_cb cb_added;
76 usb_device_removed_cb cb_removed;
77 void *data;
78 int wds[MAX_USBFS_WD_COUNT];
79 int wdd;
Ziv Hendela306ced2013-08-07 19:29:17 +030080 int wddbus;
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040081};
82
Mike Lockwood30ff2c72010-05-09 16:23:47 -040083struct usb_device {
84 char dev_name[64];
Mike Lockwoodec9e7b12011-01-22 09:17:07 -080085 unsigned char desc[4096];
Mike Lockwood30ff2c72010-05-09 16:23:47 -040086 int desc_length;
87 int fd;
88 int writeable;
89};
90
Mike Lockwood30ff2c72010-05-09 16:23:47 -040091static inline int badname(const char *name)
92{
93 while(*name) {
94 if(!isdigit(*name++)) return 1;
95 }
96 return 0;
97}
98
Benoit Goby6cc883c2012-07-31 19:22:06 -070099static int find_existing_devices_bus(char *busname,
100 usb_device_added_cb added_cb,
101 void *client_data)
102{
103 char devname[32];
104 DIR *devdir;
105 struct dirent *de;
106 int done = 0;
107
108 devdir = opendir(busname);
109 if(devdir == 0) return 0;
110
111 while ((de = readdir(devdir)) && !done) {
112 if(badname(de->d_name)) continue;
113
114 snprintf(devname, sizeof(devname), "%s/%s", busname, de->d_name);
115 done = added_cb(devname, client_data);
116 } // end of devdir while
117 closedir(devdir);
118
119 return done;
120}
121
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400122/* returns true if one of the callbacks indicates we are done */
123static int find_existing_devices(usb_device_added_cb added_cb,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400124 void *client_data)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400125{
Benoit Goby6cc883c2012-07-31 19:22:06 -0700126 char busname[32];
127 DIR *busdir;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400128 struct dirent *de;
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400129 int done = 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400130
131 busdir = opendir(USB_FS_DIR);
Benoit Gobyf4de0782012-08-01 18:54:02 -0700132 if(busdir == 0) return 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400133
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400134 while ((de = readdir(busdir)) != 0 && !done) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400135 if(badname(de->d_name)) continue;
136
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200137 snprintf(busname, sizeof(busname), USB_FS_DIR "/%s", de->d_name);
Benoit Goby6cc883c2012-07-31 19:22:06 -0700138 done = find_existing_devices_bus(busname, added_cb,
139 client_data);
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200140 } //end of busdir while
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400141 closedir(busdir);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400142
143 return done;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400144}
145
Benoit Gobyf4de0782012-08-01 18:54:02 -0700146static void watch_existing_subdirs(struct usb_host_context *context,
147 int *wds, int wd_count)
148{
149 char path[100];
150 int i, ret;
151
152 wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE);
153 if (wds[0] < 0)
154 return;
155
156 /* watch existing subdirectories of USB_FS_DIR */
157 for (i = 1; i < wd_count; i++) {
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200158 snprintf(path, sizeof(path), USB_FS_DIR "/%03d", i);
Benoit Gobyf4de0782012-08-01 18:54:02 -0700159 ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE);
160 if (ret >= 0)
161 wds[i] = ret;
162 }
163}
164
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400165struct usb_host_context *usb_host_init()
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400166{
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400167 struct usb_host_context *context = calloc(1, sizeof(struct usb_host_context));
168 if (!context) {
169 fprintf(stderr, "out of memory in usb_host_context\n");
170 return NULL;
171 }
172 context->fd = inotify_init();
173 if (context->fd < 0) {
174 fprintf(stderr, "inotify_init failed\n");
175 free(context);
176 return NULL;
177 }
178 return context;
179}
180
181void usb_host_cleanup(struct usb_host_context *context)
182{
183 close(context->fd);
184 free(context);
185}
186
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200187int usb_host_get_fd(struct usb_host_context *context)
188{
189 return context->fd;
190} /* usb_host_get_fd() */
191
192int usb_host_load(struct usb_host_context *context,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400193 usb_device_added_cb added_cb,
194 usb_device_removed_cb removed_cb,
Mike Lockwooda8055192010-07-19 20:15:15 -0400195 usb_discovery_done_cb discovery_done_cb,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400196 void *client_data)
197{
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200198 int done = 0;
199 int i;
200
201 context->cb_added = added_cb;
202 context->cb_removed = removed_cb;
203 context->data = client_data;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400204
205 D("Created device discovery thread\n");
206
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400207 /* watch for files added and deleted within USB_FS_DIR */
Ziv Hendela306ced2013-08-07 19:29:17 +0300208 context->wddbus = -1;
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200209 for (i = 0; i < MAX_USBFS_WD_COUNT; i++)
210 context->wds[i] = -1;
Benoit Gobyf4de0782012-08-01 18:54:02 -0700211
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400212 /* watch the root for new subdirectories */
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200213 context->wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE);
214 if (context->wdd < 0) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400215 fprintf(stderr, "inotify_add_watch failed\n");
Mike Lockwoode8849d12010-07-20 16:31:42 -0400216 if (discovery_done_cb)
217 discovery_done_cb(client_data);
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200218 return done;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400219 }
220
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200221 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400222
223 /* check for existing devices first, after we have inotify set up */
Benoit Goby6cc883c2012-07-31 19:22:06 -0700224 done = find_existing_devices(added_cb, client_data);
Mike Lockwooda8055192010-07-19 20:15:15 -0400225 if (discovery_done_cb)
226 done |= discovery_done_cb(client_data);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400227
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200228 return done;
229} /* usb_host_load() */
230
231int usb_host_read_event(struct usb_host_context *context)
232{
233 struct inotify_event* event;
234 char event_buf[512];
235 char path[100];
236 int i, ret, done = 0;
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200237 int offset = 0;
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200238 int wd;
239
240 ret = read(context->fd, event_buf, sizeof(event_buf));
241 if (ret >= (int)sizeof(struct inotify_event)) {
Ziv Hendela306ced2013-08-07 19:29:17 +0300242 while (offset < ret && !done) {
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200243 event = (struct inotify_event*)&event_buf[offset];
244 done = 0;
245 wd = event->wd;
246 if (wd == context->wdd) {
247 if ((event->mask & IN_CREATE) && !strcmp(event->name, "bus")) {
Ziv Hendela306ced2013-08-07 19:29:17 +0300248 context->wddbus = inotify_add_watch(context->fd, DEV_BUS_DIR, IN_CREATE | IN_DELETE);
249 if (context->wddbus < 0) {
250 done = 1;
251 } else {
252 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
253 done = find_existing_devices(context->cb_added, context->data);
254 }
255 }
256 } else if (wd == context->wddbus) {
257 if ((event->mask & IN_CREATE) && !strcmp(event->name, "usb")) {
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200258 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
259 done = find_existing_devices(context->cb_added, context->data);
Ziv Hendela306ced2013-08-07 19:29:17 +0300260 } else if ((event->mask & IN_DELETE) && !strcmp(event->name, "usb")) {
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200261 for (i = 0; i < MAX_USBFS_WD_COUNT; i++) {
262 if (context->wds[i] >= 0) {
263 inotify_rm_watch(context->fd, context->wds[i]);
264 context->wds[i] = -1;
265 }
266 }
267 }
268 } else if (wd == context->wds[0]) {
269 i = atoi(event->name);
270 snprintf(path, sizeof(path), USB_FS_DIR "/%s", event->name);
271 D("%s subdirectory %s: index: %d\n", (event->mask & IN_CREATE) ?
272 "new" : "gone", path, i);
273 if (i > 0 && i < MAX_USBFS_WD_COUNT) {
Bo Huang3c1d7b32014-01-21 14:25:22 +0800274 int local_ret = 0;
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200275 if (event->mask & IN_CREATE) {
Bo Huang3c1d7b32014-01-21 14:25:22 +0800276 local_ret = inotify_add_watch(context->fd, path,
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200277 IN_CREATE | IN_DELETE);
Bo Huang3c1d7b32014-01-21 14:25:22 +0800278 if (local_ret >= 0)
279 context->wds[i] = local_ret;
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200280 done = find_existing_devices_bus(path, context->cb_added,
281 context->data);
282 } else if (event->mask & IN_DELETE) {
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200283 inotify_rm_watch(context->fd, context->wds[i]);
284 context->wds[i] = -1;
Benoit Gobyf4de0782012-08-01 18:54:02 -0700285 }
286 }
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200287 } else {
288 for (i = 1; (i < MAX_USBFS_WD_COUNT) && !done; i++) {
289 if (wd == context->wds[i]) {
290 snprintf(path, sizeof(path), USB_FS_DIR "/%03d/%s", i, event->name);
291 if (event->mask == IN_CREATE) {
292 D("new device %s\n", path);
293 done = context->cb_added(path, context->data);
294 } else if (event->mask == IN_DELETE) {
295 D("gone device %s\n", path);
296 done = context->cb_removed(path, context->data);
297 }
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400298 }
299 }
300 }
Ziv Hendelf75ea8d2013-03-24 18:10:52 +0200301
302 offset += sizeof(struct inotify_event) + event->len;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400303 }
304 }
Guillaume Ranquetdea46b62012-10-23 17:11:44 +0200305
306 return done;
307} /* usb_host_read_event() */
308
309void usb_host_run(struct usb_host_context *context,
310 usb_device_added_cb added_cb,
311 usb_device_removed_cb removed_cb,
312 usb_discovery_done_cb discovery_done_cb,
313 void *client_data)
314{
315 int done;
316
317 done = usb_host_load(context, added_cb, removed_cb, discovery_done_cb, client_data);
318
319 while (!done) {
320
321 done = usb_host_read_event(context);
322 }
323} /* usb_host_run() */
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400324
325struct usb_device *usb_device_open(const char *dev_name)
326{
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800327 int fd, did_retry = 0, writeable = 1;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400328
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800329 D("usb_device_open %s\n", dev_name);
330
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400331retry:
332 fd = open(dev_name, O_RDWR);
333 if (fd < 0) {
334 /* if we fail, see if have read-only access */
335 fd = open(dev_name, O_RDONLY);
Mike Lockwoode15af092010-06-10 10:20:49 -0400336 D("usb_device_open open returned %d errno %d\n", fd, errno);
337 if (fd < 0 && (errno == EACCES || errno == ENOENT) && !did_retry) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400338 /* work around race condition between inotify and permissions management */
339 sleep(1);
340 did_retry = 1;
341 goto retry;
342 }
343
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800344 if (fd < 0)
345 return NULL;
346 writeable = 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400347 D("[ usb open read-only %s fd = %d]\n", dev_name, fd);
348 }
349
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800350 struct usb_device* result = usb_device_new(dev_name, fd);
351 if (result)
352 result->writeable = writeable;
353 return result;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400354}
355
356void usb_device_close(struct usb_device *device)
357{
358 close(device->fd);
359 free(device);
360}
361
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800362struct usb_device *usb_device_new(const char *dev_name, int fd)
363{
364 struct usb_device *device = calloc(1, sizeof(struct usb_device));
365 int length;
366
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800367 D("usb_device_new %s fd: %d\n", dev_name, fd);
368
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800369 if (lseek(fd, 0, SEEK_SET) != 0)
370 goto failed;
371 length = read(fd, device->desc, sizeof(device->desc));
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800372 D("usb_device_new read returned %d errno %d\n", length, errno);
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800373 if (length < 0)
374 goto failed;
375
Mike Lockwood93aff722010-12-15 12:58:04 -0800376 strncpy(device->dev_name, dev_name, sizeof(device->dev_name) - 1);
Mike Lockwoodcd185f22010-12-12 14:17:02 -0800377 device->fd = fd;
378 device->desc_length = length;
379 // assume we are writeable, since usb_device_get_fd will only return writeable fds
380 device->writeable = 1;
381 return device;
382
383failed:
384 close(fd);
385 free(device);
386 return NULL;
387}
388
389static int usb_device_reopen_writeable(struct usb_device *device)
390{
391 if (device->writeable)
392 return 1;
393
394 int fd = open(device->dev_name, O_RDWR);
395 if (fd >= 0) {
396 close(device->fd);
397 device->fd = fd;
398 device->writeable = 1;
399 return 1;
400 }
401 D("usb_device_reopen_writeable failed errno %d\n", errno);
402 return 0;
403}
404
405int usb_device_get_fd(struct usb_device *device)
406{
407 if (!usb_device_reopen_writeable(device))
408 return -1;
409 return device->fd;
410}
411
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400412const char* usb_device_get_name(struct usb_device *device)
413{
414 return device->dev_name;
415}
416
Mike Lockwood203f1022010-05-27 10:12:03 -0400417int usb_device_get_unique_id(struct usb_device *device)
418{
419 int bus = 0, dev = 0;
420 sscanf(device->dev_name, USB_FS_ID_SCANNER, &bus, &dev);
421 return bus * 1000 + dev;
422}
423
Mike Lockwood07eb4af2010-07-27 19:05:33 -0400424int usb_device_get_unique_id_from_name(const char* name)
425{
426 int bus = 0, dev = 0;
427 sscanf(name, USB_FS_ID_SCANNER, &bus, &dev);
428 return bus * 1000 + dev;
429}
430
Mike Lockwood7d700f82010-12-29 08:47:29 -0500431char* usb_device_get_name_from_unique_id(int id)
432{
433 int bus = id / 1000;
434 int dev = id % 1000;
435 char* result = (char *)calloc(1, strlen(USB_FS_ID_FORMAT));
436 snprintf(result, strlen(USB_FS_ID_FORMAT) - 1, USB_FS_ID_FORMAT, bus, dev);
437 return result;
438}
439
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400440uint16_t usb_device_get_vendor_id(struct usb_device *device)
441{
442 struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;
443 return __le16_to_cpu(desc->idVendor);
444}
445
446uint16_t usb_device_get_product_id(struct usb_device *device)
447{
448 struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;
449 return __le16_to_cpu(desc->idProduct);
450}
451
Mike Lockwood50372072010-12-13 10:15:25 -0800452const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device)
453{
454 return (struct usb_device_descriptor*)device->desc;
455}
456
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700457char* usb_device_get_string(struct usb_device *device, int id, int timeout)
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400458{
459 char string[256];
Mike Lockwood0dd1aab2015-06-18 13:38:31 -0700460 __u16 buffer[MAX_STRING_DESCRIPTOR_LENGTH / sizeof(__u16)];
461 __u16 languages[MAX_STRING_DESCRIPTOR_LENGTH / sizeof(__u16)];
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400462 int i, result;
463 int languageCount = 0;
464
Mike Lockwoodd2e798b2014-01-13 09:54:13 -0800465 if (id == 0) return NULL;
466
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400467 string[0] = 0;
468 memset(languages, 0, sizeof(languages));
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400469
470 // read list of supported languages
Mike Lockwood120b57a2011-01-27 10:46:19 -0800471 result = usb_device_control_transfer(device,
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400472 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700473 (USB_DT_STRING << 8) | 0, 0, languages, sizeof(languages),
474 timeout);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400475 if (result > 0)
476 languageCount = (result - 2) / 2;
477
478 for (i = 1; i <= languageCount; i++) {
479 memset(buffer, 0, sizeof(buffer));
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400480
Mike Lockwood120b57a2011-01-27 10:46:19 -0800481 result = usb_device_control_transfer(device,
Mike Lockwood1b7d9912010-07-24 13:57:21 -0400482 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700483 (USB_DT_STRING << 8) | id, languages[i], buffer, sizeof(buffer),
484 timeout);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400485 if (result > 0) {
486 int i;
487 // skip first word, and copy the rest to the string, changing shorts to bytes.
488 result /= 2;
489 for (i = 1; i < result; i++)
490 string[i - 1] = buffer[i];
491 string[i - 1] = 0;
492 return strdup(string);
493 }
494 }
495
496 return NULL;
497}
498
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700499char* usb_device_get_manufacturer_name(struct usb_device *device, int timeout)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400500{
501 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700502 return usb_device_get_string(device, desc->iManufacturer, timeout);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400503}
504
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700505char* usb_device_get_product_name(struct usb_device *device, int timeout)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400506{
507 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700508 return usb_device_get_string(device, desc->iProduct, timeout);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400509}
510
Mike Lockwoodf68600a2015-04-29 13:04:10 -0700511int usb_device_get_version(struct usb_device *device)
512{
513 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
514 return desc->bcdUSB;
515}
516
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700517char* usb_device_get_serial(struct usb_device *device, int timeout)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400518{
519 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
Vitalii Tomkivdfd21b82016-10-14 10:19:26 -0700520 return usb_device_get_string(device, desc->iSerialNumber, timeout);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400521}
522
523int usb_device_is_writeable(struct usb_device *device)
524{
525 return device->writeable;
526}
527
528void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter)
529{
530 iter->config = device->desc;
531 iter->config_end = device->desc + device->desc_length;
532 iter->curr_desc = device->desc;
533}
534
535struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter)
536{
537 struct usb_descriptor_header* next;
538 if (iter->curr_desc >= iter->config_end)
539 return NULL;
540 next = (struct usb_descriptor_header*)iter->curr_desc;
541 iter->curr_desc += next->bLength;
542 return next;
543}
544
545int usb_device_claim_interface(struct usb_device *device, unsigned int interface)
546{
547 return ioctl(device->fd, USBDEVFS_CLAIMINTERFACE, &interface);
548}
549
550int usb_device_release_interface(struct usb_device *device, unsigned int interface)
551{
552 return ioctl(device->fd, USBDEVFS_RELEASEINTERFACE, &interface);
553}
554
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800555int usb_device_connect_kernel_driver(struct usb_device *device,
556 unsigned int interface, int connect)
557{
558 struct usbdevfs_ioctl ctl;
559
560 ctl.ifno = interface;
561 ctl.ioctl_code = (connect ? USBDEVFS_CONNECT : USBDEVFS_DISCONNECT);
562 ctl.data = NULL;
563 return ioctl(device->fd, USBDEVFS_IOCTL, &ctl);
564}
565
Mike Lockwoodd2e798b2014-01-13 09:54:13 -0800566int usb_device_set_configuration(struct usb_device *device, int configuration)
567{
568 return ioctl(device->fd, USBDEVFS_SETCONFIGURATION, &configuration);
569}
570
571int usb_device_set_interface(struct usb_device *device, unsigned int interface,
572 unsigned int alt_setting)
573{
574 struct usbdevfs_setinterface ctl;
575
576 ctl.interface = interface;
577 ctl.altsetting = alt_setting;
578 return ioctl(device->fd, USBDEVFS_SETINTERFACE, &ctl);
579}
580
Mike Lockwood120b57a2011-01-27 10:46:19 -0800581int usb_device_control_transfer(struct usb_device *device,
582 int requestType,
583 int request,
584 int value,
585 int index,
586 void* buffer,
587 int length,
588 unsigned int timeout)
589{
590 struct usbdevfs_ctrltransfer ctrl;
591
592 // this usually requires read/write permission
593 if (!usb_device_reopen_writeable(device))
594 return -1;
595
596 memset(&ctrl, 0, sizeof(ctrl));
597 ctrl.bRequestType = requestType;
598 ctrl.bRequest = request;
599 ctrl.wValue = value;
600 ctrl.wIndex = index;
601 ctrl.wLength = length;
602 ctrl.data = buffer;
603 ctrl.timeout = timeout;
604 return ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
605}
606
607int usb_device_bulk_transfer(struct usb_device *device,
608 int endpoint,
609 void* buffer,
Philip P. Moltmann9879bb22016-09-20 14:11:26 -0700610 unsigned int length,
Mike Lockwood120b57a2011-01-27 10:46:19 -0800611 unsigned int timeout)
612{
613 struct usbdevfs_bulktransfer ctrl;
614
Mike Lockwoodc4c00d82011-03-12 12:25:11 -0500615 // need to limit request size to avoid EINVAL
616 if (length > MAX_USBFS_BUFFER_SIZE)
617 length = MAX_USBFS_BUFFER_SIZE;
618
Mike Lockwood120b57a2011-01-27 10:46:19 -0800619 memset(&ctrl, 0, sizeof(ctrl));
620 ctrl.ep = endpoint;
621 ctrl.len = length;
622 ctrl.data = buffer;
623 ctrl.timeout = timeout;
624 return ioctl(device->fd, USBDEVFS_BULK, &ctrl);
625}
626
Keun-young Parkf6411fa2016-01-12 18:22:36 -0800627int usb_device_reset(struct usb_device *device)
628{
629 return ioctl(device->fd, USBDEVFS_RESET);
630}
631
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500632struct usb_request *usb_request_new(struct usb_device *dev,
633 const struct usb_endpoint_descriptor *ep_desc)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400634{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500635 struct usbdevfs_urb *urb = calloc(1, sizeof(struct usbdevfs_urb));
636 if (!urb)
637 return NULL;
638
639 if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK)
640 urb->type = USBDEVFS_URB_TYPE_BULK;
641 else if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
642 urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
643 else {
644 D("Unsupported endpoint type %d", ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
645 free(urb);
646 return NULL;
647 }
648 urb->endpoint = ep_desc->bEndpointAddress;
649
650 struct usb_request *req = calloc(1, sizeof(struct usb_request));
651 if (!req) {
652 free(urb);
653 return NULL;
654 }
655
656 req->dev = dev;
657 req->max_packet_size = __le16_to_cpu(ep_desc->wMaxPacketSize);
658 req->private_data = urb;
Mike Lockwoodb5d68a32011-02-14 08:05:40 -0500659 req->endpoint = urb->endpoint;
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500660 urb->usercontext = req;
661
662 return req;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400663}
664
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500665void usb_request_free(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400666{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500667 free(req->private_data);
668 free(req);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400669}
670
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500671int usb_request_queue(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400672{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500673 struct usbdevfs_urb *urb = (struct usbdevfs_urb*)req->private_data;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400674 int res;
675
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400676 urb->status = -1;
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500677 urb->buffer = req->buffer;
Mike Lockwoodc4c00d82011-03-12 12:25:11 -0500678 // need to limit request size to avoid EINVAL
679 if (req->buffer_length > MAX_USBFS_BUFFER_SIZE)
680 urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
681 else
682 urb->buffer_length = req->buffer_length;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400683
684 do {
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500685 res = ioctl(req->dev->fd, USBDEVFS_SUBMITURB, urb);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400686 } while((res < 0) && (errno == EINTR));
687
688 return res;
689}
690
Philip P. Moltmann36952852016-10-17 16:57:43 -0700691struct usb_request *usb_request_wait(struct usb_device *dev, int timeoutMillis)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400692{
Philip P. Moltmann36952852016-10-17 16:57:43 -0700693 // Poll until a request becomes available if there is a timeout
694 if (timeoutMillis > 0) {
695 struct pollfd p = {.fd = dev->fd, .events = POLLOUT, .revents = 0};
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400696
Philip P. Moltmann36952852016-10-17 16:57:43 -0700697 int res = poll(&p, 1, timeoutMillis);
698
699 if (res != 1 || p.revents != POLLOUT) {
700 D("[ poll - event %d, error %d]\n", p.revents, errno);
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500701 return NULL;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400702 }
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400703 }
Philip P. Moltmann36952852016-10-17 16:57:43 -0700704
705 // Read the request. This should usually succeed as we polled before, but it can fail e.g. when
706 // two threads are reading usb requests at the same time and only a single request is available.
707 struct usbdevfs_urb *urb = NULL;
708 int res = TEMP_FAILURE_RETRY(ioctl(dev->fd, timeoutMillis == -1 ? USBDEVFS_REAPURB :
709 USBDEVFS_REAPURBNDELAY, &urb));
710 D("%s returned %d\n", timeoutMillis == -1 ? "USBDEVFS_REAPURB" : "USBDEVFS_REAPURBNDELAY", res);
711
712 if (res < 0) {
713 D("[ reap urb - error %d]\n", errno);
714 return NULL;
715 } else {
716 D("[ urb @%p status = %d, actual = %d ]\n", urb, urb->status, urb->actual_length);
717
718 struct usb_request *req = (struct usb_request*)urb->usercontext;
719 req->actual_length = urb->actual_length;
720
721 return req;
722 }
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400723}
724
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500725int usb_request_cancel(struct usb_request *req)
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400726{
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500727 struct usbdevfs_urb *urb = ((struct usbdevfs_urb*)req->private_data);
Badhri Jagan Sridharanef4087b2014-08-06 12:34:30 -0700728 return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, urb);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400729}