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