]> git.proxmox.com Git - mirror_qemu.git/blame - usb-redir.c
usb-redir: Let the usb-host know about our device filtering
[mirror_qemu.git] / usb-redir.c
CommitLineData
69354a83
HG
1/*
2 * USB redirector usb-guest
3 *
4 * Copyright (c) 2011 Red Hat, Inc.
5 *
6 * Red Hat Authors:
7 * Hans de Goede <hdegoede@redhat.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28#include "qemu-common.h"
29#include "qemu-timer.h"
30#include "monitor.h"
31#include "sysemu.h"
32
33#include <dirent.h>
34#include <sys/ioctl.h>
35#include <signal.h>
36#include <usbredirparser.h>
6af16589 37#include <usbredirfilter.h>
69354a83
HG
38
39#include "hw/usb.h"
40
41#define MAX_ENDPOINTS 32
42#define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f))
43#define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f))
44
45typedef struct AsyncURB AsyncURB;
46typedef struct USBRedirDevice USBRedirDevice;
47
48/* Struct to hold buffered packets (iso or int input packets) */
49struct buf_packet {
50 uint8_t *data;
51 int len;
52 int status;
53 QTAILQ_ENTRY(buf_packet)next;
54};
55
56struct endp_data {
57 uint8_t type;
58 uint8_t interval;
59 uint8_t interface; /* bInterfaceNumber this ep belongs to */
60 uint8_t iso_started;
61 uint8_t iso_error; /* For reporting iso errors to the HC */
62 uint8_t interrupt_started;
63 uint8_t interrupt_error;
e1537884 64 uint8_t bufpq_prefilled;
81fd7b74 65 uint8_t bufpq_dropping_packets;
69354a83 66 QTAILQ_HEAD(, buf_packet) bufpq;
e1537884 67 int bufpq_size;
e8a7dd29 68 int bufpq_target_size;
69354a83
HG
69};
70
71struct USBRedirDevice {
72 USBDevice dev;
73 /* Properties */
74 CharDriverState *cs;
75 uint8_t debug;
6af16589 76 char *filter_str;
69354a83
HG
77 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
78 const uint8_t *read_buf;
79 int read_buf_size;
80 /* For async handling of open/close */
81 QEMUBH *open_close_bh;
82 /* To delay the usb attach in case of quick chardev close + open */
83 QEMUTimer *attach_timer;
84 int64_t next_attach_time;
85 struct usbredirparser *parser;
86 struct endp_data endpoint[MAX_ENDPOINTS];
87 uint32_t packet_id;
88 QTAILQ_HEAD(, AsyncURB) asyncq;
6af16589
HG
89 /* Data for device filtering */
90 struct usb_redir_device_connect_header device_info;
91 struct usb_redir_interface_info_header interface_info;
92 struct usbredirfilter_rule *filter_rules;
93 int filter_rules_count;
69354a83
HG
94};
95
96struct AsyncURB {
97 USBRedirDevice *dev;
98 USBPacket *packet;
99 uint32_t packet_id;
100 int get;
101 union {
102 struct usb_redir_control_packet_header control_packet;
103 struct usb_redir_bulk_packet_header bulk_packet;
104 struct usb_redir_interrupt_packet_header interrupt_packet;
105 };
106 QTAILQ_ENTRY(AsyncURB)next;
107};
108
097a66ef 109static void usbredir_hello(void *priv, struct usb_redir_hello_header *h);
69354a83
HG
110static void usbredir_device_connect(void *priv,
111 struct usb_redir_device_connect_header *device_connect);
112static void usbredir_device_disconnect(void *priv);
113static void usbredir_interface_info(void *priv,
114 struct usb_redir_interface_info_header *interface_info);
115static void usbredir_ep_info(void *priv,
116 struct usb_redir_ep_info_header *ep_info);
117static void usbredir_configuration_status(void *priv, uint32_t id,
118 struct usb_redir_configuration_status_header *configuration_status);
119static void usbredir_alt_setting_status(void *priv, uint32_t id,
120 struct usb_redir_alt_setting_status_header *alt_setting_status);
121static void usbredir_iso_stream_status(void *priv, uint32_t id,
122 struct usb_redir_iso_stream_status_header *iso_stream_status);
123static void usbredir_interrupt_receiving_status(void *priv, uint32_t id,
124 struct usb_redir_interrupt_receiving_status_header
125 *interrupt_receiving_status);
126static void usbredir_bulk_streams_status(void *priv, uint32_t id,
127 struct usb_redir_bulk_streams_status_header *bulk_streams_status);
128static void usbredir_control_packet(void *priv, uint32_t id,
129 struct usb_redir_control_packet_header *control_packet,
130 uint8_t *data, int data_len);
131static void usbredir_bulk_packet(void *priv, uint32_t id,
132 struct usb_redir_bulk_packet_header *bulk_packet,
133 uint8_t *data, int data_len);
134static void usbredir_iso_packet(void *priv, uint32_t id,
135 struct usb_redir_iso_packet_header *iso_packet,
136 uint8_t *data, int data_len);
137static void usbredir_interrupt_packet(void *priv, uint32_t id,
138 struct usb_redir_interrupt_packet_header *interrupt_header,
139 uint8_t *data, int data_len);
140
141static int usbredir_handle_status(USBRedirDevice *dev,
142 int status, int actual_len);
143
144#define VERSION "qemu usb-redir guest " QEMU_VERSION
145
146/*
147 * Logging stuff
148 */
149
150#define ERROR(...) \
151 do { \
152 if (dev->debug >= usbredirparser_error) { \
153 error_report("usb-redir error: " __VA_ARGS__); \
154 } \
155 } while (0)
156#define WARNING(...) \
157 do { \
158 if (dev->debug >= usbredirparser_warning) { \
159 error_report("usb-redir warning: " __VA_ARGS__); \
160 } \
161 } while (0)
162#define INFO(...) \
163 do { \
164 if (dev->debug >= usbredirparser_info) { \
165 error_report("usb-redir: " __VA_ARGS__); \
166 } \
167 } while (0)
168#define DPRINTF(...) \
169 do { \
170 if (dev->debug >= usbredirparser_debug) { \
171 error_report("usb-redir: " __VA_ARGS__); \
172 } \
173 } while (0)
174#define DPRINTF2(...) \
175 do { \
176 if (dev->debug >= usbredirparser_debug_data) { \
177 error_report("usb-redir: " __VA_ARGS__); \
178 } \
179 } while (0)
180
181static void usbredir_log(void *priv, int level, const char *msg)
182{
183 USBRedirDevice *dev = priv;
184
185 if (dev->debug < level) {
186 return;
187 }
188
be62a2eb 189 error_report("%s", msg);
69354a83
HG
190}
191
192static void usbredir_log_data(USBRedirDevice *dev, const char *desc,
193 const uint8_t *data, int len)
194{
195 int i, j, n;
196
197 if (dev->debug < usbredirparser_debug_data) {
198 return;
199 }
200
201 for (i = 0; i < len; i += j) {
202 char buf[128];
203
204 n = sprintf(buf, "%s", desc);
205 for (j = 0; j < 8 && i + j < len; j++) {
206 n += sprintf(buf + n, " %02X", data[i + j]);
207 }
be62a2eb 208 error_report("%s", buf);
69354a83
HG
209 }
210}
211
212/*
213 * usbredirparser io functions
214 */
215
216static int usbredir_read(void *priv, uint8_t *data, int count)
217{
218 USBRedirDevice *dev = priv;
219
220 if (dev->read_buf_size < count) {
221 count = dev->read_buf_size;
222 }
223
224 memcpy(data, dev->read_buf, count);
225
226 dev->read_buf_size -= count;
227 if (dev->read_buf_size) {
228 dev->read_buf += count;
229 } else {
230 dev->read_buf = NULL;
231 }
232
233 return count;
234}
235
236static int usbredir_write(void *priv, uint8_t *data, int count)
237{
238 USBRedirDevice *dev = priv;
239
c1b71a1d
HG
240 if (!dev->cs->opened) {
241 return 0;
242 }
243
2cc6e0a1 244 return qemu_chr_fe_write(dev->cs, data, count);
69354a83
HG
245}
246
247/*
248 * Async and buffered packets helpers
249 */
250
251static AsyncURB *async_alloc(USBRedirDevice *dev, USBPacket *p)
252{
7267c094 253 AsyncURB *aurb = (AsyncURB *) g_malloc0(sizeof(AsyncURB));
69354a83
HG
254 aurb->dev = dev;
255 aurb->packet = p;
256 aurb->packet_id = dev->packet_id;
257 QTAILQ_INSERT_TAIL(&dev->asyncq, aurb, next);
258 dev->packet_id++;
259
260 return aurb;
261}
262
263static void async_free(USBRedirDevice *dev, AsyncURB *aurb)
264{
265 QTAILQ_REMOVE(&dev->asyncq, aurb, next);
7267c094 266 g_free(aurb);
69354a83
HG
267}
268
269static AsyncURB *async_find(USBRedirDevice *dev, uint32_t packet_id)
270{
271 AsyncURB *aurb;
272
273 QTAILQ_FOREACH(aurb, &dev->asyncq, next) {
274 if (aurb->packet_id == packet_id) {
275 return aurb;
276 }
277 }
278 ERROR("could not find async urb for packet_id %u\n", packet_id);
279 return NULL;
280}
281
282static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p)
283{
284 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
285 AsyncURB *aurb;
286
287 QTAILQ_FOREACH(aurb, &dev->asyncq, next) {
288 if (p != aurb->packet) {
289 continue;
290 }
291
292 DPRINTF("async cancel id %u\n", aurb->packet_id);
293 usbredirparser_send_cancel_data_packet(dev->parser, aurb->packet_id);
294 usbredirparser_do_write(dev->parser);
295
296 /* Mark it as dead */
297 aurb->packet = NULL;
298 break;
299 }
300}
301
81fd7b74 302static void bufp_alloc(USBRedirDevice *dev,
69354a83
HG
303 uint8_t *data, int len, int status, uint8_t ep)
304{
81fd7b74
HG
305 struct buf_packet *bufp;
306
307 if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets &&
308 dev->endpoint[EP2I(ep)].bufpq_size >
309 2 * dev->endpoint[EP2I(ep)].bufpq_target_size) {
310 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep);
311 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1;
312 }
313 /* Since we're interupting the stream anyways, drop enough packets to get
314 back to our target buffer size */
315 if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) {
316 if (dev->endpoint[EP2I(ep)].bufpq_size >
317 dev->endpoint[EP2I(ep)].bufpq_target_size) {
318 free(data);
319 return;
320 }
321 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
322 }
323
324 bufp = g_malloc(sizeof(struct buf_packet));
69354a83
HG
325 bufp->data = data;
326 bufp->len = len;
327 bufp->status = status;
328 QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
e1537884 329 dev->endpoint[EP2I(ep)].bufpq_size++;
69354a83
HG
330}
331
332static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp,
333 uint8_t ep)
334{
335 QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
e1537884 336 dev->endpoint[EP2I(ep)].bufpq_size--;
69354a83 337 free(bufp->data);
7267c094 338 g_free(bufp);
69354a83
HG
339}
340
341static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep)
342{
343 struct buf_packet *buf, *buf_next;
344
345 QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) {
346 bufp_free(dev, buf, ep);
347 }
348}
349
350/*
351 * USBDevice callbacks
352 */
353
354static void usbredir_handle_reset(USBDevice *udev)
355{
356 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
357
358 DPRINTF("reset device\n");
359 usbredirparser_send_reset(dev->parser);
360 usbredirparser_do_write(dev->parser);
361}
362
363static int usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
364 uint8_t ep)
365{
366 int status, len;
69354a83
HG
367 if (!dev->endpoint[EP2I(ep)].iso_started &&
368 !dev->endpoint[EP2I(ep)].iso_error) {
369 struct usb_redir_start_iso_stream_header start_iso = {
370 .endpoint = ep,
69354a83 371 };
e8a7dd29
HG
372 int pkts_per_sec;
373
374 if (dev->dev.speed == USB_SPEED_HIGH) {
375 pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval;
376 } else {
377 pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval;
378 }
379 /* Testing has shown that we need circa 60 ms buffer */
380 dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000;
381
382 /* Aim for approx 100 interrupts / second on the client to
383 balance latency and interrupt load */
384 start_iso.pkts_per_urb = pkts_per_sec / 100;
385 if (start_iso.pkts_per_urb < 1) {
386 start_iso.pkts_per_urb = 1;
387 } else if (start_iso.pkts_per_urb > 32) {
388 start_iso.pkts_per_urb = 32;
389 }
390
391 start_iso.no_urbs = (dev->endpoint[EP2I(ep)].bufpq_target_size +
392 start_iso.pkts_per_urb - 1) /
393 start_iso.pkts_per_urb;
394 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
395 as overflow buffer. Also see the usbredir protocol documentation */
396 if (!(ep & USB_DIR_IN)) {
397 start_iso.no_urbs *= 2;
398 }
399 if (start_iso.no_urbs > 16) {
400 start_iso.no_urbs = 16;
401 }
402
69354a83
HG
403 /* No id, we look at the ep when receiving a status back */
404 usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso);
405 usbredirparser_do_write(dev->parser);
32213543
HG
406 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
407 pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep);
69354a83 408 dev->endpoint[EP2I(ep)].iso_started = 1;
e1537884 409 dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
81fd7b74 410 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
69354a83
HG
411 }
412
413 if (ep & USB_DIR_IN) {
414 struct buf_packet *isop;
415
e1537884
HG
416 if (dev->endpoint[EP2I(ep)].iso_started &&
417 !dev->endpoint[EP2I(ep)].bufpq_prefilled) {
418 if (dev->endpoint[EP2I(ep)].bufpq_size <
419 dev->endpoint[EP2I(ep)].bufpq_target_size) {
420 return usbredir_handle_status(dev, 0, 0);
421 }
422 dev->endpoint[EP2I(ep)].bufpq_prefilled = 1;
423 }
424
69354a83
HG
425 isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq);
426 if (isop == NULL) {
32213543
HG
427 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
428 ep, dev->endpoint[EP2I(ep)].iso_error);
e1537884
HG
429 /* Re-fill the buffer */
430 dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
69354a83
HG
431 /* Check iso_error for stream errors, otherwise its an underrun */
432 status = dev->endpoint[EP2I(ep)].iso_error;
433 dev->endpoint[EP2I(ep)].iso_error = 0;
434 return usbredir_handle_status(dev, status, 0);
435 }
32213543
HG
436 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep,
437 isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size);
69354a83
HG
438
439 status = isop->status;
440 if (status != usb_redir_success) {
441 bufp_free(dev, isop, ep);
442 return usbredir_handle_status(dev, status, 0);
443 }
444
445 len = isop->len;
4f4321c1 446 if (len > p->iov.size) {
32213543
HG
447 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
448 ep, len, (int)p->iov.size);
69354a83
HG
449 bufp_free(dev, isop, ep);
450 return USB_RET_NAK;
451 }
4f4321c1 452 usb_packet_copy(p, isop->data, len);
69354a83
HG
453 bufp_free(dev, isop, ep);
454 return len;
455 } else {
456 /* If the stream was not started because of a pending error don't
457 send the packet to the usb-host */
458 if (dev->endpoint[EP2I(ep)].iso_started) {
459 struct usb_redir_iso_packet_header iso_packet = {
460 .endpoint = ep,
4f4321c1 461 .length = p->iov.size
69354a83 462 };
4f4321c1 463 uint8_t buf[p->iov.size];
69354a83 464 /* No id, we look at the ep when receiving a status back */
4f4321c1 465 usb_packet_copy(p, buf, p->iov.size);
69354a83 466 usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet,
4f4321c1 467 buf, p->iov.size);
69354a83
HG
468 usbredirparser_do_write(dev->parser);
469 }
470 status = dev->endpoint[EP2I(ep)].iso_error;
471 dev->endpoint[EP2I(ep)].iso_error = 0;
4f4321c1
GH
472 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status,
473 p->iov.size);
474 return usbredir_handle_status(dev, status, p->iov.size);
69354a83
HG
475 }
476}
477
478static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep)
479{
480 struct usb_redir_stop_iso_stream_header stop_iso_stream = {
481 .endpoint = ep
482 };
483 if (dev->endpoint[EP2I(ep)].iso_started) {
484 usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream);
485 DPRINTF("iso stream stopped ep %02X\n", ep);
486 dev->endpoint[EP2I(ep)].iso_started = 0;
487 }
2bd836e5 488 dev->endpoint[EP2I(ep)].iso_error = 0;
69354a83
HG
489 usbredir_free_bufpq(dev, ep);
490}
491
492static int usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
493 uint8_t ep)
494{
495 AsyncURB *aurb = async_alloc(dev, p);
496 struct usb_redir_bulk_packet_header bulk_packet;
497
4f4321c1
GH
498 DPRINTF("bulk-out ep %02X len %zd id %u\n", ep,
499 p->iov.size, aurb->packet_id);
69354a83
HG
500
501 bulk_packet.endpoint = ep;
4f4321c1 502 bulk_packet.length = p->iov.size;
69354a83
HG
503 bulk_packet.stream_id = 0;
504 aurb->bulk_packet = bulk_packet;
505
506 if (ep & USB_DIR_IN) {
507 usbredirparser_send_bulk_packet(dev->parser, aurb->packet_id,
508 &bulk_packet, NULL, 0);
509 } else {
4f4321c1
GH
510 uint8_t buf[p->iov.size];
511 usb_packet_copy(p, buf, p->iov.size);
512 usbredir_log_data(dev, "bulk data out:", buf, p->iov.size);
69354a83 513 usbredirparser_send_bulk_packet(dev->parser, aurb->packet_id,
4f4321c1 514 &bulk_packet, buf, p->iov.size);
69354a83
HG
515 }
516 usbredirparser_do_write(dev->parser);
517 return USB_RET_ASYNC;
518}
519
520static int usbredir_handle_interrupt_data(USBRedirDevice *dev,
521 USBPacket *p, uint8_t ep)
522{
523 if (ep & USB_DIR_IN) {
524 /* Input interrupt endpoint, buffered packet input */
525 struct buf_packet *intp;
526 int status, len;
527
528 if (!dev->endpoint[EP2I(ep)].interrupt_started &&
529 !dev->endpoint[EP2I(ep)].interrupt_error) {
530 struct usb_redir_start_interrupt_receiving_header start_int = {
531 .endpoint = ep,
532 };
533 /* No id, we look at the ep when receiving a status back */
534 usbredirparser_send_start_interrupt_receiving(dev->parser, 0,
535 &start_int);
536 usbredirparser_do_write(dev->parser);
537 DPRINTF("interrupt recv started ep %02X\n", ep);
538 dev->endpoint[EP2I(ep)].interrupt_started = 1;
81fd7b74
HG
539 /* We don't really want to drop interrupt packets ever, but
540 having some upper limit to how much we buffer is good. */
541 dev->endpoint[EP2I(ep)].bufpq_target_size = 1000;
542 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
69354a83
HG
543 }
544
545 intp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq);
546 if (intp == NULL) {
547 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep);
548 /* Check interrupt_error for stream errors */
549 status = dev->endpoint[EP2I(ep)].interrupt_error;
550 dev->endpoint[EP2I(ep)].interrupt_error = 0;
551 return usbredir_handle_status(dev, status, 0);
552 }
553 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep,
554 intp->status, intp->len);
555
556 status = intp->status;
557 if (status != usb_redir_success) {
558 bufp_free(dev, intp, ep);
559 return usbredir_handle_status(dev, status, 0);
560 }
561
562 len = intp->len;
4f4321c1 563 if (len > p->iov.size) {
69354a83
HG
564 ERROR("received int data is larger then packet ep %02X\n", ep);
565 bufp_free(dev, intp, ep);
566 return USB_RET_NAK;
567 }
4f4321c1 568 usb_packet_copy(p, intp->data, len);
69354a83
HG
569 bufp_free(dev, intp, ep);
570 return len;
571 } else {
572 /* Output interrupt endpoint, normal async operation */
573 AsyncURB *aurb = async_alloc(dev, p);
574 struct usb_redir_interrupt_packet_header interrupt_packet;
4f4321c1 575 uint8_t buf[p->iov.size];
69354a83 576
4f4321c1 577 DPRINTF("interrupt-out ep %02X len %zd id %u\n", ep, p->iov.size,
69354a83
HG
578 aurb->packet_id);
579
580 interrupt_packet.endpoint = ep;
4f4321c1 581 interrupt_packet.length = p->iov.size;
69354a83
HG
582 aurb->interrupt_packet = interrupt_packet;
583
4f4321c1
GH
584 usb_packet_copy(p, buf, p->iov.size);
585 usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size);
69354a83 586 usbredirparser_send_interrupt_packet(dev->parser, aurb->packet_id,
4f4321c1 587 &interrupt_packet, buf, p->iov.size);
69354a83
HG
588 usbredirparser_do_write(dev->parser);
589 return USB_RET_ASYNC;
590 }
591}
592
593static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev,
594 uint8_t ep)
595{
596 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = {
597 .endpoint = ep
598 };
599 if (dev->endpoint[EP2I(ep)].interrupt_started) {
600 usbredirparser_send_stop_interrupt_receiving(dev->parser, 0,
601 &stop_interrupt_recv);
602 DPRINTF("interrupt recv stopped ep %02X\n", ep);
603 dev->endpoint[EP2I(ep)].interrupt_started = 0;
604 }
2bd836e5 605 dev->endpoint[EP2I(ep)].interrupt_error = 0;
69354a83
HG
606 usbredir_free_bufpq(dev, ep);
607}
608
609static int usbredir_handle_data(USBDevice *udev, USBPacket *p)
610{
611 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
612 uint8_t ep;
613
079d0b7f 614 ep = p->ep->nr;
69354a83
HG
615 if (p->pid == USB_TOKEN_IN) {
616 ep |= USB_DIR_IN;
617 }
618
619 switch (dev->endpoint[EP2I(ep)].type) {
620 case USB_ENDPOINT_XFER_CONTROL:
621 ERROR("handle_data called for control transfer on ep %02X\n", ep);
622 return USB_RET_NAK;
623 case USB_ENDPOINT_XFER_ISOC:
624 return usbredir_handle_iso_data(dev, p, ep);
625 case USB_ENDPOINT_XFER_BULK:
3a93113a 626 return usbredir_handle_bulk_data(dev, p, ep);
69354a83 627 case USB_ENDPOINT_XFER_INT:
3a93113a 628 return usbredir_handle_interrupt_data(dev, p, ep);
69354a83
HG
629 default:
630 ERROR("handle_data ep %02X has unknown type %d\n", ep,
631 dev->endpoint[EP2I(ep)].type);
632 return USB_RET_NAK;
633 }
634}
635
636static int usbredir_set_config(USBRedirDevice *dev, USBPacket *p,
637 int config)
638{
639 struct usb_redir_set_configuration_header set_config;
640 AsyncURB *aurb = async_alloc(dev, p);
641 int i;
642
643 DPRINTF("set config %d id %u\n", config, aurb->packet_id);
644
645 for (i = 0; i < MAX_ENDPOINTS; i++) {
646 switch (dev->endpoint[i].type) {
647 case USB_ENDPOINT_XFER_ISOC:
648 usbredir_stop_iso_stream(dev, I2EP(i));
649 break;
650 case USB_ENDPOINT_XFER_INT:
651 if (i & 0x10) {
652 usbredir_stop_interrupt_receiving(dev, I2EP(i));
653 }
654 break;
655 }
656 usbredir_free_bufpq(dev, I2EP(i));
657 }
658
659 set_config.configuration = config;
660 usbredirparser_send_set_configuration(dev->parser, aurb->packet_id,
661 &set_config);
662 usbredirparser_do_write(dev->parser);
663 return USB_RET_ASYNC;
664}
665
666static int usbredir_get_config(USBRedirDevice *dev, USBPacket *p)
667{
668 AsyncURB *aurb = async_alloc(dev, p);
669
670 DPRINTF("get config id %u\n", aurb->packet_id);
671
672 aurb->get = 1;
673 usbredirparser_send_get_configuration(dev->parser, aurb->packet_id);
674 usbredirparser_do_write(dev->parser);
675 return USB_RET_ASYNC;
676}
677
678static int usbredir_set_interface(USBRedirDevice *dev, USBPacket *p,
679 int interface, int alt)
680{
681 struct usb_redir_set_alt_setting_header set_alt;
682 AsyncURB *aurb = async_alloc(dev, p);
683 int i;
684
685 DPRINTF("set interface %d alt %d id %u\n", interface, alt,
686 aurb->packet_id);
687
688 for (i = 0; i < MAX_ENDPOINTS; i++) {
689 if (dev->endpoint[i].interface == interface) {
690 switch (dev->endpoint[i].type) {
691 case USB_ENDPOINT_XFER_ISOC:
692 usbredir_stop_iso_stream(dev, I2EP(i));
693 break;
694 case USB_ENDPOINT_XFER_INT:
695 if (i & 0x10) {
696 usbredir_stop_interrupt_receiving(dev, I2EP(i));
697 }
698 break;
699 }
700 usbredir_free_bufpq(dev, I2EP(i));
701 }
702 }
703
704 set_alt.interface = interface;
705 set_alt.alt = alt;
706 usbredirparser_send_set_alt_setting(dev->parser, aurb->packet_id,
707 &set_alt);
708 usbredirparser_do_write(dev->parser);
709 return USB_RET_ASYNC;
710}
711
712static int usbredir_get_interface(USBRedirDevice *dev, USBPacket *p,
713 int interface)
714{
715 struct usb_redir_get_alt_setting_header get_alt;
716 AsyncURB *aurb = async_alloc(dev, p);
717
718 DPRINTF("get interface %d id %u\n", interface, aurb->packet_id);
719
720 get_alt.interface = interface;
721 aurb->get = 1;
722 usbredirparser_send_get_alt_setting(dev->parser, aurb->packet_id,
723 &get_alt);
724 usbredirparser_do_write(dev->parser);
725 return USB_RET_ASYNC;
726}
727
728static int usbredir_handle_control(USBDevice *udev, USBPacket *p,
729 int request, int value, int index, int length, uint8_t *data)
730{
731 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
732 struct usb_redir_control_packet_header control_packet;
733 AsyncURB *aurb;
734
735 /* Special cases for certain standard device requests */
736 switch (request) {
737 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
738 DPRINTF("set address %d\n", value);
739 dev->dev.addr = value;
740 return 0;
741 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
742 return usbredir_set_config(dev, p, value & 0xff);
743 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
744 return usbredir_get_config(dev, p);
745 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
746 return usbredir_set_interface(dev, p, index, value);
747 case InterfaceRequest | USB_REQ_GET_INTERFACE:
748 return usbredir_get_interface(dev, p, index);
749 }
750
751 /* "Normal" ctrl requests */
752 aurb = async_alloc(dev, p);
753
754 /* Note request is (bRequestType << 8) | bRequest */
755 DPRINTF("ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %u\n",
756 request >> 8, request & 0xff, value, index, length,
757 aurb->packet_id);
758
759 control_packet.request = request & 0xFF;
760 control_packet.requesttype = request >> 8;
761 control_packet.endpoint = control_packet.requesttype & USB_DIR_IN;
762 control_packet.value = value;
763 control_packet.index = index;
764 control_packet.length = length;
765 aurb->control_packet = control_packet;
766
767 if (control_packet.requesttype & USB_DIR_IN) {
768 usbredirparser_send_control_packet(dev->parser, aurb->packet_id,
769 &control_packet, NULL, 0);
770 } else {
771 usbredir_log_data(dev, "ctrl data out:", data, length);
772 usbredirparser_send_control_packet(dev->parser, aurb->packet_id,
773 &control_packet, data, length);
774 }
775 usbredirparser_do_write(dev->parser);
776 return USB_RET_ASYNC;
777}
778
779/*
780 * Close events can be triggered by usbredirparser_do_write which gets called
781 * from within the USBDevice data / control packet callbacks and doing a
782 * usb_detach from within these callbacks is not a good idea.
783 *
784 * So we use a bh handler to take care of close events. We also handle
785 * open events from this callback to make sure that a close directly followed
786 * by an open gets handled in the right order.
787 */
788static void usbredir_open_close_bh(void *opaque)
789{
790 USBRedirDevice *dev = opaque;
6af16589 791 uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, };
69354a83
HG
792
793 usbredir_device_disconnect(dev);
794
795 if (dev->parser) {
796 usbredirparser_destroy(dev->parser);
797 dev->parser = NULL;
798 }
799
800 if (dev->cs->opened) {
801 dev->parser = qemu_oom_check(usbredirparser_create());
802 dev->parser->priv = dev;
803 dev->parser->log_func = usbredir_log;
804 dev->parser->read_func = usbredir_read;
805 dev->parser->write_func = usbredir_write;
097a66ef 806 dev->parser->hello_func = usbredir_hello;
69354a83
HG
807 dev->parser->device_connect_func = usbredir_device_connect;
808 dev->parser->device_disconnect_func = usbredir_device_disconnect;
809 dev->parser->interface_info_func = usbredir_interface_info;
810 dev->parser->ep_info_func = usbredir_ep_info;
811 dev->parser->configuration_status_func = usbredir_configuration_status;
812 dev->parser->alt_setting_status_func = usbredir_alt_setting_status;
813 dev->parser->iso_stream_status_func = usbredir_iso_stream_status;
814 dev->parser->interrupt_receiving_status_func =
815 usbredir_interrupt_receiving_status;
816 dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status;
817 dev->parser->control_packet_func = usbredir_control_packet;
818 dev->parser->bulk_packet_func = usbredir_bulk_packet;
819 dev->parser->iso_packet_func = usbredir_iso_packet;
820 dev->parser->interrupt_packet_func = usbredir_interrupt_packet;
821 dev->read_buf = NULL;
822 dev->read_buf_size = 0;
6af16589
HG
823
824 usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version);
097a66ef 825 usbredirparser_caps_set_cap(caps, usb_redir_cap_filter);
6af16589 826 usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE, 0);
69354a83
HG
827 usbredirparser_do_write(dev->parser);
828 }
829}
830
831static void usbredir_do_attach(void *opaque)
832{
833 USBRedirDevice *dev = opaque;
834
835 usb_device_attach(&dev->dev);
836}
837
838/*
839 * chardev callbacks
840 */
841
842static int usbredir_chardev_can_read(void *opaque)
843{
844 USBRedirDevice *dev = opaque;
845
846 if (dev->parser) {
847 /* usbredir_parser_do_read will consume *all* data we give it */
848 return 1024 * 1024;
849 } else {
850 /* usbredir_open_close_bh hasn't handled the open event yet */
851 return 0;
852 }
853}
854
855static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size)
856{
857 USBRedirDevice *dev = opaque;
858
859 /* No recursion allowed! */
860 assert(dev->read_buf == NULL);
861
862 dev->read_buf = buf;
863 dev->read_buf_size = size;
864
865 usbredirparser_do_read(dev->parser);
866 /* Send any acks, etc. which may be queued now */
867 usbredirparser_do_write(dev->parser);
868}
869
870static void usbredir_chardev_event(void *opaque, int event)
871{
872 USBRedirDevice *dev = opaque;
873
874 switch (event) {
875 case CHR_EVENT_OPENED:
876 case CHR_EVENT_CLOSED:
877 qemu_bh_schedule(dev->open_close_bh);
878 break;
879 }
880}
881
882/*
883 * init + destroy
884 */
885
886static int usbredir_initfn(USBDevice *udev)
887{
888 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
889 int i;
890
891 if (dev->cs == NULL) {
892 qerror_report(QERR_MISSING_PARAMETER, "chardev");
893 return -1;
894 }
895
6af16589
HG
896 if (dev->filter_str) {
897 i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|",
898 &dev->filter_rules,
899 &dev->filter_rules_count);
900 if (i) {
901 qerror_report(QERR_INVALID_PARAMETER_VALUE, "filter",
902 "a usb device filter string");
903 return -1;
904 }
905 }
906
69354a83
HG
907 dev->open_close_bh = qemu_bh_new(usbredir_open_close_bh, dev);
908 dev->attach_timer = qemu_new_timer_ms(vm_clock, usbredir_do_attach, dev);
909
910 QTAILQ_INIT(&dev->asyncq);
911 for (i = 0; i < MAX_ENDPOINTS; i++) {
912 QTAILQ_INIT(&dev->endpoint[i].bufpq);
913 }
914
915 /* We'll do the attach once we receive the speed from the usb-host */
916 udev->auto_attach = 0;
917
65f9d986
HG
918 /* Let the backend know we are ready */
919 qemu_chr_fe_open(dev->cs);
69354a83
HG
920 qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read,
921 usbredir_chardev_read, usbredir_chardev_event, dev);
922
923 return 0;
924}
925
926static void usbredir_cleanup_device_queues(USBRedirDevice *dev)
927{
928 AsyncURB *aurb, *next_aurb;
929 int i;
930
931 QTAILQ_FOREACH_SAFE(aurb, &dev->asyncq, next, next_aurb) {
932 async_free(dev, aurb);
933 }
934 for (i = 0; i < MAX_ENDPOINTS; i++) {
935 usbredir_free_bufpq(dev, I2EP(i));
936 }
937}
938
939static void usbredir_handle_destroy(USBDevice *udev)
940{
941 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
942
65f9d986 943 qemu_chr_fe_close(dev->cs);
70f24fb6 944 qemu_chr_delete(dev->cs);
69354a83
HG
945 /* Note must be done after qemu_chr_close, as that causes a close event */
946 qemu_bh_delete(dev->open_close_bh);
947
948 qemu_del_timer(dev->attach_timer);
949 qemu_free_timer(dev->attach_timer);
950
951 usbredir_cleanup_device_queues(dev);
952
953 if (dev->parser) {
954 usbredirparser_destroy(dev->parser);
955 }
6af16589
HG
956
957 free(dev->filter_rules);
958}
959
960static int usbredir_check_filter(USBRedirDevice *dev)
961{
962 if (dev->interface_info.interface_count == 0) {
963 ERROR("No interface info for device\n");
5b3bd682 964 goto error;
6af16589
HG
965 }
966
967 if (dev->filter_rules) {
968 if (!usbredirparser_peer_has_cap(dev->parser,
969 usb_redir_cap_connect_device_version)) {
970 ERROR("Device filter specified and peer does not have the "
971 "connect_device_version capability\n");
5b3bd682 972 goto error;
6af16589
HG
973 }
974
975 if (usbredirfilter_check(
976 dev->filter_rules,
977 dev->filter_rules_count,
978 dev->device_info.device_class,
979 dev->device_info.device_subclass,
980 dev->device_info.device_protocol,
981 dev->interface_info.interface_class,
982 dev->interface_info.interface_subclass,
983 dev->interface_info.interface_protocol,
984 dev->interface_info.interface_count,
985 dev->device_info.vendor_id,
986 dev->device_info.product_id,
987 dev->device_info.device_version_bcd,
988 0) != 0) {
5b3bd682 989 goto error;
6af16589
HG
990 }
991 }
992
993 return 0;
5b3bd682
HG
994
995error:
996 usbredir_device_disconnect(dev);
097a66ef
HG
997 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) {
998 usbredirparser_send_filter_reject(dev->parser);
999 usbredirparser_do_write(dev->parser);
1000 }
5b3bd682 1001 return -1;
69354a83
HG
1002}
1003
1004/*
1005 * usbredirparser packet complete callbacks
1006 */
1007
1008static int usbredir_handle_status(USBRedirDevice *dev,
1009 int status, int actual_len)
1010{
1011 switch (status) {
1012 case usb_redir_success:
1013 return actual_len;
1014 case usb_redir_stall:
1015 return USB_RET_STALL;
1016 case usb_redir_cancelled:
1017 WARNING("returning cancelled packet to HC?\n");
1018 case usb_redir_inval:
1019 case usb_redir_ioerror:
1020 case usb_redir_timeout:
1021 default:
1022 return USB_RET_NAK;
1023 }
1024}
1025
097a66ef
HG
1026static void usbredir_hello(void *priv, struct usb_redir_hello_header *h)
1027{
1028 USBRedirDevice *dev = priv;
1029
1030 /* Try to send the filter info now that we've the usb-host's caps */
1031 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) &&
1032 dev->filter_rules) {
1033 usbredirparser_send_filter_filter(dev->parser, dev->filter_rules,
1034 dev->filter_rules_count);
1035 usbredirparser_do_write(dev->parser);
1036 }
1037}
1038
69354a83
HG
1039static void usbredir_device_connect(void *priv,
1040 struct usb_redir_device_connect_header *device_connect)
1041{
1042 USBRedirDevice *dev = priv;
6af16589 1043 const char *speed;
69354a83 1044
99f08100
HG
1045 if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) {
1046 ERROR("Received device connect while already connected\n");
1047 return;
1048 }
1049
69354a83
HG
1050 switch (device_connect->speed) {
1051 case usb_redir_speed_low:
6af16589 1052 speed = "low speed";
69354a83
HG
1053 dev->dev.speed = USB_SPEED_LOW;
1054 break;
1055 case usb_redir_speed_full:
6af16589 1056 speed = "full speed";
69354a83
HG
1057 dev->dev.speed = USB_SPEED_FULL;
1058 break;
1059 case usb_redir_speed_high:
6af16589 1060 speed = "high speed";
69354a83
HG
1061 dev->dev.speed = USB_SPEED_HIGH;
1062 break;
1063 case usb_redir_speed_super:
6af16589 1064 speed = "super speed";
69354a83
HG
1065 dev->dev.speed = USB_SPEED_SUPER;
1066 break;
1067 default:
6af16589 1068 speed = "unknown speed";
69354a83
HG
1069 dev->dev.speed = USB_SPEED_FULL;
1070 }
6af16589
HG
1071
1072 if (usbredirparser_peer_has_cap(dev->parser,
1073 usb_redir_cap_connect_device_version)) {
1074 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1075 speed, device_connect->vendor_id, device_connect->product_id,
52234bc0
HG
1076 ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 +
1077 ((device_connect->device_version_bcd & 0x0f00) >> 8),
1078 ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 +
1079 ((device_connect->device_version_bcd & 0x000f) >> 0),
6af16589
HG
1080 device_connect->device_class);
1081 } else {
1082 INFO("attaching %s device %04x:%04x class %02x\n", speed,
1083 device_connect->vendor_id, device_connect->product_id,
1084 device_connect->device_class);
1085 }
1086
69354a83 1087 dev->dev.speedmask = (1 << dev->dev.speed);
6af16589
HG
1088 dev->device_info = *device_connect;
1089
1090 if (usbredir_check_filter(dev)) {
1091 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1092 device_connect->vendor_id, device_connect->product_id);
1093 return;
1094 }
1095
69354a83
HG
1096 qemu_mod_timer(dev->attach_timer, dev->next_attach_time);
1097}
1098
1099static void usbredir_device_disconnect(void *priv)
1100{
1101 USBRedirDevice *dev = priv;
99f08100 1102 int i;
69354a83
HG
1103
1104 /* Stop any pending attaches */
1105 qemu_del_timer(dev->attach_timer);
1106
1107 if (dev->dev.attached) {
1108 usb_device_detach(&dev->dev);
69354a83
HG
1109 /*
1110 * Delay next usb device attach to give the guest a chance to see
1111 * see the detach / attach in case of quick close / open succession
1112 */
1113 dev->next_attach_time = qemu_get_clock_ms(vm_clock) + 200;
1114 }
99f08100
HG
1115
1116 /* Reset state so that the next dev connected starts with a clean slate */
1117 usbredir_cleanup_device_queues(dev);
1118 memset(dev->endpoint, 0, sizeof(dev->endpoint));
1119 for (i = 0; i < MAX_ENDPOINTS; i++) {
1120 QTAILQ_INIT(&dev->endpoint[i].bufpq);
1121 }
6af16589 1122 dev->interface_info.interface_count = 0;
69354a83
HG
1123}
1124
1125static void usbredir_interface_info(void *priv,
1126 struct usb_redir_interface_info_header *interface_info)
1127{
6af16589
HG
1128 USBRedirDevice *dev = priv;
1129
1130 dev->interface_info = *interface_info;
1131
1132 /*
1133 * If we receive interface info after the device has already been
1134 * connected (ie on a set_config), re-check the filter.
1135 */
1136 if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) {
1137 if (usbredir_check_filter(dev)) {
1138 ERROR("Device no longer matches filter after interface info "
1139 "change, disconnecting!\n");
6af16589
HG
1140 }
1141 }
69354a83
HG
1142}
1143
1144static void usbredir_ep_info(void *priv,
1145 struct usb_redir_ep_info_header *ep_info)
1146{
1147 USBRedirDevice *dev = priv;
1148 int i;
1149
1150 for (i = 0; i < MAX_ENDPOINTS; i++) {
1151 dev->endpoint[i].type = ep_info->type[i];
1152 dev->endpoint[i].interval = ep_info->interval[i];
1153 dev->endpoint[i].interface = ep_info->interface[i];
e8a7dd29
HG
1154 switch (dev->endpoint[i].type) {
1155 case usb_redir_type_invalid:
1156 break;
1157 case usb_redir_type_iso:
1158 case usb_redir_type_interrupt:
1159 if (dev->endpoint[i].interval == 0) {
1160 ERROR("Received 0 interval for isoc or irq endpoint\n");
1161 usbredir_device_disconnect(dev);
1162 }
1163 /* Fall through */
1164 case usb_redir_type_control:
1165 case usb_redir_type_bulk:
69354a83
HG
1166 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i),
1167 dev->endpoint[i].type, dev->endpoint[i].interface);
e8a7dd29
HG
1168 break;
1169 default:
1170 ERROR("Received invalid endpoint type\n");
1171 usbredir_device_disconnect(dev);
69354a83
HG
1172 }
1173 }
1174}
1175
1176static void usbredir_configuration_status(void *priv, uint32_t id,
1177 struct usb_redir_configuration_status_header *config_status)
1178{
1179 USBRedirDevice *dev = priv;
1180 AsyncURB *aurb;
1181 int len = 0;
1182
1183 DPRINTF("set config status %d config %d id %u\n", config_status->status,
1184 config_status->configuration, id);
1185
1186 aurb = async_find(dev, id);
1187 if (!aurb) {
1188 return;
1189 }
1190 if (aurb->packet) {
1191 if (aurb->get) {
1192 dev->dev.data_buf[0] = config_status->configuration;
1193 len = 1;
1194 }
4f4321c1 1195 aurb->packet->result =
69354a83
HG
1196 usbredir_handle_status(dev, config_status->status, len);
1197 usb_generic_async_ctrl_complete(&dev->dev, aurb->packet);
1198 }
1199 async_free(dev, aurb);
1200}
1201
1202static void usbredir_alt_setting_status(void *priv, uint32_t id,
1203 struct usb_redir_alt_setting_status_header *alt_setting_status)
1204{
1205 USBRedirDevice *dev = priv;
1206 AsyncURB *aurb;
1207 int len = 0;
1208
1209 DPRINTF("alt status %d intf %d alt %d id: %u\n",
1210 alt_setting_status->status,
1211 alt_setting_status->interface,
1212 alt_setting_status->alt, id);
1213
1214 aurb = async_find(dev, id);
1215 if (!aurb) {
1216 return;
1217 }
1218 if (aurb->packet) {
1219 if (aurb->get) {
1220 dev->dev.data_buf[0] = alt_setting_status->alt;
1221 len = 1;
1222 }
4f4321c1 1223 aurb->packet->result =
69354a83
HG
1224 usbredir_handle_status(dev, alt_setting_status->status, len);
1225 usb_generic_async_ctrl_complete(&dev->dev, aurb->packet);
1226 }
1227 async_free(dev, aurb);
1228}
1229
1230static void usbredir_iso_stream_status(void *priv, uint32_t id,
1231 struct usb_redir_iso_stream_status_header *iso_stream_status)
1232{
1233 USBRedirDevice *dev = priv;
1234 uint8_t ep = iso_stream_status->endpoint;
1235
1236 DPRINTF("iso status %d ep %02X id %u\n", iso_stream_status->status,
1237 ep, id);
1238
2bd836e5 1239 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) {
99f08100
HG
1240 return;
1241 }
1242
69354a83
HG
1243 dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status;
1244 if (iso_stream_status->status == usb_redir_stall) {
1245 DPRINTF("iso stream stopped by peer ep %02X\n", ep);
1246 dev->endpoint[EP2I(ep)].iso_started = 0;
1247 }
1248}
1249
1250static void usbredir_interrupt_receiving_status(void *priv, uint32_t id,
1251 struct usb_redir_interrupt_receiving_status_header
1252 *interrupt_receiving_status)
1253{
1254 USBRedirDevice *dev = priv;
1255 uint8_t ep = interrupt_receiving_status->endpoint;
1256
1257 DPRINTF("interrupt recv status %d ep %02X id %u\n",
1258 interrupt_receiving_status->status, ep, id);
1259
2bd836e5 1260 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) {
99f08100
HG
1261 return;
1262 }
1263
69354a83
HG
1264 dev->endpoint[EP2I(ep)].interrupt_error =
1265 interrupt_receiving_status->status;
1266 if (interrupt_receiving_status->status == usb_redir_stall) {
1267 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep);
1268 dev->endpoint[EP2I(ep)].interrupt_started = 0;
1269 }
1270}
1271
1272static void usbredir_bulk_streams_status(void *priv, uint32_t id,
1273 struct usb_redir_bulk_streams_status_header *bulk_streams_status)
1274{
1275}
1276
1277static void usbredir_control_packet(void *priv, uint32_t id,
1278 struct usb_redir_control_packet_header *control_packet,
1279 uint8_t *data, int data_len)
1280{
1281 USBRedirDevice *dev = priv;
1282 int len = control_packet->length;
1283 AsyncURB *aurb;
1284
1285 DPRINTF("ctrl-in status %d len %d id %u\n", control_packet->status,
1286 len, id);
1287
1288 aurb = async_find(dev, id);
1289 if (!aurb) {
1290 free(data);
1291 return;
1292 }
1293
1294 aurb->control_packet.status = control_packet->status;
1295 aurb->control_packet.length = control_packet->length;
1296 if (memcmp(&aurb->control_packet, control_packet,
1297 sizeof(*control_packet))) {
1298 ERROR("return control packet mismatch, please report this!\n");
1299 len = USB_RET_NAK;
1300 }
1301
1302 if (aurb->packet) {
1303 len = usbredir_handle_status(dev, control_packet->status, len);
1304 if (len > 0) {
1305 usbredir_log_data(dev, "ctrl data in:", data, data_len);
1306 if (data_len <= sizeof(dev->dev.data_buf)) {
1307 memcpy(dev->dev.data_buf, data, data_len);
1308 } else {
1309 ERROR("ctrl buffer too small (%d > %zu)\n",
1310 data_len, sizeof(dev->dev.data_buf));
1311 len = USB_RET_STALL;
1312 }
1313 }
4f4321c1 1314 aurb->packet->result = len;
69354a83
HG
1315 usb_generic_async_ctrl_complete(&dev->dev, aurb->packet);
1316 }
1317 async_free(dev, aurb);
1318 free(data);
1319}
1320
1321static void usbredir_bulk_packet(void *priv, uint32_t id,
1322 struct usb_redir_bulk_packet_header *bulk_packet,
1323 uint8_t *data, int data_len)
1324{
1325 USBRedirDevice *dev = priv;
1326 uint8_t ep = bulk_packet->endpoint;
1327 int len = bulk_packet->length;
1328 AsyncURB *aurb;
1329
1330 DPRINTF("bulk-in status %d ep %02X len %d id %u\n", bulk_packet->status,
1331 ep, len, id);
1332
1333 aurb = async_find(dev, id);
1334 if (!aurb) {
1335 free(data);
1336 return;
1337 }
1338
1339 if (aurb->bulk_packet.endpoint != bulk_packet->endpoint ||
1340 aurb->bulk_packet.stream_id != bulk_packet->stream_id) {
1341 ERROR("return bulk packet mismatch, please report this!\n");
1342 len = USB_RET_NAK;
1343 }
1344
1345 if (aurb->packet) {
1346 len = usbredir_handle_status(dev, bulk_packet->status, len);
1347 if (len > 0) {
1348 usbredir_log_data(dev, "bulk data in:", data, data_len);
4f4321c1
GH
1349 if (data_len <= aurb->packet->iov.size) {
1350 usb_packet_copy(aurb->packet, data, data_len);
69354a83 1351 } else {
4f4321c1
GH
1352 ERROR("bulk buffer too small (%d > %zd)\n", data_len,
1353 aurb->packet->iov.size);
69354a83
HG
1354 len = USB_RET_STALL;
1355 }
1356 }
4f4321c1 1357 aurb->packet->result = len;
69354a83
HG
1358 usb_packet_complete(&dev->dev, aurb->packet);
1359 }
1360 async_free(dev, aurb);
1361 free(data);
1362}
1363
1364static void usbredir_iso_packet(void *priv, uint32_t id,
1365 struct usb_redir_iso_packet_header *iso_packet,
1366 uint8_t *data, int data_len)
1367{
1368 USBRedirDevice *dev = priv;
1369 uint8_t ep = iso_packet->endpoint;
1370
1371 DPRINTF2("iso-in status %d ep %02X len %d id %u\n", iso_packet->status, ep,
1372 data_len, id);
1373
1374 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) {
1375 ERROR("received iso packet for non iso endpoint %02X\n", ep);
1376 free(data);
1377 return;
1378 }
1379
1380 if (dev->endpoint[EP2I(ep)].iso_started == 0) {
1381 DPRINTF("received iso packet for non started stream ep %02X\n", ep);
1382 free(data);
1383 return;
1384 }
1385
1386 /* bufp_alloc also adds the packet to the ep queue */
1387 bufp_alloc(dev, data, data_len, iso_packet->status, ep);
1388}
1389
1390static void usbredir_interrupt_packet(void *priv, uint32_t id,
1391 struct usb_redir_interrupt_packet_header *interrupt_packet,
1392 uint8_t *data, int data_len)
1393{
1394 USBRedirDevice *dev = priv;
1395 uint8_t ep = interrupt_packet->endpoint;
1396
1397 DPRINTF("interrupt-in status %d ep %02X len %d id %u\n",
1398 interrupt_packet->status, ep, data_len, id);
1399
1400 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) {
1401 ERROR("received int packet for non interrupt endpoint %02X\n", ep);
1402 free(data);
1403 return;
1404 }
1405
1406 if (ep & USB_DIR_IN) {
1407 if (dev->endpoint[EP2I(ep)].interrupt_started == 0) {
1408 DPRINTF("received int packet while not started ep %02X\n", ep);
1409 free(data);
1410 return;
1411 }
1412
1413 /* bufp_alloc also adds the packet to the ep queue */
1414 bufp_alloc(dev, data, data_len, interrupt_packet->status, ep);
1415 } else {
1416 int len = interrupt_packet->length;
1417
1418 AsyncURB *aurb = async_find(dev, id);
1419 if (!aurb) {
1420 return;
1421 }
1422
1423 if (aurb->interrupt_packet.endpoint != interrupt_packet->endpoint) {
1424 ERROR("return int packet mismatch, please report this!\n");
1425 len = USB_RET_NAK;
1426 }
1427
1428 if (aurb->packet) {
4f4321c1 1429 aurb->packet->result = usbredir_handle_status(dev,
69354a83
HG
1430 interrupt_packet->status, len);
1431 usb_packet_complete(&dev->dev, aurb->packet);
1432 }
1433 async_free(dev, aurb);
1434 }
1435}
1436
3bc36349
AL
1437static Property usbredir_properties[] = {
1438 DEFINE_PROP_CHR("chardev", USBRedirDevice, cs),
1439 DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, 0),
6af16589 1440 DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str),
3bc36349
AL
1441 DEFINE_PROP_END_OF_LIST(),
1442};
1443
62aed765
AL
1444static void usbredir_class_initfn(ObjectClass *klass, void *data)
1445{
1446 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
3bc36349 1447 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
1448
1449 uc->init = usbredir_initfn;
1450 uc->product_desc = "USB Redirection Device";
1451 uc->handle_destroy = usbredir_handle_destroy;
62aed765
AL
1452 uc->cancel_packet = usbredir_cancel_packet;
1453 uc->handle_reset = usbredir_handle_reset;
1454 uc->handle_data = usbredir_handle_data;
1455 uc->handle_control = usbredir_handle_control;
3bc36349 1456 dc->props = usbredir_properties;
62aed765
AL
1457}
1458
3bc36349
AL
1459static TypeInfo usbredir_dev_info = {
1460 .name = "usb-redir",
1461 .parent = TYPE_USB_DEVICE,
1462 .instance_size = sizeof(USBRedirDevice),
1463 .class_init = usbredir_class_initfn,
69354a83
HG
1464};
1465
83f7d43a 1466static void usbredir_register_types(void)
69354a83 1467{
3bc36349 1468 type_register_static(&usbredir_dev_info);
69354a83 1469}
83f7d43a
AF
1470
1471type_init(usbredir_register_types)