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