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