vhost: adapt vhost_verify_ring_mappings() to virtio 1 ring layout
[qemu/ar7.git] / hw / usb / core.c
blob45fa00c517177c7566dc2e22faed94a4b528d263
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 (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
102 dev->port->ops->wakeup(dev->port);
104 if (bus->ops->wakeup_endpoint) {
105 bus->ops->wakeup_endpoint(bus, ep, stream);
109 /**********************/
111 /* generic USB device helpers (you are not forced to use them when
112 writing your USB device driver, but they help handling the
113 protocol)
116 #define SETUP_STATE_IDLE 0
117 #define SETUP_STATE_SETUP 1
118 #define SETUP_STATE_DATA 2
119 #define SETUP_STATE_ACK 3
120 #define SETUP_STATE_PARAM 4
122 static void do_token_setup(USBDevice *s, USBPacket *p)
124 int request, value, index;
126 if (p->iov.size != 8) {
127 p->status = USB_RET_STALL;
128 return;
131 usb_packet_copy(p, s->setup_buf, p->iov.size);
132 s->setup_index = 0;
133 p->actual_length = 0;
134 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
135 if (s->setup_len > sizeof(s->data_buf)) {
136 fprintf(stderr,
137 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
138 s->setup_len, sizeof(s->data_buf));
139 p->status = USB_RET_STALL;
140 return;
143 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
144 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
145 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
147 if (s->setup_buf[0] & USB_DIR_IN) {
148 usb_device_handle_control(s, p, request, value, index,
149 s->setup_len, s->data_buf);
150 if (p->status == USB_RET_ASYNC) {
151 s->setup_state = SETUP_STATE_SETUP;
153 if (p->status != USB_RET_SUCCESS) {
154 return;
157 if (p->actual_length < s->setup_len) {
158 s->setup_len = p->actual_length;
160 s->setup_state = SETUP_STATE_DATA;
161 } else {
162 if (s->setup_len == 0)
163 s->setup_state = SETUP_STATE_ACK;
164 else
165 s->setup_state = SETUP_STATE_DATA;
168 p->actual_length = 8;
171 static void do_token_in(USBDevice *s, USBPacket *p)
173 int request, value, index;
175 assert(p->ep->nr == 0);
177 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
178 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
179 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
181 switch(s->setup_state) {
182 case SETUP_STATE_ACK:
183 if (!(s->setup_buf[0] & USB_DIR_IN)) {
184 usb_device_handle_control(s, p, request, value, index,
185 s->setup_len, s->data_buf);
186 if (p->status == USB_RET_ASYNC) {
187 return;
189 s->setup_state = SETUP_STATE_IDLE;
190 p->actual_length = 0;
192 break;
194 case SETUP_STATE_DATA:
195 if (s->setup_buf[0] & USB_DIR_IN) {
196 int len = s->setup_len - s->setup_index;
197 if (len > p->iov.size) {
198 len = p->iov.size;
200 usb_packet_copy(p, s->data_buf + s->setup_index, len);
201 s->setup_index += len;
202 if (s->setup_index >= s->setup_len) {
203 s->setup_state = SETUP_STATE_ACK;
205 return;
207 s->setup_state = SETUP_STATE_IDLE;
208 p->status = USB_RET_STALL;
209 break;
211 default:
212 p->status = USB_RET_STALL;
216 static void do_token_out(USBDevice *s, USBPacket *p)
218 assert(p->ep->nr == 0);
220 switch(s->setup_state) {
221 case SETUP_STATE_ACK:
222 if (s->setup_buf[0] & USB_DIR_IN) {
223 s->setup_state = SETUP_STATE_IDLE;
224 /* transfer OK */
225 } else {
226 /* ignore additional output */
228 break;
230 case SETUP_STATE_DATA:
231 if (!(s->setup_buf[0] & USB_DIR_IN)) {
232 int len = s->setup_len - s->setup_index;
233 if (len > p->iov.size) {
234 len = p->iov.size;
236 usb_packet_copy(p, s->data_buf + s->setup_index, len);
237 s->setup_index += len;
238 if (s->setup_index >= s->setup_len) {
239 s->setup_state = SETUP_STATE_ACK;
241 return;
243 s->setup_state = SETUP_STATE_IDLE;
244 p->status = USB_RET_STALL;
245 break;
247 default:
248 p->status = USB_RET_STALL;
252 static void do_parameter(USBDevice *s, USBPacket *p)
254 int i, request, value, index;
256 for (i = 0; i < 8; i++) {
257 s->setup_buf[i] = p->parameter >> (i*8);
260 s->setup_state = SETUP_STATE_PARAM;
261 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
262 s->setup_index = 0;
264 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
265 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
266 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
268 if (s->setup_len > sizeof(s->data_buf)) {
269 fprintf(stderr,
270 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
271 s->setup_len, sizeof(s->data_buf));
272 p->status = USB_RET_STALL;
273 return;
276 if (p->pid == USB_TOKEN_OUT) {
277 usb_packet_copy(p, s->data_buf, s->setup_len);
280 usb_device_handle_control(s, p, request, value, index,
281 s->setup_len, s->data_buf);
282 if (p->status == USB_RET_ASYNC) {
283 return;
286 if (p->actual_length < s->setup_len) {
287 s->setup_len = p->actual_length;
289 if (p->pid == USB_TOKEN_IN) {
290 p->actual_length = 0;
291 usb_packet_copy(p, s->data_buf, s->setup_len);
295 /* ctrl complete function for devices which use usb_generic_handle_packet and
296 may return USB_RET_ASYNC from their handle_control callback. Device code
297 which does this *must* call this function instead of the normal
298 usb_packet_complete to complete their async control packets. */
299 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
301 if (p->status < 0) {
302 s->setup_state = SETUP_STATE_IDLE;
305 switch (s->setup_state) {
306 case SETUP_STATE_SETUP:
307 if (p->actual_length < s->setup_len) {
308 s->setup_len = p->actual_length;
310 s->setup_state = SETUP_STATE_DATA;
311 p->actual_length = 8;
312 break;
314 case SETUP_STATE_ACK:
315 s->setup_state = SETUP_STATE_IDLE;
316 p->actual_length = 0;
317 break;
319 case SETUP_STATE_PARAM:
320 if (p->actual_length < s->setup_len) {
321 s->setup_len = p->actual_length;
323 if (p->pid == USB_TOKEN_IN) {
324 p->actual_length = 0;
325 usb_packet_copy(p, s->data_buf, s->setup_len);
327 break;
329 default:
330 break;
332 usb_packet_complete(s, p);
335 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
337 USBDevice *dev = port->dev;
339 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
340 return NULL;
342 if (dev->addr == addr) {
343 return dev;
345 return usb_device_find_device(dev, addr);
348 static void usb_process_one(USBPacket *p)
350 USBDevice *dev = p->ep->dev;
353 * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
354 * can be USB_RET_NAK here from a previous usb_process_one() call,
355 * or USB_RET_ASYNC from going through usb_queue_one().
357 p->status = USB_RET_SUCCESS;
359 if (p->ep->nr == 0) {
360 /* control pipe */
361 if (p->parameter) {
362 do_parameter(dev, p);
363 return;
365 switch (p->pid) {
366 case USB_TOKEN_SETUP:
367 do_token_setup(dev, p);
368 break;
369 case USB_TOKEN_IN:
370 do_token_in(dev, p);
371 break;
372 case USB_TOKEN_OUT:
373 do_token_out(dev, p);
374 break;
375 default:
376 p->status = USB_RET_STALL;
378 } else {
379 /* data pipe */
380 usb_device_handle_data(dev, p);
384 static void usb_queue_one(USBPacket *p)
386 usb_packet_set_state(p, USB_PACKET_QUEUED);
387 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
388 p->status = USB_RET_ASYNC;
391 /* Hand over a packet to a device for processing. p->status ==
392 USB_RET_ASYNC indicates the processing isn't finished yet, the
393 driver will call usb_packet_complete() when done processing it. */
394 void usb_handle_packet(USBDevice *dev, USBPacket *p)
396 if (dev == NULL) {
397 p->status = USB_RET_NODEV;
398 return;
400 assert(dev == p->ep->dev);
401 assert(dev->state == USB_STATE_DEFAULT);
402 usb_packet_check_state(p, USB_PACKET_SETUP);
403 assert(p->ep != NULL);
405 /* Submitting a new packet clears halt */
406 if (p->ep->halted) {
407 assert(QTAILQ_EMPTY(&p->ep->queue));
408 p->ep->halted = false;
411 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
412 usb_process_one(p);
413 if (p->status == USB_RET_ASYNC) {
414 /* hcd drivers cannot handle async for isoc */
415 assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
416 /* using async for interrupt packets breaks migration */
417 assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
418 (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
419 usb_packet_set_state(p, USB_PACKET_ASYNC);
420 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
421 } else if (p->status == USB_RET_ADD_TO_QUEUE) {
422 usb_queue_one(p);
423 } else {
425 * When pipelining is enabled usb-devices must always return async,
426 * otherwise packets can complete out of order!
428 assert(p->stream || !p->ep->pipeline ||
429 QTAILQ_EMPTY(&p->ep->queue));
430 if (p->status != USB_RET_NAK) {
431 usb_packet_set_state(p, USB_PACKET_COMPLETE);
434 } else {
435 usb_queue_one(p);
439 void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
441 USBEndpoint *ep = p->ep;
443 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
444 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
446 if (p->status != USB_RET_SUCCESS ||
447 (p->short_not_ok && (p->actual_length < p->iov.size))) {
448 ep->halted = true;
450 usb_packet_set_state(p, USB_PACKET_COMPLETE);
451 QTAILQ_REMOVE(&ep->queue, p, queue);
452 dev->port->ops->complete(dev->port, p);
455 /* Notify the controller that an async packet is complete. This should only
456 be called for packets previously deferred by returning USB_RET_ASYNC from
457 handle_packet. */
458 void usb_packet_complete(USBDevice *dev, USBPacket *p)
460 USBEndpoint *ep = p->ep;
462 usb_packet_check_state(p, USB_PACKET_ASYNC);
463 usb_packet_complete_one(dev, p);
465 while (!QTAILQ_EMPTY(&ep->queue)) {
466 p = QTAILQ_FIRST(&ep->queue);
467 if (ep->halted) {
468 /* Empty the queue on a halt */
469 p->status = USB_RET_REMOVE_FROM_QUEUE;
470 dev->port->ops->complete(dev->port, p);
471 continue;
473 if (p->state == USB_PACKET_ASYNC) {
474 break;
476 usb_packet_check_state(p, USB_PACKET_QUEUED);
477 usb_process_one(p);
478 if (p->status == USB_RET_ASYNC) {
479 usb_packet_set_state(p, USB_PACKET_ASYNC);
480 break;
482 usb_packet_complete_one(ep->dev, p);
486 /* Cancel an active packet. The packed must have been deferred by
487 returning USB_RET_ASYNC from handle_packet, and not yet
488 completed. */
489 void usb_cancel_packet(USBPacket * p)
491 bool callback = (p->state == USB_PACKET_ASYNC);
492 assert(usb_packet_is_inflight(p));
493 usb_packet_set_state(p, USB_PACKET_CANCELED);
494 QTAILQ_REMOVE(&p->ep->queue, p, queue);
495 if (callback) {
496 usb_device_cancel_packet(p->ep->dev, p);
501 void usb_packet_init(USBPacket *p)
503 qemu_iovec_init(&p->iov, 1);
506 static const char *usb_packet_state_name(USBPacketState state)
508 static const char *name[] = {
509 [USB_PACKET_UNDEFINED] = "undef",
510 [USB_PACKET_SETUP] = "setup",
511 [USB_PACKET_QUEUED] = "queued",
512 [USB_PACKET_ASYNC] = "async",
513 [USB_PACKET_COMPLETE] = "complete",
514 [USB_PACKET_CANCELED] = "canceled",
516 if (state < ARRAY_SIZE(name)) {
517 return name[state];
519 return "INVALID";
522 void usb_packet_check_state(USBPacket *p, USBPacketState expected)
524 USBDevice *dev;
525 USBBus *bus;
527 if (p->state == expected) {
528 return;
530 dev = p->ep->dev;
531 bus = usb_bus_from_device(dev);
532 trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
533 usb_packet_state_name(p->state),
534 usb_packet_state_name(expected));
535 assert(!"usb packet state check failed");
538 void usb_packet_set_state(USBPacket *p, USBPacketState state)
540 if (p->ep) {
541 USBDevice *dev = p->ep->dev;
542 USBBus *bus = usb_bus_from_device(dev);
543 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
544 usb_packet_state_name(p->state),
545 usb_packet_state_name(state));
546 } else {
547 trace_usb_packet_state_change(-1, "", -1, p,
548 usb_packet_state_name(p->state),
549 usb_packet_state_name(state));
551 p->state = state;
554 void usb_packet_setup(USBPacket *p, int pid,
555 USBEndpoint *ep, unsigned int stream,
556 uint64_t id, bool short_not_ok, bool int_req)
558 assert(!usb_packet_is_inflight(p));
559 assert(p->iov.iov != NULL);
560 p->id = id;
561 p->pid = pid;
562 p->ep = ep;
563 p->stream = stream;
564 p->status = USB_RET_SUCCESS;
565 p->actual_length = 0;
566 p->parameter = 0;
567 p->short_not_ok = short_not_ok;
568 p->int_req = int_req;
569 p->combined = NULL;
570 qemu_iovec_reset(&p->iov);
571 usb_packet_set_state(p, USB_PACKET_SETUP);
574 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
576 qemu_iovec_add(&p->iov, ptr, len);
579 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
581 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
583 assert(p->actual_length >= 0);
584 assert(p->actual_length + bytes <= iov->size);
585 switch (p->pid) {
586 case USB_TOKEN_SETUP:
587 case USB_TOKEN_OUT:
588 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
589 break;
590 case USB_TOKEN_IN:
591 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
592 break;
593 default:
594 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
595 abort();
597 p->actual_length += bytes;
600 void usb_packet_skip(USBPacket *p, size_t bytes)
602 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
604 assert(p->actual_length >= 0);
605 assert(p->actual_length + bytes <= iov->size);
606 if (p->pid == USB_TOKEN_IN) {
607 iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
609 p->actual_length += bytes;
612 size_t usb_packet_size(USBPacket *p)
614 return p->combined ? p->combined->iov.size : p->iov.size;
617 void usb_packet_cleanup(USBPacket *p)
619 assert(!usb_packet_is_inflight(p));
620 qemu_iovec_destroy(&p->iov);
623 void usb_ep_reset(USBDevice *dev)
625 int ep;
627 dev->ep_ctl.nr = 0;
628 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
629 dev->ep_ctl.ifnum = 0;
630 dev->ep_ctl.max_packet_size = 64;
631 dev->ep_ctl.max_streams = 0;
632 dev->ep_ctl.dev = dev;
633 dev->ep_ctl.pipeline = false;
634 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
635 dev->ep_in[ep].nr = ep + 1;
636 dev->ep_out[ep].nr = ep + 1;
637 dev->ep_in[ep].pid = USB_TOKEN_IN;
638 dev->ep_out[ep].pid = USB_TOKEN_OUT;
639 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
640 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
641 dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
642 dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
643 dev->ep_in[ep].max_packet_size = 0;
644 dev->ep_out[ep].max_packet_size = 0;
645 dev->ep_in[ep].max_streams = 0;
646 dev->ep_out[ep].max_streams = 0;
647 dev->ep_in[ep].dev = dev;
648 dev->ep_out[ep].dev = dev;
649 dev->ep_in[ep].pipeline = false;
650 dev->ep_out[ep].pipeline = false;
654 void usb_ep_init(USBDevice *dev)
656 int ep;
658 usb_ep_reset(dev);
659 QTAILQ_INIT(&dev->ep_ctl.queue);
660 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
661 QTAILQ_INIT(&dev->ep_in[ep].queue);
662 QTAILQ_INIT(&dev->ep_out[ep].queue);
666 void usb_ep_dump(USBDevice *dev)
668 static const char *tname[] = {
669 [USB_ENDPOINT_XFER_CONTROL] = "control",
670 [USB_ENDPOINT_XFER_ISOC] = "isoc",
671 [USB_ENDPOINT_XFER_BULK] = "bulk",
672 [USB_ENDPOINT_XFER_INT] = "int",
674 int ifnum, ep, first;
676 fprintf(stderr, "Device \"%s\", config %d\n",
677 dev->product_desc, dev->configuration);
678 for (ifnum = 0; ifnum < 16; ifnum++) {
679 first = 1;
680 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
681 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
682 dev->ep_in[ep].ifnum == ifnum) {
683 if (first) {
684 first = 0;
685 fprintf(stderr, " Interface %d, alternative %d\n",
686 ifnum, dev->altsetting[ifnum]);
688 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
689 tname[dev->ep_in[ep].type],
690 dev->ep_in[ep].max_packet_size);
692 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
693 dev->ep_out[ep].ifnum == ifnum) {
694 if (first) {
695 first = 0;
696 fprintf(stderr, " Interface %d, alternative %d\n",
697 ifnum, dev->altsetting[ifnum]);
699 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
700 tname[dev->ep_out[ep].type],
701 dev->ep_out[ep].max_packet_size);
705 fprintf(stderr, "--\n");
708 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
710 struct USBEndpoint *eps;
712 if (dev == NULL) {
713 return NULL;
715 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
716 if (ep == 0) {
717 return &dev->ep_ctl;
719 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
720 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
721 return eps + ep - 1;
724 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
726 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
727 return uep->type;
730 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
732 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
733 uep->type = type;
736 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
738 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
739 uep->ifnum = ifnum;
742 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
743 uint16_t raw)
745 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
746 int size, microframes;
748 size = raw & 0x7ff;
749 switch ((raw >> 11) & 3) {
750 case 1:
751 microframes = 2;
752 break;
753 case 2:
754 microframes = 3;
755 break;
756 default:
757 microframes = 1;
758 break;
760 uep->max_packet_size = size * microframes;
763 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
765 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
766 int MaxStreams;
768 MaxStreams = raw & 0x1f;
769 if (MaxStreams) {
770 uep->max_streams = 1 << MaxStreams;
771 } else {
772 uep->max_streams = 0;
776 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
778 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
779 uep->halted = halted;
782 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
783 uint64_t id)
785 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
786 USBPacket *p;
788 QTAILQ_FOREACH(p, &uep->queue, queue) {
789 if (p->id == id) {
790 return p;
794 return NULL;