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