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