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