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