blob: f5ec140ebe894db4807870be2304e09ca8b36e78 [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"
23#define D LOGD
24#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>
36
37#include <sys/ioctl.h>
38#include <sys/types.h>
39#include <sys/time.h>
40#include <sys/inotify.h>
41#include <dirent.h>
42#include <fcntl.h>
43#include <errno.h>
44#include <ctype.h>
45#include <pthread.h>
46
47#include <linux/usbdevice_fs.h>
48#include <linux/version.h>
49#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
50#include <linux/usb/ch9.h>
51#else
52#include <linux/usb_ch9.h>
53#endif
54#include <asm/byteorder.h>
55
56#include "usbhost/usbhost.h"
57
58#define USB_FS_DIR "/dev/bus/usb"
Mike Lockwood203f1022010-05-27 10:12:03 -040059#define USB_FS_ID_SCANNER "/dev/bus/usb/%d/%d"
Mike Lockwood30ff2c72010-05-09 16:23:47 -040060
Mike Lockwood30ff2c72010-05-09 16:23:47 -040061
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040062struct usb_host_context {
Mike Lockwood7a96ba42010-07-01 11:33:41 -040063 int fd;
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040064};
65
Mike Lockwood30ff2c72010-05-09 16:23:47 -040066struct usb_device {
67 char dev_name[64];
68 unsigned char desc[256];
69 int desc_length;
70 int fd;
71 int writeable;
72};
73
74struct usb_endpoint
75{
76 struct usb_device *dev;
77 struct usb_endpoint_descriptor desc;
78 struct usbdevfs_urb urb;
79};
80
Mike Lockwood30ff2c72010-05-09 16:23:47 -040081static inline int badname(const char *name)
82{
83 while(*name) {
84 if(!isdigit(*name++)) return 1;
85 }
86 return 0;
87}
88
Mike Lockwood7a96ba42010-07-01 11:33:41 -040089/* returns true if one of the callbacks indicates we are done */
90static int find_existing_devices(usb_device_added_cb added_cb,
91 usb_device_removed_cb removed_cb,
92 void *client_data)
Mike Lockwood30ff2c72010-05-09 16:23:47 -040093{
94 char busname[32], devname[32];
95 DIR *busdir , *devdir ;
96 struct dirent *de;
Mike Lockwood7a96ba42010-07-01 11:33:41 -040097 int done = 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -040098
99 busdir = opendir(USB_FS_DIR);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400100 if(busdir == 0) return 1;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400101
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400102 while ((de = readdir(busdir)) != 0 && !done) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400103 if(badname(de->d_name)) continue;
104
105 snprintf(busname, sizeof busname, "%s/%s", USB_FS_DIR, de->d_name);
106 devdir = opendir(busname);
107 if(devdir == 0) continue;
108
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400109 while ((de = readdir(devdir)) && !done) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400110 if(badname(de->d_name)) continue;
111
112 snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400113 done = added_cb(devname, client_data);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400114 } // end of devdir while
115 closedir(devdir);
116 } //end of busdir while
117 closedir(busdir);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400118
119 return done;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400120}
121
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400122struct usb_host_context *usb_host_init()
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400123{
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400124 struct usb_host_context *context = calloc(1, sizeof(struct usb_host_context));
125 if (!context) {
126 fprintf(stderr, "out of memory in usb_host_context\n");
127 return NULL;
128 }
129 context->fd = inotify_init();
130 if (context->fd < 0) {
131 fprintf(stderr, "inotify_init failed\n");
132 free(context);
133 return NULL;
134 }
135 return context;
136}
137
138void usb_host_cleanup(struct usb_host_context *context)
139{
140 close(context->fd);
141 free(context);
142}
143
144void usb_host_run(struct usb_host_context *context,
145 usb_device_added_cb added_cb,
146 usb_device_removed_cb removed_cb,
Mike Lockwooda8055192010-07-19 20:15:15 -0400147 usb_discovery_done_cb discovery_done_cb,
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400148 void *client_data)
149{
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400150 struct inotify_event* event;
151 char event_buf[512];
152 char path[100];
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400153 int i, ret, done = 0;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400154 int wd, wds[10];
155 int wd_count = sizeof(wds) / sizeof(wds[0]);
156
157 D("Created device discovery thread\n");
158
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400159 /* watch for files added and deleted within USB_FS_DIR */
160 memset(wds, 0, sizeof(wds));
161 /* watch the root for new subdirectories */
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400162 wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400163 if (wds[0] < 0) {
164 fprintf(stderr, "inotify_add_watch failed\n");
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400165 return;
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400166 }
167
168 /* watch existing subdirectories of USB_FS_DIR */
169 for (i = 1; i < wd_count; i++) {
170 snprintf(path, sizeof(path), "%s/%03d", USB_FS_DIR, i);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400171 ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400172 if (ret > 0)
173 wds[i] = ret;
174 }
175
176 /* check for existing devices first, after we have inotify set up */
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400177 done = find_existing_devices(added_cb, removed_cb, client_data);
Mike Lockwooda8055192010-07-19 20:15:15 -0400178 if (discovery_done_cb)
179 done |= discovery_done_cb(client_data);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400180
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400181 while (!done) {
182 ret = read(context->fd, event_buf, sizeof(event_buf));
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400183 if (ret >= (int)sizeof(struct inotify_event)) {
184 event = (struct inotify_event *)event_buf;
185 wd = event->wd;
186 if (wd == wds[0]) {
187 i = atoi(event->name);
188 snprintf(path, sizeof(path), "%s/%s", USB_FS_DIR, event->name);
189 D("new subdirectory %s: index: %d\n", path, i);
190 if (i > 0 && i < wd_count) {
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400191 ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400192 if (ret > 0)
193 wds[i] = ret;
194 }
195 } else {
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400196 for (i = 1; i < wd_count && !done; i++) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400197 if (wd == wds[i]) {
198 snprintf(path, sizeof(path), "%s/%03d/%s", USB_FS_DIR, i, event->name);
199 if (event->mask == IN_CREATE) {
200 D("new device %s\n", path);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400201 done = added_cb(path, client_data);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400202 } else if (event->mask == IN_DELETE) {
203 D("gone device %s\n", path);
Mike Lockwood7a96ba42010-07-01 11:33:41 -0400204 done = removed_cb(path, client_data);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400205 }
206 }
207 }
208 }
209 }
210 }
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400211}
212
213struct usb_device *usb_device_open(const char *dev_name)
214{
215 struct usb_device *device = calloc(1, sizeof(struct usb_device));
216 int fd, length, did_retry = 0;
217
218 strcpy(device->dev_name, dev_name);
219 device->writeable = 1;
220
221retry:
222 fd = open(dev_name, O_RDWR);
223 if (fd < 0) {
224 /* if we fail, see if have read-only access */
225 fd = open(dev_name, O_RDONLY);
Mike Lockwoode15af092010-06-10 10:20:49 -0400226 D("usb_device_open open returned %d errno %d\n", fd, errno);
227 if (fd < 0 && (errno == EACCES || errno == ENOENT) && !did_retry) {
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400228 /* work around race condition between inotify and permissions management */
229 sleep(1);
230 did_retry = 1;
231 goto retry;
232 }
233
234 if (fd < 0) goto fail;
235 device->writeable = 0;
236 D("[ usb open read-only %s fd = %d]\n", dev_name, fd);
237 }
238
239 length = read(fd, device->desc, sizeof(device->desc));
Mike Lockwoode15af092010-06-10 10:20:49 -0400240 D("usb_device_open read returned %d errno %d\n", fd, errno);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400241 if (length < 0)
242 goto fail;
243
244 device->fd = fd;
245 device->desc_length = length;
246 return device;
247fail:
248 close(fd);
249 free(device);
250 return NULL;
251}
252
253void usb_device_close(struct usb_device *device)
254{
255 close(device->fd);
256 free(device);
257}
258
259const char* usb_device_get_name(struct usb_device *device)
260{
261 return device->dev_name;
262}
263
Mike Lockwood203f1022010-05-27 10:12:03 -0400264int usb_device_get_unique_id(struct usb_device *device)
265{
266 int bus = 0, dev = 0;
267 sscanf(device->dev_name, USB_FS_ID_SCANNER, &bus, &dev);
268 return bus * 1000 + dev;
269}
270
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400271uint16_t usb_device_get_vendor_id(struct usb_device *device)
272{
273 struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;
274 return __le16_to_cpu(desc->idVendor);
275}
276
277uint16_t usb_device_get_product_id(struct usb_device *device)
278{
279 struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;
280 return __le16_to_cpu(desc->idProduct);
281}
282
283char* usb_device_get_string(struct usb_device *device, int id)
284{
285 char string[256];
286 struct usbdevfs_ctrltransfer ctrl;
287 __u16 buffer[128];
288 __u16 languages[128];
289 int i, result;
290 int languageCount = 0;
291
292 string[0] = 0;
293
294 // reading the string requires read/write permission
295 if (!device->writeable) {
296 int fd = open(device->dev_name, O_RDWR);
297 if (fd > 0) {
298 close(device->fd);
299 device->fd = fd;
300 device->writeable = 1;
301 } else {
302 return NULL;
303 }
304 }
305
306 memset(languages, 0, sizeof(languages));
307 memset(&ctrl, 0, sizeof(ctrl));
308
309 // read list of supported languages
310 ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
311 ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
312 ctrl.wValue = (USB_DT_STRING << 8) | 0;
313 ctrl.wIndex = 0;
314 ctrl.wLength = sizeof(languages);
315 ctrl.data = languages;
316
317 result = ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
318 if (result > 0)
319 languageCount = (result - 2) / 2;
320
321 for (i = 1; i <= languageCount; i++) {
322 memset(buffer, 0, sizeof(buffer));
323 memset(&ctrl, 0, sizeof(ctrl));
324
325 ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
326 ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
327 ctrl.wValue = (USB_DT_STRING << 8) | id;
328 ctrl.wIndex = languages[i];
329 ctrl.wLength = sizeof(buffer);
330 ctrl.data = buffer;
331
332 result = ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
333 if (result > 0) {
334 int i;
335 // skip first word, and copy the rest to the string, changing shorts to bytes.
336 result /= 2;
337 for (i = 1; i < result; i++)
338 string[i - 1] = buffer[i];
339 string[i - 1] = 0;
340 return strdup(string);
341 }
342 }
343
344 return NULL;
345}
346
347char* usb_device_get_manufacturer_name(struct usb_device *device)
348{
349 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
350
351 if (desc->iManufacturer)
352 return usb_device_get_string(device, desc->iManufacturer);
353 else
354 return NULL;
355}
356
357char* usb_device_get_product_name(struct usb_device *device)
358{
359 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
360
361 if (desc->iProduct)
362 return usb_device_get_string(device, desc->iProduct);
363 else
364 return NULL;
365}
366
367char* usb_device_get_serial(struct usb_device *device)
368{
369 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
370
371 if (desc->iSerialNumber)
372 return usb_device_get_string(device, desc->iSerialNumber);
373 else
374 return NULL;
375}
376
377int usb_device_is_writeable(struct usb_device *device)
378{
379 return device->writeable;
380}
381
382void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter)
383{
384 iter->config = device->desc;
385 iter->config_end = device->desc + device->desc_length;
386 iter->curr_desc = device->desc;
387}
388
389struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter)
390{
391 struct usb_descriptor_header* next;
392 if (iter->curr_desc >= iter->config_end)
393 return NULL;
394 next = (struct usb_descriptor_header*)iter->curr_desc;
395 iter->curr_desc += next->bLength;
396 return next;
397}
398
399int usb_device_claim_interface(struct usb_device *device, unsigned int interface)
400{
401 return ioctl(device->fd, USBDEVFS_CLAIMINTERFACE, &interface);
402}
403
404int usb_device_release_interface(struct usb_device *device, unsigned int interface)
405{
406 return ioctl(device->fd, USBDEVFS_RELEASEINTERFACE, &interface);
407}
408
409struct usb_endpoint *usb_endpoint_open(struct usb_device *dev,
410 const struct usb_endpoint_descriptor *desc)
411{
412 struct usb_endpoint *ep = calloc(1, sizeof(struct usb_endpoint));
413 memcpy(&ep->desc, desc, sizeof(ep->desc));
414 ep->dev = dev;
415 return ep;
416}
417
418void usb_endpoint_close(struct usb_endpoint *ep)
419{
420 // cancel IO here?
421 free(ep);
422}
423
424int usb_endpoint_queue(struct usb_endpoint *ep, void *data, int len)
425{
426 struct usbdevfs_urb *urb = &ep->urb;
427 int res;
428
429 D("usb_endpoint_queue\n");
430 memset(urb, 0, sizeof(*urb));
431 urb->type = USBDEVFS_URB_TYPE_BULK;
432 urb->endpoint = ep->desc.bEndpointAddress;
433 urb->status = -1;
434 urb->buffer = data;
435 urb->buffer_length = len;
436
437 do {
438 res = ioctl(ep->dev->fd, USBDEVFS_SUBMITURB, urb);
439 } while((res < 0) && (errno == EINTR));
440
441 return res;
442}
443
444int usb_endpoint_wait(struct usb_device *dev, int *out_ep_num)
445{
446 struct usbdevfs_urb *out = NULL;
447 int res;
448
449 while (1) {
450 res = ioctl(dev->fd, USBDEVFS_REAPURB, &out);
451 D("USBDEVFS_REAPURB returned %d\n", res);
452 if (res < 0) {
453 if(errno == EINTR) {
454 continue;
455 }
456 D("[ reap urb - error ]\n");
457 *out_ep_num = -1;
458 } else {
459 D("[ urb @%p status = %d, actual = %d ]\n",
460 out, out->status, out->actual_length);
461 res = out->actual_length;
462 *out_ep_num = out->endpoint;
463 }
464 break;
465 }
466 return res;
467}
468
469int usb_endpoint_cancel(struct usb_endpoint *ep)
470{
471 return ioctl(ep->dev->fd, USBDEVFS_DISCARDURB, &ep->urb);
472}
473
Mike Lockwood5e567cb2010-05-12 08:53:49 -0400474struct usb_device *usb_endpoint_get_device(struct usb_endpoint *ep)
475{
476 return ep->dev;
477}
478
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400479int usb_endpoint_number(struct usb_endpoint *ep)
480{
481 return ep->desc.bEndpointAddress;
482}
483
484int usb_endpoint_max_packet(struct usb_endpoint *ep)
485{
486 return __le16_to_cpu(ep->desc.wMaxPacketSize);
487}
488