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