]> Git Repo - qemu.git/blame - hw/usb/bus.c
tcg: add "-accel tcg,tb-size" and deprecate "-tb-size"
[qemu.git] / hw / usb / bus.c
CommitLineData
e532b2e0 1#include "qemu/osdep.h"
a27bd6c7 2#include "hw/qdev-properties.h"
f1ae32a1 3#include "hw/usb.h"
da34e65c 4#include "qapi/error.h"
d49b6836 5#include "qemu/error-report.h"
0b8fa32f 6#include "qemu/module.h"
9c17d615 7#include "sysemu/sysemu.h"
d6454270 8#include "migration/vmstate.h"
83c9089e 9#include "monitor/monitor.h"
891fb2cd 10#include "trace.h"
f348b6d1 11#include "qemu/cutils.h"
a5d2f727
GH
12
13static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
c7a2196a
GH
14
15static char *usb_get_dev_path(DeviceState *dev);
70d31cb2 16static char *usb_get_fw_dev_path(DeviceState *qdev);
7d553f27 17static void usb_qdev_unrealize(DeviceState *qdev, Error **errp);
806b6024 18
3cb75a7c
PB
19static Property usb_props[] = {
20 DEFINE_PROP_STRING("port", USBDevice, port_path),
71938a09 21 DEFINE_PROP_STRING("serial", USBDevice, serial),
3cb75a7c
PB
22 DEFINE_PROP_BIT("full-path", USBDevice, flags,
23 USB_DEV_FLAG_FULL_PATH, true),
5319dc7b
GH
24 DEFINE_PROP_BIT("msos-desc", USBDevice, flags,
25 USB_DEV_FLAG_MSOS_DESC_ENABLE, true),
3cb75a7c
PB
26 DEFINE_PROP_END_OF_LIST()
27};
28
0d936928
AL
29static void usb_bus_class_init(ObjectClass *klass, void *data)
30{
31 BusClass *k = BUS_CLASS(klass);
5f4d9173 32 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
0d936928
AL
33
34 k->print_dev = usb_bus_dev_print;
35 k->get_dev_path = usb_get_dev_path;
36 k->get_fw_dev_path = usb_get_fw_dev_path;
5f4d9173 37 hc->unplug = qdev_simple_device_unplug_cb;
0d936928
AL
38}
39
40static const TypeInfo usb_bus_info = {
41 .name = TYPE_USB_BUS,
42 .parent = TYPE_BUS,
43 .instance_size = sizeof(USBBus),
44 .class_init = usb_bus_class_init,
5f4d9173
IM
45 .interfaces = (InterfaceInfo[]) {
46 { TYPE_HOTPLUG_HANDLER },
47 { }
48 }
806b6024 49};
3cb75a7c 50
806b6024 51static int next_usb_bus = 0;
72cf2d4f 52static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
806b6024 53
495d5447
GH
54static int usb_device_post_load(void *opaque, int version_id)
55{
56 USBDevice *dev = opaque;
57
58 if (dev->state == USB_STATE_NOTATTACHED) {
eb19d2b9 59 dev->attached = false;
495d5447 60 } else {
eb19d2b9 61 dev->attached = true;
495d5447
GH
62 }
63 return 0;
64}
65
c1ecb40a
GH
66const VMStateDescription vmstate_usb_device = {
67 .name = "USBDevice",
68 .version_id = 1,
69 .minimum_version_id = 1,
495d5447 70 .post_load = usb_device_post_load,
6e3d652a 71 .fields = (VMStateField[]) {
c1ecb40a
GH
72 VMSTATE_UINT8(addr, USBDevice),
73 VMSTATE_INT32(state, USBDevice),
74 VMSTATE_INT32(remote_wakeup, USBDevice),
75 VMSTATE_INT32(setup_state, USBDevice),
76 VMSTATE_INT32(setup_len, USBDevice),
77 VMSTATE_INT32(setup_index, USBDevice),
78 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
79 VMSTATE_END_OF_LIST(),
80 }
81};
82
c889b3a5
AF
83void usb_bus_new(USBBus *bus, size_t bus_size,
84 USBBusOps *ops, DeviceState *host)
806b6024 85{
fb17dfe0 86 qbus_create_inplace(bus, bus_size, TYPE_USB_BUS, host, NULL);
5f4d9173 87 qbus_set_bus_hotplug_handler(BUS(bus), &error_abort);
07771f6f 88 bus->ops = ops;
806b6024 89 bus->busnr = next_usb_bus++;
72cf2d4f
BS
90 QTAILQ_INIT(&bus->free);
91 QTAILQ_INIT(&bus->used);
92 QTAILQ_INSERT_TAIL(&busses, bus, next);
806b6024
GH
93}
94
e5a9bece
GA
95void usb_bus_release(USBBus *bus)
96{
97 assert(next_usb_bus > 0);
98
99 QTAILQ_REMOVE(&busses, bus, next);
100}
101
806b6024
GH
102USBBus *usb_bus_find(int busnr)
103{
104 USBBus *bus;
105
106 if (-1 == busnr)
72cf2d4f
BS
107 return QTAILQ_FIRST(&busses);
108 QTAILQ_FOREACH(bus, &busses, next) {
806b6024
GH
109 if (bus->busnr == busnr)
110 return bus;
111 }
112 return NULL;
113}
114
7d553f27 115static void usb_device_realize(USBDevice *dev, Error **errp)
62aed765
AL
116{
117 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
7d553f27
GA
118
119 if (klass->realize) {
120 klass->realize(dev, errp);
62aed765 121 }
62aed765
AL
122}
123
73796fe6 124USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
62aed765
AL
125{
126 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
73796fe6
GH
127 if (klass->find_device) {
128 return klass->find_device(dev, addr);
62aed765 129 }
73796fe6 130 return NULL;
62aed765
AL
131}
132
c4fe9700 133static void usb_device_unrealize(USBDevice *dev, Error **errp)
62aed765
AL
134{
135 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
c4fe9700
MAL
136
137 if (klass->unrealize) {
138 klass->unrealize(dev, errp);
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);
ec507f11
MAL
278 USBDescString *s, *next;
279
280 QLIST_FOREACH_SAFE(s, &dev->strings, next, next) {
281 QLIST_REMOVE(s, next);
282 g_free(s->str);
283 g_free(s);
284 }
a8e662b5 285
290a5c60
HG
286 if (dev->attached) {
287 usb_device_detach(dev);
288 }
c4fe9700 289 usb_device_unrealize(dev, errp);
891fb2cd
GH
290 if (dev->port) {
291 usb_release_port(dev);
292 }
a8e662b5
GH
293}
294
62aed765 295typedef struct LegacyUSBFactory
806b6024 296{
62aed765
AL
297 const char *name;
298 const char *usbdevice_name;
3741715c 299 USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
62aed765 300} LegacyUSBFactory;
806b6024 301
62aed765
AL
302static GSList *legacy_usb_factory;
303
ba02430f 304void usb_legacy_register(const char *typename, const char *usbdevice_name,
3741715c
JK
305 USBDevice *(*usbdevice_init)(USBBus *bus,
306 const char *params))
806b6024 307{
62aed765
AL
308 if (usbdevice_name) {
309 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
ba02430f 310 f->name = typename;
62aed765
AL
311 f->usbdevice_name = usbdevice_name;
312 f->usbdevice_init = usbdevice_init;
313 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
806b6024
GH
314 }
315}
316
a5d2f727 317USBDevice *usb_create(USBBus *bus, const char *name)
806b6024
GH
318{
319 DeviceState *dev;
320
806b6024 321 dev = qdev_create(&bus->qbus, name);
62aed765 322 return USB_DEVICE(dev);
806b6024 323}
a5d2f727 324
bd8b92d5
MA
325static USBDevice *usb_try_create_simple(USBBus *bus, const char *name,
326 Error **errp)
a5d2f727 327{
bd8b92d5
MA
328 Error *err = NULL;
329 USBDevice *dev;
2af2a1b8 330
bd8b92d5
MA
331 dev = USB_DEVICE(qdev_try_create(&bus->qbus, name));
332 if (!dev) {
333 error_setg(errp, "Failed to create USB device '%s'", name);
334 return NULL;
335 }
336 object_property_set_bool(OBJECT(dev), true, "realized", &err);
337 if (err) {
4b576648
MA
338 error_propagate_prepend(errp, err,
339 "Failed to initialize USB device '%s': ",
340 name);
2af2a1b8 341 return NULL;
d44168ff 342 }
a5d2f727
GH
343 return dev;
344}
345
bd8b92d5
MA
346USBDevice *usb_create_simple(USBBus *bus, const char *name)
347{
599655c9 348 return usb_try_create_simple(bus, name, &error_abort);
bd8b92d5
MA
349}
350
090ac642
HG
351static void usb_fill_port(USBPort *port, void *opaque, int index,
352 USBPortOps *ops, int speedmask)
a5d2f727 353{
0d86d2be
GH
354 port->opaque = opaque;
355 port->index = index;
356 port->ops = ops;
843d4e0c 357 port->speedmask = speedmask;
3631e6c8 358 usb_port_location(port, NULL, index + 1);
090ac642
HG
359}
360
361void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
362 USBPortOps *ops, int speedmask)
363{
364 usb_fill_port(port, opaque, index, ops, speedmask);
72cf2d4f 365 QTAILQ_INSERT_TAIL(&bus->free, port, next);
a5d2f727
GH
366 bus->nfree++;
367}
368
f4bbaaf5
MA
369void usb_register_companion(const char *masterbus, USBPort *ports[],
370 uint32_t portcount, uint32_t firstport,
371 void *opaque, USBPortOps *ops, int speedmask,
372 Error **errp)
ae60fea9
HG
373{
374 USBBus *bus;
375 int i;
376
377 QTAILQ_FOREACH(bus, &busses, next) {
378 if (strcmp(bus->qbus.name, masterbus) == 0) {
379 break;
380 }
381 }
382
2e269f3d
MA
383 if (!bus) {
384 error_setg(errp, "USB bus '%s' not found", masterbus);
385 return;
386 }
387 if (!bus->ops->register_companion) {
388 error_setg(errp, "Can't use USB bus '%s' as masterbus,"
389 " it doesn't support companion controllers",
390 masterbus);
f4bbaaf5 391 return;
ae60fea9
HG
392 }
393
394 for (i = 0; i < portcount; i++) {
395 usb_fill_port(ports[i], opaque, i, ops, speedmask);
396 }
397
f4bbaaf5 398 bus->ops->register_companion(bus, ports, portcount, firstport, errp);
ae60fea9
HG
399}
400
c7a2196a
GH
401void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
402{
403 if (upstream) {
121829cb
EB
404 int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
405 upstream->path, portnr);
406 /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
407 assert(l < sizeof(downstream->path));
c24e4aac 408 downstream->hubcount = upstream->hubcount + 1;
c7a2196a
GH
409 } else {
410 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
c24e4aac 411 downstream->hubcount = 0;
c7a2196a
GH
412 }
413}
414
a8e662b5
GH
415void usb_unregister_port(USBBus *bus, USBPort *port)
416{
02a5c4c9
SH
417 if (port->dev) {
418 object_unparent(OBJECT(port->dev));
419 }
a8e662b5
GH
420 QTAILQ_REMOVE(&bus->free, port, next);
421 bus->nfree--;
422}
423
7d553f27 424void usb_claim_port(USBDevice *dev, Error **errp)
a5d2f727
GH
425{
426 USBBus *bus = usb_bus_from_device(dev);
427 USBPort *port;
428
891fb2cd
GH
429 assert(dev->port == NULL);
430
5f69076b
GH
431 if (dev->port_path) {
432 QTAILQ_FOREACH(port, &bus->free, next) {
433 if (strcmp(port->path, dev->port_path) == 0) {
434 break;
435 }
436 }
437 if (port == NULL) {
06f22eb7 438 error_setg(errp, "usb port %s (bus %s) not found (in use?)",
7d553f27
GA
439 dev->port_path, bus->qbus.name);
440 return;
5f69076b
GH
441 }
442 } else {
f79f2bfc 443 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
891fb2cd 444 /* Create a new hub and chain it on */
bd8b92d5 445 usb_try_create_simple(bus, "usb-hub", NULL);
891fb2cd
GH
446 }
447 if (bus->nfree == 0) {
06f22eb7 448 error_setg(errp, "tried to attach usb device %s to a bus "
7d553f27
GA
449 "with no free ports", dev->product_desc);
450 return;
891fb2cd 451 }
5f69076b
GH
452 port = QTAILQ_FIRST(&bus->free);
453 }
891fb2cd 454 trace_usb_port_claim(bus->busnr, port->path);
a5d2f727 455
72cf2d4f 456 QTAILQ_REMOVE(&bus->free, port, next);
a5d2f727
GH
457 bus->nfree--;
458
891fb2cd
GH
459 dev->port = port;
460 port->dev = dev;
a5d2f727 461
72cf2d4f 462 QTAILQ_INSERT_TAIL(&bus->used, port, next);
a5d2f727
GH
463 bus->nused++;
464}
465
891fb2cd 466void usb_release_port(USBDevice *dev)
a5d2f727
GH
467{
468 USBBus *bus = usb_bus_from_device(dev);
891fb2cd 469 USBPort *port = dev->port;
a5d2f727 470
891fb2cd
GH
471 assert(port != NULL);
472 trace_usb_port_release(bus->busnr, port->path);
473
474 QTAILQ_REMOVE(&bus->used, port, next);
475 bus->nused--;
476
477 dev->port = NULL;
478 port->dev = NULL;
479
480 QTAILQ_INSERT_TAIL(&bus->free, port, next);
481 bus->nfree++;
a5d2f727
GH
482}
483
3b7e759a
GH
484static void usb_mask_to_str(char *dest, size_t size,
485 unsigned int speedmask)
486{
487 static const struct {
488 unsigned int mask;
489 const char *name;
490 } speeds[] = {
491 { .mask = USB_SPEED_MASK_FULL, .name = "full" },
492 { .mask = USB_SPEED_MASK_HIGH, .name = "high" },
493 { .mask = USB_SPEED_MASK_SUPER, .name = "super" },
494 };
495 int i, pos = 0;
496
497 for (i = 0; i < ARRAY_SIZE(speeds); i++) {
498 if (speeds[i].mask & speedmask) {
499 pos += snprintf(dest + pos, size - pos, "%s%s",
500 pos ? "+" : "",
501 speeds[i].name);
502 }
503 }
5189e30b
PM
504
505 if (pos == 0) {
506 snprintf(dest, size, "unknown");
507 }
3b7e759a
GH
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
562static const char *usb_speed(unsigned int speed)
563{
564 static const char *txt[] = {
565 [ USB_SPEED_LOW ] = "1.5",
566 [ USB_SPEED_FULL ] = "12",
567 [ USB_SPEED_HIGH ] = "480",
290d26d2 568 [ USB_SPEED_SUPER ] = "5000",
a5d2f727
GH
569 };
570 if (speed >= ARRAY_SIZE(txt))
571 return "?";
572 return txt[speed];
573}
574
575static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
576{
62aed765 577 USBDevice *dev = USB_DEVICE(qdev);
a5d2f727
GH
578 USBBus *bus = usb_bus_from_device(dev);
579
c7a2196a 580 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
66a6593a 581 indent, "", bus->busnr, dev->addr,
c7a2196a 582 dev->port ? dev->port->path : "-",
0fe6d12e 583 usb_speed(dev->speed), dev->product_desc,
66a6593a 584 dev->attached ? ", attached" : "");
a5d2f727
GH
585}
586
c7a2196a
GH
587static char *usb_get_dev_path(DeviceState *qdev)
588{
62aed765 589 USBDevice *dev = USB_DEVICE(qdev);
eeb0cf9a
GH
590 DeviceState *hcd = qdev->parent_bus->parent;
591 char *id = NULL;
592
09e5ab63
AL
593 if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) {
594 id = qdev_get_dev_path(hcd);
eeb0cf9a
GH
595 }
596 if (id) {
597 char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
598 g_free(id);
599 return ret;
600 } else {
601 return g_strdup(dev->port->path);
602 }
c7a2196a
GH
603}
604
70d31cb2
GH
605static char *usb_get_fw_dev_path(DeviceState *qdev)
606{
62aed765 607 USBDevice *dev = USB_DEVICE(qdev);
70d31cb2 608 char *fw_path, *in;
ea87e95f 609 ssize_t pos = 0, fw_len;
70d31cb2
GH
610 long nr;
611
ea87e95f 612 fw_len = 32 + strlen(dev->port->path) * 6;
7267c094 613 fw_path = g_malloc(fw_len);
70d31cb2 614 in = dev->port->path;
ea87e95f 615 while (fw_len - pos > 0) {
70d31cb2
GH
616 nr = strtol(in, &in, 10);
617 if (in[0] == '.') {
618 /* some hub between root port and device */
830cd54f 619 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
70d31cb2
GH
620 in++;
621 } else {
622 /* the device itself */
830cd54f 623 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
ea87e95f 624 qdev_fw_name(qdev), nr);
70d31cb2
GH
625 break;
626 }
627 }
628 return fw_path;
629}
630
1ce6be24 631void hmp_info_usb(Monitor *mon, const QDict *qdict)
a5d2f727
GH
632{
633 USBBus *bus;
634 USBDevice *dev;
635 USBPort *port;
636
72cf2d4f 637 if (QTAILQ_EMPTY(&busses)) {
a5d2f727
GH
638 monitor_printf(mon, "USB support not enabled\n");
639 return;
640 }
641
72cf2d4f
BS
642 QTAILQ_FOREACH(bus, &busses, next) {
643 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
644 dev = port->dev;
645 if (!dev)
646 continue;
974826f0
GH
647 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, "
648 "Product %s%s%s\n",
649 bus->busnr, dev->addr, port->path,
650 usb_speed(dev->speed), dev->product_desc,
651 dev->qdev.id ? ", ID: " : "",
652 dev->qdev.id ?: "");
a5d2f727
GH
653 }
654 }
655}
656
0958b4cc
GH
657/* handle legacy -usbdevice cmd line option */
658USBDevice *usbdevice_create(const char *cmdline)
659{
660 USBBus *bus = usb_bus_find(-1 /* any */);
62aed765 661 LegacyUSBFactory *f = NULL;
3bc36a40 662 Error *err = NULL;
62aed765 663 GSList *i;
702f3e0f
JK
664 char driver[32];
665 const char *params;
0958b4cc 666 int len;
3bc36a40 667 USBDevice *dev;
0958b4cc
GH
668
669 params = strchr(cmdline,':');
670 if (params) {
671 params++;
672 len = params - cmdline;
673 if (len > sizeof(driver))
674 len = sizeof(driver);
675 pstrcpy(driver, len, cmdline);
676 } else {
702f3e0f 677 params = "";
0958b4cc
GH
678 pstrcpy(driver, sizeof(driver), cmdline);
679 }
680
62aed765
AL
681 for (i = legacy_usb_factory; i; i = i->next) {
682 f = i->data;
683 if (strcmp(f->usbdevice_name, driver) == 0) {
684 break;
685 }
0958b4cc 686 }
62aed765 687 if (i == NULL) {
0958b4cc
GH
688#if 0
689 /* no error because some drivers are not converted (yet) */
1ecda02b 690 error_report("usbdevice %s not found", driver);
0958b4cc
GH
691#endif
692 return NULL;
693 }
694
c128d6a6
SH
695 if (!bus) {
696 error_report("Error: no usb bus to attach usbdevice %s, "
697 "please try -machine usb=on and check that "
698 "the machine model supports USB", driver);
699 return NULL;
700 }
701
3bc36a40
MA
702 if (f->usbdevice_init) {
703 dev = f->usbdevice_init(bus, params);
704 } else {
98f22dc1 705 if (*params) {
1ecda02b 706 error_report("usbdevice %s accepts no params", driver);
0958b4cc
GH
707 return NULL;
708 }
3bc36a40
MA
709 dev = usb_create(bus, f->name);
710 }
711 if (!dev) {
712 error_report("Failed to create USB device '%s'", f->name);
713 return NULL;
0958b4cc 714 }
3bc36a40
MA
715 object_property_set_bool(OBJECT(dev), true, "realized", &err);
716 if (err) {
c29b77f9
MA
717 error_reportf_err(err, "Failed to initialize USB device '%s': ",
718 f->name);
3bc36a40
MA
719 object_unparent(OBJECT(dev));
720 return NULL;
721 }
722 return dev;
0958b4cc 723}
62aed765 724
1e351dc3
GH
725static bool usb_get_attached(Object *obj, Error **errp)
726{
727 USBDevice *dev = USB_DEVICE(obj);
728
729 return dev->attached;
730}
731
732static void usb_set_attached(Object *obj, bool value, Error **errp)
733{
734 USBDevice *dev = USB_DEVICE(obj);
735 Error *err = NULL;
736
737 if (dev->attached == value) {
738 return;
739 }
740
741 if (value) {
742 usb_device_attach(dev, &err);
021c9d25 743 error_propagate(errp, err);
1e351dc3
GH
744 } else {
745 usb_device_detach(dev);
746 }
747}
748
749static void usb_device_instance_init(Object *obj)
750{
751 USBDevice *dev = USB_DEVICE(obj);
752 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
753
754 if (klass->attached_settable) {
755 object_property_add_bool(obj, "attached",
756 usb_get_attached, usb_set_attached,
757 NULL);
758 } else {
759 object_property_add_bool(obj, "attached",
760 usb_get_attached, NULL,
761 NULL);
762 }
763}
764
39bffca2
AL
765static void usb_device_class_init(ObjectClass *klass, void *data)
766{
767 DeviceClass *k = DEVICE_CLASS(klass);
0d936928 768 k->bus_type = TYPE_USB_BUS;
7d553f27
GA
769 k->realize = usb_qdev_realize;
770 k->unrealize = usb_qdev_unrealize;
bce54474 771 k->props = usb_props;
39bffca2
AL
772}
773
8c43a6f0 774static const TypeInfo usb_device_type_info = {
62aed765
AL
775 .name = TYPE_USB_DEVICE,
776 .parent = TYPE_DEVICE,
777 .instance_size = sizeof(USBDevice),
1e351dc3 778 .instance_init = usb_device_instance_init,
62aed765
AL
779 .abstract = true,
780 .class_size = sizeof(USBDeviceClass),
39bffca2 781 .class_init = usb_device_class_init,
62aed765
AL
782};
783
83f7d43a 784static void usb_register_types(void)
62aed765 785{
0d936928 786 type_register_static(&usb_bus_info);
62aed765
AL
787 type_register_static(&usb_device_type_info);
788}
789
83f7d43a 790type_init(usb_register_types)
This page took 0.983194 seconds and 4 git commands to generate.