2
* Linux host USB redirector
4
* Copyright (c) 2005 Fabrice Bellard
6
* Copyright (c) 2008 Max Krasnyansky
7
* Support for host device auto connect & disconnect
8
* Major rewrite to support fully async operation
10
* 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
14
* 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:
21
* The above copyright notice and this permission notice shall be included in
22
* all copies or substantial portions of the Software.
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
33
#include "qemu-common.h"
34
#include "qemu-timer.h"
40
#include <sys/ioctl.h>
42
#include <linux/usbdevice_fs.h>
43
#include <linux/version.h>
46
/* We redefine it to avoid version problems */
47
struct usb_ctrltransfer {
57
typedef int USBScanFunc(void *opaque, int bus_num, int addr, const char *port,
58
int class_id, int vendor_id, int product_id,
59
const char *product_name, int speed);
64
#define DPRINTF printf
69
#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
71
#define USBPROCBUS_PATH "/proc/bus/usb"
72
#define PRODUCT_NAME_SZ 32
73
#define MAX_ENDPOINTS 15
74
#define MAX_PORTLEN 16
75
#define USBDEVBUS_PATH "/dev/bus/usb"
76
#define USBSYSBUS_PATH "/sys/bus/usb"
78
static char *usb_host_device_path;
85
static int usb_fs_type;
87
/* endpoint association data */
88
#define ISO_FRAME_DESC_PER_URB 32
89
#define INVALID_EP_TYPE 255
91
/* devio.c limits single requests to 16k */
92
#define MAX_USBFS_BUFFER_SIZE 16384
94
typedef struct AsyncURB AsyncURB;
107
struct USBAutoFilter {
115
typedef struct USBHostDevice {
125
uint32_t iso_urb_count;
128
struct endp_data ep_in[MAX_ENDPOINTS];
129
struct endp_data ep_out[MAX_ENDPOINTS];
130
QLIST_HEAD(, AsyncURB) aurbs;
132
/* Host side address */
135
char port[MAX_PORTLEN];
136
struct USBAutoFilter match;
139
QTAILQ_ENTRY(USBHostDevice) next;
142
static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
144
static int usb_host_close(USBHostDevice *dev);
145
static int parse_filter(const char *spec, struct USBAutoFilter *f);
146
static void usb_host_auto_check(void *unused);
147
static int usb_host_read_file(char *line, size_t line_size,
148
const char *device_file, const char *device_name);
149
static int usb_linux_update_endp_table(USBHostDevice *s);
151
static int usb_host_do_reset(USBHostDevice *dev)
157
gettimeofday(&s, NULL);
158
ret = ioctl(dev->fd, USBDEVFS_RESET);
159
gettimeofday(&e, NULL);
160
usecs = (e.tv_sec - s.tv_sec) * 1000000;
161
usecs += e.tv_usec - s.tv_usec;
162
if (usecs > 1000000) {
163
/* more than a second, something is fishy, broken usb device? */
164
fprintf(stderr, "husb: device %d:%d reset took %d.%06d seconds\n",
165
dev->bus_num, dev->addr, usecs / 1000000, usecs % 1000000);
170
static struct endp_data *get_endp(USBHostDevice *s, int pid, int ep)
172
struct endp_data *eps = pid == USB_TOKEN_IN ? s->ep_in : s->ep_out;
173
assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
174
assert(ep > 0 && ep <= MAX_ENDPOINTS);
178
static int is_isoc(USBHostDevice *s, int pid, int ep)
180
return get_endp(s, pid, ep)->type == USBDEVFS_URB_TYPE_ISO;
183
static int is_valid(USBHostDevice *s, int pid, int ep)
185
return get_endp(s, pid, ep)->type != INVALID_EP_TYPE;
188
static int is_halted(USBHostDevice *s, int pid, int ep)
190
return get_endp(s, pid, ep)->halted;
193
static void clear_halt(USBHostDevice *s, int pid, int ep)
195
trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep);
196
get_endp(s, pid, ep)->halted = 0;
199
static void set_halt(USBHostDevice *s, int pid, int ep)
202
trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep);
203
get_endp(s, pid, ep)->halted = 1;
207
static int is_iso_started(USBHostDevice *s, int pid, int ep)
209
return get_endp(s, pid, ep)->iso_started;
212
static void clear_iso_started(USBHostDevice *s, int pid, int ep)
214
trace_usb_host_ep_stop_iso(s->bus_num, s->addr, ep);
215
get_endp(s, pid, ep)->iso_started = 0;
218
static void set_iso_started(USBHostDevice *s, int pid, int ep)
220
struct endp_data *e = get_endp(s, pid, ep);
222
trace_usb_host_ep_start_iso(s->bus_num, s->addr, ep);
223
if (!e->iso_started) {
229
static int change_iso_inflight(USBHostDevice *s, int pid, int ep, int value)
231
struct endp_data *e = get_endp(s, pid, ep);
233
e->inflight += value;
237
static void set_iso_urb(USBHostDevice *s, int pid, int ep, AsyncURB *iso_urb)
239
get_endp(s, pid, ep)->iso_urb = iso_urb;
242
static AsyncURB *get_iso_urb(USBHostDevice *s, int pid, int ep)
244
return get_endp(s, pid, ep)->iso_urb;
247
static void set_iso_urb_idx(USBHostDevice *s, int pid, int ep, int i)
249
get_endp(s, pid, ep)->iso_urb_idx = i;
252
static int get_iso_urb_idx(USBHostDevice *s, int pid, int ep)
254
return get_endp(s, pid, ep)->iso_urb_idx;
257
static void set_iso_buffer_used(USBHostDevice *s, int pid, int ep, int i)
259
get_endp(s, pid, ep)->iso_buffer_used = i;
262
static int get_iso_buffer_used(USBHostDevice *s, int pid, int ep)
264
return get_endp(s, pid, ep)->iso_buffer_used;
267
static void set_max_packet_size(USBHostDevice *s, int pid, int ep,
270
int raw = descriptor[4] + (descriptor[5] << 8);
271
int size, microframes;
274
switch ((raw >> 11) & 3) {
275
case 1: microframes = 2; break;
276
case 2: microframes = 3; break;
277
default: microframes = 1; break;
279
get_endp(s, pid, ep)->max_packet_size = size * microframes;
282
static int get_max_packet_size(USBHostDevice *s, int pid, int ep)
284
return get_endp(s, pid, ep)->max_packet_size;
289
* We always allocate iso packet descriptors even for bulk transfers
290
* to simplify allocation and casts.
294
struct usbdevfs_urb urb;
295
struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
297
QLIST_ENTRY(AsyncURB) next;
299
/* For regular async urbs */
301
int more; /* large transfer, more urbs follow */
303
/* For buffered iso handling */
304
int iso_frame_idx; /* -1 means in flight */
307
static AsyncURB *async_alloc(USBHostDevice *s)
309
AsyncURB *aurb = g_malloc0(sizeof(AsyncURB));
311
QLIST_INSERT_HEAD(&s->aurbs, aurb, next);
315
static void async_free(AsyncURB *aurb)
317
QLIST_REMOVE(aurb, next);
321
static void do_disconnect(USBHostDevice *s)
324
usb_host_auto_check(NULL);
327
static void async_complete(void *opaque)
329
USBHostDevice *s = opaque;
336
int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
338
if (errno == EAGAIN) {
340
fprintf(stderr, "husb: %d iso urbs finished at once\n", urbs);
344
if (errno == ENODEV) {
346
trace_usb_host_disconnect(s->bus_num, s->addr);
352
perror("USBDEVFS_REAPURBNDELAY");
356
DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
357
aurb, aurb->urb.status, aurb->urb.actual_length);
359
/* If this is a buffered iso urb mark it as complete and don't do
360
anything else (it is handled further in usb_host_handle_iso_data) */
361
if (aurb->iso_frame_idx == -1) {
363
int pid = (aurb->urb.endpoint & USB_DIR_IN) ?
364
USB_TOKEN_IN : USB_TOKEN_OUT;
365
int ep = aurb->urb.endpoint & 0xf;
366
if (aurb->urb.status == -EPIPE) {
367
set_halt(s, pid, ep);
369
aurb->iso_frame_idx = 0;
371
inflight = change_iso_inflight(s, pid, ep, -1);
372
if (inflight == 0 && is_iso_started(s, pid, ep)) {
373
fprintf(stderr, "husb: out of buffers for iso stream\n");
379
trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status,
380
aurb->urb.actual_length, aurb->more);
383
switch (aurb->urb.status) {
385
p->result += aurb->urb.actual_length;
389
set_halt(s, p->pid, p->devep);
390
p->result = USB_RET_STALL;
394
p->result = USB_RET_NAK;
398
if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
399
trace_usb_host_req_complete(s->bus_num, s->addr, p->result);
400
usb_generic_async_ctrl_complete(&s->dev, p);
401
} else if (!aurb->more) {
402
trace_usb_host_req_complete(s->bus_num, s->addr, p->result);
403
usb_packet_complete(&s->dev, p);
411
static void usb_host_async_cancel(USBDevice *dev, USBPacket *p)
413
USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
416
QLIST_FOREACH(aurb, &s->aurbs, next) {
417
if (p != aurb->packet) {
421
DPRINTF("husb: async cancel: packet %p, aurb %p\n", p, aurb);
423
/* Mark it as dead (see async_complete above) */
426
int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
428
DPRINTF("husb: async. discard urb failed errno %d\n", errno);
433
static int usb_host_claim_port(USBHostDevice *s)
435
#ifdef USBDEVFS_CLAIM_PORT
436
char *h, hub_name[64], line[1024];
437
int hub_addr, portnr, ret;
439
snprintf(hub_name, sizeof(hub_name), "%d-%s",
440
s->match.bus_num, s->match.port);
442
/* try strip off last ".$portnr" to get hub */
443
h = strrchr(hub_name, '.');
448
/* no dot in there -> it is the root hub */
449
snprintf(hub_name, sizeof(hub_name), "usb%d",
451
portnr = atoi(s->match.port);
454
if (!usb_host_read_file(line, sizeof(line), "devnum",
458
if (sscanf(line, "%d", &hub_addr) != 1) {
462
if (!usb_host_device_path) {
465
snprintf(line, sizeof(line), "%s/%03d/%03d",
466
usb_host_device_path, s->match.bus_num, hub_addr);
467
s->hub_fd = open(line, O_RDWR | O_NONBLOCK);
472
ret = ioctl(s->hub_fd, USBDEVFS_CLAIM_PORT, &portnr);
479
trace_usb_host_claim_port(s->match.bus_num, hub_addr, portnr);
486
static int usb_host_disconnect_ifaces(USBHostDevice *dev, int nb_interfaces)
488
/* earlier Linux 2.4 do not support that */
489
#ifdef USBDEVFS_DISCONNECT
490
struct usbdevfs_ioctl ctrl;
493
for (interface = 0; interface < nb_interfaces; interface++) {
494
ctrl.ioctl_code = USBDEVFS_DISCONNECT;
495
ctrl.ifno = interface;
497
ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
498
if (ret < 0 && errno != ENODATA) {
499
perror("USBDEVFS_DISCONNECT");
507
static int usb_linux_get_num_interfaces(USBHostDevice *s)
509
char device_name[64], line[1024];
510
int num_interfaces = 0;
512
if (usb_fs_type != USB_FS_SYS) {
516
sprintf(device_name, "%d-%s", s->bus_num, s->port);
517
if (!usb_host_read_file(line, sizeof(line), "bNumInterfaces",
521
if (sscanf(line, "%d", &num_interfaces) != 1) {
524
return num_interfaces;
527
static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
529
const char *op = NULL;
530
int dev_descr_len, config_descr_len;
531
int interface, nb_interfaces;
534
if (configuration == 0) { /* address state - ignore */
535
dev->ninterfaces = 0;
536
dev->configuration = 0;
540
DPRINTF("husb: claiming interfaces. config %d\n", configuration);
543
dev_descr_len = dev->descr[0];
544
if (dev_descr_len > dev->descr_len) {
545
fprintf(stderr, "husb: update iface failed. descr too short\n");
550
while (i < dev->descr_len) {
551
DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
553
dev->descr[i], dev->descr[i+1]);
555
if (dev->descr[i+1] != USB_DT_CONFIG) {
559
config_descr_len = dev->descr[i];
561
DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
563
if (configuration == dev->descr[i + 5]) {
564
configuration = dev->descr[i + 5];
568
i += config_descr_len;
571
if (i >= dev->descr_len) {
573
"husb: update iface failed. no matching configuration\n");
576
nb_interfaces = dev->descr[i + 4];
578
if (usb_host_disconnect_ifaces(dev, nb_interfaces) < 0) {
582
/* XXX: only grab if all interfaces are free */
583
for (interface = 0; interface < nb_interfaces; interface++) {
584
op = "USBDEVFS_CLAIMINTERFACE";
585
ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
591
trace_usb_host_claim_interfaces(dev->bus_num, dev->addr,
592
nb_interfaces, configuration);
594
dev->ninterfaces = nb_interfaces;
595
dev->configuration = configuration;
599
if (errno == ENODEV) {
606
static int usb_host_release_interfaces(USBHostDevice *s)
610
trace_usb_host_release_interfaces(s->bus_num, s->addr);
612
for (i = 0; i < s->ninterfaces; i++) {
613
ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
615
perror("USBDEVFS_RELEASEINTERFACE");
622
static void usb_host_handle_reset(USBDevice *dev)
624
USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
626
trace_usb_host_reset(s->bus_num, s->addr);
628
usb_host_do_reset(s);;
630
usb_host_claim_interfaces(s, 0);
631
usb_linux_update_endp_table(s);
634
static void usb_host_handle_destroy(USBDevice *dev)
636
USBHostDevice *s = (USBHostDevice *)dev;
639
if (s->hub_fd != -1) {
642
QTAILQ_REMOVE(&hostdevs, s, next);
643
qemu_remove_exit_notifier(&s->exit);
646
/* iso data is special, we need to keep enough urbs in flight to make sure
647
that the controller never runs out of them, otherwise the device will
648
likely suffer a buffer underrun / overrun. */
649
static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, int pid, uint8_t ep)
652
int i, j, len = get_max_packet_size(s, pid, ep);
654
aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb));
655
for (i = 0; i < s->iso_urb_count; i++) {
656
aurb[i].urb.endpoint = ep;
657
aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len;
658
aurb[i].urb.buffer = g_malloc(aurb[i].urb.buffer_length);
659
aurb[i].urb.type = USBDEVFS_URB_TYPE_ISO;
660
aurb[i].urb.flags = USBDEVFS_URB_ISO_ASAP;
661
aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB;
662
for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++)
663
aurb[i].urb.iso_frame_desc[j].length = len;
664
if (pid == USB_TOKEN_IN) {
665
aurb[i].urb.endpoint |= 0x80;
666
/* Mark as fully consumed (idle) */
667
aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB;
670
set_iso_urb(s, pid, ep, aurb);
675
static void usb_host_stop_n_free_iso(USBHostDevice *s, int pid, uint8_t ep)
678
int i, ret, killed = 0, free = 1;
680
aurb = get_iso_urb(s, pid, ep);
685
for (i = 0; i < s->iso_urb_count; i++) {
687
if (aurb[i].iso_frame_idx == -1) {
688
ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]);
690
perror("USBDEVFS_DISCARDURB");
698
/* Make sure any urbs we've killed are reaped before we free them */
703
for (i = 0; i < s->iso_urb_count; i++) {
704
g_free(aurb[i].urb.buffer);
710
printf("husb: leaking iso urbs because of discard failure\n");
711
set_iso_urb(s, pid, ep, NULL);
712
set_iso_urb_idx(s, pid, ep, 0);
713
clear_iso_started(s, pid, ep);
716
static int urb_status_to_usb_ret(int status)
720
return USB_RET_STALL;
726
static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
729
int i, j, ret, max_packet_size, offset, len = 0;
732
max_packet_size = get_max_packet_size(s, p->pid, p->devep);
733
if (max_packet_size == 0)
736
aurb = get_iso_urb(s, p->pid, p->devep);
738
aurb = usb_host_alloc_iso(s, p->pid, p->devep);
741
i = get_iso_urb_idx(s, p->pid, p->devep);
742
j = aurb[i].iso_frame_idx;
743
if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) {
745
/* Check urb status */
746
if (aurb[i].urb.status) {
747
len = urb_status_to_usb_ret(aurb[i].urb.status);
748
/* Move to the next urb */
749
aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
750
/* Check frame status */
751
} else if (aurb[i].urb.iso_frame_desc[j].status) {
752
len = urb_status_to_usb_ret(
753
aurb[i].urb.iso_frame_desc[j].status);
754
/* Check the frame fits */
755
} else if (aurb[i].urb.iso_frame_desc[j].actual_length
757
printf("husb: received iso data is larger then packet\n");
759
/* All good copy data over */
761
len = aurb[i].urb.iso_frame_desc[j].actual_length;
762
buf = aurb[i].urb.buffer +
763
j * aurb[i].urb.iso_frame_desc[0].length;
764
usb_packet_copy(p, buf, len);
768
offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->pid, p->devep);
770
/* Check the frame fits */
771
if (len > max_packet_size) {
772
printf("husb: send iso data is larger then max packet size\n");
776
/* All good copy data over */
777
usb_packet_copy(p, aurb[i].urb.buffer + offset, len);
778
aurb[i].urb.iso_frame_desc[j].length = len;
780
set_iso_buffer_used(s, p->pid, p->devep, offset);
782
/* Start the stream once we have buffered enough data */
783
if (!is_iso_started(s, p->pid, p->devep) && i == 1 && j == 8) {
784
set_iso_started(s, p->pid, p->devep);
787
aurb[i].iso_frame_idx++;
788
if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
789
i = (i + 1) % s->iso_urb_count;
790
set_iso_urb_idx(s, p->pid, p->devep, i);
794
set_iso_started(s, p->pid, p->devep);
796
DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
800
if (is_iso_started(s, p->pid, p->devep)) {
801
/* (Re)-submit all fully consumed / filled urbs */
802
for (i = 0; i < s->iso_urb_count; i++) {
803
if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
804
ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]);
806
perror("USBDEVFS_SUBMITURB");
807
if (!in || len == 0) {
819
aurb[i].iso_frame_idx = -1;
820
change_iso_inflight(s, p->pid, p->devep, 1);
828
static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
830
USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
831
struct usbdevfs_urb *urb;
833
int ret, rem, prem, v;
837
trace_usb_host_req_data(s->bus_num, s->addr,
838
p->pid == USB_TOKEN_IN,
839
p->devep, p->iov.size);
841
if (!is_valid(s, p->pid, p->devep)) {
842
trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
846
if (p->pid == USB_TOKEN_IN) {
847
ep = p->devep | 0x80;
852
if (is_halted(s, p->pid, p->devep)) {
853
unsigned int arg = ep;
854
ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg);
856
perror("USBDEVFS_CLEAR_HALT");
857
trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
860
clear_halt(s, p->pid, p->devep);
863
if (is_isoc(s, p->pid, p->devep)) {
864
return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
868
prem = p->iov.iov[v].iov_len;
869
pbuf = p->iov.iov[v].iov_base;
874
assert(v < p->iov.niov);
875
prem = p->iov.iov[v].iov_len;
876
pbuf = p->iov.iov[v].iov_base;
879
aurb = async_alloc(s);
884
urb->type = USBDEVFS_URB_TYPE_BULK;
885
urb->usercontext = s;
887
urb->buffer_length = prem;
889
if (urb->buffer_length > MAX_USBFS_BUFFER_SIZE) {
890
urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
892
pbuf += urb->buffer_length;
893
prem -= urb->buffer_length;
894
rem -= urb->buffer_length;
899
trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
900
urb->buffer_length, aurb->more);
901
ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
903
DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
904
urb->endpoint, urb->buffer_length, aurb->more, p, aurb);
907
perror("USBDEVFS_SUBMITURB");
912
trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
916
trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_STALL);
917
return USB_RET_STALL;
922
return USB_RET_ASYNC;
925
static int ctrl_error(void)
927
if (errno == ETIMEDOUT) {
930
return USB_RET_STALL;
934
static int usb_host_set_address(USBHostDevice *s, int addr)
936
trace_usb_host_set_address(s->bus_num, s->addr, addr);
941
static int usb_host_set_config(USBHostDevice *s, int config)
945
trace_usb_host_set_config(s->bus_num, s->addr, config);
947
usb_host_release_interfaces(s);
950
ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
952
DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
954
if (ret < 0 && errno == EBUSY && first) {
955
/* happens if usb device is in use by host drivers */
956
int count = usb_linux_get_num_interfaces(s);
958
DPRINTF("husb: busy -> disconnecting %d interfaces\n", count);
959
usb_host_disconnect_ifaces(s, count);
968
usb_host_claim_interfaces(s, config);
969
usb_linux_update_endp_table(s);
973
static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
975
struct usbdevfs_setinterface si;
978
trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
980
for (i = 1; i <= MAX_ENDPOINTS; i++) {
981
if (is_isoc(s, USB_TOKEN_IN, i)) {
982
usb_host_stop_n_free_iso(s, USB_TOKEN_IN, i);
984
if (is_isoc(s, USB_TOKEN_OUT, i)) {
985
usb_host_stop_n_free_iso(s, USB_TOKEN_OUT, i);
989
si.interface = iface;
991
ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
993
DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
994
iface, alt, ret, errno);
999
usb_linux_update_endp_table(s);
1003
static int usb_host_handle_control(USBDevice *dev, USBPacket *p,
1004
int request, int value, int index, int length, uint8_t *data)
1006
USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1007
struct usbdevfs_urb *urb;
1012
* Process certain standard device requests.
1013
* These are infrequent and are processed synchronously.
1016
/* Note request is (bRequestType << 8) | bRequest */
1017
trace_usb_host_req_control(s->bus_num, s->addr, request, value, index);
1020
case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1021
return usb_host_set_address(s, value);
1023
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1024
return usb_host_set_config(s, value & 0xff);
1026
case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1027
return usb_host_set_interface(s, index, value);
1030
/* The rest are asynchronous */
1032
if (length > sizeof(dev->data_buf)) {
1033
fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
1034
length, sizeof(dev->data_buf));
1035
return USB_RET_STALL;
1038
aurb = async_alloc(s);
1042
* Setup ctrl transfer.
1044
* s->ctrl is laid out such that data buffer immediately follows
1045
* 'req' struct which is exactly what usbdevfs expects.
1049
urb->type = USBDEVFS_URB_TYPE_CONTROL;
1050
urb->endpoint = p->devep;
1052
urb->buffer = &dev->setup_buf;
1053
urb->buffer_length = length + 8;
1055
urb->usercontext = s;
1057
trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
1058
urb->buffer_length, aurb->more);
1059
ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
1061
DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
1064
DPRINTF("husb: submit failed. errno %d\n", errno);
1072
return USB_RET_STALL;
1076
return USB_RET_ASYNC;
1079
static uint8_t usb_linux_get_alt_setting(USBHostDevice *s,
1080
uint8_t configuration, uint8_t interface)
1082
uint8_t alt_setting;
1083
struct usb_ctrltransfer ct;
1086
if (usb_fs_type == USB_FS_SYS) {
1087
char device_name[64], line[1024];
1090
sprintf(device_name, "%d-%s:%d.%d", s->bus_num, s->port,
1091
(int)configuration, (int)interface);
1093
if (!usb_host_read_file(line, sizeof(line), "bAlternateSetting",
1097
if (sscanf(line, "%d", &alt_setting) != 1) {
1104
ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
1105
ct.bRequest = USB_REQ_GET_INTERFACE;
1107
ct.wIndex = interface;
1109
ct.data = &alt_setting;
1111
ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
1113
/* Assume alt 0 on error */
1120
/* returns 1 on problem encountered or 0 for success */
1121
static int usb_linux_update_endp_table(USBHostDevice *s)
1123
uint8_t *descriptors;
1124
uint8_t devep, type, alt_interface;
1125
int interface, length, i, ep, pid;
1126
struct endp_data *epd;
1128
for (i = 0; i < MAX_ENDPOINTS; i++) {
1129
s->ep_in[i].type = INVALID_EP_TYPE;
1130
s->ep_out[i].type = INVALID_EP_TYPE;
1133
if (s->configuration == 0) {
1134
/* not configured yet -- leave all endpoints disabled */
1138
/* get the desired configuration, interface, and endpoint descriptors
1139
* from device description */
1140
descriptors = &s->descr[18];
1141
length = s->descr_len - 18;
1144
if (descriptors[i + 1] != USB_DT_CONFIG ||
1145
descriptors[i + 5] != s->configuration) {
1146
fprintf(stderr, "invalid descriptor data - configuration %d\n",
1150
i += descriptors[i];
1152
while (i < length) {
1153
if (descriptors[i + 1] != USB_DT_INTERFACE ||
1154
(descriptors[i + 1] == USB_DT_INTERFACE &&
1155
descriptors[i + 4] == 0)) {
1156
i += descriptors[i];
1160
interface = descriptors[i + 2];
1161
alt_interface = usb_linux_get_alt_setting(s, s->configuration,
1164
/* the current interface descriptor is the active interface
1165
* and has endpoints */
1166
if (descriptors[i + 3] != alt_interface) {
1167
i += descriptors[i];
1171
/* advance to the endpoints */
1172
while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
1173
i += descriptors[i];
1179
while (i < length) {
1180
if (descriptors[i + 1] != USB_DT_ENDPOINT) {
1184
devep = descriptors[i + 2];
1185
pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1188
fprintf(stderr, "usb-linux: invalid ep descriptor, ep == 0\n");
1192
switch (descriptors[i + 3] & 0x3) {
1194
type = USBDEVFS_URB_TYPE_CONTROL;
1197
type = USBDEVFS_URB_TYPE_ISO;
1198
set_max_packet_size(s, pid, ep, descriptors + i);
1201
type = USBDEVFS_URB_TYPE_BULK;
1204
type = USBDEVFS_URB_TYPE_INTERRUPT;
1207
DPRINTF("usb_host: malformed endpoint type\n");
1208
type = USBDEVFS_URB_TYPE_BULK;
1210
epd = get_endp(s, pid, ep);
1211
assert(epd->type == INVALID_EP_TYPE);
1215
i += descriptors[i];
1222
* Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1223
* this function assumes this is safe, if:
1224
* 1) There are no isoc endpoints
1225
* 2) There are no interrupt endpoints with a max_packet_size > 64
1226
* Note bulk endpoints with a max_packet_size > 64 in theory also are not
1227
* usb1 compatible, but in practice this seems to work fine.
1229
static int usb_linux_full_speed_compat(USBHostDevice *dev)
1234
* usb_linux_update_endp_table only registers info about ep in the current
1235
* interface altsettings, so we need to parse the descriptors again.
1237
for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) {
1238
if (dev->descr[i + 1] == USB_DT_ENDPOINT) {
1239
switch (dev->descr[i + 3] & 0x3) {
1240
case 0x00: /* CONTROL */
1242
case 0x01: /* ISO */
1244
case 0x02: /* BULK */
1246
case 0x03: /* INTERRUPT */
1247
packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8);
1248
if (packet_size > 64)
1257
static int usb_host_open(USBHostDevice *dev, int bus_num,
1258
int addr, const char *port,
1259
const char *prod_name, int speed)
1264
trace_usb_host_open_started(bus_num, addr);
1266
if (dev->fd != -1) {
1270
if (!usb_host_device_path) {
1271
perror("husb: USB Host Device Path not set");
1274
snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
1276
fd = open(buf, O_RDWR | O_NONBLOCK);
1281
DPRINTF("husb: opened %s\n", buf);
1283
dev->bus_num = bus_num;
1285
strcpy(dev->port, port);
1288
/* read the device description */
1289
dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
1290
if (dev->descr_len <= 0) {
1291
perror("husb: reading device data failed");
1298
printf("=== begin dumping device descriptor data ===\n");
1299
for (x = 0; x < dev->descr_len; x++) {
1300
printf("%02x ", dev->descr[x]);
1302
printf("\n=== end dumping device descriptor data ===\n");
1307
/* start unconfigured -- we'll wait for the guest to set a configuration */
1308
if (!usb_host_claim_interfaces(dev, 0)) {
1312
ret = usb_linux_update_endp_table(dev);
1318
struct usbdevfs_connectinfo ci;
1320
ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
1322
perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1327
speed = USB_SPEED_LOW;
1329
speed = USB_SPEED_HIGH;
1332
dev->dev.speed = speed;
1333
dev->dev.speedmask = (1 << speed);
1334
if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
1335
dev->dev.speedmask |= USB_SPEED_MASK_FULL;
1338
trace_usb_host_open_success(bus_num, addr);
1340
if (!prod_name || prod_name[0] == '\0') {
1341
snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1342
"host:%d.%d", bus_num, addr);
1344
pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1348
ret = usb_device_attach(&dev->dev);
1353
/* USB devio uses 'write' flag to check for async completions */
1354
qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
1359
trace_usb_host_open_failure(bus_num, addr);
1360
if (dev->fd != -1) {
1367
static int usb_host_close(USBHostDevice *dev)
1371
if (dev->fd == -1) {
1375
trace_usb_host_close(dev->bus_num, dev->addr);
1377
qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
1379
for (i = 1; i <= MAX_ENDPOINTS; i++) {
1380
if (is_isoc(dev, USB_TOKEN_IN, i)) {
1381
usb_host_stop_n_free_iso(dev, USB_TOKEN_IN, i);
1383
if (is_isoc(dev, USB_TOKEN_OUT, i)) {
1384
usb_host_stop_n_free_iso(dev, USB_TOKEN_OUT, i);
1387
async_complete(dev);
1389
if (dev->dev.attached) {
1390
usb_device_detach(&dev->dev);
1392
usb_host_do_reset(dev);
1398
static void usb_host_exit_notifier(struct Notifier *n, void *data)
1400
USBHostDevice *s = container_of(n, USBHostDevice, exit);
1403
usb_host_do_reset(s);;
1407
static int usb_host_initfn(USBDevice *dev)
1409
USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1411
dev->auto_attach = 0;
1415
QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1416
s->exit.notify = usb_host_exit_notifier;
1417
qemu_add_exit_notifier(&s->exit);
1418
usb_host_auto_check(NULL);
1420
if (s->match.bus_num != 0 && s->match.port != NULL) {
1421
usb_host_claim_port(s);
1426
static const VMStateDescription vmstate_usb_host = {
1431
static struct USBDeviceInfo usb_host_dev_info = {
1432
.product_desc = "USB Host Device",
1433
.qdev.name = "usb-host",
1434
.qdev.size = sizeof(USBHostDevice),
1435
.qdev.vmsd = &vmstate_usb_host,
1436
.init = usb_host_initfn,
1437
.handle_packet = usb_generic_handle_packet,
1438
.cancel_packet = usb_host_async_cancel,
1439
.handle_data = usb_host_handle_data,
1440
.handle_control = usb_host_handle_control,
1441
.handle_reset = usb_host_handle_reset,
1442
.handle_destroy = usb_host_handle_destroy,
1443
.usbdevice_name = "host",
1444
.usbdevice_init = usb_host_device_open,
1445
.qdev.props = (Property[]) {
1446
DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1447
DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1448
DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1449
DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1450
DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1451
DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1452
DEFINE_PROP_END_OF_LIST(),
1456
static void usb_host_register_devices(void)
1458
usb_qdev_register(&usb_host_dev_info);
1460
device_init(usb_host_register_devices)
1462
USBDevice *usb_host_device_open(const char *devname)
1464
struct USBAutoFilter filter;
1468
dev = usb_create(NULL /* FIXME */, "usb-host");
1470
if (strstr(devname, "auto:")) {
1471
if (parse_filter(devname, &filter) < 0) {
1475
if ((p = strchr(devname, '.'))) {
1476
filter.bus_num = strtoul(devname, NULL, 0);
1477
filter.addr = strtoul(p + 1, NULL, 0);
1478
filter.vendor_id = 0;
1479
filter.product_id = 0;
1480
} else if ((p = strchr(devname, ':'))) {
1483
filter.vendor_id = strtoul(devname, NULL, 16);
1484
filter.product_id = strtoul(p + 1, NULL, 16);
1490
qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1491
qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
1492
qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1493
qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
1494
qdev_init_nofail(&dev->qdev);
1498
qdev_free(&dev->qdev);
1502
int usb_host_device_close(const char *devname)
1505
char product_name[PRODUCT_NAME_SZ];
1509
if (strstr(devname, "auto:")) {
1510
return usb_host_auto_del(devname);
1512
if (usb_host_find_device(&bus_num, &addr, product_name,
1513
sizeof(product_name), devname) < 0) {
1516
s = hostdev_find(bus_num, addr);
1518
usb_device_delete_addr(s->bus_num, s->dev.addr);
1526
static int get_tag_value(char *buf, int buf_size,
1527
const char *str, const char *tag,
1528
const char *stopchars)
1532
p = strstr(str, tag);
1537
while (qemu_isspace(*p)) {
1541
while (*p != '\0' && !strchr(stopchars, *p)) {
1542
if ((q - buf) < (buf_size - 1)) {
1552
* Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1553
* host's USB devices. This is legacy support since many distributions
1554
* are moving to /sys/bus/usb
1556
static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1561
int bus_num, addr, speed, device_count;
1562
int class_id, product_id, vendor_id, port;
1563
char product_name[512];
1566
if (!usb_host_device_path) {
1567
perror("husb: USB Host Device Path not set");
1570
snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1571
f = fopen(line, "r");
1573
perror("husb: cannot open devices file");
1578
bus_num = addr = class_id = product_id = vendor_id = port = 0;
1579
speed = -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */
1581
if (fgets(line, sizeof(line), f) == NULL) {
1584
if (strlen(line) > 0) {
1585
line[strlen(line) - 1] = '\0';
1587
if (line[0] == 'T' && line[1] == ':') {
1588
if (device_count && (vendor_id || product_id)) {
1589
/* New device. Add the previously discovered device. */
1591
snprintf(buf, sizeof(buf), "%d", port);
1593
snprintf(buf, sizeof(buf), "?");
1595
ret = func(opaque, bus_num, addr, buf, class_id, vendor_id,
1596
product_id, product_name, speed);
1601
if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
1604
bus_num = atoi(buf);
1605
if (get_tag_value(buf, sizeof(buf), line, "Port=", " ") < 0) {
1609
if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
1613
if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
1616
if (!strcmp(buf, "5000")) {
1617
speed = USB_SPEED_SUPER;
1618
} else if (!strcmp(buf, "480")) {
1619
speed = USB_SPEED_HIGH;
1620
} else if (!strcmp(buf, "1.5")) {
1621
speed = USB_SPEED_LOW;
1623
speed = USB_SPEED_FULL;
1625
product_name[0] = '\0';
1630
} else if (line[0] == 'P' && line[1] == ':') {
1631
if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
1634
vendor_id = strtoul(buf, NULL, 16);
1635
if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
1638
product_id = strtoul(buf, NULL, 16);
1639
} else if (line[0] == 'S' && line[1] == ':') {
1640
if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
1643
pstrcpy(product_name, sizeof(product_name), buf);
1644
} else if (line[0] == 'D' && line[1] == ':') {
1645
if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
1648
class_id = strtoul(buf, NULL, 16);
1652
if (device_count && (vendor_id || product_id)) {
1653
/* Add the last device. */
1655
snprintf(buf, sizeof(buf), "%d", port);
1657
snprintf(buf, sizeof(buf), "?");
1659
ret = func(opaque, bus_num, addr, buf, class_id, vendor_id,
1660
product_id, product_name, speed);
1670
* Read sys file-system device file
1672
* @line address of buffer to put file contents in
1673
* @line_size size of line
1674
* @device_file path to device file (printf format string)
1675
* @device_name device being opened (inserted into device_file)
1677
* @return 0 failed, 1 succeeded ('line' contains data)
1679
static int usb_host_read_file(char *line, size_t line_size,
1680
const char *device_file, const char *device_name)
1684
char filename[PATH_MAX];
1686
snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1688
f = fopen(filename, "r");
1690
ret = fgets(line, line_size, f) != NULL;
1698
* Use /sys/bus/usb/devices/ directory to determine host's USB
1701
* This code is based on Robert Schiele's original patches posted to
1702
* the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1704
static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1708
int bus_num, addr, speed, class_id, product_id, vendor_id;
1710
char port[MAX_PORTLEN];
1711
char product_name[512];
1714
dir = opendir(USBSYSBUS_PATH "/devices");
1716
perror("husb: cannot open devices directory");
1720
while ((de = readdir(dir))) {
1721
if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1722
if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) {
1726
if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
1729
if (sscanf(line, "%d", &addr) != 1) {
1732
if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1736
if (sscanf(line, "%x", &class_id) != 1) {
1740
if (!usb_host_read_file(line, sizeof(line), "idVendor",
1744
if (sscanf(line, "%x", &vendor_id) != 1) {
1747
if (!usb_host_read_file(line, sizeof(line), "idProduct",
1751
if (sscanf(line, "%x", &product_id) != 1) {
1754
if (!usb_host_read_file(line, sizeof(line), "product",
1758
if (strlen(line) > 0) {
1759
line[strlen(line) - 1] = '\0';
1761
pstrcpy(product_name, sizeof(product_name), line);
1764
if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
1767
if (!strcmp(line, "5000\n")) {
1768
speed = USB_SPEED_SUPER;
1769
} else if (!strcmp(line, "480\n")) {
1770
speed = USB_SPEED_HIGH;
1771
} else if (!strcmp(line, "1.5\n")) {
1772
speed = USB_SPEED_LOW;
1774
speed = USB_SPEED_FULL;
1777
ret = func(opaque, bus_num, addr, port, class_id, vendor_id,
1778
product_id, product_name, speed);
1792
* Determine how to access the host's USB devices and call the
1793
* specific support function.
1795
static int usb_host_scan(void *opaque, USBScanFunc *func)
1797
Monitor *mon = cur_mon;
1801
const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1802
char devpath[PATH_MAX];
1804
/* only check the host once */
1806
dir = opendir(USBSYSBUS_PATH "/devices");
1808
/* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1809
strcpy(devpath, USBDEVBUS_PATH);
1810
usb_fs_type = USB_FS_SYS;
1812
DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1815
f = fopen(USBPROCBUS_PATH "/devices", "r");
1817
/* devices found in /proc/bus/usb/ */
1818
strcpy(devpath, USBPROCBUS_PATH);
1819
usb_fs_type = USB_FS_PROC;
1821
DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1824
/* try additional methods if an access method hasn't been found yet */
1825
f = fopen(USBDEVBUS_PATH "/devices", "r");
1827
/* devices found in /dev/bus/usb/ */
1828
strcpy(devpath, USBDEVBUS_PATH);
1829
usb_fs_type = USB_FS_DEV;
1831
DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1837
monitor_printf(mon, "husb: unable to access USB devices\n");
1842
/* the module setting (used later for opening devices) */
1843
usb_host_device_path = g_malloc0(strlen(devpath)+1);
1844
strcpy(usb_host_device_path, devpath);
1846
monitor_printf(mon, "husb: using %s file-system with %s\n",
1847
fs_type[usb_fs_type], usb_host_device_path);
1851
switch (usb_fs_type) {
1854
ret = usb_host_scan_dev(opaque, func);
1857
ret = usb_host_scan_sys(opaque, func);
1866
static QEMUTimer *usb_auto_timer;
1868
static int usb_host_auto_scan(void *opaque, int bus_num,
1869
int addr, const char *port,
1870
int class_id, int vendor_id, int product_id,
1871
const char *product_name, int speed)
1873
struct USBAutoFilter *f;
1874
struct USBHostDevice *s;
1880
QTAILQ_FOREACH(s, &hostdevs, next) {
1883
if (f->bus_num > 0 && f->bus_num != bus_num) {
1886
if (f->addr > 0 && f->addr != addr) {
1889
if (f->port != NULL && (port == NULL || strcmp(f->port, port) != 0)) {
1893
if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
1897
if (f->product_id > 0 && f->product_id != product_id) {
1900
/* We got a match */
1902
if (s->errcount >= 3) {
1906
/* Already attached ? */
1910
DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1912
if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) {
1921
static void usb_host_auto_check(void *unused)
1923
struct USBHostDevice *s;
1924
int unconnected = 0;
1926
usb_host_scan(NULL, usb_host_auto_scan);
1928
QTAILQ_FOREACH(s, &hostdevs, next) {
1938
if (unconnected == 0) {
1939
/* nothing to watch */
1940
if (usb_auto_timer) {
1941
qemu_del_timer(usb_auto_timer);
1942
trace_usb_host_auto_scan_disabled();
1947
if (!usb_auto_timer) {
1948
usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
1949
if (!usb_auto_timer) {
1952
trace_usb_host_auto_scan_enabled();
1954
qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1958
* Autoconnect filter
1960
* auto:bus:dev[:vid:pid]
1961
* auto:bus.dev[:vid:pid]
1963
* bus - bus number (dec, * means any)
1964
* dev - device number (dec, * means any)
1965
* vid - vendor id (hex, * means any)
1966
* pid - product id (hex, * means any)
1968
* See 'lsusb' output.
1970
static int parse_filter(const char *spec, struct USBAutoFilter *f)
1972
enum { BUS, DEV, VID, PID, DONE };
1973
const char *p = spec;
1981
for (i = BUS; i < DONE; i++) {
1982
p = strpbrk(p, ":.");
1992
case BUS: f->bus_num = strtol(p, NULL, 10); break;
1993
case DEV: f->addr = strtol(p, NULL, 10); break;
1994
case VID: f->vendor_id = strtol(p, NULL, 16); break;
1995
case PID: f->product_id = strtol(p, NULL, 16); break;
2000
fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
2007
/**********************/
2008
/* USB host device info */
2010
struct usb_class_info {
2012
const char *class_name;
2015
static const struct usb_class_info usb_class_info[] = {
2016
{ USB_CLASS_AUDIO, "Audio"},
2017
{ USB_CLASS_COMM, "Communication"},
2018
{ USB_CLASS_HID, "HID"},
2019
{ USB_CLASS_HUB, "Hub" },
2020
{ USB_CLASS_PHYSICAL, "Physical" },
2021
{ USB_CLASS_PRINTER, "Printer" },
2022
{ USB_CLASS_MASS_STORAGE, "Storage" },
2023
{ USB_CLASS_CDC_DATA, "Data" },
2024
{ USB_CLASS_APP_SPEC, "Application Specific" },
2025
{ USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
2026
{ USB_CLASS_STILL_IMAGE, "Still Image" },
2027
{ USB_CLASS_CSCID, "Smart Card" },
2028
{ USB_CLASS_CONTENT_SEC, "Content Security" },
2032
static const char *usb_class_str(uint8_t class)
2034
const struct usb_class_info *p;
2035
for(p = usb_class_info; p->class != -1; p++) {
2036
if (p->class == class) {
2040
return p->class_name;
2043
static void usb_info_device(Monitor *mon, int bus_num,
2044
int addr, const char *port,
2045
int class_id, int vendor_id, int product_id,
2046
const char *product_name,
2049
const char *class_str, *speed_str;
2055
case USB_SPEED_FULL:
2058
case USB_SPEED_HIGH:
2061
case USB_SPEED_SUPER:
2069
monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
2070
bus_num, addr, port, speed_str);
2071
class_str = usb_class_str(class_id);
2073
monitor_printf(mon, " %s:", class_str);
2075
monitor_printf(mon, " Class %02x:", class_id);
2077
monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
2078
if (product_name[0] != '\0') {
2079
monitor_printf(mon, ", %s", product_name);
2081
monitor_printf(mon, "\n");
2084
static int usb_host_info_device(void *opaque, int bus_num, int addr,
2085
const char *path, int class_id,
2086
int vendor_id, int product_id,
2087
const char *product_name,
2090
Monitor *mon = opaque;
2092
usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id,
2093
product_name, speed);
2097
static void dec2str(int val, char *str, size_t size)
2100
snprintf(str, size, "*");
2102
snprintf(str, size, "%d", val);
2106
static void hex2str(int val, char *str, size_t size)
2109
snprintf(str, size, "*");
2111
snprintf(str, size, "%04x", val);
2115
void usb_host_info(Monitor *mon)
2117
struct USBAutoFilter *f;
2118
struct USBHostDevice *s;
2120
usb_host_scan(mon, usb_host_info_device);
2122
if (QTAILQ_EMPTY(&hostdevs)) {
2126
monitor_printf(mon, " Auto filters:\n");
2127
QTAILQ_FOREACH(s, &hostdevs, next) {
2128
char bus[10], addr[10], vid[10], pid[10];
2130
dec2str(f->bus_num, bus, sizeof(bus));
2131
dec2str(f->addr, addr, sizeof(addr));
2132
hex2str(f->vendor_id, vid, sizeof(vid));
2133
hex2str(f->product_id, pid, sizeof(pid));
2134
monitor_printf(mon, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
2135
bus, addr, f->port ? f->port : "*", vid, pid);