Reorganize struct Qcow2Cache for better struct packing
[qemu/agraf.git] / hw / usb-bus.c
blob6e2e5fd17a201477c1adc743aaf39443e83a7b2e
1 #include "hw.h"
2 #include "usb.h"
3 #include "qdev.h"
4 #include "sysemu.h"
5 #include "monitor.h"
7 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
9 static char *usb_get_dev_path(DeviceState *dev);
10 static char *usb_get_fw_dev_path(DeviceState *qdev);
12 static struct BusInfo usb_bus_info = {
13 .name = "USB",
14 .size = sizeof(USBBus),
15 .print_dev = usb_bus_dev_print,
16 .get_dev_path = usb_get_dev_path,
17 .get_fw_dev_path = usb_get_fw_dev_path,
18 .props = (Property[]) {
19 DEFINE_PROP_STRING("port", USBDevice, port_path),
20 DEFINE_PROP_END_OF_LIST()
23 static int next_usb_bus = 0;
24 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
26 void usb_bus_new(USBBus *bus, DeviceState *host)
28 qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
29 bus->busnr = next_usb_bus++;
30 bus->qbus.allow_hotplug = 1; /* Yes, we can */
31 QTAILQ_INIT(&bus->free);
32 QTAILQ_INIT(&bus->used);
33 QTAILQ_INSERT_TAIL(&busses, bus, next);
36 USBBus *usb_bus_find(int busnr)
38 USBBus *bus;
40 if (-1 == busnr)
41 return QTAILQ_FIRST(&busses);
42 QTAILQ_FOREACH(bus, &busses, next) {
43 if (bus->busnr == busnr)
44 return bus;
46 return NULL;
49 static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
51 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
52 USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base);
53 int rc;
55 pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
56 dev->info = info;
57 dev->auto_attach = 1;
58 QLIST_INIT(&dev->strings);
59 rc = dev->info->init(dev);
60 if (rc == 0 && dev->auto_attach)
61 usb_device_attach(dev);
62 return rc;
65 static int usb_qdev_exit(DeviceState *qdev)
67 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
69 usb_device_detach(dev);
70 if (dev->info->handle_destroy) {
71 dev->info->handle_destroy(dev);
73 return 0;
76 void usb_qdev_register(USBDeviceInfo *info)
78 info->qdev.bus_info = &usb_bus_info;
79 info->qdev.init = usb_qdev_init;
80 info->qdev.unplug = qdev_simple_unplug_cb;
81 info->qdev.exit = usb_qdev_exit;
82 qdev_register(&info->qdev);
85 void usb_qdev_register_many(USBDeviceInfo *info)
87 while (info->qdev.name) {
88 usb_qdev_register(info);
89 info++;
93 USBDevice *usb_create(USBBus *bus, const char *name)
95 DeviceState *dev;
97 #if 1
98 /* temporary stopgap until all usb is properly qdev-ified */
99 if (!bus) {
100 bus = usb_bus_find(-1);
101 if (!bus)
102 return NULL;
103 fprintf(stderr, "%s: no bus specified, using \"%s\" for \"%s\"\n",
104 __FUNCTION__, bus->qbus.name, name);
106 #endif
108 dev = qdev_create(&bus->qbus, name);
109 return DO_UPCAST(USBDevice, qdev, dev);
112 USBDevice *usb_create_simple(USBBus *bus, const char *name)
114 USBDevice *dev = usb_create(bus, name);
115 if (!dev) {
116 hw_error("Failed to create USB device '%s'\n", name);
118 qdev_init_nofail(&dev->qdev);
119 return dev;
122 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
123 USBPortOps *ops, int speedmask)
125 port->opaque = opaque;
126 port->index = index;
127 port->opaque = opaque;
128 port->index = index;
129 port->ops = ops;
130 port->speedmask = speedmask;
131 QTAILQ_INSERT_TAIL(&bus->free, port, next);
132 bus->nfree++;
135 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
137 if (upstream) {
138 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
139 upstream->path, portnr);
140 } else {
141 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
145 void usb_unregister_port(USBBus *bus, USBPort *port)
147 if (port->dev)
148 qdev_free(&port->dev->qdev);
149 QTAILQ_REMOVE(&bus->free, port, next);
150 bus->nfree--;
153 static void do_attach(USBDevice *dev)
155 USBBus *bus = usb_bus_from_device(dev);
156 USBPort *port;
158 if (dev->attached) {
159 fprintf(stderr, "Warning: tried to attach usb device %s twice\n",
160 dev->product_desc);
161 return;
163 if (dev->port_path) {
164 QTAILQ_FOREACH(port, &bus->free, next) {
165 if (strcmp(port->path, dev->port_path) == 0) {
166 break;
169 if (port == NULL) {
170 fprintf(stderr, "Warning: usb port %s (bus %s) not found\n",
171 dev->port_path, bus->qbus.name);
172 return;
174 } else {
175 port = QTAILQ_FIRST(&bus->free);
178 dev->attached++;
179 QTAILQ_REMOVE(&bus->free, port, next);
180 bus->nfree--;
182 usb_attach(port, dev);
184 QTAILQ_INSERT_TAIL(&bus->used, port, next);
185 bus->nused++;
188 int usb_device_attach(USBDevice *dev)
190 USBBus *bus = usb_bus_from_device(dev);
192 if (bus->nfree == 1 && dev->port_path == NULL) {
193 /* Create a new hub and chain it on
194 (unless a physical port location is specified). */
195 usb_create_simple(bus, "usb-hub");
197 do_attach(dev);
198 return 0;
201 int usb_device_detach(USBDevice *dev)
203 USBBus *bus = usb_bus_from_device(dev);
204 USBPort *port;
206 if (!dev->attached) {
207 fprintf(stderr, "Warning: tried to detach unattached usb device %s\n",
208 dev->product_desc);
209 return -1;
211 dev->attached--;
213 QTAILQ_FOREACH(port, &bus->used, next) {
214 if (port->dev == dev)
215 break;
217 assert(port != NULL);
219 QTAILQ_REMOVE(&bus->used, port, next);
220 bus->nused--;
222 usb_attach(port, NULL);
224 QTAILQ_INSERT_TAIL(&bus->free, port, next);
225 bus->nfree++;
226 return 0;
229 int usb_device_delete_addr(int busnr, int addr)
231 USBBus *bus;
232 USBPort *port;
233 USBDevice *dev;
235 bus = usb_bus_find(busnr);
236 if (!bus)
237 return -1;
239 QTAILQ_FOREACH(port, &bus->used, next) {
240 if (port->dev->addr == addr)
241 break;
243 if (!port)
244 return -1;
245 dev = port->dev;
247 qdev_free(&dev->qdev);
248 return 0;
251 static const char *usb_speed(unsigned int speed)
253 static const char *txt[] = {
254 [ USB_SPEED_LOW ] = "1.5",
255 [ USB_SPEED_FULL ] = "12",
256 [ USB_SPEED_HIGH ] = "480",
258 if (speed >= ARRAY_SIZE(txt))
259 return "?";
260 return txt[speed];
263 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
265 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
266 USBBus *bus = usb_bus_from_device(dev);
268 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
269 indent, "", bus->busnr, dev->addr,
270 dev->port ? dev->port->path : "-",
271 usb_speed(dev->speed), dev->product_desc,
272 dev->attached ? ", attached" : "");
275 static char *usb_get_dev_path(DeviceState *qdev)
277 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
278 return qemu_strdup(dev->port->path);
281 static char *usb_get_fw_dev_path(DeviceState *qdev)
283 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
284 char *fw_path, *in;
285 int pos = 0;
286 long nr;
288 fw_path = qemu_malloc(32 + strlen(dev->port->path) * 6);
289 in = dev->port->path;
290 while (true) {
291 nr = strtol(in, &in, 10);
292 if (in[0] == '.') {
293 /* some hub between root port and device */
294 pos += sprintf(fw_path + pos, "hub@%ld/", nr);
295 in++;
296 } else {
297 /* the device itself */
298 pos += sprintf(fw_path + pos, "%s@%ld", qdev_fw_name(qdev), nr);
299 break;
302 return fw_path;
305 void usb_info(Monitor *mon)
307 USBBus *bus;
308 USBDevice *dev;
309 USBPort *port;
311 if (QTAILQ_EMPTY(&busses)) {
312 monitor_printf(mon, "USB support not enabled\n");
313 return;
316 QTAILQ_FOREACH(bus, &busses, next) {
317 QTAILQ_FOREACH(port, &bus->used, next) {
318 dev = port->dev;
319 if (!dev)
320 continue;
321 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
322 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
323 dev->product_desc);
328 /* handle legacy -usbdevice cmd line option */
329 USBDevice *usbdevice_create(const char *cmdline)
331 USBBus *bus = usb_bus_find(-1 /* any */);
332 DeviceInfo *info;
333 USBDeviceInfo *usb;
334 char driver[32];
335 const char *params;
336 int len;
338 params = strchr(cmdline,':');
339 if (params) {
340 params++;
341 len = params - cmdline;
342 if (len > sizeof(driver))
343 len = sizeof(driver);
344 pstrcpy(driver, len, cmdline);
345 } else {
346 params = "";
347 pstrcpy(driver, sizeof(driver), cmdline);
350 for (info = device_info_list; info != NULL; info = info->next) {
351 if (info->bus_info != &usb_bus_info)
352 continue;
353 usb = DO_UPCAST(USBDeviceInfo, qdev, info);
354 if (usb->usbdevice_name == NULL)
355 continue;
356 if (strcmp(usb->usbdevice_name, driver) != 0)
357 continue;
358 break;
360 if (info == NULL) {
361 #if 0
362 /* no error because some drivers are not converted (yet) */
363 error_report("usbdevice %s not found", driver);
364 #endif
365 return NULL;
368 if (!usb->usbdevice_init) {
369 if (*params) {
370 error_report("usbdevice %s accepts no params", driver);
371 return NULL;
373 return usb_create_simple(bus, usb->qdev.name);
375 return usb->usbdevice_init(params);