]>
Commit | Line | Data |
---|---|---|
69354a83 HG |
1 | /* |
2 | * USB redirector usb-guest | |
3 | * | |
cb897117 | 4 | * Copyright (c) 2011-2012 Red Hat, Inc. |
69354a83 HG |
5 | * |
6 | * Red Hat Authors: | |
7 | * Hans de Goede <[email protected]> | |
8 | * | |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
25 | * THE SOFTWARE. | |
26 | */ | |
27 | ||
28 | #include "qemu-common.h" | |
1de7afc9 | 29 | #include "qemu/timer.h" |
83c9089e | 30 | #include "monitor/monitor.h" |
9c17d615 | 31 | #include "sysemu/sysemu.h" |
1de7afc9 | 32 | #include "qemu/iov.h" |
dccfcd0e | 33 | #include "sysemu/char.h" |
69354a83 HG |
34 | |
35 | #include <dirent.h> | |
36 | #include <sys/ioctl.h> | |
37 | #include <signal.h> | |
38 | #include <usbredirparser.h> | |
6af16589 | 39 | #include <usbredirfilter.h> |
69354a83 HG |
40 | |
41 | #include "hw/usb.h" | |
42 | ||
43 | #define MAX_ENDPOINTS 32 | |
1510168e | 44 | #define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */ |
69354a83 HG |
45 | #define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f)) |
46 | #define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f)) | |
7e9638d3 HG |
47 | #define USBEP2I(usb_ep) (((usb_ep)->pid == USB_TOKEN_IN) ? \ |
48 | ((usb_ep)->nr | 0x10) : ((usb_ep)->nr)) | |
49 | #define I2USBEP(d, i) (usb_ep_get(&(d)->dev, \ | |
50 | ((i) & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, \ | |
51 | (i) & 0x0f)) | |
69354a83 | 52 | |
19e83931 HG |
53 | #ifndef USBREDIR_VERSION /* This is not defined in older usbredir versions */ |
54 | #define USBREDIR_VERSION 0 | |
55 | #endif | |
56 | ||
69354a83 HG |
57 | typedef struct USBRedirDevice USBRedirDevice; |
58 | ||
b2d1fe67 | 59 | /* Struct to hold buffered packets */ |
69354a83 HG |
60 | struct buf_packet { |
61 | uint8_t *data; | |
b2d1fe67 HG |
62 | void *free_on_destroy; |
63 | uint16_t len; | |
64 | uint16_t offset; | |
65 | uint8_t status; | |
69354a83 HG |
66 | QTAILQ_ENTRY(buf_packet)next; |
67 | }; | |
68 | ||
69 | struct endp_data { | |
e97f0aca | 70 | USBRedirDevice *dev; |
69354a83 HG |
71 | uint8_t type; |
72 | uint8_t interval; | |
73 | uint8_t interface; /* bInterfaceNumber this ep belongs to */ | |
3f4be328 | 74 | uint16_t max_packet_size; /* In bytes, not wMaxPacketSize format !! */ |
19e83931 | 75 | uint32_t max_streams; |
69354a83 HG |
76 | uint8_t iso_started; |
77 | uint8_t iso_error; /* For reporting iso errors to the HC */ | |
78 | uint8_t interrupt_started; | |
79 | uint8_t interrupt_error; | |
b2d1fe67 HG |
80 | uint8_t bulk_receiving_enabled; |
81 | uint8_t bulk_receiving_started; | |
e1537884 | 82 | uint8_t bufpq_prefilled; |
81fd7b74 | 83 | uint8_t bufpq_dropping_packets; |
69354a83 | 84 | QTAILQ_HEAD(, buf_packet) bufpq; |
fc3f6e1b HG |
85 | int32_t bufpq_size; |
86 | int32_t bufpq_target_size; | |
b2d1fe67 | 87 | USBPacket *pending_async_packet; |
69354a83 HG |
88 | }; |
89 | ||
8e60452a HG |
90 | struct PacketIdQueueEntry { |
91 | uint64_t id; | |
92 | QTAILQ_ENTRY(PacketIdQueueEntry)next; | |
93 | }; | |
94 | ||
95 | struct PacketIdQueue { | |
96 | USBRedirDevice *dev; | |
97 | const char *name; | |
98 | QTAILQ_HEAD(, PacketIdQueueEntry) head; | |
99 | int size; | |
100 | }; | |
101 | ||
69354a83 HG |
102 | struct USBRedirDevice { |
103 | USBDevice dev; | |
104 | /* Properties */ | |
105 | CharDriverState *cs; | |
106 | uint8_t debug; | |
6af16589 | 107 | char *filter_str; |
65bb3a5c | 108 | int32_t bootindex; |
69354a83 HG |
109 | /* Data passed from chardev the fd_read cb to the usbredirparser read cb */ |
110 | const uint8_t *read_buf; | |
111 | int read_buf_size; | |
c874ea97 HG |
112 | /* Active chardev-watch-tag */ |
113 | guint watch; | |
19e83931 | 114 | /* For async handling of close / reject */ |
ed9873bf | 115 | QEMUBH *chardev_close_bh; |
19e83931 | 116 | QEMUBH *device_reject_bh; |
69354a83 HG |
117 | /* To delay the usb attach in case of quick chardev close + open */ |
118 | QEMUTimer *attach_timer; | |
119 | int64_t next_attach_time; | |
120 | struct usbredirparser *parser; | |
121 | struct endp_data endpoint[MAX_ENDPOINTS]; | |
8e60452a | 122 | struct PacketIdQueue cancelled; |
9a8d4067 | 123 | struct PacketIdQueue already_in_flight; |
b2d1fe67 | 124 | void (*buffered_bulk_in_complete)(USBRedirDevice *, USBPacket *, uint8_t); |
6af16589 HG |
125 | /* Data for device filtering */ |
126 | struct usb_redir_device_connect_header device_info; | |
127 | struct usb_redir_interface_info_header interface_info; | |
128 | struct usbredirfilter_rule *filter_rules; | |
129 | int filter_rules_count; | |
cdfd3530 | 130 | int compatible_speedmask; |
69354a83 HG |
131 | }; |
132 | ||
097a66ef | 133 | static void usbredir_hello(void *priv, struct usb_redir_hello_header *h); |
69354a83 HG |
134 | static void usbredir_device_connect(void *priv, |
135 | struct usb_redir_device_connect_header *device_connect); | |
136 | static void usbredir_device_disconnect(void *priv); | |
137 | static void usbredir_interface_info(void *priv, | |
138 | struct usb_redir_interface_info_header *interface_info); | |
139 | static void usbredir_ep_info(void *priv, | |
140 | struct usb_redir_ep_info_header *ep_info); | |
be4a8928 | 141 | static void usbredir_configuration_status(void *priv, uint64_t id, |
69354a83 | 142 | struct usb_redir_configuration_status_header *configuration_status); |
be4a8928 | 143 | static void usbredir_alt_setting_status(void *priv, uint64_t id, |
69354a83 | 144 | struct usb_redir_alt_setting_status_header *alt_setting_status); |
be4a8928 | 145 | static void usbredir_iso_stream_status(void *priv, uint64_t id, |
69354a83 | 146 | struct usb_redir_iso_stream_status_header *iso_stream_status); |
be4a8928 | 147 | static void usbredir_interrupt_receiving_status(void *priv, uint64_t id, |
69354a83 HG |
148 | struct usb_redir_interrupt_receiving_status_header |
149 | *interrupt_receiving_status); | |
be4a8928 | 150 | static void usbredir_bulk_streams_status(void *priv, uint64_t id, |
69354a83 | 151 | struct usb_redir_bulk_streams_status_header *bulk_streams_status); |
b2d1fe67 HG |
152 | static void usbredir_bulk_receiving_status(void *priv, uint64_t id, |
153 | struct usb_redir_bulk_receiving_status_header *bulk_receiving_status); | |
be4a8928 | 154 | static void usbredir_control_packet(void *priv, uint64_t id, |
69354a83 HG |
155 | struct usb_redir_control_packet_header *control_packet, |
156 | uint8_t *data, int data_len); | |
be4a8928 | 157 | static void usbredir_bulk_packet(void *priv, uint64_t id, |
69354a83 HG |
158 | struct usb_redir_bulk_packet_header *bulk_packet, |
159 | uint8_t *data, int data_len); | |
be4a8928 | 160 | static void usbredir_iso_packet(void *priv, uint64_t id, |
69354a83 HG |
161 | struct usb_redir_iso_packet_header *iso_packet, |
162 | uint8_t *data, int data_len); | |
be4a8928 | 163 | static void usbredir_interrupt_packet(void *priv, uint64_t id, |
69354a83 HG |
164 | struct usb_redir_interrupt_packet_header *interrupt_header, |
165 | uint8_t *data, int data_len); | |
b2d1fe67 HG |
166 | static void usbredir_buffered_bulk_packet(void *priv, uint64_t id, |
167 | struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet, | |
168 | uint8_t *data, int data_len); | |
69354a83 | 169 | |
9a77a0f5 HG |
170 | static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p, |
171 | int status); | |
69354a83 | 172 | |
35efba2c HG |
173 | #define VERSION "qemu usb-redir guest " QEMU_VERSION |
174 | ||
69354a83 HG |
175 | /* |
176 | * Logging stuff | |
177 | */ | |
178 | ||
179 | #define ERROR(...) \ | |
180 | do { \ | |
181 | if (dev->debug >= usbredirparser_error) { \ | |
182 | error_report("usb-redir error: " __VA_ARGS__); \ | |
183 | } \ | |
184 | } while (0) | |
185 | #define WARNING(...) \ | |
186 | do { \ | |
187 | if (dev->debug >= usbredirparser_warning) { \ | |
188 | error_report("usb-redir warning: " __VA_ARGS__); \ | |
189 | } \ | |
190 | } while (0) | |
191 | #define INFO(...) \ | |
192 | do { \ | |
193 | if (dev->debug >= usbredirparser_info) { \ | |
194 | error_report("usb-redir: " __VA_ARGS__); \ | |
195 | } \ | |
196 | } while (0) | |
197 | #define DPRINTF(...) \ | |
198 | do { \ | |
199 | if (dev->debug >= usbredirparser_debug) { \ | |
200 | error_report("usb-redir: " __VA_ARGS__); \ | |
201 | } \ | |
202 | } while (0) | |
203 | #define DPRINTF2(...) \ | |
204 | do { \ | |
205 | if (dev->debug >= usbredirparser_debug_data) { \ | |
206 | error_report("usb-redir: " __VA_ARGS__); \ | |
207 | } \ | |
208 | } while (0) | |
209 | ||
210 | static void usbredir_log(void *priv, int level, const char *msg) | |
211 | { | |
212 | USBRedirDevice *dev = priv; | |
213 | ||
214 | if (dev->debug < level) { | |
215 | return; | |
216 | } | |
217 | ||
be62a2eb | 218 | error_report("%s", msg); |
69354a83 HG |
219 | } |
220 | ||
221 | static void usbredir_log_data(USBRedirDevice *dev, const char *desc, | |
222 | const uint8_t *data, int len) | |
223 | { | |
224 | int i, j, n; | |
225 | ||
226 | if (dev->debug < usbredirparser_debug_data) { | |
227 | return; | |
228 | } | |
229 | ||
230 | for (i = 0; i < len; i += j) { | |
231 | char buf[128]; | |
232 | ||
233 | n = sprintf(buf, "%s", desc); | |
234 | for (j = 0; j < 8 && i + j < len; j++) { | |
235 | n += sprintf(buf + n, " %02X", data[i + j]); | |
236 | } | |
be62a2eb | 237 | error_report("%s", buf); |
69354a83 HG |
238 | } |
239 | } | |
240 | ||
241 | /* | |
242 | * usbredirparser io functions | |
243 | */ | |
244 | ||
245 | static int usbredir_read(void *priv, uint8_t *data, int count) | |
246 | { | |
247 | USBRedirDevice *dev = priv; | |
248 | ||
249 | if (dev->read_buf_size < count) { | |
250 | count = dev->read_buf_size; | |
251 | } | |
252 | ||
253 | memcpy(data, dev->read_buf, count); | |
254 | ||
255 | dev->read_buf_size -= count; | |
256 | if (dev->read_buf_size) { | |
257 | dev->read_buf += count; | |
258 | } else { | |
259 | dev->read_buf = NULL; | |
260 | } | |
261 | ||
262 | return count; | |
263 | } | |
264 | ||
c874ea97 HG |
265 | static gboolean usbredir_write_unblocked(GIOChannel *chan, GIOCondition cond, |
266 | void *opaque) | |
267 | { | |
268 | USBRedirDevice *dev = opaque; | |
269 | ||
270 | dev->watch = 0; | |
271 | usbredirparser_do_write(dev->parser); | |
272 | ||
273 | return FALSE; | |
274 | } | |
275 | ||
69354a83 HG |
276 | static int usbredir_write(void *priv, uint8_t *data, int count) |
277 | { | |
278 | USBRedirDevice *dev = priv; | |
c874ea97 | 279 | int r; |
69354a83 | 280 | |
16665b94 | 281 | if (!dev->cs->be_open) { |
c1b71a1d HG |
282 | return 0; |
283 | } | |
284 | ||
fc3f6e1b HG |
285 | /* Don't send new data to the chardev until our state is fully synced */ |
286 | if (!runstate_check(RUN_STATE_RUNNING)) { | |
287 | return 0; | |
288 | } | |
289 | ||
c874ea97 HG |
290 | r = qemu_chr_fe_write(dev->cs, data, count); |
291 | if (r < count) { | |
292 | if (!dev->watch) { | |
e02bc6de | 293 | dev->watch = qemu_chr_fe_add_watch(dev->cs, G_IO_OUT|G_IO_HUP, |
c874ea97 HG |
294 | usbredir_write_unblocked, dev); |
295 | } | |
296 | if (r < 0) { | |
297 | r = 0; | |
298 | } | |
299 | } | |
300 | return r; | |
69354a83 HG |
301 | } |
302 | ||
303 | /* | |
de550a6a | 304 | * Cancelled and buffered packets helpers |
69354a83 HG |
305 | */ |
306 | ||
8e60452a HG |
307 | static void packet_id_queue_init(struct PacketIdQueue *q, |
308 | USBRedirDevice *dev, const char *name) | |
69354a83 | 309 | { |
8e60452a HG |
310 | q->dev = dev; |
311 | q->name = name; | |
312 | QTAILQ_INIT(&q->head); | |
313 | q->size = 0; | |
314 | } | |
315 | ||
316 | static void packet_id_queue_add(struct PacketIdQueue *q, uint64_t id) | |
317 | { | |
318 | USBRedirDevice *dev = q->dev; | |
319 | struct PacketIdQueueEntry *e; | |
69354a83 | 320 | |
8e60452a HG |
321 | DPRINTF("adding packet id %"PRIu64" to %s queue\n", id, q->name); |
322 | ||
323 | e = g_malloc0(sizeof(struct PacketIdQueueEntry)); | |
324 | e->id = id; | |
325 | QTAILQ_INSERT_TAIL(&q->head, e, next); | |
326 | q->size++; | |
327 | } | |
69354a83 | 328 | |
8e60452a HG |
329 | static int packet_id_queue_remove(struct PacketIdQueue *q, uint64_t id) |
330 | { | |
331 | USBRedirDevice *dev = q->dev; | |
332 | struct PacketIdQueueEntry *e; | |
de550a6a | 333 | |
8e60452a HG |
334 | QTAILQ_FOREACH(e, &q->head, next) { |
335 | if (e->id == id) { | |
336 | DPRINTF("removing packet id %"PRIu64" from %s queue\n", | |
337 | id, q->name); | |
338 | QTAILQ_REMOVE(&q->head, e, next); | |
339 | q->size--; | |
340 | g_free(e); | |
341 | return 1; | |
342 | } | |
343 | } | |
344 | return 0; | |
345 | } | |
346 | ||
347 | static void packet_id_queue_empty(struct PacketIdQueue *q) | |
348 | { | |
349 | USBRedirDevice *dev = q->dev; | |
350 | struct PacketIdQueueEntry *e, *next_e; | |
351 | ||
352 | DPRINTF("removing %d packet-ids from %s queue\n", q->size, q->name); | |
353 | ||
354 | QTAILQ_FOREACH_SAFE(e, &q->head, next, next_e) { | |
355 | QTAILQ_REMOVE(&q->head, e, next); | |
356 | g_free(e); | |
357 | } | |
358 | q->size = 0; | |
359 | } | |
360 | ||
361 | static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p) | |
362 | { | |
363 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
b2d1fe67 | 364 | int i = USBEP2I(p->ep); |
8e60452a | 365 | |
1b36c4d8 HG |
366 | if (p->combined) { |
367 | usb_combined_packet_cancel(udev, p); | |
368 | return; | |
369 | } | |
370 | ||
b2d1fe67 HG |
371 | if (dev->endpoint[i].pending_async_packet) { |
372 | assert(dev->endpoint[i].pending_async_packet == p); | |
373 | dev->endpoint[i].pending_async_packet = NULL; | |
374 | return; | |
375 | } | |
376 | ||
8e60452a | 377 | packet_id_queue_add(&dev->cancelled, p->id); |
de550a6a HG |
378 | usbredirparser_send_cancel_data_packet(dev->parser, p->id); |
379 | usbredirparser_do_write(dev->parser); | |
69354a83 HG |
380 | } |
381 | ||
de550a6a | 382 | static int usbredir_is_cancelled(USBRedirDevice *dev, uint64_t id) |
69354a83 | 383 | { |
de550a6a HG |
384 | if (!dev->dev.attached) { |
385 | return 1; /* Treat everything as cancelled after a disconnect */ | |
386 | } | |
8e60452a | 387 | return packet_id_queue_remove(&dev->cancelled, id); |
69354a83 HG |
388 | } |
389 | ||
9a8d4067 HG |
390 | static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice *dev, |
391 | struct USBEndpoint *ep) | |
392 | { | |
393 | static USBPacket *p; | |
394 | ||
b2d1fe67 HG |
395 | /* async handled packets for bulk receiving eps do not count as inflight */ |
396 | if (dev->endpoint[USBEP2I(ep)].bulk_receiving_started) { | |
397 | return; | |
398 | } | |
399 | ||
9a8d4067 | 400 | QTAILQ_FOREACH(p, &ep->queue, queue) { |
1b36c4d8 HG |
401 | /* Skip combined packets, except for the first */ |
402 | if (p->combined && p != p->combined->first) { | |
403 | continue; | |
404 | } | |
2cb343b4 HG |
405 | if (p->state == USB_PACKET_ASYNC) { |
406 | packet_id_queue_add(&dev->already_in_flight, p->id); | |
407 | } | |
9a8d4067 HG |
408 | } |
409 | } | |
410 | ||
411 | static void usbredir_fill_already_in_flight(USBRedirDevice *dev) | |
412 | { | |
413 | int ep; | |
414 | struct USBDevice *udev = &dev->dev; | |
415 | ||
416 | usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_ctl); | |
417 | ||
418 | for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { | |
419 | usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_in[ep]); | |
420 | usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_out[ep]); | |
421 | } | |
422 | } | |
423 | ||
424 | static int usbredir_already_in_flight(USBRedirDevice *dev, uint64_t id) | |
425 | { | |
426 | return packet_id_queue_remove(&dev->already_in_flight, id); | |
427 | } | |
428 | ||
de550a6a HG |
429 | static USBPacket *usbredir_find_packet_by_id(USBRedirDevice *dev, |
430 | uint8_t ep, uint64_t id) | |
69354a83 | 431 | { |
de550a6a | 432 | USBPacket *p; |
69354a83 | 433 | |
de550a6a HG |
434 | if (usbredir_is_cancelled(dev, id)) { |
435 | return NULL; | |
436 | } | |
69354a83 | 437 | |
de550a6a HG |
438 | p = usb_ep_find_packet_by_id(&dev->dev, |
439 | (ep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT, | |
440 | ep & 0x0f, id); | |
441 | if (p == NULL) { | |
442 | ERROR("could not find packet with id %"PRIu64"\n", id); | |
69354a83 | 443 | } |
de550a6a | 444 | return p; |
69354a83 HG |
445 | } |
446 | ||
b2d1fe67 HG |
447 | static void bufp_alloc(USBRedirDevice *dev, uint8_t *data, uint16_t len, |
448 | uint8_t status, uint8_t ep, void *free_on_destroy) | |
69354a83 | 449 | { |
81fd7b74 HG |
450 | struct buf_packet *bufp; |
451 | ||
452 | if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets && | |
453 | dev->endpoint[EP2I(ep)].bufpq_size > | |
454 | 2 * dev->endpoint[EP2I(ep)].bufpq_target_size) { | |
455 | DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep); | |
456 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1; | |
457 | } | |
458 | /* Since we're interupting the stream anyways, drop enough packets to get | |
459 | back to our target buffer size */ | |
460 | if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) { | |
461 | if (dev->endpoint[EP2I(ep)].bufpq_size > | |
462 | dev->endpoint[EP2I(ep)].bufpq_target_size) { | |
463 | free(data); | |
464 | return; | |
465 | } | |
466 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; | |
467 | } | |
468 | ||
469 | bufp = g_malloc(sizeof(struct buf_packet)); | |
69354a83 HG |
470 | bufp->data = data; |
471 | bufp->len = len; | |
b2d1fe67 | 472 | bufp->offset = 0; |
69354a83 | 473 | bufp->status = status; |
b2d1fe67 | 474 | bufp->free_on_destroy = free_on_destroy; |
69354a83 | 475 | QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next); |
e1537884 | 476 | dev->endpoint[EP2I(ep)].bufpq_size++; |
69354a83 HG |
477 | } |
478 | ||
479 | static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp, | |
480 | uint8_t ep) | |
481 | { | |
482 | QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next); | |
e1537884 | 483 | dev->endpoint[EP2I(ep)].bufpq_size--; |
b2d1fe67 | 484 | free(bufp->free_on_destroy); |
7267c094 | 485 | g_free(bufp); |
69354a83 HG |
486 | } |
487 | ||
488 | static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep) | |
489 | { | |
490 | struct buf_packet *buf, *buf_next; | |
491 | ||
492 | QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) { | |
493 | bufp_free(dev, buf, ep); | |
494 | } | |
495 | } | |
496 | ||
497 | /* | |
498 | * USBDevice callbacks | |
499 | */ | |
500 | ||
501 | static void usbredir_handle_reset(USBDevice *udev) | |
502 | { | |
503 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
504 | ||
505 | DPRINTF("reset device\n"); | |
506 | usbredirparser_send_reset(dev->parser); | |
507 | usbredirparser_do_write(dev->parser); | |
508 | } | |
509 | ||
9a77a0f5 | 510 | static void usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p, |
69354a83 HG |
511 | uint8_t ep) |
512 | { | |
513 | int status, len; | |
69354a83 HG |
514 | if (!dev->endpoint[EP2I(ep)].iso_started && |
515 | !dev->endpoint[EP2I(ep)].iso_error) { | |
516 | struct usb_redir_start_iso_stream_header start_iso = { | |
517 | .endpoint = ep, | |
69354a83 | 518 | }; |
e8a7dd29 HG |
519 | int pkts_per_sec; |
520 | ||
521 | if (dev->dev.speed == USB_SPEED_HIGH) { | |
522 | pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval; | |
523 | } else { | |
524 | pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval; | |
525 | } | |
526 | /* Testing has shown that we need circa 60 ms buffer */ | |
527 | dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000; | |
528 | ||
529 | /* Aim for approx 100 interrupts / second on the client to | |
530 | balance latency and interrupt load */ | |
531 | start_iso.pkts_per_urb = pkts_per_sec / 100; | |
532 | if (start_iso.pkts_per_urb < 1) { | |
533 | start_iso.pkts_per_urb = 1; | |
534 | } else if (start_iso.pkts_per_urb > 32) { | |
535 | start_iso.pkts_per_urb = 32; | |
536 | } | |
537 | ||
538 | start_iso.no_urbs = (dev->endpoint[EP2I(ep)].bufpq_target_size + | |
539 | start_iso.pkts_per_urb - 1) / | |
540 | start_iso.pkts_per_urb; | |
541 | /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest | |
542 | as overflow buffer. Also see the usbredir protocol documentation */ | |
543 | if (!(ep & USB_DIR_IN)) { | |
544 | start_iso.no_urbs *= 2; | |
545 | } | |
546 | if (start_iso.no_urbs > 16) { | |
547 | start_iso.no_urbs = 16; | |
548 | } | |
549 | ||
69354a83 HG |
550 | /* No id, we look at the ep when receiving a status back */ |
551 | usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso); | |
552 | usbredirparser_do_write(dev->parser); | |
32213543 HG |
553 | DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n", |
554 | pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep); | |
69354a83 | 555 | dev->endpoint[EP2I(ep)].iso_started = 1; |
e1537884 | 556 | dev->endpoint[EP2I(ep)].bufpq_prefilled = 0; |
81fd7b74 | 557 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; |
69354a83 HG |
558 | } |
559 | ||
560 | if (ep & USB_DIR_IN) { | |
561 | struct buf_packet *isop; | |
562 | ||
e1537884 HG |
563 | if (dev->endpoint[EP2I(ep)].iso_started && |
564 | !dev->endpoint[EP2I(ep)].bufpq_prefilled) { | |
565 | if (dev->endpoint[EP2I(ep)].bufpq_size < | |
566 | dev->endpoint[EP2I(ep)].bufpq_target_size) { | |
9a77a0f5 | 567 | return; |
e1537884 HG |
568 | } |
569 | dev->endpoint[EP2I(ep)].bufpq_prefilled = 1; | |
570 | } | |
571 | ||
69354a83 HG |
572 | isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); |
573 | if (isop == NULL) { | |
32213543 HG |
574 | DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n", |
575 | ep, dev->endpoint[EP2I(ep)].iso_error); | |
e1537884 HG |
576 | /* Re-fill the buffer */ |
577 | dev->endpoint[EP2I(ep)].bufpq_prefilled = 0; | |
69354a83 HG |
578 | /* Check iso_error for stream errors, otherwise its an underrun */ |
579 | status = dev->endpoint[EP2I(ep)].iso_error; | |
580 | dev->endpoint[EP2I(ep)].iso_error = 0; | |
9a77a0f5 HG |
581 | p->status = status ? USB_RET_IOERROR : USB_RET_SUCCESS; |
582 | return; | |
69354a83 | 583 | } |
32213543 HG |
584 | DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep, |
585 | isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size); | |
69354a83 HG |
586 | |
587 | status = isop->status; | |
69354a83 | 588 | len = isop->len; |
4f4321c1 | 589 | if (len > p->iov.size) { |
32213543 HG |
590 | ERROR("received iso data is larger then packet ep %02X (%d > %d)\n", |
591 | ep, len, (int)p->iov.size); | |
e94ca437 HG |
592 | len = p->iov.size; |
593 | status = usb_redir_babble; | |
69354a83 | 594 | } |
4f4321c1 | 595 | usb_packet_copy(p, isop->data, len); |
69354a83 | 596 | bufp_free(dev, isop, ep); |
e94ca437 | 597 | usbredir_handle_status(dev, p, status); |
69354a83 HG |
598 | } else { |
599 | /* If the stream was not started because of a pending error don't | |
600 | send the packet to the usb-host */ | |
601 | if (dev->endpoint[EP2I(ep)].iso_started) { | |
602 | struct usb_redir_iso_packet_header iso_packet = { | |
603 | .endpoint = ep, | |
4f4321c1 | 604 | .length = p->iov.size |
69354a83 | 605 | }; |
4f4321c1 | 606 | uint8_t buf[p->iov.size]; |
69354a83 | 607 | /* No id, we look at the ep when receiving a status back */ |
4f4321c1 | 608 | usb_packet_copy(p, buf, p->iov.size); |
69354a83 | 609 | usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet, |
4f4321c1 | 610 | buf, p->iov.size); |
69354a83 HG |
611 | usbredirparser_do_write(dev->parser); |
612 | } | |
613 | status = dev->endpoint[EP2I(ep)].iso_error; | |
614 | dev->endpoint[EP2I(ep)].iso_error = 0; | |
4f4321c1 GH |
615 | DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status, |
616 | p->iov.size); | |
9a77a0f5 | 617 | usbredir_handle_status(dev, p, status); |
69354a83 HG |
618 | } |
619 | } | |
620 | ||
621 | static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep) | |
622 | { | |
623 | struct usb_redir_stop_iso_stream_header stop_iso_stream = { | |
624 | .endpoint = ep | |
625 | }; | |
626 | if (dev->endpoint[EP2I(ep)].iso_started) { | |
627 | usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream); | |
628 | DPRINTF("iso stream stopped ep %02X\n", ep); | |
629 | dev->endpoint[EP2I(ep)].iso_started = 0; | |
630 | } | |
2bd836e5 | 631 | dev->endpoint[EP2I(ep)].iso_error = 0; |
69354a83 HG |
632 | usbredir_free_bufpq(dev, ep); |
633 | } | |
634 | ||
b2d1fe67 HG |
635 | /* |
636 | * The usb-host may poll the endpoint faster then our guest, resulting in lots | |
637 | * of smaller bulkp-s. The below buffered_bulk_in_complete* functions combine | |
638 | * data from multiple bulkp-s into a single packet, avoiding bufpq overflows. | |
639 | */ | |
640 | static void usbredir_buffered_bulk_add_data_to_packet(USBRedirDevice *dev, | |
641 | struct buf_packet *bulkp, int count, USBPacket *p, uint8_t ep) | |
642 | { | |
643 | usb_packet_copy(p, bulkp->data + bulkp->offset, count); | |
644 | bulkp->offset += count; | |
645 | if (bulkp->offset == bulkp->len) { | |
646 | /* Store status in the last packet with data from this bulkp */ | |
647 | usbredir_handle_status(dev, p, bulkp->status); | |
648 | bufp_free(dev, bulkp, ep); | |
649 | } | |
650 | } | |
651 | ||
652 | static void usbredir_buffered_bulk_in_complete_raw(USBRedirDevice *dev, | |
653 | USBPacket *p, uint8_t ep) | |
654 | { | |
655 | struct buf_packet *bulkp; | |
656 | int count; | |
657 | ||
658 | while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) && | |
659 | p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) { | |
660 | count = bulkp->len - bulkp->offset; | |
661 | if (count > (p->iov.size - p->actual_length)) { | |
662 | count = p->iov.size - p->actual_length; | |
663 | } | |
664 | usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep); | |
665 | } | |
666 | } | |
667 | ||
668 | static void usbredir_buffered_bulk_in_complete_ftdi(USBRedirDevice *dev, | |
669 | USBPacket *p, uint8_t ep) | |
670 | { | |
671 | const int maxp = dev->endpoint[EP2I(ep)].max_packet_size; | |
672 | uint8_t header[2] = { 0, 0 }; | |
673 | struct buf_packet *bulkp; | |
674 | int count; | |
675 | ||
676 | while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) && | |
677 | p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) { | |
678 | if (bulkp->len < 2) { | |
679 | WARNING("malformed ftdi bulk in packet\n"); | |
680 | bufp_free(dev, bulkp, ep); | |
681 | continue; | |
682 | } | |
683 | ||
684 | if ((p->actual_length % maxp) == 0) { | |
685 | usb_packet_copy(p, bulkp->data, 2); | |
686 | memcpy(header, bulkp->data, 2); | |
687 | } else { | |
688 | if (bulkp->data[0] != header[0] || bulkp->data[1] != header[1]) { | |
689 | break; /* Different header, add to next packet */ | |
690 | } | |
691 | } | |
692 | ||
693 | if (bulkp->offset == 0) { | |
694 | bulkp->offset = 2; /* Skip header */ | |
695 | } | |
696 | count = bulkp->len - bulkp->offset; | |
697 | /* Must repeat the header at maxp interval */ | |
698 | if (count > (maxp - (p->actual_length % maxp))) { | |
699 | count = maxp - (p->actual_length % maxp); | |
700 | } | |
701 | usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep); | |
702 | } | |
703 | } | |
704 | ||
705 | static void usbredir_buffered_bulk_in_complete(USBRedirDevice *dev, | |
706 | USBPacket *p, uint8_t ep) | |
707 | { | |
708 | p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ | |
709 | dev->buffered_bulk_in_complete(dev, p, ep); | |
710 | DPRINTF("bulk-token-in ep %02X status %d len %d id %"PRIu64"\n", | |
711 | ep, p->status, p->actual_length, p->id); | |
712 | } | |
713 | ||
714 | static void usbredir_handle_buffered_bulk_in_data(USBRedirDevice *dev, | |
715 | USBPacket *p, uint8_t ep) | |
716 | { | |
717 | /* Input bulk endpoint, buffered packet input */ | |
718 | if (!dev->endpoint[EP2I(ep)].bulk_receiving_started) { | |
719 | int bpt; | |
720 | struct usb_redir_start_bulk_receiving_header start = { | |
721 | .endpoint = ep, | |
722 | .stream_id = 0, | |
723 | .no_transfers = 5, | |
724 | }; | |
725 | /* Round bytes_per_transfer up to a multiple of max_packet_size */ | |
726 | bpt = 512 + dev->endpoint[EP2I(ep)].max_packet_size - 1; | |
727 | bpt /= dev->endpoint[EP2I(ep)].max_packet_size; | |
728 | bpt *= dev->endpoint[EP2I(ep)].max_packet_size; | |
729 | start.bytes_per_transfer = bpt; | |
730 | /* No id, we look at the ep when receiving a status back */ | |
731 | usbredirparser_send_start_bulk_receiving(dev->parser, 0, &start); | |
732 | usbredirparser_do_write(dev->parser); | |
733 | DPRINTF("bulk receiving started bytes/transfer %u count %d ep %02X\n", | |
734 | start.bytes_per_transfer, start.no_transfers, ep); | |
735 | dev->endpoint[EP2I(ep)].bulk_receiving_started = 1; | |
736 | /* We don't really want to drop bulk packets ever, but | |
737 | having some upper limit to how much we buffer is good. */ | |
738 | dev->endpoint[EP2I(ep)].bufpq_target_size = 5000; | |
739 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; | |
740 | } | |
741 | ||
742 | if (QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq)) { | |
743 | DPRINTF("bulk-token-in ep %02X, no bulkp\n", ep); | |
744 | assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL); | |
745 | dev->endpoint[EP2I(ep)].pending_async_packet = p; | |
746 | p->status = USB_RET_ASYNC; | |
747 | return; | |
748 | } | |
749 | usbredir_buffered_bulk_in_complete(dev, p, ep); | |
750 | } | |
751 | ||
752 | static void usbredir_stop_bulk_receiving(USBRedirDevice *dev, uint8_t ep) | |
753 | { | |
754 | struct usb_redir_stop_bulk_receiving_header stop_bulk = { | |
755 | .endpoint = ep, | |
756 | .stream_id = 0, | |
757 | }; | |
758 | if (dev->endpoint[EP2I(ep)].bulk_receiving_started) { | |
759 | usbredirparser_send_stop_bulk_receiving(dev->parser, 0, &stop_bulk); | |
760 | DPRINTF("bulk receiving stopped ep %02X\n", ep); | |
761 | dev->endpoint[EP2I(ep)].bulk_receiving_started = 0; | |
762 | } | |
763 | usbredir_free_bufpq(dev, ep); | |
764 | } | |
765 | ||
9a77a0f5 | 766 | static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p, |
69354a83 HG |
767 | uint8_t ep) |
768 | { | |
69354a83 | 769 | struct usb_redir_bulk_packet_header bulk_packet; |
6ef3ccd1 | 770 | size_t size = usb_packet_size(p); |
b2d1fe67 | 771 | const int maxp = dev->endpoint[EP2I(ep)].max_packet_size; |
69354a83 | 772 | |
9a8d4067 | 773 | if (usbredir_already_in_flight(dev, p->id)) { |
9a77a0f5 HG |
774 | p->status = USB_RET_ASYNC; |
775 | return; | |
9a8d4067 HG |
776 | } |
777 | ||
b2d1fe67 HG |
778 | if (dev->endpoint[EP2I(ep)].bulk_receiving_enabled) { |
779 | if (size != 0 && (size % maxp) == 0) { | |
780 | usbredir_handle_buffered_bulk_in_data(dev, p, ep); | |
781 | return; | |
782 | } | |
783 | WARNING("bulk recv invalid size %zd ep %02x, disabling\n", size, ep); | |
784 | assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL); | |
785 | usbredir_stop_bulk_receiving(dev, ep); | |
786 | dev->endpoint[EP2I(ep)].bulk_receiving_enabled = 0; | |
787 | } | |
788 | ||
19e83931 HG |
789 | DPRINTF("bulk-out ep %02X stream %u len %zd id %"PRIu64"\n", |
790 | ep, p->stream, size, p->id); | |
b2d1fe67 | 791 | |
69354a83 | 792 | bulk_packet.endpoint = ep; |
1b36c4d8 | 793 | bulk_packet.length = size; |
19e83931 | 794 | bulk_packet.stream_id = p->stream; |
1b36c4d8 | 795 | bulk_packet.length_high = size >> 16; |
c19a7981 HG |
796 | assert(bulk_packet.length_high == 0 || |
797 | usbredirparser_peer_has_cap(dev->parser, | |
798 | usb_redir_cap_32bits_bulk_length)); | |
69354a83 HG |
799 | |
800 | if (ep & USB_DIR_IN) { | |
de550a6a | 801 | usbredirparser_send_bulk_packet(dev->parser, p->id, |
69354a83 HG |
802 | &bulk_packet, NULL, 0); |
803 | } else { | |
1b36c4d8 | 804 | uint8_t buf[size]; |
6ef3ccd1 | 805 | usb_packet_copy(p, buf, size); |
1b36c4d8 | 806 | usbredir_log_data(dev, "bulk data out:", buf, size); |
de550a6a | 807 | usbredirparser_send_bulk_packet(dev->parser, p->id, |
1b36c4d8 | 808 | &bulk_packet, buf, size); |
69354a83 HG |
809 | } |
810 | usbredirparser_do_write(dev->parser); | |
9a77a0f5 | 811 | p->status = USB_RET_ASYNC; |
69354a83 HG |
812 | } |
813 | ||
234e810c HG |
814 | static void usbredir_handle_interrupt_in_data(USBRedirDevice *dev, |
815 | USBPacket *p, uint8_t ep) | |
69354a83 | 816 | { |
234e810c HG |
817 | /* Input interrupt endpoint, buffered packet input */ |
818 | struct buf_packet *intp; | |
819 | int status, len; | |
69354a83 | 820 | |
234e810c HG |
821 | if (!dev->endpoint[EP2I(ep)].interrupt_started && |
822 | !dev->endpoint[EP2I(ep)].interrupt_error) { | |
823 | struct usb_redir_start_interrupt_receiving_header start_int = { | |
824 | .endpoint = ep, | |
825 | }; | |
826 | /* No id, we look at the ep when receiving a status back */ | |
827 | usbredirparser_send_start_interrupt_receiving(dev->parser, 0, | |
828 | &start_int); | |
829 | usbredirparser_do_write(dev->parser); | |
830 | DPRINTF("interrupt recv started ep %02X\n", ep); | |
831 | dev->endpoint[EP2I(ep)].interrupt_started = 1; | |
832 | /* We don't really want to drop interrupt packets ever, but | |
833 | having some upper limit to how much we buffer is good. */ | |
834 | dev->endpoint[EP2I(ep)].bufpq_target_size = 1000; | |
835 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; | |
836 | } | |
69354a83 | 837 | |
234e810c HG |
838 | intp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); |
839 | if (intp == NULL) { | |
840 | DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep); | |
841 | /* Check interrupt_error for stream errors */ | |
842 | status = dev->endpoint[EP2I(ep)].interrupt_error; | |
843 | dev->endpoint[EP2I(ep)].interrupt_error = 0; | |
844 | if (status) { | |
845 | usbredir_handle_status(dev, p, status); | |
846 | } else { | |
847 | p->status = USB_RET_NAK; | |
69354a83 | 848 | } |
234e810c HG |
849 | return; |
850 | } | |
851 | DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep, | |
852 | intp->status, intp->len); | |
853 | ||
854 | status = intp->status; | |
855 | len = intp->len; | |
856 | if (len > p->iov.size) { | |
857 | ERROR("received int data is larger then packet ep %02X\n", ep); | |
858 | len = p->iov.size; | |
859 | status = usb_redir_babble; | |
860 | } | |
861 | usb_packet_copy(p, intp->data, len); | |
862 | bufp_free(dev, intp, ep); | |
863 | usbredir_handle_status(dev, p, status); | |
864 | } | |
69354a83 | 865 | |
723aedd5 HG |
866 | /* |
867 | * Handle interrupt out data, the usbredir protocol expects us to do this | |
868 | * async, so that it can report back a completion status. But guests will | |
869 | * expect immediate completion for an interrupt endpoint, and handling this | |
870 | * async causes migration issues. So we report success directly, counting | |
871 | * on the fact that output interrupt packets normally always succeed. | |
872 | */ | |
234e810c HG |
873 | static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev, |
874 | USBPacket *p, uint8_t ep) | |
875 | { | |
234e810c HG |
876 | struct usb_redir_interrupt_packet_header interrupt_packet; |
877 | uint8_t buf[p->iov.size]; | |
9a8d4067 | 878 | |
234e810c HG |
879 | DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep, |
880 | p->iov.size, p->id); | |
69354a83 | 881 | |
234e810c HG |
882 | interrupt_packet.endpoint = ep; |
883 | interrupt_packet.length = p->iov.size; | |
884 | ||
885 | usb_packet_copy(p, buf, p->iov.size); | |
886 | usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size); | |
887 | usbredirparser_send_interrupt_packet(dev->parser, p->id, | |
888 | &interrupt_packet, buf, p->iov.size); | |
889 | usbredirparser_do_write(dev->parser); | |
69354a83 HG |
890 | } |
891 | ||
892 | static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev, | |
893 | uint8_t ep) | |
894 | { | |
895 | struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = { | |
896 | .endpoint = ep | |
897 | }; | |
898 | if (dev->endpoint[EP2I(ep)].interrupt_started) { | |
899 | usbredirparser_send_stop_interrupt_receiving(dev->parser, 0, | |
900 | &stop_interrupt_recv); | |
901 | DPRINTF("interrupt recv stopped ep %02X\n", ep); | |
902 | dev->endpoint[EP2I(ep)].interrupt_started = 0; | |
903 | } | |
2bd836e5 | 904 | dev->endpoint[EP2I(ep)].interrupt_error = 0; |
69354a83 HG |
905 | usbredir_free_bufpq(dev, ep); |
906 | } | |
907 | ||
9a77a0f5 | 908 | static void usbredir_handle_data(USBDevice *udev, USBPacket *p) |
69354a83 HG |
909 | { |
910 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
911 | uint8_t ep; | |
912 | ||
079d0b7f | 913 | ep = p->ep->nr; |
69354a83 HG |
914 | if (p->pid == USB_TOKEN_IN) { |
915 | ep |= USB_DIR_IN; | |
916 | } | |
917 | ||
918 | switch (dev->endpoint[EP2I(ep)].type) { | |
919 | case USB_ENDPOINT_XFER_CONTROL: | |
920 | ERROR("handle_data called for control transfer on ep %02X\n", ep); | |
9a77a0f5 HG |
921 | p->status = USB_RET_NAK; |
922 | break; | |
69354a83 | 923 | case USB_ENDPOINT_XFER_BULK: |
1b36c4d8 HG |
924 | if (p->state == USB_PACKET_SETUP && p->pid == USB_TOKEN_IN && |
925 | p->ep->pipeline) { | |
9a77a0f5 HG |
926 | p->status = USB_RET_ADD_TO_QUEUE; |
927 | break; | |
1b36c4d8 | 928 | } |
9a77a0f5 HG |
929 | usbredir_handle_bulk_data(dev, p, ep); |
930 | break; | |
b2d1fe67 HG |
931 | case USB_ENDPOINT_XFER_ISOC: |
932 | usbredir_handle_iso_data(dev, p, ep); | |
933 | break; | |
69354a83 | 934 | case USB_ENDPOINT_XFER_INT: |
234e810c HG |
935 | if (ep & USB_DIR_IN) { |
936 | usbredir_handle_interrupt_in_data(dev, p, ep); | |
937 | } else { | |
938 | usbredir_handle_interrupt_out_data(dev, p, ep); | |
939 | } | |
9a77a0f5 | 940 | break; |
69354a83 HG |
941 | default: |
942 | ERROR("handle_data ep %02X has unknown type %d\n", ep, | |
943 | dev->endpoint[EP2I(ep)].type); | |
9a77a0f5 | 944 | p->status = USB_RET_NAK; |
69354a83 HG |
945 | } |
946 | } | |
947 | ||
1b36c4d8 HG |
948 | static void usbredir_flush_ep_queue(USBDevice *dev, USBEndpoint *ep) |
949 | { | |
950 | if (ep->pid == USB_TOKEN_IN && ep->pipeline) { | |
951 | usb_ep_combine_input_packets(ep); | |
952 | } | |
953 | } | |
954 | ||
f8c126f3 HG |
955 | static void usbredir_stop_ep(USBRedirDevice *dev, int i) |
956 | { | |
957 | uint8_t ep = I2EP(i); | |
958 | ||
959 | switch (dev->endpoint[i].type) { | |
b2d1fe67 HG |
960 | case USB_ENDPOINT_XFER_BULK: |
961 | if (ep & USB_DIR_IN) { | |
962 | usbredir_stop_bulk_receiving(dev, ep); | |
963 | } | |
964 | break; | |
f8c126f3 HG |
965 | case USB_ENDPOINT_XFER_ISOC: |
966 | usbredir_stop_iso_stream(dev, ep); | |
967 | break; | |
968 | case USB_ENDPOINT_XFER_INT: | |
969 | if (ep & USB_DIR_IN) { | |
970 | usbredir_stop_interrupt_receiving(dev, ep); | |
971 | } | |
972 | break; | |
973 | } | |
974 | usbredir_free_bufpq(dev, ep); | |
975 | } | |
976 | ||
d8553dd0 HG |
977 | static void usbredir_ep_stopped(USBDevice *udev, USBEndpoint *uep) |
978 | { | |
979 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
980 | ||
981 | usbredir_stop_ep(dev, USBEP2I(uep)); | |
982 | usbredirparser_do_write(dev->parser); | |
983 | } | |
984 | ||
9a77a0f5 | 985 | static void usbredir_set_config(USBRedirDevice *dev, USBPacket *p, |
69354a83 HG |
986 | int config) |
987 | { | |
988 | struct usb_redir_set_configuration_header set_config; | |
69354a83 HG |
989 | int i; |
990 | ||
de550a6a | 991 | DPRINTF("set config %d id %"PRIu64"\n", config, p->id); |
69354a83 HG |
992 | |
993 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
f8c126f3 | 994 | usbredir_stop_ep(dev, i); |
69354a83 HG |
995 | } |
996 | ||
997 | set_config.configuration = config; | |
de550a6a | 998 | usbredirparser_send_set_configuration(dev->parser, p->id, &set_config); |
69354a83 | 999 | usbredirparser_do_write(dev->parser); |
9a77a0f5 | 1000 | p->status = USB_RET_ASYNC; |
69354a83 HG |
1001 | } |
1002 | ||
9a77a0f5 | 1003 | static void usbredir_get_config(USBRedirDevice *dev, USBPacket *p) |
69354a83 | 1004 | { |
de550a6a | 1005 | DPRINTF("get config id %"PRIu64"\n", p->id); |
69354a83 | 1006 | |
de550a6a | 1007 | usbredirparser_send_get_configuration(dev->parser, p->id); |
69354a83 | 1008 | usbredirparser_do_write(dev->parser); |
9a77a0f5 | 1009 | p->status = USB_RET_ASYNC; |
69354a83 HG |
1010 | } |
1011 | ||
9a77a0f5 | 1012 | static void usbredir_set_interface(USBRedirDevice *dev, USBPacket *p, |
69354a83 HG |
1013 | int interface, int alt) |
1014 | { | |
1015 | struct usb_redir_set_alt_setting_header set_alt; | |
69354a83 HG |
1016 | int i; |
1017 | ||
de550a6a | 1018 | DPRINTF("set interface %d alt %d id %"PRIu64"\n", interface, alt, p->id); |
69354a83 HG |
1019 | |
1020 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
1021 | if (dev->endpoint[i].interface == interface) { | |
f8c126f3 | 1022 | usbredir_stop_ep(dev, i); |
69354a83 HG |
1023 | } |
1024 | } | |
1025 | ||
1026 | set_alt.interface = interface; | |
1027 | set_alt.alt = alt; | |
de550a6a | 1028 | usbredirparser_send_set_alt_setting(dev->parser, p->id, &set_alt); |
69354a83 | 1029 | usbredirparser_do_write(dev->parser); |
9a77a0f5 | 1030 | p->status = USB_RET_ASYNC; |
69354a83 HG |
1031 | } |
1032 | ||
9a77a0f5 | 1033 | static void usbredir_get_interface(USBRedirDevice *dev, USBPacket *p, |
69354a83 HG |
1034 | int interface) |
1035 | { | |
1036 | struct usb_redir_get_alt_setting_header get_alt; | |
69354a83 | 1037 | |
de550a6a | 1038 | DPRINTF("get interface %d id %"PRIu64"\n", interface, p->id); |
69354a83 HG |
1039 | |
1040 | get_alt.interface = interface; | |
de550a6a | 1041 | usbredirparser_send_get_alt_setting(dev->parser, p->id, &get_alt); |
69354a83 | 1042 | usbredirparser_do_write(dev->parser); |
9a77a0f5 | 1043 | p->status = USB_RET_ASYNC; |
69354a83 HG |
1044 | } |
1045 | ||
9a77a0f5 | 1046 | static void usbredir_handle_control(USBDevice *udev, USBPacket *p, |
69354a83 HG |
1047 | int request, int value, int index, int length, uint8_t *data) |
1048 | { | |
1049 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
1050 | struct usb_redir_control_packet_header control_packet; | |
69354a83 | 1051 | |
9a8d4067 | 1052 | if (usbredir_already_in_flight(dev, p->id)) { |
9a77a0f5 HG |
1053 | p->status = USB_RET_ASYNC; |
1054 | return; | |
9a8d4067 HG |
1055 | } |
1056 | ||
69354a83 HG |
1057 | /* Special cases for certain standard device requests */ |
1058 | switch (request) { | |
1059 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: | |
1060 | DPRINTF("set address %d\n", value); | |
1061 | dev->dev.addr = value; | |
9a77a0f5 | 1062 | return; |
69354a83 | 1063 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
9a77a0f5 HG |
1064 | usbredir_set_config(dev, p, value & 0xff); |
1065 | return; | |
69354a83 | 1066 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: |
9a77a0f5 HG |
1067 | usbredir_get_config(dev, p); |
1068 | return; | |
69354a83 | 1069 | case InterfaceOutRequest | USB_REQ_SET_INTERFACE: |
9a77a0f5 HG |
1070 | usbredir_set_interface(dev, p, index, value); |
1071 | return; | |
69354a83 | 1072 | case InterfaceRequest | USB_REQ_GET_INTERFACE: |
9a77a0f5 HG |
1073 | usbredir_get_interface(dev, p, index); |
1074 | return; | |
69354a83 HG |
1075 | } |
1076 | ||
de550a6a HG |
1077 | /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */ |
1078 | DPRINTF( | |
1079 | "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64"\n", | |
1080 | request >> 8, request & 0xff, value, index, length, p->id); | |
69354a83 HG |
1081 | |
1082 | control_packet.request = request & 0xFF; | |
1083 | control_packet.requesttype = request >> 8; | |
1084 | control_packet.endpoint = control_packet.requesttype & USB_DIR_IN; | |
1085 | control_packet.value = value; | |
1086 | control_packet.index = index; | |
1087 | control_packet.length = length; | |
69354a83 HG |
1088 | |
1089 | if (control_packet.requesttype & USB_DIR_IN) { | |
de550a6a | 1090 | usbredirparser_send_control_packet(dev->parser, p->id, |
69354a83 HG |
1091 | &control_packet, NULL, 0); |
1092 | } else { | |
1093 | usbredir_log_data(dev, "ctrl data out:", data, length); | |
de550a6a | 1094 | usbredirparser_send_control_packet(dev->parser, p->id, |
69354a83 HG |
1095 | &control_packet, data, length); |
1096 | } | |
1097 | usbredirparser_do_write(dev->parser); | |
9a77a0f5 | 1098 | p->status = USB_RET_ASYNC; |
69354a83 HG |
1099 | } |
1100 | ||
19e83931 HG |
1101 | static int usbredir_alloc_streams(USBDevice *udev, USBEndpoint **eps, |
1102 | int nr_eps, int streams) | |
1103 | { | |
1104 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
1105 | #if USBREDIR_VERSION >= 0x000700 | |
1106 | struct usb_redir_alloc_bulk_streams_header alloc_streams; | |
1107 | int i; | |
1108 | ||
1109 | if (!usbredirparser_peer_has_cap(dev->parser, | |
1110 | usb_redir_cap_bulk_streams)) { | |
1111 | ERROR("peer does not support streams\n"); | |
1112 | goto reject; | |
1113 | } | |
1114 | ||
1115 | if (streams == 0) { | |
1116 | ERROR("request to allocate 0 streams\n"); | |
1117 | return -1; | |
1118 | } | |
1119 | ||
1120 | alloc_streams.no_streams = streams; | |
1121 | alloc_streams.endpoints = 0; | |
1122 | for (i = 0; i < nr_eps; i++) { | |
1123 | alloc_streams.endpoints |= 1 << USBEP2I(eps[i]); | |
1124 | } | |
1125 | usbredirparser_send_alloc_bulk_streams(dev->parser, 0, &alloc_streams); | |
1126 | usbredirparser_do_write(dev->parser); | |
1127 | ||
1128 | return 0; | |
1129 | #else | |
1130 | ERROR("usbredir_alloc_streams not implemented\n"); | |
1131 | goto reject; | |
1132 | #endif | |
1133 | reject: | |
1134 | ERROR("streams are not available, disconnecting\n"); | |
1135 | qemu_bh_schedule(dev->device_reject_bh); | |
1136 | return -1; | |
1137 | } | |
1138 | ||
1139 | static void usbredir_free_streams(USBDevice *udev, USBEndpoint **eps, | |
1140 | int nr_eps) | |
1141 | { | |
1142 | #if USBREDIR_VERSION >= 0x000700 | |
1143 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
1144 | struct usb_redir_free_bulk_streams_header free_streams; | |
1145 | int i; | |
1146 | ||
1147 | if (!usbredirparser_peer_has_cap(dev->parser, | |
1148 | usb_redir_cap_bulk_streams)) { | |
1149 | return; | |
1150 | } | |
1151 | ||
1152 | free_streams.endpoints = 0; | |
1153 | for (i = 0; i < nr_eps; i++) { | |
1154 | free_streams.endpoints |= 1 << USBEP2I(eps[i]); | |
1155 | } | |
1156 | usbredirparser_send_free_bulk_streams(dev->parser, 0, &free_streams); | |
1157 | usbredirparser_do_write(dev->parser); | |
1158 | #endif | |
1159 | } | |
1160 | ||
69354a83 HG |
1161 | /* |
1162 | * Close events can be triggered by usbredirparser_do_write which gets called | |
1163 | * from within the USBDevice data / control packet callbacks and doing a | |
1164 | * usb_detach from within these callbacks is not a good idea. | |
1165 | * | |
ed9873bf | 1166 | * So we use a bh handler to take care of close events. |
69354a83 | 1167 | */ |
ed9873bf | 1168 | static void usbredir_chardev_close_bh(void *opaque) |
69354a83 HG |
1169 | { |
1170 | USBRedirDevice *dev = opaque; | |
1171 | ||
19e83931 | 1172 | qemu_bh_cancel(dev->device_reject_bh); |
69354a83 HG |
1173 | usbredir_device_disconnect(dev); |
1174 | ||
1175 | if (dev->parser) { | |
09054d19 | 1176 | DPRINTF("destroying usbredirparser\n"); |
69354a83 HG |
1177 | usbredirparser_destroy(dev->parser); |
1178 | dev->parser = NULL; | |
1179 | } | |
c874ea97 HG |
1180 | if (dev->watch) { |
1181 | g_source_remove(dev->watch); | |
1182 | dev->watch = 0; | |
1183 | } | |
ed9873bf | 1184 | } |
69354a83 | 1185 | |
dbbf0195 | 1186 | static void usbredir_create_parser(USBRedirDevice *dev) |
ed9873bf HG |
1187 | { |
1188 | uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, }; | |
fc3f6e1b | 1189 | int flags = 0; |
6af16589 | 1190 | |
09054d19 HG |
1191 | DPRINTF("creating usbredirparser\n"); |
1192 | ||
ed9873bf HG |
1193 | dev->parser = qemu_oom_check(usbredirparser_create()); |
1194 | dev->parser->priv = dev; | |
1195 | dev->parser->log_func = usbredir_log; | |
1196 | dev->parser->read_func = usbredir_read; | |
1197 | dev->parser->write_func = usbredir_write; | |
1198 | dev->parser->hello_func = usbredir_hello; | |
1199 | dev->parser->device_connect_func = usbredir_device_connect; | |
1200 | dev->parser->device_disconnect_func = usbredir_device_disconnect; | |
1201 | dev->parser->interface_info_func = usbredir_interface_info; | |
1202 | dev->parser->ep_info_func = usbredir_ep_info; | |
1203 | dev->parser->configuration_status_func = usbredir_configuration_status; | |
1204 | dev->parser->alt_setting_status_func = usbredir_alt_setting_status; | |
1205 | dev->parser->iso_stream_status_func = usbredir_iso_stream_status; | |
1206 | dev->parser->interrupt_receiving_status_func = | |
1207 | usbredir_interrupt_receiving_status; | |
1208 | dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status; | |
b2d1fe67 | 1209 | dev->parser->bulk_receiving_status_func = usbredir_bulk_receiving_status; |
ed9873bf HG |
1210 | dev->parser->control_packet_func = usbredir_control_packet; |
1211 | dev->parser->bulk_packet_func = usbredir_bulk_packet; | |
1212 | dev->parser->iso_packet_func = usbredir_iso_packet; | |
1213 | dev->parser->interrupt_packet_func = usbredir_interrupt_packet; | |
b2d1fe67 | 1214 | dev->parser->buffered_bulk_packet_func = usbredir_buffered_bulk_packet; |
ed9873bf HG |
1215 | dev->read_buf = NULL; |
1216 | dev->read_buf_size = 0; | |
1217 | ||
1218 | usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version); | |
1219 | usbredirparser_caps_set_cap(caps, usb_redir_cap_filter); | |
0fde3b7a | 1220 | usbredirparser_caps_set_cap(caps, usb_redir_cap_ep_info_max_packet_size); |
be4a8928 | 1221 | usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids); |
c19a7981 | 1222 | usbredirparser_caps_set_cap(caps, usb_redir_cap_32bits_bulk_length); |
b2d1fe67 | 1223 | usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_receiving); |
19e83931 HG |
1224 | #if USBREDIR_VERSION >= 0x000700 |
1225 | usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_streams); | |
1226 | #endif | |
fc3f6e1b HG |
1227 | |
1228 | if (runstate_check(RUN_STATE_INMIGRATE)) { | |
1229 | flags |= usbredirparser_fl_no_hello; | |
1230 | } | |
35efba2c | 1231 | usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE, |
fc3f6e1b | 1232 | flags); |
ed9873bf | 1233 | usbredirparser_do_write(dev->parser); |
69354a83 HG |
1234 | } |
1235 | ||
910c1e6b HG |
1236 | static void usbredir_reject_device(USBRedirDevice *dev) |
1237 | { | |
1238 | usbredir_device_disconnect(dev); | |
1239 | if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) { | |
1240 | usbredirparser_send_filter_reject(dev->parser); | |
1241 | usbredirparser_do_write(dev->parser); | |
1242 | } | |
1243 | } | |
1244 | ||
19e83931 HG |
1245 | /* |
1246 | * We may need to reject the device when the hcd calls alloc_streams, doing | |
1247 | * an usb_detach from within a hcd call is not a good idea, hence this bh. | |
1248 | */ | |
1249 | static void usbredir_device_reject_bh(void *opaque) | |
1250 | { | |
1251 | USBRedirDevice *dev = opaque; | |
1252 | ||
1253 | usbredir_reject_device(dev); | |
1254 | } | |
1255 | ||
69354a83 HG |
1256 | static void usbredir_do_attach(void *opaque) |
1257 | { | |
1258 | USBRedirDevice *dev = opaque; | |
7d553f27 | 1259 | Error *local_err = NULL; |
69354a83 | 1260 | |
a508cc42 HG |
1261 | /* In order to work properly with XHCI controllers we need these caps */ |
1262 | if ((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER) && !( | |
1263 | usbredirparser_peer_has_cap(dev->parser, | |
1264 | usb_redir_cap_ep_info_max_packet_size) && | |
d3aea641 HG |
1265 | usbredirparser_peer_has_cap(dev->parser, |
1266 | usb_redir_cap_32bits_bulk_length) && | |
a508cc42 HG |
1267 | usbredirparser_peer_has_cap(dev->parser, |
1268 | usb_redir_cap_64bits_ids))) { | |
1269 | ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n"); | |
1270 | usbredir_reject_device(dev); | |
1271 | return; | |
1272 | } | |
1273 | ||
7d553f27 GA |
1274 | usb_device_attach(&dev->dev, &local_err); |
1275 | if (local_err) { | |
1276 | error_report("%s", error_get_pretty(local_err)); | |
1277 | error_free(local_err); | |
cdfd3530 | 1278 | WARNING("rejecting device due to speed mismatch\n"); |
910c1e6b | 1279 | usbredir_reject_device(dev); |
714f9db0 | 1280 | } |
69354a83 HG |
1281 | } |
1282 | ||
1283 | /* | |
1284 | * chardev callbacks | |
1285 | */ | |
1286 | ||
1287 | static int usbredir_chardev_can_read(void *opaque) | |
1288 | { | |
1289 | USBRedirDevice *dev = opaque; | |
1290 | ||
ed9873bf HG |
1291 | if (!dev->parser) { |
1292 | WARNING("chardev_can_read called on non open chardev!\n"); | |
69354a83 HG |
1293 | return 0; |
1294 | } | |
ed9873bf | 1295 | |
fc3f6e1b HG |
1296 | /* Don't read new data from the chardev until our state is fully synced */ |
1297 | if (!runstate_check(RUN_STATE_RUNNING)) { | |
1298 | return 0; | |
1299 | } | |
1300 | ||
ed9873bf HG |
1301 | /* usbredir_parser_do_read will consume *all* data we give it */ |
1302 | return 1024 * 1024; | |
69354a83 HG |
1303 | } |
1304 | ||
1305 | static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size) | |
1306 | { | |
1307 | USBRedirDevice *dev = opaque; | |
1308 | ||
1309 | /* No recursion allowed! */ | |
1310 | assert(dev->read_buf == NULL); | |
1311 | ||
1312 | dev->read_buf = buf; | |
1313 | dev->read_buf_size = size; | |
1314 | ||
1315 | usbredirparser_do_read(dev->parser); | |
1316 | /* Send any acks, etc. which may be queued now */ | |
1317 | usbredirparser_do_write(dev->parser); | |
1318 | } | |
1319 | ||
1320 | static void usbredir_chardev_event(void *opaque, int event) | |
1321 | { | |
1322 | USBRedirDevice *dev = opaque; | |
1323 | ||
1324 | switch (event) { | |
1325 | case CHR_EVENT_OPENED: | |
09054d19 | 1326 | DPRINTF("chardev open\n"); |
dbbf0195 HG |
1327 | /* Make sure any pending closes are handled (no-op if none pending) */ |
1328 | usbredir_chardev_close_bh(dev); | |
1329 | qemu_bh_cancel(dev->chardev_close_bh); | |
1330 | usbredir_create_parser(dev); | |
ed9873bf | 1331 | break; |
69354a83 | 1332 | case CHR_EVENT_CLOSED: |
09054d19 | 1333 | DPRINTF("chardev close\n"); |
ed9873bf | 1334 | qemu_bh_schedule(dev->chardev_close_bh); |
69354a83 HG |
1335 | break; |
1336 | } | |
1337 | } | |
1338 | ||
1339 | /* | |
1340 | * init + destroy | |
1341 | */ | |
1342 | ||
fc3f6e1b HG |
1343 | static void usbredir_vm_state_change(void *priv, int running, RunState state) |
1344 | { | |
1345 | USBRedirDevice *dev = priv; | |
1346 | ||
1347 | if (state == RUN_STATE_RUNNING && dev->parser != NULL) { | |
1348 | usbredirparser_do_write(dev->parser); /* Flush any pending writes */ | |
1349 | } | |
1350 | } | |
1351 | ||
bd019b73 HG |
1352 | static void usbredir_init_endpoints(USBRedirDevice *dev) |
1353 | { | |
1354 | int i; | |
1355 | ||
1356 | usb_ep_init(&dev->dev); | |
1357 | memset(dev->endpoint, 0, sizeof(dev->endpoint)); | |
1358 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
e97f0aca | 1359 | dev->endpoint[i].dev = dev; |
bd019b73 HG |
1360 | QTAILQ_INIT(&dev->endpoint[i].bufpq); |
1361 | } | |
1362 | } | |
1363 | ||
77e35b4b | 1364 | static void usbredir_realize(USBDevice *udev, Error **errp) |
69354a83 HG |
1365 | { |
1366 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
1367 | int i; | |
1368 | ||
1369 | if (dev->cs == NULL) { | |
77e35b4b GA |
1370 | error_set(errp, QERR_MISSING_PARAMETER, "chardev"); |
1371 | return; | |
69354a83 HG |
1372 | } |
1373 | ||
6af16589 HG |
1374 | if (dev->filter_str) { |
1375 | i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|", | |
1376 | &dev->filter_rules, | |
1377 | &dev->filter_rules_count); | |
1378 | if (i) { | |
77e35b4b GA |
1379 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "filter", |
1380 | "a usb device filter string"); | |
1381 | return; | |
6af16589 HG |
1382 | } |
1383 | } | |
1384 | ||
ed9873bf | 1385 | dev->chardev_close_bh = qemu_bh_new(usbredir_chardev_close_bh, dev); |
19e83931 | 1386 | dev->device_reject_bh = qemu_bh_new(usbredir_device_reject_bh, dev); |
bc72ad67 | 1387 | dev->attach_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, usbredir_do_attach, dev); |
69354a83 | 1388 | |
8e60452a | 1389 | packet_id_queue_init(&dev->cancelled, dev, "cancelled"); |
9a8d4067 | 1390 | packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight"); |
bd019b73 | 1391 | usbredir_init_endpoints(dev); |
69354a83 HG |
1392 | |
1393 | /* We'll do the attach once we receive the speed from the usb-host */ | |
1394 | udev->auto_attach = 0; | |
1395 | ||
cdfd3530 | 1396 | /* Will be cleared during setup when we find conflicts */ |
95a59dc0 | 1397 | dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH; |
cdfd3530 | 1398 | |
65f9d986 | 1399 | /* Let the backend know we are ready */ |
69354a83 HG |
1400 | qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read, |
1401 | usbredir_chardev_read, usbredir_chardev_event, dev); | |
1402 | ||
fc3f6e1b | 1403 | qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev); |
65bb3a5c | 1404 | add_boot_device_path(dev->bootindex, &udev->qdev, NULL); |
69354a83 HG |
1405 | } |
1406 | ||
1407 | static void usbredir_cleanup_device_queues(USBRedirDevice *dev) | |
1408 | { | |
69354a83 HG |
1409 | int i; |
1410 | ||
8e60452a | 1411 | packet_id_queue_empty(&dev->cancelled); |
9a8d4067 | 1412 | packet_id_queue_empty(&dev->already_in_flight); |
69354a83 HG |
1413 | for (i = 0; i < MAX_ENDPOINTS; i++) { |
1414 | usbredir_free_bufpq(dev, I2EP(i)); | |
1415 | } | |
1416 | } | |
1417 | ||
1418 | static void usbredir_handle_destroy(USBDevice *udev) | |
1419 | { | |
1420 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
1421 | ||
70f24fb6 | 1422 | qemu_chr_delete(dev->cs); |
a14ff8a6 | 1423 | dev->cs = NULL; |
69354a83 | 1424 | /* Note must be done after qemu_chr_close, as that causes a close event */ |
ed9873bf | 1425 | qemu_bh_delete(dev->chardev_close_bh); |
19e83931 | 1426 | qemu_bh_delete(dev->device_reject_bh); |
69354a83 | 1427 | |
bc72ad67 AB |
1428 | timer_del(dev->attach_timer); |
1429 | timer_free(dev->attach_timer); | |
69354a83 HG |
1430 | |
1431 | usbredir_cleanup_device_queues(dev); | |
1432 | ||
1433 | if (dev->parser) { | |
1434 | usbredirparser_destroy(dev->parser); | |
1435 | } | |
c874ea97 HG |
1436 | if (dev->watch) { |
1437 | g_source_remove(dev->watch); | |
1438 | } | |
6af16589 HG |
1439 | |
1440 | free(dev->filter_rules); | |
1441 | } | |
1442 | ||
1443 | static int usbredir_check_filter(USBRedirDevice *dev) | |
1444 | { | |
1510168e | 1445 | if (dev->interface_info.interface_count == NO_INTERFACE_INFO) { |
6af16589 | 1446 | ERROR("No interface info for device\n"); |
5b3bd682 | 1447 | goto error; |
6af16589 HG |
1448 | } |
1449 | ||
1450 | if (dev->filter_rules) { | |
1451 | if (!usbredirparser_peer_has_cap(dev->parser, | |
1452 | usb_redir_cap_connect_device_version)) { | |
1453 | ERROR("Device filter specified and peer does not have the " | |
1454 | "connect_device_version capability\n"); | |
5b3bd682 | 1455 | goto error; |
6af16589 HG |
1456 | } |
1457 | ||
1458 | if (usbredirfilter_check( | |
1459 | dev->filter_rules, | |
1460 | dev->filter_rules_count, | |
1461 | dev->device_info.device_class, | |
1462 | dev->device_info.device_subclass, | |
1463 | dev->device_info.device_protocol, | |
1464 | dev->interface_info.interface_class, | |
1465 | dev->interface_info.interface_subclass, | |
1466 | dev->interface_info.interface_protocol, | |
1467 | dev->interface_info.interface_count, | |
1468 | dev->device_info.vendor_id, | |
1469 | dev->device_info.product_id, | |
1470 | dev->device_info.device_version_bcd, | |
1471 | 0) != 0) { | |
5b3bd682 | 1472 | goto error; |
6af16589 HG |
1473 | } |
1474 | } | |
1475 | ||
1476 | return 0; | |
5b3bd682 HG |
1477 | |
1478 | error: | |
910c1e6b | 1479 | usbredir_reject_device(dev); |
5b3bd682 | 1480 | return -1; |
69354a83 HG |
1481 | } |
1482 | ||
b2d1fe67 HG |
1483 | static void usbredir_check_bulk_receiving(USBRedirDevice *dev) |
1484 | { | |
1485 | int i, j, quirks; | |
1486 | ||
1487 | if (!usbredirparser_peer_has_cap(dev->parser, | |
1488 | usb_redir_cap_bulk_receiving)) { | |
1489 | return; | |
1490 | } | |
1491 | ||
1492 | for (i = EP2I(USB_DIR_IN); i < MAX_ENDPOINTS; i++) { | |
1493 | dev->endpoint[i].bulk_receiving_enabled = 0; | |
1494 | } | |
1495 | for (i = 0; i < dev->interface_info.interface_count; i++) { | |
1496 | quirks = usb_get_quirks(dev->device_info.vendor_id, | |
1497 | dev->device_info.product_id, | |
1498 | dev->interface_info.interface_class[i], | |
1499 | dev->interface_info.interface_subclass[i], | |
1500 | dev->interface_info.interface_protocol[i]); | |
1501 | if (!(quirks & USB_QUIRK_BUFFER_BULK_IN)) { | |
1502 | continue; | |
1503 | } | |
1504 | if (quirks & USB_QUIRK_IS_FTDI) { | |
1505 | dev->buffered_bulk_in_complete = | |
1506 | usbredir_buffered_bulk_in_complete_ftdi; | |
1507 | } else { | |
1508 | dev->buffered_bulk_in_complete = | |
1509 | usbredir_buffered_bulk_in_complete_raw; | |
1510 | } | |
1511 | ||
1512 | for (j = EP2I(USB_DIR_IN); j < MAX_ENDPOINTS; j++) { | |
1513 | if (dev->endpoint[j].interface == | |
1514 | dev->interface_info.interface[i] && | |
1515 | dev->endpoint[j].type == USB_ENDPOINT_XFER_BULK && | |
1516 | dev->endpoint[j].max_packet_size != 0) { | |
1517 | dev->endpoint[j].bulk_receiving_enabled = 1; | |
1518 | /* | |
1519 | * With buffering pipelining is not necessary. Also packet | |
1520 | * combining and bulk in buffering don't play nice together! | |
1521 | */ | |
1522 | I2USBEP(dev, j)->pipeline = false; | |
1523 | break; /* Only buffer for the first ep of each intf */ | |
1524 | } | |
1525 | } | |
1526 | } | |
1527 | } | |
1528 | ||
69354a83 HG |
1529 | /* |
1530 | * usbredirparser packet complete callbacks | |
1531 | */ | |
1532 | ||
9a77a0f5 HG |
1533 | static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p, |
1534 | int status) | |
69354a83 HG |
1535 | { |
1536 | switch (status) { | |
1537 | case usb_redir_success: | |
9a77a0f5 HG |
1538 | p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ |
1539 | break; | |
69354a83 | 1540 | case usb_redir_stall: |
9a77a0f5 HG |
1541 | p->status = USB_RET_STALL; |
1542 | break; | |
69354a83 | 1543 | case usb_redir_cancelled: |
18113340 HG |
1544 | /* |
1545 | * When the usbredir-host unredirects a device, it will report a status | |
1546 | * of cancelled for all pending packets, followed by a disconnect msg. | |
1547 | */ | |
9a77a0f5 HG |
1548 | p->status = USB_RET_IOERROR; |
1549 | break; | |
69354a83 | 1550 | case usb_redir_inval: |
d61000a8 | 1551 | WARNING("got invalid param error from usb-host?\n"); |
9a77a0f5 HG |
1552 | p->status = USB_RET_IOERROR; |
1553 | break; | |
adae502c | 1554 | case usb_redir_babble: |
9a77a0f5 HG |
1555 | p->status = USB_RET_BABBLE; |
1556 | break; | |
69354a83 HG |
1557 | case usb_redir_ioerror: |
1558 | case usb_redir_timeout: | |
1559 | default: | |
9a77a0f5 | 1560 | p->status = USB_RET_IOERROR; |
69354a83 HG |
1561 | } |
1562 | } | |
1563 | ||
097a66ef HG |
1564 | static void usbredir_hello(void *priv, struct usb_redir_hello_header *h) |
1565 | { | |
1566 | USBRedirDevice *dev = priv; | |
1567 | ||
1568 | /* Try to send the filter info now that we've the usb-host's caps */ | |
1569 | if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) && | |
1570 | dev->filter_rules) { | |
1571 | usbredirparser_send_filter_filter(dev->parser, dev->filter_rules, | |
1572 | dev->filter_rules_count); | |
1573 | usbredirparser_do_write(dev->parser); | |
1574 | } | |
1575 | } | |
1576 | ||
69354a83 HG |
1577 | static void usbredir_device_connect(void *priv, |
1578 | struct usb_redir_device_connect_header *device_connect) | |
1579 | { | |
1580 | USBRedirDevice *dev = priv; | |
6af16589 | 1581 | const char *speed; |
69354a83 | 1582 | |
e93379b0 | 1583 | if (timer_pending(dev->attach_timer) || dev->dev.attached) { |
99f08100 HG |
1584 | ERROR("Received device connect while already connected\n"); |
1585 | return; | |
1586 | } | |
1587 | ||
69354a83 HG |
1588 | switch (device_connect->speed) { |
1589 | case usb_redir_speed_low: | |
6af16589 | 1590 | speed = "low speed"; |
69354a83 | 1591 | dev->dev.speed = USB_SPEED_LOW; |
cdfd3530 | 1592 | dev->compatible_speedmask &= ~USB_SPEED_MASK_FULL; |
95a59dc0 | 1593 | dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH; |
69354a83 HG |
1594 | break; |
1595 | case usb_redir_speed_full: | |
6af16589 | 1596 | speed = "full speed"; |
69354a83 | 1597 | dev->dev.speed = USB_SPEED_FULL; |
95a59dc0 | 1598 | dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH; |
69354a83 HG |
1599 | break; |
1600 | case usb_redir_speed_high: | |
6af16589 | 1601 | speed = "high speed"; |
69354a83 HG |
1602 | dev->dev.speed = USB_SPEED_HIGH; |
1603 | break; | |
1604 | case usb_redir_speed_super: | |
6af16589 | 1605 | speed = "super speed"; |
69354a83 HG |
1606 | dev->dev.speed = USB_SPEED_SUPER; |
1607 | break; | |
1608 | default: | |
6af16589 | 1609 | speed = "unknown speed"; |
69354a83 HG |
1610 | dev->dev.speed = USB_SPEED_FULL; |
1611 | } | |
6af16589 HG |
1612 | |
1613 | if (usbredirparser_peer_has_cap(dev->parser, | |
1614 | usb_redir_cap_connect_device_version)) { | |
1615 | INFO("attaching %s device %04x:%04x version %d.%d class %02x\n", | |
1616 | speed, device_connect->vendor_id, device_connect->product_id, | |
52234bc0 HG |
1617 | ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 + |
1618 | ((device_connect->device_version_bcd & 0x0f00) >> 8), | |
1619 | ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 + | |
1620 | ((device_connect->device_version_bcd & 0x000f) >> 0), | |
6af16589 HG |
1621 | device_connect->device_class); |
1622 | } else { | |
1623 | INFO("attaching %s device %04x:%04x class %02x\n", speed, | |
1624 | device_connect->vendor_id, device_connect->product_id, | |
1625 | device_connect->device_class); | |
1626 | } | |
1627 | ||
cdfd3530 | 1628 | dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask; |
6af16589 HG |
1629 | dev->device_info = *device_connect; |
1630 | ||
1631 | if (usbredir_check_filter(dev)) { | |
1632 | WARNING("Device %04x:%04x rejected by device filter, not attaching\n", | |
1633 | device_connect->vendor_id, device_connect->product_id); | |
1634 | return; | |
1635 | } | |
1636 | ||
b2d1fe67 | 1637 | usbredir_check_bulk_receiving(dev); |
bc72ad67 | 1638 | timer_mod(dev->attach_timer, dev->next_attach_time); |
69354a83 HG |
1639 | } |
1640 | ||
1641 | static void usbredir_device_disconnect(void *priv) | |
1642 | { | |
1643 | USBRedirDevice *dev = priv; | |
1644 | ||
1645 | /* Stop any pending attaches */ | |
bc72ad67 | 1646 | timer_del(dev->attach_timer); |
69354a83 HG |
1647 | |
1648 | if (dev->dev.attached) { | |
09054d19 | 1649 | DPRINTF("detaching device\n"); |
69354a83 | 1650 | usb_device_detach(&dev->dev); |
69354a83 HG |
1651 | /* |
1652 | * Delay next usb device attach to give the guest a chance to see | |
1653 | * see the detach / attach in case of quick close / open succession | |
1654 | */ | |
bc72ad67 | 1655 | dev->next_attach_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 200; |
69354a83 | 1656 | } |
99f08100 HG |
1657 | |
1658 | /* Reset state so that the next dev connected starts with a clean slate */ | |
1659 | usbredir_cleanup_device_queues(dev); | |
bd019b73 | 1660 | usbredir_init_endpoints(dev); |
1510168e | 1661 | dev->interface_info.interface_count = NO_INTERFACE_INFO; |
a0625c56 HG |
1662 | dev->dev.addr = 0; |
1663 | dev->dev.speed = 0; | |
95a59dc0 | 1664 | dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH; |
69354a83 HG |
1665 | } |
1666 | ||
1667 | static void usbredir_interface_info(void *priv, | |
1668 | struct usb_redir_interface_info_header *interface_info) | |
1669 | { | |
6af16589 HG |
1670 | USBRedirDevice *dev = priv; |
1671 | ||
1672 | dev->interface_info = *interface_info; | |
1673 | ||
1674 | /* | |
1675 | * If we receive interface info after the device has already been | |
b2d1fe67 | 1676 | * connected (ie on a set_config), re-check interface dependent things. |
6af16589 | 1677 | */ |
e93379b0 | 1678 | if (timer_pending(dev->attach_timer) || dev->dev.attached) { |
b2d1fe67 | 1679 | usbredir_check_bulk_receiving(dev); |
6af16589 HG |
1680 | if (usbredir_check_filter(dev)) { |
1681 | ERROR("Device no longer matches filter after interface info " | |
1682 | "change, disconnecting!\n"); | |
6af16589 HG |
1683 | } |
1684 | } | |
69354a83 HG |
1685 | } |
1686 | ||
cdfd3530 JK |
1687 | static void usbredir_mark_speed_incompatible(USBRedirDevice *dev, int speed) |
1688 | { | |
1689 | dev->compatible_speedmask &= ~(1 << speed); | |
1690 | dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask; | |
1691 | } | |
1692 | ||
6ba43f1f HG |
1693 | static void usbredir_set_pipeline(USBRedirDevice *dev, struct USBEndpoint *uep) |
1694 | { | |
1695 | if (uep->type != USB_ENDPOINT_XFER_BULK) { | |
1696 | return; | |
1697 | } | |
1698 | if (uep->pid == USB_TOKEN_OUT) { | |
1699 | uep->pipeline = true; | |
1700 | } | |
1b36c4d8 HG |
1701 | if (uep->pid == USB_TOKEN_IN && uep->max_packet_size != 0 && |
1702 | usbredirparser_peer_has_cap(dev->parser, | |
1703 | usb_redir_cap_32bits_bulk_length)) { | |
1704 | uep->pipeline = true; | |
1705 | } | |
6ba43f1f HG |
1706 | } |
1707 | ||
7e03d178 HG |
1708 | static void usbredir_setup_usb_eps(USBRedirDevice *dev) |
1709 | { | |
1710 | struct USBEndpoint *usb_ep; | |
7e9638d3 | 1711 | int i; |
7e03d178 HG |
1712 | |
1713 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
7e9638d3 | 1714 | usb_ep = I2USBEP(dev, i); |
7e03d178 HG |
1715 | usb_ep->type = dev->endpoint[i].type; |
1716 | usb_ep->ifnum = dev->endpoint[i].interface; | |
1717 | usb_ep->max_packet_size = dev->endpoint[i].max_packet_size; | |
19e83931 | 1718 | usb_ep->max_streams = dev->endpoint[i].max_streams; |
7e03d178 HG |
1719 | usbredir_set_pipeline(dev, usb_ep); |
1720 | } | |
1721 | } | |
1722 | ||
69354a83 HG |
1723 | static void usbredir_ep_info(void *priv, |
1724 | struct usb_redir_ep_info_header *ep_info) | |
1725 | { | |
1726 | USBRedirDevice *dev = priv; | |
1727 | int i; | |
1728 | ||
1729 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
1730 | dev->endpoint[i].type = ep_info->type[i]; | |
1731 | dev->endpoint[i].interval = ep_info->interval[i]; | |
1732 | dev->endpoint[i].interface = ep_info->interface[i]; | |
7e03d178 HG |
1733 | if (usbredirparser_peer_has_cap(dev->parser, |
1734 | usb_redir_cap_ep_info_max_packet_size)) { | |
1735 | dev->endpoint[i].max_packet_size = ep_info->max_packet_size[i]; | |
1736 | } | |
19e83931 HG |
1737 | #if USBREDIR_VERSION >= 0x000700 |
1738 | if (usbredirparser_peer_has_cap(dev->parser, | |
1739 | usb_redir_cap_bulk_streams)) { | |
1740 | dev->endpoint[i].max_streams = ep_info->max_streams[i]; | |
1741 | } | |
1742 | #endif | |
e8a7dd29 HG |
1743 | switch (dev->endpoint[i].type) { |
1744 | case usb_redir_type_invalid: | |
1745 | break; | |
1746 | case usb_redir_type_iso: | |
cdfd3530 | 1747 | usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL); |
95a59dc0 | 1748 | usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH); |
cdfd3530 | 1749 | /* Fall through */ |
e8a7dd29 | 1750 | case usb_redir_type_interrupt: |
cdfd3530 JK |
1751 | if (!usbredirparser_peer_has_cap(dev->parser, |
1752 | usb_redir_cap_ep_info_max_packet_size) || | |
1753 | ep_info->max_packet_size[i] > 64) { | |
1754 | usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL); | |
1755 | } | |
95a59dc0 HG |
1756 | if (!usbredirparser_peer_has_cap(dev->parser, |
1757 | usb_redir_cap_ep_info_max_packet_size) || | |
1758 | ep_info->max_packet_size[i] > 1024) { | |
1759 | usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH); | |
1760 | } | |
e8a7dd29 HG |
1761 | if (dev->endpoint[i].interval == 0) { |
1762 | ERROR("Received 0 interval for isoc or irq endpoint\n"); | |
24ac283a HG |
1763 | usbredir_reject_device(dev); |
1764 | return; | |
e8a7dd29 HG |
1765 | } |
1766 | /* Fall through */ | |
1767 | case usb_redir_type_control: | |
1768 | case usb_redir_type_bulk: | |
69354a83 HG |
1769 | DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i), |
1770 | dev->endpoint[i].type, dev->endpoint[i].interface); | |
e8a7dd29 HG |
1771 | break; |
1772 | default: | |
1773 | ERROR("Received invalid endpoint type\n"); | |
24ac283a | 1774 | usbredir_reject_device(dev); |
0454b611 | 1775 | return; |
69354a83 HG |
1776 | } |
1777 | } | |
cdfd3530 JK |
1778 | /* The new ep info may have caused a speed incompatibility, recheck */ |
1779 | if (dev->dev.attached && | |
1780 | !(dev->dev.port->speedmask & dev->dev.speedmask)) { | |
1781 | ERROR("Device no longer matches speed after endpoint info change, " | |
1782 | "disconnecting!\n"); | |
1783 | usbredir_reject_device(dev); | |
1784 | return; | |
1785 | } | |
7e03d178 | 1786 | usbredir_setup_usb_eps(dev); |
b2d1fe67 | 1787 | usbredir_check_bulk_receiving(dev); |
69354a83 HG |
1788 | } |
1789 | ||
be4a8928 | 1790 | static void usbredir_configuration_status(void *priv, uint64_t id, |
69354a83 HG |
1791 | struct usb_redir_configuration_status_header *config_status) |
1792 | { | |
1793 | USBRedirDevice *dev = priv; | |
de550a6a | 1794 | USBPacket *p; |
69354a83 | 1795 | |
be4a8928 HG |
1796 | DPRINTF("set config status %d config %d id %"PRIu64"\n", |
1797 | config_status->status, config_status->configuration, id); | |
69354a83 | 1798 | |
de550a6a HG |
1799 | p = usbredir_find_packet_by_id(dev, 0, id); |
1800 | if (p) { | |
cb897117 | 1801 | if (dev->dev.setup_buf[0] & USB_DIR_IN) { |
69354a83 | 1802 | dev->dev.data_buf[0] = config_status->configuration; |
9a77a0f5 | 1803 | p->actual_length = 1; |
69354a83 | 1804 | } |
9a77a0f5 | 1805 | usbredir_handle_status(dev, p, config_status->status); |
de550a6a | 1806 | usb_generic_async_ctrl_complete(&dev->dev, p); |
69354a83 | 1807 | } |
69354a83 HG |
1808 | } |
1809 | ||
be4a8928 | 1810 | static void usbredir_alt_setting_status(void *priv, uint64_t id, |
69354a83 HG |
1811 | struct usb_redir_alt_setting_status_header *alt_setting_status) |
1812 | { | |
1813 | USBRedirDevice *dev = priv; | |
de550a6a | 1814 | USBPacket *p; |
69354a83 | 1815 | |
be4a8928 HG |
1816 | DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n", |
1817 | alt_setting_status->status, alt_setting_status->interface, | |
69354a83 HG |
1818 | alt_setting_status->alt, id); |
1819 | ||
de550a6a HG |
1820 | p = usbredir_find_packet_by_id(dev, 0, id); |
1821 | if (p) { | |
cb897117 | 1822 | if (dev->dev.setup_buf[0] & USB_DIR_IN) { |
69354a83 | 1823 | dev->dev.data_buf[0] = alt_setting_status->alt; |
9a77a0f5 | 1824 | p->actual_length = 1; |
69354a83 | 1825 | } |
9a77a0f5 | 1826 | usbredir_handle_status(dev, p, alt_setting_status->status); |
de550a6a | 1827 | usb_generic_async_ctrl_complete(&dev->dev, p); |
69354a83 | 1828 | } |
69354a83 HG |
1829 | } |
1830 | ||
be4a8928 | 1831 | static void usbredir_iso_stream_status(void *priv, uint64_t id, |
69354a83 HG |
1832 | struct usb_redir_iso_stream_status_header *iso_stream_status) |
1833 | { | |
1834 | USBRedirDevice *dev = priv; | |
1835 | uint8_t ep = iso_stream_status->endpoint; | |
1836 | ||
be4a8928 | 1837 | DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status, |
69354a83 HG |
1838 | ep, id); |
1839 | ||
2bd836e5 | 1840 | if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) { |
99f08100 HG |
1841 | return; |
1842 | } | |
1843 | ||
69354a83 HG |
1844 | dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status; |
1845 | if (iso_stream_status->status == usb_redir_stall) { | |
1846 | DPRINTF("iso stream stopped by peer ep %02X\n", ep); | |
1847 | dev->endpoint[EP2I(ep)].iso_started = 0; | |
1848 | } | |
1849 | } | |
1850 | ||
be4a8928 | 1851 | static void usbredir_interrupt_receiving_status(void *priv, uint64_t id, |
69354a83 HG |
1852 | struct usb_redir_interrupt_receiving_status_header |
1853 | *interrupt_receiving_status) | |
1854 | { | |
1855 | USBRedirDevice *dev = priv; | |
1856 | uint8_t ep = interrupt_receiving_status->endpoint; | |
1857 | ||
be4a8928 | 1858 | DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n", |
69354a83 HG |
1859 | interrupt_receiving_status->status, ep, id); |
1860 | ||
2bd836e5 | 1861 | if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) { |
99f08100 HG |
1862 | return; |
1863 | } | |
1864 | ||
69354a83 HG |
1865 | dev->endpoint[EP2I(ep)].interrupt_error = |
1866 | interrupt_receiving_status->status; | |
1867 | if (interrupt_receiving_status->status == usb_redir_stall) { | |
1868 | DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep); | |
1869 | dev->endpoint[EP2I(ep)].interrupt_started = 0; | |
1870 | } | |
1871 | } | |
1872 | ||
be4a8928 | 1873 | static void usbredir_bulk_streams_status(void *priv, uint64_t id, |
69354a83 HG |
1874 | struct usb_redir_bulk_streams_status_header *bulk_streams_status) |
1875 | { | |
19e83931 HG |
1876 | #if USBREDIR_VERSION >= 0x000700 |
1877 | USBRedirDevice *dev = priv; | |
1878 | ||
1879 | if (bulk_streams_status->status == usb_redir_success) { | |
1880 | DPRINTF("bulk streams status %d eps %08x\n", | |
1881 | bulk_streams_status->status, bulk_streams_status->endpoints); | |
1882 | } else { | |
1883 | ERROR("bulk streams %s failed status %d eps %08x\n", | |
1884 | (bulk_streams_status->no_streams == 0) ? "free" : "alloc", | |
1885 | bulk_streams_status->status, bulk_streams_status->endpoints); | |
1886 | ERROR("usb-redir-host does not provide streams, disconnecting\n"); | |
1887 | usbredir_reject_device(dev); | |
1888 | } | |
1889 | #endif | |
69354a83 HG |
1890 | } |
1891 | ||
b2d1fe67 HG |
1892 | static void usbredir_bulk_receiving_status(void *priv, uint64_t id, |
1893 | struct usb_redir_bulk_receiving_status_header *bulk_receiving_status) | |
1894 | { | |
1895 | USBRedirDevice *dev = priv; | |
1896 | uint8_t ep = bulk_receiving_status->endpoint; | |
1897 | ||
1898 | DPRINTF("bulk recv status %d ep %02X id %"PRIu64"\n", | |
1899 | bulk_receiving_status->status, ep, id); | |
1900 | ||
1901 | if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].bulk_receiving_started) { | |
1902 | return; | |
1903 | } | |
1904 | ||
1905 | if (bulk_receiving_status->status == usb_redir_stall) { | |
1906 | DPRINTF("bulk receiving stopped by peer ep %02X\n", ep); | |
1907 | dev->endpoint[EP2I(ep)].bulk_receiving_started = 0; | |
1908 | } | |
1909 | } | |
1910 | ||
be4a8928 | 1911 | static void usbredir_control_packet(void *priv, uint64_t id, |
69354a83 HG |
1912 | struct usb_redir_control_packet_header *control_packet, |
1913 | uint8_t *data, int data_len) | |
1914 | { | |
1915 | USBRedirDevice *dev = priv; | |
de550a6a | 1916 | USBPacket *p; |
69354a83 | 1917 | int len = control_packet->length; |
69354a83 | 1918 | |
be4a8928 | 1919 | DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status, |
69354a83 HG |
1920 | len, id); |
1921 | ||
95a59dc0 HG |
1922 | /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices |
1923 | * to work redirected to a not superspeed capable hcd */ | |
1924 | if (dev->dev.speed == USB_SPEED_SUPER && | |
1925 | !((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER)) && | |
1926 | control_packet->requesttype == 0x80 && | |
1927 | control_packet->request == 6 && | |
1928 | control_packet->value == 0x100 && control_packet->index == 0 && | |
1929 | data_len >= 18 && data[7] == 9) { | |
1930 | data[7] = 64; | |
1931 | } | |
1932 | ||
de550a6a HG |
1933 | p = usbredir_find_packet_by_id(dev, 0, id); |
1934 | if (p) { | |
9a77a0f5 | 1935 | usbredir_handle_status(dev, p, control_packet->status); |
e94ca437 | 1936 | if (data_len > 0) { |
69354a83 | 1937 | usbredir_log_data(dev, "ctrl data in:", data, data_len); |
e94ca437 | 1938 | if (data_len > sizeof(dev->dev.data_buf)) { |
69354a83 HG |
1939 | ERROR("ctrl buffer too small (%d > %zu)\n", |
1940 | data_len, sizeof(dev->dev.data_buf)); | |
9a77a0f5 | 1941 | p->status = USB_RET_STALL; |
e94ca437 | 1942 | data_len = len = sizeof(dev->dev.data_buf); |
69354a83 | 1943 | } |
e94ca437 | 1944 | memcpy(dev->dev.data_buf, data, data_len); |
69354a83 | 1945 | } |
9a77a0f5 | 1946 | p->actual_length = len; |
de550a6a | 1947 | usb_generic_async_ctrl_complete(&dev->dev, p); |
69354a83 | 1948 | } |
69354a83 HG |
1949 | free(data); |
1950 | } | |
1951 | ||
be4a8928 | 1952 | static void usbredir_bulk_packet(void *priv, uint64_t id, |
69354a83 HG |
1953 | struct usb_redir_bulk_packet_header *bulk_packet, |
1954 | uint8_t *data, int data_len) | |
1955 | { | |
1956 | USBRedirDevice *dev = priv; | |
1957 | uint8_t ep = bulk_packet->endpoint; | |
c19a7981 | 1958 | int len = (bulk_packet->length_high << 16) | bulk_packet->length; |
de550a6a | 1959 | USBPacket *p; |
69354a83 | 1960 | |
19e83931 HG |
1961 | DPRINTF("bulk-in status %d ep %02X stream %u len %d id %"PRIu64"\n", |
1962 | bulk_packet->status, ep, bulk_packet->stream_id, len, id); | |
69354a83 | 1963 | |
de550a6a HG |
1964 | p = usbredir_find_packet_by_id(dev, ep, id); |
1965 | if (p) { | |
6ef3ccd1 | 1966 | size_t size = usb_packet_size(p); |
9a77a0f5 | 1967 | usbredir_handle_status(dev, p, bulk_packet->status); |
e94ca437 | 1968 | if (data_len > 0) { |
69354a83 | 1969 | usbredir_log_data(dev, "bulk data in:", data, data_len); |
e94ca437 | 1970 | if (data_len > size) { |
2979a361 HG |
1971 | ERROR("bulk got more data then requested (%d > %zd)\n", |
1972 | data_len, p->iov.size); | |
9a77a0f5 | 1973 | p->status = USB_RET_BABBLE; |
e94ca437 HG |
1974 | data_len = len = size; |
1975 | } | |
6ef3ccd1 | 1976 | usb_packet_copy(p, data, data_len); |
69354a83 | 1977 | } |
9a77a0f5 | 1978 | p->actual_length = len; |
1b36c4d8 HG |
1979 | if (p->pid == USB_TOKEN_IN && p->ep->pipeline) { |
1980 | usb_combined_input_packet_complete(&dev->dev, p); | |
1981 | } else { | |
1982 | usb_packet_complete(&dev->dev, p); | |
1983 | } | |
69354a83 | 1984 | } |
69354a83 HG |
1985 | free(data); |
1986 | } | |
1987 | ||
be4a8928 | 1988 | static void usbredir_iso_packet(void *priv, uint64_t id, |
69354a83 HG |
1989 | struct usb_redir_iso_packet_header *iso_packet, |
1990 | uint8_t *data, int data_len) | |
1991 | { | |
1992 | USBRedirDevice *dev = priv; | |
1993 | uint8_t ep = iso_packet->endpoint; | |
1994 | ||
be4a8928 HG |
1995 | DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n", |
1996 | iso_packet->status, ep, data_len, id); | |
69354a83 HG |
1997 | |
1998 | if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) { | |
1999 | ERROR("received iso packet for non iso endpoint %02X\n", ep); | |
2000 | free(data); | |
2001 | return; | |
2002 | } | |
2003 | ||
2004 | if (dev->endpoint[EP2I(ep)].iso_started == 0) { | |
2005 | DPRINTF("received iso packet for non started stream ep %02X\n", ep); | |
2006 | free(data); | |
2007 | return; | |
2008 | } | |
2009 | ||
2010 | /* bufp_alloc also adds the packet to the ep queue */ | |
b2d1fe67 | 2011 | bufp_alloc(dev, data, data_len, iso_packet->status, ep, data); |
69354a83 HG |
2012 | } |
2013 | ||
be4a8928 | 2014 | static void usbredir_interrupt_packet(void *priv, uint64_t id, |
69354a83 HG |
2015 | struct usb_redir_interrupt_packet_header *interrupt_packet, |
2016 | uint8_t *data, int data_len) | |
2017 | { | |
2018 | USBRedirDevice *dev = priv; | |
2019 | uint8_t ep = interrupt_packet->endpoint; | |
2020 | ||
be4a8928 | 2021 | DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n", |
69354a83 HG |
2022 | interrupt_packet->status, ep, data_len, id); |
2023 | ||
2024 | if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) { | |
2025 | ERROR("received int packet for non interrupt endpoint %02X\n", ep); | |
2026 | free(data); | |
2027 | return; | |
2028 | } | |
2029 | ||
2030 | if (ep & USB_DIR_IN) { | |
2031 | if (dev->endpoint[EP2I(ep)].interrupt_started == 0) { | |
2032 | DPRINTF("received int packet while not started ep %02X\n", ep); | |
2033 | free(data); | |
2034 | return; | |
2035 | } | |
2036 | ||
8beba930 | 2037 | if (QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq)) { |
82fb0c89 | 2038 | usb_wakeup(usb_ep_get(&dev->dev, USB_TOKEN_IN, ep & 0x0f), 0); |
8beba930 HG |
2039 | } |
2040 | ||
69354a83 | 2041 | /* bufp_alloc also adds the packet to the ep queue */ |
b2d1fe67 | 2042 | bufp_alloc(dev, data, data_len, interrupt_packet->status, ep, data); |
69354a83 | 2043 | } else { |
723aedd5 HG |
2044 | /* |
2045 | * We report output interrupt packets as completed directly upon | |
2046 | * submission, so all we can do here if one failed is warn. | |
2047 | */ | |
2048 | if (interrupt_packet->status) { | |
2049 | WARNING("interrupt output failed status %d ep %02X id %"PRIu64"\n", | |
2050 | interrupt_packet->status, ep, id); | |
69354a83 | 2051 | } |
69354a83 HG |
2052 | } |
2053 | } | |
2054 | ||
b2d1fe67 HG |
2055 | static void usbredir_buffered_bulk_packet(void *priv, uint64_t id, |
2056 | struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet, | |
2057 | uint8_t *data, int data_len) | |
2058 | { | |
2059 | USBRedirDevice *dev = priv; | |
2060 | uint8_t status, ep = buffered_bulk_packet->endpoint; | |
2061 | void *free_on_destroy; | |
2062 | int i, len; | |
2063 | ||
2064 | DPRINTF("buffered-bulk-in status %d ep %02X len %d id %"PRIu64"\n", | |
2065 | buffered_bulk_packet->status, ep, data_len, id); | |
2066 | ||
2067 | if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_BULK) { | |
2068 | ERROR("received buffered-bulk packet for non bulk ep %02X\n", ep); | |
2069 | free(data); | |
2070 | return; | |
2071 | } | |
2072 | ||
2073 | if (dev->endpoint[EP2I(ep)].bulk_receiving_started == 0) { | |
2074 | DPRINTF("received buffered-bulk packet on not started ep %02X\n", ep); | |
2075 | free(data); | |
2076 | return; | |
2077 | } | |
2078 | ||
2079 | /* Data must be in maxp chunks for buffered_bulk_add_*_data_to_packet */ | |
2080 | len = dev->endpoint[EP2I(ep)].max_packet_size; | |
2081 | status = usb_redir_success; | |
2082 | free_on_destroy = NULL; | |
2083 | for (i = 0; i < data_len; i += len) { | |
2084 | if (len >= (data_len - i)) { | |
2085 | len = data_len - i; | |
2086 | status = buffered_bulk_packet->status; | |
2087 | free_on_destroy = data; | |
2088 | } | |
2089 | /* bufp_alloc also adds the packet to the ep queue */ | |
2090 | bufp_alloc(dev, data + i, len, status, ep, free_on_destroy); | |
2091 | } | |
2092 | ||
2093 | if (dev->endpoint[EP2I(ep)].pending_async_packet) { | |
2094 | USBPacket *p = dev->endpoint[EP2I(ep)].pending_async_packet; | |
2095 | dev->endpoint[EP2I(ep)].pending_async_packet = NULL; | |
2096 | usbredir_buffered_bulk_in_complete(dev, p, ep); | |
2097 | usb_packet_complete(&dev->dev, p); | |
2098 | } | |
2099 | } | |
2100 | ||
fc3f6e1b HG |
2101 | /* |
2102 | * Migration code | |
2103 | */ | |
2104 | ||
2105 | static void usbredir_pre_save(void *priv) | |
2106 | { | |
2107 | USBRedirDevice *dev = priv; | |
2108 | ||
2109 | usbredir_fill_already_in_flight(dev); | |
2110 | } | |
2111 | ||
2112 | static int usbredir_post_load(void *priv, int version_id) | |
2113 | { | |
2114 | USBRedirDevice *dev = priv; | |
fc3f6e1b | 2115 | |
3713e148 HG |
2116 | if (dev->parser == NULL) { |
2117 | return 0; | |
2118 | } | |
2119 | ||
fc3f6e1b HG |
2120 | switch (dev->device_info.speed) { |
2121 | case usb_redir_speed_low: | |
2122 | dev->dev.speed = USB_SPEED_LOW; | |
2123 | break; | |
2124 | case usb_redir_speed_full: | |
2125 | dev->dev.speed = USB_SPEED_FULL; | |
2126 | break; | |
2127 | case usb_redir_speed_high: | |
2128 | dev->dev.speed = USB_SPEED_HIGH; | |
2129 | break; | |
2130 | case usb_redir_speed_super: | |
2131 | dev->dev.speed = USB_SPEED_SUPER; | |
2132 | break; | |
2133 | default: | |
2134 | dev->dev.speed = USB_SPEED_FULL; | |
2135 | } | |
2136 | dev->dev.speedmask = (1 << dev->dev.speed); | |
2137 | ||
7e03d178 | 2138 | usbredir_setup_usb_eps(dev); |
b2d1fe67 | 2139 | usbredir_check_bulk_receiving(dev); |
7e03d178 | 2140 | |
fc3f6e1b HG |
2141 | return 0; |
2142 | } | |
2143 | ||
2144 | /* For usbredirparser migration */ | |
2145 | static void usbredir_put_parser(QEMUFile *f, void *priv, size_t unused) | |
2146 | { | |
2147 | USBRedirDevice *dev = priv; | |
2148 | uint8_t *data; | |
2149 | int len; | |
2150 | ||
2151 | if (dev->parser == NULL) { | |
2152 | qemu_put_be32(f, 0); | |
2153 | return; | |
2154 | } | |
2155 | ||
2156 | usbredirparser_serialize(dev->parser, &data, &len); | |
2157 | qemu_oom_check(data); | |
2158 | ||
2159 | qemu_put_be32(f, len); | |
2160 | qemu_put_buffer(f, data, len); | |
2161 | ||
2162 | free(data); | |
2163 | } | |
2164 | ||
2165 | static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused) | |
2166 | { | |
2167 | USBRedirDevice *dev = priv; | |
2168 | uint8_t *data; | |
2169 | int len, ret; | |
2170 | ||
2171 | len = qemu_get_be32(f); | |
2172 | if (len == 0) { | |
2173 | return 0; | |
2174 | } | |
2175 | ||
2176 | /* | |
5c16f767 HG |
2177 | * If our chardev is not open already at this point the usbredir connection |
2178 | * has been broken (non seamless migration, or restore from disk). | |
2179 | * | |
2180 | * In this case create a temporary parser to receive the migration data, | |
2181 | * and schedule the close_bh to report the device as disconnected to the | |
2182 | * guest and to destroy the parser again. | |
fc3f6e1b HG |
2183 | */ |
2184 | if (dev->parser == NULL) { | |
5c16f767 HG |
2185 | WARNING("usb-redir connection broken during migration\n"); |
2186 | usbredir_create_parser(dev); | |
2187 | qemu_bh_schedule(dev->chardev_close_bh); | |
fc3f6e1b HG |
2188 | } |
2189 | ||
2190 | data = g_malloc(len); | |
2191 | qemu_get_buffer(f, data, len); | |
2192 | ||
2193 | ret = usbredirparser_unserialize(dev->parser, data, len); | |
2194 | ||
2195 | g_free(data); | |
2196 | ||
2197 | return ret; | |
2198 | } | |
2199 | ||
2200 | static const VMStateInfo usbredir_parser_vmstate_info = { | |
2201 | .name = "usb-redir-parser", | |
2202 | .put = usbredir_put_parser, | |
2203 | .get = usbredir_get_parser, | |
2204 | }; | |
2205 | ||
2206 | ||
2207 | /* For buffered packets (iso/irq) queue migration */ | |
2208 | static void usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused) | |
2209 | { | |
2210 | struct endp_data *endp = priv; | |
e97f0aca | 2211 | USBRedirDevice *dev = endp->dev; |
fc3f6e1b | 2212 | struct buf_packet *bufp; |
b2d1fe67 | 2213 | int len, i = 0; |
fc3f6e1b HG |
2214 | |
2215 | qemu_put_be32(f, endp->bufpq_size); | |
2216 | QTAILQ_FOREACH(bufp, &endp->bufpq, next) { | |
b2d1fe67 | 2217 | len = bufp->len - bufp->offset; |
e97f0aca | 2218 | DPRINTF("put_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size, |
b2d1fe67 HG |
2219 | len, bufp->status); |
2220 | qemu_put_be32(f, len); | |
fc3f6e1b | 2221 | qemu_put_be32(f, bufp->status); |
b2d1fe67 | 2222 | qemu_put_buffer(f, bufp->data + bufp->offset, len); |
e97f0aca | 2223 | i++; |
fc3f6e1b | 2224 | } |
e97f0aca | 2225 | assert(i == endp->bufpq_size); |
fc3f6e1b HG |
2226 | } |
2227 | ||
2228 | static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused) | |
2229 | { | |
2230 | struct endp_data *endp = priv; | |
e97f0aca | 2231 | USBRedirDevice *dev = endp->dev; |
fc3f6e1b HG |
2232 | struct buf_packet *bufp; |
2233 | int i; | |
2234 | ||
2235 | endp->bufpq_size = qemu_get_be32(f); | |
2236 | for (i = 0; i < endp->bufpq_size; i++) { | |
2237 | bufp = g_malloc(sizeof(struct buf_packet)); | |
2238 | bufp->len = qemu_get_be32(f); | |
2239 | bufp->status = qemu_get_be32(f); | |
b2d1fe67 | 2240 | bufp->offset = 0; |
fc3f6e1b | 2241 | bufp->data = qemu_oom_check(malloc(bufp->len)); /* regular malloc! */ |
b2d1fe67 | 2242 | bufp->free_on_destroy = bufp->data; |
fc3f6e1b HG |
2243 | qemu_get_buffer(f, bufp->data, bufp->len); |
2244 | QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next); | |
e97f0aca HG |
2245 | DPRINTF("get_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size, |
2246 | bufp->len, bufp->status); | |
fc3f6e1b HG |
2247 | } |
2248 | return 0; | |
2249 | } | |
2250 | ||
2251 | static const VMStateInfo usbredir_ep_bufpq_vmstate_info = { | |
2252 | .name = "usb-redir-bufpq", | |
2253 | .put = usbredir_put_bufpq, | |
2254 | .get = usbredir_get_bufpq, | |
2255 | }; | |
2256 | ||
2257 | ||
2258 | /* For endp_data migration */ | |
b2d1fe67 HG |
2259 | static const VMStateDescription usbredir_bulk_receiving_vmstate = { |
2260 | .name = "usb-redir-ep/bulk-receiving", | |
2261 | .version_id = 1, | |
2262 | .minimum_version_id = 1, | |
2263 | .fields = (VMStateField[]) { | |
2264 | VMSTATE_UINT8(bulk_receiving_started, struct endp_data), | |
2265 | VMSTATE_END_OF_LIST() | |
2266 | } | |
2267 | }; | |
2268 | ||
2269 | static bool usbredir_bulk_receiving_needed(void *priv) | |
2270 | { | |
2271 | struct endp_data *endp = priv; | |
2272 | ||
2273 | return endp->bulk_receiving_started; | |
2274 | } | |
2275 | ||
19e83931 HG |
2276 | static const VMStateDescription usbredir_stream_vmstate = { |
2277 | .name = "usb-redir-ep/stream-state", | |
2278 | .version_id = 1, | |
2279 | .minimum_version_id = 1, | |
2280 | .fields = (VMStateField[]) { | |
2281 | VMSTATE_UINT32(max_streams, struct endp_data), | |
2282 | VMSTATE_END_OF_LIST() | |
2283 | } | |
2284 | }; | |
2285 | ||
2286 | static bool usbredir_stream_needed(void *priv) | |
2287 | { | |
2288 | struct endp_data *endp = priv; | |
2289 | ||
2290 | return endp->max_streams; | |
2291 | } | |
2292 | ||
fc3f6e1b HG |
2293 | static const VMStateDescription usbredir_ep_vmstate = { |
2294 | .name = "usb-redir-ep", | |
2295 | .version_id = 1, | |
2296 | .minimum_version_id = 1, | |
2297 | .fields = (VMStateField[]) { | |
2298 | VMSTATE_UINT8(type, struct endp_data), | |
2299 | VMSTATE_UINT8(interval, struct endp_data), | |
2300 | VMSTATE_UINT8(interface, struct endp_data), | |
2301 | VMSTATE_UINT16(max_packet_size, struct endp_data), | |
2302 | VMSTATE_UINT8(iso_started, struct endp_data), | |
2303 | VMSTATE_UINT8(iso_error, struct endp_data), | |
2304 | VMSTATE_UINT8(interrupt_started, struct endp_data), | |
2305 | VMSTATE_UINT8(interrupt_error, struct endp_data), | |
2306 | VMSTATE_UINT8(bufpq_prefilled, struct endp_data), | |
2307 | VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data), | |
2308 | { | |
2309 | .name = "bufpq", | |
2310 | .version_id = 0, | |
2311 | .field_exists = NULL, | |
2312 | .size = 0, | |
2313 | .info = &usbredir_ep_bufpq_vmstate_info, | |
2314 | .flags = VMS_SINGLE, | |
2315 | .offset = 0, | |
2316 | }, | |
2317 | VMSTATE_INT32(bufpq_target_size, struct endp_data), | |
2318 | VMSTATE_END_OF_LIST() | |
b2d1fe67 HG |
2319 | }, |
2320 | .subsections = (VMStateSubsection[]) { | |
2321 | { | |
2322 | .vmsd = &usbredir_bulk_receiving_vmstate, | |
2323 | .needed = usbredir_bulk_receiving_needed, | |
19e83931 HG |
2324 | }, { |
2325 | .vmsd = &usbredir_stream_vmstate, | |
2326 | .needed = usbredir_stream_needed, | |
b2d1fe67 HG |
2327 | }, { |
2328 | /* empty */ | |
2329 | } | |
fc3f6e1b HG |
2330 | } |
2331 | }; | |
2332 | ||
2333 | ||
2334 | /* For PacketIdQueue migration */ | |
2335 | static void usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused) | |
2336 | { | |
2337 | struct PacketIdQueue *q = priv; | |
2338 | USBRedirDevice *dev = q->dev; | |
2339 | struct PacketIdQueueEntry *e; | |
2340 | int remain = q->size; | |
2341 | ||
2342 | DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size); | |
2343 | qemu_put_be32(f, q->size); | |
2344 | QTAILQ_FOREACH(e, &q->head, next) { | |
2345 | qemu_put_be64(f, e->id); | |
2346 | remain--; | |
2347 | } | |
2348 | assert(remain == 0); | |
2349 | } | |
2350 | ||
2351 | static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused) | |
2352 | { | |
2353 | struct PacketIdQueue *q = priv; | |
2354 | USBRedirDevice *dev = q->dev; | |
2355 | int i, size; | |
2356 | uint64_t id; | |
2357 | ||
2358 | size = qemu_get_be32(f); | |
2359 | DPRINTF("get_packet_id_q %s size %d\n", q->name, size); | |
2360 | for (i = 0; i < size; i++) { | |
2361 | id = qemu_get_be64(f); | |
2362 | packet_id_queue_add(q, id); | |
2363 | } | |
2364 | assert(q->size == size); | |
2365 | return 0; | |
2366 | } | |
2367 | ||
2368 | static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = { | |
2369 | .name = "usb-redir-packet-id-q", | |
2370 | .put = usbredir_put_packet_id_q, | |
2371 | .get = usbredir_get_packet_id_q, | |
2372 | }; | |
2373 | ||
2374 | static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = { | |
2375 | .name = "usb-redir-packet-id-queue", | |
2376 | .version_id = 1, | |
2377 | .minimum_version_id = 1, | |
2378 | .fields = (VMStateField[]) { | |
2379 | { | |
2380 | .name = "queue", | |
2381 | .version_id = 0, | |
2382 | .field_exists = NULL, | |
2383 | .size = 0, | |
2384 | .info = &usbredir_ep_packet_id_q_vmstate_info, | |
2385 | .flags = VMS_SINGLE, | |
2386 | .offset = 0, | |
2387 | }, | |
2388 | VMSTATE_END_OF_LIST() | |
2389 | } | |
2390 | }; | |
2391 | ||
2392 | ||
2393 | /* For usb_redir_device_connect_header migration */ | |
2394 | static const VMStateDescription usbredir_device_info_vmstate = { | |
2395 | .name = "usb-redir-device-info", | |
2396 | .version_id = 1, | |
2397 | .minimum_version_id = 1, | |
2398 | .fields = (VMStateField[]) { | |
2399 | VMSTATE_UINT8(speed, struct usb_redir_device_connect_header), | |
2400 | VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header), | |
2401 | VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header), | |
2402 | VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header), | |
2403 | VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header), | |
2404 | VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header), | |
2405 | VMSTATE_UINT16(device_version_bcd, | |
2406 | struct usb_redir_device_connect_header), | |
2407 | VMSTATE_END_OF_LIST() | |
2408 | } | |
2409 | }; | |
2410 | ||
2411 | ||
2412 | /* For usb_redir_interface_info_header migration */ | |
2413 | static const VMStateDescription usbredir_interface_info_vmstate = { | |
2414 | .name = "usb-redir-interface-info", | |
2415 | .version_id = 1, | |
2416 | .minimum_version_id = 1, | |
2417 | .fields = (VMStateField[]) { | |
2418 | VMSTATE_UINT32(interface_count, | |
2419 | struct usb_redir_interface_info_header), | |
2420 | VMSTATE_UINT8_ARRAY(interface, | |
2421 | struct usb_redir_interface_info_header, 32), | |
2422 | VMSTATE_UINT8_ARRAY(interface_class, | |
2423 | struct usb_redir_interface_info_header, 32), | |
2424 | VMSTATE_UINT8_ARRAY(interface_subclass, | |
2425 | struct usb_redir_interface_info_header, 32), | |
2426 | VMSTATE_UINT8_ARRAY(interface_protocol, | |
2427 | struct usb_redir_interface_info_header, 32), | |
2428 | VMSTATE_END_OF_LIST() | |
2429 | } | |
2430 | }; | |
2431 | ||
2432 | ||
2433 | /* And finally the USBRedirDevice vmstate itself */ | |
2434 | static const VMStateDescription usbredir_vmstate = { | |
2435 | .name = "usb-redir", | |
2436 | .version_id = 1, | |
2437 | .minimum_version_id = 1, | |
2438 | .pre_save = usbredir_pre_save, | |
2439 | .post_load = usbredir_post_load, | |
2440 | .fields = (VMStateField[]) { | |
2441 | VMSTATE_USB_DEVICE(dev, USBRedirDevice), | |
2442 | VMSTATE_TIMER(attach_timer, USBRedirDevice), | |
2443 | { | |
2444 | .name = "parser", | |
2445 | .version_id = 0, | |
2446 | .field_exists = NULL, | |
2447 | .size = 0, | |
2448 | .info = &usbredir_parser_vmstate_info, | |
2449 | .flags = VMS_SINGLE, | |
2450 | .offset = 0, | |
2451 | }, | |
2452 | VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1, | |
2453 | usbredir_ep_vmstate, struct endp_data), | |
2454 | VMSTATE_STRUCT(cancelled, USBRedirDevice, 1, | |
2455 | usbredir_ep_packet_id_queue_vmstate, | |
2456 | struct PacketIdQueue), | |
2457 | VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1, | |
2458 | usbredir_ep_packet_id_queue_vmstate, | |
2459 | struct PacketIdQueue), | |
2460 | VMSTATE_STRUCT(device_info, USBRedirDevice, 1, | |
2461 | usbredir_device_info_vmstate, | |
2462 | struct usb_redir_device_connect_header), | |
2463 | VMSTATE_STRUCT(interface_info, USBRedirDevice, 1, | |
2464 | usbredir_interface_info_vmstate, | |
2465 | struct usb_redir_interface_info_header), | |
2466 | VMSTATE_END_OF_LIST() | |
2467 | } | |
2468 | }; | |
2469 | ||
3bc36349 AL |
2470 | static Property usbredir_properties[] = { |
2471 | DEFINE_PROP_CHR("chardev", USBRedirDevice, cs), | |
618fbc95 | 2472 | DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, usbredirparser_warning), |
6af16589 | 2473 | DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str), |
65bb3a5c | 2474 | DEFINE_PROP_INT32("bootindex", USBRedirDevice, bootindex, -1), |
3bc36349 AL |
2475 | DEFINE_PROP_END_OF_LIST(), |
2476 | }; | |
2477 | ||
62aed765 AL |
2478 | static void usbredir_class_initfn(ObjectClass *klass, void *data) |
2479 | { | |
2480 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
3bc36349 | 2481 | DeviceClass *dc = DEVICE_CLASS(klass); |
62aed765 | 2482 | |
77e35b4b | 2483 | uc->realize = usbredir_realize; |
62aed765 AL |
2484 | uc->product_desc = "USB Redirection Device"; |
2485 | uc->handle_destroy = usbredir_handle_destroy; | |
62aed765 AL |
2486 | uc->cancel_packet = usbredir_cancel_packet; |
2487 | uc->handle_reset = usbredir_handle_reset; | |
2488 | uc->handle_data = usbredir_handle_data; | |
2489 | uc->handle_control = usbredir_handle_control; | |
1b36c4d8 | 2490 | uc->flush_ep_queue = usbredir_flush_ep_queue; |
d8553dd0 | 2491 | uc->ep_stopped = usbredir_ep_stopped; |
19e83931 HG |
2492 | uc->alloc_streams = usbredir_alloc_streams; |
2493 | uc->free_streams = usbredir_free_streams; | |
fc3f6e1b | 2494 | dc->vmsd = &usbredir_vmstate; |
3bc36349 | 2495 | dc->props = usbredir_properties; |
125ee0ed | 2496 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
62aed765 AL |
2497 | } |
2498 | ||
8c43a6f0 | 2499 | static const TypeInfo usbredir_dev_info = { |
3bc36349 AL |
2500 | .name = "usb-redir", |
2501 | .parent = TYPE_USB_DEVICE, | |
2502 | .instance_size = sizeof(USBRedirDevice), | |
2503 | .class_init = usbredir_class_initfn, | |
69354a83 HG |
2504 | }; |
2505 | ||
83f7d43a | 2506 | static void usbredir_register_types(void) |
69354a83 | 2507 | { |
3bc36349 | 2508 | type_register_static(&usbredir_dev_info); |
69354a83 | 2509 | } |
83f7d43a AF |
2510 | |
2511 | type_init(usbredir_register_types) |