blob: 1ecfbc45655532510baf35702e5c98173e2b6188 [file] [log] [blame]
bellardbb36d472005-11-05 14:22:28 +00001/*
2 * Linux host USB redirector
3 *
4 * Copyright (c) 2005 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
aliguori64838172008-08-21 19:31:10 +00006 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
aliguori5d0c5752008-09-14 01:07:41 +00008 * Major rewrite to support fully async operation
aliguori4b096fc2008-08-21 19:28:55 +00009 *
aliguori0f431522008-10-07 20:06:37 +000010 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
13 *
bellardbb36d472005-11-05 14:22:28 +000014 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
31 */
aliguori446ab122008-09-14 01:06:09 +000032
pbrook87ecb682007-11-17 17:14:51 +000033#include "qemu-common.h"
aliguori1f3870a2008-08-21 19:27:48 +000034#include "qemu-timer.h"
aliguori376253e2009-03-05 23:01:23 +000035#include "monitor.h"
bellardbb36d472005-11-05 14:22:28 +000036
bellardbb36d472005-11-05 14:22:28 +000037#include <dirent.h>
38#include <sys/ioctl.h>
balrogb9dc0332007-10-04 22:47:34 +000039#include <signal.h>
bellardbb36d472005-11-05 14:22:28 +000040
aliguori446ab122008-09-14 01:06:09 +000041#include <linux/usbdevice_fs.h>
42#include <linux/version.h>
43#include "hw/usb.h"
bellardbb36d472005-11-05 14:22:28 +000044
blueswir1d9cf1572008-09-15 14:57:11 +000045/* We redefine it to avoid version problems */
46struct usb_ctrltransfer {
47 uint8_t bRequestType;
48 uint8_t bRequest;
49 uint16_t wValue;
50 uint16_t wIndex;
51 uint16_t wLength;
52 uint32_t timeout;
53 void *data;
54};
55
56struct usb_ctrlrequest {
57 uint8_t bRequestType;
58 uint8_t bRequest;
59 uint16_t wValue;
60 uint16_t wIndex;
61 uint16_t wLength;
62};
63
bellarda594cfb2005-11-06 16:13:29 +000064typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
ths5fafdf22007-09-16 21:08:06 +000065 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +000066 const char *product_name, int speed);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +010067
Markus Armbruster0745eb12009-11-27 13:05:53 +010068//#define DEBUG
aliguori64838172008-08-21 19:31:10 +000069
70#ifdef DEBUG
malcd0f2c4c2010-02-07 02:03:50 +030071#define DPRINTF printf
aliguori64838172008-08-21 19:31:10 +000072#else
malcd0f2c4c2010-02-07 02:03:50 +030073#define DPRINTF(...)
aliguori64838172008-08-21 19:31:10 +000074#endif
bellardbb36d472005-11-05 14:22:28 +000075
blueswir15be8e1f2008-10-25 11:47:20 +000076#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
77
aliguori0f431522008-10-07 20:06:37 +000078#define USBPROCBUS_PATH "/proc/bus/usb"
bellard1f6e24e2006-06-26 21:00:51 +000079#define PRODUCT_NAME_SZ 32
balrogb9dc0332007-10-04 22:47:34 +000080#define MAX_ENDPOINTS 16
aliguori0f431522008-10-07 20:06:37 +000081#define USBDEVBUS_PATH "/dev/bus/usb"
82#define USBSYSBUS_PATH "/sys/bus/usb"
83
84static char *usb_host_device_path;
85
86#define USB_FS_NONE 0
87#define USB_FS_PROC 1
88#define USB_FS_DEV 2
89#define USB_FS_SYS 3
90
91static int usb_fs_type;
bellardbb36d472005-11-05 14:22:28 +000092
balrogb9dc0332007-10-04 22:47:34 +000093/* endpoint association data */
94struct endp_data {
95 uint8_t type;
aliguori64838172008-08-21 19:31:10 +000096 uint8_t halted;
balrogb9dc0332007-10-04 22:47:34 +000097};
98
aliguori446ab122008-09-14 01:06:09 +000099enum {
100 CTRL_STATE_IDLE = 0,
101 CTRL_STATE_SETUP,
102 CTRL_STATE_DATA,
103 CTRL_STATE_ACK
104};
105
106/*
107 * Control transfer state.
108 * Note that 'buffer' _must_ follow 'req' field because
109 * we need contigious buffer when we submit control URB.
110 */
111struct ctrl_struct {
112 uint16_t len;
113 uint16_t offset;
114 uint8_t state;
115 struct usb_ctrlrequest req;
Christian Krausefd7a4462010-01-24 17:34:52 +0100116 uint8_t buffer[8192];
aliguori446ab122008-09-14 01:06:09 +0000117};
118
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100119struct USBAutoFilter {
120 uint32_t bus_num;
121 uint32_t addr;
122 uint32_t vendor_id;
123 uint32_t product_id;
124};
125
bellardbb36d472005-11-05 14:22:28 +0000126typedef struct USBHostDevice {
127 USBDevice dev;
aliguori64838172008-08-21 19:31:10 +0000128 int fd;
129
130 uint8_t descr[1024];
131 int descr_len;
132 int configuration;
aliguori446ab122008-09-14 01:06:09 +0000133 int ninterfaces;
aliguori24772c12008-08-21 19:31:52 +0000134 int closing;
aliguori64838172008-08-21 19:31:10 +0000135
aliguori446ab122008-09-14 01:06:09 +0000136 struct ctrl_struct ctrl;
balrogb9dc0332007-10-04 22:47:34 +0000137 struct endp_data endp_table[MAX_ENDPOINTS];
aliguori4b096fc2008-08-21 19:28:55 +0000138
aliguori4b096fc2008-08-21 19:28:55 +0000139 /* Host side address */
140 int bus_num;
141 int addr;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100142 struct USBAutoFilter match;
aliguori4b096fc2008-08-21 19:28:55 +0000143
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100144 QTAILQ_ENTRY(USBHostDevice) next;
bellardbb36d472005-11-05 14:22:28 +0000145} USBHostDevice;
146
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100147static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
148
149static int usb_host_close(USBHostDevice *dev);
150static int parse_filter(const char *spec, struct USBAutoFilter *f);
151static void usb_host_auto_check(void *unused);
152
aliguori64838172008-08-21 19:31:10 +0000153static int is_isoc(USBHostDevice *s, int ep)
154{
155 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
156}
157
158static int is_halted(USBHostDevice *s, int ep)
159{
160 return s->endp_table[ep - 1].halted;
161}
162
163static void clear_halt(USBHostDevice *s, int ep)
164{
165 s->endp_table[ep - 1].halted = 0;
166}
167
168static void set_halt(USBHostDevice *s, int ep)
169{
170 s->endp_table[ep - 1].halted = 1;
171}
172
aliguori64838172008-08-21 19:31:10 +0000173/*
174 * Async URB state.
175 * We always allocate one isoc descriptor even for bulk transfers
176 * to simplify allocation and casts.
177 */
178typedef struct AsyncURB
balrogb9dc0332007-10-04 22:47:34 +0000179{
aliguori64838172008-08-21 19:31:10 +0000180 struct usbdevfs_urb urb;
181 struct usbdevfs_iso_packet_desc isocpd;
182
183 USBPacket *packet;
184 USBHostDevice *hdev;
185} AsyncURB;
186
187static AsyncURB *async_alloc(void)
188{
189 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
balrogb9dc0332007-10-04 22:47:34 +0000190}
191
aliguori64838172008-08-21 19:31:10 +0000192static void async_free(AsyncURB *aurb)
balrogb9dc0332007-10-04 22:47:34 +0000193{
aliguori64838172008-08-21 19:31:10 +0000194 qemu_free(aurb);
195}
balrogb9dc0332007-10-04 22:47:34 +0000196
aliguori446ab122008-09-14 01:06:09 +0000197static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
198{
199 switch(s->ctrl.state) {
200 case CTRL_STATE_SETUP:
201 if (p->len < s->ctrl.len)
202 s->ctrl.len = p->len;
203 s->ctrl.state = CTRL_STATE_DATA;
204 p->len = 8;
205 break;
206
207 case CTRL_STATE_ACK:
208 s->ctrl.state = CTRL_STATE_IDLE;
209 p->len = 0;
210 break;
211
212 default:
213 break;
214 }
215}
216
aliguori64838172008-08-21 19:31:10 +0000217static void async_complete(void *opaque)
218{
219 USBHostDevice *s = opaque;
220 AsyncURB *aurb;
balrogb9dc0332007-10-04 22:47:34 +0000221
aliguori64838172008-08-21 19:31:10 +0000222 while (1) {
223 USBPacket *p;
224
225 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
226 if (r < 0) {
227 if (errno == EAGAIN)
228 return;
229
aliguori24772c12008-08-21 19:31:52 +0000230 if (errno == ENODEV && !s->closing) {
aliguori64838172008-08-21 19:31:10 +0000231 printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100232 usb_host_close(s);
233 usb_host_auto_check(NULL);
aliguori64838172008-08-21 19:31:10 +0000234 return;
235 }
236
malcd0f2c4c2010-02-07 02:03:50 +0300237 DPRINTF("husb: async. reap urb failed errno %d\n", errno);
aliguori64838172008-08-21 19:31:10 +0000238 return;
balrogb9dc0332007-10-04 22:47:34 +0000239 }
aliguori64838172008-08-21 19:31:10 +0000240
241 p = aurb->packet;
242
malcd0f2c4c2010-02-07 02:03:50 +0300243 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
aliguori64838172008-08-21 19:31:10 +0000244 aurb, aurb->urb.status, aurb->urb.actual_length);
245
246 if (p) {
247 switch (aurb->urb.status) {
248 case 0:
249 p->len = aurb->urb.actual_length;
aliguori446ab122008-09-14 01:06:09 +0000250 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
251 async_complete_ctrl(s, p);
aliguori64838172008-08-21 19:31:10 +0000252 break;
253
254 case -EPIPE:
255 set_halt(s, p->devep);
Paul Bolledcc7e252009-10-13 13:40:08 +0200256 p->len = USB_RET_STALL;
257 break;
258
aliguori64838172008-08-21 19:31:10 +0000259 default:
260 p->len = USB_RET_NAK;
261 break;
262 }
263
264 usb_packet_complete(p);
265 }
266
267 async_free(aurb);
balrogb9dc0332007-10-04 22:47:34 +0000268 }
balrogb9dc0332007-10-04 22:47:34 +0000269}
270
aliguori64838172008-08-21 19:31:10 +0000271static void async_cancel(USBPacket *unused, void *opaque)
balrogb9dc0332007-10-04 22:47:34 +0000272{
aliguori64838172008-08-21 19:31:10 +0000273 AsyncURB *aurb = opaque;
274 USBHostDevice *s = aurb->hdev;
balrogb9dc0332007-10-04 22:47:34 +0000275
malcd0f2c4c2010-02-07 02:03:50 +0300276 DPRINTF("husb: async cancel. aurb %p\n", aurb);
balrogb9dc0332007-10-04 22:47:34 +0000277
aliguori64838172008-08-21 19:31:10 +0000278 /* Mark it as dead (see async_complete above) */
279 aurb->packet = NULL;
280
281 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
282 if (r < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300283 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
balrogb9dc0332007-10-04 22:47:34 +0000284 }
balrogb9dc0332007-10-04 22:47:34 +0000285}
286
aliguori446ab122008-09-14 01:06:09 +0000287static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
balrogb9dc0332007-10-04 22:47:34 +0000288{
289 int dev_descr_len, config_descr_len;
290 int interface, nb_interfaces, nb_configurations;
291 int ret, i;
292
293 if (configuration == 0) /* address state - ignore */
294 return 1;
295
malcd0f2c4c2010-02-07 02:03:50 +0300296 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
aliguori446ab122008-09-14 01:06:09 +0000297
balrogb9dc0332007-10-04 22:47:34 +0000298 i = 0;
299 dev_descr_len = dev->descr[0];
300 if (dev_descr_len > dev->descr_len)
301 goto fail;
302 nb_configurations = dev->descr[17];
303
304 i += dev_descr_len;
305 while (i < dev->descr_len) {
malcd0f2c4c2010-02-07 02:03:50 +0300306 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
balrogb9dc0332007-10-04 22:47:34 +0000307 dev->descr[i], dev->descr[i+1]);
aliguori64838172008-08-21 19:31:10 +0000308
balrogb9dc0332007-10-04 22:47:34 +0000309 if (dev->descr[i+1] != USB_DT_CONFIG) {
310 i += dev->descr[i];
311 continue;
312 }
313 config_descr_len = dev->descr[i];
314
aliguori64838172008-08-21 19:31:10 +0000315 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
aliguori1f3870a2008-08-21 19:27:48 +0000316
aliguori446ab122008-09-14 01:06:09 +0000317 if (configuration < 0 || configuration == dev->descr[i + 5]) {
318 configuration = dev->descr[i + 5];
balrogb9dc0332007-10-04 22:47:34 +0000319 break;
aliguori446ab122008-09-14 01:06:09 +0000320 }
balrogb9dc0332007-10-04 22:47:34 +0000321
322 i += config_descr_len;
323 }
324
325 if (i >= dev->descr_len) {
aliguori0d380642008-08-21 19:32:29 +0000326 fprintf(stderr, "husb: update iface failed. no matching configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000327 goto fail;
328 }
329 nb_interfaces = dev->descr[i + 4];
330
331#ifdef USBDEVFS_DISCONNECT
332 /* earlier Linux 2.4 do not support that */
333 {
334 struct usbdevfs_ioctl ctrl;
335 for (interface = 0; interface < nb_interfaces; interface++) {
336 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
337 ctrl.ifno = interface;
338 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
339 if (ret < 0 && errno != ENODATA) {
340 perror("USBDEVFS_DISCONNECT");
341 goto fail;
342 }
343 }
344 }
345#endif
346
347 /* XXX: only grab if all interfaces are free */
348 for (interface = 0; interface < nb_interfaces; interface++) {
349 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
350 if (ret < 0) {
351 if (errno == EBUSY) {
aliguori64838172008-08-21 19:31:10 +0000352 printf("husb: update iface. device already grabbed\n");
balrogb9dc0332007-10-04 22:47:34 +0000353 } else {
aliguori64838172008-08-21 19:31:10 +0000354 perror("husb: failed to claim interface");
balrogb9dc0332007-10-04 22:47:34 +0000355 }
356 fail:
357 return 0;
358 }
359 }
360
aliguori64838172008-08-21 19:31:10 +0000361 printf("husb: %d interfaces claimed for configuration %d\n",
balrogb9dc0332007-10-04 22:47:34 +0000362 nb_interfaces, configuration);
balrogb9dc0332007-10-04 22:47:34 +0000363
aliguori446ab122008-09-14 01:06:09 +0000364 dev->ninterfaces = nb_interfaces;
365 dev->configuration = configuration;
366 return 1;
367}
368
369static int usb_host_release_interfaces(USBHostDevice *s)
370{
371 int ret, i;
372
malcd0f2c4c2010-02-07 02:03:50 +0300373 DPRINTF("husb: releasing interfaces\n");
aliguori446ab122008-09-14 01:06:09 +0000374
375 for (i = 0; i < s->ninterfaces; i++) {
376 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
377 if (ret < 0) {
378 perror("husb: failed to release interface");
379 return 0;
380 }
381 }
382
balrogb9dc0332007-10-04 22:47:34 +0000383 return 1;
384}
385
bellard059809e2006-07-19 18:06:15 +0000386static void usb_host_handle_reset(USBDevice *dev)
bellardbb36d472005-11-05 14:22:28 +0000387{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100388 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
aliguori64838172008-08-21 19:31:10 +0000389
malcd0f2c4c2010-02-07 02:03:50 +0300390 DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr);
aliguori64838172008-08-21 19:31:10 +0000391
bellardbb36d472005-11-05 14:22:28 +0000392 ioctl(s->fd, USBDEVFS_RESET);
aliguori446ab122008-09-14 01:06:09 +0000393
394 usb_host_claim_interfaces(s, s->configuration);
ths5fafdf22007-09-16 21:08:06 +0000395}
bellardbb36d472005-11-05 14:22:28 +0000396
bellard059809e2006-07-19 18:06:15 +0000397static void usb_host_handle_destroy(USBDevice *dev)
398{
399 USBHostDevice *s = (USBHostDevice *)dev;
400
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100401 usb_host_close(s);
402 QTAILQ_REMOVE(&hostdevs, s, next);
bellard059809e2006-07-19 18:06:15 +0000403}
404
balrogb9dc0332007-10-04 22:47:34 +0000405static int usb_linux_update_endp_table(USBHostDevice *s);
406
aliguori446ab122008-09-14 01:06:09 +0000407static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
bellardbb36d472005-11-05 14:22:28 +0000408{
aliguori64838172008-08-21 19:31:10 +0000409 struct usbdevfs_urb *urb;
aliguori446ab122008-09-14 01:06:09 +0000410 AsyncURB *aurb;
bellardbb36d472005-11-05 14:22:28 +0000411 int ret;
412
aliguori64838172008-08-21 19:31:10 +0000413 aurb = async_alloc();
aliguori64838172008-08-21 19:31:10 +0000414 aurb->hdev = s;
415 aurb->packet = p;
balrogb9dc0332007-10-04 22:47:34 +0000416
aliguori64838172008-08-21 19:31:10 +0000417 urb = &aurb->urb;
418
pbrook4d611c92006-08-12 01:04:27 +0000419 if (p->pid == USB_TOKEN_IN)
aliguori64838172008-08-21 19:31:10 +0000420 urb->endpoint = p->devep | 0x80;
421 else
422 urb->endpoint = p->devep;
423
424 if (is_halted(s, p->devep)) {
425 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
426 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300427 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
aliguori64838172008-08-21 19:31:10 +0000428 urb->endpoint, errno);
bellardbb36d472005-11-05 14:22:28 +0000429 return USB_RET_NAK;
bellardbb36d472005-11-05 14:22:28 +0000430 }
aliguori64838172008-08-21 19:31:10 +0000431 clear_halt(s, p->devep);
balrog046833e2007-10-31 00:27:50 +0000432 }
433
aliguori64838172008-08-21 19:31:10 +0000434 urb->buffer = p->data;
balrogb9dc0332007-10-04 22:47:34 +0000435 urb->buffer_length = p->len;
aliguori64838172008-08-21 19:31:10 +0000436
437 if (is_isoc(s, p->devep)) {
438 /* Setup ISOC transfer */
439 urb->type = USBDEVFS_URB_TYPE_ISO;
440 urb->flags = USBDEVFS_URB_ISO_ASAP;
441 urb->number_of_packets = 1;
442 urb->iso_frame_desc[0].length = p->len;
balrogb9dc0332007-10-04 22:47:34 +0000443 } else {
aliguori64838172008-08-21 19:31:10 +0000444 /* Setup bulk transfer */
445 urb->type = USBDEVFS_URB_TYPE_BULK;
446 }
447
448 urb->usercontext = s;
449
450 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
451
malcd0f2c4c2010-02-07 02:03:50 +0300452 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
aliguori64838172008-08-21 19:31:10 +0000453
454 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300455 DPRINTF("husb: submit failed. errno %d\n", errno);
aliguori64838172008-08-21 19:31:10 +0000456 async_free(aurb);
457
balrogb9dc0332007-10-04 22:47:34 +0000458 switch(errno) {
459 case ETIMEDOUT:
460 return USB_RET_NAK;
461 case EPIPE:
462 default:
463 return USB_RET_STALL;
464 }
465 }
aliguori64838172008-08-21 19:31:10 +0000466
467 usb_defer_packet(p, async_cancel, aurb);
balrogb9dc0332007-10-04 22:47:34 +0000468 return USB_RET_ASYNC;
balrogb9dc0332007-10-04 22:47:34 +0000469}
470
aliguori446ab122008-09-14 01:06:09 +0000471static int ctrl_error(void)
472{
473 if (errno == ETIMEDOUT)
474 return USB_RET_NAK;
475 else
476 return USB_RET_STALL;
477}
478
479static int usb_host_set_address(USBHostDevice *s, int addr)
480{
malcd0f2c4c2010-02-07 02:03:50 +0300481 DPRINTF("husb: ctrl set addr %u\n", addr);
aliguori446ab122008-09-14 01:06:09 +0000482 s->dev.addr = addr;
483 return 0;
484}
485
486static int usb_host_set_config(USBHostDevice *s, int config)
487{
488 usb_host_release_interfaces(s);
489
490 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
491
malcd0f2c4c2010-02-07 02:03:50 +0300492 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
aliguori446ab122008-09-14 01:06:09 +0000493
494 if (ret < 0)
495 return ctrl_error();
496
497 usb_host_claim_interfaces(s, config);
498 return 0;
499}
500
501static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
502{
503 struct usbdevfs_setinterface si;
504 int ret;
505
506 si.interface = iface;
507 si.altsetting = alt;
508 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
509
malcd0f2c4c2010-02-07 02:03:50 +0300510 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
aliguori446ab122008-09-14 01:06:09 +0000511 iface, alt, ret, errno);
512
513 if (ret < 0)
514 return ctrl_error();
515
516 usb_linux_update_endp_table(s);
517 return 0;
518}
519
520static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
521{
522 struct usbdevfs_urb *urb;
523 AsyncURB *aurb;
524 int ret, value, index;
Jim Parisc4c0e232009-08-24 14:56:12 -0400525 int buffer_len;
aliguori446ab122008-09-14 01:06:09 +0000526
527 /*
528 * Process certain standard device requests.
529 * These are infrequent and are processed synchronously.
530 */
531 value = le16_to_cpu(s->ctrl.req.wValue);
532 index = le16_to_cpu(s->ctrl.req.wIndex);
533
malcd0f2c4c2010-02-07 02:03:50 +0300534 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
aliguori446ab122008-09-14 01:06:09 +0000535 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
536 s->ctrl.len);
537
538 if (s->ctrl.req.bRequestType == 0) {
539 switch (s->ctrl.req.bRequest) {
540 case USB_REQ_SET_ADDRESS:
541 return usb_host_set_address(s, value);
542
543 case USB_REQ_SET_CONFIGURATION:
544 return usb_host_set_config(s, value & 0xff);
545 }
546 }
547
548 if (s->ctrl.req.bRequestType == 1 &&
549 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
550 return usb_host_set_interface(s, index, value);
551
552 /* The rest are asynchronous */
553
Jim Parisc4c0e232009-08-24 14:56:12 -0400554 buffer_len = 8 + s->ctrl.len;
555 if (buffer_len > sizeof(s->ctrl.buffer)) {
malcb2e3b6e2009-09-12 03:18:18 +0400556 fprintf(stderr, "husb: ctrl buffer too small (%u > %zu)\n",
557 buffer_len, sizeof(s->ctrl.buffer));
558 return USB_RET_STALL;
Jim Parisc4c0e232009-08-24 14:56:12 -0400559 }
560
aliguori446ab122008-09-14 01:06:09 +0000561 aurb = async_alloc();
aliguori446ab122008-09-14 01:06:09 +0000562 aurb->hdev = s;
563 aurb->packet = p;
564
565 /*
566 * Setup ctrl transfer.
567 *
568 * s->ctrl is layed out such that data buffer immediately follows
569 * 'req' struct which is exactly what usbdevfs expects.
570 */
571 urb = &aurb->urb;
572
573 urb->type = USBDEVFS_URB_TYPE_CONTROL;
574 urb->endpoint = p->devep;
575
576 urb->buffer = &s->ctrl.req;
Jim Parisc4c0e232009-08-24 14:56:12 -0400577 urb->buffer_length = buffer_len;
aliguori446ab122008-09-14 01:06:09 +0000578
579 urb->usercontext = s;
580
581 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
582
malcd0f2c4c2010-02-07 02:03:50 +0300583 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
aliguori446ab122008-09-14 01:06:09 +0000584
585 if (ret < 0) {
malcd0f2c4c2010-02-07 02:03:50 +0300586 DPRINTF("husb: submit failed. errno %d\n", errno);
aliguori446ab122008-09-14 01:06:09 +0000587 async_free(aurb);
588
589 switch(errno) {
590 case ETIMEDOUT:
591 return USB_RET_NAK;
592 case EPIPE:
593 default:
594 return USB_RET_STALL;
595 }
596 }
597
598 usb_defer_packet(p, async_cancel, aurb);
599 return USB_RET_ASYNC;
600}
601
602static int do_token_setup(USBDevice *dev, USBPacket *p)
603{
604 USBHostDevice *s = (USBHostDevice *) dev;
605 int ret = 0;
606
607 if (p->len != 8)
608 return USB_RET_STALL;
609
610 memcpy(&s->ctrl.req, p->data, 8);
611 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
612 s->ctrl.offset = 0;
613 s->ctrl.state = CTRL_STATE_SETUP;
614
615 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
616 ret = usb_host_handle_control(s, p);
617 if (ret < 0)
618 return ret;
619
620 if (ret < s->ctrl.len)
621 s->ctrl.len = ret;
622 s->ctrl.state = CTRL_STATE_DATA;
623 } else {
624 if (s->ctrl.len == 0)
625 s->ctrl.state = CTRL_STATE_ACK;
626 else
627 s->ctrl.state = CTRL_STATE_DATA;
628 }
629
630 return ret;
631}
632
633static int do_token_in(USBDevice *dev, USBPacket *p)
634{
635 USBHostDevice *s = (USBHostDevice *) dev;
636 int ret = 0;
637
638 if (p->devep != 0)
639 return usb_host_handle_data(s, p);
640
641 switch(s->ctrl.state) {
642 case CTRL_STATE_ACK:
643 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
644 ret = usb_host_handle_control(s, p);
645 if (ret == USB_RET_ASYNC)
646 return USB_RET_ASYNC;
647
648 s->ctrl.state = CTRL_STATE_IDLE;
649 return ret > 0 ? 0 : ret;
650 }
651
652 return 0;
653
654 case CTRL_STATE_DATA:
655 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
656 int len = s->ctrl.len - s->ctrl.offset;
657 if (len > p->len)
658 len = p->len;
659 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
660 s->ctrl.offset += len;
661 if (s->ctrl.offset >= s->ctrl.len)
662 s->ctrl.state = CTRL_STATE_ACK;
663 return len;
664 }
665
666 s->ctrl.state = CTRL_STATE_IDLE;
667 return USB_RET_STALL;
668
669 default:
670 return USB_RET_STALL;
671 }
672}
673
674static int do_token_out(USBDevice *dev, USBPacket *p)
675{
676 USBHostDevice *s = (USBHostDevice *) dev;
677
678 if (p->devep != 0)
679 return usb_host_handle_data(s, p);
680
681 switch(s->ctrl.state) {
682 case CTRL_STATE_ACK:
683 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
684 s->ctrl.state = CTRL_STATE_IDLE;
685 /* transfer OK */
686 } else {
687 /* ignore additional output */
688 }
689 return 0;
690
691 case CTRL_STATE_DATA:
692 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
693 int len = s->ctrl.len - s->ctrl.offset;
694 if (len > p->len)
695 len = p->len;
696 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
697 s->ctrl.offset += len;
698 if (s->ctrl.offset >= s->ctrl.len)
699 s->ctrl.state = CTRL_STATE_ACK;
700 return len;
701 }
702
703 s->ctrl.state = CTRL_STATE_IDLE;
704 return USB_RET_STALL;
705
706 default:
707 return USB_RET_STALL;
708 }
709}
710
711/*
712 * Packet handler.
713 * Called by the HC (host controller).
714 *
715 * Returns length of the transaction or one of the USB_RET_XXX codes.
716 */
blueswir1d9cf1572008-09-15 14:57:11 +0000717static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
aliguori446ab122008-09-14 01:06:09 +0000718{
719 switch(p->pid) {
720 case USB_MSG_ATTACH:
721 s->state = USB_STATE_ATTACHED;
722 return 0;
723
724 case USB_MSG_DETACH:
725 s->state = USB_STATE_NOTATTACHED;
726 return 0;
727
728 case USB_MSG_RESET:
729 s->remote_wakeup = 0;
730 s->addr = 0;
731 s->state = USB_STATE_DEFAULT;
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200732 s->info->handle_reset(s);
aliguori446ab122008-09-14 01:06:09 +0000733 return 0;
734 }
735
736 /* Rest of the PIDs must match our address */
737 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
738 return USB_RET_NODEV;
739
740 switch (p->pid) {
741 case USB_TOKEN_SETUP:
742 return do_token_setup(s, p);
743
744 case USB_TOKEN_IN:
745 return do_token_in(s, p);
746
747 case USB_TOKEN_OUT:
748 return do_token_out(s, p);
749
750 default:
751 return USB_RET_STALL;
752 }
753}
754
balrogb9dc0332007-10-04 22:47:34 +0000755/* returns 1 on problem encountered or 0 for success */
756static int usb_linux_update_endp_table(USBHostDevice *s)
757{
758 uint8_t *descriptors;
759 uint8_t devep, type, configuration, alt_interface;
pbrooke41b3912008-10-28 18:22:59 +0000760 struct usb_ctrltransfer ct;
balrogb9dc0332007-10-04 22:47:34 +0000761 int interface, ret, length, i;
762
763 ct.bRequestType = USB_DIR_IN;
764 ct.bRequest = USB_REQ_GET_CONFIGURATION;
765 ct.wValue = 0;
766 ct.wIndex = 0;
767 ct.wLength = 1;
768 ct.data = &configuration;
769 ct.timeout = 50;
770
771 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
772 if (ret < 0) {
773 perror("usb_linux_update_endp_table");
774 return 1;
775 }
776
777 /* in address state */
778 if (configuration == 0)
779 return 1;
780
781 /* get the desired configuration, interface, and endpoint descriptors
782 * from device description */
783 descriptors = &s->descr[18];
784 length = s->descr_len - 18;
785 i = 0;
786
787 if (descriptors[i + 1] != USB_DT_CONFIG ||
788 descriptors[i + 5] != configuration) {
malcd0f2c4c2010-02-07 02:03:50 +0300789 DPRINTF("invalid descriptor data - configuration\n");
balrogb9dc0332007-10-04 22:47:34 +0000790 return 1;
791 }
792 i += descriptors[i];
793
794 while (i < length) {
795 if (descriptors[i + 1] != USB_DT_INTERFACE ||
796 (descriptors[i + 1] == USB_DT_INTERFACE &&
797 descriptors[i + 4] == 0)) {
798 i += descriptors[i];
799 continue;
800 }
801
802 interface = descriptors[i + 2];
803
804 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
805 ct.bRequest = USB_REQ_GET_INTERFACE;
806 ct.wValue = 0;
807 ct.wIndex = interface;
808 ct.wLength = 1;
809 ct.data = &alt_interface;
810 ct.timeout = 50;
811
812 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
813 if (ret < 0) {
Jason Wesseld55ebf52009-05-18 10:00:28 -0500814 alt_interface = interface;
balrogb9dc0332007-10-04 22:47:34 +0000815 }
816
817 /* the current interface descriptor is the active interface
818 * and has endpoints */
819 if (descriptors[i + 3] != alt_interface) {
820 i += descriptors[i];
821 continue;
822 }
823
824 /* advance to the endpoints */
825 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
826 i += descriptors[i];
827
828 if (i >= length)
829 break;
830
831 while (i < length) {
832 if (descriptors[i + 1] != USB_DT_ENDPOINT)
833 break;
834
835 devep = descriptors[i + 2];
836 switch (descriptors[i + 3] & 0x3) {
837 case 0x00:
838 type = USBDEVFS_URB_TYPE_CONTROL;
839 break;
840 case 0x01:
841 type = USBDEVFS_URB_TYPE_ISO;
842 break;
843 case 0x02:
844 type = USBDEVFS_URB_TYPE_BULK;
845 break;
846 case 0x03:
847 type = USBDEVFS_URB_TYPE_INTERRUPT;
848 break;
balrogb9dc0332007-10-04 22:47:34 +0000849 }
850 s->endp_table[(devep & 0xf) - 1].type = type;
aliguori64838172008-08-21 19:31:10 +0000851 s->endp_table[(devep & 0xf) - 1].halted = 0;
balrogb9dc0332007-10-04 22:47:34 +0000852
853 i += descriptors[i];
854 }
855 }
856 return 0;
857}
858
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100859static int usb_host_open(USBHostDevice *dev, int bus_num,
860 int addr, const char *prod_name)
bellardbb36d472005-11-05 14:22:28 +0000861{
balrogb9dc0332007-10-04 22:47:34 +0000862 int fd = -1, ret;
bellardbb36d472005-11-05 14:22:28 +0000863 struct usbdevfs_connectinfo ci;
bellarda594cfb2005-11-06 16:13:29 +0000864 char buf[1024];
aliguori1f3870a2008-08-21 19:27:48 +0000865
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100866 if (dev->fd != -1)
867 goto fail;
868
aliguori64838172008-08-21 19:31:10 +0000869 printf("husb: open device %d.%d\n", bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000870
aliguori0f431522008-10-07 20:06:37 +0000871 if (!usb_host_device_path) {
872 perror("husb: USB Host Device Path not set");
873 goto fail;
874 }
875 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
bellarda594cfb2005-11-06 16:13:29 +0000876 bus_num, addr);
balrogb9dc0332007-10-04 22:47:34 +0000877 fd = open(buf, O_RDWR | O_NONBLOCK);
bellardbb36d472005-11-05 14:22:28 +0000878 if (fd < 0) {
bellarda594cfb2005-11-06 16:13:29 +0000879 perror(buf);
aliguori1f3870a2008-08-21 19:27:48 +0000880 goto fail;
bellardbb36d472005-11-05 14:22:28 +0000881 }
malcd0f2c4c2010-02-07 02:03:50 +0300882 DPRINTF("husb: opened %s\n", buf);
bellardbb36d472005-11-05 14:22:28 +0000883
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200884 dev->bus_num = bus_num;
885 dev->addr = addr;
Gerd Hoffmann22f84e72009-09-25 16:55:28 +0200886 dev->fd = fd;
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200887
balrogb9dc0332007-10-04 22:47:34 +0000888 /* read the device description */
889 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
890 if (dev->descr_len <= 0) {
aliguori64838172008-08-21 19:31:10 +0000891 perror("husb: reading device data failed");
bellardbb36d472005-11-05 14:22:28 +0000892 goto fail;
893 }
ths3b46e622007-09-17 08:09:54 +0000894
balrogb9dc0332007-10-04 22:47:34 +0000895#ifdef DEBUG
bellard868bfe22005-11-13 21:53:15 +0000896 {
balrogb9dc0332007-10-04 22:47:34 +0000897 int x;
898 printf("=== begin dumping device descriptor data ===\n");
899 for (x = 0; x < dev->descr_len; x++)
900 printf("%02x ", dev->descr[x]);
901 printf("\n=== end dumping device descriptor data ===\n");
bellarda594cfb2005-11-06 16:13:29 +0000902 }
903#endif
904
balrogb9dc0332007-10-04 22:47:34 +0000905
aliguori446ab122008-09-14 01:06:09 +0000906 /*
907 * Initial configuration is -1 which makes us claim first
908 * available config. We used to start with 1, which does not
909 * always work. I've seen devices where first config starts
910 * with 2.
911 */
912 if (!usb_host_claim_interfaces(dev, -1))
balrogb9dc0332007-10-04 22:47:34 +0000913 goto fail;
bellardbb36d472005-11-05 14:22:28 +0000914
915 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
916 if (ret < 0) {
balrog046833e2007-10-31 00:27:50 +0000917 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
bellardbb36d472005-11-05 14:22:28 +0000918 goto fail;
919 }
920
aliguori64838172008-08-21 19:31:10 +0000921 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
bellardbb36d472005-11-05 14:22:28 +0000922
balrogb9dc0332007-10-04 22:47:34 +0000923 ret = usb_linux_update_endp_table(dev);
924 if (ret)
bellardbb36d472005-11-05 14:22:28 +0000925 goto fail;
balrogb9dc0332007-10-04 22:47:34 +0000926
bellardbb36d472005-11-05 14:22:28 +0000927 if (ci.slow)
928 dev->dev.speed = USB_SPEED_LOW;
929 else
930 dev->dev.speed = USB_SPEED_HIGH;
bellardbb36d472005-11-05 14:22:28 +0000931
aliguori4b096fc2008-08-21 19:28:55 +0000932 if (!prod_name || prod_name[0] == '\0')
Markus Armbruster0fe6d122009-12-09 17:07:51 +0100933 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
aliguori4b096fc2008-08-21 19:28:55 +0000934 "host:%d.%d", bus_num, addr);
bellard1f6e24e2006-06-26 21:00:51 +0000935 else
Markus Armbruster0fe6d122009-12-09 17:07:51 +0100936 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
aliguori4b096fc2008-08-21 19:28:55 +0000937 prod_name);
bellard1f6e24e2006-06-26 21:00:51 +0000938
aliguori64838172008-08-21 19:31:10 +0000939 /* USB devio uses 'write' flag to check for async completions */
940 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
aliguori1f3870a2008-08-21 19:27:48 +0000941
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100942 usb_device_attach(&dev->dev);
943 return 0;
aliguori4b096fc2008-08-21 19:28:55 +0000944
balrogb9dc0332007-10-04 22:47:34 +0000945fail:
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100946 dev->fd = -1;
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200947 if (fd != -1)
948 close(fd);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100949 return -1;
950}
951
952static int usb_host_close(USBHostDevice *dev)
953{
954 if (dev->fd == -1)
955 return -1;
956
957 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
958 dev->closing = 1;
959 async_complete(dev);
960 dev->closing = 0;
961 usb_device_detach(&dev->dev);
962 close(dev->fd);
963 dev->fd = -1;
964 return 0;
965}
966
967static int usb_host_initfn(USBDevice *dev)
968{
969 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
970
971 dev->auto_attach = 0;
972 s->fd = -1;
973 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
974 usb_host_auto_check(NULL);
975 return 0;
bellardbb36d472005-11-05 14:22:28 +0000976}
977
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200978static struct USBDeviceInfo usb_host_dev_info = {
Markus Armbruster06384692009-12-09 17:07:52 +0100979 .product_desc = "USB Host Device",
Markus Armbruster556cd092009-12-09 17:07:53 +0100980 .qdev.name = "usb-host",
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200981 .qdev.size = sizeof(USBHostDevice),
982 .init = usb_host_initfn,
983 .handle_packet = usb_host_handle_packet,
984 .handle_reset = usb_host_handle_reset,
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200985 .handle_destroy = usb_host_handle_destroy,
Gerd Hoffmann26a9e822009-10-26 15:56:50 +0100986 .usbdevice_name = "host",
987 .usbdevice_init = usb_host_device_open,
988 .qdev.props = (Property[]) {
989 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
990 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
991 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
992 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
993 DEFINE_PROP_END_OF_LIST(),
994 },
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200995};
996
997static void usb_host_register_devices(void)
998{
999 usb_qdev_register(&usb_host_dev_info);
1000}
1001device_init(usb_host_register_devices)
1002
aliguori4b096fc2008-08-21 19:28:55 +00001003USBDevice *usb_host_device_open(const char *devname)
1004{
Markus Armbruster0745eb12009-11-27 13:05:53 +01001005 struct USBAutoFilter filter;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001006 USBDevice *dev;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001007 char *p;
1008
Markus Armbruster556cd092009-12-09 17:07:53 +01001009 dev = usb_create(NULL /* FIXME */, "usb-host");
aliguori4b096fc2008-08-21 19:28:55 +00001010
aliguori5d0c5752008-09-14 01:07:41 +00001011 if (strstr(devname, "auto:")) {
Markus Armbruster0745eb12009-11-27 13:05:53 +01001012 if (parse_filter(devname, &filter) < 0)
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001013 goto fail;
1014 } else {
1015 if ((p = strchr(devname, '.'))) {
Markus Armbruster0745eb12009-11-27 13:05:53 +01001016 filter.bus_num = strtoul(devname, NULL, 0);
1017 filter.addr = strtoul(p + 1, NULL, 0);
1018 filter.vendor_id = 0;
1019 filter.product_id = 0;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001020 } else if ((p = strchr(devname, ':'))) {
Markus Armbruster0745eb12009-11-27 13:05:53 +01001021 filter.bus_num = 0;
1022 filter.addr = 0;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001023 filter.vendor_id = strtoul(devname, NULL, 16);
Markus Armbruster0745eb12009-11-27 13:05:53 +01001024 filter.product_id = strtoul(p + 1, NULL, 16);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001025 } else {
1026 goto fail;
1027 }
aliguori5d0c5752008-09-14 01:07:41 +00001028 }
1029
Markus Armbruster0745eb12009-11-27 13:05:53 +01001030 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1031 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001032 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1033 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
Kevin Wolfbeb6f0d2010-01-15 12:56:41 +01001034 qdev_init_nofail(&dev->qdev);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001035 return dev;
aliguori4b096fc2008-08-21 19:28:55 +00001036
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001037fail:
1038 qdev_free(&dev->qdev);
1039 return NULL;
aliguori4b096fc2008-08-21 19:28:55 +00001040}
aliguori5d0c5752008-09-14 01:07:41 +00001041
1042int usb_host_device_close(const char *devname)
1043{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001044#if 0
aliguori5d0c5752008-09-14 01:07:41 +00001045 char product_name[PRODUCT_NAME_SZ];
1046 int bus_num, addr;
1047 USBHostDevice *s;
1048
1049 if (strstr(devname, "auto:"))
1050 return usb_host_auto_del(devname);
1051
1052 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1053 devname) < 0)
1054 return -1;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001055
aliguori5d0c5752008-09-14 01:07:41 +00001056 s = hostdev_find(bus_num, addr);
1057 if (s) {
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001058 usb_device_delete_addr(s->bus_num, s->dev.addr);
aliguori5d0c5752008-09-14 01:07:41 +00001059 return 0;
1060 }
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001061#endif
aliguori5d0c5752008-09-14 01:07:41 +00001062
1063 return -1;
1064}
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001065
bellarda594cfb2005-11-06 16:13:29 +00001066static int get_tag_value(char *buf, int buf_size,
ths5fafdf22007-09-16 21:08:06 +00001067 const char *str, const char *tag,
bellarda594cfb2005-11-06 16:13:29 +00001068 const char *stopchars)
bellardbb36d472005-11-05 14:22:28 +00001069{
bellarda594cfb2005-11-06 16:13:29 +00001070 const char *p;
1071 char *q;
1072 p = strstr(str, tag);
1073 if (!p)
1074 return -1;
1075 p += strlen(tag);
blueswir147398b92008-11-22 20:04:24 +00001076 while (qemu_isspace(*p))
bellarda594cfb2005-11-06 16:13:29 +00001077 p++;
1078 q = buf;
1079 while (*p != '\0' && !strchr(stopchars, *p)) {
1080 if ((q - buf) < (buf_size - 1))
1081 *q++ = *p;
1082 p++;
1083 }
1084 *q = '\0';
1085 return q - buf;
1086}
bellardbb36d472005-11-05 14:22:28 +00001087
aliguori0f431522008-10-07 20:06:37 +00001088/*
1089 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1090 * host's USB devices. This is legacy support since many distributions
1091 * are moving to /sys/bus/usb
1092 */
1093static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
bellarda594cfb2005-11-06 16:13:29 +00001094{
Blue Swirl660f11b2009-07-31 21:16:51 +00001095 FILE *f = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001096 char line[1024];
1097 char buf[1024];
1098 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
bellarda594cfb2005-11-06 16:13:29 +00001099 char product_name[512];
aliguori0f431522008-10-07 20:06:37 +00001100 int ret = 0;
ths3b46e622007-09-17 08:09:54 +00001101
aliguori0f431522008-10-07 20:06:37 +00001102 if (!usb_host_device_path) {
1103 perror("husb: USB Host Device Path not set");
1104 goto the_end;
bellarda594cfb2005-11-06 16:13:29 +00001105 }
aliguori0f431522008-10-07 20:06:37 +00001106 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1107 f = fopen(line, "r");
1108 if (!f) {
1109 perror("husb: cannot open devices file");
1110 goto the_end;
1111 }
1112
bellarda594cfb2005-11-06 16:13:29 +00001113 device_count = 0;
1114 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
bellardbb36d472005-11-05 14:22:28 +00001115 for(;;) {
bellarda594cfb2005-11-06 16:13:29 +00001116 if (fgets(line, sizeof(line), f) == NULL)
bellardbb36d472005-11-05 14:22:28 +00001117 break;
bellarda594cfb2005-11-06 16:13:29 +00001118 if (strlen(line) > 0)
1119 line[strlen(line) - 1] = '\0';
1120 if (line[0] == 'T' && line[1] == ':') {
pbrook38ca0f62006-03-11 18:03:38 +00001121 if (device_count && (vendor_id || product_id)) {
1122 /* New device. Add the previously discovered device. */
ths5fafdf22007-09-16 21:08:06 +00001123 ret = func(opaque, bus_num, addr, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001124 product_id, product_name, speed);
1125 if (ret)
1126 goto the_end;
1127 }
1128 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
1129 goto fail;
1130 bus_num = atoi(buf);
1131 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
1132 goto fail;
1133 addr = atoi(buf);
1134 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
1135 goto fail;
1136 if (!strcmp(buf, "480"))
1137 speed = USB_SPEED_HIGH;
1138 else if (!strcmp(buf, "1.5"))
1139 speed = USB_SPEED_LOW;
1140 else
1141 speed = USB_SPEED_FULL;
1142 product_name[0] = '\0';
1143 class_id = 0xff;
1144 device_count++;
1145 product_id = 0;
1146 vendor_id = 0;
1147 } else if (line[0] == 'P' && line[1] == ':') {
1148 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
1149 goto fail;
1150 vendor_id = strtoul(buf, NULL, 16);
1151 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
1152 goto fail;
1153 product_id = strtoul(buf, NULL, 16);
1154 } else if (line[0] == 'S' && line[1] == ':') {
1155 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
1156 goto fail;
1157 pstrcpy(product_name, sizeof(product_name), buf);
1158 } else if (line[0] == 'D' && line[1] == ':') {
1159 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
1160 goto fail;
1161 class_id = strtoul(buf, NULL, 16);
1162 }
1163 fail: ;
1164 }
pbrook38ca0f62006-03-11 18:03:38 +00001165 if (device_count && (vendor_id || product_id)) {
1166 /* Add the last device. */
ths5fafdf22007-09-16 21:08:06 +00001167 ret = func(opaque, bus_num, addr, class_id, vendor_id,
bellarda594cfb2005-11-06 16:13:29 +00001168 product_id, product_name, speed);
1169 }
1170 the_end:
aliguori0f431522008-10-07 20:06:37 +00001171 if (f)
1172 fclose(f);
1173 return ret;
1174}
1175
1176/*
1177 * Read sys file-system device file
1178 *
1179 * @line address of buffer to put file contents in
1180 * @line_size size of line
1181 * @device_file path to device file (printf format string)
1182 * @device_name device being opened (inserted into device_file)
1183 *
1184 * @return 0 failed, 1 succeeded ('line' contains data)
1185 */
1186static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
1187{
1188 FILE *f;
1189 int ret = 0;
1190 char filename[PATH_MAX];
1191
blueswir1b4e237a2008-12-28 15:45:20 +00001192 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1193 device_file);
aliguori0f431522008-10-07 20:06:37 +00001194 f = fopen(filename, "r");
1195 if (f) {
Kirill A. Shutemov9f99cee2010-01-20 00:56:17 +01001196 ret = fgets(line, line_size, f) != NULL;
aliguori0f431522008-10-07 20:06:37 +00001197 fclose(f);
aliguori0f431522008-10-07 20:06:37 +00001198 }
1199
1200 return ret;
1201}
1202
1203/*
1204 * Use /sys/bus/usb/devices/ directory to determine host's USB
1205 * devices.
1206 *
1207 * This code is based on Robert Schiele's original patches posted to
1208 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1209 */
1210static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1211{
Blue Swirl660f11b2009-07-31 21:16:51 +00001212 DIR *dir = NULL;
aliguori0f431522008-10-07 20:06:37 +00001213 char line[1024];
1214 int bus_num, addr, speed, class_id, product_id, vendor_id;
1215 int ret = 0;
1216 char product_name[512];
1217 struct dirent *de;
1218
1219 dir = opendir(USBSYSBUS_PATH "/devices");
1220 if (!dir) {
1221 perror("husb: cannot open devices directory");
1222 goto the_end;
1223 }
1224
1225 while ((de = readdir(dir))) {
1226 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1227 char *tmpstr = de->d_name;
1228 if (!strncmp(de->d_name, "usb", 3))
1229 tmpstr += 3;
1230 bus_num = atoi(tmpstr);
1231
blueswir1b4e237a2008-12-28 15:45:20 +00001232 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001233 goto the_end;
1234 if (sscanf(line, "%d", &addr) != 1)
1235 goto the_end;
1236
blueswir1b4e237a2008-12-28 15:45:20 +00001237 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1238 de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001239 goto the_end;
1240 if (sscanf(line, "%x", &class_id) != 1)
1241 goto the_end;
1242
blueswir1b4e237a2008-12-28 15:45:20 +00001243 if (!usb_host_read_file(line, sizeof(line), "idVendor", de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001244 goto the_end;
1245 if (sscanf(line, "%x", &vendor_id) != 1)
1246 goto the_end;
1247
blueswir1b4e237a2008-12-28 15:45:20 +00001248 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1249 de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001250 goto the_end;
1251 if (sscanf(line, "%x", &product_id) != 1)
1252 goto the_end;
1253
blueswir1b4e237a2008-12-28 15:45:20 +00001254 if (!usb_host_read_file(line, sizeof(line), "product",
1255 de->d_name)) {
aliguori0f431522008-10-07 20:06:37 +00001256 *product_name = 0;
1257 } else {
1258 if (strlen(line) > 0)
1259 line[strlen(line) - 1] = '\0';
1260 pstrcpy(product_name, sizeof(product_name), line);
1261 }
1262
blueswir1b4e237a2008-12-28 15:45:20 +00001263 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name))
aliguori0f431522008-10-07 20:06:37 +00001264 goto the_end;
1265 if (!strcmp(line, "480\n"))
1266 speed = USB_SPEED_HIGH;
1267 else if (!strcmp(line, "1.5\n"))
1268 speed = USB_SPEED_LOW;
1269 else
1270 speed = USB_SPEED_FULL;
1271
1272 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1273 product_id, product_name, speed);
1274 if (ret)
1275 goto the_end;
1276 }
1277 }
1278 the_end:
1279 if (dir)
1280 closedir(dir);
1281 return ret;
1282}
1283
1284/*
1285 * Determine how to access the host's USB devices and call the
1286 * specific support function.
1287 */
1288static int usb_host_scan(void *opaque, USBScanFunc *func)
1289{
aliguori376253e2009-03-05 23:01:23 +00001290 Monitor *mon = cur_mon;
Blue Swirl660f11b2009-07-31 21:16:51 +00001291 FILE *f = NULL;
1292 DIR *dir = NULL;
aliguori0f431522008-10-07 20:06:37 +00001293 int ret = 0;
aliguori0f431522008-10-07 20:06:37 +00001294 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1295 char devpath[PATH_MAX];
1296
1297 /* only check the host once */
1298 if (!usb_fs_type) {
Mark McLoughlin55496242009-07-03 09:28:02 +01001299 dir = opendir(USBSYSBUS_PATH "/devices");
1300 if (dir) {
1301 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1302 strcpy(devpath, USBDEVBUS_PATH);
1303 usb_fs_type = USB_FS_SYS;
1304 closedir(dir);
malcd0f2c4c2010-02-07 02:03:50 +03001305 DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
Mark McLoughlin55496242009-07-03 09:28:02 +01001306 goto found_devices;
1307 }
aliguori0f431522008-10-07 20:06:37 +00001308 f = fopen(USBPROCBUS_PATH "/devices", "r");
1309 if (f) {
1310 /* devices found in /proc/bus/usb/ */
1311 strcpy(devpath, USBPROCBUS_PATH);
1312 usb_fs_type = USB_FS_PROC;
1313 fclose(f);
malcd0f2c4c2010-02-07 02:03:50 +03001314 DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001315 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001316 }
1317 /* try additional methods if an access method hasn't been found yet */
1318 f = fopen(USBDEVBUS_PATH "/devices", "r");
aliguorif16a0db2008-10-21 16:34:20 +00001319 if (f) {
aliguori0f431522008-10-07 20:06:37 +00001320 /* devices found in /dev/bus/usb/ */
1321 strcpy(devpath, USBDEVBUS_PATH);
1322 usb_fs_type = USB_FS_DEV;
1323 fclose(f);
malcd0f2c4c2010-02-07 02:03:50 +03001324 DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
aliguorif16a0db2008-10-21 16:34:20 +00001325 goto found_devices;
aliguori0f431522008-10-07 20:06:37 +00001326 }
aliguorif16a0db2008-10-21 16:34:20 +00001327 found_devices:
aliguori22babeb2008-10-21 16:27:28 +00001328 if (!usb_fs_type) {
Gerd Hoffmanneba6fe82009-12-15 11:43:02 +01001329 if (mon)
1330 monitor_printf(mon, "husb: unable to access USB devices\n");
aliguorif16a0db2008-10-21 16:34:20 +00001331 return -ENOENT;
aliguori0f431522008-10-07 20:06:37 +00001332 }
1333
1334 /* the module setting (used later for opening devices) */
1335 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
aliguori1eec6142009-02-05 22:06:18 +00001336 strcpy(usb_host_device_path, devpath);
Gerd Hoffmanneba6fe82009-12-15 11:43:02 +01001337 if (mon)
1338 monitor_printf(mon, "husb: using %s file-system with %s\n",
1339 fs_type[usb_fs_type], usb_host_device_path);
aliguori0f431522008-10-07 20:06:37 +00001340 }
1341
1342 switch (usb_fs_type) {
1343 case USB_FS_PROC:
1344 case USB_FS_DEV:
1345 ret = usb_host_scan_dev(opaque, func);
1346 break;
1347 case USB_FS_SYS:
1348 ret = usb_host_scan_sys(opaque, func);
1349 break;
aliguorif16a0db2008-10-21 16:34:20 +00001350 default:
1351 ret = -EINVAL;
1352 break;
aliguori0f431522008-10-07 20:06:37 +00001353 }
bellarda594cfb2005-11-06 16:13:29 +00001354 return ret;
1355}
1356
aliguori4b096fc2008-08-21 19:28:55 +00001357static QEMUTimer *usb_auto_timer;
aliguori4b096fc2008-08-21 19:28:55 +00001358
1359static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001360 int class_id, int vendor_id, int product_id,
1361 const char *product_name, int speed)
aliguori4b096fc2008-08-21 19:28:55 +00001362{
1363 struct USBAutoFilter *f;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001364 struct USBHostDevice *s;
aliguori4b096fc2008-08-21 19:28:55 +00001365
1366 /* Ignore hubs */
1367 if (class_id == 9)
1368 return 0;
1369
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001370 QTAILQ_FOREACH(s, &hostdevs, next) {
1371 f = &s->match;
1372
1373 if (f->bus_num > 0 && f->bus_num != bus_num)
aliguori4b096fc2008-08-21 19:28:55 +00001374 continue;
1375
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001376 if (f->addr > 0 && f->addr != addr)
aliguori4b096fc2008-08-21 19:28:55 +00001377 continue;
1378
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001379 if (f->vendor_id > 0 && f->vendor_id != vendor_id)
aliguori4b096fc2008-08-21 19:28:55 +00001380 continue;
1381
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001382 if (f->product_id > 0 && f->product_id != product_id)
aliguori4b096fc2008-08-21 19:28:55 +00001383 continue;
1384
1385 /* We got a match */
1386
Markus Armbruster33e66b82009-10-07 01:15:57 +02001387 /* Already attached ? */
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001388 if (s->fd != -1)
aliguori4b096fc2008-08-21 19:28:55 +00001389 return 0;
1390
malcd0f2c4c2010-02-07 02:03:50 +03001391 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
aliguori4b096fc2008-08-21 19:28:55 +00001392
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001393 usb_host_open(s, bus_num, addr, product_name);
aliguori4b096fc2008-08-21 19:28:55 +00001394 }
1395
1396 return 0;
1397}
1398
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001399static void usb_host_auto_check(void *unused)
aliguori4b096fc2008-08-21 19:28:55 +00001400{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001401 struct USBHostDevice *s;
1402 int unconnected = 0;
1403
aliguori4b096fc2008-08-21 19:28:55 +00001404 usb_host_scan(NULL, usb_host_auto_scan);
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001405
1406 QTAILQ_FOREACH(s, &hostdevs, next) {
1407 if (s->fd == -1)
1408 unconnected++;
1409 }
1410
1411 if (unconnected == 0) {
1412 /* nothing to watch */
1413 if (usb_auto_timer)
1414 qemu_del_timer(usb_auto_timer);
1415 return;
1416 }
1417
1418 if (!usb_auto_timer) {
1419 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_check, NULL);
1420 if (!usb_auto_timer)
1421 return;
1422 }
aliguori4b096fc2008-08-21 19:28:55 +00001423 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1424}
1425
1426/*
aliguori5d0c5752008-09-14 01:07:41 +00001427 * Autoconnect filter
1428 * Format:
1429 * auto:bus:dev[:vid:pid]
1430 * auto:bus.dev[:vid:pid]
1431 *
1432 * bus - bus number (dec, * means any)
1433 * dev - device number (dec, * means any)
1434 * vid - vendor id (hex, * means any)
1435 * pid - product id (hex, * means any)
1436 *
1437 * See 'lsusb' output.
aliguori4b096fc2008-08-21 19:28:55 +00001438 */
aliguori5d0c5752008-09-14 01:07:41 +00001439static int parse_filter(const char *spec, struct USBAutoFilter *f)
aliguori4b096fc2008-08-21 19:28:55 +00001440{
aliguori5d0c5752008-09-14 01:07:41 +00001441 enum { BUS, DEV, VID, PID, DONE };
1442 const char *p = spec;
1443 int i;
1444
Markus Armbruster0745eb12009-11-27 13:05:53 +01001445 f->bus_num = 0;
1446 f->addr = 0;
1447 f->vendor_id = 0;
1448 f->product_id = 0;
aliguori5d0c5752008-09-14 01:07:41 +00001449
1450 for (i = BUS; i < DONE; i++) {
1451 p = strpbrk(p, ":.");
1452 if (!p) break;
1453 p++;
1454
1455 if (*p == '*')
1456 continue;
1457
1458 switch(i) {
1459 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1460 case DEV: f->addr = strtol(p, NULL, 10); break;
1461 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1462 case PID: f->product_id = strtol(p, NULL, 16); break;
1463 }
aliguori4b096fc2008-08-21 19:28:55 +00001464 }
1465
aliguori5d0c5752008-09-14 01:07:41 +00001466 if (i < DEV) {
1467 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1468 return -1;
1469 }
1470
1471 return 0;
1472}
1473
bellarda594cfb2005-11-06 16:13:29 +00001474/**********************/
1475/* USB host device info */
bellardbb36d472005-11-05 14:22:28 +00001476
bellarda594cfb2005-11-06 16:13:29 +00001477struct usb_class_info {
1478 int class;
1479 const char *class_name;
1480};
1481
1482static const struct usb_class_info usb_class_info[] = {
1483 { USB_CLASS_AUDIO, "Audio"},
1484 { USB_CLASS_COMM, "Communication"},
1485 { USB_CLASS_HID, "HID"},
1486 { USB_CLASS_HUB, "Hub" },
1487 { USB_CLASS_PHYSICAL, "Physical" },
1488 { USB_CLASS_PRINTER, "Printer" },
1489 { USB_CLASS_MASS_STORAGE, "Storage" },
1490 { USB_CLASS_CDC_DATA, "Data" },
1491 { USB_CLASS_APP_SPEC, "Application Specific" },
1492 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1493 { USB_CLASS_STILL_IMAGE, "Still Image" },
balrogb9dc0332007-10-04 22:47:34 +00001494 { USB_CLASS_CSCID, "Smart Card" },
bellarda594cfb2005-11-06 16:13:29 +00001495 { USB_CLASS_CONTENT_SEC, "Content Security" },
1496 { -1, NULL }
1497};
1498
1499static const char *usb_class_str(uint8_t class)
1500{
1501 const struct usb_class_info *p;
1502 for(p = usb_class_info; p->class != -1; p++) {
1503 if (p->class == class)
bellardbb36d472005-11-05 14:22:28 +00001504 break;
bellardbb36d472005-11-05 14:22:28 +00001505 }
bellarda594cfb2005-11-06 16:13:29 +00001506 return p->class_name;
bellardbb36d472005-11-05 14:22:28 +00001507}
1508
Blue Swirl179da8a2009-09-07 19:00:18 +00001509static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
pbrook9596ebb2007-11-18 01:44:38 +00001510 int vendor_id, int product_id,
1511 const char *product_name,
1512 int speed)
bellardbb36d472005-11-05 14:22:28 +00001513{
bellarda594cfb2005-11-06 16:13:29 +00001514 const char *class_str, *speed_str;
1515
1516 switch(speed) {
ths5fafdf22007-09-16 21:08:06 +00001517 case USB_SPEED_LOW:
1518 speed_str = "1.5";
bellarda594cfb2005-11-06 16:13:29 +00001519 break;
ths5fafdf22007-09-16 21:08:06 +00001520 case USB_SPEED_FULL:
1521 speed_str = "12";
bellarda594cfb2005-11-06 16:13:29 +00001522 break;
ths5fafdf22007-09-16 21:08:06 +00001523 case USB_SPEED_HIGH:
1524 speed_str = "480";
bellarda594cfb2005-11-06 16:13:29 +00001525 break;
1526 default:
ths5fafdf22007-09-16 21:08:06 +00001527 speed_str = "?";
bellarda594cfb2005-11-06 16:13:29 +00001528 break;
bellardbb36d472005-11-05 14:22:28 +00001529 }
bellarda594cfb2005-11-06 16:13:29 +00001530
aliguori376253e2009-03-05 23:01:23 +00001531 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
bellarda594cfb2005-11-06 16:13:29 +00001532 bus_num, addr, speed_str);
1533 class_str = usb_class_str(class_id);
ths5fafdf22007-09-16 21:08:06 +00001534 if (class_str)
aliguori376253e2009-03-05 23:01:23 +00001535 monitor_printf(mon, " %s:", class_str);
bellarda594cfb2005-11-06 16:13:29 +00001536 else
aliguori376253e2009-03-05 23:01:23 +00001537 monitor_printf(mon, " Class %02x:", class_id);
1538 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
bellarda594cfb2005-11-06 16:13:29 +00001539 if (product_name[0] != '\0')
aliguori376253e2009-03-05 23:01:23 +00001540 monitor_printf(mon, ", %s", product_name);
1541 monitor_printf(mon, "\n");
bellarda594cfb2005-11-06 16:13:29 +00001542}
1543
ths5fafdf22007-09-16 21:08:06 +00001544static int usb_host_info_device(void *opaque, int bus_num, int addr,
bellarda594cfb2005-11-06 16:13:29 +00001545 int class_id,
ths5fafdf22007-09-16 21:08:06 +00001546 int vendor_id, int product_id,
bellarda594cfb2005-11-06 16:13:29 +00001547 const char *product_name,
1548 int speed)
1549{
Blue Swirl179da8a2009-09-07 19:00:18 +00001550 Monitor *mon = opaque;
1551
1552 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
bellarda594cfb2005-11-06 16:13:29 +00001553 product_name, speed);
1554 return 0;
1555}
1556
aliguoriac4ffb52008-09-22 15:04:31 +00001557static void dec2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001558{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001559 if (val == 0)
aliguoriac4ffb52008-09-22 15:04:31 +00001560 snprintf(str, size, "*");
aliguori5d0c5752008-09-14 01:07:41 +00001561 else
aliguoriac4ffb52008-09-22 15:04:31 +00001562 snprintf(str, size, "%d", val);
aliguori5d0c5752008-09-14 01:07:41 +00001563}
1564
aliguoriac4ffb52008-09-22 15:04:31 +00001565static void hex2str(int val, char *str, size_t size)
aliguori5d0c5752008-09-14 01:07:41 +00001566{
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001567 if (val == 0)
aliguoriac4ffb52008-09-22 15:04:31 +00001568 snprintf(str, size, "*");
aliguori5d0c5752008-09-14 01:07:41 +00001569 else
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001570 snprintf(str, size, "%04x", val);
aliguori5d0c5752008-09-14 01:07:41 +00001571}
1572
aliguori376253e2009-03-05 23:01:23 +00001573void usb_host_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00001574{
aliguori5d0c5752008-09-14 01:07:41 +00001575 struct USBAutoFilter *f;
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001576 struct USBHostDevice *s;
aliguori5d0c5752008-09-14 01:07:41 +00001577
Blue Swirl179da8a2009-09-07 19:00:18 +00001578 usb_host_scan(mon, usb_host_info_device);
aliguori5d0c5752008-09-14 01:07:41 +00001579
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001580 if (QTAILQ_EMPTY(&hostdevs))
1581 return;
1582 monitor_printf(mon, " Auto filters:\n");
1583 QTAILQ_FOREACH(s, &hostdevs, next) {
aliguori5d0c5752008-09-14 01:07:41 +00001584 char bus[10], addr[10], vid[10], pid[10];
Gerd Hoffmann26a9e822009-10-26 15:56:50 +01001585 f = &s->match;
aliguoriac4ffb52008-09-22 15:04:31 +00001586 dec2str(f->bus_num, bus, sizeof(bus));
1587 dec2str(f->addr, addr, sizeof(addr));
1588 hex2str(f->vendor_id, vid, sizeof(vid));
1589 hex2str(f->product_id, pid, sizeof(pid));
aliguori376253e2009-03-05 23:01:23 +00001590 monitor_printf(mon, " Device %s.%s ID %s:%s\n",
1591 bus, addr, vid, pid);
aliguori5d0c5752008-09-14 01:07:41 +00001592 }
bellardbb36d472005-11-05 14:22:28 +00001593}