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