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