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