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