really fix net.h
[qemu.git] / hw / usb-bus.c
blob98987a197392fde6c6b05b8d8e10f26e7e8b214c
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->devname, sizeof(dev->devname), qdev->info->name);
47 dev->info = info;
48 rc = dev->info->init(dev);
49 if (rc == 0)
50 usb_device_attach(dev);
51 return rc;
54 static int usb_qdev_exit(DeviceState *qdev)
56 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
58 usb_device_detach(dev);
59 if (dev->info->handle_destroy) {
60 dev->info->handle_destroy(dev);
62 return 0;
65 void usb_qdev_register(USBDeviceInfo *info)
67 info->qdev.bus_info = &usb_bus_info;
68 info->qdev.init = usb_qdev_init;
69 info->qdev.unplug = qdev_simple_unplug_cb;
70 info->qdev.exit = usb_qdev_exit;
71 qdev_register(&info->qdev);
74 void usb_qdev_register_many(USBDeviceInfo *info)
76 while (info->qdev.name) {
77 usb_qdev_register(info);
78 info++;
82 USBDevice *usb_create(USBBus *bus, const char *name)
84 DeviceState *dev;
86 #if 1
87 /* temporary stopgap until all usb is properly qdev-ified */
88 if (!bus) {
89 bus = usb_bus_find(-1);
90 if (!bus)
91 return NULL;
92 fprintf(stderr, "%s: no bus specified, using \"%s\" for \"%s\"\n",
93 __FUNCTION__, bus->qbus.name, name);
95 #endif
97 dev = qdev_create(&bus->qbus, name);
98 return DO_UPCAST(USBDevice, qdev, dev);
101 USBDevice *usb_create_simple(USBBus *bus, const char *name)
103 USBDevice *dev = usb_create(bus, name);
104 qdev_init_nofail(&dev->qdev);
105 return dev;
108 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
109 usb_attachfn attach)
111 port->opaque = opaque;
112 port->index = index;
113 port->attach = attach;
114 QTAILQ_INSERT_TAIL(&bus->free, port, next);
115 bus->nfree++;
118 void usb_unregister_port(USBBus *bus, USBPort *port)
120 if (port->dev)
121 qdev_free(&port->dev->qdev);
122 QTAILQ_REMOVE(&bus->free, port, next);
123 bus->nfree--;
126 static void do_attach(USBDevice *dev)
128 USBBus *bus = usb_bus_from_device(dev);
129 USBPort *port;
131 if (dev->attached) {
132 fprintf(stderr, "Warning: tried to attach usb device %s twice\n",
133 dev->devname);
134 return;
136 dev->attached++;
138 port = QTAILQ_FIRST(&bus->free);
139 QTAILQ_REMOVE(&bus->free, port, next);
140 bus->nfree--;
142 usb_attach(port, dev);
144 QTAILQ_INSERT_TAIL(&bus->used, port, next);
145 bus->nused++;
148 int usb_device_attach(USBDevice *dev)
150 USBBus *bus = usb_bus_from_device(dev);
151 USBDevice *hub;
153 if (bus->nfree == 1) {
154 /* Create a new hub and chain it on. */
155 hub = usb_create_simple(bus, "QEMU USB Hub");
157 do_attach(dev);
158 return 0;
161 int usb_device_detach(USBDevice *dev)
163 USBBus *bus = usb_bus_from_device(dev);
164 USBPort *port;
166 if (!dev->attached) {
167 fprintf(stderr, "Warning: tried to detach unattached usb device %s\n",
168 dev->devname);
169 return -1;
171 dev->attached--;
173 QTAILQ_FOREACH(port, &bus->used, next) {
174 if (port->dev == dev)
175 break;
177 assert(port != NULL);
179 QTAILQ_REMOVE(&bus->used, port, next);
180 bus->nused--;
182 usb_attach(port, NULL);
184 QTAILQ_INSERT_TAIL(&bus->free, port, next);
185 bus->nfree++;
186 return 0;
189 int usb_device_delete_addr(int busnr, int addr)
191 USBBus *bus;
192 USBPort *port;
193 USBDevice *dev;
195 bus = usb_bus_find(busnr);
196 if (!bus)
197 return -1;
199 QTAILQ_FOREACH(port, &bus->used, next) {
200 if (port->dev->addr == addr)
201 break;
203 if (!port)
204 return -1;
205 dev = port->dev;
207 qdev_free(&dev->qdev);
208 return 0;
211 static const char *usb_speed(unsigned int speed)
213 static const char *txt[] = {
214 [ USB_SPEED_LOW ] = "1.5",
215 [ USB_SPEED_FULL ] = "12",
216 [ USB_SPEED_HIGH ] = "480",
218 if (speed >= ARRAY_SIZE(txt))
219 return "?";
220 return txt[speed];
223 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
225 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
226 USBBus *bus = usb_bus_from_device(dev);
228 monitor_printf(mon, "%*saddr %d.%d, speed %s, name %s\n", indent, "",
229 bus->busnr, dev->addr,
230 usb_speed(dev->speed), dev->devname);
233 void usb_info(Monitor *mon)
235 USBBus *bus;
236 USBDevice *dev;
237 USBPort *port;
239 if (QTAILQ_EMPTY(&busses)) {
240 monitor_printf(mon, "USB support not enabled\n");
241 return;
244 QTAILQ_FOREACH(bus, &busses, next) {
245 QTAILQ_FOREACH(port, &bus->used, next) {
246 dev = port->dev;
247 if (!dev)
248 continue;
249 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
250 bus->busnr, dev->addr, usb_speed(dev->speed), dev->devname);