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