master
c 832 lines 23.6 KB
Raw
1 /*
2 * QEMU USB emulation
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5 *
6 * 2008 Generic packet handler rewrite by Max Krasnyansky
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26 #include "qemu/osdep.h"
27 #include "hw/usb/usb.h"
28 #include "qemu/iov.h"
29 #include "qemu/log.h"
30 #include "trace.h"
31
32 void usb_pick_speed(USBPort *port)
33 {
34 static const int speeds[] = {
35 USB_SPEED_SUPER,
36 USB_SPEED_HIGH,
37 USB_SPEED_FULL,
38 USB_SPEED_LOW,
39 };
40 USBDevice *udev = port->dev;
41 int i;
42
43 for (i = 0; i < ARRAY_SIZE(speeds); i++) {
44 if ((udev->speedmask & (1 << speeds[i])) &&
45 (port->speedmask & (1 << speeds[i]))) {
46 udev->speed = speeds[i];
47 return;
48 }
49 }
50 }
51
52 void usb_attach(USBPort *port)
53 {
54 USBDevice *dev = port->dev;
55
56 assert(dev != NULL);
57 assert(dev->attached);
58 assert(dev->state == USB_STATE_NOTATTACHED);
59 usb_pick_speed(port);
60 port->ops->attach(port);
61 dev->state = USB_STATE_ATTACHED;
62 usb_device_handle_attach(dev);
63 }
64
65 void usb_detach(USBPort *port)
66 {
67 USBDevice *dev = port->dev;
68
69 assert(dev != NULL);
70 assert(dev->state != USB_STATE_NOTATTACHED);
71 port->ops->detach(port);
72 dev->state = USB_STATE_NOTATTACHED;
73 }
74
75 void usb_port_reset(USBPort *port)
76 {
77 USBDevice *dev = port->dev;
78
79 assert(dev != NULL);
80 usb_detach(port);
81 usb_attach(port);
82 usb_device_reset(dev);
83 }
84
85 void usb_device_reset(USBDevice *dev)
86 {
87 if (dev == NULL || !dev->attached) {
88 return;
89 }
90 usb_device_handle_reset(dev);
91 dev->remote_wakeup = 0;
92 dev->addr = 0;
93 dev->state = USB_STATE_DEFAULT;
94 }
95
96 void usb_wakeup(USBEndpoint *ep, unsigned int stream)
97 {
98 USBDevice *dev = ep->dev;
99 USBBus *bus = usb_bus_from_device(dev);
100
101 if (!phase_check(PHASE_MACHINE_READY)) {
102 /*
103 * This is machine init cold plug. No need to wakeup anyone,
104 * all devices will be reset anyway. And trying to wakeup can
105 * cause problems due to hitting uninitialized devices.
106 */
107 return;
108 }
109 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
110 dev->port->ops->wakeup(dev->port);
111 }
112 if (bus->ops->wakeup_endpoint) {
113 bus->ops->wakeup_endpoint(bus, ep, stream);
114 }
115 }
116
117 /**********************/
118
119 /* generic USB device helpers (you are not forced to use them when
120 writing your USB device driver, but they help handling the
121 protocol)
122 */
123
124 #define SETUP_STATE_IDLE 0
125 #define SETUP_STATE_SETUP 1
126 #define SETUP_STATE_DATA 2
127 #define SETUP_STATE_ACK 3
128 #define SETUP_STATE_PARAM 4
129
130 static void do_token_setup(USBDevice *s, USBPacket *p)
131 {
132 int request, value, index;
133 unsigned int setup_len;
134
135 if (p->iov.size != 8) {
136 p->status = USB_RET_STALL;
137 return;
138 }
139
140 usb_packet_copy(p, s->setup_buf, p->iov.size);
141 s->setup_index = 0;
142 p->actual_length = 0;
143 setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
144 if (setup_len > sizeof(s->data_buf)) {
145 fprintf(stderr,
146 "usb_generic_handle_packet: ctrl buffer too small (%u > %zu)\n",
147 setup_len, sizeof(s->data_buf));
148 p->status = USB_RET_STALL;
149 return;
150 }
151 s->setup_len = setup_len;
152
153 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
154 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
155 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
156
157 if (s->setup_buf[0] & USB_DIR_IN) {
158 usb_pcap_ctrl(p, true);
159 usb_device_handle_control(s, p, request, value, index,
160 s->setup_len, s->data_buf);
161 if (p->status == USB_RET_ASYNC) {
162 s->setup_state = SETUP_STATE_SETUP;
163 }
164 if (p->status != USB_RET_SUCCESS) {
165 return;
166 }
167
168 if (p->actual_length < s->setup_len) {
169 s->setup_len = p->actual_length;
170 }
171 s->setup_state = SETUP_STATE_DATA;
172 } else {
173 if (s->setup_len == 0)
174 s->setup_state = SETUP_STATE_ACK;
175 else
176 s->setup_state = SETUP_STATE_DATA;
177 }
178
179 p->actual_length = 8;
180 }
181
182 static void do_token_in(USBDevice *s, USBPacket *p)
183 {
184 int request, value, index;
185
186 assert(p->ep->nr == 0);
187
188 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
189 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
190 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
191
192 switch(s->setup_state) {
193 case SETUP_STATE_ACK:
194 if (!(s->setup_buf[0] & USB_DIR_IN)) {
195 usb_pcap_ctrl(p, true);
196 usb_device_handle_control(s, p, request, value, index,
197 s->setup_len, s->data_buf);
198 if (p->status == USB_RET_ASYNC) {
199 return;
200 }
201 s->setup_state = SETUP_STATE_IDLE;
202 p->actual_length = 0;
203 usb_pcap_ctrl(p, false);
204 }
205 break;
206
207 case SETUP_STATE_DATA:
208 if (s->setup_buf[0] & USB_DIR_IN) {
209 int len = s->setup_len - s->setup_index;
210 if (len > p->iov.size) {
211 len = p->iov.size;
212 }
213 usb_packet_copy(p, s->data_buf + s->setup_index, len);
214 s->setup_index += len;
215 if (s->setup_index >= s->setup_len) {
216 s->setup_state = SETUP_STATE_ACK;
217 }
218 return;
219 }
220 s->setup_state = SETUP_STATE_IDLE;
221 p->status = USB_RET_STALL;
222 usb_pcap_ctrl(p, false);
223 break;
224
225 default:
226 p->status = USB_RET_STALL;
227 }
228 }
229
230 static void do_token_out(USBDevice *s, USBPacket *p)
231 {
232 assert(p->ep->nr == 0);
233
234 switch(s->setup_state) {
235 case SETUP_STATE_ACK:
236 if (s->setup_buf[0] & USB_DIR_IN) {
237 s->setup_state = SETUP_STATE_IDLE;
238 usb_pcap_ctrl(p, false);
239 /* transfer OK */
240 } else {
241 /* ignore additional output */
242 }
243 break;
244
245 case SETUP_STATE_DATA:
246 if (!(s->setup_buf[0] & USB_DIR_IN)) {
247 int len = s->setup_len - s->setup_index;
248 if (len > p->iov.size) {
249 len = p->iov.size;
250 }
251 usb_packet_copy(p, s->data_buf + s->setup_index, len);
252 s->setup_index += len;
253 if (s->setup_index >= s->setup_len) {
254 s->setup_state = SETUP_STATE_ACK;
255 }
256 return;
257 }
258 s->setup_state = SETUP_STATE_IDLE;
259 p->status = USB_RET_STALL;
260 usb_pcap_ctrl(p, false);
261 break;
262
263 default:
264 p->status = USB_RET_STALL;
265 }
266 }
267
268 static void do_parameter(USBDevice *s, USBPacket *p)
269 {
270 int i, request, value, index;
271 unsigned int setup_len;
272
273 for (i = 0; i < 8; i++) {
274 s->setup_buf[i] = p->parameter >> (i*8);
275 }
276
277 s->setup_state = SETUP_STATE_PARAM;
278 s->setup_index = 0;
279
280 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
281 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
282 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
283
284 setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
285 if (setup_len > sizeof(s->data_buf)) {
286 fprintf(stderr,
287 "usb_generic_handle_packet: ctrl buffer too small (%u > %zu)\n",
288 setup_len, sizeof(s->data_buf));
289 p->status = USB_RET_STALL;
290 return;
291 }
292 if ((p->pid == USB_TOKEN_OUT || p->pid == USB_TOKEN_IN) &&
293 setup_len > p->iov.size) {
294 qemu_log_mask(LOG_GUEST_ERROR,
295 "xhci: setup state param length %u > iov size %zu\n",
296 setup_len, p->iov.size);
297 p->status = USB_RET_STALL;
298 return;
299 }
300
301 s->setup_len = setup_len;
302
303 if (p->pid == USB_TOKEN_OUT) {
304 usb_packet_copy(p, s->data_buf, s->setup_len);
305 }
306
307 usb_pcap_ctrl(p, true);
308 usb_device_handle_control(s, p, request, value, index,
309 s->setup_len, s->data_buf);
310 if (p->status == USB_RET_ASYNC) {
311 return;
312 }
313
314 if (p->actual_length < s->setup_len) {
315 s->setup_len = p->actual_length;
316 }
317 if (p->pid == USB_TOKEN_IN) {
318 p->actual_length = 0;
319 usb_packet_copy(p, s->data_buf, s->setup_len);
320 }
321 usb_pcap_ctrl(p, false);
322 }
323
324 /* ctrl complete function for devices which use usb_generic_handle_packet and
325 may return USB_RET_ASYNC from their handle_control callback. Device code
326 which does this *must* call this function instead of the normal
327 usb_packet_complete to complete their async control packets. */
328 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
329 {
330 if (p->status < 0) {
331 s->setup_state = SETUP_STATE_IDLE;
332 usb_pcap_ctrl(p, false);
333 }
334
335 switch (s->setup_state) {
336 case SETUP_STATE_SETUP:
337 if (p->actual_length < s->setup_len) {
338 s->setup_len = p->actual_length;
339 }
340 s->setup_state = SETUP_STATE_DATA;
341 p->actual_length = 8;
342 break;
343
344 case SETUP_STATE_ACK:
345 s->setup_state = SETUP_STATE_IDLE;
346 p->actual_length = 0;
347 usb_pcap_ctrl(p, false);
348 break;
349
350 case SETUP_STATE_PARAM:
351 if (p->actual_length < s->setup_len) {
352 s->setup_len = p->actual_length;
353 }
354 if (p->pid == USB_TOKEN_IN) {
355 p->actual_length = 0;
356 usb_packet_copy(p, s->data_buf, s->setup_len);
357 }
358 usb_pcap_ctrl(p, false);
359 break;
360
361 default:
362 break;
363 }
364 usb_packet_complete(s, p);
365 }
366
367 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
368 {
369 USBDevice *dev = port->dev;
370
371 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
372 return NULL;
373 }
374 if (dev->addr == addr) {
375 return dev;
376 }
377 return usb_device_find_device(dev, addr);
378 }
379
380 static void usb_process_one(USBPacket *p)
381 {
382 USBDevice *dev = p->ep->dev;
383 bool nak;
384
385 /*
386 * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
387 * can be USB_RET_NAK here from a previous usb_process_one() call,
388 * or USB_RET_ASYNC from going through usb_queue_one().
389 */
390 nak = (p->status == USB_RET_NAK);
391 p->status = USB_RET_SUCCESS;
392
393 if (p->ep->nr == 0) {
394 /* control pipe */
395 if (p->parameter) {
396 do_parameter(dev, p);
397 return;
398 }
399 switch (p->pid) {
400 case USB_TOKEN_SETUP:
401 do_token_setup(dev, p);
402 break;
403 case USB_TOKEN_IN:
404 do_token_in(dev, p);
405 break;
406 case USB_TOKEN_OUT:
407 do_token_out(dev, p);
408 break;
409 default:
410 p->status = USB_RET_STALL;
411 }
412 } else {
413 /* data pipe */
414 if (!nak) {
415 usb_pcap_data(p, true);
416 }
417 usb_device_handle_data(dev, p);
418 }
419 }
420
421 static void usb_queue_one(USBPacket *p)
422 {
423 usb_packet_set_state(p, USB_PACKET_QUEUED);
424 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
425 p->status = USB_RET_ASYNC;
426 }
427
428 /* Hand over a packet to a device for processing. p->status ==
429 USB_RET_ASYNC indicates the processing isn't finished yet, the
430 driver will call usb_packet_complete() when done processing it. */
431 void usb_handle_packet(USBDevice *dev, USBPacket *p)
432 {
433 if (dev == NULL) {
434 p->status = USB_RET_NODEV;
435 return;
436 }
437 assert(p->ep);
438 assert(dev == p->ep->dev);
439 assert(dev->state == USB_STATE_DEFAULT);
440 usb_packet_check_state(p, USB_PACKET_SETUP);
441
442 /* Submitting a new packet clears halt */
443 if (p->ep->halted) {
444 assert(QTAILQ_EMPTY(&p->ep->queue));
445 p->ep->halted = false;
446 }
447
448 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
449 usb_process_one(p);
450 if (p->status == USB_RET_ASYNC) {
451 /* hcd drivers cannot handle async for isoc */
452 assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
453 /* using async for interrupt packets breaks migration */
454 assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
455 (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
456 usb_packet_set_state(p, USB_PACKET_ASYNC);
457 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
458 } else if (p->status == USB_RET_ADD_TO_QUEUE) {
459 usb_queue_one(p);
460 } else {
461 /*
462 * When pipelining is enabled usb-devices must always return async,
463 * otherwise packets can complete out of order!
464 */
465 assert(p->stream || !p->ep->pipeline ||
466 QTAILQ_EMPTY(&p->ep->queue));
467 if (p->status != USB_RET_NAK) {
468 usb_pcap_data(p, false);
469 usb_packet_set_state(p, USB_PACKET_COMPLETE);
470 }
471 }
472 } else {
473 usb_queue_one(p);
474 }
475 }
476
477 void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
478 {
479 USBEndpoint *ep = p->ep;
480
481 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
482 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
483
484 if (p->status != USB_RET_SUCCESS ||
485 (p->short_not_ok && (p->actual_length < p->iov.size))) {
486 ep->halted = true;
487 }
488 usb_pcap_data(p, false);
489 usb_packet_set_state(p, USB_PACKET_COMPLETE);
490 QTAILQ_REMOVE(&ep->queue, p, queue);
491 dev->port->ops->complete(dev->port, p);
492 }
493
494 /* Notify the controller that an async packet is complete. This should only
495 be called for packets previously deferred by returning USB_RET_ASYNC from
496 handle_packet. */
497 void usb_packet_complete(USBDevice *dev, USBPacket *p)
498 {
499 USBEndpoint *ep = p->ep;
500
501 usb_packet_check_state(p, USB_PACKET_ASYNC);
502 usb_packet_complete_one(dev, p);
503
504 while (!QTAILQ_EMPTY(&ep->queue)) {
505 p = QTAILQ_FIRST(&ep->queue);
506 if (ep->halted) {
507 /* Empty the queue on a halt */
508 p->status = USB_RET_REMOVE_FROM_QUEUE;
509 dev->port->ops->complete(dev->port, p);
510 continue;
511 }
512 if (p->state == USB_PACKET_ASYNC) {
513 break;
514 }
515 usb_packet_check_state(p, USB_PACKET_QUEUED);
516 usb_process_one(p);
517 if (p->status == USB_RET_ASYNC) {
518 usb_packet_set_state(p, USB_PACKET_ASYNC);
519 break;
520 }
521 usb_packet_complete_one(ep->dev, p);
522 }
523 }
524
525 /* Cancel an active packet. The packed must have been deferred by
526 returning USB_RET_ASYNC from handle_packet, and not yet
527 completed. */
528 void usb_cancel_packet(USBPacket * p)
529 {
530 bool callback = (p->state == USB_PACKET_ASYNC);
531 assert(usb_packet_is_inflight(p));
532 usb_packet_set_state(p, USB_PACKET_CANCELED);
533 QTAILQ_REMOVE(&p->ep->queue, p, queue);
534 if (callback) {
535 usb_device_cancel_packet(p->ep->dev, p);
536 }
537 }
538
539
540 void usb_packet_init(USBPacket *p)
541 {
542 qemu_iovec_init(&p->iov, 1);
543 }
544
545 static const char *usb_packet_state_name(USBPacketState state)
546 {
547 static const char *name[] = {
548 [USB_PACKET_UNDEFINED] = "undef",
549 [USB_PACKET_SETUP] = "setup",
550 [USB_PACKET_QUEUED] = "queued",
551 [USB_PACKET_ASYNC] = "async",
552 [USB_PACKET_COMPLETE] = "complete",
553 [USB_PACKET_CANCELED] = "canceled",
554 };
555 if (state < ARRAY_SIZE(name)) {
556 return name[state];
557 }
558 return "INVALID";
559 }
560
561 void usb_packet_check_state(USBPacket *p, USBPacketState expected)
562 {
563 USBDevice *dev;
564 USBBus *bus;
565
566 if (p->state == expected) {
567 return;
568 }
569 dev = p->ep->dev;
570 bus = usb_bus_from_device(dev);
571 trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
572 usb_packet_state_name(p->state),
573 usb_packet_state_name(expected));
574 assert(!"usb packet state check failed");
575 }
576
577 void usb_packet_set_state(USBPacket *p, USBPacketState state)
578 {
579 if (p->ep) {
580 USBDevice *dev = p->ep->dev;
581 USBBus *bus = usb_bus_from_device(dev);
582 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
583 usb_packet_state_name(p->state),
584 usb_packet_state_name(state));
585 } else {
586 trace_usb_packet_state_change(-1, "", -1, p,
587 usb_packet_state_name(p->state),
588 usb_packet_state_name(state));
589 }
590 p->state = state;
591 }
592
593 void usb_packet_setup(USBPacket *p, int pid,
594 USBEndpoint *ep, unsigned int stream,
595 uint64_t id, bool short_not_ok, bool int_req)
596 {
597 assert(!usb_packet_is_inflight(p));
598 assert(p->iov.iov != NULL);
599 p->id = id;
600 p->pid = pid;
601 p->ep = ep;
602 p->stream = stream;
603 p->status = USB_RET_SUCCESS;
604 p->actual_length = 0;
605 p->parameter = 0;
606 p->short_not_ok = short_not_ok;
607 p->int_req = int_req;
608 p->combined = NULL;
609 qemu_iovec_reset(&p->iov);
610 usb_packet_set_state(p, USB_PACKET_SETUP);
611 }
612
613 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
614 {
615 qemu_iovec_add(&p->iov, ptr, len);
616 }
617
618 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
619 {
620 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
621
622 assert(p->actual_length >= 0);
623 assert(p->actual_length + bytes <= iov->size);
624 switch (p->pid) {
625 case USB_TOKEN_SETUP:
626 case USB_TOKEN_OUT:
627 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
628 break;
629 case USB_TOKEN_IN:
630 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
631 break;
632 default:
633 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
634 abort();
635 }
636 p->actual_length += bytes;
637 }
638
639 void usb_packet_skip(USBPacket *p, size_t bytes)
640 {
641 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
642
643 assert(p->actual_length >= 0);
644 assert(p->actual_length + bytes <= iov->size);
645 if (p->pid == USB_TOKEN_IN) {
646 iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
647 }
648 p->actual_length += bytes;
649 }
650
651 size_t usb_packet_size(USBPacket *p)
652 {
653 return p->combined ? p->combined->iov.size : p->iov.size;
654 }
655
656 void usb_packet_cleanup(USBPacket *p)
657 {
658 assert(!usb_packet_is_inflight(p));
659 qemu_iovec_destroy(&p->iov);
660 }
661
662 void usb_ep_reset(USBDevice *dev)
663 {
664 int ep;
665
666 dev->ep_ctl.nr = 0;
667 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
668 dev->ep_ctl.ifnum = 0;
669 dev->ep_ctl.max_packet_size = 64;
670 dev->ep_ctl.max_streams = 0;
671 dev->ep_ctl.dev = dev;
672 dev->ep_ctl.pipeline = false;
673 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
674 dev->ep_in[ep].nr = ep + 1;
675 dev->ep_out[ep].nr = ep + 1;
676 dev->ep_in[ep].pid = USB_TOKEN_IN;
677 dev->ep_out[ep].pid = USB_TOKEN_OUT;
678 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
679 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
680 dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
681 dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
682 dev->ep_in[ep].max_packet_size = 0;
683 dev->ep_out[ep].max_packet_size = 0;
684 dev->ep_in[ep].max_streams = 0;
685 dev->ep_out[ep].max_streams = 0;
686 dev->ep_in[ep].dev = dev;
687 dev->ep_out[ep].dev = dev;
688 dev->ep_in[ep].pipeline = false;
689 dev->ep_out[ep].pipeline = false;
690 }
691 }
692
693 void usb_ep_init(USBDevice *dev)
694 {
695 int ep;
696
697 usb_ep_reset(dev);
698 QTAILQ_INIT(&dev->ep_ctl.queue);
699 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
700 QTAILQ_INIT(&dev->ep_in[ep].queue);
701 QTAILQ_INIT(&dev->ep_out[ep].queue);
702 }
703 }
704
705 void usb_ep_dump(USBDevice *dev)
706 {
707 static const char *tname[] = {
708 [USB_ENDPOINT_XFER_CONTROL] = "control",
709 [USB_ENDPOINT_XFER_ISOC] = "isoc",
710 [USB_ENDPOINT_XFER_BULK] = "bulk",
711 [USB_ENDPOINT_XFER_INT] = "int",
712 };
713 int ifnum, ep, first;
714
715 fprintf(stderr, "Device \"%s\", config %d\n",
716 dev->product_desc, dev->configuration);
717 for (ifnum = 0; ifnum < 16; ifnum++) {
718 first = 1;
719 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
720 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
721 dev->ep_in[ep].ifnum == ifnum) {
722 if (first) {
723 first = 0;
724 fprintf(stderr, " Interface %d, alternative %d\n",
725 ifnum, dev->altsetting[ifnum]);
726 }
727 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
728 tname[dev->ep_in[ep].type],
729 dev->ep_in[ep].max_packet_size);
730 }
731 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
732 dev->ep_out[ep].ifnum == ifnum) {
733 if (first) {
734 first = 0;
735 fprintf(stderr, " Interface %d, alternative %d\n",
736 ifnum, dev->altsetting[ifnum]);
737 }
738 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
739 tname[dev->ep_out[ep].type],
740 dev->ep_out[ep].max_packet_size);
741 }
742 }
743 }
744 fprintf(stderr, "--\n");
745 }
746
747 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
748 {
749 struct USBEndpoint *eps;
750
751 assert(dev != NULL);
752 if (ep == 0) {
753 return &dev->ep_ctl;
754 }
755 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
756 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
757 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
758 return eps + ep - 1;
759 }
760
761 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
762 {
763 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
764 return uep->type;
765 }
766
767 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
768 {
769 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
770 uep->type = type;
771 }
772
773 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
774 {
775 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
776 uep->ifnum = ifnum;
777 }
778
779 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
780 uint16_t raw)
781 {
782 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
783 int size, microframes;
784
785 size = raw & 0x7ff;
786 switch ((raw >> 11) & 3) {
787 case 1:
788 microframes = 2;
789 break;
790 case 2:
791 microframes = 3;
792 break;
793 default:
794 microframes = 1;
795 break;
796 }
797 uep->max_packet_size = size * microframes;
798 }
799
800 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
801 {
802 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
803 int MaxStreams;
804
805 MaxStreams = raw & 0x1f;
806 if (MaxStreams) {
807 uep->max_streams = 1 << MaxStreams;
808 } else {
809 uep->max_streams = 0;
810 }
811 }
812
813 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
814 {
815 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
816 uep->halted = halted;
817 }
818
819 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
820 uint64_t id)
821 {
822 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
823 USBPacket *p;
824
825 QTAILQ_FOREACH(p, &uep->queue, queue) {
826 if (p->id == id) {
827 return p;
828 }
829 }
830
831 return NULL;
832 }