#include <libusb.h>
#include "qapi/error.h"
-#include "qemu-common.h"
+#include "migration/vmstate.h"
#include "monitor/monitor.h"
#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+#include "qemu/module.h"
#include "sysemu/sysemu.h"
#include "trace.h"
uint32_t options;
uint32_t loglevel;
bool needs_autoscan;
-
+ bool allow_guest_reset;
/* state */
QTAILQ_ENTRY(USBHostDevice) next;
int seen, errcount;
/* callbacks & friends */
QEMUBH *bh_nodev;
QEMUBH *bh_postld;
+ bool bh_postld_pending;
Notifier exit;
/* request queues */
#define BULK_TIMEOUT 0 /* unlimited */
#define INTR_TIMEOUT 0 /* unlimited */
-#if LIBUSBX_API_VERSION >= 0x01000103
+#ifndef LIBUSB_API_VERSION
+# define LIBUSB_API_VERSION LIBUSBX_API_VERSION
+#endif
+#if LIBUSB_API_VERSION >= 0x01000103
# define HAVE_STREAMS 1
#endif
if (rc != 0) {
return -1;
}
+#if LIBUSB_API_VERSION >= 0x01000106
+ libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, loglevel);
+#else
libusb_set_debug(ctx, loglevel);
+#endif
#ifdef CONFIG_WIN32
/* FIXME: add support for Windows. */
#else
size_t off;
int rc, i;
-#if LIBUSBX_API_VERSION >= 0x01000102
+#if LIBUSB_API_VERSION >= 0x01000102
rc = libusb_get_port_numbers(dev, path, 7);
#else
rc = libusb_get_port_path(ctx, dev, path, 7);
int rc;
Error *local_err = NULL;
+ if (s->bh_postld_pending) {
+ return -1;
+ }
+
trace_usb_host_open_started(bus_num, addr);
if (s->dh != NULL) {
if (s->dh) {
usb_host_release_interfaces(s);
+ libusb_reset_device(s->dh);
usb_host_attach_kernel(s);
+ libusb_close(s->dh);
}
}
if (rc != 0) {
return;
}
- for (i = 0; i < conf->bNumInterfaces; i++) {
+ for (i = 0; i < USB_MAX_INTERFACES; i++) {
rc = libusb_kernel_driver_active(s->dh, i);
usb_host_libusb_error("libusb_kernel_driver_active", rc);
if (rc != 1) {
+ if (rc == 0) {
+ s->ifs[i].detached = true;
+ }
continue;
}
trace_usb_host_detach_kernel(s->bus_num, s->addr, i);
if (rc != 0) {
return;
}
- for (i = 0; i < conf->bNumInterfaces; i++) {
+ for (i = 0; i < USB_MAX_INTERFACES; i++) {
if (!s->ifs[i].detached) {
continue;
}
{
USBDevice *udev = USB_DEVICE(s);
struct libusb_config_descriptor *conf;
- int rc, i;
+ int rc, i, claimed;
for (i = 0; i < USB_MAX_INTERFACES; i++) {
udev->altsetting[i] = 0;
return USB_RET_STALL;
}
- for (i = 0; i < conf->bNumInterfaces; i++) {
+ claimed = 0;
+ for (i = 0; i < USB_MAX_INTERFACES; i++) {
trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i);
rc = libusb_claim_interface(s->dh, i);
- usb_host_libusb_error("libusb_claim_interface", rc);
- if (rc != 0) {
- return USB_RET_STALL;
+ if (rc == 0) {
+ s->ifs[i].claimed = true;
+ if (++claimed == conf->bNumInterfaces) {
+ break;
+ }
}
- s->ifs[i].claimed = true;
+ }
+ if (claimed != conf->bNumInterfaces) {
+ return USB_RET_STALL;
}
udev->ninterfaces = conf->bNumInterfaces;
static void usb_host_release_interfaces(USBHostDevice *s)
{
- USBDevice *udev = USB_DEVICE(s);
int i, rc;
- for (i = 0; i < udev->ninterfaces; i++) {
+ for (i = 0; i < USB_MAX_INTERFACES; i++) {
if (!s->ifs[i].claimed) {
continue;
}
static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
{
- int rc;
+ int rc = 0;
trace_usb_host_set_config(s->bus_num, s->addr, config);
usb_host_release_interfaces(s);
- rc = libusb_set_configuration(s->dh, config);
- if (rc != 0) {
- usb_host_libusb_error("libusb_set_configuration", rc);
- p->status = USB_RET_STALL;
- if (rc == LIBUSB_ERROR_NO_DEVICE) {
- usb_host_nodev(s);
+ if (s->ddesc.bNumConfigurations != 1) {
+ rc = libusb_set_configuration(s->dh, config);
+ if (rc != 0) {
+ usb_host_libusb_error("libusb_set_configuration", rc);
+ p->status = USB_RET_STALL;
+ if (rc == LIBUSB_ERROR_NO_DEVICE) {
+ usb_host_nodev(s);
+ }
+ return;
}
- return;
}
p->status = usb_host_claim_interfaces(s, config);
if (p->status != USB_RET_SUCCESS) {
USBHostDevice *s = USB_HOST_DEVICE(udev);
int rc;
+ if (!s->allow_guest_reset) {
+ return;
+ }
+ if (udev->addr == 0) {
+ return;
+ }
+
trace_usb_host_reset(s->bus_num, s->addr);
rc = libusb_reset_device(s->dh);
if (udev->attached) {
usb_device_detach(udev);
}
+ dev->bh_postld_pending = false;
usb_host_auto_check(NULL);
}
dev->bh_postld = qemu_bh_new(usb_host_post_load_bh, dev);
}
qemu_bh_schedule(dev->bh_postld);
+ dev->bh_postld_pending = true;
return 0;
}
DEFINE_PROP_UINT32("productid", USBHostDevice, match.product_id, 0),
DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32),
+ DEFINE_PROP_BOOL("guest-reset", USBHostDevice, allow_guest_reset, true),
DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel,
LIBUSB_LOG_LEVEL_WARNING),
DEFINE_PROP_BIT("pipeline", USBHostDevice, options,