1 /* 2 * USB redirector usb-guest 3 * 4 * Copyright (c) 2011-2012 Red Hat, Inc. 5 * 6 * Red Hat Authors: 7 * Hans de Goede <hdegoede@redhat.com> 8 * 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 */ 27 28 #include "qemu-common.h" 29 #include "qemu/timer.h" 30 #include "monitor/monitor.h" 31 #include "sysemu/sysemu.h" 32 #include "qemu/iov.h" 33 #include "char/char.h" 34 35 #include <dirent.h> 36 #include <sys/ioctl.h> 37 #include <signal.h> 38 #include <usbredirparser.h> 39 #include <usbredirfilter.h> 40 41 #include "hw/usb.h" 42 43 #define MAX_ENDPOINTS 32 44 #define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */ 45 #define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f)) 46 #define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f)) 47 48 typedef struct USBRedirDevice USBRedirDevice; 49 50 /* Struct to hold buffered packets (iso or int input packets) */ 51 struct buf_packet { 52 uint8_t *data; 53 int len; 54 int status; 55 QTAILQ_ENTRY(buf_packet)next; 56 }; 57 58 struct endp_data { 59 uint8_t type; 60 uint8_t interval; 61 uint8_t interface; /* bInterfaceNumber this ep belongs to */ 62 uint16_t max_packet_size; /* In bytes, not wMaxPacketSize format !! */ 63 uint8_t iso_started; 64 uint8_t iso_error; /* For reporting iso errors to the HC */ 65 uint8_t interrupt_started; 66 uint8_t interrupt_error; 67 uint8_t bufpq_prefilled; 68 uint8_t bufpq_dropping_packets; 69 QTAILQ_HEAD(, buf_packet) bufpq; 70 int32_t bufpq_size; 71 int32_t bufpq_target_size; 72 }; 73 74 struct PacketIdQueueEntry { 75 uint64_t id; 76 QTAILQ_ENTRY(PacketIdQueueEntry)next; 77 }; 78 79 struct PacketIdQueue { 80 USBRedirDevice *dev; 81 const char *name; 82 QTAILQ_HEAD(, PacketIdQueueEntry) head; 83 int size; 84 }; 85 86 struct USBRedirDevice { 87 USBDevice dev; 88 /* Properties */ 89 CharDriverState *cs; 90 uint8_t debug; 91 char *filter_str; 92 int32_t bootindex; 93 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */ 94 const uint8_t *read_buf; 95 int read_buf_size; 96 /* For async handling of close */ 97 QEMUBH *chardev_close_bh; 98 /* To delay the usb attach in case of quick chardev close + open */ 99 QEMUTimer *attach_timer; 100 int64_t next_attach_time; 101 struct usbredirparser *parser; 102 struct endp_data endpoint[MAX_ENDPOINTS]; 103 struct PacketIdQueue cancelled; 104 struct PacketIdQueue already_in_flight; 105 /* Data for device filtering */ 106 struct usb_redir_device_connect_header device_info; 107 struct usb_redir_interface_info_header interface_info; 108 struct usbredirfilter_rule *filter_rules; 109 int filter_rules_count; 110 int compatible_speedmask; 111 }; 112 113 static void usbredir_hello(void *priv, struct usb_redir_hello_header *h); 114 static void usbredir_device_connect(void *priv, 115 struct usb_redir_device_connect_header *device_connect); 116 static void usbredir_device_disconnect(void *priv); 117 static void usbredir_interface_info(void *priv, 118 struct usb_redir_interface_info_header *interface_info); 119 static void usbredir_ep_info(void *priv, 120 struct usb_redir_ep_info_header *ep_info); 121 static void usbredir_configuration_status(void *priv, uint64_t id, 122 struct usb_redir_configuration_status_header *configuration_status); 123 static void usbredir_alt_setting_status(void *priv, uint64_t id, 124 struct usb_redir_alt_setting_status_header *alt_setting_status); 125 static void usbredir_iso_stream_status(void *priv, uint64_t id, 126 struct usb_redir_iso_stream_status_header *iso_stream_status); 127 static void usbredir_interrupt_receiving_status(void *priv, uint64_t id, 128 struct usb_redir_interrupt_receiving_status_header 129 *interrupt_receiving_status); 130 static void usbredir_bulk_streams_status(void *priv, uint64_t id, 131 struct usb_redir_bulk_streams_status_header *bulk_streams_status); 132 static void usbredir_control_packet(void *priv, uint64_t id, 133 struct usb_redir_control_packet_header *control_packet, 134 uint8_t *data, int data_len); 135 static void usbredir_bulk_packet(void *priv, uint64_t id, 136 struct usb_redir_bulk_packet_header *bulk_packet, 137 uint8_t *data, int data_len); 138 static void usbredir_iso_packet(void *priv, uint64_t id, 139 struct usb_redir_iso_packet_header *iso_packet, 140 uint8_t *data, int data_len); 141 static void usbredir_interrupt_packet(void *priv, uint64_t id, 142 struct usb_redir_interrupt_packet_header *interrupt_header, 143 uint8_t *data, int data_len); 144 145 static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p, 146 int status); 147 148 #define VERSION "qemu usb-redir guest " QEMU_VERSION 149 150 /* 151 * Logging stuff 152 */ 153 154 #define ERROR(...) \ 155 do { \ 156 if (dev->debug >= usbredirparser_error) { \ 157 error_report("usb-redir error: " __VA_ARGS__); \ 158 } \ 159 } while (0) 160 #define WARNING(...) \ 161 do { \ 162 if (dev->debug >= usbredirparser_warning) { \ 163 error_report("usb-redir warning: " __VA_ARGS__); \ 164 } \ 165 } while (0) 166 #define INFO(...) \ 167 do { \ 168 if (dev->debug >= usbredirparser_info) { \ 169 error_report("usb-redir: " __VA_ARGS__); \ 170 } \ 171 } while (0) 172 #define DPRINTF(...) \ 173 do { \ 174 if (dev->debug >= usbredirparser_debug) { \ 175 error_report("usb-redir: " __VA_ARGS__); \ 176 } \ 177 } while (0) 178 #define DPRINTF2(...) \ 179 do { \ 180 if (dev->debug >= usbredirparser_debug_data) { \ 181 error_report("usb-redir: " __VA_ARGS__); \ 182 } \ 183 } while (0) 184 185 static void usbredir_log(void *priv, int level, const char *msg) 186 { 187 USBRedirDevice *dev = priv; 188 189 if (dev->debug < level) { 190 return; 191 } 192 193 error_report("%s", msg); 194 } 195 196 static void usbredir_log_data(USBRedirDevice *dev, const char *desc, 197 const uint8_t *data, int len) 198 { 199 int i, j, n; 200 201 if (dev->debug < usbredirparser_debug_data) { 202 return; 203 } 204 205 for (i = 0; i < len; i += j) { 206 char buf[128]; 207 208 n = sprintf(buf, "%s", desc); 209 for (j = 0; j < 8 && i + j < len; j++) { 210 n += sprintf(buf + n, " %02X", data[i + j]); 211 } 212 error_report("%s", buf); 213 } 214 } 215 216 /* 217 * usbredirparser io functions 218 */ 219 220 static int usbredir_read(void *priv, uint8_t *data, int count) 221 { 222 USBRedirDevice *dev = priv; 223 224 if (dev->read_buf_size < count) { 225 count = dev->read_buf_size; 226 } 227 228 memcpy(data, dev->read_buf, count); 229 230 dev->read_buf_size -= count; 231 if (dev->read_buf_size) { 232 dev->read_buf += count; 233 } else { 234 dev->read_buf = NULL; 235 } 236 237 return count; 238 } 239 240 static int usbredir_write(void *priv, uint8_t *data, int count) 241 { 242 USBRedirDevice *dev = priv; 243 244 if (!dev->cs->opened) { 245 return 0; 246 } 247 248 /* Don't send new data to the chardev until our state is fully synced */ 249 if (!runstate_check(RUN_STATE_RUNNING)) { 250 return 0; 251 } 252 253 return qemu_chr_fe_write(dev->cs, data, count); 254 } 255 256 /* 257 * Cancelled and buffered packets helpers 258 */ 259 260 static void packet_id_queue_init(struct PacketIdQueue *q, 261 USBRedirDevice *dev, const char *name) 262 { 263 q->dev = dev; 264 q->name = name; 265 QTAILQ_INIT(&q->head); 266 q->size = 0; 267 } 268 269 static void packet_id_queue_add(struct PacketIdQueue *q, uint64_t id) 270 { 271 USBRedirDevice *dev = q->dev; 272 struct PacketIdQueueEntry *e; 273 274 DPRINTF("adding packet id %"PRIu64" to %s queue\n", id, q->name); 275 276 e = g_malloc0(sizeof(struct PacketIdQueueEntry)); 277 e->id = id; 278 QTAILQ_INSERT_TAIL(&q->head, e, next); 279 q->size++; 280 } 281 282 static int packet_id_queue_remove(struct PacketIdQueue *q, uint64_t id) 283 { 284 USBRedirDevice *dev = q->dev; 285 struct PacketIdQueueEntry *e; 286 287 QTAILQ_FOREACH(e, &q->head, next) { 288 if (e->id == id) { 289 DPRINTF("removing packet id %"PRIu64" from %s queue\n", 290 id, q->name); 291 QTAILQ_REMOVE(&q->head, e, next); 292 q->size--; 293 g_free(e); 294 return 1; 295 } 296 } 297 return 0; 298 } 299 300 static void packet_id_queue_empty(struct PacketIdQueue *q) 301 { 302 USBRedirDevice *dev = q->dev; 303 struct PacketIdQueueEntry *e, *next_e; 304 305 DPRINTF("removing %d packet-ids from %s queue\n", q->size, q->name); 306 307 QTAILQ_FOREACH_SAFE(e, &q->head, next, next_e) { 308 QTAILQ_REMOVE(&q->head, e, next); 309 g_free(e); 310 } 311 q->size = 0; 312 } 313 314 static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p) 315 { 316 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); 317 318 if (p->combined) { 319 usb_combined_packet_cancel(udev, p); 320 return; 321 } 322 323 packet_id_queue_add(&dev->cancelled, p->id); 324 usbredirparser_send_cancel_data_packet(dev->parser, p->id); 325 usbredirparser_do_write(dev->parser); 326 } 327 328 static int usbredir_is_cancelled(USBRedirDevice *dev, uint64_t id) 329 { 330 if (!dev->dev.attached) { 331 return 1; /* Treat everything as cancelled after a disconnect */ 332 } 333 return packet_id_queue_remove(&dev->cancelled, id); 334 } 335 336 static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice *dev, 337 struct USBEndpoint *ep) 338 { 339 static USBPacket *p; 340 341 QTAILQ_FOREACH(p, &ep->queue, queue) { 342 /* Skip combined packets, except for the first */ 343 if (p->combined && p != p->combined->first) { 344 continue; 345 } 346 if (p->state == USB_PACKET_ASYNC) { 347 packet_id_queue_add(&dev->already_in_flight, p->id); 348 } 349 } 350 } 351 352 static void usbredir_fill_already_in_flight(USBRedirDevice *dev) 353 { 354 int ep; 355 struct USBDevice *udev = &dev->dev; 356 357 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_ctl); 358 359 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { 360 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_in[ep]); 361 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_out[ep]); 362 } 363 } 364 365 static int usbredir_already_in_flight(USBRedirDevice *dev, uint64_t id) 366 { 367 return packet_id_queue_remove(&dev->already_in_flight, id); 368 } 369 370 static USBPacket *usbredir_find_packet_by_id(USBRedirDevice *dev, 371 uint8_t ep, uint64_t id) 372 { 373 USBPacket *p; 374 375 if (usbredir_is_cancelled(dev, id)) { 376 return NULL; 377 } 378 379 p = usb_ep_find_packet_by_id(&dev->dev, 380 (ep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT, 381 ep & 0x0f, id); 382 if (p == NULL) { 383 ERROR("could not find packet with id %"PRIu64"\n", id); 384 } 385 return p; 386 } 387 388 static void bufp_alloc(USBRedirDevice *dev, 389 uint8_t *data, int len, int status, uint8_t ep) 390 { 391 struct buf_packet *bufp; 392 393 if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets && 394 dev->endpoint[EP2I(ep)].bufpq_size > 395 2 * dev->endpoint[EP2I(ep)].bufpq_target_size) { 396 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep); 397 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1; 398 } 399 /* Since we're interupting the stream anyways, drop enough packets to get 400 back to our target buffer size */ 401 if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) { 402 if (dev->endpoint[EP2I(ep)].bufpq_size > 403 dev->endpoint[EP2I(ep)].bufpq_target_size) { 404 free(data); 405 return; 406 } 407 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; 408 } 409 410 bufp = g_malloc(sizeof(struct buf_packet)); 411 bufp->data = data; 412 bufp->len = len; 413 bufp->status = status; 414 QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next); 415 dev->endpoint[EP2I(ep)].bufpq_size++; 416 } 417 418 static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp, 419 uint8_t ep) 420 { 421 QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next); 422 dev->endpoint[EP2I(ep)].bufpq_size--; 423 free(bufp->data); 424 g_free(bufp); 425 } 426 427 static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep) 428 { 429 struct buf_packet *buf, *buf_next; 430 431 QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) { 432 bufp_free(dev, buf, ep); 433 } 434 } 435 436 /* 437 * USBDevice callbacks 438 */ 439 440 static void usbredir_handle_reset(USBDevice *udev) 441 { 442 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); 443 444 DPRINTF("reset device\n"); 445 usbredirparser_send_reset(dev->parser); 446 usbredirparser_do_write(dev->parser); 447 } 448 449 static void usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p, 450 uint8_t ep) 451 { 452 int status, len; 453 if (!dev->endpoint[EP2I(ep)].iso_started && 454 !dev->endpoint[EP2I(ep)].iso_error) { 455 struct usb_redir_start_iso_stream_header start_iso = { 456 .endpoint = ep, 457 }; 458 int pkts_per_sec; 459 460 if (dev->dev.speed == USB_SPEED_HIGH) { 461 pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval; 462 } else { 463 pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval; 464 } 465 /* Testing has shown that we need circa 60 ms buffer */ 466 dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000; 467 468 /* Aim for approx 100 interrupts / second on the client to 469 balance latency and interrupt load */ 470 start_iso.pkts_per_urb = pkts_per_sec / 100; 471 if (start_iso.pkts_per_urb < 1) { 472 start_iso.pkts_per_urb = 1; 473 } else if (start_iso.pkts_per_urb > 32) { 474 start_iso.pkts_per_urb = 32; 475 } 476 477 start_iso.no_urbs = (dev->endpoint[EP2I(ep)].bufpq_target_size + 478 start_iso.pkts_per_urb - 1) / 479 start_iso.pkts_per_urb; 480 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest 481 as overflow buffer. Also see the usbredir protocol documentation */ 482 if (!(ep & USB_DIR_IN)) { 483 start_iso.no_urbs *= 2; 484 } 485 if (start_iso.no_urbs > 16) { 486 start_iso.no_urbs = 16; 487 } 488 489 /* No id, we look at the ep when receiving a status back */ 490 usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso); 491 usbredirparser_do_write(dev->parser); 492 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n", 493 pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep); 494 dev->endpoint[EP2I(ep)].iso_started = 1; 495 dev->endpoint[EP2I(ep)].bufpq_prefilled = 0; 496 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; 497 } 498 499 if (ep & USB_DIR_IN) { 500 struct buf_packet *isop; 501 502 if (dev->endpoint[EP2I(ep)].iso_started && 503 !dev->endpoint[EP2I(ep)].bufpq_prefilled) { 504 if (dev->endpoint[EP2I(ep)].bufpq_size < 505 dev->endpoint[EP2I(ep)].bufpq_target_size) { 506 return; 507 } 508 dev->endpoint[EP2I(ep)].bufpq_prefilled = 1; 509 } 510 511 isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); 512 if (isop == NULL) { 513 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n", 514 ep, dev->endpoint[EP2I(ep)].iso_error); 515 /* Re-fill the buffer */ 516 dev->endpoint[EP2I(ep)].bufpq_prefilled = 0; 517 /* Check iso_error for stream errors, otherwise its an underrun */ 518 status = dev->endpoint[EP2I(ep)].iso_error; 519 dev->endpoint[EP2I(ep)].iso_error = 0; 520 p->status = status ? USB_RET_IOERROR : USB_RET_SUCCESS; 521 return; 522 } 523 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep, 524 isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size); 525 526 status = isop->status; 527 len = isop->len; 528 if (len > p->iov.size) { 529 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n", 530 ep, len, (int)p->iov.size); 531 len = p->iov.size; 532 status = usb_redir_babble; 533 } 534 usb_packet_copy(p, isop->data, len); 535 bufp_free(dev, isop, ep); 536 usbredir_handle_status(dev, p, status); 537 } else { 538 /* If the stream was not started because of a pending error don't 539 send the packet to the usb-host */ 540 if (dev->endpoint[EP2I(ep)].iso_started) { 541 struct usb_redir_iso_packet_header iso_packet = { 542 .endpoint = ep, 543 .length = p->iov.size 544 }; 545 uint8_t buf[p->iov.size]; 546 /* No id, we look at the ep when receiving a status back */ 547 usb_packet_copy(p, buf, p->iov.size); 548 usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet, 549 buf, p->iov.size); 550 usbredirparser_do_write(dev->parser); 551 } 552 status = dev->endpoint[EP2I(ep)].iso_error; 553 dev->endpoint[EP2I(ep)].iso_error = 0; 554 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status, 555 p->iov.size); 556 usbredir_handle_status(dev, p, status); 557 } 558 } 559 560 static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep) 561 { 562 struct usb_redir_stop_iso_stream_header stop_iso_stream = { 563 .endpoint = ep 564 }; 565 if (dev->endpoint[EP2I(ep)].iso_started) { 566 usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream); 567 DPRINTF("iso stream stopped ep %02X\n", ep); 568 dev->endpoint[EP2I(ep)].iso_started = 0; 569 } 570 dev->endpoint[EP2I(ep)].iso_error = 0; 571 usbredir_free_bufpq(dev, ep); 572 } 573 574 static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p, 575 uint8_t ep) 576 { 577 struct usb_redir_bulk_packet_header bulk_packet; 578 size_t size = (p->combined) ? p->combined->iov.size : p->iov.size; 579 580 DPRINTF("bulk-out ep %02X len %zd id %"PRIu64"\n", ep, size, p->id); 581 582 if (usbredir_already_in_flight(dev, p->id)) { 583 p->status = USB_RET_ASYNC; 584 return; 585 } 586 587 bulk_packet.endpoint = ep; 588 bulk_packet.length = size; 589 bulk_packet.stream_id = 0; 590 bulk_packet.length_high = size >> 16; 591 assert(bulk_packet.length_high == 0 || 592 usbredirparser_peer_has_cap(dev->parser, 593 usb_redir_cap_32bits_bulk_length)); 594 595 if (ep & USB_DIR_IN) { 596 usbredirparser_send_bulk_packet(dev->parser, p->id, 597 &bulk_packet, NULL, 0); 598 } else { 599 uint8_t buf[size]; 600 if (p->combined) { 601 iov_to_buf(p->combined->iov.iov, p->combined->iov.niov, 602 0, buf, size); 603 } else { 604 usb_packet_copy(p, buf, size); 605 } 606 usbredir_log_data(dev, "bulk data out:", buf, size); 607 usbredirparser_send_bulk_packet(dev->parser, p->id, 608 &bulk_packet, buf, size); 609 } 610 usbredirparser_do_write(dev->parser); 611 p->status = USB_RET_ASYNC; 612 } 613 614 static void usbredir_handle_interrupt_in_data(USBRedirDevice *dev, 615 USBPacket *p, uint8_t ep) 616 { 617 /* Input interrupt endpoint, buffered packet input */ 618 struct buf_packet *intp; 619 int status, len; 620 621 if (!dev->endpoint[EP2I(ep)].interrupt_started && 622 !dev->endpoint[EP2I(ep)].interrupt_error) { 623 struct usb_redir_start_interrupt_receiving_header start_int = { 624 .endpoint = ep, 625 }; 626 /* No id, we look at the ep when receiving a status back */ 627 usbredirparser_send_start_interrupt_receiving(dev->parser, 0, 628 &start_int); 629 usbredirparser_do_write(dev->parser); 630 DPRINTF("interrupt recv started ep %02X\n", ep); 631 dev->endpoint[EP2I(ep)].interrupt_started = 1; 632 /* We don't really want to drop interrupt packets ever, but 633 having some upper limit to how much we buffer is good. */ 634 dev->endpoint[EP2I(ep)].bufpq_target_size = 1000; 635 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; 636 } 637 638 intp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); 639 if (intp == NULL) { 640 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep); 641 /* Check interrupt_error for stream errors */ 642 status = dev->endpoint[EP2I(ep)].interrupt_error; 643 dev->endpoint[EP2I(ep)].interrupt_error = 0; 644 if (status) { 645 usbredir_handle_status(dev, p, status); 646 } else { 647 p->status = USB_RET_NAK; 648 } 649 return; 650 } 651 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep, 652 intp->status, intp->len); 653 654 status = intp->status; 655 len = intp->len; 656 if (len > p->iov.size) { 657 ERROR("received int data is larger then packet ep %02X\n", ep); 658 len = p->iov.size; 659 status = usb_redir_babble; 660 } 661 usb_packet_copy(p, intp->data, len); 662 bufp_free(dev, intp, ep); 663 usbredir_handle_status(dev, p, status); 664 } 665 666 /* 667 * Handle interrupt out data, the usbredir protocol expects us to do this 668 * async, so that it can report back a completion status. But guests will 669 * expect immediate completion for an interrupt endpoint, and handling this 670 * async causes migration issues. So we report success directly, counting 671 * on the fact that output interrupt packets normally always succeed. 672 */ 673 static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev, 674 USBPacket *p, uint8_t ep) 675 { 676 struct usb_redir_interrupt_packet_header interrupt_packet; 677 uint8_t buf[p->iov.size]; 678 679 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep, 680 p->iov.size, p->id); 681 682 interrupt_packet.endpoint = ep; 683 interrupt_packet.length = p->iov.size; 684 685 usb_packet_copy(p, buf, p->iov.size); 686 usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size); 687 usbredirparser_send_interrupt_packet(dev->parser, p->id, 688 &interrupt_packet, buf, p->iov.size); 689 usbredirparser_do_write(dev->parser); 690 } 691 692 static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev, 693 uint8_t ep) 694 { 695 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = { 696 .endpoint = ep 697 }; 698 if (dev->endpoint[EP2I(ep)].interrupt_started) { 699 usbredirparser_send_stop_interrupt_receiving(dev->parser, 0, 700 &stop_interrupt_recv); 701 DPRINTF("interrupt recv stopped ep %02X\n", ep); 702 dev->endpoint[EP2I(ep)].interrupt_started = 0; 703 } 704 dev->endpoint[EP2I(ep)].interrupt_error = 0; 705 usbredir_free_bufpq(dev, ep); 706 } 707 708 static void usbredir_handle_data(USBDevice *udev, USBPacket *p) 709 { 710 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); 711 uint8_t ep; 712 713 ep = p->ep->nr; 714 if (p->pid == USB_TOKEN_IN) { 715 ep |= USB_DIR_IN; 716 } 717 718 switch (dev->endpoint[EP2I(ep)].type) { 719 case USB_ENDPOINT_XFER_CONTROL: 720 ERROR("handle_data called for control transfer on ep %02X\n", ep); 721 p->status = USB_RET_NAK; 722 break; 723 case USB_ENDPOINT_XFER_ISOC: 724 usbredir_handle_iso_data(dev, p, ep); 725 break; 726 case USB_ENDPOINT_XFER_BULK: 727 if (p->state == USB_PACKET_SETUP && p->pid == USB_TOKEN_IN && 728 p->ep->pipeline) { 729 p->status = USB_RET_ADD_TO_QUEUE; 730 break; 731 } 732 usbredir_handle_bulk_data(dev, p, ep); 733 break; 734 case USB_ENDPOINT_XFER_INT: 735 if (ep & USB_DIR_IN) { 736 usbredir_handle_interrupt_in_data(dev, p, ep); 737 } else { 738 usbredir_handle_interrupt_out_data(dev, p, ep); 739 } 740 break; 741 default: 742 ERROR("handle_data ep %02X has unknown type %d\n", ep, 743 dev->endpoint[EP2I(ep)].type); 744 p->status = USB_RET_NAK; 745 } 746 } 747 748 static void usbredir_flush_ep_queue(USBDevice *dev, USBEndpoint *ep) 749 { 750 if (ep->pid == USB_TOKEN_IN && ep->pipeline) { 751 usb_ep_combine_input_packets(ep); 752 } 753 } 754 755 static void usbredir_set_config(USBRedirDevice *dev, USBPacket *p, 756 int config) 757 { 758 struct usb_redir_set_configuration_header set_config; 759 int i; 760 761 DPRINTF("set config %d id %"PRIu64"\n", config, p->id); 762 763 for (i = 0; i < MAX_ENDPOINTS; i++) { 764 switch (dev->endpoint[i].type) { 765 case USB_ENDPOINT_XFER_ISOC: 766 usbredir_stop_iso_stream(dev, I2EP(i)); 767 break; 768 case USB_ENDPOINT_XFER_INT: 769 if (i & 0x10) { 770 usbredir_stop_interrupt_receiving(dev, I2EP(i)); 771 } 772 break; 773 } 774 usbredir_free_bufpq(dev, I2EP(i)); 775 } 776 777 set_config.configuration = config; 778 usbredirparser_send_set_configuration(dev->parser, p->id, &set_config); 779 usbredirparser_do_write(dev->parser); 780 p->status = USB_RET_ASYNC; 781 } 782 783 static void usbredir_get_config(USBRedirDevice *dev, USBPacket *p) 784 { 785 DPRINTF("get config id %"PRIu64"\n", p->id); 786 787 usbredirparser_send_get_configuration(dev->parser, p->id); 788 usbredirparser_do_write(dev->parser); 789 p->status = USB_RET_ASYNC; 790 } 791 792 static void usbredir_set_interface(USBRedirDevice *dev, USBPacket *p, 793 int interface, int alt) 794 { 795 struct usb_redir_set_alt_setting_header set_alt; 796 int i; 797 798 DPRINTF("set interface %d alt %d id %"PRIu64"\n", interface, alt, p->id); 799 800 for (i = 0; i < MAX_ENDPOINTS; i++) { 801 if (dev->endpoint[i].interface == interface) { 802 switch (dev->endpoint[i].type) { 803 case USB_ENDPOINT_XFER_ISOC: 804 usbredir_stop_iso_stream(dev, I2EP(i)); 805 break; 806 case USB_ENDPOINT_XFER_INT: 807 if (i & 0x10) { 808 usbredir_stop_interrupt_receiving(dev, I2EP(i)); 809 } 810 break; 811 } 812 usbredir_free_bufpq(dev, I2EP(i)); 813 } 814 } 815 816 set_alt.interface = interface; 817 set_alt.alt = alt; 818 usbredirparser_send_set_alt_setting(dev->parser, p->id, &set_alt); 819 usbredirparser_do_write(dev->parser); 820 p->status = USB_RET_ASYNC; 821 } 822 823 static void usbredir_get_interface(USBRedirDevice *dev, USBPacket *p, 824 int interface) 825 { 826 struct usb_redir_get_alt_setting_header get_alt; 827 828 DPRINTF("get interface %d id %"PRIu64"\n", interface, p->id); 829 830 get_alt.interface = interface; 831 usbredirparser_send_get_alt_setting(dev->parser, p->id, &get_alt); 832 usbredirparser_do_write(dev->parser); 833 p->status = USB_RET_ASYNC; 834 } 835 836 static void usbredir_handle_control(USBDevice *udev, USBPacket *p, 837 int request, int value, int index, int length, uint8_t *data) 838 { 839 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); 840 struct usb_redir_control_packet_header control_packet; 841 842 if (usbredir_already_in_flight(dev, p->id)) { 843 p->status = USB_RET_ASYNC; 844 return; 845 } 846 847 /* Special cases for certain standard device requests */ 848 switch (request) { 849 case DeviceOutRequest | USB_REQ_SET_ADDRESS: 850 DPRINTF("set address %d\n", value); 851 dev->dev.addr = value; 852 return; 853 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: 854 usbredir_set_config(dev, p, value & 0xff); 855 return; 856 case DeviceRequest | USB_REQ_GET_CONFIGURATION: 857 usbredir_get_config(dev, p); 858 return; 859 case InterfaceOutRequest | USB_REQ_SET_INTERFACE: 860 usbredir_set_interface(dev, p, index, value); 861 return; 862 case InterfaceRequest | USB_REQ_GET_INTERFACE: 863 usbredir_get_interface(dev, p, index); 864 return; 865 } 866 867 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */ 868 DPRINTF( 869 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64"\n", 870 request >> 8, request & 0xff, value, index, length, p->id); 871 872 control_packet.request = request & 0xFF; 873 control_packet.requesttype = request >> 8; 874 control_packet.endpoint = control_packet.requesttype & USB_DIR_IN; 875 control_packet.value = value; 876 control_packet.index = index; 877 control_packet.length = length; 878 879 if (control_packet.requesttype & USB_DIR_IN) { 880 usbredirparser_send_control_packet(dev->parser, p->id, 881 &control_packet, NULL, 0); 882 } else { 883 usbredir_log_data(dev, "ctrl data out:", data, length); 884 usbredirparser_send_control_packet(dev->parser, p->id, 885 &control_packet, data, length); 886 } 887 usbredirparser_do_write(dev->parser); 888 p->status = USB_RET_ASYNC; 889 } 890 891 /* 892 * Close events can be triggered by usbredirparser_do_write which gets called 893 * from within the USBDevice data / control packet callbacks and doing a 894 * usb_detach from within these callbacks is not a good idea. 895 * 896 * So we use a bh handler to take care of close events. 897 */ 898 static void usbredir_chardev_close_bh(void *opaque) 899 { 900 USBRedirDevice *dev = opaque; 901 902 usbredir_device_disconnect(dev); 903 904 if (dev->parser) { 905 DPRINTF("destroying usbredirparser\n"); 906 usbredirparser_destroy(dev->parser); 907 dev->parser = NULL; 908 } 909 } 910 911 static void usbredir_create_parser(USBRedirDevice *dev) 912 { 913 uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, }; 914 int flags = 0; 915 916 DPRINTF("creating usbredirparser\n"); 917 918 dev->parser = qemu_oom_check(usbredirparser_create()); 919 dev->parser->priv = dev; 920 dev->parser->log_func = usbredir_log; 921 dev->parser->read_func = usbredir_read; 922 dev->parser->write_func = usbredir_write; 923 dev->parser->hello_func = usbredir_hello; 924 dev->parser->device_connect_func = usbredir_device_connect; 925 dev->parser->device_disconnect_func = usbredir_device_disconnect; 926 dev->parser->interface_info_func = usbredir_interface_info; 927 dev->parser->ep_info_func = usbredir_ep_info; 928 dev->parser->configuration_status_func = usbredir_configuration_status; 929 dev->parser->alt_setting_status_func = usbredir_alt_setting_status; 930 dev->parser->iso_stream_status_func = usbredir_iso_stream_status; 931 dev->parser->interrupt_receiving_status_func = 932 usbredir_interrupt_receiving_status; 933 dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status; 934 dev->parser->control_packet_func = usbredir_control_packet; 935 dev->parser->bulk_packet_func = usbredir_bulk_packet; 936 dev->parser->iso_packet_func = usbredir_iso_packet; 937 dev->parser->interrupt_packet_func = usbredir_interrupt_packet; 938 dev->read_buf = NULL; 939 dev->read_buf_size = 0; 940 941 usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version); 942 usbredirparser_caps_set_cap(caps, usb_redir_cap_filter); 943 usbredirparser_caps_set_cap(caps, usb_redir_cap_ep_info_max_packet_size); 944 usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids); 945 usbredirparser_caps_set_cap(caps, usb_redir_cap_32bits_bulk_length); 946 947 if (runstate_check(RUN_STATE_INMIGRATE)) { 948 flags |= usbredirparser_fl_no_hello; 949 } 950 usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE, 951 flags); 952 usbredirparser_do_write(dev->parser); 953 } 954 955 static void usbredir_reject_device(USBRedirDevice *dev) 956 { 957 usbredir_device_disconnect(dev); 958 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) { 959 usbredirparser_send_filter_reject(dev->parser); 960 usbredirparser_do_write(dev->parser); 961 } 962 } 963 964 static void usbredir_do_attach(void *opaque) 965 { 966 USBRedirDevice *dev = opaque; 967 968 /* In order to work properly with XHCI controllers we need these caps */ 969 if ((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER) && !( 970 usbredirparser_peer_has_cap(dev->parser, 971 usb_redir_cap_ep_info_max_packet_size) && 972 usbredirparser_peer_has_cap(dev->parser, 973 usb_redir_cap_64bits_ids))) { 974 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n"); 975 usbredir_reject_device(dev); 976 return; 977 } 978 979 if (usb_device_attach(&dev->dev) != 0) { 980 WARNING("rejecting device due to speed mismatch\n"); 981 usbredir_reject_device(dev); 982 } 983 } 984 985 /* 986 * chardev callbacks 987 */ 988 989 static int usbredir_chardev_can_read(void *opaque) 990 { 991 USBRedirDevice *dev = opaque; 992 993 if (!dev->parser) { 994 WARNING("chardev_can_read called on non open chardev!\n"); 995 return 0; 996 } 997 998 /* Don't read new data from the chardev until our state is fully synced */ 999 if (!runstate_check(RUN_STATE_RUNNING)) { 1000 return 0; 1001 } 1002 1003 /* usbredir_parser_do_read will consume *all* data we give it */ 1004 return 1024 * 1024; 1005 } 1006 1007 static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size) 1008 { 1009 USBRedirDevice *dev = opaque; 1010 1011 /* No recursion allowed! */ 1012 assert(dev->read_buf == NULL); 1013 1014 dev->read_buf = buf; 1015 dev->read_buf_size = size; 1016 1017 usbredirparser_do_read(dev->parser); 1018 /* Send any acks, etc. which may be queued now */ 1019 usbredirparser_do_write(dev->parser); 1020 } 1021 1022 static void usbredir_chardev_event(void *opaque, int event) 1023 { 1024 USBRedirDevice *dev = opaque; 1025 1026 switch (event) { 1027 case CHR_EVENT_OPENED: 1028 DPRINTF("chardev open\n"); 1029 /* Make sure any pending closes are handled (no-op if none pending) */ 1030 usbredir_chardev_close_bh(dev); 1031 qemu_bh_cancel(dev->chardev_close_bh); 1032 usbredir_create_parser(dev); 1033 break; 1034 case CHR_EVENT_CLOSED: 1035 DPRINTF("chardev close\n"); 1036 qemu_bh_schedule(dev->chardev_close_bh); 1037 break; 1038 } 1039 } 1040 1041 /* 1042 * init + destroy 1043 */ 1044 1045 static void usbredir_vm_state_change(void *priv, int running, RunState state) 1046 { 1047 USBRedirDevice *dev = priv; 1048 1049 if (state == RUN_STATE_RUNNING && dev->parser != NULL) { 1050 usbredirparser_do_write(dev->parser); /* Flush any pending writes */ 1051 } 1052 } 1053 1054 static int usbredir_initfn(USBDevice *udev) 1055 { 1056 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); 1057 int i; 1058 1059 if (dev->cs == NULL) { 1060 qerror_report(QERR_MISSING_PARAMETER, "chardev"); 1061 return -1; 1062 } 1063 1064 if (dev->filter_str) { 1065 i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|", 1066 &dev->filter_rules, 1067 &dev->filter_rules_count); 1068 if (i) { 1069 qerror_report(QERR_INVALID_PARAMETER_VALUE, "filter", 1070 "a usb device filter string"); 1071 return -1; 1072 } 1073 } 1074 1075 dev->chardev_close_bh = qemu_bh_new(usbredir_chardev_close_bh, dev); 1076 dev->attach_timer = qemu_new_timer_ms(vm_clock, usbredir_do_attach, dev); 1077 1078 packet_id_queue_init(&dev->cancelled, dev, "cancelled"); 1079 packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight"); 1080 for (i = 0; i < MAX_ENDPOINTS; i++) { 1081 QTAILQ_INIT(&dev->endpoint[i].bufpq); 1082 } 1083 1084 /* We'll do the attach once we receive the speed from the usb-host */ 1085 udev->auto_attach = 0; 1086 1087 /* Will be cleared during setup when we find conflicts */ 1088 dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH; 1089 1090 /* Let the backend know we are ready */ 1091 qemu_chr_fe_open(dev->cs); 1092 qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read, 1093 usbredir_chardev_read, usbredir_chardev_event, dev); 1094 1095 qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev); 1096 add_boot_device_path(dev->bootindex, &udev->qdev, NULL); 1097 return 0; 1098 } 1099 1100 static void usbredir_cleanup_device_queues(USBRedirDevice *dev) 1101 { 1102 int i; 1103 1104 packet_id_queue_empty(&dev->cancelled); 1105 packet_id_queue_empty(&dev->already_in_flight); 1106 for (i = 0; i < MAX_ENDPOINTS; i++) { 1107 usbredir_free_bufpq(dev, I2EP(i)); 1108 } 1109 } 1110 1111 static void usbredir_handle_destroy(USBDevice *udev) 1112 { 1113 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); 1114 1115 qemu_chr_fe_close(dev->cs); 1116 qemu_chr_delete(dev->cs); 1117 /* Note must be done after qemu_chr_close, as that causes a close event */ 1118 qemu_bh_delete(dev->chardev_close_bh); 1119 1120 qemu_del_timer(dev->attach_timer); 1121 qemu_free_timer(dev->attach_timer); 1122 1123 usbredir_cleanup_device_queues(dev); 1124 1125 if (dev->parser) { 1126 usbredirparser_destroy(dev->parser); 1127 } 1128 1129 free(dev->filter_rules); 1130 } 1131 1132 static int usbredir_check_filter(USBRedirDevice *dev) 1133 { 1134 if (dev->interface_info.interface_count == NO_INTERFACE_INFO) { 1135 ERROR("No interface info for device\n"); 1136 goto error; 1137 } 1138 1139 if (dev->filter_rules) { 1140 if (!usbredirparser_peer_has_cap(dev->parser, 1141 usb_redir_cap_connect_device_version)) { 1142 ERROR("Device filter specified and peer does not have the " 1143 "connect_device_version capability\n"); 1144 goto error; 1145 } 1146 1147 if (usbredirfilter_check( 1148 dev->filter_rules, 1149 dev->filter_rules_count, 1150 dev->device_info.device_class, 1151 dev->device_info.device_subclass, 1152 dev->device_info.device_protocol, 1153 dev->interface_info.interface_class, 1154 dev->interface_info.interface_subclass, 1155 dev->interface_info.interface_protocol, 1156 dev->interface_info.interface_count, 1157 dev->device_info.vendor_id, 1158 dev->device_info.product_id, 1159 dev->device_info.device_version_bcd, 1160 0) != 0) { 1161 goto error; 1162 } 1163 } 1164 1165 return 0; 1166 1167 error: 1168 usbredir_reject_device(dev); 1169 return -1; 1170 } 1171 1172 /* 1173 * usbredirparser packet complete callbacks 1174 */ 1175 1176 static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p, 1177 int status) 1178 { 1179 switch (status) { 1180 case usb_redir_success: 1181 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ 1182 break; 1183 case usb_redir_stall: 1184 p->status = USB_RET_STALL; 1185 break; 1186 case usb_redir_cancelled: 1187 /* 1188 * When the usbredir-host unredirects a device, it will report a status 1189 * of cancelled for all pending packets, followed by a disconnect msg. 1190 */ 1191 p->status = USB_RET_IOERROR; 1192 break; 1193 case usb_redir_inval: 1194 WARNING("got invalid param error from usb-host?\n"); 1195 p->status = USB_RET_IOERROR; 1196 break; 1197 case usb_redir_babble: 1198 p->status = USB_RET_BABBLE; 1199 break; 1200 case usb_redir_ioerror: 1201 case usb_redir_timeout: 1202 default: 1203 p->status = USB_RET_IOERROR; 1204 } 1205 } 1206 1207 static void usbredir_hello(void *priv, struct usb_redir_hello_header *h) 1208 { 1209 USBRedirDevice *dev = priv; 1210 1211 /* Try to send the filter info now that we've the usb-host's caps */ 1212 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) && 1213 dev->filter_rules) { 1214 usbredirparser_send_filter_filter(dev->parser, dev->filter_rules, 1215 dev->filter_rules_count); 1216 usbredirparser_do_write(dev->parser); 1217 } 1218 } 1219 1220 static void usbredir_device_connect(void *priv, 1221 struct usb_redir_device_connect_header *device_connect) 1222 { 1223 USBRedirDevice *dev = priv; 1224 const char *speed; 1225 1226 if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) { 1227 ERROR("Received device connect while already connected\n"); 1228 return; 1229 } 1230 1231 switch (device_connect->speed) { 1232 case usb_redir_speed_low: 1233 speed = "low speed"; 1234 dev->dev.speed = USB_SPEED_LOW; 1235 dev->compatible_speedmask &= ~USB_SPEED_MASK_FULL; 1236 dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH; 1237 break; 1238 case usb_redir_speed_full: 1239 speed = "full speed"; 1240 dev->dev.speed = USB_SPEED_FULL; 1241 dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH; 1242 break; 1243 case usb_redir_speed_high: 1244 speed = "high speed"; 1245 dev->dev.speed = USB_SPEED_HIGH; 1246 break; 1247 case usb_redir_speed_super: 1248 speed = "super speed"; 1249 dev->dev.speed = USB_SPEED_SUPER; 1250 break; 1251 default: 1252 speed = "unknown speed"; 1253 dev->dev.speed = USB_SPEED_FULL; 1254 } 1255 1256 if (usbredirparser_peer_has_cap(dev->parser, 1257 usb_redir_cap_connect_device_version)) { 1258 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n", 1259 speed, device_connect->vendor_id, device_connect->product_id, 1260 ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 + 1261 ((device_connect->device_version_bcd & 0x0f00) >> 8), 1262 ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 + 1263 ((device_connect->device_version_bcd & 0x000f) >> 0), 1264 device_connect->device_class); 1265 } else { 1266 INFO("attaching %s device %04x:%04x class %02x\n", speed, 1267 device_connect->vendor_id, device_connect->product_id, 1268 device_connect->device_class); 1269 } 1270 1271 dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask; 1272 dev->device_info = *device_connect; 1273 1274 if (usbredir_check_filter(dev)) { 1275 WARNING("Device %04x:%04x rejected by device filter, not attaching\n", 1276 device_connect->vendor_id, device_connect->product_id); 1277 return; 1278 } 1279 1280 qemu_mod_timer(dev->attach_timer, dev->next_attach_time); 1281 } 1282 1283 static void usbredir_device_disconnect(void *priv) 1284 { 1285 USBRedirDevice *dev = priv; 1286 int i; 1287 1288 /* Stop any pending attaches */ 1289 qemu_del_timer(dev->attach_timer); 1290 1291 if (dev->dev.attached) { 1292 DPRINTF("detaching device\n"); 1293 usb_device_detach(&dev->dev); 1294 /* 1295 * Delay next usb device attach to give the guest a chance to see 1296 * see the detach / attach in case of quick close / open succession 1297 */ 1298 dev->next_attach_time = qemu_get_clock_ms(vm_clock) + 200; 1299 } 1300 1301 /* Reset state so that the next dev connected starts with a clean slate */ 1302 usbredir_cleanup_device_queues(dev); 1303 memset(dev->endpoint, 0, sizeof(dev->endpoint)); 1304 for (i = 0; i < MAX_ENDPOINTS; i++) { 1305 QTAILQ_INIT(&dev->endpoint[i].bufpq); 1306 } 1307 usb_ep_init(&dev->dev); 1308 dev->interface_info.interface_count = NO_INTERFACE_INFO; 1309 dev->dev.addr = 0; 1310 dev->dev.speed = 0; 1311 dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH; 1312 } 1313 1314 static void usbredir_interface_info(void *priv, 1315 struct usb_redir_interface_info_header *interface_info) 1316 { 1317 USBRedirDevice *dev = priv; 1318 1319 dev->interface_info = *interface_info; 1320 1321 /* 1322 * If we receive interface info after the device has already been 1323 * connected (ie on a set_config), re-check the filter. 1324 */ 1325 if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) { 1326 if (usbredir_check_filter(dev)) { 1327 ERROR("Device no longer matches filter after interface info " 1328 "change, disconnecting!\n"); 1329 } 1330 } 1331 } 1332 1333 static void usbredir_mark_speed_incompatible(USBRedirDevice *dev, int speed) 1334 { 1335 dev->compatible_speedmask &= ~(1 << speed); 1336 dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask; 1337 } 1338 1339 static void usbredir_set_pipeline(USBRedirDevice *dev, struct USBEndpoint *uep) 1340 { 1341 if (uep->type != USB_ENDPOINT_XFER_BULK) { 1342 return; 1343 } 1344 if (uep->pid == USB_TOKEN_OUT) { 1345 uep->pipeline = true; 1346 } 1347 if (uep->pid == USB_TOKEN_IN && uep->max_packet_size != 0 && 1348 usbredirparser_peer_has_cap(dev->parser, 1349 usb_redir_cap_32bits_bulk_length)) { 1350 uep->pipeline = true; 1351 } 1352 } 1353 1354 static void usbredir_setup_usb_eps(USBRedirDevice *dev) 1355 { 1356 struct USBEndpoint *usb_ep; 1357 int i, pid; 1358 1359 for (i = 0; i < MAX_ENDPOINTS; i++) { 1360 pid = (i & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT; 1361 usb_ep = usb_ep_get(&dev->dev, pid, i & 0x0f); 1362 usb_ep->type = dev->endpoint[i].type; 1363 usb_ep->ifnum = dev->endpoint[i].interface; 1364 usb_ep->max_packet_size = dev->endpoint[i].max_packet_size; 1365 usbredir_set_pipeline(dev, usb_ep); 1366 } 1367 } 1368 1369 static void usbredir_ep_info(void *priv, 1370 struct usb_redir_ep_info_header *ep_info) 1371 { 1372 USBRedirDevice *dev = priv; 1373 int i; 1374 1375 for (i = 0; i < MAX_ENDPOINTS; i++) { 1376 dev->endpoint[i].type = ep_info->type[i]; 1377 dev->endpoint[i].interval = ep_info->interval[i]; 1378 dev->endpoint[i].interface = ep_info->interface[i]; 1379 if (usbredirparser_peer_has_cap(dev->parser, 1380 usb_redir_cap_ep_info_max_packet_size)) { 1381 dev->endpoint[i].max_packet_size = ep_info->max_packet_size[i]; 1382 } 1383 switch (dev->endpoint[i].type) { 1384 case usb_redir_type_invalid: 1385 break; 1386 case usb_redir_type_iso: 1387 usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL); 1388 usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH); 1389 /* Fall through */ 1390 case usb_redir_type_interrupt: 1391 if (!usbredirparser_peer_has_cap(dev->parser, 1392 usb_redir_cap_ep_info_max_packet_size) || 1393 ep_info->max_packet_size[i] > 64) { 1394 usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL); 1395 } 1396 if (!usbredirparser_peer_has_cap(dev->parser, 1397 usb_redir_cap_ep_info_max_packet_size) || 1398 ep_info->max_packet_size[i] > 1024) { 1399 usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH); 1400 } 1401 if (dev->endpoint[i].interval == 0) { 1402 ERROR("Received 0 interval for isoc or irq endpoint\n"); 1403 usbredir_reject_device(dev); 1404 return; 1405 } 1406 /* Fall through */ 1407 case usb_redir_type_control: 1408 case usb_redir_type_bulk: 1409 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i), 1410 dev->endpoint[i].type, dev->endpoint[i].interface); 1411 break; 1412 default: 1413 ERROR("Received invalid endpoint type\n"); 1414 usbredir_reject_device(dev); 1415 return; 1416 } 1417 } 1418 /* The new ep info may have caused a speed incompatibility, recheck */ 1419 if (dev->dev.attached && 1420 !(dev->dev.port->speedmask & dev->dev.speedmask)) { 1421 ERROR("Device no longer matches speed after endpoint info change, " 1422 "disconnecting!\n"); 1423 usbredir_reject_device(dev); 1424 return; 1425 } 1426 usbredir_setup_usb_eps(dev); 1427 } 1428 1429 static void usbredir_configuration_status(void *priv, uint64_t id, 1430 struct usb_redir_configuration_status_header *config_status) 1431 { 1432 USBRedirDevice *dev = priv; 1433 USBPacket *p; 1434 1435 DPRINTF("set config status %d config %d id %"PRIu64"\n", 1436 config_status->status, config_status->configuration, id); 1437 1438 p = usbredir_find_packet_by_id(dev, 0, id); 1439 if (p) { 1440 if (dev->dev.setup_buf[0] & USB_DIR_IN) { 1441 dev->dev.data_buf[0] = config_status->configuration; 1442 p->actual_length = 1; 1443 } 1444 usbredir_handle_status(dev, p, config_status->status); 1445 usb_generic_async_ctrl_complete(&dev->dev, p); 1446 } 1447 } 1448 1449 static void usbredir_alt_setting_status(void *priv, uint64_t id, 1450 struct usb_redir_alt_setting_status_header *alt_setting_status) 1451 { 1452 USBRedirDevice *dev = priv; 1453 USBPacket *p; 1454 1455 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n", 1456 alt_setting_status->status, alt_setting_status->interface, 1457 alt_setting_status->alt, id); 1458 1459 p = usbredir_find_packet_by_id(dev, 0, id); 1460 if (p) { 1461 if (dev->dev.setup_buf[0] & USB_DIR_IN) { 1462 dev->dev.data_buf[0] = alt_setting_status->alt; 1463 p->actual_length = 1; 1464 } 1465 usbredir_handle_status(dev, p, alt_setting_status->status); 1466 usb_generic_async_ctrl_complete(&dev->dev, p); 1467 } 1468 } 1469 1470 static void usbredir_iso_stream_status(void *priv, uint64_t id, 1471 struct usb_redir_iso_stream_status_header *iso_stream_status) 1472 { 1473 USBRedirDevice *dev = priv; 1474 uint8_t ep = iso_stream_status->endpoint; 1475 1476 DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status, 1477 ep, id); 1478 1479 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) { 1480 return; 1481 } 1482 1483 dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status; 1484 if (iso_stream_status->status == usb_redir_stall) { 1485 DPRINTF("iso stream stopped by peer ep %02X\n", ep); 1486 dev->endpoint[EP2I(ep)].iso_started = 0; 1487 } 1488 } 1489 1490 static void usbredir_interrupt_receiving_status(void *priv, uint64_t id, 1491 struct usb_redir_interrupt_receiving_status_header 1492 *interrupt_receiving_status) 1493 { 1494 USBRedirDevice *dev = priv; 1495 uint8_t ep = interrupt_receiving_status->endpoint; 1496 1497 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n", 1498 interrupt_receiving_status->status, ep, id); 1499 1500 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) { 1501 return; 1502 } 1503 1504 dev->endpoint[EP2I(ep)].interrupt_error = 1505 interrupt_receiving_status->status; 1506 if (interrupt_receiving_status->status == usb_redir_stall) { 1507 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep); 1508 dev->endpoint[EP2I(ep)].interrupt_started = 0; 1509 } 1510 } 1511 1512 static void usbredir_bulk_streams_status(void *priv, uint64_t id, 1513 struct usb_redir_bulk_streams_status_header *bulk_streams_status) 1514 { 1515 } 1516 1517 static void usbredir_control_packet(void *priv, uint64_t id, 1518 struct usb_redir_control_packet_header *control_packet, 1519 uint8_t *data, int data_len) 1520 { 1521 USBRedirDevice *dev = priv; 1522 USBPacket *p; 1523 int len = control_packet->length; 1524 1525 DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status, 1526 len, id); 1527 1528 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices 1529 * to work redirected to a not superspeed capable hcd */ 1530 if (dev->dev.speed == USB_SPEED_SUPER && 1531 !((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER)) && 1532 control_packet->requesttype == 0x80 && 1533 control_packet->request == 6 && 1534 control_packet->value == 0x100 && control_packet->index == 0 && 1535 data_len >= 18 && data[7] == 9) { 1536 data[7] = 64; 1537 } 1538 1539 p = usbredir_find_packet_by_id(dev, 0, id); 1540 if (p) { 1541 usbredir_handle_status(dev, p, control_packet->status); 1542 if (data_len > 0) { 1543 usbredir_log_data(dev, "ctrl data in:", data, data_len); 1544 if (data_len > sizeof(dev->dev.data_buf)) { 1545 ERROR("ctrl buffer too small (%d > %zu)\n", 1546 data_len, sizeof(dev->dev.data_buf)); 1547 p->status = USB_RET_STALL; 1548 data_len = len = sizeof(dev->dev.data_buf); 1549 } 1550 memcpy(dev->dev.data_buf, data, data_len); 1551 } 1552 p->actual_length = len; 1553 usb_generic_async_ctrl_complete(&dev->dev, p); 1554 } 1555 free(data); 1556 } 1557 1558 static void usbredir_bulk_packet(void *priv, uint64_t id, 1559 struct usb_redir_bulk_packet_header *bulk_packet, 1560 uint8_t *data, int data_len) 1561 { 1562 USBRedirDevice *dev = priv; 1563 uint8_t ep = bulk_packet->endpoint; 1564 int len = (bulk_packet->length_high << 16) | bulk_packet->length; 1565 USBPacket *p; 1566 1567 DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64"\n", 1568 bulk_packet->status, ep, len, id); 1569 1570 p = usbredir_find_packet_by_id(dev, ep, id); 1571 if (p) { 1572 size_t size = (p->combined) ? p->combined->iov.size : p->iov.size; 1573 usbredir_handle_status(dev, p, bulk_packet->status); 1574 if (data_len > 0) { 1575 usbredir_log_data(dev, "bulk data in:", data, data_len); 1576 if (data_len > size) { 1577 ERROR("bulk got more data then requested (%d > %zd)\n", 1578 data_len, p->iov.size); 1579 p->status = USB_RET_BABBLE; 1580 data_len = len = size; 1581 } 1582 if (p->combined) { 1583 iov_from_buf(p->combined->iov.iov, p->combined->iov.niov, 1584 0, data, data_len); 1585 } else { 1586 usb_packet_copy(p, data, data_len); 1587 } 1588 } 1589 p->actual_length = len; 1590 if (p->pid == USB_TOKEN_IN && p->ep->pipeline) { 1591 usb_combined_input_packet_complete(&dev->dev, p); 1592 } else { 1593 usb_packet_complete(&dev->dev, p); 1594 } 1595 } 1596 free(data); 1597 } 1598 1599 static void usbredir_iso_packet(void *priv, uint64_t id, 1600 struct usb_redir_iso_packet_header *iso_packet, 1601 uint8_t *data, int data_len) 1602 { 1603 USBRedirDevice *dev = priv; 1604 uint8_t ep = iso_packet->endpoint; 1605 1606 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n", 1607 iso_packet->status, ep, data_len, id); 1608 1609 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) { 1610 ERROR("received iso packet for non iso endpoint %02X\n", ep); 1611 free(data); 1612 return; 1613 } 1614 1615 if (dev->endpoint[EP2I(ep)].iso_started == 0) { 1616 DPRINTF("received iso packet for non started stream ep %02X\n", ep); 1617 free(data); 1618 return; 1619 } 1620 1621 /* bufp_alloc also adds the packet to the ep queue */ 1622 bufp_alloc(dev, data, data_len, iso_packet->status, ep); 1623 } 1624 1625 static void usbredir_interrupt_packet(void *priv, uint64_t id, 1626 struct usb_redir_interrupt_packet_header *interrupt_packet, 1627 uint8_t *data, int data_len) 1628 { 1629 USBRedirDevice *dev = priv; 1630 uint8_t ep = interrupt_packet->endpoint; 1631 1632 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n", 1633 interrupt_packet->status, ep, data_len, id); 1634 1635 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) { 1636 ERROR("received int packet for non interrupt endpoint %02X\n", ep); 1637 free(data); 1638 return; 1639 } 1640 1641 if (ep & USB_DIR_IN) { 1642 if (dev->endpoint[EP2I(ep)].interrupt_started == 0) { 1643 DPRINTF("received int packet while not started ep %02X\n", ep); 1644 free(data); 1645 return; 1646 } 1647 1648 if (QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq)) { 1649 usb_wakeup(usb_ep_get(&dev->dev, USB_TOKEN_IN, ep & 0x0f)); 1650 } 1651 1652 /* bufp_alloc also adds the packet to the ep queue */ 1653 bufp_alloc(dev, data, data_len, interrupt_packet->status, ep); 1654 } else { 1655 /* 1656 * We report output interrupt packets as completed directly upon 1657 * submission, so all we can do here if one failed is warn. 1658 */ 1659 if (interrupt_packet->status) { 1660 WARNING("interrupt output failed status %d ep %02X id %"PRIu64"\n", 1661 interrupt_packet->status, ep, id); 1662 } 1663 } 1664 } 1665 1666 /* 1667 * Migration code 1668 */ 1669 1670 static void usbredir_pre_save(void *priv) 1671 { 1672 USBRedirDevice *dev = priv; 1673 1674 usbredir_fill_already_in_flight(dev); 1675 } 1676 1677 static int usbredir_post_load(void *priv, int version_id) 1678 { 1679 USBRedirDevice *dev = priv; 1680 1681 switch (dev->device_info.speed) { 1682 case usb_redir_speed_low: 1683 dev->dev.speed = USB_SPEED_LOW; 1684 break; 1685 case usb_redir_speed_full: 1686 dev->dev.speed = USB_SPEED_FULL; 1687 break; 1688 case usb_redir_speed_high: 1689 dev->dev.speed = USB_SPEED_HIGH; 1690 break; 1691 case usb_redir_speed_super: 1692 dev->dev.speed = USB_SPEED_SUPER; 1693 break; 1694 default: 1695 dev->dev.speed = USB_SPEED_FULL; 1696 } 1697 dev->dev.speedmask = (1 << dev->dev.speed); 1698 1699 usbredir_setup_usb_eps(dev); 1700 1701 return 0; 1702 } 1703 1704 /* For usbredirparser migration */ 1705 static void usbredir_put_parser(QEMUFile *f, void *priv, size_t unused) 1706 { 1707 USBRedirDevice *dev = priv; 1708 uint8_t *data; 1709 int len; 1710 1711 if (dev->parser == NULL) { 1712 qemu_put_be32(f, 0); 1713 return; 1714 } 1715 1716 usbredirparser_serialize(dev->parser, &data, &len); 1717 qemu_oom_check(data); 1718 1719 qemu_put_be32(f, len); 1720 qemu_put_buffer(f, data, len); 1721 1722 free(data); 1723 } 1724 1725 static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused) 1726 { 1727 USBRedirDevice *dev = priv; 1728 uint8_t *data; 1729 int len, ret; 1730 1731 len = qemu_get_be32(f); 1732 if (len == 0) { 1733 return 0; 1734 } 1735 1736 /* 1737 * If our chardev is not open already at this point the usbredir connection 1738 * has been broken (non seamless migration, or restore from disk). 1739 * 1740 * In this case create a temporary parser to receive the migration data, 1741 * and schedule the close_bh to report the device as disconnected to the 1742 * guest and to destroy the parser again. 1743 */ 1744 if (dev->parser == NULL) { 1745 WARNING("usb-redir connection broken during migration\n"); 1746 usbredir_create_parser(dev); 1747 qemu_bh_schedule(dev->chardev_close_bh); 1748 } 1749 1750 data = g_malloc(len); 1751 qemu_get_buffer(f, data, len); 1752 1753 ret = usbredirparser_unserialize(dev->parser, data, len); 1754 1755 g_free(data); 1756 1757 return ret; 1758 } 1759 1760 static const VMStateInfo usbredir_parser_vmstate_info = { 1761 .name = "usb-redir-parser", 1762 .put = usbredir_put_parser, 1763 .get = usbredir_get_parser, 1764 }; 1765 1766 1767 /* For buffered packets (iso/irq) queue migration */ 1768 static void usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused) 1769 { 1770 struct endp_data *endp = priv; 1771 struct buf_packet *bufp; 1772 int remain = endp->bufpq_size; 1773 1774 qemu_put_be32(f, endp->bufpq_size); 1775 QTAILQ_FOREACH(bufp, &endp->bufpq, next) { 1776 qemu_put_be32(f, bufp->len); 1777 qemu_put_be32(f, bufp->status); 1778 qemu_put_buffer(f, bufp->data, bufp->len); 1779 remain--; 1780 } 1781 assert(remain == 0); 1782 } 1783 1784 static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused) 1785 { 1786 struct endp_data *endp = priv; 1787 struct buf_packet *bufp; 1788 int i; 1789 1790 endp->bufpq_size = qemu_get_be32(f); 1791 for (i = 0; i < endp->bufpq_size; i++) { 1792 bufp = g_malloc(sizeof(struct buf_packet)); 1793 bufp->len = qemu_get_be32(f); 1794 bufp->status = qemu_get_be32(f); 1795 bufp->data = qemu_oom_check(malloc(bufp->len)); /* regular malloc! */ 1796 qemu_get_buffer(f, bufp->data, bufp->len); 1797 QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next); 1798 } 1799 return 0; 1800 } 1801 1802 static const VMStateInfo usbredir_ep_bufpq_vmstate_info = { 1803 .name = "usb-redir-bufpq", 1804 .put = usbredir_put_bufpq, 1805 .get = usbredir_get_bufpq, 1806 }; 1807 1808 1809 /* For endp_data migration */ 1810 static const VMStateDescription usbredir_ep_vmstate = { 1811 .name = "usb-redir-ep", 1812 .version_id = 1, 1813 .minimum_version_id = 1, 1814 .fields = (VMStateField[]) { 1815 VMSTATE_UINT8(type, struct endp_data), 1816 VMSTATE_UINT8(interval, struct endp_data), 1817 VMSTATE_UINT8(interface, struct endp_data), 1818 VMSTATE_UINT16(max_packet_size, struct endp_data), 1819 VMSTATE_UINT8(iso_started, struct endp_data), 1820 VMSTATE_UINT8(iso_error, struct endp_data), 1821 VMSTATE_UINT8(interrupt_started, struct endp_data), 1822 VMSTATE_UINT8(interrupt_error, struct endp_data), 1823 VMSTATE_UINT8(bufpq_prefilled, struct endp_data), 1824 VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data), 1825 { 1826 .name = "bufpq", 1827 .version_id = 0, 1828 .field_exists = NULL, 1829 .size = 0, 1830 .info = &usbredir_ep_bufpq_vmstate_info, 1831 .flags = VMS_SINGLE, 1832 .offset = 0, 1833 }, 1834 VMSTATE_INT32(bufpq_target_size, struct endp_data), 1835 VMSTATE_END_OF_LIST() 1836 } 1837 }; 1838 1839 1840 /* For PacketIdQueue migration */ 1841 static void usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused) 1842 { 1843 struct PacketIdQueue *q = priv; 1844 USBRedirDevice *dev = q->dev; 1845 struct PacketIdQueueEntry *e; 1846 int remain = q->size; 1847 1848 DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size); 1849 qemu_put_be32(f, q->size); 1850 QTAILQ_FOREACH(e, &q->head, next) { 1851 qemu_put_be64(f, e->id); 1852 remain--; 1853 } 1854 assert(remain == 0); 1855 } 1856 1857 static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused) 1858 { 1859 struct PacketIdQueue *q = priv; 1860 USBRedirDevice *dev = q->dev; 1861 int i, size; 1862 uint64_t id; 1863 1864 size = qemu_get_be32(f); 1865 DPRINTF("get_packet_id_q %s size %d\n", q->name, size); 1866 for (i = 0; i < size; i++) { 1867 id = qemu_get_be64(f); 1868 packet_id_queue_add(q, id); 1869 } 1870 assert(q->size == size); 1871 return 0; 1872 } 1873 1874 static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = { 1875 .name = "usb-redir-packet-id-q", 1876 .put = usbredir_put_packet_id_q, 1877 .get = usbredir_get_packet_id_q, 1878 }; 1879 1880 static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = { 1881 .name = "usb-redir-packet-id-queue", 1882 .version_id = 1, 1883 .minimum_version_id = 1, 1884 .fields = (VMStateField[]) { 1885 { 1886 .name = "queue", 1887 .version_id = 0, 1888 .field_exists = NULL, 1889 .size = 0, 1890 .info = &usbredir_ep_packet_id_q_vmstate_info, 1891 .flags = VMS_SINGLE, 1892 .offset = 0, 1893 }, 1894 VMSTATE_END_OF_LIST() 1895 } 1896 }; 1897 1898 1899 /* For usb_redir_device_connect_header migration */ 1900 static const VMStateDescription usbredir_device_info_vmstate = { 1901 .name = "usb-redir-device-info", 1902 .version_id = 1, 1903 .minimum_version_id = 1, 1904 .fields = (VMStateField[]) { 1905 VMSTATE_UINT8(speed, struct usb_redir_device_connect_header), 1906 VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header), 1907 VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header), 1908 VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header), 1909 VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header), 1910 VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header), 1911 VMSTATE_UINT16(device_version_bcd, 1912 struct usb_redir_device_connect_header), 1913 VMSTATE_END_OF_LIST() 1914 } 1915 }; 1916 1917 1918 /* For usb_redir_interface_info_header migration */ 1919 static const VMStateDescription usbredir_interface_info_vmstate = { 1920 .name = "usb-redir-interface-info", 1921 .version_id = 1, 1922 .minimum_version_id = 1, 1923 .fields = (VMStateField[]) { 1924 VMSTATE_UINT32(interface_count, 1925 struct usb_redir_interface_info_header), 1926 VMSTATE_UINT8_ARRAY(interface, 1927 struct usb_redir_interface_info_header, 32), 1928 VMSTATE_UINT8_ARRAY(interface_class, 1929 struct usb_redir_interface_info_header, 32), 1930 VMSTATE_UINT8_ARRAY(interface_subclass, 1931 struct usb_redir_interface_info_header, 32), 1932 VMSTATE_UINT8_ARRAY(interface_protocol, 1933 struct usb_redir_interface_info_header, 32), 1934 VMSTATE_END_OF_LIST() 1935 } 1936 }; 1937 1938 1939 /* And finally the USBRedirDevice vmstate itself */ 1940 static const VMStateDescription usbredir_vmstate = { 1941 .name = "usb-redir", 1942 .version_id = 1, 1943 .minimum_version_id = 1, 1944 .pre_save = usbredir_pre_save, 1945 .post_load = usbredir_post_load, 1946 .fields = (VMStateField[]) { 1947 VMSTATE_USB_DEVICE(dev, USBRedirDevice), 1948 VMSTATE_TIMER(attach_timer, USBRedirDevice), 1949 { 1950 .name = "parser", 1951 .version_id = 0, 1952 .field_exists = NULL, 1953 .size = 0, 1954 .info = &usbredir_parser_vmstate_info, 1955 .flags = VMS_SINGLE, 1956 .offset = 0, 1957 }, 1958 VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1, 1959 usbredir_ep_vmstate, struct endp_data), 1960 VMSTATE_STRUCT(cancelled, USBRedirDevice, 1, 1961 usbredir_ep_packet_id_queue_vmstate, 1962 struct PacketIdQueue), 1963 VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1, 1964 usbredir_ep_packet_id_queue_vmstate, 1965 struct PacketIdQueue), 1966 VMSTATE_STRUCT(device_info, USBRedirDevice, 1, 1967 usbredir_device_info_vmstate, 1968 struct usb_redir_device_connect_header), 1969 VMSTATE_STRUCT(interface_info, USBRedirDevice, 1, 1970 usbredir_interface_info_vmstate, 1971 struct usb_redir_interface_info_header), 1972 VMSTATE_END_OF_LIST() 1973 } 1974 }; 1975 1976 static Property usbredir_properties[] = { 1977 DEFINE_PROP_CHR("chardev", USBRedirDevice, cs), 1978 DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, usbredirparser_warning), 1979 DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str), 1980 DEFINE_PROP_INT32("bootindex", USBRedirDevice, bootindex, -1), 1981 DEFINE_PROP_END_OF_LIST(), 1982 }; 1983 1984 static void usbredir_class_initfn(ObjectClass *klass, void *data) 1985 { 1986 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 1987 DeviceClass *dc = DEVICE_CLASS(klass); 1988 1989 uc->init = usbredir_initfn; 1990 uc->product_desc = "USB Redirection Device"; 1991 uc->handle_destroy = usbredir_handle_destroy; 1992 uc->cancel_packet = usbredir_cancel_packet; 1993 uc->handle_reset = usbredir_handle_reset; 1994 uc->handle_data = usbredir_handle_data; 1995 uc->handle_control = usbredir_handle_control; 1996 uc->flush_ep_queue = usbredir_flush_ep_queue; 1997 dc->vmsd = &usbredir_vmstate; 1998 dc->props = usbredir_properties; 1999 } 2000 2001 static TypeInfo usbredir_dev_info = { 2002 .name = "usb-redir", 2003 .parent = TYPE_USB_DEVICE, 2004 .instance_size = sizeof(USBRedirDevice), 2005 .class_init = usbredir_class_initfn, 2006 }; 2007 2008 static void usbredir_register_types(void) 2009 { 2010 type_register_static(&usbredir_dev_info); 2011 } 2012 2013 type_init(usbredir_register_types) 2014