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