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