block: Align block status requests
[qemu.git] / hw / usb / core.c
blob241ae66b15059b2963acc1debbe0e50e674497b6
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 "qemu-common.h"
28 #include "hw/usb.h"
29 #include "qemu/iov.h"
30 #include "trace.h"
32 void usb_pick_speed(USBPort *port)
34 static const int speeds[] = {
35 USB_SPEED_SUPER,
36 USB_SPEED_HIGH,
37 USB_SPEED_FULL,
38 USB_SPEED_LOW,
40 USBDevice *udev = port->dev;
41 int i;
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;
52 void usb_attach(USBPort *port)
54 USBDevice *dev = port->dev;
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);
65 void usb_detach(USBPort *port)
67 USBDevice *dev = port->dev;
69 assert(dev != NULL);
70 assert(dev->state != USB_STATE_NOTATTACHED);
71 port->ops->detach(port);
72 dev->state = USB_STATE_NOTATTACHED;
75 void usb_port_reset(USBPort *port)
77 USBDevice *dev = port->dev;
79 assert(dev != NULL);
80 usb_detach(port);
81 usb_attach(port);
82 usb_device_reset(dev);
85 void usb_device_reset(USBDevice *dev)
87 if (dev == NULL || !dev->attached) {
88 return;
90 dev->remote_wakeup = 0;
91 dev->addr = 0;
92 dev->state = USB_STATE_DEFAULT;
93 usb_device_handle_reset(dev);
96 void usb_wakeup(USBEndpoint *ep, unsigned int stream)
98 USBDevice *dev = ep->dev;
99 USBBus *bus = usb_bus_from_device(dev);
101 if (!qdev_hotplug) {
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.
107 return;
109 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
110 dev->port->ops->wakeup(dev->port);
112 if (bus->ops->wakeup_endpoint) {
113 bus->ops->wakeup_endpoint(bus, ep, stream);
117 /**********************/
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)
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
130 static void do_token_setup(USBDevice *s, USBPacket *p)
132 int request, value, index;
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 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
143 if (s->setup_len > sizeof(s->data_buf)) {
144 fprintf(stderr,
145 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
146 s->setup_len, sizeof(s->data_buf));
147 p->status = USB_RET_STALL;
148 return;
151 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
152 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
153 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
155 if (s->setup_buf[0] & USB_DIR_IN) {
156 usb_device_handle_control(s, p, request, value, index,
157 s->setup_len, s->data_buf);
158 if (p->status == USB_RET_ASYNC) {
159 s->setup_state = SETUP_STATE_SETUP;
161 if (p->status != USB_RET_SUCCESS) {
162 return;
165 if (p->actual_length < s->setup_len) {
166 s->setup_len = p->actual_length;
168 s->setup_state = SETUP_STATE_DATA;
169 } else {
170 if (s->setup_len == 0)
171 s->setup_state = SETUP_STATE_ACK;
172 else
173 s->setup_state = SETUP_STATE_DATA;
176 p->actual_length = 8;
179 static void do_token_in(USBDevice *s, USBPacket *p)
181 int request, value, index;
183 assert(p->ep->nr == 0);
185 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
186 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
187 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
189 switch(s->setup_state) {
190 case SETUP_STATE_ACK:
191 if (!(s->setup_buf[0] & USB_DIR_IN)) {
192 usb_device_handle_control(s, p, request, value, index,
193 s->setup_len, s->data_buf);
194 if (p->status == USB_RET_ASYNC) {
195 return;
197 s->setup_state = SETUP_STATE_IDLE;
198 p->actual_length = 0;
200 break;
202 case SETUP_STATE_DATA:
203 if (s->setup_buf[0] & USB_DIR_IN) {
204 int len = s->setup_len - s->setup_index;
205 if (len > p->iov.size) {
206 len = p->iov.size;
208 usb_packet_copy(p, s->data_buf + s->setup_index, len);
209 s->setup_index += len;
210 if (s->setup_index >= s->setup_len) {
211 s->setup_state = SETUP_STATE_ACK;
213 return;
215 s->setup_state = SETUP_STATE_IDLE;
216 p->status = USB_RET_STALL;
217 break;
219 default:
220 p->status = USB_RET_STALL;
224 static void do_token_out(USBDevice *s, USBPacket *p)
226 assert(p->ep->nr == 0);
228 switch(s->setup_state) {
229 case SETUP_STATE_ACK:
230 if (s->setup_buf[0] & USB_DIR_IN) {
231 s->setup_state = SETUP_STATE_IDLE;
232 /* transfer OK */
233 } else {
234 /* ignore additional output */
236 break;
238 case SETUP_STATE_DATA:
239 if (!(s->setup_buf[0] & USB_DIR_IN)) {
240 int len = s->setup_len - s->setup_index;
241 if (len > p->iov.size) {
242 len = p->iov.size;
244 usb_packet_copy(p, s->data_buf + s->setup_index, len);
245 s->setup_index += len;
246 if (s->setup_index >= s->setup_len) {
247 s->setup_state = SETUP_STATE_ACK;
249 return;
251 s->setup_state = SETUP_STATE_IDLE;
252 p->status = USB_RET_STALL;
253 break;
255 default:
256 p->status = USB_RET_STALL;
260 static void do_parameter(USBDevice *s, USBPacket *p)
262 int i, request, value, index;
264 for (i = 0; i < 8; i++) {
265 s->setup_buf[i] = p->parameter >> (i*8);
268 s->setup_state = SETUP_STATE_PARAM;
269 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
270 s->setup_index = 0;
272 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
273 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
274 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
276 if (s->setup_len > sizeof(s->data_buf)) {
277 fprintf(stderr,
278 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
279 s->setup_len, sizeof(s->data_buf));
280 p->status = USB_RET_STALL;
281 return;
284 if (p->pid == USB_TOKEN_OUT) {
285 usb_packet_copy(p, s->data_buf, s->setup_len);
288 usb_device_handle_control(s, p, request, value, index,
289 s->setup_len, s->data_buf);
290 if (p->status == USB_RET_ASYNC) {
291 return;
294 if (p->actual_length < s->setup_len) {
295 s->setup_len = p->actual_length;
297 if (p->pid == USB_TOKEN_IN) {
298 p->actual_length = 0;
299 usb_packet_copy(p, s->data_buf, s->setup_len);
303 /* ctrl complete function for devices which use usb_generic_handle_packet and
304 may return USB_RET_ASYNC from their handle_control callback. Device code
305 which does this *must* call this function instead of the normal
306 usb_packet_complete to complete their async control packets. */
307 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
309 if (p->status < 0) {
310 s->setup_state = SETUP_STATE_IDLE;
313 switch (s->setup_state) {
314 case SETUP_STATE_SETUP:
315 if (p->actual_length < s->setup_len) {
316 s->setup_len = p->actual_length;
318 s->setup_state = SETUP_STATE_DATA;
319 p->actual_length = 8;
320 break;
322 case SETUP_STATE_ACK:
323 s->setup_state = SETUP_STATE_IDLE;
324 p->actual_length = 0;
325 break;
327 case SETUP_STATE_PARAM:
328 if (p->actual_length < s->setup_len) {
329 s->setup_len = p->actual_length;
331 if (p->pid == USB_TOKEN_IN) {
332 p->actual_length = 0;
333 usb_packet_copy(p, s->data_buf, s->setup_len);
335 break;
337 default:
338 break;
340 usb_packet_complete(s, p);
343 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
345 USBDevice *dev = port->dev;
347 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
348 return NULL;
350 if (dev->addr == addr) {
351 return dev;
353 return usb_device_find_device(dev, addr);
356 static void usb_process_one(USBPacket *p)
358 USBDevice *dev = p->ep->dev;
361 * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
362 * can be USB_RET_NAK here from a previous usb_process_one() call,
363 * or USB_RET_ASYNC from going through usb_queue_one().
365 p->status = USB_RET_SUCCESS;
367 if (p->ep->nr == 0) {
368 /* control pipe */
369 if (p->parameter) {
370 do_parameter(dev, p);
371 return;
373 switch (p->pid) {
374 case USB_TOKEN_SETUP:
375 do_token_setup(dev, p);
376 break;
377 case USB_TOKEN_IN:
378 do_token_in(dev, p);
379 break;
380 case USB_TOKEN_OUT:
381 do_token_out(dev, p);
382 break;
383 default:
384 p->status = USB_RET_STALL;
386 } else {
387 /* data pipe */
388 usb_device_handle_data(dev, p);
392 static void usb_queue_one(USBPacket *p)
394 usb_packet_set_state(p, USB_PACKET_QUEUED);
395 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
396 p->status = USB_RET_ASYNC;
399 /* Hand over a packet to a device for processing. p->status ==
400 USB_RET_ASYNC indicates the processing isn't finished yet, the
401 driver will call usb_packet_complete() when done processing it. */
402 void usb_handle_packet(USBDevice *dev, USBPacket *p)
404 if (dev == NULL) {
405 p->status = USB_RET_NODEV;
406 return;
408 assert(dev == p->ep->dev);
409 assert(dev->state == USB_STATE_DEFAULT);
410 usb_packet_check_state(p, USB_PACKET_SETUP);
411 assert(p->ep != NULL);
413 /* Submitting a new packet clears halt */
414 if (p->ep->halted) {
415 assert(QTAILQ_EMPTY(&p->ep->queue));
416 p->ep->halted = false;
419 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
420 usb_process_one(p);
421 if (p->status == USB_RET_ASYNC) {
422 /* hcd drivers cannot handle async for isoc */
423 assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
424 /* using async for interrupt packets breaks migration */
425 assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
426 (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
427 usb_packet_set_state(p, USB_PACKET_ASYNC);
428 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
429 } else if (p->status == USB_RET_ADD_TO_QUEUE) {
430 usb_queue_one(p);
431 } else {
433 * When pipelining is enabled usb-devices must always return async,
434 * otherwise packets can complete out of order!
436 assert(p->stream || !p->ep->pipeline ||
437 QTAILQ_EMPTY(&p->ep->queue));
438 if (p->status != USB_RET_NAK) {
439 usb_packet_set_state(p, USB_PACKET_COMPLETE);
442 } else {
443 usb_queue_one(p);
447 void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
449 USBEndpoint *ep = p->ep;
451 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
452 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
454 if (p->status != USB_RET_SUCCESS ||
455 (p->short_not_ok && (p->actual_length < p->iov.size))) {
456 ep->halted = true;
458 usb_packet_set_state(p, USB_PACKET_COMPLETE);
459 QTAILQ_REMOVE(&ep->queue, p, queue);
460 dev->port->ops->complete(dev->port, p);
463 /* Notify the controller that an async packet is complete. This should only
464 be called for packets previously deferred by returning USB_RET_ASYNC from
465 handle_packet. */
466 void usb_packet_complete(USBDevice *dev, USBPacket *p)
468 USBEndpoint *ep = p->ep;
470 usb_packet_check_state(p, USB_PACKET_ASYNC);
471 usb_packet_complete_one(dev, p);
473 while (!QTAILQ_EMPTY(&ep->queue)) {
474 p = QTAILQ_FIRST(&ep->queue);
475 if (ep->halted) {
476 /* Empty the queue on a halt */
477 p->status = USB_RET_REMOVE_FROM_QUEUE;
478 dev->port->ops->complete(dev->port, p);
479 continue;
481 if (p->state == USB_PACKET_ASYNC) {
482 break;
484 usb_packet_check_state(p, USB_PACKET_QUEUED);
485 usb_process_one(p);
486 if (p->status == USB_RET_ASYNC) {
487 usb_packet_set_state(p, USB_PACKET_ASYNC);
488 break;
490 usb_packet_complete_one(ep->dev, p);
494 /* Cancel an active packet. The packed must have been deferred by
495 returning USB_RET_ASYNC from handle_packet, and not yet
496 completed. */
497 void usb_cancel_packet(USBPacket * p)
499 bool callback = (p->state == USB_PACKET_ASYNC);
500 assert(usb_packet_is_inflight(p));
501 usb_packet_set_state(p, USB_PACKET_CANCELED);
502 QTAILQ_REMOVE(&p->ep->queue, p, queue);
503 if (callback) {
504 usb_device_cancel_packet(p->ep->dev, p);
509 void usb_packet_init(USBPacket *p)
511 qemu_iovec_init(&p->iov, 1);
514 static const char *usb_packet_state_name(USBPacketState state)
516 static const char *name[] = {
517 [USB_PACKET_UNDEFINED] = "undef",
518 [USB_PACKET_SETUP] = "setup",
519 [USB_PACKET_QUEUED] = "queued",
520 [USB_PACKET_ASYNC] = "async",
521 [USB_PACKET_COMPLETE] = "complete",
522 [USB_PACKET_CANCELED] = "canceled",
524 if (state < ARRAY_SIZE(name)) {
525 return name[state];
527 return "INVALID";
530 void usb_packet_check_state(USBPacket *p, USBPacketState expected)
532 USBDevice *dev;
533 USBBus *bus;
535 if (p->state == expected) {
536 return;
538 dev = p->ep->dev;
539 bus = usb_bus_from_device(dev);
540 trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
541 usb_packet_state_name(p->state),
542 usb_packet_state_name(expected));
543 assert(!"usb packet state check failed");
546 void usb_packet_set_state(USBPacket *p, USBPacketState state)
548 if (p->ep) {
549 USBDevice *dev = p->ep->dev;
550 USBBus *bus = usb_bus_from_device(dev);
551 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
552 usb_packet_state_name(p->state),
553 usb_packet_state_name(state));
554 } else {
555 trace_usb_packet_state_change(-1, "", -1, p,
556 usb_packet_state_name(p->state),
557 usb_packet_state_name(state));
559 p->state = state;
562 void usb_packet_setup(USBPacket *p, int pid,
563 USBEndpoint *ep, unsigned int stream,
564 uint64_t id, bool short_not_ok, bool int_req)
566 assert(!usb_packet_is_inflight(p));
567 assert(p->iov.iov != NULL);
568 p->id = id;
569 p->pid = pid;
570 p->ep = ep;
571 p->stream = stream;
572 p->status = USB_RET_SUCCESS;
573 p->actual_length = 0;
574 p->parameter = 0;
575 p->short_not_ok = short_not_ok;
576 p->int_req = int_req;
577 p->combined = NULL;
578 qemu_iovec_reset(&p->iov);
579 usb_packet_set_state(p, USB_PACKET_SETUP);
582 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
584 qemu_iovec_add(&p->iov, ptr, len);
587 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
589 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
591 assert(p->actual_length >= 0);
592 assert(p->actual_length + bytes <= iov->size);
593 switch (p->pid) {
594 case USB_TOKEN_SETUP:
595 case USB_TOKEN_OUT:
596 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
597 break;
598 case USB_TOKEN_IN:
599 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
600 break;
601 default:
602 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
603 abort();
605 p->actual_length += bytes;
608 void usb_packet_skip(USBPacket *p, size_t bytes)
610 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
612 assert(p->actual_length >= 0);
613 assert(p->actual_length + bytes <= iov->size);
614 if (p->pid == USB_TOKEN_IN) {
615 iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
617 p->actual_length += bytes;
620 size_t usb_packet_size(USBPacket *p)
622 return p->combined ? p->combined->iov.size : p->iov.size;
625 void usb_packet_cleanup(USBPacket *p)
627 assert(!usb_packet_is_inflight(p));
628 qemu_iovec_destroy(&p->iov);
631 void usb_ep_reset(USBDevice *dev)
633 int ep;
635 dev->ep_ctl.nr = 0;
636 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
637 dev->ep_ctl.ifnum = 0;
638 dev->ep_ctl.max_packet_size = 64;
639 dev->ep_ctl.max_streams = 0;
640 dev->ep_ctl.dev = dev;
641 dev->ep_ctl.pipeline = false;
642 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
643 dev->ep_in[ep].nr = ep + 1;
644 dev->ep_out[ep].nr = ep + 1;
645 dev->ep_in[ep].pid = USB_TOKEN_IN;
646 dev->ep_out[ep].pid = USB_TOKEN_OUT;
647 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
648 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
649 dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
650 dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
651 dev->ep_in[ep].max_packet_size = 0;
652 dev->ep_out[ep].max_packet_size = 0;
653 dev->ep_in[ep].max_streams = 0;
654 dev->ep_out[ep].max_streams = 0;
655 dev->ep_in[ep].dev = dev;
656 dev->ep_out[ep].dev = dev;
657 dev->ep_in[ep].pipeline = false;
658 dev->ep_out[ep].pipeline = false;
662 void usb_ep_init(USBDevice *dev)
664 int ep;
666 usb_ep_reset(dev);
667 QTAILQ_INIT(&dev->ep_ctl.queue);
668 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
669 QTAILQ_INIT(&dev->ep_in[ep].queue);
670 QTAILQ_INIT(&dev->ep_out[ep].queue);
674 void usb_ep_dump(USBDevice *dev)
676 static const char *tname[] = {
677 [USB_ENDPOINT_XFER_CONTROL] = "control",
678 [USB_ENDPOINT_XFER_ISOC] = "isoc",
679 [USB_ENDPOINT_XFER_BULK] = "bulk",
680 [USB_ENDPOINT_XFER_INT] = "int",
682 int ifnum, ep, first;
684 fprintf(stderr, "Device \"%s\", config %d\n",
685 dev->product_desc, dev->configuration);
686 for (ifnum = 0; ifnum < 16; ifnum++) {
687 first = 1;
688 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
689 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
690 dev->ep_in[ep].ifnum == ifnum) {
691 if (first) {
692 first = 0;
693 fprintf(stderr, " Interface %d, alternative %d\n",
694 ifnum, dev->altsetting[ifnum]);
696 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
697 tname[dev->ep_in[ep].type],
698 dev->ep_in[ep].max_packet_size);
700 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
701 dev->ep_out[ep].ifnum == ifnum) {
702 if (first) {
703 first = 0;
704 fprintf(stderr, " Interface %d, alternative %d\n",
705 ifnum, dev->altsetting[ifnum]);
707 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
708 tname[dev->ep_out[ep].type],
709 dev->ep_out[ep].max_packet_size);
713 fprintf(stderr, "--\n");
716 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
718 struct USBEndpoint *eps;
720 if (dev == NULL) {
721 return NULL;
723 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
724 if (ep == 0) {
725 return &dev->ep_ctl;
727 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
728 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
729 return eps + ep - 1;
732 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
734 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
735 return uep->type;
738 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
740 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
741 uep->type = type;
744 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
746 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
747 uep->ifnum = ifnum;
750 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
751 uint16_t raw)
753 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
754 int size, microframes;
756 size = raw & 0x7ff;
757 switch ((raw >> 11) & 3) {
758 case 1:
759 microframes = 2;
760 break;
761 case 2:
762 microframes = 3;
763 break;
764 default:
765 microframes = 1;
766 break;
768 uep->max_packet_size = size * microframes;
771 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
773 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
774 int MaxStreams;
776 MaxStreams = raw & 0x1f;
777 if (MaxStreams) {
778 uep->max_streams = 1 << MaxStreams;
779 } else {
780 uep->max_streams = 0;
784 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
786 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
787 uep->halted = halted;
790 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
791 uint64_t id)
793 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
794 USBPacket *p;
796 QTAILQ_FOREACH(p, &uep->queue, queue) {
797 if (p->id == id) {
798 return p;
802 return NULL;