target/mips: Declare generic FPU / Coprocessor functions in translate.h
[qemu/ar7.git] / hw / usb / core.c
blobe960036f4d5c1c6d521db73419841708db62b683
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 (%d > %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_device_handle_control(s, p, request, value, index,
158 s->setup_len, s->data_buf);
159 if (p->status == USB_RET_ASYNC) {
160 s->setup_state = SETUP_STATE_SETUP;
162 if (p->status != USB_RET_SUCCESS) {
163 return;
166 if (p->actual_length < s->setup_len) {
167 s->setup_len = p->actual_length;
169 s->setup_state = SETUP_STATE_DATA;
170 } else {
171 if (s->setup_len == 0)
172 s->setup_state = SETUP_STATE_ACK;
173 else
174 s->setup_state = SETUP_STATE_DATA;
177 p->actual_length = 8;
180 static void do_token_in(USBDevice *s, USBPacket *p)
182 int request, value, index;
184 assert(p->ep->nr == 0);
186 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
187 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
188 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
190 switch(s->setup_state) {
191 case SETUP_STATE_ACK:
192 if (!(s->setup_buf[0] & USB_DIR_IN)) {
193 usb_device_handle_control(s, p, request, value, index,
194 s->setup_len, s->data_buf);
195 if (p->status == USB_RET_ASYNC) {
196 return;
198 s->setup_state = SETUP_STATE_IDLE;
199 p->actual_length = 0;
201 break;
203 case SETUP_STATE_DATA:
204 if (s->setup_buf[0] & USB_DIR_IN) {
205 int len = s->setup_len - s->setup_index;
206 if (len > p->iov.size) {
207 len = p->iov.size;
209 usb_packet_copy(p, s->data_buf + s->setup_index, len);
210 s->setup_index += len;
211 if (s->setup_index >= s->setup_len) {
212 s->setup_state = SETUP_STATE_ACK;
214 return;
216 s->setup_state = SETUP_STATE_IDLE;
217 p->status = USB_RET_STALL;
218 break;
220 default:
221 p->status = USB_RET_STALL;
225 static void do_token_out(USBDevice *s, USBPacket *p)
227 assert(p->ep->nr == 0);
229 switch(s->setup_state) {
230 case SETUP_STATE_ACK:
231 if (s->setup_buf[0] & USB_DIR_IN) {
232 s->setup_state = SETUP_STATE_IDLE;
233 /* transfer OK */
234 } else {
235 /* ignore additional output */
237 break;
239 case SETUP_STATE_DATA:
240 if (!(s->setup_buf[0] & USB_DIR_IN)) {
241 int len = s->setup_len - s->setup_index;
242 if (len > p->iov.size) {
243 len = p->iov.size;
245 usb_packet_copy(p, s->data_buf + s->setup_index, len);
246 s->setup_index += len;
247 if (s->setup_index >= s->setup_len) {
248 s->setup_state = SETUP_STATE_ACK;
250 return;
252 s->setup_state = SETUP_STATE_IDLE;
253 p->status = USB_RET_STALL;
254 break;
256 default:
257 p->status = USB_RET_STALL;
261 static void do_parameter(USBDevice *s, USBPacket *p)
263 int i, request, value, index;
264 unsigned int setup_len;
266 for (i = 0; i < 8; i++) {
267 s->setup_buf[i] = p->parameter >> (i*8);
270 s->setup_state = SETUP_STATE_PARAM;
271 s->setup_index = 0;
273 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
274 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
275 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
277 setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
278 if (setup_len > sizeof(s->data_buf)) {
279 fprintf(stderr,
280 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
281 setup_len, sizeof(s->data_buf));
282 p->status = USB_RET_STALL;
283 return;
285 s->setup_len = setup_len;
287 if (p->pid == USB_TOKEN_OUT) {
288 usb_packet_copy(p, s->data_buf, s->setup_len);
291 usb_device_handle_control(s, p, request, value, index,
292 s->setup_len, s->data_buf);
293 if (p->status == USB_RET_ASYNC) {
294 return;
297 if (p->actual_length < s->setup_len) {
298 s->setup_len = p->actual_length;
300 if (p->pid == USB_TOKEN_IN) {
301 p->actual_length = 0;
302 usb_packet_copy(p, s->data_buf, s->setup_len);
306 /* ctrl complete function for devices which use usb_generic_handle_packet and
307 may return USB_RET_ASYNC from their handle_control callback. Device code
308 which does this *must* call this function instead of the normal
309 usb_packet_complete to complete their async control packets. */
310 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
312 if (p->status < 0) {
313 s->setup_state = SETUP_STATE_IDLE;
316 switch (s->setup_state) {
317 case SETUP_STATE_SETUP:
318 if (p->actual_length < s->setup_len) {
319 s->setup_len = p->actual_length;
321 s->setup_state = SETUP_STATE_DATA;
322 p->actual_length = 8;
323 break;
325 case SETUP_STATE_ACK:
326 s->setup_state = SETUP_STATE_IDLE;
327 p->actual_length = 0;
328 break;
330 case SETUP_STATE_PARAM:
331 if (p->actual_length < s->setup_len) {
332 s->setup_len = p->actual_length;
334 if (p->pid == USB_TOKEN_IN) {
335 p->actual_length = 0;
336 usb_packet_copy(p, s->data_buf, s->setup_len);
338 break;
340 default:
341 break;
343 usb_packet_complete(s, p);
346 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
348 USBDevice *dev = port->dev;
350 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
351 return NULL;
353 if (dev->addr == addr) {
354 return dev;
356 return usb_device_find_device(dev, addr);
359 static void usb_process_one(USBPacket *p)
361 USBDevice *dev = p->ep->dev;
364 * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
365 * can be USB_RET_NAK here from a previous usb_process_one() call,
366 * or USB_RET_ASYNC from going through usb_queue_one().
368 p->status = USB_RET_SUCCESS;
370 if (p->ep->nr == 0) {
371 /* control pipe */
372 if (p->parameter) {
373 do_parameter(dev, p);
374 return;
376 switch (p->pid) {
377 case USB_TOKEN_SETUP:
378 do_token_setup(dev, p);
379 break;
380 case USB_TOKEN_IN:
381 do_token_in(dev, p);
382 break;
383 case USB_TOKEN_OUT:
384 do_token_out(dev, p);
385 break;
386 default:
387 p->status = USB_RET_STALL;
389 } else {
390 /* data pipe */
391 usb_device_handle_data(dev, p);
395 static void usb_queue_one(USBPacket *p)
397 usb_packet_set_state(p, USB_PACKET_QUEUED);
398 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
399 p->status = USB_RET_ASYNC;
402 /* Hand over a packet to a device for processing. p->status ==
403 USB_RET_ASYNC indicates the processing isn't finished yet, the
404 driver will call usb_packet_complete() when done processing it. */
405 void usb_handle_packet(USBDevice *dev, USBPacket *p)
407 if (dev == NULL) {
408 p->status = USB_RET_NODEV;
409 return;
411 assert(dev == p->ep->dev);
412 assert(dev->state == USB_STATE_DEFAULT);
413 usb_packet_check_state(p, USB_PACKET_SETUP);
414 assert(p->ep != NULL);
416 /* Submitting a new packet clears halt */
417 if (p->ep->halted) {
418 assert(QTAILQ_EMPTY(&p->ep->queue));
419 p->ep->halted = false;
422 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
423 usb_process_one(p);
424 if (p->status == USB_RET_ASYNC) {
425 /* hcd drivers cannot handle async for isoc */
426 assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
427 /* using async for interrupt packets breaks migration */
428 assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
429 (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
430 usb_packet_set_state(p, USB_PACKET_ASYNC);
431 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
432 } else if (p->status == USB_RET_ADD_TO_QUEUE) {
433 usb_queue_one(p);
434 } else {
436 * When pipelining is enabled usb-devices must always return async,
437 * otherwise packets can complete out of order!
439 assert(p->stream || !p->ep->pipeline ||
440 QTAILQ_EMPTY(&p->ep->queue));
441 if (p->status != USB_RET_NAK) {
442 usb_packet_set_state(p, USB_PACKET_COMPLETE);
445 } else {
446 usb_queue_one(p);
450 void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
452 USBEndpoint *ep = p->ep;
454 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
455 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
457 if (p->status != USB_RET_SUCCESS ||
458 (p->short_not_ok && (p->actual_length < p->iov.size))) {
459 ep->halted = true;
461 usb_packet_set_state(p, USB_PACKET_COMPLETE);
462 QTAILQ_REMOVE(&ep->queue, p, queue);
463 dev->port->ops->complete(dev->port, p);
466 /* Notify the controller that an async packet is complete. This should only
467 be called for packets previously deferred by returning USB_RET_ASYNC from
468 handle_packet. */
469 void usb_packet_complete(USBDevice *dev, USBPacket *p)
471 USBEndpoint *ep = p->ep;
473 usb_packet_check_state(p, USB_PACKET_ASYNC);
474 usb_packet_complete_one(dev, p);
476 while (!QTAILQ_EMPTY(&ep->queue)) {
477 p = QTAILQ_FIRST(&ep->queue);
478 if (ep->halted) {
479 /* Empty the queue on a halt */
480 p->status = USB_RET_REMOVE_FROM_QUEUE;
481 dev->port->ops->complete(dev->port, p);
482 continue;
484 if (p->state == USB_PACKET_ASYNC) {
485 break;
487 usb_packet_check_state(p, USB_PACKET_QUEUED);
488 usb_process_one(p);
489 if (p->status == USB_RET_ASYNC) {
490 usb_packet_set_state(p, USB_PACKET_ASYNC);
491 break;
493 usb_packet_complete_one(ep->dev, p);
497 /* Cancel an active packet. The packed must have been deferred by
498 returning USB_RET_ASYNC from handle_packet, and not yet
499 completed. */
500 void usb_cancel_packet(USBPacket * p)
502 bool callback = (p->state == USB_PACKET_ASYNC);
503 assert(usb_packet_is_inflight(p));
504 usb_packet_set_state(p, USB_PACKET_CANCELED);
505 QTAILQ_REMOVE(&p->ep->queue, p, queue);
506 if (callback) {
507 usb_device_cancel_packet(p->ep->dev, p);
512 void usb_packet_init(USBPacket *p)
514 qemu_iovec_init(&p->iov, 1);
517 static const char *usb_packet_state_name(USBPacketState state)
519 static const char *name[] = {
520 [USB_PACKET_UNDEFINED] = "undef",
521 [USB_PACKET_SETUP] = "setup",
522 [USB_PACKET_QUEUED] = "queued",
523 [USB_PACKET_ASYNC] = "async",
524 [USB_PACKET_COMPLETE] = "complete",
525 [USB_PACKET_CANCELED] = "canceled",
527 if (state < ARRAY_SIZE(name)) {
528 return name[state];
530 return "INVALID";
533 void usb_packet_check_state(USBPacket *p, USBPacketState expected)
535 USBDevice *dev;
536 USBBus *bus;
538 if (p->state == expected) {
539 return;
541 dev = p->ep->dev;
542 bus = usb_bus_from_device(dev);
543 trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
544 usb_packet_state_name(p->state),
545 usb_packet_state_name(expected));
546 assert(!"usb packet state check failed");
549 void usb_packet_set_state(USBPacket *p, USBPacketState state)
551 if (p->ep) {
552 USBDevice *dev = p->ep->dev;
553 USBBus *bus = usb_bus_from_device(dev);
554 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
555 usb_packet_state_name(p->state),
556 usb_packet_state_name(state));
557 } else {
558 trace_usb_packet_state_change(-1, "", -1, p,
559 usb_packet_state_name(p->state),
560 usb_packet_state_name(state));
562 p->state = state;
565 void usb_packet_setup(USBPacket *p, int pid,
566 USBEndpoint *ep, unsigned int stream,
567 uint64_t id, bool short_not_ok, bool int_req)
569 assert(!usb_packet_is_inflight(p));
570 assert(p->iov.iov != NULL);
571 p->id = id;
572 p->pid = pid;
573 p->ep = ep;
574 p->stream = stream;
575 p->status = USB_RET_SUCCESS;
576 p->actual_length = 0;
577 p->parameter = 0;
578 p->short_not_ok = short_not_ok;
579 p->int_req = int_req;
580 p->combined = NULL;
581 qemu_iovec_reset(&p->iov);
582 usb_packet_set_state(p, USB_PACKET_SETUP);
585 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
587 qemu_iovec_add(&p->iov, ptr, len);
590 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
592 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
594 assert(p->actual_length >= 0);
595 assert(p->actual_length + bytes <= iov->size);
596 switch (p->pid) {
597 case USB_TOKEN_SETUP:
598 case USB_TOKEN_OUT:
599 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
600 break;
601 case USB_TOKEN_IN:
602 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
603 break;
604 default:
605 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
606 abort();
608 p->actual_length += bytes;
611 void usb_packet_skip(USBPacket *p, size_t bytes)
613 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
615 assert(p->actual_length >= 0);
616 assert(p->actual_length + bytes <= iov->size);
617 if (p->pid == USB_TOKEN_IN) {
618 iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
620 p->actual_length += bytes;
623 size_t usb_packet_size(USBPacket *p)
625 return p->combined ? p->combined->iov.size : p->iov.size;
628 void usb_packet_cleanup(USBPacket *p)
630 assert(!usb_packet_is_inflight(p));
631 qemu_iovec_destroy(&p->iov);
634 void usb_ep_reset(USBDevice *dev)
636 int ep;
638 dev->ep_ctl.nr = 0;
639 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
640 dev->ep_ctl.ifnum = 0;
641 dev->ep_ctl.max_packet_size = 64;
642 dev->ep_ctl.max_streams = 0;
643 dev->ep_ctl.dev = dev;
644 dev->ep_ctl.pipeline = false;
645 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
646 dev->ep_in[ep].nr = ep + 1;
647 dev->ep_out[ep].nr = ep + 1;
648 dev->ep_in[ep].pid = USB_TOKEN_IN;
649 dev->ep_out[ep].pid = USB_TOKEN_OUT;
650 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
651 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
652 dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
653 dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
654 dev->ep_in[ep].max_packet_size = 0;
655 dev->ep_out[ep].max_packet_size = 0;
656 dev->ep_in[ep].max_streams = 0;
657 dev->ep_out[ep].max_streams = 0;
658 dev->ep_in[ep].dev = dev;
659 dev->ep_out[ep].dev = dev;
660 dev->ep_in[ep].pipeline = false;
661 dev->ep_out[ep].pipeline = false;
665 void usb_ep_init(USBDevice *dev)
667 int ep;
669 usb_ep_reset(dev);
670 QTAILQ_INIT(&dev->ep_ctl.queue);
671 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
672 QTAILQ_INIT(&dev->ep_in[ep].queue);
673 QTAILQ_INIT(&dev->ep_out[ep].queue);
677 void usb_ep_dump(USBDevice *dev)
679 static const char *tname[] = {
680 [USB_ENDPOINT_XFER_CONTROL] = "control",
681 [USB_ENDPOINT_XFER_ISOC] = "isoc",
682 [USB_ENDPOINT_XFER_BULK] = "bulk",
683 [USB_ENDPOINT_XFER_INT] = "int",
685 int ifnum, ep, first;
687 fprintf(stderr, "Device \"%s\", config %d\n",
688 dev->product_desc, dev->configuration);
689 for (ifnum = 0; ifnum < 16; ifnum++) {
690 first = 1;
691 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
692 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
693 dev->ep_in[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, IN, %s, %d max\n", ep,
700 tname[dev->ep_in[ep].type],
701 dev->ep_in[ep].max_packet_size);
703 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
704 dev->ep_out[ep].ifnum == ifnum) {
705 if (first) {
706 first = 0;
707 fprintf(stderr, " Interface %d, alternative %d\n",
708 ifnum, dev->altsetting[ifnum]);
710 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
711 tname[dev->ep_out[ep].type],
712 dev->ep_out[ep].max_packet_size);
716 fprintf(stderr, "--\n");
719 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
721 struct USBEndpoint *eps;
723 assert(dev != NULL);
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 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
730 return eps + ep - 1;
733 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
735 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
736 return uep->type;
739 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
741 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
742 uep->type = type;
745 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
747 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
748 uep->ifnum = ifnum;
751 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
752 uint16_t raw)
754 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
755 int size, microframes;
757 size = raw & 0x7ff;
758 switch ((raw >> 11) & 3) {
759 case 1:
760 microframes = 2;
761 break;
762 case 2:
763 microframes = 3;
764 break;
765 default:
766 microframes = 1;
767 break;
769 uep->max_packet_size = size * microframes;
772 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
774 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
775 int MaxStreams;
777 MaxStreams = raw & 0x1f;
778 if (MaxStreams) {
779 uep->max_streams = 1 << MaxStreams;
780 } else {
781 uep->max_streams = 0;
785 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
787 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
788 uep->halted = halted;
791 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
792 uint64_t id)
794 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
795 USBPacket *p;
797 QTAILQ_FOREACH(p, &uep->queue, queue) {
798 if (p->id == id) {
799 return p;
803 return NULL;