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