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