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