Bug Summary

File:hw/usb/bus.c
Location:line 521, column 13
Description:Value stored to 'pos' is never read

Annotated Source Code

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