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