]>
Commit | Line | Data |
---|---|---|
806b6024 GH |
1 | #include "hw.h" |
2 | #include "usb.h" | |
3 | #include "qdev.h" | |
a5d2f727 GH |
4 | #include "sysemu.h" |
5 | #include "monitor.h" | |
6 | ||
7 | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); | |
c7a2196a GH |
8 | |
9 | static char *usb_get_dev_path(DeviceState *dev); | |
70d31cb2 | 10 | static char *usb_get_fw_dev_path(DeviceState *qdev); |
806b6024 GH |
11 | |
12 | static struct BusInfo usb_bus_info = { | |
a5d2f727 GH |
13 | .name = "USB", |
14 | .size = sizeof(USBBus), | |
15 | .print_dev = usb_bus_dev_print, | |
c7a2196a | 16 | .get_dev_path = usb_get_dev_path, |
70d31cb2 | 17 | .get_fw_dev_path = usb_get_fw_dev_path, |
5f69076b GH |
18 | .props = (Property[]) { |
19 | DEFINE_PROP_STRING("port", USBDevice, port_path), | |
20 | DEFINE_PROP_END_OF_LIST() | |
21 | }, | |
806b6024 GH |
22 | }; |
23 | static int next_usb_bus = 0; | |
72cf2d4f | 24 | static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses); |
806b6024 | 25 | |
c1ecb40a GH |
26 | const VMStateDescription vmstate_usb_device = { |
27 | .name = "USBDevice", | |
28 | .version_id = 1, | |
29 | .minimum_version_id = 1, | |
30 | .fields = (VMStateField []) { | |
31 | VMSTATE_UINT8(addr, USBDevice), | |
32 | VMSTATE_INT32(state, USBDevice), | |
33 | VMSTATE_INT32(remote_wakeup, USBDevice), | |
34 | VMSTATE_INT32(setup_state, USBDevice), | |
35 | VMSTATE_INT32(setup_len, USBDevice), | |
36 | VMSTATE_INT32(setup_index, USBDevice), | |
37 | VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8), | |
38 | VMSTATE_END_OF_LIST(), | |
39 | } | |
40 | }; | |
41 | ||
07771f6f | 42 | void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host) |
806b6024 | 43 | { |
b2317837 | 44 | qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL); |
07771f6f | 45 | bus->ops = ops; |
806b6024 | 46 | bus->busnr = next_usb_bus++; |
ef816d83 | 47 | bus->qbus.allow_hotplug = 1; /* Yes, we can */ |
72cf2d4f BS |
48 | QTAILQ_INIT(&bus->free); |
49 | QTAILQ_INIT(&bus->used); | |
50 | QTAILQ_INSERT_TAIL(&busses, bus, next); | |
806b6024 GH |
51 | } |
52 | ||
53 | USBBus *usb_bus_find(int busnr) | |
54 | { | |
55 | USBBus *bus; | |
56 | ||
57 | if (-1 == busnr) | |
72cf2d4f BS |
58 | return QTAILQ_FIRST(&busses); |
59 | QTAILQ_FOREACH(bus, &busses, next) { | |
806b6024 GH |
60 | if (bus->busnr == busnr) |
61 | return bus; | |
62 | } | |
63 | return NULL; | |
64 | } | |
65 | ||
66 | static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base) | |
67 | { | |
68 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
69 | USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base); | |
70 | int rc; | |
71 | ||
06384698 | 72 | pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc); |
806b6024 | 73 | dev->info = info; |
61e094c0 | 74 | dev->auto_attach = 1; |
132a3f55 | 75 | QLIST_INIT(&dev->strings); |
806b6024 | 76 | rc = dev->info->init(dev); |
61e094c0 | 77 | if (rc == 0 && dev->auto_attach) |
a5d2f727 | 78 | usb_device_attach(dev); |
806b6024 GH |
79 | return rc; |
80 | } | |
81 | ||
a8e662b5 GH |
82 | static int usb_qdev_exit(DeviceState *qdev) |
83 | { | |
84 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
07771f6f | 85 | USBBus *bus = usb_bus_from_device(dev); |
a8e662b5 GH |
86 | |
87 | usb_device_detach(dev); | |
07771f6f | 88 | bus->ops->device_destroy(bus, dev); |
a8e662b5 GH |
89 | if (dev->info->handle_destroy) { |
90 | dev->info->handle_destroy(dev); | |
91 | } | |
92 | return 0; | |
93 | } | |
94 | ||
806b6024 GH |
95 | void usb_qdev_register(USBDeviceInfo *info) |
96 | { | |
97 | info->qdev.bus_info = &usb_bus_info; | |
98 | info->qdev.init = usb_qdev_init; | |
ef816d83 | 99 | info->qdev.unplug = qdev_simple_unplug_cb; |
a8e662b5 | 100 | info->qdev.exit = usb_qdev_exit; |
806b6024 GH |
101 | qdev_register(&info->qdev); |
102 | } | |
103 | ||
104 | void usb_qdev_register_many(USBDeviceInfo *info) | |
105 | { | |
106 | while (info->qdev.name) { | |
107 | usb_qdev_register(info); | |
108 | info++; | |
109 | } | |
110 | } | |
111 | ||
a5d2f727 | 112 | USBDevice *usb_create(USBBus *bus, const char *name) |
806b6024 GH |
113 | { |
114 | DeviceState *dev; | |
115 | ||
116 | #if 1 | |
117 | /* temporary stopgap until all usb is properly qdev-ified */ | |
118 | if (!bus) { | |
119 | bus = usb_bus_find(-1); | |
120 | if (!bus) | |
121 | return NULL; | |
122 | fprintf(stderr, "%s: no bus specified, using \"%s\" for \"%s\"\n", | |
123 | __FUNCTION__, bus->qbus.name, name); | |
124 | } | |
125 | #endif | |
126 | ||
127 | dev = qdev_create(&bus->qbus, name); | |
806b6024 GH |
128 | return DO_UPCAST(USBDevice, qdev, dev); |
129 | } | |
a5d2f727 GH |
130 | |
131 | USBDevice *usb_create_simple(USBBus *bus, const char *name) | |
132 | { | |
133 | USBDevice *dev = usb_create(bus, name); | |
d44168ff PB |
134 | if (!dev) { |
135 | hw_error("Failed to create USB device '%s'\n", name); | |
136 | } | |
e23a1b33 | 137 | qdev_init_nofail(&dev->qdev); |
a5d2f727 GH |
138 | return dev; |
139 | } | |
140 | ||
141 | void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index, | |
ace1318b | 142 | USBPortOps *ops, int speedmask) |
a5d2f727 GH |
143 | { |
144 | port->opaque = opaque; | |
145 | port->index = index; | |
0d86d2be GH |
146 | port->opaque = opaque; |
147 | port->index = index; | |
148 | port->ops = ops; | |
843d4e0c | 149 | port->speedmask = speedmask; |
72cf2d4f | 150 | QTAILQ_INSERT_TAIL(&bus->free, port, next); |
a5d2f727 GH |
151 | bus->nfree++; |
152 | } | |
153 | ||
c7a2196a GH |
154 | void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr) |
155 | { | |
156 | if (upstream) { | |
157 | snprintf(downstream->path, sizeof(downstream->path), "%s.%d", | |
158 | upstream->path, portnr); | |
159 | } else { | |
160 | snprintf(downstream->path, sizeof(downstream->path), "%d", portnr); | |
161 | } | |
162 | } | |
163 | ||
a8e662b5 GH |
164 | void usb_unregister_port(USBBus *bus, USBPort *port) |
165 | { | |
166 | if (port->dev) | |
167 | qdev_free(&port->dev->qdev); | |
168 | QTAILQ_REMOVE(&bus->free, port, next); | |
169 | bus->nfree--; | |
170 | } | |
171 | ||
a5d2f727 GH |
172 | static void do_attach(USBDevice *dev) |
173 | { | |
174 | USBBus *bus = usb_bus_from_device(dev); | |
175 | USBPort *port; | |
176 | ||
177 | if (dev->attached) { | |
178 | fprintf(stderr, "Warning: tried to attach usb device %s twice\n", | |
0fe6d12e | 179 | dev->product_desc); |
a5d2f727 GH |
180 | return; |
181 | } | |
5f69076b GH |
182 | if (dev->port_path) { |
183 | QTAILQ_FOREACH(port, &bus->free, next) { | |
184 | if (strcmp(port->path, dev->port_path) == 0) { | |
185 | break; | |
186 | } | |
187 | } | |
188 | if (port == NULL) { | |
189 | fprintf(stderr, "Warning: usb port %s (bus %s) not found\n", | |
190 | dev->port_path, bus->qbus.name); | |
191 | return; | |
192 | } | |
193 | } else { | |
194 | port = QTAILQ_FIRST(&bus->free); | |
195 | } | |
a5d2f727 | 196 | |
5f69076b | 197 | dev->attached++; |
72cf2d4f | 198 | QTAILQ_REMOVE(&bus->free, port, next); |
a5d2f727 GH |
199 | bus->nfree--; |
200 | ||
201 | usb_attach(port, dev); | |
202 | ||
72cf2d4f | 203 | QTAILQ_INSERT_TAIL(&bus->used, port, next); |
a5d2f727 GH |
204 | bus->nused++; |
205 | } | |
206 | ||
207 | int usb_device_attach(USBDevice *dev) | |
208 | { | |
209 | USBBus *bus = usb_bus_from_device(dev); | |
a5d2f727 | 210 | |
5f69076b GH |
211 | if (bus->nfree == 1 && dev->port_path == NULL) { |
212 | /* Create a new hub and chain it on | |
213 | (unless a physical port location is specified). */ | |
d4c4e6fd | 214 | usb_create_simple(bus, "usb-hub"); |
a5d2f727 GH |
215 | } |
216 | do_attach(dev); | |
217 | return 0; | |
218 | } | |
219 | ||
a8e662b5 GH |
220 | int usb_device_detach(USBDevice *dev) |
221 | { | |
222 | USBBus *bus = usb_bus_from_device(dev); | |
223 | USBPort *port; | |
224 | ||
225 | if (!dev->attached) { | |
226 | fprintf(stderr, "Warning: tried to detach unattached usb device %s\n", | |
0fe6d12e | 227 | dev->product_desc); |
a8e662b5 GH |
228 | return -1; |
229 | } | |
230 | dev->attached--; | |
231 | ||
232 | QTAILQ_FOREACH(port, &bus->used, next) { | |
233 | if (port->dev == dev) | |
234 | break; | |
235 | } | |
236 | assert(port != NULL); | |
237 | ||
238 | QTAILQ_REMOVE(&bus->used, port, next); | |
239 | bus->nused--; | |
240 | ||
241 | usb_attach(port, NULL); | |
242 | ||
243 | QTAILQ_INSERT_TAIL(&bus->free, port, next); | |
244 | bus->nfree++; | |
245 | return 0; | |
246 | } | |
247 | ||
a5d2f727 GH |
248 | int usb_device_delete_addr(int busnr, int addr) |
249 | { | |
250 | USBBus *bus; | |
251 | USBPort *port; | |
252 | USBDevice *dev; | |
253 | ||
254 | bus = usb_bus_find(busnr); | |
255 | if (!bus) | |
256 | return -1; | |
257 | ||
72cf2d4f | 258 | QTAILQ_FOREACH(port, &bus->used, next) { |
a5d2f727 GH |
259 | if (port->dev->addr == addr) |
260 | break; | |
261 | } | |
262 | if (!port) | |
263 | return -1; | |
a5d2f727 | 264 | dev = port->dev; |
a5d2f727 | 265 | |
a8e662b5 | 266 | qdev_free(&dev->qdev); |
a5d2f727 GH |
267 | return 0; |
268 | } | |
269 | ||
270 | static const char *usb_speed(unsigned int speed) | |
271 | { | |
272 | static const char *txt[] = { | |
273 | [ USB_SPEED_LOW ] = "1.5", | |
274 | [ USB_SPEED_FULL ] = "12", | |
275 | [ USB_SPEED_HIGH ] = "480", | |
276 | }; | |
277 | if (speed >= ARRAY_SIZE(txt)) | |
278 | return "?"; | |
279 | return txt[speed]; | |
280 | } | |
281 | ||
282 | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) | |
283 | { | |
284 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
285 | USBBus *bus = usb_bus_from_device(dev); | |
286 | ||
c7a2196a | 287 | monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n", |
66a6593a | 288 | indent, "", bus->busnr, dev->addr, |
c7a2196a | 289 | dev->port ? dev->port->path : "-", |
0fe6d12e | 290 | usb_speed(dev->speed), dev->product_desc, |
66a6593a | 291 | dev->attached ? ", attached" : ""); |
a5d2f727 GH |
292 | } |
293 | ||
c7a2196a GH |
294 | static char *usb_get_dev_path(DeviceState *qdev) |
295 | { | |
296 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
297 | return qemu_strdup(dev->port->path); | |
298 | } | |
299 | ||
70d31cb2 GH |
300 | static char *usb_get_fw_dev_path(DeviceState *qdev) |
301 | { | |
302 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
303 | char *fw_path, *in; | |
ea87e95f | 304 | ssize_t pos = 0, fw_len; |
70d31cb2 GH |
305 | long nr; |
306 | ||
ea87e95f BS |
307 | fw_len = 32 + strlen(dev->port->path) * 6; |
308 | fw_path = qemu_malloc(fw_len); | |
70d31cb2 | 309 | in = dev->port->path; |
ea87e95f | 310 | while (fw_len - pos > 0) { |
70d31cb2 GH |
311 | nr = strtol(in, &in, 10); |
312 | if (in[0] == '.') { | |
313 | /* some hub between root port and device */ | |
ea87e95f | 314 | pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr); |
70d31cb2 GH |
315 | in++; |
316 | } else { | |
317 | /* the device itself */ | |
ea87e95f BS |
318 | pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld", |
319 | qdev_fw_name(qdev), nr); | |
70d31cb2 GH |
320 | break; |
321 | } | |
322 | } | |
323 | return fw_path; | |
324 | } | |
325 | ||
a5d2f727 GH |
326 | void usb_info(Monitor *mon) |
327 | { | |
328 | USBBus *bus; | |
329 | USBDevice *dev; | |
330 | USBPort *port; | |
331 | ||
72cf2d4f | 332 | if (QTAILQ_EMPTY(&busses)) { |
a5d2f727 GH |
333 | monitor_printf(mon, "USB support not enabled\n"); |
334 | return; | |
335 | } | |
336 | ||
72cf2d4f BS |
337 | QTAILQ_FOREACH(bus, &busses, next) { |
338 | QTAILQ_FOREACH(port, &bus->used, next) { | |
a5d2f727 GH |
339 | dev = port->dev; |
340 | if (!dev) | |
341 | continue; | |
c7a2196a GH |
342 | monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n", |
343 | bus->busnr, dev->addr, port->path, usb_speed(dev->speed), | |
0fe6d12e | 344 | dev->product_desc); |
a5d2f727 GH |
345 | } |
346 | } | |
347 | } | |
348 | ||
0958b4cc GH |
349 | /* handle legacy -usbdevice cmd line option */ |
350 | USBDevice *usbdevice_create(const char *cmdline) | |
351 | { | |
352 | USBBus *bus = usb_bus_find(-1 /* any */); | |
353 | DeviceInfo *info; | |
354 | USBDeviceInfo *usb; | |
702f3e0f JK |
355 | char driver[32]; |
356 | const char *params; | |
0958b4cc GH |
357 | int len; |
358 | ||
359 | params = strchr(cmdline,':'); | |
360 | if (params) { | |
361 | params++; | |
362 | len = params - cmdline; | |
363 | if (len > sizeof(driver)) | |
364 | len = sizeof(driver); | |
365 | pstrcpy(driver, len, cmdline); | |
366 | } else { | |
702f3e0f | 367 | params = ""; |
0958b4cc GH |
368 | pstrcpy(driver, sizeof(driver), cmdline); |
369 | } | |
370 | ||
371 | for (info = device_info_list; info != NULL; info = info->next) { | |
372 | if (info->bus_info != &usb_bus_info) | |
373 | continue; | |
374 | usb = DO_UPCAST(USBDeviceInfo, qdev, info); | |
375 | if (usb->usbdevice_name == NULL) | |
376 | continue; | |
377 | if (strcmp(usb->usbdevice_name, driver) != 0) | |
378 | continue; | |
379 | break; | |
380 | } | |
381 | if (info == NULL) { | |
382 | #if 0 | |
383 | /* no error because some drivers are not converted (yet) */ | |
1ecda02b | 384 | error_report("usbdevice %s not found", driver); |
0958b4cc GH |
385 | #endif |
386 | return NULL; | |
387 | } | |
388 | ||
389 | if (!usb->usbdevice_init) { | |
98f22dc1 | 390 | if (*params) { |
1ecda02b | 391 | error_report("usbdevice %s accepts no params", driver); |
0958b4cc GH |
392 | return NULL; |
393 | } | |
394 | return usb_create_simple(bus, usb->qdev.name); | |
395 | } | |
396 | return usb->usbdevice_init(params); | |
397 | } |