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