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