#include "desc.h"
#include "qemu/error-report.h"
-#define NUM_PORTS 8
+#define MAX_PORTS 8
typedef struct USBHubPort {
USBPort port;
typedef struct USBHubState {
USBDevice dev;
USBEndpoint *intr;
- USBHubPort ports[NUM_PORTS];
+ uint32_t num_ports;
+ USBHubPort ports[MAX_PORTS];
} USBHubState;
#define TYPE_USB_HUB "usb-hub"
{
.bEndpointAddress = USB_DIR_IN | 0x01,
.bmAttributes = USB_ENDPOINT_XFER_INT,
- .wMaxPacketSize = 1 + DIV_ROUND_UP(NUM_PORTS, 8),
+ .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
.bInterval = 0xff,
},
}
USBDevice *downstream;
int i;
- for (i = 0; i < NUM_PORTS; i++) {
+ for (i = 0; i < s->num_ports; i++) {
port = &s->ports[i];
if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
continue;
int i;
trace_usb_hub_reset(s->dev.addr);
- for (i = 0; i < NUM_PORTS; i++) {
+ for (i = 0; i < s->num_ports; i++) {
port = s->ports + i;
port->wPortStatus = PORT_STAT_POWER;
port->wPortChange = 0;
{
unsigned int n = index - 1;
USBHubPort *port;
- if (n >= NUM_PORTS) {
+ if (n >= s->num_ports) {
goto fail;
}
port = &s->ports[n];
trace_usb_hub_set_port_feature(s->dev.addr, index,
feature_name(value));
- if (n >= NUM_PORTS) {
+ if (n >= s->num_ports) {
goto fail;
}
port = &s->ports[n];
trace_usb_hub_clear_port_feature(s->dev.addr, index,
feature_name(value));
- if (n >= NUM_PORTS) {
+ if (n >= s->num_ports) {
goto fail;
}
port = &s->ports[n];
unsigned int n, limit, var_hub_size = 0;
memcpy(data, qemu_hub_hub_descriptor,
sizeof(qemu_hub_hub_descriptor));
- data[2] = NUM_PORTS;
+ data[2] = s->num_ports;
/* fill DeviceRemovable bits */
- limit = DIV_ROUND_UP(NUM_PORTS + 1, 8) + 7;
+ limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7;
for (n = 7; n < limit; n++) {
data[n] = 0x00;
var_hub_size++;
}
/* fill PortPwrCtrlMask bits */
- limit = limit + DIV_ROUND_UP(NUM_PORTS, 8);
+ limit = limit + DIV_ROUND_UP(s->num_ports, 8);
for (;n < limit; n++) {
data[n] = 0xff;
var_hub_size++;
unsigned int status;
uint8_t buf[4];
int i, n;
- n = DIV_ROUND_UP(NUM_PORTS + 1, 8);
+ n = DIV_ROUND_UP(s->num_ports + 1, 8);
if (p->iov.size == 1) { /* FreeBSD workaround */
n = 1;
} else if (n > p->iov.size) {
return;
}
status = 0;
- for(i = 0; i < NUM_PORTS; i++) {
+ for (i = 0; i < s->num_ports; i++) {
port = &s->ports[i];
if (port->wPortChange)
status |= (1 << (i + 1));
USBHubState *s = (USBHubState *)dev;
int i;
- for (i = 0; i < NUM_PORTS; i++) {
+ for (i = 0; i < s->num_ports; i++) {
usb_unregister_port(usb_bus_from_device(dev),
&s->ports[i].port);
}
USBHubPort *port;
int i;
+ if (s->num_ports < 1 || s->num_ports > MAX_PORTS) {
+ error_setg(errp, "num_ports (%d) out of range (1..%d)",
+ s->num_ports, MAX_PORTS);
+ return;
+ }
+
if (dev->port->hubcount == 5) {
error_setg(errp, "usb hub chain too deep");
return;
usb_desc_create_serial(dev);
usb_desc_init(dev);
s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
- for (i = 0; i < NUM_PORTS; i++) {
+ for (i = 0; i < s->num_ports; i++) {
port = &s->ports[i];
usb_register_port(usb_bus_from_device(dev),
&port->port, s, i, &usb_hub_port_ops,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_USB_DEVICE(dev, USBHubState),
- VMSTATE_STRUCT_ARRAY(ports, USBHubState, NUM_PORTS, 0,
+ VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0,
vmstate_usb_hub_port, USBHubPort),
VMSTATE_END_OF_LIST()
}
};
+static Property usb_hub_properties[] = {
+ DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8),
+ DEFINE_PROP_END_OF_LIST(),
+};
+
static void usb_hub_class_initfn(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
dc->fw_name = "hub";
dc->vmsd = &vmstate_usb_hub;
+ dc->props = usb_hub_properties;
}
static const TypeInfo hub_info = {