]>
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; |
69354a83 HG |
578 | |
579 | if (ep & USB_DIR_IN) { | |
de550a6a | 580 | usbredirparser_send_bulk_packet(dev->parser, p->id, |
69354a83 HG |
581 | &bulk_packet, NULL, 0); |
582 | } else { | |
4f4321c1 GH |
583 | uint8_t buf[p->iov.size]; |
584 | usb_packet_copy(p, buf, p->iov.size); | |
585 | usbredir_log_data(dev, "bulk data out:", buf, p->iov.size); | |
de550a6a | 586 | usbredirparser_send_bulk_packet(dev->parser, p->id, |
4f4321c1 | 587 | &bulk_packet, buf, p->iov.size); |
69354a83 HG |
588 | } |
589 | usbredirparser_do_write(dev->parser); | |
590 | return USB_RET_ASYNC; | |
591 | } | |
592 | ||
593 | static int usbredir_handle_interrupt_data(USBRedirDevice *dev, | |
594 | USBPacket *p, uint8_t ep) | |
595 | { | |
596 | if (ep & USB_DIR_IN) { | |
597 | /* Input interrupt endpoint, buffered packet input */ | |
598 | struct buf_packet *intp; | |
599 | int status, len; | |
600 | ||
601 | if (!dev->endpoint[EP2I(ep)].interrupt_started && | |
602 | !dev->endpoint[EP2I(ep)].interrupt_error) { | |
603 | struct usb_redir_start_interrupt_receiving_header start_int = { | |
604 | .endpoint = ep, | |
605 | }; | |
606 | /* No id, we look at the ep when receiving a status back */ | |
607 | usbredirparser_send_start_interrupt_receiving(dev->parser, 0, | |
608 | &start_int); | |
609 | usbredirparser_do_write(dev->parser); | |
610 | DPRINTF("interrupt recv started ep %02X\n", ep); | |
611 | dev->endpoint[EP2I(ep)].interrupt_started = 1; | |
81fd7b74 HG |
612 | /* We don't really want to drop interrupt packets ever, but |
613 | having some upper limit to how much we buffer is good. */ | |
614 | dev->endpoint[EP2I(ep)].bufpq_target_size = 1000; | |
615 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; | |
69354a83 HG |
616 | } |
617 | ||
618 | intp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); | |
619 | if (intp == NULL) { | |
620 | DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep); | |
621 | /* Check interrupt_error for stream errors */ | |
622 | status = dev->endpoint[EP2I(ep)].interrupt_error; | |
623 | dev->endpoint[EP2I(ep)].interrupt_error = 0; | |
e6472210 HG |
624 | if (status) { |
625 | return usbredir_handle_status(dev, status, 0); | |
626 | } | |
627 | return USB_RET_NAK; | |
69354a83 HG |
628 | } |
629 | DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep, | |
630 | intp->status, intp->len); | |
631 | ||
632 | status = intp->status; | |
633 | if (status != usb_redir_success) { | |
634 | bufp_free(dev, intp, ep); | |
635 | return usbredir_handle_status(dev, status, 0); | |
636 | } | |
637 | ||
638 | len = intp->len; | |
4f4321c1 | 639 | if (len > p->iov.size) { |
69354a83 HG |
640 | ERROR("received int data is larger then packet ep %02X\n", ep); |
641 | bufp_free(dev, intp, ep); | |
4d819a9b | 642 | return USB_RET_BABBLE; |
69354a83 | 643 | } |
4f4321c1 | 644 | usb_packet_copy(p, intp->data, len); |
69354a83 HG |
645 | bufp_free(dev, intp, ep); |
646 | return len; | |
647 | } else { | |
648 | /* Output interrupt endpoint, normal async operation */ | |
69354a83 | 649 | struct usb_redir_interrupt_packet_header interrupt_packet; |
4f4321c1 | 650 | uint8_t buf[p->iov.size]; |
69354a83 | 651 | |
de550a6a HG |
652 | DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep, |
653 | p->iov.size, p->id); | |
69354a83 | 654 | |
9a8d4067 HG |
655 | if (usbredir_already_in_flight(dev, p->id)) { |
656 | return USB_RET_ASYNC; | |
657 | } | |
658 | ||
69354a83 | 659 | interrupt_packet.endpoint = ep; |
4f4321c1 | 660 | interrupt_packet.length = p->iov.size; |
69354a83 | 661 | |
4f4321c1 GH |
662 | usb_packet_copy(p, buf, p->iov.size); |
663 | usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size); | |
de550a6a | 664 | usbredirparser_send_interrupt_packet(dev->parser, p->id, |
4f4321c1 | 665 | &interrupt_packet, buf, p->iov.size); |
69354a83 HG |
666 | usbredirparser_do_write(dev->parser); |
667 | return USB_RET_ASYNC; | |
668 | } | |
669 | } | |
670 | ||
671 | static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev, | |
672 | uint8_t ep) | |
673 | { | |
674 | struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = { | |
675 | .endpoint = ep | |
676 | }; | |
677 | if (dev->endpoint[EP2I(ep)].interrupt_started) { | |
678 | usbredirparser_send_stop_interrupt_receiving(dev->parser, 0, | |
679 | &stop_interrupt_recv); | |
680 | DPRINTF("interrupt recv stopped ep %02X\n", ep); | |
681 | dev->endpoint[EP2I(ep)].interrupt_started = 0; | |
682 | } | |
2bd836e5 | 683 | dev->endpoint[EP2I(ep)].interrupt_error = 0; |
69354a83 HG |
684 | usbredir_free_bufpq(dev, ep); |
685 | } | |
686 | ||
687 | static int usbredir_handle_data(USBDevice *udev, USBPacket *p) | |
688 | { | |
689 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
690 | uint8_t ep; | |
691 | ||
079d0b7f | 692 | ep = p->ep->nr; |
69354a83 HG |
693 | if (p->pid == USB_TOKEN_IN) { |
694 | ep |= USB_DIR_IN; | |
695 | } | |
696 | ||
697 | switch (dev->endpoint[EP2I(ep)].type) { | |
698 | case USB_ENDPOINT_XFER_CONTROL: | |
699 | ERROR("handle_data called for control transfer on ep %02X\n", ep); | |
700 | return USB_RET_NAK; | |
701 | case USB_ENDPOINT_XFER_ISOC: | |
702 | return usbredir_handle_iso_data(dev, p, ep); | |
703 | case USB_ENDPOINT_XFER_BULK: | |
3a93113a | 704 | return usbredir_handle_bulk_data(dev, p, ep); |
69354a83 | 705 | case USB_ENDPOINT_XFER_INT: |
3a93113a | 706 | return usbredir_handle_interrupt_data(dev, p, ep); |
69354a83 HG |
707 | default: |
708 | ERROR("handle_data ep %02X has unknown type %d\n", ep, | |
709 | dev->endpoint[EP2I(ep)].type); | |
710 | return USB_RET_NAK; | |
711 | } | |
712 | } | |
713 | ||
714 | static int usbredir_set_config(USBRedirDevice *dev, USBPacket *p, | |
715 | int config) | |
716 | { | |
717 | struct usb_redir_set_configuration_header set_config; | |
69354a83 HG |
718 | int i; |
719 | ||
de550a6a | 720 | DPRINTF("set config %d id %"PRIu64"\n", config, p->id); |
69354a83 HG |
721 | |
722 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
723 | switch (dev->endpoint[i].type) { | |
724 | case USB_ENDPOINT_XFER_ISOC: | |
725 | usbredir_stop_iso_stream(dev, I2EP(i)); | |
726 | break; | |
727 | case USB_ENDPOINT_XFER_INT: | |
728 | if (i & 0x10) { | |
729 | usbredir_stop_interrupt_receiving(dev, I2EP(i)); | |
730 | } | |
731 | break; | |
732 | } | |
733 | usbredir_free_bufpq(dev, I2EP(i)); | |
734 | } | |
735 | ||
736 | set_config.configuration = config; | |
de550a6a | 737 | usbredirparser_send_set_configuration(dev->parser, p->id, &set_config); |
69354a83 HG |
738 | usbredirparser_do_write(dev->parser); |
739 | return USB_RET_ASYNC; | |
740 | } | |
741 | ||
742 | static int usbredir_get_config(USBRedirDevice *dev, USBPacket *p) | |
743 | { | |
de550a6a | 744 | DPRINTF("get config id %"PRIu64"\n", p->id); |
69354a83 | 745 | |
de550a6a | 746 | usbredirparser_send_get_configuration(dev->parser, p->id); |
69354a83 HG |
747 | usbredirparser_do_write(dev->parser); |
748 | return USB_RET_ASYNC; | |
749 | } | |
750 | ||
751 | static int usbredir_set_interface(USBRedirDevice *dev, USBPacket *p, | |
752 | int interface, int alt) | |
753 | { | |
754 | struct usb_redir_set_alt_setting_header set_alt; | |
69354a83 HG |
755 | int i; |
756 | ||
de550a6a | 757 | DPRINTF("set interface %d alt %d id %"PRIu64"\n", interface, alt, p->id); |
69354a83 HG |
758 | |
759 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
760 | if (dev->endpoint[i].interface == interface) { | |
761 | switch (dev->endpoint[i].type) { | |
762 | case USB_ENDPOINT_XFER_ISOC: | |
763 | usbredir_stop_iso_stream(dev, I2EP(i)); | |
764 | break; | |
765 | case USB_ENDPOINT_XFER_INT: | |
766 | if (i & 0x10) { | |
767 | usbredir_stop_interrupt_receiving(dev, I2EP(i)); | |
768 | } | |
769 | break; | |
770 | } | |
771 | usbredir_free_bufpq(dev, I2EP(i)); | |
772 | } | |
773 | } | |
774 | ||
775 | set_alt.interface = interface; | |
776 | set_alt.alt = alt; | |
de550a6a | 777 | usbredirparser_send_set_alt_setting(dev->parser, p->id, &set_alt); |
69354a83 HG |
778 | usbredirparser_do_write(dev->parser); |
779 | return USB_RET_ASYNC; | |
780 | } | |
781 | ||
782 | static int usbredir_get_interface(USBRedirDevice *dev, USBPacket *p, | |
783 | int interface) | |
784 | { | |
785 | struct usb_redir_get_alt_setting_header get_alt; | |
69354a83 | 786 | |
de550a6a | 787 | DPRINTF("get interface %d id %"PRIu64"\n", interface, p->id); |
69354a83 HG |
788 | |
789 | get_alt.interface = interface; | |
de550a6a | 790 | usbredirparser_send_get_alt_setting(dev->parser, p->id, &get_alt); |
69354a83 HG |
791 | usbredirparser_do_write(dev->parser); |
792 | return USB_RET_ASYNC; | |
793 | } | |
794 | ||
795 | static int usbredir_handle_control(USBDevice *udev, USBPacket *p, | |
796 | int request, int value, int index, int length, uint8_t *data) | |
797 | { | |
798 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
799 | struct usb_redir_control_packet_header control_packet; | |
69354a83 | 800 | |
9a8d4067 HG |
801 | if (usbredir_already_in_flight(dev, p->id)) { |
802 | return USB_RET_ASYNC; | |
803 | } | |
804 | ||
69354a83 HG |
805 | /* Special cases for certain standard device requests */ |
806 | switch (request) { | |
807 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: | |
808 | DPRINTF("set address %d\n", value); | |
809 | dev->dev.addr = value; | |
810 | return 0; | |
811 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: | |
812 | return usbredir_set_config(dev, p, value & 0xff); | |
813 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: | |
814 | return usbredir_get_config(dev, p); | |
815 | case InterfaceOutRequest | USB_REQ_SET_INTERFACE: | |
816 | return usbredir_set_interface(dev, p, index, value); | |
817 | case InterfaceRequest | USB_REQ_GET_INTERFACE: | |
818 | return usbredir_get_interface(dev, p, index); | |
819 | } | |
820 | ||
de550a6a HG |
821 | /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */ |
822 | DPRINTF( | |
823 | "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64"\n", | |
824 | request >> 8, request & 0xff, value, index, length, p->id); | |
69354a83 HG |
825 | |
826 | control_packet.request = request & 0xFF; | |
827 | control_packet.requesttype = request >> 8; | |
828 | control_packet.endpoint = control_packet.requesttype & USB_DIR_IN; | |
829 | control_packet.value = value; | |
830 | control_packet.index = index; | |
831 | control_packet.length = length; | |
69354a83 HG |
832 | |
833 | if (control_packet.requesttype & USB_DIR_IN) { | |
de550a6a | 834 | usbredirparser_send_control_packet(dev->parser, p->id, |
69354a83 HG |
835 | &control_packet, NULL, 0); |
836 | } else { | |
837 | usbredir_log_data(dev, "ctrl data out:", data, length); | |
de550a6a | 838 | usbredirparser_send_control_packet(dev->parser, p->id, |
69354a83 HG |
839 | &control_packet, data, length); |
840 | } | |
841 | usbredirparser_do_write(dev->parser); | |
842 | return USB_RET_ASYNC; | |
843 | } | |
844 | ||
845 | /* | |
846 | * Close events can be triggered by usbredirparser_do_write which gets called | |
847 | * from within the USBDevice data / control packet callbacks and doing a | |
848 | * usb_detach from within these callbacks is not a good idea. | |
849 | * | |
ed9873bf | 850 | * So we use a bh handler to take care of close events. |
69354a83 | 851 | */ |
ed9873bf | 852 | static void usbredir_chardev_close_bh(void *opaque) |
69354a83 HG |
853 | { |
854 | USBRedirDevice *dev = opaque; | |
855 | ||
856 | usbredir_device_disconnect(dev); | |
857 | ||
858 | if (dev->parser) { | |
09054d19 | 859 | DPRINTF("destroying usbredirparser\n"); |
69354a83 HG |
860 | usbredirparser_destroy(dev->parser); |
861 | dev->parser = NULL; | |
862 | } | |
ed9873bf | 863 | } |
69354a83 | 864 | |
dbbf0195 | 865 | static void usbredir_create_parser(USBRedirDevice *dev) |
ed9873bf HG |
866 | { |
867 | uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, }; | |
fc3f6e1b | 868 | int flags = 0; |
6af16589 | 869 | |
09054d19 HG |
870 | DPRINTF("creating usbredirparser\n"); |
871 | ||
ed9873bf HG |
872 | dev->parser = qemu_oom_check(usbredirparser_create()); |
873 | dev->parser->priv = dev; | |
874 | dev->parser->log_func = usbredir_log; | |
875 | dev->parser->read_func = usbredir_read; | |
876 | dev->parser->write_func = usbredir_write; | |
877 | dev->parser->hello_func = usbredir_hello; | |
878 | dev->parser->device_connect_func = usbredir_device_connect; | |
879 | dev->parser->device_disconnect_func = usbredir_device_disconnect; | |
880 | dev->parser->interface_info_func = usbredir_interface_info; | |
881 | dev->parser->ep_info_func = usbredir_ep_info; | |
882 | dev->parser->configuration_status_func = usbredir_configuration_status; | |
883 | dev->parser->alt_setting_status_func = usbredir_alt_setting_status; | |
884 | dev->parser->iso_stream_status_func = usbredir_iso_stream_status; | |
885 | dev->parser->interrupt_receiving_status_func = | |
886 | usbredir_interrupt_receiving_status; | |
887 | dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status; | |
888 | dev->parser->control_packet_func = usbredir_control_packet; | |
889 | dev->parser->bulk_packet_func = usbredir_bulk_packet; | |
890 | dev->parser->iso_packet_func = usbredir_iso_packet; | |
891 | dev->parser->interrupt_packet_func = usbredir_interrupt_packet; | |
892 | dev->read_buf = NULL; | |
893 | dev->read_buf_size = 0; | |
894 | ||
895 | usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version); | |
896 | usbredirparser_caps_set_cap(caps, usb_redir_cap_filter); | |
0fde3b7a | 897 | usbredirparser_caps_set_cap(caps, usb_redir_cap_ep_info_max_packet_size); |
be4a8928 | 898 | usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids); |
fc3f6e1b HG |
899 | |
900 | if (runstate_check(RUN_STATE_INMIGRATE)) { | |
901 | flags |= usbredirparser_fl_no_hello; | |
902 | } | |
35efba2c | 903 | usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE, |
fc3f6e1b | 904 | flags); |
ed9873bf | 905 | usbredirparser_do_write(dev->parser); |
69354a83 HG |
906 | } |
907 | ||
910c1e6b HG |
908 | static void usbredir_reject_device(USBRedirDevice *dev) |
909 | { | |
910 | usbredir_device_disconnect(dev); | |
911 | if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) { | |
912 | usbredirparser_send_filter_reject(dev->parser); | |
913 | usbredirparser_do_write(dev->parser); | |
914 | } | |
915 | } | |
916 | ||
69354a83 HG |
917 | static void usbredir_do_attach(void *opaque) |
918 | { | |
919 | USBRedirDevice *dev = opaque; | |
920 | ||
a508cc42 HG |
921 | /* In order to work properly with XHCI controllers we need these caps */ |
922 | if ((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER) && !( | |
923 | usbredirparser_peer_has_cap(dev->parser, | |
924 | usb_redir_cap_ep_info_max_packet_size) && | |
925 | usbredirparser_peer_has_cap(dev->parser, | |
926 | usb_redir_cap_64bits_ids))) { | |
927 | ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n"); | |
928 | usbredir_reject_device(dev); | |
929 | return; | |
930 | } | |
931 | ||
714f9db0 | 932 | if (usb_device_attach(&dev->dev) != 0) { |
910c1e6b | 933 | usbredir_reject_device(dev); |
714f9db0 | 934 | } |
69354a83 HG |
935 | } |
936 | ||
937 | /* | |
938 | * chardev callbacks | |
939 | */ | |
940 | ||
941 | static int usbredir_chardev_can_read(void *opaque) | |
942 | { | |
943 | USBRedirDevice *dev = opaque; | |
944 | ||
ed9873bf HG |
945 | if (!dev->parser) { |
946 | WARNING("chardev_can_read called on non open chardev!\n"); | |
69354a83 HG |
947 | return 0; |
948 | } | |
ed9873bf | 949 | |
fc3f6e1b HG |
950 | /* Don't read new data from the chardev until our state is fully synced */ |
951 | if (!runstate_check(RUN_STATE_RUNNING)) { | |
952 | return 0; | |
953 | } | |
954 | ||
ed9873bf HG |
955 | /* usbredir_parser_do_read will consume *all* data we give it */ |
956 | return 1024 * 1024; | |
69354a83 HG |
957 | } |
958 | ||
959 | static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size) | |
960 | { | |
961 | USBRedirDevice *dev = opaque; | |
962 | ||
963 | /* No recursion allowed! */ | |
964 | assert(dev->read_buf == NULL); | |
965 | ||
966 | dev->read_buf = buf; | |
967 | dev->read_buf_size = size; | |
968 | ||
969 | usbredirparser_do_read(dev->parser); | |
970 | /* Send any acks, etc. which may be queued now */ | |
971 | usbredirparser_do_write(dev->parser); | |
972 | } | |
973 | ||
974 | static void usbredir_chardev_event(void *opaque, int event) | |
975 | { | |
976 | USBRedirDevice *dev = opaque; | |
977 | ||
978 | switch (event) { | |
979 | case CHR_EVENT_OPENED: | |
09054d19 | 980 | DPRINTF("chardev open\n"); |
dbbf0195 HG |
981 | /* Make sure any pending closes are handled (no-op if none pending) */ |
982 | usbredir_chardev_close_bh(dev); | |
983 | qemu_bh_cancel(dev->chardev_close_bh); | |
984 | usbredir_create_parser(dev); | |
ed9873bf | 985 | break; |
69354a83 | 986 | case CHR_EVENT_CLOSED: |
09054d19 | 987 | DPRINTF("chardev close\n"); |
ed9873bf | 988 | qemu_bh_schedule(dev->chardev_close_bh); |
69354a83 HG |
989 | break; |
990 | } | |
991 | } | |
992 | ||
993 | /* | |
994 | * init + destroy | |
995 | */ | |
996 | ||
fc3f6e1b HG |
997 | static void usbredir_vm_state_change(void *priv, int running, RunState state) |
998 | { | |
999 | USBRedirDevice *dev = priv; | |
1000 | ||
1001 | if (state == RUN_STATE_RUNNING && dev->parser != NULL) { | |
1002 | usbredirparser_do_write(dev->parser); /* Flush any pending writes */ | |
1003 | } | |
1004 | } | |
1005 | ||
69354a83 HG |
1006 | static int usbredir_initfn(USBDevice *udev) |
1007 | { | |
1008 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
1009 | int i; | |
1010 | ||
1011 | if (dev->cs == NULL) { | |
1012 | qerror_report(QERR_MISSING_PARAMETER, "chardev"); | |
1013 | return -1; | |
1014 | } | |
1015 | ||
6af16589 HG |
1016 | if (dev->filter_str) { |
1017 | i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|", | |
1018 | &dev->filter_rules, | |
1019 | &dev->filter_rules_count); | |
1020 | if (i) { | |
1021 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "filter", | |
1022 | "a usb device filter string"); | |
1023 | return -1; | |
1024 | } | |
1025 | } | |
1026 | ||
ed9873bf | 1027 | dev->chardev_close_bh = qemu_bh_new(usbredir_chardev_close_bh, dev); |
69354a83 HG |
1028 | dev->attach_timer = qemu_new_timer_ms(vm_clock, usbredir_do_attach, dev); |
1029 | ||
8e60452a | 1030 | packet_id_queue_init(&dev->cancelled, dev, "cancelled"); |
9a8d4067 | 1031 | packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight"); |
69354a83 HG |
1032 | for (i = 0; i < MAX_ENDPOINTS; i++) { |
1033 | QTAILQ_INIT(&dev->endpoint[i].bufpq); | |
1034 | } | |
1035 | ||
1036 | /* We'll do the attach once we receive the speed from the usb-host */ | |
1037 | udev->auto_attach = 0; | |
1038 | ||
65f9d986 HG |
1039 | /* Let the backend know we are ready */ |
1040 | qemu_chr_fe_open(dev->cs); | |
69354a83 HG |
1041 | qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read, |
1042 | usbredir_chardev_read, usbredir_chardev_event, dev); | |
1043 | ||
fc3f6e1b | 1044 | qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev); |
65bb3a5c | 1045 | add_boot_device_path(dev->bootindex, &udev->qdev, NULL); |
69354a83 HG |
1046 | return 0; |
1047 | } | |
1048 | ||
1049 | static void usbredir_cleanup_device_queues(USBRedirDevice *dev) | |
1050 | { | |
69354a83 HG |
1051 | int i; |
1052 | ||
8e60452a | 1053 | packet_id_queue_empty(&dev->cancelled); |
9a8d4067 | 1054 | packet_id_queue_empty(&dev->already_in_flight); |
69354a83 HG |
1055 | for (i = 0; i < MAX_ENDPOINTS; i++) { |
1056 | usbredir_free_bufpq(dev, I2EP(i)); | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | static void usbredir_handle_destroy(USBDevice *udev) | |
1061 | { | |
1062 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
1063 | ||
65f9d986 | 1064 | qemu_chr_fe_close(dev->cs); |
70f24fb6 | 1065 | qemu_chr_delete(dev->cs); |
69354a83 | 1066 | /* Note must be done after qemu_chr_close, as that causes a close event */ |
ed9873bf | 1067 | qemu_bh_delete(dev->chardev_close_bh); |
69354a83 HG |
1068 | |
1069 | qemu_del_timer(dev->attach_timer); | |
1070 | qemu_free_timer(dev->attach_timer); | |
1071 | ||
1072 | usbredir_cleanup_device_queues(dev); | |
1073 | ||
1074 | if (dev->parser) { | |
1075 | usbredirparser_destroy(dev->parser); | |
1076 | } | |
6af16589 HG |
1077 | |
1078 | free(dev->filter_rules); | |
1079 | } | |
1080 | ||
1081 | static int usbredir_check_filter(USBRedirDevice *dev) | |
1082 | { | |
1510168e | 1083 | if (dev->interface_info.interface_count == NO_INTERFACE_INFO) { |
6af16589 | 1084 | ERROR("No interface info for device\n"); |
5b3bd682 | 1085 | goto error; |
6af16589 HG |
1086 | } |
1087 | ||
1088 | if (dev->filter_rules) { | |
1089 | if (!usbredirparser_peer_has_cap(dev->parser, | |
1090 | usb_redir_cap_connect_device_version)) { | |
1091 | ERROR("Device filter specified and peer does not have the " | |
1092 | "connect_device_version capability\n"); | |
5b3bd682 | 1093 | goto error; |
6af16589 HG |
1094 | } |
1095 | ||
1096 | if (usbredirfilter_check( | |
1097 | dev->filter_rules, | |
1098 | dev->filter_rules_count, | |
1099 | dev->device_info.device_class, | |
1100 | dev->device_info.device_subclass, | |
1101 | dev->device_info.device_protocol, | |
1102 | dev->interface_info.interface_class, | |
1103 | dev->interface_info.interface_subclass, | |
1104 | dev->interface_info.interface_protocol, | |
1105 | dev->interface_info.interface_count, | |
1106 | dev->device_info.vendor_id, | |
1107 | dev->device_info.product_id, | |
1108 | dev->device_info.device_version_bcd, | |
1109 | 0) != 0) { | |
5b3bd682 | 1110 | goto error; |
6af16589 HG |
1111 | } |
1112 | } | |
1113 | ||
1114 | return 0; | |
5b3bd682 HG |
1115 | |
1116 | error: | |
910c1e6b | 1117 | usbredir_reject_device(dev); |
5b3bd682 | 1118 | return -1; |
69354a83 HG |
1119 | } |
1120 | ||
1121 | /* | |
1122 | * usbredirparser packet complete callbacks | |
1123 | */ | |
1124 | ||
1125 | static int usbredir_handle_status(USBRedirDevice *dev, | |
1126 | int status, int actual_len) | |
1127 | { | |
1128 | switch (status) { | |
1129 | case usb_redir_success: | |
1130 | return actual_len; | |
1131 | case usb_redir_stall: | |
1132 | return USB_RET_STALL; | |
1133 | case usb_redir_cancelled: | |
18113340 HG |
1134 | /* |
1135 | * When the usbredir-host unredirects a device, it will report a status | |
1136 | * of cancelled for all pending packets, followed by a disconnect msg. | |
1137 | */ | |
1138 | return USB_RET_IOERROR; | |
69354a83 | 1139 | case usb_redir_inval: |
d61000a8 | 1140 | WARNING("got invalid param error from usb-host?\n"); |
18113340 | 1141 | return USB_RET_IOERROR; |
adae502c HG |
1142 | case usb_redir_babble: |
1143 | return USB_RET_BABBLE; | |
69354a83 HG |
1144 | case usb_redir_ioerror: |
1145 | case usb_redir_timeout: | |
1146 | default: | |
d61000a8 | 1147 | return USB_RET_IOERROR; |
69354a83 HG |
1148 | } |
1149 | } | |
1150 | ||
097a66ef HG |
1151 | static void usbredir_hello(void *priv, struct usb_redir_hello_header *h) |
1152 | { | |
1153 | USBRedirDevice *dev = priv; | |
1154 | ||
1155 | /* Try to send the filter info now that we've the usb-host's caps */ | |
1156 | if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) && | |
1157 | dev->filter_rules) { | |
1158 | usbredirparser_send_filter_filter(dev->parser, dev->filter_rules, | |
1159 | dev->filter_rules_count); | |
1160 | usbredirparser_do_write(dev->parser); | |
1161 | } | |
1162 | } | |
1163 | ||
69354a83 HG |
1164 | static void usbredir_device_connect(void *priv, |
1165 | struct usb_redir_device_connect_header *device_connect) | |
1166 | { | |
1167 | USBRedirDevice *dev = priv; | |
6af16589 | 1168 | const char *speed; |
69354a83 | 1169 | |
99f08100 HG |
1170 | if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) { |
1171 | ERROR("Received device connect while already connected\n"); | |
1172 | return; | |
1173 | } | |
1174 | ||
69354a83 HG |
1175 | switch (device_connect->speed) { |
1176 | case usb_redir_speed_low: | |
6af16589 | 1177 | speed = "low speed"; |
69354a83 HG |
1178 | dev->dev.speed = USB_SPEED_LOW; |
1179 | break; | |
1180 | case usb_redir_speed_full: | |
6af16589 | 1181 | speed = "full speed"; |
69354a83 HG |
1182 | dev->dev.speed = USB_SPEED_FULL; |
1183 | break; | |
1184 | case usb_redir_speed_high: | |
6af16589 | 1185 | speed = "high speed"; |
69354a83 HG |
1186 | dev->dev.speed = USB_SPEED_HIGH; |
1187 | break; | |
1188 | case usb_redir_speed_super: | |
6af16589 | 1189 | speed = "super speed"; |
69354a83 HG |
1190 | dev->dev.speed = USB_SPEED_SUPER; |
1191 | break; | |
1192 | default: | |
6af16589 | 1193 | speed = "unknown speed"; |
69354a83 HG |
1194 | dev->dev.speed = USB_SPEED_FULL; |
1195 | } | |
6af16589 HG |
1196 | |
1197 | if (usbredirparser_peer_has_cap(dev->parser, | |
1198 | usb_redir_cap_connect_device_version)) { | |
1199 | INFO("attaching %s device %04x:%04x version %d.%d class %02x\n", | |
1200 | speed, device_connect->vendor_id, device_connect->product_id, | |
52234bc0 HG |
1201 | ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 + |
1202 | ((device_connect->device_version_bcd & 0x0f00) >> 8), | |
1203 | ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 + | |
1204 | ((device_connect->device_version_bcd & 0x000f) >> 0), | |
6af16589 HG |
1205 | device_connect->device_class); |
1206 | } else { | |
1207 | INFO("attaching %s device %04x:%04x class %02x\n", speed, | |
1208 | device_connect->vendor_id, device_connect->product_id, | |
1209 | device_connect->device_class); | |
1210 | } | |
1211 | ||
69354a83 | 1212 | dev->dev.speedmask = (1 << dev->dev.speed); |
6af16589 HG |
1213 | dev->device_info = *device_connect; |
1214 | ||
1215 | if (usbredir_check_filter(dev)) { | |
1216 | WARNING("Device %04x:%04x rejected by device filter, not attaching\n", | |
1217 | device_connect->vendor_id, device_connect->product_id); | |
1218 | return; | |
1219 | } | |
1220 | ||
69354a83 HG |
1221 | qemu_mod_timer(dev->attach_timer, dev->next_attach_time); |
1222 | } | |
1223 | ||
1224 | static void usbredir_device_disconnect(void *priv) | |
1225 | { | |
1226 | USBRedirDevice *dev = priv; | |
99f08100 | 1227 | int i; |
69354a83 HG |
1228 | |
1229 | /* Stop any pending attaches */ | |
1230 | qemu_del_timer(dev->attach_timer); | |
1231 | ||
1232 | if (dev->dev.attached) { | |
09054d19 | 1233 | DPRINTF("detaching device\n"); |
69354a83 | 1234 | usb_device_detach(&dev->dev); |
69354a83 HG |
1235 | /* |
1236 | * Delay next usb device attach to give the guest a chance to see | |
1237 | * see the detach / attach in case of quick close / open succession | |
1238 | */ | |
1239 | dev->next_attach_time = qemu_get_clock_ms(vm_clock) + 200; | |
1240 | } | |
99f08100 HG |
1241 | |
1242 | /* Reset state so that the next dev connected starts with a clean slate */ | |
1243 | usbredir_cleanup_device_queues(dev); | |
1244 | memset(dev->endpoint, 0, sizeof(dev->endpoint)); | |
1245 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
1246 | QTAILQ_INIT(&dev->endpoint[i].bufpq); | |
1247 | } | |
0454b611 | 1248 | usb_ep_init(&dev->dev); |
1510168e | 1249 | dev->interface_info.interface_count = NO_INTERFACE_INFO; |
a0625c56 HG |
1250 | dev->dev.addr = 0; |
1251 | dev->dev.speed = 0; | |
69354a83 HG |
1252 | } |
1253 | ||
1254 | static void usbredir_interface_info(void *priv, | |
1255 | struct usb_redir_interface_info_header *interface_info) | |
1256 | { | |
6af16589 HG |
1257 | USBRedirDevice *dev = priv; |
1258 | ||
1259 | dev->interface_info = *interface_info; | |
1260 | ||
1261 | /* | |
1262 | * If we receive interface info after the device has already been | |
1263 | * connected (ie on a set_config), re-check the filter. | |
1264 | */ | |
1265 | if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) { | |
1266 | if (usbredir_check_filter(dev)) { | |
1267 | ERROR("Device no longer matches filter after interface info " | |
1268 | "change, disconnecting!\n"); | |
6af16589 HG |
1269 | } |
1270 | } | |
69354a83 HG |
1271 | } |
1272 | ||
1273 | static void usbredir_ep_info(void *priv, | |
1274 | struct usb_redir_ep_info_header *ep_info) | |
1275 | { | |
1276 | USBRedirDevice *dev = priv; | |
0454b611 | 1277 | struct USBEndpoint *usb_ep; |
69354a83 HG |
1278 | int i; |
1279 | ||
1280 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
1281 | dev->endpoint[i].type = ep_info->type[i]; | |
1282 | dev->endpoint[i].interval = ep_info->interval[i]; | |
1283 | dev->endpoint[i].interface = ep_info->interface[i]; | |
e8a7dd29 HG |
1284 | switch (dev->endpoint[i].type) { |
1285 | case usb_redir_type_invalid: | |
1286 | break; | |
1287 | case usb_redir_type_iso: | |
1288 | case usb_redir_type_interrupt: | |
1289 | if (dev->endpoint[i].interval == 0) { | |
1290 | ERROR("Received 0 interval for isoc or irq endpoint\n"); | |
1291 | usbredir_device_disconnect(dev); | |
1292 | } | |
1293 | /* Fall through */ | |
1294 | case usb_redir_type_control: | |
1295 | case usb_redir_type_bulk: | |
69354a83 HG |
1296 | DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i), |
1297 | dev->endpoint[i].type, dev->endpoint[i].interface); | |
e8a7dd29 HG |
1298 | break; |
1299 | default: | |
1300 | ERROR("Received invalid endpoint type\n"); | |
1301 | usbredir_device_disconnect(dev); | |
0454b611 | 1302 | return; |
69354a83 | 1303 | } |
0454b611 HG |
1304 | usb_ep = usb_ep_get(&dev->dev, |
1305 | (i & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, | |
1306 | i & 0x0f); | |
1307 | usb_ep->type = dev->endpoint[i].type; | |
1308 | usb_ep->ifnum = dev->endpoint[i].interface; | |
0fde3b7a HG |
1309 | if (usbredirparser_peer_has_cap(dev->parser, |
1310 | usb_redir_cap_ep_info_max_packet_size)) { | |
3f4be328 HG |
1311 | dev->endpoint[i].max_packet_size = |
1312 | usb_ep->max_packet_size = ep_info->max_packet_size[i]; | |
0fde3b7a | 1313 | } |
6c67446a HG |
1314 | if (ep_info->type[i] == usb_redir_type_bulk) { |
1315 | usb_ep->pipeline = true; | |
1316 | } | |
69354a83 HG |
1317 | } |
1318 | } | |
1319 | ||
be4a8928 | 1320 | static void usbredir_configuration_status(void *priv, uint64_t id, |
69354a83 HG |
1321 | struct usb_redir_configuration_status_header *config_status) |
1322 | { | |
1323 | USBRedirDevice *dev = priv; | |
de550a6a | 1324 | USBPacket *p; |
69354a83 HG |
1325 | int len = 0; |
1326 | ||
be4a8928 HG |
1327 | DPRINTF("set config status %d config %d id %"PRIu64"\n", |
1328 | config_status->status, config_status->configuration, id); | |
69354a83 | 1329 | |
de550a6a HG |
1330 | p = usbredir_find_packet_by_id(dev, 0, id); |
1331 | if (p) { | |
cb897117 | 1332 | if (dev->dev.setup_buf[0] & USB_DIR_IN) { |
69354a83 HG |
1333 | dev->dev.data_buf[0] = config_status->configuration; |
1334 | len = 1; | |
1335 | } | |
de550a6a HG |
1336 | p->result = usbredir_handle_status(dev, config_status->status, len); |
1337 | usb_generic_async_ctrl_complete(&dev->dev, p); | |
69354a83 | 1338 | } |
69354a83 HG |
1339 | } |
1340 | ||
be4a8928 | 1341 | static void usbredir_alt_setting_status(void *priv, uint64_t id, |
69354a83 HG |
1342 | struct usb_redir_alt_setting_status_header *alt_setting_status) |
1343 | { | |
1344 | USBRedirDevice *dev = priv; | |
de550a6a | 1345 | USBPacket *p; |
69354a83 HG |
1346 | int len = 0; |
1347 | ||
be4a8928 HG |
1348 | DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n", |
1349 | alt_setting_status->status, alt_setting_status->interface, | |
69354a83 HG |
1350 | alt_setting_status->alt, id); |
1351 | ||
de550a6a HG |
1352 | p = usbredir_find_packet_by_id(dev, 0, id); |
1353 | if (p) { | |
cb897117 | 1354 | if (dev->dev.setup_buf[0] & USB_DIR_IN) { |
69354a83 HG |
1355 | dev->dev.data_buf[0] = alt_setting_status->alt; |
1356 | len = 1; | |
1357 | } | |
de550a6a | 1358 | p->result = |
69354a83 | 1359 | usbredir_handle_status(dev, alt_setting_status->status, len); |
de550a6a | 1360 | usb_generic_async_ctrl_complete(&dev->dev, p); |
69354a83 | 1361 | } |
69354a83 HG |
1362 | } |
1363 | ||
be4a8928 | 1364 | static void usbredir_iso_stream_status(void *priv, uint64_t id, |
69354a83 HG |
1365 | struct usb_redir_iso_stream_status_header *iso_stream_status) |
1366 | { | |
1367 | USBRedirDevice *dev = priv; | |
1368 | uint8_t ep = iso_stream_status->endpoint; | |
1369 | ||
be4a8928 | 1370 | DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status, |
69354a83 HG |
1371 | ep, id); |
1372 | ||
2bd836e5 | 1373 | if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) { |
99f08100 HG |
1374 | return; |
1375 | } | |
1376 | ||
69354a83 HG |
1377 | dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status; |
1378 | if (iso_stream_status->status == usb_redir_stall) { | |
1379 | DPRINTF("iso stream stopped by peer ep %02X\n", ep); | |
1380 | dev->endpoint[EP2I(ep)].iso_started = 0; | |
1381 | } | |
1382 | } | |
1383 | ||
be4a8928 | 1384 | static void usbredir_interrupt_receiving_status(void *priv, uint64_t id, |
69354a83 HG |
1385 | struct usb_redir_interrupt_receiving_status_header |
1386 | *interrupt_receiving_status) | |
1387 | { | |
1388 | USBRedirDevice *dev = priv; | |
1389 | uint8_t ep = interrupt_receiving_status->endpoint; | |
1390 | ||
be4a8928 | 1391 | DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n", |
69354a83 HG |
1392 | interrupt_receiving_status->status, ep, id); |
1393 | ||
2bd836e5 | 1394 | if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) { |
99f08100 HG |
1395 | return; |
1396 | } | |
1397 | ||
69354a83 HG |
1398 | dev->endpoint[EP2I(ep)].interrupt_error = |
1399 | interrupt_receiving_status->status; | |
1400 | if (interrupt_receiving_status->status == usb_redir_stall) { | |
1401 | DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep); | |
1402 | dev->endpoint[EP2I(ep)].interrupt_started = 0; | |
1403 | } | |
1404 | } | |
1405 | ||
be4a8928 | 1406 | static void usbredir_bulk_streams_status(void *priv, uint64_t id, |
69354a83 HG |
1407 | struct usb_redir_bulk_streams_status_header *bulk_streams_status) |
1408 | { | |
1409 | } | |
1410 | ||
be4a8928 | 1411 | static void usbredir_control_packet(void *priv, uint64_t id, |
69354a83 HG |
1412 | struct usb_redir_control_packet_header *control_packet, |
1413 | uint8_t *data, int data_len) | |
1414 | { | |
1415 | USBRedirDevice *dev = priv; | |
de550a6a | 1416 | USBPacket *p; |
69354a83 | 1417 | int len = control_packet->length; |
69354a83 | 1418 | |
be4a8928 | 1419 | DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status, |
69354a83 HG |
1420 | len, id); |
1421 | ||
de550a6a HG |
1422 | p = usbredir_find_packet_by_id(dev, 0, id); |
1423 | if (p) { | |
69354a83 HG |
1424 | len = usbredir_handle_status(dev, control_packet->status, len); |
1425 | if (len > 0) { | |
1426 | usbredir_log_data(dev, "ctrl data in:", data, data_len); | |
1427 | if (data_len <= sizeof(dev->dev.data_buf)) { | |
1428 | memcpy(dev->dev.data_buf, data, data_len); | |
1429 | } else { | |
1430 | ERROR("ctrl buffer too small (%d > %zu)\n", | |
1431 | data_len, sizeof(dev->dev.data_buf)); | |
1432 | len = USB_RET_STALL; | |
1433 | } | |
1434 | } | |
de550a6a HG |
1435 | p->result = len; |
1436 | usb_generic_async_ctrl_complete(&dev->dev, p); | |
69354a83 | 1437 | } |
69354a83 HG |
1438 | free(data); |
1439 | } | |
1440 | ||
be4a8928 | 1441 | static void usbredir_bulk_packet(void *priv, uint64_t id, |
69354a83 HG |
1442 | struct usb_redir_bulk_packet_header *bulk_packet, |
1443 | uint8_t *data, int data_len) | |
1444 | { | |
1445 | USBRedirDevice *dev = priv; | |
1446 | uint8_t ep = bulk_packet->endpoint; | |
1447 | int len = bulk_packet->length; | |
de550a6a | 1448 | USBPacket *p; |
69354a83 | 1449 | |
be4a8928 HG |
1450 | DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64"\n", |
1451 | bulk_packet->status, ep, len, id); | |
69354a83 | 1452 | |
de550a6a HG |
1453 | p = usbredir_find_packet_by_id(dev, ep, id); |
1454 | if (p) { | |
69354a83 HG |
1455 | len = usbredir_handle_status(dev, bulk_packet->status, len); |
1456 | if (len > 0) { | |
1457 | usbredir_log_data(dev, "bulk data in:", data, data_len); | |
de550a6a HG |
1458 | if (data_len <= p->iov.size) { |
1459 | usb_packet_copy(p, data, data_len); | |
69354a83 | 1460 | } else { |
2979a361 HG |
1461 | ERROR("bulk got more data then requested (%d > %zd)\n", |
1462 | data_len, p->iov.size); | |
1463 | len = USB_RET_BABBLE; | |
69354a83 HG |
1464 | } |
1465 | } | |
de550a6a HG |
1466 | p->result = len; |
1467 | usb_packet_complete(&dev->dev, p); | |
69354a83 | 1468 | } |
69354a83 HG |
1469 | free(data); |
1470 | } | |
1471 | ||
be4a8928 | 1472 | static void usbredir_iso_packet(void *priv, uint64_t id, |
69354a83 HG |
1473 | struct usb_redir_iso_packet_header *iso_packet, |
1474 | uint8_t *data, int data_len) | |
1475 | { | |
1476 | USBRedirDevice *dev = priv; | |
1477 | uint8_t ep = iso_packet->endpoint; | |
1478 | ||
be4a8928 HG |
1479 | DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n", |
1480 | iso_packet->status, ep, data_len, id); | |
69354a83 HG |
1481 | |
1482 | if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) { | |
1483 | ERROR("received iso packet for non iso endpoint %02X\n", ep); | |
1484 | free(data); | |
1485 | return; | |
1486 | } | |
1487 | ||
1488 | if (dev->endpoint[EP2I(ep)].iso_started == 0) { | |
1489 | DPRINTF("received iso packet for non started stream ep %02X\n", ep); | |
1490 | free(data); | |
1491 | return; | |
1492 | } | |
1493 | ||
1494 | /* bufp_alloc also adds the packet to the ep queue */ | |
1495 | bufp_alloc(dev, data, data_len, iso_packet->status, ep); | |
1496 | } | |
1497 | ||
be4a8928 | 1498 | static void usbredir_interrupt_packet(void *priv, uint64_t id, |
69354a83 HG |
1499 | struct usb_redir_interrupt_packet_header *interrupt_packet, |
1500 | uint8_t *data, int data_len) | |
1501 | { | |
1502 | USBRedirDevice *dev = priv; | |
1503 | uint8_t ep = interrupt_packet->endpoint; | |
1504 | ||
be4a8928 | 1505 | DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n", |
69354a83 HG |
1506 | interrupt_packet->status, ep, data_len, id); |
1507 | ||
1508 | if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) { | |
1509 | ERROR("received int packet for non interrupt endpoint %02X\n", ep); | |
1510 | free(data); | |
1511 | return; | |
1512 | } | |
1513 | ||
1514 | if (ep & USB_DIR_IN) { | |
1515 | if (dev->endpoint[EP2I(ep)].interrupt_started == 0) { | |
1516 | DPRINTF("received int packet while not started ep %02X\n", ep); | |
1517 | free(data); | |
1518 | return; | |
1519 | } | |
1520 | ||
1521 | /* bufp_alloc also adds the packet to the ep queue */ | |
1522 | bufp_alloc(dev, data, data_len, interrupt_packet->status, ep); | |
1523 | } else { | |
1524 | int len = interrupt_packet->length; | |
1525 | ||
de550a6a HG |
1526 | USBPacket *p = usbredir_find_packet_by_id(dev, ep, id); |
1527 | if (p) { | |
1528 | p->result = usbredir_handle_status(dev, | |
69354a83 | 1529 | interrupt_packet->status, len); |
de550a6a | 1530 | usb_packet_complete(&dev->dev, p); |
69354a83 | 1531 | } |
69354a83 HG |
1532 | } |
1533 | } | |
1534 | ||
fc3f6e1b HG |
1535 | /* |
1536 | * Migration code | |
1537 | */ | |
1538 | ||
1539 | static void usbredir_pre_save(void *priv) | |
1540 | { | |
1541 | USBRedirDevice *dev = priv; | |
1542 | ||
1543 | usbredir_fill_already_in_flight(dev); | |
1544 | } | |
1545 | ||
1546 | static int usbredir_post_load(void *priv, int version_id) | |
1547 | { | |
1548 | USBRedirDevice *dev = priv; | |
1549 | struct USBEndpoint *usb_ep; | |
1550 | int i; | |
1551 | ||
1552 | switch (dev->device_info.speed) { | |
1553 | case usb_redir_speed_low: | |
1554 | dev->dev.speed = USB_SPEED_LOW; | |
1555 | break; | |
1556 | case usb_redir_speed_full: | |
1557 | dev->dev.speed = USB_SPEED_FULL; | |
1558 | break; | |
1559 | case usb_redir_speed_high: | |
1560 | dev->dev.speed = USB_SPEED_HIGH; | |
1561 | break; | |
1562 | case usb_redir_speed_super: | |
1563 | dev->dev.speed = USB_SPEED_SUPER; | |
1564 | break; | |
1565 | default: | |
1566 | dev->dev.speed = USB_SPEED_FULL; | |
1567 | } | |
1568 | dev->dev.speedmask = (1 << dev->dev.speed); | |
1569 | ||
1570 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
1571 | usb_ep = usb_ep_get(&dev->dev, | |
1572 | (i & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, | |
1573 | i & 0x0f); | |
1574 | usb_ep->type = dev->endpoint[i].type; | |
1575 | usb_ep->ifnum = dev->endpoint[i].interface; | |
1576 | usb_ep->max_packet_size = dev->endpoint[i].max_packet_size; | |
1577 | if (dev->endpoint[i].type == usb_redir_type_bulk) { | |
1578 | usb_ep->pipeline = true; | |
1579 | } | |
1580 | } | |
1581 | return 0; | |
1582 | } | |
1583 | ||
1584 | /* For usbredirparser migration */ | |
1585 | static void usbredir_put_parser(QEMUFile *f, void *priv, size_t unused) | |
1586 | { | |
1587 | USBRedirDevice *dev = priv; | |
1588 | uint8_t *data; | |
1589 | int len; | |
1590 | ||
1591 | if (dev->parser == NULL) { | |
1592 | qemu_put_be32(f, 0); | |
1593 | return; | |
1594 | } | |
1595 | ||
1596 | usbredirparser_serialize(dev->parser, &data, &len); | |
1597 | qemu_oom_check(data); | |
1598 | ||
1599 | qemu_put_be32(f, len); | |
1600 | qemu_put_buffer(f, data, len); | |
1601 | ||
1602 | free(data); | |
1603 | } | |
1604 | ||
1605 | static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused) | |
1606 | { | |
1607 | USBRedirDevice *dev = priv; | |
1608 | uint8_t *data; | |
1609 | int len, ret; | |
1610 | ||
1611 | len = qemu_get_be32(f); | |
1612 | if (len == 0) { | |
1613 | return 0; | |
1614 | } | |
1615 | ||
1616 | /* | |
5c16f767 HG |
1617 | * If our chardev is not open already at this point the usbredir connection |
1618 | * has been broken (non seamless migration, or restore from disk). | |
1619 | * | |
1620 | * In this case create a temporary parser to receive the migration data, | |
1621 | * and schedule the close_bh to report the device as disconnected to the | |
1622 | * guest and to destroy the parser again. | |
fc3f6e1b HG |
1623 | */ |
1624 | if (dev->parser == NULL) { | |
5c16f767 HG |
1625 | WARNING("usb-redir connection broken during migration\n"); |
1626 | usbredir_create_parser(dev); | |
1627 | qemu_bh_schedule(dev->chardev_close_bh); | |
fc3f6e1b HG |
1628 | } |
1629 | ||
1630 | data = g_malloc(len); | |
1631 | qemu_get_buffer(f, data, len); | |
1632 | ||
1633 | ret = usbredirparser_unserialize(dev->parser, data, len); | |
1634 | ||
1635 | g_free(data); | |
1636 | ||
1637 | return ret; | |
1638 | } | |
1639 | ||
1640 | static const VMStateInfo usbredir_parser_vmstate_info = { | |
1641 | .name = "usb-redir-parser", | |
1642 | .put = usbredir_put_parser, | |
1643 | .get = usbredir_get_parser, | |
1644 | }; | |
1645 | ||
1646 | ||
1647 | /* For buffered packets (iso/irq) queue migration */ | |
1648 | static void usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused) | |
1649 | { | |
1650 | struct endp_data *endp = priv; | |
1651 | struct buf_packet *bufp; | |
1652 | int remain = endp->bufpq_size; | |
1653 | ||
1654 | qemu_put_be32(f, endp->bufpq_size); | |
1655 | QTAILQ_FOREACH(bufp, &endp->bufpq, next) { | |
1656 | qemu_put_be32(f, bufp->len); | |
1657 | qemu_put_be32(f, bufp->status); | |
1658 | qemu_put_buffer(f, bufp->data, bufp->len); | |
1659 | remain--; | |
1660 | } | |
1661 | assert(remain == 0); | |
1662 | } | |
1663 | ||
1664 | static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused) | |
1665 | { | |
1666 | struct endp_data *endp = priv; | |
1667 | struct buf_packet *bufp; | |
1668 | int i; | |
1669 | ||
1670 | endp->bufpq_size = qemu_get_be32(f); | |
1671 | for (i = 0; i < endp->bufpq_size; i++) { | |
1672 | bufp = g_malloc(sizeof(struct buf_packet)); | |
1673 | bufp->len = qemu_get_be32(f); | |
1674 | bufp->status = qemu_get_be32(f); | |
1675 | bufp->data = qemu_oom_check(malloc(bufp->len)); /* regular malloc! */ | |
1676 | qemu_get_buffer(f, bufp->data, bufp->len); | |
1677 | QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next); | |
1678 | } | |
1679 | return 0; | |
1680 | } | |
1681 | ||
1682 | static const VMStateInfo usbredir_ep_bufpq_vmstate_info = { | |
1683 | .name = "usb-redir-bufpq", | |
1684 | .put = usbredir_put_bufpq, | |
1685 | .get = usbredir_get_bufpq, | |
1686 | }; | |
1687 | ||
1688 | ||
1689 | /* For endp_data migration */ | |
1690 | static const VMStateDescription usbredir_ep_vmstate = { | |
1691 | .name = "usb-redir-ep", | |
1692 | .version_id = 1, | |
1693 | .minimum_version_id = 1, | |
1694 | .fields = (VMStateField[]) { | |
1695 | VMSTATE_UINT8(type, struct endp_data), | |
1696 | VMSTATE_UINT8(interval, struct endp_data), | |
1697 | VMSTATE_UINT8(interface, struct endp_data), | |
1698 | VMSTATE_UINT16(max_packet_size, struct endp_data), | |
1699 | VMSTATE_UINT8(iso_started, struct endp_data), | |
1700 | VMSTATE_UINT8(iso_error, struct endp_data), | |
1701 | VMSTATE_UINT8(interrupt_started, struct endp_data), | |
1702 | VMSTATE_UINT8(interrupt_error, struct endp_data), | |
1703 | VMSTATE_UINT8(bufpq_prefilled, struct endp_data), | |
1704 | VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data), | |
1705 | { | |
1706 | .name = "bufpq", | |
1707 | .version_id = 0, | |
1708 | .field_exists = NULL, | |
1709 | .size = 0, | |
1710 | .info = &usbredir_ep_bufpq_vmstate_info, | |
1711 | .flags = VMS_SINGLE, | |
1712 | .offset = 0, | |
1713 | }, | |
1714 | VMSTATE_INT32(bufpq_target_size, struct endp_data), | |
1715 | VMSTATE_END_OF_LIST() | |
1716 | } | |
1717 | }; | |
1718 | ||
1719 | ||
1720 | /* For PacketIdQueue migration */ | |
1721 | static void usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused) | |
1722 | { | |
1723 | struct PacketIdQueue *q = priv; | |
1724 | USBRedirDevice *dev = q->dev; | |
1725 | struct PacketIdQueueEntry *e; | |
1726 | int remain = q->size; | |
1727 | ||
1728 | DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size); | |
1729 | qemu_put_be32(f, q->size); | |
1730 | QTAILQ_FOREACH(e, &q->head, next) { | |
1731 | qemu_put_be64(f, e->id); | |
1732 | remain--; | |
1733 | } | |
1734 | assert(remain == 0); | |
1735 | } | |
1736 | ||
1737 | static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused) | |
1738 | { | |
1739 | struct PacketIdQueue *q = priv; | |
1740 | USBRedirDevice *dev = q->dev; | |
1741 | int i, size; | |
1742 | uint64_t id; | |
1743 | ||
1744 | size = qemu_get_be32(f); | |
1745 | DPRINTF("get_packet_id_q %s size %d\n", q->name, size); | |
1746 | for (i = 0; i < size; i++) { | |
1747 | id = qemu_get_be64(f); | |
1748 | packet_id_queue_add(q, id); | |
1749 | } | |
1750 | assert(q->size == size); | |
1751 | return 0; | |
1752 | } | |
1753 | ||
1754 | static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = { | |
1755 | .name = "usb-redir-packet-id-q", | |
1756 | .put = usbredir_put_packet_id_q, | |
1757 | .get = usbredir_get_packet_id_q, | |
1758 | }; | |
1759 | ||
1760 | static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = { | |
1761 | .name = "usb-redir-packet-id-queue", | |
1762 | .version_id = 1, | |
1763 | .minimum_version_id = 1, | |
1764 | .fields = (VMStateField[]) { | |
1765 | { | |
1766 | .name = "queue", | |
1767 | .version_id = 0, | |
1768 | .field_exists = NULL, | |
1769 | .size = 0, | |
1770 | .info = &usbredir_ep_packet_id_q_vmstate_info, | |
1771 | .flags = VMS_SINGLE, | |
1772 | .offset = 0, | |
1773 | }, | |
1774 | VMSTATE_END_OF_LIST() | |
1775 | } | |
1776 | }; | |
1777 | ||
1778 | ||
1779 | /* For usb_redir_device_connect_header migration */ | |
1780 | static const VMStateDescription usbredir_device_info_vmstate = { | |
1781 | .name = "usb-redir-device-info", | |
1782 | .version_id = 1, | |
1783 | .minimum_version_id = 1, | |
1784 | .fields = (VMStateField[]) { | |
1785 | VMSTATE_UINT8(speed, struct usb_redir_device_connect_header), | |
1786 | VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header), | |
1787 | VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header), | |
1788 | VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header), | |
1789 | VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header), | |
1790 | VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header), | |
1791 | VMSTATE_UINT16(device_version_bcd, | |
1792 | struct usb_redir_device_connect_header), | |
1793 | VMSTATE_END_OF_LIST() | |
1794 | } | |
1795 | }; | |
1796 | ||
1797 | ||
1798 | /* For usb_redir_interface_info_header migration */ | |
1799 | static const VMStateDescription usbredir_interface_info_vmstate = { | |
1800 | .name = "usb-redir-interface-info", | |
1801 | .version_id = 1, | |
1802 | .minimum_version_id = 1, | |
1803 | .fields = (VMStateField[]) { | |
1804 | VMSTATE_UINT32(interface_count, | |
1805 | struct usb_redir_interface_info_header), | |
1806 | VMSTATE_UINT8_ARRAY(interface, | |
1807 | struct usb_redir_interface_info_header, 32), | |
1808 | VMSTATE_UINT8_ARRAY(interface_class, | |
1809 | struct usb_redir_interface_info_header, 32), | |
1810 | VMSTATE_UINT8_ARRAY(interface_subclass, | |
1811 | struct usb_redir_interface_info_header, 32), | |
1812 | VMSTATE_UINT8_ARRAY(interface_protocol, | |
1813 | struct usb_redir_interface_info_header, 32), | |
1814 | VMSTATE_END_OF_LIST() | |
1815 | } | |
1816 | }; | |
1817 | ||
1818 | ||
1819 | /* And finally the USBRedirDevice vmstate itself */ | |
1820 | static const VMStateDescription usbredir_vmstate = { | |
1821 | .name = "usb-redir", | |
1822 | .version_id = 1, | |
1823 | .minimum_version_id = 1, | |
1824 | .pre_save = usbredir_pre_save, | |
1825 | .post_load = usbredir_post_load, | |
1826 | .fields = (VMStateField[]) { | |
1827 | VMSTATE_USB_DEVICE(dev, USBRedirDevice), | |
1828 | VMSTATE_TIMER(attach_timer, USBRedirDevice), | |
1829 | { | |
1830 | .name = "parser", | |
1831 | .version_id = 0, | |
1832 | .field_exists = NULL, | |
1833 | .size = 0, | |
1834 | .info = &usbredir_parser_vmstate_info, | |
1835 | .flags = VMS_SINGLE, | |
1836 | .offset = 0, | |
1837 | }, | |
1838 | VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1, | |
1839 | usbredir_ep_vmstate, struct endp_data), | |
1840 | VMSTATE_STRUCT(cancelled, USBRedirDevice, 1, | |
1841 | usbredir_ep_packet_id_queue_vmstate, | |
1842 | struct PacketIdQueue), | |
1843 | VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1, | |
1844 | usbredir_ep_packet_id_queue_vmstate, | |
1845 | struct PacketIdQueue), | |
1846 | VMSTATE_STRUCT(device_info, USBRedirDevice, 1, | |
1847 | usbredir_device_info_vmstate, | |
1848 | struct usb_redir_device_connect_header), | |
1849 | VMSTATE_STRUCT(interface_info, USBRedirDevice, 1, | |
1850 | usbredir_interface_info_vmstate, | |
1851 | struct usb_redir_interface_info_header), | |
1852 | VMSTATE_END_OF_LIST() | |
1853 | } | |
1854 | }; | |
1855 | ||
3bc36349 AL |
1856 | static Property usbredir_properties[] = { |
1857 | DEFINE_PROP_CHR("chardev", USBRedirDevice, cs), | |
1858 | DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, 0), | |
6af16589 | 1859 | DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str), |
65bb3a5c | 1860 | DEFINE_PROP_INT32("bootindex", USBRedirDevice, bootindex, -1), |
3bc36349 AL |
1861 | DEFINE_PROP_END_OF_LIST(), |
1862 | }; | |
1863 | ||
62aed765 AL |
1864 | static void usbredir_class_initfn(ObjectClass *klass, void *data) |
1865 | { | |
1866 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
3bc36349 | 1867 | DeviceClass *dc = DEVICE_CLASS(klass); |
62aed765 AL |
1868 | |
1869 | uc->init = usbredir_initfn; | |
1870 | uc->product_desc = "USB Redirection Device"; | |
1871 | uc->handle_destroy = usbredir_handle_destroy; | |
62aed765 AL |
1872 | uc->cancel_packet = usbredir_cancel_packet; |
1873 | uc->handle_reset = usbredir_handle_reset; | |
1874 | uc->handle_data = usbredir_handle_data; | |
1875 | uc->handle_control = usbredir_handle_control; | |
fc3f6e1b | 1876 | dc->vmsd = &usbredir_vmstate; |
3bc36349 | 1877 | dc->props = usbredir_properties; |
62aed765 AL |
1878 | } |
1879 | ||
3bc36349 AL |
1880 | static TypeInfo usbredir_dev_info = { |
1881 | .name = "usb-redir", | |
1882 | .parent = TYPE_USB_DEVICE, | |
1883 | .instance_size = sizeof(USBRedirDevice), | |
1884 | .class_init = usbredir_class_initfn, | |
69354a83 HG |
1885 | }; |
1886 | ||
83f7d43a | 1887 | static void usbredir_register_types(void) |
69354a83 | 1888 | { |
3bc36349 | 1889 | type_register_static(&usbredir_dev_info); |
69354a83 | 1890 | } |
83f7d43a AF |
1891 | |
1892 | type_init(usbredir_register_types) |