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