blob: ccf70734e3f3b3cf80d31b9bda4cfb2a6e790158 [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"
Shahar Havivib373a632010-06-16 15:16:11 +030036#include "sysemu.h"
bellardbb36d472005-11-05 14:22:28 +000037
bellardbb36d472005-11-05 14:22:28 +000038#include <dirent.h>
39#include <sys/ioctl.h>
balrogb9dc0332007-10-04 22:47:34 +000040#include <signal.h>
bellardbb36d472005-11-05 14:22:28 +000041
aliguori446ab122008-09-14 01:06:09 +000042#include <linux/usbdevice_fs.h>
43#include <linux/version.h>
44#include "hw/usb.h"
bellardbb36d472005-11-05 14:22:28 +000045
blueswir1d9cf1572008-09-15 14:57:11 +000046/* We redefine it to avoid version problems */
47struct usb_ctrltransfer {
48 uint8_t bRequestType;
49 uint8_t bRequest;
50 uint16_t wValue;
51 uint16_t wIndex;
52 uint16_t wLength;
53 uint32_t timeout;
54 void *data;
55};
56
57struct usb_ctrlrequest {
58 uint8_t bRequestType;
59 uint8_t bRequest;
60 uint16_t wValue;
61 uint16_t wIndex;
62 uint16_t wLength;
63};
64
Hans de Goede0f5160d2010-11-10 10:06:23 +010065typedef int USBScanFunc(void *opaque, int bus_num, int addr, int devpath,
66 int class_id, int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +000067 const char *product_name, int speed);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +010068
Markus Armbruster0745eb12009-11-27 13:05:53 +010069//#define DEBUG
aliguori64838172008-08-21 19:31:10 +000070
71#ifdef DEBUG
malcd0f2c4c2010-02-07 02:03:50 +030072#define DPRINTF printf
aliguori64838172008-08-21 19:31:10 +000073#else
malcd0f2c4c2010-02-07 02:03:50 +030074#define DPRINTF(...)
aliguori64838172008-08-21 19:31:10 +000075#endif
bellardbb36d472005-11-05 14:22:28 +000076
blueswir15be8e1f2008-10-25 11:47:20 +000077#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
78
aliguori0f431522008-10-07 20:06:37 +000079#define USBPROCBUS_PATH "/proc/bus/usb"
bellard1f6e24e2006-06-26 21:00:51 +000080#define PRODUCT_NAME_SZ 32
balrogb9dc0332007-10-04 22:47:34 +000081#define MAX_ENDPOINTS 16
aliguori0f431522008-10-07 20:06:37 +000082#define USBDEVBUS_PATH "/dev/bus/usb"
83#define USBSYSBUS_PATH "/sys/bus/usb"
84
85static char *usb_host_device_path;
86
87#define USB_FS_NONE 0
88#define USB_FS_PROC 1
89#define USB_FS_DEV 2
90#define USB_FS_SYS 3
91
92static int usb_fs_type;
bellardbb36d472005-11-05 14:22:28 +000093
balrogb9dc0332007-10-04 22:47:34 +000094/* endpoint association data */
95struct endp_data {
96 uint8_t type;
aliguori64838172008-08-21 19:31:10 +000097 uint8_t halted;
balrogb9dc0332007-10-04 22:47:34 +000098};
99
aliguori446ab122008-09-14 01:06:09 +0000100enum {
101 CTRL_STATE_IDLE = 0,
102 CTRL_STATE_SETUP,
103 CTRL_STATE_DATA,
104 CTRL_STATE_ACK
105};
106
107/*
108 * Control transfer state.
David Ahern27911042010-04-24 10:26:22 -0600109 * Note that 'buffer' _must_ follow 'req' field because
aliguori446ab122008-09-14 01:06:09 +0000110 * we need contigious buffer when we submit control URB.
David Ahern27911042010-04-24 10:26:22 -0600111 */
aliguori446ab122008-09-14 01:06:09 +0000112struct ctrl_struct {
113 uint16_t len;
114 uint16_t offset;
115 uint8_t state;
116 struct usb_ctrlrequest req;
Christian Krausefd7a4462010-01-24 17:34:52 +0100117 uint8_t buffer[8192];
aliguori446ab122008-09-14 01:06:09 +0000118};
119
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100120struct USBAutoFilter {
121 uint32_t bus_num;
122 uint32_t addr;
123 uint32_t vendor_id;
124 uint32_t product_id;
125};
126
bellardbb36d472005-11-05 14:22:28 +0000127typedef struct USBHostDevice {
128 USBDevice dev;
aliguori64838172008-08-21 19:31:10 +0000129 int fd;
130
131 uint8_t descr[1024];
132 int descr_len;
133 int configuration;
aliguori446ab122008-09-14 01:06:09 +0000134 int ninterfaces;
aliguori24772c12008-08-21 19:31:52 +0000135 int closing;
Shahar Havivib373a632010-06-16 15:16:11 +0300136 Notifier exit;
aliguori64838172008-08-21 19:31:10 +0000137
aliguori446ab122008-09-14 01:06:09 +0000138 struct ctrl_struct ctrl;
balrogb9dc0332007-10-04 22:47:34 +0000139 struct endp_data endp_table[MAX_ENDPOINTS];
aliguori4b096fc2008-08-21 19:28:55 +0000140
aliguori4b096fc2008-08-21 19:28:55 +0000141 /* Host side address */
142 int bus_num;
143 int addr;
Hans de Goede0f5160d2010-11-10 10:06:23 +0100144 int devpath;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100145 struct USBAutoFilter match;
aliguori4b096fc2008-08-21 19:28:55 +0000146
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100147 QTAILQ_ENTRY(USBHostDevice) next;
bellardbb36d472005-11-05 14:22:28 +0000148} USBHostDevice;
149
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100150static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
151
152static int usb_host_close(USBHostDevice *dev);
153static int parse_filter(const char *spec, struct USBAutoFilter *f);
154static void usb_host_auto_check(void *unused);
Hans de Goede2cc59d82010-11-10 10:06:25 +0100155static int usb_host_read_file(char *line, size_t line_size,
156 const char *device_file, const char *device_name);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100157
aliguori64838172008-08-21 19:31:10 +0000158static int is_isoc(USBHostDevice *s, int ep)
159{
160 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
161}
162
163static int is_halted(USBHostDevice *s, int ep)
164{
165 return s->endp_table[ep - 1].halted;
166}
167
168static void clear_halt(USBHostDevice *s, int ep)
169{
170 s->endp_table[ep - 1].halted = 0;
171}
172
173static void set_halt(USBHostDevice *s, int ep)
174{
175 s->endp_table[ep - 1].halted = 1;
176}
177
David Ahern27911042010-04-24 10:26:22 -0600178/*
aliguori64838172008-08-21 19:31:10 +0000179 * Async URB state.
180 * We always allocate one isoc descriptor even for bulk transfers
David Ahern27911042010-04-24 10:26:22 -0600181 * to simplify allocation and casts.
aliguori64838172008-08-21 19:31:10 +0000182 */
183typedef struct AsyncURB
balrogb9dc0332007-10-04 22:47:34 +0000184{
aliguori64838172008-08-21 19:31:10 +0000185 struct usbdevfs_urb urb;
186 struct usbdevfs_iso_packet_desc isocpd;
187
188 USBPacket *packet;
189 USBHostDevice *hdev;
190} AsyncURB;
191
192static AsyncURB *async_alloc(void)
193{
194 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
balrogb9dc0332007-10-04 22:47:34 +0000195}
196
aliguori64838172008-08-21 19:31:10 +0000197static void async_free(AsyncURB *aurb)
balrogb9dc0332007-10-04 22:47:34 +0000198{
aliguori64838172008-08-21 19:31:10 +0000199 qemu_free(aurb);
200}
balrogb9dc0332007-10-04 22:47:34 +0000201
aliguori446ab122008-09-14 01:06:09 +0000202static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
203{
204 switch(s->ctrl.state) {
205 case CTRL_STATE_SETUP:
206 if (p->len < s->ctrl.len)
207 s->ctrl.len = p->len;
208 s->ctrl.state = CTRL_STATE_DATA;
209 p->len = 8;
210 break;
211
212 case CTRL_STATE_ACK:
213 s->ctrl.state = CTRL_STATE_IDLE;
214 p->len = 0;
215 break;
216
217 default:
218 break;
219 }
220}
221
aliguori64838172008-08-21 19:31:10 +0000222static void async_complete(void *opaque)
223{
224 USBHostDevice *s = opaque;
225 AsyncURB *aurb;
balrogb9dc0332007-10-04 22:47:34 +0000226
aliguori64838172008-08-21 19:31:10 +0000227 while (1) {
David Ahern27911042010-04-24 10:26:22 -0600228 USBPacket *p;
aliguori64838172008-08-21 19:31:10 +0000229
David Ahern27911042010-04-24 10:26:22 -0600230 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
aliguori64838172008-08-21 19:31:10 +0000231 if (r < 0) {
David Ahern27911042010-04-24 10:26:22 -0600232 if (errno == EAGAIN) {
aliguori64838172008-08-21 19:31:10 +0000233 return;
David Ahern27911042010-04-24 10:26:22 -0600234 }
aliguori24772c12008-08-21 19:31:52 +0000235 if (errno == ENODEV && !s->closing) {
David Ahern27911042010-04-24 10:26:22 -0600236 printf("husb: device %d.%d disconnected\n",
237 s->bus_num, s->addr);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100238 usb_host_close(s);
239 usb_host_auto_check(NULL);
aliguori64838172008-08-21 19:31:10 +0000240 return;
241 }
242
malcd0f2c4c2010-02-07 02:03:50 +0300243 DPRINTF("husb: async. reap urb failed errno %d\n", errno);
aliguori64838172008-08-21 19:31:10 +0000244 return;
balrogb9dc0332007-10-04 22:47:34 +0000245 }
aliguori64838172008-08-21 19:31:10 +0000246
247 p = aurb->packet;
248
David Ahern27911042010-04-24 10:26:22 -0600249 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
aliguori64838172008-08-21 19:31:10 +0000250 aurb, aurb->urb.status, aurb->urb.actual_length);
251
David Ahern27911042010-04-24 10:26:22 -0600252 if (p) {
aliguori64838172008-08-21 19:31:10 +0000253 switch (aurb->urb.status) {
254 case 0:
255 p->len = aurb->urb.actual_length;
David Ahern27911042010-04-24 10:26:22 -0600256 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
aliguori446ab122008-09-14 01:06:09 +0000257 async_complete_ctrl(s, p);
David Ahern27911042010-04-24 10:26:22 -0600258 }
aliguori64838172008-08-21 19:31:10 +0000259 break;
260
261 case -EPIPE:
262 set_halt(s, p->devep);
David Ahern27911042010-04-24 10:26:22 -0600263 p->len = USB_RET_STALL;
264 break;
Paul Bolledcc7e252009-10-13 13:40:08 +0200265
aliguori64838172008-08-21 19:31:10 +0000266 default:
267 p->len = USB_RET_NAK;
268 break;
269 }
270
271 usb_packet_complete(p);
David Ahern27911042010-04-24 10:26:22 -0600272 }
aliguori64838172008-08-21 19:31:10 +0000273
274 async_free(aurb);
balrogb9dc0332007-10-04 22:47:34 +0000275 }
balrogb9dc0332007-10-04 22:47:34 +0000276}
277
aliguori64838172008-08-21 19:31:10 +0000278static void async_cancel(USBPacket *unused, void *opaque)
balrogb9dc0332007-10-04 22:47:34 +0000279{
aliguori64838172008-08-21 19:31:10 +0000280 AsyncURB *aurb = opaque;
281 USBHostDevice *s = aurb->hdev;
balrogb9dc0332007-10-04 22:47:34 +0000282
malcd0f2c4c2010-02-07 02:03:50 +0300283 DPRINTF("husb: async cancel. aurb %p\n", aurb);
balrogb9dc0332007-10-04 22:47:34 +0000284
aliguori64838172008-08-21 19:31:10 +0000285 /* Mark it as dead (see async_complete above) */
286 aurb->packet = NULL;
287
288 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
289 if (r < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300290 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
balrogb9dc0332007-10-04 22:47:34 +0000291 }
balrogb9dc0332007-10-04 22:47:34 +0000292}
293
aliguori446ab122008-09-14 01:06:09 +0000294static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
balrogb9dc0332007-10-04 22:47:34 +0000295{
296 int dev_descr_len, config_descr_len;
Blue Swirld4c4e6f2010-04-25 18:23:04 +0000297 int interface, nb_interfaces;
balrogb9dc0332007-10-04 22:47:34 +0000298 int ret, i;
299
300 if (configuration == 0) /* address state - ignore */
301 return 1;
302
malcd0f2c4c2010-02-07 02:03:50 +0300303 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
aliguori446ab122008-09-14 01:06:09 +0000304
balrogb9dc0332007-10-04 22:47:34 +0000305 i = 0;
306 dev_descr_len = dev->descr[0];
David Ahern27911042010-04-24 10:26:22 -0600307 if (dev_descr_len > dev->descr_len) {
balrogb9dc0332007-10-04 22:47:34 +0000308 goto fail;
David Ahern27911042010-04-24 10:26:22 -0600309 }
balrogb9dc0332007-10-04 22:47:34 +0000310
311 i += dev_descr_len;
312 while (i < dev->descr_len) {
David Ahern27911042010-04-24 10:26:22 -0600313 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
314 i, dev->descr_len,
balrogb9dc0332007-10-04 22:47:34 +0000315 dev->descr[i], dev->descr[i+1]);
aliguori64838172008-08-21 19:31:10 +0000316
balrogb9dc0332007-10-04 22:47:34 +0000317 if (dev->descr[i+1] != USB_DT_CONFIG) {
318 i += dev->descr[i];
319 continue;
320 }
321 config_descr_len = dev->descr[i];
322
David Ahern27911042010-04-24 10:26:22 -0600323 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
aliguori1f3870a2008-08-21 19:27:48 +0000324
aliguori446ab122008-09-14 01:06:09 +0000325 if (configuration < 0 || configuration == dev->descr[i + 5]) {
326 configuration = dev->descr[i + 5];
balrogb9dc0332007-10-04 22:47:34 +0000327 break;
aliguori446ab122008-09-14 01:06:09 +0000328 }
balrogb9dc0332007-10-04 22:47:34 +0000329
330 i += config_descr_len;
331 }
332
333 if (i >= dev->descr_len) {
David Ahern27911042010-04-24 10:26:22 -0600334 fprintf(stderr,
335 "husb: update iface failed. no matching configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000336 goto fail;
337 }
338 nb_interfaces = dev->descr[i + 4];
339
340#ifdef USBDEVFS_DISCONNECT
341 /* earlier Linux 2.4 do not support that */
342 {
343 struct usbdevfs_ioctl ctrl;
344 for (interface = 0; interface < nb_interfaces; interface++) {
345 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
346 ctrl.ifno = interface;
347 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
348 if (ret < 0 && errno != ENODATA) {
349 perror("USBDEVFS_DISCONNECT");
350 goto fail;
351 }
352 }
353 }
354#endif
355
356 /* XXX: only grab if all interfaces are free */
357 for (interface = 0; interface < nb_interfaces; interface++) {
358 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
359 if (ret < 0) {
360 if (errno == EBUSY) {
aliguori64838172008-08-21 19:31:10 +0000361 printf("husb: update iface. device already grabbed\n");
balrogb9dc0332007-10-04 22:47:34 +0000362 } else {
aliguori64838172008-08-21 19:31:10 +0000363 perror("husb: failed to claim interface");
balrogb9dc0332007-10-04 22:47:34 +0000364 }
365 fail:
366 return 0;
367 }
368 }
369
aliguori64838172008-08-21 19:31:10 +0000370 printf("husb: %d interfaces claimed for configuration %d\n",
balrogb9dc0332007-10-04 22:47:34 +0000371 nb_interfaces, configuration);
balrogb9dc0332007-10-04 22:47:34 +0000372
aliguori446ab122008-09-14 01:06:09 +0000373 dev->ninterfaces = nb_interfaces;
374 dev->configuration = configuration;
375 return 1;
376}
377
378static int usb_host_release_interfaces(USBHostDevice *s)
379{
380 int ret, i;
381
malcd0f2c4c2010-02-07 02:03:50 +0300382 DPRINTF("husb: releasing interfaces\n");
aliguori446ab122008-09-14 01:06:09 +0000383
384 for (i = 0; i < s->ninterfaces; i++) {
385 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
386 if (ret < 0) {
387 perror("husb: failed to release interface");
388 return 0;
389 }
390 }
391
balrogb9dc0332007-10-04 22:47:34 +0000392 return 1;
393}
394
bellard059809e2006-07-19 18:06:15 +0000395static void usb_host_handle_reset(USBDevice *dev)
bellardbb36d472005-11-05 14:22:28 +0000396{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100397 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
aliguori64838172008-08-21 19:31:10 +0000398
malcd0f2c4c2010-02-07 02:03:50 +0300399 DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr);
aliguori64838172008-08-21 19:31:10 +0000400
bellardbb36d472005-11-05 14:22:28 +0000401 ioctl(s->fd, USBDEVFS_RESET);
aliguori446ab122008-09-14 01:06:09 +0000402
403 usb_host_claim_interfaces(s, s->configuration);
ths5fafdf22007-09-16 21:08:06 +0000404}
bellardbb36d472005-11-05 14:22:28 +0000405
bellard059809e2006-07-19 18:06:15 +0000406static void usb_host_handle_destroy(USBDevice *dev)
407{
408 USBHostDevice *s = (USBHostDevice *)dev;
409
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100410 usb_host_close(s);
411 QTAILQ_REMOVE(&hostdevs, s, next);
Shahar Havivib373a632010-06-16 15:16:11 +0300412 qemu_remove_exit_notifier(&s->exit);
bellard059809e2006-07-19 18:06:15 +0000413}
414
balrogb9dc0332007-10-04 22:47:34 +0000415static int usb_linux_update_endp_table(USBHostDevice *s);
416
aliguori446ab122008-09-14 01:06:09 +0000417static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
bellardbb36d472005-11-05 14:22:28 +0000418{
aliguori64838172008-08-21 19:31:10 +0000419 struct usbdevfs_urb *urb;
aliguori446ab122008-09-14 01:06:09 +0000420 AsyncURB *aurb;
bellardbb36d472005-11-05 14:22:28 +0000421 int ret;
422
aliguori64838172008-08-21 19:31:10 +0000423 aurb = async_alloc();
aliguori64838172008-08-21 19:31:10 +0000424 aurb->hdev = s;
425 aurb->packet = p;
balrogb9dc0332007-10-04 22:47:34 +0000426
aliguori64838172008-08-21 19:31:10 +0000427 urb = &aurb->urb;
428
David Ahern27911042010-04-24 10:26:22 -0600429 if (p->pid == USB_TOKEN_IN) {
430 urb->endpoint = p->devep | 0x80;
431 } else {
432 urb->endpoint = p->devep;
433 }
aliguori64838172008-08-21 19:31:10 +0000434
435 if (is_halted(s, p->devep)) {
David Ahern27911042010-04-24 10:26:22 -0600436 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
aliguori64838172008-08-21 19:31:10 +0000437 if (ret < 0) {
David Ahern27911042010-04-24 10:26:22 -0600438 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
aliguori64838172008-08-21 19:31:10 +0000439 urb->endpoint, errno);
bellardbb36d472005-11-05 14:22:28 +0000440 return USB_RET_NAK;
bellardbb36d472005-11-05 14:22:28 +0000441 }
aliguori64838172008-08-21 19:31:10 +0000442 clear_halt(s, p->devep);
balrog046833e2007-10-31 00:27:50 +0000443 }
444
aliguori64838172008-08-21 19:31:10 +0000445 urb->buffer = p->data;
balrogb9dc0332007-10-04 22:47:34 +0000446 urb->buffer_length = p->len;
aliguori64838172008-08-21 19:31:10 +0000447
448 if (is_isoc(s, p->devep)) {
449 /* Setup ISOC transfer */
450 urb->type = USBDEVFS_URB_TYPE_ISO;
451 urb->flags = USBDEVFS_URB_ISO_ASAP;
452 urb->number_of_packets = 1;
453 urb->iso_frame_desc[0].length = p->len;
balrogb9dc0332007-10-04 22:47:34 +0000454 } else {
aliguori64838172008-08-21 19:31:10 +0000455 /* Setup bulk transfer */
456 urb->type = USBDEVFS_URB_TYPE_BULK;
457 }
458
459 urb->usercontext = s;
460
461 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
462
David Ahern27911042010-04-24 10:26:22 -0600463 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
464 urb->endpoint, p->len, aurb);
aliguori64838172008-08-21 19:31:10 +0000465
466 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300467 DPRINTF("husb: submit failed. errno %d\n", errno);
aliguori64838172008-08-21 19:31:10 +0000468 async_free(aurb);
469
balrogb9dc0332007-10-04 22:47:34 +0000470 switch(errno) {
471 case ETIMEDOUT:
472 return USB_RET_NAK;
473 case EPIPE:
474 default:
475 return USB_RET_STALL;
476 }
477 }
aliguori64838172008-08-21 19:31:10 +0000478
479 usb_defer_packet(p, async_cancel, aurb);
balrogb9dc0332007-10-04 22:47:34 +0000480 return USB_RET_ASYNC;
balrogb9dc0332007-10-04 22:47:34 +0000481}
482
aliguori446ab122008-09-14 01:06:09 +0000483static int ctrl_error(void)
484{
David Ahern27911042010-04-24 10:26:22 -0600485 if (errno == ETIMEDOUT) {
aliguori446ab122008-09-14 01:06:09 +0000486 return USB_RET_NAK;
David Ahern27911042010-04-24 10:26:22 -0600487 } else {
aliguori446ab122008-09-14 01:06:09 +0000488 return USB_RET_STALL;
David Ahern27911042010-04-24 10:26:22 -0600489 }
aliguori446ab122008-09-14 01:06:09 +0000490}
491
492static int usb_host_set_address(USBHostDevice *s, int addr)
493{
malcd0f2c4c2010-02-07 02:03:50 +0300494 DPRINTF("husb: ctrl set addr %u\n", addr);
aliguori446ab122008-09-14 01:06:09 +0000495 s->dev.addr = addr;
496 return 0;
497}
498
499static int usb_host_set_config(USBHostDevice *s, int config)
500{
501 usb_host_release_interfaces(s);
502
503 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
David Ahern27911042010-04-24 10:26:22 -0600504
malcd0f2c4c2010-02-07 02:03:50 +0300505 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
David Ahern27911042010-04-24 10:26:22 -0600506
507 if (ret < 0) {
aliguori446ab122008-09-14 01:06:09 +0000508 return ctrl_error();
David Ahern27911042010-04-24 10:26:22 -0600509 }
aliguori446ab122008-09-14 01:06:09 +0000510 usb_host_claim_interfaces(s, config);
511 return 0;
512}
513
514static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
515{
516 struct usbdevfs_setinterface si;
517 int ret;
518
519 si.interface = iface;
520 si.altsetting = alt;
521 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
aliguori446ab122008-09-14 01:06:09 +0000522
David Ahern27911042010-04-24 10:26:22 -0600523 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
524 iface, alt, ret, errno);
525
526 if (ret < 0) {
527 return ctrl_error();
528 }
aliguori446ab122008-09-14 01:06:09 +0000529 usb_linux_update_endp_table(s);
530 return 0;
531}
532
533static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
534{
535 struct usbdevfs_urb *urb;
536 AsyncURB *aurb;
537 int ret, value, index;
Jim Parisc4c0e232009-08-24 14:56:12 -0400538 int buffer_len;
aliguori446ab122008-09-14 01:06:09 +0000539
David Ahern27911042010-04-24 10:26:22 -0600540 /*
aliguori446ab122008-09-14 01:06:09 +0000541 * Process certain standard device requests.
542 * These are infrequent and are processed synchronously.
543 */
544 value = le16_to_cpu(s->ctrl.req.wValue);
545 index = le16_to_cpu(s->ctrl.req.wIndex);
546
malcd0f2c4c2010-02-07 02:03:50 +0300547 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
David Ahern27911042010-04-24 10:26:22 -0600548 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
549 s->ctrl.len);
aliguori446ab122008-09-14 01:06:09 +0000550
551 if (s->ctrl.req.bRequestType == 0) {
552 switch (s->ctrl.req.bRequest) {
553 case USB_REQ_SET_ADDRESS:
554 return usb_host_set_address(s, value);
555
556 case USB_REQ_SET_CONFIGURATION:
557 return usb_host_set_config(s, value & 0xff);
558 }
559 }
560
561 if (s->ctrl.req.bRequestType == 1 &&
David Ahern27911042010-04-24 10:26:22 -0600562 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE) {
aliguori446ab122008-09-14 01:06:09 +0000563 return usb_host_set_interface(s, index, value);
David Ahern27911042010-04-24 10:26:22 -0600564 }
aliguori446ab122008-09-14 01:06:09 +0000565
566 /* The rest are asynchronous */
567
Jim Parisc4c0e232009-08-24 14:56:12 -0400568 buffer_len = 8 + s->ctrl.len;
569 if (buffer_len > sizeof(s->ctrl.buffer)) {
malcb2e3b6e2009-09-12 03:18:18 +0400570 fprintf(stderr, "husb: ctrl buffer too small (%u > %zu)\n",
571 buffer_len, sizeof(s->ctrl.buffer));
572 return USB_RET_STALL;
Jim Parisc4c0e232009-08-24 14:56:12 -0400573 }
574
aliguori446ab122008-09-14 01:06:09 +0000575 aurb = async_alloc();
aliguori446ab122008-09-14 01:06:09 +0000576 aurb->hdev = s;
577 aurb->packet = p;
578
David Ahern27911042010-04-24 10:26:22 -0600579 /*
aliguori446ab122008-09-14 01:06:09 +0000580 * Setup ctrl transfer.
581 *
582 * s->ctrl is layed out such that data buffer immediately follows
583 * 'req' struct which is exactly what usbdevfs expects.
David Ahern27911042010-04-24 10:26:22 -0600584 */
aliguori446ab122008-09-14 01:06:09 +0000585 urb = &aurb->urb;
586
587 urb->type = USBDEVFS_URB_TYPE_CONTROL;
588 urb->endpoint = p->devep;
589
590 urb->buffer = &s->ctrl.req;
Jim Parisc4c0e232009-08-24 14:56:12 -0400591 urb->buffer_length = buffer_len;
aliguori446ab122008-09-14 01:06:09 +0000592
593 urb->usercontext = s;
594
595 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
596
malcd0f2c4c2010-02-07 02:03:50 +0300597 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
aliguori446ab122008-09-14 01:06:09 +0000598
599 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300600 DPRINTF("husb: submit failed. errno %d\n", errno);
aliguori446ab122008-09-14 01:06:09 +0000601 async_free(aurb);
602
603 switch(errno) {
604 case ETIMEDOUT:
605 return USB_RET_NAK;
606 case EPIPE:
607 default:
608 return USB_RET_STALL;
609 }
610 }
611
612 usb_defer_packet(p, async_cancel, aurb);
613 return USB_RET_ASYNC;
614}
615
616static int do_token_setup(USBDevice *dev, USBPacket *p)
617{
618 USBHostDevice *s = (USBHostDevice *) dev;
619 int ret = 0;
620
David Ahern27911042010-04-24 10:26:22 -0600621 if (p->len != 8) {
aliguori446ab122008-09-14 01:06:09 +0000622 return USB_RET_STALL;
David Ahern27911042010-04-24 10:26:22 -0600623 }
624
aliguori446ab122008-09-14 01:06:09 +0000625 memcpy(&s->ctrl.req, p->data, 8);
626 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
627 s->ctrl.offset = 0;
628 s->ctrl.state = CTRL_STATE_SETUP;
629
630 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
631 ret = usb_host_handle_control(s, p);
David Ahern27911042010-04-24 10:26:22 -0600632 if (ret < 0) {
aliguori446ab122008-09-14 01:06:09 +0000633 return ret;
David Ahern27911042010-04-24 10:26:22 -0600634 }
aliguori446ab122008-09-14 01:06:09 +0000635
David Ahern27911042010-04-24 10:26:22 -0600636 if (ret < s->ctrl.len) {
aliguori446ab122008-09-14 01:06:09 +0000637 s->ctrl.len = ret;
David Ahern27911042010-04-24 10:26:22 -0600638 }
aliguori446ab122008-09-14 01:06:09 +0000639 s->ctrl.state = CTRL_STATE_DATA;
640 } else {
David Ahern27911042010-04-24 10:26:22 -0600641 if (s->ctrl.len == 0) {
aliguori446ab122008-09-14 01:06:09 +0000642 s->ctrl.state = CTRL_STATE_ACK;
David Ahern27911042010-04-24 10:26:22 -0600643 } else {
aliguori446ab122008-09-14 01:06:09 +0000644 s->ctrl.state = CTRL_STATE_DATA;
David Ahern27911042010-04-24 10:26:22 -0600645 }
aliguori446ab122008-09-14 01:06:09 +0000646 }
647
648 return ret;
649}
650
651static int do_token_in(USBDevice *dev, USBPacket *p)
652{
653 USBHostDevice *s = (USBHostDevice *) dev;
654 int ret = 0;
655
David Ahern27911042010-04-24 10:26:22 -0600656 if (p->devep != 0) {
aliguori446ab122008-09-14 01:06:09 +0000657 return usb_host_handle_data(s, p);
David Ahern27911042010-04-24 10:26:22 -0600658 }
aliguori446ab122008-09-14 01:06:09 +0000659
660 switch(s->ctrl.state) {
661 case CTRL_STATE_ACK:
662 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
663 ret = usb_host_handle_control(s, p);
David Ahern27911042010-04-24 10:26:22 -0600664 if (ret == USB_RET_ASYNC) {
aliguori446ab122008-09-14 01:06:09 +0000665 return USB_RET_ASYNC;
David Ahern27911042010-04-24 10:26:22 -0600666 }
aliguori446ab122008-09-14 01:06:09 +0000667 s->ctrl.state = CTRL_STATE_IDLE;
668 return ret > 0 ? 0 : ret;
669 }
670
671 return 0;
672
673 case CTRL_STATE_DATA:
674 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
675 int len = s->ctrl.len - s->ctrl.offset;
David Ahern27911042010-04-24 10:26:22 -0600676 if (len > p->len) {
aliguori446ab122008-09-14 01:06:09 +0000677 len = p->len;
David Ahern27911042010-04-24 10:26:22 -0600678 }
aliguori446ab122008-09-14 01:06:09 +0000679 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
680 s->ctrl.offset += len;
David Ahern27911042010-04-24 10:26:22 -0600681 if (s->ctrl.offset >= s->ctrl.len) {
aliguori446ab122008-09-14 01:06:09 +0000682 s->ctrl.state = CTRL_STATE_ACK;
David Ahern27911042010-04-24 10:26:22 -0600683 }
aliguori446ab122008-09-14 01:06:09 +0000684 return len;
685 }
686
687 s->ctrl.state = CTRL_STATE_IDLE;
688 return USB_RET_STALL;
689
690 default:
691 return USB_RET_STALL;
692 }
693}
694
695static int do_token_out(USBDevice *dev, USBPacket *p)
696{
697 USBHostDevice *s = (USBHostDevice *) dev;
698
David Ahern27911042010-04-24 10:26:22 -0600699 if (p->devep != 0) {
aliguori446ab122008-09-14 01:06:09 +0000700 return usb_host_handle_data(s, p);
David Ahern27911042010-04-24 10:26:22 -0600701 }
aliguori446ab122008-09-14 01:06:09 +0000702
703 switch(s->ctrl.state) {
704 case CTRL_STATE_ACK:
705 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
706 s->ctrl.state = CTRL_STATE_IDLE;
707 /* transfer OK */
708 } else {
709 /* ignore additional output */
710 }
711 return 0;
712
713 case CTRL_STATE_DATA:
714 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
715 int len = s->ctrl.len - s->ctrl.offset;
David Ahern27911042010-04-24 10:26:22 -0600716 if (len > p->len) {
aliguori446ab122008-09-14 01:06:09 +0000717 len = p->len;
David Ahern27911042010-04-24 10:26:22 -0600718 }
aliguori446ab122008-09-14 01:06:09 +0000719 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
720 s->ctrl.offset += len;
David Ahern27911042010-04-24 10:26:22 -0600721 if (s->ctrl.offset >= s->ctrl.len) {
aliguori446ab122008-09-14 01:06:09 +0000722 s->ctrl.state = CTRL_STATE_ACK;
David Ahern27911042010-04-24 10:26:22 -0600723 }
aliguori446ab122008-09-14 01:06:09 +0000724 return len;
725 }
726
727 s->ctrl.state = CTRL_STATE_IDLE;
728 return USB_RET_STALL;
729
730 default:
731 return USB_RET_STALL;
732 }
733}
734
735/*
736 * Packet handler.
737 * Called by the HC (host controller).
738 *
739 * Returns length of the transaction or one of the USB_RET_XXX codes.
740 */
blueswir1d9cf1572008-09-15 14:57:11 +0000741static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
aliguori446ab122008-09-14 01:06:09 +0000742{
743 switch(p->pid) {
744 case USB_MSG_ATTACH:
745 s->state = USB_STATE_ATTACHED;
746 return 0;
747
748 case USB_MSG_DETACH:
749 s->state = USB_STATE_NOTATTACHED;
750 return 0;
751
752 case USB_MSG_RESET:
753 s->remote_wakeup = 0;
754 s->addr = 0;
755 s->state = USB_STATE_DEFAULT;
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200756 s->info->handle_reset(s);
aliguori446ab122008-09-14 01:06:09 +0000757 return 0;
758 }
759
760 /* Rest of the PIDs must match our address */
David Ahern27911042010-04-24 10:26:22 -0600761 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr) {
aliguori446ab122008-09-14 01:06:09 +0000762 return USB_RET_NODEV;
David Ahern27911042010-04-24 10:26:22 -0600763 }
aliguori446ab122008-09-14 01:06:09 +0000764
765 switch (p->pid) {
766 case USB_TOKEN_SETUP:
767 return do_token_setup(s, p);
768
769 case USB_TOKEN_IN:
770 return do_token_in(s, p);
771
772 case USB_TOKEN_OUT:
773 return do_token_out(s, p);
David Ahern27911042010-04-24 10:26:22 -0600774
aliguori446ab122008-09-14 01:06:09 +0000775 default:
776 return USB_RET_STALL;
777 }
778}
779
Hans de Goede71d71bb2010-11-10 10:06:24 +0100780static int usb_linux_get_configuration(USBHostDevice *s)
balrogb9dc0332007-10-04 22:47:34 +0000781{
Hans de Goede71d71bb2010-11-10 10:06:24 +0100782 uint8_t configuration;
pbrooke41b3912008-10-28 18:22:59 +0000783 struct usb_ctrltransfer ct;
Hans de Goede71d71bb2010-11-10 10:06:24 +0100784 int ret;
balrogb9dc0332007-10-04 22:47:34 +0000785
Hans de Goede2cc59d82010-11-10 10:06:25 +0100786 if (usb_fs_type == USB_FS_SYS) {
787 char device_name[32], line[1024];
788 int configuration;
789
790 sprintf(device_name, "%d-%d", s->bus_num, s->devpath);
791
792 if (!usb_host_read_file(line, sizeof(line), "bConfigurationValue",
793 device_name)) {
794 goto usbdevfs;
795 }
796 if (sscanf(line, "%d", &configuration) != 1) {
797 goto usbdevfs;
798 }
799 return configuration;
800 }
801
802usbdevfs:
balrogb9dc0332007-10-04 22:47:34 +0000803 ct.bRequestType = USB_DIR_IN;
804 ct.bRequest = USB_REQ_GET_CONFIGURATION;
805 ct.wValue = 0;
806 ct.wIndex = 0;
807 ct.wLength = 1;
808 ct.data = &configuration;
809 ct.timeout = 50;
810
811 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
812 if (ret < 0) {
Hans de Goede71d71bb2010-11-10 10:06:24 +0100813 perror("usb_linux_get_configuration");
814 return -1;
balrogb9dc0332007-10-04 22:47:34 +0000815 }
816
817 /* in address state */
David Ahern27911042010-04-24 10:26:22 -0600818 if (configuration == 0) {
Hans de Goede71d71bb2010-11-10 10:06:24 +0100819 return -1;
David Ahern27911042010-04-24 10:26:22 -0600820 }
balrogb9dc0332007-10-04 22:47:34 +0000821
Hans de Goede71d71bb2010-11-10 10:06:24 +0100822 return configuration;
823}
824
825/* returns 1 on problem encountered or 0 for success */
826static int usb_linux_update_endp_table(USBHostDevice *s)
827{
828 uint8_t *descriptors;
829 uint8_t devep, type, configuration, alt_interface;
830 struct usb_ctrltransfer ct;
831 int interface, ret, length, i;
832
833 i = usb_linux_get_configuration(s);
834 if (i < 0)
835 return 1;
836 configuration = i;
837
balrogb9dc0332007-10-04 22:47:34 +0000838 /* get the desired configuration, interface, and endpoint descriptors
839 * from device description */
840 descriptors = &s->descr[18];
841 length = s->descr_len - 18;
842 i = 0;
843
844 if (descriptors[i + 1] != USB_DT_CONFIG ||
845 descriptors[i + 5] != configuration) {
malcd0f2c4c2010-02-07 02:03:50 +0300846 DPRINTF("invalid descriptor data - configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000847 return 1;
848 }
849 i += descriptors[i];
850
851 while (i < length) {
852 if (descriptors[i + 1] != USB_DT_INTERFACE ||
853 (descriptors[i + 1] == USB_DT_INTERFACE &&
854 descriptors[i + 4] == 0)) {
855 i += descriptors[i];
856 continue;
857 }
858
859 interface = descriptors[i + 2];
860
861 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
862 ct.bRequest = USB_REQ_GET_INTERFACE;
863 ct.wValue = 0;
864 ct.wIndex = interface;
865 ct.wLength = 1;
866 ct.data = &alt_interface;
867 ct.timeout = 50;
868
869 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
870 if (ret < 0) {
Jason Wesseld55ebf52009-05-18 10:00:28 -0500871 alt_interface = interface;
balrogb9dc0332007-10-04 22:47:34 +0000872 }
873
874 /* the current interface descriptor is the active interface
875 * and has endpoints */
876 if (descriptors[i + 3] != alt_interface) {
877 i += descriptors[i];
878 continue;
879 }
880
881 /* advance to the endpoints */
David Ahern27911042010-04-24 10:26:22 -0600882 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
balrogb9dc0332007-10-04 22:47:34 +0000883 i += descriptors[i];
David Ahern27911042010-04-24 10:26:22 -0600884 }
balrogb9dc0332007-10-04 22:47:34 +0000885
886 if (i >= length)
887 break;
888
889 while (i < length) {
David Ahern27911042010-04-24 10:26:22 -0600890 if (descriptors[i + 1] != USB_DT_ENDPOINT) {
balrogb9dc0332007-10-04 22:47:34 +0000891 break;
David Ahern27911042010-04-24 10:26:22 -0600892 }
balrogb9dc0332007-10-04 22:47:34 +0000893
894 devep = descriptors[i + 2];
895 switch (descriptors[i + 3] & 0x3) {
896 case 0x00:
897 type = USBDEVFS_URB_TYPE_CONTROL;
898 break;
899 case 0x01:
900 type = USBDEVFS_URB_TYPE_ISO;
901 break;
902 case 0x02:
903 type = USBDEVFS_URB_TYPE_BULK;
904 break;
905 case 0x03:
906 type = USBDEVFS_URB_TYPE_INTERRUPT;
907 break;
Anthony Liguoriddbda432010-03-17 16:00:24 -0500908 default:
909 DPRINTF("usb_host: malformed endpoint type\n");
910 type = USBDEVFS_URB_TYPE_BULK;
balrogb9dc0332007-10-04 22:47:34 +0000911 }
912 s->endp_table[(devep & 0xf) - 1].type = type;
aliguori64838172008-08-21 19:31:10 +0000913 s->endp_table[(devep & 0xf) - 1].halted = 0;
balrogb9dc0332007-10-04 22:47:34 +0000914
915 i += descriptors[i];
916 }
917 }
918 return 0;
919}
920
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100921static int usb_host_open(USBHostDevice *dev, int bus_num,
Hans de Goede0f5160d2010-11-10 10:06:23 +0100922 int addr, int devpath, const char *prod_name)
bellardbb36d472005-11-05 14:22:28 +0000923{
balrogb9dc0332007-10-04 22:47:34 +0000924 int fd = -1, ret;
bellardbb36d472005-11-05 14:22:28 +0000925 struct usbdevfs_connectinfo ci;
bellarda594cfb2005-11-06 16:13:29 +0000926 char buf[1024];
aliguori1f3870a2008-08-21 19:27:48 +0000927
David Ahern27911042010-04-24 10:26:22 -0600928 if (dev->fd != -1) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100929 goto fail;
David Ahern27911042010-04-24 10:26:22 -0600930 }
aliguori64838172008-08-21 19:31:10 +0000931 printf("husb: open device %d.%d\n", bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000932
aliguori0f431522008-10-07 20:06:37 +0000933 if (!usb_host_device_path) {
934 perror("husb: USB Host Device Path not set");
935 goto fail;
936 }
937 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
bellarda594cfb2005-11-06 16:13:29 +0000938 bus_num, addr);
balrogb9dc0332007-10-04 22:47:34 +0000939 fd = open(buf, O_RDWR | O_NONBLOCK);
bellardbb36d472005-11-05 14:22:28 +0000940 if (fd < 0) {
bellarda594cfb2005-11-06 16:13:29 +0000941 perror(buf);
aliguori1f3870a2008-08-21 19:27:48 +0000942 goto fail;
bellardbb36d472005-11-05 14:22:28 +0000943 }
malcd0f2c4c2010-02-07 02:03:50 +0300944 DPRINTF("husb: opened %s\n", buf);
bellardbb36d472005-11-05 14:22:28 +0000945
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200946 dev->bus_num = bus_num;
947 dev->addr = addr;
Hans de Goede0f5160d2010-11-10 10:06:23 +0100948 dev->devpath = devpath;
Gerd Hoffmann22f84e72009-09-25 16:55:28 +0200949 dev->fd = fd;
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200950
balrogb9dc0332007-10-04 22:47:34 +0000951 /* read the device description */
952 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
953 if (dev->descr_len <= 0) {
aliguori64838172008-08-21 19:31:10 +0000954 perror("husb: reading device data failed");
bellardbb36d472005-11-05 14:22:28 +0000955 goto fail;
956 }
ths3b46e622007-09-17 08:09:54 +0000957
balrogb9dc0332007-10-04 22:47:34 +0000958#ifdef DEBUG
bellard868bfe22005-11-13 21:53:15 +0000959 {
balrogb9dc0332007-10-04 22:47:34 +0000960 int x;
961 printf("=== begin dumping device descriptor data ===\n");
David Ahern27911042010-04-24 10:26:22 -0600962 for (x = 0; x < dev->descr_len; x++) {
balrogb9dc0332007-10-04 22:47:34 +0000963 printf("%02x ", dev->descr[x]);
David Ahern27911042010-04-24 10:26:22 -0600964 }
balrogb9dc0332007-10-04 22:47:34 +0000965 printf("\n=== end dumping device descriptor data ===\n");
bellarda594cfb2005-11-06 16:13:29 +0000966 }
967#endif
968
balrogb9dc0332007-10-04 22:47:34 +0000969
David Ahern27911042010-04-24 10:26:22 -0600970 /*
971 * Initial configuration is -1 which makes us claim first
aliguori446ab122008-09-14 01:06:09 +0000972 * available config. We used to start with 1, which does not
David Ahern27911042010-04-24 10:26:22 -0600973 * always work. I've seen devices where first config starts
aliguori446ab122008-09-14 01:06:09 +0000974 * with 2.
975 */
David Ahern27911042010-04-24 10:26:22 -0600976 if (!usb_host_claim_interfaces(dev, -1)) {
balrogb9dc0332007-10-04 22:47:34 +0000977 goto fail;
David Ahern27911042010-04-24 10:26:22 -0600978 }
bellardbb36d472005-11-05 14:22:28 +0000979
980 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
981 if (ret < 0) {
balrog046833e2007-10-31 00:27:50 +0000982 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
bellardbb36d472005-11-05 14:22:28 +0000983 goto fail;
984 }
985
aliguori64838172008-08-21 19:31:10 +0000986 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
bellardbb36d472005-11-05 14:22:28 +0000987
balrogb9dc0332007-10-04 22:47:34 +0000988 ret = usb_linux_update_endp_table(dev);
David Ahern27911042010-04-24 10:26:22 -0600989 if (ret) {
bellardbb36d472005-11-05 14:22:28 +0000990 goto fail;
David Ahern27911042010-04-24 10:26:22 -0600991 }
balrogb9dc0332007-10-04 22:47:34 +0000992
David Ahern27911042010-04-24 10:26:22 -0600993 if (ci.slow) {
bellardbb36d472005-11-05 14:22:28 +0000994 dev->dev.speed = USB_SPEED_LOW;
David Ahern27911042010-04-24 10:26:22 -0600995 } else {
bellardbb36d472005-11-05 14:22:28 +0000996 dev->dev.speed = USB_SPEED_HIGH;
David Ahern27911042010-04-24 10:26:22 -0600997 }
bellardbb36d472005-11-05 14:22:28 +0000998
David Ahern27911042010-04-24 10:26:22 -0600999 if (!prod_name || prod_name[0] == '\0') {
Markus Armbruster0fe6d122009-12-09 17:07:51 +01001000 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
aliguori4b096fc2008-08-21 19:28:55 +00001001 "host:%d.%d", bus_num, addr);
David Ahern27911042010-04-24 10:26:22 -06001002 } else {
Markus Armbruster0fe6d122009-12-09 17:07:51 +01001003 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
aliguori4b096fc2008-08-21 19:28:55 +00001004 prod_name);
David Ahern27911042010-04-24 10:26:22 -06001005 }
bellard1f6e24e2006-06-26 21:00:51 +00001006
aliguori64838172008-08-21 19:31:10 +00001007 /* USB devio uses 'write' flag to check for async completions */
1008 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
aliguori1f3870a2008-08-21 19:27:48 +00001009
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001010 usb_device_attach(&dev->dev);
1011 return 0;
aliguori4b096fc2008-08-21 19:28:55 +00001012
balrogb9dc0332007-10-04 22:47:34 +00001013fail:
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001014 dev->fd = -1;
David Ahern27911042010-04-24 10:26:22 -06001015 if (fd != -1) {
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001016 close(fd);
David Ahern27911042010-04-24 10:26:22 -06001017 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001018 return -1;
1019}
1020
1021static int usb_host_close(USBHostDevice *dev)
1022{
David Ahern27911042010-04-24 10:26:22 -06001023 if (dev->fd == -1) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001024 return -1;
David Ahern27911042010-04-24 10:26:22 -06001025 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001026
1027 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
1028 dev->closing = 1;
1029 async_complete(dev);
1030 dev->closing = 0;
1031 usb_device_detach(&dev->dev);
Shahar Havivi00ff2272010-06-16 15:15:37 +03001032 ioctl(dev->fd, USBDEVFS_RESET);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001033 close(dev->fd);
1034 dev->fd = -1;
1035 return 0;
1036}
1037
Shahar Havivib373a632010-06-16 15:16:11 +03001038static void usb_host_exit_notifier(struct Notifier* n)
1039{
1040 USBHostDevice *s = container_of(n, USBHostDevice, exit);
1041
1042 if (s->fd != -1) {
1043 ioctl(s->fd, USBDEVFS_RESET);
1044 }
1045}
1046
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001047static int usb_host_initfn(USBDevice *dev)
1048{
1049 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1050
1051 dev->auto_attach = 0;
1052 s->fd = -1;
1053 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
Shahar Havivib373a632010-06-16 15:16:11 +03001054 s->exit.notify = usb_host_exit_notifier;
1055 qemu_add_exit_notifier(&s->exit);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001056 usb_host_auto_check(NULL);
1057 return 0;
bellardbb36d472005-11-05 14:22:28 +00001058}
1059
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001060static struct USBDeviceInfo usb_host_dev_info = {
Markus Armbruster06384692009-12-09 17:07:52 +01001061 .product_desc = "USB Host Device",
Markus Armbruster556cd092009-12-09 17:07:53 +01001062 .qdev.name = "usb-host",
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001063 .qdev.size = sizeof(USBHostDevice),
1064 .init = usb_host_initfn,
1065 .handle_packet = usb_host_handle_packet,
1066 .handle_reset = usb_host_handle_reset,
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001067 .handle_destroy = usb_host_handle_destroy,
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001068 .usbdevice_name = "host",
1069 .usbdevice_init = usb_host_device_open,
1070 .qdev.props = (Property[]) {
1071 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1072 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1073 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1074 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1075 DEFINE_PROP_END_OF_LIST(),
1076 },
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001077};
1078
1079static void usb_host_register_devices(void)
1080{
1081 usb_qdev_register(&usb_host_dev_info);
1082}
1083device_init(usb_host_register_devices)
1084
aliguori4b096fc2008-08-21 19:28:55 +00001085USBDevice *usb_host_device_open(const char *devname)
1086{
Markus Armbruster0745eb12009-11-27 13:05:53 +01001087 struct USBAutoFilter filter;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001088 USBDevice *dev;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001089 char *p;
1090
Markus Armbruster556cd092009-12-09 17:07:53 +01001091 dev = usb_create(NULL /* FIXME */, "usb-host");
aliguori4b096fc2008-08-21 19:28:55 +00001092
aliguori5d0c5752008-09-14 01:07:41 +00001093 if (strstr(devname, "auto:")) {
David Ahern27911042010-04-24 10:26:22 -06001094 if (parse_filter(devname, &filter) < 0) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001095 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001096 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001097 } else {
1098 if ((p = strchr(devname, '.'))) {
Markus Armbruster0745eb12009-11-27 13:05:53 +01001099 filter.bus_num = strtoul(devname, NULL, 0);
1100 filter.addr = strtoul(p + 1, NULL, 0);
1101 filter.vendor_id = 0;
1102 filter.product_id = 0;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001103 } else if ((p = strchr(devname, ':'))) {
Markus Armbruster0745eb12009-11-27 13:05:53 +01001104 filter.bus_num = 0;
1105 filter.addr = 0;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001106 filter.vendor_id = strtoul(devname, NULL, 16);
Markus Armbruster0745eb12009-11-27 13:05:53 +01001107 filter.product_id = strtoul(p + 1, NULL, 16);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001108 } else {
1109 goto fail;
1110 }
aliguori5d0c5752008-09-14 01:07:41 +00001111 }
1112
Markus Armbruster0745eb12009-11-27 13:05:53 +01001113 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1114 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001115 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1116 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
Kevin Wolfbeb6f0d2010-01-15 12:56:41 +01001117 qdev_init_nofail(&dev->qdev);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001118 return dev;
aliguori4b096fc2008-08-21 19:28:55 +00001119
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001120fail:
1121 qdev_free(&dev->qdev);
1122 return NULL;
aliguori4b096fc2008-08-21 19:28:55 +00001123}
aliguori5d0c5752008-09-14 01:07:41 +00001124
1125int usb_host_device_close(const char *devname)
1126{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001127#if 0
aliguori5d0c5752008-09-14 01:07:41 +00001128 char product_name[PRODUCT_NAME_SZ];
1129 int bus_num, addr;
1130 USBHostDevice *s;
1131
David Ahern27911042010-04-24 10:26:22 -06001132 if (strstr(devname, "auto:")) {
aliguori5d0c5752008-09-14 01:07:41 +00001133 return usb_host_auto_del(devname);
David Ahern27911042010-04-24 10:26:22 -06001134 }
1135 if (usb_host_find_device(&bus_num, &addr, product_name,
1136 sizeof(product_name), devname) < 0) {
aliguori5d0c5752008-09-14 01:07:41 +00001137 return -1;
David Ahern27911042010-04-24 10:26:22 -06001138 }
aliguori5d0c5752008-09-14 01:07:41 +00001139 s = hostdev_find(bus_num, addr);
1140 if (s) {
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001141 usb_device_delete_addr(s->bus_num, s->dev.addr);
aliguori5d0c5752008-09-14 01:07:41 +00001142 return 0;
1143 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001144#endif
aliguori5d0c5752008-09-14 01:07:41 +00001145
1146 return -1;
1147}
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001148
bellarda594cfb2005-11-06 16:13:29 +00001149static int get_tag_value(char *buf, int buf_size,
ths5fafdf22007-09-16 21:08:06 +00001150 const char *str, const char *tag,
bellarda594cfb2005-11-06 16:13:29 +00001151 const char *stopchars)
bellardbb36d472005-11-05 14:22:28 +00001152{
bellarda594cfb2005-11-06 16:13:29 +00001153 const char *p;
1154 char *q;
1155 p = strstr(str, tag);
David Ahern27911042010-04-24 10:26:22 -06001156 if (!p) {
bellarda594cfb2005-11-06 16:13:29 +00001157 return -1;
David Ahern27911042010-04-24 10:26:22 -06001158 }
bellarda594cfb2005-11-06 16:13:29 +00001159 p += strlen(tag);
David Ahern27911042010-04-24 10:26:22 -06001160 while (qemu_isspace(*p)) {
bellarda594cfb2005-11-06 16:13:29 +00001161 p++;
David Ahern27911042010-04-24 10:26:22 -06001162 }
bellarda594cfb2005-11-06 16:13:29 +00001163 q = buf;
1164 while (*p != '\0' && !strchr(stopchars, *p)) {
David Ahern27911042010-04-24 10:26:22 -06001165 if ((q - buf) < (buf_size - 1)) {
bellarda594cfb2005-11-06 16:13:29 +00001166 *q++ = *p;
David Ahern27911042010-04-24 10:26:22 -06001167 }
bellarda594cfb2005-11-06 16:13:29 +00001168 p++;
1169 }
1170 *q = '\0';
1171 return q - buf;
1172}
bellardbb36d472005-11-05 14:22:28 +00001173
aliguori0f431522008-10-07 20:06:37 +00001174/*
1175 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1176 * host's USB devices. This is legacy support since many distributions
1177 * are moving to /sys/bus/usb
1178 */
1179static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
bellarda594cfb2005-11-06 16:13:29 +00001180{
Blue Swirl660f11b2009-07-31 21:16:51 +00001181 FILE *f = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001182 char line[1024];
1183 char buf[1024];
1184 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
bellarda594cfb2005-11-06 16:13:29 +00001185 char product_name[512];
aliguori0f431522008-10-07 20:06:37 +00001186 int ret = 0;
ths3b46e622007-09-17 08:09:54 +00001187
aliguori0f431522008-10-07 20:06:37 +00001188 if (!usb_host_device_path) {
1189 perror("husb: USB Host Device Path not set");
1190 goto the_end;
bellarda594cfb2005-11-06 16:13:29 +00001191 }
aliguori0f431522008-10-07 20:06:37 +00001192 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1193 f = fopen(line, "r");
1194 if (!f) {
1195 perror("husb: cannot open devices file");
1196 goto the_end;
1197 }
1198
bellarda594cfb2005-11-06 16:13:29 +00001199 device_count = 0;
1200 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
bellardbb36d472005-11-05 14:22:28 +00001201 for(;;) {
David Ahern27911042010-04-24 10:26:22 -06001202 if (fgets(line, sizeof(line), f) == NULL) {
bellardbb36d472005-11-05 14:22:28 +00001203 break;
David Ahern27911042010-04-24 10:26:22 -06001204 }
1205 if (strlen(line) > 0) {
bellarda594cfb2005-11-06 16:13:29 +00001206 line[strlen(line) - 1] = '\0';
David Ahern27911042010-04-24 10:26:22 -06001207 }
bellarda594cfb2005-11-06 16:13:29 +00001208 if (line[0] == 'T' && line[1] == ':') {
pbrook38ca0f62006-03-11 18:03:38 +00001209 if (device_count && (vendor_id || product_id)) {
1210 /* New device. Add the previously discovered device. */
Hans de Goede0f5160d2010-11-10 10:06:23 +01001211 ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001212 product_id, product_name, speed);
David Ahern27911042010-04-24 10:26:22 -06001213 if (ret) {
bellarda594cfb2005-11-06 16:13:29 +00001214 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001215 }
bellarda594cfb2005-11-06 16:13:29 +00001216 }
David Ahern27911042010-04-24 10:26:22 -06001217 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001218 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001219 }
bellarda594cfb2005-11-06 16:13:29 +00001220 bus_num = atoi(buf);
David Ahern27911042010-04-24 10:26:22 -06001221 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001222 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001223 }
bellarda594cfb2005-11-06 16:13:29 +00001224 addr = atoi(buf);
David Ahern27911042010-04-24 10:26:22 -06001225 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001226 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001227 }
1228 if (!strcmp(buf, "480")) {
bellarda594cfb2005-11-06 16:13:29 +00001229 speed = USB_SPEED_HIGH;
David Ahern27911042010-04-24 10:26:22 -06001230 } else if (!strcmp(buf, "1.5")) {
bellarda594cfb2005-11-06 16:13:29 +00001231 speed = USB_SPEED_LOW;
David Ahern27911042010-04-24 10:26:22 -06001232 } else {
bellarda594cfb2005-11-06 16:13:29 +00001233 speed = USB_SPEED_FULL;
David Ahern27911042010-04-24 10:26:22 -06001234 }
bellarda594cfb2005-11-06 16:13:29 +00001235 product_name[0] = '\0';
1236 class_id = 0xff;
1237 device_count++;
1238 product_id = 0;
1239 vendor_id = 0;
1240 } else if (line[0] == 'P' && line[1] == ':') {
David Ahern27911042010-04-24 10:26:22 -06001241 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001242 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001243 }
bellarda594cfb2005-11-06 16:13:29 +00001244 vendor_id = strtoul(buf, NULL, 16);
David Ahern27911042010-04-24 10:26:22 -06001245 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001246 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001247 }
bellarda594cfb2005-11-06 16:13:29 +00001248 product_id = strtoul(buf, NULL, 16);
1249 } else if (line[0] == 'S' && line[1] == ':') {
David Ahern27911042010-04-24 10:26:22 -06001250 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001251 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001252 }
bellarda594cfb2005-11-06 16:13:29 +00001253 pstrcpy(product_name, sizeof(product_name), buf);
1254 } else if (line[0] == 'D' && line[1] == ':') {
David Ahern27911042010-04-24 10:26:22 -06001255 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
bellarda594cfb2005-11-06 16:13:29 +00001256 goto fail;
David Ahern27911042010-04-24 10:26:22 -06001257 }
bellarda594cfb2005-11-06 16:13:29 +00001258 class_id = strtoul(buf, NULL, 16);
1259 }
1260 fail: ;
1261 }
pbrook38ca0f62006-03-11 18:03:38 +00001262 if (device_count && (vendor_id || product_id)) {
1263 /* Add the last device. */
Hans de Goede0f5160d2010-11-10 10:06:23 +01001264 ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001265 product_id, product_name, speed);
1266 }
1267 the_end:
David Ahern27911042010-04-24 10:26:22 -06001268 if (f) {
aliguori0f431522008-10-07 20:06:37 +00001269 fclose(f);
David Ahern27911042010-04-24 10:26:22 -06001270 }
aliguori0f431522008-10-07 20:06:37 +00001271 return ret;
1272}
1273
1274/*
1275 * Read sys file-system device file
1276 *
1277 * @line address of buffer to put file contents in
1278 * @line_size size of line
1279 * @device_file path to device file (printf format string)
1280 * @device_name device being opened (inserted into device_file)
1281 *
1282 * @return 0 failed, 1 succeeded ('line' contains data)
1283 */
David Ahern27911042010-04-24 10:26:22 -06001284static int usb_host_read_file(char *line, size_t line_size,
1285 const char *device_file, const char *device_name)
aliguori0f431522008-10-07 20:06:37 +00001286{
1287 FILE *f;
1288 int ret = 0;
1289 char filename[PATH_MAX];
1290
blueswir1b4e237a2008-12-28 15:45:20 +00001291 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1292 device_file);
aliguori0f431522008-10-07 20:06:37 +00001293 f = fopen(filename, "r");
1294 if (f) {
Kirill A. Shutemov9f99cee2010-01-20 00:56:17 +01001295 ret = fgets(line, line_size, f) != NULL;
aliguori0f431522008-10-07 20:06:37 +00001296 fclose(f);
aliguori0f431522008-10-07 20:06:37 +00001297 }
1298
1299 return ret;
1300}
1301
1302/*
1303 * Use /sys/bus/usb/devices/ directory to determine host's USB
1304 * devices.
1305 *
1306 * This code is based on Robert Schiele's original patches posted to
1307 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1308 */
1309static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1310{
Blue Swirl660f11b2009-07-31 21:16:51 +00001311 DIR *dir = NULL;
aliguori0f431522008-10-07 20:06:37 +00001312 char line[1024];
Hans de Goede0f5160d2010-11-10 10:06:23 +01001313 int bus_num, addr, devpath, speed, class_id, product_id, vendor_id;
aliguori0f431522008-10-07 20:06:37 +00001314 int ret = 0;
1315 char product_name[512];
1316 struct dirent *de;
1317
1318 dir = opendir(USBSYSBUS_PATH "/devices");
1319 if (!dir) {
1320 perror("husb: cannot open devices directory");
1321 goto the_end;
1322 }
1323
1324 while ((de = readdir(dir))) {
1325 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1326 char *tmpstr = de->d_name;
David Ahern27911042010-04-24 10:26:22 -06001327 if (!strncmp(de->d_name, "usb", 3)) {
aliguori0f431522008-10-07 20:06:37 +00001328 tmpstr += 3;
David Ahern27911042010-04-24 10:26:22 -06001329 }
Hans de Goede0f5160d2010-11-10 10:06:23 +01001330 if (sscanf(tmpstr, "%d-%d", &bus_num, &devpath) < 1) {
1331 goto the_end;
1332 }
aliguori0f431522008-10-07 20:06:37 +00001333
David Ahern27911042010-04-24 10:26:22 -06001334 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001335 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001336 }
1337 if (sscanf(line, "%d", &addr) != 1) {
aliguori0f431522008-10-07 20:06:37 +00001338 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001339 }
blueswir1b4e237a2008-12-28 15:45:20 +00001340 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
David Ahern27911042010-04-24 10:26:22 -06001341 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001342 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001343 }
1344 if (sscanf(line, "%x", &class_id) != 1) {
aliguori0f431522008-10-07 20:06:37 +00001345 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001346 }
aliguori0f431522008-10-07 20:06:37 +00001347
David Ahern27911042010-04-24 10:26:22 -06001348 if (!usb_host_read_file(line, sizeof(line), "idVendor",
1349 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001350 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001351 }
1352 if (sscanf(line, "%x", &vendor_id) != 1) {
aliguori0f431522008-10-07 20:06:37 +00001353 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001354 }
blueswir1b4e237a2008-12-28 15:45:20 +00001355 if (!usb_host_read_file(line, sizeof(line), "idProduct",
David Ahern27911042010-04-24 10:26:22 -06001356 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001357 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001358 }
1359 if (sscanf(line, "%x", &product_id) != 1) {
aliguori0f431522008-10-07 20:06:37 +00001360 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001361 }
blueswir1b4e237a2008-12-28 15:45:20 +00001362 if (!usb_host_read_file(line, sizeof(line), "product",
1363 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001364 *product_name = 0;
1365 } else {
David Ahern27911042010-04-24 10:26:22 -06001366 if (strlen(line) > 0) {
aliguori0f431522008-10-07 20:06:37 +00001367 line[strlen(line) - 1] = '\0';
David Ahern27911042010-04-24 10:26:22 -06001368 }
aliguori0f431522008-10-07 20:06:37 +00001369 pstrcpy(product_name, sizeof(product_name), line);
1370 }
1371
David Ahern27911042010-04-24 10:26:22 -06001372 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001373 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001374 }
1375 if (!strcmp(line, "480\n")) {
aliguori0f431522008-10-07 20:06:37 +00001376 speed = USB_SPEED_HIGH;
David Ahern27911042010-04-24 10:26:22 -06001377 } else if (!strcmp(line, "1.5\n")) {
aliguori0f431522008-10-07 20:06:37 +00001378 speed = USB_SPEED_LOW;
David Ahern27911042010-04-24 10:26:22 -06001379 } else {
aliguori0f431522008-10-07 20:06:37 +00001380 speed = USB_SPEED_FULL;
David Ahern27911042010-04-24 10:26:22 -06001381 }
aliguori0f431522008-10-07 20:06:37 +00001382
Hans de Goede0f5160d2010-11-10 10:06:23 +01001383 ret = func(opaque, bus_num, addr, devpath, class_id, vendor_id,
aliguori0f431522008-10-07 20:06:37 +00001384 product_id, product_name, speed);
David Ahern27911042010-04-24 10:26:22 -06001385 if (ret) {
aliguori0f431522008-10-07 20:06:37 +00001386 goto the_end;
David Ahern27911042010-04-24 10:26:22 -06001387 }
aliguori0f431522008-10-07 20:06:37 +00001388 }
1389 }
1390 the_end:
David Ahern27911042010-04-24 10:26:22 -06001391 if (dir) {
aliguori0f431522008-10-07 20:06:37 +00001392 closedir(dir);
David Ahern27911042010-04-24 10:26:22 -06001393 }
aliguori0f431522008-10-07 20:06:37 +00001394 return ret;
1395}
1396
1397/*
1398 * Determine how to access the host's USB devices and call the
1399 * specific support function.
1400 */
1401static int usb_host_scan(void *opaque, USBScanFunc *func)
1402{
aliguori376253e2009-03-05 23:01:23 +00001403 Monitor *mon = cur_mon;
Blue Swirl660f11b2009-07-31 21:16:51 +00001404 FILE *f = NULL;
1405 DIR *dir = NULL;
aliguori0f431522008-10-07 20:06:37 +00001406 int ret = 0;
aliguori0f431522008-10-07 20:06:37 +00001407 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1408 char devpath[PATH_MAX];
1409
1410 /* only check the host once */
1411 if (!usb_fs_type) {
Mark McLoughlin55496242009-07-03 09:28:02 +01001412 dir = opendir(USBSYSBUS_PATH "/devices");
1413 if (dir) {
1414 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1415 strcpy(devpath, USBDEVBUS_PATH);
1416 usb_fs_type = USB_FS_SYS;
1417 closedir(dir);
malcd0f2c4c2010-02-07 02:03:50 +03001418 DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
Mark McLoughlin55496242009-07-03 09:28:02 +01001419 goto found_devices;
1420 }
aliguori0f431522008-10-07 20:06:37 +00001421 f = fopen(USBPROCBUS_PATH "/devices", "r");
1422 if (f) {
1423 /* devices found in /proc/bus/usb/ */
1424 strcpy(devpath, USBPROCBUS_PATH);
1425 usb_fs_type = USB_FS_PROC;
1426 fclose(f);
malcd0f2c4c2010-02-07 02:03:50 +03001427 DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001428 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001429 }
1430 /* try additional methods if an access method hasn't been found yet */
1431 f = fopen(USBDEVBUS_PATH "/devices", "r");
aliguorif16a0db2008-10-21 16:34:20 +00001432 if (f) {
aliguori0f431522008-10-07 20:06:37 +00001433 /* devices found in /dev/bus/usb/ */
1434 strcpy(devpath, USBDEVBUS_PATH);
1435 usb_fs_type = USB_FS_DEV;
1436 fclose(f);
malcd0f2c4c2010-02-07 02:03:50 +03001437 DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001438 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001439 }
aliguorif16a0db2008-10-21 16:34:20 +00001440 found_devices:
aliguori22babeb2008-10-21 16:27:28 +00001441 if (!usb_fs_type) {
David Ahern27911042010-04-24 10:26:22 -06001442 if (mon) {
Gerd Hoffmanneba6fe82009-12-15 11:43:02 +01001443 monitor_printf(mon, "husb: unable to access USB devices\n");
David Ahern27911042010-04-24 10:26:22 -06001444 }
aliguorif16a0db2008-10-21 16:34:20 +00001445 return -ENOENT;
aliguori0f431522008-10-07 20:06:37 +00001446 }
1447
1448 /* the module setting (used later for opening devices) */
1449 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
aliguori1eec6142009-02-05 22:06:18 +00001450 strcpy(usb_host_device_path, devpath);
David Ahern27911042010-04-24 10:26:22 -06001451 if (mon) {
Gerd Hoffmanneba6fe82009-12-15 11:43:02 +01001452 monitor_printf(mon, "husb: using %s file-system with %s\n",
1453 fs_type[usb_fs_type], usb_host_device_path);
David Ahern27911042010-04-24 10:26:22 -06001454 }
aliguori0f431522008-10-07 20:06:37 +00001455 }
1456
1457 switch (usb_fs_type) {
1458 case USB_FS_PROC:
1459 case USB_FS_DEV:
1460 ret = usb_host_scan_dev(opaque, func);
1461 break;
1462 case USB_FS_SYS:
1463 ret = usb_host_scan_sys(opaque, func);
1464 break;
aliguorif16a0db2008-10-21 16:34:20 +00001465 default:
1466 ret = -EINVAL;
1467 break;
aliguori0f431522008-10-07 20:06:37 +00001468 }
bellarda594cfb2005-11-06 16:13:29 +00001469 return ret;
1470}
1471
aliguori4b096fc2008-08-21 19:28:55 +00001472static QEMUTimer *usb_auto_timer;
aliguori4b096fc2008-08-21 19:28:55 +00001473
Hans de Goede0f5160d2010-11-10 10:06:23 +01001474static int usb_host_auto_scan(void *opaque, int bus_num, int addr, int devpath,
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001475 int class_id, int vendor_id, int product_id,
1476 const char *product_name, int speed)
aliguori4b096fc2008-08-21 19:28:55 +00001477{
1478 struct USBAutoFilter *f;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001479 struct USBHostDevice *s;
aliguori4b096fc2008-08-21 19:28:55 +00001480
1481 /* Ignore hubs */
1482 if (class_id == 9)
1483 return 0;
1484
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001485 QTAILQ_FOREACH(s, &hostdevs, next) {
1486 f = &s->match;
1487
David Ahern27911042010-04-24 10:26:22 -06001488 if (f->bus_num > 0 && f->bus_num != bus_num) {
aliguori4b096fc2008-08-21 19:28:55 +00001489 continue;
David Ahern27911042010-04-24 10:26:22 -06001490 }
1491 if (f->addr > 0 && f->addr != addr) {
aliguori4b096fc2008-08-21 19:28:55 +00001492 continue;
David Ahern27911042010-04-24 10:26:22 -06001493 }
aliguori4b096fc2008-08-21 19:28:55 +00001494
David Ahern27911042010-04-24 10:26:22 -06001495 if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
aliguori4b096fc2008-08-21 19:28:55 +00001496 continue;
David Ahern27911042010-04-24 10:26:22 -06001497 }
aliguori4b096fc2008-08-21 19:28:55 +00001498
David Ahern27911042010-04-24 10:26:22 -06001499 if (f->product_id > 0 && f->product_id != product_id) {
aliguori4b096fc2008-08-21 19:28:55 +00001500 continue;
David Ahern27911042010-04-24 10:26:22 -06001501 }
aliguori4b096fc2008-08-21 19:28:55 +00001502 /* We got a match */
1503
Markus Armbruster33e66b82009-10-07 01:15:57 +02001504 /* Already attached ? */
David Ahern27911042010-04-24 10:26:22 -06001505 if (s->fd != -1) {
aliguori4b096fc2008-08-21 19:28:55 +00001506 return 0;
David Ahern27911042010-04-24 10:26:22 -06001507 }
malcd0f2c4c2010-02-07 02:03:50 +03001508 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
aliguori4b096fc2008-08-21 19:28:55 +00001509
Hans de Goede0f5160d2010-11-10 10:06:23 +01001510 usb_host_open(s, bus_num, addr, devpath, product_name);
aliguori4b096fc2008-08-21 19:28:55 +00001511 }
1512
1513 return 0;
1514}
1515
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001516static void usb_host_auto_check(void *unused)
aliguori4b096fc2008-08-21 19:28:55 +00001517{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001518 struct USBHostDevice *s;
1519 int unconnected = 0;
1520
aliguori4b096fc2008-08-21 19:28:55 +00001521 usb_host_scan(NULL, usb_host_auto_scan);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001522
1523 QTAILQ_FOREACH(s, &hostdevs, next) {
David Ahern27911042010-04-24 10:26:22 -06001524 if (s->fd == -1) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001525 unconnected++;
David Ahern27911042010-04-24 10:26:22 -06001526 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001527 }
1528
1529 if (unconnected == 0) {
1530 /* nothing to watch */
David Ahern27911042010-04-24 10:26:22 -06001531 if (usb_auto_timer) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001532 qemu_del_timer(usb_auto_timer);
David Ahern27911042010-04-24 10:26:22 -06001533 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001534 return;
1535 }
1536
1537 if (!usb_auto_timer) {
1538 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_check, NULL);
David Ahern27911042010-04-24 10:26:22 -06001539 if (!usb_auto_timer) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001540 return;
David Ahern27911042010-04-24 10:26:22 -06001541 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001542 }
aliguori4b096fc2008-08-21 19:28:55 +00001543 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1544}
1545
1546/*
aliguori5d0c5752008-09-14 01:07:41 +00001547 * Autoconnect filter
1548 * Format:
1549 * auto:bus:dev[:vid:pid]
1550 * auto:bus.dev[:vid:pid]
1551 *
1552 * bus - bus number (dec, * means any)
1553 * dev - device number (dec, * means any)
1554 * vid - vendor id (hex, * means any)
1555 * pid - product id (hex, * means any)
1556 *
1557 * See 'lsusb' output.
aliguori4b096fc2008-08-21 19:28:55 +00001558 */
aliguori5d0c5752008-09-14 01:07:41 +00001559static int parse_filter(const char *spec, struct USBAutoFilter *f)
aliguori4b096fc2008-08-21 19:28:55 +00001560{
aliguori5d0c5752008-09-14 01:07:41 +00001561 enum { BUS, DEV, VID, PID, DONE };
1562 const char *p = spec;
1563 int i;
1564
Markus Armbruster0745eb12009-11-27 13:05:53 +01001565 f->bus_num = 0;
1566 f->addr = 0;
1567 f->vendor_id = 0;
1568 f->product_id = 0;
aliguori5d0c5752008-09-14 01:07:41 +00001569
1570 for (i = BUS; i < DONE; i++) {
David Ahern27911042010-04-24 10:26:22 -06001571 p = strpbrk(p, ":.");
1572 if (!p) {
1573 break;
1574 }
aliguori5d0c5752008-09-14 01:07:41 +00001575 p++;
aliguori5d0c5752008-09-14 01:07:41 +00001576
David Ahern27911042010-04-24 10:26:22 -06001577 if (*p == '*') {
1578 continue;
1579 }
aliguori5d0c5752008-09-14 01:07:41 +00001580 switch(i) {
1581 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1582 case DEV: f->addr = strtol(p, NULL, 10); break;
1583 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1584 case PID: f->product_id = strtol(p, NULL, 16); break;
1585 }
aliguori4b096fc2008-08-21 19:28:55 +00001586 }
1587
aliguori5d0c5752008-09-14 01:07:41 +00001588 if (i < DEV) {
1589 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1590 return -1;
1591 }
1592
1593 return 0;
1594}
1595
bellarda594cfb2005-11-06 16:13:29 +00001596/**********************/
1597/* USB host device info */
bellardbb36d472005-11-05 14:22:28 +00001598
bellarda594cfb2005-11-06 16:13:29 +00001599struct usb_class_info {
1600 int class;
1601 const char *class_name;
1602};
1603
1604static const struct usb_class_info usb_class_info[] = {
1605 { USB_CLASS_AUDIO, "Audio"},
1606 { USB_CLASS_COMM, "Communication"},
1607 { USB_CLASS_HID, "HID"},
1608 { USB_CLASS_HUB, "Hub" },
1609 { USB_CLASS_PHYSICAL, "Physical" },
1610 { USB_CLASS_PRINTER, "Printer" },
1611 { USB_CLASS_MASS_STORAGE, "Storage" },
1612 { USB_CLASS_CDC_DATA, "Data" },
1613 { USB_CLASS_APP_SPEC, "Application Specific" },
1614 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1615 { USB_CLASS_STILL_IMAGE, "Still Image" },
balrogb9dc0332007-10-04 22:47:34 +00001616 { USB_CLASS_CSCID, "Smart Card" },
bellarda594cfb2005-11-06 16:13:29 +00001617 { USB_CLASS_CONTENT_SEC, "Content Security" },
1618 { -1, NULL }
1619};
1620
1621static const char *usb_class_str(uint8_t class)
1622{
1623 const struct usb_class_info *p;
1624 for(p = usb_class_info; p->class != -1; p++) {
David Ahern27911042010-04-24 10:26:22 -06001625 if (p->class == class) {
bellardbb36d472005-11-05 14:22:28 +00001626 break;
David Ahern27911042010-04-24 10:26:22 -06001627 }
bellardbb36d472005-11-05 14:22:28 +00001628 }
bellarda594cfb2005-11-06 16:13:29 +00001629 return p->class_name;
bellardbb36d472005-11-05 14:22:28 +00001630}
1631
Blue Swirl179da8a2009-09-07 19:00:18 +00001632static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
pbrook9596ebb2007-11-18 01:44:38 +00001633 int vendor_id, int product_id,
1634 const char *product_name,
1635 int speed)
bellardbb36d472005-11-05 14:22:28 +00001636{
bellarda594cfb2005-11-06 16:13:29 +00001637 const char *class_str, *speed_str;
1638
1639 switch(speed) {
ths5fafdf22007-09-16 21:08:06 +00001640 case USB_SPEED_LOW:
1641 speed_str = "1.5";
bellarda594cfb2005-11-06 16:13:29 +00001642 break;
ths5fafdf22007-09-16 21:08:06 +00001643 case USB_SPEED_FULL:
1644 speed_str = "12";
bellarda594cfb2005-11-06 16:13:29 +00001645 break;
ths5fafdf22007-09-16 21:08:06 +00001646 case USB_SPEED_HIGH:
1647 speed_str = "480";
bellarda594cfb2005-11-06 16:13:29 +00001648 break;
1649 default:
ths5fafdf22007-09-16 21:08:06 +00001650 speed_str = "?";
bellarda594cfb2005-11-06 16:13:29 +00001651 break;
bellardbb36d472005-11-05 14:22:28 +00001652 }
bellarda594cfb2005-11-06 16:13:29 +00001653
aliguori376253e2009-03-05 23:01:23 +00001654 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
bellarda594cfb2005-11-06 16:13:29 +00001655 bus_num, addr, speed_str);
1656 class_str = usb_class_str(class_id);
David Ahern27911042010-04-24 10:26:22 -06001657 if (class_str) {
aliguori376253e2009-03-05 23:01:23 +00001658 monitor_printf(mon, " %s:", class_str);
David Ahern27911042010-04-24 10:26:22 -06001659 } else {
aliguori376253e2009-03-05 23:01:23 +00001660 monitor_printf(mon, " Class %02x:", class_id);
David Ahern27911042010-04-24 10:26:22 -06001661 }
aliguori376253e2009-03-05 23:01:23 +00001662 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
David Ahern27911042010-04-24 10:26:22 -06001663 if (product_name[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001664 monitor_printf(mon, ", %s", product_name);
David Ahern27911042010-04-24 10:26:22 -06001665 }
aliguori376253e2009-03-05 23:01:23 +00001666 monitor_printf(mon, "\n");
bellarda594cfb2005-11-06 16:13:29 +00001667}
1668
ths5fafdf22007-09-16 21:08:06 +00001669static int usb_host_info_device(void *opaque, int bus_num, int addr,
Hans de Goede0f5160d2010-11-10 10:06:23 +01001670 int devpath, int class_id,
ths5fafdf22007-09-16 21:08:06 +00001671 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +00001672 const char *product_name,
1673 int speed)
1674{
Blue Swirl179da8a2009-09-07 19:00:18 +00001675 Monitor *mon = opaque;
1676
1677 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
bellarda594cfb2005-11-06 16:13:29 +00001678 product_name, speed);
1679 return 0;
1680}
1681
aliguoriac4ffb52008-09-22 15:04:31 +00001682static void dec2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001683{
David Ahern27911042010-04-24 10:26:22 -06001684 if (val == 0) {
aliguoriac4ffb52008-09-22 15:04:31 +00001685 snprintf(str, size, "*");
David Ahern27911042010-04-24 10:26:22 -06001686 } else {
1687 snprintf(str, size, "%d", val);
1688 }
aliguori5d0c5752008-09-14 01:07:41 +00001689}
1690
aliguoriac4ffb52008-09-22 15:04:31 +00001691static void hex2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001692{
David Ahern27911042010-04-24 10:26:22 -06001693 if (val == 0) {
aliguoriac4ffb52008-09-22 15:04:31 +00001694 snprintf(str, size, "*");
David Ahern27911042010-04-24 10:26:22 -06001695 } else {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001696 snprintf(str, size, "%04x", val);
David Ahern27911042010-04-24 10:26:22 -06001697 }
aliguori5d0c5752008-09-14 01:07:41 +00001698}
1699
aliguori376253e2009-03-05 23:01:23 +00001700void usb_host_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00001701{
aliguori5d0c5752008-09-14 01:07:41 +00001702 struct USBAutoFilter *f;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001703 struct USBHostDevice *s;
aliguori5d0c5752008-09-14 01:07:41 +00001704
Blue Swirl179da8a2009-09-07 19:00:18 +00001705 usb_host_scan(mon, usb_host_info_device);
aliguori5d0c5752008-09-14 01:07:41 +00001706
David Ahern27911042010-04-24 10:26:22 -06001707 if (QTAILQ_EMPTY(&hostdevs)) {
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001708 return;
David Ahern27911042010-04-24 10:26:22 -06001709 }
1710
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001711 monitor_printf(mon, " Auto filters:\n");
1712 QTAILQ_FOREACH(s, &hostdevs, next) {
aliguori5d0c5752008-09-14 01:07:41 +00001713 char bus[10], addr[10], vid[10], pid[10];
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001714 f = &s->match;
aliguoriac4ffb52008-09-22 15:04:31 +00001715 dec2str(f->bus_num, bus, sizeof(bus));
1716 dec2str(f->addr, addr, sizeof(addr));
1717 hex2str(f->vendor_id, vid, sizeof(vid));
1718 hex2str(f->product_id, pid, sizeof(pid));
aliguori376253e2009-03-05 23:01:23 +00001719 monitor_printf(mon, " Device %s.%s ID %s:%s\n",
1720 bus, addr, vid, pid);
aliguori5d0c5752008-09-14 01:07:41 +00001721 }
bellardbb36d472005-11-05 14:22:28 +00001722}