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