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 | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 6 | * Support for host device auto connect & disconnect |
| 7 | * Copyright (c) 2008 Max Krasnyansky |
| 8 | * |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 27 | #include "qemu-common.h" |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 28 | #include "qemu-timer.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 29 | #include "hw/usb.h" |
| 30 | #include "console.h" |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 31 | |
| 32 | #if defined(__linux__) |
| 33 | #include <dirent.h> |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <linux/usbdevice_fs.h> |
| 36 | #include <linux/version.h> |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 37 | #include <signal.h> |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 38 | |
| 39 | /* We redefine it to avoid version problems */ |
| 40 | struct usb_ctrltransfer { |
| 41 | uint8_t bRequestType; |
| 42 | uint8_t bRequest; |
| 43 | uint16_t wValue; |
| 44 | uint16_t wIndex; |
| 45 | uint16_t wLength; |
| 46 | uint32_t timeout; |
| 47 | void *data; |
| 48 | }; |
| 49 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 50 | typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 51 | int vendor_id, int product_id, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 52 | const char *product_name, int speed); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 53 | static int usb_host_find_device(int *pbus_num, int *paddr, |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 54 | char *product_name, int product_name_size, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 55 | const char *devname); |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 56 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 57 | //#define DEBUG |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 58 | //#define DEBUG_ISOCH |
| 59 | //#define USE_ASYNCIO |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 60 | |
| 61 | #define USBDEVFS_PATH "/proc/bus/usb" |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 62 | #define PRODUCT_NAME_SZ 32 |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 63 | #define SIG_ISOCOMPLETE (SIGRTMIN+7) |
| 64 | #define MAX_ENDPOINTS 16 |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 65 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 66 | struct sigaction sigact; |
| 67 | |
| 68 | /* endpoint association data */ |
| 69 | struct endp_data { |
| 70 | uint8_t type; |
| 71 | }; |
| 72 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 73 | |
| 74 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 75 | /* FIXME: move USBPacket to PendingURB */ |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 76 | typedef struct USBHostDevice { |
| 77 | USBDevice dev; |
| 78 | int fd; |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 79 | int pipe_fds[2]; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 80 | USBPacket *packet; |
| 81 | struct endp_data endp_table[MAX_ENDPOINTS]; |
| 82 | int configuration; |
| 83 | uint8_t descr[1024]; |
| 84 | int descr_len; |
| 85 | int urbs_ready; |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 86 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 87 | QEMUTimer *timer; |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 88 | |
| 89 | /* Host side address */ |
| 90 | int bus_num; |
| 91 | int addr; |
| 92 | |
| 93 | struct USBHostDevice *next; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 94 | } USBHostDevice; |
| 95 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 96 | static USBHostDevice *hostdev_list; |
| 97 | |
| 98 | static void hostdev_link(USBHostDevice *dev) |
| 99 | { |
| 100 | dev->next = hostdev_list; |
| 101 | hostdev_list = dev; |
| 102 | } |
| 103 | |
| 104 | static void hostdev_unlink(USBHostDevice *dev) |
| 105 | { |
| 106 | USBHostDevice *pdev = hostdev_list; |
| 107 | USBHostDevice **prev = &hostdev_list; |
| 108 | |
| 109 | while (pdev) { |
| 110 | if (pdev == dev) { |
| 111 | *prev = dev->next; |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | prev = &pdev->next; |
| 116 | pdev = pdev->next; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static USBHostDevice *hostdev_find(int bus_num, int addr) |
| 121 | { |
| 122 | USBHostDevice *s = hostdev_list; |
| 123 | while (s) { |
| 124 | if (s->bus_num == bus_num && s->addr == addr) |
| 125 | return s; |
| 126 | s = s->next; |
| 127 | } |
| 128 | return NULL; |
| 129 | } |
| 130 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 131 | typedef struct PendingURB { |
| 132 | struct usbdevfs_urb *urb; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 133 | int status; |
| 134 | struct PendingURB *next; |
| 135 | } PendingURB; |
| 136 | |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 137 | static PendingURB *pending_urbs = NULL; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 138 | |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 139 | static int add_pending_urb(struct usbdevfs_urb *urb) |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 140 | { |
| 141 | PendingURB *purb = qemu_mallocz(sizeof(PendingURB)); |
| 142 | if (purb) { |
| 143 | purb->urb = urb; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 144 | purb->status = 0; |
| 145 | purb->next = pending_urbs; |
| 146 | pending_urbs = purb; |
| 147 | return 1; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 152 | static int del_pending_urb(struct usbdevfs_urb *urb) |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 153 | { |
| 154 | PendingURB *purb = pending_urbs; |
| 155 | PendingURB *prev = NULL; |
| 156 | |
| 157 | while (purb && purb->urb != urb) { |
| 158 | prev = purb; |
| 159 | purb = purb->next; |
| 160 | } |
| 161 | |
| 162 | if (purb && purb->urb == urb) { |
| 163 | if (prev) { |
| 164 | prev->next = purb->next; |
| 165 | } else { |
| 166 | pending_urbs = purb->next; |
| 167 | } |
| 168 | qemu_free(purb); |
| 169 | return 1; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 174 | #ifdef USE_ASYNCIO |
| 175 | static PendingURB *get_pending_urb(struct usbdevfs_urb *urb) |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 176 | { |
| 177 | PendingURB *purb = pending_urbs; |
| 178 | |
| 179 | while (purb && purb->urb != urb) { |
| 180 | purb = purb->next; |
| 181 | } |
| 182 | |
| 183 | if (purb && purb->urb == urb) { |
| 184 | return purb; |
| 185 | } |
| 186 | return NULL; |
| 187 | } |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 188 | #endif |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 189 | |
| 190 | static int usb_host_update_interfaces(USBHostDevice *dev, int configuration) |
| 191 | { |
| 192 | int dev_descr_len, config_descr_len; |
| 193 | int interface, nb_interfaces, nb_configurations; |
| 194 | int ret, i; |
| 195 | |
| 196 | if (configuration == 0) /* address state - ignore */ |
| 197 | return 1; |
| 198 | |
| 199 | i = 0; |
| 200 | dev_descr_len = dev->descr[0]; |
| 201 | if (dev_descr_len > dev->descr_len) |
| 202 | goto fail; |
| 203 | nb_configurations = dev->descr[17]; |
| 204 | |
| 205 | i += dev_descr_len; |
| 206 | while (i < dev->descr_len) { |
| 207 | #ifdef DEBUG |
| 208 | printf("i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len, |
| 209 | dev->descr[i], dev->descr[i+1]); |
| 210 | #endif |
| 211 | if (dev->descr[i+1] != USB_DT_CONFIG) { |
| 212 | i += dev->descr[i]; |
| 213 | continue; |
| 214 | } |
| 215 | config_descr_len = dev->descr[i]; |
| 216 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 217 | #ifdef DEBUG |
| 218 | printf("config #%d need %d\n", dev->descr[i + 5], configuration); |
| 219 | #endif |
| 220 | |
| 221 | if (configuration < 0 || configuration == dev->descr[i + 5]) |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 222 | break; |
| 223 | |
| 224 | i += config_descr_len; |
| 225 | } |
| 226 | |
| 227 | if (i >= dev->descr_len) { |
| 228 | printf("usb_host: error - device has no matching configuration\n"); |
| 229 | goto fail; |
| 230 | } |
| 231 | nb_interfaces = dev->descr[i + 4]; |
| 232 | |
| 233 | #ifdef USBDEVFS_DISCONNECT |
| 234 | /* earlier Linux 2.4 do not support that */ |
| 235 | { |
| 236 | struct usbdevfs_ioctl ctrl; |
| 237 | for (interface = 0; interface < nb_interfaces; interface++) { |
| 238 | ctrl.ioctl_code = USBDEVFS_DISCONNECT; |
| 239 | ctrl.ifno = interface; |
| 240 | ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl); |
| 241 | if (ret < 0 && errno != ENODATA) { |
| 242 | perror("USBDEVFS_DISCONNECT"); |
| 243 | goto fail; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | #endif |
| 248 | |
| 249 | /* XXX: only grab if all interfaces are free */ |
| 250 | for (interface = 0; interface < nb_interfaces; interface++) { |
| 251 | ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface); |
| 252 | if (ret < 0) { |
| 253 | if (errno == EBUSY) { |
| 254 | fprintf(stderr, |
| 255 | "usb_host: warning - device already grabbed\n"); |
| 256 | } else { |
| 257 | perror("USBDEVFS_CLAIMINTERFACE"); |
| 258 | } |
| 259 | fail: |
| 260 | return 0; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | #ifdef DEBUG |
| 265 | printf("usb_host: %d interfaces claimed for configuration %d\n", |
| 266 | nb_interfaces, configuration); |
| 267 | #endif |
| 268 | |
| 269 | return 1; |
| 270 | } |
| 271 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 272 | static void usb_host_handle_reset(USBDevice *dev) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 273 | { |
| 274 | #if 0 |
| 275 | USBHostDevice *s = (USBHostDevice *)dev; |
| 276 | /* USBDEVFS_RESET, but not the first time as it has already be |
| 277 | done by the host OS */ |
| 278 | ioctl(s->fd, USBDEVFS_RESET); |
| 279 | #endif |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 280 | } |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 281 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 282 | static void usb_host_handle_destroy(USBDevice *dev) |
| 283 | { |
| 284 | USBHostDevice *s = (USBHostDevice *)dev; |
| 285 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 286 | qemu_del_timer(s->timer); |
| 287 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 288 | hostdev_unlink(s); |
| 289 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 290 | if (s->fd >= 0) |
| 291 | close(s->fd); |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 292 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 293 | qemu_free(s); |
| 294 | } |
| 295 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 296 | static int usb_linux_update_endp_table(USBHostDevice *s); |
| 297 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 298 | static int usb_host_handle_control(USBDevice *dev, |
| 299 | int request, |
| 300 | int value, |
| 301 | int index, |
| 302 | int length, |
| 303 | uint8_t *data) |
| 304 | { |
| 305 | USBHostDevice *s = (USBHostDevice *)dev; |
| 306 | struct usb_ctrltransfer ct; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 307 | struct usbdevfs_setinterface si; |
| 308 | int intf_update_required = 0; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 309 | int ret; |
| 310 | |
| 311 | if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) { |
| 312 | /* specific SET_ADDRESS support */ |
| 313 | dev->addr = value; |
| 314 | return 0; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 315 | } else if (request == ((USB_RECIP_INTERFACE << 8) | |
| 316 | USB_REQ_SET_INTERFACE)) { |
| 317 | /* set alternate setting for the interface */ |
| 318 | si.interface = index; |
| 319 | si.altsetting = value; |
| 320 | ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si); |
| 321 | usb_linux_update_endp_table(s); |
| 322 | } else if (request == (DeviceOutRequest | USB_REQ_SET_CONFIGURATION)) { |
| 323 | #ifdef DEBUG |
| 324 | printf("usb_host_handle_control: SET_CONFIGURATION request - " |
| 325 | "config %d\n", value & 0xff); |
| 326 | #endif |
| 327 | if (s->configuration != (value & 0xff)) { |
| 328 | s->configuration = (value & 0xff); |
| 329 | intf_update_required = 1; |
| 330 | } |
| 331 | goto do_request; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 332 | } else { |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 333 | do_request: |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 334 | ct.bRequestType = request >> 8; |
| 335 | ct.bRequest = request; |
| 336 | ct.wValue = value; |
| 337 | ct.wIndex = index; |
| 338 | ct.wLength = length; |
| 339 | ct.timeout = 50; |
| 340 | ct.data = data; |
| 341 | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | if (ret < 0) { |
| 345 | switch(errno) { |
| 346 | case ETIMEDOUT: |
| 347 | return USB_RET_NAK; |
| 348 | default: |
| 349 | return USB_RET_STALL; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 350 | } |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 351 | } else { |
| 352 | if (intf_update_required) { |
| 353 | #ifdef DEBUG |
| 354 | printf("usb_host_handle_control: updating interfaces\n"); |
| 355 | #endif |
| 356 | usb_host_update_interfaces(s, value & 0xff); |
| 357 | } |
| 358 | return ret; |
| 359 | } |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 360 | } |
| 361 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 362 | static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p); |
| 363 | |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 364 | static int usb_host_handle_data(USBDevice *dev, USBPacket *p) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 365 | { |
| 366 | USBHostDevice *s = (USBHostDevice *)dev; |
| 367 | struct usbdevfs_bulktransfer bt; |
| 368 | int ret; |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 369 | uint8_t devep = p->devep; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 370 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 371 | if (s->endp_table[p->devep - 1].type == USBDEVFS_URB_TYPE_ISO) { |
| 372 | return usb_host_handle_isoch(dev, p); |
| 373 | } |
| 374 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 375 | /* XXX: optimize and handle all data types by looking at the |
| 376 | config descriptor */ |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 377 | if (p->pid == USB_TOKEN_IN) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 378 | devep |= 0x80; |
| 379 | bt.ep = devep; |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 380 | bt.len = p->len; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 381 | bt.timeout = 50; |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 382 | bt.data = p->data; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 383 | ret = ioctl(s->fd, USBDEVFS_BULK, &bt); |
| 384 | if (ret < 0) { |
| 385 | switch(errno) { |
| 386 | case ETIMEDOUT: |
| 387 | return USB_RET_NAK; |
| 388 | case EPIPE: |
| 389 | default: |
| 390 | #ifdef DEBUG |
| 391 | printf("handle_data: errno=%d\n", errno); |
| 392 | #endif |
| 393 | return USB_RET_STALL; |
| 394 | } |
| 395 | } else { |
| 396 | return ret; |
| 397 | } |
| 398 | } |
| 399 | |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 400 | #ifdef USE_ASYNCIO |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 401 | static void urb_completion_pipe_read(void *opaque) |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 402 | { |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 403 | USBHostDevice *s = opaque; |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 404 | USBPacket *p = s->packet; |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 405 | PendingURB *pending_urb = NULL; |
| 406 | struct usbdevfs_urb *purb = NULL; |
| 407 | int len, ret; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 408 | |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 409 | len = read(s->pipe_fds[0], &pending_urb, sizeof(pending_urb)); |
| 410 | if (len != sizeof(pending_urb)) { |
| 411 | printf("urb_completion: error reading pending_urb, len=%d\n", len); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | /* FIXME: handle pending_urb->status */ |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 416 | del_pending_urb(pending_urb->urb); |
| 417 | |
| 418 | if (!p) { |
| 419 | s->urbs_ready++; |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb); |
| 424 | if (ret < 0) { |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 425 | printf("urb_completion: REAPURBNDELAY ioctl=%d errno=%d\n", |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 426 | ret, errno); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | #ifdef DEBUG_ISOCH |
| 431 | if (purb == pending_urb->urb) { |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 432 | printf("urb_completion: urb mismatch reaped=%p pending=%p\n", |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 433 | purb, urb); |
| 434 | } |
| 435 | #endif |
| 436 | |
| 437 | p->len = purb->actual_length; |
| 438 | usb_packet_complete(p); |
| 439 | qemu_free(purb); |
| 440 | s->packet = NULL; |
| 441 | } |
| 442 | |
| 443 | static void isoch_done(int signum, siginfo_t *info, void *context) |
| 444 | { |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 445 | struct usbdevfs_urb *urb = (struct usbdevfs_urb *)info->si_addr; |
| 446 | USBHostDevice *s = (USBHostDevice *)urb->usercontext; |
| 447 | PendingURB *purb; |
| 448 | |
| 449 | if (info->si_code != SI_ASYNCIO || |
| 450 | info->si_signo != SIG_ISOCOMPLETE) { |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | purb = get_pending_urb(urb); |
| 455 | if (purb) { |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 456 | purb->status = info->si_errno; |
| 457 | write(s->pipe_fds[1], &purb, sizeof(purb)); |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
balrog | 4d043a0 | 2007-10-04 22:55:53 +0000 | [diff] [blame] | 460 | #endif |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 461 | |
| 462 | static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p) |
| 463 | { |
| 464 | USBHostDevice *s = (USBHostDevice *)dev; |
| 465 | struct usbdevfs_urb *urb, *purb = NULL; |
| 466 | int ret; |
| 467 | uint8_t devep = p->devep; |
| 468 | |
| 469 | if (p->pid == USB_TOKEN_IN) |
| 470 | devep |= 0x80; |
| 471 | |
| 472 | urb = qemu_mallocz(sizeof(struct usbdevfs_urb) + |
| 473 | sizeof(struct usbdevfs_iso_packet_desc)); |
| 474 | if (!urb) { |
| 475 | printf("usb_host_handle_isoch: malloc failed\n"); |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | urb->type = USBDEVFS_URB_TYPE_ISO; |
| 480 | urb->endpoint = devep; |
| 481 | urb->status = 0; |
| 482 | urb->flags = USBDEVFS_URB_ISO_ASAP; |
| 483 | urb->buffer = p->data; |
| 484 | urb->buffer_length = p->len; |
| 485 | urb->actual_length = 0; |
| 486 | urb->start_frame = 0; |
| 487 | urb->error_count = 0; |
| 488 | #ifdef USE_ASYNCIO |
| 489 | urb->signr = SIG_ISOCOMPLETE; |
| 490 | #else |
| 491 | urb->signr = 0; |
| 492 | #endif |
| 493 | urb->usercontext = s; |
| 494 | urb->number_of_packets = 1; |
| 495 | urb->iso_frame_desc[0].length = p->len; |
| 496 | urb->iso_frame_desc[0].actual_length = 0; |
| 497 | urb->iso_frame_desc[0].status = 0; |
| 498 | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
| 499 | if (ret == 0) { |
| 500 | if (!add_pending_urb(urb)) { |
| 501 | printf("usb_host_handle_isoch: add_pending_urb failed %p\n", urb); |
| 502 | } |
| 503 | } else { |
| 504 | printf("usb_host_handle_isoch: SUBMITURB ioctl=%d errno=%d\n", |
| 505 | ret, errno); |
| 506 | qemu_free(urb); |
| 507 | switch(errno) { |
| 508 | case ETIMEDOUT: |
| 509 | return USB_RET_NAK; |
| 510 | case EPIPE: |
| 511 | default: |
| 512 | return USB_RET_STALL; |
| 513 | } |
| 514 | } |
| 515 | #ifdef USE_ASYNCIO |
| 516 | /* FIXME: handle urbs_ready together with sync io |
| 517 | * workaround for injecting the signaled urbs into current frame */ |
| 518 | if (s->urbs_ready > 0) { |
| 519 | ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb); |
| 520 | if (ret == 0) { |
| 521 | ret = purb->actual_length; |
| 522 | qemu_free(purb); |
| 523 | s->urbs_ready--; |
| 524 | } |
| 525 | return ret; |
| 526 | } |
| 527 | s->packet = p; |
| 528 | return USB_RET_ASYNC; |
| 529 | #else |
| 530 | ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb); |
| 531 | if (ret == 0) { |
| 532 | if (del_pending_urb(purb)) { |
| 533 | ret = purb->actual_length; |
| 534 | qemu_free(purb); |
| 535 | } else { |
| 536 | printf("usb_host_handle_isoch: del_pending_urb failed %p\n", purb); |
| 537 | } |
| 538 | } else { |
| 539 | #ifdef DEBUG_ISOCH |
| 540 | printf("usb_host_handle_isoch: REAPURBNDELAY ioctl=%d errno=%d\n", |
| 541 | ret, errno); |
| 542 | #endif |
| 543 | } |
| 544 | return ret; |
| 545 | #endif |
| 546 | } |
| 547 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 548 | /* returns 1 on problem encountered or 0 for success */ |
| 549 | static int usb_linux_update_endp_table(USBHostDevice *s) |
| 550 | { |
| 551 | uint8_t *descriptors; |
| 552 | uint8_t devep, type, configuration, alt_interface; |
| 553 | struct usb_ctrltransfer ct; |
| 554 | int interface, ret, length, i; |
| 555 | |
| 556 | ct.bRequestType = USB_DIR_IN; |
| 557 | ct.bRequest = USB_REQ_GET_CONFIGURATION; |
| 558 | ct.wValue = 0; |
| 559 | ct.wIndex = 0; |
| 560 | ct.wLength = 1; |
| 561 | ct.data = &configuration; |
| 562 | ct.timeout = 50; |
| 563 | |
| 564 | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
| 565 | if (ret < 0) { |
| 566 | perror("usb_linux_update_endp_table"); |
| 567 | return 1; |
| 568 | } |
| 569 | |
| 570 | /* in address state */ |
| 571 | if (configuration == 0) |
| 572 | return 1; |
| 573 | |
| 574 | /* get the desired configuration, interface, and endpoint descriptors |
| 575 | * from device description */ |
| 576 | descriptors = &s->descr[18]; |
| 577 | length = s->descr_len - 18; |
| 578 | i = 0; |
| 579 | |
| 580 | if (descriptors[i + 1] != USB_DT_CONFIG || |
| 581 | descriptors[i + 5] != configuration) { |
| 582 | printf("invalid descriptor data - configuration\n"); |
| 583 | return 1; |
| 584 | } |
| 585 | i += descriptors[i]; |
| 586 | |
| 587 | while (i < length) { |
| 588 | if (descriptors[i + 1] != USB_DT_INTERFACE || |
| 589 | (descriptors[i + 1] == USB_DT_INTERFACE && |
| 590 | descriptors[i + 4] == 0)) { |
| 591 | i += descriptors[i]; |
| 592 | continue; |
| 593 | } |
| 594 | |
| 595 | interface = descriptors[i + 2]; |
| 596 | |
| 597 | ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE; |
| 598 | ct.bRequest = USB_REQ_GET_INTERFACE; |
| 599 | ct.wValue = 0; |
| 600 | ct.wIndex = interface; |
| 601 | ct.wLength = 1; |
| 602 | ct.data = &alt_interface; |
| 603 | ct.timeout = 50; |
| 604 | |
| 605 | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
| 606 | if (ret < 0) { |
| 607 | perror("usb_linux_update_endp_table"); |
| 608 | return 1; |
| 609 | } |
| 610 | |
| 611 | /* the current interface descriptor is the active interface |
| 612 | * and has endpoints */ |
| 613 | if (descriptors[i + 3] != alt_interface) { |
| 614 | i += descriptors[i]; |
| 615 | continue; |
| 616 | } |
| 617 | |
| 618 | /* advance to the endpoints */ |
| 619 | while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) |
| 620 | i += descriptors[i]; |
| 621 | |
| 622 | if (i >= length) |
| 623 | break; |
| 624 | |
| 625 | while (i < length) { |
| 626 | if (descriptors[i + 1] != USB_DT_ENDPOINT) |
| 627 | break; |
| 628 | |
| 629 | devep = descriptors[i + 2]; |
| 630 | switch (descriptors[i + 3] & 0x3) { |
| 631 | case 0x00: |
| 632 | type = USBDEVFS_URB_TYPE_CONTROL; |
| 633 | break; |
| 634 | case 0x01: |
| 635 | type = USBDEVFS_URB_TYPE_ISO; |
| 636 | break; |
| 637 | case 0x02: |
| 638 | type = USBDEVFS_URB_TYPE_BULK; |
| 639 | break; |
| 640 | case 0x03: |
| 641 | type = USBDEVFS_URB_TYPE_INTERRUPT; |
| 642 | break; |
| 643 | default: |
| 644 | printf("usb_host: malformed endpoint type\n"); |
| 645 | type = USBDEVFS_URB_TYPE_BULK; |
| 646 | } |
| 647 | s->endp_table[(devep & 0xf) - 1].type = type; |
| 648 | |
| 649 | i += descriptors[i]; |
| 650 | } |
| 651 | } |
| 652 | return 0; |
| 653 | } |
| 654 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 655 | static void usb_host_device_check(void *priv) |
| 656 | { |
| 657 | USBHostDevice *s = priv; |
| 658 | struct usbdevfs_connectinfo ci; |
| 659 | int err; |
| 660 | |
| 661 | err = ioctl(s->fd, USBDEVFS_CONNECTINFO, &ci); |
| 662 | if (err < 0) { |
| 663 | printf("usb device %d.%d disconnected\n", 0, s->dev.addr); |
| 664 | usb_device_del_addr(0, s->dev.addr); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000); |
| 669 | } |
| 670 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 671 | static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 672 | { |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 673 | int fd = -1, ret; |
| 674 | USBHostDevice *dev = NULL; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 675 | struct usbdevfs_connectinfo ci; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 676 | char buf[1024]; |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 677 | |
| 678 | dev = qemu_mallocz(sizeof(USBHostDevice)); |
| 679 | if (!dev) |
| 680 | goto fail; |
| 681 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 682 | dev->bus_num = bus_num; |
| 683 | dev->addr = addr; |
| 684 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 685 | dev->timer = qemu_new_timer(rt_clock, usb_host_device_check, (void *) dev); |
| 686 | if (!dev->timer) |
| 687 | goto fail; |
| 688 | |
| 689 | #ifdef DEBUG |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 690 | printf("usb_host_device_open %d.%d\n", bus_num, addr); |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 691 | #endif |
| 692 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 693 | snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d", |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 694 | bus_num, addr); |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 695 | fd = open(buf, O_RDWR | O_NONBLOCK); |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 696 | if (fd < 0) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 697 | perror(buf); |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 698 | goto fail; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 699 | } |
| 700 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 701 | /* read the device description */ |
| 702 | dev->descr_len = read(fd, dev->descr, sizeof(dev->descr)); |
| 703 | if (dev->descr_len <= 0) { |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 704 | perror("usb_host_device_open: reading device data failed"); |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 705 | goto fail; |
| 706 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 707 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 708 | #ifdef DEBUG |
bellard | 868bfe2 | 2005-11-13 21:53:15 +0000 | [diff] [blame] | 709 | { |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 710 | int x; |
| 711 | printf("=== begin dumping device descriptor data ===\n"); |
| 712 | for (x = 0; x < dev->descr_len; x++) |
| 713 | printf("%02x ", dev->descr[x]); |
| 714 | printf("\n=== end dumping device descriptor data ===\n"); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 715 | } |
| 716 | #endif |
| 717 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 718 | dev->fd = fd; |
| 719 | dev->configuration = 1; |
| 720 | |
| 721 | /* XXX - do something about initial configuration */ |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 722 | if (!usb_host_update_interfaces(dev, -1)) |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 723 | goto fail; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 724 | |
| 725 | ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci); |
| 726 | if (ret < 0) { |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 727 | perror("usb_host_device_open: USBDEVFS_CONNECTINFO"); |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 728 | goto fail; |
| 729 | } |
| 730 | |
| 731 | #ifdef DEBUG |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 732 | printf("host USB device %d.%d grabbed\n", bus_num, addr); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 733 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 734 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 735 | ret = usb_linux_update_endp_table(dev); |
| 736 | if (ret) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 737 | goto fail; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 738 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 739 | if (ci.slow) |
| 740 | dev->dev.speed = USB_SPEED_LOW; |
| 741 | else |
| 742 | dev->dev.speed = USB_SPEED_HIGH; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 743 | dev->dev.handle_packet = usb_generic_handle_packet; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 744 | |
| 745 | dev->dev.handle_reset = usb_host_handle_reset; |
| 746 | dev->dev.handle_control = usb_host_handle_control; |
| 747 | dev->dev.handle_data = usb_host_handle_data; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 748 | dev->dev.handle_destroy = usb_host_handle_destroy; |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 749 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 750 | if (!prod_name || prod_name[0] == '\0') |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 751 | snprintf(dev->dev.devname, sizeof(dev->dev.devname), |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 752 | "host:%d.%d", bus_num, addr); |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 753 | else |
| 754 | pstrcpy(dev->dev.devname, sizeof(dev->dev.devname), |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 755 | prod_name); |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 756 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 757 | #ifdef USE_ASYNCIO |
| 758 | /* set up the signal handlers */ |
| 759 | sigemptyset(&sigact.sa_mask); |
| 760 | sigact.sa_sigaction = isoch_done; |
| 761 | sigact.sa_flags = SA_SIGINFO; |
| 762 | sigact.sa_restorer = 0; |
| 763 | ret = sigaction(SIG_ISOCOMPLETE, &sigact, NULL); |
| 764 | if (ret < 0) { |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 765 | perror("usb_host_device_open: sigaction failed"); |
| 766 | goto fail; |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 767 | } |
balrog | 046833e | 2007-10-31 00:27:50 +0000 | [diff] [blame] | 768 | |
| 769 | if (pipe(dev->pipe_fds) < 0) { |
| 770 | perror("usb_host_device_open: pipe creation failed"); |
| 771 | goto fail; |
| 772 | } |
| 773 | fcntl(dev->pipe_fds[0], F_SETFL, O_NONBLOCK | O_ASYNC); |
| 774 | fcntl(dev->pipe_fds[1], F_SETFL, O_NONBLOCK); |
| 775 | qemu_set_fd_handler(dev->pipe_fds[0], urb_completion_pipe_read, NULL, dev); |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 776 | #endif |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 777 | |
| 778 | /* Start the timer to detect disconnect */ |
| 779 | qemu_mod_timer(dev->timer, qemu_get_clock(rt_clock) + 1000); |
| 780 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 781 | hostdev_link(dev); |
| 782 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 783 | dev->urbs_ready = 0; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 784 | return (USBDevice *)dev; |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 785 | |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 786 | fail: |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 787 | if (dev) { |
| 788 | if (dev->timer) |
| 789 | qemu_del_timer(dev->timer); |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 790 | qemu_free(dev); |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 791 | } |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 792 | close(fd); |
| 793 | return NULL; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 794 | } |
| 795 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 796 | USBDevice *usb_host_device_open(const char *devname) |
| 797 | { |
| 798 | int bus_num, addr; |
| 799 | char product_name[PRODUCT_NAME_SZ]; |
| 800 | |
| 801 | if (usb_host_find_device(&bus_num, &addr, |
| 802 | product_name, sizeof(product_name), |
| 803 | devname) < 0) |
| 804 | return NULL; |
| 805 | |
| 806 | if (hostdev_find(bus_num, addr)) { |
| 807 | printf("host usb device %d.%d is already open\n", bus_num, addr); |
| 808 | return NULL; |
| 809 | } |
| 810 | |
| 811 | return usb_host_device_open_addr(bus_num, addr, product_name); |
| 812 | } |
| 813 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 814 | static int get_tag_value(char *buf, int buf_size, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 815 | const char *str, const char *tag, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 816 | const char *stopchars) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 817 | { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 818 | const char *p; |
| 819 | char *q; |
| 820 | p = strstr(str, tag); |
| 821 | if (!p) |
| 822 | return -1; |
| 823 | p += strlen(tag); |
| 824 | while (isspace(*p)) |
| 825 | p++; |
| 826 | q = buf; |
| 827 | while (*p != '\0' && !strchr(stopchars, *p)) { |
| 828 | if ((q - buf) < (buf_size - 1)) |
| 829 | *q++ = *p; |
| 830 | p++; |
| 831 | } |
| 832 | *q = '\0'; |
| 833 | return q - buf; |
| 834 | } |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 835 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 836 | static int usb_host_scan(void *opaque, USBScanFunc *func) |
| 837 | { |
| 838 | FILE *f; |
| 839 | char line[1024]; |
| 840 | char buf[1024]; |
| 841 | int bus_num, addr, speed, device_count, class_id, product_id, vendor_id; |
| 842 | int ret; |
| 843 | char product_name[512]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 844 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 845 | f = fopen(USBDEVFS_PATH "/devices", "r"); |
| 846 | if (!f) { |
| 847 | term_printf("Could not open %s\n", USBDEVFS_PATH "/devices"); |
| 848 | return 0; |
| 849 | } |
| 850 | device_count = 0; |
| 851 | bus_num = addr = speed = class_id = product_id = vendor_id = 0; |
| 852 | ret = 0; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 853 | for(;;) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 854 | if (fgets(line, sizeof(line), f) == NULL) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 855 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 856 | if (strlen(line) > 0) |
| 857 | line[strlen(line) - 1] = '\0'; |
| 858 | if (line[0] == 'T' && line[1] == ':') { |
pbrook | 38ca0f6 | 2006-03-11 18:03:38 +0000 | [diff] [blame] | 859 | if (device_count && (vendor_id || product_id)) { |
| 860 | /* New device. Add the previously discovered device. */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 861 | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 862 | product_id, product_name, speed); |
| 863 | if (ret) |
| 864 | goto the_end; |
| 865 | } |
| 866 | if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) |
| 867 | goto fail; |
| 868 | bus_num = atoi(buf); |
| 869 | if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) |
| 870 | goto fail; |
| 871 | addr = atoi(buf); |
| 872 | if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) |
| 873 | goto fail; |
| 874 | if (!strcmp(buf, "480")) |
| 875 | speed = USB_SPEED_HIGH; |
| 876 | else if (!strcmp(buf, "1.5")) |
| 877 | speed = USB_SPEED_LOW; |
| 878 | else |
| 879 | speed = USB_SPEED_FULL; |
| 880 | product_name[0] = '\0'; |
| 881 | class_id = 0xff; |
| 882 | device_count++; |
| 883 | product_id = 0; |
| 884 | vendor_id = 0; |
| 885 | } else if (line[0] == 'P' && line[1] == ':') { |
| 886 | if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) |
| 887 | goto fail; |
| 888 | vendor_id = strtoul(buf, NULL, 16); |
| 889 | if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) |
| 890 | goto fail; |
| 891 | product_id = strtoul(buf, NULL, 16); |
| 892 | } else if (line[0] == 'S' && line[1] == ':') { |
| 893 | if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) |
| 894 | goto fail; |
| 895 | pstrcpy(product_name, sizeof(product_name), buf); |
| 896 | } else if (line[0] == 'D' && line[1] == ':') { |
| 897 | if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) |
| 898 | goto fail; |
| 899 | class_id = strtoul(buf, NULL, 16); |
| 900 | } |
| 901 | fail: ; |
| 902 | } |
pbrook | 38ca0f6 | 2006-03-11 18:03:38 +0000 | [diff] [blame] | 903 | if (device_count && (vendor_id || product_id)) { |
| 904 | /* Add the last device. */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 905 | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 906 | product_id, product_name, speed); |
| 907 | } |
| 908 | the_end: |
| 909 | fclose(f); |
| 910 | return ret; |
| 911 | } |
| 912 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 913 | struct USBAutoFilter { |
| 914 | struct USBAutoFilter *next; |
| 915 | int bus_num; |
| 916 | int addr; |
| 917 | int vendor_id; |
| 918 | int product_id; |
| 919 | }; |
| 920 | |
| 921 | static QEMUTimer *usb_auto_timer; |
| 922 | static struct USBAutoFilter *usb_auto_filter; |
| 923 | |
| 924 | static int usb_host_auto_scan(void *opaque, int bus_num, int addr, |
| 925 | int class_id, int vendor_id, int product_id, |
| 926 | const char *product_name, int speed) |
| 927 | { |
| 928 | struct USBAutoFilter *f; |
| 929 | struct USBDevice *dev; |
| 930 | |
| 931 | /* Ignore hubs */ |
| 932 | if (class_id == 9) |
| 933 | return 0; |
| 934 | |
| 935 | for (f = usb_auto_filter; f; f = f->next) { |
| 936 | // printf("Auto match: bus_num %d addr %d vid %d pid %d\n", |
| 937 | // bus_num, addr, vendor_id, product_id); |
| 938 | |
| 939 | if (f->bus_num >= 0 && f->bus_num != bus_num) |
| 940 | continue; |
| 941 | |
| 942 | if (f->addr >= 0 && f->addr != addr) |
| 943 | continue; |
| 944 | |
| 945 | if (f->vendor_id >= 0 && f->vendor_id != vendor_id) |
| 946 | continue; |
| 947 | |
| 948 | if (f->product_id >= 0 && f->product_id != product_id) |
| 949 | continue; |
| 950 | |
| 951 | /* We got a match */ |
| 952 | |
| 953 | /* Allredy attached ? */ |
| 954 | if (hostdev_find(bus_num, addr)) |
| 955 | return 0; |
| 956 | |
| 957 | printf("Auto open: bus_num %d addr %d\n", bus_num, addr); |
| 958 | |
| 959 | dev = usb_host_device_open_addr(bus_num, addr, product_name); |
| 960 | if (dev) |
| 961 | usb_device_add_dev(dev); |
| 962 | } |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static void usb_host_auto_timer(void *unused) |
| 968 | { |
| 969 | usb_host_scan(NULL, usb_host_auto_scan); |
| 970 | qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000); |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * Add autoconnect filter |
| 975 | * -1 means 'any' (device, vendor, etc) |
| 976 | */ |
| 977 | static void usb_host_auto_add(int bus_num, int addr, int vendor_id, int product_id) |
| 978 | { |
| 979 | struct USBAutoFilter *f = qemu_mallocz(sizeof(*f)); |
| 980 | if (!f) { |
| 981 | printf("Failed to allocate auto filter\n"); |
| 982 | return; |
| 983 | } |
| 984 | |
| 985 | f->bus_num = bus_num; |
| 986 | f->addr = addr; |
| 987 | f->vendor_id = vendor_id; |
| 988 | f->product_id = product_id; |
| 989 | |
| 990 | if (!usb_auto_filter) { |
| 991 | /* |
| 992 | * First entry. Init and start the monitor. |
| 993 | * Right now we're using timer to check for new devices. |
| 994 | * If this turns out to be too expensive we can move that into a |
| 995 | * separate thread. |
| 996 | */ |
| 997 | usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL); |
| 998 | if (!usb_auto_timer) { |
| 999 | printf("Failed to allocate timer\n"); |
| 1000 | qemu_free(f); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | /* Check for new devices every two seconds */ |
| 1005 | qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000); |
| 1006 | } |
| 1007 | |
| 1008 | printf("Auto filter: bus_num %d addr %d vid %d pid %d\n", |
| 1009 | bus_num, addr, vendor_id, product_id); |
| 1010 | |
| 1011 | f->next = usb_auto_filter; |
| 1012 | usb_auto_filter = f; |
| 1013 | } |
| 1014 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1015 | typedef struct FindDeviceState { |
| 1016 | int vendor_id; |
| 1017 | int product_id; |
| 1018 | int bus_num; |
| 1019 | int addr; |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 1020 | char product_name[PRODUCT_NAME_SZ]; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1021 | } FindDeviceState; |
| 1022 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1023 | static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1024 | int class_id, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1025 | int vendor_id, int product_id, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1026 | const char *product_name, int speed) |
| 1027 | { |
| 1028 | FindDeviceState *s = opaque; |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 1029 | if ((vendor_id == s->vendor_id && |
| 1030 | product_id == s->product_id) || |
| 1031 | (bus_num == s->bus_num && |
| 1032 | addr == s->addr)) { |
| 1033 | pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1034 | s->bus_num = bus_num; |
| 1035 | s->addr = addr; |
| 1036 | return 1; |
| 1037 | } else { |
| 1038 | return 0; |
| 1039 | } |
| 1040 | } |
| 1041 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1042 | /* the syntax is : |
| 1043 | 'bus.addr' (decimal numbers) or |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1044 | 'vendor_id:product_id' (hexa numbers) */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1045 | static int usb_host_find_device(int *pbus_num, int *paddr, |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 1046 | char *product_name, int product_name_size, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1047 | const char *devname) |
| 1048 | { |
| 1049 | const char *p; |
| 1050 | int ret; |
| 1051 | FindDeviceState fs; |
| 1052 | |
| 1053 | p = strchr(devname, '.'); |
| 1054 | if (p) { |
| 1055 | *pbus_num = strtoul(devname, NULL, 0); |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 1056 | |
| 1057 | if (*(p + 1) == '*') { |
| 1058 | usb_host_auto_add(*pbus_num, -1, -1, -1); |
| 1059 | return -1; |
| 1060 | } |
| 1061 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1062 | *paddr = strtoul(p + 1, NULL, 0); |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 1063 | fs.bus_num = *pbus_num; |
| 1064 | fs.addr = *paddr; |
| 1065 | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
| 1066 | if (ret) |
| 1067 | pstrcpy(product_name, product_name_size, fs.product_name); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1068 | return 0; |
| 1069 | } |
| 1070 | p = strchr(devname, ':'); |
| 1071 | if (p) { |
| 1072 | fs.vendor_id = strtoul(devname, NULL, 16); |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 1073 | |
| 1074 | if (*(p + 1) == '*') { |
| 1075 | usb_host_auto_add(-1, -1, fs.vendor_id, -1); |
| 1076 | return -1; |
| 1077 | } |
| 1078 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1079 | fs.product_id = strtoul(p + 1, NULL, 16); |
| 1080 | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
| 1081 | if (ret) { |
| 1082 | *pbus_num = fs.bus_num; |
| 1083 | *paddr = fs.addr; |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 1084 | pstrcpy(product_name, product_name_size, fs.product_name); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1085 | return 0; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1086 | } |
| 1087 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1088 | return -1; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1091 | /**********************/ |
| 1092 | /* USB host device info */ |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1093 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1094 | struct usb_class_info { |
| 1095 | int class; |
| 1096 | const char *class_name; |
| 1097 | }; |
| 1098 | |
| 1099 | static const struct usb_class_info usb_class_info[] = { |
| 1100 | { USB_CLASS_AUDIO, "Audio"}, |
| 1101 | { USB_CLASS_COMM, "Communication"}, |
| 1102 | { USB_CLASS_HID, "HID"}, |
| 1103 | { USB_CLASS_HUB, "Hub" }, |
| 1104 | { USB_CLASS_PHYSICAL, "Physical" }, |
| 1105 | { USB_CLASS_PRINTER, "Printer" }, |
| 1106 | { USB_CLASS_MASS_STORAGE, "Storage" }, |
| 1107 | { USB_CLASS_CDC_DATA, "Data" }, |
| 1108 | { USB_CLASS_APP_SPEC, "Application Specific" }, |
| 1109 | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" }, |
| 1110 | { USB_CLASS_STILL_IMAGE, "Still Image" }, |
balrog | b9dc033 | 2007-10-04 22:47:34 +0000 | [diff] [blame] | 1111 | { USB_CLASS_CSCID, "Smart Card" }, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1112 | { USB_CLASS_CONTENT_SEC, "Content Security" }, |
| 1113 | { -1, NULL } |
| 1114 | }; |
| 1115 | |
| 1116 | static const char *usb_class_str(uint8_t class) |
| 1117 | { |
| 1118 | const struct usb_class_info *p; |
| 1119 | for(p = usb_class_info; p->class != -1; p++) { |
| 1120 | if (p->class == class) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1121 | break; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1122 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1123 | return p->class_name; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1126 | static void usb_info_device(int bus_num, int addr, int class_id, |
| 1127 | int vendor_id, int product_id, |
| 1128 | const char *product_name, |
| 1129 | int speed) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1130 | { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1131 | const char *class_str, *speed_str; |
| 1132 | |
| 1133 | switch(speed) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1134 | case USB_SPEED_LOW: |
| 1135 | speed_str = "1.5"; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1136 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1137 | case USB_SPEED_FULL: |
| 1138 | speed_str = "12"; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1139 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1140 | case USB_SPEED_HIGH: |
| 1141 | speed_str = "480"; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1142 | break; |
| 1143 | default: |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1144 | speed_str = "?"; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1145 | break; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1146 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1147 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1148 | term_printf(" Device %d.%d, speed %s Mb/s\n", |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1149 | bus_num, addr, speed_str); |
| 1150 | class_str = usb_class_str(class_id); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1151 | if (class_str) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1152 | term_printf(" %s:", class_str); |
| 1153 | else |
| 1154 | term_printf(" Class %02x:", class_id); |
| 1155 | term_printf(" USB device %04x:%04x", vendor_id, product_id); |
| 1156 | if (product_name[0] != '\0') |
| 1157 | term_printf(", %s", product_name); |
| 1158 | term_printf("\n"); |
| 1159 | } |
| 1160 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1161 | static int usb_host_info_device(void *opaque, int bus_num, int addr, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1162 | int class_id, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1163 | int vendor_id, int product_id, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1164 | const char *product_name, |
| 1165 | int speed) |
| 1166 | { |
| 1167 | usb_info_device(bus_num, addr, class_id, vendor_id, product_id, |
| 1168 | product_name, speed); |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | void usb_host_info(void) |
| 1173 | { |
| 1174 | usb_host_scan(NULL, usb_host_info_device); |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame^] | 1177 | |
| 1178 | |
| 1179 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1180 | #else |
| 1181 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1182 | void usb_host_info(void) |
| 1183 | { |
| 1184 | term_printf("USB host devices not supported\n"); |
| 1185 | } |
| 1186 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1187 | /* XXX: modify configure to compile the right host driver */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1188 | USBDevice *usb_host_device_open(const char *devname) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 1189 | { |
| 1190 | return NULL; |
| 1191 | } |
| 1192 | |
| 1193 | #endif |