root / hw / usb-bus.c @ 21eea4b3
History | View | Annotate | Download (4.8 kB)
1 | 806b6024 | Gerd Hoffmann | #include "hw.h" |
---|---|---|---|
2 | 806b6024 | Gerd Hoffmann | #include "usb.h" |
3 | 806b6024 | Gerd Hoffmann | #include "qdev.h" |
4 | a5d2f727 | Gerd Hoffmann | #include "sysemu.h" |
5 | a5d2f727 | Gerd Hoffmann | #include "monitor.h" |
6 | a5d2f727 | Gerd Hoffmann | |
7 | a5d2f727 | Gerd Hoffmann | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); |
8 | 806b6024 | Gerd Hoffmann | |
9 | 806b6024 | Gerd Hoffmann | static struct BusInfo usb_bus_info = { |
10 | a5d2f727 | Gerd Hoffmann | .name = "USB",
|
11 | a5d2f727 | Gerd Hoffmann | .size = sizeof(USBBus),
|
12 | a5d2f727 | Gerd Hoffmann | .print_dev = usb_bus_dev_print, |
13 | 806b6024 | Gerd Hoffmann | }; |
14 | 806b6024 | Gerd Hoffmann | static int next_usb_bus = 0; |
15 | 72cf2d4f | Blue Swirl | static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
|
16 | 806b6024 | Gerd Hoffmann | |
17 | b2317837 | Gerd Hoffmann | void usb_bus_new(USBBus *bus, DeviceState *host)
|
18 | 806b6024 | Gerd Hoffmann | { |
19 | b2317837 | Gerd Hoffmann | qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
|
20 | 806b6024 | Gerd Hoffmann | bus->busnr = next_usb_bus++; |
21 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&bus->free); |
22 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&bus->used); |
23 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&busses, bus, next); |
24 | 806b6024 | Gerd Hoffmann | } |
25 | 806b6024 | Gerd Hoffmann | |
26 | 806b6024 | Gerd Hoffmann | USBBus *usb_bus_find(int busnr)
|
27 | 806b6024 | Gerd Hoffmann | { |
28 | 806b6024 | Gerd Hoffmann | USBBus *bus; |
29 | 806b6024 | Gerd Hoffmann | |
30 | 806b6024 | Gerd Hoffmann | if (-1 == busnr) |
31 | 72cf2d4f | Blue Swirl | return QTAILQ_FIRST(&busses);
|
32 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(bus, &busses, next) { |
33 | 806b6024 | Gerd Hoffmann | if (bus->busnr == busnr)
|
34 | 806b6024 | Gerd Hoffmann | return bus;
|
35 | 806b6024 | Gerd Hoffmann | } |
36 | 806b6024 | Gerd Hoffmann | return NULL; |
37 | 806b6024 | Gerd Hoffmann | } |
38 | 806b6024 | Gerd Hoffmann | |
39 | 806b6024 | Gerd Hoffmann | static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base) |
40 | 806b6024 | Gerd Hoffmann | { |
41 | 806b6024 | Gerd Hoffmann | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); |
42 | 806b6024 | Gerd Hoffmann | USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base); |
43 | 806b6024 | Gerd Hoffmann | int rc;
|
44 | 806b6024 | Gerd Hoffmann | |
45 | 806b6024 | Gerd Hoffmann | pstrcpy(dev->devname, sizeof(dev->devname), qdev->info->name);
|
46 | 806b6024 | Gerd Hoffmann | dev->info = info; |
47 | 806b6024 | Gerd Hoffmann | rc = dev->info->init(dev); |
48 | a5d2f727 | Gerd Hoffmann | if (rc == 0) |
49 | a5d2f727 | Gerd Hoffmann | usb_device_attach(dev); |
50 | 806b6024 | Gerd Hoffmann | return rc;
|
51 | 806b6024 | Gerd Hoffmann | } |
52 | 806b6024 | Gerd Hoffmann | |
53 | 806b6024 | Gerd Hoffmann | void usb_qdev_register(USBDeviceInfo *info)
|
54 | 806b6024 | Gerd Hoffmann | { |
55 | 806b6024 | Gerd Hoffmann | info->qdev.bus_info = &usb_bus_info; |
56 | 806b6024 | Gerd Hoffmann | info->qdev.init = usb_qdev_init; |
57 | 806b6024 | Gerd Hoffmann | qdev_register(&info->qdev); |
58 | 806b6024 | Gerd Hoffmann | } |
59 | 806b6024 | Gerd Hoffmann | |
60 | 806b6024 | Gerd Hoffmann | void usb_qdev_register_many(USBDeviceInfo *info)
|
61 | 806b6024 | Gerd Hoffmann | { |
62 | 806b6024 | Gerd Hoffmann | while (info->qdev.name) {
|
63 | 806b6024 | Gerd Hoffmann | usb_qdev_register(info); |
64 | 806b6024 | Gerd Hoffmann | info++; |
65 | 806b6024 | Gerd Hoffmann | } |
66 | 806b6024 | Gerd Hoffmann | } |
67 | 806b6024 | Gerd Hoffmann | |
68 | a5d2f727 | Gerd Hoffmann | USBDevice *usb_create(USBBus *bus, const char *name) |
69 | 806b6024 | Gerd Hoffmann | { |
70 | 806b6024 | Gerd Hoffmann | DeviceState *dev; |
71 | 806b6024 | Gerd Hoffmann | |
72 | 806b6024 | Gerd Hoffmann | #if 1 |
73 | 806b6024 | Gerd Hoffmann | /* temporary stopgap until all usb is properly qdev-ified */
|
74 | 806b6024 | Gerd Hoffmann | if (!bus) {
|
75 | 806b6024 | Gerd Hoffmann | bus = usb_bus_find(-1);
|
76 | 806b6024 | Gerd Hoffmann | if (!bus)
|
77 | 806b6024 | Gerd Hoffmann | return NULL; |
78 | 806b6024 | Gerd Hoffmann | fprintf(stderr, "%s: no bus specified, using \"%s\" for \"%s\"\n",
|
79 | 806b6024 | Gerd Hoffmann | __FUNCTION__, bus->qbus.name, name); |
80 | 806b6024 | Gerd Hoffmann | } |
81 | 806b6024 | Gerd Hoffmann | #endif
|
82 | 806b6024 | Gerd Hoffmann | |
83 | 806b6024 | Gerd Hoffmann | dev = qdev_create(&bus->qbus, name); |
84 | 806b6024 | Gerd Hoffmann | return DO_UPCAST(USBDevice, qdev, dev);
|
85 | 806b6024 | Gerd Hoffmann | } |
86 | a5d2f727 | Gerd Hoffmann | |
87 | a5d2f727 | Gerd Hoffmann | USBDevice *usb_create_simple(USBBus *bus, const char *name) |
88 | a5d2f727 | Gerd Hoffmann | { |
89 | a5d2f727 | Gerd Hoffmann | USBDevice *dev = usb_create(bus, name); |
90 | a5d2f727 | Gerd Hoffmann | qdev_init(&dev->qdev); |
91 | a5d2f727 | Gerd Hoffmann | return dev;
|
92 | a5d2f727 | Gerd Hoffmann | } |
93 | a5d2f727 | Gerd Hoffmann | |
94 | a5d2f727 | Gerd Hoffmann | void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index, |
95 | a5d2f727 | Gerd Hoffmann | usb_attachfn attach) |
96 | a5d2f727 | Gerd Hoffmann | { |
97 | a5d2f727 | Gerd Hoffmann | port->opaque = opaque; |
98 | a5d2f727 | Gerd Hoffmann | port->index = index; |
99 | a5d2f727 | Gerd Hoffmann | port->attach = attach; |
100 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&bus->free, port, next); |
101 | a5d2f727 | Gerd Hoffmann | bus->nfree++; |
102 | a5d2f727 | Gerd Hoffmann | } |
103 | a5d2f727 | Gerd Hoffmann | |
104 | a5d2f727 | Gerd Hoffmann | static void do_attach(USBDevice *dev) |
105 | a5d2f727 | Gerd Hoffmann | { |
106 | a5d2f727 | Gerd Hoffmann | USBBus *bus = usb_bus_from_device(dev); |
107 | a5d2f727 | Gerd Hoffmann | USBPort *port; |
108 | a5d2f727 | Gerd Hoffmann | |
109 | a5d2f727 | Gerd Hoffmann | if (dev->attached) {
|
110 | a5d2f727 | Gerd Hoffmann | fprintf(stderr, "Warning: tried to attach usb device %s twice\n",
|
111 | a5d2f727 | Gerd Hoffmann | dev->devname); |
112 | a5d2f727 | Gerd Hoffmann | return;
|
113 | a5d2f727 | Gerd Hoffmann | } |
114 | a5d2f727 | Gerd Hoffmann | dev->attached++; |
115 | a5d2f727 | Gerd Hoffmann | |
116 | 72cf2d4f | Blue Swirl | port = QTAILQ_FIRST(&bus->free); |
117 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&bus->free, port, next); |
118 | a5d2f727 | Gerd Hoffmann | bus->nfree--; |
119 | a5d2f727 | Gerd Hoffmann | |
120 | a5d2f727 | Gerd Hoffmann | usb_attach(port, dev); |
121 | a5d2f727 | Gerd Hoffmann | |
122 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&bus->used, port, next); |
123 | a5d2f727 | Gerd Hoffmann | bus->nused++; |
124 | a5d2f727 | Gerd Hoffmann | } |
125 | a5d2f727 | Gerd Hoffmann | |
126 | a5d2f727 | Gerd Hoffmann | int usb_device_attach(USBDevice *dev)
|
127 | a5d2f727 | Gerd Hoffmann | { |
128 | a5d2f727 | Gerd Hoffmann | USBBus *bus = usb_bus_from_device(dev); |
129 | a5d2f727 | Gerd Hoffmann | USBDevice *hub; |
130 | a5d2f727 | Gerd Hoffmann | |
131 | a5d2f727 | Gerd Hoffmann | if (bus->nfree == 1) { |
132 | a5d2f727 | Gerd Hoffmann | /* Create a new hub and chain it on. */
|
133 | a5d2f727 | Gerd Hoffmann | hub = usb_create_simple(bus, "QEMU USB Hub");
|
134 | a5d2f727 | Gerd Hoffmann | } |
135 | a5d2f727 | Gerd Hoffmann | do_attach(dev); |
136 | a5d2f727 | Gerd Hoffmann | return 0; |
137 | a5d2f727 | Gerd Hoffmann | } |
138 | a5d2f727 | Gerd Hoffmann | |
139 | a5d2f727 | Gerd Hoffmann | int usb_device_delete_addr(int busnr, int addr) |
140 | a5d2f727 | Gerd Hoffmann | { |
141 | a5d2f727 | Gerd Hoffmann | USBBus *bus; |
142 | a5d2f727 | Gerd Hoffmann | USBPort *port; |
143 | a5d2f727 | Gerd Hoffmann | USBDevice *dev; |
144 | a5d2f727 | Gerd Hoffmann | |
145 | a5d2f727 | Gerd Hoffmann | bus = usb_bus_find(busnr); |
146 | a5d2f727 | Gerd Hoffmann | if (!bus)
|
147 | a5d2f727 | Gerd Hoffmann | return -1; |
148 | a5d2f727 | Gerd Hoffmann | |
149 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(port, &bus->used, next) { |
150 | a5d2f727 | Gerd Hoffmann | if (port->dev->addr == addr)
|
151 | a5d2f727 | Gerd Hoffmann | break;
|
152 | a5d2f727 | Gerd Hoffmann | } |
153 | a5d2f727 | Gerd Hoffmann | if (!port)
|
154 | a5d2f727 | Gerd Hoffmann | return -1; |
155 | a5d2f727 | Gerd Hoffmann | |
156 | a5d2f727 | Gerd Hoffmann | dev = port->dev; |
157 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&bus->used, port, next); |
158 | a5d2f727 | Gerd Hoffmann | bus->nused--; |
159 | a5d2f727 | Gerd Hoffmann | |
160 | a5d2f727 | Gerd Hoffmann | usb_attach(port, NULL);
|
161 | a5d2f727 | Gerd Hoffmann | dev->info->handle_destroy(dev); |
162 | a5d2f727 | Gerd Hoffmann | |
163 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&bus->free, port, next); |
164 | a5d2f727 | Gerd Hoffmann | bus->nfree++; |
165 | a5d2f727 | Gerd Hoffmann | return 0; |
166 | a5d2f727 | Gerd Hoffmann | } |
167 | a5d2f727 | Gerd Hoffmann | |
168 | a5d2f727 | Gerd Hoffmann | static const char *usb_speed(unsigned int speed) |
169 | a5d2f727 | Gerd Hoffmann | { |
170 | a5d2f727 | Gerd Hoffmann | static const char *txt[] = { |
171 | a5d2f727 | Gerd Hoffmann | [ USB_SPEED_LOW ] = "1.5",
|
172 | a5d2f727 | Gerd Hoffmann | [ USB_SPEED_FULL ] = "12",
|
173 | a5d2f727 | Gerd Hoffmann | [ USB_SPEED_HIGH ] = "480",
|
174 | a5d2f727 | Gerd Hoffmann | }; |
175 | a5d2f727 | Gerd Hoffmann | if (speed >= ARRAY_SIZE(txt))
|
176 | a5d2f727 | Gerd Hoffmann | return "?"; |
177 | a5d2f727 | Gerd Hoffmann | return txt[speed];
|
178 | a5d2f727 | Gerd Hoffmann | } |
179 | a5d2f727 | Gerd Hoffmann | |
180 | a5d2f727 | Gerd Hoffmann | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) |
181 | a5d2f727 | Gerd Hoffmann | { |
182 | a5d2f727 | Gerd Hoffmann | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); |
183 | a5d2f727 | Gerd Hoffmann | USBBus *bus = usb_bus_from_device(dev); |
184 | a5d2f727 | Gerd Hoffmann | |
185 | a5d2f727 | Gerd Hoffmann | monitor_printf(mon, "%*saddr %d.%d, speed %s, name %s\n", indent, "", |
186 | a5d2f727 | Gerd Hoffmann | bus->busnr, dev->addr, |
187 | a5d2f727 | Gerd Hoffmann | usb_speed(dev->speed), dev->devname); |
188 | a5d2f727 | Gerd Hoffmann | } |
189 | a5d2f727 | Gerd Hoffmann | |
190 | a5d2f727 | Gerd Hoffmann | void usb_info(Monitor *mon)
|
191 | a5d2f727 | Gerd Hoffmann | { |
192 | a5d2f727 | Gerd Hoffmann | USBBus *bus; |
193 | a5d2f727 | Gerd Hoffmann | USBDevice *dev; |
194 | a5d2f727 | Gerd Hoffmann | USBPort *port; |
195 | a5d2f727 | Gerd Hoffmann | |
196 | 72cf2d4f | Blue Swirl | if (QTAILQ_EMPTY(&busses)) {
|
197 | a5d2f727 | Gerd Hoffmann | monitor_printf(mon, "USB support not enabled\n");
|
198 | a5d2f727 | Gerd Hoffmann | return;
|
199 | a5d2f727 | Gerd Hoffmann | } |
200 | a5d2f727 | Gerd Hoffmann | |
201 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(bus, &busses, next) { |
202 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(port, &bus->used, next) { |
203 | a5d2f727 | Gerd Hoffmann | dev = port->dev; |
204 | a5d2f727 | Gerd Hoffmann | if (!dev)
|
205 | a5d2f727 | Gerd Hoffmann | continue;
|
206 | a5d2f727 | Gerd Hoffmann | monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
|
207 | a5d2f727 | Gerd Hoffmann | bus->busnr, dev->addr, usb_speed(dev->speed), dev->devname); |
208 | a5d2f727 | Gerd Hoffmann | } |
209 | a5d2f727 | Gerd Hoffmann | } |
210 | a5d2f727 | Gerd Hoffmann | } |