]>
Commit | Line | Data |
---|---|---|
69354a83 HG |
1 | /* |
2 | * USB redirector usb-guest | |
3 | * | |
4 | * Copyright (c) 2011 Red Hat, Inc. | |
5 | * | |
6 | * Red Hat Authors: | |
7 | * Hans de Goede <hdegoede@redhat.com> | |
8 | * | |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
25 | * THE SOFTWARE. | |
26 | */ | |
27 | ||
28 | #include "qemu-common.h" | |
29 | #include "qemu-timer.h" | |
30 | #include "monitor.h" | |
31 | #include "sysemu.h" | |
32 | ||
33 | #include <dirent.h> | |
34 | #include <sys/ioctl.h> | |
35 | #include <signal.h> | |
36 | #include <usbredirparser.h> | |
6af16589 | 37 | #include <usbredirfilter.h> |
69354a83 HG |
38 | |
39 | #include "hw/usb.h" | |
40 | ||
41 | #define MAX_ENDPOINTS 32 | |
1510168e | 42 | #define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */ |
69354a83 HG |
43 | #define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f)) |
44 | #define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f)) | |
45 | ||
46 | typedef struct AsyncURB AsyncURB; | |
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 */ | |
61 | uint8_t iso_started; | |
62 | uint8_t iso_error; /* For reporting iso errors to the HC */ | |
63 | uint8_t interrupt_started; | |
64 | uint8_t interrupt_error; | |
e1537884 | 65 | uint8_t bufpq_prefilled; |
81fd7b74 | 66 | uint8_t bufpq_dropping_packets; |
69354a83 | 67 | QTAILQ_HEAD(, buf_packet) bufpq; |
e1537884 | 68 | int bufpq_size; |
e8a7dd29 | 69 | int bufpq_target_size; |
69354a83 HG |
70 | }; |
71 | ||
72 | struct USBRedirDevice { | |
73 | USBDevice dev; | |
74 | /* Properties */ | |
75 | CharDriverState *cs; | |
76 | uint8_t debug; | |
6af16589 | 77 | char *filter_str; |
65bb3a5c | 78 | int32_t bootindex; |
69354a83 HG |
79 | /* Data passed from chardev the fd_read cb to the usbredirparser read cb */ |
80 | const uint8_t *read_buf; | |
81 | int read_buf_size; | |
82 | /* For async handling of open/close */ | |
83 | QEMUBH *open_close_bh; | |
84 | /* To delay the usb attach in case of quick chardev close + open */ | |
85 | QEMUTimer *attach_timer; | |
86 | int64_t next_attach_time; | |
87 | struct usbredirparser *parser; | |
88 | struct endp_data endpoint[MAX_ENDPOINTS]; | |
89 | uint32_t packet_id; | |
90 | QTAILQ_HEAD(, AsyncURB) asyncq; | |
6af16589 HG |
91 | /* Data for device filtering */ |
92 | struct usb_redir_device_connect_header device_info; | |
93 | struct usb_redir_interface_info_header interface_info; | |
94 | struct usbredirfilter_rule *filter_rules; | |
95 | int filter_rules_count; | |
69354a83 HG |
96 | }; |
97 | ||
98 | struct AsyncURB { | |
99 | USBRedirDevice *dev; | |
100 | USBPacket *packet; | |
101 | uint32_t packet_id; | |
102 | int get; | |
103 | union { | |
104 | struct usb_redir_control_packet_header control_packet; | |
105 | struct usb_redir_bulk_packet_header bulk_packet; | |
106 | struct usb_redir_interrupt_packet_header interrupt_packet; | |
107 | }; | |
108 | QTAILQ_ENTRY(AsyncURB)next; | |
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); | |
119 | static void usbredir_configuration_status(void *priv, uint32_t id, | |
120 | struct usb_redir_configuration_status_header *configuration_status); | |
121 | static void usbredir_alt_setting_status(void *priv, uint32_t id, | |
122 | struct usb_redir_alt_setting_status_header *alt_setting_status); | |
123 | static void usbredir_iso_stream_status(void *priv, uint32_t id, | |
124 | struct usb_redir_iso_stream_status_header *iso_stream_status); | |
125 | static void usbredir_interrupt_receiving_status(void *priv, uint32_t id, | |
126 | struct usb_redir_interrupt_receiving_status_header | |
127 | *interrupt_receiving_status); | |
128 | static void usbredir_bulk_streams_status(void *priv, uint32_t id, | |
129 | struct usb_redir_bulk_streams_status_header *bulk_streams_status); | |
130 | static void usbredir_control_packet(void *priv, uint32_t id, | |
131 | struct usb_redir_control_packet_header *control_packet, | |
132 | uint8_t *data, int data_len); | |
133 | static void usbredir_bulk_packet(void *priv, uint32_t id, | |
134 | struct usb_redir_bulk_packet_header *bulk_packet, | |
135 | uint8_t *data, int data_len); | |
136 | static void usbredir_iso_packet(void *priv, uint32_t id, | |
137 | struct usb_redir_iso_packet_header *iso_packet, | |
138 | uint8_t *data, int data_len); | |
139 | static void usbredir_interrupt_packet(void *priv, uint32_t id, | |
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 | ||
146 | #define VERSION "qemu usb-redir guest " QEMU_VERSION | |
147 | ||
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 | ||
2cc6e0a1 | 246 | return qemu_chr_fe_write(dev->cs, data, count); |
69354a83 HG |
247 | } |
248 | ||
249 | /* | |
250 | * Async and buffered packets helpers | |
251 | */ | |
252 | ||
253 | static AsyncURB *async_alloc(USBRedirDevice *dev, USBPacket *p) | |
254 | { | |
7267c094 | 255 | AsyncURB *aurb = (AsyncURB *) g_malloc0(sizeof(AsyncURB)); |
69354a83 HG |
256 | aurb->dev = dev; |
257 | aurb->packet = p; | |
258 | aurb->packet_id = dev->packet_id; | |
259 | QTAILQ_INSERT_TAIL(&dev->asyncq, aurb, next); | |
260 | dev->packet_id++; | |
261 | ||
262 | return aurb; | |
263 | } | |
264 | ||
265 | static void async_free(USBRedirDevice *dev, AsyncURB *aurb) | |
266 | { | |
267 | QTAILQ_REMOVE(&dev->asyncq, aurb, next); | |
7267c094 | 268 | g_free(aurb); |
69354a83 HG |
269 | } |
270 | ||
271 | static AsyncURB *async_find(USBRedirDevice *dev, uint32_t packet_id) | |
272 | { | |
273 | AsyncURB *aurb; | |
274 | ||
275 | QTAILQ_FOREACH(aurb, &dev->asyncq, next) { | |
276 | if (aurb->packet_id == packet_id) { | |
277 | return aurb; | |
278 | } | |
279 | } | |
280 | ERROR("could not find async urb for packet_id %u\n", packet_id); | |
281 | return NULL; | |
282 | } | |
283 | ||
284 | static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p) | |
285 | { | |
286 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
287 | AsyncURB *aurb; | |
288 | ||
289 | QTAILQ_FOREACH(aurb, &dev->asyncq, next) { | |
290 | if (p != aurb->packet) { | |
291 | continue; | |
292 | } | |
293 | ||
294 | DPRINTF("async cancel id %u\n", aurb->packet_id); | |
295 | usbredirparser_send_cancel_data_packet(dev->parser, aurb->packet_id); | |
296 | usbredirparser_do_write(dev->parser); | |
297 | ||
298 | /* Mark it as dead */ | |
299 | aurb->packet = NULL; | |
300 | break; | |
301 | } | |
302 | } | |
303 | ||
81fd7b74 | 304 | static void bufp_alloc(USBRedirDevice *dev, |
69354a83 HG |
305 | uint8_t *data, int len, int status, uint8_t ep) |
306 | { | |
81fd7b74 HG |
307 | struct buf_packet *bufp; |
308 | ||
309 | if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets && | |
310 | dev->endpoint[EP2I(ep)].bufpq_size > | |
311 | 2 * dev->endpoint[EP2I(ep)].bufpq_target_size) { | |
312 | DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep); | |
313 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1; | |
314 | } | |
315 | /* Since we're interupting the stream anyways, drop enough packets to get | |
316 | back to our target buffer size */ | |
317 | if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) { | |
318 | if (dev->endpoint[EP2I(ep)].bufpq_size > | |
319 | dev->endpoint[EP2I(ep)].bufpq_target_size) { | |
320 | free(data); | |
321 | return; | |
322 | } | |
323 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; | |
324 | } | |
325 | ||
326 | bufp = g_malloc(sizeof(struct buf_packet)); | |
69354a83 HG |
327 | bufp->data = data; |
328 | bufp->len = len; | |
329 | bufp->status = status; | |
330 | QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next); | |
e1537884 | 331 | dev->endpoint[EP2I(ep)].bufpq_size++; |
69354a83 HG |
332 | } |
333 | ||
334 | static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp, | |
335 | uint8_t ep) | |
336 | { | |
337 | QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next); | |
e1537884 | 338 | dev->endpoint[EP2I(ep)].bufpq_size--; |
69354a83 | 339 | free(bufp->data); |
7267c094 | 340 | g_free(bufp); |
69354a83 HG |
341 | } |
342 | ||
343 | static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep) | |
344 | { | |
345 | struct buf_packet *buf, *buf_next; | |
346 | ||
347 | QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) { | |
348 | bufp_free(dev, buf, ep); | |
349 | } | |
350 | } | |
351 | ||
352 | /* | |
353 | * USBDevice callbacks | |
354 | */ | |
355 | ||
356 | static void usbredir_handle_reset(USBDevice *udev) | |
357 | { | |
358 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
359 | ||
360 | DPRINTF("reset device\n"); | |
361 | usbredirparser_send_reset(dev->parser); | |
362 | usbredirparser_do_write(dev->parser); | |
363 | } | |
364 | ||
365 | static int usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p, | |
366 | uint8_t ep) | |
367 | { | |
368 | int status, len; | |
69354a83 HG |
369 | if (!dev->endpoint[EP2I(ep)].iso_started && |
370 | !dev->endpoint[EP2I(ep)].iso_error) { | |
371 | struct usb_redir_start_iso_stream_header start_iso = { | |
372 | .endpoint = ep, | |
69354a83 | 373 | }; |
e8a7dd29 HG |
374 | int pkts_per_sec; |
375 | ||
376 | if (dev->dev.speed == USB_SPEED_HIGH) { | |
377 | pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval; | |
378 | } else { | |
379 | pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval; | |
380 | } | |
381 | /* Testing has shown that we need circa 60 ms buffer */ | |
382 | dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000; | |
383 | ||
384 | /* Aim for approx 100 interrupts / second on the client to | |
385 | balance latency and interrupt load */ | |
386 | start_iso.pkts_per_urb = pkts_per_sec / 100; | |
387 | if (start_iso.pkts_per_urb < 1) { | |
388 | start_iso.pkts_per_urb = 1; | |
389 | } else if (start_iso.pkts_per_urb > 32) { | |
390 | start_iso.pkts_per_urb = 32; | |
391 | } | |
392 | ||
393 | start_iso.no_urbs = (dev->endpoint[EP2I(ep)].bufpq_target_size + | |
394 | start_iso.pkts_per_urb - 1) / | |
395 | start_iso.pkts_per_urb; | |
396 | /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest | |
397 | as overflow buffer. Also see the usbredir protocol documentation */ | |
398 | if (!(ep & USB_DIR_IN)) { | |
399 | start_iso.no_urbs *= 2; | |
400 | } | |
401 | if (start_iso.no_urbs > 16) { | |
402 | start_iso.no_urbs = 16; | |
403 | } | |
404 | ||
69354a83 HG |
405 | /* No id, we look at the ep when receiving a status back */ |
406 | usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso); | |
407 | usbredirparser_do_write(dev->parser); | |
32213543 HG |
408 | DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n", |
409 | pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep); | |
69354a83 | 410 | dev->endpoint[EP2I(ep)].iso_started = 1; |
e1537884 | 411 | dev->endpoint[EP2I(ep)].bufpq_prefilled = 0; |
81fd7b74 | 412 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; |
69354a83 HG |
413 | } |
414 | ||
415 | if (ep & USB_DIR_IN) { | |
416 | struct buf_packet *isop; | |
417 | ||
e1537884 HG |
418 | if (dev->endpoint[EP2I(ep)].iso_started && |
419 | !dev->endpoint[EP2I(ep)].bufpq_prefilled) { | |
420 | if (dev->endpoint[EP2I(ep)].bufpq_size < | |
421 | dev->endpoint[EP2I(ep)].bufpq_target_size) { | |
422 | return usbredir_handle_status(dev, 0, 0); | |
423 | } | |
424 | dev->endpoint[EP2I(ep)].bufpq_prefilled = 1; | |
425 | } | |
426 | ||
69354a83 HG |
427 | isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); |
428 | if (isop == NULL) { | |
32213543 HG |
429 | DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n", |
430 | ep, dev->endpoint[EP2I(ep)].iso_error); | |
e1537884 HG |
431 | /* Re-fill the buffer */ |
432 | dev->endpoint[EP2I(ep)].bufpq_prefilled = 0; | |
69354a83 HG |
433 | /* Check iso_error for stream errors, otherwise its an underrun */ |
434 | status = dev->endpoint[EP2I(ep)].iso_error; | |
435 | dev->endpoint[EP2I(ep)].iso_error = 0; | |
d61000a8 | 436 | return status ? USB_RET_IOERROR : 0; |
69354a83 | 437 | } |
32213543 HG |
438 | DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep, |
439 | isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size); | |
69354a83 HG |
440 | |
441 | status = isop->status; | |
442 | if (status != usb_redir_success) { | |
443 | bufp_free(dev, isop, ep); | |
d61000a8 | 444 | return USB_RET_IOERROR; |
69354a83 HG |
445 | } |
446 | ||
447 | len = isop->len; | |
4f4321c1 | 448 | if (len > p->iov.size) { |
32213543 HG |
449 | ERROR("received iso data is larger then packet ep %02X (%d > %d)\n", |
450 | ep, len, (int)p->iov.size); | |
69354a83 | 451 | bufp_free(dev, isop, ep); |
4d819a9b | 452 | return USB_RET_BABBLE; |
69354a83 | 453 | } |
4f4321c1 | 454 | usb_packet_copy(p, isop->data, len); |
69354a83 HG |
455 | bufp_free(dev, isop, ep); |
456 | return len; | |
457 | } else { | |
458 | /* If the stream was not started because of a pending error don't | |
459 | send the packet to the usb-host */ | |
460 | if (dev->endpoint[EP2I(ep)].iso_started) { | |
461 | struct usb_redir_iso_packet_header iso_packet = { | |
462 | .endpoint = ep, | |
4f4321c1 | 463 | .length = p->iov.size |
69354a83 | 464 | }; |
4f4321c1 | 465 | uint8_t buf[p->iov.size]; |
69354a83 | 466 | /* No id, we look at the ep when receiving a status back */ |
4f4321c1 | 467 | usb_packet_copy(p, buf, p->iov.size); |
69354a83 | 468 | usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet, |
4f4321c1 | 469 | buf, p->iov.size); |
69354a83 HG |
470 | usbredirparser_do_write(dev->parser); |
471 | } | |
472 | status = dev->endpoint[EP2I(ep)].iso_error; | |
473 | dev->endpoint[EP2I(ep)].iso_error = 0; | |
4f4321c1 GH |
474 | DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status, |
475 | p->iov.size); | |
476 | return usbredir_handle_status(dev, status, p->iov.size); | |
69354a83 HG |
477 | } |
478 | } | |
479 | ||
480 | static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep) | |
481 | { | |
482 | struct usb_redir_stop_iso_stream_header stop_iso_stream = { | |
483 | .endpoint = ep | |
484 | }; | |
485 | if (dev->endpoint[EP2I(ep)].iso_started) { | |
486 | usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream); | |
487 | DPRINTF("iso stream stopped ep %02X\n", ep); | |
488 | dev->endpoint[EP2I(ep)].iso_started = 0; | |
489 | } | |
2bd836e5 | 490 | dev->endpoint[EP2I(ep)].iso_error = 0; |
69354a83 HG |
491 | usbredir_free_bufpq(dev, ep); |
492 | } | |
493 | ||
494 | static int usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p, | |
495 | uint8_t ep) | |
496 | { | |
497 | AsyncURB *aurb = async_alloc(dev, p); | |
498 | struct usb_redir_bulk_packet_header bulk_packet; | |
499 | ||
4f4321c1 GH |
500 | DPRINTF("bulk-out ep %02X len %zd id %u\n", ep, |
501 | p->iov.size, aurb->packet_id); | |
69354a83 HG |
502 | |
503 | bulk_packet.endpoint = ep; | |
4f4321c1 | 504 | bulk_packet.length = p->iov.size; |
69354a83 HG |
505 | bulk_packet.stream_id = 0; |
506 | aurb->bulk_packet = bulk_packet; | |
507 | ||
508 | if (ep & USB_DIR_IN) { | |
509 | usbredirparser_send_bulk_packet(dev->parser, aurb->packet_id, | |
510 | &bulk_packet, NULL, 0); | |
511 | } else { | |
4f4321c1 GH |
512 | uint8_t buf[p->iov.size]; |
513 | usb_packet_copy(p, buf, p->iov.size); | |
514 | usbredir_log_data(dev, "bulk data out:", buf, p->iov.size); | |
69354a83 | 515 | usbredirparser_send_bulk_packet(dev->parser, aurb->packet_id, |
4f4321c1 | 516 | &bulk_packet, buf, p->iov.size); |
69354a83 HG |
517 | } |
518 | usbredirparser_do_write(dev->parser); | |
519 | return USB_RET_ASYNC; | |
520 | } | |
521 | ||
522 | static int usbredir_handle_interrupt_data(USBRedirDevice *dev, | |
523 | USBPacket *p, uint8_t ep) | |
524 | { | |
525 | if (ep & USB_DIR_IN) { | |
526 | /* Input interrupt endpoint, buffered packet input */ | |
527 | struct buf_packet *intp; | |
528 | int status, len; | |
529 | ||
530 | if (!dev->endpoint[EP2I(ep)].interrupt_started && | |
531 | !dev->endpoint[EP2I(ep)].interrupt_error) { | |
532 | struct usb_redir_start_interrupt_receiving_header start_int = { | |
533 | .endpoint = ep, | |
534 | }; | |
535 | /* No id, we look at the ep when receiving a status back */ | |
536 | usbredirparser_send_start_interrupt_receiving(dev->parser, 0, | |
537 | &start_int); | |
538 | usbredirparser_do_write(dev->parser); | |
539 | DPRINTF("interrupt recv started ep %02X\n", ep); | |
540 | dev->endpoint[EP2I(ep)].interrupt_started = 1; | |
81fd7b74 HG |
541 | /* We don't really want to drop interrupt packets ever, but |
542 | having some upper limit to how much we buffer is good. */ | |
543 | dev->endpoint[EP2I(ep)].bufpq_target_size = 1000; | |
544 | dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0; | |
69354a83 HG |
545 | } |
546 | ||
547 | intp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq); | |
548 | if (intp == NULL) { | |
549 | DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep); | |
550 | /* Check interrupt_error for stream errors */ | |
551 | status = dev->endpoint[EP2I(ep)].interrupt_error; | |
552 | dev->endpoint[EP2I(ep)].interrupt_error = 0; | |
e6472210 HG |
553 | if (status) { |
554 | return usbredir_handle_status(dev, status, 0); | |
555 | } | |
556 | return USB_RET_NAK; | |
69354a83 HG |
557 | } |
558 | DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep, | |
559 | intp->status, intp->len); | |
560 | ||
561 | status = intp->status; | |
562 | if (status != usb_redir_success) { | |
563 | bufp_free(dev, intp, ep); | |
564 | return usbredir_handle_status(dev, status, 0); | |
565 | } | |
566 | ||
567 | len = intp->len; | |
4f4321c1 | 568 | if (len > p->iov.size) { |
69354a83 HG |
569 | ERROR("received int data is larger then packet ep %02X\n", ep); |
570 | bufp_free(dev, intp, ep); | |
4d819a9b | 571 | return USB_RET_BABBLE; |
69354a83 | 572 | } |
4f4321c1 | 573 | usb_packet_copy(p, intp->data, len); |
69354a83 HG |
574 | bufp_free(dev, intp, ep); |
575 | return len; | |
576 | } else { | |
577 | /* Output interrupt endpoint, normal async operation */ | |
578 | AsyncURB *aurb = async_alloc(dev, p); | |
579 | struct usb_redir_interrupt_packet_header interrupt_packet; | |
4f4321c1 | 580 | uint8_t buf[p->iov.size]; |
69354a83 | 581 | |
4f4321c1 | 582 | DPRINTF("interrupt-out ep %02X len %zd id %u\n", ep, p->iov.size, |
69354a83 HG |
583 | aurb->packet_id); |
584 | ||
585 | interrupt_packet.endpoint = ep; | |
4f4321c1 | 586 | interrupt_packet.length = p->iov.size; |
69354a83 HG |
587 | aurb->interrupt_packet = interrupt_packet; |
588 | ||
4f4321c1 GH |
589 | usb_packet_copy(p, buf, p->iov.size); |
590 | usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size); | |
69354a83 | 591 | usbredirparser_send_interrupt_packet(dev->parser, aurb->packet_id, |
4f4321c1 | 592 | &interrupt_packet, buf, p->iov.size); |
69354a83 HG |
593 | usbredirparser_do_write(dev->parser); |
594 | return USB_RET_ASYNC; | |
595 | } | |
596 | } | |
597 | ||
598 | static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev, | |
599 | uint8_t ep) | |
600 | { | |
601 | struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = { | |
602 | .endpoint = ep | |
603 | }; | |
604 | if (dev->endpoint[EP2I(ep)].interrupt_started) { | |
605 | usbredirparser_send_stop_interrupt_receiving(dev->parser, 0, | |
606 | &stop_interrupt_recv); | |
607 | DPRINTF("interrupt recv stopped ep %02X\n", ep); | |
608 | dev->endpoint[EP2I(ep)].interrupt_started = 0; | |
609 | } | |
2bd836e5 | 610 | dev->endpoint[EP2I(ep)].interrupt_error = 0; |
69354a83 HG |
611 | usbredir_free_bufpq(dev, ep); |
612 | } | |
613 | ||
614 | static int usbredir_handle_data(USBDevice *udev, USBPacket *p) | |
615 | { | |
616 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
617 | uint8_t ep; | |
618 | ||
079d0b7f | 619 | ep = p->ep->nr; |
69354a83 HG |
620 | if (p->pid == USB_TOKEN_IN) { |
621 | ep |= USB_DIR_IN; | |
622 | } | |
623 | ||
624 | switch (dev->endpoint[EP2I(ep)].type) { | |
625 | case USB_ENDPOINT_XFER_CONTROL: | |
626 | ERROR("handle_data called for control transfer on ep %02X\n", ep); | |
627 | return USB_RET_NAK; | |
628 | case USB_ENDPOINT_XFER_ISOC: | |
629 | return usbredir_handle_iso_data(dev, p, ep); | |
630 | case USB_ENDPOINT_XFER_BULK: | |
3a93113a | 631 | return usbredir_handle_bulk_data(dev, p, ep); |
69354a83 | 632 | case USB_ENDPOINT_XFER_INT: |
3a93113a | 633 | return usbredir_handle_interrupt_data(dev, p, ep); |
69354a83 HG |
634 | default: |
635 | ERROR("handle_data ep %02X has unknown type %d\n", ep, | |
636 | dev->endpoint[EP2I(ep)].type); | |
637 | return USB_RET_NAK; | |
638 | } | |
639 | } | |
640 | ||
641 | static int usbredir_set_config(USBRedirDevice *dev, USBPacket *p, | |
642 | int config) | |
643 | { | |
644 | struct usb_redir_set_configuration_header set_config; | |
645 | AsyncURB *aurb = async_alloc(dev, p); | |
646 | int i; | |
647 | ||
648 | DPRINTF("set config %d id %u\n", config, aurb->packet_id); | |
649 | ||
650 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
651 | switch (dev->endpoint[i].type) { | |
652 | case USB_ENDPOINT_XFER_ISOC: | |
653 | usbredir_stop_iso_stream(dev, I2EP(i)); | |
654 | break; | |
655 | case USB_ENDPOINT_XFER_INT: | |
656 | if (i & 0x10) { | |
657 | usbredir_stop_interrupt_receiving(dev, I2EP(i)); | |
658 | } | |
659 | break; | |
660 | } | |
661 | usbredir_free_bufpq(dev, I2EP(i)); | |
662 | } | |
663 | ||
664 | set_config.configuration = config; | |
665 | usbredirparser_send_set_configuration(dev->parser, aurb->packet_id, | |
666 | &set_config); | |
667 | usbredirparser_do_write(dev->parser); | |
668 | return USB_RET_ASYNC; | |
669 | } | |
670 | ||
671 | static int usbredir_get_config(USBRedirDevice *dev, USBPacket *p) | |
672 | { | |
673 | AsyncURB *aurb = async_alloc(dev, p); | |
674 | ||
675 | DPRINTF("get config id %u\n", aurb->packet_id); | |
676 | ||
677 | aurb->get = 1; | |
678 | usbredirparser_send_get_configuration(dev->parser, aurb->packet_id); | |
679 | usbredirparser_do_write(dev->parser); | |
680 | return USB_RET_ASYNC; | |
681 | } | |
682 | ||
683 | static int usbredir_set_interface(USBRedirDevice *dev, USBPacket *p, | |
684 | int interface, int alt) | |
685 | { | |
686 | struct usb_redir_set_alt_setting_header set_alt; | |
687 | AsyncURB *aurb = async_alloc(dev, p); | |
688 | int i; | |
689 | ||
690 | DPRINTF("set interface %d alt %d id %u\n", interface, alt, | |
691 | aurb->packet_id); | |
692 | ||
693 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
694 | if (dev->endpoint[i].interface == interface) { | |
695 | switch (dev->endpoint[i].type) { | |
696 | case USB_ENDPOINT_XFER_ISOC: | |
697 | usbredir_stop_iso_stream(dev, I2EP(i)); | |
698 | break; | |
699 | case USB_ENDPOINT_XFER_INT: | |
700 | if (i & 0x10) { | |
701 | usbredir_stop_interrupt_receiving(dev, I2EP(i)); | |
702 | } | |
703 | break; | |
704 | } | |
705 | usbredir_free_bufpq(dev, I2EP(i)); | |
706 | } | |
707 | } | |
708 | ||
709 | set_alt.interface = interface; | |
710 | set_alt.alt = alt; | |
711 | usbredirparser_send_set_alt_setting(dev->parser, aurb->packet_id, | |
712 | &set_alt); | |
713 | usbredirparser_do_write(dev->parser); | |
714 | return USB_RET_ASYNC; | |
715 | } | |
716 | ||
717 | static int usbredir_get_interface(USBRedirDevice *dev, USBPacket *p, | |
718 | int interface) | |
719 | { | |
720 | struct usb_redir_get_alt_setting_header get_alt; | |
721 | AsyncURB *aurb = async_alloc(dev, p); | |
722 | ||
723 | DPRINTF("get interface %d id %u\n", interface, aurb->packet_id); | |
724 | ||
725 | get_alt.interface = interface; | |
726 | aurb->get = 1; | |
727 | usbredirparser_send_get_alt_setting(dev->parser, aurb->packet_id, | |
728 | &get_alt); | |
729 | usbredirparser_do_write(dev->parser); | |
730 | return USB_RET_ASYNC; | |
731 | } | |
732 | ||
733 | static int usbredir_handle_control(USBDevice *udev, USBPacket *p, | |
734 | int request, int value, int index, int length, uint8_t *data) | |
735 | { | |
736 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
737 | struct usb_redir_control_packet_header control_packet; | |
738 | AsyncURB *aurb; | |
739 | ||
740 | /* Special cases for certain standard device requests */ | |
741 | switch (request) { | |
742 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: | |
743 | DPRINTF("set address %d\n", value); | |
744 | dev->dev.addr = value; | |
745 | return 0; | |
746 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: | |
747 | return usbredir_set_config(dev, p, value & 0xff); | |
748 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: | |
749 | return usbredir_get_config(dev, p); | |
750 | case InterfaceOutRequest | USB_REQ_SET_INTERFACE: | |
751 | return usbredir_set_interface(dev, p, index, value); | |
752 | case InterfaceRequest | USB_REQ_GET_INTERFACE: | |
753 | return usbredir_get_interface(dev, p, index); | |
754 | } | |
755 | ||
756 | /* "Normal" ctrl requests */ | |
757 | aurb = async_alloc(dev, p); | |
758 | ||
759 | /* Note request is (bRequestType << 8) | bRequest */ | |
760 | DPRINTF("ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %u\n", | |
761 | request >> 8, request & 0xff, value, index, length, | |
762 | aurb->packet_id); | |
763 | ||
764 | control_packet.request = request & 0xFF; | |
765 | control_packet.requesttype = request >> 8; | |
766 | control_packet.endpoint = control_packet.requesttype & USB_DIR_IN; | |
767 | control_packet.value = value; | |
768 | control_packet.index = index; | |
769 | control_packet.length = length; | |
770 | aurb->control_packet = control_packet; | |
771 | ||
772 | if (control_packet.requesttype & USB_DIR_IN) { | |
773 | usbredirparser_send_control_packet(dev->parser, aurb->packet_id, | |
774 | &control_packet, NULL, 0); | |
775 | } else { | |
776 | usbredir_log_data(dev, "ctrl data out:", data, length); | |
777 | usbredirparser_send_control_packet(dev->parser, aurb->packet_id, | |
778 | &control_packet, data, length); | |
779 | } | |
780 | usbredirparser_do_write(dev->parser); | |
781 | return USB_RET_ASYNC; | |
782 | } | |
783 | ||
784 | /* | |
785 | * Close events can be triggered by usbredirparser_do_write which gets called | |
786 | * from within the USBDevice data / control packet callbacks and doing a | |
787 | * usb_detach from within these callbacks is not a good idea. | |
788 | * | |
789 | * So we use a bh handler to take care of close events. We also handle | |
790 | * open events from this callback to make sure that a close directly followed | |
791 | * by an open gets handled in the right order. | |
792 | */ | |
793 | static void usbredir_open_close_bh(void *opaque) | |
794 | { | |
795 | USBRedirDevice *dev = opaque; | |
6af16589 | 796 | uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, }; |
69354a83 HG |
797 | |
798 | usbredir_device_disconnect(dev); | |
799 | ||
800 | if (dev->parser) { | |
801 | usbredirparser_destroy(dev->parser); | |
802 | dev->parser = NULL; | |
803 | } | |
804 | ||
805 | if (dev->cs->opened) { | |
806 | dev->parser = qemu_oom_check(usbredirparser_create()); | |
807 | dev->parser->priv = dev; | |
808 | dev->parser->log_func = usbredir_log; | |
809 | dev->parser->read_func = usbredir_read; | |
810 | dev->parser->write_func = usbredir_write; | |
097a66ef | 811 | dev->parser->hello_func = usbredir_hello; |
69354a83 HG |
812 | dev->parser->device_connect_func = usbredir_device_connect; |
813 | dev->parser->device_disconnect_func = usbredir_device_disconnect; | |
814 | dev->parser->interface_info_func = usbredir_interface_info; | |
815 | dev->parser->ep_info_func = usbredir_ep_info; | |
816 | dev->parser->configuration_status_func = usbredir_configuration_status; | |
817 | dev->parser->alt_setting_status_func = usbredir_alt_setting_status; | |
818 | dev->parser->iso_stream_status_func = usbredir_iso_stream_status; | |
819 | dev->parser->interrupt_receiving_status_func = | |
820 | usbredir_interrupt_receiving_status; | |
821 | dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status; | |
822 | dev->parser->control_packet_func = usbredir_control_packet; | |
823 | dev->parser->bulk_packet_func = usbredir_bulk_packet; | |
824 | dev->parser->iso_packet_func = usbredir_iso_packet; | |
825 | dev->parser->interrupt_packet_func = usbredir_interrupt_packet; | |
826 | dev->read_buf = NULL; | |
827 | dev->read_buf_size = 0; | |
6af16589 HG |
828 | |
829 | usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version); | |
097a66ef | 830 | usbredirparser_caps_set_cap(caps, usb_redir_cap_filter); |
6af16589 | 831 | usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE, 0); |
69354a83 HG |
832 | usbredirparser_do_write(dev->parser); |
833 | } | |
834 | } | |
835 | ||
836 | static void usbredir_do_attach(void *opaque) | |
837 | { | |
838 | USBRedirDevice *dev = opaque; | |
839 | ||
714f9db0 HG |
840 | if (usb_device_attach(&dev->dev) != 0) { |
841 | usbredir_device_disconnect(dev); | |
842 | if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) { | |
843 | usbredirparser_send_filter_reject(dev->parser); | |
844 | usbredirparser_do_write(dev->parser); | |
845 | } | |
846 | } | |
69354a83 HG |
847 | } |
848 | ||
849 | /* | |
850 | * chardev callbacks | |
851 | */ | |
852 | ||
853 | static int usbredir_chardev_can_read(void *opaque) | |
854 | { | |
855 | USBRedirDevice *dev = opaque; | |
856 | ||
857 | if (dev->parser) { | |
858 | /* usbredir_parser_do_read will consume *all* data we give it */ | |
859 | return 1024 * 1024; | |
860 | } else { | |
861 | /* usbredir_open_close_bh hasn't handled the open event yet */ | |
862 | return 0; | |
863 | } | |
864 | } | |
865 | ||
866 | static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size) | |
867 | { | |
868 | USBRedirDevice *dev = opaque; | |
869 | ||
870 | /* No recursion allowed! */ | |
871 | assert(dev->read_buf == NULL); | |
872 | ||
873 | dev->read_buf = buf; | |
874 | dev->read_buf_size = size; | |
875 | ||
876 | usbredirparser_do_read(dev->parser); | |
877 | /* Send any acks, etc. which may be queued now */ | |
878 | usbredirparser_do_write(dev->parser); | |
879 | } | |
880 | ||
881 | static void usbredir_chardev_event(void *opaque, int event) | |
882 | { | |
883 | USBRedirDevice *dev = opaque; | |
884 | ||
885 | switch (event) { | |
886 | case CHR_EVENT_OPENED: | |
887 | case CHR_EVENT_CLOSED: | |
888 | qemu_bh_schedule(dev->open_close_bh); | |
889 | break; | |
890 | } | |
891 | } | |
892 | ||
893 | /* | |
894 | * init + destroy | |
895 | */ | |
896 | ||
897 | static int usbredir_initfn(USBDevice *udev) | |
898 | { | |
899 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
900 | int i; | |
901 | ||
902 | if (dev->cs == NULL) { | |
903 | qerror_report(QERR_MISSING_PARAMETER, "chardev"); | |
904 | return -1; | |
905 | } | |
906 | ||
6af16589 HG |
907 | if (dev->filter_str) { |
908 | i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|", | |
909 | &dev->filter_rules, | |
910 | &dev->filter_rules_count); | |
911 | if (i) { | |
912 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "filter", | |
913 | "a usb device filter string"); | |
914 | return -1; | |
915 | } | |
916 | } | |
917 | ||
69354a83 HG |
918 | dev->open_close_bh = qemu_bh_new(usbredir_open_close_bh, dev); |
919 | dev->attach_timer = qemu_new_timer_ms(vm_clock, usbredir_do_attach, dev); | |
920 | ||
921 | QTAILQ_INIT(&dev->asyncq); | |
922 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
923 | QTAILQ_INIT(&dev->endpoint[i].bufpq); | |
924 | } | |
925 | ||
926 | /* We'll do the attach once we receive the speed from the usb-host */ | |
927 | udev->auto_attach = 0; | |
928 | ||
65f9d986 HG |
929 | /* Let the backend know we are ready */ |
930 | qemu_chr_fe_open(dev->cs); | |
69354a83 HG |
931 | qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read, |
932 | usbredir_chardev_read, usbredir_chardev_event, dev); | |
933 | ||
65bb3a5c | 934 | add_boot_device_path(dev->bootindex, &udev->qdev, NULL); |
69354a83 HG |
935 | return 0; |
936 | } | |
937 | ||
938 | static void usbredir_cleanup_device_queues(USBRedirDevice *dev) | |
939 | { | |
940 | AsyncURB *aurb, *next_aurb; | |
941 | int i; | |
942 | ||
943 | QTAILQ_FOREACH_SAFE(aurb, &dev->asyncq, next, next_aurb) { | |
944 | async_free(dev, aurb); | |
945 | } | |
946 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
947 | usbredir_free_bufpq(dev, I2EP(i)); | |
948 | } | |
949 | } | |
950 | ||
951 | static void usbredir_handle_destroy(USBDevice *udev) | |
952 | { | |
953 | USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev); | |
954 | ||
65f9d986 | 955 | qemu_chr_fe_close(dev->cs); |
70f24fb6 | 956 | qemu_chr_delete(dev->cs); |
69354a83 HG |
957 | /* Note must be done after qemu_chr_close, as that causes a close event */ |
958 | qemu_bh_delete(dev->open_close_bh); | |
959 | ||
960 | qemu_del_timer(dev->attach_timer); | |
961 | qemu_free_timer(dev->attach_timer); | |
962 | ||
963 | usbredir_cleanup_device_queues(dev); | |
964 | ||
965 | if (dev->parser) { | |
966 | usbredirparser_destroy(dev->parser); | |
967 | } | |
6af16589 HG |
968 | |
969 | free(dev->filter_rules); | |
970 | } | |
971 | ||
972 | static int usbredir_check_filter(USBRedirDevice *dev) | |
973 | { | |
1510168e | 974 | if (dev->interface_info.interface_count == NO_INTERFACE_INFO) { |
6af16589 | 975 | ERROR("No interface info for device\n"); |
5b3bd682 | 976 | goto error; |
6af16589 HG |
977 | } |
978 | ||
979 | if (dev->filter_rules) { | |
980 | if (!usbredirparser_peer_has_cap(dev->parser, | |
981 | usb_redir_cap_connect_device_version)) { | |
982 | ERROR("Device filter specified and peer does not have the " | |
983 | "connect_device_version capability\n"); | |
5b3bd682 | 984 | goto error; |
6af16589 HG |
985 | } |
986 | ||
987 | if (usbredirfilter_check( | |
988 | dev->filter_rules, | |
989 | dev->filter_rules_count, | |
990 | dev->device_info.device_class, | |
991 | dev->device_info.device_subclass, | |
992 | dev->device_info.device_protocol, | |
993 | dev->interface_info.interface_class, | |
994 | dev->interface_info.interface_subclass, | |
995 | dev->interface_info.interface_protocol, | |
996 | dev->interface_info.interface_count, | |
997 | dev->device_info.vendor_id, | |
998 | dev->device_info.product_id, | |
999 | dev->device_info.device_version_bcd, | |
1000 | 0) != 0) { | |
5b3bd682 | 1001 | goto error; |
6af16589 HG |
1002 | } |
1003 | } | |
1004 | ||
1005 | return 0; | |
5b3bd682 HG |
1006 | |
1007 | error: | |
1008 | usbredir_device_disconnect(dev); | |
097a66ef HG |
1009 | if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) { |
1010 | usbredirparser_send_filter_reject(dev->parser); | |
1011 | usbredirparser_do_write(dev->parser); | |
1012 | } | |
5b3bd682 | 1013 | return -1; |
69354a83 HG |
1014 | } |
1015 | ||
1016 | /* | |
1017 | * usbredirparser packet complete callbacks | |
1018 | */ | |
1019 | ||
1020 | static int usbredir_handle_status(USBRedirDevice *dev, | |
1021 | int status, int actual_len) | |
1022 | { | |
1023 | switch (status) { | |
1024 | case usb_redir_success: | |
1025 | return actual_len; | |
1026 | case usb_redir_stall: | |
1027 | return USB_RET_STALL; | |
1028 | case usb_redir_cancelled: | |
1029 | WARNING("returning cancelled packet to HC?\n"); | |
d61000a8 | 1030 | return USB_RET_NAK; |
69354a83 | 1031 | case usb_redir_inval: |
d61000a8 HG |
1032 | WARNING("got invalid param error from usb-host?\n"); |
1033 | return USB_RET_NAK; | |
69354a83 HG |
1034 | case usb_redir_ioerror: |
1035 | case usb_redir_timeout: | |
1036 | default: | |
d61000a8 | 1037 | return USB_RET_IOERROR; |
69354a83 HG |
1038 | } |
1039 | } | |
1040 | ||
097a66ef HG |
1041 | static void usbredir_hello(void *priv, struct usb_redir_hello_header *h) |
1042 | { | |
1043 | USBRedirDevice *dev = priv; | |
1044 | ||
1045 | /* Try to send the filter info now that we've the usb-host's caps */ | |
1046 | if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) && | |
1047 | dev->filter_rules) { | |
1048 | usbredirparser_send_filter_filter(dev->parser, dev->filter_rules, | |
1049 | dev->filter_rules_count); | |
1050 | usbredirparser_do_write(dev->parser); | |
1051 | } | |
1052 | } | |
1053 | ||
69354a83 HG |
1054 | static void usbredir_device_connect(void *priv, |
1055 | struct usb_redir_device_connect_header *device_connect) | |
1056 | { | |
1057 | USBRedirDevice *dev = priv; | |
6af16589 | 1058 | const char *speed; |
69354a83 | 1059 | |
99f08100 HG |
1060 | if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) { |
1061 | ERROR("Received device connect while already connected\n"); | |
1062 | return; | |
1063 | } | |
1064 | ||
69354a83 HG |
1065 | switch (device_connect->speed) { |
1066 | case usb_redir_speed_low: | |
6af16589 | 1067 | speed = "low speed"; |
69354a83 HG |
1068 | dev->dev.speed = USB_SPEED_LOW; |
1069 | break; | |
1070 | case usb_redir_speed_full: | |
6af16589 | 1071 | speed = "full speed"; |
69354a83 HG |
1072 | dev->dev.speed = USB_SPEED_FULL; |
1073 | break; | |
1074 | case usb_redir_speed_high: | |
6af16589 | 1075 | speed = "high speed"; |
69354a83 HG |
1076 | dev->dev.speed = USB_SPEED_HIGH; |
1077 | break; | |
1078 | case usb_redir_speed_super: | |
6af16589 | 1079 | speed = "super speed"; |
69354a83 HG |
1080 | dev->dev.speed = USB_SPEED_SUPER; |
1081 | break; | |
1082 | default: | |
6af16589 | 1083 | speed = "unknown speed"; |
69354a83 HG |
1084 | dev->dev.speed = USB_SPEED_FULL; |
1085 | } | |
6af16589 HG |
1086 | |
1087 | if (usbredirparser_peer_has_cap(dev->parser, | |
1088 | usb_redir_cap_connect_device_version)) { | |
1089 | INFO("attaching %s device %04x:%04x version %d.%d class %02x\n", | |
1090 | speed, device_connect->vendor_id, device_connect->product_id, | |
52234bc0 HG |
1091 | ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 + |
1092 | ((device_connect->device_version_bcd & 0x0f00) >> 8), | |
1093 | ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 + | |
1094 | ((device_connect->device_version_bcd & 0x000f) >> 0), | |
6af16589 HG |
1095 | device_connect->device_class); |
1096 | } else { | |
1097 | INFO("attaching %s device %04x:%04x class %02x\n", speed, | |
1098 | device_connect->vendor_id, device_connect->product_id, | |
1099 | device_connect->device_class); | |
1100 | } | |
1101 | ||
69354a83 | 1102 | dev->dev.speedmask = (1 << dev->dev.speed); |
6af16589 HG |
1103 | dev->device_info = *device_connect; |
1104 | ||
1105 | if (usbredir_check_filter(dev)) { | |
1106 | WARNING("Device %04x:%04x rejected by device filter, not attaching\n", | |
1107 | device_connect->vendor_id, device_connect->product_id); | |
1108 | return; | |
1109 | } | |
1110 | ||
69354a83 HG |
1111 | qemu_mod_timer(dev->attach_timer, dev->next_attach_time); |
1112 | } | |
1113 | ||
1114 | static void usbredir_device_disconnect(void *priv) | |
1115 | { | |
1116 | USBRedirDevice *dev = priv; | |
99f08100 | 1117 | int i; |
69354a83 HG |
1118 | |
1119 | /* Stop any pending attaches */ | |
1120 | qemu_del_timer(dev->attach_timer); | |
1121 | ||
1122 | if (dev->dev.attached) { | |
1123 | usb_device_detach(&dev->dev); | |
69354a83 HG |
1124 | /* |
1125 | * Delay next usb device attach to give the guest a chance to see | |
1126 | * see the detach / attach in case of quick close / open succession | |
1127 | */ | |
1128 | dev->next_attach_time = qemu_get_clock_ms(vm_clock) + 200; | |
1129 | } | |
99f08100 HG |
1130 | |
1131 | /* Reset state so that the next dev connected starts with a clean slate */ | |
1132 | usbredir_cleanup_device_queues(dev); | |
1133 | memset(dev->endpoint, 0, sizeof(dev->endpoint)); | |
1134 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
1135 | QTAILQ_INIT(&dev->endpoint[i].bufpq); | |
1136 | } | |
0454b611 | 1137 | usb_ep_init(&dev->dev); |
1510168e | 1138 | dev->interface_info.interface_count = NO_INTERFACE_INFO; |
69354a83 HG |
1139 | } |
1140 | ||
1141 | static void usbredir_interface_info(void *priv, | |
1142 | struct usb_redir_interface_info_header *interface_info) | |
1143 | { | |
6af16589 HG |
1144 | USBRedirDevice *dev = priv; |
1145 | ||
1146 | dev->interface_info = *interface_info; | |
1147 | ||
1148 | /* | |
1149 | * If we receive interface info after the device has already been | |
1150 | * connected (ie on a set_config), re-check the filter. | |
1151 | */ | |
1152 | if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) { | |
1153 | if (usbredir_check_filter(dev)) { | |
1154 | ERROR("Device no longer matches filter after interface info " | |
1155 | "change, disconnecting!\n"); | |
6af16589 HG |
1156 | } |
1157 | } | |
69354a83 HG |
1158 | } |
1159 | ||
1160 | static void usbredir_ep_info(void *priv, | |
1161 | struct usb_redir_ep_info_header *ep_info) | |
1162 | { | |
1163 | USBRedirDevice *dev = priv; | |
0454b611 | 1164 | struct USBEndpoint *usb_ep; |
69354a83 HG |
1165 | int i; |
1166 | ||
1167 | for (i = 0; i < MAX_ENDPOINTS; i++) { | |
1168 | dev->endpoint[i].type = ep_info->type[i]; | |
1169 | dev->endpoint[i].interval = ep_info->interval[i]; | |
1170 | dev->endpoint[i].interface = ep_info->interface[i]; | |
e8a7dd29 HG |
1171 | switch (dev->endpoint[i].type) { |
1172 | case usb_redir_type_invalid: | |
1173 | break; | |
1174 | case usb_redir_type_iso: | |
1175 | case usb_redir_type_interrupt: | |
1176 | if (dev->endpoint[i].interval == 0) { | |
1177 | ERROR("Received 0 interval for isoc or irq endpoint\n"); | |
1178 | usbredir_device_disconnect(dev); | |
1179 | } | |
1180 | /* Fall through */ | |
1181 | case usb_redir_type_control: | |
1182 | case usb_redir_type_bulk: | |
69354a83 HG |
1183 | DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i), |
1184 | dev->endpoint[i].type, dev->endpoint[i].interface); | |
e8a7dd29 HG |
1185 | break; |
1186 | default: | |
1187 | ERROR("Received invalid endpoint type\n"); | |
1188 | usbredir_device_disconnect(dev); | |
0454b611 | 1189 | return; |
69354a83 | 1190 | } |
0454b611 HG |
1191 | usb_ep = usb_ep_get(&dev->dev, |
1192 | (i & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, | |
1193 | i & 0x0f); | |
1194 | usb_ep->type = dev->endpoint[i].type; | |
1195 | usb_ep->ifnum = dev->endpoint[i].interface; | |
69354a83 HG |
1196 | } |
1197 | } | |
1198 | ||
1199 | static void usbredir_configuration_status(void *priv, uint32_t id, | |
1200 | struct usb_redir_configuration_status_header *config_status) | |
1201 | { | |
1202 | USBRedirDevice *dev = priv; | |
1203 | AsyncURB *aurb; | |
1204 | int len = 0; | |
1205 | ||
1206 | DPRINTF("set config status %d config %d id %u\n", config_status->status, | |
1207 | config_status->configuration, id); | |
1208 | ||
1209 | aurb = async_find(dev, id); | |
1210 | if (!aurb) { | |
1211 | return; | |
1212 | } | |
1213 | if (aurb->packet) { | |
1214 | if (aurb->get) { | |
1215 | dev->dev.data_buf[0] = config_status->configuration; | |
1216 | len = 1; | |
1217 | } | |
4f4321c1 | 1218 | aurb->packet->result = |
69354a83 HG |
1219 | usbredir_handle_status(dev, config_status->status, len); |
1220 | usb_generic_async_ctrl_complete(&dev->dev, aurb->packet); | |
1221 | } | |
1222 | async_free(dev, aurb); | |
1223 | } | |
1224 | ||
1225 | static void usbredir_alt_setting_status(void *priv, uint32_t id, | |
1226 | struct usb_redir_alt_setting_status_header *alt_setting_status) | |
1227 | { | |
1228 | USBRedirDevice *dev = priv; | |
1229 | AsyncURB *aurb; | |
1230 | int len = 0; | |
1231 | ||
1232 | DPRINTF("alt status %d intf %d alt %d id: %u\n", | |
1233 | alt_setting_status->status, | |
1234 | alt_setting_status->interface, | |
1235 | alt_setting_status->alt, id); | |
1236 | ||
1237 | aurb = async_find(dev, id); | |
1238 | if (!aurb) { | |
1239 | return; | |
1240 | } | |
1241 | if (aurb->packet) { | |
1242 | if (aurb->get) { | |
1243 | dev->dev.data_buf[0] = alt_setting_status->alt; | |
1244 | len = 1; | |
1245 | } | |
4f4321c1 | 1246 | aurb->packet->result = |
69354a83 HG |
1247 | usbredir_handle_status(dev, alt_setting_status->status, len); |
1248 | usb_generic_async_ctrl_complete(&dev->dev, aurb->packet); | |
1249 | } | |
1250 | async_free(dev, aurb); | |
1251 | } | |
1252 | ||
1253 | static void usbredir_iso_stream_status(void *priv, uint32_t id, | |
1254 | struct usb_redir_iso_stream_status_header *iso_stream_status) | |
1255 | { | |
1256 | USBRedirDevice *dev = priv; | |
1257 | uint8_t ep = iso_stream_status->endpoint; | |
1258 | ||
1259 | DPRINTF("iso status %d ep %02X id %u\n", iso_stream_status->status, | |
1260 | ep, id); | |
1261 | ||
2bd836e5 | 1262 | if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) { |
99f08100 HG |
1263 | return; |
1264 | } | |
1265 | ||
69354a83 HG |
1266 | dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status; |
1267 | if (iso_stream_status->status == usb_redir_stall) { | |
1268 | DPRINTF("iso stream stopped by peer ep %02X\n", ep); | |
1269 | dev->endpoint[EP2I(ep)].iso_started = 0; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | static void usbredir_interrupt_receiving_status(void *priv, uint32_t id, | |
1274 | struct usb_redir_interrupt_receiving_status_header | |
1275 | *interrupt_receiving_status) | |
1276 | { | |
1277 | USBRedirDevice *dev = priv; | |
1278 | uint8_t ep = interrupt_receiving_status->endpoint; | |
1279 | ||
1280 | DPRINTF("interrupt recv status %d ep %02X id %u\n", | |
1281 | interrupt_receiving_status->status, ep, id); | |
1282 | ||
2bd836e5 | 1283 | if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) { |
99f08100 HG |
1284 | return; |
1285 | } | |
1286 | ||
69354a83 HG |
1287 | dev->endpoint[EP2I(ep)].interrupt_error = |
1288 | interrupt_receiving_status->status; | |
1289 | if (interrupt_receiving_status->status == usb_redir_stall) { | |
1290 | DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep); | |
1291 | dev->endpoint[EP2I(ep)].interrupt_started = 0; | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | static void usbredir_bulk_streams_status(void *priv, uint32_t id, | |
1296 | struct usb_redir_bulk_streams_status_header *bulk_streams_status) | |
1297 | { | |
1298 | } | |
1299 | ||
1300 | static void usbredir_control_packet(void *priv, uint32_t id, | |
1301 | struct usb_redir_control_packet_header *control_packet, | |
1302 | uint8_t *data, int data_len) | |
1303 | { | |
1304 | USBRedirDevice *dev = priv; | |
1305 | int len = control_packet->length; | |
1306 | AsyncURB *aurb; | |
1307 | ||
1308 | DPRINTF("ctrl-in status %d len %d id %u\n", control_packet->status, | |
1309 | len, id); | |
1310 | ||
1311 | aurb = async_find(dev, id); | |
1312 | if (!aurb) { | |
1313 | free(data); | |
1314 | return; | |
1315 | } | |
1316 | ||
1317 | aurb->control_packet.status = control_packet->status; | |
1318 | aurb->control_packet.length = control_packet->length; | |
1319 | if (memcmp(&aurb->control_packet, control_packet, | |
1320 | sizeof(*control_packet))) { | |
1321 | ERROR("return control packet mismatch, please report this!\n"); | |
1322 | len = USB_RET_NAK; | |
1323 | } | |
1324 | ||
1325 | if (aurb->packet) { | |
1326 | len = usbredir_handle_status(dev, control_packet->status, len); | |
1327 | if (len > 0) { | |
1328 | usbredir_log_data(dev, "ctrl data in:", data, data_len); | |
1329 | if (data_len <= sizeof(dev->dev.data_buf)) { | |
1330 | memcpy(dev->dev.data_buf, data, data_len); | |
1331 | } else { | |
1332 | ERROR("ctrl buffer too small (%d > %zu)\n", | |
1333 | data_len, sizeof(dev->dev.data_buf)); | |
1334 | len = USB_RET_STALL; | |
1335 | } | |
1336 | } | |
4f4321c1 | 1337 | aurb->packet->result = len; |
69354a83 HG |
1338 | usb_generic_async_ctrl_complete(&dev->dev, aurb->packet); |
1339 | } | |
1340 | async_free(dev, aurb); | |
1341 | free(data); | |
1342 | } | |
1343 | ||
1344 | static void usbredir_bulk_packet(void *priv, uint32_t id, | |
1345 | struct usb_redir_bulk_packet_header *bulk_packet, | |
1346 | uint8_t *data, int data_len) | |
1347 | { | |
1348 | USBRedirDevice *dev = priv; | |
1349 | uint8_t ep = bulk_packet->endpoint; | |
1350 | int len = bulk_packet->length; | |
1351 | AsyncURB *aurb; | |
1352 | ||
1353 | DPRINTF("bulk-in status %d ep %02X len %d id %u\n", bulk_packet->status, | |
1354 | ep, len, id); | |
1355 | ||
1356 | aurb = async_find(dev, id); | |
1357 | if (!aurb) { | |
1358 | free(data); | |
1359 | return; | |
1360 | } | |
1361 | ||
1362 | if (aurb->bulk_packet.endpoint != bulk_packet->endpoint || | |
1363 | aurb->bulk_packet.stream_id != bulk_packet->stream_id) { | |
1364 | ERROR("return bulk packet mismatch, please report this!\n"); | |
1365 | len = USB_RET_NAK; | |
1366 | } | |
1367 | ||
1368 | if (aurb->packet) { | |
1369 | len = usbredir_handle_status(dev, bulk_packet->status, len); | |
1370 | if (len > 0) { | |
1371 | usbredir_log_data(dev, "bulk data in:", data, data_len); | |
4f4321c1 GH |
1372 | if (data_len <= aurb->packet->iov.size) { |
1373 | usb_packet_copy(aurb->packet, data, data_len); | |
69354a83 | 1374 | } else { |
4f4321c1 GH |
1375 | ERROR("bulk buffer too small (%d > %zd)\n", data_len, |
1376 | aurb->packet->iov.size); | |
69354a83 HG |
1377 | len = USB_RET_STALL; |
1378 | } | |
1379 | } | |
4f4321c1 | 1380 | aurb->packet->result = len; |
69354a83 HG |
1381 | usb_packet_complete(&dev->dev, aurb->packet); |
1382 | } | |
1383 | async_free(dev, aurb); | |
1384 | free(data); | |
1385 | } | |
1386 | ||
1387 | static void usbredir_iso_packet(void *priv, uint32_t id, | |
1388 | struct usb_redir_iso_packet_header *iso_packet, | |
1389 | uint8_t *data, int data_len) | |
1390 | { | |
1391 | USBRedirDevice *dev = priv; | |
1392 | uint8_t ep = iso_packet->endpoint; | |
1393 | ||
1394 | DPRINTF2("iso-in status %d ep %02X len %d id %u\n", iso_packet->status, ep, | |
1395 | data_len, id); | |
1396 | ||
1397 | if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) { | |
1398 | ERROR("received iso packet for non iso endpoint %02X\n", ep); | |
1399 | free(data); | |
1400 | return; | |
1401 | } | |
1402 | ||
1403 | if (dev->endpoint[EP2I(ep)].iso_started == 0) { | |
1404 | DPRINTF("received iso packet for non started stream ep %02X\n", ep); | |
1405 | free(data); | |
1406 | return; | |
1407 | } | |
1408 | ||
1409 | /* bufp_alloc also adds the packet to the ep queue */ | |
1410 | bufp_alloc(dev, data, data_len, iso_packet->status, ep); | |
1411 | } | |
1412 | ||
1413 | static void usbredir_interrupt_packet(void *priv, uint32_t id, | |
1414 | struct usb_redir_interrupt_packet_header *interrupt_packet, | |
1415 | uint8_t *data, int data_len) | |
1416 | { | |
1417 | USBRedirDevice *dev = priv; | |
1418 | uint8_t ep = interrupt_packet->endpoint; | |
1419 | ||
1420 | DPRINTF("interrupt-in status %d ep %02X len %d id %u\n", | |
1421 | interrupt_packet->status, ep, data_len, id); | |
1422 | ||
1423 | if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) { | |
1424 | ERROR("received int packet for non interrupt endpoint %02X\n", ep); | |
1425 | free(data); | |
1426 | return; | |
1427 | } | |
1428 | ||
1429 | if (ep & USB_DIR_IN) { | |
1430 | if (dev->endpoint[EP2I(ep)].interrupt_started == 0) { | |
1431 | DPRINTF("received int packet while not started ep %02X\n", ep); | |
1432 | free(data); | |
1433 | return; | |
1434 | } | |
1435 | ||
1436 | /* bufp_alloc also adds the packet to the ep queue */ | |
1437 | bufp_alloc(dev, data, data_len, interrupt_packet->status, ep); | |
1438 | } else { | |
1439 | int len = interrupt_packet->length; | |
1440 | ||
1441 | AsyncURB *aurb = async_find(dev, id); | |
1442 | if (!aurb) { | |
1443 | return; | |
1444 | } | |
1445 | ||
1446 | if (aurb->interrupt_packet.endpoint != interrupt_packet->endpoint) { | |
1447 | ERROR("return int packet mismatch, please report this!\n"); | |
1448 | len = USB_RET_NAK; | |
1449 | } | |
1450 | ||
1451 | if (aurb->packet) { | |
4f4321c1 | 1452 | aurb->packet->result = usbredir_handle_status(dev, |
69354a83 HG |
1453 | interrupt_packet->status, len); |
1454 | usb_packet_complete(&dev->dev, aurb->packet); | |
1455 | } | |
1456 | async_free(dev, aurb); | |
1457 | } | |
1458 | } | |
1459 | ||
3bc36349 AL |
1460 | static Property usbredir_properties[] = { |
1461 | DEFINE_PROP_CHR("chardev", USBRedirDevice, cs), | |
1462 | DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, 0), | |
6af16589 | 1463 | DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str), |
65bb3a5c | 1464 | DEFINE_PROP_INT32("bootindex", USBRedirDevice, bootindex, -1), |
3bc36349 AL |
1465 | DEFINE_PROP_END_OF_LIST(), |
1466 | }; | |
1467 | ||
62aed765 AL |
1468 | static void usbredir_class_initfn(ObjectClass *klass, void *data) |
1469 | { | |
1470 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
3bc36349 | 1471 | DeviceClass *dc = DEVICE_CLASS(klass); |
62aed765 AL |
1472 | |
1473 | uc->init = usbredir_initfn; | |
1474 | uc->product_desc = "USB Redirection Device"; | |
1475 | uc->handle_destroy = usbredir_handle_destroy; | |
62aed765 AL |
1476 | uc->cancel_packet = usbredir_cancel_packet; |
1477 | uc->handle_reset = usbredir_handle_reset; | |
1478 | uc->handle_data = usbredir_handle_data; | |
1479 | uc->handle_control = usbredir_handle_control; | |
3bc36349 | 1480 | dc->props = usbredir_properties; |
62aed765 AL |
1481 | } |
1482 | ||
3bc36349 AL |
1483 | static TypeInfo usbredir_dev_info = { |
1484 | .name = "usb-redir", | |
1485 | .parent = TYPE_USB_DEVICE, | |
1486 | .instance_size = sizeof(USBRedirDevice), | |
1487 | .class_init = usbredir_class_initfn, | |
69354a83 HG |
1488 | }; |
1489 | ||
83f7d43a | 1490 | static void usbredir_register_types(void) |
69354a83 | 1491 | { |
3bc36349 | 1492 | type_register_static(&usbredir_dev_info); |
69354a83 | 1493 | } |
83f7d43a AF |
1494 | |
1495 | type_init(usbredir_register_types) |