]>
Commit | Line | Data |
---|---|---|
e532b2e0 | 1 | #include "qemu/osdep.h" |
f1ae32a1 GH |
2 | #include "hw/hw.h" |
3 | #include "hw/usb.h" | |
4 | #include "hw/qdev.h" | |
da34e65c | 5 | #include "qapi/error.h" |
d49b6836 | 6 | #include "qemu/error-report.h" |
9c17d615 | 7 | #include "sysemu/sysemu.h" |
83c9089e | 8 | #include "monitor/monitor.h" |
891fb2cd | 9 | #include "trace.h" |
f348b6d1 | 10 | #include "qemu/cutils.h" |
a5d2f727 GH |
11 | |
12 | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); | |
c7a2196a GH |
13 | |
14 | static char *usb_get_dev_path(DeviceState *dev); | |
70d31cb2 | 15 | static char *usb_get_fw_dev_path(DeviceState *qdev); |
7d553f27 | 16 | static void usb_qdev_unrealize(DeviceState *qdev, Error **errp); |
806b6024 | 17 | |
3cb75a7c PB |
18 | static Property usb_props[] = { |
19 | DEFINE_PROP_STRING("port", USBDevice, port_path), | |
71938a09 | 20 | DEFINE_PROP_STRING("serial", USBDevice, serial), |
3cb75a7c PB |
21 | DEFINE_PROP_BIT("full-path", USBDevice, flags, |
22 | USB_DEV_FLAG_FULL_PATH, true), | |
5319dc7b GH |
23 | DEFINE_PROP_BIT("msos-desc", USBDevice, flags, |
24 | USB_DEV_FLAG_MSOS_DESC_ENABLE, true), | |
3cb75a7c PB |
25 | DEFINE_PROP_END_OF_LIST() |
26 | }; | |
27 | ||
0d936928 AL |
28 | static void usb_bus_class_init(ObjectClass *klass, void *data) |
29 | { | |
30 | BusClass *k = BUS_CLASS(klass); | |
5f4d9173 | 31 | HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); |
0d936928 AL |
32 | |
33 | k->print_dev = usb_bus_dev_print; | |
34 | k->get_dev_path = usb_get_dev_path; | |
35 | k->get_fw_dev_path = usb_get_fw_dev_path; | |
5f4d9173 | 36 | hc->unplug = qdev_simple_device_unplug_cb; |
0d936928 AL |
37 | } |
38 | ||
39 | static const TypeInfo usb_bus_info = { | |
40 | .name = TYPE_USB_BUS, | |
41 | .parent = TYPE_BUS, | |
42 | .instance_size = sizeof(USBBus), | |
43 | .class_init = usb_bus_class_init, | |
5f4d9173 IM |
44 | .interfaces = (InterfaceInfo[]) { |
45 | { TYPE_HOTPLUG_HANDLER }, | |
46 | { } | |
47 | } | |
806b6024 | 48 | }; |
3cb75a7c | 49 | |
806b6024 | 50 | static int next_usb_bus = 0; |
72cf2d4f | 51 | static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses); |
806b6024 | 52 | |
495d5447 GH |
53 | static int usb_device_post_load(void *opaque, int version_id) |
54 | { | |
55 | USBDevice *dev = opaque; | |
56 | ||
57 | if (dev->state == USB_STATE_NOTATTACHED) { | |
eb19d2b9 | 58 | dev->attached = false; |
495d5447 | 59 | } else { |
eb19d2b9 | 60 | dev->attached = true; |
495d5447 | 61 | } |
9f8e9895 MT |
62 | if (dev->setup_index < 0 || |
63 | dev->setup_len < 0 || | |
719ffe1f MT |
64 | dev->setup_index > dev->setup_len || |
65 | dev->setup_len > sizeof(dev->data_buf)) { | |
c60174e8 GH |
66 | return -EINVAL; |
67 | } | |
495d5447 GH |
68 | return 0; |
69 | } | |
70 | ||
c1ecb40a GH |
71 | const VMStateDescription vmstate_usb_device = { |
72 | .name = "USBDevice", | |
73 | .version_id = 1, | |
74 | .minimum_version_id = 1, | |
495d5447 | 75 | .post_load = usb_device_post_load, |
6e3d652a | 76 | .fields = (VMStateField[]) { |
c1ecb40a GH |
77 | VMSTATE_UINT8(addr, USBDevice), |
78 | VMSTATE_INT32(state, USBDevice), | |
79 | VMSTATE_INT32(remote_wakeup, USBDevice), | |
80 | VMSTATE_INT32(setup_state, USBDevice), | |
81 | VMSTATE_INT32(setup_len, USBDevice), | |
82 | VMSTATE_INT32(setup_index, USBDevice), | |
83 | VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8), | |
84 | VMSTATE_END_OF_LIST(), | |
85 | } | |
86 | }; | |
87 | ||
c889b3a5 AF |
88 | void usb_bus_new(USBBus *bus, size_t bus_size, |
89 | USBBusOps *ops, DeviceState *host) | |
806b6024 | 90 | { |
fb17dfe0 | 91 | qbus_create_inplace(bus, bus_size, TYPE_USB_BUS, host, NULL); |
5f4d9173 | 92 | qbus_set_bus_hotplug_handler(BUS(bus), &error_abort); |
07771f6f | 93 | bus->ops = ops; |
806b6024 | 94 | bus->busnr = next_usb_bus++; |
72cf2d4f BS |
95 | QTAILQ_INIT(&bus->free); |
96 | QTAILQ_INIT(&bus->used); | |
97 | QTAILQ_INSERT_TAIL(&busses, bus, next); | |
806b6024 GH |
98 | } |
99 | ||
e5a9bece GA |
100 | void usb_bus_release(USBBus *bus) |
101 | { | |
102 | assert(next_usb_bus > 0); | |
103 | ||
104 | QTAILQ_REMOVE(&busses, bus, next); | |
105 | } | |
106 | ||
806b6024 GH |
107 | USBBus *usb_bus_find(int busnr) |
108 | { | |
109 | USBBus *bus; | |
110 | ||
111 | if (-1 == busnr) | |
72cf2d4f BS |
112 | return QTAILQ_FIRST(&busses); |
113 | QTAILQ_FOREACH(bus, &busses, next) { | |
806b6024 GH |
114 | if (bus->busnr == busnr) |
115 | return bus; | |
116 | } | |
117 | return NULL; | |
118 | } | |
119 | ||
7d553f27 | 120 | static void usb_device_realize(USBDevice *dev, Error **errp) |
62aed765 AL |
121 | { |
122 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
7d553f27 GA |
123 | |
124 | if (klass->realize) { | |
125 | klass->realize(dev, errp); | |
62aed765 | 126 | } |
62aed765 AL |
127 | } |
128 | ||
73796fe6 | 129 | USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr) |
62aed765 AL |
130 | { |
131 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
73796fe6 GH |
132 | if (klass->find_device) { |
133 | return klass->find_device(dev, addr); | |
62aed765 | 134 | } |
73796fe6 | 135 | return NULL; |
62aed765 AL |
136 | } |
137 | ||
62aed765 | 138 | static void usb_device_handle_destroy(USBDevice *dev) |
62aed765 AL |
139 | { |
140 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
62aed765 AL |
141 | if (klass->handle_destroy) { |
142 | klass->handle_destroy(dev); | |
62aed765 | 143 | } |
62aed765 AL |
144 | } |
145 | ||
146 | void usb_device_cancel_packet(USBDevice *dev, USBPacket *p) | |
147 | { | |
148 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
149 | if (klass->cancel_packet) { | |
150 | klass->cancel_packet(dev, p); | |
151 | } | |
152 | } | |
153 | ||
154 | void usb_device_handle_attach(USBDevice *dev) | |
155 | { | |
156 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
157 | if (klass->handle_attach) { | |
158 | klass->handle_attach(dev); | |
159 | } | |
160 | } | |
161 | ||
162 | void usb_device_handle_reset(USBDevice *dev) | |
163 | { | |
164 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
165 | if (klass->handle_reset) { | |
166 | klass->handle_reset(dev); | |
167 | } | |
168 | } | |
169 | ||
9a77a0f5 HG |
170 | void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request, |
171 | int value, int index, int length, uint8_t *data) | |
62aed765 AL |
172 | { |
173 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
174 | if (klass->handle_control) { | |
9a77a0f5 | 175 | klass->handle_control(dev, p, request, value, index, length, data); |
62aed765 | 176 | } |
62aed765 AL |
177 | } |
178 | ||
9a77a0f5 | 179 | void usb_device_handle_data(USBDevice *dev, USBPacket *p) |
62aed765 AL |
180 | { |
181 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
182 | if (klass->handle_data) { | |
9a77a0f5 | 183 | klass->handle_data(dev, p); |
62aed765 | 184 | } |
62aed765 AL |
185 | } |
186 | ||
187 | const char *usb_device_get_product_desc(USBDevice *dev) | |
188 | { | |
189 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
190 | return klass->product_desc; | |
191 | } | |
192 | ||
193 | const USBDesc *usb_device_get_usb_desc(USBDevice *dev) | |
194 | { | |
195 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
386ab487 HG |
196 | if (dev->usb_desc) { |
197 | return dev->usb_desc; | |
198 | } | |
62aed765 AL |
199 | return klass->usb_desc; |
200 | } | |
201 | ||
202 | void usb_device_set_interface(USBDevice *dev, int interface, | |
203 | int alt_old, int alt_new) | |
204 | { | |
205 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
206 | if (klass->set_interface) { | |
207 | klass->set_interface(dev, interface, alt_old, alt_new); | |
208 | } | |
209 | } | |
210 | ||
36dfe324 HG |
211 | void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep) |
212 | { | |
213 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
214 | if (klass->flush_ep_queue) { | |
215 | klass->flush_ep_queue(dev, ep); | |
216 | } | |
217 | } | |
218 | ||
f79738b0 HG |
219 | void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep) |
220 | { | |
221 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
222 | if (klass->ep_stopped) { | |
223 | klass->ep_stopped(dev, ep); | |
224 | } | |
225 | } | |
226 | ||
3b444ead HG |
227 | int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps, |
228 | int streams) | |
229 | { | |
230 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
231 | if (klass->alloc_streams) { | |
232 | return klass->alloc_streams(dev, eps, nr_eps, streams); | |
233 | } | |
234 | return 0; | |
235 | } | |
236 | ||
237 | void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps) | |
238 | { | |
239 | USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); | |
240 | if (klass->free_streams) { | |
241 | klass->free_streams(dev, eps, nr_eps); | |
242 | } | |
243 | } | |
244 | ||
7d553f27 | 245 | static void usb_qdev_realize(DeviceState *qdev, Error **errp) |
806b6024 | 246 | { |
62aed765 | 247 | USBDevice *dev = USB_DEVICE(qdev); |
7d553f27 | 248 | Error *local_err = NULL; |
806b6024 | 249 | |
62aed765 AL |
250 | pstrcpy(dev->product_desc, sizeof(dev->product_desc), |
251 | usb_device_get_product_desc(dev)); | |
61e094c0 | 252 | dev->auto_attach = 1; |
132a3f55 | 253 | QLIST_INIT(&dev->strings); |
d8e17efd | 254 | usb_ep_init(dev); |
7d553f27 GA |
255 | |
256 | usb_claim_port(dev, &local_err); | |
257 | if (local_err) { | |
258 | error_propagate(errp, local_err); | |
259 | return; | |
891fb2cd | 260 | } |
7d553f27 GA |
261 | |
262 | usb_device_realize(dev, &local_err); | |
263 | if (local_err) { | |
db3a5ed7 | 264 | usb_release_port(dev); |
7d553f27 GA |
265 | error_propagate(errp, local_err); |
266 | return; | |
f462141f | 267 | } |
7d553f27 | 268 | |
f462141f | 269 | if (dev->auto_attach) { |
7d553f27 GA |
270 | usb_device_attach(dev, &local_err); |
271 | if (local_err) { | |
272 | usb_qdev_unrealize(qdev, NULL); | |
273 | error_propagate(errp, local_err); | |
274 | return; | |
f462141f | 275 | } |
891fb2cd | 276 | } |
806b6024 GH |
277 | } |
278 | ||
7d553f27 | 279 | static void usb_qdev_unrealize(DeviceState *qdev, Error **errp) |
a8e662b5 | 280 | { |
62aed765 | 281 | USBDevice *dev = USB_DEVICE(qdev); |
a8e662b5 | 282 | |
290a5c60 HG |
283 | if (dev->attached) { |
284 | usb_device_detach(dev); | |
285 | } | |
62aed765 | 286 | usb_device_handle_destroy(dev); |
891fb2cd GH |
287 | if (dev->port) { |
288 | usb_release_port(dev); | |
289 | } | |
a8e662b5 GH |
290 | } |
291 | ||
62aed765 | 292 | typedef struct LegacyUSBFactory |
806b6024 | 293 | { |
62aed765 AL |
294 | const char *name; |
295 | const char *usbdevice_name; | |
3741715c | 296 | USBDevice *(*usbdevice_init)(USBBus *bus, const char *params); |
62aed765 | 297 | } LegacyUSBFactory; |
806b6024 | 298 | |
62aed765 AL |
299 | static GSList *legacy_usb_factory; |
300 | ||
ba02430f | 301 | void usb_legacy_register(const char *typename, const char *usbdevice_name, |
3741715c JK |
302 | USBDevice *(*usbdevice_init)(USBBus *bus, |
303 | const char *params)) | |
806b6024 | 304 | { |
62aed765 AL |
305 | if (usbdevice_name) { |
306 | LegacyUSBFactory *f = g_malloc0(sizeof(*f)); | |
ba02430f | 307 | f->name = typename; |
62aed765 AL |
308 | f->usbdevice_name = usbdevice_name; |
309 | f->usbdevice_init = usbdevice_init; | |
310 | legacy_usb_factory = g_slist_append(legacy_usb_factory, f); | |
806b6024 GH |
311 | } |
312 | } | |
313 | ||
a5d2f727 | 314 | USBDevice *usb_create(USBBus *bus, const char *name) |
806b6024 GH |
315 | { |
316 | DeviceState *dev; | |
317 | ||
806b6024 | 318 | dev = qdev_create(&bus->qbus, name); |
62aed765 | 319 | return USB_DEVICE(dev); |
806b6024 | 320 | } |
a5d2f727 | 321 | |
bd8b92d5 MA |
322 | static USBDevice *usb_try_create_simple(USBBus *bus, const char *name, |
323 | Error **errp) | |
a5d2f727 | 324 | { |
bd8b92d5 MA |
325 | Error *err = NULL; |
326 | USBDevice *dev; | |
2af2a1b8 | 327 | |
bd8b92d5 MA |
328 | dev = USB_DEVICE(qdev_try_create(&bus->qbus, name)); |
329 | if (!dev) { | |
330 | error_setg(errp, "Failed to create USB device '%s'", name); | |
331 | return NULL; | |
332 | } | |
333 | object_property_set_bool(OBJECT(dev), true, "realized", &err); | |
334 | if (err) { | |
e43bfd9c MA |
335 | error_propagate(errp, err); |
336 | error_prepend(errp, "Failed to initialize USB device '%s': ", | |
337 | name); | |
bd8b92d5 | 338 | object_unparent(OBJECT(dev)); |
2af2a1b8 | 339 | return NULL; |
d44168ff | 340 | } |
a5d2f727 GH |
341 | return dev; |
342 | } | |
343 | ||
bd8b92d5 MA |
344 | USBDevice *usb_create_simple(USBBus *bus, const char *name) |
345 | { | |
599655c9 | 346 | return usb_try_create_simple(bus, name, &error_abort); |
bd8b92d5 MA |
347 | } |
348 | ||
090ac642 HG |
349 | static void usb_fill_port(USBPort *port, void *opaque, int index, |
350 | USBPortOps *ops, int speedmask) | |
a5d2f727 | 351 | { |
0d86d2be GH |
352 | port->opaque = opaque; |
353 | port->index = index; | |
354 | port->ops = ops; | |
843d4e0c | 355 | port->speedmask = speedmask; |
3631e6c8 | 356 | usb_port_location(port, NULL, index + 1); |
090ac642 HG |
357 | } |
358 | ||
359 | void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index, | |
360 | USBPortOps *ops, int speedmask) | |
361 | { | |
362 | usb_fill_port(port, opaque, index, ops, speedmask); | |
72cf2d4f | 363 | QTAILQ_INSERT_TAIL(&bus->free, port, next); |
a5d2f727 GH |
364 | bus->nfree++; |
365 | } | |
366 | ||
f4bbaaf5 MA |
367 | void usb_register_companion(const char *masterbus, USBPort *ports[], |
368 | uint32_t portcount, uint32_t firstport, | |
369 | void *opaque, USBPortOps *ops, int speedmask, | |
370 | Error **errp) | |
ae60fea9 HG |
371 | { |
372 | USBBus *bus; | |
373 | int i; | |
374 | ||
375 | QTAILQ_FOREACH(bus, &busses, next) { | |
376 | if (strcmp(bus->qbus.name, masterbus) == 0) { | |
377 | break; | |
378 | } | |
379 | } | |
380 | ||
2e269f3d MA |
381 | if (!bus) { |
382 | error_setg(errp, "USB bus '%s' not found", masterbus); | |
383 | return; | |
384 | } | |
385 | if (!bus->ops->register_companion) { | |
386 | error_setg(errp, "Can't use USB bus '%s' as masterbus," | |
387 | " it doesn't support companion controllers", | |
388 | masterbus); | |
f4bbaaf5 | 389 | return; |
ae60fea9 HG |
390 | } |
391 | ||
392 | for (i = 0; i < portcount; i++) { | |
393 | usb_fill_port(ports[i], opaque, i, ops, speedmask); | |
394 | } | |
395 | ||
f4bbaaf5 | 396 | bus->ops->register_companion(bus, ports, portcount, firstport, errp); |
ae60fea9 HG |
397 | } |
398 | ||
c7a2196a GH |
399 | void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr) |
400 | { | |
401 | if (upstream) { | |
402 | snprintf(downstream->path, sizeof(downstream->path), "%s.%d", | |
403 | upstream->path, portnr); | |
c24e4aac | 404 | downstream->hubcount = upstream->hubcount + 1; |
c7a2196a GH |
405 | } else { |
406 | snprintf(downstream->path, sizeof(downstream->path), "%d", portnr); | |
c24e4aac | 407 | downstream->hubcount = 0; |
c7a2196a GH |
408 | } |
409 | } | |
410 | ||
a8e662b5 GH |
411 | void usb_unregister_port(USBBus *bus, USBPort *port) |
412 | { | |
02a5c4c9 SH |
413 | if (port->dev) { |
414 | object_unparent(OBJECT(port->dev)); | |
415 | } | |
a8e662b5 GH |
416 | QTAILQ_REMOVE(&bus->free, port, next); |
417 | bus->nfree--; | |
418 | } | |
419 | ||
7d553f27 | 420 | void usb_claim_port(USBDevice *dev, Error **errp) |
a5d2f727 GH |
421 | { |
422 | USBBus *bus = usb_bus_from_device(dev); | |
423 | USBPort *port; | |
424 | ||
891fb2cd GH |
425 | assert(dev->port == NULL); |
426 | ||
5f69076b GH |
427 | if (dev->port_path) { |
428 | QTAILQ_FOREACH(port, &bus->free, next) { | |
429 | if (strcmp(port->path, dev->port_path) == 0) { | |
430 | break; | |
431 | } | |
432 | } | |
433 | if (port == NULL) { | |
06f22eb7 | 434 | error_setg(errp, "usb port %s (bus %s) not found (in use?)", |
7d553f27 GA |
435 | dev->port_path, bus->qbus.name); |
436 | return; | |
5f69076b GH |
437 | } |
438 | } else { | |
f79f2bfc | 439 | if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) { |
891fb2cd | 440 | /* Create a new hub and chain it on */ |
bd8b92d5 | 441 | usb_try_create_simple(bus, "usb-hub", NULL); |
891fb2cd GH |
442 | } |
443 | if (bus->nfree == 0) { | |
06f22eb7 | 444 | error_setg(errp, "tried to attach usb device %s to a bus " |
7d553f27 GA |
445 | "with no free ports", dev->product_desc); |
446 | return; | |
891fb2cd | 447 | } |
5f69076b GH |
448 | port = QTAILQ_FIRST(&bus->free); |
449 | } | |
891fb2cd | 450 | trace_usb_port_claim(bus->busnr, port->path); |
a5d2f727 | 451 | |
72cf2d4f | 452 | QTAILQ_REMOVE(&bus->free, port, next); |
a5d2f727 GH |
453 | bus->nfree--; |
454 | ||
891fb2cd GH |
455 | dev->port = port; |
456 | port->dev = dev; | |
a5d2f727 | 457 | |
72cf2d4f | 458 | QTAILQ_INSERT_TAIL(&bus->used, port, next); |
a5d2f727 GH |
459 | bus->nused++; |
460 | } | |
461 | ||
891fb2cd | 462 | void usb_release_port(USBDevice *dev) |
a5d2f727 GH |
463 | { |
464 | USBBus *bus = usb_bus_from_device(dev); | |
891fb2cd | 465 | USBPort *port = dev->port; |
a5d2f727 | 466 | |
891fb2cd GH |
467 | assert(port != NULL); |
468 | trace_usb_port_release(bus->busnr, port->path); | |
469 | ||
470 | QTAILQ_REMOVE(&bus->used, port, next); | |
471 | bus->nused--; | |
472 | ||
473 | dev->port = NULL; | |
474 | port->dev = NULL; | |
475 | ||
476 | QTAILQ_INSERT_TAIL(&bus->free, port, next); | |
477 | bus->nfree++; | |
a5d2f727 GH |
478 | } |
479 | ||
3b7e759a GH |
480 | static void usb_mask_to_str(char *dest, size_t size, |
481 | unsigned int speedmask) | |
482 | { | |
483 | static const struct { | |
484 | unsigned int mask; | |
485 | const char *name; | |
486 | } speeds[] = { | |
487 | { .mask = USB_SPEED_MASK_FULL, .name = "full" }, | |
488 | { .mask = USB_SPEED_MASK_HIGH, .name = "high" }, | |
489 | { .mask = USB_SPEED_MASK_SUPER, .name = "super" }, | |
490 | }; | |
491 | int i, pos = 0; | |
492 | ||
493 | for (i = 0; i < ARRAY_SIZE(speeds); i++) { | |
494 | if (speeds[i].mask & speedmask) { | |
495 | pos += snprintf(dest + pos, size - pos, "%s%s", | |
496 | pos ? "+" : "", | |
497 | speeds[i].name); | |
498 | } | |
499 | } | |
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 | int usb_device_delete_addr(int busnr, int addr) |
555 | { | |
556 | USBBus *bus; | |
557 | USBPort *port; | |
558 | USBDevice *dev; | |
559 | ||
560 | bus = usb_bus_find(busnr); | |
561 | if (!bus) | |
562 | return -1; | |
563 | ||
72cf2d4f | 564 | QTAILQ_FOREACH(port, &bus->used, next) { |
a5d2f727 GH |
565 | if (port->dev->addr == addr) |
566 | break; | |
567 | } | |
568 | if (!port) | |
569 | return -1; | |
a5d2f727 | 570 | dev = port->dev; |
a5d2f727 | 571 | |
02a5c4c9 | 572 | object_unparent(OBJECT(dev)); |
a5d2f727 GH |
573 | return 0; |
574 | } | |
575 | ||
576 | static const char *usb_speed(unsigned int speed) | |
577 | { | |
578 | static const char *txt[] = { | |
579 | [ USB_SPEED_LOW ] = "1.5", | |
580 | [ USB_SPEED_FULL ] = "12", | |
581 | [ USB_SPEED_HIGH ] = "480", | |
290d26d2 | 582 | [ USB_SPEED_SUPER ] = "5000", |
a5d2f727 GH |
583 | }; |
584 | if (speed >= ARRAY_SIZE(txt)) | |
585 | return "?"; | |
586 | return txt[speed]; | |
587 | } | |
588 | ||
589 | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) | |
590 | { | |
62aed765 | 591 | USBDevice *dev = USB_DEVICE(qdev); |
a5d2f727 GH |
592 | USBBus *bus = usb_bus_from_device(dev); |
593 | ||
c7a2196a | 594 | monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n", |
66a6593a | 595 | indent, "", bus->busnr, dev->addr, |
c7a2196a | 596 | dev->port ? dev->port->path : "-", |
0fe6d12e | 597 | usb_speed(dev->speed), dev->product_desc, |
66a6593a | 598 | dev->attached ? ", attached" : ""); |
a5d2f727 GH |
599 | } |
600 | ||
c7a2196a GH |
601 | static char *usb_get_dev_path(DeviceState *qdev) |
602 | { | |
62aed765 | 603 | USBDevice *dev = USB_DEVICE(qdev); |
eeb0cf9a GH |
604 | DeviceState *hcd = qdev->parent_bus->parent; |
605 | char *id = NULL; | |
606 | ||
09e5ab63 AL |
607 | if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) { |
608 | id = qdev_get_dev_path(hcd); | |
eeb0cf9a GH |
609 | } |
610 | if (id) { | |
611 | char *ret = g_strdup_printf("%s/%s", id, dev->port->path); | |
612 | g_free(id); | |
613 | return ret; | |
614 | } else { | |
615 | return g_strdup(dev->port->path); | |
616 | } | |
c7a2196a GH |
617 | } |
618 | ||
70d31cb2 GH |
619 | static char *usb_get_fw_dev_path(DeviceState *qdev) |
620 | { | |
62aed765 | 621 | USBDevice *dev = USB_DEVICE(qdev); |
70d31cb2 | 622 | char *fw_path, *in; |
ea87e95f | 623 | ssize_t pos = 0, fw_len; |
70d31cb2 GH |
624 | long nr; |
625 | ||
ea87e95f | 626 | fw_len = 32 + strlen(dev->port->path) * 6; |
7267c094 | 627 | fw_path = g_malloc(fw_len); |
70d31cb2 | 628 | in = dev->port->path; |
ea87e95f | 629 | while (fw_len - pos > 0) { |
70d31cb2 GH |
630 | nr = strtol(in, &in, 10); |
631 | if (in[0] == '.') { | |
632 | /* some hub between root port and device */ | |
830cd54f | 633 | pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr); |
70d31cb2 GH |
634 | in++; |
635 | } else { | |
636 | /* the device itself */ | |
830cd54f | 637 | pos += snprintf(fw_path + pos, fw_len - pos, "%s@%lx", |
ea87e95f | 638 | qdev_fw_name(qdev), nr); |
70d31cb2 GH |
639 | break; |
640 | } | |
641 | } | |
642 | return fw_path; | |
643 | } | |
644 | ||
1ce6be24 | 645 | void hmp_info_usb(Monitor *mon, const QDict *qdict) |
a5d2f727 GH |
646 | { |
647 | USBBus *bus; | |
648 | USBDevice *dev; | |
649 | USBPort *port; | |
650 | ||
72cf2d4f | 651 | if (QTAILQ_EMPTY(&busses)) { |
a5d2f727 GH |
652 | monitor_printf(mon, "USB support not enabled\n"); |
653 | return; | |
654 | } | |
655 | ||
72cf2d4f BS |
656 | QTAILQ_FOREACH(bus, &busses, next) { |
657 | QTAILQ_FOREACH(port, &bus->used, next) { | |
a5d2f727 GH |
658 | dev = port->dev; |
659 | if (!dev) | |
660 | continue; | |
974826f0 GH |
661 | monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, " |
662 | "Product %s%s%s\n", | |
663 | bus->busnr, dev->addr, port->path, | |
664 | usb_speed(dev->speed), dev->product_desc, | |
665 | dev->qdev.id ? ", ID: " : "", | |
666 | dev->qdev.id ?: ""); | |
a5d2f727 GH |
667 | } |
668 | } | |
669 | } | |
670 | ||
0958b4cc GH |
671 | /* handle legacy -usbdevice cmd line option */ |
672 | USBDevice *usbdevice_create(const char *cmdline) | |
673 | { | |
674 | USBBus *bus = usb_bus_find(-1 /* any */); | |
62aed765 | 675 | LegacyUSBFactory *f = NULL; |
3bc36a40 | 676 | Error *err = NULL; |
62aed765 | 677 | GSList *i; |
702f3e0f JK |
678 | char driver[32]; |
679 | const char *params; | |
0958b4cc | 680 | int len; |
3bc36a40 | 681 | USBDevice *dev; |
0958b4cc GH |
682 | |
683 | params = strchr(cmdline,':'); | |
684 | if (params) { | |
685 | params++; | |
686 | len = params - cmdline; | |
687 | if (len > sizeof(driver)) | |
688 | len = sizeof(driver); | |
689 | pstrcpy(driver, len, cmdline); | |
690 | } else { | |
702f3e0f | 691 | params = ""; |
0958b4cc GH |
692 | pstrcpy(driver, sizeof(driver), cmdline); |
693 | } | |
694 | ||
62aed765 AL |
695 | for (i = legacy_usb_factory; i; i = i->next) { |
696 | f = i->data; | |
697 | if (strcmp(f->usbdevice_name, driver) == 0) { | |
698 | break; | |
699 | } | |
0958b4cc | 700 | } |
62aed765 | 701 | if (i == NULL) { |
0958b4cc GH |
702 | #if 0 |
703 | /* no error because some drivers are not converted (yet) */ | |
1ecda02b | 704 | error_report("usbdevice %s not found", driver); |
0958b4cc GH |
705 | #endif |
706 | return NULL; | |
707 | } | |
708 | ||
c128d6a6 SH |
709 | if (!bus) { |
710 | error_report("Error: no usb bus to attach usbdevice %s, " | |
711 | "please try -machine usb=on and check that " | |
712 | "the machine model supports USB", driver); | |
713 | return NULL; | |
714 | } | |
715 | ||
3bc36a40 MA |
716 | if (f->usbdevice_init) { |
717 | dev = f->usbdevice_init(bus, params); | |
718 | } else { | |
98f22dc1 | 719 | if (*params) { |
1ecda02b | 720 | error_report("usbdevice %s accepts no params", driver); |
0958b4cc GH |
721 | return NULL; |
722 | } | |
3bc36a40 MA |
723 | dev = usb_create(bus, f->name); |
724 | } | |
725 | if (!dev) { | |
726 | error_report("Failed to create USB device '%s'", f->name); | |
727 | return NULL; | |
0958b4cc | 728 | } |
3bc36a40 MA |
729 | object_property_set_bool(OBJECT(dev), true, "realized", &err); |
730 | if (err) { | |
c29b77f9 MA |
731 | error_reportf_err(err, "Failed to initialize USB device '%s': ", |
732 | f->name); | |
3bc36a40 MA |
733 | object_unparent(OBJECT(dev)); |
734 | return NULL; | |
735 | } | |
736 | return dev; | |
0958b4cc | 737 | } |
62aed765 | 738 | |
39bffca2 AL |
739 | static void usb_device_class_init(ObjectClass *klass, void *data) |
740 | { | |
741 | DeviceClass *k = DEVICE_CLASS(klass); | |
0d936928 | 742 | k->bus_type = TYPE_USB_BUS; |
7d553f27 GA |
743 | k->realize = usb_qdev_realize; |
744 | k->unrealize = usb_qdev_unrealize; | |
bce54474 | 745 | k->props = usb_props; |
39bffca2 AL |
746 | } |
747 | ||
8c43a6f0 | 748 | static const TypeInfo usb_device_type_info = { |
62aed765 AL |
749 | .name = TYPE_USB_DEVICE, |
750 | .parent = TYPE_DEVICE, | |
751 | .instance_size = sizeof(USBDevice), | |
752 | .abstract = true, | |
753 | .class_size = sizeof(USBDeviceClass), | |
39bffca2 | 754 | .class_init = usb_device_class_init, |
62aed765 AL |
755 | }; |
756 | ||
83f7d43a | 757 | static void usb_register_types(void) |
62aed765 | 758 | { |
0d936928 | 759 | type_register_static(&usb_bus_info); |
62aed765 AL |
760 | type_register_static(&usb_device_type_info); |
761 | } | |
762 | ||
83f7d43a | 763 | type_init(usb_register_types) |