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/osdep.h" 29 #include "qapi/error.h" 30 #include "qemu-common.h" 31 #include "qemu/timer.h" 32 #include "sysemu/sysemu.h" 33 #include "qapi/qmp/qerror.h" 34 #include "qemu/error-report.h" 35 #include "qemu/iov.h" 36 #include "sysemu/char.h" 37 38 #include <usbredirparser.h> 39 #include <usbredirfilter.h> 40 41 #include "hw/usb.h" 42 43 /* ERROR is defined below. Remove any previous definition. */ 44 #undef ERROR 45 46 #define MAX_ENDPOINTS 32 47 #define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */ 48 #define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f)) 49 #define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f)) 50 #define USBEP2I(usb_ep) (((usb_ep)->pid == USB_TOKEN_IN) ? \ 51 ((usb_ep)->nr | 0x10) : ((usb_ep)->nr)) 52 #define I2USBEP(d, i) (usb_ep_get(&(d)->dev, \ 53 ((i) & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, \ 54 (i) & 0x0f)) 55 56 #ifndef USBREDIR_VERSION /* This is not defined in older usbredir versions */ 57 #define USBREDIR_VERSION 0 58 #endif 59 60 typedef struct USBRedirDevice USBRedirDevice; 61 62 /* Struct to hold buffered packets */ 63 struct buf_packet { 64 uint8_t *data; 65 void *free_on_destroy; 66 uint16_t len; 67 uint16_t offset; 68 uint8_t status; 69 QTAILQ_ENTRY(buf_packet)next; 70 }; 71 72 struct endp_data { 73 USBRedirDevice *dev; 74 uint8_t type; 75 uint8_t interval; 76 uint8_t interface; /* bInterfaceNumber this ep belongs to */ 77 uint16_t max_packet_size; /* In bytes, not wMaxPacketSize format !! */ 78 uint32_t max_streams; 79 uint8_t iso_started; 80 uint8_t iso_error; /* For reporting iso errors to the HC */ 81 uint8_t interrupt_started; 82 uint8_t interrupt_error; 83 uint8_t bulk_receiving_enabled; 84 uint8_t bulk_receiving_started; 85 uint8_t bufpq_prefilled; 86 uint8_t bufpq_dropping_packets; 87 QTAILQ_HEAD(, buf_packet) bufpq; 88 int32_t bufpq_size; 89 int32_t bufpq_target_size; 90 USBPacket *pending_async_packet; 91 }; 92 93 struct PacketIdQueueEntry { 94 uint64_t id; 95 QTAILQ_ENTRY(PacketIdQueueEntry)next; 96 }; 97 98 struct PacketIdQueue { 99 USBRedirDevice *dev; 100 const char *name; 101 QTAILQ_HEAD(, PacketIdQueueEntry) head; 102 int size; 103 }; 104 105 struct USBRedirDevice { 106 USBDevice dev; 107 /* Properties */ 108 CharBackend cs; 109 uint8_t debug; 110 char *filter_str; 111 int32_t bootindex; 112 bool enable_streams; 113 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */ 114 const uint8_t *read_buf; 115 int read_buf_size; 116 /* Active chardev-watch-tag */ 117 guint watch; 118 /* For async handling of close / reject */ 119 QEMUBH *chardev_close_bh; 120 QEMUBH *device_reject_bh; 121 /* To delay the usb attach in case of quick chardev close + open */ 122 QEMUTimer *attach_timer; 123 int64_t next_attach_time; 124 struct usbredirparser *parser; 125 struct endp_data endpoint[MAX_ENDPOINTS]; 126 struct PacketIdQueue cancelled; 127 struct PacketIdQueue already_in_flight; 128 void (*buffered_bulk_in_complete)(USBRedirDevice *, USBPacket *, uint8_t); 129 /* Data for device filtering */ 130 struct usb_redir_device_connect_header device_info; 131 struct usb_redir_interface_info_header interface_info; 132 struct usbredirfilter_rule *filter_rules; 133 int filter_rules_count; 134 int compatible_speedmask; 135 }; 136 137 #define TYPE_USB_REDIR "usb-redir" 138 #define USB_REDIRECT(obj) OBJECT_CHECK(USBRedirDevice, (obj), TYPE_USB_REDIR) 139 140 static void usbredir_hello(void *priv, struct usb_redir_hello_header *h); 141 static void usbredir_device_connect(void *priv, 142 struct usb_redir_device_connect_header *device_connect); 143 static void usbredir_device_disconnect(void *priv); 144 static void usbredir_interface_info(void *priv, 145 struct usb_redir_interface_info_header *interface_info); 146 static void usbredir_ep_info(void *priv, 147 struct usb_redir_ep_info_header *ep_info); 148 static void usbredir_configuration_status(void *priv, uint64_t id, 149 struct usb_redir_configuration_status_header *configuration_status); 150 static void usbredir_alt_setting_status(void *priv, uint64_t id, 151 struct usb_redir_alt_setting_status_header *alt_setting_status); 152 static void usbredir_iso_stream_status(void *priv, uint64_t id, 153 struct usb_redir_iso_stream_status_header *iso_stream_status); 154 static void usbredir_interrupt_receiving_status(void *priv, uint64_t id, 155 struct usb_redir_interrupt_receiving_status_header 156 *interrupt_receiving_status); 157 static void usbredir_bulk_streams_status(void *priv, uint64_t id, 158 struct usb_redir_bulk_streams_status_header *bulk_streams_status); 159 static void usbredir_bulk_receiving_status(void *priv, uint64_t id, 160 struct usb_redir_bulk_receiving_status_header *bulk_receiving_status); 161 static void usbredir_control_packet(void *priv, uint64_t id, 162 struct usb_redir_control_packet_header *control_packet, 163 uint8_t *data, int data_len); 164 static void usbredir_bulk_packet(void *priv, uint64_t id, 165 struct usb_redir_bulk_packet_header *bulk_packet, 166 uint8_t *data, int data_len); 167 static void usbredir_iso_packet(void *priv, uint64_t id, 168 struct usb_redir_iso_packet_header *iso_packet, 169 uint8_t *data, int data_len); 170 static void usbredir_interrupt_packet(void *priv, uint64_t id, 171 struct usb_redir_interrupt_packet_header *interrupt_header, 172 uint8_t *data, int data_len); 173 static void usbredir_buffered_bulk_packet(void *priv, uint64_t id, 174 struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet, 175 uint8_t *data, int data_len); 176 177 static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p, 178 int status); 179 180 #define VERSION "qemu usb-redir guest " QEMU_VERSION 181 182 /* 183 * Logging stuff 184 */ 185 186 #define ERROR(...) \ 187 do { \ 188 if (dev->debug >= usbredirparser_error) { \ 189 error_report("usb-redir error: " __VA_ARGS__); \ 190 } \ 191 } while (0) 192 #define WARNING(...) \ 193 do { \ 194 if (dev->debug >= usbredirparser_warning) { \ 195 error_report("usb-redir warning: " __VA_ARGS__); \ 196 } \ 197 } while (0) 198 #define INFO(...) \ 199 do { \ 200 if (dev->debug >= usbredirparser_info) { \ 201 error_report("usb-redir: " __VA_ARGS__); \ 202 } \ 203 } while (0) 204 #define DPRINTF(...) \ 205 do { \ 206 if (dev->debug >= usbredirparser_debug) { \ 207 error_report("usb-redir: " __VA_ARGS__); \ 208 } \ 209 } while (0) 210 #define DPRINTF2(...) \ 211 do { \ 212 if (dev->debug >= usbredirparser_debug_data) { \ 213 error_report("usb-redir: " __VA_ARGS__); \ 214 } \ 215 } while (0) 216 217 static void usbredir_log(void *priv, int level, const char *msg) 218 { 219 USBRedirDevice *dev = priv; 220 221 if (dev->debug < level) { 222 return; 223 } 224 225 error_report("%s", msg); 226 } 227 228 static void usbredir_log_data(USBRedirDevice *dev, const char *desc, 229 const uint8_t *data, int len) 230 { 231 int i, j, n; 232 233 if (dev->debug < usbredirparser_debug_data) { 234 return; 235 } 236 237 for (i = 0; i < len; i += j) { 238 char buf[128]; 239 240 n = sprintf(buf, "%s", desc); 241 for (j = 0; j < 8 && i + j < len; j++) { 242 n += sprintf(buf + n, " %02X", data[i + j]); 243 } 244 error_report("%s", buf); 245 } 246 } 247 248 /* 249 * usbredirparser io functions 250 */ 251 252 static int usbredir_read(void *priv, uint8_t *data, int count) 253 { 254 USBRedirDevice *dev = priv; 255 256 if (dev->read_buf_size < count) { 257 count = dev->read_buf_size; 258 } 259 260 memcpy(data, dev->read_buf, count); 261 262 dev->read_buf_size -= count; 263 if (dev->read_buf_size) { 264 dev->read_buf += count; 265 } else { 266 dev->read_buf = NULL; 267 } 268 269 return count; 270 } 271 272 static gboolean usbredir_write_unblocked(GIOChannel *chan, GIOCondition cond, 273 void *opaque) 274 { 275 USBRedirDevice *dev = opaque; 276 277 dev->watch = 0; 278 usbredirparser_do_write(dev->parser); 279 280 return FALSE; 281 } 282 283 static int usbredir_write(void *priv, uint8_t *data, int count) 284 { 285 USBRedirDevice *dev = priv; 286 CharDriverState *chr = qemu_chr_fe_get_driver(&dev->cs); 287 int r; 288 289 if (!chr->be_open) { 290 return 0; 291 } 292 293 /* Don't send new data to the chardev until our state is fully synced */ 294 if (!runstate_check(RUN_STATE_RUNNING)) { 295 return 0; 296 } 297 298 r = qemu_chr_fe_write(&dev->cs, data, count); 299 if (r < count) { 300 if (!dev->watch) { 301 dev->watch = qemu_chr_fe_add_watch(&dev->cs, G_IO_OUT | G_IO_HUP, 302 usbredir_write_unblocked, dev); 303 } 304 if (r < 0) { 305 r = 0; 306 } 307 } 308 return r; 309 } 310 311 /* 312 * Cancelled and buffered packets helpers 313 */ 314 315 static void packet_id_queue_init(struct PacketIdQueue *q, 316 USBRedirDevice *dev, const char *name) 317 { 318 q->dev = dev; 319 q->name = name; 320 QTAILQ_INIT(&q->head); 321 q->size = 0; 322 } 323 324 static void packet_id_queue_add(struct PacketIdQueue *q, uint64_t id) 325 { 326 USBRedirDevice *dev = q->dev; 327 struct PacketIdQueueEntry *e; 328 329 DPRINTF("adding packet id %"PRIu64" to %s queue\n", id, q->name); 330 331 e = g_new0(struct PacketIdQueueEntry, 1); 332 e->id = id; 333 QTAILQ_INSERT_TAIL(&q->head, e, next); 334 q->size++; 335 } 336 337 static int packet_id_queue_remove(struct PacketIdQueue *q, uint64_t id) 338 { 339 USBRedirDevice *dev = q->dev; 340 struct PacketIdQueueEntry *e; 341 342 QTAILQ_FOREACH(e, &q->head, next) { 343 if (e->id == id) { 344 DPRINTF("removing packet id %"PRIu64" from %s queue\n", 345 id, q->name); 346 QTAILQ_REMOVE(&q->head, e, next); 347 q->size--; 348 g_free(e); 349 return 1; 350 } 351 } 352 return 0; 353 } 354 355 static void packet_id_queue_empty(struct PacketIdQueue *q) 356 { 357 USBRedirDevice *dev = q->dev; 358 struct PacketIdQueueEntry *e, *next_e; 359 360 DPRINTF("removing %d packet-ids from %s queue\n", q->size, q->name); 361 362 QTAILQ_FOREACH_SAFE(e, &q->head, next, next_e) { 363 QTAILQ_REMOVE(&q->head, e, next); 364 g_free(e); 365 } 366 q->size = 0; 367 } 368 369 static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p) 370 { 371 USBRedirDevice *dev = USB_REDIRECT(udev); 372 int i = USBEP2I(p->ep); 373 374 if (p->combined) { 375 usb_combined_packet_cancel(udev, p); 376 return; 377 } 378 379 if (dev->endpoint[i].pending_async_packet) { 380 assert(dev->endpoint[i].pending_async_packet == p); 381 dev->endpoint[i].pending_async_packet = NULL; 382 return; 383 } 384 385 packet_id_queue_add(&dev->cancelled, p->id); 386 usbredirparser_send_cancel_data_packet(dev->parser, p->id); 387 usbredirparser_do_write(dev->parser); 388 } 389 390 static int usbredir_is_cancelled(USBRedirDevice *dev, uint64_t id) 391 { 392 if (!dev->dev.attached) { 393 return 1; /* Treat everything as cancelled after a disconnect */ 394 } 395 return packet_id_queue_remove(&dev->cancelled, id); 396 } 397 398 static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice *dev, 399 struct USBEndpoint *ep) 400 { 401 static USBPacket *p; 402 403 /* async handled packets for bulk receiving eps do not count as inflight */ 404 if (dev->endpoint[USBEP2I(ep)].bulk_receiving_started) { 405 return; 406 } 407 408 QTAILQ_FOREACH(p, &ep->queue, queue) { 409 /* Skip combined packets, except for the first */ 410 if (p->combined && p != p->combined->first) { 411 continue; 412 } 413 if (p->state == USB_PACKET_ASYNC) { 414 packet_id_queue_add(&dev->already_in_flight, p->id); 415 } 416 } 417 } 418 419 static void usbredir_fill_already_in_flight(USBRedirDevice *dev) 420 { 421 int ep; 422 struct USBDevice *udev = &dev->dev; 423 424 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_ctl); 425 426 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { 427 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_in[ep]); 428 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_out[ep]); 429 } 430 } 431 432 static int usbredir_already_in_flight(USBRedirDevice *dev, uint64_t id) 433 { 434 return packet_id_queue_remove(&dev->already_in_flight, id); 435 } 436 437 static USBPacket *usbredir_find_packet_by_id(USBRedirDevice *dev, 438 uint8_t ep, uint64_t id) 439 { 440 USBPacket *p; 441 442 if (usbredir_is_cancelled(dev, id)) { 443 return NULL; 444 } 445 446 p = usb_ep_find_packet_by_id(&dev->dev, 447 (ep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT, 448 ep & 0x0f, id); 449 if (p == NULL) { 450 ERROR("could not find packet with id %"PRIu64"\n", id); 451 } 452 return p; 453 } 454 455 static int bufp_alloc(USBRedirDevice *dev, uint8_t *data, uint16_t len, 456 uint8_t status, uint8_t ep, void *free_on_destroy) 457 { 458 struct buf_packet *bufp; 459 460 if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets && 461 dev->endpoint[EP2I(ep)].bufpq_size > 462 2 * dev->endpoint[EP2I(ep)].bufpq_target_size) { 463 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep); 464 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1; 465 } 466 /* Since we're interupting the stream anyways, drop enough packets to get 467 back to our target buffer size */ 468 if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) { 469 if (dev->endpoint[EP2I(ep)].bufpq_size > 470 dev->endpoint[EP2I(ep)].bufpq_target_size) { 471 free(data); 472 return -1; 473 } 474 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; 475 } 476 477 bufp = g_new(struct buf_packet, 1); 478 bufp->data = data; 479 bufp->len = len; 480 bufp->offset = 0; 481 bufp->status = status; 482 bufp->free_on_destroy = free_on_destroy; 483 QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next); 484 dev->endpoint[EP2I(ep)].bufpq_size++; 485 return 0; 486 } 487 488 static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp, 489 uint8_t ep) 490 { 491 QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next); 492 dev->endpoint[EP2I(ep)].bufpq_size--; 493 free(bufp->free_on_destroy); 494 g_free(bufp); 495 } 496 497 static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep) 498 { 499 struct buf_packet *buf, *buf_next; 500 501 QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) { 502 bufp_free(dev, buf, ep); 503 } 504 } 505 506 /* 507 * USBDevice callbacks 508 */ 509 510 static void usbredir_handle_reset(USBDevice *udev) 511 { 512 USBRedirDevice *dev = USB_REDIRECT(udev); 513 514 DPRINTF("reset device\n"); 515 usbredirparser_send_reset(dev->parser); 516 usbredirparser_do_write(dev->parser); 517 } 518 519 static void usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p, 520 uint8_t ep) 521 { 522 int status, len; 523 if (!dev->endpoint[EP2I(ep)].iso_started && 524 !dev->endpoint[EP2I(ep)].iso_error) { 525 struct usb_redir_start_iso_stream_header start_iso = { 526 .endpoint = ep, 527 }; 528 int pkts_per_sec; 529 530 if (dev->dev.speed == USB_SPEED_HIGH) { 531 pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval; 532 } else { 533 pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval; 534 } 535 /* Testing has shown that we need circa 60 ms buffer */ 536 dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000; 537 538 /* Aim for approx 100 interrupts / second on the client to 539 balance latency and interrupt load */ 540 start_iso.pkts_per_urb = pkts_per_sec / 100; 541 if (start_iso.pkts_per_urb < 1) { 542 start_iso.pkts_per_urb = 1; 543 } else if (start_iso.pkts_per_urb > 32) { 544 start_iso.pkts_per_urb = 32; 545 } 546 547 start_iso.no_urbs = DIV_ROUND_UP( 548 dev->endpoint[EP2I(ep)].bufpq_target_size, 549 start_iso.pkts_per_urb); 550 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest 551 as overflow buffer. Also see the usbredir protocol documentation */ 552 if (!(ep & USB_DIR_IN)) { 553 start_iso.no_urbs *= 2; 554 } 555 if (start_iso.no_urbs > 16) { 556 start_iso.no_urbs = 16; 557 } 558 559 /* No id, we look at the ep when receiving a status back */ 560 usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso); 561 usbredirparser_do_write(dev->parser); 562 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n", 563 pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep); 564 dev->endpoint[EP2I(ep)].iso_started = 1; 565 dev->endpoint[EP2I(ep)].bufpq_prefilled = 0; 566 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; 567 } 568 569 if (ep & USB_DIR_IN) { 570 struct buf_packet *isop; 571 572 if (dev->endpoint[EP2I(ep)].iso_started && 573 !dev->endpoint[EP2I(ep)].bufpq_prefilled) { 574 if (dev->endpoint[EP2I(ep)].bufpq_size < 575 dev->endpoint[EP2I(ep)].bufpq_target_size) { 576 return; 577 } 578 dev->endpoint[EP2I(ep)].bufpq_prefilled = 1; 579 } 580 581 isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); 582 if (isop == NULL) { 583 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n", 584 ep, dev->endpoint[EP2I(ep)].iso_error); 585 /* Re-fill the buffer */ 586 dev->endpoint[EP2I(ep)].bufpq_prefilled = 0; 587 /* Check iso_error for stream errors, otherwise its an underrun */ 588 status = dev->endpoint[EP2I(ep)].iso_error; 589 dev->endpoint[EP2I(ep)].iso_error = 0; 590 p->status = status ? USB_RET_IOERROR : USB_RET_SUCCESS; 591 return; 592 } 593 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep, 594 isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size); 595 596 status = isop->status; 597 len = isop->len; 598 if (len > p->iov.size) { 599 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n", 600 ep, len, (int)p->iov.size); 601 len = p->iov.size; 602 status = usb_redir_babble; 603 } 604 usb_packet_copy(p, isop->data, len); 605 bufp_free(dev, isop, ep); 606 usbredir_handle_status(dev, p, status); 607 } else { 608 /* If the stream was not started because of a pending error don't 609 send the packet to the usb-host */ 610 if (dev->endpoint[EP2I(ep)].iso_started) { 611 struct usb_redir_iso_packet_header iso_packet = { 612 .endpoint = ep, 613 .length = p->iov.size 614 }; 615 uint8_t buf[p->iov.size]; 616 /* No id, we look at the ep when receiving a status back */ 617 usb_packet_copy(p, buf, p->iov.size); 618 usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet, 619 buf, p->iov.size); 620 usbredirparser_do_write(dev->parser); 621 } 622 status = dev->endpoint[EP2I(ep)].iso_error; 623 dev->endpoint[EP2I(ep)].iso_error = 0; 624 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status, 625 p->iov.size); 626 usbredir_handle_status(dev, p, status); 627 } 628 } 629 630 static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep) 631 { 632 struct usb_redir_stop_iso_stream_header stop_iso_stream = { 633 .endpoint = ep 634 }; 635 if (dev->endpoint[EP2I(ep)].iso_started) { 636 usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream); 637 DPRINTF("iso stream stopped ep %02X\n", ep); 638 dev->endpoint[EP2I(ep)].iso_started = 0; 639 } 640 dev->endpoint[EP2I(ep)].iso_error = 0; 641 usbredir_free_bufpq(dev, ep); 642 } 643 644 /* 645 * The usb-host may poll the endpoint faster then our guest, resulting in lots 646 * of smaller bulkp-s. The below buffered_bulk_in_complete* functions combine 647 * data from multiple bulkp-s into a single packet, avoiding bufpq overflows. 648 */ 649 static void usbredir_buffered_bulk_add_data_to_packet(USBRedirDevice *dev, 650 struct buf_packet *bulkp, int count, USBPacket *p, uint8_t ep) 651 { 652 usb_packet_copy(p, bulkp->data + bulkp->offset, count); 653 bulkp->offset += count; 654 if (bulkp->offset == bulkp->len) { 655 /* Store status in the last packet with data from this bulkp */ 656 usbredir_handle_status(dev, p, bulkp->status); 657 bufp_free(dev, bulkp, ep); 658 } 659 } 660 661 static void usbredir_buffered_bulk_in_complete_raw(USBRedirDevice *dev, 662 USBPacket *p, uint8_t ep) 663 { 664 struct buf_packet *bulkp; 665 int count; 666 667 while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) && 668 p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) { 669 count = bulkp->len - bulkp->offset; 670 if (count > (p->iov.size - p->actual_length)) { 671 count = p->iov.size - p->actual_length; 672 } 673 usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep); 674 } 675 } 676 677 static void usbredir_buffered_bulk_in_complete_ftdi(USBRedirDevice *dev, 678 USBPacket *p, uint8_t ep) 679 { 680 const int maxp = dev->endpoint[EP2I(ep)].max_packet_size; 681 uint8_t header[2] = { 0, 0 }; 682 struct buf_packet *bulkp; 683 int count; 684 685 while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) && 686 p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) { 687 if (bulkp->len < 2) { 688 WARNING("malformed ftdi bulk in packet\n"); 689 bufp_free(dev, bulkp, ep); 690 continue; 691 } 692 693 if ((p->actual_length % maxp) == 0) { 694 usb_packet_copy(p, bulkp->data, 2); 695 memcpy(header, bulkp->data, 2); 696 } else { 697 if (bulkp->data[0] != header[0] || bulkp->data[1] != header[1]) { 698 break; /* Different header, add to next packet */ 699 } 700 } 701 702 if (bulkp->offset == 0) { 703 bulkp->offset = 2; /* Skip header */ 704 } 705 count = bulkp->len - bulkp->offset; 706 /* Must repeat the header at maxp interval */ 707 if (count > (maxp - (p->actual_length % maxp))) { 708 count = maxp - (p->actual_length % maxp); 709 } 710 usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep); 711 } 712 } 713 714 static void usbredir_buffered_bulk_in_complete(USBRedirDevice *dev, 715 USBPacket *p, uint8_t ep) 716 { 717 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ 718 dev->buffered_bulk_in_complete(dev, p, ep); 719 DPRINTF("bulk-token-in ep %02X status %d len %d id %"PRIu64"\n", 720 ep, p->status, p->actual_length, p->id); 721 } 722 723 static void usbredir_handle_buffered_bulk_in_data(USBRedirDevice *dev, 724 USBPacket *p, uint8_t ep) 725 { 726 /* Input bulk endpoint, buffered packet input */ 727 if (!dev->endpoint[EP2I(ep)].bulk_receiving_started) { 728 int bpt; 729 struct usb_redir_start_bulk_receiving_header start = { 730 .endpoint = ep, 731 .stream_id = 0, 732 .no_transfers = 5, 733 }; 734 /* Round bytes_per_transfer up to a multiple of max_packet_size */ 735 bpt = 512 + dev->endpoint[EP2I(ep)].max_packet_size - 1; 736 bpt /= dev->endpoint[EP2I(ep)].max_packet_size; 737 bpt *= dev->endpoint[EP2I(ep)].max_packet_size; 738 start.bytes_per_transfer = bpt; 739 /* No id, we look at the ep when receiving a status back */ 740 usbredirparser_send_start_bulk_receiving(dev->parser, 0, &start); 741 usbredirparser_do_write(dev->parser); 742 DPRINTF("bulk receiving started bytes/transfer %u count %d ep %02X\n", 743 start.bytes_per_transfer, start.no_transfers, ep); 744 dev->endpoint[EP2I(ep)].bulk_receiving_started = 1; 745 /* We don't really want to drop bulk packets ever, but 746 having some upper limit to how much we buffer is good. */ 747 dev->endpoint[EP2I(ep)].bufpq_target_size = 5000; 748 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; 749 } 750 751 if (QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq)) { 752 DPRINTF("bulk-token-in ep %02X, no bulkp\n", ep); 753 assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL); 754 dev->endpoint[EP2I(ep)].pending_async_packet = p; 755 p->status = USB_RET_ASYNC; 756 return; 757 } 758 usbredir_buffered_bulk_in_complete(dev, p, ep); 759 } 760 761 static void usbredir_stop_bulk_receiving(USBRedirDevice *dev, uint8_t ep) 762 { 763 struct usb_redir_stop_bulk_receiving_header stop_bulk = { 764 .endpoint = ep, 765 .stream_id = 0, 766 }; 767 if (dev->endpoint[EP2I(ep)].bulk_receiving_started) { 768 usbredirparser_send_stop_bulk_receiving(dev->parser, 0, &stop_bulk); 769 DPRINTF("bulk receiving stopped ep %02X\n", ep); 770 dev->endpoint[EP2I(ep)].bulk_receiving_started = 0; 771 } 772 usbredir_free_bufpq(dev, ep); 773 } 774 775 static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p, 776 uint8_t ep) 777 { 778 struct usb_redir_bulk_packet_header bulk_packet; 779 size_t size = usb_packet_size(p); 780 const int maxp = dev->endpoint[EP2I(ep)].max_packet_size; 781 782 if (usbredir_already_in_flight(dev, p->id)) { 783 p->status = USB_RET_ASYNC; 784 return; 785 } 786 787 if (dev->endpoint[EP2I(ep)].bulk_receiving_enabled) { 788 if (size != 0 && (size % maxp) == 0) { 789 usbredir_handle_buffered_bulk_in_data(dev, p, ep); 790 return; 791 } 792 WARNING("bulk recv invalid size %zd ep %02x, disabling\n", size, ep); 793 assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL); 794 usbredir_stop_bulk_receiving(dev, ep); 795 dev->endpoint[EP2I(ep)].bulk_receiving_enabled = 0; 796 } 797 798 DPRINTF("bulk-out ep %02X stream %u len %zd id %"PRIu64"\n", 799 ep, p->stream, size, p->id); 800 801 bulk_packet.endpoint = ep; 802 bulk_packet.length = size; 803 bulk_packet.stream_id = p->stream; 804 bulk_packet.length_high = size >> 16; 805 assert(bulk_packet.length_high == 0 || 806 usbredirparser_peer_has_cap(dev->parser, 807 usb_redir_cap_32bits_bulk_length)); 808 809 if (ep & USB_DIR_IN) { 810 usbredirparser_send_bulk_packet(dev->parser, p->id, 811 &bulk_packet, NULL, 0); 812 } else { 813 uint8_t buf[size]; 814 usb_packet_copy(p, buf, size); 815 usbredir_log_data(dev, "bulk data out:", buf, size); 816 usbredirparser_send_bulk_packet(dev->parser, p->id, 817 &bulk_packet, buf, size); 818 } 819 usbredirparser_do_write(dev->parser); 820 p->status = USB_RET_ASYNC; 821 } 822 823 static void usbredir_handle_interrupt_in_data(USBRedirDevice *dev, 824 USBPacket *p, uint8_t ep) 825 { 826 /* Input interrupt endpoint, buffered packet input */ 827 struct buf_packet *intp; 828 int status, len; 829 830 if (!dev->endpoint[EP2I(ep)].interrupt_started && 831 !dev->endpoint[EP2I(ep)].interrupt_error) { 832 struct usb_redir_start_interrupt_receiving_header start_int = { 833 .endpoint = ep, 834 }; 835 /* No id, we look at the ep when receiving a status back */ 836 usbredirparser_send_start_interrupt_receiving(dev->parser, 0, 837 &start_int); 838 usbredirparser_do_write(dev->parser); 839 DPRINTF("interrupt recv started ep %02X\n", ep); 840 dev->endpoint[EP2I(ep)].interrupt_started = 1; 841 /* We don't really want to drop interrupt packets ever, but 842 having some upper limit to how much we buffer is good. */ 843 dev->endpoint[EP2I(ep)].bufpq_target_size = 1000; 844 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; 845 } 846 847 intp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); 848 if (intp == NULL) { 849 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep); 850 /* Check interrupt_error for stream errors */ 851 status = dev->endpoint[EP2I(ep)].interrupt_error; 852 dev->endpoint[EP2I(ep)].interrupt_error = 0; 853 if (status) { 854 usbredir_handle_status(dev, p, status); 855 } else { 856 p->status = USB_RET_NAK; 857 } 858 return; 859 } 860 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep, 861 intp->status, intp->len); 862 863 status = intp->status; 864 len = intp->len; 865 if (len > p->iov.size) { 866 ERROR("received int data is larger then packet ep %02X\n", ep); 867 len = p->iov.size; 868 status = usb_redir_babble; 869 } 870 usb_packet_copy(p, intp->data, len); 871 bufp_free(dev, intp, ep); 872 usbredir_handle_status(dev, p, status); 873 } 874 875 /* 876 * Handle interrupt out data, the usbredir protocol expects us to do this 877 * async, so that it can report back a completion status. But guests will 878 * expect immediate completion for an interrupt endpoint, and handling this 879 * async causes migration issues. So we report success directly, counting 880 * on the fact that output interrupt packets normally always succeed. 881 */ 882 static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev, 883 USBPacket *p, uint8_t ep) 884 { 885 struct usb_redir_interrupt_packet_header interrupt_packet; 886 uint8_t buf[p->iov.size]; 887 888 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep, 889 p->iov.size, p->id); 890 891 interrupt_packet.endpoint = ep; 892 interrupt_packet.length = p->iov.size; 893 894 usb_packet_copy(p, buf, p->iov.size); 895 usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size); 896 usbredirparser_send_interrupt_packet(dev->parser, p->id, 897 &interrupt_packet, buf, p->iov.size); 898 usbredirparser_do_write(dev->parser); 899 } 900 901 static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev, 902 uint8_t ep) 903 { 904 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = { 905 .endpoint = ep 906 }; 907 if (dev->endpoint[EP2I(ep)].interrupt_started) { 908 usbredirparser_send_stop_interrupt_receiving(dev->parser, 0, 909 &stop_interrupt_recv); 910 DPRINTF("interrupt recv stopped ep %02X\n", ep); 911 dev->endpoint[EP2I(ep)].interrupt_started = 0; 912 } 913 dev->endpoint[EP2I(ep)].interrupt_error = 0; 914 usbredir_free_bufpq(dev, ep); 915 } 916 917 static void usbredir_handle_data(USBDevice *udev, USBPacket *p) 918 { 919 USBRedirDevice *dev = USB_REDIRECT(udev); 920 uint8_t ep; 921 922 ep = p->ep->nr; 923 if (p->pid == USB_TOKEN_IN) { 924 ep |= USB_DIR_IN; 925 } 926 927 switch (dev->endpoint[EP2I(ep)].type) { 928 case USB_ENDPOINT_XFER_CONTROL: 929 ERROR("handle_data called for control transfer on ep %02X\n", ep); 930 p->status = USB_RET_NAK; 931 break; 932 case USB_ENDPOINT_XFER_BULK: 933 if (p->state == USB_PACKET_SETUP && p->pid == USB_TOKEN_IN && 934 p->ep->pipeline) { 935 p->status = USB_RET_ADD_TO_QUEUE; 936 break; 937 } 938 usbredir_handle_bulk_data(dev, p, ep); 939 break; 940 case USB_ENDPOINT_XFER_ISOC: 941 usbredir_handle_iso_data(dev, p, ep); 942 break; 943 case USB_ENDPOINT_XFER_INT: 944 if (ep & USB_DIR_IN) { 945 usbredir_handle_interrupt_in_data(dev, p, ep); 946 } else { 947 usbredir_handle_interrupt_out_data(dev, p, ep); 948 } 949 break; 950 default: 951 ERROR("handle_data ep %02X has unknown type %d\n", ep, 952 dev->endpoint[EP2I(ep)].type); 953 p->status = USB_RET_NAK; 954 } 955 } 956 957 static void usbredir_flush_ep_queue(USBDevice *dev, USBEndpoint *ep) 958 { 959 if (ep->pid == USB_TOKEN_IN && ep->pipeline) { 960 usb_ep_combine_input_packets(ep); 961 } 962 } 963 964 static void usbredir_stop_ep(USBRedirDevice *dev, int i) 965 { 966 uint8_t ep = I2EP(i); 967 968 switch (dev->endpoint[i].type) { 969 case USB_ENDPOINT_XFER_BULK: 970 if (ep & USB_DIR_IN) { 971 usbredir_stop_bulk_receiving(dev, ep); 972 } 973 break; 974 case USB_ENDPOINT_XFER_ISOC: 975 usbredir_stop_iso_stream(dev, ep); 976 break; 977 case USB_ENDPOINT_XFER_INT: 978 if (ep & USB_DIR_IN) { 979 usbredir_stop_interrupt_receiving(dev, ep); 980 } 981 break; 982 } 983 usbredir_free_bufpq(dev, ep); 984 } 985 986 static void usbredir_ep_stopped(USBDevice *udev, USBEndpoint *uep) 987 { 988 USBRedirDevice *dev = USB_REDIRECT(udev); 989 990 usbredir_stop_ep(dev, USBEP2I(uep)); 991 usbredirparser_do_write(dev->parser); 992 } 993 994 static void usbredir_set_config(USBRedirDevice *dev, USBPacket *p, 995 int config) 996 { 997 struct usb_redir_set_configuration_header set_config; 998 int i; 999 1000 DPRINTF("set config %d id %"PRIu64"\n", config, p->id); 1001 1002 for (i = 0; i < MAX_ENDPOINTS; i++) { 1003 usbredir_stop_ep(dev, i); 1004 } 1005 1006 set_config.configuration = config; 1007 usbredirparser_send_set_configuration(dev->parser, p->id, &set_config); 1008 usbredirparser_do_write(dev->parser); 1009 p->status = USB_RET_ASYNC; 1010 } 1011 1012 static void usbredir_get_config(USBRedirDevice *dev, USBPacket *p) 1013 { 1014 DPRINTF("get config id %"PRIu64"\n", p->id); 1015 1016 usbredirparser_send_get_configuration(dev->parser, p->id); 1017 usbredirparser_do_write(dev->parser); 1018 p->status = USB_RET_ASYNC; 1019 } 1020 1021 static void usbredir_set_interface(USBRedirDevice *dev, USBPacket *p, 1022 int interface, int alt) 1023 { 1024 struct usb_redir_set_alt_setting_header set_alt; 1025 int i; 1026 1027 DPRINTF("set interface %d alt %d id %"PRIu64"\n", interface, alt, p->id); 1028 1029 for (i = 0; i < MAX_ENDPOINTS; i++) { 1030 if (dev->endpoint[i].interface == interface) { 1031 usbredir_stop_ep(dev, i); 1032 } 1033 } 1034 1035 set_alt.interface = interface; 1036 set_alt.alt = alt; 1037 usbredirparser_send_set_alt_setting(dev->parser, p->id, &set_alt); 1038 usbredirparser_do_write(dev->parser); 1039 p->status = USB_RET_ASYNC; 1040 } 1041 1042 static void usbredir_get_interface(USBRedirDevice *dev, USBPacket *p, 1043 int interface) 1044 { 1045 struct usb_redir_get_alt_setting_header get_alt; 1046 1047 DPRINTF("get interface %d id %"PRIu64"\n", interface, p->id); 1048 1049 get_alt.interface = interface; 1050 usbredirparser_send_get_alt_setting(dev->parser, p->id, &get_alt); 1051 usbredirparser_do_write(dev->parser); 1052 p->status = USB_RET_ASYNC; 1053 } 1054 1055 static void usbredir_handle_control(USBDevice *udev, USBPacket *p, 1056 int request, int value, int index, int length, uint8_t *data) 1057 { 1058 USBRedirDevice *dev = USB_REDIRECT(udev); 1059 struct usb_redir_control_packet_header control_packet; 1060 1061 if (usbredir_already_in_flight(dev, p->id)) { 1062 p->status = USB_RET_ASYNC; 1063 return; 1064 } 1065 1066 /* Special cases for certain standard device requests */ 1067 switch (request) { 1068 case DeviceOutRequest | USB_REQ_SET_ADDRESS: 1069 DPRINTF("set address %d\n", value); 1070 dev->dev.addr = value; 1071 return; 1072 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: 1073 usbredir_set_config(dev, p, value & 0xff); 1074 return; 1075 case DeviceRequest | USB_REQ_GET_CONFIGURATION: 1076 usbredir_get_config(dev, p); 1077 return; 1078 case InterfaceOutRequest | USB_REQ_SET_INTERFACE: 1079 usbredir_set_interface(dev, p, index, value); 1080 return; 1081 case InterfaceRequest | USB_REQ_GET_INTERFACE: 1082 usbredir_get_interface(dev, p, index); 1083 return; 1084 } 1085 1086 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */ 1087 DPRINTF( 1088 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64"\n", 1089 request >> 8, request & 0xff, value, index, length, p->id); 1090 1091 control_packet.request = request & 0xFF; 1092 control_packet.requesttype = request >> 8; 1093 control_packet.endpoint = control_packet.requesttype & USB_DIR_IN; 1094 control_packet.value = value; 1095 control_packet.index = index; 1096 control_packet.length = length; 1097 1098 if (control_packet.requesttype & USB_DIR_IN) { 1099 usbredirparser_send_control_packet(dev->parser, p->id, 1100 &control_packet, NULL, 0); 1101 } else { 1102 usbredir_log_data(dev, "ctrl data out:", data, length); 1103 usbredirparser_send_control_packet(dev->parser, p->id, 1104 &control_packet, data, length); 1105 } 1106 usbredirparser_do_write(dev->parser); 1107 p->status = USB_RET_ASYNC; 1108 } 1109 1110 static int usbredir_alloc_streams(USBDevice *udev, USBEndpoint **eps, 1111 int nr_eps, int streams) 1112 { 1113 USBRedirDevice *dev = USB_REDIRECT(udev); 1114 #if USBREDIR_VERSION >= 0x000700 1115 struct usb_redir_alloc_bulk_streams_header alloc_streams; 1116 int i; 1117 1118 if (!usbredirparser_peer_has_cap(dev->parser, 1119 usb_redir_cap_bulk_streams)) { 1120 ERROR("peer does not support streams\n"); 1121 goto reject; 1122 } 1123 1124 if (streams == 0) { 1125 ERROR("request to allocate 0 streams\n"); 1126 return -1; 1127 } 1128 1129 alloc_streams.no_streams = streams; 1130 alloc_streams.endpoints = 0; 1131 for (i = 0; i < nr_eps; i++) { 1132 alloc_streams.endpoints |= 1 << USBEP2I(eps[i]); 1133 } 1134 usbredirparser_send_alloc_bulk_streams(dev->parser, 0, &alloc_streams); 1135 usbredirparser_do_write(dev->parser); 1136 1137 return 0; 1138 #else 1139 ERROR("usbredir_alloc_streams not implemented\n"); 1140 goto reject; 1141 #endif 1142 reject: 1143 ERROR("streams are not available, disconnecting\n"); 1144 qemu_bh_schedule(dev->device_reject_bh); 1145 return -1; 1146 } 1147 1148 static void usbredir_free_streams(USBDevice *udev, USBEndpoint **eps, 1149 int nr_eps) 1150 { 1151 #if USBREDIR_VERSION >= 0x000700 1152 USBRedirDevice *dev = USB_REDIRECT(udev); 1153 struct usb_redir_free_bulk_streams_header free_streams; 1154 int i; 1155 1156 if (!usbredirparser_peer_has_cap(dev->parser, 1157 usb_redir_cap_bulk_streams)) { 1158 return; 1159 } 1160 1161 free_streams.endpoints = 0; 1162 for (i = 0; i < nr_eps; i++) { 1163 free_streams.endpoints |= 1 << USBEP2I(eps[i]); 1164 } 1165 usbredirparser_send_free_bulk_streams(dev->parser, 0, &free_streams); 1166 usbredirparser_do_write(dev->parser); 1167 #endif 1168 } 1169 1170 /* 1171 * Close events can be triggered by usbredirparser_do_write which gets called 1172 * from within the USBDevice data / control packet callbacks and doing a 1173 * usb_detach from within these callbacks is not a good idea. 1174 * 1175 * So we use a bh handler to take care of close events. 1176 */ 1177 static void usbredir_chardev_close_bh(void *opaque) 1178 { 1179 USBRedirDevice *dev = opaque; 1180 1181 qemu_bh_cancel(dev->device_reject_bh); 1182 usbredir_device_disconnect(dev); 1183 1184 if (dev->parser) { 1185 DPRINTF("destroying usbredirparser\n"); 1186 usbredirparser_destroy(dev->parser); 1187 dev->parser = NULL; 1188 } 1189 if (dev->watch) { 1190 g_source_remove(dev->watch); 1191 dev->watch = 0; 1192 } 1193 } 1194 1195 static void usbredir_create_parser(USBRedirDevice *dev) 1196 { 1197 uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, }; 1198 int flags = 0; 1199 1200 DPRINTF("creating usbredirparser\n"); 1201 1202 dev->parser = qemu_oom_check(usbredirparser_create()); 1203 dev->parser->priv = dev; 1204 dev->parser->log_func = usbredir_log; 1205 dev->parser->read_func = usbredir_read; 1206 dev->parser->write_func = usbredir_write; 1207 dev->parser->hello_func = usbredir_hello; 1208 dev->parser->device_connect_func = usbredir_device_connect; 1209 dev->parser->device_disconnect_func = usbredir_device_disconnect; 1210 dev->parser->interface_info_func = usbredir_interface_info; 1211 dev->parser->ep_info_func = usbredir_ep_info; 1212 dev->parser->configuration_status_func = usbredir_configuration_status; 1213 dev->parser->alt_setting_status_func = usbredir_alt_setting_status; 1214 dev->parser->iso_stream_status_func = usbredir_iso_stream_status; 1215 dev->parser->interrupt_receiving_status_func = 1216 usbredir_interrupt_receiving_status; 1217 dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status; 1218 dev->parser->bulk_receiving_status_func = usbredir_bulk_receiving_status; 1219 dev->parser->control_packet_func = usbredir_control_packet; 1220 dev->parser->bulk_packet_func = usbredir_bulk_packet; 1221 dev->parser->iso_packet_func = usbredir_iso_packet; 1222 dev->parser->interrupt_packet_func = usbredir_interrupt_packet; 1223 dev->parser->buffered_bulk_packet_func = usbredir_buffered_bulk_packet; 1224 dev->read_buf = NULL; 1225 dev->read_buf_size = 0; 1226 1227 usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version); 1228 usbredirparser_caps_set_cap(caps, usb_redir_cap_filter); 1229 usbredirparser_caps_set_cap(caps, usb_redir_cap_ep_info_max_packet_size); 1230 usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids); 1231 usbredirparser_caps_set_cap(caps, usb_redir_cap_32bits_bulk_length); 1232 usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_receiving); 1233 #if USBREDIR_VERSION >= 0x000700 1234 if (dev->enable_streams) { 1235 usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_streams); 1236 } 1237 #endif 1238 1239 if (runstate_check(RUN_STATE_INMIGRATE)) { 1240 flags |= usbredirparser_fl_no_hello; 1241 } 1242 usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE, 1243 flags); 1244 usbredirparser_do_write(dev->parser); 1245 } 1246 1247 static void usbredir_reject_device(USBRedirDevice *dev) 1248 { 1249 usbredir_device_disconnect(dev); 1250 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) { 1251 usbredirparser_send_filter_reject(dev->parser); 1252 usbredirparser_do_write(dev->parser); 1253 } 1254 } 1255 1256 /* 1257 * We may need to reject the device when the hcd calls alloc_streams, doing 1258 * an usb_detach from within a hcd call is not a good idea, hence this bh. 1259 */ 1260 static void usbredir_device_reject_bh(void *opaque) 1261 { 1262 USBRedirDevice *dev = opaque; 1263 1264 usbredir_reject_device(dev); 1265 } 1266 1267 static void usbredir_do_attach(void *opaque) 1268 { 1269 USBRedirDevice *dev = opaque; 1270 Error *local_err = NULL; 1271 1272 /* In order to work properly with XHCI controllers we need these caps */ 1273 if ((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER) && !( 1274 usbredirparser_peer_has_cap(dev->parser, 1275 usb_redir_cap_ep_info_max_packet_size) && 1276 usbredirparser_peer_has_cap(dev->parser, 1277 usb_redir_cap_32bits_bulk_length) && 1278 usbredirparser_peer_has_cap(dev->parser, 1279 usb_redir_cap_64bits_ids))) { 1280 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n"); 1281 usbredir_reject_device(dev); 1282 return; 1283 } 1284 1285 usb_device_attach(&dev->dev, &local_err); 1286 if (local_err) { 1287 error_report_err(local_err); 1288 WARNING("rejecting device due to speed mismatch\n"); 1289 usbredir_reject_device(dev); 1290 } 1291 } 1292 1293 /* 1294 * chardev callbacks 1295 */ 1296 1297 static int usbredir_chardev_can_read(void *opaque) 1298 { 1299 USBRedirDevice *dev = opaque; 1300 1301 if (!dev->parser) { 1302 WARNING("chardev_can_read called on non open chardev!\n"); 1303 return 0; 1304 } 1305 1306 /* Don't read new data from the chardev until our state is fully synced */ 1307 if (!runstate_check(RUN_STATE_RUNNING)) { 1308 return 0; 1309 } 1310 1311 /* usbredir_parser_do_read will consume *all* data we give it */ 1312 return 1024 * 1024; 1313 } 1314 1315 static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size) 1316 { 1317 USBRedirDevice *dev = opaque; 1318 1319 /* No recursion allowed! */ 1320 assert(dev->read_buf == NULL); 1321 1322 dev->read_buf = buf; 1323 dev->read_buf_size = size; 1324 1325 usbredirparser_do_read(dev->parser); 1326 /* Send any acks, etc. which may be queued now */ 1327 usbredirparser_do_write(dev->parser); 1328 } 1329 1330 static void usbredir_chardev_event(void *opaque, int event) 1331 { 1332 USBRedirDevice *dev = opaque; 1333 1334 switch (event) { 1335 case CHR_EVENT_OPENED: 1336 DPRINTF("chardev open\n"); 1337 /* Make sure any pending closes are handled (no-op if none pending) */ 1338 usbredir_chardev_close_bh(dev); 1339 qemu_bh_cancel(dev->chardev_close_bh); 1340 usbredir_create_parser(dev); 1341 break; 1342 case CHR_EVENT_CLOSED: 1343 DPRINTF("chardev close\n"); 1344 qemu_bh_schedule(dev->chardev_close_bh); 1345 break; 1346 } 1347 } 1348 1349 /* 1350 * init + destroy 1351 */ 1352 1353 static void usbredir_vm_state_change(void *priv, int running, RunState state) 1354 { 1355 USBRedirDevice *dev = priv; 1356 1357 if (state == RUN_STATE_RUNNING && dev->parser != NULL) { 1358 usbredirparser_do_write(dev->parser); /* Flush any pending writes */ 1359 } 1360 } 1361 1362 static void usbredir_init_endpoints(USBRedirDevice *dev) 1363 { 1364 int i; 1365 1366 usb_ep_init(&dev->dev); 1367 memset(dev->endpoint, 0, sizeof(dev->endpoint)); 1368 for (i = 0; i < MAX_ENDPOINTS; i++) { 1369 dev->endpoint[i].dev = dev; 1370 QTAILQ_INIT(&dev->endpoint[i].bufpq); 1371 } 1372 } 1373 1374 static void usbredir_realize(USBDevice *udev, Error **errp) 1375 { 1376 USBRedirDevice *dev = USB_REDIRECT(udev); 1377 int i; 1378 1379 if (!qemu_chr_fe_get_driver(&dev->cs)) { 1380 error_setg(errp, QERR_MISSING_PARAMETER, "chardev"); 1381 return; 1382 } 1383 1384 if (dev->filter_str) { 1385 i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|", 1386 &dev->filter_rules, 1387 &dev->filter_rules_count); 1388 if (i) { 1389 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "filter", 1390 "a usb device filter string"); 1391 return; 1392 } 1393 } 1394 1395 dev->chardev_close_bh = qemu_bh_new(usbredir_chardev_close_bh, dev); 1396 dev->device_reject_bh = qemu_bh_new(usbredir_device_reject_bh, dev); 1397 dev->attach_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, usbredir_do_attach, dev); 1398 1399 packet_id_queue_init(&dev->cancelled, dev, "cancelled"); 1400 packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight"); 1401 usbredir_init_endpoints(dev); 1402 1403 /* We'll do the attach once we receive the speed from the usb-host */ 1404 udev->auto_attach = 0; 1405 1406 /* Will be cleared during setup when we find conflicts */ 1407 dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH; 1408 1409 /* Let the backend know we are ready */ 1410 qemu_chr_fe_set_handlers(&dev->cs, usbredir_chardev_can_read, 1411 usbredir_chardev_read, usbredir_chardev_event, 1412 dev, NULL, true); 1413 1414 qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev); 1415 } 1416 1417 static void usbredir_cleanup_device_queues(USBRedirDevice *dev) 1418 { 1419 int i; 1420 1421 packet_id_queue_empty(&dev->cancelled); 1422 packet_id_queue_empty(&dev->already_in_flight); 1423 for (i = 0; i < MAX_ENDPOINTS; i++) { 1424 usbredir_free_bufpq(dev, I2EP(i)); 1425 } 1426 } 1427 1428 static void usbredir_handle_destroy(USBDevice *udev) 1429 { 1430 USBRedirDevice *dev = USB_REDIRECT(udev); 1431 CharDriverState *chr = qemu_chr_fe_get_driver(&dev->cs); 1432 1433 qemu_chr_fe_deinit(&dev->cs); 1434 qemu_chr_delete(chr); 1435 1436 /* Note must be done after qemu_chr_close, as that causes a close event */ 1437 qemu_bh_delete(dev->chardev_close_bh); 1438 qemu_bh_delete(dev->device_reject_bh); 1439 1440 timer_del(dev->attach_timer); 1441 timer_free(dev->attach_timer); 1442 1443 usbredir_cleanup_device_queues(dev); 1444 1445 if (dev->parser) { 1446 usbredirparser_destroy(dev->parser); 1447 } 1448 if (dev->watch) { 1449 g_source_remove(dev->watch); 1450 } 1451 1452 free(dev->filter_rules); 1453 } 1454 1455 static int usbredir_check_filter(USBRedirDevice *dev) 1456 { 1457 if (dev->interface_info.interface_count == NO_INTERFACE_INFO) { 1458 ERROR("No interface info for device\n"); 1459 goto error; 1460 } 1461 1462 if (dev->filter_rules) { 1463 if (!usbredirparser_peer_has_cap(dev->parser, 1464 usb_redir_cap_connect_device_version)) { 1465 ERROR("Device filter specified and peer does not have the " 1466 "connect_device_version capability\n"); 1467 goto error; 1468 } 1469 1470 if (usbredirfilter_check( 1471 dev->filter_rules, 1472 dev->filter_rules_count, 1473 dev->device_info.device_class, 1474 dev->device_info.device_subclass, 1475 dev->device_info.device_protocol, 1476 dev->interface_info.interface_class, 1477 dev->interface_info.interface_subclass, 1478 dev->interface_info.interface_protocol, 1479 dev->interface_info.interface_count, 1480 dev->device_info.vendor_id, 1481 dev->device_info.product_id, 1482 dev->device_info.device_version_bcd, 1483 0) != 0) { 1484 goto error; 1485 } 1486 } 1487 1488 return 0; 1489 1490 error: 1491 usbredir_reject_device(dev); 1492 return -1; 1493 } 1494 1495 static void usbredir_check_bulk_receiving(USBRedirDevice *dev) 1496 { 1497 int i, j, quirks; 1498 1499 if (!usbredirparser_peer_has_cap(dev->parser, 1500 usb_redir_cap_bulk_receiving)) { 1501 return; 1502 } 1503 1504 for (i = EP2I(USB_DIR_IN); i < MAX_ENDPOINTS; i++) { 1505 dev->endpoint[i].bulk_receiving_enabled = 0; 1506 } 1507 for (i = 0; i < dev->interface_info.interface_count; i++) { 1508 quirks = usb_get_quirks(dev->device_info.vendor_id, 1509 dev->device_info.product_id, 1510 dev->interface_info.interface_class[i], 1511 dev->interface_info.interface_subclass[i], 1512 dev->interface_info.interface_protocol[i]); 1513 if (!(quirks & USB_QUIRK_BUFFER_BULK_IN)) { 1514 continue; 1515 } 1516 if (quirks & USB_QUIRK_IS_FTDI) { 1517 dev->buffered_bulk_in_complete = 1518 usbredir_buffered_bulk_in_complete_ftdi; 1519 } else { 1520 dev->buffered_bulk_in_complete = 1521 usbredir_buffered_bulk_in_complete_raw; 1522 } 1523 1524 for (j = EP2I(USB_DIR_IN); j < MAX_ENDPOINTS; j++) { 1525 if (dev->endpoint[j].interface == 1526 dev->interface_info.interface[i] && 1527 dev->endpoint[j].type == USB_ENDPOINT_XFER_BULK && 1528 dev->endpoint[j].max_packet_size != 0) { 1529 dev->endpoint[j].bulk_receiving_enabled = 1; 1530 /* 1531 * With buffering pipelining is not necessary. Also packet 1532 * combining and bulk in buffering don't play nice together! 1533 */ 1534 I2USBEP(dev, j)->pipeline = false; 1535 break; /* Only buffer for the first ep of each intf */ 1536 } 1537 } 1538 } 1539 } 1540 1541 /* 1542 * usbredirparser packet complete callbacks 1543 */ 1544 1545 static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p, 1546 int status) 1547 { 1548 switch (status) { 1549 case usb_redir_success: 1550 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ 1551 break; 1552 case usb_redir_stall: 1553 p->status = USB_RET_STALL; 1554 break; 1555 case usb_redir_cancelled: 1556 /* 1557 * When the usbredir-host unredirects a device, it will report a status 1558 * of cancelled for all pending packets, followed by a disconnect msg. 1559 */ 1560 p->status = USB_RET_IOERROR; 1561 break; 1562 case usb_redir_inval: 1563 WARNING("got invalid param error from usb-host?\n"); 1564 p->status = USB_RET_IOERROR; 1565 break; 1566 case usb_redir_babble: 1567 p->status = USB_RET_BABBLE; 1568 break; 1569 case usb_redir_ioerror: 1570 case usb_redir_timeout: 1571 default: 1572 p->status = USB_RET_IOERROR; 1573 } 1574 } 1575 1576 static void usbredir_hello(void *priv, struct usb_redir_hello_header *h) 1577 { 1578 USBRedirDevice *dev = priv; 1579 1580 /* Try to send the filter info now that we've the usb-host's caps */ 1581 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) && 1582 dev->filter_rules) { 1583 usbredirparser_send_filter_filter(dev->parser, dev->filter_rules, 1584 dev->filter_rules_count); 1585 usbredirparser_do_write(dev->parser); 1586 } 1587 } 1588 1589 static void usbredir_device_connect(void *priv, 1590 struct usb_redir_device_connect_header *device_connect) 1591 { 1592 USBRedirDevice *dev = priv; 1593 const char *speed; 1594 1595 if (timer_pending(dev->attach_timer) || dev->dev.attached) { 1596 ERROR("Received device connect while already connected\n"); 1597 return; 1598 } 1599 1600 switch (device_connect->speed) { 1601 case usb_redir_speed_low: 1602 speed = "low speed"; 1603 dev->dev.speed = USB_SPEED_LOW; 1604 dev->compatible_speedmask &= ~USB_SPEED_MASK_FULL; 1605 dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH; 1606 break; 1607 case usb_redir_speed_full: 1608 speed = "full speed"; 1609 dev->dev.speed = USB_SPEED_FULL; 1610 dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH; 1611 break; 1612 case usb_redir_speed_high: 1613 speed = "high speed"; 1614 dev->dev.speed = USB_SPEED_HIGH; 1615 break; 1616 case usb_redir_speed_super: 1617 speed = "super speed"; 1618 dev->dev.speed = USB_SPEED_SUPER; 1619 break; 1620 default: 1621 speed = "unknown speed"; 1622 dev->dev.speed = USB_SPEED_FULL; 1623 } 1624 1625 if (usbredirparser_peer_has_cap(dev->parser, 1626 usb_redir_cap_connect_device_version)) { 1627 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n", 1628 speed, device_connect->vendor_id, device_connect->product_id, 1629 ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 + 1630 ((device_connect->device_version_bcd & 0x0f00) >> 8), 1631 ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 + 1632 ((device_connect->device_version_bcd & 0x000f) >> 0), 1633 device_connect->device_class); 1634 } else { 1635 INFO("attaching %s device %04x:%04x class %02x\n", speed, 1636 device_connect->vendor_id, device_connect->product_id, 1637 device_connect->device_class); 1638 } 1639 1640 dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask; 1641 dev->device_info = *device_connect; 1642 1643 if (usbredir_check_filter(dev)) { 1644 WARNING("Device %04x:%04x rejected by device filter, not attaching\n", 1645 device_connect->vendor_id, device_connect->product_id); 1646 return; 1647 } 1648 1649 usbredir_check_bulk_receiving(dev); 1650 timer_mod(dev->attach_timer, dev->next_attach_time); 1651 } 1652 1653 static void usbredir_device_disconnect(void *priv) 1654 { 1655 USBRedirDevice *dev = priv; 1656 1657 /* Stop any pending attaches */ 1658 timer_del(dev->attach_timer); 1659 1660 if (dev->dev.attached) { 1661 DPRINTF("detaching device\n"); 1662 usb_device_detach(&dev->dev); 1663 /* 1664 * Delay next usb device attach to give the guest a chance to see 1665 * see the detach / attach in case of quick close / open succession 1666 */ 1667 dev->next_attach_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 200; 1668 } 1669 1670 /* Reset state so that the next dev connected starts with a clean slate */ 1671 usbredir_cleanup_device_queues(dev); 1672 usbredir_init_endpoints(dev); 1673 dev->interface_info.interface_count = NO_INTERFACE_INFO; 1674 dev->dev.addr = 0; 1675 dev->dev.speed = 0; 1676 dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH; 1677 } 1678 1679 static void usbredir_interface_info(void *priv, 1680 struct usb_redir_interface_info_header *interface_info) 1681 { 1682 USBRedirDevice *dev = priv; 1683 1684 dev->interface_info = *interface_info; 1685 1686 /* 1687 * If we receive interface info after the device has already been 1688 * connected (ie on a set_config), re-check interface dependent things. 1689 */ 1690 if (timer_pending(dev->attach_timer) || dev->dev.attached) { 1691 usbredir_check_bulk_receiving(dev); 1692 if (usbredir_check_filter(dev)) { 1693 ERROR("Device no longer matches filter after interface info " 1694 "change, disconnecting!\n"); 1695 } 1696 } 1697 } 1698 1699 static void usbredir_mark_speed_incompatible(USBRedirDevice *dev, int speed) 1700 { 1701 dev->compatible_speedmask &= ~(1 << speed); 1702 dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask; 1703 } 1704 1705 static void usbredir_set_pipeline(USBRedirDevice *dev, struct USBEndpoint *uep) 1706 { 1707 if (uep->type != USB_ENDPOINT_XFER_BULK) { 1708 return; 1709 } 1710 if (uep->pid == USB_TOKEN_OUT) { 1711 uep->pipeline = true; 1712 } 1713 if (uep->pid == USB_TOKEN_IN && uep->max_packet_size != 0 && 1714 usbredirparser_peer_has_cap(dev->parser, 1715 usb_redir_cap_32bits_bulk_length)) { 1716 uep->pipeline = true; 1717 } 1718 } 1719 1720 static void usbredir_setup_usb_eps(USBRedirDevice *dev) 1721 { 1722 struct USBEndpoint *usb_ep; 1723 int i; 1724 1725 for (i = 0; i < MAX_ENDPOINTS; i++) { 1726 usb_ep = I2USBEP(dev, i); 1727 usb_ep->type = dev->endpoint[i].type; 1728 usb_ep->ifnum = dev->endpoint[i].interface; 1729 usb_ep->max_packet_size = dev->endpoint[i].max_packet_size; 1730 usb_ep->max_streams = dev->endpoint[i].max_streams; 1731 usbredir_set_pipeline(dev, usb_ep); 1732 } 1733 } 1734 1735 static void usbredir_ep_info(void *priv, 1736 struct usb_redir_ep_info_header *ep_info) 1737 { 1738 USBRedirDevice *dev = priv; 1739 int i; 1740 1741 for (i = 0; i < MAX_ENDPOINTS; i++) { 1742 dev->endpoint[i].type = ep_info->type[i]; 1743 dev->endpoint[i].interval = ep_info->interval[i]; 1744 dev->endpoint[i].interface = ep_info->interface[i]; 1745 if (usbredirparser_peer_has_cap(dev->parser, 1746 usb_redir_cap_ep_info_max_packet_size)) { 1747 dev->endpoint[i].max_packet_size = ep_info->max_packet_size[i]; 1748 } 1749 #if USBREDIR_VERSION >= 0x000700 1750 if (usbredirparser_peer_has_cap(dev->parser, 1751 usb_redir_cap_bulk_streams)) { 1752 dev->endpoint[i].max_streams = ep_info->max_streams[i]; 1753 } 1754 #endif 1755 switch (dev->endpoint[i].type) { 1756 case usb_redir_type_invalid: 1757 break; 1758 case usb_redir_type_iso: 1759 usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL); 1760 usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH); 1761 /* Fall through */ 1762 case usb_redir_type_interrupt: 1763 if (!usbredirparser_peer_has_cap(dev->parser, 1764 usb_redir_cap_ep_info_max_packet_size) || 1765 ep_info->max_packet_size[i] > 64) { 1766 usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL); 1767 } 1768 if (!usbredirparser_peer_has_cap(dev->parser, 1769 usb_redir_cap_ep_info_max_packet_size) || 1770 ep_info->max_packet_size[i] > 1024) { 1771 usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH); 1772 } 1773 if (dev->endpoint[i].interval == 0) { 1774 ERROR("Received 0 interval for isoc or irq endpoint\n"); 1775 usbredir_reject_device(dev); 1776 return; 1777 } 1778 /* Fall through */ 1779 case usb_redir_type_control: 1780 case usb_redir_type_bulk: 1781 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i), 1782 dev->endpoint[i].type, dev->endpoint[i].interface); 1783 break; 1784 default: 1785 ERROR("Received invalid endpoint type\n"); 1786 usbredir_reject_device(dev); 1787 return; 1788 } 1789 } 1790 /* The new ep info may have caused a speed incompatibility, recheck */ 1791 if (dev->dev.attached && 1792 !(dev->dev.port->speedmask & dev->dev.speedmask)) { 1793 ERROR("Device no longer matches speed after endpoint info change, " 1794 "disconnecting!\n"); 1795 usbredir_reject_device(dev); 1796 return; 1797 } 1798 usbredir_setup_usb_eps(dev); 1799 usbredir_check_bulk_receiving(dev); 1800 } 1801 1802 static void usbredir_configuration_status(void *priv, uint64_t id, 1803 struct usb_redir_configuration_status_header *config_status) 1804 { 1805 USBRedirDevice *dev = priv; 1806 USBPacket *p; 1807 1808 DPRINTF("set config status %d config %d id %"PRIu64"\n", 1809 config_status->status, config_status->configuration, id); 1810 1811 p = usbredir_find_packet_by_id(dev, 0, id); 1812 if (p) { 1813 if (dev->dev.setup_buf[0] & USB_DIR_IN) { 1814 dev->dev.data_buf[0] = config_status->configuration; 1815 p->actual_length = 1; 1816 } 1817 usbredir_handle_status(dev, p, config_status->status); 1818 usb_generic_async_ctrl_complete(&dev->dev, p); 1819 } 1820 } 1821 1822 static void usbredir_alt_setting_status(void *priv, uint64_t id, 1823 struct usb_redir_alt_setting_status_header *alt_setting_status) 1824 { 1825 USBRedirDevice *dev = priv; 1826 USBPacket *p; 1827 1828 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n", 1829 alt_setting_status->status, alt_setting_status->interface, 1830 alt_setting_status->alt, id); 1831 1832 p = usbredir_find_packet_by_id(dev, 0, id); 1833 if (p) { 1834 if (dev->dev.setup_buf[0] & USB_DIR_IN) { 1835 dev->dev.data_buf[0] = alt_setting_status->alt; 1836 p->actual_length = 1; 1837 } 1838 usbredir_handle_status(dev, p, alt_setting_status->status); 1839 usb_generic_async_ctrl_complete(&dev->dev, p); 1840 } 1841 } 1842 1843 static void usbredir_iso_stream_status(void *priv, uint64_t id, 1844 struct usb_redir_iso_stream_status_header *iso_stream_status) 1845 { 1846 USBRedirDevice *dev = priv; 1847 uint8_t ep = iso_stream_status->endpoint; 1848 1849 DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status, 1850 ep, id); 1851 1852 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) { 1853 return; 1854 } 1855 1856 dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status; 1857 if (iso_stream_status->status == usb_redir_stall) { 1858 DPRINTF("iso stream stopped by peer ep %02X\n", ep); 1859 dev->endpoint[EP2I(ep)].iso_started = 0; 1860 } 1861 } 1862 1863 static void usbredir_interrupt_receiving_status(void *priv, uint64_t id, 1864 struct usb_redir_interrupt_receiving_status_header 1865 *interrupt_receiving_status) 1866 { 1867 USBRedirDevice *dev = priv; 1868 uint8_t ep = interrupt_receiving_status->endpoint; 1869 1870 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n", 1871 interrupt_receiving_status->status, ep, id); 1872 1873 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) { 1874 return; 1875 } 1876 1877 dev->endpoint[EP2I(ep)].interrupt_error = 1878 interrupt_receiving_status->status; 1879 if (interrupt_receiving_status->status == usb_redir_stall) { 1880 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep); 1881 dev->endpoint[EP2I(ep)].interrupt_started = 0; 1882 } 1883 } 1884 1885 static void usbredir_bulk_streams_status(void *priv, uint64_t id, 1886 struct usb_redir_bulk_streams_status_header *bulk_streams_status) 1887 { 1888 #if USBREDIR_VERSION >= 0x000700 1889 USBRedirDevice *dev = priv; 1890 1891 if (bulk_streams_status->status == usb_redir_success) { 1892 DPRINTF("bulk streams status %d eps %08x\n", 1893 bulk_streams_status->status, bulk_streams_status->endpoints); 1894 } else { 1895 ERROR("bulk streams %s failed status %d eps %08x\n", 1896 (bulk_streams_status->no_streams == 0) ? "free" : "alloc", 1897 bulk_streams_status->status, bulk_streams_status->endpoints); 1898 ERROR("usb-redir-host does not provide streams, disconnecting\n"); 1899 usbredir_reject_device(dev); 1900 } 1901 #endif 1902 } 1903 1904 static void usbredir_bulk_receiving_status(void *priv, uint64_t id, 1905 struct usb_redir_bulk_receiving_status_header *bulk_receiving_status) 1906 { 1907 USBRedirDevice *dev = priv; 1908 uint8_t ep = bulk_receiving_status->endpoint; 1909 1910 DPRINTF("bulk recv status %d ep %02X id %"PRIu64"\n", 1911 bulk_receiving_status->status, ep, id); 1912 1913 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].bulk_receiving_started) { 1914 return; 1915 } 1916 1917 if (bulk_receiving_status->status == usb_redir_stall) { 1918 DPRINTF("bulk receiving stopped by peer ep %02X\n", ep); 1919 dev->endpoint[EP2I(ep)].bulk_receiving_started = 0; 1920 } 1921 } 1922 1923 static void usbredir_control_packet(void *priv, uint64_t id, 1924 struct usb_redir_control_packet_header *control_packet, 1925 uint8_t *data, int data_len) 1926 { 1927 USBRedirDevice *dev = priv; 1928 USBPacket *p; 1929 int len = control_packet->length; 1930 1931 DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status, 1932 len, id); 1933 1934 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices 1935 * to work redirected to a not superspeed capable hcd */ 1936 if (dev->dev.speed == USB_SPEED_SUPER && 1937 !((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER)) && 1938 control_packet->requesttype == 0x80 && 1939 control_packet->request == 6 && 1940 control_packet->value == 0x100 && control_packet->index == 0 && 1941 data_len >= 18 && data[7] == 9) { 1942 data[7] = 64; 1943 } 1944 1945 p = usbredir_find_packet_by_id(dev, 0, id); 1946 if (p) { 1947 usbredir_handle_status(dev, p, control_packet->status); 1948 if (data_len > 0) { 1949 usbredir_log_data(dev, "ctrl data in:", data, data_len); 1950 if (data_len > sizeof(dev->dev.data_buf)) { 1951 ERROR("ctrl buffer too small (%d > %zu)\n", 1952 data_len, sizeof(dev->dev.data_buf)); 1953 p->status = USB_RET_STALL; 1954 data_len = len = sizeof(dev->dev.data_buf); 1955 } 1956 memcpy(dev->dev.data_buf, data, data_len); 1957 } 1958 p->actual_length = len; 1959 usb_generic_async_ctrl_complete(&dev->dev, p); 1960 } 1961 free(data); 1962 } 1963 1964 static void usbredir_bulk_packet(void *priv, uint64_t id, 1965 struct usb_redir_bulk_packet_header *bulk_packet, 1966 uint8_t *data, int data_len) 1967 { 1968 USBRedirDevice *dev = priv; 1969 uint8_t ep = bulk_packet->endpoint; 1970 int len = (bulk_packet->length_high << 16) | bulk_packet->length; 1971 USBPacket *p; 1972 1973 DPRINTF("bulk-in status %d ep %02X stream %u len %d id %"PRIu64"\n", 1974 bulk_packet->status, ep, bulk_packet->stream_id, len, id); 1975 1976 p = usbredir_find_packet_by_id(dev, ep, id); 1977 if (p) { 1978 size_t size = usb_packet_size(p); 1979 usbredir_handle_status(dev, p, bulk_packet->status); 1980 if (data_len > 0) { 1981 usbredir_log_data(dev, "bulk data in:", data, data_len); 1982 if (data_len > size) { 1983 ERROR("bulk got more data then requested (%d > %zd)\n", 1984 data_len, p->iov.size); 1985 p->status = USB_RET_BABBLE; 1986 data_len = len = size; 1987 } 1988 usb_packet_copy(p, data, data_len); 1989 } 1990 p->actual_length = len; 1991 if (p->pid == USB_TOKEN_IN && p->ep->pipeline) { 1992 usb_combined_input_packet_complete(&dev->dev, p); 1993 } else { 1994 usb_packet_complete(&dev->dev, p); 1995 } 1996 } 1997 free(data); 1998 } 1999 2000 static void usbredir_iso_packet(void *priv, uint64_t id, 2001 struct usb_redir_iso_packet_header *iso_packet, 2002 uint8_t *data, int data_len) 2003 { 2004 USBRedirDevice *dev = priv; 2005 uint8_t ep = iso_packet->endpoint; 2006 2007 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n", 2008 iso_packet->status, ep, data_len, id); 2009 2010 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) { 2011 ERROR("received iso packet for non iso endpoint %02X\n", ep); 2012 free(data); 2013 return; 2014 } 2015 2016 if (dev->endpoint[EP2I(ep)].iso_started == 0) { 2017 DPRINTF("received iso packet for non started stream ep %02X\n", ep); 2018 free(data); 2019 return; 2020 } 2021 2022 /* bufp_alloc also adds the packet to the ep queue */ 2023 bufp_alloc(dev, data, data_len, iso_packet->status, ep, data); 2024 } 2025 2026 static void usbredir_interrupt_packet(void *priv, uint64_t id, 2027 struct usb_redir_interrupt_packet_header *interrupt_packet, 2028 uint8_t *data, int data_len) 2029 { 2030 USBRedirDevice *dev = priv; 2031 uint8_t ep = interrupt_packet->endpoint; 2032 2033 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n", 2034 interrupt_packet->status, ep, data_len, id); 2035 2036 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) { 2037 ERROR("received int packet for non interrupt endpoint %02X\n", ep); 2038 free(data); 2039 return; 2040 } 2041 2042 if (ep & USB_DIR_IN) { 2043 bool q_was_empty; 2044 2045 if (dev->endpoint[EP2I(ep)].interrupt_started == 0) { 2046 DPRINTF("received int packet while not started ep %02X\n", ep); 2047 free(data); 2048 return; 2049 } 2050 2051 q_was_empty = QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq); 2052 2053 /* bufp_alloc also adds the packet to the ep queue */ 2054 bufp_alloc(dev, data, data_len, interrupt_packet->status, ep, data); 2055 2056 if (q_was_empty) { 2057 usb_wakeup(usb_ep_get(&dev->dev, USB_TOKEN_IN, ep & 0x0f), 0); 2058 } 2059 } else { 2060 /* 2061 * We report output interrupt packets as completed directly upon 2062 * submission, so all we can do here if one failed is warn. 2063 */ 2064 if (interrupt_packet->status) { 2065 WARNING("interrupt output failed status %d ep %02X id %"PRIu64"\n", 2066 interrupt_packet->status, ep, id); 2067 } 2068 } 2069 } 2070 2071 static void usbredir_buffered_bulk_packet(void *priv, uint64_t id, 2072 struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet, 2073 uint8_t *data, int data_len) 2074 { 2075 USBRedirDevice *dev = priv; 2076 uint8_t status, ep = buffered_bulk_packet->endpoint; 2077 void *free_on_destroy; 2078 int i, len; 2079 2080 DPRINTF("buffered-bulk-in status %d ep %02X len %d id %"PRIu64"\n", 2081 buffered_bulk_packet->status, ep, data_len, id); 2082 2083 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_BULK) { 2084 ERROR("received buffered-bulk packet for non bulk ep %02X\n", ep); 2085 free(data); 2086 return; 2087 } 2088 2089 if (dev->endpoint[EP2I(ep)].bulk_receiving_started == 0) { 2090 DPRINTF("received buffered-bulk packet on not started ep %02X\n", ep); 2091 free(data); 2092 return; 2093 } 2094 2095 /* Data must be in maxp chunks for buffered_bulk_add_*_data_to_packet */ 2096 len = dev->endpoint[EP2I(ep)].max_packet_size; 2097 status = usb_redir_success; 2098 free_on_destroy = NULL; 2099 for (i = 0; i < data_len; i += len) { 2100 int r; 2101 if (len >= (data_len - i)) { 2102 len = data_len - i; 2103 status = buffered_bulk_packet->status; 2104 free_on_destroy = data; 2105 } 2106 /* bufp_alloc also adds the packet to the ep queue */ 2107 r = bufp_alloc(dev, data + i, len, status, ep, free_on_destroy); 2108 if (r) { 2109 break; 2110 } 2111 } 2112 2113 if (dev->endpoint[EP2I(ep)].pending_async_packet) { 2114 USBPacket *p = dev->endpoint[EP2I(ep)].pending_async_packet; 2115 dev->endpoint[EP2I(ep)].pending_async_packet = NULL; 2116 usbredir_buffered_bulk_in_complete(dev, p, ep); 2117 usb_packet_complete(&dev->dev, p); 2118 } 2119 } 2120 2121 /* 2122 * Migration code 2123 */ 2124 2125 static void usbredir_pre_save(void *priv) 2126 { 2127 USBRedirDevice *dev = priv; 2128 2129 usbredir_fill_already_in_flight(dev); 2130 } 2131 2132 static int usbredir_post_load(void *priv, int version_id) 2133 { 2134 USBRedirDevice *dev = priv; 2135 2136 if (dev->parser == NULL) { 2137 return 0; 2138 } 2139 2140 switch (dev->device_info.speed) { 2141 case usb_redir_speed_low: 2142 dev->dev.speed = USB_SPEED_LOW; 2143 break; 2144 case usb_redir_speed_full: 2145 dev->dev.speed = USB_SPEED_FULL; 2146 break; 2147 case usb_redir_speed_high: 2148 dev->dev.speed = USB_SPEED_HIGH; 2149 break; 2150 case usb_redir_speed_super: 2151 dev->dev.speed = USB_SPEED_SUPER; 2152 break; 2153 default: 2154 dev->dev.speed = USB_SPEED_FULL; 2155 } 2156 dev->dev.speedmask = (1 << dev->dev.speed); 2157 2158 usbredir_setup_usb_eps(dev); 2159 usbredir_check_bulk_receiving(dev); 2160 2161 return 0; 2162 } 2163 2164 /* For usbredirparser migration */ 2165 static void usbredir_put_parser(QEMUFile *f, void *priv, size_t unused) 2166 { 2167 USBRedirDevice *dev = priv; 2168 uint8_t *data; 2169 int len; 2170 2171 if (dev->parser == NULL) { 2172 qemu_put_be32(f, 0); 2173 return; 2174 } 2175 2176 usbredirparser_serialize(dev->parser, &data, &len); 2177 qemu_oom_check(data); 2178 2179 qemu_put_be32(f, len); 2180 qemu_put_buffer(f, data, len); 2181 2182 free(data); 2183 } 2184 2185 static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused) 2186 { 2187 USBRedirDevice *dev = priv; 2188 uint8_t *data; 2189 int len, ret; 2190 2191 len = qemu_get_be32(f); 2192 if (len == 0) { 2193 return 0; 2194 } 2195 2196 /* 2197 * If our chardev is not open already at this point the usbredir connection 2198 * has been broken (non seamless migration, or restore from disk). 2199 * 2200 * In this case create a temporary parser to receive the migration data, 2201 * and schedule the close_bh to report the device as disconnected to the 2202 * guest and to destroy the parser again. 2203 */ 2204 if (dev->parser == NULL) { 2205 WARNING("usb-redir connection broken during migration\n"); 2206 usbredir_create_parser(dev); 2207 qemu_bh_schedule(dev->chardev_close_bh); 2208 } 2209 2210 data = g_malloc(len); 2211 qemu_get_buffer(f, data, len); 2212 2213 ret = usbredirparser_unserialize(dev->parser, data, len); 2214 2215 g_free(data); 2216 2217 return ret; 2218 } 2219 2220 static const VMStateInfo usbredir_parser_vmstate_info = { 2221 .name = "usb-redir-parser", 2222 .put = usbredir_put_parser, 2223 .get = usbredir_get_parser, 2224 }; 2225 2226 2227 /* For buffered packets (iso/irq) queue migration */ 2228 static void usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused) 2229 { 2230 struct endp_data *endp = priv; 2231 USBRedirDevice *dev = endp->dev; 2232 struct buf_packet *bufp; 2233 int len, i = 0; 2234 2235 qemu_put_be32(f, endp->bufpq_size); 2236 QTAILQ_FOREACH(bufp, &endp->bufpq, next) { 2237 len = bufp->len - bufp->offset; 2238 DPRINTF("put_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size, 2239 len, bufp->status); 2240 qemu_put_be32(f, len); 2241 qemu_put_be32(f, bufp->status); 2242 qemu_put_buffer(f, bufp->data + bufp->offset, len); 2243 i++; 2244 } 2245 assert(i == endp->bufpq_size); 2246 } 2247 2248 static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused) 2249 { 2250 struct endp_data *endp = priv; 2251 USBRedirDevice *dev = endp->dev; 2252 struct buf_packet *bufp; 2253 int i; 2254 2255 endp->bufpq_size = qemu_get_be32(f); 2256 for (i = 0; i < endp->bufpq_size; i++) { 2257 bufp = g_new(struct buf_packet, 1); 2258 bufp->len = qemu_get_be32(f); 2259 bufp->status = qemu_get_be32(f); 2260 bufp->offset = 0; 2261 bufp->data = qemu_oom_check(malloc(bufp->len)); /* regular malloc! */ 2262 bufp->free_on_destroy = bufp->data; 2263 qemu_get_buffer(f, bufp->data, bufp->len); 2264 QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next); 2265 DPRINTF("get_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size, 2266 bufp->len, bufp->status); 2267 } 2268 return 0; 2269 } 2270 2271 static const VMStateInfo usbredir_ep_bufpq_vmstate_info = { 2272 .name = "usb-redir-bufpq", 2273 .put = usbredir_put_bufpq, 2274 .get = usbredir_get_bufpq, 2275 }; 2276 2277 2278 /* For endp_data migration */ 2279 static bool usbredir_bulk_receiving_needed(void *priv) 2280 { 2281 struct endp_data *endp = priv; 2282 2283 return endp->bulk_receiving_started; 2284 } 2285 2286 static const VMStateDescription usbredir_bulk_receiving_vmstate = { 2287 .name = "usb-redir-ep/bulk-receiving", 2288 .version_id = 1, 2289 .minimum_version_id = 1, 2290 .needed = usbredir_bulk_receiving_needed, 2291 .fields = (VMStateField[]) { 2292 VMSTATE_UINT8(bulk_receiving_started, struct endp_data), 2293 VMSTATE_END_OF_LIST() 2294 } 2295 }; 2296 2297 static bool usbredir_stream_needed(void *priv) 2298 { 2299 struct endp_data *endp = priv; 2300 2301 return endp->max_streams; 2302 } 2303 2304 static const VMStateDescription usbredir_stream_vmstate = { 2305 .name = "usb-redir-ep/stream-state", 2306 .version_id = 1, 2307 .minimum_version_id = 1, 2308 .needed = usbredir_stream_needed, 2309 .fields = (VMStateField[]) { 2310 VMSTATE_UINT32(max_streams, struct endp_data), 2311 VMSTATE_END_OF_LIST() 2312 } 2313 }; 2314 2315 static const VMStateDescription usbredir_ep_vmstate = { 2316 .name = "usb-redir-ep", 2317 .version_id = 1, 2318 .minimum_version_id = 1, 2319 .fields = (VMStateField[]) { 2320 VMSTATE_UINT8(type, struct endp_data), 2321 VMSTATE_UINT8(interval, struct endp_data), 2322 VMSTATE_UINT8(interface, struct endp_data), 2323 VMSTATE_UINT16(max_packet_size, struct endp_data), 2324 VMSTATE_UINT8(iso_started, struct endp_data), 2325 VMSTATE_UINT8(iso_error, struct endp_data), 2326 VMSTATE_UINT8(interrupt_started, struct endp_data), 2327 VMSTATE_UINT8(interrupt_error, struct endp_data), 2328 VMSTATE_UINT8(bufpq_prefilled, struct endp_data), 2329 VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data), 2330 { 2331 .name = "bufpq", 2332 .version_id = 0, 2333 .field_exists = NULL, 2334 .size = 0, 2335 .info = &usbredir_ep_bufpq_vmstate_info, 2336 .flags = VMS_SINGLE, 2337 .offset = 0, 2338 }, 2339 VMSTATE_INT32(bufpq_target_size, struct endp_data), 2340 VMSTATE_END_OF_LIST() 2341 }, 2342 .subsections = (const VMStateDescription*[]) { 2343 &usbredir_bulk_receiving_vmstate, 2344 &usbredir_stream_vmstate, 2345 NULL 2346 } 2347 }; 2348 2349 2350 /* For PacketIdQueue migration */ 2351 static void usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused) 2352 { 2353 struct PacketIdQueue *q = priv; 2354 USBRedirDevice *dev = q->dev; 2355 struct PacketIdQueueEntry *e; 2356 int remain = q->size; 2357 2358 DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size); 2359 qemu_put_be32(f, q->size); 2360 QTAILQ_FOREACH(e, &q->head, next) { 2361 qemu_put_be64(f, e->id); 2362 remain--; 2363 } 2364 assert(remain == 0); 2365 } 2366 2367 static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused) 2368 { 2369 struct PacketIdQueue *q = priv; 2370 USBRedirDevice *dev = q->dev; 2371 int i, size; 2372 uint64_t id; 2373 2374 size = qemu_get_be32(f); 2375 DPRINTF("get_packet_id_q %s size %d\n", q->name, size); 2376 for (i = 0; i < size; i++) { 2377 id = qemu_get_be64(f); 2378 packet_id_queue_add(q, id); 2379 } 2380 assert(q->size == size); 2381 return 0; 2382 } 2383 2384 static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = { 2385 .name = "usb-redir-packet-id-q", 2386 .put = usbredir_put_packet_id_q, 2387 .get = usbredir_get_packet_id_q, 2388 }; 2389 2390 static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = { 2391 .name = "usb-redir-packet-id-queue", 2392 .version_id = 1, 2393 .minimum_version_id = 1, 2394 .fields = (VMStateField[]) { 2395 { 2396 .name = "queue", 2397 .version_id = 0, 2398 .field_exists = NULL, 2399 .size = 0, 2400 .info = &usbredir_ep_packet_id_q_vmstate_info, 2401 .flags = VMS_SINGLE, 2402 .offset = 0, 2403 }, 2404 VMSTATE_END_OF_LIST() 2405 } 2406 }; 2407 2408 2409 /* For usb_redir_device_connect_header migration */ 2410 static const VMStateDescription usbredir_device_info_vmstate = { 2411 .name = "usb-redir-device-info", 2412 .version_id = 1, 2413 .minimum_version_id = 1, 2414 .fields = (VMStateField[]) { 2415 VMSTATE_UINT8(speed, struct usb_redir_device_connect_header), 2416 VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header), 2417 VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header), 2418 VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header), 2419 VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header), 2420 VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header), 2421 VMSTATE_UINT16(device_version_bcd, 2422 struct usb_redir_device_connect_header), 2423 VMSTATE_END_OF_LIST() 2424 } 2425 }; 2426 2427 2428 /* For usb_redir_interface_info_header migration */ 2429 static const VMStateDescription usbredir_interface_info_vmstate = { 2430 .name = "usb-redir-interface-info", 2431 .version_id = 1, 2432 .minimum_version_id = 1, 2433 .fields = (VMStateField[]) { 2434 VMSTATE_UINT32(interface_count, 2435 struct usb_redir_interface_info_header), 2436 VMSTATE_UINT8_ARRAY(interface, 2437 struct usb_redir_interface_info_header, 32), 2438 VMSTATE_UINT8_ARRAY(interface_class, 2439 struct usb_redir_interface_info_header, 32), 2440 VMSTATE_UINT8_ARRAY(interface_subclass, 2441 struct usb_redir_interface_info_header, 32), 2442 VMSTATE_UINT8_ARRAY(interface_protocol, 2443 struct usb_redir_interface_info_header, 32), 2444 VMSTATE_END_OF_LIST() 2445 } 2446 }; 2447 2448 2449 /* And finally the USBRedirDevice vmstate itself */ 2450 static const VMStateDescription usbredir_vmstate = { 2451 .name = "usb-redir", 2452 .version_id = 1, 2453 .minimum_version_id = 1, 2454 .pre_save = usbredir_pre_save, 2455 .post_load = usbredir_post_load, 2456 .fields = (VMStateField[]) { 2457 VMSTATE_USB_DEVICE(dev, USBRedirDevice), 2458 VMSTATE_TIMER_PTR(attach_timer, USBRedirDevice), 2459 { 2460 .name = "parser", 2461 .version_id = 0, 2462 .field_exists = NULL, 2463 .size = 0, 2464 .info = &usbredir_parser_vmstate_info, 2465 .flags = VMS_SINGLE, 2466 .offset = 0, 2467 }, 2468 VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1, 2469 usbredir_ep_vmstate, struct endp_data), 2470 VMSTATE_STRUCT(cancelled, USBRedirDevice, 1, 2471 usbredir_ep_packet_id_queue_vmstate, 2472 struct PacketIdQueue), 2473 VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1, 2474 usbredir_ep_packet_id_queue_vmstate, 2475 struct PacketIdQueue), 2476 VMSTATE_STRUCT(device_info, USBRedirDevice, 1, 2477 usbredir_device_info_vmstate, 2478 struct usb_redir_device_connect_header), 2479 VMSTATE_STRUCT(interface_info, USBRedirDevice, 1, 2480 usbredir_interface_info_vmstate, 2481 struct usb_redir_interface_info_header), 2482 VMSTATE_END_OF_LIST() 2483 } 2484 }; 2485 2486 static Property usbredir_properties[] = { 2487 DEFINE_PROP_CHR("chardev", USBRedirDevice, cs), 2488 DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, usbredirparser_warning), 2489 DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str), 2490 DEFINE_PROP_BOOL("streams", USBRedirDevice, enable_streams, true), 2491 DEFINE_PROP_END_OF_LIST(), 2492 }; 2493 2494 static void usbredir_class_initfn(ObjectClass *klass, void *data) 2495 { 2496 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 2497 DeviceClass *dc = DEVICE_CLASS(klass); 2498 2499 uc->realize = usbredir_realize; 2500 uc->product_desc = "USB Redirection Device"; 2501 uc->handle_destroy = usbredir_handle_destroy; 2502 uc->cancel_packet = usbredir_cancel_packet; 2503 uc->handle_reset = usbredir_handle_reset; 2504 uc->handle_data = usbredir_handle_data; 2505 uc->handle_control = usbredir_handle_control; 2506 uc->flush_ep_queue = usbredir_flush_ep_queue; 2507 uc->ep_stopped = usbredir_ep_stopped; 2508 uc->alloc_streams = usbredir_alloc_streams; 2509 uc->free_streams = usbredir_free_streams; 2510 dc->vmsd = &usbredir_vmstate; 2511 dc->props = usbredir_properties; 2512 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 2513 } 2514 2515 static void usbredir_instance_init(Object *obj) 2516 { 2517 USBDevice *udev = USB_DEVICE(obj); 2518 USBRedirDevice *dev = USB_REDIRECT(udev); 2519 2520 device_add_bootindex_property(obj, &dev->bootindex, 2521 "bootindex", NULL, 2522 &udev->qdev, NULL); 2523 } 2524 2525 static const TypeInfo usbredir_dev_info = { 2526 .name = TYPE_USB_REDIR, 2527 .parent = TYPE_USB_DEVICE, 2528 .instance_size = sizeof(USBRedirDevice), 2529 .class_init = usbredir_class_initfn, 2530 .instance_init = usbredir_instance_init, 2531 }; 2532 2533 static void usbredir_register_types(void) 2534 { 2535 type_register_static(&usbredir_dev_info); 2536 } 2537 2538 type_init(usbredir_register_types) 2539