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