8 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
10 static char *usb_get_dev_path(DeviceState
*dev
);
11 static char *usb_get_fw_dev_path(DeviceState
*qdev
);
12 static int usb_qdev_exit(DeviceState
*qdev
);
14 static struct BusInfo usb_bus_info
= {
16 .size
= sizeof(USBBus
),
17 .print_dev
= usb_bus_dev_print
,
18 .get_dev_path
= usb_get_dev_path
,
19 .get_fw_dev_path
= usb_get_fw_dev_path
,
20 .props
= (Property
[]) {
21 DEFINE_PROP_STRING("port", USBDevice
, port_path
),
22 DEFINE_PROP_END_OF_LIST()
25 static int next_usb_bus
= 0;
26 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
28 const VMStateDescription vmstate_usb_device
= {
31 .minimum_version_id
= 1,
32 .fields
= (VMStateField
[]) {
33 VMSTATE_UINT8(addr
, USBDevice
),
34 VMSTATE_INT32(state
, USBDevice
),
35 VMSTATE_INT32(remote_wakeup
, USBDevice
),
36 VMSTATE_INT32(setup_state
, USBDevice
),
37 VMSTATE_INT32(setup_len
, USBDevice
),
38 VMSTATE_INT32(setup_index
, USBDevice
),
39 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
40 VMSTATE_END_OF_LIST(),
44 void usb_bus_new(USBBus
*bus
, USBBusOps
*ops
, DeviceState
*host
)
46 qbus_create_inplace(&bus
->qbus
, &usb_bus_info
, host
, NULL
);
48 bus
->busnr
= next_usb_bus
++;
49 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
50 QTAILQ_INIT(&bus
->free
);
51 QTAILQ_INIT(&bus
->used
);
52 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
55 USBBus
*usb_bus_find(int busnr
)
60 return QTAILQ_FIRST(&busses
);
61 QTAILQ_FOREACH(bus
, &busses
, next
) {
62 if (bus
->busnr
== busnr
)
68 static int usb_device_init(USBDevice
*dev
)
70 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
72 return klass
->init(dev
);
77 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
79 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
80 if (klass
->find_device
) {
81 return klass
->find_device(dev
, addr
);
86 static void usb_device_handle_destroy(USBDevice
*dev
)
88 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
89 if (klass
->handle_destroy
) {
90 klass
->handle_destroy(dev
);
94 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
96 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
97 if (klass
->cancel_packet
) {
98 klass
->cancel_packet(dev
, p
);
102 void usb_device_handle_attach(USBDevice
*dev
)
104 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
105 if (klass
->handle_attach
) {
106 klass
->handle_attach(dev
);
110 void usb_device_handle_reset(USBDevice
*dev
)
112 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
113 if (klass
->handle_reset
) {
114 klass
->handle_reset(dev
);
118 int usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
119 int value
, int index
, int length
, uint8_t *data
)
121 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
122 if (klass
->handle_control
) {
123 return klass
->handle_control(dev
, p
, request
, value
, index
, length
,
129 int usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
131 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
132 if (klass
->handle_data
) {
133 return klass
->handle_data(dev
, p
);
138 const char *usb_device_get_product_desc(USBDevice
*dev
)
140 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
141 return klass
->product_desc
;
144 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
146 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
147 return klass
->usb_desc
;
150 void usb_device_set_interface(USBDevice
*dev
, int interface
,
151 int alt_old
, int alt_new
)
153 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
154 if (klass
->set_interface
) {
155 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
159 static int usb_qdev_init(DeviceState
*qdev
)
161 USBDevice
*dev
= USB_DEVICE(qdev
);
164 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
165 usb_device_get_product_desc(dev
));
166 dev
->auto_attach
= 1;
167 QLIST_INIT(&dev
->strings
);
169 rc
= usb_claim_port(dev
);
173 rc
= usb_device_init(dev
);
175 usb_release_port(dev
);
178 if (dev
->auto_attach
) {
179 rc
= usb_device_attach(dev
);
188 static int usb_qdev_exit(DeviceState
*qdev
)
190 USBDevice
*dev
= USB_DEVICE(qdev
);
193 usb_device_detach(dev
);
195 usb_device_handle_destroy(dev
);
197 usb_release_port(dev
);
202 typedef struct LegacyUSBFactory
205 const char *usbdevice_name
;
206 USBDevice
*(*usbdevice_init
)(const char *params
);
209 static GSList
*legacy_usb_factory
;
211 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
212 USBDevice
*(*usbdevice_init
)(const char *params
))
214 if (usbdevice_name
) {
215 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
217 f
->usbdevice_name
= usbdevice_name
;
218 f
->usbdevice_init
= usbdevice_init
;
219 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
223 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
228 /* temporary stopgap until all usb is properly qdev-ified */
230 bus
= usb_bus_find(-1);
233 error_report("%s: no bus specified, using \"%s\" for \"%s\"",
234 __FUNCTION__
, bus
->qbus
.name
, name
);
238 dev
= qdev_create(&bus
->qbus
, name
);
239 return USB_DEVICE(dev
);
242 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
244 USBDevice
*dev
= usb_create(bus
, name
);
248 error_report("Failed to create USB device '%s'", name
);
251 rc
= qdev_init(&dev
->qdev
);
253 error_report("Failed to initialize USB device '%s'", name
);
259 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
260 USBPortOps
*ops
, int speedmask
)
262 port
->opaque
= opaque
;
265 port
->speedmask
= speedmask
;
266 usb_port_location(port
, NULL
, index
+ 1);
269 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
270 USBPortOps
*ops
, int speedmask
)
272 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
273 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
277 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
278 uint32_t portcount
, uint32_t firstport
,
279 void *opaque
, USBPortOps
*ops
, int speedmask
)
284 QTAILQ_FOREACH(bus
, &busses
, next
) {
285 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
290 if (!bus
|| !bus
->ops
->register_companion
) {
291 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
294 error_printf_unless_qmp(
295 "USB bus '%s' does not allow companion controllers\n",
301 for (i
= 0; i
< portcount
; i
++) {
302 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
305 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
308 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
311 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
312 upstream
->path
, portnr
);
314 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
318 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
321 qdev_free(&port
->dev
->qdev
);
322 QTAILQ_REMOVE(&bus
->free
, port
, next
);
326 int usb_claim_port(USBDevice
*dev
)
328 USBBus
*bus
= usb_bus_from_device(dev
);
331 assert(dev
->port
== NULL
);
333 if (dev
->port_path
) {
334 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
335 if (strcmp(port
->path
, dev
->port_path
) == 0) {
340 error_report("Error: usb port %s (bus %s) not found (in use?)",
341 dev
->port_path
, bus
->qbus
.name
);
345 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
346 /* Create a new hub and chain it on */
347 usb_create_simple(bus
, "usb-hub");
349 if (bus
->nfree
== 0) {
350 error_report("Error: tried to attach usb device %s to a bus "
351 "with no free ports", dev
->product_desc
);
354 port
= QTAILQ_FIRST(&bus
->free
);
356 trace_usb_port_claim(bus
->busnr
, port
->path
);
358 QTAILQ_REMOVE(&bus
->free
, port
, next
);
364 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
369 void usb_release_port(USBDevice
*dev
)
371 USBBus
*bus
= usb_bus_from_device(dev
);
372 USBPort
*port
= dev
->port
;
374 assert(port
!= NULL
);
375 trace_usb_port_release(bus
->busnr
, port
->path
);
377 QTAILQ_REMOVE(&bus
->used
, port
, next
);
383 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
387 int usb_device_attach(USBDevice
*dev
)
389 USBBus
*bus
= usb_bus_from_device(dev
);
390 USBPort
*port
= dev
->port
;
392 assert(port
!= NULL
);
393 assert(!dev
->attached
);
394 trace_usb_port_attach(bus
->busnr
, port
->path
);
396 if (!(port
->speedmask
& dev
->speedmask
)) {
397 error_report("Warning: speed mismatch trying to attach "
398 "usb device %s to bus %s",
399 dev
->product_desc
, bus
->qbus
.name
);
409 int usb_device_detach(USBDevice
*dev
)
411 USBBus
*bus
= usb_bus_from_device(dev
);
412 USBPort
*port
= dev
->port
;
414 assert(port
!= NULL
);
415 assert(dev
->attached
);
416 trace_usb_port_detach(bus
->busnr
, port
->path
);
423 int usb_device_delete_addr(int busnr
, int addr
)
429 bus
= usb_bus_find(busnr
);
433 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
434 if (port
->dev
->addr
== addr
)
441 qdev_free(&dev
->qdev
);
445 static const char *usb_speed(unsigned int speed
)
447 static const char *txt
[] = {
448 [ USB_SPEED_LOW
] = "1.5",
449 [ USB_SPEED_FULL
] = "12",
450 [ USB_SPEED_HIGH
] = "480",
451 [ USB_SPEED_SUPER
] = "5000",
453 if (speed
>= ARRAY_SIZE(txt
))
458 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
460 USBDevice
*dev
= USB_DEVICE(qdev
);
461 USBBus
*bus
= usb_bus_from_device(dev
);
463 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
464 indent
, "", bus
->busnr
, dev
->addr
,
465 dev
->port
? dev
->port
->path
: "-",
466 usb_speed(dev
->speed
), dev
->product_desc
,
467 dev
->attached
? ", attached" : "");
470 static char *usb_get_dev_path(DeviceState
*qdev
)
472 USBDevice
*dev
= USB_DEVICE(qdev
);
473 return g_strdup(dev
->port
->path
);
476 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
478 USBDevice
*dev
= USB_DEVICE(qdev
);
480 ssize_t pos
= 0, fw_len
;
483 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
484 fw_path
= g_malloc(fw_len
);
485 in
= dev
->port
->path
;
486 while (fw_len
- pos
> 0) {
487 nr
= strtol(in
, &in
, 10);
489 /* some hub between root port and device */
490 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
493 /* the device itself */
494 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
495 qdev_fw_name(qdev
), nr
);
502 void usb_info(Monitor
*mon
)
508 if (QTAILQ_EMPTY(&busses
)) {
509 monitor_printf(mon
, "USB support not enabled\n");
513 QTAILQ_FOREACH(bus
, &busses
, next
) {
514 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
518 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
519 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
525 /* handle legacy -usbdevice cmd line option */
526 USBDevice
*usbdevice_create(const char *cmdline
)
528 USBBus
*bus
= usb_bus_find(-1 /* any */);
529 LegacyUSBFactory
*f
= NULL
;
535 params
= strchr(cmdline
,':');
538 len
= params
- cmdline
;
539 if (len
> sizeof(driver
))
540 len
= sizeof(driver
);
541 pstrcpy(driver
, len
, cmdline
);
544 pstrcpy(driver
, sizeof(driver
), cmdline
);
547 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
549 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
555 /* no error because some drivers are not converted (yet) */
556 error_report("usbdevice %s not found", driver
);
561 if (!f
->usbdevice_init
) {
563 error_report("usbdevice %s accepts no params", driver
);
566 return usb_create_simple(bus
, f
->name
);
568 return f
->usbdevice_init(params
);
571 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
573 DeviceClass
*k
= DEVICE_CLASS(klass
);
574 k
->bus_info
= &usb_bus_info
;
575 k
->init
= usb_qdev_init
;
576 k
->unplug
= qdev_simple_unplug_cb
;
577 k
->exit
= usb_qdev_exit
;
580 static TypeInfo usb_device_type_info
= {
581 .name
= TYPE_USB_DEVICE
,
582 .parent
= TYPE_DEVICE
,
583 .instance_size
= sizeof(USBDevice
),
585 .class_size
= sizeof(USBDeviceClass
),
586 .class_init
= usb_device_class_init
,
589 static void usb_register_types(void)
591 type_register_static(&usb_device_type_info
);
594 type_init(usb_register_types
)