migration: Fix analyze-migration.py when ignore-shared is used
[qemu/kevin.git] / hw / usb / core.c
blob975f76250a1a34b79252975b13724368577240f4
1 /*
2 * QEMU USB emulation
4 * Copyright (c) 2005 Fabrice Bellard
6 * 2008 Generic packet handler rewrite by Max Krasnyansky
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 #include "qemu/osdep.h"
27 #include "hw/usb.h"
28 #include "qemu/iov.h"
29 #include "trace.h"
31 void usb_pick_speed(USBPort *port)
33 static const int speeds[] = {
34 USB_SPEED_SUPER,
35 USB_SPEED_HIGH,
36 USB_SPEED_FULL,
37 USB_SPEED_LOW,
39 USBDevice *udev = port->dev;
40 int i;
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;
51 void usb_attach(USBPort *port)
53 USBDevice *dev = port->dev;
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);
64 void usb_detach(USBPort *port)
66 USBDevice *dev = port->dev;
68 assert(dev != NULL);
69 assert(dev->state != USB_STATE_NOTATTACHED);
70 port->ops->detach(port);
71 dev->state = USB_STATE_NOTATTACHED;
74 void usb_port_reset(USBPort *port)
76 USBDevice *dev = port->dev;
78 assert(dev != NULL);
79 usb_detach(port);
80 usb_attach(port);
81 usb_device_reset(dev);
84 void usb_device_reset(USBDevice *dev)
86 if (dev == NULL || !dev->attached) {
87 return;
89 usb_device_handle_reset(dev);
90 dev->remote_wakeup = 0;
91 dev->addr = 0;
92 dev->state = USB_STATE_DEFAULT;
95 void usb_wakeup(USBEndpoint *ep, unsigned int stream)
97 USBDevice *dev = ep->dev;
98 USBBus *bus = usb_bus_from_device(dev);
100 if (!phase_check(PHASE_MACHINE_READY)) {
102 * This is machine init cold plug. No need to wakeup anyone,
103 * all devices will be reset anyway. And trying to wakeup can
104 * cause problems due to hitting uninitialized devices.
106 return;
108 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
109 dev->port->ops->wakeup(dev->port);
111 if (bus->ops->wakeup_endpoint) {
112 bus->ops->wakeup_endpoint(bus, ep, stream);
116 /**********************/
118 /* generic USB device helpers (you are not forced to use them when
119 writing your USB device driver, but they help handling the
120 protocol)
123 #define SETUP_STATE_IDLE 0
124 #define SETUP_STATE_SETUP 1
125 #define SETUP_STATE_DATA 2
126 #define SETUP_STATE_ACK 3
127 #define SETUP_STATE_PARAM 4
129 static void do_token_setup(USBDevice *s, USBPacket *p)
131 int request, value, index;
132 unsigned int setup_len;
134 if (p->iov.size != 8) {
135 p->status = USB_RET_STALL;
136 return;
139 usb_packet_copy(p, s->setup_buf, p->iov.size);
140 s->setup_index = 0;
141 p->actual_length = 0;
142 setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
143 if (setup_len > sizeof(s->data_buf)) {
144 fprintf(stderr,
145 "usb_generic_handle_packet: ctrl buffer too small (%u > %zu)\n",
146 setup_len, sizeof(s->data_buf));
147 p->status = USB_RET_STALL;
148 return;
150 s->setup_len = setup_len;
152 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
153 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
154 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
156 if (s->setup_buf[0] & USB_DIR_IN) {
157 usb_pcap_ctrl(p, true);
158 usb_device_handle_control(s, p, request, value, index,
159 s->setup_len, s->data_buf);
160 if (p->status == USB_RET_ASYNC) {
161 s->setup_state = SETUP_STATE_SETUP;
163 if (p->status != USB_RET_SUCCESS) {
164 return;
167 if (p->actual_length < s->setup_len) {
168 s->setup_len = p->actual_length;
170 s->setup_state = SETUP_STATE_DATA;
171 } else {
172 if (s->setup_len == 0)
173 s->setup_state = SETUP_STATE_ACK;
174 else
175 s->setup_state = SETUP_STATE_DATA;
178 p->actual_length = 8;
181 static void do_token_in(USBDevice *s, USBPacket *p)
183 int request, value, index;
185 assert(p->ep->nr == 0);
187 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
188 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
189 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
191 switch(s->setup_state) {
192 case SETUP_STATE_ACK:
193 if (!(s->setup_buf[0] & USB_DIR_IN)) {
194 usb_pcap_ctrl(p, true);
195 usb_device_handle_control(s, p, request, value, index,
196 s->setup_len, s->data_buf);
197 if (p->status == USB_RET_ASYNC) {
198 return;
200 s->setup_state = SETUP_STATE_IDLE;
201 p->actual_length = 0;
202 usb_pcap_ctrl(p, false);
204 break;
206 case SETUP_STATE_DATA:
207 if (s->setup_buf[0] & USB_DIR_IN) {
208 int len = s->setup_len - s->setup_index;
209 if (len > p->iov.size) {
210 len = p->iov.size;
212 usb_packet_copy(p, s->data_buf + s->setup_index, len);
213 s->setup_index += len;
214 if (s->setup_index >= s->setup_len) {
215 s->setup_state = SETUP_STATE_ACK;
217 return;
219 s->setup_state = SETUP_STATE_IDLE;
220 p->status = USB_RET_STALL;
221 usb_pcap_ctrl(p, false);
222 break;
224 default:
225 p->status = USB_RET_STALL;
229 static void do_token_out(USBDevice *s, USBPacket *p)
231 assert(p->ep->nr == 0);
233 switch(s->setup_state) {
234 case SETUP_STATE_ACK:
235 if (s->setup_buf[0] & USB_DIR_IN) {
236 s->setup_state = SETUP_STATE_IDLE;
237 usb_pcap_ctrl(p, false);
238 /* transfer OK */
239 } else {
240 /* ignore additional output */
242 break;
244 case SETUP_STATE_DATA:
245 if (!(s->setup_buf[0] & USB_DIR_IN)) {
246 int len = s->setup_len - s->setup_index;
247 if (len > p->iov.size) {
248 len = p->iov.size;
250 usb_packet_copy(p, s->data_buf + s->setup_index, len);
251 s->setup_index += len;
252 if (s->setup_index >= s->setup_len) {
253 s->setup_state = SETUP_STATE_ACK;
255 return;
257 s->setup_state = SETUP_STATE_IDLE;
258 p->status = USB_RET_STALL;
259 usb_pcap_ctrl(p, false);
260 break;
262 default:
263 p->status = USB_RET_STALL;
267 static void do_parameter(USBDevice *s, USBPacket *p)
269 int i, request, value, index;
270 unsigned int setup_len;
272 for (i = 0; i < 8; i++) {
273 s->setup_buf[i] = p->parameter >> (i*8);
276 s->setup_state = SETUP_STATE_PARAM;
277 s->setup_index = 0;
279 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
280 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
281 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
283 setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
284 if (setup_len > sizeof(s->data_buf)) {
285 fprintf(stderr,
286 "usb_generic_handle_packet: ctrl buffer too small (%u > %zu)\n",
287 setup_len, sizeof(s->data_buf));
288 p->status = USB_RET_STALL;
289 return;
291 s->setup_len = setup_len;
293 if (p->pid == USB_TOKEN_OUT) {
294 usb_packet_copy(p, s->data_buf, s->setup_len);
297 usb_pcap_ctrl(p, true);
298 usb_device_handle_control(s, p, request, value, index,
299 s->setup_len, s->data_buf);
300 if (p->status == USB_RET_ASYNC) {
301 return;
304 if (p->actual_length < s->setup_len) {
305 s->setup_len = p->actual_length;
307 if (p->pid == USB_TOKEN_IN) {
308 p->actual_length = 0;
309 usb_packet_copy(p, s->data_buf, s->setup_len);
311 usb_pcap_ctrl(p, false);
314 /* ctrl complete function for devices which use usb_generic_handle_packet and
315 may return USB_RET_ASYNC from their handle_control callback. Device code
316 which does this *must* call this function instead of the normal
317 usb_packet_complete to complete their async control packets. */
318 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
320 if (p->status < 0) {
321 s->setup_state = SETUP_STATE_IDLE;
322 usb_pcap_ctrl(p, false);
325 switch (s->setup_state) {
326 case SETUP_STATE_SETUP:
327 if (p->actual_length < s->setup_len) {
328 s->setup_len = p->actual_length;
330 s->setup_state = SETUP_STATE_DATA;
331 p->actual_length = 8;
332 break;
334 case SETUP_STATE_ACK:
335 s->setup_state = SETUP_STATE_IDLE;
336 p->actual_length = 0;
337 usb_pcap_ctrl(p, false);
338 break;
340 case SETUP_STATE_PARAM:
341 if (p->actual_length < s->setup_len) {
342 s->setup_len = p->actual_length;
344 if (p->pid == USB_TOKEN_IN) {
345 p->actual_length = 0;
346 usb_packet_copy(p, s->data_buf, s->setup_len);
348 break;
350 default:
351 break;
353 usb_packet_complete(s, p);
356 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
358 USBDevice *dev = port->dev;
360 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
361 return NULL;
363 if (dev->addr == addr) {
364 return dev;
366 return usb_device_find_device(dev, addr);
369 static void usb_process_one(USBPacket *p)
371 USBDevice *dev = p->ep->dev;
372 bool nak;
375 * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
376 * can be USB_RET_NAK here from a previous usb_process_one() call,
377 * or USB_RET_ASYNC from going through usb_queue_one().
379 nak = (p->status == USB_RET_NAK);
380 p->status = USB_RET_SUCCESS;
382 if (p->ep->nr == 0) {
383 /* control pipe */
384 if (p->parameter) {
385 do_parameter(dev, p);
386 return;
388 switch (p->pid) {
389 case USB_TOKEN_SETUP:
390 do_token_setup(dev, p);
391 break;
392 case USB_TOKEN_IN:
393 do_token_in(dev, p);
394 break;
395 case USB_TOKEN_OUT:
396 do_token_out(dev, p);
397 break;
398 default:
399 p->status = USB_RET_STALL;
401 } else {
402 /* data pipe */
403 if (!nak) {
404 usb_pcap_data(p, true);
406 usb_device_handle_data(dev, p);
410 static void usb_queue_one(USBPacket *p)
412 usb_packet_set_state(p, USB_PACKET_QUEUED);
413 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
414 p->status = USB_RET_ASYNC;
417 /* Hand over a packet to a device for processing. p->status ==
418 USB_RET_ASYNC indicates the processing isn't finished yet, the
419 driver will call usb_packet_complete() when done processing it. */
420 void usb_handle_packet(USBDevice *dev, USBPacket *p)
422 if (dev == NULL) {
423 p->status = USB_RET_NODEV;
424 return;
426 assert(dev == p->ep->dev);
427 assert(dev->state == USB_STATE_DEFAULT);
428 usb_packet_check_state(p, USB_PACKET_SETUP);
429 assert(p->ep != NULL);
431 /* Submitting a new packet clears halt */
432 if (p->ep->halted) {
433 assert(QTAILQ_EMPTY(&p->ep->queue));
434 p->ep->halted = false;
437 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
438 usb_process_one(p);
439 if (p->status == USB_RET_ASYNC) {
440 /* hcd drivers cannot handle async for isoc */
441 assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
442 /* using async for interrupt packets breaks migration */
443 assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
444 (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
445 usb_packet_set_state(p, USB_PACKET_ASYNC);
446 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
447 } else if (p->status == USB_RET_ADD_TO_QUEUE) {
448 usb_queue_one(p);
449 } else {
451 * When pipelining is enabled usb-devices must always return async,
452 * otherwise packets can complete out of order!
454 assert(p->stream || !p->ep->pipeline ||
455 QTAILQ_EMPTY(&p->ep->queue));
456 if (p->status != USB_RET_NAK) {
457 usb_pcap_data(p, false);
458 usb_packet_set_state(p, USB_PACKET_COMPLETE);
461 } else {
462 usb_queue_one(p);
466 void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
468 USBEndpoint *ep = p->ep;
470 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
471 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
473 if (p->status != USB_RET_SUCCESS ||
474 (p->short_not_ok && (p->actual_length < p->iov.size))) {
475 ep->halted = true;
477 usb_pcap_data(p, false);
478 usb_packet_set_state(p, USB_PACKET_COMPLETE);
479 QTAILQ_REMOVE(&ep->queue, p, queue);
480 dev->port->ops->complete(dev->port, p);
483 /* Notify the controller that an async packet is complete. This should only
484 be called for packets previously deferred by returning USB_RET_ASYNC from
485 handle_packet. */
486 void usb_packet_complete(USBDevice *dev, USBPacket *p)
488 USBEndpoint *ep = p->ep;
490 usb_packet_check_state(p, USB_PACKET_ASYNC);
491 usb_packet_complete_one(dev, p);
493 while (!QTAILQ_EMPTY(&ep->queue)) {
494 p = QTAILQ_FIRST(&ep->queue);
495 if (ep->halted) {
496 /* Empty the queue on a halt */
497 p->status = USB_RET_REMOVE_FROM_QUEUE;
498 dev->port->ops->complete(dev->port, p);
499 continue;
501 if (p->state == USB_PACKET_ASYNC) {
502 break;
504 usb_packet_check_state(p, USB_PACKET_QUEUED);
505 usb_process_one(p);
506 if (p->status == USB_RET_ASYNC) {
507 usb_packet_set_state(p, USB_PACKET_ASYNC);
508 break;
510 usb_packet_complete_one(ep->dev, p);
514 /* Cancel an active packet. The packed must have been deferred by
515 returning USB_RET_ASYNC from handle_packet, and not yet
516 completed. */
517 void usb_cancel_packet(USBPacket * p)
519 bool callback = (p->state == USB_PACKET_ASYNC);
520 assert(usb_packet_is_inflight(p));
521 usb_packet_set_state(p, USB_PACKET_CANCELED);
522 QTAILQ_REMOVE(&p->ep->queue, p, queue);
523 if (callback) {
524 usb_device_cancel_packet(p->ep->dev, p);
529 void usb_packet_init(USBPacket *p)
531 qemu_iovec_init(&p->iov, 1);
534 static const char *usb_packet_state_name(USBPacketState state)
536 static const char *name[] = {
537 [USB_PACKET_UNDEFINED] = "undef",
538 [USB_PACKET_SETUP] = "setup",
539 [USB_PACKET_QUEUED] = "queued",
540 [USB_PACKET_ASYNC] = "async",
541 [USB_PACKET_COMPLETE] = "complete",
542 [USB_PACKET_CANCELED] = "canceled",
544 if (state < ARRAY_SIZE(name)) {
545 return name[state];
547 return "INVALID";
550 void usb_packet_check_state(USBPacket *p, USBPacketState expected)
552 USBDevice *dev;
553 USBBus *bus;
555 if (p->state == expected) {
556 return;
558 dev = p->ep->dev;
559 bus = usb_bus_from_device(dev);
560 trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
561 usb_packet_state_name(p->state),
562 usb_packet_state_name(expected));
563 assert(!"usb packet state check failed");
566 void usb_packet_set_state(USBPacket *p, USBPacketState state)
568 if (p->ep) {
569 USBDevice *dev = p->ep->dev;
570 USBBus *bus = usb_bus_from_device(dev);
571 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
572 usb_packet_state_name(p->state),
573 usb_packet_state_name(state));
574 } else {
575 trace_usb_packet_state_change(-1, "", -1, p,
576 usb_packet_state_name(p->state),
577 usb_packet_state_name(state));
579 p->state = state;
582 void usb_packet_setup(USBPacket *p, int pid,
583 USBEndpoint *ep, unsigned int stream,
584 uint64_t id, bool short_not_ok, bool int_req)
586 assert(!usb_packet_is_inflight(p));
587 assert(p->iov.iov != NULL);
588 p->id = id;
589 p->pid = pid;
590 p->ep = ep;
591 p->stream = stream;
592 p->status = USB_RET_SUCCESS;
593 p->actual_length = 0;
594 p->parameter = 0;
595 p->short_not_ok = short_not_ok;
596 p->int_req = int_req;
597 p->combined = NULL;
598 qemu_iovec_reset(&p->iov);
599 usb_packet_set_state(p, USB_PACKET_SETUP);
602 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
604 qemu_iovec_add(&p->iov, ptr, len);
607 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
609 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
611 assert(p->actual_length >= 0);
612 assert(p->actual_length + bytes <= iov->size);
613 switch (p->pid) {
614 case USB_TOKEN_SETUP:
615 case USB_TOKEN_OUT:
616 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
617 break;
618 case USB_TOKEN_IN:
619 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
620 break;
621 default:
622 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
623 abort();
625 p->actual_length += bytes;
628 void usb_packet_skip(USBPacket *p, size_t bytes)
630 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
632 assert(p->actual_length >= 0);
633 assert(p->actual_length + bytes <= iov->size);
634 if (p->pid == USB_TOKEN_IN) {
635 iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
637 p->actual_length += bytes;
640 size_t usb_packet_size(USBPacket *p)
642 return p->combined ? p->combined->iov.size : p->iov.size;
645 void usb_packet_cleanup(USBPacket *p)
647 assert(!usb_packet_is_inflight(p));
648 qemu_iovec_destroy(&p->iov);
651 void usb_ep_reset(USBDevice *dev)
653 int ep;
655 dev->ep_ctl.nr = 0;
656 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
657 dev->ep_ctl.ifnum = 0;
658 dev->ep_ctl.max_packet_size = 64;
659 dev->ep_ctl.max_streams = 0;
660 dev->ep_ctl.dev = dev;
661 dev->ep_ctl.pipeline = false;
662 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
663 dev->ep_in[ep].nr = ep + 1;
664 dev->ep_out[ep].nr = ep + 1;
665 dev->ep_in[ep].pid = USB_TOKEN_IN;
666 dev->ep_out[ep].pid = USB_TOKEN_OUT;
667 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
668 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
669 dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
670 dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
671 dev->ep_in[ep].max_packet_size = 0;
672 dev->ep_out[ep].max_packet_size = 0;
673 dev->ep_in[ep].max_streams = 0;
674 dev->ep_out[ep].max_streams = 0;
675 dev->ep_in[ep].dev = dev;
676 dev->ep_out[ep].dev = dev;
677 dev->ep_in[ep].pipeline = false;
678 dev->ep_out[ep].pipeline = false;
682 void usb_ep_init(USBDevice *dev)
684 int ep;
686 usb_ep_reset(dev);
687 QTAILQ_INIT(&dev->ep_ctl.queue);
688 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
689 QTAILQ_INIT(&dev->ep_in[ep].queue);
690 QTAILQ_INIT(&dev->ep_out[ep].queue);
694 void usb_ep_dump(USBDevice *dev)
696 static const char *tname[] = {
697 [USB_ENDPOINT_XFER_CONTROL] = "control",
698 [USB_ENDPOINT_XFER_ISOC] = "isoc",
699 [USB_ENDPOINT_XFER_BULK] = "bulk",
700 [USB_ENDPOINT_XFER_INT] = "int",
702 int ifnum, ep, first;
704 fprintf(stderr, "Device \"%s\", config %d\n",
705 dev->product_desc, dev->configuration);
706 for (ifnum = 0; ifnum < 16; ifnum++) {
707 first = 1;
708 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
709 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
710 dev->ep_in[ep].ifnum == ifnum) {
711 if (first) {
712 first = 0;
713 fprintf(stderr, " Interface %d, alternative %d\n",
714 ifnum, dev->altsetting[ifnum]);
716 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
717 tname[dev->ep_in[ep].type],
718 dev->ep_in[ep].max_packet_size);
720 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
721 dev->ep_out[ep].ifnum == ifnum) {
722 if (first) {
723 first = 0;
724 fprintf(stderr, " Interface %d, alternative %d\n",
725 ifnum, dev->altsetting[ifnum]);
727 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
728 tname[dev->ep_out[ep].type],
729 dev->ep_out[ep].max_packet_size);
733 fprintf(stderr, "--\n");
736 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
738 struct USBEndpoint *eps;
740 assert(dev != NULL);
741 if (ep == 0) {
742 return &dev->ep_ctl;
744 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
745 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
746 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
747 return eps + ep - 1;
750 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
752 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
753 return uep->type;
756 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
758 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
759 uep->type = type;
762 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
764 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
765 uep->ifnum = ifnum;
768 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
769 uint16_t raw)
771 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
772 int size, microframes;
774 size = raw & 0x7ff;
775 switch ((raw >> 11) & 3) {
776 case 1:
777 microframes = 2;
778 break;
779 case 2:
780 microframes = 3;
781 break;
782 default:
783 microframes = 1;
784 break;
786 uep->max_packet_size = size * microframes;
789 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
791 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
792 int MaxStreams;
794 MaxStreams = raw & 0x1f;
795 if (MaxStreams) {
796 uep->max_streams = 1 << MaxStreams;
797 } else {
798 uep->max_streams = 0;
802 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
804 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
805 uep->halted = halted;
808 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
809 uint64_t id)
811 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
812 USBPacket *p;
814 QTAILQ_FOREACH(p, &uep->queue, queue) {
815 if (p->id == id) {
816 return p;
820 return NULL;