blob: 88273ff479ad57ecdfadf8781b4829edfe8d77dc [file] [log] [blame]
bellardbb36d472005-11-05 14:22:28 +00001/*
2 * Linux host USB redirector
3 *
4 * Copyright (c) 2005 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
aliguori64838172008-08-21 19:31:10 +00006 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
aliguori5d0c5752008-09-14 01:07:41 +00008 * Major rewrite to support fully async operation
aliguori4b096fc2008-08-21 19:28:55 +00009 *
aliguori0f431522008-10-07 20:06:37 +000010 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
13 *
bellardbb36d472005-11-05 14:22:28 +000014 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
31 */
aliguori446ab122008-09-14 01:06:09 +000032
pbrook87ecb682007-11-17 17:14:51 +000033#include "qemu-common.h"
aliguori1f3870a2008-08-21 19:27:48 +000034#include "qemu-timer.h"
aliguori376253e2009-03-05 23:01:23 +000035#include "monitor.h"
bellardbb36d472005-11-05 14:22:28 +000036
bellardbb36d472005-11-05 14:22:28 +000037#include <dirent.h>
38#include <sys/ioctl.h>
balrogb9dc0332007-10-04 22:47:34 +000039#include <signal.h>
bellardbb36d472005-11-05 14:22:28 +000040
aliguori446ab122008-09-14 01:06:09 +000041#include <linux/usbdevice_fs.h>
42#include <linux/version.h>
43#include "hw/usb.h"
bellardbb36d472005-11-05 14:22:28 +000044
blueswir1d9cf1572008-09-15 14:57:11 +000045/* We redefine it to avoid version problems */
46struct usb_ctrltransfer {
47 uint8_t bRequestType;
48 uint8_t bRequest;
49 uint16_t wValue;
50 uint16_t wIndex;
51 uint16_t wLength;
52 uint32_t timeout;
53 void *data;
54};
55
56struct usb_ctrlrequest {
57 uint8_t bRequestType;
58 uint8_t bRequest;
59 uint16_t wValue;
60 uint16_t wIndex;
61 uint16_t wLength;
62};
63
bellarda594cfb2005-11-06 16:13:29 +000064typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
ths5fafdf22007-09-16 21:08:06 +000065 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +000066 const char *product_name, int speed);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +010067
Markus Armbruster0745eb12009-11-27 13:05:53 +010068//#define DEBUG
aliguori64838172008-08-21 19:31:10 +000069
70#ifdef DEBUG
malcd0f2c4c2010-02-07 02:03:50 +030071#define DPRINTF printf
aliguori64838172008-08-21 19:31:10 +000072#else
malcd0f2c4c2010-02-07 02:03:50 +030073#define DPRINTF(...)
aliguori64838172008-08-21 19:31:10 +000074#endif
bellardbb36d472005-11-05 14:22:28 +000075
blueswir15be8e1f2008-10-25 11:47:20 +000076#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
77
aliguori0f431522008-10-07 20:06:37 +000078#define USBPROCBUS_PATH "/proc/bus/usb"
bellard1f6e24e2006-06-26 21:00:51 +000079#define PRODUCT_NAME_SZ 32
balrogb9dc0332007-10-04 22:47:34 +000080#define MAX_ENDPOINTS 16
aliguori0f431522008-10-07 20:06:37 +000081#define USBDEVBUS_PATH "/dev/bus/usb"
82#define USBSYSBUS_PATH "/sys/bus/usb"
83
84static char *usb_host_device_path;
85
86#define USB_FS_NONE 0
87#define USB_FS_PROC 1
88#define USB_FS_DEV 2
89#define USB_FS_SYS 3
90
91static int usb_fs_type;
bellardbb36d472005-11-05 14:22:28 +000092
balrogb9dc0332007-10-04 22:47:34 +000093/* endpoint association data */
94struct endp_data {
95 uint8_t type;
aliguori64838172008-08-21 19:31:10 +000096 uint8_t halted;
balrogb9dc0332007-10-04 22:47:34 +000097};
98
aliguori446ab122008-09-14 01:06:09 +000099enum {
100 CTRL_STATE_IDLE = 0,
101 CTRL_STATE_SETUP,
102 CTRL_STATE_DATA,
103 CTRL_STATE_ACK
104};
105
106/*
107 * Control transfer state.
David Ahern27911042010-04-24 10:26:22 -0600108 * Note that 'buffer' _must_ follow 'req' field because
aliguori446ab122008-09-14 01:06:09 +0000109 * we need contigious buffer when we submit control URB.
David Ahern27911042010-04-24 10:26:22 -0600110 */
aliguori446ab122008-09-14 01:06:09 +0000111struct ctrl_struct {
112 uint16_t len;
113 uint16_t offset;
114 uint8_t state;
115 struct usb_ctrlrequest req;
Christian Krausefd7a4462010-01-24 17:34:52 +0100116 uint8_t buffer[8192];
aliguori446ab122008-09-14 01:06:09 +0000117};
118
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100119struct USBAutoFilter {
120 uint32_t bus_num;
121 uint32_t addr;
122 uint32_t vendor_id;
123 uint32_t product_id;
124};
125
bellardbb36d472005-11-05 14:22:28 +0000126typedef struct USBHostDevice {
127 USBDevice dev;
aliguori64838172008-08-21 19:31:10 +0000128 int fd;
129
130 uint8_t descr[1024];
131 int descr_len;
132 int configuration;
aliguori446ab122008-09-14 01:06:09 +0000133 int ninterfaces;
aliguori24772c12008-08-21 19:31:52 +0000134 int closing;
aliguori64838172008-08-21 19:31:10 +0000135
aliguori446ab122008-09-14 01:06:09 +0000136 struct ctrl_struct ctrl;
balrogb9dc0332007-10-04 22:47:34 +0000137 struct endp_data endp_table[MAX_ENDPOINTS];
aliguori4b096fc2008-08-21 19:28:55 +0000138
aliguori4b096fc2008-08-21 19:28:55 +0000139 /* Host side address */
140 int bus_num;
141 int addr;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100142 struct USBAutoFilter match;
aliguori4b096fc2008-08-21 19:28:55 +0000143
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100144 QTAILQ_ENTRY(USBHostDevice) next;
bellardbb36d472005-11-05 14:22:28 +0000145} USBHostDevice;
146
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100147static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
148
149static int usb_host_close(USBHostDevice *dev);
150static int parse_filter(const char *spec, struct USBAutoFilter *f);
151static void usb_host_auto_check(void *unused);
152
aliguori64838172008-08-21 19:31:10 +0000153static int is_isoc(USBHostDevice *s, int ep)
154{
155 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
156}
157
158static int is_halted(USBHostDevice *s, int ep)
159{
160 return s->endp_table[ep - 1].halted;
161}
162
163static void clear_halt(USBHostDevice *s, int ep)
164{
165 s->endp_table[ep - 1].halted = 0;
166}
167
168static void set_halt(USBHostDevice *s, int ep)
169{
170 s->endp_table[ep - 1].halted = 1;
171}
172
David Ahern27911042010-04-24 10:26:22 -0600173/*
aliguori64838172008-08-21 19:31:10 +0000174 * Async URB state.
175 * We always allocate one isoc descriptor even for bulk transfers
David Ahern27911042010-04-24 10:26:22 -0600176 * to simplify allocation and casts.
aliguori64838172008-08-21 19:31:10 +0000177 */
178typedef struct AsyncURB
balrogb9dc0332007-10-04 22:47:34 +0000179{
aliguori64838172008-08-21 19:31:10 +0000180 struct usbdevfs_urb urb;
181 struct usbdevfs_iso_packet_desc isocpd;
182
183 USBPacket *packet;
184 USBHostDevice *hdev;
185} AsyncURB;
186
187static AsyncURB *async_alloc(void)
188{
189 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
balrogb9dc0332007-10-04 22:47:34 +0000190}
191
aliguori64838172008-08-21 19:31:10 +0000192static void async_free(AsyncURB *aurb)
balrogb9dc0332007-10-04 22:47:34 +0000193{
aliguori64838172008-08-21 19:31:10 +0000194 qemu_free(aurb);
195}
balrogb9dc0332007-10-04 22:47:34 +0000196
aliguori446ab122008-09-14 01:06:09 +0000197static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
198{
199 switch(s->ctrl.state) {
200 case CTRL_STATE_SETUP:
201 if (p->len < s->ctrl.len)
202 s->ctrl.len = p->len;
203 s->ctrl.state = CTRL_STATE_DATA;
204 p->len = 8;
205 break;
206
207 case CTRL_STATE_ACK:
208 s->ctrl.state = CTRL_STATE_IDLE;
209 p->len = 0;
210 break;
211
212 default:
213 break;
214 }
215}
216
aliguori64838172008-08-21 19:31:10 +0000217static void async_complete(void *opaque)
218{
219 USBHostDevice *s = opaque;
220 AsyncURB *aurb;
balrogb9dc0332007-10-04 22:47:34 +0000221
aliguori64838172008-08-21 19:31:10 +0000222 while (1) {
David Ahern27911042010-04-24 10:26:22 -0600223 USBPacket *p;
aliguori64838172008-08-21 19:31:10 +0000224
David Ahern27911042010-04-24 10:26:22 -0600225 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
aliguori64838172008-08-21 19:31:10 +0000226 if (r < 0) {
David Ahern27911042010-04-24 10:26:22 -0600227 if (errno == EAGAIN) {
aliguori64838172008-08-21 19:31:10 +0000228 return;
David Ahern27911042010-04-24 10:26:22 -0600229 }
aliguori24772c12008-08-21 19:31:52 +0000230 if (errno == ENODEV && !s->closing) {
David Ahern27911042010-04-24 10:26:22 -0600231 printf("husb: device %d.%d disconnected\n",
232 s->bus_num, s->addr);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100233 usb_host_close(s);
234 usb_host_auto_check(NULL);
aliguori64838172008-08-21 19:31:10 +0000235 return;
236 }
237
malcd0f2c4c2010-02-07 02:03:50 +0300238 DPRINTF("husb: async. reap urb failed errno %d\n", errno);
aliguori64838172008-08-21 19:31:10 +0000239 return;
balrogb9dc0332007-10-04 22:47:34 +0000240 }
aliguori64838172008-08-21 19:31:10 +0000241
242 p = aurb->packet;
243
David Ahern27911042010-04-24 10:26:22 -0600244 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
aliguori64838172008-08-21 19:31:10 +0000245 aurb, aurb->urb.status, aurb->urb.actual_length);
246
David Ahern27911042010-04-24 10:26:22 -0600247 if (p) {
aliguori64838172008-08-21 19:31:10 +0000248 switch (aurb->urb.status) {
249 case 0:
250 p->len = aurb->urb.actual_length;
David Ahern27911042010-04-24 10:26:22 -0600251 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
aliguori446ab122008-09-14 01:06:09 +0000252 async_complete_ctrl(s, p);
David Ahern27911042010-04-24 10:26:22 -0600253 }
aliguori64838172008-08-21 19:31:10 +0000254 break;
255
256 case -EPIPE:
257 set_halt(s, p->devep);
David Ahern27911042010-04-24 10:26:22 -0600258 p->len = USB_RET_STALL;
259 break;
Paul Bolledcc7e252009-10-13 13:40:08 +0200260
aliguori64838172008-08-21 19:31:10 +0000261 default:
262 p->len = USB_RET_NAK;
263 break;
264 }
265
266 usb_packet_complete(p);
David Ahern27911042010-04-24 10:26:22 -0600267 }
aliguori64838172008-08-21 19:31:10 +0000268
269 async_free(aurb);
balrogb9dc0332007-10-04 22:47:34 +0000270 }
balrogb9dc0332007-10-04 22:47:34 +0000271}
272
aliguori64838172008-08-21 19:31:10 +0000273static void async_cancel(USBPacket *unused, void *opaque)
balrogb9dc0332007-10-04 22:47:34 +0000274{
aliguori64838172008-08-21 19:31:10 +0000275 AsyncURB *aurb = opaque;
276 USBHostDevice *s = aurb->hdev;
balrogb9dc0332007-10-04 22:47:34 +0000277
malcd0f2c4c2010-02-07 02:03:50 +0300278 DPRINTF("husb: async cancel. aurb %p\n", aurb);
balrogb9dc0332007-10-04 22:47:34 +0000279
aliguori64838172008-08-21 19:31:10 +0000280 /* Mark it as dead (see async_complete above) */
281 aurb->packet = NULL;
282
283 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
284 if (r < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300285 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
balrogb9dc0332007-10-04 22:47:34 +0000286 }
balrogb9dc0332007-10-04 22:47:34 +0000287}
288
aliguori446ab122008-09-14 01:06:09 +0000289static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
balrogb9dc0332007-10-04 22:47:34 +0000290{
291 int dev_descr_len, config_descr_len;
Blue Swirld4c4e6f2010-04-25 18:23:04 +0000292 int interface, nb_interfaces;
balrogb9dc0332007-10-04 22:47:34 +0000293 int ret, i;
294
295 if (configuration == 0) /* address state - ignore */
296 return 1;
297
malcd0f2c4c2010-02-07 02:03:50 +0300298 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
aliguori446ab122008-09-14 01:06:09 +0000299
balrogb9dc0332007-10-04 22:47:34 +0000300 i = 0;
301 dev_descr_len = dev->descr[0];
David Ahern27911042010-04-24 10:26:22 -0600302 if (dev_descr_len > dev->descr_len) {
balrogb9dc0332007-10-04 22:47:34 +0000303 goto fail;
David Ahern27911042010-04-24 10:26:22 -0600304 }
balrogb9dc0332007-10-04 22:47:34 +0000305
306 i += dev_descr_len;
307 while (i < dev->descr_len) {
David Ahern27911042010-04-24 10:26:22 -0600308 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
309 i, dev->descr_len,
balrogb9dc0332007-10-04 22:47:34 +0000310 dev->descr[i], dev->descr[i+1]);
aliguori64838172008-08-21 19:31:10 +0000311
balrogb9dc0332007-10-04 22:47:34 +0000312 if (dev->descr[i+1] != USB_DT_CONFIG) {
313 i += dev->descr[i];
314 continue;
315 }
316 config_descr_len = dev->descr[i];
317
David Ahern27911042010-04-24 10:26:22 -0600318 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
aliguori1f3870a2008-08-21 19:27:48 +0000319
aliguori446ab122008-09-14 01:06:09 +0000320 if (configuration < 0 || configuration == dev->descr[i + 5]) {
321 configuration = dev->descr[i + 5];
balrogb9dc0332007-10-04 22:47:34 +0000322 break;
aliguori446ab122008-09-14 01:06:09 +0000323 }
balrogb9dc0332007-10-04 22:47:34 +0000324
325 i += config_descr_len;
326 }
327
328 if (i >= dev->descr_len) {
David Ahern27911042010-04-24 10:26:22 -0600329 fprintf(stderr,
330 "husb: update iface failed. no matching configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000331 goto fail;
332 }
333 nb_interfaces = dev->descr[i + 4];
334
335#ifdef USBDEVFS_DISCONNECT
336 /* earlier Linux 2.4 do not support that */
337 {
338 struct usbdevfs_ioctl ctrl;
339 for (interface = 0; interface < nb_interfaces; interface++) {
340 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
341 ctrl.ifno = interface;
342 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
343 if (ret < 0 && errno != ENODATA) {
344 perror("USBDEVFS_DISCONNECT");
345 goto fail;
346 }
347 }
348 }
349#endif
350
351 /* XXX: only grab if all interfaces are free */
352 for (interface = 0; interface < nb_interfaces; interface++) {
353 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
354 if (ret < 0) {
355 if (errno == EBUSY) {
aliguori64838172008-08-21 19:31:10 +0000356 printf("husb: update iface. device already grabbed\n");
balrogb9dc0332007-10-04 22:47:34 +0000357 } else {
aliguori64838172008-08-21 19:31:10 +0000358 perror("husb: failed to claim interface");
balrogb9dc0332007-10-04 22:47:34 +0000359 }
360 fail:
361 return 0;
362 }
363 }
364
aliguori64838172008-08-21 19:31:10 +0000365 printf("husb: %d interfaces claimed for configuration %d\n",
balrogb9dc0332007-10-04 22:47:34 +0000366 nb_interfaces, configuration);
balrogb9dc0332007-10-04 22:47:34 +0000367
aliguori446ab122008-09-14 01:06:09 +0000368 dev->ninterfaces = nb_interfaces;
369 dev->configuration = configuration;
370 return 1;
371}
372
373static int usb_host_release_interfaces(USBHostDevice *s)
374{
375 int ret, i;
376
malcd0f2c4c2010-02-07 02:03:50 +0300377 DPRINTF("husb: releasing interfaces\n");
aliguori446ab122008-09-14 01:06:09 +0000378
379 for (i = 0; i < s->ninterfaces; i++) {
380 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
381 if (ret < 0) {
382 perror("husb: failed to release interface");
383 return 0;
384 }
385 }
386
balrogb9dc0332007-10-04 22:47:34 +0000387 return 1;
388}
389
bellard059809e2006-07-19 18:06:15 +0000390static void usb_host_handle_reset(USBDevice *dev)
bellardbb36d472005-11-05 14:22:28 +0000391{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100392 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
aliguori64838172008-08-21 19:31:10 +0000393
malcd0f2c4c2010-02-07 02:03:50 +0300394 DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr);
aliguori64838172008-08-21 19:31:10 +0000395
bellardbb36d472005-11-05 14:22:28 +0000396 ioctl(s->fd, USBDEVFS_RESET);
aliguori446ab122008-09-14 01:06:09 +0000397
398 usb_host_claim_interfaces(s, s->configuration);
ths5fafdf22007-09-16 21:08:06 +0000399}
bellardbb36d472005-11-05 14:22:28 +0000400
bellard059809e2006-07-19 18:06:15 +0000401static void usb_host_handle_destroy(USBDevice *dev)
402{
403 USBHostDevice *s = (USBHostDevice *)dev;
404
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100405 usb_host_close(s);
406 QTAILQ_REMOVE(&hostdevs, s, next);
bellard059809e2006-07-19 18:06:15 +0000407}
408
balrogb9dc0332007-10-04 22:47:34 +0000409static int usb_linux_update_endp_table(USBHostDevice *s);
410
aliguori446ab122008-09-14 01:06:09 +0000411static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
bellardbb36d472005-11-05 14:22:28 +0000412{
aliguori64838172008-08-21 19:31:10 +0000413 struct usbdevfs_urb *urb;
aliguori446ab122008-09-14 01:06:09 +0000414 AsyncURB *aurb;
bellardbb36d472005-11-05 14:22:28 +0000415 int ret;
416
aliguori64838172008-08-21 19:31:10 +0000417 aurb = async_alloc();
aliguori64838172008-08-21 19:31:10 +0000418 aurb->hdev = s;
419 aurb->packet = p;
balrogb9dc0332007-10-04 22:47:34 +0000420
aliguori64838172008-08-21 19:31:10 +0000421 urb = &aurb->urb;
422
David Ahern27911042010-04-24 10:26:22 -0600423 if (p->pid == USB_TOKEN_IN) {
424 urb->endpoint = p->devep | 0x80;
425 } else {
426 urb->endpoint = p->devep;
427 }
aliguori64838172008-08-21 19:31:10 +0000428
429 if (is_halted(s, p->devep)) {
David Ahern27911042010-04-24 10:26:22 -0600430 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
aliguori64838172008-08-21 19:31:10 +0000431 if (ret < 0) {
David Ahern27911042010-04-24 10:26:22 -0600432 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
aliguori64838172008-08-21 19:31:10 +0000433 urb->endpoint, errno);
bellardbb36d472005-11-05 14:22:28 +0000434 return USB_RET_NAK;
bellardbb36d472005-11-05 14:22:28 +0000435 }
aliguori64838172008-08-21 19:31:10 +0000436 clear_halt(s, p->devep);
balrog046833e2007-10-31 00:27:50 +0000437 }
438
aliguori64838172008-08-21 19:31:10 +0000439 urb->buffer = p->data;
balrogb9dc0332007-10-04 22:47:34 +0000440 urb->buffer_length = p->len;
aliguori64838172008-08-21 19:31:10 +0000441
442 if (is_isoc(s, p->devep)) {
443 /* Setup ISOC transfer */
444 urb->type = USBDEVFS_URB_TYPE_ISO;
445 urb->flags = USBDEVFS_URB_ISO_ASAP;
446 urb->number_of_packets = 1;
447 urb->iso_frame_desc[0].length = p->len;
balrogb9dc0332007-10-04 22:47:34 +0000448 } else {
aliguori64838172008-08-21 19:31:10 +0000449 /* Setup bulk transfer */
450 urb->type = USBDEVFS_URB_TYPE_BULK;
451 }
452
453 urb->usercontext = s;
454
455 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
456
David Ahern27911042010-04-24 10:26:22 -0600457 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
458 urb->endpoint, p->len, aurb);
aliguori64838172008-08-21 19:31:10 +0000459
460 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300461 DPRINTF("husb: submit failed. errno %d\n", errno);
aliguori64838172008-08-21 19:31:10 +0000462 async_free(aurb);
463
balrogb9dc0332007-10-04 22:47:34 +0000464 switch(errno) {
465 case ETIMEDOUT:
466 return USB_RET_NAK;
467 case EPIPE:
468 default:
469 return USB_RET_STALL;
470 }
471 }
aliguori64838172008-08-21 19:31:10 +0000472
473 usb_defer_packet(p, async_cancel, aurb);
balrogb9dc0332007-10-04 22:47:34 +0000474 return USB_RET_ASYNC;
balrogb9dc0332007-10-04 22:47:34 +0000475}
476
aliguori446ab122008-09-14 01:06:09 +0000477static int ctrl_error(void)
478{
David Ahern27911042010-04-24 10:26:22 -0600479 if (errno == ETIMEDOUT) {
aliguori446ab122008-09-14 01:06:09 +0000480 return USB_RET_NAK;
David Ahern27911042010-04-24 10:26:22 -0600481 } else {
aliguori446ab122008-09-14 01:06:09 +0000482 return USB_RET_STALL;
David Ahern27911042010-04-24 10:26:22 -0600483 }
aliguori446ab122008-09-14 01:06:09 +0000484}
485
486static int usb_host_set_address(USBHostDevice *s, int addr)
487{
malcd0f2c4c2010-02-07 02:03:50 +0300488 DPRINTF("husb: ctrl set addr %u\n", addr);
aliguori446ab122008-09-14 01:06:09 +0000489 s->dev.addr = addr;
490 return 0;
491}
492
493static int usb_host_set_config(USBHostDevice *s, int config)
494{
495 usb_host_release_interfaces(s);
496
497 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
David Ahern27911042010-04-24 10:26:22 -0600498
malcd0f2c4c2010-02-07 02:03:50 +0300499 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
David Ahern27911042010-04-24 10:26:22 -0600500
501 if (ret < 0) {
aliguori446ab122008-09-14 01:06:09 +0000502 return ctrl_error();
David Ahern27911042010-04-24 10:26:22 -0600503 }
aliguori446ab122008-09-14 01:06:09 +0000504 usb_host_claim_interfaces(s, config);
505 return 0;
506}
507
508static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
509{
510 struct usbdevfs_setinterface si;
511 int ret;
512
513 si.interface = iface;
514 si.altsetting = alt;
515 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
aliguori446ab122008-09-14 01:06:09 +0000516
David Ahern27911042010-04-24 10:26:22 -0600517 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
518 iface, alt, ret, errno);
519
520 if (ret < 0) {
521 return ctrl_error();
522 }
aliguori446ab122008-09-14 01:06:09 +0000523 usb_linux_update_endp_table(s);
524 return 0;
525}
526
527static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
528{
529 struct usbdevfs_urb *urb;
530 AsyncURB *aurb;
531 int ret, value, index;
Jim Parisc4c0e232009-08-24 14:56:12 -0400532 int buffer_len;
aliguori446ab122008-09-14 01:06:09 +0000533
David Ahern27911042010-04-24 10:26:22 -0600534 /*
aliguori446ab122008-09-14 01:06:09 +0000535 * Process certain standard device requests.
536 * These are infrequent and are processed synchronously.
537 */
538 value = le16_to_cpu(s->ctrl.req.wValue);
539 index = le16_to_cpu(s->ctrl.req.wIndex);
540
malcd0f2c4c2010-02-07 02:03:50 +0300541 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
David Ahern27911042010-04-24 10:26:22 -0600542 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
543 s->ctrl.len);
aliguori446ab122008-09-14 01:06:09 +0000544
545 if (s->ctrl.req.bRequestType == 0) {
546 switch (s->ctrl.req.bRequest) {
547 case USB_REQ_SET_ADDRESS:
548 return usb_host_set_address(s, value);
549
550 case USB_REQ_SET_CONFIGURATION:
551 return usb_host_set_config(s, value & 0xff);
552 }
553 }
554
555 if (s->ctrl.req.bRequestType == 1 &&
David Ahern27911042010-04-24 10:26:22 -0600556 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE) {
aliguori446ab122008-09-14 01:06:09 +0000557 return usb_host_set_interface(s, index, value);
David Ahern27911042010-04-24 10:26:22 -0600558 }
aliguori446ab122008-09-14 01:06:09 +0000559
560 /* The rest are asynchronous */
561
Jim Parisc4c0e232009-08-24 14:56:12 -0400562 buffer_len = 8 + s->ctrl.len;
563 if (buffer_len > sizeof(s->ctrl.buffer)) {
malcb2e3b6e2009-09-12 03:18:18 +0400564 fprintf(stderr, "husb: ctrl buffer too small (%u > %zu)\n",
565 buffer_len, sizeof(s->ctrl.buffer));
566 return USB_RET_STALL;
Jim Parisc4c0e232009-08-24 14:56:12 -0400567 }
568
aliguori446ab122008-09-14 01:06:09 +0000569 aurb = async_alloc();
aliguori446ab122008-09-14 01:06:09 +0000570 aurb->hdev = s;
571 aurb->packet = p;
572
David Ahern27911042010-04-24 10:26:22 -0600573 /*
aliguori446ab122008-09-14 01:06:09 +0000574 * Setup ctrl transfer.
575 *
576 * s->ctrl is layed out such that data buffer immediately follows
577 * 'req' struct which is exactly what usbdevfs expects.
David Ahern27911042010-04-24 10:26:22 -0600578 */
aliguori446ab122008-09-14 01:06:09 +0000579 urb = &aurb->urb;
580
581 urb->type = USBDEVFS_URB_TYPE_CONTROL;
582 urb->endpoint = p->devep;
583
584 urb->buffer = &s->ctrl.req;
Jim Parisc4c0e232009-08-24 14:56:12 -0400585 urb->buffer_length = buffer_len;
aliguori446ab122008-09-14 01:06:09 +0000586
587 urb->usercontext = s;
588
589 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
590
malcd0f2c4c2010-02-07 02:03:50 +0300591 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
aliguori446ab122008-09-14 01:06:09 +0000592
593 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300594 DPRINTF("husb: submit failed. errno %d\n", errno);
aliguori446ab122008-09-14 01:06:09 +0000595 async_free(aurb);
596
597 switch(errno) {
598 case ETIMEDOUT:
599 return USB_RET_NAK;
600 case EPIPE:
601 default:
602 return USB_RET_STALL;
603 }
604 }
605
606 usb_defer_packet(p, async_cancel, aurb);
607 return USB_RET_ASYNC;
608}
609
610static int do_token_setup(USBDevice *dev, USBPacket *p)
611{
612 USBHostDevice *s = (USBHostDevice *) dev;
613 int ret = 0;
614
David Ahern27911042010-04-24 10:26:22 -0600615 if (p->len != 8) {
aliguori446ab122008-09-14 01:06:09 +0000616 return USB_RET_STALL;
David Ahern27911042010-04-24 10:26:22 -0600617 }
618
aliguori446ab122008-09-14 01:06:09 +0000619 memcpy(&s->ctrl.req, p->data, 8);
620 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
621 s->ctrl.offset = 0;
622 s->ctrl.state = CTRL_STATE_SETUP;
623
624 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
625 ret = usb_host_handle_control(s, p);
David Ahern27911042010-04-24 10:26:22 -0600626 if (ret < 0) {
aliguori446ab122008-09-14 01:06:09 +0000627 return ret;
David Ahern27911042010-04-24 10:26:22 -0600628 }
aliguori446ab122008-09-14 01:06:09 +0000629
David Ahern27911042010-04-24 10:26:22 -0600630 if (ret < s->ctrl.len) {
aliguori446ab122008-09-14 01:06:09 +0000631 s->ctrl.len = ret;
David Ahern27911042010-04-24 10:26:22 -0600632 }
aliguori446ab122008-09-14 01:06:09 +0000633 s->ctrl.state = CTRL_STATE_DATA;
634 } else {
David Ahern27911042010-04-24 10:26:22 -0600635 if (s->ctrl.len == 0) {
aliguori446ab122008-09-14 01:06:09 +0000636 s->ctrl.state = CTRL_STATE_ACK;
David Ahern27911042010-04-24 10:26:22 -0600637 } else {
aliguori446ab122008-09-14 01:06:09 +0000638 s->ctrl.state = CTRL_STATE_DATA;
David Ahern27911042010-04-24 10:26:22 -0600639 }
aliguori446ab122008-09-14 01:06:09 +0000640 }
641
642 return ret;
643}
644
645static int do_token_in(USBDevice *dev, USBPacket *p)
646{
647 USBHostDevice *s = (USBHostDevice *) dev;
648 int ret = 0;
649
David Ahern27911042010-04-24 10:26:22 -0600650 if (p->devep != 0) {
aliguori446ab122008-09-14 01:06:09 +0000651 return usb_host_handle_data(s, p);
David Ahern27911042010-04-24 10:26:22 -0600652 }
aliguori446ab122008-09-14 01:06:09 +0000653
654 switch(s->ctrl.state) {
655 case CTRL_STATE_ACK:
656 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
657 ret = usb_host_handle_control(s, p);
David Ahern27911042010-04-24 10:26:22 -0600658 if (ret == USB_RET_ASYNC) {
aliguori446ab122008-09-14 01:06:09 +0000659 return USB_RET_ASYNC;
David Ahern27911042010-04-24 10:26:22 -0600660 }
aliguori446ab122008-09-14 01:06:09 +0000661 s->ctrl.state = CTRL_STATE_IDLE;
662 return ret > 0 ? 0 : ret;
663 }
664
665 return 0;
666
667 case CTRL_STATE_DATA:
668 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
669 int len = s->ctrl.len - s->ctrl.offset;
David Ahern27911042010-04-24 10:26:22 -0600670 if (len > p->len) {
aliguori446ab122008-09-14 01:06:09 +0000671 len = p->len;
David Ahern27911042010-04-24 10:26:22 -0600672 }
aliguori446ab122008-09-14 01:06:09 +0000673 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
674 s->ctrl.offset += len;
David Ahern27911042010-04-24 10:26:22 -0600675 if (s->ctrl.offset >= s->ctrl.len) {
aliguori446ab122008-09-14 01:06:09 +0000676 s->ctrl.state = CTRL_STATE_ACK;
David Ahern27911042010-04-24 10:26:22 -0600677 }
aliguori446ab122008-09-14 01:06:09 +0000678 return len;
679 }
680
681 s->ctrl.state = CTRL_STATE_IDLE;
682 return USB_RET_STALL;
683
684 default:
685 return USB_RET_STALL;
686 }
687}
688
689static int do_token_out(USBDevice *dev, USBPacket *p)
690{
691 USBHostDevice *s = (USBHostDevice *) dev;
692
David Ahern27911042010-04-24 10:26:22 -0600693 if (p->devep != 0) {
aliguori446ab122008-09-14 01:06:09 +0000694 return usb_host_handle_data(s, p);
David Ahern27911042010-04-24 10:26:22 -0600695 }
aliguori446ab122008-09-14 01:06:09 +0000696
697 switch(s->ctrl.state) {
698 case CTRL_STATE_ACK:
699 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
700 s->ctrl.state = CTRL_STATE_IDLE;
701 /* transfer OK */
702 } else {
703 /* ignore additional output */
704 }
705 return 0;
706
707 case CTRL_STATE_DATA:
708 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
709 int len = s->ctrl.len - s->ctrl.offset;
David Ahern27911042010-04-24 10:26:22 -0600710 if (len > p->len) {
aliguori446ab122008-09-14 01:06:09 +0000711 len = p->len;
David Ahern27911042010-04-24 10:26:22 -0600712 }
aliguori446ab122008-09-14 01:06:09 +0000713 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
714 s->ctrl.offset += len;
David Ahern27911042010-04-24 10:26:22 -0600715 if (s->ctrl.offset >= s->ctrl.len) {
aliguori446ab122008-09-14 01:06:09 +0000716 s->ctrl.state = CTRL_STATE_ACK;
David Ahern27911042010-04-24 10:26:22 -0600717 }
aliguori446ab122008-09-14 01:06:09 +0000718 return len;
719 }
720
721 s->ctrl.state = CTRL_STATE_IDLE;
722 return USB_RET_STALL;
723
724 default:
725 return USB_RET_STALL;
726 }
727}
728
729/*
730 * Packet handler.
731 * Called by the HC (host controller).
732 *
733 * Returns length of the transaction or one of the USB_RET_XXX codes.
734 */
blueswir1d9cf1572008-09-15 14:57:11 +0000735static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
aliguori446ab122008-09-14 01:06:09 +0000736{
737 switch(p->pid) {
738 case USB_MSG_ATTACH:
739 s->state = USB_STATE_ATTACHED;
740 return 0;
741
742 case USB_MSG_DETACH:
743 s->state = USB_STATE_NOTATTACHED;
744 return 0;
745
746 case USB_MSG_RESET:
747 s->remote_wakeup = 0;
748 s->addr = 0;
749 s->state = USB_STATE_DEFAULT;
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200750 s->info->handle_reset(s);
aliguori446ab122008-09-14 01:06:09 +0000751 return 0;
752 }
753
754 /* Rest of the PIDs must match our address */
David Ahern27911042010-04-24 10:26:22 -0600755 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr) {
aliguori446ab122008-09-14 01:06:09 +0000756 return USB_RET_NODEV;
David Ahern27911042010-04-24 10:26:22 -0600757 }
aliguori446ab122008-09-14 01:06:09 +0000758
759 switch (p->pid) {
760 case USB_TOKEN_SETUP:
761 return do_token_setup(s, p);
762
763 case USB_TOKEN_IN:
764 return do_token_in(s, p);
765
766 case USB_TOKEN_OUT:
767 return do_token_out(s, p);
David Ahern27911042010-04-24 10:26:22 -0600768
aliguori446ab122008-09-14 01:06:09 +0000769 default:
770 return USB_RET_STALL;
771 }
772}
773
balrogb9dc0332007-10-04 22:47:34 +0000774/* returns 1 on problem encountered or 0 for success */
775static int usb_linux_update_endp_table(USBHostDevice *s)
776{
777 uint8_t *descriptors;
778 uint8_t devep, type, configuration, alt_interface;
pbrooke41b3912008-10-28 18:22:59 +0000779 struct usb_ctrltransfer ct;
balrogb9dc0332007-10-04 22:47:34 +0000780 int interface, ret, length, i;
781
782 ct.bRequestType = USB_DIR_IN;
783 ct.bRequest = USB_REQ_GET_CONFIGURATION;
784 ct.wValue = 0;
785 ct.wIndex = 0;
786 ct.wLength = 1;
787 ct.data = &configuration;
788 ct.timeout = 50;
789
790 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
791 if (ret < 0) {
792 perror("usb_linux_update_endp_table");
793 return 1;
794 }
795
796 /* in address state */
David Ahern27911042010-04-24 10:26:22 -0600797 if (configuration == 0) {
balrogb9dc0332007-10-04 22:47:34 +0000798 return 1;
David Ahern27911042010-04-24 10:26:22 -0600799 }
balrogb9dc0332007-10-04 22:47:34 +0000800
801 /* get the desired configuration, interface, and endpoint descriptors
802 * from device description */
803 descriptors = &s->descr[18];
804 length = s->descr_len - 18;
805 i = 0;
806
807 if (descriptors[i + 1] != USB_DT_CONFIG ||
808 descriptors[i + 5] != configuration) {
malcd0f2c4c2010-02-07 02:03:50 +0300809 DPRINTF("invalid descriptor data - configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000810 return 1;
811 }
812 i += descriptors[i];
813
814 while (i < length) {
815 if (descriptors[i + 1] != USB_DT_INTERFACE ||
816 (descriptors[i + 1] == USB_DT_INTERFACE &&
817 descriptors[i + 4] == 0)) {
818 i += descriptors[i];
819 continue;
820 }
821
822 interface = descriptors[i + 2];
823
824 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
825 ct.bRequest = USB_REQ_GET_INTERFACE;
826 ct.wValue = 0;
827 ct.wIndex = interface;
828 ct.wLength = 1;
829 ct.data = &alt_interface;
830 ct.timeout = 50;
831
832 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
833 if (ret < 0) {
Jason Wesseld55ebf52009-05-18 10:00:28 -0500834 alt_interface = interface;
balrogb9dc0332007-10-04 22:47:34 +0000835 }
836
837 /* the current interface descriptor is the active interface
838 * and has endpoints */
839 if (descriptors[i + 3] != alt_interface) {
840 i += descriptors[i];
841 continue;
842 }
843
844 /* advance to the endpoints */
David Ahern27911042010-04-24 10:26:22 -0600845 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
balrogb9dc0332007-10-04 22:47:34 +0000846 i += descriptors[i];
David Ahern27911042010-04-24 10:26:22 -0600847 }
balrogb9dc0332007-10-04 22:47:34 +0000848
849 if (i >= length)
850 break;
851
852 while (i < length) {
David Ahern27911042010-04-24 10:26:22 -0600853 if (descriptors[i + 1] != USB_DT_ENDPOINT) {
balrogb9dc0332007-10-04 22:47:34 +0000854 break;
David Ahern27911042010-04-24 10:26:22 -0600855 }
balrogb9dc0332007-10-04 22:47:34 +0000856
857 devep = descriptors[i + 2];
858 switch (descriptors[i + 3] & 0x3) {
859 case 0x00:
860 type = USBDEVFS_URB_TYPE_CONTROL;
861 break;
862 case 0x01:
863 type = USBDEVFS_URB_TYPE_ISO;
864 break;
865 case 0x02:
866 type = USBDEVFS_URB_TYPE_BULK;
867 break;
868 case 0x03:
869 type = USBDEVFS_URB_TYPE_INTERRUPT;
870 break;
Anthony Liguoriddbda432010-03-17 16:00:24 -0500871 default:
872 DPRINTF("usb_host: malformed endpoint type\n");
873 type = USBDEVFS_URB_TYPE_BULK;
balrogb9dc0332007-10-04 22:47:34 +0000874 }
875 s->endp_table[(devep & 0xf) - 1].type = type;
aliguori64838172008-08-21 19:31:10 +0000876 s->endp_table[(devep & 0xf) - 1].halted = 0;
balrogb9dc0332007-10-04 22:47:34 +0000877
878 i += descriptors[i];
879 }
880 }
881 return 0;
882}
883
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100884static int usb_host_open(USBHostDevice *dev, int bus_num,
885 int addr, const char *prod_name)
bellardbb36d472005-11-05 14:22:28 +0000886{
balrogb9dc0332007-10-04 22:47:34 +0000887 int fd = -1, ret;
bellardbb36d472005-11-05 14:22:28 +0000888 struct usbdevfs_connectinfo ci;
bellarda594cfb2005-11-06 16:13:29 +0000889 char buf[1024];
aliguori1f3870a2008-08-21 19:27:48 +0000890
David Ahern27911042010-04-24 10:26:22 -0600891 if (dev->fd != -1) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100892 goto fail;
David Ahern27911042010-04-24 10:26:22 -0600893 }
aliguori64838172008-08-21 19:31:10 +0000894 printf("husb: open device %d.%d\n", bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000895
aliguori0f431522008-10-07 20:06:37 +0000896 if (!usb_host_device_path) {
897 perror("husb: USB Host Device Path not set");
898 goto fail;
899 }
900 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
bellarda594cfb2005-11-06 16:13:29 +0000901 bus_num, addr);
balrogb9dc0332007-10-04 22:47:34 +0000902 fd = open(buf, O_RDWR | O_NONBLOCK);
bellardbb36d472005-11-05 14:22:28 +0000903 if (fd < 0) {
bellarda594cfb2005-11-06 16:13:29 +0000904 perror(buf);
aliguori1f3870a2008-08-21 19:27:48 +0000905 goto fail;
bellardbb36d472005-11-05 14:22:28 +0000906 }
malcd0f2c4c2010-02-07 02:03:50 +0300907 DPRINTF("husb: opened %s\n", buf);
bellardbb36d472005-11-05 14:22:28 +0000908
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200909 dev->bus_num = bus_num;
910 dev->addr = addr;
Gerd Hoffmann22f84e72009-09-25 16:55:28 +0200911 dev->fd = fd;
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200912
balrogb9dc0332007-10-04 22:47:34 +0000913 /* read the device description */
914 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
915 if (dev->descr_len <= 0) {
aliguori64838172008-08-21 19:31:10 +0000916 perror("husb: reading device data failed");
bellardbb36d472005-11-05 14:22:28 +0000917 goto fail;
918 }
ths3b46e622007-09-17 08:09:54 +0000919
balrogb9dc0332007-10-04 22:47:34 +0000920#ifdef DEBUG
bellard868bfe22005-11-13 21:53:15 +0000921 {
balrogb9dc0332007-10-04 22:47:34 +0000922 int x;
923 printf("=== begin dumping device descriptor data ===\n");
David Ahern27911042010-04-24 10:26:22 -0600924 for (x = 0; x < dev->descr_len; x++) {
balrogb9dc0332007-10-04 22:47:34 +0000925 printf("%02x ", dev->descr[x]);
David Ahern27911042010-04-24 10:26:22 -0600926 }
balrogb9dc0332007-10-04 22:47:34 +0000927 printf("\n=== end dumping device descriptor data ===\n");
bellarda594cfb2005-11-06 16:13:29 +0000928 }
929#endif
930
balrogb9dc0332007-10-04 22:47:34 +0000931
David Ahern27911042010-04-24 10:26:22 -0600932 /*
933 * Initial configuration is -1 which makes us claim first
aliguori446ab122008-09-14 01:06:09 +0000934 * available config. We used to start with 1, which does not
David Ahern27911042010-04-24 10:26:22 -0600935 * always work. I've seen devices where first config starts
aliguori446ab122008-09-14 01:06:09 +0000936 * with 2.
937 */
David Ahern27911042010-04-24 10:26:22 -0600938 if (!usb_host_claim_interfaces(dev, -1)) {
balrogb9dc0332007-10-04 22:47:34 +0000939 goto fail;
David Ahern27911042010-04-24 10:26:22 -0600940 }
bellardbb36d472005-11-05 14:22:28 +0000941
942 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
943 if (ret < 0) {
balrog046833e2007-10-31 00:27:50 +0000944 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
bellardbb36d472005-11-05 14:22:28 +0000945 goto fail;
946 }
947
aliguori64838172008-08-21 19:31:10 +0000948 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
bellardbb36d472005-11-05 14:22:28 +0000949
balrogb9dc0332007-10-04 22:47:34 +0000950 ret = usb_linux_update_endp_table(dev);
David Ahern27911042010-04-24 10:26:22 -0600951 if (ret) {
bellardbb36d472005-11-05 14:22:28 +0000952 goto fail;
David Ahern27911042010-04-24 10:26:22 -0600953 }
balrogb9dc0332007-10-04 22:47:34 +0000954
David Ahern27911042010-04-24 10:26:22 -0600955 if (ci.slow) {
bellardbb36d472005-11-05 14:22:28 +0000956 dev->dev.speed = USB_SPEED_LOW;
David Ahern27911042010-04-24 10:26:22 -0600957 } else {
bellardbb36d472005-11-05 14:22:28 +0000958 dev->dev.speed = USB_SPEED_HIGH;
David Ahern27911042010-04-24 10:26:22 -0600959 }
bellardbb36d472005-11-05 14:22:28 +0000960
David Ahern27911042010-04-24 10:26:22 -0600961 if (!prod_name || prod_name[0] == '\0') {
Markus Armbruster0fe6d122009-12-09 17:07:51 +0100962 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
aliguori4b096fc2008-08-21 19:28:55 +0000963 "host:%d.%d", bus_num, addr);
David Ahern27911042010-04-24 10:26:22 -0600964 } else {
Markus Armbruster0fe6d122009-12-09 17:07:51 +0100965 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
aliguori4b096fc2008-08-21 19:28:55 +0000966 prod_name);
David Ahern27911042010-04-24 10:26:22 -0600967 }
bellard1f6e24e2006-06-26 21:00:51 +0000968
aliguori64838172008-08-21 19:31:10 +0000969 /* USB devio uses 'write' flag to check for async completions */
970 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
aliguori1f3870a2008-08-21 19:27:48 +0000971
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100972 usb_device_attach(&dev->dev);
973 return 0;
aliguori4b096fc2008-08-21 19:28:55 +0000974
balrogb9dc0332007-10-04 22:47:34 +0000975fail:
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100976 dev->fd = -1;
David Ahern27911042010-04-24 10:26:22 -0600977 if (fd != -1) {
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200978 close(fd);
David Ahern27911042010-04-24 10:26:22 -0600979 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100980 return -1;
981}
982
983static int usb_host_close(USBHostDevice *dev)
984{
David Ahern27911042010-04-24 10:26:22 -0600985 if (dev->fd == -1) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100986 return -1;
David Ahern27911042010-04-24 10:26:22 -0600987 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100988
989 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
990 dev->closing = 1;
991 async_complete(dev);
992 dev->closing = 0;
993 usb_device_detach(&dev->dev);
994 close(dev->fd);
995 dev->fd = -1;
996 return 0;
997}
998
999static int usb_host_initfn(USBDevice *dev)
1000{
1001 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1002
1003 dev->auto_attach = 0;
1004 s->fd = -1;
1005 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1006 usb_host_auto_check(NULL);
1007 return 0;
bellardbb36d472005-11-05 14:22:28 +00001008}
1009
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001010static struct USBDeviceInfo usb_host_dev_info = {
Markus Armbruster06384692009-12-09 17:07:52 +01001011 .product_desc = "USB Host Device",
Markus Armbruster556cd092009-12-09 17:07:53 +01001012 .qdev.name = "usb-host",
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001013 .qdev.size = sizeof(USBHostDevice),
1014 .init = usb_host_initfn,
1015 .handle_packet = usb_host_handle_packet,
1016 .handle_reset = usb_host_handle_reset,
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001017 .handle_destroy = usb_host_handle_destroy,
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001018 .usbdevice_name = "host",
1019 .usbdevice_init = usb_host_device_open,
1020 .qdev.props = (Property[]) {
1021 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1022 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1023 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1024 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1025 DEFINE_PROP_END_OF_LIST(),
1026 },
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001027};
1028
1029static void usb_host_register_devices(void)
1030{
1031 usb_qdev_register(&usb_host_dev_info);
1032}
1033device_init(usb_host_register_devices)
1034
aliguori4b096fc2008-08-21 19:28:55 +00001035USBDevice *usb_host_device_open(const char *devname)
1036{
Markus Armbruster0745eb12009-11-27 13:05:53 +01001037 struct USBAutoFilter filter;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001038 USBDevice *dev;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001039 char *p;
1040
Markus Armbruster556cd092009-12-09 17:07:53 +01001041 dev = usb_create(NULL /* FIXME */, "usb-host");
aliguori4b096fc2008-08-21 19:28:55 +00001042
aliguori5d0c5752008-09-14 01:07:41 +00001043 if (strstr(devname, "auto:")) {
David Ahern27911042010-04-24 10:26:22 -06001044 if (parse_filter(devname, &filter) < 0) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001045 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001046 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001047 } else {
1048 if ((p = strchr(devname, '.'))) {
Markus Armbruster0745eb12009-11-27 13:05:53 +01001049 filter.bus_num = strtoul(devname, NULL, 0);
1050 filter.addr = strtoul(p + 1, NULL, 0);
1051 filter.vendor_id = 0;
1052 filter.product_id = 0;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001053 } else if ((p = strchr(devname, ':'))) {
Markus Armbruster0745eb12009-11-27 13:05:53 +01001054 filter.bus_num = 0;
1055 filter.addr = 0;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001056 filter.vendor_id = strtoul(devname, NULL, 16);
Markus Armbruster0745eb12009-11-27 13:05:53 +01001057 filter.product_id = strtoul(p + 1, NULL, 16);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001058 } else {
1059 goto fail;
1060 }
aliguori5d0c5752008-09-14 01:07:41 +00001061 }
1062
Markus Armbruster0745eb12009-11-27 13:05:53 +01001063 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1064 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001065 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1066 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
Kevin Wolfbeb6f0d2010-01-15 12:56:41 +01001067 qdev_init_nofail(&dev->qdev);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001068 return dev;
aliguori4b096fc2008-08-21 19:28:55 +00001069
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001070fail:
1071 qdev_free(&dev->qdev);
1072 return NULL;
aliguori4b096fc2008-08-21 19:28:55 +00001073}
aliguori5d0c5752008-09-14 01:07:41 +00001074
1075int usb_host_device_close(const char *devname)
1076{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001077#if 0
aliguori5d0c5752008-09-14 01:07:41 +00001078 char product_name[PRODUCT_NAME_SZ];
1079 int bus_num, addr;
1080 USBHostDevice *s;
1081
David Ahern27911042010-04-24 10:26:22 -06001082 if (strstr(devname, "auto:")) {
aliguori5d0c5752008-09-14 01:07:41 +00001083 return usb_host_auto_del(devname);
David Ahern27911042010-04-24 10:26:22 -06001084 }
1085 if (usb_host_find_device(&bus_num, &addr, product_name,
1086 sizeof(product_name), devname) < 0) {
aliguori5d0c5752008-09-14 01:07:41 +00001087 return -1;
David Ahern27911042010-04-24 10:26:22 -06001088 }
aliguori5d0c5752008-09-14 01:07:41 +00001089 s = hostdev_find(bus_num, addr);
1090 if (s) {
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001091 usb_device_delete_addr(s->bus_num, s->dev.addr);
aliguori5d0c5752008-09-14 01:07:41 +00001092 return 0;
1093 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001094#endif
aliguori5d0c5752008-09-14 01:07:41 +00001095
1096 return -1;
1097}
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001098
bellarda594cfb2005-11-06 16:13:29 +00001099static int get_tag_value(char *buf, int buf_size,
ths5fafdf22007-09-16 21:08:06 +00001100 const char *str, const char *tag,
bellarda594cfb2005-11-06 16:13:29 +00001101 const char *stopchars)
bellardbb36d472005-11-05 14:22:28 +00001102{
bellarda594cfb2005-11-06 16:13:29 +00001103 const char *p;
1104 char *q;
1105 p = strstr(str, tag);
David Ahern27911042010-04-24 10:26:22 -06001106 if (!p) {
bellarda594cfb2005-11-06 16:13:29 +00001107 return -1;
David Ahern27911042010-04-24 10:26:22 -06001108 }
bellarda594cfb2005-11-06 16:13:29 +00001109 p += strlen(tag);
David Ahern27911042010-04-24 10:26:22 -06001110 while (qemu_isspace(*p)) {
bellarda594cfb2005-11-06 16:13:29 +00001111 p++;
David Ahern27911042010-04-24 10:26:22 -06001112 }
bellarda594cfb2005-11-06 16:13:29 +00001113 q = buf;
1114 while (*p != '\0' && !strchr(stopchars, *p)) {
David Ahern27911042010-04-24 10:26:22 -06001115 if ((q - buf) < (buf_size - 1)) {
bellarda594cfb2005-11-06 16:13:29 +00001116 *q++ = *p;
David Ahern27911042010-04-24 10:26:22 -06001117 }
bellarda594cfb2005-11-06 16:13:29 +00001118 p++;
1119 }
1120 *q = '\0';
1121 return q - buf;
1122}
bellardbb36d472005-11-05 14:22:28 +00001123
aliguori0f431522008-10-07 20:06:37 +00001124/*
1125 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1126 * host's USB devices. This is legacy support since many distributions
1127 * are moving to /sys/bus/usb
1128 */
1129static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
bellarda594cfb2005-11-06 16:13:29 +00001130{
Blue Swirl660f11b2009-07-31 21:16:51 +00001131 FILE *f = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001132 char line[1024];
1133 char buf[1024];
1134 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
bellarda594cfb2005-11-06 16:13:29 +00001135 char product_name[512];
aliguori0f431522008-10-07 20:06:37 +00001136 int ret = 0;
ths3b46e622007-09-17 08:09:54 +00001137
aliguori0f431522008-10-07 20:06:37 +00001138 if (!usb_host_device_path) {
1139 perror("husb: USB Host Device Path not set");
1140 goto the_end;
bellarda594cfb2005-11-06 16:13:29 +00001141 }
aliguori0f431522008-10-07 20:06:37 +00001142 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1143 f = fopen(line, "r");
1144 if (!f) {
1145 perror("husb: cannot open devices file");
1146 goto the_end;
1147 }
1148
bellarda594cfb2005-11-06 16:13:29 +00001149 device_count = 0;
1150 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
bellardbb36d472005-11-05 14:22:28 +00001151 for(;;) {
David Ahern27911042010-04-24 10:26:22 -06001152 if (fgets(line, sizeof(line), f) == NULL) {
bellardbb36d472005-11-05 14:22:28 +00001153 break;
David Ahern27911042010-04-24 10:26:22 -06001154 }
1155 if (strlen(line) > 0) {
bellarda594cfb2005-11-06 16:13:29 +00001156 line[strlen(line) - 1] = '\0';
David Ahern27911042010-04-24 10:26:22 -06001157 }
bellarda594cfb2005-11-06 16:13:29 +00001158 if (line[0] == 'T' && line[1] == ':') {
pbrook38ca0f62006-03-11 18:03:38 +00001159 if (device_count && (vendor_id || product_id)) {
1160 /* New device. Add the previously discovered device. */
ths5fafdf22007-09-16 21:08:06 +00001161 ret = func(opaque, bus_num, addr, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001162 product_id, product_name, speed);
David Ahern27911042010-04-24 10:26:22 -06001163 if (ret) {
bellarda594cfb2005-11-06 16:13:29 +00001164 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001165 }
bellarda594cfb2005-11-06 16:13:29 +00001166 }
David Ahern27911042010-04-24 10:26:22 -06001167 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001168 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001169 }
bellarda594cfb2005-11-06 16:13:29 +00001170 bus_num = atoi(buf);
David Ahern27911042010-04-24 10:26:22 -06001171 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001172 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001173 }
bellarda594cfb2005-11-06 16:13:29 +00001174 addr = atoi(buf);
David Ahern27911042010-04-24 10:26:22 -06001175 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001176 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001177 }
1178 if (!strcmp(buf, "480")) {
bellarda594cfb2005-11-06 16:13:29 +00001179 speed = USB_SPEED_HIGH;
David Ahern27911042010-04-24 10:26:22 -06001180 } else if (!strcmp(buf, "1.5")) {
bellarda594cfb2005-11-06 16:13:29 +00001181 speed = USB_SPEED_LOW;
David Ahern27911042010-04-24 10:26:22 -06001182 } else {
bellarda594cfb2005-11-06 16:13:29 +00001183 speed = USB_SPEED_FULL;
David Ahern27911042010-04-24 10:26:22 -06001184 }
bellarda594cfb2005-11-06 16:13:29 +00001185 product_name[0] = '\0';
1186 class_id = 0xff;
1187 device_count++;
1188 product_id = 0;
1189 vendor_id = 0;
1190 } else if (line[0] == 'P' && line[1] == ':') {
David Ahern27911042010-04-24 10:26:22 -06001191 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001192 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001193 }
bellarda594cfb2005-11-06 16:13:29 +00001194 vendor_id = strtoul(buf, NULL, 16);
David Ahern27911042010-04-24 10:26:22 -06001195 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001196 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001197 }
bellarda594cfb2005-11-06 16:13:29 +00001198 product_id = strtoul(buf, NULL, 16);
1199 } else if (line[0] == 'S' && line[1] == ':') {
David Ahern27911042010-04-24 10:26:22 -06001200 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001201 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001202 }
bellarda594cfb2005-11-06 16:13:29 +00001203 pstrcpy(product_name, sizeof(product_name), buf);
1204 } else if (line[0] == 'D' && line[1] == ':') {
David Ahern27911042010-04-24 10:26:22 -06001205 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001206 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001207 }
bellarda594cfb2005-11-06 16:13:29 +00001208 class_id = strtoul(buf, NULL, 16);
1209 }
1210 fail: ;
1211 }
pbrook38ca0f62006-03-11 18:03:38 +00001212 if (device_count && (vendor_id || product_id)) {
1213 /* Add the last device. */
ths5fafdf22007-09-16 21:08:06 +00001214 ret = func(opaque, bus_num, addr, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001215 product_id, product_name, speed);
1216 }
1217 the_end:
David Ahern27911042010-04-24 10:26:22 -06001218 if (f) {
aliguori0f431522008-10-07 20:06:37 +00001219 fclose(f);
David Ahern27911042010-04-24 10:26:22 -06001220 }
aliguori0f431522008-10-07 20:06:37 +00001221 return ret;
1222}
1223
1224/*
1225 * Read sys file-system device file
1226 *
1227 * @line address of buffer to put file contents in
1228 * @line_size size of line
1229 * @device_file path to device file (printf format string)
1230 * @device_name device being opened (inserted into device_file)
1231 *
1232 * @return 0 failed, 1 succeeded ('line' contains data)
1233 */
David Ahern27911042010-04-24 10:26:22 -06001234static int usb_host_read_file(char *line, size_t line_size,
1235 const char *device_file, const char *device_name)
aliguori0f431522008-10-07 20:06:37 +00001236{
1237 FILE *f;
1238 int ret = 0;
1239 char filename[PATH_MAX];
1240
blueswir1b4e237a2008-12-28 15:45:20 +00001241 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1242 device_file);
aliguori0f431522008-10-07 20:06:37 +00001243 f = fopen(filename, "r");
1244 if (f) {
Kirill A. Shutemov9f99cee2010-01-20 00:56:17 +01001245 ret = fgets(line, line_size, f) != NULL;
aliguori0f431522008-10-07 20:06:37 +00001246 fclose(f);
aliguori0f431522008-10-07 20:06:37 +00001247 }
1248
1249 return ret;
1250}
1251
1252/*
1253 * Use /sys/bus/usb/devices/ directory to determine host's USB
1254 * devices.
1255 *
1256 * This code is based on Robert Schiele's original patches posted to
1257 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1258 */
1259static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1260{
Blue Swirl660f11b2009-07-31 21:16:51 +00001261 DIR *dir = NULL;
aliguori0f431522008-10-07 20:06:37 +00001262 char line[1024];
1263 int bus_num, addr, speed, class_id, product_id, vendor_id;
1264 int ret = 0;
1265 char product_name[512];
1266 struct dirent *de;
1267
1268 dir = opendir(USBSYSBUS_PATH "/devices");
1269 if (!dir) {
1270 perror("husb: cannot open devices directory");
1271 goto the_end;
1272 }
1273
1274 while ((de = readdir(dir))) {
1275 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1276 char *tmpstr = de->d_name;
David Ahern27911042010-04-24 10:26:22 -06001277 if (!strncmp(de->d_name, "usb", 3)) {
aliguori0f431522008-10-07 20:06:37 +00001278 tmpstr += 3;
David Ahern27911042010-04-24 10:26:22 -06001279 }
aliguori0f431522008-10-07 20:06:37 +00001280 bus_num = atoi(tmpstr);
1281
David Ahern27911042010-04-24 10:26:22 -06001282 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001283 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001284 }
1285 if (sscanf(line, "%d", &addr) != 1) {
aliguori0f431522008-10-07 20:06:37 +00001286 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001287 }
blueswir1b4e237a2008-12-28 15:45:20 +00001288 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
David Ahern27911042010-04-24 10:26:22 -06001289 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001290 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001291 }
1292 if (sscanf(line, "%x", &class_id) != 1) {
aliguori0f431522008-10-07 20:06:37 +00001293 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001294 }
aliguori0f431522008-10-07 20:06:37 +00001295
David Ahern27911042010-04-24 10:26:22 -06001296 if (!usb_host_read_file(line, sizeof(line), "idVendor",
1297 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001298 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001299 }
1300 if (sscanf(line, "%x", &vendor_id) != 1) {
aliguori0f431522008-10-07 20:06:37 +00001301 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001302 }
blueswir1b4e237a2008-12-28 15:45:20 +00001303 if (!usb_host_read_file(line, sizeof(line), "idProduct",
David Ahern27911042010-04-24 10:26:22 -06001304 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001305 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001306 }
1307 if (sscanf(line, "%x", &product_id) != 1) {
aliguori0f431522008-10-07 20:06:37 +00001308 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001309 }
blueswir1b4e237a2008-12-28 15:45:20 +00001310 if (!usb_host_read_file(line, sizeof(line), "product",
1311 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001312 *product_name = 0;
1313 } else {
David Ahern27911042010-04-24 10:26:22 -06001314 if (strlen(line) > 0) {
aliguori0f431522008-10-07 20:06:37 +00001315 line[strlen(line) - 1] = '\0';
David Ahern27911042010-04-24 10:26:22 -06001316 }
aliguori0f431522008-10-07 20:06:37 +00001317 pstrcpy(product_name, sizeof(product_name), line);
1318 }
1319
David Ahern27911042010-04-24 10:26:22 -06001320 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001321 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001322 }
1323 if (!strcmp(line, "480\n")) {
aliguori0f431522008-10-07 20:06:37 +00001324 speed = USB_SPEED_HIGH;
David Ahern27911042010-04-24 10:26:22 -06001325 } else if (!strcmp(line, "1.5\n")) {
aliguori0f431522008-10-07 20:06:37 +00001326 speed = USB_SPEED_LOW;
David Ahern27911042010-04-24 10:26:22 -06001327 } else {
aliguori0f431522008-10-07 20:06:37 +00001328 speed = USB_SPEED_FULL;
David Ahern27911042010-04-24 10:26:22 -06001329 }
aliguori0f431522008-10-07 20:06:37 +00001330
1331 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1332 product_id, product_name, speed);
David Ahern27911042010-04-24 10:26:22 -06001333 if (ret) {
aliguori0f431522008-10-07 20:06:37 +00001334 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001335 }
aliguori0f431522008-10-07 20:06:37 +00001336 }
1337 }
1338 the_end:
David Ahern27911042010-04-24 10:26:22 -06001339 if (dir) {
aliguori0f431522008-10-07 20:06:37 +00001340 closedir(dir);
David Ahern27911042010-04-24 10:26:22 -06001341 }
aliguori0f431522008-10-07 20:06:37 +00001342 return ret;
1343}
1344
1345/*
1346 * Determine how to access the host's USB devices and call the
1347 * specific support function.
1348 */
1349static int usb_host_scan(void *opaque, USBScanFunc *func)
1350{
aliguori376253e2009-03-05 23:01:23 +00001351 Monitor *mon = cur_mon;
Blue Swirl660f11b2009-07-31 21:16:51 +00001352 FILE *f = NULL;
1353 DIR *dir = NULL;
aliguori0f431522008-10-07 20:06:37 +00001354 int ret = 0;
aliguori0f431522008-10-07 20:06:37 +00001355 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1356 char devpath[PATH_MAX];
1357
1358 /* only check the host once */
1359 if (!usb_fs_type) {
Mark McLoughlin55496242009-07-03 09:28:02 +01001360 dir = opendir(USBSYSBUS_PATH "/devices");
1361 if (dir) {
1362 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1363 strcpy(devpath, USBDEVBUS_PATH);
1364 usb_fs_type = USB_FS_SYS;
1365 closedir(dir);
malcd0f2c4c2010-02-07 02:03:50 +03001366 DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
Mark McLoughlin55496242009-07-03 09:28:02 +01001367 goto found_devices;
1368 }
aliguori0f431522008-10-07 20:06:37 +00001369 f = fopen(USBPROCBUS_PATH "/devices", "r");
1370 if (f) {
1371 /* devices found in /proc/bus/usb/ */
1372 strcpy(devpath, USBPROCBUS_PATH);
1373 usb_fs_type = USB_FS_PROC;
1374 fclose(f);
malcd0f2c4c2010-02-07 02:03:50 +03001375 DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001376 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001377 }
1378 /* try additional methods if an access method hasn't been found yet */
1379 f = fopen(USBDEVBUS_PATH "/devices", "r");
aliguorif16a0db2008-10-21 16:34:20 +00001380 if (f) {
aliguori0f431522008-10-07 20:06:37 +00001381 /* devices found in /dev/bus/usb/ */
1382 strcpy(devpath, USBDEVBUS_PATH);
1383 usb_fs_type = USB_FS_DEV;
1384 fclose(f);
malcd0f2c4c2010-02-07 02:03:50 +03001385 DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001386 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001387 }
aliguorif16a0db2008-10-21 16:34:20 +00001388 found_devices:
aliguori22babeb2008-10-21 16:27:28 +00001389 if (!usb_fs_type) {
David Ahern27911042010-04-24 10:26:22 -06001390 if (mon) {
Gerd Hoffmanneba6fe82009-12-15 11:43:02 +01001391 monitor_printf(mon, "husb: unable to access USB devices\n");
David Ahern27911042010-04-24 10:26:22 -06001392 }
aliguorif16a0db2008-10-21 16:34:20 +00001393 return -ENOENT;
aliguori0f431522008-10-07 20:06:37 +00001394 }
1395
1396 /* the module setting (used later for opening devices) */
1397 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
aliguori1eec6142009-02-05 22:06:18 +00001398 strcpy(usb_host_device_path, devpath);
David Ahern27911042010-04-24 10:26:22 -06001399 if (mon) {
Gerd Hoffmanneba6fe82009-12-15 11:43:02 +01001400 monitor_printf(mon, "husb: using %s file-system with %s\n",
1401 fs_type[usb_fs_type], usb_host_device_path);
David Ahern27911042010-04-24 10:26:22 -06001402 }
aliguori0f431522008-10-07 20:06:37 +00001403 }
1404
1405 switch (usb_fs_type) {
1406 case USB_FS_PROC:
1407 case USB_FS_DEV:
1408 ret = usb_host_scan_dev(opaque, func);
1409 break;
1410 case USB_FS_SYS:
1411 ret = usb_host_scan_sys(opaque, func);
1412 break;
aliguorif16a0db2008-10-21 16:34:20 +00001413 default:
1414 ret = -EINVAL;
1415 break;
aliguori0f431522008-10-07 20:06:37 +00001416 }
bellarda594cfb2005-11-06 16:13:29 +00001417 return ret;
1418}
1419
aliguori4b096fc2008-08-21 19:28:55 +00001420static QEMUTimer *usb_auto_timer;
aliguori4b096fc2008-08-21 19:28:55 +00001421
1422static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001423 int class_id, int vendor_id, int product_id,
1424 const char *product_name, int speed)
aliguori4b096fc2008-08-21 19:28:55 +00001425{
1426 struct USBAutoFilter *f;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001427 struct USBHostDevice *s;
aliguori4b096fc2008-08-21 19:28:55 +00001428
1429 /* Ignore hubs */
1430 if (class_id == 9)
1431 return 0;
1432
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001433 QTAILQ_FOREACH(s, &hostdevs, next) {
1434 f = &s->match;
1435
David Ahern27911042010-04-24 10:26:22 -06001436 if (f->bus_num > 0 && f->bus_num != bus_num) {
aliguori4b096fc2008-08-21 19:28:55 +00001437 continue;
David Ahern27911042010-04-24 10:26:22 -06001438 }
1439 if (f->addr > 0 && f->addr != addr) {
aliguori4b096fc2008-08-21 19:28:55 +00001440 continue;
David Ahern27911042010-04-24 10:26:22 -06001441 }
aliguori4b096fc2008-08-21 19:28:55 +00001442
David Ahern27911042010-04-24 10:26:22 -06001443 if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
aliguori4b096fc2008-08-21 19:28:55 +00001444 continue;
David Ahern27911042010-04-24 10:26:22 -06001445 }
aliguori4b096fc2008-08-21 19:28:55 +00001446
David Ahern27911042010-04-24 10:26:22 -06001447 if (f->product_id > 0 && f->product_id != product_id) {
aliguori4b096fc2008-08-21 19:28:55 +00001448 continue;
David Ahern27911042010-04-24 10:26:22 -06001449 }
aliguori4b096fc2008-08-21 19:28:55 +00001450 /* We got a match */
1451
Markus Armbruster33e66b82009-10-07 01:15:57 +02001452 /* Already attached ? */
David Ahern27911042010-04-24 10:26:22 -06001453 if (s->fd != -1) {
aliguori4b096fc2008-08-21 19:28:55 +00001454 return 0;
David Ahern27911042010-04-24 10:26:22 -06001455 }
malcd0f2c4c2010-02-07 02:03:50 +03001456 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
aliguori4b096fc2008-08-21 19:28:55 +00001457
David Ahern27911042010-04-24 10:26:22 -06001458 usb_host_open(s, bus_num, addr, product_name);
aliguori4b096fc2008-08-21 19:28:55 +00001459 }
1460
1461 return 0;
1462}
1463
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001464static void usb_host_auto_check(void *unused)
aliguori4b096fc2008-08-21 19:28:55 +00001465{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001466 struct USBHostDevice *s;
1467 int unconnected = 0;
1468
aliguori4b096fc2008-08-21 19:28:55 +00001469 usb_host_scan(NULL, usb_host_auto_scan);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001470
1471 QTAILQ_FOREACH(s, &hostdevs, next) {
David Ahern27911042010-04-24 10:26:22 -06001472 if (s->fd == -1) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001473 unconnected++;
David Ahern27911042010-04-24 10:26:22 -06001474 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001475 }
1476
1477 if (unconnected == 0) {
1478 /* nothing to watch */
David Ahern27911042010-04-24 10:26:22 -06001479 if (usb_auto_timer) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001480 qemu_del_timer(usb_auto_timer);
David Ahern27911042010-04-24 10:26:22 -06001481 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001482 return;
1483 }
1484
1485 if (!usb_auto_timer) {
1486 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_check, NULL);
David Ahern27911042010-04-24 10:26:22 -06001487 if (!usb_auto_timer) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001488 return;
David Ahern27911042010-04-24 10:26:22 -06001489 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001490 }
aliguori4b096fc2008-08-21 19:28:55 +00001491 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1492}
1493
1494/*
aliguori5d0c5752008-09-14 01:07:41 +00001495 * Autoconnect filter
1496 * Format:
1497 * auto:bus:dev[:vid:pid]
1498 * auto:bus.dev[:vid:pid]
1499 *
1500 * bus - bus number (dec, * means any)
1501 * dev - device number (dec, * means any)
1502 * vid - vendor id (hex, * means any)
1503 * pid - product id (hex, * means any)
1504 *
1505 * See 'lsusb' output.
aliguori4b096fc2008-08-21 19:28:55 +00001506 */
aliguori5d0c5752008-09-14 01:07:41 +00001507static int parse_filter(const char *spec, struct USBAutoFilter *f)
aliguori4b096fc2008-08-21 19:28:55 +00001508{
aliguori5d0c5752008-09-14 01:07:41 +00001509 enum { BUS, DEV, VID, PID, DONE };
1510 const char *p = spec;
1511 int i;
1512
Markus Armbruster0745eb12009-11-27 13:05:53 +01001513 f->bus_num = 0;
1514 f->addr = 0;
1515 f->vendor_id = 0;
1516 f->product_id = 0;
aliguori5d0c5752008-09-14 01:07:41 +00001517
1518 for (i = BUS; i < DONE; i++) {
David Ahern27911042010-04-24 10:26:22 -06001519 p = strpbrk(p, ":.");
1520 if (!p) {
1521 break;
1522 }
aliguori5d0c5752008-09-14 01:07:41 +00001523 p++;
aliguori5d0c5752008-09-14 01:07:41 +00001524
David Ahern27911042010-04-24 10:26:22 -06001525 if (*p == '*') {
1526 continue;
1527 }
aliguori5d0c5752008-09-14 01:07:41 +00001528 switch(i) {
1529 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1530 case DEV: f->addr = strtol(p, NULL, 10); break;
1531 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1532 case PID: f->product_id = strtol(p, NULL, 16); break;
1533 }
aliguori4b096fc2008-08-21 19:28:55 +00001534 }
1535
aliguori5d0c5752008-09-14 01:07:41 +00001536 if (i < DEV) {
1537 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1538 return -1;
1539 }
1540
1541 return 0;
1542}
1543
bellarda594cfb2005-11-06 16:13:29 +00001544/**********************/
1545/* USB host device info */
bellardbb36d472005-11-05 14:22:28 +00001546
bellarda594cfb2005-11-06 16:13:29 +00001547struct usb_class_info {
1548 int class;
1549 const char *class_name;
1550};
1551
1552static const struct usb_class_info usb_class_info[] = {
1553 { USB_CLASS_AUDIO, "Audio"},
1554 { USB_CLASS_COMM, "Communication"},
1555 { USB_CLASS_HID, "HID"},
1556 { USB_CLASS_HUB, "Hub" },
1557 { USB_CLASS_PHYSICAL, "Physical" },
1558 { USB_CLASS_PRINTER, "Printer" },
1559 { USB_CLASS_MASS_STORAGE, "Storage" },
1560 { USB_CLASS_CDC_DATA, "Data" },
1561 { USB_CLASS_APP_SPEC, "Application Specific" },
1562 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1563 { USB_CLASS_STILL_IMAGE, "Still Image" },
balrogb9dc0332007-10-04 22:47:34 +00001564 { USB_CLASS_CSCID, "Smart Card" },
bellarda594cfb2005-11-06 16:13:29 +00001565 { USB_CLASS_CONTENT_SEC, "Content Security" },
1566 { -1, NULL }
1567};
1568
1569static const char *usb_class_str(uint8_t class)
1570{
1571 const struct usb_class_info *p;
1572 for(p = usb_class_info; p->class != -1; p++) {
David Ahern27911042010-04-24 10:26:22 -06001573 if (p->class == class) {
bellardbb36d472005-11-05 14:22:28 +00001574 break;
David Ahern27911042010-04-24 10:26:22 -06001575 }
bellardbb36d472005-11-05 14:22:28 +00001576 }
bellarda594cfb2005-11-06 16:13:29 +00001577 return p->class_name;
bellardbb36d472005-11-05 14:22:28 +00001578}
1579
Blue Swirl179da8a2009-09-07 19:00:18 +00001580static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
pbrook9596ebb2007-11-18 01:44:38 +00001581 int vendor_id, int product_id,
1582 const char *product_name,
1583 int speed)
bellardbb36d472005-11-05 14:22:28 +00001584{
bellarda594cfb2005-11-06 16:13:29 +00001585 const char *class_str, *speed_str;
1586
1587 switch(speed) {
ths5fafdf22007-09-16 21:08:06 +00001588 case USB_SPEED_LOW:
1589 speed_str = "1.5";
bellarda594cfb2005-11-06 16:13:29 +00001590 break;
ths5fafdf22007-09-16 21:08:06 +00001591 case USB_SPEED_FULL:
1592 speed_str = "12";
bellarda594cfb2005-11-06 16:13:29 +00001593 break;
ths5fafdf22007-09-16 21:08:06 +00001594 case USB_SPEED_HIGH:
1595 speed_str = "480";
bellarda594cfb2005-11-06 16:13:29 +00001596 break;
1597 default:
ths5fafdf22007-09-16 21:08:06 +00001598 speed_str = "?";
bellarda594cfb2005-11-06 16:13:29 +00001599 break;
bellardbb36d472005-11-05 14:22:28 +00001600 }
bellarda594cfb2005-11-06 16:13:29 +00001601
aliguori376253e2009-03-05 23:01:23 +00001602 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
bellarda594cfb2005-11-06 16:13:29 +00001603 bus_num, addr, speed_str);
1604 class_str = usb_class_str(class_id);
David Ahern27911042010-04-24 10:26:22 -06001605 if (class_str) {
aliguori376253e2009-03-05 23:01:23 +00001606 monitor_printf(mon, " %s:", class_str);
David Ahern27911042010-04-24 10:26:22 -06001607 } else {
aliguori376253e2009-03-05 23:01:23 +00001608 monitor_printf(mon, " Class %02x:", class_id);
David Ahern27911042010-04-24 10:26:22 -06001609 }
aliguori376253e2009-03-05 23:01:23 +00001610 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
David Ahern27911042010-04-24 10:26:22 -06001611 if (product_name[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001612 monitor_printf(mon, ", %s", product_name);
David Ahern27911042010-04-24 10:26:22 -06001613 }
aliguori376253e2009-03-05 23:01:23 +00001614 monitor_printf(mon, "\n");
bellarda594cfb2005-11-06 16:13:29 +00001615}
1616
ths5fafdf22007-09-16 21:08:06 +00001617static int usb_host_info_device(void *opaque, int bus_num, int addr,
bellarda594cfb2005-11-06 16:13:29 +00001618 int class_id,
ths5fafdf22007-09-16 21:08:06 +00001619 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +00001620 const char *product_name,
1621 int speed)
1622{
Blue Swirl179da8a2009-09-07 19:00:18 +00001623 Monitor *mon = opaque;
1624
1625 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
bellarda594cfb2005-11-06 16:13:29 +00001626 product_name, speed);
1627 return 0;
1628}
1629
aliguoriac4ffb52008-09-22 15:04:31 +00001630static void dec2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001631{
David Ahern27911042010-04-24 10:26:22 -06001632 if (val == 0) {
aliguoriac4ffb52008-09-22 15:04:31 +00001633 snprintf(str, size, "*");
David Ahern27911042010-04-24 10:26:22 -06001634 } else {
1635 snprintf(str, size, "%d", val);
1636 }
aliguori5d0c5752008-09-14 01:07:41 +00001637}
1638
aliguoriac4ffb52008-09-22 15:04:31 +00001639static void hex2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001640{
David Ahern27911042010-04-24 10:26:22 -06001641 if (val == 0) {
aliguoriac4ffb52008-09-22 15:04:31 +00001642 snprintf(str, size, "*");
David Ahern27911042010-04-24 10:26:22 -06001643 } else {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001644 snprintf(str, size, "%04x", val);
David Ahern27911042010-04-24 10:26:22 -06001645 }
aliguori5d0c5752008-09-14 01:07:41 +00001646}
1647
aliguori376253e2009-03-05 23:01:23 +00001648void usb_host_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00001649{
aliguori5d0c5752008-09-14 01:07:41 +00001650 struct USBAutoFilter *f;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001651 struct USBHostDevice *s;
aliguori5d0c5752008-09-14 01:07:41 +00001652
Blue Swirl179da8a2009-09-07 19:00:18 +00001653 usb_host_scan(mon, usb_host_info_device);
aliguori5d0c5752008-09-14 01:07:41 +00001654
David Ahern27911042010-04-24 10:26:22 -06001655 if (QTAILQ_EMPTY(&hostdevs)) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001656 return;
David Ahern27911042010-04-24 10:26:22 -06001657 }
1658
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001659 monitor_printf(mon, " Auto filters:\n");
1660 QTAILQ_FOREACH(s, &hostdevs, next) {
aliguori5d0c5752008-09-14 01:07:41 +00001661 char bus[10], addr[10], vid[10], pid[10];
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001662 f = &s->match;
aliguoriac4ffb52008-09-22 15:04:31 +00001663 dec2str(f->bus_num, bus, sizeof(bus));
1664 dec2str(f->addr, addr, sizeof(addr));
1665 hex2str(f->vendor_id, vid, sizeof(vid));
1666 hex2str(f->product_id, pid, sizeof(pid));
aliguori376253e2009-03-05 23:01:23 +00001667 monitor_printf(mon, " Device %s.%s ID %s:%s\n",
1668 bus, addr, vid, pid);
aliguori5d0c5752008-09-14 01:07:41 +00001669 }
bellardbb36d472005-11-05 14:22:28 +00001670}