36
#include "qemu/osdep.h"
37
#include "qom/object.h"
45
#include <linux/usbdevice_fs.h>
48
#include "qapi/error.h"
49
#include "migration/vmstate.h"
50
#include "monitor/monitor.h"
51
#include "qemu/error-report.h"
52
#include "qemu/main-loop.h"
53
#include "qemu/module.h"
54
#include "sysemu/runstate.h"
55
#include "sysemu/sysemu.h"
58
#include "hw/qdev-properties.h"
63
#define TYPE_USB_HOST_DEVICE "usb-host"
64
OBJECT_DECLARE_SIMPLE_TYPE(USBHostDevice, USB_HOST_DEVICE)
66
typedef struct USBHostRequest USBHostRequest;
67
typedef struct USBHostIsoXfer USBHostIsoXfer;
68
typedef struct USBHostIsoRing USBHostIsoRing;
78
enum USBHostDeviceOptions {
79
USB_HOST_OPT_PIPELINE,
86
struct USBAutoFilter match;
89
uint32_t iso_urb_count;
90
uint32_t iso_urb_frames;
94
bool allow_one_guest_reset;
95
bool allow_all_guest_resets;
96
bool suppress_remote_wake;
99
QTAILQ_ENTRY(USBHostDevice) next;
107
libusb_device_handle *dh;
108
struct libusb_device_descriptor ddesc;
113
} ifs[USB_MAX_INTERFACES];
118
bool bh_postld_pending;
122
QTAILQ_HEAD(, USBHostRequest) requests;
123
QTAILQ_HEAD(, USBHostIsoRing) isorings;
126
struct USBHostRequest {
130
struct libusb_transfer *xfer;
131
unsigned char *buffer;
135
QTAILQ_ENTRY(USBHostRequest) next;
138
struct USBHostIsoXfer {
139
USBHostIsoRing *ring;
140
struct libusb_transfer *xfer;
143
QTAILQ_ENTRY(USBHostIsoXfer) next;
146
struct USBHostIsoRing {
149
QTAILQ_HEAD(, USBHostIsoXfer) unused;
150
QTAILQ_HEAD(, USBHostIsoXfer) inflight;
151
QTAILQ_HEAD(, USBHostIsoXfer) copy;
152
QTAILQ_ENTRY(USBHostIsoRing) next;
155
static QTAILQ_HEAD(, USBHostDevice) hostdevs =
156
QTAILQ_HEAD_INITIALIZER(hostdevs);
158
static void usb_host_auto_check(void *unused);
159
static void usb_host_release_interfaces(USBHostDevice *s);
160
static void usb_host_nodev(USBHostDevice *s);
161
static void usb_host_detach_kernel(USBHostDevice *s);
162
static void usb_host_attach_kernel(USBHostDevice *s);
166
#ifndef LIBUSB_LOG_LEVEL_WARNING
167
#define LIBUSB_LOG_LEVEL_WARNING 2
172
#define CONTROL_TIMEOUT 10000
173
#define BULK_TIMEOUT 0
174
#define INTR_TIMEOUT 0
176
#ifndef LIBUSB_API_VERSION
177
# define LIBUSB_API_VERSION LIBUSBX_API_VERSION
179
#if LIBUSB_API_VERSION >= 0x01000103
180
# define HAVE_STREAMS 1
182
#if LIBUSB_API_VERSION >= 0x01000106
183
# define HAVE_SUPER_PLUS 1
186
static const char *speed_name[] = {
187
[LIBUSB_SPEED_UNKNOWN] = "?",
188
[LIBUSB_SPEED_LOW] = "1.5",
189
[LIBUSB_SPEED_FULL] = "12",
190
[LIBUSB_SPEED_HIGH] = "480",
191
[LIBUSB_SPEED_SUPER] = "5000",
192
#ifdef HAVE_SUPER_PLUS
193
[LIBUSB_SPEED_SUPER_PLUS] = "5000+",
197
static const unsigned int speed_map[] = {
198
[LIBUSB_SPEED_LOW] = USB_SPEED_LOW,
199
[LIBUSB_SPEED_FULL] = USB_SPEED_FULL,
200
[LIBUSB_SPEED_HIGH] = USB_SPEED_HIGH,
201
[LIBUSB_SPEED_SUPER] = USB_SPEED_SUPER,
202
#ifdef HAVE_SUPER_PLUS
203
[LIBUSB_SPEED_SUPER_PLUS] = USB_SPEED_SUPER,
207
static const unsigned int status_map[] = {
208
[LIBUSB_TRANSFER_COMPLETED] = USB_RET_SUCCESS,
209
[LIBUSB_TRANSFER_ERROR] = USB_RET_IOERROR,
210
[LIBUSB_TRANSFER_TIMED_OUT] = USB_RET_IOERROR,
211
[LIBUSB_TRANSFER_CANCELLED] = USB_RET_IOERROR,
212
[LIBUSB_TRANSFER_STALL] = USB_RET_STALL,
213
[LIBUSB_TRANSFER_NO_DEVICE] = USB_RET_NODEV,
214
[LIBUSB_TRANSFER_OVERFLOW] = USB_RET_BABBLE,
217
static const char *err_names[] = {
218
[-LIBUSB_ERROR_IO] = "IO",
219
[-LIBUSB_ERROR_INVALID_PARAM] = "INVALID_PARAM",
220
[-LIBUSB_ERROR_ACCESS] = "ACCESS",
221
[-LIBUSB_ERROR_NO_DEVICE] = "NO_DEVICE",
222
[-LIBUSB_ERROR_NOT_FOUND] = "NOT_FOUND",
223
[-LIBUSB_ERROR_BUSY] = "BUSY",
224
[-LIBUSB_ERROR_TIMEOUT] = "TIMEOUT",
225
[-LIBUSB_ERROR_OVERFLOW] = "OVERFLOW",
226
[-LIBUSB_ERROR_PIPE] = "PIPE",
227
[-LIBUSB_ERROR_INTERRUPTED] = "INTERRUPTED",
228
[-LIBUSB_ERROR_NO_MEM] = "NO_MEM",
229
[-LIBUSB_ERROR_NOT_SUPPORTED] = "NOT_SUPPORTED",
230
[-LIBUSB_ERROR_OTHER] = "OTHER",
233
static libusb_context *ctx;
234
static uint32_t loglevel;
238
static void usb_host_handle_fd(void *opaque)
240
struct timeval tv = { 0, 0 };
241
libusb_handle_events_timeout(ctx, &tv);
244
static void usb_host_add_fd(int fd, short events, void *user_data)
246
qemu_set_fd_handler(fd,
247
(events & POLLIN) ? usb_host_handle_fd : NULL,
248
(events & POLLOUT) ? usb_host_handle_fd : NULL,
252
static void usb_host_del_fd(int fd, void *user_data)
254
qemu_set_fd_handler(fd, NULL, NULL, NULL);
259
static QEMUTimer *poll_timer;
260
static uint32_t request_count;
262
static void usb_host_timer_kick(void)
266
delay_ns = request_count
267
? (NANOSECONDS_PER_SECOND / 100)
268
: (NANOSECONDS_PER_SECOND);
269
timer_mod(poll_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delay_ns);
272
static void usb_host_timer(void *opaque)
274
struct timeval tv = { 0, 0 };
276
libusb_handle_events_timeout(ctx, &tv);
277
usb_host_timer_kick();
282
static int usb_host_init(void)
285
const struct libusb_pollfd **poll;
292
rc = libusb_init(&ctx);
296
#if LIBUSB_API_VERSION >= 0x01000106
297
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, loglevel);
299
libusb_set_debug(ctx, loglevel);
302
poll_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, usb_host_timer, NULL);
303
usb_host_timer_kick();
305
libusb_set_pollfd_notifiers(ctx, usb_host_add_fd,
308
poll = libusb_get_pollfds(ctx);
311
for (i = 0; poll[i] != NULL; i++) {
312
usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx);
320
static int usb_host_get_port(libusb_device *dev, char *port, size_t len)
326
#if LIBUSB_API_VERSION >= 0x01000102
327
rc = libusb_get_port_numbers(dev, path, 7);
329
rc = libusb_get_port_path(ctx, dev, path, 7);
334
off = snprintf(port, len, "%d", path[0]);
335
for (i = 1; i < rc; i++) {
336
off += snprintf(port+off, len-off, ".%d", path[i]);
341
static void usb_host_libusb_error(const char *func, int rc)
349
if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) {
350
errname = err_names[-rc];
354
error_report("%s: %d [%s]", func, rc, errname);
359
static bool usb_host_use_combining(USBEndpoint *ep)
366
if (ep->pid != USB_TOKEN_IN) {
369
type = usb_ep_get_type(ep->dev, ep->pid, ep->nr);
370
if (type != USB_ENDPOINT_XFER_BULK) {
378
static USBHostRequest *usb_host_req_alloc(USBHostDevice *s, USBPacket *p,
379
bool in, size_t bufsize)
381
USBHostRequest *r = g_new0(USBHostRequest, 1);
386
r->xfer = libusb_alloc_transfer(0);
388
r->buffer = g_malloc(bufsize);
390
QTAILQ_INSERT_TAIL(&s->requests, r, next);
393
usb_host_timer_kick();
398
static void usb_host_req_free(USBHostRequest *r)
403
QTAILQ_REMOVE(&r->host->requests, r, next);
404
libusb_free_transfer(r->xfer);
409
static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p)
413
QTAILQ_FOREACH(r, &s->requests, next) {
421
static void LIBUSB_CALL usb_host_req_complete_ctrl(struct libusb_transfer *xfer)
423
USBHostRequest *r = xfer->user_data;
424
USBHostDevice *s = r->host;
425
bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
431
r->p->status = status_map[xfer->status];
432
r->p->actual_length = xfer->actual_length;
433
if (r->in && xfer->actual_length) {
434
USBDevice *udev = USB_DEVICE(s);
435
struct libusb_config_descriptor *conf = (void *)r->cbuf;
436
memcpy(r->cbuf, r->buffer + 8, xfer->actual_length);
440
if (r->usb3ep0quirk && xfer->actual_length >= 18 &&
449
if (s->suppress_remote_wake &&
450
udev->setup_buf[0] == USB_DIR_IN &&
451
udev->setup_buf[1] == USB_REQ_GET_DESCRIPTOR &&
452
udev->setup_buf[3] == USB_DT_CONFIG && udev->setup_buf[2] == 0 &&
453
xfer->actual_length >
454
offsetof(struct libusb_config_descriptor, bmAttributes) &&
455
(conf->bmAttributes & USB_CFG_ATT_WAKEUP)) {
456
trace_usb_host_remote_wakeup_removed(s->bus_num, s->addr);
457
conf->bmAttributes &= ~USB_CFG_ATT_WAKEUP;
460
trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
461
r->p->status, r->p->actual_length);
462
usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
465
usb_host_req_free(r);
471
static void LIBUSB_CALL usb_host_req_complete_data(struct libusb_transfer *xfer)
473
USBHostRequest *r = xfer->user_data;
474
USBHostDevice *s = r->host;
475
bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
481
r->p->status = status_map[xfer->status];
482
if (r->in && xfer->actual_length) {
483
usb_packet_copy(r->p, r->buffer, xfer->actual_length);
485
trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
486
r->p->status, r->p->actual_length);
487
if (usb_host_use_combining(r->p->ep)) {
488
usb_combined_input_packet_complete(USB_DEVICE(s), r->p);
490
usb_packet_complete(USB_DEVICE(s), r->p);
494
usb_host_req_free(r);
500
static void usb_host_req_abort(USBHostRequest *r)
502
USBHostDevice *s = r->host;
503
bool inflight = (r->p && r->p->state == USB_PACKET_ASYNC);
506
r->p->status = USB_RET_NODEV;
507
trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
508
r->p->status, r->p->actual_length);
509
if (r->p->ep->nr == 0) {
510
usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
512
usb_packet_complete(USB_DEVICE(s), r->p);
516
libusb_cancel_transfer(r->xfer);
522
static void LIBUSB_CALL
523
usb_host_req_complete_iso(struct libusb_transfer *transfer)
525
USBHostIsoXfer *xfer = transfer->user_data;
529
g_free(transfer->buffer);
530
libusb_free_transfer(transfer);
534
QTAILQ_REMOVE(&xfer->ring->inflight, xfer, next);
535
if (QTAILQ_EMPTY(&xfer->ring->inflight)) {
536
USBHostDevice *s = xfer->ring->host;
537
trace_usb_host_iso_stop(s->bus_num, s->addr, xfer->ring->ep->nr);
539
if (xfer->ring->ep->pid == USB_TOKEN_IN) {
540
QTAILQ_INSERT_TAIL(&xfer->ring->copy, xfer, next);
541
usb_wakeup(xfer->ring->ep, 0);
543
QTAILQ_INSERT_TAIL(&xfer->ring->unused, xfer, next);
547
static USBHostIsoRing *usb_host_iso_alloc(USBHostDevice *s, USBEndpoint *ep)
549
USBHostIsoRing *ring = g_new0(USBHostIsoRing, 1);
550
USBHostIsoXfer *xfer;
552
int packets = s->iso_urb_frames;
557
QTAILQ_INIT(&ring->unused);
558
QTAILQ_INIT(&ring->inflight);
559
QTAILQ_INIT(&ring->copy);
560
QTAILQ_INSERT_TAIL(&s->isorings, ring, next);
562
for (i = 0; i < s->iso_urb_count; i++) {
563
xfer = g_new0(USBHostIsoXfer, 1);
565
xfer->xfer = libusb_alloc_transfer(packets);
566
xfer->xfer->dev_handle = s->dh;
567
xfer->xfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
569
xfer->xfer->endpoint = ring->ep->nr;
570
if (ring->ep->pid == USB_TOKEN_IN) {
571
xfer->xfer->endpoint |= USB_DIR_IN;
573
xfer->xfer->callback = usb_host_req_complete_iso;
574
xfer->xfer->user_data = xfer;
576
xfer->xfer->num_iso_packets = packets;
577
xfer->xfer->length = ring->ep->max_packet_size * packets;
578
xfer->xfer->buffer = g_malloc0(xfer->xfer->length);
580
QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
586
static USBHostIsoRing *usb_host_iso_find(USBHostDevice *s, USBEndpoint *ep)
588
USBHostIsoRing *ring;
590
QTAILQ_FOREACH(ring, &s->isorings, next) {
591
if (ring->ep == ep) {
598
static void usb_host_iso_reset_xfer(USBHostIsoXfer *xfer)
600
libusb_set_iso_packet_lengths(xfer->xfer,
601
xfer->ring->ep->max_packet_size);
603
xfer->copy_complete = false;
606
static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight)
609
xfer->xfer->user_data = NULL;
611
g_free(xfer->xfer->buffer);
612
libusb_free_transfer(xfer->xfer);
617
static void usb_host_iso_free(USBHostIsoRing *ring)
619
USBHostIsoXfer *xfer;
621
while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) {
622
QTAILQ_REMOVE(&ring->inflight, xfer, next);
623
usb_host_iso_free_xfer(xfer, true);
625
while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
626
QTAILQ_REMOVE(&ring->unused, xfer, next);
627
usb_host_iso_free_xfer(xfer, false);
629
while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) {
630
QTAILQ_REMOVE(&ring->copy, xfer, next);
631
usb_host_iso_free_xfer(xfer, false);
634
QTAILQ_REMOVE(&ring->host->isorings, ring, next);
638
static void usb_host_iso_free_all(USBHostDevice *s)
640
USBHostIsoRing *ring;
642
while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) {
643
usb_host_iso_free(ring);
647
static bool usb_host_iso_data_copy(USBHostIsoXfer *xfer, USBPacket *p)
652
buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet);
653
if (p->pid == USB_TOKEN_OUT) {
655
if (psize > xfer->ring->ep->max_packet_size) {
657
psize = xfer->ring->ep->max_packet_size;
659
xfer->xfer->iso_packet_desc[xfer->packet].length = psize;
661
psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length;
662
if (psize > p->iov.size) {
667
usb_packet_copy(p, buf, psize);
669
xfer->copy_complete = (xfer->packet == xfer->xfer->num_iso_packets);
670
return xfer->copy_complete;
673
static void usb_host_iso_data_in(USBHostDevice *s, USBPacket *p)
675
USBHostIsoRing *ring;
676
USBHostIsoXfer *xfer;
677
bool disconnect = false;
680
ring = usb_host_iso_find(s, p->ep);
682
ring = usb_host_iso_alloc(s, p->ep);
686
xfer = QTAILQ_FIRST(&ring->copy);
688
if (usb_host_iso_data_copy(xfer, p)) {
689
QTAILQ_REMOVE(&ring->copy, xfer, next);
690
QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
695
while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
696
QTAILQ_REMOVE(&ring->unused, xfer, next);
697
usb_host_iso_reset_xfer(xfer);
698
rc = libusb_submit_transfer(xfer->xfer);
700
usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
701
QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
702
if (rc == LIBUSB_ERROR_NO_DEVICE) {
707
if (QTAILQ_EMPTY(&ring->inflight)) {
708
trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
710
QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
718
static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p)
720
USBHostIsoRing *ring;
721
USBHostIsoXfer *xfer;
722
bool disconnect = false;
725
ring = usb_host_iso_find(s, p->ep);
727
ring = usb_host_iso_alloc(s, p->ep);
731
xfer = QTAILQ_FIRST(&ring->copy);
732
while (xfer != NULL && xfer->copy_complete) {
734
xfer = QTAILQ_NEXT(xfer, next);
737
xfer = QTAILQ_FIRST(&ring->unused);
739
trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr);
742
QTAILQ_REMOVE(&ring->unused, xfer, next);
743
usb_host_iso_reset_xfer(xfer);
744
QTAILQ_INSERT_TAIL(&ring->copy, xfer, next);
746
usb_host_iso_data_copy(xfer, p);
748
if (QTAILQ_EMPTY(&ring->inflight)) {
751
if (filled*2 < s->iso_urb_count) {
757
while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL &&
758
xfer->copy_complete) {
759
QTAILQ_REMOVE(&ring->copy, xfer, next);
760
rc = libusb_submit_transfer(xfer->xfer);
762
usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
763
QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
764
if (rc == LIBUSB_ERROR_NO_DEVICE) {
769
if (QTAILQ_EMPTY(&ring->inflight)) {
770
trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
772
QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
782
static void usb_host_speed_compat(USBHostDevice *s)
784
USBDevice *udev = USB_DEVICE(s);
785
struct libusb_config_descriptor *conf;
786
const struct libusb_interface_descriptor *intf;
787
const struct libusb_endpoint_descriptor *endp;
789
struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp;
791
bool compat_high = true;
792
bool compat_full = true;
797
rc = libusb_get_config_descriptor(s->dev, c, &conf);
801
for (i = 0; i < conf->bNumInterfaces; i++) {
802
for (a = 0; a < conf->interface[i].num_altsetting; a++) {
803
intf = &conf->interface[i].altsetting[a];
805
if (intf->bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE &&
806
intf->bInterfaceSubClass == 6) {
807
udev->flags |= (1 << USB_DEV_FLAG_IS_SCSI_STORAGE);
811
for (e = 0; e < intf->bNumEndpoints; e++) {
812
endp = &intf->endpoint[e];
813
type = endp->bmAttributes & 0x3;
821
rc = libusb_get_ss_endpoint_companion_descriptor
822
(ctx, endp, &endp_ss_comp);
823
if (rc == LIBUSB_SUCCESS) {
824
int streams = endp_ss_comp->bmAttributes & 0x1f;
829
libusb_free_ss_endpoint_companion_descriptor
835
if (endp->wMaxPacketSize > 64) {
838
if (endp->wMaxPacketSize > 1024) {
846
libusb_free_config_descriptor(conf);
849
udev->speedmask = (1 << udev->speed);
850
if (udev->speed == USB_SPEED_SUPER && compat_high) {
851
udev->speedmask |= USB_SPEED_MASK_HIGH;
853
if (udev->speed == USB_SPEED_SUPER && compat_full) {
854
udev->speedmask |= USB_SPEED_MASK_FULL;
856
if (udev->speed == USB_SPEED_HIGH && compat_full) {
857
udev->speedmask |= USB_SPEED_MASK_FULL;
861
static void usb_host_ep_update(USBHostDevice *s)
863
static const char *tname[] = {
864
[USB_ENDPOINT_XFER_CONTROL] = "control",
865
[USB_ENDPOINT_XFER_ISOC] = "isoc",
866
[USB_ENDPOINT_XFER_BULK] = "bulk",
867
[USB_ENDPOINT_XFER_INT] = "int",
869
USBDevice *udev = USB_DEVICE(s);
870
struct libusb_config_descriptor *conf;
871
const struct libusb_interface_descriptor *intf;
872
const struct libusb_endpoint_descriptor *endp;
874
struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp;
881
rc = libusb_get_active_config_descriptor(s->dev, &conf);
885
trace_usb_host_parse_config(s->bus_num, s->addr,
886
conf->bConfigurationValue, true);
888
for (i = 0; i < conf->bNumInterfaces; i++) {
895
intf = &conf->interface[i].altsetting[0];
896
alt = udev->altsetting[intf->bInterfaceNumber];
899
assert(alt < conf->interface[i].num_altsetting);
900
intf = &conf->interface[i].altsetting[alt];
903
trace_usb_host_parse_interface(s->bus_num, s->addr,
904
intf->bInterfaceNumber,
905
intf->bAlternateSetting, true);
906
for (e = 0; e < intf->bNumEndpoints; e++) {
907
endp = &intf->endpoint[e];
909
devep = endp->bEndpointAddress;
910
pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
912
type = endp->bmAttributes & 0x3;
915
trace_usb_host_parse_error(s->bus_num, s->addr,
916
"invalid endpoint address");
919
if (usb_ep_get_type(udev, pid, ep) != USB_ENDPOINT_XFER_INVALID) {
920
trace_usb_host_parse_error(s->bus_num, s->addr,
921
"duplicate endpoint address");
925
trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
926
(devep & USB_DIR_IN) ? "in" : "out",
928
usb_ep_set_max_packet_size(udev, pid, ep,
929
endp->wMaxPacketSize);
930
usb_ep_set_type(udev, pid, ep, type);
931
usb_ep_set_ifnum(udev, pid, ep, i);
932
usb_ep_set_halted(udev, pid, ep, 0);
934
if (type == LIBUSB_TRANSFER_TYPE_BULK &&
935
libusb_get_ss_endpoint_companion_descriptor(ctx, endp,
936
&endp_ss_comp) == LIBUSB_SUCCESS) {
937
usb_ep_set_max_streams(udev, pid, ep,
938
endp_ss_comp->bmAttributes);
939
libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp);
945
libusb_free_config_descriptor(conf);
948
static int usb_host_open(USBHostDevice *s, libusb_device *dev, int hostfd)
950
USBDevice *udev = USB_DEVICE(s);
955
Error *local_err = NULL;
957
if (s->bh_postld_pending) {
965
bus_num = libusb_get_bus_number(dev);
966
addr = libusb_get_device_address(dev);
967
trace_usb_host_open_started(bus_num, addr);
969
rc = libusb_open(dev, &s->dh);
974
#if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
975
trace_usb_host_open_hostfd(hostfd);
977
rc = libusb_wrap_sys_device(ctx, hostfd, &s->dh);
982
dev = libusb_get_device(s->dh);
983
bus_num = libusb_get_bus_number(dev);
984
addr = libusb_get_device_address(dev);
986
g_assert_not_reached();
991
s->bus_num = bus_num;
994
usb_host_detach_kernel(s);
996
libusb_get_device_descriptor(dev, &s->ddesc);
997
usb_host_get_port(s->dev, s->port, sizeof(s->port));
1000
usb_host_ep_update(s);
1002
libusb_speed = libusb_get_device_speed(dev);
1003
#if LIBUSB_API_VERSION >= 0x01000107 && defined(CONFIG_LINUX) && \
1004
defined(USBDEVFS_GET_SPEED)
1005
if (hostfd && libusb_speed == 0) {
1013
rc = ioctl(hostfd, USBDEVFS_GET_SPEED, NULL);
1016
libusb_speed = LIBUSB_SPEED_LOW;
1019
libusb_speed = LIBUSB_SPEED_FULL;
1023
libusb_speed = LIBUSB_SPEED_HIGH;
1026
libusb_speed = LIBUSB_SPEED_SUPER;
1029
#ifdef HAVE_SUPER_PLUS
1030
libusb_speed = LIBUSB_SPEED_SUPER_PLUS;
1032
libusb_speed = LIBUSB_SPEED_SUPER;
1038
udev->speed = speed_map[libusb_speed];
1039
usb_host_speed_compat(s);
1041
if (s->ddesc.iProduct) {
1042
libusb_get_string_descriptor_ascii(s->dh, s->ddesc.iProduct,
1043
(unsigned char *)udev->product_desc,
1044
sizeof(udev->product_desc));
1046
snprintf(udev->product_desc, sizeof(udev->product_desc),
1047
"host:%d.%d", bus_num, addr);
1050
usb_device_attach(udev, &local_err);
1052
error_report_err(local_err);
1056
trace_usb_host_open_success(bus_num, addr);
1060
trace_usb_host_open_failure(bus_num, addr);
1061
if (s->dh != NULL) {
1062
usb_host_release_interfaces(s);
1063
libusb_reset_device(s->dh);
1064
usb_host_attach_kernel(s);
1065
libusb_close(s->dh);
1072
static void usb_host_abort_xfers(USBHostDevice *s)
1074
USBHostRequest *r, *rtmp;
1077
QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
1078
usb_host_req_abort(r);
1081
while (QTAILQ_FIRST(&s->requests) != NULL) {
1083
memset(&tv, 0, sizeof(tv));
1085
libusb_handle_events_timeout(ctx, &tv);
1094
QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
1095
QTAILQ_REMOVE(&s->requests, r, next);
1102
static int usb_host_close(USBHostDevice *s)
1104
USBDevice *udev = USB_DEVICE(s);
1106
if (s->dh == NULL) {
1110
trace_usb_host_close(s->bus_num, s->addr);
1112
usb_host_abort_xfers(s);
1113
usb_host_iso_free_all(s);
1115
if (udev->attached) {
1116
usb_device_detach(udev);
1119
usb_host_release_interfaces(s);
1120
libusb_reset_device(s->dh);
1121
usb_host_attach_kernel(s);
1122
libusb_close(s->dh);
1126
if (s->hostfd != -1) {
1131
usb_host_auto_check(NULL);
1135
static void usb_host_nodev_bh(void *opaque)
1137
USBHostDevice *s = opaque;
1141
static void usb_host_nodev(USBHostDevice *s)
1144
s->bh_nodev = qemu_bh_new_guarded(usb_host_nodev_bh, s,
1145
&DEVICE(s)->mem_reentrancy_guard);
1147
qemu_bh_schedule(s->bh_nodev);
1150
static void usb_host_exit_notifier(struct Notifier *n, void *data)
1152
USBHostDevice *s = container_of(n, USBHostDevice, exit);
1155
usb_host_abort_xfers(s);
1156
usb_host_release_interfaces(s);
1157
libusb_reset_device(s->dh);
1158
usb_host_attach_kernel(s);
1159
libusb_close(s->dh);
1163
static libusb_device *usb_host_find_ref(int bus, int addr)
1165
libusb_device **devs = NULL;
1166
libusb_device *ret = NULL;
1169
n = libusb_get_device_list(ctx, &devs);
1170
for (i = 0; i < n; i++) {
1171
if (libusb_get_bus_number(devs[i]) == bus &&
1172
libusb_get_device_address(devs[i]) == addr) {
1173
ret = libusb_ref_device(devs[i]);
1177
libusb_free_device_list(devs, 1);
1181
static void usb_host_realize(USBDevice *udev, Error **errp)
1183
USBHostDevice *s = USB_HOST_DEVICE(udev);
1184
libusb_device *ldev;
1187
if (usb_host_init() != 0) {
1188
error_setg(errp, "failed to init libusb");
1191
if (s->match.vendor_id > 0xffff) {
1192
error_setg(errp, "vendorid out of range");
1195
if (s->match.product_id > 0xffff) {
1196
error_setg(errp, "productid out of range");
1199
if (s->match.addr > 127) {
1200
error_setg(errp, "hostaddr out of range");
1204
loglevel = s->loglevel;
1205
udev->flags |= (1 << USB_DEV_FLAG_IS_HOST);
1206
udev->auto_attach = 0;
1207
QTAILQ_INIT(&s->requests);
1208
QTAILQ_INIT(&s->isorings);
1211
#if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
1212
if (s->hostdevice) {
1214
s->needs_autoscan = false;
1215
fd = qemu_open(s->hostdevice, O_RDWR, errp);
1219
rc = usb_host_open(s, NULL, fd);
1221
error_setg(errp, "failed to open host usb device %s", s->hostdevice);
1226
if (s->match.addr && s->match.bus_num &&
1227
!s->match.vendor_id &&
1228
!s->match.product_id &&
1230
s->needs_autoscan = false;
1231
ldev = usb_host_find_ref(s->match.bus_num,
1234
error_setg(errp, "failed to find host usb device %d:%d",
1235
s->match.bus_num, s->match.addr);
1238
rc = usb_host_open(s, ldev, 0);
1239
libusb_unref_device(ldev);
1241
error_setg(errp, "failed to open host usb device %d:%d",
1242
s->match.bus_num, s->match.addr);
1246
s->needs_autoscan = true;
1247
QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1248
usb_host_auto_check(NULL);
1251
s->exit.notify = usb_host_exit_notifier;
1252
qemu_add_exit_notifier(&s->exit);
1255
static void usb_host_instance_init(Object *obj)
1257
USBDevice *udev = USB_DEVICE(obj);
1258
USBHostDevice *s = USB_HOST_DEVICE(udev);
1260
device_add_bootindex_property(obj, &s->bootindex,
1265
static void usb_host_unrealize(USBDevice *udev)
1267
USBHostDevice *s = USB_HOST_DEVICE(udev);
1269
qemu_remove_exit_notifier(&s->exit);
1270
if (s->needs_autoscan) {
1271
QTAILQ_REMOVE(&hostdevs, s, next);
1276
static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p)
1278
USBHostDevice *s = USB_HOST_DEVICE(udev);
1282
usb_combined_packet_cancel(udev, p);
1286
trace_usb_host_req_canceled(s->bus_num, s->addr, p);
1288
r = usb_host_req_find(s, p);
1291
libusb_cancel_transfer(r->xfer);
1295
static void usb_host_detach_kernel(USBHostDevice *s)
1297
struct libusb_config_descriptor *conf;
1300
rc = libusb_get_active_config_descriptor(s->dev, &conf);
1304
for (i = 0; i < USB_MAX_INTERFACES; i++) {
1305
rc = libusb_kernel_driver_active(s->dh, i);
1306
usb_host_libusb_error("libusb_kernel_driver_active", rc);
1309
s->ifs[i].detached = true;
1313
trace_usb_host_detach_kernel(s->bus_num, s->addr, i);
1314
rc = libusb_detach_kernel_driver(s->dh, i);
1315
usb_host_libusb_error("libusb_detach_kernel_driver", rc);
1316
s->ifs[i].detached = true;
1318
libusb_free_config_descriptor(conf);
1321
static void usb_host_attach_kernel(USBHostDevice *s)
1323
struct libusb_config_descriptor *conf;
1326
rc = libusb_get_active_config_descriptor(s->dev, &conf);
1330
for (i = 0; i < USB_MAX_INTERFACES; i++) {
1331
if (!s->ifs[i].detached) {
1334
trace_usb_host_attach_kernel(s->bus_num, s->addr, i);
1335
libusb_attach_kernel_driver(s->dh, i);
1336
s->ifs[i].detached = false;
1338
libusb_free_config_descriptor(conf);
1341
static int usb_host_claim_interfaces(USBHostDevice *s, int configuration)
1343
USBDevice *udev = USB_DEVICE(s);
1344
struct libusb_config_descriptor *conf;
1347
for (i = 0; i < USB_MAX_INTERFACES; i++) {
1348
udev->altsetting[i] = 0;
1350
udev->ninterfaces = 0;
1351
udev->configuration = 0;
1353
usb_host_detach_kernel(s);
1355
rc = libusb_get_active_config_descriptor(s->dev, &conf);
1357
if (rc == LIBUSB_ERROR_NOT_FOUND) {
1359
return USB_RET_SUCCESS;
1361
return USB_RET_STALL;
1365
for (i = 0; i < USB_MAX_INTERFACES; i++) {
1366
trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i);
1367
rc = libusb_claim_interface(s->dh, i);
1369
s->ifs[i].claimed = true;
1370
if (++claimed == conf->bNumInterfaces) {
1375
if (claimed != conf->bNumInterfaces) {
1376
return USB_RET_STALL;
1379
udev->ninterfaces = conf->bNumInterfaces;
1380
udev->configuration = configuration;
1382
libusb_free_config_descriptor(conf);
1383
return USB_RET_SUCCESS;
1386
static void usb_host_release_interfaces(USBHostDevice *s)
1390
for (i = 0; i < USB_MAX_INTERFACES; i++) {
1391
if (!s->ifs[i].claimed) {
1394
trace_usb_host_release_interface(s->bus_num, s->addr, i);
1395
rc = libusb_release_interface(s->dh, i);
1396
usb_host_libusb_error("libusb_release_interface", rc);
1397
s->ifs[i].claimed = false;
1401
static void usb_host_set_address(USBHostDevice *s, int addr)
1403
USBDevice *udev = USB_DEVICE(s);
1405
trace_usb_host_set_address(s->bus_num, s->addr, addr);
1409
static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
1413
trace_usb_host_set_config(s->bus_num, s->addr, config);
1415
usb_host_release_interfaces(s);
1416
if (s->ddesc.bNumConfigurations != 1) {
1417
rc = libusb_set_configuration(s->dh, config);
1419
usb_host_libusb_error("libusb_set_configuration", rc);
1420
p->status = USB_RET_STALL;
1421
if (rc == LIBUSB_ERROR_NO_DEVICE) {
1427
p->status = usb_host_claim_interfaces(s, config);
1428
if (p->status != USB_RET_SUCCESS) {
1431
usb_host_ep_update(s);
1434
static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
1437
USBDevice *udev = USB_DEVICE(s);
1440
trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
1442
usb_host_iso_free_all(s);
1444
if (iface >= USB_MAX_INTERFACES) {
1445
p->status = USB_RET_STALL;
1449
rc = libusb_set_interface_alt_setting(s->dh, iface, alt);
1451
usb_host_libusb_error("libusb_set_interface_alt_setting", rc);
1452
p->status = USB_RET_STALL;
1453
if (rc == LIBUSB_ERROR_NO_DEVICE) {
1459
udev->altsetting[iface] = alt;
1460
usb_host_ep_update(s);
1463
static void usb_host_handle_control(USBDevice *udev, USBPacket *p,
1464
int request, int value, int index,
1465
int length, uint8_t *data)
1467
USBHostDevice *s = USB_HOST_DEVICE(udev);
1471
trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
1473
if (s->dh == NULL) {
1474
p->status = USB_RET_NODEV;
1475
trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1480
case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1481
usb_host_set_address(s, value);
1482
trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1485
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1486
usb_host_set_config(s, value & 0xff, p);
1487
trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1490
case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1491
usb_host_set_interface(s, index, value, p);
1492
trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1495
case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
1497
int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1498
libusb_clear_halt(s->dh, index);
1499
usb_ep_set_halted(udev, pid, index & 0x0f, 0);
1500
trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1505
r = usb_host_req_alloc(s, p, (request >> 8) & USB_DIR_IN, length + 8);
1508
memcpy(r->buffer, udev->setup_buf, 8);
1510
memcpy(r->buffer + 8, r->cbuf, r->clen);
1515
if ((udev->speedmask & USB_SPEED_MASK_SUPER) &&
1516
!(udev->port->speedmask & USB_SPEED_MASK_SUPER) &&
1517
request == 0x8006 && value == 0x100 && index == 0) {
1518
r->usb3ep0quirk = true;
1521
libusb_fill_control_transfer(r->xfer, s->dh, r->buffer,
1522
usb_host_req_complete_ctrl, r,
1524
rc = libusb_submit_transfer(r->xfer);
1526
p->status = USB_RET_NODEV;
1527
trace_usb_host_req_complete(s->bus_num, s->addr, p,
1528
p->status, p->actual_length);
1529
if (rc == LIBUSB_ERROR_NO_DEVICE) {
1535
p->status = USB_RET_ASYNC;
1538
static void usb_host_handle_data(USBDevice *udev, USBPacket *p)
1540
USBHostDevice *s = USB_HOST_DEVICE(udev);
1545
if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) {
1546
p->status = USB_RET_ADD_TO_QUEUE;
1550
trace_usb_host_req_data(s->bus_num, s->addr, p,
1551
p->pid == USB_TOKEN_IN,
1552
p->ep->nr, p->iov.size);
1554
if (s->dh == NULL) {
1555
p->status = USB_RET_NODEV;
1556
trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1559
if (p->ep->halted) {
1560
p->status = USB_RET_STALL;
1561
trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1565
switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) {
1566
case USB_ENDPOINT_XFER_BULK:
1567
size = usb_packet_size(p);
1568
r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size);
1570
usb_packet_copy(p, r->buffer, size);
1572
ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1575
libusb_fill_bulk_stream_transfer(r->xfer, s->dh, ep, p->stream,
1577
usb_host_req_complete_data, r,
1580
usb_host_req_free(r);
1581
p->status = USB_RET_STALL;
1585
libusb_fill_bulk_transfer(r->xfer, s->dh, ep,
1587
usb_host_req_complete_data, r,
1591
case USB_ENDPOINT_XFER_INT:
1592
r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size);
1594
usb_packet_copy(p, r->buffer, p->iov.size);
1596
ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1597
libusb_fill_interrupt_transfer(r->xfer, s->dh, ep,
1598
r->buffer, p->iov.size,
1599
usb_host_req_complete_data, r,
1602
case USB_ENDPOINT_XFER_ISOC:
1603
if (p->pid == USB_TOKEN_IN) {
1604
usb_host_iso_data_in(s, p);
1606
usb_host_iso_data_out(s, p);
1608
trace_usb_host_req_complete(s->bus_num, s->addr, p,
1609
p->status, p->actual_length);
1612
p->status = USB_RET_STALL;
1613
trace_usb_host_req_complete(s->bus_num, s->addr, p,
1614
p->status, p->actual_length);
1618
rc = libusb_submit_transfer(r->xfer);
1620
p->status = USB_RET_NODEV;
1621
trace_usb_host_req_complete(s->bus_num, s->addr, p,
1622
p->status, p->actual_length);
1623
if (rc == LIBUSB_ERROR_NO_DEVICE) {
1629
p->status = USB_RET_ASYNC;
1632
static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
1634
if (usb_host_use_combining(ep)) {
1635
usb_ep_combine_input_packets(ep);
1639
static void usb_host_handle_reset(USBDevice *udev)
1641
USBHostDevice *s = USB_HOST_DEVICE(udev);
1644
if (!s->allow_one_guest_reset && !s->allow_all_guest_resets) {
1647
if (!s->allow_all_guest_resets && udev->addr == 0) {
1651
trace_usb_host_reset(s->bus_num, s->addr);
1653
rc = libusb_reset_device(s->dh);
1659
static int usb_host_alloc_streams(USBDevice *udev, USBEndpoint **eps,
1660
int nr_eps, int streams)
1663
USBHostDevice *s = USB_HOST_DEVICE(udev);
1664
unsigned char endpoints[30];
1667
for (i = 0; i < nr_eps; i++) {
1668
endpoints[i] = eps[i]->nr;
1669
if (eps[i]->pid == USB_TOKEN_IN) {
1670
endpoints[i] |= 0x80;
1673
rc = libusb_alloc_streams(s->dh, streams, endpoints, nr_eps);
1675
usb_host_libusb_error("libusb_alloc_streams", rc);
1676
} else if (rc != streams) {
1677
error_report("libusb_alloc_streams: got less streams "
1678
"then requested %d < %d", rc, streams);
1681
return (rc == streams) ? 0 : -1;
1683
error_report("libusb_alloc_streams: error not implemented");
1688
static void usb_host_free_streams(USBDevice *udev, USBEndpoint **eps,
1692
USBHostDevice *s = USB_HOST_DEVICE(udev);
1693
unsigned char endpoints[30];
1696
for (i = 0; i < nr_eps; i++) {
1697
endpoints[i] = eps[i]->nr;
1698
if (eps[i]->pid == USB_TOKEN_IN) {
1699
endpoints[i] |= 0x80;
1702
libusb_free_streams(s->dh, endpoints, nr_eps);
1722
static void usb_host_post_load_bh(void *opaque)
1724
USBHostDevice *dev = opaque;
1725
USBDevice *udev = USB_DEVICE(dev);
1727
if (dev->dh != NULL) {
1728
usb_host_close(dev);
1730
if (udev->attached) {
1731
usb_device_detach(udev);
1733
dev->bh_postld_pending = false;
1734
usb_host_auto_check(NULL);
1737
static int usb_host_post_load(void *opaque, int version_id)
1739
USBHostDevice *dev = opaque;
1741
if (!dev->bh_postld) {
1742
dev->bh_postld = qemu_bh_new_guarded(usb_host_post_load_bh, dev,
1743
&DEVICE(dev)->mem_reentrancy_guard);
1745
qemu_bh_schedule(dev->bh_postld);
1746
dev->bh_postld_pending = true;
1750
static const VMStateDescription vmstate_usb_host = {
1753
.minimum_version_id = 1,
1754
.post_load = usb_host_post_load,
1755
.fields = (const VMStateField[]) {
1756
VMSTATE_USB_DEVICE(parent_obj, USBHostDevice),
1757
VMSTATE_END_OF_LIST()
1761
static Property usb_host_dev_properties[] = {
1762
DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1763
DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1764
DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1765
DEFINE_PROP_UINT32("vendorid", USBHostDevice, match.vendor_id, 0),
1766
DEFINE_PROP_UINT32("productid", USBHostDevice, match.product_id, 0),
1767
#if LIBUSB_API_VERSION >= 0x01000107
1768
DEFINE_PROP_STRING("hostdevice", USBHostDevice, hostdevice),
1770
DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1771
DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32),
1772
DEFINE_PROP_BOOL("guest-reset", USBHostDevice,
1773
allow_one_guest_reset, true),
1774
DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice,
1775
allow_all_guest_resets, false),
1776
DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel,
1777
LIBUSB_LOG_LEVEL_WARNING),
1778
DEFINE_PROP_BIT("pipeline", USBHostDevice, options,
1779
USB_HOST_OPT_PIPELINE, true),
1780
DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice,
1781
suppress_remote_wake, true),
1782
DEFINE_PROP_END_OF_LIST(),
1785
static void usb_host_class_initfn(ObjectClass *klass, void *data)
1787
DeviceClass *dc = DEVICE_CLASS(klass);
1788
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1790
uc->realize = usb_host_realize;
1791
uc->product_desc = "USB Host Device";
1792
uc->cancel_packet = usb_host_cancel_packet;
1793
uc->handle_data = usb_host_handle_data;
1794
uc->handle_control = usb_host_handle_control;
1795
uc->handle_reset = usb_host_handle_reset;
1796
uc->unrealize = usb_host_unrealize;
1797
uc->flush_ep_queue = usb_host_flush_ep_queue;
1798
uc->alloc_streams = usb_host_alloc_streams;
1799
uc->free_streams = usb_host_free_streams;
1800
dc->vmsd = &vmstate_usb_host;
1801
device_class_set_props(dc, usb_host_dev_properties);
1802
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1805
static const TypeInfo usb_host_dev_info = {
1806
.name = TYPE_USB_HOST_DEVICE,
1807
.parent = TYPE_USB_DEVICE,
1808
.instance_size = sizeof(USBHostDevice),
1809
.class_init = usb_host_class_initfn,
1810
.instance_init = usb_host_instance_init,
1812
module_obj(TYPE_USB_HOST_DEVICE);
1815
static void usb_host_register_types(void)
1817
type_register_static(&usb_host_dev_info);
1818
monitor_register_hmp("usbhost", true, hmp_info_usbhost);
1821
type_init(usb_host_register_types)
1825
static QEMUTimer *usb_auto_timer;
1826
static VMChangeStateEntry *usb_vmstate;
1828
static void usb_host_vm_state(void *unused, bool running, RunState state)
1831
usb_host_auto_check(unused);
1835
static void usb_host_auto_check(void *unused)
1837
struct USBHostDevice *s;
1838
struct USBAutoFilter *f;
1839
libusb_device **devs = NULL;
1840
struct libusb_device_descriptor ddesc;
1843
if (usb_host_init() != 0) {
1847
if (runstate_is_running()) {
1848
n = libusb_get_device_list(ctx, &devs);
1849
for (i = 0; i < n; i++) {
1850
if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1853
if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1856
QTAILQ_FOREACH(s, &hostdevs, next) {
1858
if (f->bus_num > 0 &&
1859
f->bus_num != libusb_get_bus_number(devs[i])) {
1863
f->addr != libusb_get_device_address(devs[i])) {
1866
if (f->port != NULL) {
1867
char port[16] = "-";
1868
usb_host_get_port(devs[i], port, sizeof(port));
1869
if (strcmp(f->port, port) != 0) {
1873
if (f->vendor_id > 0 &&
1874
f->vendor_id != ddesc.idVendor) {
1877
if (f->product_id > 0 &&
1878
f->product_id != ddesc.idProduct) {
1884
if (s->errcount >= 3) {
1887
if (s->dh != NULL) {
1890
if (usb_host_open(s, devs[i], 0) < 0) {
1897
libusb_free_device_list(devs, 1);
1899
QTAILQ_FOREACH(s, &hostdevs, next) {
1911
usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
1913
if (!usb_auto_timer) {
1914
usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL);
1915
if (!usb_auto_timer) {
1918
trace_usb_host_auto_scan_enabled();
1920
timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000);
1923
void hmp_info_usbhost(Monitor *mon, const QDict *qdict)
1925
libusb_device **devs = NULL;
1926
struct libusb_device_descriptor ddesc;
1930
if (usb_host_init() != 0) {
1934
n = libusb_get_device_list(ctx, &devs);
1935
for (i = 0; i < n; i++) {
1936
if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1939
if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1942
usb_host_get_port(devs[i], port, sizeof(port));
1943
monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1944
libusb_get_bus_number(devs[i]),
1945
libusb_get_device_address(devs[i]),
1947
speed_name[libusb_get_device_speed(devs[i])]);
1948
monitor_printf(mon, " Class %02x:", ddesc.bDeviceClass);
1949
monitor_printf(mon, " USB device %04x:%04x",
1950
ddesc.idVendor, ddesc.idProduct);
1951
if (ddesc.iProduct) {
1952
libusb_device_handle *handle;
1953
if (libusb_open(devs[i], &handle) == 0) {
1954
unsigned char name[64] = "";
1955
libusb_get_string_descriptor_ascii(handle,
1957
name, sizeof(name));
1958
libusb_close(handle);
1959
monitor_printf(mon, ", %s", name);
1962
monitor_printf(mon, "\n");
1964
libusb_free_device_list(devs, 1);