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