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