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