7 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
9 static struct BusInfo usb_bus_info
= {
11 .size
= sizeof(USBBus
),
12 .print_dev
= usb_bus_dev_print
,
14 static int next_usb_bus
= 0;
15 static TAILQ_HEAD(, USBBus
) busses
= TAILQ_HEAD_INITIALIZER(busses
);
17 USBBus
*usb_bus_new(DeviceState
*host
)
21 bus
= FROM_QBUS(USBBus
, qbus_create(&usb_bus_info
, host
, NULL
));
22 bus
->busnr
= next_usb_bus
++;
23 TAILQ_INIT(&bus
->free
);
24 TAILQ_INIT(&bus
->used
);
25 TAILQ_INSERT_TAIL(&busses
, bus
, next
);
29 USBBus
*usb_bus_find(int busnr
)
34 return TAILQ_FIRST(&busses
);
35 TAILQ_FOREACH(bus
, &busses
, next
) {
36 if (bus
->busnr
== busnr
)
42 static int usb_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
44 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
45 USBDeviceInfo
*info
= DO_UPCAST(USBDeviceInfo
, qdev
, base
);
48 pstrcpy(dev
->devname
, sizeof(dev
->devname
), qdev
->info
->name
);
50 rc
= dev
->info
->init(dev
);
52 usb_device_attach(dev
);
56 void usb_qdev_register(USBDeviceInfo
*info
)
58 info
->qdev
.bus_info
= &usb_bus_info
;
59 info
->qdev
.init
= usb_qdev_init
;
60 qdev_register(&info
->qdev
);
63 void usb_qdev_register_many(USBDeviceInfo
*info
)
65 while (info
->qdev
.name
) {
66 usb_qdev_register(info
);
71 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
76 /* temporary stopgap until all usb is properly qdev-ified */
78 bus
= usb_bus_find(-1);
81 fprintf(stderr
, "%s: no bus specified, using \"%s\" for \"%s\"\n",
82 __FUNCTION__
, bus
->qbus
.name
, name
);
86 dev
= qdev_create(&bus
->qbus
, name
);
87 return DO_UPCAST(USBDevice
, qdev
, dev
);
90 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
92 USBDevice
*dev
= usb_create(bus
, name
);
93 qdev_init(&dev
->qdev
);
97 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
100 port
->opaque
= opaque
;
102 port
->attach
= attach
;
103 TAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
107 static void do_attach(USBDevice
*dev
)
109 USBBus
*bus
= usb_bus_from_device(dev
);
113 fprintf(stderr
, "Warning: tried to attach usb device %s twice\n",
119 port
= TAILQ_FIRST(&bus
->free
);
120 TAILQ_REMOVE(&bus
->free
, port
, next
);
123 usb_attach(port
, dev
);
125 TAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
129 int usb_device_attach(USBDevice
*dev
)
131 USBBus
*bus
= usb_bus_from_device(dev
);
134 if (bus
->nfree
== 1) {
135 /* Create a new hub and chain it on. */
136 hub
= usb_create_simple(bus
, "QEMU USB Hub");
142 int usb_device_delete_addr(int busnr
, int addr
)
148 bus
= usb_bus_find(busnr
);
152 TAILQ_FOREACH(port
, &bus
->used
, next
) {
153 if (port
->dev
->addr
== addr
)
160 TAILQ_REMOVE(&bus
->used
, port
, next
);
163 usb_attach(port
, NULL
);
164 dev
->info
->handle_destroy(dev
);
166 TAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
171 static const char *usb_speed(unsigned int speed
)
173 static const char *txt
[] = {
174 [ USB_SPEED_LOW
] = "1.5",
175 [ USB_SPEED_FULL
] = "12",
176 [ USB_SPEED_HIGH
] = "480",
178 if (speed
>= ARRAY_SIZE(txt
))
183 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
185 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
186 USBBus
*bus
= usb_bus_from_device(dev
);
188 monitor_printf(mon
, "%*saddr %d.%d, speed %s, name %s\n", indent
, "",
189 bus
->busnr
, dev
->addr
,
190 usb_speed(dev
->speed
), dev
->devname
);
193 void usb_info(Monitor
*mon
)
199 if (TAILQ_EMPTY(&busses
)) {
200 monitor_printf(mon
, "USB support not enabled\n");
204 TAILQ_FOREACH(bus
, &busses
, next
) {
205 TAILQ_FOREACH(port
, &bus
->used
, next
) {
209 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
210 bus
->busnr
, dev
->addr
, usb_speed(dev
->speed
), dev
->devname
);