virtagent: Makefile fixups
[qemu/mdroth.git] / hw / usb-bus.c
blobb692503f545e3cf2b9cae74a1735543d0eac0e96
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 struct BusInfo usb_bus_info = {
10 .name = "USB",
11 .size = sizeof(USBBus),
12 .print_dev = usb_bus_dev_print,
14 static int next_usb_bus = 0;
15 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
17 void usb_bus_new(USBBus *bus, DeviceState *host)
19 qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
20 bus->busnr = next_usb_bus++;
21 bus->qbus.allow_hotplug = 1; /* Yes, we can */
22 QTAILQ_INIT(&bus->free);
23 QTAILQ_INIT(&bus->used);
24 QTAILQ_INSERT_TAIL(&busses, bus, next);
27 USBBus *usb_bus_find(int busnr)
29 USBBus *bus;
31 if (-1 == busnr)
32 return QTAILQ_FIRST(&busses);
33 QTAILQ_FOREACH(bus, &busses, next) {
34 if (bus->busnr == busnr)
35 return bus;
37 return NULL;
40 static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
42 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
43 USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base);
44 int rc;
46 pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
47 dev->info = info;
48 dev->auto_attach = 1;
49 rc = dev->info->init(dev);
50 if (rc == 0 && dev->auto_attach)
51 usb_device_attach(dev);
52 return rc;
55 static int usb_qdev_exit(DeviceState *qdev)
57 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
59 usb_device_detach(dev);
60 if (dev->info->handle_destroy) {
61 dev->info->handle_destroy(dev);
63 return 0;
66 void usb_qdev_register(USBDeviceInfo *info)
68 info->qdev.bus_info = &usb_bus_info;
69 info->qdev.init = usb_qdev_init;
70 info->qdev.unplug = qdev_simple_unplug_cb;
71 info->qdev.exit = usb_qdev_exit;
72 qdev_register(&info->qdev);
75 void usb_qdev_register_many(USBDeviceInfo *info)
77 while (info->qdev.name) {
78 usb_qdev_register(info);
79 info++;
83 USBDevice *usb_create(USBBus *bus, const char *name)
85 DeviceState *dev;
87 #if 1
88 /* temporary stopgap until all usb is properly qdev-ified */
89 if (!bus) {
90 bus = usb_bus_find(-1);
91 if (!bus)
92 return NULL;
93 fprintf(stderr, "%s: no bus specified, using \"%s\" for \"%s\"\n",
94 __FUNCTION__, bus->qbus.name, name);
96 #endif
98 dev = qdev_create(&bus->qbus, name);
99 return DO_UPCAST(USBDevice, qdev, dev);
102 USBDevice *usb_create_simple(USBBus *bus, const char *name)
104 USBDevice *dev = usb_create(bus, name);
105 if (!dev) {
106 hw_error("Failed to create USB device '%s'\n", name);
108 qdev_init_nofail(&dev->qdev);
109 return dev;
112 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
113 usb_attachfn attach)
115 port->opaque = opaque;
116 port->index = index;
117 port->attach = attach;
118 QTAILQ_INSERT_TAIL(&bus->free, port, next);
119 bus->nfree++;
122 void usb_unregister_port(USBBus *bus, USBPort *port)
124 if (port->dev)
125 qdev_free(&port->dev->qdev);
126 QTAILQ_REMOVE(&bus->free, port, next);
127 bus->nfree--;
130 static void do_attach(USBDevice *dev)
132 USBBus *bus = usb_bus_from_device(dev);
133 USBPort *port;
135 if (dev->attached) {
136 fprintf(stderr, "Warning: tried to attach usb device %s twice\n",
137 dev->product_desc);
138 return;
140 dev->attached++;
142 port = QTAILQ_FIRST(&bus->free);
143 QTAILQ_REMOVE(&bus->free, port, next);
144 bus->nfree--;
146 usb_attach(port, dev);
148 QTAILQ_INSERT_TAIL(&bus->used, port, next);
149 bus->nused++;
152 int usb_device_attach(USBDevice *dev)
154 USBBus *bus = usb_bus_from_device(dev);
156 if (bus->nfree == 1) {
157 /* Create a new hub and chain it on. */
158 usb_create_simple(bus, "usb-hub");
160 do_attach(dev);
161 return 0;
164 int usb_device_detach(USBDevice *dev)
166 USBBus *bus = usb_bus_from_device(dev);
167 USBPort *port;
169 if (!dev->attached) {
170 fprintf(stderr, "Warning: tried to detach unattached usb device %s\n",
171 dev->product_desc);
172 return -1;
174 dev->attached--;
176 QTAILQ_FOREACH(port, &bus->used, next) {
177 if (port->dev == dev)
178 break;
180 assert(port != NULL);
182 QTAILQ_REMOVE(&bus->used, port, next);
183 bus->nused--;
185 usb_attach(port, NULL);
187 QTAILQ_INSERT_TAIL(&bus->free, port, next);
188 bus->nfree++;
189 return 0;
192 int usb_device_delete_addr(int busnr, int addr)
194 USBBus *bus;
195 USBPort *port;
196 USBDevice *dev;
198 bus = usb_bus_find(busnr);
199 if (!bus)
200 return -1;
202 QTAILQ_FOREACH(port, &bus->used, next) {
203 if (port->dev->addr == addr)
204 break;
206 if (!port)
207 return -1;
208 dev = port->dev;
210 qdev_free(&dev->qdev);
211 return 0;
214 static const char *usb_speed(unsigned int speed)
216 static const char *txt[] = {
217 [ USB_SPEED_LOW ] = "1.5",
218 [ USB_SPEED_FULL ] = "12",
219 [ USB_SPEED_HIGH ] = "480",
221 if (speed >= ARRAY_SIZE(txt))
222 return "?";
223 return txt[speed];
226 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
228 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
229 USBBus *bus = usb_bus_from_device(dev);
231 monitor_printf(mon, "%*saddr %d.%d, speed %s, name %s%s\n",
232 indent, "", bus->busnr, dev->addr,
233 usb_speed(dev->speed), dev->product_desc,
234 dev->attached ? ", attached" : "");
237 void usb_info(Monitor *mon)
239 USBBus *bus;
240 USBDevice *dev;
241 USBPort *port;
243 if (QTAILQ_EMPTY(&busses)) {
244 monitor_printf(mon, "USB support not enabled\n");
245 return;
248 QTAILQ_FOREACH(bus, &busses, next) {
249 QTAILQ_FOREACH(port, &bus->used, next) {
250 dev = port->dev;
251 if (!dev)
252 continue;
253 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
254 bus->busnr, dev->addr, usb_speed(dev->speed),
255 dev->product_desc);
260 /* handle legacy -usbdevice cmd line option */
261 USBDevice *usbdevice_create(const char *cmdline)
263 USBBus *bus = usb_bus_find(-1 /* any */);
264 DeviceInfo *info;
265 USBDeviceInfo *usb;
266 char driver[32];
267 const char *params;
268 int len;
270 params = strchr(cmdline,':');
271 if (params) {
272 params++;
273 len = params - cmdline;
274 if (len > sizeof(driver))
275 len = sizeof(driver);
276 pstrcpy(driver, len, cmdline);
277 } else {
278 params = "";
279 pstrcpy(driver, sizeof(driver), cmdline);
282 for (info = device_info_list; info != NULL; info = info->next) {
283 if (info->bus_info != &usb_bus_info)
284 continue;
285 usb = DO_UPCAST(USBDeviceInfo, qdev, info);
286 if (usb->usbdevice_name == NULL)
287 continue;
288 if (strcmp(usb->usbdevice_name, driver) != 0)
289 continue;
290 break;
292 if (info == NULL) {
293 #if 0
294 /* no error because some drivers are not converted (yet) */
295 error_report("usbdevice %s not found", driver);
296 #endif
297 return NULL;
300 if (!usb->usbdevice_init) {
301 if (*params) {
302 error_report("usbdevice %s accepts no params", driver);
303 return NULL;
305 return usb_create_simple(bus, usb->qdev.name);
307 return usb->usbdevice_init(params);