Commit b947c12c authored by Aurelien Jarno's avatar Aurelien Jarno
Browse files

Merge branch 'usb.4' of git://anongit.freedesktop.org/spice/qemu

* 'usb.4' of git://anongit.freedesktop.org/spice/qemu: (32 commits)
  usb: zap pdev from usbport
  usb: rewrite fw path, fix numbering
  usb: add port property.
  usb: keep track of physical port address.
  usb storage: handle long responses
  usb storage: fix status reporting
  usb storage: high speed support
  usb: add device qualifier support
  usb: add usb_desc_attach
  usb: add attach callback
  usb: add speed mask to ports
  usb: hid: change serial number to "42".
  usb: hid: remote wakeup support.
  usb: hub: remote wakeup support.
  usb: uhci: remote wakeup support.
  usb: add usb_wakeup() + wakeup callback to port ops
  usb: rework attach/detach workflow
  usb: create USBPortOps, move attach there.
  usb: move remote wakeup handling to common code
  usb: move USB_REQ_{GET,SET}_CONFIGURATION handling to common code
  ...
parents 543c4c94 ace1318b
......@@ -329,8 +329,8 @@ F: hw/lsi53c895a.c
F: hw/scsi*
USB
M: qemu-devel@nongnu.org
S: Odd Fixes
M: Gerd Hoffmann <kraxel@redhat.com>
S: Maintained
F: hw/usb*
vhost
......
......@@ -88,7 +88,7 @@ common-obj-y += eeprom93xx.o
common-obj-y += scsi-disk.o cdrom.o
common-obj-y += scsi-generic.o scsi-bus.o
common-obj-y += usb.o usb-hub.o usb-$(HOST_USB).o usb-hid.o usb-msd.o usb-wacom.o
common-obj-y += usb-serial.o usb-net.o usb-bus.o
common-obj-y += usb-serial.o usb-net.o usb-bus.o usb-desc.o
common-obj-$(CONFIG_SSI) += ssi.o
common-obj-$(CONFIG_SSI_SD) += ssi-sd.o
common-obj-$(CONFIG_SD) += sd.o
......
This diff is collapsed.
......@@ -5,13 +5,20 @@
#include "monitor.h"
static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
static char *usbbus_get_fw_dev_path(DeviceState *dev);
static char *usb_get_dev_path(DeviceState *dev);
static char *usb_get_fw_dev_path(DeviceState *qdev);
static struct BusInfo usb_bus_info = {
.name = "USB",
.size = sizeof(USBBus),
.print_dev = usb_bus_dev_print,
.get_fw_dev_path = usbbus_get_fw_dev_path,
.get_dev_path = usb_get_dev_path,
.get_fw_dev_path = usb_get_fw_dev_path,
.props = (Property[]) {
DEFINE_PROP_STRING("port", USBDevice, port_path),
DEFINE_PROP_END_OF_LIST()
},
};
static int next_usb_bus = 0;
static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
......@@ -48,6 +55,7 @@ static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
dev->info = info;
dev->auto_attach = 1;
QLIST_INIT(&dev->strings);
rc = dev->info->init(dev);
if (rc == 0 && dev->auto_attach)
usb_device_attach(dev);
......@@ -112,16 +120,28 @@ USBDevice *usb_create_simple(USBBus *bus, const char *name)
}
void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
USBDevice *pdev, usb_attachfn attach)
USBPortOps *ops, int speedmask)
{
port->opaque = opaque;
port->index = index;
port->attach = attach;
port->pdev = pdev;
port->opaque = opaque;
port->index = index;
port->ops = ops;
port->speedmask = speedmask;
QTAILQ_INSERT_TAIL(&bus->free, port, next);
bus->nfree++;
}
void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
{
if (upstream) {
snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
upstream->path, portnr);
} else {
snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
}
}
void usb_unregister_port(USBBus *bus, USBPort *port)
{
if (port->dev)
......@@ -140,9 +160,22 @@ static void do_attach(USBDevice *dev)
dev->product_desc);
return;
}
dev->attached++;
if (dev->port_path) {
QTAILQ_FOREACH(port, &bus->free, next) {
if (strcmp(port->path, dev->port_path) == 0) {
break;
}
}
if (port == NULL) {
fprintf(stderr, "Warning: usb port %s (bus %s) not found\n",
dev->port_path, bus->qbus.name);
return;
}
} else {
port = QTAILQ_FIRST(&bus->free);
}
port = QTAILQ_FIRST(&bus->free);
dev->attached++;
QTAILQ_REMOVE(&bus->free, port, next);
bus->nfree--;
......@@ -156,8 +189,9 @@ int usb_device_attach(USBDevice *dev)
{
USBBus *bus = usb_bus_from_device(dev);
if (bus->nfree == 1) {
/* Create a new hub and chain it on. */
if (bus->nfree == 1 && dev->port_path == NULL) {
/* Create a new hub and chain it on
(unless a physical port location is specified). */
usb_create_simple(bus, "usb-hub");
}
do_attach(dev);
......@@ -231,12 +265,43 @@ static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
USBBus *bus = usb_bus_from_device(dev);
monitor_printf(mon, "%*saddr %d.%d, speed %s, name %s%s\n",
monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
indent, "", bus->busnr, dev->addr,
dev->port ? dev->port->path : "-",
usb_speed(dev->speed), dev->product_desc,
dev->attached ? ", attached" : "");
}
static char *usb_get_dev_path(DeviceState *qdev)
{
USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
return qemu_strdup(dev->port->path);
}
static char *usb_get_fw_dev_path(DeviceState *qdev)
{
USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
char *fw_path, *in;
int pos = 0;
long nr;
fw_path = qemu_malloc(32 + strlen(dev->port->path) * 6);
in = dev->port->path;
while (true) {
nr = strtol(in, &in, 10);
if (in[0] == '.') {
/* some hub between root port and device */
pos += sprintf(fw_path + pos, "hub@%ld/", nr);
in++;
} else {
/* the device itself */
pos += sprintf(fw_path + pos, "%s@%ld", qdev_fw_name(qdev), nr);
break;
}
}
return fw_path;
}
void usb_info(Monitor *mon)
{
USBBus *bus;
......@@ -253,8 +318,8 @@ void usb_info(Monitor *mon)
dev = port->dev;
if (!dev)
continue;
monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
bus->busnr, dev->addr, usb_speed(dev->speed),
monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
dev->product_desc);
}
}
......@@ -309,43 +374,3 @@ USBDevice *usbdevice_create(const char *cmdline)
}
return usb->usbdevice_init(params);
}
static int usbbus_get_fw_dev_path_helper(USBDevice *d, USBBus *bus, char *p,
int len)
{
int l = 0;
USBPort *port;
QTAILQ_FOREACH(port, &bus->used, next) {
if (port->dev == d) {
if (port->pdev) {
l = usbbus_get_fw_dev_path_helper(port->pdev, bus, p, len);
}
l += snprintf(p + l, len - l, "%s@%x/", qdev_fw_name(&d->qdev),
port->index);
break;
}
}
return l;
}
static char *usbbus_get_fw_dev_path(DeviceState *dev)
{
USBDevice *d = (USBDevice*)dev;
USBBus *bus = usb_bus_from_device(d);
char path[100];
int l;
assert(d->attached != 0);
l = usbbus_get_fw_dev_path_helper(d, bus, path, sizeof(path));
if (l == 0) {
abort();
}
path[l-1] = '\0';
return strdup(path);
}
#include "usb.h"
#include "usb-desc.h"
#include "trace.h"
/* ------------------------------------------------------------------ */
static uint8_t usb_lo(uint16_t val)
{
return val & 0xff;
}
static uint8_t usb_hi(uint16_t val)
{
return (val >> 8) & 0xff;
}
int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
uint8_t *dest, size_t len)
{
uint8_t bLength = 0x12;
if (len < bLength) {
return -1;
}
dest[0x00] = bLength;
dest[0x01] = USB_DT_DEVICE;
dest[0x02] = usb_lo(dev->bcdUSB);
dest[0x03] = usb_hi(dev->bcdUSB);
dest[0x04] = dev->bDeviceClass;
dest[0x05] = dev->bDeviceSubClass;
dest[0x06] = dev->bDeviceProtocol;
dest[0x07] = dev->bMaxPacketSize0;
dest[0x08] = usb_lo(id->idVendor);
dest[0x09] = usb_hi(id->idVendor);
dest[0x0a] = usb_lo(id->idProduct);
dest[0x0b] = usb_hi(id->idProduct);
dest[0x0c] = usb_lo(id->bcdDevice);
dest[0x0d] = usb_hi(id->bcdDevice);
dest[0x0e] = id->iManufacturer;
dest[0x0f] = id->iProduct;
dest[0x10] = id->iSerialNumber;
dest[0x11] = dev->bNumConfigurations;
return bLength;
}
int usb_desc_device_qualifier(const USBDescDevice *dev,
uint8_t *dest, size_t len)
{
uint8_t bLength = 0x0a;
if (len < bLength) {
return -1;
}
dest[0x00] = bLength;
dest[0x01] = USB_DT_DEVICE_QUALIFIER;
dest[0x02] = usb_lo(dev->bcdUSB);
dest[0x03] = usb_hi(dev->bcdUSB);
dest[0x04] = dev->bDeviceClass;
dest[0x05] = dev->bDeviceSubClass;
dest[0x06] = dev->bDeviceProtocol;
dest[0x07] = dev->bMaxPacketSize0;
dest[0x08] = dev->bNumConfigurations;
dest[0x09] = 0; /* reserved */
return bLength;
}
int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
{
uint8_t bLength = 0x09;
uint16_t wTotalLength = 0;
int i, rc, count;
if (len < bLength) {
return -1;
}
dest[0x00] = bLength;
dest[0x01] = USB_DT_CONFIG;
dest[0x04] = conf->bNumInterfaces;
dest[0x05] = conf->bConfigurationValue;
dest[0x06] = conf->iConfiguration;
dest[0x07] = conf->bmAttributes;
dest[0x08] = conf->bMaxPower;
wTotalLength += bLength;
count = conf->nif ? conf->nif : conf->bNumInterfaces;
for (i = 0; i < count; i++) {
rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength);
if (rc < 0) {
return rc;
}
wTotalLength += rc;
}
dest[0x02] = usb_lo(wTotalLength);
dest[0x03] = usb_hi(wTotalLength);
return wTotalLength;
}
int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
{
uint8_t bLength = 0x09;
int i, rc, pos = 0;
if (len < bLength) {
return -1;
}
dest[0x00] = bLength;
dest[0x01] = USB_DT_INTERFACE;
dest[0x02] = iface->bInterfaceNumber;
dest[0x03] = iface->bAlternateSetting;
dest[0x04] = iface->bNumEndpoints;
dest[0x05] = iface->bInterfaceClass;
dest[0x06] = iface->bInterfaceSubClass;
dest[0x07] = iface->bInterfaceProtocol;
dest[0x08] = iface->iInterface;
pos += bLength;
for (i = 0; i < iface->ndesc; i++) {
rc = usb_desc_other(iface->descs + i, dest + pos, len - pos);
if (rc < 0) {
return rc;
}
pos += rc;
}
for (i = 0; i < iface->bNumEndpoints; i++) {
rc = usb_desc_endpoint(iface->eps + i, dest + pos, len - pos);
if (rc < 0) {
return rc;
}
pos += rc;
}
return pos;
}
int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
{
uint8_t bLength = 0x07;
if (len < bLength) {
return -1;
}
dest[0x00] = bLength;
dest[0x01] = USB_DT_ENDPOINT;
dest[0x02] = ep->bEndpointAddress;
dest[0x03] = ep->bmAttributes;
dest[0x04] = usb_lo(ep->wMaxPacketSize);
dest[0x05] = usb_hi(ep->wMaxPacketSize);
dest[0x06] = ep->bInterval;
return bLength;
}
int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
{
int bLength = desc->length ? desc->length : desc->data[0];
if (len < bLength) {
return -1;
}
memcpy(dest, desc->data, bLength);
return bLength;
}
/* ------------------------------------------------------------------ */
static void usb_desc_setdefaults(USBDevice *dev)
{
const USBDesc *desc = dev->info->usb_desc;
assert(desc != NULL);
switch (dev->speed) {
case USB_SPEED_LOW:
case USB_SPEED_FULL:
dev->device = desc->full;
break;
case USB_SPEED_HIGH:
dev->device = desc->high;
break;
}
dev->config = dev->device->confs;
}
void usb_desc_init(USBDevice *dev)
{
dev->speed = USB_SPEED_FULL;
usb_desc_setdefaults(dev);
}
void usb_desc_attach(USBDevice *dev)
{
const USBDesc *desc = dev->info->usb_desc;
assert(desc != NULL);
if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
dev->speed = USB_SPEED_HIGH;
} else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
dev->speed = USB_SPEED_FULL;
} else {
fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
dev->info->product_desc);
return;
}
usb_desc_setdefaults(dev);
}
void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
{
USBDescString *s;
QLIST_FOREACH(s, &dev->strings, next) {
if (s->index == index) {
break;
}
}
if (s == NULL) {
s = qemu_mallocz(sizeof(*s));
s->index = index;
QLIST_INSERT_HEAD(&dev->strings, s, next);
}
qemu_free(s->str);
s->str = qemu_strdup(str);
}
const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
{
USBDescString *s;
QLIST_FOREACH(s, &dev->strings, next) {
if (s->index == index) {
return s->str;
}
}
return NULL;
}
int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
{
uint8_t bLength, pos, i;
const char *str;
if (len < 4) {
return -1;
}
if (index == 0) {
/* language ids */
dest[0] = 4;
dest[1] = USB_DT_STRING;
dest[2] = 0x09;
dest[3] = 0x04;
return 4;
}
str = usb_desc_get_string(dev, index);
if (str == NULL) {
str = dev->info->usb_desc->str[index];
if (str == NULL) {
return 0;
}
}
bLength = strlen(str) * 2 + 2;
dest[0] = bLength;
dest[1] = USB_DT_STRING;
i = 0; pos = 2;
while (pos+1 < bLength && pos+1 < len) {
dest[pos++] = str[i++];
dest[pos++] = 0;
}
return pos;
}
int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
{
const USBDesc *desc = dev->info->usb_desc;
const USBDescDevice *other_dev;
uint8_t buf[256];
uint8_t type = value >> 8;
uint8_t index = value & 0xff;
int ret = -1;
if (dev->speed == USB_SPEED_HIGH) {
other_dev = dev->info->usb_desc->full;
} else {
other_dev = dev->info->usb_desc->high;
}
switch(type) {
case USB_DT_DEVICE:
ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
trace_usb_desc_device(dev->addr, len, ret);
break;
case USB_DT_CONFIG:
if (index < dev->device->bNumConfigurations) {
ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
}
trace_usb_desc_config(dev->addr, index, len, ret);
break;
case USB_DT_STRING:
ret = usb_desc_string(dev, index, buf, sizeof(buf));
trace_usb_desc_string(dev->addr, index, len, ret);
break;
case USB_DT_DEVICE_QUALIFIER:
if (other_dev != NULL) {
ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
}
trace_usb_desc_device_qualifier(dev->addr, len, ret);
break;
case USB_DT_OTHER_SPEED_CONFIG:
if (other_dev != NULL && index < other_dev->bNumConfigurations) {
ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
}
trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
break;
default:
fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
dev->addr, type, len);
break;
}
if (ret > 0) {
if (ret > len) {
ret = len;
}
memcpy(dest, buf, ret);
}
return ret;
}
int usb_desc_handle_control(USBDevice *dev, int request, int value,
int index, int length, uint8_t *data)
{
const USBDesc *desc = dev->info->usb_desc;
int i, ret = -1;
assert(desc != NULL);
switch(request) {
case DeviceOutRequest | USB_REQ_SET_ADDRESS:
dev->addr = value;
trace_usb_set_addr(dev->addr);
ret = 0;
break;
case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
ret = usb_desc_get_descriptor(dev, value, data, length);
break;
case DeviceRequest | USB_REQ_GET_CONFIGURATION:
data[0] = dev->config->bConfigurationValue;
ret = 1;
break;
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
for (i = 0; i < dev->device->bNumConfigurations; i++) {
if (dev->device->confs[i].bConfigurationValue == value) {
dev->config = dev->device->confs + i;
ret = 0;
}
}
trace_usb_set_config(dev->addr, value, ret);
break;
case DeviceRequest | USB_REQ_GET_STATUS:
data[0] = 0;
if (dev->config->bmAttributes & 0x40) {
data[0] |= 1 << USB_DEVICE_SELF_POWERED;
}
if (dev->remote_wakeup) {
data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
}
data[1] = 0x00;
ret = 2;
break;
case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
if (value == USB_DEVICE_REMOTE_WAKEUP) {
dev->remote_wakeup = 0;
ret = 0;
}
trace_usb_clear_device_feature(dev->addr, value, ret);
break;
case DeviceOutRequest | USB_REQ_SET_FEATURE:
if (value == USB_DEVICE_REMOTE_WAKEUP) {
dev->remote_wakeup = 1;