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