]>
Commit | Line | Data |
---|---|---|
bb36d470 FB |
1 | /* |
2 | * Linux host USB redirector | |
3 | * | |
4 | * Copyright (c) 2005 Fabrice Bellard | |
5fafdf24 | 5 | * |
64838171 AL |
6 | * Copyright (c) 2008 Max Krasnyansky |
7 | * Support for host device auto connect & disconnect | |
5d0c5750 | 8 | * Major rewrite to support fully async operation |
4b096fc9 | 9 | * |
0f431527 AL |
10 | * Copyright 2008 TJ <[email protected]> |
11 | * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition | |
12 | * to the legacy /proc/bus/usb USB device discovery and handling | |
13 | * | |
bb36d470 FB |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
15 | * of this software and associated documentation files (the "Software"), to deal | |
16 | * in the Software without restriction, including without limitation the rights | |
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
18 | * copies of the Software, and to permit persons to whom the Software is | |
19 | * furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
30 | * THE SOFTWARE. | |
31 | */ | |
446ab128 | 32 | |
87ecb68b | 33 | #include "qemu-common.h" |
1de7afc9 | 34 | #include "qemu/timer.h" |
83c9089e | 35 | #include "monitor/monitor.h" |
9c17d615 | 36 | #include "sysemu/sysemu.h" |
e6a2f500 | 37 | #include "trace.h" |
bb36d470 | 38 | |
bb36d470 FB |
39 | #include <dirent.h> |
40 | #include <sys/ioctl.h> | |
bb36d470 | 41 | |
446ab128 AL |
42 | #include <linux/usbdevice_fs.h> |
43 | #include <linux/version.h> | |
44 | #include "hw/usb.h" | |
96dd9aac | 45 | #include "hw/usb/desc.h" |
4075975d | 46 | #include "hw/usb/host.h" |
bb36d470 | 47 | |
2b2325ff GH |
48 | #ifdef CONFIG_USB_LIBUSB |
49 | # define DEVNAME "usb-host-linux" | |
50 | #else | |
51 | # define DEVNAME "usb-host" | |
52 | #endif | |
53 | ||
d9cf1578 BS |
54 | /* We redefine it to avoid version problems */ |
55 | struct usb_ctrltransfer { | |
56 | uint8_t bRequestType; | |
57 | uint8_t bRequest; | |
58 | uint16_t wValue; | |
59 | uint16_t wIndex; | |
60 | uint16_t wLength; | |
61 | uint32_t timeout; | |
62 | void *data; | |
63 | }; | |
64 | ||
ba9acab9 | 65 | typedef int USBScanFunc(void *opaque, int bus_num, int addr, const char *port, |
0f5160d1 | 66 | int class_id, int vendor_id, int product_id, |
a594cfbf | 67 | const char *product_name, int speed); |
26a9e82a | 68 | |
0745eb1e | 69 | //#define DEBUG |
64838171 AL |
70 | |
71 | #ifdef DEBUG | |
d0f2c4c6 | 72 | #define DPRINTF printf |
64838171 | 73 | #else |
d0f2c4c6 | 74 | #define DPRINTF(...) |
64838171 | 75 | #endif |
bb36d470 | 76 | |
1f6e24e7 | 77 | #define PRODUCT_NAME_SZ 32 |
5557d820 | 78 | #define MAX_PORTLEN 16 |
bb36d470 | 79 | |
b9dc033c | 80 | /* endpoint association data */ |
060dc841 | 81 | #define ISO_FRAME_DESC_PER_URB 32 |
060dc841 | 82 | |
71138531 GH |
83 | /* devio.c limits single requests to 16k */ |
84 | #define MAX_USBFS_BUFFER_SIZE 16384 | |
85 | ||
060dc841 HG |
86 | typedef struct AsyncURB AsyncURB; |
87 | ||
b9dc033c | 88 | struct endp_data { |
64838171 | 89 | uint8_t halted; |
bb6d5498 | 90 | uint8_t iso_started; |
060dc841 HG |
91 | AsyncURB *iso_urb; |
92 | int iso_urb_idx; | |
bb6d5498 | 93 | int iso_buffer_used; |
82887262 | 94 | int inflight; |
b9dc033c AZ |
95 | }; |
96 | ||
39c20577 GH |
97 | enum USBHostDeviceOptions { |
98 | USB_HOST_OPT_PIPELINE, | |
99 | }; | |
100 | ||
bb36d470 FB |
101 | typedef struct USBHostDevice { |
102 | USBDevice dev; | |
64838171 | 103 | int fd; |
9516bb47 | 104 | int hub_fd; |
c75fead6 | 105 | int hub_port; |
64838171 | 106 | |
f8ddbfbc | 107 | uint8_t descr[8192]; |
64838171 | 108 | int descr_len; |
24772c1e | 109 | int closing; |
b81bcd8a | 110 | uint32_t iso_urb_count; |
39c20577 | 111 | uint32_t options; |
b373a63a | 112 | Notifier exit; |
a229c053 | 113 | QEMUBH *bh; |
64838171 | 114 | |
d8e17efd GH |
115 | struct endp_data ep_in[USB_MAX_ENDPOINTS]; |
116 | struct endp_data ep_out[USB_MAX_ENDPOINTS]; | |
7a8fc83f | 117 | QLIST_HEAD(, AsyncURB) aurbs; |
4b096fc9 | 118 | |
4b096fc9 AL |
119 | /* Host side address */ |
120 | int bus_num; | |
121 | int addr; | |
5557d820 | 122 | char port[MAX_PORTLEN]; |
26a9e82a | 123 | struct USBAutoFilter match; |
65bb3a5c | 124 | int32_t bootindex; |
3ee886c5 | 125 | int seen, errcount; |
4b096fc9 | 126 | |
26a9e82a | 127 | QTAILQ_ENTRY(USBHostDevice) next; |
bb36d470 FB |
128 | } USBHostDevice; |
129 | ||
26a9e82a GH |
130 | static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs); |
131 | ||
132 | static int usb_host_close(USBHostDevice *dev); | |
26a9e82a | 133 | static void usb_host_auto_check(void *unused); |
2cc59d8c HG |
134 | static int usb_host_read_file(char *line, size_t line_size, |
135 | const char *device_file, const char *device_name); | |
537e8f1a | 136 | static void usb_linux_update_endp_table(USBHostDevice *s); |
26a9e82a | 137 | |
d8e17efd GH |
138 | static int usb_host_usbfs_type(USBHostDevice *s, USBPacket *p) |
139 | { | |
140 | static const int usbfs[] = { | |
141 | [USB_ENDPOINT_XFER_CONTROL] = USBDEVFS_URB_TYPE_CONTROL, | |
142 | [USB_ENDPOINT_XFER_ISOC] = USBDEVFS_URB_TYPE_ISO, | |
143 | [USB_ENDPOINT_XFER_BULK] = USBDEVFS_URB_TYPE_BULK, | |
144 | [USB_ENDPOINT_XFER_INT] = USBDEVFS_URB_TYPE_INTERRUPT, | |
145 | }; | |
079d0b7f | 146 | uint8_t type = p->ep->type; |
d8e17efd GH |
147 | assert(type < ARRAY_SIZE(usbfs)); |
148 | return usbfs[type]; | |
149 | } | |
150 | ||
c7662daa GH |
151 | static int usb_host_do_reset(USBHostDevice *dev) |
152 | { | |
153 | struct timeval s, e; | |
154 | uint32_t usecs; | |
155 | int ret; | |
156 | ||
157 | gettimeofday(&s, NULL); | |
158 | ret = ioctl(dev->fd, USBDEVFS_RESET); | |
159 | gettimeofday(&e, NULL); | |
160 | usecs = (e.tv_sec - s.tv_sec) * 1000000; | |
161 | usecs += e.tv_usec - s.tv_usec; | |
162 | if (usecs > 1000000) { | |
163 | /* more than a second, something is fishy, broken usb device? */ | |
164 | fprintf(stderr, "husb: device %d:%d reset took %d.%06d seconds\n", | |
165 | dev->bus_num, dev->addr, usecs / 1000000, usecs % 1000000); | |
166 | } | |
167 | return ret; | |
168 | } | |
169 | ||
c0e5750b | 170 | static struct endp_data *get_endp(USBHostDevice *s, int pid, int ep) |
ca3a36cf | 171 | { |
c0e5750b GH |
172 | struct endp_data *eps = pid == USB_TOKEN_IN ? s->ep_in : s->ep_out; |
173 | assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT); | |
d8e17efd | 174 | assert(ep > 0 && ep <= USB_MAX_ENDPOINTS); |
c0e5750b | 175 | return eps + ep - 1; |
ca3a36cf GH |
176 | } |
177 | ||
c0e5750b | 178 | static int is_isoc(USBHostDevice *s, int pid, int ep) |
64838171 | 179 | { |
d8e17efd | 180 | return usb_ep_get_type(&s->dev, pid, ep) == USB_ENDPOINT_XFER_ISOC; |
64838171 AL |
181 | } |
182 | ||
c0e5750b | 183 | static int is_valid(USBHostDevice *s, int pid, int ep) |
a0b5fece | 184 | { |
d8e17efd | 185 | return usb_ep_get_type(&s->dev, pid, ep) != USB_ENDPOINT_XFER_INVALID; |
a0b5fece HG |
186 | } |
187 | ||
c0e5750b | 188 | static int is_halted(USBHostDevice *s, int pid, int ep) |
64838171 | 189 | { |
c0e5750b | 190 | return get_endp(s, pid, ep)->halted; |
64838171 AL |
191 | } |
192 | ||
c0e5750b | 193 | static void clear_halt(USBHostDevice *s, int pid, int ep) |
64838171 | 194 | { |
e6a2f500 | 195 | trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep); |
c0e5750b | 196 | get_endp(s, pid, ep)->halted = 0; |
64838171 AL |
197 | } |
198 | ||
c0e5750b | 199 | static void set_halt(USBHostDevice *s, int pid, int ep) |
64838171 | 200 | { |
c0e5750b GH |
201 | if (ep != 0) { |
202 | trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep); | |
203 | get_endp(s, pid, ep)->halted = 1; | |
204 | } | |
64838171 AL |
205 | } |
206 | ||
c0e5750b | 207 | static int is_iso_started(USBHostDevice *s, int pid, int ep) |
bb6d5498 | 208 | { |
c0e5750b | 209 | return get_endp(s, pid, ep)->iso_started; |
bb6d5498 HG |
210 | } |
211 | ||
c0e5750b | 212 | static void clear_iso_started(USBHostDevice *s, int pid, int ep) |
bb6d5498 | 213 | { |
c32da151 | 214 | trace_usb_host_iso_stop(s->bus_num, s->addr, ep); |
c0e5750b | 215 | get_endp(s, pid, ep)->iso_started = 0; |
bb6d5498 HG |
216 | } |
217 | ||
c0e5750b | 218 | static void set_iso_started(USBHostDevice *s, int pid, int ep) |
bb6d5498 | 219 | { |
c0e5750b | 220 | struct endp_data *e = get_endp(s, pid, ep); |
e6a2f500 | 221 | |
c32da151 | 222 | trace_usb_host_iso_start(s->bus_num, s->addr, ep); |
82887262 GH |
223 | if (!e->iso_started) { |
224 | e->iso_started = 1; | |
225 | e->inflight = 0; | |
226 | } | |
227 | } | |
228 | ||
c0e5750b | 229 | static int change_iso_inflight(USBHostDevice *s, int pid, int ep, int value) |
82887262 | 230 | { |
c0e5750b | 231 | struct endp_data *e = get_endp(s, pid, ep); |
82887262 GH |
232 | |
233 | e->inflight += value; | |
234 | return e->inflight; | |
bb6d5498 HG |
235 | } |
236 | ||
c0e5750b | 237 | static void set_iso_urb(USBHostDevice *s, int pid, int ep, AsyncURB *iso_urb) |
060dc841 | 238 | { |
c0e5750b | 239 | get_endp(s, pid, ep)->iso_urb = iso_urb; |
060dc841 HG |
240 | } |
241 | ||
c0e5750b | 242 | static AsyncURB *get_iso_urb(USBHostDevice *s, int pid, int ep) |
060dc841 | 243 | { |
c0e5750b | 244 | return get_endp(s, pid, ep)->iso_urb; |
060dc841 HG |
245 | } |
246 | ||
c0e5750b | 247 | static void set_iso_urb_idx(USBHostDevice *s, int pid, int ep, int i) |
060dc841 | 248 | { |
c0e5750b | 249 | get_endp(s, pid, ep)->iso_urb_idx = i; |
060dc841 HG |
250 | } |
251 | ||
c0e5750b | 252 | static int get_iso_urb_idx(USBHostDevice *s, int pid, int ep) |
060dc841 | 253 | { |
c0e5750b | 254 | return get_endp(s, pid, ep)->iso_urb_idx; |
060dc841 HG |
255 | } |
256 | ||
c0e5750b | 257 | static void set_iso_buffer_used(USBHostDevice *s, int pid, int ep, int i) |
bb6d5498 | 258 | { |
c0e5750b | 259 | get_endp(s, pid, ep)->iso_buffer_used = i; |
bb6d5498 HG |
260 | } |
261 | ||
c0e5750b | 262 | static int get_iso_buffer_used(USBHostDevice *s, int pid, int ep) |
bb6d5498 | 263 | { |
c0e5750b | 264 | return get_endp(s, pid, ep)->iso_buffer_used; |
bb6d5498 HG |
265 | } |
266 | ||
2791104c | 267 | /* |
64838171 | 268 | * Async URB state. |
060dc841 | 269 | * We always allocate iso packet descriptors even for bulk transfers |
2791104c | 270 | * to simplify allocation and casts. |
64838171 | 271 | */ |
060dc841 | 272 | struct AsyncURB |
64838171 AL |
273 | { |
274 | struct usbdevfs_urb urb; | |
060dc841 | 275 | struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB]; |
7a8fc83f GH |
276 | USBHostDevice *hdev; |
277 | QLIST_ENTRY(AsyncURB) next; | |
b9dc033c | 278 | |
060dc841 | 279 | /* For regular async urbs */ |
64838171 | 280 | USBPacket *packet; |
71138531 | 281 | int more; /* large transfer, more urbs follow */ |
060dc841 HG |
282 | |
283 | /* For buffered iso handling */ | |
284 | int iso_frame_idx; /* -1 means in flight */ | |
285 | }; | |
b9dc033c | 286 | |
7a8fc83f | 287 | static AsyncURB *async_alloc(USBHostDevice *s) |
b9dc033c | 288 | { |
7267c094 | 289 | AsyncURB *aurb = g_malloc0(sizeof(AsyncURB)); |
7a8fc83f GH |
290 | aurb->hdev = s; |
291 | QLIST_INSERT_HEAD(&s->aurbs, aurb, next); | |
292 | return aurb; | |
b9dc033c AZ |
293 | } |
294 | ||
64838171 | 295 | static void async_free(AsyncURB *aurb) |
b9dc033c | 296 | { |
7a8fc83f | 297 | QLIST_REMOVE(aurb, next); |
7267c094 | 298 | g_free(aurb); |
64838171 | 299 | } |
b9dc033c | 300 | |
41c01ee7 GH |
301 | static void do_disconnect(USBHostDevice *s) |
302 | { | |
41c01ee7 GH |
303 | usb_host_close(s); |
304 | usb_host_auto_check(NULL); | |
305 | } | |
306 | ||
64838171 AL |
307 | static void async_complete(void *opaque) |
308 | { | |
309 | USBHostDevice *s = opaque; | |
310 | AsyncURB *aurb; | |
82887262 | 311 | int urbs = 0; |
64838171 AL |
312 | |
313 | while (1) { | |
2791104c | 314 | USBPacket *p; |
b9dc033c | 315 | |
2791104c | 316 | int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb); |
64838171 | 317 | if (r < 0) { |
2791104c | 318 | if (errno == EAGAIN) { |
82887262 | 319 | if (urbs > 2) { |
c32da151 GH |
320 | /* indicates possible latency issues */ |
321 | trace_usb_host_iso_many_urbs(s->bus_num, s->addr, urbs); | |
82887262 | 322 | } |
64838171 | 323 | return; |
2791104c | 324 | } |
40197c35 GH |
325 | if (errno == ENODEV) { |
326 | if (!s->closing) { | |
327 | trace_usb_host_disconnect(s->bus_num, s->addr); | |
328 | do_disconnect(s); | |
329 | } | |
64838171 AL |
330 | return; |
331 | } | |
332 | ||
e6a2f500 | 333 | perror("USBDEVFS_REAPURBNDELAY"); |
64838171 | 334 | return; |
b9dc033c | 335 | } |
64838171 | 336 | |
2791104c | 337 | DPRINTF("husb: async completed. aurb %p status %d alen %d\n", |
64838171 AL |
338 | aurb, aurb->urb.status, aurb->urb.actual_length); |
339 | ||
060dc841 HG |
340 | /* If this is a buffered iso urb mark it as complete and don't do |
341 | anything else (it is handled further in usb_host_handle_iso_data) */ | |
342 | if (aurb->iso_frame_idx == -1) { | |
82887262 | 343 | int inflight; |
c0e5750b GH |
344 | int pid = (aurb->urb.endpoint & USB_DIR_IN) ? |
345 | USB_TOKEN_IN : USB_TOKEN_OUT; | |
346 | int ep = aurb->urb.endpoint & 0xf; | |
060dc841 | 347 | if (aurb->urb.status == -EPIPE) { |
c0e5750b | 348 | set_halt(s, pid, ep); |
060dc841 HG |
349 | } |
350 | aurb->iso_frame_idx = 0; | |
82887262 | 351 | urbs++; |
c0e5750b GH |
352 | inflight = change_iso_inflight(s, pid, ep, -1); |
353 | if (inflight == 0 && is_iso_started(s, pid, ep)) { | |
c32da151 GH |
354 | /* can be latency issues, or simply end of stream */ |
355 | trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, ep); | |
82887262 | 356 | } |
060dc841 HG |
357 | continue; |
358 | } | |
359 | ||
360 | p = aurb->packet; | |
e6a2f500 GH |
361 | trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status, |
362 | aurb->urb.actual_length, aurb->more); | |
060dc841 | 363 | |
2791104c | 364 | if (p) { |
64838171 AL |
365 | switch (aurb->urb.status) { |
366 | case 0: | |
71e0aa39 GH |
367 | p->actual_length += aurb->urb.actual_length; |
368 | if (!aurb->more) { | |
369 | /* Clear previous ASYNC status */ | |
370 | p->status = USB_RET_SUCCESS; | |
371 | } | |
64838171 AL |
372 | break; |
373 | ||
374 | case -EPIPE: | |
079d0b7f | 375 | set_halt(s, p->pid, p->ep->nr); |
9a77a0f5 | 376 | p->status = USB_RET_STALL; |
2791104c | 377 | break; |
dcc7e25f | 378 | |
4d819a9b | 379 | case -EOVERFLOW: |
9a77a0f5 | 380 | p->status = USB_RET_BABBLE; |
4d819a9b HG |
381 | break; |
382 | ||
64838171 | 383 | default: |
9a77a0f5 | 384 | p->status = USB_RET_IOERROR; |
64838171 AL |
385 | break; |
386 | } | |
387 | ||
50b7963e | 388 | if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) { |
8c908fca GH |
389 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
390 | p->status, aurb->urb.actual_length); | |
50b7963e | 391 | usb_generic_async_ctrl_complete(&s->dev, p); |
71138531 | 392 | } else if (!aurb->more) { |
8c908fca GH |
393 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
394 | p->status, aurb->urb.actual_length); | |
50b7963e HG |
395 | usb_packet_complete(&s->dev, p); |
396 | } | |
2791104c | 397 | } |
64838171 AL |
398 | |
399 | async_free(aurb); | |
b9dc033c | 400 | } |
b9dc033c AZ |
401 | } |
402 | ||
eb5e680a | 403 | static void usb_host_async_cancel(USBDevice *dev, USBPacket *p) |
b9dc033c | 404 | { |
eb5e680a | 405 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
227ebeb5 | 406 | AsyncURB *aurb; |
b9dc033c | 407 | |
19b89252 | 408 | trace_usb_host_req_canceled(s->bus_num, s->addr, p); |
6aebe407 | 409 | |
227ebeb5 GH |
410 | QLIST_FOREACH(aurb, &s->aurbs, next) { |
411 | if (p != aurb->packet) { | |
412 | continue; | |
413 | } | |
64838171 | 414 | |
6aebe407 | 415 | trace_usb_host_urb_canceled(s->bus_num, s->addr, aurb); |
b9dc033c | 416 | |
227ebeb5 GH |
417 | /* Mark it as dead (see async_complete above) */ |
418 | aurb->packet = NULL; | |
419 | ||
420 | int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb); | |
421 | if (r < 0) { | |
422 | DPRINTF("husb: async. discard urb failed errno %d\n", errno); | |
423 | } | |
b9dc033c | 424 | } |
b9dc033c AZ |
425 | } |
426 | ||
097db438 GH |
427 | static int usb_host_open_device(int bus, int addr) |
428 | { | |
429 | const char *usbfs = NULL; | |
430 | char filename[32]; | |
431 | struct stat st; | |
432 | int fd, rc; | |
433 | ||
434 | rc = stat("/dev/bus/usb", &st); | |
435 | if (rc == 0 && S_ISDIR(st.st_mode)) { | |
436 | /* udev-created device nodes available */ | |
437 | usbfs = "/dev/bus/usb"; | |
438 | } else { | |
439 | /* fallback: usbfs mounted below /proc */ | |
440 | usbfs = "/proc/bus/usb"; | |
441 | } | |
442 | ||
443 | snprintf(filename, sizeof(filename), "%s/%03d/%03d", | |
444 | usbfs, bus, addr); | |
445 | fd = open(filename, O_RDWR | O_NONBLOCK); | |
446 | if (fd < 0) { | |
447 | fprintf(stderr, "husb: open %s: %s\n", filename, strerror(errno)); | |
448 | } | |
449 | return fd; | |
450 | } | |
451 | ||
e6274727 GH |
452 | static int usb_host_claim_port(USBHostDevice *s) |
453 | { | |
454 | #ifdef USBDEVFS_CLAIM_PORT | |
455 | char *h, hub_name[64], line[1024]; | |
c75fead6 | 456 | int hub_addr, ret; |
e6274727 GH |
457 | |
458 | snprintf(hub_name, sizeof(hub_name), "%d-%s", | |
459 | s->match.bus_num, s->match.port); | |
460 | ||
461 | /* try strip off last ".$portnr" to get hub */ | |
462 | h = strrchr(hub_name, '.'); | |
463 | if (h != NULL) { | |
c75fead6 | 464 | s->hub_port = atoi(h+1); |
e6274727 GH |
465 | *h = '\0'; |
466 | } else { | |
467 | /* no dot in there -> it is the root hub */ | |
468 | snprintf(hub_name, sizeof(hub_name), "usb%d", | |
469 | s->match.bus_num); | |
c75fead6 | 470 | s->hub_port = atoi(s->match.port); |
e6274727 GH |
471 | } |
472 | ||
473 | if (!usb_host_read_file(line, sizeof(line), "devnum", | |
474 | hub_name)) { | |
475 | return -1; | |
476 | } | |
477 | if (sscanf(line, "%d", &hub_addr) != 1) { | |
478 | return -1; | |
479 | } | |
480 | ||
097db438 | 481 | s->hub_fd = usb_host_open_device(s->match.bus_num, hub_addr); |
e6274727 GH |
482 | if (s->hub_fd < 0) { |
483 | return -1; | |
484 | } | |
485 | ||
c75fead6 | 486 | ret = ioctl(s->hub_fd, USBDEVFS_CLAIM_PORT, &s->hub_port); |
e6274727 GH |
487 | if (ret < 0) { |
488 | close(s->hub_fd); | |
489 | s->hub_fd = -1; | |
490 | return -1; | |
491 | } | |
492 | ||
c75fead6 | 493 | trace_usb_host_claim_port(s->match.bus_num, hub_addr, s->hub_port); |
e6274727 GH |
494 | return 0; |
495 | #else | |
496 | return -1; | |
497 | #endif | |
498 | } | |
499 | ||
c75fead6 GH |
500 | static void usb_host_release_port(USBHostDevice *s) |
501 | { | |
502 | if (s->hub_fd == -1) { | |
503 | return; | |
504 | } | |
505 | #ifdef USBDEVFS_RELEASE_PORT | |
506 | ioctl(s->hub_fd, USBDEVFS_RELEASE_PORT, &s->hub_port); | |
507 | #endif | |
508 | close(s->hub_fd); | |
509 | s->hub_fd = -1; | |
510 | } | |
511 | ||
e6274727 GH |
512 | static int usb_host_disconnect_ifaces(USBHostDevice *dev, int nb_interfaces) |
513 | { | |
514 | /* earlier Linux 2.4 do not support that */ | |
515 | #ifdef USBDEVFS_DISCONNECT | |
516 | struct usbdevfs_ioctl ctrl; | |
517 | int ret, interface; | |
518 | ||
519 | for (interface = 0; interface < nb_interfaces; interface++) { | |
520 | ctrl.ioctl_code = USBDEVFS_DISCONNECT; | |
521 | ctrl.ifno = interface; | |
522 | ctrl.data = 0; | |
523 | ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl); | |
524 | if (ret < 0 && errno != ENODATA) { | |
525 | perror("USBDEVFS_DISCONNECT"); | |
526 | return -1; | |
527 | } | |
528 | } | |
529 | #endif | |
530 | return 0; | |
531 | } | |
532 | ||
0fcc3bfc GH |
533 | static int usb_linux_get_num_interfaces(USBHostDevice *s) |
534 | { | |
535 | char device_name[64], line[1024]; | |
536 | int num_interfaces = 0; | |
537 | ||
0fcc3bfc GH |
538 | sprintf(device_name, "%d-%s", s->bus_num, s->port); |
539 | if (!usb_host_read_file(line, sizeof(line), "bNumInterfaces", | |
540 | device_name)) { | |
541 | return -1; | |
542 | } | |
543 | if (sscanf(line, "%d", &num_interfaces) != 1) { | |
544 | return -1; | |
545 | } | |
546 | return num_interfaces; | |
547 | } | |
548 | ||
446ab128 | 549 | static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) |
b9dc033c | 550 | { |
41c01ee7 | 551 | const char *op = NULL; |
b9dc033c | 552 | int dev_descr_len, config_descr_len; |
d4c4e6fd | 553 | int interface, nb_interfaces; |
b9dc033c AZ |
554 | int ret, i; |
555 | ||
1de14d43 GH |
556 | for (i = 0; i < USB_MAX_INTERFACES; i++) { |
557 | dev->dev.altsetting[i] = 0; | |
558 | } | |
559 | ||
eb7700bb | 560 | if (configuration == 0) { /* address state - ignore */ |
65360511 GH |
561 | dev->dev.ninterfaces = 0; |
562 | dev->dev.configuration = 0; | |
b9dc033c | 563 | return 1; |
eb7700bb | 564 | } |
b9dc033c | 565 | |
d0f2c4c6 | 566 | DPRINTF("husb: claiming interfaces. config %d\n", configuration); |
446ab128 | 567 | |
b9dc033c AZ |
568 | i = 0; |
569 | dev_descr_len = dev->descr[0]; | |
2791104c | 570 | if (dev_descr_len > dev->descr_len) { |
61c1117f HG |
571 | fprintf(stderr, "husb: update iface failed. descr too short\n"); |
572 | return 0; | |
2791104c | 573 | } |
b9dc033c AZ |
574 | |
575 | i += dev_descr_len; | |
576 | while (i < dev->descr_len) { | |
2791104c DA |
577 | DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n", |
578 | i, dev->descr_len, | |
b9dc033c | 579 | dev->descr[i], dev->descr[i+1]); |
64838171 | 580 | |
b9dc033c AZ |
581 | if (dev->descr[i+1] != USB_DT_CONFIG) { |
582 | i += dev->descr[i]; | |
583 | continue; | |
584 | } | |
585 | config_descr_len = dev->descr[i]; | |
586 | ||
e6a2f500 | 587 | DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration); |
1f3870ab | 588 | |
eb7700bb | 589 | if (configuration == dev->descr[i + 5]) { |
446ab128 | 590 | configuration = dev->descr[i + 5]; |
b9dc033c | 591 | break; |
446ab128 | 592 | } |
b9dc033c AZ |
593 | |
594 | i += config_descr_len; | |
595 | } | |
596 | ||
597 | if (i >= dev->descr_len) { | |
2791104c DA |
598 | fprintf(stderr, |
599 | "husb: update iface failed. no matching configuration\n"); | |
61c1117f | 600 | return 0; |
b9dc033c AZ |
601 | } |
602 | nb_interfaces = dev->descr[i + 4]; | |
603 | ||
e6274727 GH |
604 | if (usb_host_disconnect_ifaces(dev, nb_interfaces) < 0) { |
605 | goto fail; | |
b9dc033c | 606 | } |
b9dc033c AZ |
607 | |
608 | /* XXX: only grab if all interfaces are free */ | |
609 | for (interface = 0; interface < nb_interfaces; interface++) { | |
41c01ee7 | 610 | op = "USBDEVFS_CLAIMINTERFACE"; |
b9dc033c AZ |
611 | ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface); |
612 | if (ret < 0) { | |
41c01ee7 | 613 | goto fail; |
b9dc033c AZ |
614 | } |
615 | } | |
616 | ||
e6a2f500 GH |
617 | trace_usb_host_claim_interfaces(dev->bus_num, dev->addr, |
618 | nb_interfaces, configuration); | |
b9dc033c | 619 | |
65360511 GH |
620 | dev->dev.ninterfaces = nb_interfaces; |
621 | dev->dev.configuration = configuration; | |
446ab128 | 622 | return 1; |
41c01ee7 GH |
623 | |
624 | fail: | |
625 | if (errno == ENODEV) { | |
626 | do_disconnect(dev); | |
627 | } | |
628 | perror(op); | |
629 | return 0; | |
446ab128 AL |
630 | } |
631 | ||
632 | static int usb_host_release_interfaces(USBHostDevice *s) | |
633 | { | |
634 | int ret, i; | |
635 | ||
e6a2f500 | 636 | trace_usb_host_release_interfaces(s->bus_num, s->addr); |
446ab128 | 637 | |
65360511 | 638 | for (i = 0; i < s->dev.ninterfaces; i++) { |
446ab128 AL |
639 | ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i); |
640 | if (ret < 0) { | |
e6a2f500 | 641 | perror("USBDEVFS_RELEASEINTERFACE"); |
446ab128 AL |
642 | return 0; |
643 | } | |
644 | } | |
b9dc033c AZ |
645 | return 1; |
646 | } | |
647 | ||
059809e4 | 648 | static void usb_host_handle_reset(USBDevice *dev) |
bb36d470 | 649 | { |
26a9e82a | 650 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
64838171 | 651 | |
e6a2f500 | 652 | trace_usb_host_reset(s->bus_num, s->addr); |
64838171 | 653 | |
c7e775e4 | 654 | usb_host_do_reset(s); |
446ab128 | 655 | |
eb7700bb | 656 | usb_host_claim_interfaces(s, 0); |
9b87e19b | 657 | usb_linux_update_endp_table(s); |
5fafdf24 | 658 | } |
bb36d470 | 659 | |
059809e4 FB |
660 | static void usb_host_handle_destroy(USBDevice *dev) |
661 | { | |
662 | USBHostDevice *s = (USBHostDevice *)dev; | |
663 | ||
c75fead6 | 664 | usb_host_release_port(s); |
26a9e82a GH |
665 | usb_host_close(s); |
666 | QTAILQ_REMOVE(&hostdevs, s, next); | |
b373a63a | 667 | qemu_remove_exit_notifier(&s->exit); |
059809e4 FB |
668 | } |
669 | ||
060dc841 HG |
670 | /* iso data is special, we need to keep enough urbs in flight to make sure |
671 | that the controller never runs out of them, otherwise the device will | |
672 | likely suffer a buffer underrun / overrun. */ | |
c0e5750b | 673 | static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, int pid, uint8_t ep) |
060dc841 HG |
674 | { |
675 | AsyncURB *aurb; | |
f003397c | 676 | int i, j, len = usb_ep_get_max_packet_size(&s->dev, pid, ep); |
060dc841 | 677 | |
7267c094 | 678 | aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb)); |
b81bcd8a | 679 | for (i = 0; i < s->iso_urb_count; i++) { |
060dc841 HG |
680 | aurb[i].urb.endpoint = ep; |
681 | aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len; | |
7267c094 | 682 | aurb[i].urb.buffer = g_malloc(aurb[i].urb.buffer_length); |
060dc841 HG |
683 | aurb[i].urb.type = USBDEVFS_URB_TYPE_ISO; |
684 | aurb[i].urb.flags = USBDEVFS_URB_ISO_ASAP; | |
685 | aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB; | |
686 | for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++) | |
687 | aurb[i].urb.iso_frame_desc[j].length = len; | |
c0e5750b | 688 | if (pid == USB_TOKEN_IN) { |
060dc841 HG |
689 | aurb[i].urb.endpoint |= 0x80; |
690 | /* Mark as fully consumed (idle) */ | |
691 | aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB; | |
692 | } | |
693 | } | |
c0e5750b | 694 | set_iso_urb(s, pid, ep, aurb); |
060dc841 HG |
695 | |
696 | return aurb; | |
697 | } | |
698 | ||
c0e5750b | 699 | static void usb_host_stop_n_free_iso(USBHostDevice *s, int pid, uint8_t ep) |
060dc841 HG |
700 | { |
701 | AsyncURB *aurb; | |
702 | int i, ret, killed = 0, free = 1; | |
703 | ||
c0e5750b | 704 | aurb = get_iso_urb(s, pid, ep); |
060dc841 HG |
705 | if (!aurb) { |
706 | return; | |
707 | } | |
708 | ||
b81bcd8a | 709 | for (i = 0; i < s->iso_urb_count; i++) { |
060dc841 HG |
710 | /* in flight? */ |
711 | if (aurb[i].iso_frame_idx == -1) { | |
712 | ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]); | |
713 | if (ret < 0) { | |
e6a2f500 | 714 | perror("USBDEVFS_DISCARDURB"); |
060dc841 HG |
715 | free = 0; |
716 | continue; | |
717 | } | |
718 | killed++; | |
719 | } | |
720 | } | |
721 | ||
722 | /* Make sure any urbs we've killed are reaped before we free them */ | |
723 | if (killed) { | |
724 | async_complete(s); | |
725 | } | |
726 | ||
b81bcd8a | 727 | for (i = 0; i < s->iso_urb_count; i++) { |
7267c094 | 728 | g_free(aurb[i].urb.buffer); |
060dc841 HG |
729 | } |
730 | ||
731 | if (free) | |
7267c094 | 732 | g_free(aurb); |
060dc841 HG |
733 | else |
734 | printf("husb: leaking iso urbs because of discard failure\n"); | |
c0e5750b GH |
735 | set_iso_urb(s, pid, ep, NULL); |
736 | set_iso_urb_idx(s, pid, ep, 0); | |
737 | clear_iso_started(s, pid, ep); | |
060dc841 HG |
738 | } |
739 | ||
9a77a0f5 | 740 | static void urb_status_to_usb_ret(int status, USBPacket *p) |
060dc841 HG |
741 | { |
742 | switch (status) { | |
743 | case -EPIPE: | |
9a77a0f5 HG |
744 | p->status = USB_RET_STALL; |
745 | break; | |
4d819a9b | 746 | case -EOVERFLOW: |
9a77a0f5 HG |
747 | p->status = USB_RET_BABBLE; |
748 | break; | |
060dc841 | 749 | default: |
9a77a0f5 | 750 | p->status = USB_RET_IOERROR; |
060dc841 HG |
751 | } |
752 | } | |
753 | ||
9a77a0f5 | 754 | static void usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in) |
060dc841 HG |
755 | { |
756 | AsyncURB *aurb; | |
9a77a0f5 | 757 | int i, j, max_packet_size, offset, len; |
4f4321c1 | 758 | uint8_t *buf; |
975f2998 | 759 | |
079d0b7f | 760 | max_packet_size = p->ep->max_packet_size; |
9a77a0f5 HG |
761 | if (max_packet_size == 0) { |
762 | p->status = USB_RET_NAK; | |
763 | return; | |
764 | } | |
060dc841 | 765 | |
079d0b7f | 766 | aurb = get_iso_urb(s, p->pid, p->ep->nr); |
060dc841 | 767 | if (!aurb) { |
079d0b7f | 768 | aurb = usb_host_alloc_iso(s, p->pid, p->ep->nr); |
060dc841 HG |
769 | } |
770 | ||
079d0b7f | 771 | i = get_iso_urb_idx(s, p->pid, p->ep->nr); |
060dc841 HG |
772 | j = aurb[i].iso_frame_idx; |
773 | if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) { | |
bb6d5498 HG |
774 | if (in) { |
775 | /* Check urb status */ | |
776 | if (aurb[i].urb.status) { | |
9a77a0f5 | 777 | urb_status_to_usb_ret(aurb[i].urb.status, p); |
bb6d5498 HG |
778 | /* Move to the next urb */ |
779 | aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1; | |
780 | /* Check frame status */ | |
781 | } else if (aurb[i].urb.iso_frame_desc[j].status) { | |
9a77a0f5 | 782 | urb_status_to_usb_ret(aurb[i].urb.iso_frame_desc[j].status, p); |
bb6d5498 | 783 | /* Check the frame fits */ |
4f4321c1 GH |
784 | } else if (aurb[i].urb.iso_frame_desc[j].actual_length |
785 | > p->iov.size) { | |
bb6d5498 | 786 | printf("husb: received iso data is larger then packet\n"); |
9a77a0f5 | 787 | p->status = USB_RET_BABBLE; |
bb6d5498 HG |
788 | /* All good copy data over */ |
789 | } else { | |
790 | len = aurb[i].urb.iso_frame_desc[j].actual_length; | |
4f4321c1 GH |
791 | buf = aurb[i].urb.buffer + |
792 | j * aurb[i].urb.iso_frame_desc[0].length; | |
793 | usb_packet_copy(p, buf, len); | |
bb6d5498 | 794 | } |
060dc841 | 795 | } else { |
4f4321c1 | 796 | len = p->iov.size; |
079d0b7f | 797 | offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->pid, p->ep->nr); |
bb6d5498 HG |
798 | |
799 | /* Check the frame fits */ | |
800 | if (len > max_packet_size) { | |
801 | printf("husb: send iso data is larger then max packet size\n"); | |
9a77a0f5 HG |
802 | p->status = USB_RET_NAK; |
803 | return; | |
bb6d5498 HG |
804 | } |
805 | ||
806 | /* All good copy data over */ | |
4f4321c1 | 807 | usb_packet_copy(p, aurb[i].urb.buffer + offset, len); |
bb6d5498 HG |
808 | aurb[i].urb.iso_frame_desc[j].length = len; |
809 | offset += len; | |
079d0b7f | 810 | set_iso_buffer_used(s, p->pid, p->ep->nr, offset); |
bb6d5498 HG |
811 | |
812 | /* Start the stream once we have buffered enough data */ | |
079d0b7f GH |
813 | if (!is_iso_started(s, p->pid, p->ep->nr) && i == 1 && j == 8) { |
814 | set_iso_started(s, p->pid, p->ep->nr); | |
bb6d5498 | 815 | } |
060dc841 HG |
816 | } |
817 | aurb[i].iso_frame_idx++; | |
818 | if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) { | |
b81bcd8a | 819 | i = (i + 1) % s->iso_urb_count; |
079d0b7f | 820 | set_iso_urb_idx(s, p->pid, p->ep->nr, i); |
060dc841 | 821 | } |
bb6d5498 HG |
822 | } else { |
823 | if (in) { | |
079d0b7f | 824 | set_iso_started(s, p->pid, p->ep->nr); |
bb6d5498 HG |
825 | } else { |
826 | DPRINTF("hubs: iso out error no free buffer, dropping packet\n"); | |
827 | } | |
060dc841 HG |
828 | } |
829 | ||
079d0b7f | 830 | if (is_iso_started(s, p->pid, p->ep->nr)) { |
bb6d5498 | 831 | /* (Re)-submit all fully consumed / filled urbs */ |
b81bcd8a | 832 | for (i = 0; i < s->iso_urb_count; i++) { |
bb6d5498 | 833 | if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) { |
9a77a0f5 | 834 | if (ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]) < 0) { |
e6a2f500 | 835 | perror("USBDEVFS_SUBMITURB"); |
9a77a0f5 | 836 | if (!in || p->status == USB_RET_SUCCESS) { |
bb6d5498 HG |
837 | switch(errno) { |
838 | case ETIMEDOUT: | |
9a77a0f5 | 839 | p->status = USB_RET_NAK; |
0225e254 | 840 | break; |
bb6d5498 HG |
841 | case EPIPE: |
842 | default: | |
9a77a0f5 | 843 | p->status = USB_RET_STALL; |
bb6d5498 | 844 | } |
060dc841 | 845 | } |
bb6d5498 | 846 | break; |
060dc841 | 847 | } |
bb6d5498 | 848 | aurb[i].iso_frame_idx = -1; |
079d0b7f | 849 | change_iso_inflight(s, p->pid, p->ep->nr, 1); |
060dc841 | 850 | } |
060dc841 HG |
851 | } |
852 | } | |
060dc841 HG |
853 | } |
854 | ||
9a77a0f5 | 855 | static void usb_host_handle_data(USBDevice *dev, USBPacket *p) |
bb36d470 | 856 | { |
50b7963e | 857 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
64838171 | 858 | struct usbdevfs_urb *urb; |
446ab128 | 859 | AsyncURB *aurb; |
b621bab4 | 860 | int ret, rem, prem, v; |
71138531 | 861 | uint8_t *pbuf; |
060dc841 | 862 | uint8_t ep; |
b9dc033c | 863 | |
19b89252 | 864 | trace_usb_host_req_data(s->bus_num, s->addr, p, |
e6a2f500 | 865 | p->pid == USB_TOKEN_IN, |
079d0b7f | 866 | p->ep->nr, p->iov.size); |
e6a2f500 | 867 | |
079d0b7f | 868 | if (!is_valid(s, p->pid, p->ep->nr)) { |
9a77a0f5 | 869 | p->status = USB_RET_NAK; |
8c908fca GH |
870 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
871 | p->status, p->actual_length); | |
9a77a0f5 | 872 | return; |
a0b5fece HG |
873 | } |
874 | ||
2791104c | 875 | if (p->pid == USB_TOKEN_IN) { |
079d0b7f | 876 | ep = p->ep->nr | 0x80; |
2791104c | 877 | } else { |
079d0b7f | 878 | ep = p->ep->nr; |
2791104c | 879 | } |
64838171 | 880 | |
079d0b7f | 881 | if (is_halted(s, p->pid, p->ep->nr)) { |
9b87e19b GH |
882 | unsigned int arg = ep; |
883 | ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg); | |
64838171 | 884 | if (ret < 0) { |
e6a2f500 | 885 | perror("USBDEVFS_CLEAR_HALT"); |
9a77a0f5 | 886 | p->status = USB_RET_NAK; |
8c908fca GH |
887 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
888 | p->status, p->actual_length); | |
9a77a0f5 | 889 | return; |
bb36d470 | 890 | } |
079d0b7f | 891 | clear_halt(s, p->pid, p->ep->nr); |
4d043a09 AZ |
892 | } |
893 | ||
079d0b7f | 894 | if (is_isoc(s, p->pid, p->ep->nr)) { |
9a77a0f5 HG |
895 | usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN); |
896 | return; | |
bb6d5498 | 897 | } |
060dc841 | 898 | |
b621bab4 | 899 | v = 0; |
818d59dc GH |
900 | prem = 0; |
901 | pbuf = NULL; | |
b621bab4 | 902 | rem = p->iov.size; |
0b377169 GH |
903 | do { |
904 | if (prem == 0 && rem > 0) { | |
b621bab4 GH |
905 | assert(v < p->iov.niov); |
906 | prem = p->iov.iov[v].iov_len; | |
907 | pbuf = p->iov.iov[v].iov_base; | |
908 | assert(prem <= rem); | |
818d59dc | 909 | v++; |
b621bab4 | 910 | } |
71138531 GH |
911 | aurb = async_alloc(s); |
912 | aurb->packet = p; | |
913 | ||
914 | urb = &aurb->urb; | |
915 | urb->endpoint = ep; | |
d8e17efd | 916 | urb->type = usb_host_usbfs_type(s, p); |
71138531 GH |
917 | urb->usercontext = s; |
918 | urb->buffer = pbuf; | |
b621bab4 | 919 | urb->buffer_length = prem; |
71138531 | 920 | |
b621bab4 | 921 | if (urb->buffer_length > MAX_USBFS_BUFFER_SIZE) { |
71138531 | 922 | urb->buffer_length = MAX_USBFS_BUFFER_SIZE; |
71138531 GH |
923 | } |
924 | pbuf += urb->buffer_length; | |
b621bab4 | 925 | prem -= urb->buffer_length; |
71138531 | 926 | rem -= urb->buffer_length; |
b621bab4 GH |
927 | if (rem) { |
928 | aurb->more = 1; | |
929 | } | |
b9dc033c | 930 | |
e6a2f500 GH |
931 | trace_usb_host_urb_submit(s->bus_num, s->addr, aurb, |
932 | urb->buffer_length, aurb->more); | |
71138531 | 933 | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
b9dc033c | 934 | |
71138531 GH |
935 | DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n", |
936 | urb->endpoint, urb->buffer_length, aurb->more, p, aurb); | |
b9dc033c | 937 | |
71138531 | 938 | if (ret < 0) { |
e6a2f500 | 939 | perror("USBDEVFS_SUBMITURB"); |
71138531 | 940 | async_free(aurb); |
b9dc033c | 941 | |
71138531 GH |
942 | switch(errno) { |
943 | case ETIMEDOUT: | |
9a77a0f5 | 944 | p->status = USB_RET_NAK; |
8c908fca GH |
945 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
946 | p->status, p->actual_length); | |
9a77a0f5 | 947 | break; |
71138531 GH |
948 | case EPIPE: |
949 | default: | |
9a77a0f5 | 950 | p->status = USB_RET_STALL; |
8c908fca GH |
951 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
952 | p->status, p->actual_length); | |
71138531 | 953 | } |
9a77a0f5 | 954 | return; |
b9dc033c | 955 | } |
0b377169 | 956 | } while (rem > 0); |
64838171 | 957 | |
9a77a0f5 | 958 | p->status = USB_RET_ASYNC; |
b9dc033c AZ |
959 | } |
960 | ||
446ab128 AL |
961 | static int ctrl_error(void) |
962 | { | |
2791104c | 963 | if (errno == ETIMEDOUT) { |
446ab128 | 964 | return USB_RET_NAK; |
2791104c | 965 | } else { |
446ab128 | 966 | return USB_RET_STALL; |
2791104c | 967 | } |
446ab128 AL |
968 | } |
969 | ||
9a77a0f5 | 970 | static void usb_host_set_address(USBHostDevice *s, int addr) |
446ab128 | 971 | { |
e6a2f500 | 972 | trace_usb_host_set_address(s->bus_num, s->addr, addr); |
446ab128 | 973 | s->dev.addr = addr; |
446ab128 AL |
974 | } |
975 | ||
9a77a0f5 | 976 | static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p) |
446ab128 | 977 | { |
0fcc3bfc GH |
978 | int ret, first = 1; |
979 | ||
e6a2f500 GH |
980 | trace_usb_host_set_config(s->bus_num, s->addr, config); |
981 | ||
446ab128 AL |
982 | usb_host_release_interfaces(s); |
983 | ||
0fcc3bfc GH |
984 | again: |
985 | ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config); | |
2791104c | 986 | |
d0f2c4c6 | 987 | DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno); |
2791104c | 988 | |
0fcc3bfc GH |
989 | if (ret < 0 && errno == EBUSY && first) { |
990 | /* happens if usb device is in use by host drivers */ | |
991 | int count = usb_linux_get_num_interfaces(s); | |
992 | if (count > 0) { | |
993 | DPRINTF("husb: busy -> disconnecting %d interfaces\n", count); | |
994 | usb_host_disconnect_ifaces(s, count); | |
995 | first = 0; | |
996 | goto again; | |
997 | } | |
998 | } | |
999 | ||
2791104c | 1000 | if (ret < 0) { |
9a77a0f5 HG |
1001 | p->status = ctrl_error(); |
1002 | return; | |
2791104c | 1003 | } |
446ab128 | 1004 | usb_host_claim_interfaces(s, config); |
eb7700bb | 1005 | usb_linux_update_endp_table(s); |
446ab128 AL |
1006 | } |
1007 | ||
9a77a0f5 HG |
1008 | static void usb_host_set_interface(USBHostDevice *s, int iface, int alt, |
1009 | USBPacket *p) | |
446ab128 AL |
1010 | { |
1011 | struct usbdevfs_setinterface si; | |
060dc841 HG |
1012 | int i, ret; |
1013 | ||
e6a2f500 GH |
1014 | trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt); |
1015 | ||
d8e17efd | 1016 | for (i = 1; i <= USB_MAX_ENDPOINTS; i++) { |
c0e5750b GH |
1017 | if (is_isoc(s, USB_TOKEN_IN, i)) { |
1018 | usb_host_stop_n_free_iso(s, USB_TOKEN_IN, i); | |
1019 | } | |
1020 | if (is_isoc(s, USB_TOKEN_OUT, i)) { | |
1021 | usb_host_stop_n_free_iso(s, USB_TOKEN_OUT, i); | |
060dc841 HG |
1022 | } |
1023 | } | |
446ab128 | 1024 | |
1de14d43 | 1025 | if (iface >= USB_MAX_INTERFACES) { |
9a77a0f5 HG |
1026 | p->status = USB_RET_STALL; |
1027 | return; | |
1de14d43 GH |
1028 | } |
1029 | ||
446ab128 AL |
1030 | si.interface = iface; |
1031 | si.altsetting = alt; | |
1032 | ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si); | |
446ab128 | 1033 | |
2791104c DA |
1034 | DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n", |
1035 | iface, alt, ret, errno); | |
1036 | ||
1037 | if (ret < 0) { | |
9a77a0f5 HG |
1038 | p->status = ctrl_error(); |
1039 | return; | |
2791104c | 1040 | } |
1de14d43 GH |
1041 | |
1042 | s->dev.altsetting[iface] = alt; | |
446ab128 | 1043 | usb_linux_update_endp_table(s); |
446ab128 AL |
1044 | } |
1045 | ||
9a77a0f5 | 1046 | static void usb_host_handle_control(USBDevice *dev, USBPacket *p, |
50b7963e | 1047 | int request, int value, int index, int length, uint8_t *data) |
446ab128 | 1048 | { |
50b7963e | 1049 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
446ab128 AL |
1050 | struct usbdevfs_urb *urb; |
1051 | AsyncURB *aurb; | |
50b7963e | 1052 | int ret; |
446ab128 | 1053 | |
2791104c | 1054 | /* |
446ab128 AL |
1055 | * Process certain standard device requests. |
1056 | * These are infrequent and are processed synchronously. | |
1057 | */ | |
446ab128 | 1058 | |
50b7963e | 1059 | /* Note request is (bRequestType << 8) | bRequest */ |
19b89252 | 1060 | trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index); |
446ab128 | 1061 | |
50b7963e HG |
1062 | switch (request) { |
1063 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: | |
9a77a0f5 HG |
1064 | usb_host_set_address(s, value); |
1065 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); | |
1066 | return; | |
446ab128 | 1067 | |
50b7963e | 1068 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
9a77a0f5 HG |
1069 | usb_host_set_config(s, value & 0xff, p); |
1070 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); | |
1071 | return; | |
446ab128 | 1072 | |
50b7963e | 1073 | case InterfaceOutRequest | USB_REQ_SET_INTERFACE: |
9a77a0f5 HG |
1074 | usb_host_set_interface(s, index, value, p); |
1075 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); | |
1076 | return; | |
a2498f76 GH |
1077 | |
1078 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: | |
1079 | if (value == 0) { /* clear halt */ | |
1080 | int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT; | |
1081 | ioctl(s->fd, USBDEVFS_CLEAR_HALT, &index); | |
1082 | clear_halt(s, pid, index & 0x0f); | |
1083 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, 0); | |
9a77a0f5 | 1084 | return; |
a2498f76 | 1085 | } |
2791104c | 1086 | } |
446ab128 AL |
1087 | |
1088 | /* The rest are asynchronous */ | |
50b7963e HG |
1089 | if (length > sizeof(dev->data_buf)) { |
1090 | fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n", | |
1091 | length, sizeof(dev->data_buf)); | |
9a77a0f5 HG |
1092 | p->status = USB_RET_STALL; |
1093 | return; | |
c4c0e236 JP |
1094 | } |
1095 | ||
7a8fc83f | 1096 | aurb = async_alloc(s); |
446ab128 AL |
1097 | aurb->packet = p; |
1098 | ||
2791104c | 1099 | /* |
446ab128 AL |
1100 | * Setup ctrl transfer. |
1101 | * | |
a0102082 | 1102 | * s->ctrl is laid out such that data buffer immediately follows |
446ab128 | 1103 | * 'req' struct which is exactly what usbdevfs expects. |
2791104c | 1104 | */ |
446ab128 AL |
1105 | urb = &aurb->urb; |
1106 | ||
1107 | urb->type = USBDEVFS_URB_TYPE_CONTROL; | |
079d0b7f | 1108 | urb->endpoint = p->ep->nr; |
446ab128 | 1109 | |
50b7963e HG |
1110 | urb->buffer = &dev->setup_buf; |
1111 | urb->buffer_length = length + 8; | |
446ab128 AL |
1112 | |
1113 | urb->usercontext = s; | |
1114 | ||
e6a2f500 GH |
1115 | trace_usb_host_urb_submit(s->bus_num, s->addr, aurb, |
1116 | urb->buffer_length, aurb->more); | |
446ab128 AL |
1117 | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
1118 | ||
d0f2c4c6 | 1119 | DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb); |
446ab128 AL |
1120 | |
1121 | if (ret < 0) { | |
d0f2c4c6 | 1122 | DPRINTF("husb: submit failed. errno %d\n", errno); |
446ab128 AL |
1123 | async_free(aurb); |
1124 | ||
1125 | switch(errno) { | |
1126 | case ETIMEDOUT: | |
9a77a0f5 HG |
1127 | p->status = USB_RET_NAK; |
1128 | break; | |
446ab128 AL |
1129 | case EPIPE: |
1130 | default: | |
9a77a0f5 HG |
1131 | p->status = USB_RET_STALL; |
1132 | break; | |
446ab128 | 1133 | } |
9a77a0f5 | 1134 | return; |
446ab128 AL |
1135 | } |
1136 | ||
9a77a0f5 | 1137 | p->status = USB_RET_ASYNC; |
446ab128 AL |
1138 | } |
1139 | ||
537e8f1a | 1140 | static void usb_linux_update_endp_table(USBHostDevice *s) |
71d71bbd | 1141 | { |
96dd9aac GH |
1142 | static const char *tname[] = { |
1143 | [USB_ENDPOINT_XFER_CONTROL] = "control", | |
1144 | [USB_ENDPOINT_XFER_ISOC] = "isoc", | |
1145 | [USB_ENDPOINT_XFER_BULK] = "bulk", | |
1146 | [USB_ENDPOINT_XFER_INT] = "int", | |
1147 | }; | |
1148 | uint8_t devep, type; | |
1149 | uint16_t mps, v, p; | |
1150 | int ep, pid; | |
1151 | unsigned int i, configuration = -1, interface = -1, altsetting = -1; | |
c0e5750b | 1152 | struct endp_data *epd; |
96dd9aac GH |
1153 | USBDescriptor *d; |
1154 | bool active = false; | |
71d71bbd | 1155 | |
19deaa08 | 1156 | usb_ep_reset(&s->dev); |
a0b5fece | 1157 | |
96dd9aac GH |
1158 | for (i = 0;; i += d->bLength) { |
1159 | if (i+2 >= s->descr_len) { | |
1160 | break; | |
b9dc033c | 1161 | } |
96dd9aac GH |
1162 | d = (void *)(s->descr + i); |
1163 | if (d->bLength < 2) { | |
1164 | trace_usb_host_parse_error(s->bus_num, s->addr, | |
1165 | "descriptor too short"); | |
537e8f1a | 1166 | return; |
b9dc033c | 1167 | } |
96dd9aac GH |
1168 | if (i + d->bLength > s->descr_len) { |
1169 | trace_usb_host_parse_error(s->bus_num, s->addr, | |
1170 | "descriptor too long"); | |
537e8f1a | 1171 | return; |
2791104c | 1172 | } |
96dd9aac GH |
1173 | switch (d->bDescriptorType) { |
1174 | case 0: | |
1175 | trace_usb_host_parse_error(s->bus_num, s->addr, | |
1176 | "invalid descriptor type"); | |
537e8f1a | 1177 | return; |
96dd9aac GH |
1178 | case USB_DT_DEVICE: |
1179 | if (d->bLength < 0x12) { | |
1180 | trace_usb_host_parse_error(s->bus_num, s->addr, | |
1181 | "device descriptor too short"); | |
537e8f1a | 1182 | return; |
96dd9aac GH |
1183 | } |
1184 | v = (d->u.device.idVendor_hi << 8) | d->u.device.idVendor_lo; | |
1185 | p = (d->u.device.idProduct_hi << 8) | d->u.device.idProduct_lo; | |
1186 | trace_usb_host_parse_device(s->bus_num, s->addr, v, p); | |
b9dc033c | 1187 | break; |
96dd9aac GH |
1188 | case USB_DT_CONFIG: |
1189 | if (d->bLength < 0x09) { | |
1190 | trace_usb_host_parse_error(s->bus_num, s->addr, | |
1191 | "config descriptor too short"); | |
537e8f1a | 1192 | return; |
2791104c | 1193 | } |
96dd9aac GH |
1194 | configuration = d->u.config.bConfigurationValue; |
1195 | active = (configuration == s->dev.configuration); | |
1196 | trace_usb_host_parse_config(s->bus_num, s->addr, | |
1197 | configuration, active); | |
1198 | break; | |
1199 | case USB_DT_INTERFACE: | |
1200 | if (d->bLength < 0x09) { | |
1201 | trace_usb_host_parse_error(s->bus_num, s->addr, | |
1202 | "interface descriptor too short"); | |
537e8f1a | 1203 | return; |
96dd9aac GH |
1204 | } |
1205 | interface = d->u.interface.bInterfaceNumber; | |
1206 | altsetting = d->u.interface.bAlternateSetting; | |
1207 | active = (configuration == s->dev.configuration) && | |
1208 | (altsetting == s->dev.altsetting[interface]); | |
1209 | trace_usb_host_parse_interface(s->bus_num, s->addr, | |
1210 | interface, altsetting, active); | |
1211 | break; | |
1212 | case USB_DT_ENDPOINT: | |
1213 | if (d->bLength < 0x07) { | |
1214 | trace_usb_host_parse_error(s->bus_num, s->addr, | |
1215 | "endpoint descriptor too short"); | |
537e8f1a | 1216 | return; |
96dd9aac GH |
1217 | } |
1218 | devep = d->u.endpoint.bEndpointAddress; | |
c0e5750b GH |
1219 | pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT; |
1220 | ep = devep & 0xf; | |
1221 | if (ep == 0) { | |
96dd9aac GH |
1222 | trace_usb_host_parse_error(s->bus_num, s->addr, |
1223 | "invalid endpoint address"); | |
537e8f1a | 1224 | return; |
130314f8 HG |
1225 | } |
1226 | ||
96dd9aac GH |
1227 | type = d->u.endpoint.bmAttributes & 0x3; |
1228 | mps = d->u.endpoint.wMaxPacketSize_lo | | |
1229 | (d->u.endpoint.wMaxPacketSize_hi << 8); | |
1230 | trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep, | |
1231 | (devep & USB_DIR_IN) ? "in" : "out", | |
1232 | tname[type], active); | |
1233 | ||
1234 | if (active) { | |
1235 | usb_ep_set_max_packet_size(&s->dev, pid, ep, mps); | |
1236 | assert(usb_ep_get_type(&s->dev, pid, ep) == | |
1237 | USB_ENDPOINT_XFER_INVALID); | |
1238 | usb_ep_set_type(&s->dev, pid, ep, type); | |
1239 | usb_ep_set_ifnum(&s->dev, pid, ep, interface); | |
1240 | if ((s->options & (1 << USB_HOST_OPT_PIPELINE)) && | |
6ba43f1f HG |
1241 | (type == USB_ENDPOINT_XFER_BULK) && |
1242 | (pid == USB_TOKEN_OUT)) { | |
96dd9aac GH |
1243 | usb_ep_set_pipeline(&s->dev, pid, ep, true); |
1244 | } | |
d8e17efd | 1245 | |
96dd9aac GH |
1246 | epd = get_endp(s, pid, ep); |
1247 | epd->halted = 0; | |
1248 | } | |
b9dc033c | 1249 | |
96dd9aac GH |
1250 | break; |
1251 | default: | |
1252 | trace_usb_host_parse_unknown(s->bus_num, s->addr, | |
1253 | d->bLength, d->bDescriptorType); | |
1254 | break; | |
b9dc033c AZ |
1255 | } |
1256 | } | |
b9dc033c AZ |
1257 | } |
1258 | ||
e4b17767 HG |
1259 | /* |
1260 | * Check if we can safely redirect a usb2 device to a usb1 virtual controller, | |
1261 | * this function assumes this is safe, if: | |
1262 | * 1) There are no isoc endpoints | |
1263 | * 2) There are no interrupt endpoints with a max_packet_size > 64 | |
1264 | * Note bulk endpoints with a max_packet_size > 64 in theory also are not | |
1265 | * usb1 compatible, but in practice this seems to work fine. | |
1266 | */ | |
1267 | static int usb_linux_full_speed_compat(USBHostDevice *dev) | |
1268 | { | |
1269 | int i, packet_size; | |
1270 | ||
1271 | /* | |
1272 | * usb_linux_update_endp_table only registers info about ep in the current | |
1273 | * interface altsettings, so we need to parse the descriptors again. | |
1274 | */ | |
1275 | for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) { | |
1276 | if (dev->descr[i + 1] == USB_DT_ENDPOINT) { | |
1277 | switch (dev->descr[i + 3] & 0x3) { | |
1278 | case 0x00: /* CONTROL */ | |
1279 | break; | |
1280 | case 0x01: /* ISO */ | |
1281 | return 0; | |
1282 | case 0x02: /* BULK */ | |
1283 | break; | |
1284 | case 0x03: /* INTERRUPT */ | |
1285 | packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8); | |
1286 | if (packet_size > 64) | |
1287 | return 0; | |
1288 | break; | |
1289 | } | |
1290 | } | |
1291 | } | |
1292 | return 1; | |
1293 | } | |
1294 | ||
26a9e82a | 1295 | static int usb_host_open(USBHostDevice *dev, int bus_num, |
ba9acab9 GH |
1296 | int addr, const char *port, |
1297 | const char *prod_name, int speed) | |
bb36d470 | 1298 | { |
b9dc033c | 1299 | int fd = -1, ret; |
1f3870ab | 1300 | |
e6a2f500 GH |
1301 | trace_usb_host_open_started(bus_num, addr); |
1302 | ||
2791104c | 1303 | if (dev->fd != -1) { |
26a9e82a | 1304 | goto fail; |
2791104c | 1305 | } |
3b46e624 | 1306 | |
097db438 | 1307 | fd = usb_host_open_device(bus_num, addr); |
bb36d470 | 1308 | if (fd < 0) { |
1f3870ab | 1309 | goto fail; |
bb36d470 | 1310 | } |
d0f2c4c6 | 1311 | DPRINTF("husb: opened %s\n", buf); |
bb36d470 | 1312 | |
806b6024 GH |
1313 | dev->bus_num = bus_num; |
1314 | dev->addr = addr; | |
03607847 | 1315 | pstrcpy(dev->port, sizeof(dev->port), port); |
22f84e73 | 1316 | dev->fd = fd; |
806b6024 | 1317 | |
b9dc033c AZ |
1318 | /* read the device description */ |
1319 | dev->descr_len = read(fd, dev->descr, sizeof(dev->descr)); | |
1320 | if (dev->descr_len <= 0) { | |
64838171 | 1321 | perror("husb: reading device data failed"); |
bb36d470 FB |
1322 | goto fail; |
1323 | } | |
3b46e624 | 1324 | |
b9dc033c | 1325 | #ifdef DEBUG |
868bfe2b | 1326 | { |
b9dc033c AZ |
1327 | int x; |
1328 | printf("=== begin dumping device descriptor data ===\n"); | |
2791104c | 1329 | for (x = 0; x < dev->descr_len; x++) { |
b9dc033c | 1330 | printf("%02x ", dev->descr[x]); |
2791104c | 1331 | } |
b9dc033c | 1332 | printf("\n=== end dumping device descriptor data ===\n"); |
bb36d470 | 1333 | } |
a594cfbf FB |
1334 | #endif |
1335 | ||
b9dc033c | 1336 | |
eb7700bb GH |
1337 | /* start unconfigured -- we'll wait for the guest to set a configuration */ |
1338 | if (!usb_host_claim_interfaces(dev, 0)) { | |
b9dc033c | 1339 | goto fail; |
2791104c | 1340 | } |
bb36d470 | 1341 | |
19deaa08 | 1342 | usb_ep_init(&dev->dev); |
537e8f1a | 1343 | usb_linux_update_endp_table(dev); |
b9dc033c | 1344 | |
3991c35e HG |
1345 | if (speed == -1) { |
1346 | struct usbdevfs_connectinfo ci; | |
1347 | ||
1348 | ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci); | |
1349 | if (ret < 0) { | |
1350 | perror("usb_host_device_open: USBDEVFS_CONNECTINFO"); | |
1351 | goto fail; | |
1352 | } | |
1353 | ||
1354 | if (ci.slow) { | |
1355 | speed = USB_SPEED_LOW; | |
1356 | } else { | |
1357 | speed = USB_SPEED_HIGH; | |
1358 | } | |
2791104c | 1359 | } |
3991c35e | 1360 | dev->dev.speed = speed; |
ba3f9bfb | 1361 | dev->dev.speedmask = (1 << speed); |
e4b17767 HG |
1362 | if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) { |
1363 | dev->dev.speedmask |= USB_SPEED_MASK_FULL; | |
1364 | } | |
3991c35e | 1365 | |
e6a2f500 | 1366 | trace_usb_host_open_success(bus_num, addr); |
bb36d470 | 1367 | |
2791104c | 1368 | if (!prod_name || prod_name[0] == '\0') { |
0fe6d12e | 1369 | snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc), |
4b096fc9 | 1370 | "host:%d.%d", bus_num, addr); |
2791104c | 1371 | } else { |
0fe6d12e | 1372 | pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc), |
4b096fc9 | 1373 | prod_name); |
2791104c | 1374 | } |
1f6e24e7 | 1375 | |
fa19bf83 HG |
1376 | ret = usb_device_attach(&dev->dev); |
1377 | if (ret) { | |
1378 | goto fail; | |
1379 | } | |
1380 | ||
64838171 AL |
1381 | /* USB devio uses 'write' flag to check for async completions */ |
1382 | qemu_set_fd_handler(dev->fd, NULL, async_complete, dev); | |
1f3870ab | 1383 | |
26a9e82a | 1384 | return 0; |
4b096fc9 | 1385 | |
b9dc033c | 1386 | fail: |
e6a2f500 | 1387 | trace_usb_host_open_failure(bus_num, addr); |
1f45a81b GH |
1388 | if (dev->fd != -1) { |
1389 | close(dev->fd); | |
1390 | dev->fd = -1; | |
2791104c | 1391 | } |
26a9e82a GH |
1392 | return -1; |
1393 | } | |
1394 | ||
1395 | static int usb_host_close(USBHostDevice *dev) | |
1396 | { | |
060dc841 HG |
1397 | int i; |
1398 | ||
39fba3ad | 1399 | if (dev->fd == -1) { |
26a9e82a | 1400 | return -1; |
2791104c | 1401 | } |
26a9e82a | 1402 | |
e6a2f500 GH |
1403 | trace_usb_host_close(dev->bus_num, dev->addr); |
1404 | ||
26a9e82a GH |
1405 | qemu_set_fd_handler(dev->fd, NULL, NULL, NULL); |
1406 | dev->closing = 1; | |
d8e17efd | 1407 | for (i = 1; i <= USB_MAX_ENDPOINTS; i++) { |
c0e5750b GH |
1408 | if (is_isoc(dev, USB_TOKEN_IN, i)) { |
1409 | usb_host_stop_n_free_iso(dev, USB_TOKEN_IN, i); | |
1410 | } | |
1411 | if (is_isoc(dev, USB_TOKEN_OUT, i)) { | |
1412 | usb_host_stop_n_free_iso(dev, USB_TOKEN_OUT, i); | |
060dc841 HG |
1413 | } |
1414 | } | |
26a9e82a GH |
1415 | async_complete(dev); |
1416 | dev->closing = 0; | |
39fba3ad GH |
1417 | if (dev->dev.attached) { |
1418 | usb_device_detach(&dev->dev); | |
1419 | } | |
c7662daa | 1420 | usb_host_do_reset(dev); |
26a9e82a GH |
1421 | close(dev->fd); |
1422 | dev->fd = -1; | |
1423 | return 0; | |
1424 | } | |
1425 | ||
9e8dd451 | 1426 | static void usb_host_exit_notifier(struct Notifier *n, void *data) |
b373a63a SH |
1427 | { |
1428 | USBHostDevice *s = container_of(n, USBHostDevice, exit); | |
1429 | ||
c75fead6 | 1430 | usb_host_release_port(s); |
b373a63a | 1431 | if (s->fd != -1) { |
c7e775e4 | 1432 | usb_host_do_reset(s); |
b373a63a SH |
1433 | } |
1434 | } | |
1435 | ||
a229c053 GH |
1436 | /* |
1437 | * This is *NOT* about restoring state. We have absolutely no idea | |
1438 | * what state the host device is in at the moment and whenever it is | |
1439 | * still present in the first place. Attemping to contine where we | |
1440 | * left off is impossible. | |
1441 | * | |
1442 | * What we are going to to to here is emulate a surprise removal of | |
1443 | * the usb device passed through, then kick host scan so the device | |
1444 | * will get re-attached (and re-initialized by the guest) in case it | |
1445 | * is still present. | |
1446 | * | |
1447 | * As the device removal will change the state of other devices (usb | |
1448 | * host controller, most likely interrupt controller too) we have to | |
1449 | * wait with it until *all* vmstate is loaded. Thus post_load just | |
1450 | * kicks a bottom half which then does the actual work. | |
1451 | */ | |
1452 | static void usb_host_post_load_bh(void *opaque) | |
1453 | { | |
1454 | USBHostDevice *dev = opaque; | |
1455 | ||
1456 | if (dev->fd != -1) { | |
1457 | usb_host_close(dev); | |
1458 | } | |
1459 | if (dev->dev.attached) { | |
1460 | usb_device_detach(&dev->dev); | |
1461 | } | |
1462 | usb_host_auto_check(NULL); | |
1463 | } | |
1464 | ||
1465 | static int usb_host_post_load(void *opaque, int version_id) | |
1466 | { | |
1467 | USBHostDevice *dev = opaque; | |
1468 | ||
1469 | qemu_bh_schedule(dev->bh); | |
1470 | return 0; | |
1471 | } | |
1472 | ||
26a9e82a GH |
1473 | static int usb_host_initfn(USBDevice *dev) |
1474 | { | |
1475 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); | |
1476 | ||
be41efde | 1477 | dev->flags |= (1 << USB_DEV_FLAG_IS_HOST); |
26a9e82a GH |
1478 | dev->auto_attach = 0; |
1479 | s->fd = -1; | |
9516bb47 GH |
1480 | s->hub_fd = -1; |
1481 | ||
26a9e82a | 1482 | QTAILQ_INSERT_TAIL(&hostdevs, s, next); |
b373a63a SH |
1483 | s->exit.notify = usb_host_exit_notifier; |
1484 | qemu_add_exit_notifier(&s->exit); | |
a229c053 | 1485 | s->bh = qemu_bh_new(usb_host_post_load_bh, s); |
26a9e82a | 1486 | usb_host_auto_check(NULL); |
9516bb47 | 1487 | |
9516bb47 | 1488 | if (s->match.bus_num != 0 && s->match.port != NULL) { |
e6274727 | 1489 | usb_host_claim_port(s); |
9516bb47 | 1490 | } |
65bb3a5c | 1491 | add_boot_device_path(s->bootindex, &dev->qdev, NULL); |
26a9e82a | 1492 | return 0; |
a594cfbf | 1493 | } |
bb36d470 | 1494 | |
d6791578 | 1495 | static const VMStateDescription vmstate_usb_host = { |
2b2325ff | 1496 | .name = DEVNAME, |
a229c053 GH |
1497 | .version_id = 1, |
1498 | .minimum_version_id = 1, | |
1499 | .post_load = usb_host_post_load, | |
1500 | .fields = (VMStateField[]) { | |
1501 | VMSTATE_USB_DEVICE(dev, USBHostDevice), | |
1502 | VMSTATE_END_OF_LIST() | |
1503 | } | |
d6791578 GH |
1504 | }; |
1505 | ||
39bffca2 AL |
1506 | static Property usb_host_dev_properties[] = { |
1507 | DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0), | |
1508 | DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0), | |
1509 | DEFINE_PROP_STRING("hostport", USBHostDevice, match.port), | |
1510 | DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0), | |
1511 | DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0), | |
1512 | DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4), | |
65bb3a5c | 1513 | DEFINE_PROP_INT32("bootindex", USBHostDevice, bootindex, -1), |
39c20577 GH |
1514 | DEFINE_PROP_BIT("pipeline", USBHostDevice, options, |
1515 | USB_HOST_OPT_PIPELINE, true), | |
39bffca2 AL |
1516 | DEFINE_PROP_END_OF_LIST(), |
1517 | }; | |
1518 | ||
62aed765 AL |
1519 | static void usb_host_class_initfn(ObjectClass *klass, void *data) |
1520 | { | |
39bffca2 | 1521 | DeviceClass *dc = DEVICE_CLASS(klass); |
62aed765 AL |
1522 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
1523 | ||
1524 | uc->init = usb_host_initfn; | |
1525 | uc->product_desc = "USB Host Device"; | |
62aed765 AL |
1526 | uc->cancel_packet = usb_host_async_cancel; |
1527 | uc->handle_data = usb_host_handle_data; | |
1528 | uc->handle_control = usb_host_handle_control; | |
1529 | uc->handle_reset = usb_host_handle_reset; | |
1530 | uc->handle_destroy = usb_host_handle_destroy; | |
39bffca2 AL |
1531 | dc->vmsd = &vmstate_usb_host; |
1532 | dc->props = usb_host_dev_properties; | |
125ee0ed | 1533 | set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); |
62aed765 AL |
1534 | } |
1535 | ||
8c43a6f0 | 1536 | static const TypeInfo usb_host_dev_info = { |
2b2325ff | 1537 | .name = DEVNAME, |
39bffca2 AL |
1538 | .parent = TYPE_USB_DEVICE, |
1539 | .instance_size = sizeof(USBHostDevice), | |
1540 | .class_init = usb_host_class_initfn, | |
806b6024 GH |
1541 | }; |
1542 | ||
83f7d43a | 1543 | static void usb_host_register_types(void) |
806b6024 | 1544 | { |
39bffca2 | 1545 | type_register_static(&usb_host_dev_info); |
806b6024 | 1546 | } |
83f7d43a AF |
1547 | |
1548 | type_init(usb_host_register_types) | |
806b6024 | 1549 | |
0f431527 AL |
1550 | /* |
1551 | * Read sys file-system device file | |
1552 | * | |
1553 | * @line address of buffer to put file contents in | |
1554 | * @line_size size of line | |
1555 | * @device_file path to device file (printf format string) | |
1556 | * @device_name device being opened (inserted into device_file) | |
1557 | * | |
1558 | * @return 0 failed, 1 succeeded ('line' contains data) | |
1559 | */ | |
2791104c DA |
1560 | static int usb_host_read_file(char *line, size_t line_size, |
1561 | const char *device_file, const char *device_name) | |
0f431527 AL |
1562 | { |
1563 | FILE *f; | |
1564 | int ret = 0; | |
1565 | char filename[PATH_MAX]; | |
1566 | ||
097db438 | 1567 | snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/%s", device_name, |
b4e237aa | 1568 | device_file); |
0f431527 AL |
1569 | f = fopen(filename, "r"); |
1570 | if (f) { | |
9f99cee7 | 1571 | ret = fgets(line, line_size, f) != NULL; |
0f431527 | 1572 | fclose(f); |
0f431527 AL |
1573 | } |
1574 | ||
1575 | return ret; | |
1576 | } | |
1577 | ||
1578 | /* | |
1579 | * Use /sys/bus/usb/devices/ directory to determine host's USB | |
1580 | * devices. | |
1581 | * | |
1582 | * This code is based on Robert Schiele's original patches posted to | |
1583 | * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950 | |
1584 | */ | |
097db438 | 1585 | static int usb_host_scan(void *opaque, USBScanFunc *func) |
0f431527 | 1586 | { |
660f11be | 1587 | DIR *dir = NULL; |
0f431527 | 1588 | char line[1024]; |
5557d820 | 1589 | int bus_num, addr, speed, class_id, product_id, vendor_id; |
0f431527 | 1590 | int ret = 0; |
5557d820 | 1591 | char port[MAX_PORTLEN]; |
0f431527 AL |
1592 | char product_name[512]; |
1593 | struct dirent *de; | |
1594 | ||
097db438 | 1595 | dir = opendir("/sys/bus/usb/devices"); |
0f431527 | 1596 | if (!dir) { |
097db438 GH |
1597 | perror("husb: opendir /sys/bus/usb/devices"); |
1598 | fprintf(stderr, "husb: please make sure sysfs is mounted at /sys\n"); | |
0f431527 AL |
1599 | goto the_end; |
1600 | } | |
1601 | ||
1602 | while ((de = readdir(dir))) { | |
1603 | if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) { | |
5557d820 GH |
1604 | if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) { |
1605 | continue; | |
0f5160d1 | 1606 | } |
0f431527 | 1607 | |
2791104c | 1608 | if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) { |
0f431527 | 1609 | goto the_end; |
2791104c DA |
1610 | } |
1611 | if (sscanf(line, "%d", &addr) != 1) { | |
0f431527 | 1612 | goto the_end; |
2791104c | 1613 | } |
b4e237aa | 1614 | if (!usb_host_read_file(line, sizeof(line), "bDeviceClass", |
2791104c | 1615 | de->d_name)) { |
0f431527 | 1616 | goto the_end; |
2791104c DA |
1617 | } |
1618 | if (sscanf(line, "%x", &class_id) != 1) { | |
0f431527 | 1619 | goto the_end; |
2791104c | 1620 | } |
0f431527 | 1621 | |
2791104c DA |
1622 | if (!usb_host_read_file(line, sizeof(line), "idVendor", |
1623 | de->d_name)) { | |
0f431527 | 1624 | goto the_end; |
2791104c DA |
1625 | } |
1626 | if (sscanf(line, "%x", &vendor_id) != 1) { | |
0f431527 | 1627 | goto the_end; |
2791104c | 1628 | } |
b4e237aa | 1629 | if (!usb_host_read_file(line, sizeof(line), "idProduct", |
2791104c | 1630 | de->d_name)) { |
0f431527 | 1631 | goto the_end; |
2791104c DA |
1632 | } |
1633 | if (sscanf(line, "%x", &product_id) != 1) { | |
0f431527 | 1634 | goto the_end; |
2791104c | 1635 | } |
b4e237aa BS |
1636 | if (!usb_host_read_file(line, sizeof(line), "product", |
1637 | de->d_name)) { | |
0f431527 AL |
1638 | *product_name = 0; |
1639 | } else { | |
2791104c | 1640 | if (strlen(line) > 0) { |
0f431527 | 1641 | line[strlen(line) - 1] = '\0'; |
2791104c | 1642 | } |
0f431527 AL |
1643 | pstrcpy(product_name, sizeof(product_name), line); |
1644 | } | |
1645 | ||
2791104c | 1646 | if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) { |
0f431527 | 1647 | goto the_end; |
2791104c | 1648 | } |
f264cfbf HG |
1649 | if (!strcmp(line, "5000\n")) { |
1650 | speed = USB_SPEED_SUPER; | |
1651 | } else if (!strcmp(line, "480\n")) { | |
0f431527 | 1652 | speed = USB_SPEED_HIGH; |
2791104c | 1653 | } else if (!strcmp(line, "1.5\n")) { |
0f431527 | 1654 | speed = USB_SPEED_LOW; |
2791104c | 1655 | } else { |
0f431527 | 1656 | speed = USB_SPEED_FULL; |
2791104c | 1657 | } |
0f431527 | 1658 | |
5557d820 | 1659 | ret = func(opaque, bus_num, addr, port, class_id, vendor_id, |
0f431527 | 1660 | product_id, product_name, speed); |
2791104c | 1661 | if (ret) { |
0f431527 | 1662 | goto the_end; |
2791104c | 1663 | } |
0f431527 AL |
1664 | } |
1665 | } | |
1666 | the_end: | |
2791104c | 1667 | if (dir) { |
0f431527 | 1668 | closedir(dir); |
2791104c | 1669 | } |
0f431527 AL |
1670 | return ret; |
1671 | } | |
1672 | ||
4b096fc9 | 1673 | static QEMUTimer *usb_auto_timer; |
c06c68c9 | 1674 | static VMChangeStateEntry *usb_vmstate; |
4b096fc9 | 1675 | |
ba9acab9 GH |
1676 | static int usb_host_auto_scan(void *opaque, int bus_num, |
1677 | int addr, const char *port, | |
26a9e82a GH |
1678 | int class_id, int vendor_id, int product_id, |
1679 | const char *product_name, int speed) | |
4b096fc9 AL |
1680 | { |
1681 | struct USBAutoFilter *f; | |
26a9e82a | 1682 | struct USBHostDevice *s; |
4b096fc9 AL |
1683 | |
1684 | /* Ignore hubs */ | |
1685 | if (class_id == 9) | |
1686 | return 0; | |
1687 | ||
26a9e82a GH |
1688 | QTAILQ_FOREACH(s, &hostdevs, next) { |
1689 | f = &s->match; | |
1690 | ||
2791104c | 1691 | if (f->bus_num > 0 && f->bus_num != bus_num) { |
4b096fc9 | 1692 | continue; |
2791104c DA |
1693 | } |
1694 | if (f->addr > 0 && f->addr != addr) { | |
4b096fc9 | 1695 | continue; |
2791104c | 1696 | } |
46635308 | 1697 | if (f->port != NULL && strcmp(f->port, port) != 0) { |
9056a297 GH |
1698 | continue; |
1699 | } | |
4b096fc9 | 1700 | |
2791104c | 1701 | if (f->vendor_id > 0 && f->vendor_id != vendor_id) { |
4b096fc9 | 1702 | continue; |
2791104c | 1703 | } |
4b096fc9 | 1704 | |
2791104c | 1705 | if (f->product_id > 0 && f->product_id != product_id) { |
4b096fc9 | 1706 | continue; |
2791104c | 1707 | } |
4b096fc9 | 1708 | /* We got a match */ |
3ee886c5 GH |
1709 | s->seen++; |
1710 | if (s->errcount >= 3) { | |
1711 | return 0; | |
1712 | } | |
4b096fc9 | 1713 | |
33e66b86 | 1714 | /* Already attached ? */ |
2791104c | 1715 | if (s->fd != -1) { |
4b096fc9 | 1716 | return 0; |
2791104c | 1717 | } |
d0f2c4c6 | 1718 | DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr); |
4b096fc9 | 1719 | |
3ee886c5 GH |
1720 | if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) { |
1721 | s->errcount++; | |
1722 | } | |
97f86166 | 1723 | break; |
4b096fc9 AL |
1724 | } |
1725 | ||
1726 | return 0; | |
1727 | } | |
1728 | ||
c06c68c9 GH |
1729 | static void usb_host_vm_state(void *unused, int running, RunState state) |
1730 | { | |
1731 | if (running) { | |
1732 | usb_host_auto_check(unused); | |
1733 | } | |
1734 | } | |
1735 | ||
26a9e82a | 1736 | static void usb_host_auto_check(void *unused) |
4b096fc9 | 1737 | { |
26a9e82a GH |
1738 | struct USBHostDevice *s; |
1739 | int unconnected = 0; | |
1740 | ||
a844ed84 GH |
1741 | if (runstate_is_running()) { |
1742 | usb_host_scan(NULL, usb_host_auto_scan); | |
26a9e82a | 1743 | |
a844ed84 GH |
1744 | QTAILQ_FOREACH(s, &hostdevs, next) { |
1745 | if (s->fd == -1) { | |
1746 | unconnected++; | |
1747 | } | |
1748 | if (s->seen == 0) { | |
1749 | s->errcount = 0; | |
1750 | } | |
1751 | s->seen = 0; | |
3ee886c5 | 1752 | } |
26a9e82a | 1753 | |
a844ed84 GH |
1754 | if (unconnected == 0) { |
1755 | /* nothing to watch */ | |
1756 | if (usb_auto_timer) { | |
bc72ad67 | 1757 | timer_del(usb_auto_timer); |
a844ed84 GH |
1758 | trace_usb_host_auto_scan_disabled(); |
1759 | } | |
1760 | return; | |
2791104c | 1761 | } |
26a9e82a GH |
1762 | } |
1763 | ||
c06c68c9 GH |
1764 | if (!usb_vmstate) { |
1765 | usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL); | |
1766 | } | |
26a9e82a | 1767 | if (!usb_auto_timer) { |
bc72ad67 | 1768 | usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL); |
2791104c | 1769 | if (!usb_auto_timer) { |
26a9e82a | 1770 | return; |
2791104c | 1771 | } |
e6a2f500 | 1772 | trace_usb_host_auto_scan_enabled(); |
26a9e82a | 1773 | } |
bc72ad67 | 1774 | timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000); |
4b096fc9 AL |
1775 | } |
1776 | ||
2b2325ff GH |
1777 | #ifndef CONFIG_USB_LIBUSB |
1778 | ||
a594cfbf FB |
1779 | /**********************/ |
1780 | /* USB host device info */ | |
1781 | ||
1782 | struct usb_class_info { | |
1783 | int class; | |
1784 | const char *class_name; | |
1785 | }; | |
1786 | ||
1787 | static const struct usb_class_info usb_class_info[] = { | |
1788 | { USB_CLASS_AUDIO, "Audio"}, | |
1789 | { USB_CLASS_COMM, "Communication"}, | |
1790 | { USB_CLASS_HID, "HID"}, | |
1791 | { USB_CLASS_HUB, "Hub" }, | |
1792 | { USB_CLASS_PHYSICAL, "Physical" }, | |
1793 | { USB_CLASS_PRINTER, "Printer" }, | |
1794 | { USB_CLASS_MASS_STORAGE, "Storage" }, | |
1795 | { USB_CLASS_CDC_DATA, "Data" }, | |
1796 | { USB_CLASS_APP_SPEC, "Application Specific" }, | |
1797 | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" }, | |
1798 | { USB_CLASS_STILL_IMAGE, "Still Image" }, | |
b9dc033c | 1799 | { USB_CLASS_CSCID, "Smart Card" }, |
a594cfbf FB |
1800 | { USB_CLASS_CONTENT_SEC, "Content Security" }, |
1801 | { -1, NULL } | |
1802 | }; | |
1803 | ||
1804 | static const char *usb_class_str(uint8_t class) | |
bb36d470 | 1805 | { |
a594cfbf FB |
1806 | const struct usb_class_info *p; |
1807 | for(p = usb_class_info; p->class != -1; p++) { | |
2791104c | 1808 | if (p->class == class) { |
a594cfbf | 1809 | break; |
2791104c | 1810 | } |
bb36d470 | 1811 | } |
a594cfbf FB |
1812 | return p->class_name; |
1813 | } | |
1814 | ||
ba9acab9 GH |
1815 | static void usb_info_device(Monitor *mon, int bus_num, |
1816 | int addr, const char *port, | |
5557d820 | 1817 | int class_id, int vendor_id, int product_id, |
9596ebb7 PB |
1818 | const char *product_name, |
1819 | int speed) | |
a594cfbf FB |
1820 | { |
1821 | const char *class_str, *speed_str; | |
1822 | ||
1823 | switch(speed) { | |
5fafdf24 TS |
1824 | case USB_SPEED_LOW: |
1825 | speed_str = "1.5"; | |
a594cfbf | 1826 | break; |
5fafdf24 TS |
1827 | case USB_SPEED_FULL: |
1828 | speed_str = "12"; | |
a594cfbf | 1829 | break; |
5fafdf24 TS |
1830 | case USB_SPEED_HIGH: |
1831 | speed_str = "480"; | |
a594cfbf | 1832 | break; |
f264cfbf HG |
1833 | case USB_SPEED_SUPER: |
1834 | speed_str = "5000"; | |
1835 | break; | |
a594cfbf | 1836 | default: |
5fafdf24 | 1837 | speed_str = "?"; |
a594cfbf FB |
1838 | break; |
1839 | } | |
1840 | ||
5557d820 GH |
1841 | monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n", |
1842 | bus_num, addr, port, speed_str); | |
a594cfbf | 1843 | class_str = usb_class_str(class_id); |
2791104c | 1844 | if (class_str) { |
376253ec | 1845 | monitor_printf(mon, " %s:", class_str); |
2791104c | 1846 | } else { |
376253ec | 1847 | monitor_printf(mon, " Class %02x:", class_id); |
2791104c | 1848 | } |
376253ec | 1849 | monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id); |
2791104c | 1850 | if (product_name[0] != '\0') { |
376253ec | 1851 | monitor_printf(mon, ", %s", product_name); |
2791104c | 1852 | } |
376253ec | 1853 | monitor_printf(mon, "\n"); |
a594cfbf FB |
1854 | } |
1855 | ||
5fafdf24 | 1856 | static int usb_host_info_device(void *opaque, int bus_num, int addr, |
ba9acab9 | 1857 | const char *path, int class_id, |
5fafdf24 | 1858 | int vendor_id, int product_id, |
a594cfbf FB |
1859 | const char *product_name, |
1860 | int speed) | |
1861 | { | |
179da8af BS |
1862 | Monitor *mon = opaque; |
1863 | ||
5557d820 | 1864 | usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id, |
a594cfbf FB |
1865 | product_name, speed); |
1866 | return 0; | |
1867 | } | |
1868 | ||
ac4ffb5a | 1869 | static void dec2str(int val, char *str, size_t size) |
5d0c5750 | 1870 | { |
2791104c | 1871 | if (val == 0) { |
ac4ffb5a | 1872 | snprintf(str, size, "*"); |
2791104c DA |
1873 | } else { |
1874 | snprintf(str, size, "%d", val); | |
1875 | } | |
5d0c5750 AL |
1876 | } |
1877 | ||
ac4ffb5a | 1878 | static void hex2str(int val, char *str, size_t size) |
5d0c5750 | 1879 | { |
2791104c | 1880 | if (val == 0) { |
ac4ffb5a | 1881 | snprintf(str, size, "*"); |
2791104c | 1882 | } else { |
26a9e82a | 1883 | snprintf(str, size, "%04x", val); |
2791104c | 1884 | } |
5d0c5750 AL |
1885 | } |
1886 | ||
84f2d0ea | 1887 | void usb_host_info(Monitor *mon, const QDict *qdict) |
a594cfbf | 1888 | { |
5d0c5750 | 1889 | struct USBAutoFilter *f; |
26a9e82a | 1890 | struct USBHostDevice *s; |
5d0c5750 | 1891 | |
179da8af | 1892 | usb_host_scan(mon, usb_host_info_device); |
5d0c5750 | 1893 | |
2791104c | 1894 | if (QTAILQ_EMPTY(&hostdevs)) { |
26a9e82a | 1895 | return; |
2791104c DA |
1896 | } |
1897 | ||
26a9e82a GH |
1898 | monitor_printf(mon, " Auto filters:\n"); |
1899 | QTAILQ_FOREACH(s, &hostdevs, next) { | |
5d0c5750 | 1900 | char bus[10], addr[10], vid[10], pid[10]; |
26a9e82a | 1901 | f = &s->match; |
ac4ffb5a AL |
1902 | dec2str(f->bus_num, bus, sizeof(bus)); |
1903 | dec2str(f->addr, addr, sizeof(addr)); | |
1904 | hex2str(f->vendor_id, vid, sizeof(vid)); | |
1905 | hex2str(f->product_id, pid, sizeof(pid)); | |
9056a297 GH |
1906 | monitor_printf(mon, " Bus %s, Addr %s, Port %s, ID %s:%s\n", |
1907 | bus, addr, f->port ? f->port : "*", vid, pid); | |
5d0c5750 | 1908 | } |
bb36d470 | 1909 | } |
2b2325ff GH |
1910 | |
1911 | #endif |