]>
Commit | Line | Data |
---|---|---|
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 | |
13 | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); | |
c7a2196a GH |
14 | |
15 | static char *usb_get_dev_path(DeviceState *dev); | |
70d31cb2 | 16 | static char *usb_get_fw_dev_path(DeviceState *qdev); |
b69c3c21 | 17 | static void usb_qdev_unrealize(DeviceState *qdev); |
806b6024 | 18 | |
3cb75a7c PB |
19 | static 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 |
29 | static 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 | ||
40 | static 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 | 51 | static int next_usb_bus = 0; |
72cf2d4f | 52 | static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses); |
806b6024 | 53 | |
495d5447 GH |
54 | static 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 |
66 | const 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 |
83 | void 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); |
cd7c8660 | 87 | qbus_set_bus_hotplug_handler(BUS(bus)); |
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 |
95 | void usb_bus_release(USBBus *bus) |
96 | { | |
97 | assert(next_usb_bus > 0); | |
98 | ||
99 | QTAILQ_REMOVE(&busses, bus, next); | |
100 | } | |
101 | ||
806b6024 GH |
102 | USBBus *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 | 115 | static 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 | 124 | USBDevice *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 | ||
b69c3c21 | 133 | static void usb_device_unrealize(USBDevice *dev) |
62aed765 AL |
134 | { |
135 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
c4fe9700 MAL |
136 | |
137 | if (klass->unrealize) { | |
b69c3c21 | 138 | klass->unrealize(dev); |
62aed765 | 139 | } |
62aed765 AL |
140 | } |
141 | ||
142 | void 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 | ||
150 | void 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 | ||
158 | void 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 |
166 | void 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 | 175 | void 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 | ||
183 | const char *usb_device_get_product_desc(USBDevice *dev) | |
184 | { | |
185 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
186 | return klass->product_desc; | |
187 | } | |
188 | ||
189 | const 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 | ||
198 | void 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 |
207 | void 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 |
215 | void 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 |
223 | int 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 | ||
233 | void 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 | 241 | static 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) { | |
b69c3c21 | 268 | usb_qdev_unrealize(qdev); |
7d553f27 GA |
269 | error_propagate(errp, local_err); |
270 | return; | |
f462141f | 271 | } |
891fb2cd | 272 | } |
806b6024 GH |
273 | } |
274 | ||
b69c3c21 | 275 | static void usb_qdev_unrealize(DeviceState *qdev) |
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 | } | |
b69c3c21 | 289 | usb_device_unrealize(dev); |
891fb2cd GH |
290 | if (dev->port) { |
291 | usb_release_port(dev); | |
292 | } | |
a8e662b5 GH |
293 | } |
294 | ||
62aed765 | 295 | typedef struct LegacyUSBFactory |
806b6024 | 296 | { |
62aed765 AL |
297 | const char *name; |
298 | const char *usbdevice_name; | |
590ce74a | 299 | USBDevice *(*usbdevice_init)(const char *params); |
62aed765 | 300 | } LegacyUSBFactory; |
806b6024 | 301 | |
62aed765 AL |
302 | static GSList *legacy_usb_factory; |
303 | ||
ba02430f | 304 | void usb_legacy_register(const char *typename, const char *usbdevice_name, |
590ce74a | 305 | USBDevice *(*usbdevice_init)(const char *params)) |
806b6024 | 306 | { |
62aed765 AL |
307 | if (usbdevice_name) { |
308 | LegacyUSBFactory *f = g_malloc0(sizeof(*f)); | |
ba02430f | 309 | f->name = typename; |
62aed765 AL |
310 | f->usbdevice_name = usbdevice_name; |
311 | f->usbdevice_init = usbdevice_init; | |
312 | legacy_usb_factory = g_slist_append(legacy_usb_factory, f); | |
806b6024 GH |
313 | } |
314 | } | |
315 | ||
32aaaebe MA |
316 | USBDevice *usb_new(const char *name) |
317 | { | |
318 | return USB_DEVICE(qdev_new(name)); | |
319 | } | |
320 | ||
53577051 | 321 | static USBDevice *usb_try_new(const char *name) |
32aaaebe | 322 | { |
53577051 | 323 | return USB_DEVICE(qdev_try_new(name)); |
32aaaebe MA |
324 | } |
325 | ||
53577051 | 326 | bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Error **errp) |
a5d2f727 | 327 | { |
53577051 | 328 | return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp); |
a5d2f727 GH |
329 | } |
330 | ||
bd8b92d5 MA |
331 | USBDevice *usb_create_simple(USBBus *bus, const char *name) |
332 | { | |
53577051 MA |
333 | USBDevice *dev = usb_new(name); |
334 | ||
335 | usb_realize_and_unref(dev, bus, &error_abort); | |
336 | return dev; | |
bd8b92d5 MA |
337 | } |
338 | ||
090ac642 HG |
339 | static void usb_fill_port(USBPort *port, void *opaque, int index, |
340 | USBPortOps *ops, int speedmask) | |
a5d2f727 | 341 | { |
0d86d2be GH |
342 | port->opaque = opaque; |
343 | port->index = index; | |
344 | port->ops = ops; | |
843d4e0c | 345 | port->speedmask = speedmask; |
3631e6c8 | 346 | usb_port_location(port, NULL, index + 1); |
090ac642 HG |
347 | } |
348 | ||
349 | void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index, | |
350 | USBPortOps *ops, int speedmask) | |
351 | { | |
352 | usb_fill_port(port, opaque, index, ops, speedmask); | |
72cf2d4f | 353 | QTAILQ_INSERT_TAIL(&bus->free, port, next); |
a5d2f727 GH |
354 | bus->nfree++; |
355 | } | |
356 | ||
f4bbaaf5 MA |
357 | void usb_register_companion(const char *masterbus, USBPort *ports[], |
358 | uint32_t portcount, uint32_t firstport, | |
359 | void *opaque, USBPortOps *ops, int speedmask, | |
360 | Error **errp) | |
ae60fea9 HG |
361 | { |
362 | USBBus *bus; | |
363 | int i; | |
364 | ||
365 | QTAILQ_FOREACH(bus, &busses, next) { | |
366 | if (strcmp(bus->qbus.name, masterbus) == 0) { | |
367 | break; | |
368 | } | |
369 | } | |
370 | ||
2e269f3d MA |
371 | if (!bus) { |
372 | error_setg(errp, "USB bus '%s' not found", masterbus); | |
373 | return; | |
374 | } | |
375 | if (!bus->ops->register_companion) { | |
376 | error_setg(errp, "Can't use USB bus '%s' as masterbus," | |
377 | " it doesn't support companion controllers", | |
378 | masterbus); | |
f4bbaaf5 | 379 | return; |
ae60fea9 HG |
380 | } |
381 | ||
382 | for (i = 0; i < portcount; i++) { | |
383 | usb_fill_port(ports[i], opaque, i, ops, speedmask); | |
384 | } | |
385 | ||
f4bbaaf5 | 386 | bus->ops->register_companion(bus, ports, portcount, firstport, errp); |
ae60fea9 HG |
387 | } |
388 | ||
c7a2196a GH |
389 | void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr) |
390 | { | |
391 | if (upstream) { | |
121829cb EB |
392 | int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d", |
393 | upstream->path, portnr); | |
394 | /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */ | |
395 | assert(l < sizeof(downstream->path)); | |
c24e4aac | 396 | downstream->hubcount = upstream->hubcount + 1; |
c7a2196a GH |
397 | } else { |
398 | snprintf(downstream->path, sizeof(downstream->path), "%d", portnr); | |
c24e4aac | 399 | downstream->hubcount = 0; |
c7a2196a GH |
400 | } |
401 | } | |
402 | ||
a8e662b5 GH |
403 | void usb_unregister_port(USBBus *bus, USBPort *port) |
404 | { | |
02a5c4c9 SH |
405 | if (port->dev) { |
406 | object_unparent(OBJECT(port->dev)); | |
407 | } | |
a8e662b5 GH |
408 | QTAILQ_REMOVE(&bus->free, port, next); |
409 | bus->nfree--; | |
410 | } | |
411 | ||
7d553f27 | 412 | void usb_claim_port(USBDevice *dev, Error **errp) |
a5d2f727 GH |
413 | { |
414 | USBBus *bus = usb_bus_from_device(dev); | |
415 | USBPort *port; | |
53577051 | 416 | USBDevice *hub; |
a5d2f727 | 417 | |
891fb2cd GH |
418 | assert(dev->port == NULL); |
419 | ||
5f69076b GH |
420 | if (dev->port_path) { |
421 | QTAILQ_FOREACH(port, &bus->free, next) { | |
422 | if (strcmp(port->path, dev->port_path) == 0) { | |
423 | break; | |
424 | } | |
425 | } | |
426 | if (port == NULL) { | |
06f22eb7 | 427 | error_setg(errp, "usb port %s (bus %s) not found (in use?)", |
7d553f27 GA |
428 | dev->port_path, bus->qbus.name); |
429 | return; | |
5f69076b GH |
430 | } |
431 | } else { | |
f79f2bfc | 432 | if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) { |
891fb2cd | 433 | /* Create a new hub and chain it on */ |
53577051 MA |
434 | hub = usb_try_new("usb-hub"); |
435 | if (hub) { | |
436 | usb_realize_and_unref(hub, bus, NULL); | |
437 | } | |
891fb2cd GH |
438 | } |
439 | if (bus->nfree == 0) { | |
06f22eb7 | 440 | error_setg(errp, "tried to attach usb device %s to a bus " |
7d553f27 GA |
441 | "with no free ports", dev->product_desc); |
442 | return; | |
891fb2cd | 443 | } |
5f69076b GH |
444 | port = QTAILQ_FIRST(&bus->free); |
445 | } | |
891fb2cd | 446 | trace_usb_port_claim(bus->busnr, port->path); |
a5d2f727 | 447 | |
72cf2d4f | 448 | QTAILQ_REMOVE(&bus->free, port, next); |
a5d2f727 GH |
449 | bus->nfree--; |
450 | ||
891fb2cd GH |
451 | dev->port = port; |
452 | port->dev = dev; | |
a5d2f727 | 453 | |
72cf2d4f | 454 | QTAILQ_INSERT_TAIL(&bus->used, port, next); |
a5d2f727 GH |
455 | bus->nused++; |
456 | } | |
457 | ||
891fb2cd | 458 | void usb_release_port(USBDevice *dev) |
a5d2f727 GH |
459 | { |
460 | USBBus *bus = usb_bus_from_device(dev); | |
891fb2cd | 461 | USBPort *port = dev->port; |
a5d2f727 | 462 | |
891fb2cd GH |
463 | assert(port != NULL); |
464 | trace_usb_port_release(bus->busnr, port->path); | |
465 | ||
466 | QTAILQ_REMOVE(&bus->used, port, next); | |
467 | bus->nused--; | |
468 | ||
469 | dev->port = NULL; | |
470 | port->dev = NULL; | |
471 | ||
472 | QTAILQ_INSERT_TAIL(&bus->free, port, next); | |
473 | bus->nfree++; | |
a5d2f727 GH |
474 | } |
475 | ||
3b7e759a GH |
476 | static void usb_mask_to_str(char *dest, size_t size, |
477 | unsigned int speedmask) | |
478 | { | |
479 | static const struct { | |
480 | unsigned int mask; | |
481 | const char *name; | |
482 | } speeds[] = { | |
483 | { .mask = USB_SPEED_MASK_FULL, .name = "full" }, | |
484 | { .mask = USB_SPEED_MASK_HIGH, .name = "high" }, | |
485 | { .mask = USB_SPEED_MASK_SUPER, .name = "super" }, | |
486 | }; | |
487 | int i, pos = 0; | |
488 | ||
489 | for (i = 0; i < ARRAY_SIZE(speeds); i++) { | |
490 | if (speeds[i].mask & speedmask) { | |
491 | pos += snprintf(dest + pos, size - pos, "%s%s", | |
492 | pos ? "+" : "", | |
493 | speeds[i].name); | |
494 | } | |
495 | } | |
5189e30b PM |
496 | |
497 | if (pos == 0) { | |
498 | snprintf(dest, size, "unknown"); | |
499 | } | |
3b7e759a GH |
500 | } |
501 | ||
594a5360 | 502 | void usb_check_attach(USBDevice *dev, Error **errp) |
a8e662b5 GH |
503 | { |
504 | USBBus *bus = usb_bus_from_device(dev); | |
891fb2cd | 505 | USBPort *port = dev->port; |
3b7e759a | 506 | char devspeed[32], portspeed[32]; |
a8e662b5 | 507 | |
891fb2cd GH |
508 | assert(port != NULL); |
509 | assert(!dev->attached); | |
3b7e759a GH |
510 | usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask); |
511 | usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask); | |
512 | trace_usb_port_attach(bus->busnr, port->path, | |
513 | devspeed, portspeed); | |
891fb2cd GH |
514 | |
515 | if (!(port->speedmask & dev->speedmask)) { | |
7d553f27 GA |
516 | error_setg(errp, "Warning: speed mismatch trying to attach" |
517 | " usb device \"%s\" (%s speed)" | |
518 | " to bus \"%s\", port \"%s\" (%s speed)", | |
519 | dev->product_desc, devspeed, | |
520 | bus->qbus.name, port->path, portspeed); | |
521 | return; | |
a8e662b5 | 522 | } |
594a5360 GA |
523 | } |
524 | ||
525 | void usb_device_attach(USBDevice *dev, Error **errp) | |
526 | { | |
527 | USBPort *port = dev->port; | |
528 | Error *local_err = NULL; | |
529 | ||
530 | usb_check_attach(dev, &local_err); | |
531 | if (local_err) { | |
532 | error_propagate(errp, local_err); | |
533 | return; | |
534 | } | |
a8e662b5 | 535 | |
eb19d2b9 | 536 | dev->attached = true; |
891fb2cd | 537 | usb_attach(port); |
891fb2cd GH |
538 | } |
539 | ||
540 | int usb_device_detach(USBDevice *dev) | |
541 | { | |
542 | USBBus *bus = usb_bus_from_device(dev); | |
543 | USBPort *port = dev->port; | |
a8e662b5 | 544 | |
891fb2cd GH |
545 | assert(port != NULL); |
546 | assert(dev->attached); | |
547 | trace_usb_port_detach(bus->busnr, port->path); | |
a8e662b5 | 548 | |
891fb2cd | 549 | usb_detach(port); |
eb19d2b9 | 550 | dev->attached = false; |
a8e662b5 GH |
551 | return 0; |
552 | } | |
553 | ||
a5d2f727 GH |
554 | static const char *usb_speed(unsigned int speed) |
555 | { | |
556 | static const char *txt[] = { | |
557 | [ USB_SPEED_LOW ] = "1.5", | |
558 | [ USB_SPEED_FULL ] = "12", | |
559 | [ USB_SPEED_HIGH ] = "480", | |
290d26d2 | 560 | [ USB_SPEED_SUPER ] = "5000", |
a5d2f727 GH |
561 | }; |
562 | if (speed >= ARRAY_SIZE(txt)) | |
563 | return "?"; | |
564 | return txt[speed]; | |
565 | } | |
566 | ||
567 | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) | |
568 | { | |
62aed765 | 569 | USBDevice *dev = USB_DEVICE(qdev); |
a5d2f727 GH |
570 | USBBus *bus = usb_bus_from_device(dev); |
571 | ||
c7a2196a | 572 | monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n", |
66a6593a | 573 | indent, "", bus->busnr, dev->addr, |
c7a2196a | 574 | dev->port ? dev->port->path : "-", |
0fe6d12e | 575 | usb_speed(dev->speed), dev->product_desc, |
66a6593a | 576 | dev->attached ? ", attached" : ""); |
a5d2f727 GH |
577 | } |
578 | ||
c7a2196a GH |
579 | static char *usb_get_dev_path(DeviceState *qdev) |
580 | { | |
62aed765 | 581 | USBDevice *dev = USB_DEVICE(qdev); |
eeb0cf9a GH |
582 | DeviceState *hcd = qdev->parent_bus->parent; |
583 | char *id = NULL; | |
584 | ||
09e5ab63 AL |
585 | if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) { |
586 | id = qdev_get_dev_path(hcd); | |
eeb0cf9a GH |
587 | } |
588 | if (id) { | |
589 | char *ret = g_strdup_printf("%s/%s", id, dev->port->path); | |
590 | g_free(id); | |
591 | return ret; | |
592 | } else { | |
593 | return g_strdup(dev->port->path); | |
594 | } | |
c7a2196a GH |
595 | } |
596 | ||
70d31cb2 GH |
597 | static char *usb_get_fw_dev_path(DeviceState *qdev) |
598 | { | |
62aed765 | 599 | USBDevice *dev = USB_DEVICE(qdev); |
70d31cb2 | 600 | char *fw_path, *in; |
ea87e95f | 601 | ssize_t pos = 0, fw_len; |
70d31cb2 GH |
602 | long nr; |
603 | ||
ea87e95f | 604 | fw_len = 32 + strlen(dev->port->path) * 6; |
7267c094 | 605 | fw_path = g_malloc(fw_len); |
70d31cb2 | 606 | in = dev->port->path; |
ea87e95f | 607 | while (fw_len - pos > 0) { |
70d31cb2 GH |
608 | nr = strtol(in, &in, 10); |
609 | if (in[0] == '.') { | |
610 | /* some hub between root port and device */ | |
830cd54f | 611 | pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr); |
70d31cb2 GH |
612 | in++; |
613 | } else { | |
614 | /* the device itself */ | |
830cd54f | 615 | pos += snprintf(fw_path + pos, fw_len - pos, "%s@%lx", |
ea87e95f | 616 | qdev_fw_name(qdev), nr); |
70d31cb2 GH |
617 | break; |
618 | } | |
619 | } | |
620 | return fw_path; | |
621 | } | |
622 | ||
1ce6be24 | 623 | void hmp_info_usb(Monitor *mon, const QDict *qdict) |
a5d2f727 GH |
624 | { |
625 | USBBus *bus; | |
626 | USBDevice *dev; | |
627 | USBPort *port; | |
628 | ||
72cf2d4f | 629 | if (QTAILQ_EMPTY(&busses)) { |
a5d2f727 GH |
630 | monitor_printf(mon, "USB support not enabled\n"); |
631 | return; | |
632 | } | |
633 | ||
72cf2d4f BS |
634 | QTAILQ_FOREACH(bus, &busses, next) { |
635 | QTAILQ_FOREACH(port, &bus->used, next) { | |
a5d2f727 GH |
636 | dev = port->dev; |
637 | if (!dev) | |
638 | continue; | |
974826f0 GH |
639 | monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, " |
640 | "Product %s%s%s\n", | |
641 | bus->busnr, dev->addr, port->path, | |
642 | usb_speed(dev->speed), dev->product_desc, | |
643 | dev->qdev.id ? ", ID: " : "", | |
644 | dev->qdev.id ?: ""); | |
a5d2f727 GH |
645 | } |
646 | } | |
647 | } | |
648 | ||
0958b4cc GH |
649 | /* handle legacy -usbdevice cmd line option */ |
650 | USBDevice *usbdevice_create(const char *cmdline) | |
651 | { | |
652 | USBBus *bus = usb_bus_find(-1 /* any */); | |
62aed765 | 653 | LegacyUSBFactory *f = NULL; |
3bc36a40 | 654 | Error *err = NULL; |
62aed765 | 655 | GSList *i; |
702f3e0f JK |
656 | char driver[32]; |
657 | const char *params; | |
0958b4cc | 658 | int len; |
3bc36a40 | 659 | USBDevice *dev; |
0958b4cc GH |
660 | |
661 | params = strchr(cmdline,':'); | |
662 | if (params) { | |
663 | params++; | |
664 | len = params - cmdline; | |
665 | if (len > sizeof(driver)) | |
666 | len = sizeof(driver); | |
667 | pstrcpy(driver, len, cmdline); | |
668 | } else { | |
702f3e0f | 669 | params = ""; |
0958b4cc GH |
670 | pstrcpy(driver, sizeof(driver), cmdline); |
671 | } | |
672 | ||
62aed765 AL |
673 | for (i = legacy_usb_factory; i; i = i->next) { |
674 | f = i->data; | |
675 | if (strcmp(f->usbdevice_name, driver) == 0) { | |
676 | break; | |
677 | } | |
0958b4cc | 678 | } |
62aed765 | 679 | if (i == NULL) { |
0958b4cc GH |
680 | #if 0 |
681 | /* no error because some drivers are not converted (yet) */ | |
1ecda02b | 682 | error_report("usbdevice %s not found", driver); |
0958b4cc GH |
683 | #endif |
684 | return NULL; | |
685 | } | |
686 | ||
c128d6a6 SH |
687 | if (!bus) { |
688 | error_report("Error: no usb bus to attach usbdevice %s, " | |
689 | "please try -machine usb=on and check that " | |
690 | "the machine model supports USB", driver); | |
691 | return NULL; | |
692 | } | |
693 | ||
3bc36a40 | 694 | if (f->usbdevice_init) { |
590ce74a | 695 | dev = f->usbdevice_init(params); |
3bc36a40 | 696 | } else { |
98f22dc1 | 697 | if (*params) { |
1ecda02b | 698 | error_report("usbdevice %s accepts no params", driver); |
0958b4cc GH |
699 | return NULL; |
700 | } | |
590ce74a | 701 | dev = usb_new(f->name); |
3bc36a40 MA |
702 | } |
703 | if (!dev) { | |
704 | error_report("Failed to create USB device '%s'", f->name); | |
705 | return NULL; | |
0958b4cc | 706 | } |
118bfd76 | 707 | if (!usb_realize_and_unref(dev, bus, &err)) { |
c29b77f9 MA |
708 | error_reportf_err(err, "Failed to initialize USB device '%s': ", |
709 | f->name); | |
3bc36a40 MA |
710 | object_unparent(OBJECT(dev)); |
711 | return NULL; | |
712 | } | |
713 | return dev; | |
0958b4cc | 714 | } |
62aed765 | 715 | |
1e351dc3 GH |
716 | static bool usb_get_attached(Object *obj, Error **errp) |
717 | { | |
718 | USBDevice *dev = USB_DEVICE(obj); | |
719 | ||
720 | return dev->attached; | |
721 | } | |
722 | ||
723 | static void usb_set_attached(Object *obj, bool value, Error **errp) | |
724 | { | |
725 | USBDevice *dev = USB_DEVICE(obj); | |
1e351dc3 GH |
726 | |
727 | if (dev->attached == value) { | |
728 | return; | |
729 | } | |
730 | ||
731 | if (value) { | |
992861fb | 732 | usb_device_attach(dev, errp); |
1e351dc3 GH |
733 | } else { |
734 | usb_device_detach(dev); | |
735 | } | |
736 | } | |
737 | ||
738 | static void usb_device_instance_init(Object *obj) | |
739 | { | |
740 | USBDevice *dev = USB_DEVICE(obj); | |
741 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
742 | ||
743 | if (klass->attached_settable) { | |
744 | object_property_add_bool(obj, "attached", | |
d2623129 | 745 | usb_get_attached, usb_set_attached); |
1e351dc3 GH |
746 | } else { |
747 | object_property_add_bool(obj, "attached", | |
d2623129 | 748 | usb_get_attached, NULL); |
1e351dc3 GH |
749 | } |
750 | } | |
751 | ||
39bffca2 AL |
752 | static void usb_device_class_init(ObjectClass *klass, void *data) |
753 | { | |
754 | DeviceClass *k = DEVICE_CLASS(klass); | |
0d936928 | 755 | k->bus_type = TYPE_USB_BUS; |
7d553f27 GA |
756 | k->realize = usb_qdev_realize; |
757 | k->unrealize = usb_qdev_unrealize; | |
4f67d30b | 758 | device_class_set_props(k, usb_props); |
39bffca2 AL |
759 | } |
760 | ||
8c43a6f0 | 761 | static const TypeInfo usb_device_type_info = { |
62aed765 AL |
762 | .name = TYPE_USB_DEVICE, |
763 | .parent = TYPE_DEVICE, | |
764 | .instance_size = sizeof(USBDevice), | |
1e351dc3 | 765 | .instance_init = usb_device_instance_init, |
62aed765 AL |
766 | .abstract = true, |
767 | .class_size = sizeof(USBDeviceClass), | |
39bffca2 | 768 | .class_init = usb_device_class_init, |
62aed765 AL |
769 | }; |
770 | ||
83f7d43a | 771 | static void usb_register_types(void) |
62aed765 | 772 | { |
0d936928 | 773 | type_register_static(&usb_bus_info); |
62aed765 AL |
774 | type_register_static(&usb_device_type_info); |
775 | } | |
776 | ||
83f7d43a | 777 | type_init(usb_register_types) |