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