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