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