blob: 67e4acd5115e1f0ed558418a52c231acd4308ec3 [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);
ths5fafdf22007-09-16 21:08:06 +000067static int usb_host_find_device(int *pbus_num, int *paddr,
bellard1f6e24e2006-06-26 21:00:51 +000068 char *product_name, int product_name_size,
bellarda594cfb2005-11-06 16:13:29 +000069 const char *devname);
bellarda594cfb2005-11-06 16:13:29 +000070//#define DEBUG
aliguori64838172008-08-21 19:31:10 +000071
72#ifdef DEBUG
73#define dprintf printf
74#else
75#define dprintf(...)
76#endif
bellardbb36d472005-11-05 14:22:28 +000077
blueswir15be8e1f2008-10-25 11:47:20 +000078#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
79
aliguori0f431522008-10-07 20:06:37 +000080#define USBPROCBUS_PATH "/proc/bus/usb"
bellard1f6e24e2006-06-26 21:00:51 +000081#define PRODUCT_NAME_SZ 32
balrogb9dc0332007-10-04 22:47:34 +000082#define MAX_ENDPOINTS 16
aliguori0f431522008-10-07 20:06:37 +000083#define USBDEVBUS_PATH "/dev/bus/usb"
84#define USBSYSBUS_PATH "/sys/bus/usb"
85
86static char *usb_host_device_path;
87
88#define USB_FS_NONE 0
89#define USB_FS_PROC 1
90#define USB_FS_DEV 2
91#define USB_FS_SYS 3
92
93static int usb_fs_type;
bellardbb36d472005-11-05 14:22:28 +000094
balrogb9dc0332007-10-04 22:47:34 +000095/* endpoint association data */
96struct endp_data {
97 uint8_t type;
aliguori64838172008-08-21 19:31:10 +000098 uint8_t halted;
balrogb9dc0332007-10-04 22:47:34 +000099};
100
aliguori446ab122008-09-14 01:06:09 +0000101enum {
102 CTRL_STATE_IDLE = 0,
103 CTRL_STATE_SETUP,
104 CTRL_STATE_DATA,
105 CTRL_STATE_ACK
106};
107
108/*
109 * Control transfer state.
110 * Note that 'buffer' _must_ follow 'req' field because
111 * we need contigious buffer when we submit control URB.
112 */
113struct ctrl_struct {
114 uint16_t len;
115 uint16_t offset;
116 uint8_t state;
117 struct usb_ctrlrequest req;
118 uint8_t buffer[1024];
119};
120
bellardbb36d472005-11-05 14:22:28 +0000121typedef struct USBHostDevice {
122 USBDevice dev;
aliguori64838172008-08-21 19:31:10 +0000123 int fd;
124
125 uint8_t descr[1024];
126 int descr_len;
127 int configuration;
aliguori446ab122008-09-14 01:06:09 +0000128 int ninterfaces;
aliguori24772c12008-08-21 19:31:52 +0000129 int closing;
aliguori64838172008-08-21 19:31:10 +0000130
aliguori446ab122008-09-14 01:06:09 +0000131 struct ctrl_struct ctrl;
balrogb9dc0332007-10-04 22:47:34 +0000132 struct endp_data endp_table[MAX_ENDPOINTS];
aliguori4b096fc2008-08-21 19:28:55 +0000133
aliguori4b096fc2008-08-21 19:28:55 +0000134 /* Host side address */
135 int bus_num;
136 int addr;
137
138 struct USBHostDevice *next;
bellardbb36d472005-11-05 14:22:28 +0000139} USBHostDevice;
140
aliguori64838172008-08-21 19:31:10 +0000141static int is_isoc(USBHostDevice *s, int ep)
142{
143 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
144}
145
146static int is_halted(USBHostDevice *s, int ep)
147{
148 return s->endp_table[ep - 1].halted;
149}
150
151static void clear_halt(USBHostDevice *s, int ep)
152{
153 s->endp_table[ep - 1].halted = 0;
154}
155
156static void set_halt(USBHostDevice *s, int ep)
157{
158 s->endp_table[ep - 1].halted = 1;
159}
160
aliguori4b096fc2008-08-21 19:28:55 +0000161static USBHostDevice *hostdev_list;
162
163static void hostdev_link(USBHostDevice *dev)
164{
165 dev->next = hostdev_list;
166 hostdev_list = dev;
167}
168
169static void hostdev_unlink(USBHostDevice *dev)
170{
171 USBHostDevice *pdev = hostdev_list;
172 USBHostDevice **prev = &hostdev_list;
173
174 while (pdev) {
175 if (pdev == dev) {
176 *prev = dev->next;
177 return;
178 }
179
180 prev = &pdev->next;
181 pdev = pdev->next;
182 }
183}
184
185static USBHostDevice *hostdev_find(int bus_num, int addr)
186{
187 USBHostDevice *s = hostdev_list;
188 while (s) {
189 if (s->bus_num == bus_num && s->addr == addr)
190 return s;
191 s = s->next;
192 }
193 return NULL;
194}
195
aliguori64838172008-08-21 19:31:10 +0000196/*
197 * Async URB state.
198 * We always allocate one isoc descriptor even for bulk transfers
199 * to simplify allocation and casts.
200 */
201typedef struct AsyncURB
balrogb9dc0332007-10-04 22:47:34 +0000202{
aliguori64838172008-08-21 19:31:10 +0000203 struct usbdevfs_urb urb;
204 struct usbdevfs_iso_packet_desc isocpd;
205
206 USBPacket *packet;
207 USBHostDevice *hdev;
208} AsyncURB;
209
210static AsyncURB *async_alloc(void)
211{
212 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
balrogb9dc0332007-10-04 22:47:34 +0000213}
214
aliguori64838172008-08-21 19:31:10 +0000215static void async_free(AsyncURB *aurb)
balrogb9dc0332007-10-04 22:47:34 +0000216{
aliguori64838172008-08-21 19:31:10 +0000217 qemu_free(aurb);
218}
balrogb9dc0332007-10-04 22:47:34 +0000219
aliguori446ab122008-09-14 01:06:09 +0000220static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
221{
222 switch(s->ctrl.state) {
223 case CTRL_STATE_SETUP:
224 if (p->len < s->ctrl.len)
225 s->ctrl.len = p->len;
226 s->ctrl.state = CTRL_STATE_DATA;
227 p->len = 8;
228 break;
229
230 case CTRL_STATE_ACK:
231 s->ctrl.state = CTRL_STATE_IDLE;
232 p->len = 0;
233 break;
234
235 default:
236 break;
237 }
238}
239
aliguori64838172008-08-21 19:31:10 +0000240static void async_complete(void *opaque)
241{
242 USBHostDevice *s = opaque;
243 AsyncURB *aurb;
balrogb9dc0332007-10-04 22:47:34 +0000244
aliguori64838172008-08-21 19:31:10 +0000245 while (1) {
246 USBPacket *p;
247
248 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
249 if (r < 0) {
250 if (errno == EAGAIN)
251 return;
252
aliguori24772c12008-08-21 19:31:52 +0000253 if (errno == ENODEV && !s->closing) {
aliguori64838172008-08-21 19:31:10 +0000254 printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
255 usb_device_del_addr(0, s->dev.addr);
256 return;
257 }
258
259 dprintf("husb: async. reap urb failed errno %d\n", errno);
260 return;
balrogb9dc0332007-10-04 22:47:34 +0000261 }
aliguori64838172008-08-21 19:31:10 +0000262
263 p = aurb->packet;
264
265 dprintf("husb: async completed. aurb %p status %d alen %d\n",
266 aurb, aurb->urb.status, aurb->urb.actual_length);
267
268 if (p) {
269 switch (aurb->urb.status) {
270 case 0:
271 p->len = aurb->urb.actual_length;
aliguori446ab122008-09-14 01:06:09 +0000272 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
273 async_complete_ctrl(s, p);
aliguori64838172008-08-21 19:31:10 +0000274 break;
275
276 case -EPIPE:
277 set_halt(s, p->devep);
278 /* fall through */
279 default:
280 p->len = USB_RET_NAK;
281 break;
282 }
283
284 usb_packet_complete(p);
285 }
286
287 async_free(aurb);
balrogb9dc0332007-10-04 22:47:34 +0000288 }
balrogb9dc0332007-10-04 22:47:34 +0000289}
290
aliguori64838172008-08-21 19:31:10 +0000291static void async_cancel(USBPacket *unused, void *opaque)
balrogb9dc0332007-10-04 22:47:34 +0000292{
aliguori64838172008-08-21 19:31:10 +0000293 AsyncURB *aurb = opaque;
294 USBHostDevice *s = aurb->hdev;
balrogb9dc0332007-10-04 22:47:34 +0000295
aliguori64838172008-08-21 19:31:10 +0000296 dprintf("husb: async cancel. aurb %p\n", aurb);
balrogb9dc0332007-10-04 22:47:34 +0000297
aliguori64838172008-08-21 19:31:10 +0000298 /* Mark it as dead (see async_complete above) */
299 aurb->packet = NULL;
300
301 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
302 if (r < 0) {
303 dprintf("husb: async. discard urb failed errno %d\n", errno);
balrogb9dc0332007-10-04 22:47:34 +0000304 }
balrogb9dc0332007-10-04 22:47:34 +0000305}
306
aliguori446ab122008-09-14 01:06:09 +0000307static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
balrogb9dc0332007-10-04 22:47:34 +0000308{
309 int dev_descr_len, config_descr_len;
310 int interface, nb_interfaces, nb_configurations;
311 int ret, i;
312
313 if (configuration == 0) /* address state - ignore */
314 return 1;
315
aliguori446ab122008-09-14 01:06:09 +0000316 dprintf("husb: claiming interfaces. config %d\n", configuration);
317
balrogb9dc0332007-10-04 22:47:34 +0000318 i = 0;
319 dev_descr_len = dev->descr[0];
320 if (dev_descr_len > dev->descr_len)
321 goto fail;
322 nb_configurations = dev->descr[17];
323
324 i += dev_descr_len;
325 while (i < dev->descr_len) {
aliguori64838172008-08-21 19:31:10 +0000326 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
balrogb9dc0332007-10-04 22:47:34 +0000327 dev->descr[i], dev->descr[i+1]);
aliguori64838172008-08-21 19:31:10 +0000328
balrogb9dc0332007-10-04 22:47:34 +0000329 if (dev->descr[i+1] != USB_DT_CONFIG) {
330 i += dev->descr[i];
331 continue;
332 }
333 config_descr_len = dev->descr[i];
334
aliguori64838172008-08-21 19:31:10 +0000335 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
aliguori1f3870a2008-08-21 19:27:48 +0000336
aliguori446ab122008-09-14 01:06:09 +0000337 if (configuration < 0 || configuration == dev->descr[i + 5]) {
338 configuration = dev->descr[i + 5];
balrogb9dc0332007-10-04 22:47:34 +0000339 break;
aliguori446ab122008-09-14 01:06:09 +0000340 }
balrogb9dc0332007-10-04 22:47:34 +0000341
342 i += config_descr_len;
343 }
344
345 if (i >= dev->descr_len) {
aliguori0d380642008-08-21 19:32:29 +0000346 fprintf(stderr, "husb: update iface failed. no matching configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000347 goto fail;
348 }
349 nb_interfaces = dev->descr[i + 4];
350
351#ifdef USBDEVFS_DISCONNECT
352 /* earlier Linux 2.4 do not support that */
353 {
354 struct usbdevfs_ioctl ctrl;
355 for (interface = 0; interface < nb_interfaces; interface++) {
356 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
357 ctrl.ifno = interface;
358 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
359 if (ret < 0 && errno != ENODATA) {
360 perror("USBDEVFS_DISCONNECT");
361 goto fail;
362 }
363 }
364 }
365#endif
366
367 /* XXX: only grab if all interfaces are free */
368 for (interface = 0; interface < nb_interfaces; interface++) {
369 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
370 if (ret < 0) {
371 if (errno == EBUSY) {
aliguori64838172008-08-21 19:31:10 +0000372 printf("husb: update iface. device already grabbed\n");
balrogb9dc0332007-10-04 22:47:34 +0000373 } else {
aliguori64838172008-08-21 19:31:10 +0000374 perror("husb: failed to claim interface");
balrogb9dc0332007-10-04 22:47:34 +0000375 }
376 fail:
377 return 0;
378 }
379 }
380
aliguori64838172008-08-21 19:31:10 +0000381 printf("husb: %d interfaces claimed for configuration %d\n",
balrogb9dc0332007-10-04 22:47:34 +0000382 nb_interfaces, configuration);
balrogb9dc0332007-10-04 22:47:34 +0000383
aliguori446ab122008-09-14 01:06:09 +0000384 dev->ninterfaces = nb_interfaces;
385 dev->configuration = configuration;
386 return 1;
387}
388
389static int usb_host_release_interfaces(USBHostDevice *s)
390{
391 int ret, i;
392
393 dprintf("husb: releasing interfaces\n");
394
395 for (i = 0; i < s->ninterfaces; i++) {
396 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
397 if (ret < 0) {
398 perror("husb: failed to release interface");
399 return 0;
400 }
401 }
402
balrogb9dc0332007-10-04 22:47:34 +0000403 return 1;
404}
405
bellard059809e2006-07-19 18:06:15 +0000406static void usb_host_handle_reset(USBDevice *dev)
bellardbb36d472005-11-05 14:22:28 +0000407{
aliguori446ab122008-09-14 01:06:09 +0000408 USBHostDevice *s = (USBHostDevice *) dev;
aliguori64838172008-08-21 19:31:10 +0000409
410 dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
411
bellardbb36d472005-11-05 14:22:28 +0000412 ioctl(s->fd, USBDEVFS_RESET);
aliguori446ab122008-09-14 01:06:09 +0000413
414 usb_host_claim_interfaces(s, s->configuration);
ths5fafdf22007-09-16 21:08:06 +0000415}
bellardbb36d472005-11-05 14:22:28 +0000416
bellard059809e2006-07-19 18:06:15 +0000417static void usb_host_handle_destroy(USBDevice *dev)
418{
419 USBHostDevice *s = (USBHostDevice *)dev;
420
aliguori24772c12008-08-21 19:31:52 +0000421 s->closing = 1;
422
aliguori64838172008-08-21 19:31:10 +0000423 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
aliguori1f3870a2008-08-21 19:27:48 +0000424
aliguori4b096fc2008-08-21 19:28:55 +0000425 hostdev_unlink(s);
426
aliguori64838172008-08-21 19:31:10 +0000427 async_complete(s);
428
bellard059809e2006-07-19 18:06:15 +0000429 if (s->fd >= 0)
430 close(s->fd);
aliguori1f3870a2008-08-21 19:27:48 +0000431
bellard059809e2006-07-19 18:06:15 +0000432 qemu_free(s);
433}
434
balrogb9dc0332007-10-04 22:47:34 +0000435static int usb_linux_update_endp_table(USBHostDevice *s);
436
aliguori446ab122008-09-14 01:06:09 +0000437static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
bellardbb36d472005-11-05 14:22:28 +0000438{
aliguori64838172008-08-21 19:31:10 +0000439 struct usbdevfs_urb *urb;
aliguori446ab122008-09-14 01:06:09 +0000440 AsyncURB *aurb;
bellardbb36d472005-11-05 14:22:28 +0000441 int ret;
442
aliguori64838172008-08-21 19:31:10 +0000443 aurb = async_alloc();
aliguori64838172008-08-21 19:31:10 +0000444 aurb->hdev = s;
445 aurb->packet = p;
balrogb9dc0332007-10-04 22:47:34 +0000446
aliguori64838172008-08-21 19:31:10 +0000447 urb = &aurb->urb;
448
pbrook4d611c92006-08-12 01:04:27 +0000449 if (p->pid == USB_TOKEN_IN)
aliguori64838172008-08-21 19:31:10 +0000450 urb->endpoint = p->devep | 0x80;
451 else
452 urb->endpoint = p->devep;
453
454 if (is_halted(s, p->devep)) {
455 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
456 if (ret < 0) {
457 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
458 urb->endpoint, errno);
bellardbb36d472005-11-05 14:22:28 +0000459 return USB_RET_NAK;
bellardbb36d472005-11-05 14:22:28 +0000460 }
aliguori64838172008-08-21 19:31:10 +0000461 clear_halt(s, p->devep);
balrog046833e2007-10-31 00:27:50 +0000462 }
463
aliguori64838172008-08-21 19:31:10 +0000464 urb->buffer = p->data;
balrogb9dc0332007-10-04 22:47:34 +0000465 urb->buffer_length = p->len;
aliguori64838172008-08-21 19:31:10 +0000466
467 if (is_isoc(s, p->devep)) {
468 /* Setup ISOC transfer */
469 urb->type = USBDEVFS_URB_TYPE_ISO;
470 urb->flags = USBDEVFS_URB_ISO_ASAP;
471 urb->number_of_packets = 1;
472 urb->iso_frame_desc[0].length = p->len;
balrogb9dc0332007-10-04 22:47:34 +0000473 } else {
aliguori64838172008-08-21 19:31:10 +0000474 /* Setup bulk transfer */
475 urb->type = USBDEVFS_URB_TYPE_BULK;
476 }
477
478 urb->usercontext = s;
479
480 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
481
482 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
483
484 if (ret < 0) {
485 dprintf("husb: submit failed. errno %d\n", errno);
486 async_free(aurb);
487
balrogb9dc0332007-10-04 22:47:34 +0000488 switch(errno) {
489 case ETIMEDOUT:
490 return USB_RET_NAK;
491 case EPIPE:
492 default:
493 return USB_RET_STALL;
494 }
495 }
aliguori64838172008-08-21 19:31:10 +0000496
497 usb_defer_packet(p, async_cancel, aurb);
balrogb9dc0332007-10-04 22:47:34 +0000498 return USB_RET_ASYNC;
balrogb9dc0332007-10-04 22:47:34 +0000499}
500
aliguori446ab122008-09-14 01:06:09 +0000501static int ctrl_error(void)
502{
503 if (errno == ETIMEDOUT)
504 return USB_RET_NAK;
505 else
506 return USB_RET_STALL;
507}
508
509static int usb_host_set_address(USBHostDevice *s, int addr)
510{
511 dprintf("husb: ctrl set addr %u\n", addr);
512 s->dev.addr = addr;
513 return 0;
514}
515
516static int usb_host_set_config(USBHostDevice *s, int config)
517{
518 usb_host_release_interfaces(s);
519
520 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
521
522 dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
523
524 if (ret < 0)
525 return ctrl_error();
526
527 usb_host_claim_interfaces(s, config);
528 return 0;
529}
530
531static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
532{
533 struct usbdevfs_setinterface si;
534 int ret;
535
536 si.interface = iface;
537 si.altsetting = alt;
538 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
539
540 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
541 iface, alt, ret, errno);
542
543 if (ret < 0)
544 return ctrl_error();
545
546 usb_linux_update_endp_table(s);
547 return 0;
548}
549
550static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
551{
552 struct usbdevfs_urb *urb;
553 AsyncURB *aurb;
554 int ret, value, index;
555
556 /*
557 * Process certain standard device requests.
558 * These are infrequent and are processed synchronously.
559 */
560 value = le16_to_cpu(s->ctrl.req.wValue);
561 index = le16_to_cpu(s->ctrl.req.wIndex);
562
563 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
564 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
565 s->ctrl.len);
566
567 if (s->ctrl.req.bRequestType == 0) {
568 switch (s->ctrl.req.bRequest) {
569 case USB_REQ_SET_ADDRESS:
570 return usb_host_set_address(s, value);
571
572 case USB_REQ_SET_CONFIGURATION:
573 return usb_host_set_config(s, value & 0xff);
574 }
575 }
576
577 if (s->ctrl.req.bRequestType == 1 &&
578 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
579 return usb_host_set_interface(s, index, value);
580
581 /* The rest are asynchronous */
582
583 aurb = async_alloc();
aliguori446ab122008-09-14 01:06:09 +0000584 aurb->hdev = s;
585 aurb->packet = p;
586
587 /*
588 * Setup ctrl transfer.
589 *
590 * s->ctrl is layed out such that data buffer immediately follows
591 * 'req' struct which is exactly what usbdevfs expects.
592 */
593 urb = &aurb->urb;
594
595 urb->type = USBDEVFS_URB_TYPE_CONTROL;
596 urb->endpoint = p->devep;
597
598 urb->buffer = &s->ctrl.req;
599 urb->buffer_length = 8 + s->ctrl.len;
600
601 urb->usercontext = s;
602
603 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
604
605 dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
606
607 if (ret < 0) {
608 dprintf("husb: submit failed. errno %d\n", errno);
609 async_free(aurb);
610
611 switch(errno) {
612 case ETIMEDOUT:
613 return USB_RET_NAK;
614 case EPIPE:
615 default:
616 return USB_RET_STALL;
617 }
618 }
619
620 usb_defer_packet(p, async_cancel, aurb);
621 return USB_RET_ASYNC;
622}
623
624static int do_token_setup(USBDevice *dev, USBPacket *p)
625{
626 USBHostDevice *s = (USBHostDevice *) dev;
627 int ret = 0;
628
629 if (p->len != 8)
630 return USB_RET_STALL;
631
632 memcpy(&s->ctrl.req, p->data, 8);
633 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
634 s->ctrl.offset = 0;
635 s->ctrl.state = CTRL_STATE_SETUP;
636
637 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
638 ret = usb_host_handle_control(s, p);
639 if (ret < 0)
640 return ret;
641
642 if (ret < s->ctrl.len)
643 s->ctrl.len = ret;
644 s->ctrl.state = CTRL_STATE_DATA;
645 } else {
646 if (s->ctrl.len == 0)
647 s->ctrl.state = CTRL_STATE_ACK;
648 else
649 s->ctrl.state = CTRL_STATE_DATA;
650 }
651
652 return ret;
653}
654
655static int do_token_in(USBDevice *dev, USBPacket *p)
656{
657 USBHostDevice *s = (USBHostDevice *) dev;
658 int ret = 0;
659
660 if (p->devep != 0)
661 return usb_host_handle_data(s, p);
662
663 switch(s->ctrl.state) {
664 case CTRL_STATE_ACK:
665 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
666 ret = usb_host_handle_control(s, p);
667 if (ret == USB_RET_ASYNC)
668 return USB_RET_ASYNC;
669
670 s->ctrl.state = CTRL_STATE_IDLE;
671 return ret > 0 ? 0 : ret;
672 }
673
674 return 0;
675
676 case CTRL_STATE_DATA:
677 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
678 int len = s->ctrl.len - s->ctrl.offset;
679 if (len > p->len)
680 len = p->len;
681 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
682 s->ctrl.offset += len;
683 if (s->ctrl.offset >= s->ctrl.len)
684 s->ctrl.state = CTRL_STATE_ACK;
685 return len;
686 }
687
688 s->ctrl.state = CTRL_STATE_IDLE;
689 return USB_RET_STALL;
690
691 default:
692 return USB_RET_STALL;
693 }
694}
695
696static int do_token_out(USBDevice *dev, USBPacket *p)
697{
698 USBHostDevice *s = (USBHostDevice *) dev;
699
700 if (p->devep != 0)
701 return usb_host_handle_data(s, p);
702
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;
716 if (len > p->len)
717 len = p->len;
718 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
719 s->ctrl.offset += len;
720 if (s->ctrl.offset >= s->ctrl.len)
721 s->ctrl.state = CTRL_STATE_ACK;
722 return len;
723 }
724
725 s->ctrl.state = CTRL_STATE_IDLE;
726 return USB_RET_STALL;
727
728 default:
729 return USB_RET_STALL;
730 }
731}
732
733/*
734 * Packet handler.
735 * Called by the HC (host controller).
736 *
737 * Returns length of the transaction or one of the USB_RET_XXX codes.
738 */
blueswir1d9cf1572008-09-15 14:57:11 +0000739static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
aliguori446ab122008-09-14 01:06:09 +0000740{
741 switch(p->pid) {
742 case USB_MSG_ATTACH:
743 s->state = USB_STATE_ATTACHED;
744 return 0;
745
746 case USB_MSG_DETACH:
747 s->state = USB_STATE_NOTATTACHED;
748 return 0;
749
750 case USB_MSG_RESET:
751 s->remote_wakeup = 0;
752 s->addr = 0;
753 s->state = USB_STATE_DEFAULT;
754 s->handle_reset(s);
755 return 0;
756 }
757
758 /* Rest of the PIDs must match our address */
759 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
760 return USB_RET_NODEV;
761
762 switch (p->pid) {
763 case USB_TOKEN_SETUP:
764 return do_token_setup(s, p);
765
766 case USB_TOKEN_IN:
767 return do_token_in(s, p);
768
769 case USB_TOKEN_OUT:
770 return do_token_out(s, p);
771
772 default:
773 return USB_RET_STALL;
774 }
775}
776
balrogb9dc0332007-10-04 22:47:34 +0000777/* returns 1 on problem encountered or 0 for success */
778static int usb_linux_update_endp_table(USBHostDevice *s)
779{
780 uint8_t *descriptors;
781 uint8_t devep, type, configuration, alt_interface;
pbrooke41b3912008-10-28 18:22:59 +0000782 struct usb_ctrltransfer ct;
balrogb9dc0332007-10-04 22:47:34 +0000783 int interface, ret, length, i;
784
785 ct.bRequestType = USB_DIR_IN;
786 ct.bRequest = USB_REQ_GET_CONFIGURATION;
787 ct.wValue = 0;
788 ct.wIndex = 0;
789 ct.wLength = 1;
790 ct.data = &configuration;
791 ct.timeout = 50;
792
793 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
794 if (ret < 0) {
795 perror("usb_linux_update_endp_table");
796 return 1;
797 }
798
799 /* in address state */
800 if (configuration == 0)
801 return 1;
802
803 /* get the desired configuration, interface, and endpoint descriptors
804 * from device description */
805 descriptors = &s->descr[18];
806 length = s->descr_len - 18;
807 i = 0;
808
809 if (descriptors[i + 1] != USB_DT_CONFIG ||
810 descriptors[i + 5] != configuration) {
aliguori64838172008-08-21 19:31:10 +0000811 dprintf("invalid descriptor data - configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000812 return 1;
813 }
814 i += descriptors[i];
815
816 while (i < length) {
817 if (descriptors[i + 1] != USB_DT_INTERFACE ||
818 (descriptors[i + 1] == USB_DT_INTERFACE &&
819 descriptors[i + 4] == 0)) {
820 i += descriptors[i];
821 continue;
822 }
823
824 interface = descriptors[i + 2];
825
826 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
827 ct.bRequest = USB_REQ_GET_INTERFACE;
828 ct.wValue = 0;
829 ct.wIndex = interface;
830 ct.wLength = 1;
831 ct.data = &alt_interface;
832 ct.timeout = 50;
833
834 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
835 if (ret < 0) {
Jason Wesseld55ebf52009-05-18 10:00:28 -0500836 alt_interface = interface;
balrogb9dc0332007-10-04 22:47:34 +0000837 }
838
839 /* the current interface descriptor is the active interface
840 * and has endpoints */
841 if (descriptors[i + 3] != alt_interface) {
842 i += descriptors[i];
843 continue;
844 }
845
846 /* advance to the endpoints */
847 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
848 i += descriptors[i];
849
850 if (i >= length)
851 break;
852
853 while (i < length) {
854 if (descriptors[i + 1] != USB_DT_ENDPOINT)
855 break;
856
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;
871 default:
aliguori64838172008-08-21 19:31:10 +0000872 dprintf("usb_host: malformed endpoint type\n");
balrogb9dc0332007-10-04 22:47:34 +0000873 type = USBDEVFS_URB_TYPE_BULK;
874 }
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
aliguori4b096fc2008-08-21 19:28:55 +0000884static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
bellardbb36d472005-11-05 14:22:28 +0000885{
balrogb9dc0332007-10-04 22:47:34 +0000886 int fd = -1, ret;
887 USBHostDevice *dev = NULL;
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
891 dev = qemu_mallocz(sizeof(USBHostDevice));
aliguori1f3870a2008-08-21 19:27:48 +0000892
aliguori4b096fc2008-08-21 19:28:55 +0000893 dev->bus_num = bus_num;
894 dev->addr = addr;
895
aliguori64838172008-08-21 19:31:10 +0000896 printf("husb: open device %d.%d\n", bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000897
aliguori0f431522008-10-07 20:06:37 +0000898 if (!usb_host_device_path) {
899 perror("husb: USB Host Device Path not set");
900 goto fail;
901 }
902 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
bellarda594cfb2005-11-06 16:13:29 +0000903 bus_num, addr);
balrogb9dc0332007-10-04 22:47:34 +0000904 fd = open(buf, O_RDWR | O_NONBLOCK);
bellardbb36d472005-11-05 14:22:28 +0000905 if (fd < 0) {
bellarda594cfb2005-11-06 16:13:29 +0000906 perror(buf);
aliguori1f3870a2008-08-21 19:27:48 +0000907 goto fail;
bellardbb36d472005-11-05 14:22:28 +0000908 }
aliguori0f431522008-10-07 20:06:37 +0000909 dprintf("husb: opened %s\n", buf);
bellardbb36d472005-11-05 14:22:28 +0000910
balrogb9dc0332007-10-04 22:47:34 +0000911 /* read the device description */
912 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
913 if (dev->descr_len <= 0) {
aliguori64838172008-08-21 19:31:10 +0000914 perror("husb: reading device data failed");
bellardbb36d472005-11-05 14:22:28 +0000915 goto fail;
916 }
ths3b46e622007-09-17 08:09:54 +0000917
balrogb9dc0332007-10-04 22:47:34 +0000918#ifdef DEBUG
bellard868bfe22005-11-13 21:53:15 +0000919 {
balrogb9dc0332007-10-04 22:47:34 +0000920 int x;
921 printf("=== begin dumping device descriptor data ===\n");
922 for (x = 0; x < dev->descr_len; x++)
923 printf("%02x ", dev->descr[x]);
924 printf("\n=== end dumping device descriptor data ===\n");
bellarda594cfb2005-11-06 16:13:29 +0000925 }
926#endif
927
balrogb9dc0332007-10-04 22:47:34 +0000928 dev->fd = fd;
balrogb9dc0332007-10-04 22:47:34 +0000929
aliguori446ab122008-09-14 01:06:09 +0000930 /*
931 * Initial configuration is -1 which makes us claim first
932 * available config. We used to start with 1, which does not
933 * always work. I've seen devices where first config starts
934 * with 2.
935 */
936 if (!usb_host_claim_interfaces(dev, -1))
balrogb9dc0332007-10-04 22:47:34 +0000937 goto fail;
bellardbb36d472005-11-05 14:22:28 +0000938
939 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
940 if (ret < 0) {
balrog046833e2007-10-31 00:27:50 +0000941 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
bellardbb36d472005-11-05 14:22:28 +0000942 goto fail;
943 }
944
aliguori64838172008-08-21 19:31:10 +0000945 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
bellardbb36d472005-11-05 14:22:28 +0000946
balrogb9dc0332007-10-04 22:47:34 +0000947 ret = usb_linux_update_endp_table(dev);
948 if (ret)
bellardbb36d472005-11-05 14:22:28 +0000949 goto fail;
balrogb9dc0332007-10-04 22:47:34 +0000950
bellardbb36d472005-11-05 14:22:28 +0000951 if (ci.slow)
952 dev->dev.speed = USB_SPEED_LOW;
953 else
954 dev->dev.speed = USB_SPEED_HIGH;
bellardbb36d472005-11-05 14:22:28 +0000955
aliguori446ab122008-09-14 01:06:09 +0000956 dev->dev.handle_packet = usb_host_handle_packet;
957 dev->dev.handle_reset = usb_host_handle_reset;
bellard059809e2006-07-19 18:06:15 +0000958 dev->dev.handle_destroy = usb_host_handle_destroy;
bellard1f6e24e2006-06-26 21:00:51 +0000959
aliguori4b096fc2008-08-21 19:28:55 +0000960 if (!prod_name || prod_name[0] == '\0')
bellard1f6e24e2006-06-26 21:00:51 +0000961 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
aliguori4b096fc2008-08-21 19:28:55 +0000962 "host:%d.%d", bus_num, addr);
bellard1f6e24e2006-06-26 21:00:51 +0000963 else
964 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
aliguori4b096fc2008-08-21 19:28:55 +0000965 prod_name);
bellard1f6e24e2006-06-26 21:00:51 +0000966
aliguori64838172008-08-21 19:31:10 +0000967 /* USB devio uses 'write' flag to check for async completions */
968 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
aliguori1f3870a2008-08-21 19:27:48 +0000969
aliguori4b096fc2008-08-21 19:28:55 +0000970 hostdev_link(dev);
971
aliguori64838172008-08-21 19:31:10 +0000972 return (USBDevice *) dev;
aliguori4b096fc2008-08-21 19:28:55 +0000973
balrogb9dc0332007-10-04 22:47:34 +0000974fail:
aliguori24772c12008-08-21 19:31:52 +0000975 if (dev)
balrogb9dc0332007-10-04 22:47:34 +0000976 qemu_free(dev);
aliguori24772c12008-08-21 19:31:52 +0000977
balrogb9dc0332007-10-04 22:47:34 +0000978 close(fd);
979 return NULL;
bellardbb36d472005-11-05 14:22:28 +0000980}
981
aliguori5d0c5752008-09-14 01:07:41 +0000982static int usb_host_auto_add(const char *spec);
983static int usb_host_auto_del(const char *spec);
984
aliguori4b096fc2008-08-21 19:28:55 +0000985USBDevice *usb_host_device_open(const char *devname)
986{
aliguori376253e2009-03-05 23:01:23 +0000987 Monitor *mon = cur_mon;
aliguori4b096fc2008-08-21 19:28:55 +0000988 int bus_num, addr;
989 char product_name[PRODUCT_NAME_SZ];
990
aliguori5d0c5752008-09-14 01:07:41 +0000991 if (strstr(devname, "auto:")) {
992 usb_host_auto_add(devname);
993 return NULL;
994 }
995
996 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
aliguori4b096fc2008-08-21 19:28:55 +0000997 devname) < 0)
998 return NULL;
999
aliguori5d0c5752008-09-14 01:07:41 +00001000 if (hostdev_find(bus_num, addr)) {
aliguori376253e2009-03-05 23:01:23 +00001001 monitor_printf(mon, "husb: host usb device %d.%d is already open\n",
1002 bus_num, addr);
aliguori5d0c5752008-09-14 01:07:41 +00001003 return NULL;
1004 }
aliguori4b096fc2008-08-21 19:28:55 +00001005
1006 return usb_host_device_open_addr(bus_num, addr, product_name);
1007}
aliguori5d0c5752008-09-14 01:07:41 +00001008
1009int usb_host_device_close(const char *devname)
1010{
1011 char product_name[PRODUCT_NAME_SZ];
1012 int bus_num, addr;
1013 USBHostDevice *s;
1014
1015 if (strstr(devname, "auto:"))
1016 return usb_host_auto_del(devname);
1017
1018 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1019 devname) < 0)
1020 return -1;
1021
1022 s = hostdev_find(bus_num, addr);
1023 if (s) {
1024 usb_device_del_addr(0, s->dev.addr);
1025 return 0;
1026 }
1027
1028 return -1;
1029}
aliguori4b096fc2008-08-21 19:28:55 +00001030
bellarda594cfb2005-11-06 16:13:29 +00001031static int get_tag_value(char *buf, int buf_size,
ths5fafdf22007-09-16 21:08:06 +00001032 const char *str, const char *tag,
bellarda594cfb2005-11-06 16:13:29 +00001033 const char *stopchars)
bellardbb36d472005-11-05 14:22:28 +00001034{
bellarda594cfb2005-11-06 16:13:29 +00001035 const char *p;
1036 char *q;
1037 p = strstr(str, tag);
1038 if (!p)
1039 return -1;
1040 p += strlen(tag);
blueswir147398b92008-11-22 20:04:24 +00001041 while (qemu_isspace(*p))
bellarda594cfb2005-11-06 16:13:29 +00001042 p++;
1043 q = buf;
1044 while (*p != '\0' && !strchr(stopchars, *p)) {
1045 if ((q - buf) < (buf_size - 1))
1046 *q++ = *p;
1047 p++;
1048 }
1049 *q = '\0';
1050 return q - buf;
1051}
bellardbb36d472005-11-05 14:22:28 +00001052
aliguori0f431522008-10-07 20:06:37 +00001053/*
1054 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1055 * host's USB devices. This is legacy support since many distributions
1056 * are moving to /sys/bus/usb
1057 */
1058static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
bellarda594cfb2005-11-06 16:13:29 +00001059{
aliguori0f431522008-10-07 20:06:37 +00001060 FILE *f = 0;
bellarda594cfb2005-11-06 16:13:29 +00001061 char line[1024];
1062 char buf[1024];
1063 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
bellarda594cfb2005-11-06 16:13:29 +00001064 char product_name[512];
aliguori0f431522008-10-07 20:06:37 +00001065 int ret = 0;
ths3b46e622007-09-17 08:09:54 +00001066
aliguori0f431522008-10-07 20:06:37 +00001067 if (!usb_host_device_path) {
1068 perror("husb: USB Host Device Path not set");
1069 goto the_end;
bellarda594cfb2005-11-06 16:13:29 +00001070 }
aliguori0f431522008-10-07 20:06:37 +00001071 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1072 f = fopen(line, "r");
1073 if (!f) {
1074 perror("husb: cannot open devices file");
1075 goto the_end;
1076 }
1077
bellarda594cfb2005-11-06 16:13:29 +00001078 device_count = 0;
1079 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
bellardbb36d472005-11-05 14:22:28 +00001080 for(;;) {
bellarda594cfb2005-11-06 16:13:29 +00001081 if (fgets(line, sizeof(line), f) == NULL)
bellardbb36d472005-11-05 14:22:28 +00001082 break;
bellarda594cfb2005-11-06 16:13:29 +00001083 if (strlen(line) > 0)
1084 line[strlen(line) - 1] = '\0';
1085 if (line[0] == 'T' && line[1] == ':') {
pbrook38ca0f62006-03-11 18:03:38 +00001086 if (device_count && (vendor_id || product_id)) {
1087 /* New device. Add the previously discovered device. */
ths5fafdf22007-09-16 21:08:06 +00001088 ret = func(opaque, bus_num, addr, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001089 product_id, product_name, speed);
1090 if (ret)
1091 goto the_end;
1092 }
1093 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
1094 goto fail;
1095 bus_num = atoi(buf);
1096 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
1097 goto fail;
1098 addr = atoi(buf);
1099 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
1100 goto fail;
1101 if (!strcmp(buf, "480"))
1102 speed = USB_SPEED_HIGH;
1103 else if (!strcmp(buf, "1.5"))
1104 speed = USB_SPEED_LOW;
1105 else
1106 speed = USB_SPEED_FULL;
1107 product_name[0] = '\0';
1108 class_id = 0xff;
1109 device_count++;
1110 product_id = 0;
1111 vendor_id = 0;
1112 } else if (line[0] == 'P' && line[1] == ':') {
1113 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
1114 goto fail;
1115 vendor_id = strtoul(buf, NULL, 16);
1116 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
1117 goto fail;
1118 product_id = strtoul(buf, NULL, 16);
1119 } else if (line[0] == 'S' && line[1] == ':') {
1120 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
1121 goto fail;
1122 pstrcpy(product_name, sizeof(product_name), buf);
1123 } else if (line[0] == 'D' && line[1] == ':') {
1124 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
1125 goto fail;
1126 class_id = strtoul(buf, NULL, 16);
1127 }
1128 fail: ;
1129 }
pbrook38ca0f62006-03-11 18:03:38 +00001130 if (device_count && (vendor_id || product_id)) {
1131 /* Add the last device. */
ths5fafdf22007-09-16 21:08:06 +00001132 ret = func(opaque, bus_num, addr, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001133 product_id, product_name, speed);
1134 }
1135 the_end:
aliguori0f431522008-10-07 20:06:37 +00001136 if (f)
1137 fclose(f);
1138 return ret;
1139}
1140
1141/*
1142 * Read sys file-system device file
1143 *
1144 * @line address of buffer to put file contents in
1145 * @line_size size of line
1146 * @device_file path to device file (printf format string)
1147 * @device_name device being opened (inserted into device_file)
1148 *
1149 * @return 0 failed, 1 succeeded ('line' contains data)
1150 */
1151static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
1152{
aliguori376253e2009-03-05 23:01:23 +00001153 Monitor *mon = cur_mon;
aliguori0f431522008-10-07 20:06:37 +00001154 FILE *f;
1155 int ret = 0;
1156 char filename[PATH_MAX];
1157
blueswir1b4e237a2008-12-28 15:45:20 +00001158 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1159 device_file);
aliguori0f431522008-10-07 20:06:37 +00001160 f = fopen(filename, "r");
1161 if (f) {
1162 fgets(line, line_size, f);
1163 fclose(f);
1164 ret = 1;
1165 } else {
aliguori376253e2009-03-05 23:01:23 +00001166 monitor_printf(mon, "husb: could not open %s\n", filename);
aliguori0f431522008-10-07 20:06:37 +00001167 }
1168
1169 return ret;
1170}
1171
1172/*
1173 * Use /sys/bus/usb/devices/ directory to determine host's USB
1174 * devices.
1175 *
1176 * This code is based on Robert Schiele's original patches posted to
1177 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1178 */
1179static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1180{
1181 DIR *dir = 0;
1182 char line[1024];
1183 int bus_num, addr, speed, class_id, product_id, vendor_id;
1184 int ret = 0;
1185 char product_name[512];
1186 struct dirent *de;
1187
1188 dir = opendir(USBSYSBUS_PATH "/devices");
1189 if (!dir) {
1190 perror("husb: cannot open devices directory");
1191 goto the_end;
1192 }
1193
1194 while ((de = readdir(dir))) {
1195 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1196 char *tmpstr = de->d_name;
1197 if (!strncmp(de->d_name, "usb", 3))
1198 tmpstr += 3;
1199 bus_num = atoi(tmpstr);
1200
blueswir1b4e237a2008-12-28 15:45:20 +00001201 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001202 goto the_end;
1203 if (sscanf(line, "%d", &addr) != 1)
1204 goto the_end;
1205
blueswir1b4e237a2008-12-28 15:45:20 +00001206 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1207 de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001208 goto the_end;
1209 if (sscanf(line, "%x", &class_id) != 1)
1210 goto the_end;
1211
blueswir1b4e237a2008-12-28 15:45:20 +00001212 if (!usb_host_read_file(line, sizeof(line), "idVendor", de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001213 goto the_end;
1214 if (sscanf(line, "%x", &vendor_id) != 1)
1215 goto the_end;
1216
blueswir1b4e237a2008-12-28 15:45:20 +00001217 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1218 de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001219 goto the_end;
1220 if (sscanf(line, "%x", &product_id) != 1)
1221 goto the_end;
1222
blueswir1b4e237a2008-12-28 15:45:20 +00001223 if (!usb_host_read_file(line, sizeof(line), "product",
1224 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001225 *product_name = 0;
1226 } else {
1227 if (strlen(line) > 0)
1228 line[strlen(line) - 1] = '\0';
1229 pstrcpy(product_name, sizeof(product_name), line);
1230 }
1231
blueswir1b4e237a2008-12-28 15:45:20 +00001232 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001233 goto the_end;
1234 if (!strcmp(line, "480\n"))
1235 speed = USB_SPEED_HIGH;
1236 else if (!strcmp(line, "1.5\n"))
1237 speed = USB_SPEED_LOW;
1238 else
1239 speed = USB_SPEED_FULL;
1240
1241 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1242 product_id, product_name, speed);
1243 if (ret)
1244 goto the_end;
1245 }
1246 }
1247 the_end:
1248 if (dir)
1249 closedir(dir);
1250 return ret;
1251}
1252
1253/*
1254 * Determine how to access the host's USB devices and call the
1255 * specific support function.
1256 */
1257static int usb_host_scan(void *opaque, USBScanFunc *func)
1258{
aliguori376253e2009-03-05 23:01:23 +00001259 Monitor *mon = cur_mon;
aliguori0f431522008-10-07 20:06:37 +00001260 FILE *f = 0;
1261 DIR *dir = 0;
1262 int ret = 0;
aliguori0f431522008-10-07 20:06:37 +00001263 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1264 char devpath[PATH_MAX];
1265
1266 /* only check the host once */
1267 if (!usb_fs_type) {
1268 f = fopen(USBPROCBUS_PATH "/devices", "r");
1269 if (f) {
1270 /* devices found in /proc/bus/usb/ */
1271 strcpy(devpath, USBPROCBUS_PATH);
1272 usb_fs_type = USB_FS_PROC;
1273 fclose(f);
blueswir15be8e1f2008-10-25 11:47:20 +00001274 dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001275 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001276 }
1277 /* try additional methods if an access method hasn't been found yet */
1278 f = fopen(USBDEVBUS_PATH "/devices", "r");
aliguorif16a0db2008-10-21 16:34:20 +00001279 if (f) {
aliguori0f431522008-10-07 20:06:37 +00001280 /* devices found in /dev/bus/usb/ */
1281 strcpy(devpath, USBDEVBUS_PATH);
1282 usb_fs_type = USB_FS_DEV;
1283 fclose(f);
blueswir15be8e1f2008-10-25 11:47:20 +00001284 dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001285 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001286 }
1287 dir = opendir(USBSYSBUS_PATH "/devices");
aliguorif16a0db2008-10-21 16:34:20 +00001288 if (dir) {
aliguori0f431522008-10-07 20:06:37 +00001289 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1290 strcpy(devpath, USBDEVBUS_PATH);
1291 usb_fs_type = USB_FS_SYS;
1292 closedir(dir);
blueswir15be8e1f2008-10-25 11:47:20 +00001293 dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001294 goto found_devices;
aliguori22babeb2008-10-21 16:27:28 +00001295 }
aliguorif16a0db2008-10-21 16:34:20 +00001296 found_devices:
aliguori22babeb2008-10-21 16:27:28 +00001297 if (!usb_fs_type) {
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, "husb: unable to access USB devices\n");
aliguorif16a0db2008-10-21 16:34:20 +00001299 return -ENOENT;
aliguori0f431522008-10-07 20:06:37 +00001300 }
1301
1302 /* the module setting (used later for opening devices) */
1303 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
aliguori1eec6142009-02-05 22:06:18 +00001304 strcpy(usb_host_device_path, devpath);
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, "husb: using %s file-system with %s\n",
1306 fs_type[usb_fs_type], usb_host_device_path);
aliguori0f431522008-10-07 20:06:37 +00001307 }
1308
1309 switch (usb_fs_type) {
1310 case USB_FS_PROC:
1311 case USB_FS_DEV:
1312 ret = usb_host_scan_dev(opaque, func);
1313 break;
1314 case USB_FS_SYS:
1315 ret = usb_host_scan_sys(opaque, func);
1316 break;
aliguorif16a0db2008-10-21 16:34:20 +00001317 default:
1318 ret = -EINVAL;
1319 break;
aliguori0f431522008-10-07 20:06:37 +00001320 }
bellarda594cfb2005-11-06 16:13:29 +00001321 return ret;
1322}
1323
aliguori4b096fc2008-08-21 19:28:55 +00001324struct USBAutoFilter {
1325 struct USBAutoFilter *next;
1326 int bus_num;
1327 int addr;
1328 int vendor_id;
1329 int product_id;
1330};
1331
1332static QEMUTimer *usb_auto_timer;
1333static struct USBAutoFilter *usb_auto_filter;
1334
1335static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1336 int class_id, int vendor_id, int product_id,
1337 const char *product_name, int speed)
1338{
1339 struct USBAutoFilter *f;
1340 struct USBDevice *dev;
1341
1342 /* Ignore hubs */
1343 if (class_id == 9)
1344 return 0;
1345
1346 for (f = usb_auto_filter; f; f = f->next) {
aliguori4b096fc2008-08-21 19:28:55 +00001347 if (f->bus_num >= 0 && f->bus_num != bus_num)
1348 continue;
1349
1350 if (f->addr >= 0 && f->addr != addr)
1351 continue;
1352
1353 if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
1354 continue;
1355
1356 if (f->product_id >= 0 && f->product_id != product_id)
1357 continue;
1358
1359 /* We got a match */
1360
1361 /* Allredy attached ? */
1362 if (hostdev_find(bus_num, addr))
1363 return 0;
1364
aliguori64838172008-08-21 19:31:10 +00001365 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
aliguori4b096fc2008-08-21 19:28:55 +00001366
1367 dev = usb_host_device_open_addr(bus_num, addr, product_name);
1368 if (dev)
1369 usb_device_add_dev(dev);
1370 }
1371
1372 return 0;
1373}
1374
1375static void usb_host_auto_timer(void *unused)
1376{
1377 usb_host_scan(NULL, usb_host_auto_scan);
1378 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1379}
1380
1381/*
aliguori5d0c5752008-09-14 01:07:41 +00001382 * Autoconnect filter
1383 * Format:
1384 * auto:bus:dev[:vid:pid]
1385 * auto:bus.dev[:vid:pid]
1386 *
1387 * bus - bus number (dec, * means any)
1388 * dev - device number (dec, * means any)
1389 * vid - vendor id (hex, * means any)
1390 * pid - product id (hex, * means any)
1391 *
1392 * See 'lsusb' output.
aliguori4b096fc2008-08-21 19:28:55 +00001393 */
aliguori5d0c5752008-09-14 01:07:41 +00001394static int parse_filter(const char *spec, struct USBAutoFilter *f)
aliguori4b096fc2008-08-21 19:28:55 +00001395{
aliguori5d0c5752008-09-14 01:07:41 +00001396 enum { BUS, DEV, VID, PID, DONE };
1397 const char *p = spec;
1398 int i;
1399
1400 f->bus_num = -1;
1401 f->addr = -1;
1402 f->vendor_id = -1;
1403 f->product_id = -1;
1404
1405 for (i = BUS; i < DONE; i++) {
1406 p = strpbrk(p, ":.");
1407 if (!p) break;
1408 p++;
1409
1410 if (*p == '*')
1411 continue;
1412
1413 switch(i) {
1414 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1415 case DEV: f->addr = strtol(p, NULL, 10); break;
1416 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1417 case PID: f->product_id = strtol(p, NULL, 16); break;
1418 }
aliguori4b096fc2008-08-21 19:28:55 +00001419 }
1420
aliguori5d0c5752008-09-14 01:07:41 +00001421 if (i < DEV) {
1422 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1423 return -1;
1424 }
1425
1426 return 0;
1427}
1428
1429static int match_filter(const struct USBAutoFilter *f1,
1430 const struct USBAutoFilter *f2)
1431{
1432 return f1->bus_num == f2->bus_num &&
1433 f1->addr == f2->addr &&
1434 f1->vendor_id == f2->vendor_id &&
1435 f1->product_id == f2->product_id;
1436}
1437
1438static int usb_host_auto_add(const char *spec)
1439{
1440 struct USBAutoFilter filter, *f;
1441
1442 if (parse_filter(spec, &filter) < 0)
1443 return -1;
1444
1445 f = qemu_mallocz(sizeof(*f));
aliguori5d0c5752008-09-14 01:07:41 +00001446
1447 *f = filter;
aliguori4b096fc2008-08-21 19:28:55 +00001448
1449 if (!usb_auto_filter) {
1450 /*
1451 * First entry. Init and start the monitor.
1452 * Right now we're using timer to check for new devices.
1453 * If this turns out to be too expensive we can move that into a
1454 * separate thread.
1455 */
1456 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
1457 if (!usb_auto_timer) {
aliguori0d380642008-08-21 19:32:29 +00001458 fprintf(stderr, "husb: failed to allocate auto scan timer\n");
aliguori4b096fc2008-08-21 19:28:55 +00001459 qemu_free(f);
aliguori5d0c5752008-09-14 01:07:41 +00001460 return -1;
aliguori4b096fc2008-08-21 19:28:55 +00001461 }
1462
1463 /* Check for new devices every two seconds */
1464 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1465 }
1466
aliguori5d0c5752008-09-14 01:07:41 +00001467 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1468 f->bus_num, f->addr, f->vendor_id, f->product_id);
aliguori4b096fc2008-08-21 19:28:55 +00001469
1470 f->next = usb_auto_filter;
1471 usb_auto_filter = f;
aliguori5d0c5752008-09-14 01:07:41 +00001472
1473 return 0;
1474}
1475
1476static int usb_host_auto_del(const char *spec)
1477{
1478 struct USBAutoFilter *pf = usb_auto_filter;
1479 struct USBAutoFilter **prev = &usb_auto_filter;
1480 struct USBAutoFilter filter;
1481
1482 if (parse_filter(spec, &filter) < 0)
1483 return -1;
1484
1485 while (pf) {
1486 if (match_filter(pf, &filter)) {
1487 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1488 pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
1489
1490 *prev = pf->next;
1491
1492 if (!usb_auto_filter) {
1493 /* No more filters. Stop scanning. */
1494 qemu_del_timer(usb_auto_timer);
1495 qemu_free_timer(usb_auto_timer);
1496 }
1497
1498 return 0;
1499 }
1500
1501 prev = &pf->next;
1502 pf = pf->next;
1503 }
1504
1505 return -1;
aliguori4b096fc2008-08-21 19:28:55 +00001506}
1507
bellarda594cfb2005-11-06 16:13:29 +00001508typedef struct FindDeviceState {
1509 int vendor_id;
1510 int product_id;
1511 int bus_num;
1512 int addr;
bellard1f6e24e2006-06-26 21:00:51 +00001513 char product_name[PRODUCT_NAME_SZ];
bellarda594cfb2005-11-06 16:13:29 +00001514} FindDeviceState;
1515
ths5fafdf22007-09-16 21:08:06 +00001516static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
bellarda594cfb2005-11-06 16:13:29 +00001517 int class_id,
ths5fafdf22007-09-16 21:08:06 +00001518 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +00001519 const char *product_name, int speed)
1520{
1521 FindDeviceState *s = opaque;
bellard1f6e24e2006-06-26 21:00:51 +00001522 if ((vendor_id == s->vendor_id &&
1523 product_id == s->product_id) ||
1524 (bus_num == s->bus_num &&
1525 addr == s->addr)) {
1526 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
bellarda594cfb2005-11-06 16:13:29 +00001527 s->bus_num = bus_num;
1528 s->addr = addr;
1529 return 1;
1530 } else {
1531 return 0;
1532 }
1533}
1534
ths5fafdf22007-09-16 21:08:06 +00001535/* the syntax is :
1536 'bus.addr' (decimal numbers) or
bellarda594cfb2005-11-06 16:13:29 +00001537 'vendor_id:product_id' (hexa numbers) */
ths5fafdf22007-09-16 21:08:06 +00001538static int usb_host_find_device(int *pbus_num, int *paddr,
bellard1f6e24e2006-06-26 21:00:51 +00001539 char *product_name, int product_name_size,
bellarda594cfb2005-11-06 16:13:29 +00001540 const char *devname)
1541{
1542 const char *p;
1543 int ret;
1544 FindDeviceState fs;
1545
1546 p = strchr(devname, '.');
1547 if (p) {
1548 *pbus_num = strtoul(devname, NULL, 0);
1549 *paddr = strtoul(p + 1, NULL, 0);
bellard1f6e24e2006-06-26 21:00:51 +00001550 fs.bus_num = *pbus_num;
1551 fs.addr = *paddr;
1552 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1553 if (ret)
1554 pstrcpy(product_name, product_name_size, fs.product_name);
bellarda594cfb2005-11-06 16:13:29 +00001555 return 0;
1556 }
aliguori5d0c5752008-09-14 01:07:41 +00001557
bellarda594cfb2005-11-06 16:13:29 +00001558 p = strchr(devname, ':');
1559 if (p) {
1560 fs.vendor_id = strtoul(devname, NULL, 16);
1561 fs.product_id = strtoul(p + 1, NULL, 16);
1562 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1563 if (ret) {
1564 *pbus_num = fs.bus_num;
1565 *paddr = fs.addr;
bellard1f6e24e2006-06-26 21:00:51 +00001566 pstrcpy(product_name, product_name_size, fs.product_name);
bellarda594cfb2005-11-06 16:13:29 +00001567 return 0;
bellardbb36d472005-11-05 14:22:28 +00001568 }
1569 }
bellarda594cfb2005-11-06 16:13:29 +00001570 return -1;
bellardbb36d472005-11-05 14:22:28 +00001571}
1572
bellarda594cfb2005-11-06 16:13:29 +00001573/**********************/
1574/* USB host device info */
bellardbb36d472005-11-05 14:22:28 +00001575
bellarda594cfb2005-11-06 16:13:29 +00001576struct usb_class_info {
1577 int class;
1578 const char *class_name;
1579};
1580
1581static const struct usb_class_info usb_class_info[] = {
1582 { USB_CLASS_AUDIO, "Audio"},
1583 { USB_CLASS_COMM, "Communication"},
1584 { USB_CLASS_HID, "HID"},
1585 { USB_CLASS_HUB, "Hub" },
1586 { USB_CLASS_PHYSICAL, "Physical" },
1587 { USB_CLASS_PRINTER, "Printer" },
1588 { USB_CLASS_MASS_STORAGE, "Storage" },
1589 { USB_CLASS_CDC_DATA, "Data" },
1590 { USB_CLASS_APP_SPEC, "Application Specific" },
1591 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1592 { USB_CLASS_STILL_IMAGE, "Still Image" },
balrogb9dc0332007-10-04 22:47:34 +00001593 { USB_CLASS_CSCID, "Smart Card" },
bellarda594cfb2005-11-06 16:13:29 +00001594 { USB_CLASS_CONTENT_SEC, "Content Security" },
1595 { -1, NULL }
1596};
1597
1598static const char *usb_class_str(uint8_t class)
1599{
1600 const struct usb_class_info *p;
1601 for(p = usb_class_info; p->class != -1; p++) {
1602 if (p->class == class)
bellardbb36d472005-11-05 14:22:28 +00001603 break;
bellardbb36d472005-11-05 14:22:28 +00001604 }
bellarda594cfb2005-11-06 16:13:29 +00001605 return p->class_name;
bellardbb36d472005-11-05 14:22:28 +00001606}
1607
pbrook9596ebb2007-11-18 01:44:38 +00001608static void usb_info_device(int bus_num, int addr, int class_id,
1609 int vendor_id, int product_id,
1610 const char *product_name,
1611 int speed)
bellardbb36d472005-11-05 14:22:28 +00001612{
aliguori376253e2009-03-05 23:01:23 +00001613 Monitor *mon = cur_mon;
bellarda594cfb2005-11-06 16:13:29 +00001614 const char *class_str, *speed_str;
1615
1616 switch(speed) {
ths5fafdf22007-09-16 21:08:06 +00001617 case USB_SPEED_LOW:
1618 speed_str = "1.5";
bellarda594cfb2005-11-06 16:13:29 +00001619 break;
ths5fafdf22007-09-16 21:08:06 +00001620 case USB_SPEED_FULL:
1621 speed_str = "12";
bellarda594cfb2005-11-06 16:13:29 +00001622 break;
ths5fafdf22007-09-16 21:08:06 +00001623 case USB_SPEED_HIGH:
1624 speed_str = "480";
bellarda594cfb2005-11-06 16:13:29 +00001625 break;
1626 default:
ths5fafdf22007-09-16 21:08:06 +00001627 speed_str = "?";
bellarda594cfb2005-11-06 16:13:29 +00001628 break;
bellardbb36d472005-11-05 14:22:28 +00001629 }
bellarda594cfb2005-11-06 16:13:29 +00001630
aliguori376253e2009-03-05 23:01:23 +00001631 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
bellarda594cfb2005-11-06 16:13:29 +00001632 bus_num, addr, speed_str);
1633 class_str = usb_class_str(class_id);
ths5fafdf22007-09-16 21:08:06 +00001634 if (class_str)
aliguori376253e2009-03-05 23:01:23 +00001635 monitor_printf(mon, " %s:", class_str);
bellarda594cfb2005-11-06 16:13:29 +00001636 else
aliguori376253e2009-03-05 23:01:23 +00001637 monitor_printf(mon, " Class %02x:", class_id);
1638 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
bellarda594cfb2005-11-06 16:13:29 +00001639 if (product_name[0] != '\0')
aliguori376253e2009-03-05 23:01:23 +00001640 monitor_printf(mon, ", %s", product_name);
1641 monitor_printf(mon, "\n");
bellarda594cfb2005-11-06 16:13:29 +00001642}
1643
ths5fafdf22007-09-16 21:08:06 +00001644static int usb_host_info_device(void *opaque, int bus_num, int addr,
bellarda594cfb2005-11-06 16:13:29 +00001645 int class_id,
ths5fafdf22007-09-16 21:08:06 +00001646 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +00001647 const char *product_name,
1648 int speed)
1649{
1650 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
1651 product_name, speed);
1652 return 0;
1653}
1654
aliguoriac4ffb52008-09-22 15:04:31 +00001655static void dec2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001656{
1657 if (val == -1)
aliguoriac4ffb52008-09-22 15:04:31 +00001658 snprintf(str, size, "*");
aliguori5d0c5752008-09-14 01:07:41 +00001659 else
aliguoriac4ffb52008-09-22 15:04:31 +00001660 snprintf(str, size, "%d", val);
aliguori5d0c5752008-09-14 01:07:41 +00001661}
1662
aliguoriac4ffb52008-09-22 15:04:31 +00001663static void hex2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001664{
1665 if (val == -1)
aliguoriac4ffb52008-09-22 15:04:31 +00001666 snprintf(str, size, "*");
aliguori5d0c5752008-09-14 01:07:41 +00001667 else
aliguoriac4ffb52008-09-22 15:04:31 +00001668 snprintf(str, size, "%x", val);
aliguori5d0c5752008-09-14 01:07:41 +00001669}
1670
aliguori376253e2009-03-05 23:01:23 +00001671void usb_host_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00001672{
aliguori5d0c5752008-09-14 01:07:41 +00001673 struct USBAutoFilter *f;
1674
bellarda594cfb2005-11-06 16:13:29 +00001675 usb_host_scan(NULL, usb_host_info_device);
aliguori5d0c5752008-09-14 01:07:41 +00001676
1677 if (usb_auto_filter)
aliguori376253e2009-03-05 23:01:23 +00001678 monitor_printf(mon, " Auto filters:\n");
aliguori5d0c5752008-09-14 01:07:41 +00001679 for (f = usb_auto_filter; f; f = f->next) {
1680 char bus[10], addr[10], vid[10], pid[10];
aliguoriac4ffb52008-09-22 15:04:31 +00001681 dec2str(f->bus_num, bus, sizeof(bus));
1682 dec2str(f->addr, addr, sizeof(addr));
1683 hex2str(f->vendor_id, vid, sizeof(vid));
1684 hex2str(f->product_id, pid, sizeof(pid));
aliguori376253e2009-03-05 23:01:23 +00001685 monitor_printf(mon, " Device %s.%s ID %s:%s\n",
1686 bus, addr, vid, pid);
aliguori5d0c5752008-09-14 01:07:41 +00001687 }
bellardbb36d472005-11-05 14:22:28 +00001688}