28
#include "qemu/osdep.h"
29
#include "qemu/cutils.h"
30
#include "qemu/units.h"
31
#include "qapi/error.h"
32
#include "qemu/timer.h"
33
#include "sysemu/runstate.h"
34
#include "sysemu/sysemu.h"
35
#include "qapi/qmp/qerror.h"
36
#include "qemu/error-report.h"
38
#include "qemu/module.h"
39
#include "chardev/char-fe.h"
41
#include <usbredirparser.h>
42
#include <usbredirfilter.h>
44
#include "hw/qdev-properties.h"
45
#include "hw/qdev-properties-system.h"
47
#include "migration/qemu-file-types.h"
48
#include "migration/vmstate.h"
49
#include "qom/object.h"
54
#define MAX_ENDPOINTS 32
55
#define NO_INTERFACE_INFO 255
56
#define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f))
57
#define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f))
58
#define USBEP2I(usb_ep) (((usb_ep)->pid == USB_TOKEN_IN) ? \
59
((usb_ep)->nr | 0x10) : ((usb_ep)->nr))
60
#define I2USBEP(d, i) (usb_ep_get(&(d)->dev, \
61
((i) & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, \
64
#ifndef USBREDIR_VERSION
65
#define USBREDIR_VERSION 0
68
typedef struct USBRedirDevice USBRedirDevice;
73
void *free_on_destroy;
77
QTAILQ_ENTRY(buf_packet)next;
85
uint16_t max_packet_size;
89
uint8_t interrupt_started;
90
uint8_t interrupt_error;
91
uint8_t bulk_receiving_enabled;
92
uint8_t bulk_receiving_started;
93
uint8_t bufpq_prefilled;
94
uint8_t bufpq_dropping_packets;
95
QTAILQ_HEAD(, buf_packet) bufpq;
97
int32_t bufpq_target_size;
98
USBPacket *pending_async_packet;
101
struct PacketIdQueueEntry {
103
QTAILQ_ENTRY(PacketIdQueueEntry)next;
106
struct PacketIdQueue {
109
QTAILQ_HEAD(, PacketIdQueueEntry) head;
113
struct USBRedirDevice {
118
bool suppress_remote_wake;
124
const uint8_t *read_buf;
129
QEMUBH *chardev_close_bh;
130
QEMUBH *device_reject_bh;
132
QEMUTimer *attach_timer;
133
int64_t next_attach_time;
134
struct usbredirparser *parser;
135
struct endp_data endpoint[MAX_ENDPOINTS];
136
struct PacketIdQueue cancelled;
137
struct PacketIdQueue already_in_flight;
138
void (*buffered_bulk_in_complete)(USBRedirDevice *, USBPacket *, uint8_t);
140
struct usb_redir_device_connect_header device_info;
141
struct usb_redir_interface_info_header interface_info;
142
struct usbredirfilter_rule *filter_rules;
143
int filter_rules_count;
144
int compatible_speedmask;
145
VMChangeStateEntry *vmstate;
148
#define TYPE_USB_REDIR "usb-redir"
149
DECLARE_INSTANCE_CHECKER(USBRedirDevice, USB_REDIRECT,
152
static void usbredir_hello(void *priv, struct usb_redir_hello_header *h);
153
static void usbredir_device_connect(void *priv,
154
struct usb_redir_device_connect_header *device_connect);
155
static void usbredir_device_disconnect(void *priv);
156
static void usbredir_interface_info(void *priv,
157
struct usb_redir_interface_info_header *interface_info);
158
static void usbredir_ep_info(void *priv,
159
struct usb_redir_ep_info_header *ep_info);
160
static void usbredir_configuration_status(void *priv, uint64_t id,
161
struct usb_redir_configuration_status_header *configuration_status);
162
static void usbredir_alt_setting_status(void *priv, uint64_t id,
163
struct usb_redir_alt_setting_status_header *alt_setting_status);
164
static void usbredir_iso_stream_status(void *priv, uint64_t id,
165
struct usb_redir_iso_stream_status_header *iso_stream_status);
166
static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
167
struct usb_redir_interrupt_receiving_status_header
168
*interrupt_receiving_status);
169
static void usbredir_bulk_streams_status(void *priv, uint64_t id,
170
struct usb_redir_bulk_streams_status_header *bulk_streams_status);
171
static void usbredir_bulk_receiving_status(void *priv, uint64_t id,
172
struct usb_redir_bulk_receiving_status_header *bulk_receiving_status);
173
static void usbredir_control_packet(void *priv, uint64_t id,
174
struct usb_redir_control_packet_header *control_packet,
175
uint8_t *data, int data_len);
176
static void usbredir_bulk_packet(void *priv, uint64_t id,
177
struct usb_redir_bulk_packet_header *bulk_packet,
178
uint8_t *data, int data_len);
179
static void usbredir_iso_packet(void *priv, uint64_t id,
180
struct usb_redir_iso_packet_header *iso_packet,
181
uint8_t *data, int data_len);
182
static void usbredir_interrupt_packet(void *priv, uint64_t id,
183
struct usb_redir_interrupt_packet_header *interrupt_header,
184
uint8_t *data, int data_len);
185
static void usbredir_buffered_bulk_packet(void *priv, uint64_t id,
186
struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet,
187
uint8_t *data, int data_len);
189
static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p,
192
#define VERSION "qemu usb-redir guest " QEMU_VERSION
200
if (dev->debug >= usbredirparser_error) { \
201
error_report("usb-redir error: " __VA_ARGS__); \
204
#define WARNING(...) \
206
if (dev->debug >= usbredirparser_warning) { \
207
warn_report("" __VA_ARGS__); \
212
if (dev->debug >= usbredirparser_info) { \
213
error_report("usb-redir: " __VA_ARGS__); \
216
#define DPRINTF(...) \
218
if (dev->debug >= usbredirparser_debug) { \
219
error_report("usb-redir: " __VA_ARGS__); \
222
#define DPRINTF2(...) \
224
if (dev->debug >= usbredirparser_debug_data) { \
225
error_report("usb-redir: " __VA_ARGS__); \
229
static void usbredir_log(void *priv, int level, const char *msg)
231
USBRedirDevice *dev = priv;
233
if (dev->debug < level) {
237
error_report("%s", msg);
240
static void usbredir_log_data(USBRedirDevice *dev, const char *desc,
241
const uint8_t *data, int len)
243
if (dev->debug < usbredirparser_debug_data) {
246
qemu_hexdump(stderr, desc, data, len);
253
static int usbredir_read(void *priv, uint8_t *data, int count)
255
USBRedirDevice *dev = priv;
257
if (dev->read_buf_size < count) {
258
count = dev->read_buf_size;
261
memcpy(data, dev->read_buf, count);
263
dev->read_buf_size -= count;
264
if (dev->read_buf_size) {
265
dev->read_buf += count;
267
dev->read_buf = NULL;
273
static gboolean usbredir_write_unblocked(void *do_not_use, GIOCondition cond,
276
USBRedirDevice *dev = opaque;
279
usbredirparser_do_write(dev->parser);
281
return G_SOURCE_REMOVE;
284
static int usbredir_write(void *priv, uint8_t *data, int count)
286
USBRedirDevice *dev = priv;
289
if (!qemu_chr_fe_backend_open(&dev->cs)) {
294
if (!runstate_check(RUN_STATE_RUNNING)) {
300
DPRINTF("usbredir_write recursion\n");
303
dev->in_write = true;
305
r = qemu_chr_fe_write(&dev->cs, data, count);
308
dev->watch = qemu_chr_fe_add_watch(&dev->cs, G_IO_OUT | G_IO_HUP,
309
usbredir_write_unblocked, dev);
315
dev->in_write = false;
323
static void packet_id_queue_init(struct PacketIdQueue *q,
324
USBRedirDevice *dev, const char *name)
328
QTAILQ_INIT(&q->head);
332
static void packet_id_queue_add(struct PacketIdQueue *q, uint64_t id)
334
USBRedirDevice *dev = q->dev;
335
struct PacketIdQueueEntry *e;
337
DPRINTF("adding packet id %"PRIu64" to %s queue\n", id, q->name);
339
e = g_new0(struct PacketIdQueueEntry, 1);
341
QTAILQ_INSERT_TAIL(&q->head, e, next);
345
static int packet_id_queue_remove(struct PacketIdQueue *q, uint64_t id)
347
USBRedirDevice *dev = q->dev;
348
struct PacketIdQueueEntry *e;
350
QTAILQ_FOREACH(e, &q->head, next) {
352
DPRINTF("removing packet id %"PRIu64" from %s queue\n",
354
QTAILQ_REMOVE(&q->head, e, next);
363
static void packet_id_queue_empty(struct PacketIdQueue *q)
365
USBRedirDevice *dev = q->dev;
366
struct PacketIdQueueEntry *e, *next_e;
368
DPRINTF("removing %d packet-ids from %s queue\n", q->size, q->name);
370
QTAILQ_FOREACH_SAFE(e, &q->head, next, next_e) {
371
QTAILQ_REMOVE(&q->head, e, next);
377
static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p)
379
USBRedirDevice *dev = USB_REDIRECT(udev);
380
int i = USBEP2I(p->ep);
383
usb_combined_packet_cancel(udev, p);
387
if (dev->endpoint[i].pending_async_packet) {
388
assert(dev->endpoint[i].pending_async_packet == p);
389
dev->endpoint[i].pending_async_packet = NULL;
393
packet_id_queue_add(&dev->cancelled, p->id);
394
usbredirparser_send_cancel_data_packet(dev->parser, p->id);
395
usbredirparser_do_write(dev->parser);
398
static int usbredir_is_cancelled(USBRedirDevice *dev, uint64_t id)
400
if (!dev->dev.attached) {
403
return packet_id_queue_remove(&dev->cancelled, id);
406
static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice *dev,
407
struct USBEndpoint *ep)
412
if (dev->endpoint[USBEP2I(ep)].bulk_receiving_started) {
416
QTAILQ_FOREACH(p, &ep->queue, queue) {
418
if (p->combined && p != p->combined->first) {
421
if (p->state == USB_PACKET_ASYNC) {
422
packet_id_queue_add(&dev->already_in_flight, p->id);
427
static void usbredir_fill_already_in_flight(USBRedirDevice *dev)
430
struct USBDevice *udev = &dev->dev;
432
usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_ctl);
434
for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
435
usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_in[ep]);
436
usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_out[ep]);
440
static int usbredir_already_in_flight(USBRedirDevice *dev, uint64_t id)
442
return packet_id_queue_remove(&dev->already_in_flight, id);
445
static USBPacket *usbredir_find_packet_by_id(USBRedirDevice *dev,
446
uint8_t ep, uint64_t id)
450
if (usbredir_is_cancelled(dev, id)) {
454
p = usb_ep_find_packet_by_id(&dev->dev,
455
(ep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT,
458
ERROR("could not find packet with id %"PRIu64"\n", id);
463
static int bufp_alloc(USBRedirDevice *dev, uint8_t *data, uint16_t len,
464
uint8_t status, uint8_t ep, void *free_on_destroy)
466
struct buf_packet *bufp;
468
if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets &&
469
dev->endpoint[EP2I(ep)].bufpq_size >
470
2 * dev->endpoint[EP2I(ep)].bufpq_target_size) {
471
DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep);
472
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1;
476
if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) {
477
if (dev->endpoint[EP2I(ep)].bufpq_size >
478
dev->endpoint[EP2I(ep)].bufpq_target_size) {
479
free(free_on_destroy);
482
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
485
bufp = g_new(struct buf_packet, 1);
489
bufp->status = status;
490
bufp->free_on_destroy = free_on_destroy;
491
QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
492
dev->endpoint[EP2I(ep)].bufpq_size++;
496
static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp,
499
QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
500
dev->endpoint[EP2I(ep)].bufpq_size--;
501
free(bufp->free_on_destroy);
505
static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep)
507
struct buf_packet *buf, *buf_next;
509
QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) {
510
bufp_free(dev, buf, ep);
518
static void usbredir_handle_reset(USBDevice *udev)
520
USBRedirDevice *dev = USB_REDIRECT(udev);
522
DPRINTF("reset device\n");
523
usbredirparser_send_reset(dev->parser);
524
usbredirparser_do_write(dev->parser);
527
static void usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
531
if (!dev->endpoint[EP2I(ep)].iso_started &&
532
!dev->endpoint[EP2I(ep)].iso_error) {
533
struct usb_redir_start_iso_stream_header start_iso = {
538
if (dev->dev.speed == USB_SPEED_HIGH) {
539
pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval;
541
pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval;
544
dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000;
548
start_iso.pkts_per_urb = pkts_per_sec / 100;
549
if (start_iso.pkts_per_urb < 1) {
550
start_iso.pkts_per_urb = 1;
551
} else if (start_iso.pkts_per_urb > 32) {
552
start_iso.pkts_per_urb = 32;
555
start_iso.no_urbs = DIV_ROUND_UP(
556
dev->endpoint[EP2I(ep)].bufpq_target_size,
557
start_iso.pkts_per_urb);
560
if (!(ep & USB_DIR_IN)) {
561
start_iso.no_urbs *= 2;
563
if (start_iso.no_urbs > 16) {
564
start_iso.no_urbs = 16;
568
usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso);
569
usbredirparser_do_write(dev->parser);
570
DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
571
pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep);
572
dev->endpoint[EP2I(ep)].iso_started = 1;
573
dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
574
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
577
if (ep & USB_DIR_IN) {
578
struct buf_packet *isop;
580
if (dev->endpoint[EP2I(ep)].iso_started &&
581
!dev->endpoint[EP2I(ep)].bufpq_prefilled) {
582
if (dev->endpoint[EP2I(ep)].bufpq_size <
583
dev->endpoint[EP2I(ep)].bufpq_target_size) {
586
dev->endpoint[EP2I(ep)].bufpq_prefilled = 1;
589
isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq);
591
DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
592
ep, dev->endpoint[EP2I(ep)].iso_error);
594
dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
596
status = dev->endpoint[EP2I(ep)].iso_error;
597
dev->endpoint[EP2I(ep)].iso_error = 0;
598
p->status = status ? USB_RET_IOERROR : USB_RET_SUCCESS;
601
DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep,
602
isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size);
604
status = isop->status;
606
if (len > p->iov.size) {
607
ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
608
ep, len, (int)p->iov.size);
610
status = usb_redir_babble;
612
usb_packet_copy(p, isop->data, len);
613
bufp_free(dev, isop, ep);
614
usbredir_handle_status(dev, p, status);
618
if (dev->endpoint[EP2I(ep)].iso_started) {
619
struct usb_redir_iso_packet_header iso_packet = {
621
.length = p->iov.size
623
g_autofree uint8_t *buf = g_malloc(p->iov.size);
625
usb_packet_copy(p, buf, p->iov.size);
626
usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet,
628
usbredirparser_do_write(dev->parser);
630
status = dev->endpoint[EP2I(ep)].iso_error;
631
dev->endpoint[EP2I(ep)].iso_error = 0;
632
DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status,
634
usbredir_handle_status(dev, p, status);
638
static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep)
640
struct usb_redir_stop_iso_stream_header stop_iso_stream = {
643
if (dev->endpoint[EP2I(ep)].iso_started) {
644
usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream);
645
DPRINTF("iso stream stopped ep %02X\n", ep);
646
dev->endpoint[EP2I(ep)].iso_started = 0;
648
dev->endpoint[EP2I(ep)].iso_error = 0;
649
usbredir_free_bufpq(dev, ep);
657
static void usbredir_buffered_bulk_add_data_to_packet(USBRedirDevice *dev,
658
struct buf_packet *bulkp, int count, USBPacket *p, uint8_t ep)
660
usb_packet_copy(p, bulkp->data + bulkp->offset, count);
661
bulkp->offset += count;
662
if (bulkp->offset == bulkp->len) {
664
usbredir_handle_status(dev, p, bulkp->status);
665
bufp_free(dev, bulkp, ep);
669
static void usbredir_buffered_bulk_in_complete_raw(USBRedirDevice *dev,
670
USBPacket *p, uint8_t ep)
672
struct buf_packet *bulkp;
675
while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) &&
676
p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) {
677
count = bulkp->len - bulkp->offset;
678
if (count > (p->iov.size - p->actual_length)) {
679
count = p->iov.size - p->actual_length;
681
usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep);
685
static void usbredir_buffered_bulk_in_complete_ftdi(USBRedirDevice *dev,
686
USBPacket *p, uint8_t ep)
688
const int maxp = dev->endpoint[EP2I(ep)].max_packet_size;
689
uint8_t header[2] = { 0, 0 };
690
struct buf_packet *bulkp;
693
while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) &&
694
p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) {
695
if (bulkp->len < 2) {
696
WARNING("malformed ftdi bulk in packet\n");
697
bufp_free(dev, bulkp, ep);
701
if ((p->actual_length % maxp) == 0) {
702
usb_packet_copy(p, bulkp->data, 2);
703
memcpy(header, bulkp->data, 2);
705
if (bulkp->data[0] != header[0] || bulkp->data[1] != header[1]) {
710
if (bulkp->offset == 0) {
713
count = bulkp->len - bulkp->offset;
715
if (count > (maxp - (p->actual_length % maxp))) {
716
count = maxp - (p->actual_length % maxp);
718
usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep);
722
static void usbredir_buffered_bulk_in_complete(USBRedirDevice *dev,
723
USBPacket *p, uint8_t ep)
725
p->status = USB_RET_SUCCESS;
726
dev->buffered_bulk_in_complete(dev, p, ep);
727
DPRINTF("bulk-token-in ep %02X status %d len %d id %"PRIu64"\n",
728
ep, p->status, p->actual_length, p->id);
731
static void usbredir_handle_buffered_bulk_in_data(USBRedirDevice *dev,
732
USBPacket *p, uint8_t ep)
735
if (!dev->endpoint[EP2I(ep)].bulk_receiving_started) {
737
struct usb_redir_start_bulk_receiving_header start = {
743
bpt = 512 + dev->endpoint[EP2I(ep)].max_packet_size - 1;
744
bpt /= dev->endpoint[EP2I(ep)].max_packet_size;
745
bpt *= dev->endpoint[EP2I(ep)].max_packet_size;
746
start.bytes_per_transfer = bpt;
748
usbredirparser_send_start_bulk_receiving(dev->parser, 0, &start);
749
usbredirparser_do_write(dev->parser);
750
DPRINTF("bulk receiving started bytes/transfer %u count %d ep %02X\n",
751
start.bytes_per_transfer, start.no_transfers, ep);
752
dev->endpoint[EP2I(ep)].bulk_receiving_started = 1;
755
dev->endpoint[EP2I(ep)].bufpq_target_size = 5000;
756
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
759
if (QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq)) {
760
DPRINTF("bulk-token-in ep %02X, no bulkp\n", ep);
761
assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL);
762
dev->endpoint[EP2I(ep)].pending_async_packet = p;
763
p->status = USB_RET_ASYNC;
766
usbredir_buffered_bulk_in_complete(dev, p, ep);
769
static void usbredir_stop_bulk_receiving(USBRedirDevice *dev, uint8_t ep)
771
struct usb_redir_stop_bulk_receiving_header stop_bulk = {
775
if (dev->endpoint[EP2I(ep)].bulk_receiving_started) {
776
usbredirparser_send_stop_bulk_receiving(dev->parser, 0, &stop_bulk);
777
DPRINTF("bulk receiving stopped ep %02X\n", ep);
778
dev->endpoint[EP2I(ep)].bulk_receiving_started = 0;
780
usbredir_free_bufpq(dev, ep);
783
static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
786
struct usb_redir_bulk_packet_header bulk_packet;
787
size_t size = usb_packet_size(p);
788
const int maxp = dev->endpoint[EP2I(ep)].max_packet_size;
790
if (usbredir_already_in_flight(dev, p->id)) {
791
p->status = USB_RET_ASYNC;
795
if (dev->endpoint[EP2I(ep)].bulk_receiving_enabled) {
796
if (size != 0 && (size % maxp) == 0) {
797
usbredir_handle_buffered_bulk_in_data(dev, p, ep);
800
WARNING("bulk recv invalid size %zd ep %02x, disabling\n", size, ep);
801
assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL);
802
usbredir_stop_bulk_receiving(dev, ep);
803
dev->endpoint[EP2I(ep)].bulk_receiving_enabled = 0;
806
DPRINTF("bulk-out ep %02X stream %u len %zd id %"PRIu64"\n",
807
ep, p->stream, size, p->id);
809
bulk_packet.endpoint = ep;
810
bulk_packet.length = size;
811
bulk_packet.stream_id = p->stream;
812
bulk_packet.length_high = size >> 16;
813
assert(bulk_packet.length_high == 0 ||
814
usbredirparser_peer_has_cap(dev->parser,
815
usb_redir_cap_32bits_bulk_length));
817
if (ep & USB_DIR_IN || size == 0) {
818
usbredirparser_send_bulk_packet(dev->parser, p->id,
819
&bulk_packet, NULL, 0);
821
g_autofree uint8_t *buf = g_malloc(size);
822
usb_packet_copy(p, buf, size);
823
usbredir_log_data(dev, "bulk data out:", buf, size);
824
usbredirparser_send_bulk_packet(dev->parser, p->id,
825
&bulk_packet, buf, size);
827
usbredirparser_do_write(dev->parser);
828
p->status = USB_RET_ASYNC;
831
static void usbredir_handle_interrupt_in_data(USBRedirDevice *dev,
832
USBPacket *p, uint8_t ep)
835
struct buf_packet *intp, *intp_to_free;
836
int status, len, sum;
838
if (!dev->endpoint[EP2I(ep)].interrupt_started &&
839
!dev->endpoint[EP2I(ep)].interrupt_error) {
840
struct usb_redir_start_interrupt_receiving_header start_int = {
844
usbredirparser_send_start_interrupt_receiving(dev->parser, 0,
846
usbredirparser_do_write(dev->parser);
847
DPRINTF("interrupt recv started ep %02X\n", ep);
848
dev->endpoint[EP2I(ep)].interrupt_started = 1;
851
dev->endpoint[EP2I(ep)].bufpq_target_size = 1000;
852
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
857
QTAILQ_FOREACH(intp, &dev->endpoint[EP2I(ep)].bufpq, next) {
859
if (intp->len < dev->endpoint[EP2I(ep)].max_packet_size ||
865
DPRINTF2("interrupt-token-in ep %02X, no intp, buffered %d\n", ep, sum);
867
status = dev->endpoint[EP2I(ep)].interrupt_error;
868
dev->endpoint[EP2I(ep)].interrupt_error = 0;
870
usbredir_handle_status(dev, p, status);
872
p->status = USB_RET_NAK;
879
status = usb_redir_success;
881
QTAILQ_FOREACH(intp, &dev->endpoint[EP2I(ep)].bufpq, next) {
883
bufp_free(dev, intp_to_free, ep);
885
DPRINTF("interrupt-token-in ep %02X fragment status %d len %d\n", ep,
886
intp->status, intp->len);
890
if (status == usb_redir_success) {
891
status = intp->status;
893
if (sum > p->iov.size) {
894
ERROR("received int data is larger then packet ep %02X\n", ep);
895
len -= (sum - p->iov.size);
897
status = usb_redir_babble;
900
usb_packet_copy(p, intp->data, len);
903
if (intp->len < dev->endpoint[EP2I(ep)].max_packet_size ||
908
bufp_free(dev, intp_to_free, ep);
910
DPRINTF("interrupt-token-in ep %02X summary status %d len %d\n", ep,
912
usbredir_handle_status(dev, p, status);
922
static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev,
923
USBPacket *p, uint8_t ep)
925
struct usb_redir_interrupt_packet_header interrupt_packet;
926
g_autofree uint8_t *buf = g_malloc(p->iov.size);
928
DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep,
931
interrupt_packet.endpoint = ep;
932
interrupt_packet.length = p->iov.size;
934
usb_packet_copy(p, buf, p->iov.size);
935
usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size);
936
usbredirparser_send_interrupt_packet(dev->parser, p->id,
937
&interrupt_packet, buf, p->iov.size);
938
usbredirparser_do_write(dev->parser);
941
static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev,
944
struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = {
947
if (dev->endpoint[EP2I(ep)].interrupt_started) {
948
usbredirparser_send_stop_interrupt_receiving(dev->parser, 0,
949
&stop_interrupt_recv);
950
DPRINTF("interrupt recv stopped ep %02X\n", ep);
951
dev->endpoint[EP2I(ep)].interrupt_started = 0;
953
dev->endpoint[EP2I(ep)].interrupt_error = 0;
954
usbredir_free_bufpq(dev, ep);
957
static void usbredir_handle_data(USBDevice *udev, USBPacket *p)
959
USBRedirDevice *dev = USB_REDIRECT(udev);
963
if (p->pid == USB_TOKEN_IN) {
967
switch (dev->endpoint[EP2I(ep)].type) {
968
case USB_ENDPOINT_XFER_CONTROL:
969
ERROR("handle_data called for control transfer on ep %02X\n", ep);
970
p->status = USB_RET_NAK;
972
case USB_ENDPOINT_XFER_BULK:
973
if (p->state == USB_PACKET_SETUP && p->pid == USB_TOKEN_IN &&
975
p->status = USB_RET_ADD_TO_QUEUE;
978
usbredir_handle_bulk_data(dev, p, ep);
980
case USB_ENDPOINT_XFER_ISOC:
981
usbredir_handle_iso_data(dev, p, ep);
983
case USB_ENDPOINT_XFER_INT:
984
if (ep & USB_DIR_IN) {
985
usbredir_handle_interrupt_in_data(dev, p, ep);
987
usbredir_handle_interrupt_out_data(dev, p, ep);
991
ERROR("handle_data ep %02X has unknown type %d\n", ep,
992
dev->endpoint[EP2I(ep)].type);
993
p->status = USB_RET_NAK;
997
static void usbredir_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
999
if (ep->pid == USB_TOKEN_IN && ep->pipeline) {
1000
usb_ep_combine_input_packets(ep);
1004
static void usbredir_stop_ep(USBRedirDevice *dev, int i)
1006
uint8_t ep = I2EP(i);
1008
switch (dev->endpoint[i].type) {
1009
case USB_ENDPOINT_XFER_BULK:
1010
if (ep & USB_DIR_IN) {
1011
usbredir_stop_bulk_receiving(dev, ep);
1014
case USB_ENDPOINT_XFER_ISOC:
1015
usbredir_stop_iso_stream(dev, ep);
1017
case USB_ENDPOINT_XFER_INT:
1018
if (ep & USB_DIR_IN) {
1019
usbredir_stop_interrupt_receiving(dev, ep);
1023
usbredir_free_bufpq(dev, ep);
1026
static void usbredir_ep_stopped(USBDevice *udev, USBEndpoint *uep)
1028
USBRedirDevice *dev = USB_REDIRECT(udev);
1030
usbredir_stop_ep(dev, USBEP2I(uep));
1031
usbredirparser_do_write(dev->parser);
1034
static void usbredir_set_config(USBRedirDevice *dev, USBPacket *p,
1037
struct usb_redir_set_configuration_header set_config;
1040
DPRINTF("set config %d id %"PRIu64"\n", config, p->id);
1042
for (i = 0; i < MAX_ENDPOINTS; i++) {
1043
usbredir_stop_ep(dev, i);
1046
set_config.configuration = config;
1047
usbredirparser_send_set_configuration(dev->parser, p->id, &set_config);
1048
usbredirparser_do_write(dev->parser);
1049
p->status = USB_RET_ASYNC;
1052
static void usbredir_get_config(USBRedirDevice *dev, USBPacket *p)
1054
DPRINTF("get config id %"PRIu64"\n", p->id);
1056
usbredirparser_send_get_configuration(dev->parser, p->id);
1057
usbredirparser_do_write(dev->parser);
1058
p->status = USB_RET_ASYNC;
1061
static void usbredir_set_interface(USBRedirDevice *dev, USBPacket *p,
1062
int interface, int alt)
1064
struct usb_redir_set_alt_setting_header set_alt;
1067
DPRINTF("set interface %d alt %d id %"PRIu64"\n", interface, alt, p->id);
1069
for (i = 0; i < MAX_ENDPOINTS; i++) {
1070
if (dev->endpoint[i].interface == interface) {
1071
usbredir_stop_ep(dev, i);
1075
set_alt.interface = interface;
1077
usbredirparser_send_set_alt_setting(dev->parser, p->id, &set_alt);
1078
usbredirparser_do_write(dev->parser);
1079
p->status = USB_RET_ASYNC;
1082
static void usbredir_get_interface(USBRedirDevice *dev, USBPacket *p,
1085
struct usb_redir_get_alt_setting_header get_alt;
1087
DPRINTF("get interface %d id %"PRIu64"\n", interface, p->id);
1089
get_alt.interface = interface;
1090
usbredirparser_send_get_alt_setting(dev->parser, p->id, &get_alt);
1091
usbredirparser_do_write(dev->parser);
1092
p->status = USB_RET_ASYNC;
1095
static void usbredir_handle_control(USBDevice *udev, USBPacket *p,
1096
int request, int value, int index, int length, uint8_t *data)
1098
USBRedirDevice *dev = USB_REDIRECT(udev);
1099
struct usb_redir_control_packet_header control_packet;
1101
if (usbredir_already_in_flight(dev, p->id)) {
1102
p->status = USB_RET_ASYNC;
1108
case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1109
DPRINTF("set address %d\n", value);
1110
dev->dev.addr = value;
1112
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1113
usbredir_set_config(dev, p, value & 0xff);
1115
case DeviceRequest | USB_REQ_GET_CONFIGURATION:
1116
usbredir_get_config(dev, p);
1118
case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1119
usbredir_set_interface(dev, p, index, value);
1121
case InterfaceRequest | USB_REQ_GET_INTERFACE:
1122
usbredir_get_interface(dev, p, index);
1128
"ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64"\n",
1129
request >> 8, request & 0xff, value, index, length, p->id);
1131
control_packet.request = request & 0xFF;
1132
control_packet.requesttype = request >> 8;
1133
control_packet.endpoint = control_packet.requesttype & USB_DIR_IN;
1134
control_packet.value = value;
1135
control_packet.index = index;
1136
control_packet.length = length;
1138
if (control_packet.requesttype & USB_DIR_IN) {
1139
usbredirparser_send_control_packet(dev->parser, p->id,
1140
&control_packet, NULL, 0);
1142
usbredir_log_data(dev, "ctrl data out:", data, length);
1143
usbredirparser_send_control_packet(dev->parser, p->id,
1144
&control_packet, data, length);
1146
usbredirparser_do_write(dev->parser);
1147
p->status = USB_RET_ASYNC;
1150
static int usbredir_alloc_streams(USBDevice *udev, USBEndpoint **eps,
1151
int nr_eps, int streams)
1153
USBRedirDevice *dev = USB_REDIRECT(udev);
1154
#if USBREDIR_VERSION >= 0x000700
1155
struct usb_redir_alloc_bulk_streams_header alloc_streams;
1158
if (!usbredirparser_peer_has_cap(dev->parser,
1159
usb_redir_cap_bulk_streams)) {
1160
ERROR("peer does not support streams\n");
1165
ERROR("request to allocate 0 streams\n");
1169
alloc_streams.no_streams = streams;
1170
alloc_streams.endpoints = 0;
1171
for (i = 0; i < nr_eps; i++) {
1172
alloc_streams.endpoints |= 1 << USBEP2I(eps[i]);
1174
usbredirparser_send_alloc_bulk_streams(dev->parser, 0, &alloc_streams);
1175
usbredirparser_do_write(dev->parser);
1179
ERROR("usbredir_alloc_streams not implemented\n");
1183
ERROR("streams are not available, disconnecting\n");
1184
qemu_bh_schedule(dev->device_reject_bh);
1188
static void usbredir_free_streams(USBDevice *udev, USBEndpoint **eps,
1191
#if USBREDIR_VERSION >= 0x000700
1192
USBRedirDevice *dev = USB_REDIRECT(udev);
1193
struct usb_redir_free_bulk_streams_header free_streams;
1196
if (!usbredirparser_peer_has_cap(dev->parser,
1197
usb_redir_cap_bulk_streams)) {
1201
free_streams.endpoints = 0;
1202
for (i = 0; i < nr_eps; i++) {
1203
free_streams.endpoints |= 1 << USBEP2I(eps[i]);
1205
usbredirparser_send_free_bulk_streams(dev->parser, 0, &free_streams);
1206
usbredirparser_do_write(dev->parser);
1217
static void usbredir_chardev_close_bh(void *opaque)
1219
USBRedirDevice *dev = opaque;
1221
qemu_bh_cancel(dev->device_reject_bh);
1222
usbredir_device_disconnect(dev);
1225
DPRINTF("destroying usbredirparser\n");
1226
usbredirparser_destroy(dev->parser);
1230
g_source_remove(dev->watch);
1235
static void usbredir_create_parser(USBRedirDevice *dev)
1237
uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, };
1240
DPRINTF("creating usbredirparser\n");
1242
dev->parser = usbredirparser_create();
1244
error_report("usbredirparser_create() failed");
1247
dev->parser->priv = dev;
1248
dev->parser->log_func = usbredir_log;
1249
dev->parser->read_func = usbredir_read;
1250
dev->parser->write_func = usbredir_write;
1251
dev->parser->hello_func = usbredir_hello;
1252
dev->parser->device_connect_func = usbredir_device_connect;
1253
dev->parser->device_disconnect_func = usbredir_device_disconnect;
1254
dev->parser->interface_info_func = usbredir_interface_info;
1255
dev->parser->ep_info_func = usbredir_ep_info;
1256
dev->parser->configuration_status_func = usbredir_configuration_status;
1257
dev->parser->alt_setting_status_func = usbredir_alt_setting_status;
1258
dev->parser->iso_stream_status_func = usbredir_iso_stream_status;
1259
dev->parser->interrupt_receiving_status_func =
1260
usbredir_interrupt_receiving_status;
1261
dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status;
1262
dev->parser->bulk_receiving_status_func = usbredir_bulk_receiving_status;
1263
dev->parser->control_packet_func = usbredir_control_packet;
1264
dev->parser->bulk_packet_func = usbredir_bulk_packet;
1265
dev->parser->iso_packet_func = usbredir_iso_packet;
1266
dev->parser->interrupt_packet_func = usbredir_interrupt_packet;
1267
dev->parser->buffered_bulk_packet_func = usbredir_buffered_bulk_packet;
1268
dev->read_buf = NULL;
1269
dev->read_buf_size = 0;
1271
usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version);
1272
usbredirparser_caps_set_cap(caps, usb_redir_cap_filter);
1273
usbredirparser_caps_set_cap(caps, usb_redir_cap_ep_info_max_packet_size);
1274
usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids);
1275
usbredirparser_caps_set_cap(caps, usb_redir_cap_32bits_bulk_length);
1276
usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_receiving);
1277
#if USBREDIR_VERSION >= 0x000700
1278
if (dev->enable_streams) {
1279
usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_streams);
1283
if (runstate_check(RUN_STATE_INMIGRATE)) {
1284
flags |= usbredirparser_fl_no_hello;
1286
usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE,
1288
usbredirparser_do_write(dev->parser);
1291
static void usbredir_reject_device(USBRedirDevice *dev)
1293
usbredir_device_disconnect(dev);
1294
if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) {
1295
usbredirparser_send_filter_reject(dev->parser);
1296
usbredirparser_do_write(dev->parser);
1304
static void usbredir_device_reject_bh(void *opaque)
1306
USBRedirDevice *dev = opaque;
1308
usbredir_reject_device(dev);
1311
static void usbredir_do_attach(void *opaque)
1313
USBRedirDevice *dev = opaque;
1314
Error *local_err = NULL;
1317
if ((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER) && !(
1318
usbredirparser_peer_has_cap(dev->parser,
1319
usb_redir_cap_ep_info_max_packet_size) &&
1320
usbredirparser_peer_has_cap(dev->parser,
1321
usb_redir_cap_32bits_bulk_length) &&
1322
usbredirparser_peer_has_cap(dev->parser,
1323
usb_redir_cap_64bits_ids))) {
1324
ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
1325
usbredir_reject_device(dev);
1329
usb_device_attach(&dev->dev, &local_err);
1331
error_report_err(local_err);
1332
WARNING("rejecting device due to speed mismatch\n");
1333
usbredir_reject_device(dev);
1341
static int usbredir_chardev_can_read(void *opaque)
1343
USBRedirDevice *dev = opaque;
1346
WARNING("chardev_can_read called on non open chardev!\n");
1351
if (!runstate_check(RUN_STATE_RUNNING)) {
1359
static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size)
1361
USBRedirDevice *dev = opaque;
1364
assert(dev->read_buf == NULL);
1366
dev->read_buf = buf;
1367
dev->read_buf_size = size;
1369
usbredirparser_do_read(dev->parser);
1371
usbredirparser_do_write(dev->parser);
1374
static void usbredir_chardev_event(void *opaque, QEMUChrEvent event)
1376
USBRedirDevice *dev = opaque;
1379
case CHR_EVENT_OPENED:
1380
DPRINTF("chardev open\n");
1382
usbredir_chardev_close_bh(dev);
1383
qemu_bh_cancel(dev->chardev_close_bh);
1384
usbredir_create_parser(dev);
1386
case CHR_EVENT_CLOSED:
1387
DPRINTF("chardev close\n");
1388
qemu_bh_schedule(dev->chardev_close_bh);
1390
case CHR_EVENT_BREAK:
1391
case CHR_EVENT_MUX_IN:
1392
case CHR_EVENT_MUX_OUT:
1402
static void usbredir_vm_state_change(void *priv, bool running, RunState state)
1404
USBRedirDevice *dev = priv;
1406
if (running && dev->parser != NULL) {
1407
usbredirparser_do_write(dev->parser);
1411
static void usbredir_init_endpoints(USBRedirDevice *dev)
1415
usb_ep_init(&dev->dev);
1416
memset(dev->endpoint, 0, sizeof(dev->endpoint));
1417
for (i = 0; i < MAX_ENDPOINTS; i++) {
1418
dev->endpoint[i].dev = dev;
1419
QTAILQ_INIT(&dev->endpoint[i].bufpq);
1423
static void usbredir_realize(USBDevice *udev, Error **errp)
1425
USBRedirDevice *dev = USB_REDIRECT(udev);
1428
if (!qemu_chr_fe_backend_connected(&dev->cs)) {
1429
error_setg(errp, QERR_MISSING_PARAMETER, "chardev");
1433
if (dev->filter_str) {
1434
i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|",
1436
&dev->filter_rules_count);
1438
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "filter",
1439
"a usb device filter string");
1444
dev->chardev_close_bh = qemu_bh_new_guarded(usbredir_chardev_close_bh, dev,
1445
&DEVICE(dev)->mem_reentrancy_guard);
1446
dev->device_reject_bh = qemu_bh_new_guarded(usbredir_device_reject_bh, dev,
1447
&DEVICE(dev)->mem_reentrancy_guard);
1448
dev->attach_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, usbredir_do_attach, dev);
1450
packet_id_queue_init(&dev->cancelled, dev, "cancelled");
1451
packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight");
1452
usbredir_init_endpoints(dev);
1455
udev->auto_attach = 0;
1458
dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
1461
qemu_chr_fe_set_handlers(&dev->cs, usbredir_chardev_can_read,
1462
usbredir_chardev_read, usbredir_chardev_event,
1463
NULL, dev, NULL, true);
1466
qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev);
1469
static void usbredir_cleanup_device_queues(USBRedirDevice *dev)
1473
packet_id_queue_empty(&dev->cancelled);
1474
packet_id_queue_empty(&dev->already_in_flight);
1475
for (i = 0; i < MAX_ENDPOINTS; i++) {
1476
usbredir_free_bufpq(dev, I2EP(i));
1480
static void usbredir_unrealize(USBDevice *udev)
1482
USBRedirDevice *dev = USB_REDIRECT(udev);
1484
qemu_chr_fe_deinit(&dev->cs, true);
1487
qemu_bh_delete(dev->chardev_close_bh);
1488
qemu_bh_delete(dev->device_reject_bh);
1490
timer_free(dev->attach_timer);
1492
usbredir_cleanup_device_queues(dev);
1495
usbredirparser_destroy(dev->parser);
1498
g_source_remove(dev->watch);
1501
free(dev->filter_rules);
1502
qemu_del_vm_change_state_handler(dev->vmstate);
1505
static int usbredir_check_filter(USBRedirDevice *dev)
1507
if (dev->interface_info.interface_count == NO_INTERFACE_INFO) {
1508
ERROR("No interface info for device\n");
1512
if (dev->filter_rules) {
1513
if (!usbredirparser_peer_has_cap(dev->parser,
1514
usb_redir_cap_connect_device_version)) {
1515
ERROR("Device filter specified and peer does not have the "
1516
"connect_device_version capability\n");
1520
if (usbredirfilter_check(
1522
dev->filter_rules_count,
1523
dev->device_info.device_class,
1524
dev->device_info.device_subclass,
1525
dev->device_info.device_protocol,
1526
dev->interface_info.interface_class,
1527
dev->interface_info.interface_subclass,
1528
dev->interface_info.interface_protocol,
1529
dev->interface_info.interface_count,
1530
dev->device_info.vendor_id,
1531
dev->device_info.product_id,
1532
dev->device_info.device_version_bcd,
1541
usbredir_reject_device(dev);
1545
static void usbredir_check_bulk_receiving(USBRedirDevice *dev)
1549
if (!usbredirparser_peer_has_cap(dev->parser,
1550
usb_redir_cap_bulk_receiving)) {
1554
for (i = EP2I(USB_DIR_IN); i < MAX_ENDPOINTS; i++) {
1555
dev->endpoint[i].bulk_receiving_enabled = 0;
1558
if (dev->interface_info.interface_count == NO_INTERFACE_INFO) {
1562
for (i = 0; i < dev->interface_info.interface_count; i++) {
1563
quirks = usb_get_quirks(dev->device_info.vendor_id,
1564
dev->device_info.product_id,
1565
dev->interface_info.interface_class[i],
1566
dev->interface_info.interface_subclass[i],
1567
dev->interface_info.interface_protocol[i]);
1568
if (!(quirks & USB_QUIRK_BUFFER_BULK_IN)) {
1571
if (quirks & USB_QUIRK_IS_FTDI) {
1572
dev->buffered_bulk_in_complete =
1573
usbredir_buffered_bulk_in_complete_ftdi;
1575
dev->buffered_bulk_in_complete =
1576
usbredir_buffered_bulk_in_complete_raw;
1579
for (j = EP2I(USB_DIR_IN); j < MAX_ENDPOINTS; j++) {
1580
if (dev->endpoint[j].interface ==
1581
dev->interface_info.interface[i] &&
1582
dev->endpoint[j].type == USB_ENDPOINT_XFER_BULK &&
1583
dev->endpoint[j].max_packet_size != 0) {
1584
dev->endpoint[j].bulk_receiving_enabled = 1;
1589
I2USBEP(dev, j)->pipeline = false;
1600
static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p,
1604
case usb_redir_success:
1605
p->status = USB_RET_SUCCESS;
1607
case usb_redir_stall:
1608
p->status = USB_RET_STALL;
1610
case usb_redir_cancelled:
1615
p->status = USB_RET_IOERROR;
1617
case usb_redir_inval:
1618
WARNING("got invalid param error from usb-host?\n");
1619
p->status = USB_RET_IOERROR;
1621
case usb_redir_babble:
1622
p->status = USB_RET_BABBLE;
1624
case usb_redir_ioerror:
1625
case usb_redir_timeout:
1627
p->status = USB_RET_IOERROR;
1631
static void usbredir_hello(void *priv, struct usb_redir_hello_header *h)
1633
USBRedirDevice *dev = priv;
1636
if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) &&
1637
dev->filter_rules) {
1638
usbredirparser_send_filter_filter(dev->parser, dev->filter_rules,
1639
dev->filter_rules_count);
1640
usbredirparser_do_write(dev->parser);
1644
static void usbredir_device_connect(void *priv,
1645
struct usb_redir_device_connect_header *device_connect)
1647
USBRedirDevice *dev = priv;
1650
if (timer_pending(dev->attach_timer) || dev->dev.attached) {
1651
ERROR("Received device connect while already connected\n");
1655
switch (device_connect->speed) {
1656
case usb_redir_speed_low:
1657
speed = "low speed";
1658
dev->dev.speed = USB_SPEED_LOW;
1659
dev->compatible_speedmask &= ~USB_SPEED_MASK_FULL;
1660
dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH;
1662
case usb_redir_speed_full:
1663
speed = "full speed";
1664
dev->dev.speed = USB_SPEED_FULL;
1665
dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH;
1667
case usb_redir_speed_high:
1668
speed = "high speed";
1669
dev->dev.speed = USB_SPEED_HIGH;
1671
case usb_redir_speed_super:
1672
speed = "super speed";
1673
dev->dev.speed = USB_SPEED_SUPER;
1676
speed = "unknown speed";
1677
dev->dev.speed = USB_SPEED_FULL;
1680
if (usbredirparser_peer_has_cap(dev->parser,
1681
usb_redir_cap_connect_device_version)) {
1682
INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1683
speed, device_connect->vendor_id, device_connect->product_id,
1684
((device_connect->device_version_bcd & 0xf000) >> 12) * 10 +
1685
((device_connect->device_version_bcd & 0x0f00) >> 8),
1686
((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 +
1687
((device_connect->device_version_bcd & 0x000f) >> 0),
1688
device_connect->device_class);
1690
INFO("attaching %s device %04x:%04x class %02x\n", speed,
1691
device_connect->vendor_id, device_connect->product_id,
1692
device_connect->device_class);
1695
dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask;
1696
dev->device_info = *device_connect;
1698
if (usbredir_check_filter(dev)) {
1699
WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1700
device_connect->vendor_id, device_connect->product_id);
1704
usbredir_check_bulk_receiving(dev);
1705
timer_mod(dev->attach_timer, dev->next_attach_time);
1708
static void usbredir_device_disconnect(void *priv)
1710
USBRedirDevice *dev = priv;
1713
timer_del(dev->attach_timer);
1715
if (dev->dev.attached) {
1716
DPRINTF("detaching device\n");
1717
usb_device_detach(&dev->dev);
1722
dev->next_attach_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 200;
1726
usbredir_cleanup_device_queues(dev);
1727
usbredir_init_endpoints(dev);
1728
dev->interface_info.interface_count = NO_INTERFACE_INFO;
1731
dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
1734
static void usbredir_interface_info(void *priv,
1735
struct usb_redir_interface_info_header *interface_info)
1737
USBRedirDevice *dev = priv;
1739
dev->interface_info = *interface_info;
1745
if (timer_pending(dev->attach_timer) || dev->dev.attached) {
1746
usbredir_check_bulk_receiving(dev);
1747
if (usbredir_check_filter(dev)) {
1748
ERROR("Device no longer matches filter after interface info "
1749
"change, disconnecting!\n");
1754
static void usbredir_mark_speed_incompatible(USBRedirDevice *dev, int speed)
1756
dev->compatible_speedmask &= ~(1 << speed);
1757
dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask;
1760
static void usbredir_set_pipeline(USBRedirDevice *dev, struct USBEndpoint *uep)
1762
if (uep->type != USB_ENDPOINT_XFER_BULK) {
1765
if (uep->pid == USB_TOKEN_OUT) {
1766
uep->pipeline = true;
1768
if (uep->pid == USB_TOKEN_IN && uep->max_packet_size != 0 &&
1769
usbredirparser_peer_has_cap(dev->parser,
1770
usb_redir_cap_32bits_bulk_length)) {
1771
uep->pipeline = true;
1775
static void usbredir_setup_usb_eps(USBRedirDevice *dev)
1777
struct USBEndpoint *usb_ep;
1780
for (i = 0; i < MAX_ENDPOINTS; i++) {
1781
usb_ep = I2USBEP(dev, i);
1782
usb_ep->type = dev->endpoint[i].type;
1783
usb_ep->ifnum = dev->endpoint[i].interface;
1784
usb_ep->max_packet_size = dev->endpoint[i].max_packet_size;
1785
usb_ep->max_streams = dev->endpoint[i].max_streams;
1786
usbredir_set_pipeline(dev, usb_ep);
1790
static void usbredir_ep_info(void *priv,
1791
struct usb_redir_ep_info_header *ep_info)
1793
USBRedirDevice *dev = priv;
1796
assert(dev != NULL);
1797
for (i = 0; i < MAX_ENDPOINTS; i++) {
1798
dev->endpoint[i].type = ep_info->type[i];
1799
dev->endpoint[i].interval = ep_info->interval[i];
1800
dev->endpoint[i].interface = ep_info->interface[i];
1801
if (usbredirparser_peer_has_cap(dev->parser,
1802
usb_redir_cap_ep_info_max_packet_size)) {
1803
dev->endpoint[i].max_packet_size = ep_info->max_packet_size[i];
1805
#if USBREDIR_VERSION >= 0x000700
1806
if (usbredirparser_peer_has_cap(dev->parser,
1807
usb_redir_cap_bulk_streams)) {
1808
dev->endpoint[i].max_streams = ep_info->max_streams[i];
1811
switch (dev->endpoint[i].type) {
1812
case usb_redir_type_invalid:
1814
case usb_redir_type_iso:
1815
usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL);
1816
usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH);
1818
case usb_redir_type_interrupt:
1819
if (!usbredirparser_peer_has_cap(dev->parser,
1820
usb_redir_cap_ep_info_max_packet_size) ||
1821
ep_info->max_packet_size[i] > 64) {
1822
usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL);
1824
if (!usbredirparser_peer_has_cap(dev->parser,
1825
usb_redir_cap_ep_info_max_packet_size) ||
1826
ep_info->max_packet_size[i] > 1024) {
1827
usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH);
1829
if (dev->endpoint[i].interval == 0) {
1830
ERROR("Received 0 interval for isoc or irq endpoint\n");
1831
usbredir_reject_device(dev);
1835
case usb_redir_type_control:
1836
case usb_redir_type_bulk:
1837
DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i),
1838
dev->endpoint[i].type, dev->endpoint[i].interface);
1841
ERROR("Received invalid endpoint type\n");
1842
usbredir_reject_device(dev);
1847
if (dev->dev.attached &&
1848
!(dev->dev.port->speedmask & dev->dev.speedmask)) {
1849
ERROR("Device no longer matches speed after endpoint info change, "
1850
"disconnecting!\n");
1851
usbredir_reject_device(dev);
1854
usbredir_setup_usb_eps(dev);
1855
usbredir_check_bulk_receiving(dev);
1858
static void usbredir_configuration_status(void *priv, uint64_t id,
1859
struct usb_redir_configuration_status_header *config_status)
1861
USBRedirDevice *dev = priv;
1864
DPRINTF("set config status %d config %d id %"PRIu64"\n",
1865
config_status->status, config_status->configuration, id);
1867
p = usbredir_find_packet_by_id(dev, 0, id);
1869
if (dev->dev.setup_buf[0] & USB_DIR_IN) {
1870
dev->dev.data_buf[0] = config_status->configuration;
1871
p->actual_length = 1;
1873
usbredir_handle_status(dev, p, config_status->status);
1874
usb_generic_async_ctrl_complete(&dev->dev, p);
1878
static void usbredir_alt_setting_status(void *priv, uint64_t id,
1879
struct usb_redir_alt_setting_status_header *alt_setting_status)
1881
USBRedirDevice *dev = priv;
1884
DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n",
1885
alt_setting_status->status, alt_setting_status->interface,
1886
alt_setting_status->alt, id);
1888
p = usbredir_find_packet_by_id(dev, 0, id);
1890
if (dev->dev.setup_buf[0] & USB_DIR_IN) {
1891
dev->dev.data_buf[0] = alt_setting_status->alt;
1892
p->actual_length = 1;
1894
usbredir_handle_status(dev, p, alt_setting_status->status);
1895
usb_generic_async_ctrl_complete(&dev->dev, p);
1899
static void usbredir_iso_stream_status(void *priv, uint64_t id,
1900
struct usb_redir_iso_stream_status_header *iso_stream_status)
1902
USBRedirDevice *dev = priv;
1903
uint8_t ep = iso_stream_status->endpoint;
1905
DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status,
1908
if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) {
1912
dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status;
1913
if (iso_stream_status->status == usb_redir_stall) {
1914
DPRINTF("iso stream stopped by peer ep %02X\n", ep);
1915
dev->endpoint[EP2I(ep)].iso_started = 0;
1919
static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
1920
struct usb_redir_interrupt_receiving_status_header
1921
*interrupt_receiving_status)
1923
USBRedirDevice *dev = priv;
1924
uint8_t ep = interrupt_receiving_status->endpoint;
1926
DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n",
1927
interrupt_receiving_status->status, ep, id);
1929
if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) {
1933
dev->endpoint[EP2I(ep)].interrupt_error =
1934
interrupt_receiving_status->status;
1935
if (interrupt_receiving_status->status == usb_redir_stall) {
1936
DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep);
1937
dev->endpoint[EP2I(ep)].interrupt_started = 0;
1941
static void usbredir_bulk_streams_status(void *priv, uint64_t id,
1942
struct usb_redir_bulk_streams_status_header *bulk_streams_status)
1944
#if USBREDIR_VERSION >= 0x000700
1945
USBRedirDevice *dev = priv;
1947
if (bulk_streams_status->status == usb_redir_success) {
1948
DPRINTF("bulk streams status %d eps %08x\n",
1949
bulk_streams_status->status, bulk_streams_status->endpoints);
1951
ERROR("bulk streams %s failed status %d eps %08x\n",
1952
(bulk_streams_status->no_streams == 0) ? "free" : "alloc",
1953
bulk_streams_status->status, bulk_streams_status->endpoints);
1954
ERROR("usb-redir-host does not provide streams, disconnecting\n");
1955
usbredir_reject_device(dev);
1960
static void usbredir_bulk_receiving_status(void *priv, uint64_t id,
1961
struct usb_redir_bulk_receiving_status_header *bulk_receiving_status)
1963
USBRedirDevice *dev = priv;
1964
uint8_t ep = bulk_receiving_status->endpoint;
1966
DPRINTF("bulk recv status %d ep %02X id %"PRIu64"\n",
1967
bulk_receiving_status->status, ep, id);
1969
if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].bulk_receiving_started) {
1973
if (bulk_receiving_status->status == usb_redir_stall) {
1974
DPRINTF("bulk receiving stopped by peer ep %02X\n", ep);
1975
dev->endpoint[EP2I(ep)].bulk_receiving_started = 0;
1979
static void usbredir_control_packet(void *priv, uint64_t id,
1980
struct usb_redir_control_packet_header *control_packet,
1981
uint8_t *data, int data_len)
1983
USBRedirDevice *dev = priv;
1985
int len = control_packet->length;
1987
DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status,
1992
if (dev->dev.speed == USB_SPEED_SUPER &&
1993
!((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER)) &&
1994
control_packet->requesttype == 0x80 &&
1995
control_packet->request == 6 &&
1996
control_packet->value == 0x100 && control_packet->index == 0 &&
1997
data_len >= 18 && data[7] == 9) {
2001
p = usbredir_find_packet_by_id(dev, 0, id);
2003
usbredir_handle_status(dev, p, control_packet->status);
2005
usbredir_log_data(dev, "ctrl data in:", data, data_len);
2006
if (data_len > sizeof(dev->dev.data_buf)) {
2007
ERROR("ctrl buffer too small (%d > %zu)\n",
2008
data_len, sizeof(dev->dev.data_buf));
2009
p->status = USB_RET_STALL;
2010
data_len = len = sizeof(dev->dev.data_buf);
2012
memcpy(dev->dev.data_buf, data, data_len);
2014
p->actual_length = len;
2020
if (dev->suppress_remote_wake &&
2021
control_packet->requesttype == USB_DIR_IN &&
2022
control_packet->request == USB_REQ_GET_DESCRIPTOR &&
2023
control_packet->value == (USB_DT_CONFIG << 8) &&
2024
control_packet->index == 0 &&
2026
len > 7 && (dev->dev.data_buf[7] & USB_CFG_ATT_WAKEUP)) {
2027
DPRINTF("Removed remote wake %04X:%04X\n",
2028
dev->device_info.vendor_id,
2029
dev->device_info.product_id);
2030
dev->dev.data_buf[7] &= ~USB_CFG_ATT_WAKEUP;
2032
usb_generic_async_ctrl_complete(&dev->dev, p);
2037
static void usbredir_bulk_packet(void *priv, uint64_t id,
2038
struct usb_redir_bulk_packet_header *bulk_packet,
2039
uint8_t *data, int data_len)
2041
USBRedirDevice *dev = priv;
2042
uint8_t ep = bulk_packet->endpoint;
2043
int len = (bulk_packet->length_high << 16) | bulk_packet->length;
2046
DPRINTF("bulk-in status %d ep %02X stream %u len %d id %"PRIu64"\n",
2047
bulk_packet->status, ep, bulk_packet->stream_id, len, id);
2049
p = usbredir_find_packet_by_id(dev, ep, id);
2051
size_t size = usb_packet_size(p);
2052
usbredir_handle_status(dev, p, bulk_packet->status);
2054
usbredir_log_data(dev, "bulk data in:", data, data_len);
2055
if (data_len > size) {
2056
ERROR("bulk got more data then requested (%d > %zd)\n",
2057
data_len, p->iov.size);
2058
p->status = USB_RET_BABBLE;
2059
data_len = len = size;
2061
usb_packet_copy(p, data, data_len);
2063
p->actual_length = len;
2064
if (p->pid == USB_TOKEN_IN && p->ep->pipeline) {
2065
usb_combined_input_packet_complete(&dev->dev, p);
2067
usb_packet_complete(&dev->dev, p);
2073
static void usbredir_iso_packet(void *priv, uint64_t id,
2074
struct usb_redir_iso_packet_header *iso_packet,
2075
uint8_t *data, int data_len)
2077
USBRedirDevice *dev = priv;
2078
uint8_t ep = iso_packet->endpoint;
2080
DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n",
2081
iso_packet->status, ep, data_len, id);
2083
if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) {
2084
ERROR("received iso packet for non iso endpoint %02X\n", ep);
2089
if (dev->endpoint[EP2I(ep)].iso_started == 0) {
2090
DPRINTF("received iso packet for non started stream ep %02X\n", ep);
2096
bufp_alloc(dev, data, data_len, iso_packet->status, ep, data);
2099
static void usbredir_interrupt_packet(void *priv, uint64_t id,
2100
struct usb_redir_interrupt_packet_header *interrupt_packet,
2101
uint8_t *data, int data_len)
2103
USBRedirDevice *dev = priv;
2104
uint8_t ep = interrupt_packet->endpoint;
2106
DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n",
2107
interrupt_packet->status, ep, data_len, id);
2109
if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) {
2110
ERROR("received int packet for non interrupt endpoint %02X\n", ep);
2115
if (ep & USB_DIR_IN) {
2116
if (dev->endpoint[EP2I(ep)].interrupt_started == 0) {
2117
DPRINTF("received int packet while not started ep %02X\n", ep);
2123
bufp_alloc(dev, data, data_len, interrupt_packet->status, ep, data);
2126
usb_wakeup(usb_ep_get(&dev->dev, USB_TOKEN_IN, ep & 0x0f), 0);
2132
if (interrupt_packet->status) {
2133
WARNING("interrupt output failed status %d ep %02X id %"PRIu64"\n",
2134
interrupt_packet->status, ep, id);
2139
static void usbredir_buffered_bulk_packet(void *priv, uint64_t id,
2140
struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet,
2141
uint8_t *data, int data_len)
2143
USBRedirDevice *dev = priv;
2144
uint8_t status, ep = buffered_bulk_packet->endpoint;
2145
void *free_on_destroy;
2148
DPRINTF("buffered-bulk-in status %d ep %02X len %d id %"PRIu64"\n",
2149
buffered_bulk_packet->status, ep, data_len, id);
2151
if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_BULK) {
2152
ERROR("received buffered-bulk packet for non bulk ep %02X\n", ep);
2157
if (dev->endpoint[EP2I(ep)].bulk_receiving_started == 0) {
2158
DPRINTF("received buffered-bulk packet on not started ep %02X\n", ep);
2164
len = dev->endpoint[EP2I(ep)].max_packet_size;
2165
status = usb_redir_success;
2166
free_on_destroy = NULL;
2167
for (i = 0; i < data_len; i += len) {
2169
if (len >= (data_len - i)) {
2171
status = buffered_bulk_packet->status;
2172
free_on_destroy = data;
2175
r = bufp_alloc(dev, data + i, len, status, ep, free_on_destroy);
2181
if (dev->endpoint[EP2I(ep)].pending_async_packet) {
2182
USBPacket *p = dev->endpoint[EP2I(ep)].pending_async_packet;
2183
dev->endpoint[EP2I(ep)].pending_async_packet = NULL;
2184
usbredir_buffered_bulk_in_complete(dev, p, ep);
2185
usb_packet_complete(&dev->dev, p);
2193
static int usbredir_pre_save(void *priv)
2195
USBRedirDevice *dev = priv;
2197
usbredir_fill_already_in_flight(dev);
2202
static int usbredir_post_load(void *priv, int version_id)
2204
USBRedirDevice *dev = priv;
2206
if (dev == NULL || dev->parser == NULL) {
2210
switch (dev->device_info.speed) {
2211
case usb_redir_speed_low:
2212
dev->dev.speed = USB_SPEED_LOW;
2214
case usb_redir_speed_full:
2215
dev->dev.speed = USB_SPEED_FULL;
2217
case usb_redir_speed_high:
2218
dev->dev.speed = USB_SPEED_HIGH;
2220
case usb_redir_speed_super:
2221
dev->dev.speed = USB_SPEED_SUPER;
2224
dev->dev.speed = USB_SPEED_FULL;
2226
dev->dev.speedmask = (1 << dev->dev.speed);
2228
usbredir_setup_usb_eps(dev);
2229
usbredir_check_bulk_receiving(dev);
2235
static int usbredir_put_parser(QEMUFile *f, void *priv, size_t unused,
2236
const VMStateField *field, JSONWriter *vmdesc)
2238
USBRedirDevice *dev = priv;
2242
if (dev->parser == NULL) {
2243
qemu_put_be32(f, 0);
2247
usbredirparser_serialize(dev->parser, &data, &len);
2249
error_report("usbredirparser_serialize failed");
2253
qemu_put_be32(f, len);
2254
qemu_put_buffer(f, data, len);
2261
static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused,
2262
const VMStateField *field)
2264
USBRedirDevice *dev = priv;
2268
len = qemu_get_be32(f);
2281
if (dev->parser == NULL) {
2282
WARNING("usb-redir connection broken during migration\n");
2283
usbredir_create_parser(dev);
2284
qemu_bh_schedule(dev->chardev_close_bh);
2287
data = g_malloc(len);
2288
qemu_get_buffer(f, data, len);
2290
ret = usbredirparser_unserialize(dev->parser, data, len);
2297
static const VMStateInfo usbredir_parser_vmstate_info = {
2298
.name = "usb-redir-parser",
2299
.put = usbredir_put_parser,
2300
.get = usbredir_get_parser,
2305
static int usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused,
2306
const VMStateField *field, JSONWriter *vmdesc)
2308
struct endp_data *endp = priv;
2309
USBRedirDevice *dev = endp->dev;
2310
struct buf_packet *bufp;
2313
qemu_put_be32(f, endp->bufpq_size);
2314
QTAILQ_FOREACH(bufp, &endp->bufpq, next) {
2315
len = bufp->len - bufp->offset;
2316
DPRINTF("put_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size,
2318
qemu_put_be32(f, len);
2319
qemu_put_be32(f, bufp->status);
2320
qemu_put_buffer(f, bufp->data + bufp->offset, len);
2323
assert(i == endp->bufpq_size);
2328
static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused,
2329
const VMStateField *field)
2331
struct endp_data *endp = priv;
2332
USBRedirDevice *dev = endp->dev;
2333
struct buf_packet *bufp;
2336
endp->bufpq_size = qemu_get_be32(f);
2337
for (i = 0; i < endp->bufpq_size; i++) {
2338
bufp = g_new(struct buf_packet, 1);
2339
bufp->len = qemu_get_be32(f);
2340
bufp->status = qemu_get_be32(f);
2342
bufp->data = malloc(bufp->len);
2344
error_report("usbredir_get_bufpq: out of memory");
2347
bufp->free_on_destroy = bufp->data;
2348
qemu_get_buffer(f, bufp->data, bufp->len);
2349
QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next);
2350
DPRINTF("get_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size,
2351
bufp->len, bufp->status);
2356
static const VMStateInfo usbredir_ep_bufpq_vmstate_info = {
2357
.name = "usb-redir-bufpq",
2358
.put = usbredir_put_bufpq,
2359
.get = usbredir_get_bufpq,
2364
static bool usbredir_bulk_receiving_needed(void *priv)
2366
struct endp_data *endp = priv;
2368
return endp->bulk_receiving_started;
2371
static const VMStateDescription usbredir_bulk_receiving_vmstate = {
2372
.name = "usb-redir-ep/bulk-receiving",
2374
.minimum_version_id = 1,
2375
.needed = usbredir_bulk_receiving_needed,
2376
.fields = (const VMStateField[]) {
2377
VMSTATE_UINT8(bulk_receiving_started, struct endp_data),
2378
VMSTATE_END_OF_LIST()
2382
static bool usbredir_stream_needed(void *priv)
2384
struct endp_data *endp = priv;
2386
return endp->max_streams;
2389
static const VMStateDescription usbredir_stream_vmstate = {
2390
.name = "usb-redir-ep/stream-state",
2392
.minimum_version_id = 1,
2393
.needed = usbredir_stream_needed,
2394
.fields = (const VMStateField[]) {
2395
VMSTATE_UINT32(max_streams, struct endp_data),
2396
VMSTATE_END_OF_LIST()
2400
static const VMStateDescription usbredir_ep_vmstate = {
2401
.name = "usb-redir-ep",
2403
.minimum_version_id = 1,
2404
.fields = (const VMStateField[]) {
2405
VMSTATE_UINT8(type, struct endp_data),
2406
VMSTATE_UINT8(interval, struct endp_data),
2407
VMSTATE_UINT8(interface, struct endp_data),
2408
VMSTATE_UINT16(max_packet_size, struct endp_data),
2409
VMSTATE_UINT8(iso_started, struct endp_data),
2410
VMSTATE_UINT8(iso_error, struct endp_data),
2411
VMSTATE_UINT8(interrupt_started, struct endp_data),
2412
VMSTATE_UINT8(interrupt_error, struct endp_data),
2413
VMSTATE_UINT8(bufpq_prefilled, struct endp_data),
2414
VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data),
2418
.field_exists = NULL,
2420
.info = &usbredir_ep_bufpq_vmstate_info,
2421
.flags = VMS_SINGLE,
2424
VMSTATE_INT32(bufpq_target_size, struct endp_data),
2425
VMSTATE_END_OF_LIST()
2427
.subsections = (const VMStateDescription * const []) {
2428
&usbredir_bulk_receiving_vmstate,
2429
&usbredir_stream_vmstate,
2436
static int usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused,
2437
const VMStateField *field,
2440
struct PacketIdQueue *q = priv;
2441
USBRedirDevice *dev = q->dev;
2442
struct PacketIdQueueEntry *e;
2443
int remain = q->size;
2445
DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size);
2446
qemu_put_be32(f, q->size);
2447
QTAILQ_FOREACH(e, &q->head, next) {
2448
qemu_put_be64(f, e->id);
2451
assert(remain == 0);
2456
static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused,
2457
const VMStateField *field)
2459
struct PacketIdQueue *q = priv;
2460
USBRedirDevice *dev = q->dev;
2464
size = qemu_get_be32(f);
2465
DPRINTF("get_packet_id_q %s size %d\n", q->name, size);
2466
for (i = 0; i < size; i++) {
2467
id = qemu_get_be64(f);
2468
packet_id_queue_add(q, id);
2470
assert(q->size == size);
2474
static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = {
2475
.name = "usb-redir-packet-id-q",
2476
.put = usbredir_put_packet_id_q,
2477
.get = usbredir_get_packet_id_q,
2480
static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = {
2481
.name = "usb-redir-packet-id-queue",
2483
.minimum_version_id = 1,
2484
.fields = (const VMStateField[]) {
2488
.field_exists = NULL,
2490
.info = &usbredir_ep_packet_id_q_vmstate_info,
2491
.flags = VMS_SINGLE,
2494
VMSTATE_END_OF_LIST()
2500
static const VMStateDescription usbredir_device_info_vmstate = {
2501
.name = "usb-redir-device-info",
2503
.minimum_version_id = 1,
2504
.fields = (const VMStateField[]) {
2505
VMSTATE_UINT8(speed, struct usb_redir_device_connect_header),
2506
VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header),
2507
VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header),
2508
VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header),
2509
VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header),
2510
VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header),
2511
VMSTATE_UINT16(device_version_bcd,
2512
struct usb_redir_device_connect_header),
2513
VMSTATE_END_OF_LIST()
2519
static const VMStateDescription usbredir_interface_info_vmstate = {
2520
.name = "usb-redir-interface-info",
2522
.minimum_version_id = 1,
2523
.fields = (const VMStateField[]) {
2524
VMSTATE_UINT32(interface_count,
2525
struct usb_redir_interface_info_header),
2526
VMSTATE_UINT8_ARRAY(interface,
2527
struct usb_redir_interface_info_header, 32),
2528
VMSTATE_UINT8_ARRAY(interface_class,
2529
struct usb_redir_interface_info_header, 32),
2530
VMSTATE_UINT8_ARRAY(interface_subclass,
2531
struct usb_redir_interface_info_header, 32),
2532
VMSTATE_UINT8_ARRAY(interface_protocol,
2533
struct usb_redir_interface_info_header, 32),
2534
VMSTATE_END_OF_LIST()
2540
static const VMStateDescription usbredir_vmstate = {
2541
.name = "usb-redir",
2543
.minimum_version_id = 1,
2544
.pre_save = usbredir_pre_save,
2545
.post_load = usbredir_post_load,
2546
.fields = (const VMStateField[]) {
2547
VMSTATE_USB_DEVICE(dev, USBRedirDevice),
2548
VMSTATE_TIMER_PTR(attach_timer, USBRedirDevice),
2552
.field_exists = NULL,
2554
.info = &usbredir_parser_vmstate_info,
2555
.flags = VMS_SINGLE,
2558
VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1,
2559
usbredir_ep_vmstate, struct endp_data),
2560
VMSTATE_STRUCT(cancelled, USBRedirDevice, 1,
2561
usbredir_ep_packet_id_queue_vmstate,
2562
struct PacketIdQueue),
2563
VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1,
2564
usbredir_ep_packet_id_queue_vmstate,
2565
struct PacketIdQueue),
2566
VMSTATE_STRUCT(device_info, USBRedirDevice, 1,
2567
usbredir_device_info_vmstate,
2568
struct usb_redir_device_connect_header),
2569
VMSTATE_STRUCT(interface_info, USBRedirDevice, 1,
2570
usbredir_interface_info_vmstate,
2571
struct usb_redir_interface_info_header),
2572
VMSTATE_END_OF_LIST()
2576
static Property usbredir_properties[] = {
2577
DEFINE_PROP_CHR("chardev", USBRedirDevice, cs),
2578
DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, usbredirparser_warning),
2579
DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str),
2580
DEFINE_PROP_BOOL("streams", USBRedirDevice, enable_streams, true),
2581
DEFINE_PROP_BOOL("suppress-remote-wake", USBRedirDevice,
2582
suppress_remote_wake, true),
2583
DEFINE_PROP_END_OF_LIST(),
2586
static void usbredir_class_initfn(ObjectClass *klass, void *data)
2588
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
2589
DeviceClass *dc = DEVICE_CLASS(klass);
2591
uc->realize = usbredir_realize;
2592
uc->product_desc = "USB Redirection Device";
2593
uc->unrealize = usbredir_unrealize;
2594
uc->cancel_packet = usbredir_cancel_packet;
2595
uc->handle_reset = usbredir_handle_reset;
2596
uc->handle_data = usbredir_handle_data;
2597
uc->handle_control = usbredir_handle_control;
2598
uc->flush_ep_queue = usbredir_flush_ep_queue;
2599
uc->ep_stopped = usbredir_ep_stopped;
2600
uc->alloc_streams = usbredir_alloc_streams;
2601
uc->free_streams = usbredir_free_streams;
2602
dc->vmsd = &usbredir_vmstate;
2603
device_class_set_props(dc, usbredir_properties);
2604
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2607
static void usbredir_instance_init(Object *obj)
2609
USBDevice *udev = USB_DEVICE(obj);
2610
USBRedirDevice *dev = USB_REDIRECT(udev);
2612
device_add_bootindex_property(obj, &dev->bootindex,
2617
static const TypeInfo usbredir_dev_info = {
2618
.name = TYPE_USB_REDIR,
2619
.parent = TYPE_USB_DEVICE,
2620
.instance_size = sizeof(USBRedirDevice),
2621
.class_init = usbredir_class_initfn,
2622
.instance_init = usbredir_instance_init,
2624
module_obj(TYPE_USB_REDIR);
2627
static void usbredir_register_types(void)
2629
type_register_static(&usbredir_dev_info);
2632
type_init(usbredir_register_types)