]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/bus.c
usb: Do not prefix error_setg() messages with "Error: "
[mirror_qemu.git] / hw / usb / bus.c
CommitLineData
f1ae32a1
GH
1#include "hw/hw.h"
2#include "hw/usb.h"
3#include "hw/qdev.h"
9c17d615 4#include "sysemu/sysemu.h"
83c9089e 5#include "monitor/monitor.h"
891fb2cd 6#include "trace.h"
a5d2f727
GH
7
8static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
c7a2196a
GH
9
10static char *usb_get_dev_path(DeviceState *dev);
70d31cb2 11static char *usb_get_fw_dev_path(DeviceState *qdev);
7d553f27 12static void usb_qdev_unrealize(DeviceState *qdev, Error **errp);
806b6024 13
3cb75a7c
PB
14static Property usb_props[] = {
15 DEFINE_PROP_STRING("port", USBDevice, port_path),
71938a09 16 DEFINE_PROP_STRING("serial", USBDevice, serial),
3cb75a7c
PB
17 DEFINE_PROP_BIT("full-path", USBDevice, flags,
18 USB_DEV_FLAG_FULL_PATH, true),
5319dc7b
GH
19 DEFINE_PROP_BIT("msos-desc", USBDevice, flags,
20 USB_DEV_FLAG_MSOS_DESC_ENABLE, true),
3cb75a7c
PB
21 DEFINE_PROP_END_OF_LIST()
22};
23
0d936928
AL
24static void usb_bus_class_init(ObjectClass *klass, void *data)
25{
26 BusClass *k = BUS_CLASS(klass);
5f4d9173 27 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
0d936928
AL
28
29 k->print_dev = usb_bus_dev_print;
30 k->get_dev_path = usb_get_dev_path;
31 k->get_fw_dev_path = usb_get_fw_dev_path;
5f4d9173 32 hc->unplug = qdev_simple_device_unplug_cb;
0d936928
AL
33}
34
35static const TypeInfo usb_bus_info = {
36 .name = TYPE_USB_BUS,
37 .parent = TYPE_BUS,
38 .instance_size = sizeof(USBBus),
39 .class_init = usb_bus_class_init,
5f4d9173
IM
40 .interfaces = (InterfaceInfo[]) {
41 { TYPE_HOTPLUG_HANDLER },
42 { }
43 }
806b6024 44};
3cb75a7c 45
806b6024 46static int next_usb_bus = 0;
72cf2d4f 47static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
806b6024 48
495d5447
GH
49static int usb_device_post_load(void *opaque, int version_id)
50{
51 USBDevice *dev = opaque;
52
53 if (dev->state == USB_STATE_NOTATTACHED) {
54 dev->attached = 0;
55 } else {
56 dev->attached = 1;
57 }
9f8e9895
MT
58 if (dev->setup_index < 0 ||
59 dev->setup_len < 0 ||
719ffe1f
MT
60 dev->setup_index > dev->setup_len ||
61 dev->setup_len > sizeof(dev->data_buf)) {
c60174e8
GH
62 return -EINVAL;
63 }
495d5447
GH
64 return 0;
65}
66
c1ecb40a
GH
67const VMStateDescription vmstate_usb_device = {
68 .name = "USBDevice",
69 .version_id = 1,
70 .minimum_version_id = 1,
495d5447 71 .post_load = usb_device_post_load,
6e3d652a 72 .fields = (VMStateField[]) {
c1ecb40a
GH
73 VMSTATE_UINT8(addr, USBDevice),
74 VMSTATE_INT32(state, USBDevice),
75 VMSTATE_INT32(remote_wakeup, USBDevice),
76 VMSTATE_INT32(setup_state, USBDevice),
77 VMSTATE_INT32(setup_len, USBDevice),
78 VMSTATE_INT32(setup_index, USBDevice),
79 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
80 VMSTATE_END_OF_LIST(),
81 }
82};
83
c889b3a5
AF
84void usb_bus_new(USBBus *bus, size_t bus_size,
85 USBBusOps *ops, DeviceState *host)
806b6024 86{
fb17dfe0 87 qbus_create_inplace(bus, bus_size, TYPE_USB_BUS, host, NULL);
5f4d9173 88 qbus_set_bus_hotplug_handler(BUS(bus), &error_abort);
07771f6f 89 bus->ops = ops;
806b6024 90 bus->busnr = next_usb_bus++;
72cf2d4f
BS
91 QTAILQ_INIT(&bus->free);
92 QTAILQ_INIT(&bus->used);
93 QTAILQ_INSERT_TAIL(&busses, bus, next);
806b6024
GH
94}
95
e5a9bece
GA
96void usb_bus_release(USBBus *bus)
97{
98 assert(next_usb_bus > 0);
99
100 QTAILQ_REMOVE(&busses, bus, next);
101}
102
806b6024
GH
103USBBus *usb_bus_find(int busnr)
104{
105 USBBus *bus;
106
107 if (-1 == busnr)
72cf2d4f
BS
108 return QTAILQ_FIRST(&busses);
109 QTAILQ_FOREACH(bus, &busses, next) {
806b6024
GH
110 if (bus->busnr == busnr)
111 return bus;
112 }
113 return NULL;
114}
115
7d553f27 116static void usb_device_realize(USBDevice *dev, Error **errp)
62aed765
AL
117{
118 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
7d553f27
GA
119
120 if (klass->realize) {
121 klass->realize(dev, errp);
62aed765 122 }
62aed765
AL
123}
124
73796fe6 125USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
62aed765
AL
126{
127 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
73796fe6
GH
128 if (klass->find_device) {
129 return klass->find_device(dev, addr);
62aed765 130 }
73796fe6 131 return NULL;
62aed765
AL
132}
133
62aed765 134static void usb_device_handle_destroy(USBDevice *dev)
62aed765
AL
135{
136 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
62aed765
AL
137 if (klass->handle_destroy) {
138 klass->handle_destroy(dev);
62aed765 139 }
62aed765
AL
140}
141
142void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
143{
144 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
145 if (klass->cancel_packet) {
146 klass->cancel_packet(dev, p);
147 }
148}
149
150void usb_device_handle_attach(USBDevice *dev)
151{
152 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
153 if (klass->handle_attach) {
154 klass->handle_attach(dev);
155 }
156}
157
158void usb_device_handle_reset(USBDevice *dev)
159{
160 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
161 if (klass->handle_reset) {
162 klass->handle_reset(dev);
163 }
164}
165
9a77a0f5
HG
166void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
167 int value, int index, int length, uint8_t *data)
62aed765
AL
168{
169 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
170 if (klass->handle_control) {
9a77a0f5 171 klass->handle_control(dev, p, request, value, index, length, data);
62aed765 172 }
62aed765
AL
173}
174
9a77a0f5 175void usb_device_handle_data(USBDevice *dev, USBPacket *p)
62aed765
AL
176{
177 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
178 if (klass->handle_data) {
9a77a0f5 179 klass->handle_data(dev, p);
62aed765 180 }
62aed765
AL
181}
182
183const char *usb_device_get_product_desc(USBDevice *dev)
184{
185 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
186 return klass->product_desc;
187}
188
189const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
190{
191 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
386ab487
HG
192 if (dev->usb_desc) {
193 return dev->usb_desc;
194 }
62aed765
AL
195 return klass->usb_desc;
196}
197
198void usb_device_set_interface(USBDevice *dev, int interface,
199 int alt_old, int alt_new)
200{
201 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
202 if (klass->set_interface) {
203 klass->set_interface(dev, interface, alt_old, alt_new);
204 }
205}
206
36dfe324
HG
207void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
208{
209 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
210 if (klass->flush_ep_queue) {
211 klass->flush_ep_queue(dev, ep);
212 }
213}
214
f79738b0
HG
215void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
216{
217 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
218 if (klass->ep_stopped) {
219 klass->ep_stopped(dev, ep);
220 }
221}
222
3b444ead
HG
223int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
224 int streams)
225{
226 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
227 if (klass->alloc_streams) {
228 return klass->alloc_streams(dev, eps, nr_eps, streams);
229 }
230 return 0;
231}
232
233void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
234{
235 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
236 if (klass->free_streams) {
237 klass->free_streams(dev, eps, nr_eps);
238 }
239}
240
7d553f27 241static void usb_qdev_realize(DeviceState *qdev, Error **errp)
806b6024 242{
62aed765 243 USBDevice *dev = USB_DEVICE(qdev);
7d553f27 244 Error *local_err = NULL;
806b6024 245
62aed765
AL
246 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
247 usb_device_get_product_desc(dev));
61e094c0 248 dev->auto_attach = 1;
132a3f55 249 QLIST_INIT(&dev->strings);
d8e17efd 250 usb_ep_init(dev);
7d553f27
GA
251
252 usb_claim_port(dev, &local_err);
253 if (local_err) {
254 error_propagate(errp, local_err);
255 return;
891fb2cd 256 }
7d553f27
GA
257
258 usb_device_realize(dev, &local_err);
259 if (local_err) {
db3a5ed7 260 usb_release_port(dev);
7d553f27
GA
261 error_propagate(errp, local_err);
262 return;
f462141f 263 }
7d553f27 264
f462141f 265 if (dev->auto_attach) {
7d553f27
GA
266 usb_device_attach(dev, &local_err);
267 if (local_err) {
268 usb_qdev_unrealize(qdev, NULL);
269 error_propagate(errp, local_err);
270 return;
f462141f 271 }
891fb2cd 272 }
806b6024
GH
273}
274
7d553f27 275static void usb_qdev_unrealize(DeviceState *qdev, Error **errp)
a8e662b5 276{
62aed765 277 USBDevice *dev = USB_DEVICE(qdev);
a8e662b5 278
290a5c60
HG
279 if (dev->attached) {
280 usb_device_detach(dev);
281 }
62aed765 282 usb_device_handle_destroy(dev);
891fb2cd
GH
283 if (dev->port) {
284 usb_release_port(dev);
285 }
a8e662b5
GH
286}
287
62aed765 288typedef struct LegacyUSBFactory
806b6024 289{
62aed765
AL
290 const char *name;
291 const char *usbdevice_name;
3741715c 292 USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
62aed765 293} LegacyUSBFactory;
806b6024 294
62aed765
AL
295static GSList *legacy_usb_factory;
296
ba02430f 297void usb_legacy_register(const char *typename, const char *usbdevice_name,
3741715c
JK
298 USBDevice *(*usbdevice_init)(USBBus *bus,
299 const char *params))
806b6024 300{
62aed765
AL
301 if (usbdevice_name) {
302 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
ba02430f 303 f->name = typename;
62aed765
AL
304 f->usbdevice_name = usbdevice_name;
305 f->usbdevice_init = usbdevice_init;
306 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
806b6024
GH
307 }
308}
309
a5d2f727 310USBDevice *usb_create(USBBus *bus, const char *name)
806b6024
GH
311{
312 DeviceState *dev;
313
806b6024 314 dev = qdev_create(&bus->qbus, name);
62aed765 315 return USB_DEVICE(dev);
806b6024 316}
a5d2f727
GH
317
318USBDevice *usb_create_simple(USBBus *bus, const char *name)
319{
320 USBDevice *dev = usb_create(bus, name);
2af2a1b8
GH
321 int rc;
322
2af2a1b8
GH
323 rc = qdev_init(&dev->qdev);
324 if (rc < 0) {
be62a2eb 325 error_report("Failed to initialize USB device '%s'", name);
2af2a1b8 326 return NULL;
d44168ff 327 }
a5d2f727
GH
328 return dev;
329}
330
090ac642
HG
331static void usb_fill_port(USBPort *port, void *opaque, int index,
332 USBPortOps *ops, int speedmask)
a5d2f727 333{
0d86d2be
GH
334 port->opaque = opaque;
335 port->index = index;
336 port->ops = ops;
843d4e0c 337 port->speedmask = speedmask;
3631e6c8 338 usb_port_location(port, NULL, index + 1);
090ac642
HG
339}
340
341void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
342 USBPortOps *ops, int speedmask)
343{
344 usb_fill_port(port, opaque, index, ops, speedmask);
72cf2d4f 345 QTAILQ_INSERT_TAIL(&bus->free, port, next);
a5d2f727
GH
346 bus->nfree++;
347}
348
ae60fea9
HG
349int usb_register_companion(const char *masterbus, USBPort *ports[],
350 uint32_t portcount, uint32_t firstport,
351 void *opaque, USBPortOps *ops, int speedmask)
352{
353 USBBus *bus;
354 int i;
355
356 QTAILQ_FOREACH(bus, &busses, next) {
357 if (strcmp(bus->qbus.name, masterbus) == 0) {
358 break;
359 }
360 }
361
362 if (!bus || !bus->ops->register_companion) {
363 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
364 "an USB masterbus");
365 if (bus) {
366 error_printf_unless_qmp(
367 "USB bus '%s' does not allow companion controllers\n",
368 masterbus);
369 }
370 return -1;
371 }
372
373 for (i = 0; i < portcount; i++) {
374 usb_fill_port(ports[i], opaque, i, ops, speedmask);
375 }
376
377 return bus->ops->register_companion(bus, ports, portcount, firstport);
378}
379
c7a2196a
GH
380void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
381{
382 if (upstream) {
383 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
384 upstream->path, portnr);
c24e4aac 385 downstream->hubcount = upstream->hubcount + 1;
c7a2196a
GH
386 } else {
387 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
c24e4aac 388 downstream->hubcount = 0;
c7a2196a
GH
389 }
390}
391
a8e662b5
GH
392void usb_unregister_port(USBBus *bus, USBPort *port)
393{
02a5c4c9
SH
394 if (port->dev) {
395 object_unparent(OBJECT(port->dev));
396 }
a8e662b5
GH
397 QTAILQ_REMOVE(&bus->free, port, next);
398 bus->nfree--;
399}
400
7d553f27 401void usb_claim_port(USBDevice *dev, Error **errp)
a5d2f727
GH
402{
403 USBBus *bus = usb_bus_from_device(dev);
404 USBPort *port;
405
891fb2cd
GH
406 assert(dev->port == NULL);
407
5f69076b
GH
408 if (dev->port_path) {
409 QTAILQ_FOREACH(port, &bus->free, next) {
410 if (strcmp(port->path, dev->port_path) == 0) {
411 break;
412 }
413 }
414 if (port == NULL) {
06f22eb7 415 error_setg(errp, "usb port %s (bus %s) not found (in use?)",
7d553f27
GA
416 dev->port_path, bus->qbus.name);
417 return;
5f69076b
GH
418 }
419 } else {
f79f2bfc 420 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
891fb2cd
GH
421 /* Create a new hub and chain it on */
422 usb_create_simple(bus, "usb-hub");
423 }
424 if (bus->nfree == 0) {
06f22eb7 425 error_setg(errp, "tried to attach usb device %s to a bus "
7d553f27
GA
426 "with no free ports", dev->product_desc);
427 return;
891fb2cd 428 }
5f69076b
GH
429 port = QTAILQ_FIRST(&bus->free);
430 }
891fb2cd 431 trace_usb_port_claim(bus->busnr, port->path);
a5d2f727 432
72cf2d4f 433 QTAILQ_REMOVE(&bus->free, port, next);
a5d2f727
GH
434 bus->nfree--;
435
891fb2cd
GH
436 dev->port = port;
437 port->dev = dev;
a5d2f727 438
72cf2d4f 439 QTAILQ_INSERT_TAIL(&bus->used, port, next);
a5d2f727
GH
440 bus->nused++;
441}
442
891fb2cd 443void usb_release_port(USBDevice *dev)
a5d2f727
GH
444{
445 USBBus *bus = usb_bus_from_device(dev);
891fb2cd 446 USBPort *port = dev->port;
a5d2f727 447
891fb2cd
GH
448 assert(port != NULL);
449 trace_usb_port_release(bus->busnr, port->path);
450
451 QTAILQ_REMOVE(&bus->used, port, next);
452 bus->nused--;
453
454 dev->port = NULL;
455 port->dev = NULL;
456
457 QTAILQ_INSERT_TAIL(&bus->free, port, next);
458 bus->nfree++;
a5d2f727
GH
459}
460
3b7e759a
GH
461static void usb_mask_to_str(char *dest, size_t size,
462 unsigned int speedmask)
463{
464 static const struct {
465 unsigned int mask;
466 const char *name;
467 } speeds[] = {
468 { .mask = USB_SPEED_MASK_FULL, .name = "full" },
469 { .mask = USB_SPEED_MASK_HIGH, .name = "high" },
470 { .mask = USB_SPEED_MASK_SUPER, .name = "super" },
471 };
472 int i, pos = 0;
473
474 for (i = 0; i < ARRAY_SIZE(speeds); i++) {
475 if (speeds[i].mask & speedmask) {
476 pos += snprintf(dest + pos, size - pos, "%s%s",
477 pos ? "+" : "",
478 speeds[i].name);
479 }
480 }
481}
482
594a5360 483void usb_check_attach(USBDevice *dev, Error **errp)
a8e662b5
GH
484{
485 USBBus *bus = usb_bus_from_device(dev);
891fb2cd 486 USBPort *port = dev->port;
3b7e759a 487 char devspeed[32], portspeed[32];
a8e662b5 488
891fb2cd
GH
489 assert(port != NULL);
490 assert(!dev->attached);
3b7e759a
GH
491 usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
492 usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
493 trace_usb_port_attach(bus->busnr, port->path,
494 devspeed, portspeed);
891fb2cd
GH
495
496 if (!(port->speedmask & dev->speedmask)) {
7d553f27
GA
497 error_setg(errp, "Warning: speed mismatch trying to attach"
498 " usb device \"%s\" (%s speed)"
499 " to bus \"%s\", port \"%s\" (%s speed)",
500 dev->product_desc, devspeed,
501 bus->qbus.name, port->path, portspeed);
502 return;
a8e662b5 503 }
594a5360
GA
504}
505
506void usb_device_attach(USBDevice *dev, Error **errp)
507{
508 USBPort *port = dev->port;
509 Error *local_err = NULL;
510
511 usb_check_attach(dev, &local_err);
512 if (local_err) {
513 error_propagate(errp, local_err);
514 return;
515 }
a8e662b5 516
891fb2cd
GH
517 dev->attached++;
518 usb_attach(port);
891fb2cd
GH
519}
520
521int usb_device_detach(USBDevice *dev)
522{
523 USBBus *bus = usb_bus_from_device(dev);
524 USBPort *port = dev->port;
a8e662b5 525
891fb2cd
GH
526 assert(port != NULL);
527 assert(dev->attached);
528 trace_usb_port_detach(bus->busnr, port->path);
a8e662b5 529
891fb2cd
GH
530 usb_detach(port);
531 dev->attached--;
a8e662b5
GH
532 return 0;
533}
534
a5d2f727
GH
535int usb_device_delete_addr(int busnr, int addr)
536{
537 USBBus *bus;
538 USBPort *port;
539 USBDevice *dev;
540
541 bus = usb_bus_find(busnr);
542 if (!bus)
543 return -1;
544
72cf2d4f 545 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
546 if (port->dev->addr == addr)
547 break;
548 }
549 if (!port)
550 return -1;
a5d2f727 551 dev = port->dev;
a5d2f727 552
02a5c4c9 553 object_unparent(OBJECT(dev));
a5d2f727
GH
554 return 0;
555}
556
557static const char *usb_speed(unsigned int speed)
558{
559 static const char *txt[] = {
560 [ USB_SPEED_LOW ] = "1.5",
561 [ USB_SPEED_FULL ] = "12",
562 [ USB_SPEED_HIGH ] = "480",
290d26d2 563 [ USB_SPEED_SUPER ] = "5000",
a5d2f727
GH
564 };
565 if (speed >= ARRAY_SIZE(txt))
566 return "?";
567 return txt[speed];
568}
569
570static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
571{
62aed765 572 USBDevice *dev = USB_DEVICE(qdev);
a5d2f727
GH
573 USBBus *bus = usb_bus_from_device(dev);
574
c7a2196a 575 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
66a6593a 576 indent, "", bus->busnr, dev->addr,
c7a2196a 577 dev->port ? dev->port->path : "-",
0fe6d12e 578 usb_speed(dev->speed), dev->product_desc,
66a6593a 579 dev->attached ? ", attached" : "");
a5d2f727
GH
580}
581
c7a2196a
GH
582static char *usb_get_dev_path(DeviceState *qdev)
583{
62aed765 584 USBDevice *dev = USB_DEVICE(qdev);
eeb0cf9a
GH
585 DeviceState *hcd = qdev->parent_bus->parent;
586 char *id = NULL;
587
09e5ab63
AL
588 if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) {
589 id = qdev_get_dev_path(hcd);
eeb0cf9a
GH
590 }
591 if (id) {
592 char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
593 g_free(id);
594 return ret;
595 } else {
596 return g_strdup(dev->port->path);
597 }
c7a2196a
GH
598}
599
70d31cb2
GH
600static char *usb_get_fw_dev_path(DeviceState *qdev)
601{
62aed765 602 USBDevice *dev = USB_DEVICE(qdev);
70d31cb2 603 char *fw_path, *in;
ea87e95f 604 ssize_t pos = 0, fw_len;
70d31cb2
GH
605 long nr;
606
ea87e95f 607 fw_len = 32 + strlen(dev->port->path) * 6;
7267c094 608 fw_path = g_malloc(fw_len);
70d31cb2 609 in = dev->port->path;
ea87e95f 610 while (fw_len - pos > 0) {
70d31cb2
GH
611 nr = strtol(in, &in, 10);
612 if (in[0] == '.') {
613 /* some hub between root port and device */
830cd54f 614 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
70d31cb2
GH
615 in++;
616 } else {
617 /* the device itself */
830cd54f 618 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
ea87e95f 619 qdev_fw_name(qdev), nr);
70d31cb2
GH
620 break;
621 }
622 }
623 return fw_path;
624}
625
84f2d0ea 626void usb_info(Monitor *mon, const QDict *qdict)
a5d2f727
GH
627{
628 USBBus *bus;
629 USBDevice *dev;
630 USBPort *port;
631
72cf2d4f 632 if (QTAILQ_EMPTY(&busses)) {
a5d2f727
GH
633 monitor_printf(mon, "USB support not enabled\n");
634 return;
635 }
636
72cf2d4f
BS
637 QTAILQ_FOREACH(bus, &busses, next) {
638 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
639 dev = port->dev;
640 if (!dev)
641 continue;
c7a2196a
GH
642 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
643 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
0fe6d12e 644 dev->product_desc);
a5d2f727
GH
645 }
646 }
647}
648
0958b4cc
GH
649/* handle legacy -usbdevice cmd line option */
650USBDevice *usbdevice_create(const char *cmdline)
651{
652 USBBus *bus = usb_bus_find(-1 /* any */);
62aed765 653 LegacyUSBFactory *f = NULL;
3bc36a40 654 Error *err = NULL;
62aed765 655 GSList *i;
702f3e0f
JK
656 char driver[32];
657 const char *params;
0958b4cc 658 int len;
3bc36a40 659 USBDevice *dev;
0958b4cc
GH
660
661 params = strchr(cmdline,':');
662 if (params) {
663 params++;
664 len = params - cmdline;
665 if (len > sizeof(driver))
666 len = sizeof(driver);
667 pstrcpy(driver, len, cmdline);
668 } else {
702f3e0f 669 params = "";
0958b4cc
GH
670 pstrcpy(driver, sizeof(driver), cmdline);
671 }
672
62aed765
AL
673 for (i = legacy_usb_factory; i; i = i->next) {
674 f = i->data;
675 if (strcmp(f->usbdevice_name, driver) == 0) {
676 break;
677 }
0958b4cc 678 }
62aed765 679 if (i == NULL) {
0958b4cc
GH
680#if 0
681 /* no error because some drivers are not converted (yet) */
1ecda02b 682 error_report("usbdevice %s not found", driver);
0958b4cc
GH
683#endif
684 return NULL;
685 }
686
c128d6a6
SH
687 if (!bus) {
688 error_report("Error: no usb bus to attach usbdevice %s, "
689 "please try -machine usb=on and check that "
690 "the machine model supports USB", driver);
691 return NULL;
692 }
693
3bc36a40
MA
694 if (f->usbdevice_init) {
695 dev = f->usbdevice_init(bus, params);
696 } else {
98f22dc1 697 if (*params) {
1ecda02b 698 error_report("usbdevice %s accepts no params", driver);
0958b4cc
GH
699 return NULL;
700 }
3bc36a40
MA
701 dev = usb_create(bus, f->name);
702 }
703 if (!dev) {
704 error_report("Failed to create USB device '%s'", f->name);
705 return NULL;
0958b4cc 706 }
3bc36a40
MA
707 object_property_set_bool(OBJECT(dev), true, "realized", &err);
708 if (err) {
709 error_report("Failed to initialize USB device '%s': %s",
710 f->name, error_get_pretty(err));
711 error_free(err);
712 object_unparent(OBJECT(dev));
713 return NULL;
714 }
715 return dev;
0958b4cc 716}
62aed765 717
39bffca2
AL
718static void usb_device_class_init(ObjectClass *klass, void *data)
719{
720 DeviceClass *k = DEVICE_CLASS(klass);
0d936928 721 k->bus_type = TYPE_USB_BUS;
7d553f27
GA
722 k->realize = usb_qdev_realize;
723 k->unrealize = usb_qdev_unrealize;
bce54474 724 k->props = usb_props;
39bffca2
AL
725}
726
8c43a6f0 727static const TypeInfo usb_device_type_info = {
62aed765
AL
728 .name = TYPE_USB_DEVICE,
729 .parent = TYPE_DEVICE,
730 .instance_size = sizeof(USBDevice),
731 .abstract = true,
732 .class_size = sizeof(USBDeviceClass),
39bffca2 733 .class_init = usb_device_class_init,
62aed765
AL
734};
735
83f7d43a 736static void usb_register_types(void)
62aed765 737{
0d936928 738 type_register_static(&usb_bus_info);
62aed765
AL
739 type_register_static(&usb_device_type_info);
740}
741
83f7d43a 742type_init(usb_register_types)