blob: eeefc84563efa0e39b41c1bdb588ada71e33a711 [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"
pbrook87ecb682007-11-17 17:14:51 +000035#include "console.h"
bellardbb36d472005-11-05 14:22:28 +000036
37#if defined(__linux__)
38#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
bellarda594cfb2005-11-06 16:13:29 +000065typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
ths5fafdf22007-09-16 21:08:06 +000066 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +000067 const char *product_name, int speed);
ths5fafdf22007-09-16 21:08:06 +000068static int usb_host_find_device(int *pbus_num, int *paddr,
bellard1f6e24e2006-06-26 21:00:51 +000069 char *product_name, int product_name_size,
bellarda594cfb2005-11-06 16:13:29 +000070 const char *devname);
bellarda594cfb2005-11-06 16:13:29 +000071//#define DEBUG
aliguori64838172008-08-21 19:31:10 +000072
73#ifdef DEBUG
74#define dprintf printf
75#else
76#define dprintf(...)
77#endif
bellardbb36d472005-11-05 14:22:28 +000078
blueswir15be8e1f2008-10-25 11:47:20 +000079#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
80
aliguori0f431522008-10-07 20:06:37 +000081#define USBPROCBUS_PATH "/proc/bus/usb"
bellard1f6e24e2006-06-26 21:00:51 +000082#define PRODUCT_NAME_SZ 32
balrogb9dc0332007-10-04 22:47:34 +000083#define MAX_ENDPOINTS 16
aliguori0f431522008-10-07 20:06:37 +000084#define USBDEVBUS_PATH "/dev/bus/usb"
85#define USBSYSBUS_PATH "/sys/bus/usb"
86
87static char *usb_host_device_path;
88
89#define USB_FS_NONE 0
90#define USB_FS_PROC 1
91#define USB_FS_DEV 2
92#define USB_FS_SYS 3
93
94static int usb_fs_type;
bellardbb36d472005-11-05 14:22:28 +000095
balrogb9dc0332007-10-04 22:47:34 +000096/* endpoint association data */
97struct endp_data {
98 uint8_t type;
aliguori64838172008-08-21 19:31:10 +000099 uint8_t halted;
balrogb9dc0332007-10-04 22:47:34 +0000100};
101
aliguori446ab122008-09-14 01:06:09 +0000102enum {
103 CTRL_STATE_IDLE = 0,
104 CTRL_STATE_SETUP,
105 CTRL_STATE_DATA,
106 CTRL_STATE_ACK
107};
108
109/*
110 * Control transfer state.
111 * Note that 'buffer' _must_ follow 'req' field because
112 * we need contigious buffer when we submit control URB.
113 */
114struct ctrl_struct {
115 uint16_t len;
116 uint16_t offset;
117 uint8_t state;
118 struct usb_ctrlrequest req;
119 uint8_t buffer[1024];
120};
121
bellardbb36d472005-11-05 14:22:28 +0000122typedef struct USBHostDevice {
123 USBDevice dev;
aliguori64838172008-08-21 19:31:10 +0000124 int fd;
125
126 uint8_t descr[1024];
127 int descr_len;
128 int configuration;
aliguori446ab122008-09-14 01:06:09 +0000129 int ninterfaces;
aliguori24772c12008-08-21 19:31:52 +0000130 int closing;
aliguori64838172008-08-21 19:31:10 +0000131
aliguori446ab122008-09-14 01:06:09 +0000132 struct ctrl_struct ctrl;
balrogb9dc0332007-10-04 22:47:34 +0000133 struct endp_data endp_table[MAX_ENDPOINTS];
aliguori4b096fc2008-08-21 19:28:55 +0000134
aliguori4b096fc2008-08-21 19:28:55 +0000135 /* Host side address */
136 int bus_num;
137 int addr;
138
139 struct USBHostDevice *next;
bellardbb36d472005-11-05 14:22:28 +0000140} USBHostDevice;
141
aliguori64838172008-08-21 19:31:10 +0000142static int is_isoc(USBHostDevice *s, int ep)
143{
144 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
145}
146
147static int is_halted(USBHostDevice *s, int ep)
148{
149 return s->endp_table[ep - 1].halted;
150}
151
152static void clear_halt(USBHostDevice *s, int ep)
153{
154 s->endp_table[ep - 1].halted = 0;
155}
156
157static void set_halt(USBHostDevice *s, int ep)
158{
159 s->endp_table[ep - 1].halted = 1;
160}
161
aliguori4b096fc2008-08-21 19:28:55 +0000162static USBHostDevice *hostdev_list;
163
164static void hostdev_link(USBHostDevice *dev)
165{
166 dev->next = hostdev_list;
167 hostdev_list = dev;
168}
169
170static void hostdev_unlink(USBHostDevice *dev)
171{
172 USBHostDevice *pdev = hostdev_list;
173 USBHostDevice **prev = &hostdev_list;
174
175 while (pdev) {
176 if (pdev == dev) {
177 *prev = dev->next;
178 return;
179 }
180
181 prev = &pdev->next;
182 pdev = pdev->next;
183 }
184}
185
186static USBHostDevice *hostdev_find(int bus_num, int addr)
187{
188 USBHostDevice *s = hostdev_list;
189 while (s) {
190 if (s->bus_num == bus_num && s->addr == addr)
191 return s;
192 s = s->next;
193 }
194 return NULL;
195}
196
aliguori64838172008-08-21 19:31:10 +0000197/*
198 * Async URB state.
199 * We always allocate one isoc descriptor even for bulk transfers
200 * to simplify allocation and casts.
201 */
202typedef struct AsyncURB
balrogb9dc0332007-10-04 22:47:34 +0000203{
aliguori64838172008-08-21 19:31:10 +0000204 struct usbdevfs_urb urb;
205 struct usbdevfs_iso_packet_desc isocpd;
206
207 USBPacket *packet;
208 USBHostDevice *hdev;
209} AsyncURB;
210
211static AsyncURB *async_alloc(void)
212{
213 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
balrogb9dc0332007-10-04 22:47:34 +0000214}
215
aliguori64838172008-08-21 19:31:10 +0000216static void async_free(AsyncURB *aurb)
balrogb9dc0332007-10-04 22:47:34 +0000217{
aliguori64838172008-08-21 19:31:10 +0000218 qemu_free(aurb);
219}
balrogb9dc0332007-10-04 22:47:34 +0000220
aliguori446ab122008-09-14 01:06:09 +0000221static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
222{
223 switch(s->ctrl.state) {
224 case CTRL_STATE_SETUP:
225 if (p->len < s->ctrl.len)
226 s->ctrl.len = p->len;
227 s->ctrl.state = CTRL_STATE_DATA;
228 p->len = 8;
229 break;
230
231 case CTRL_STATE_ACK:
232 s->ctrl.state = CTRL_STATE_IDLE;
233 p->len = 0;
234 break;
235
236 default:
237 break;
238 }
239}
240
aliguori64838172008-08-21 19:31:10 +0000241static void async_complete(void *opaque)
242{
243 USBHostDevice *s = opaque;
244 AsyncURB *aurb;
balrogb9dc0332007-10-04 22:47:34 +0000245
aliguori64838172008-08-21 19:31:10 +0000246 while (1) {
247 USBPacket *p;
248
249 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
250 if (r < 0) {
251 if (errno == EAGAIN)
252 return;
253
aliguori24772c12008-08-21 19:31:52 +0000254 if (errno == ENODEV && !s->closing) {
aliguori64838172008-08-21 19:31:10 +0000255 printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
256 usb_device_del_addr(0, s->dev.addr);
257 return;
258 }
259
260 dprintf("husb: async. reap urb failed errno %d\n", errno);
261 return;
balrogb9dc0332007-10-04 22:47:34 +0000262 }
aliguori64838172008-08-21 19:31:10 +0000263
264 p = aurb->packet;
265
266 dprintf("husb: async completed. aurb %p status %d alen %d\n",
267 aurb, aurb->urb.status, aurb->urb.actual_length);
268
269 if (p) {
270 switch (aurb->urb.status) {
271 case 0:
272 p->len = aurb->urb.actual_length;
aliguori446ab122008-09-14 01:06:09 +0000273 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
274 async_complete_ctrl(s, p);
aliguori64838172008-08-21 19:31:10 +0000275 break;
276
277 case -EPIPE:
278 set_halt(s, p->devep);
279 /* fall through */
280 default:
281 p->len = USB_RET_NAK;
282 break;
283 }
284
285 usb_packet_complete(p);
286 }
287
288 async_free(aurb);
balrogb9dc0332007-10-04 22:47:34 +0000289 }
balrogb9dc0332007-10-04 22:47:34 +0000290}
291
aliguori64838172008-08-21 19:31:10 +0000292static void async_cancel(USBPacket *unused, void *opaque)
balrogb9dc0332007-10-04 22:47:34 +0000293{
aliguori64838172008-08-21 19:31:10 +0000294 AsyncURB *aurb = opaque;
295 USBHostDevice *s = aurb->hdev;
balrogb9dc0332007-10-04 22:47:34 +0000296
aliguori64838172008-08-21 19:31:10 +0000297 dprintf("husb: async cancel. aurb %p\n", aurb);
balrogb9dc0332007-10-04 22:47:34 +0000298
aliguori64838172008-08-21 19:31:10 +0000299 /* Mark it as dead (see async_complete above) */
300 aurb->packet = NULL;
301
302 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
303 if (r < 0) {
304 dprintf("husb: async. discard urb failed errno %d\n", errno);
balrogb9dc0332007-10-04 22:47:34 +0000305 }
balrogb9dc0332007-10-04 22:47:34 +0000306}
307
aliguori446ab122008-09-14 01:06:09 +0000308static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
balrogb9dc0332007-10-04 22:47:34 +0000309{
310 int dev_descr_len, config_descr_len;
311 int interface, nb_interfaces, nb_configurations;
312 int ret, i;
313
314 if (configuration == 0) /* address state - ignore */
315 return 1;
316
aliguori446ab122008-09-14 01:06:09 +0000317 dprintf("husb: claiming interfaces. config %d\n", configuration);
318
balrogb9dc0332007-10-04 22:47:34 +0000319 i = 0;
320 dev_descr_len = dev->descr[0];
321 if (dev_descr_len > dev->descr_len)
322 goto fail;
323 nb_configurations = dev->descr[17];
324
325 i += dev_descr_len;
326 while (i < dev->descr_len) {
aliguori64838172008-08-21 19:31:10 +0000327 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
balrogb9dc0332007-10-04 22:47:34 +0000328 dev->descr[i], dev->descr[i+1]);
aliguori64838172008-08-21 19:31:10 +0000329
balrogb9dc0332007-10-04 22:47:34 +0000330 if (dev->descr[i+1] != USB_DT_CONFIG) {
331 i += dev->descr[i];
332 continue;
333 }
334 config_descr_len = dev->descr[i];
335
aliguori64838172008-08-21 19:31:10 +0000336 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
aliguori1f3870a2008-08-21 19:27:48 +0000337
aliguori446ab122008-09-14 01:06:09 +0000338 if (configuration < 0 || configuration == dev->descr[i + 5]) {
339 configuration = dev->descr[i + 5];
balrogb9dc0332007-10-04 22:47:34 +0000340 break;
aliguori446ab122008-09-14 01:06:09 +0000341 }
balrogb9dc0332007-10-04 22:47:34 +0000342
343 i += config_descr_len;
344 }
345
346 if (i >= dev->descr_len) {
aliguori0d380642008-08-21 19:32:29 +0000347 fprintf(stderr, "husb: update iface failed. no matching configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000348 goto fail;
349 }
350 nb_interfaces = dev->descr[i + 4];
351
352#ifdef USBDEVFS_DISCONNECT
353 /* earlier Linux 2.4 do not support that */
354 {
355 struct usbdevfs_ioctl ctrl;
356 for (interface = 0; interface < nb_interfaces; interface++) {
357 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
358 ctrl.ifno = interface;
359 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
360 if (ret < 0 && errno != ENODATA) {
361 perror("USBDEVFS_DISCONNECT");
362 goto fail;
363 }
364 }
365 }
366#endif
367
368 /* XXX: only grab if all interfaces are free */
369 for (interface = 0; interface < nb_interfaces; interface++) {
370 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
371 if (ret < 0) {
372 if (errno == EBUSY) {
aliguori64838172008-08-21 19:31:10 +0000373 printf("husb: update iface. device already grabbed\n");
balrogb9dc0332007-10-04 22:47:34 +0000374 } else {
aliguori64838172008-08-21 19:31:10 +0000375 perror("husb: failed to claim interface");
balrogb9dc0332007-10-04 22:47:34 +0000376 }
377 fail:
378 return 0;
379 }
380 }
381
aliguori64838172008-08-21 19:31:10 +0000382 printf("husb: %d interfaces claimed for configuration %d\n",
balrogb9dc0332007-10-04 22:47:34 +0000383 nb_interfaces, configuration);
balrogb9dc0332007-10-04 22:47:34 +0000384
aliguori446ab122008-09-14 01:06:09 +0000385 dev->ninterfaces = nb_interfaces;
386 dev->configuration = configuration;
387 return 1;
388}
389
390static int usb_host_release_interfaces(USBHostDevice *s)
391{
392 int ret, i;
393
394 dprintf("husb: releasing interfaces\n");
395
396 for (i = 0; i < s->ninterfaces; i++) {
397 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
398 if (ret < 0) {
399 perror("husb: failed to release interface");
400 return 0;
401 }
402 }
403
balrogb9dc0332007-10-04 22:47:34 +0000404 return 1;
405}
406
bellard059809e2006-07-19 18:06:15 +0000407static void usb_host_handle_reset(USBDevice *dev)
bellardbb36d472005-11-05 14:22:28 +0000408{
aliguori446ab122008-09-14 01:06:09 +0000409 USBHostDevice *s = (USBHostDevice *) dev;
aliguori64838172008-08-21 19:31:10 +0000410
411 dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
412
bellardbb36d472005-11-05 14:22:28 +0000413 ioctl(s->fd, USBDEVFS_RESET);
aliguori446ab122008-09-14 01:06:09 +0000414
415 usb_host_claim_interfaces(s, s->configuration);
ths5fafdf22007-09-16 21:08:06 +0000416}
bellardbb36d472005-11-05 14:22:28 +0000417
bellard059809e2006-07-19 18:06:15 +0000418static void usb_host_handle_destroy(USBDevice *dev)
419{
420 USBHostDevice *s = (USBHostDevice *)dev;
421
aliguori24772c12008-08-21 19:31:52 +0000422 s->closing = 1;
423
aliguori64838172008-08-21 19:31:10 +0000424 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
aliguori1f3870a2008-08-21 19:27:48 +0000425
aliguori4b096fc2008-08-21 19:28:55 +0000426 hostdev_unlink(s);
427
aliguori64838172008-08-21 19:31:10 +0000428 async_complete(s);
429
bellard059809e2006-07-19 18:06:15 +0000430 if (s->fd >= 0)
431 close(s->fd);
aliguori1f3870a2008-08-21 19:27:48 +0000432
bellard059809e2006-07-19 18:06:15 +0000433 qemu_free(s);
434}
435
balrogb9dc0332007-10-04 22:47:34 +0000436static int usb_linux_update_endp_table(USBHostDevice *s);
437
aliguori446ab122008-09-14 01:06:09 +0000438static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
bellardbb36d472005-11-05 14:22:28 +0000439{
aliguori64838172008-08-21 19:31:10 +0000440 struct usbdevfs_urb *urb;
aliguori446ab122008-09-14 01:06:09 +0000441 AsyncURB *aurb;
bellardbb36d472005-11-05 14:22:28 +0000442 int ret;
443
aliguori64838172008-08-21 19:31:10 +0000444 aurb = async_alloc();
445 if (!aurb) {
446 dprintf("husb: async malloc failed\n");
447 return USB_RET_NAK;
balrogb9dc0332007-10-04 22:47:34 +0000448 }
aliguori64838172008-08-21 19:31:10 +0000449 aurb->hdev = s;
450 aurb->packet = p;
balrogb9dc0332007-10-04 22:47:34 +0000451
aliguori64838172008-08-21 19:31:10 +0000452 urb = &aurb->urb;
453
pbrook4d611c92006-08-12 01:04:27 +0000454 if (p->pid == USB_TOKEN_IN)
aliguori64838172008-08-21 19:31:10 +0000455 urb->endpoint = p->devep | 0x80;
456 else
457 urb->endpoint = p->devep;
458
459 if (is_halted(s, p->devep)) {
460 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
461 if (ret < 0) {
462 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
463 urb->endpoint, errno);
bellardbb36d472005-11-05 14:22:28 +0000464 return USB_RET_NAK;
bellardbb36d472005-11-05 14:22:28 +0000465 }
aliguori64838172008-08-21 19:31:10 +0000466 clear_halt(s, p->devep);
balrog046833e2007-10-31 00:27:50 +0000467 }
468
aliguori64838172008-08-21 19:31:10 +0000469 urb->buffer = p->data;
balrogb9dc0332007-10-04 22:47:34 +0000470 urb->buffer_length = p->len;
aliguori64838172008-08-21 19:31:10 +0000471
472 if (is_isoc(s, p->devep)) {
473 /* Setup ISOC transfer */
474 urb->type = USBDEVFS_URB_TYPE_ISO;
475 urb->flags = USBDEVFS_URB_ISO_ASAP;
476 urb->number_of_packets = 1;
477 urb->iso_frame_desc[0].length = p->len;
balrogb9dc0332007-10-04 22:47:34 +0000478 } else {
aliguori64838172008-08-21 19:31:10 +0000479 /* Setup bulk transfer */
480 urb->type = USBDEVFS_URB_TYPE_BULK;
481 }
482
483 urb->usercontext = s;
484
485 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
486
487 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
488
489 if (ret < 0) {
490 dprintf("husb: submit failed. errno %d\n", errno);
491 async_free(aurb);
492
balrogb9dc0332007-10-04 22:47:34 +0000493 switch(errno) {
494 case ETIMEDOUT:
495 return USB_RET_NAK;
496 case EPIPE:
497 default:
498 return USB_RET_STALL;
499 }
500 }
aliguori64838172008-08-21 19:31:10 +0000501
502 usb_defer_packet(p, async_cancel, aurb);
balrogb9dc0332007-10-04 22:47:34 +0000503 return USB_RET_ASYNC;
balrogb9dc0332007-10-04 22:47:34 +0000504}
505
aliguori446ab122008-09-14 01:06:09 +0000506static int ctrl_error(void)
507{
508 if (errno == ETIMEDOUT)
509 return USB_RET_NAK;
510 else
511 return USB_RET_STALL;
512}
513
514static int usb_host_set_address(USBHostDevice *s, int addr)
515{
516 dprintf("husb: ctrl set addr %u\n", addr);
517 s->dev.addr = addr;
518 return 0;
519}
520
521static int usb_host_set_config(USBHostDevice *s, int config)
522{
523 usb_host_release_interfaces(s);
524
525 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
526
527 dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
528
529 if (ret < 0)
530 return ctrl_error();
531
532 usb_host_claim_interfaces(s, config);
533 return 0;
534}
535
536static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
537{
538 struct usbdevfs_setinterface si;
539 int ret;
540
541 si.interface = iface;
542 si.altsetting = alt;
543 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
544
545 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
546 iface, alt, ret, errno);
547
548 if (ret < 0)
549 return ctrl_error();
550
551 usb_linux_update_endp_table(s);
552 return 0;
553}
554
555static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
556{
557 struct usbdevfs_urb *urb;
558 AsyncURB *aurb;
559 int ret, value, index;
560
561 /*
562 * Process certain standard device requests.
563 * These are infrequent and are processed synchronously.
564 */
565 value = le16_to_cpu(s->ctrl.req.wValue);
566 index = le16_to_cpu(s->ctrl.req.wIndex);
567
568 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
569 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
570 s->ctrl.len);
571
572 if (s->ctrl.req.bRequestType == 0) {
573 switch (s->ctrl.req.bRequest) {
574 case USB_REQ_SET_ADDRESS:
575 return usb_host_set_address(s, value);
576
577 case USB_REQ_SET_CONFIGURATION:
578 return usb_host_set_config(s, value & 0xff);
579 }
580 }
581
582 if (s->ctrl.req.bRequestType == 1 &&
583 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
584 return usb_host_set_interface(s, index, value);
585
586 /* The rest are asynchronous */
587
588 aurb = async_alloc();
589 if (!aurb) {
590 dprintf("husb: async malloc failed\n");
591 return USB_RET_NAK;
592 }
593 aurb->hdev = s;
594 aurb->packet = p;
595
596 /*
597 * Setup ctrl transfer.
598 *
599 * s->ctrl is layed out such that data buffer immediately follows
600 * 'req' struct which is exactly what usbdevfs expects.
601 */
602 urb = &aurb->urb;
603
604 urb->type = USBDEVFS_URB_TYPE_CONTROL;
605 urb->endpoint = p->devep;
606
607 urb->buffer = &s->ctrl.req;
608 urb->buffer_length = 8 + s->ctrl.len;
609
610 urb->usercontext = s;
611
612 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
613
614 dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
615
616 if (ret < 0) {
617 dprintf("husb: submit failed. errno %d\n", errno);
618 async_free(aurb);
619
620 switch(errno) {
621 case ETIMEDOUT:
622 return USB_RET_NAK;
623 case EPIPE:
624 default:
625 return USB_RET_STALL;
626 }
627 }
628
629 usb_defer_packet(p, async_cancel, aurb);
630 return USB_RET_ASYNC;
631}
632
633static int do_token_setup(USBDevice *dev, USBPacket *p)
634{
635 USBHostDevice *s = (USBHostDevice *) dev;
636 int ret = 0;
637
638 if (p->len != 8)
639 return USB_RET_STALL;
640
641 memcpy(&s->ctrl.req, p->data, 8);
642 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
643 s->ctrl.offset = 0;
644 s->ctrl.state = CTRL_STATE_SETUP;
645
646 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
647 ret = usb_host_handle_control(s, p);
648 if (ret < 0)
649 return ret;
650
651 if (ret < s->ctrl.len)
652 s->ctrl.len = ret;
653 s->ctrl.state = CTRL_STATE_DATA;
654 } else {
655 if (s->ctrl.len == 0)
656 s->ctrl.state = CTRL_STATE_ACK;
657 else
658 s->ctrl.state = CTRL_STATE_DATA;
659 }
660
661 return ret;
662}
663
664static int do_token_in(USBDevice *dev, USBPacket *p)
665{
666 USBHostDevice *s = (USBHostDevice *) dev;
667 int ret = 0;
668
669 if (p->devep != 0)
670 return usb_host_handle_data(s, p);
671
672 switch(s->ctrl.state) {
673 case CTRL_STATE_ACK:
674 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
675 ret = usb_host_handle_control(s, p);
676 if (ret == USB_RET_ASYNC)
677 return USB_RET_ASYNC;
678
679 s->ctrl.state = CTRL_STATE_IDLE;
680 return ret > 0 ? 0 : ret;
681 }
682
683 return 0;
684
685 case CTRL_STATE_DATA:
686 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
687 int len = s->ctrl.len - s->ctrl.offset;
688 if (len > p->len)
689 len = p->len;
690 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
691 s->ctrl.offset += len;
692 if (s->ctrl.offset >= s->ctrl.len)
693 s->ctrl.state = CTRL_STATE_ACK;
694 return len;
695 }
696
697 s->ctrl.state = CTRL_STATE_IDLE;
698 return USB_RET_STALL;
699
700 default:
701 return USB_RET_STALL;
702 }
703}
704
705static int do_token_out(USBDevice *dev, USBPacket *p)
706{
707 USBHostDevice *s = (USBHostDevice *) dev;
708
709 if (p->devep != 0)
710 return usb_host_handle_data(s, p);
711
712 switch(s->ctrl.state) {
713 case CTRL_STATE_ACK:
714 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
715 s->ctrl.state = CTRL_STATE_IDLE;
716 /* transfer OK */
717 } else {
718 /* ignore additional output */
719 }
720 return 0;
721
722 case CTRL_STATE_DATA:
723 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
724 int len = s->ctrl.len - s->ctrl.offset;
725 if (len > p->len)
726 len = p->len;
727 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
728 s->ctrl.offset += len;
729 if (s->ctrl.offset >= s->ctrl.len)
730 s->ctrl.state = CTRL_STATE_ACK;
731 return len;
732 }
733
734 s->ctrl.state = CTRL_STATE_IDLE;
735 return USB_RET_STALL;
736
737 default:
738 return USB_RET_STALL;
739 }
740}
741
742/*
743 * Packet handler.
744 * Called by the HC (host controller).
745 *
746 * Returns length of the transaction or one of the USB_RET_XXX codes.
747 */
blueswir1d9cf1572008-09-15 14:57:11 +0000748static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
aliguori446ab122008-09-14 01:06:09 +0000749{
750 switch(p->pid) {
751 case USB_MSG_ATTACH:
752 s->state = USB_STATE_ATTACHED;
753 return 0;
754
755 case USB_MSG_DETACH:
756 s->state = USB_STATE_NOTATTACHED;
757 return 0;
758
759 case USB_MSG_RESET:
760 s->remote_wakeup = 0;
761 s->addr = 0;
762 s->state = USB_STATE_DEFAULT;
763 s->handle_reset(s);
764 return 0;
765 }
766
767 /* Rest of the PIDs must match our address */
768 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
769 return USB_RET_NODEV;
770
771 switch (p->pid) {
772 case USB_TOKEN_SETUP:
773 return do_token_setup(s, p);
774
775 case USB_TOKEN_IN:
776 return do_token_in(s, p);
777
778 case USB_TOKEN_OUT:
779 return do_token_out(s, p);
780
781 default:
782 return USB_RET_STALL;
783 }
784}
785
balrogb9dc0332007-10-04 22:47:34 +0000786/* returns 1 on problem encountered or 0 for success */
787static int usb_linux_update_endp_table(USBHostDevice *s)
788{
789 uint8_t *descriptors;
790 uint8_t devep, type, configuration, alt_interface;
pbrooke41b3912008-10-28 18:22:59 +0000791 struct usb_ctrltransfer ct;
balrogb9dc0332007-10-04 22:47:34 +0000792 int interface, ret, length, i;
793
794 ct.bRequestType = USB_DIR_IN;
795 ct.bRequest = USB_REQ_GET_CONFIGURATION;
796 ct.wValue = 0;
797 ct.wIndex = 0;
798 ct.wLength = 1;
799 ct.data = &configuration;
800 ct.timeout = 50;
801
802 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
803 if (ret < 0) {
804 perror("usb_linux_update_endp_table");
805 return 1;
806 }
807
808 /* in address state */
809 if (configuration == 0)
810 return 1;
811
812 /* get the desired configuration, interface, and endpoint descriptors
813 * from device description */
814 descriptors = &s->descr[18];
815 length = s->descr_len - 18;
816 i = 0;
817
818 if (descriptors[i + 1] != USB_DT_CONFIG ||
819 descriptors[i + 5] != configuration) {
aliguori64838172008-08-21 19:31:10 +0000820 dprintf("invalid descriptor data - configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000821 return 1;
822 }
823 i += descriptors[i];
824
825 while (i < length) {
826 if (descriptors[i + 1] != USB_DT_INTERFACE ||
827 (descriptors[i + 1] == USB_DT_INTERFACE &&
828 descriptors[i + 4] == 0)) {
829 i += descriptors[i];
830 continue;
831 }
832
833 interface = descriptors[i + 2];
834
835 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
836 ct.bRequest = USB_REQ_GET_INTERFACE;
837 ct.wValue = 0;
838 ct.wIndex = interface;
839 ct.wLength = 1;
840 ct.data = &alt_interface;
841 ct.timeout = 50;
842
843 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
844 if (ret < 0) {
845 perror("usb_linux_update_endp_table");
846 return 1;
847 }
848
849 /* the current interface descriptor is the active interface
850 * and has endpoints */
851 if (descriptors[i + 3] != alt_interface) {
852 i += descriptors[i];
853 continue;
854 }
855
856 /* advance to the endpoints */
857 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
858 i += descriptors[i];
859
860 if (i >= length)
861 break;
862
863 while (i < length) {
864 if (descriptors[i + 1] != USB_DT_ENDPOINT)
865 break;
866
867 devep = descriptors[i + 2];
868 switch (descriptors[i + 3] & 0x3) {
869 case 0x00:
870 type = USBDEVFS_URB_TYPE_CONTROL;
871 break;
872 case 0x01:
873 type = USBDEVFS_URB_TYPE_ISO;
874 break;
875 case 0x02:
876 type = USBDEVFS_URB_TYPE_BULK;
877 break;
878 case 0x03:
879 type = USBDEVFS_URB_TYPE_INTERRUPT;
880 break;
881 default:
aliguori64838172008-08-21 19:31:10 +0000882 dprintf("usb_host: malformed endpoint type\n");
balrogb9dc0332007-10-04 22:47:34 +0000883 type = USBDEVFS_URB_TYPE_BULK;
884 }
885 s->endp_table[(devep & 0xf) - 1].type = type;
aliguori64838172008-08-21 19:31:10 +0000886 s->endp_table[(devep & 0xf) - 1].halted = 0;
balrogb9dc0332007-10-04 22:47:34 +0000887
888 i += descriptors[i];
889 }
890 }
891 return 0;
892}
893
aliguori4b096fc2008-08-21 19:28:55 +0000894static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
bellardbb36d472005-11-05 14:22:28 +0000895{
balrogb9dc0332007-10-04 22:47:34 +0000896 int fd = -1, ret;
897 USBHostDevice *dev = NULL;
bellardbb36d472005-11-05 14:22:28 +0000898 struct usbdevfs_connectinfo ci;
bellarda594cfb2005-11-06 16:13:29 +0000899 char buf[1024];
aliguori1f3870a2008-08-21 19:27:48 +0000900
901 dev = qemu_mallocz(sizeof(USBHostDevice));
902 if (!dev)
903 goto fail;
904
aliguori4b096fc2008-08-21 19:28:55 +0000905 dev->bus_num = bus_num;
906 dev->addr = addr;
907
aliguori64838172008-08-21 19:31:10 +0000908 printf("husb: open device %d.%d\n", bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000909
aliguori0f431522008-10-07 20:06:37 +0000910 if (!usb_host_device_path) {
911 perror("husb: USB Host Device Path not set");
912 goto fail;
913 }
914 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
bellarda594cfb2005-11-06 16:13:29 +0000915 bus_num, addr);
balrogb9dc0332007-10-04 22:47:34 +0000916 fd = open(buf, O_RDWR | O_NONBLOCK);
bellardbb36d472005-11-05 14:22:28 +0000917 if (fd < 0) {
bellarda594cfb2005-11-06 16:13:29 +0000918 perror(buf);
aliguori1f3870a2008-08-21 19:27:48 +0000919 goto fail;
bellardbb36d472005-11-05 14:22:28 +0000920 }
aliguori0f431522008-10-07 20:06:37 +0000921 dprintf("husb: opened %s\n", buf);
bellardbb36d472005-11-05 14:22:28 +0000922
balrogb9dc0332007-10-04 22:47:34 +0000923 /* read the device description */
924 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
925 if (dev->descr_len <= 0) {
aliguori64838172008-08-21 19:31:10 +0000926 perror("husb: reading device data failed");
bellardbb36d472005-11-05 14:22:28 +0000927 goto fail;
928 }
ths3b46e622007-09-17 08:09:54 +0000929
balrogb9dc0332007-10-04 22:47:34 +0000930#ifdef DEBUG
bellard868bfe22005-11-13 21:53:15 +0000931 {
balrogb9dc0332007-10-04 22:47:34 +0000932 int x;
933 printf("=== begin dumping device descriptor data ===\n");
934 for (x = 0; x < dev->descr_len; x++)
935 printf("%02x ", dev->descr[x]);
936 printf("\n=== end dumping device descriptor data ===\n");
bellarda594cfb2005-11-06 16:13:29 +0000937 }
938#endif
939
balrogb9dc0332007-10-04 22:47:34 +0000940 dev->fd = fd;
balrogb9dc0332007-10-04 22:47:34 +0000941
aliguori446ab122008-09-14 01:06:09 +0000942 /*
943 * Initial configuration is -1 which makes us claim first
944 * available config. We used to start with 1, which does not
945 * always work. I've seen devices where first config starts
946 * with 2.
947 */
948 if (!usb_host_claim_interfaces(dev, -1))
balrogb9dc0332007-10-04 22:47:34 +0000949 goto fail;
bellardbb36d472005-11-05 14:22:28 +0000950
951 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
952 if (ret < 0) {
balrog046833e2007-10-31 00:27:50 +0000953 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
bellardbb36d472005-11-05 14:22:28 +0000954 goto fail;
955 }
956
aliguori64838172008-08-21 19:31:10 +0000957 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
bellardbb36d472005-11-05 14:22:28 +0000958
balrogb9dc0332007-10-04 22:47:34 +0000959 ret = usb_linux_update_endp_table(dev);
960 if (ret)
bellardbb36d472005-11-05 14:22:28 +0000961 goto fail;
balrogb9dc0332007-10-04 22:47:34 +0000962
bellardbb36d472005-11-05 14:22:28 +0000963 if (ci.slow)
964 dev->dev.speed = USB_SPEED_LOW;
965 else
966 dev->dev.speed = USB_SPEED_HIGH;
bellardbb36d472005-11-05 14:22:28 +0000967
aliguori446ab122008-09-14 01:06:09 +0000968 dev->dev.handle_packet = usb_host_handle_packet;
969 dev->dev.handle_reset = usb_host_handle_reset;
bellard059809e2006-07-19 18:06:15 +0000970 dev->dev.handle_destroy = usb_host_handle_destroy;
bellard1f6e24e2006-06-26 21:00:51 +0000971
aliguori4b096fc2008-08-21 19:28:55 +0000972 if (!prod_name || prod_name[0] == '\0')
bellard1f6e24e2006-06-26 21:00:51 +0000973 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
aliguori4b096fc2008-08-21 19:28:55 +0000974 "host:%d.%d", bus_num, addr);
bellard1f6e24e2006-06-26 21:00:51 +0000975 else
976 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
aliguori4b096fc2008-08-21 19:28:55 +0000977 prod_name);
bellard1f6e24e2006-06-26 21:00:51 +0000978
aliguori64838172008-08-21 19:31:10 +0000979 /* USB devio uses 'write' flag to check for async completions */
980 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
aliguori1f3870a2008-08-21 19:27:48 +0000981
aliguori4b096fc2008-08-21 19:28:55 +0000982 hostdev_link(dev);
983
aliguori64838172008-08-21 19:31:10 +0000984 return (USBDevice *) dev;
aliguori4b096fc2008-08-21 19:28:55 +0000985
balrogb9dc0332007-10-04 22:47:34 +0000986fail:
aliguori24772c12008-08-21 19:31:52 +0000987 if (dev)
balrogb9dc0332007-10-04 22:47:34 +0000988 qemu_free(dev);
aliguori24772c12008-08-21 19:31:52 +0000989
balrogb9dc0332007-10-04 22:47:34 +0000990 close(fd);
991 return NULL;
bellardbb36d472005-11-05 14:22:28 +0000992}
993
aliguori5d0c5752008-09-14 01:07:41 +0000994static int usb_host_auto_add(const char *spec);
995static int usb_host_auto_del(const char *spec);
996
aliguori4b096fc2008-08-21 19:28:55 +0000997USBDevice *usb_host_device_open(const char *devname)
998{
999 int bus_num, addr;
1000 char product_name[PRODUCT_NAME_SZ];
1001
aliguori5d0c5752008-09-14 01:07:41 +00001002 if (strstr(devname, "auto:")) {
1003 usb_host_auto_add(devname);
1004 return NULL;
1005 }
1006
1007 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
aliguori4b096fc2008-08-21 19:28:55 +00001008 devname) < 0)
1009 return NULL;
1010
aliguori5d0c5752008-09-14 01:07:41 +00001011 if (hostdev_find(bus_num, addr)) {
1012 term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
1013 return NULL;
1014 }
aliguori4b096fc2008-08-21 19:28:55 +00001015
1016 return usb_host_device_open_addr(bus_num, addr, product_name);
1017}
aliguori5d0c5752008-09-14 01:07:41 +00001018
1019int usb_host_device_close(const char *devname)
1020{
1021 char product_name[PRODUCT_NAME_SZ];
1022 int bus_num, addr;
1023 USBHostDevice *s;
1024
1025 if (strstr(devname, "auto:"))
1026 return usb_host_auto_del(devname);
1027
1028 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1029 devname) < 0)
1030 return -1;
1031
1032 s = hostdev_find(bus_num, addr);
1033 if (s) {
1034 usb_device_del_addr(0, s->dev.addr);
1035 return 0;
1036 }
1037
1038 return -1;
1039}
aliguori4b096fc2008-08-21 19:28:55 +00001040
bellarda594cfb2005-11-06 16:13:29 +00001041static int get_tag_value(char *buf, int buf_size,
ths5fafdf22007-09-16 21:08:06 +00001042 const char *str, const char *tag,
bellarda594cfb2005-11-06 16:13:29 +00001043 const char *stopchars)
bellardbb36d472005-11-05 14:22:28 +00001044{
bellarda594cfb2005-11-06 16:13:29 +00001045 const char *p;
1046 char *q;
1047 p = strstr(str, tag);
1048 if (!p)
1049 return -1;
1050 p += strlen(tag);
1051 while (isspace(*p))
1052 p++;
1053 q = buf;
1054 while (*p != '\0' && !strchr(stopchars, *p)) {
1055 if ((q - buf) < (buf_size - 1))
1056 *q++ = *p;
1057 p++;
1058 }
1059 *q = '\0';
1060 return q - buf;
1061}
bellardbb36d472005-11-05 14:22:28 +00001062
aliguori0f431522008-10-07 20:06:37 +00001063/*
1064 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1065 * host's USB devices. This is legacy support since many distributions
1066 * are moving to /sys/bus/usb
1067 */
1068static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
bellarda594cfb2005-11-06 16:13:29 +00001069{
aliguori0f431522008-10-07 20:06:37 +00001070 FILE *f = 0;
bellarda594cfb2005-11-06 16:13:29 +00001071 char line[1024];
1072 char buf[1024];
1073 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
bellarda594cfb2005-11-06 16:13:29 +00001074 char product_name[512];
aliguori0f431522008-10-07 20:06:37 +00001075 int ret = 0;
ths3b46e622007-09-17 08:09:54 +00001076
aliguori0f431522008-10-07 20:06:37 +00001077 if (!usb_host_device_path) {
1078 perror("husb: USB Host Device Path not set");
1079 goto the_end;
bellarda594cfb2005-11-06 16:13:29 +00001080 }
aliguori0f431522008-10-07 20:06:37 +00001081 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1082 f = fopen(line, "r");
1083 if (!f) {
1084 perror("husb: cannot open devices file");
1085 goto the_end;
1086 }
1087
bellarda594cfb2005-11-06 16:13:29 +00001088 device_count = 0;
1089 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
bellardbb36d472005-11-05 14:22:28 +00001090 for(;;) {
bellarda594cfb2005-11-06 16:13:29 +00001091 if (fgets(line, sizeof(line), f) == NULL)
bellardbb36d472005-11-05 14:22:28 +00001092 break;
bellarda594cfb2005-11-06 16:13:29 +00001093 if (strlen(line) > 0)
1094 line[strlen(line) - 1] = '\0';
1095 if (line[0] == 'T' && line[1] == ':') {
pbrook38ca0f62006-03-11 18:03:38 +00001096 if (device_count && (vendor_id || product_id)) {
1097 /* New device. Add the previously discovered device. */
ths5fafdf22007-09-16 21:08:06 +00001098 ret = func(opaque, bus_num, addr, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001099 product_id, product_name, speed);
1100 if (ret)
1101 goto the_end;
1102 }
1103 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
1104 goto fail;
1105 bus_num = atoi(buf);
1106 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
1107 goto fail;
1108 addr = atoi(buf);
1109 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
1110 goto fail;
1111 if (!strcmp(buf, "480"))
1112 speed = USB_SPEED_HIGH;
1113 else if (!strcmp(buf, "1.5"))
1114 speed = USB_SPEED_LOW;
1115 else
1116 speed = USB_SPEED_FULL;
1117 product_name[0] = '\0';
1118 class_id = 0xff;
1119 device_count++;
1120 product_id = 0;
1121 vendor_id = 0;
1122 } else if (line[0] == 'P' && line[1] == ':') {
1123 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
1124 goto fail;
1125 vendor_id = strtoul(buf, NULL, 16);
1126 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
1127 goto fail;
1128 product_id = strtoul(buf, NULL, 16);
1129 } else if (line[0] == 'S' && line[1] == ':') {
1130 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
1131 goto fail;
1132 pstrcpy(product_name, sizeof(product_name), buf);
1133 } else if (line[0] == 'D' && line[1] == ':') {
1134 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
1135 goto fail;
1136 class_id = strtoul(buf, NULL, 16);
1137 }
1138 fail: ;
1139 }
pbrook38ca0f62006-03-11 18:03:38 +00001140 if (device_count && (vendor_id || product_id)) {
1141 /* Add the last device. */
ths5fafdf22007-09-16 21:08:06 +00001142 ret = func(opaque, bus_num, addr, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001143 product_id, product_name, speed);
1144 }
1145 the_end:
aliguori0f431522008-10-07 20:06:37 +00001146 if (f)
1147 fclose(f);
1148 return ret;
1149}
1150
1151/*
1152 * Read sys file-system device file
1153 *
1154 * @line address of buffer to put file contents in
1155 * @line_size size of line
1156 * @device_file path to device file (printf format string)
1157 * @device_name device being opened (inserted into device_file)
1158 *
1159 * @return 0 failed, 1 succeeded ('line' contains data)
1160 */
1161static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
1162{
1163 FILE *f;
1164 int ret = 0;
1165 char filename[PATH_MAX];
1166
1167 snprintf(filename, PATH_MAX, device_file, device_name);
1168 f = fopen(filename, "r");
1169 if (f) {
1170 fgets(line, line_size, f);
1171 fclose(f);
1172 ret = 1;
1173 } else {
1174 term_printf("husb: could not open %s\n", filename);
1175 }
1176
1177 return ret;
1178}
1179
1180/*
1181 * Use /sys/bus/usb/devices/ directory to determine host's USB
1182 * devices.
1183 *
1184 * This code is based on Robert Schiele's original patches posted to
1185 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1186 */
1187static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1188{
1189 DIR *dir = 0;
1190 char line[1024];
1191 int bus_num, addr, speed, class_id, product_id, vendor_id;
1192 int ret = 0;
1193 char product_name[512];
1194 struct dirent *de;
1195
1196 dir = opendir(USBSYSBUS_PATH "/devices");
1197 if (!dir) {
1198 perror("husb: cannot open devices directory");
1199 goto the_end;
1200 }
1201
1202 while ((de = readdir(dir))) {
1203 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1204 char *tmpstr = de->d_name;
1205 if (!strncmp(de->d_name, "usb", 3))
1206 tmpstr += 3;
1207 bus_num = atoi(tmpstr);
1208
1209 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/devnum", de->d_name))
1210 goto the_end;
1211 if (sscanf(line, "%d", &addr) != 1)
1212 goto the_end;
1213
1214 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/bDeviceClass", de->d_name))
1215 goto the_end;
1216 if (sscanf(line, "%x", &class_id) != 1)
1217 goto the_end;
1218
1219 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/idVendor", de->d_name))
1220 goto the_end;
1221 if (sscanf(line, "%x", &vendor_id) != 1)
1222 goto the_end;
1223
1224 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/idProduct", de->d_name))
1225 goto the_end;
1226 if (sscanf(line, "%x", &product_id) != 1)
1227 goto the_end;
1228
1229 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/product", de->d_name)) {
1230 *product_name = 0;
1231 } else {
1232 if (strlen(line) > 0)
1233 line[strlen(line) - 1] = '\0';
1234 pstrcpy(product_name, sizeof(product_name), line);
1235 }
1236
1237 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/speed", de->d_name))
1238 goto the_end;
1239 if (!strcmp(line, "480\n"))
1240 speed = USB_SPEED_HIGH;
1241 else if (!strcmp(line, "1.5\n"))
1242 speed = USB_SPEED_LOW;
1243 else
1244 speed = USB_SPEED_FULL;
1245
1246 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1247 product_id, product_name, speed);
1248 if (ret)
1249 goto the_end;
1250 }
1251 }
1252 the_end:
1253 if (dir)
1254 closedir(dir);
1255 return ret;
1256}
1257
1258/*
1259 * Determine how to access the host's USB devices and call the
1260 * specific support function.
1261 */
1262static int usb_host_scan(void *opaque, USBScanFunc *func)
1263{
1264 FILE *f = 0;
1265 DIR *dir = 0;
1266 int ret = 0;
aliguori0f431522008-10-07 20:06:37 +00001267 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1268 char devpath[PATH_MAX];
1269
1270 /* only check the host once */
1271 if (!usb_fs_type) {
1272 f = fopen(USBPROCBUS_PATH "/devices", "r");
1273 if (f) {
1274 /* devices found in /proc/bus/usb/ */
1275 strcpy(devpath, USBPROCBUS_PATH);
1276 usb_fs_type = USB_FS_PROC;
1277 fclose(f);
blueswir15be8e1f2008-10-25 11:47:20 +00001278 dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001279 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001280 }
1281 /* try additional methods if an access method hasn't been found yet */
1282 f = fopen(USBDEVBUS_PATH "/devices", "r");
aliguorif16a0db2008-10-21 16:34:20 +00001283 if (f) {
aliguori0f431522008-10-07 20:06:37 +00001284 /* devices found in /dev/bus/usb/ */
1285 strcpy(devpath, USBDEVBUS_PATH);
1286 usb_fs_type = USB_FS_DEV;
1287 fclose(f);
blueswir15be8e1f2008-10-25 11:47:20 +00001288 dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001289 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001290 }
1291 dir = opendir(USBSYSBUS_PATH "/devices");
aliguorif16a0db2008-10-21 16:34:20 +00001292 if (dir) {
aliguori0f431522008-10-07 20:06:37 +00001293 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1294 strcpy(devpath, USBDEVBUS_PATH);
1295 usb_fs_type = USB_FS_SYS;
1296 closedir(dir);
blueswir15be8e1f2008-10-25 11:47:20 +00001297 dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001298 goto found_devices;
aliguori22babeb2008-10-21 16:27:28 +00001299 }
aliguorif16a0db2008-10-21 16:34:20 +00001300 found_devices:
aliguori22babeb2008-10-21 16:27:28 +00001301 if (!usb_fs_type) {
aliguori0f431522008-10-07 20:06:37 +00001302 term_printf("husb: unable to access USB devices\n");
aliguorif16a0db2008-10-21 16:34:20 +00001303 return -ENOENT;
aliguori0f431522008-10-07 20:06:37 +00001304 }
1305
1306 /* the module setting (used later for opening devices) */
1307 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1308 if (usb_host_device_path) {
1309 strcpy(usb_host_device_path, devpath);
1310 term_printf("husb: using %s file-system with %s\n", fs_type[usb_fs_type], usb_host_device_path);
1311 } else {
1312 /* out of memory? */
1313 perror("husb: unable to allocate memory for device path");
aliguorif16a0db2008-10-21 16:34:20 +00001314 return -ENOMEM;
aliguori0f431522008-10-07 20:06:37 +00001315 }
1316 }
1317
1318 switch (usb_fs_type) {
1319 case USB_FS_PROC:
1320 case USB_FS_DEV:
1321 ret = usb_host_scan_dev(opaque, func);
1322 break;
1323 case USB_FS_SYS:
1324 ret = usb_host_scan_sys(opaque, func);
1325 break;
aliguorif16a0db2008-10-21 16:34:20 +00001326 default:
1327 ret = -EINVAL;
1328 break;
aliguori0f431522008-10-07 20:06:37 +00001329 }
bellarda594cfb2005-11-06 16:13:29 +00001330 return ret;
1331}
1332
aliguori4b096fc2008-08-21 19:28:55 +00001333struct USBAutoFilter {
1334 struct USBAutoFilter *next;
1335 int bus_num;
1336 int addr;
1337 int vendor_id;
1338 int product_id;
1339};
1340
1341static QEMUTimer *usb_auto_timer;
1342static struct USBAutoFilter *usb_auto_filter;
1343
1344static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1345 int class_id, int vendor_id, int product_id,
1346 const char *product_name, int speed)
1347{
1348 struct USBAutoFilter *f;
1349 struct USBDevice *dev;
1350
1351 /* Ignore hubs */
1352 if (class_id == 9)
1353 return 0;
1354
1355 for (f = usb_auto_filter; f; f = f->next) {
aliguori4b096fc2008-08-21 19:28:55 +00001356 if (f->bus_num >= 0 && f->bus_num != bus_num)
1357 continue;
1358
1359 if (f->addr >= 0 && f->addr != addr)
1360 continue;
1361
1362 if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
1363 continue;
1364
1365 if (f->product_id >= 0 && f->product_id != product_id)
1366 continue;
1367
1368 /* We got a match */
1369
1370 /* Allredy attached ? */
1371 if (hostdev_find(bus_num, addr))
1372 return 0;
1373
aliguori64838172008-08-21 19:31:10 +00001374 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
aliguori4b096fc2008-08-21 19:28:55 +00001375
1376 dev = usb_host_device_open_addr(bus_num, addr, product_name);
1377 if (dev)
1378 usb_device_add_dev(dev);
1379 }
1380
1381 return 0;
1382}
1383
1384static void usb_host_auto_timer(void *unused)
1385{
1386 usb_host_scan(NULL, usb_host_auto_scan);
1387 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1388}
1389
1390/*
aliguori5d0c5752008-09-14 01:07:41 +00001391 * Autoconnect filter
1392 * Format:
1393 * auto:bus:dev[:vid:pid]
1394 * auto:bus.dev[:vid:pid]
1395 *
1396 * bus - bus number (dec, * means any)
1397 * dev - device number (dec, * means any)
1398 * vid - vendor id (hex, * means any)
1399 * pid - product id (hex, * means any)
1400 *
1401 * See 'lsusb' output.
aliguori4b096fc2008-08-21 19:28:55 +00001402 */
aliguori5d0c5752008-09-14 01:07:41 +00001403static int parse_filter(const char *spec, struct USBAutoFilter *f)
aliguori4b096fc2008-08-21 19:28:55 +00001404{
aliguori5d0c5752008-09-14 01:07:41 +00001405 enum { BUS, DEV, VID, PID, DONE };
1406 const char *p = spec;
1407 int i;
1408
1409 f->bus_num = -1;
1410 f->addr = -1;
1411 f->vendor_id = -1;
1412 f->product_id = -1;
1413
1414 for (i = BUS; i < DONE; i++) {
1415 p = strpbrk(p, ":.");
1416 if (!p) break;
1417 p++;
1418
1419 if (*p == '*')
1420 continue;
1421
1422 switch(i) {
1423 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1424 case DEV: f->addr = strtol(p, NULL, 10); break;
1425 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1426 case PID: f->product_id = strtol(p, NULL, 16); break;
1427 }
aliguori4b096fc2008-08-21 19:28:55 +00001428 }
1429
aliguori5d0c5752008-09-14 01:07:41 +00001430 if (i < DEV) {
1431 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1432 return -1;
1433 }
1434
1435 return 0;
1436}
1437
1438static int match_filter(const struct USBAutoFilter *f1,
1439 const struct USBAutoFilter *f2)
1440{
1441 return f1->bus_num == f2->bus_num &&
1442 f1->addr == f2->addr &&
1443 f1->vendor_id == f2->vendor_id &&
1444 f1->product_id == f2->product_id;
1445}
1446
1447static int usb_host_auto_add(const char *spec)
1448{
1449 struct USBAutoFilter filter, *f;
1450
1451 if (parse_filter(spec, &filter) < 0)
1452 return -1;
1453
1454 f = qemu_mallocz(sizeof(*f));
1455 if (!f) {
1456 fprintf(stderr, "husb: failed to allocate auto filter\n");
1457 return -1;
1458 }
1459
1460 *f = filter;
aliguori4b096fc2008-08-21 19:28:55 +00001461
1462 if (!usb_auto_filter) {
1463 /*
1464 * First entry. Init and start the monitor.
1465 * Right now we're using timer to check for new devices.
1466 * If this turns out to be too expensive we can move that into a
1467 * separate thread.
1468 */
1469 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
1470 if (!usb_auto_timer) {
aliguori0d380642008-08-21 19:32:29 +00001471 fprintf(stderr, "husb: failed to allocate auto scan timer\n");
aliguori4b096fc2008-08-21 19:28:55 +00001472 qemu_free(f);
aliguori5d0c5752008-09-14 01:07:41 +00001473 return -1;
aliguori4b096fc2008-08-21 19:28:55 +00001474 }
1475
1476 /* Check for new devices every two seconds */
1477 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1478 }
1479
aliguori5d0c5752008-09-14 01:07:41 +00001480 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1481 f->bus_num, f->addr, f->vendor_id, f->product_id);
aliguori4b096fc2008-08-21 19:28:55 +00001482
1483 f->next = usb_auto_filter;
1484 usb_auto_filter = f;
aliguori5d0c5752008-09-14 01:07:41 +00001485
1486 return 0;
1487}
1488
1489static int usb_host_auto_del(const char *spec)
1490{
1491 struct USBAutoFilter *pf = usb_auto_filter;
1492 struct USBAutoFilter **prev = &usb_auto_filter;
1493 struct USBAutoFilter filter;
1494
1495 if (parse_filter(spec, &filter) < 0)
1496 return -1;
1497
1498 while (pf) {
1499 if (match_filter(pf, &filter)) {
1500 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1501 pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
1502
1503 *prev = pf->next;
1504
1505 if (!usb_auto_filter) {
1506 /* No more filters. Stop scanning. */
1507 qemu_del_timer(usb_auto_timer);
1508 qemu_free_timer(usb_auto_timer);
1509 }
1510
1511 return 0;
1512 }
1513
1514 prev = &pf->next;
1515 pf = pf->next;
1516 }
1517
1518 return -1;
aliguori4b096fc2008-08-21 19:28:55 +00001519}
1520
bellarda594cfb2005-11-06 16:13:29 +00001521typedef struct FindDeviceState {
1522 int vendor_id;
1523 int product_id;
1524 int bus_num;
1525 int addr;
bellard1f6e24e2006-06-26 21:00:51 +00001526 char product_name[PRODUCT_NAME_SZ];
bellarda594cfb2005-11-06 16:13:29 +00001527} FindDeviceState;
1528
ths5fafdf22007-09-16 21:08:06 +00001529static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
bellarda594cfb2005-11-06 16:13:29 +00001530 int class_id,
ths5fafdf22007-09-16 21:08:06 +00001531 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +00001532 const char *product_name, int speed)
1533{
1534 FindDeviceState *s = opaque;
bellard1f6e24e2006-06-26 21:00:51 +00001535 if ((vendor_id == s->vendor_id &&
1536 product_id == s->product_id) ||
1537 (bus_num == s->bus_num &&
1538 addr == s->addr)) {
1539 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
bellarda594cfb2005-11-06 16:13:29 +00001540 s->bus_num = bus_num;
1541 s->addr = addr;
1542 return 1;
1543 } else {
1544 return 0;
1545 }
1546}
1547
ths5fafdf22007-09-16 21:08:06 +00001548/* the syntax is :
1549 'bus.addr' (decimal numbers) or
bellarda594cfb2005-11-06 16:13:29 +00001550 'vendor_id:product_id' (hexa numbers) */
ths5fafdf22007-09-16 21:08:06 +00001551static int usb_host_find_device(int *pbus_num, int *paddr,
bellard1f6e24e2006-06-26 21:00:51 +00001552 char *product_name, int product_name_size,
bellarda594cfb2005-11-06 16:13:29 +00001553 const char *devname)
1554{
1555 const char *p;
1556 int ret;
1557 FindDeviceState fs;
1558
1559 p = strchr(devname, '.');
1560 if (p) {
1561 *pbus_num = strtoul(devname, NULL, 0);
1562 *paddr = strtoul(p + 1, NULL, 0);
bellard1f6e24e2006-06-26 21:00:51 +00001563 fs.bus_num = *pbus_num;
1564 fs.addr = *paddr;
1565 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1566 if (ret)
1567 pstrcpy(product_name, product_name_size, fs.product_name);
bellarda594cfb2005-11-06 16:13:29 +00001568 return 0;
1569 }
aliguori5d0c5752008-09-14 01:07:41 +00001570
bellarda594cfb2005-11-06 16:13:29 +00001571 p = strchr(devname, ':');
1572 if (p) {
1573 fs.vendor_id = strtoul(devname, NULL, 16);
1574 fs.product_id = strtoul(p + 1, NULL, 16);
1575 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1576 if (ret) {
1577 *pbus_num = fs.bus_num;
1578 *paddr = fs.addr;
bellard1f6e24e2006-06-26 21:00:51 +00001579 pstrcpy(product_name, product_name_size, fs.product_name);
bellarda594cfb2005-11-06 16:13:29 +00001580 return 0;
bellardbb36d472005-11-05 14:22:28 +00001581 }
1582 }
bellarda594cfb2005-11-06 16:13:29 +00001583 return -1;
bellardbb36d472005-11-05 14:22:28 +00001584}
1585
bellarda594cfb2005-11-06 16:13:29 +00001586/**********************/
1587/* USB host device info */
bellardbb36d472005-11-05 14:22:28 +00001588
bellarda594cfb2005-11-06 16:13:29 +00001589struct usb_class_info {
1590 int class;
1591 const char *class_name;
1592};
1593
1594static const struct usb_class_info usb_class_info[] = {
1595 { USB_CLASS_AUDIO, "Audio"},
1596 { USB_CLASS_COMM, "Communication"},
1597 { USB_CLASS_HID, "HID"},
1598 { USB_CLASS_HUB, "Hub" },
1599 { USB_CLASS_PHYSICAL, "Physical" },
1600 { USB_CLASS_PRINTER, "Printer" },
1601 { USB_CLASS_MASS_STORAGE, "Storage" },
1602 { USB_CLASS_CDC_DATA, "Data" },
1603 { USB_CLASS_APP_SPEC, "Application Specific" },
1604 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1605 { USB_CLASS_STILL_IMAGE, "Still Image" },
balrogb9dc0332007-10-04 22:47:34 +00001606 { USB_CLASS_CSCID, "Smart Card" },
bellarda594cfb2005-11-06 16:13:29 +00001607 { USB_CLASS_CONTENT_SEC, "Content Security" },
1608 { -1, NULL }
1609};
1610
1611static const char *usb_class_str(uint8_t class)
1612{
1613 const struct usb_class_info *p;
1614 for(p = usb_class_info; p->class != -1; p++) {
1615 if (p->class == class)
bellardbb36d472005-11-05 14:22:28 +00001616 break;
bellardbb36d472005-11-05 14:22:28 +00001617 }
bellarda594cfb2005-11-06 16:13:29 +00001618 return p->class_name;
bellardbb36d472005-11-05 14:22:28 +00001619}
1620
pbrook9596ebb2007-11-18 01:44:38 +00001621static void usb_info_device(int bus_num, int addr, int class_id,
1622 int vendor_id, int product_id,
1623 const char *product_name,
1624 int speed)
bellardbb36d472005-11-05 14:22:28 +00001625{
bellarda594cfb2005-11-06 16:13:29 +00001626 const char *class_str, *speed_str;
1627
1628 switch(speed) {
ths5fafdf22007-09-16 21:08:06 +00001629 case USB_SPEED_LOW:
1630 speed_str = "1.5";
bellarda594cfb2005-11-06 16:13:29 +00001631 break;
ths5fafdf22007-09-16 21:08:06 +00001632 case USB_SPEED_FULL:
1633 speed_str = "12";
bellarda594cfb2005-11-06 16:13:29 +00001634 break;
ths5fafdf22007-09-16 21:08:06 +00001635 case USB_SPEED_HIGH:
1636 speed_str = "480";
bellarda594cfb2005-11-06 16:13:29 +00001637 break;
1638 default:
ths5fafdf22007-09-16 21:08:06 +00001639 speed_str = "?";
bellarda594cfb2005-11-06 16:13:29 +00001640 break;
bellardbb36d472005-11-05 14:22:28 +00001641 }
bellarda594cfb2005-11-06 16:13:29 +00001642
ths5fafdf22007-09-16 21:08:06 +00001643 term_printf(" Device %d.%d, speed %s Mb/s\n",
bellarda594cfb2005-11-06 16:13:29 +00001644 bus_num, addr, speed_str);
1645 class_str = usb_class_str(class_id);
ths5fafdf22007-09-16 21:08:06 +00001646 if (class_str)
bellarda594cfb2005-11-06 16:13:29 +00001647 term_printf(" %s:", class_str);
1648 else
1649 term_printf(" Class %02x:", class_id);
1650 term_printf(" USB device %04x:%04x", vendor_id, product_id);
1651 if (product_name[0] != '\0')
1652 term_printf(", %s", product_name);
1653 term_printf("\n");
1654}
1655
ths5fafdf22007-09-16 21:08:06 +00001656static int usb_host_info_device(void *opaque, int bus_num, int addr,
bellarda594cfb2005-11-06 16:13:29 +00001657 int class_id,
ths5fafdf22007-09-16 21:08:06 +00001658 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +00001659 const char *product_name,
1660 int speed)
1661{
1662 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
1663 product_name, speed);
1664 return 0;
1665}
1666
aliguoriac4ffb52008-09-22 15:04:31 +00001667static void dec2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001668{
1669 if (val == -1)
aliguoriac4ffb52008-09-22 15:04:31 +00001670 snprintf(str, size, "*");
aliguori5d0c5752008-09-14 01:07:41 +00001671 else
aliguoriac4ffb52008-09-22 15:04:31 +00001672 snprintf(str, size, "%d", val);
aliguori5d0c5752008-09-14 01:07:41 +00001673}
1674
aliguoriac4ffb52008-09-22 15:04:31 +00001675static void hex2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001676{
1677 if (val == -1)
aliguoriac4ffb52008-09-22 15:04:31 +00001678 snprintf(str, size, "*");
aliguori5d0c5752008-09-14 01:07:41 +00001679 else
aliguoriac4ffb52008-09-22 15:04:31 +00001680 snprintf(str, size, "%x", val);
aliguori5d0c5752008-09-14 01:07:41 +00001681}
1682
bellarda594cfb2005-11-06 16:13:29 +00001683void usb_host_info(void)
1684{
aliguori5d0c5752008-09-14 01:07:41 +00001685 struct USBAutoFilter *f;
1686
bellarda594cfb2005-11-06 16:13:29 +00001687 usb_host_scan(NULL, usb_host_info_device);
aliguori5d0c5752008-09-14 01:07:41 +00001688
1689 if (usb_auto_filter)
1690 term_printf(" Auto filters:\n");
1691 for (f = usb_auto_filter; f; f = f->next) {
1692 char bus[10], addr[10], vid[10], pid[10];
aliguoriac4ffb52008-09-22 15:04:31 +00001693 dec2str(f->bus_num, bus, sizeof(bus));
1694 dec2str(f->addr, addr, sizeof(addr));
1695 hex2str(f->vendor_id, vid, sizeof(vid));
1696 hex2str(f->product_id, pid, sizeof(pid));
aliguori5d0c5752008-09-14 01:07:41 +00001697 term_printf(" Device %s.%s ID %s:%s\n", bus, addr, vid, pid);
1698 }
bellardbb36d472005-11-05 14:22:28 +00001699}
1700
1701#else
1702
aliguori446ab122008-09-14 01:06:09 +00001703#include "hw/usb.h"
1704
bellarda594cfb2005-11-06 16:13:29 +00001705void usb_host_info(void)
1706{
1707 term_printf("USB host devices not supported\n");
1708}
1709
bellardbb36d472005-11-05 14:22:28 +00001710/* XXX: modify configure to compile the right host driver */
bellarda594cfb2005-11-06 16:13:29 +00001711USBDevice *usb_host_device_open(const char *devname)
bellardbb36d472005-11-05 14:22:28 +00001712{
1713 return NULL;
1714}
1715
aliguori5d0c5752008-09-14 01:07:41 +00001716int usb_host_device_close(const char *devname)
1717{
1718 return 0;
1719}
1720
bellardbb36d472005-11-05 14:22:28 +00001721#endif