]> git.proxmox.com Git - mirror_qemu.git/blob - hw/usb/core.c
Merge remote-tracking branch 'remotes/gkurz/tags/for-upstream' into staging
[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/osdep.h"
27 #include "qemu-common.h"
28 #include "hw/usb.h"
29 #include "qemu/iov.h"
30 #include "trace.h"
31
32 void usb_pick_speed(USBPort *port)
33 {
34 static const int speeds[] = {
35 USB_SPEED_SUPER,
36 USB_SPEED_HIGH,
37 USB_SPEED_FULL,
38 USB_SPEED_LOW,
39 };
40 USBDevice *udev = port->dev;
41 int i;
42
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;
48 }
49 }
50 }
51
52 void usb_attach(USBPort *port)
53 {
54 USBDevice *dev = port->dev;
55
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);
63 }
64
65 void usb_detach(USBPort *port)
66 {
67 USBDevice *dev = port->dev;
68
69 assert(dev != NULL);
70 assert(dev->state != USB_STATE_NOTATTACHED);
71 port->ops->detach(port);
72 dev->state = USB_STATE_NOTATTACHED;
73 }
74
75 void usb_port_reset(USBPort *port)
76 {
77 USBDevice *dev = port->dev;
78
79 assert(dev != NULL);
80 usb_detach(port);
81 usb_attach(port);
82 usb_device_reset(dev);
83 }
84
85 void usb_device_reset(USBDevice *dev)
86 {
87 if (dev == NULL || !dev->attached) {
88 return;
89 }
90 dev->remote_wakeup = 0;
91 dev->addr = 0;
92 dev->state = USB_STATE_DEFAULT;
93 usb_device_handle_reset(dev);
94 }
95
96 void usb_wakeup(USBEndpoint *ep, unsigned int stream)
97 {
98 USBDevice *dev = ep->dev;
99 USBBus *bus = usb_bus_from_device(dev);
100
101 if (!qdev_hotplug) {
102 /*
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.
106 */
107 return;
108 }
109 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
110 dev->port->ops->wakeup(dev->port);
111 }
112 if (bus->ops->wakeup_endpoint) {
113 bus->ops->wakeup_endpoint(bus, ep, stream);
114 }
115 }
116
117 /**********************/
118
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)
122 */
123
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
129
130 static void do_token_setup(USBDevice *s, USBPacket *p)
131 {
132 int request, value, index;
133
134 if (p->iov.size != 8) {
135 p->status = USB_RET_STALL;
136 return;
137 }
138
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;
149 }
150
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];
154
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;
160 }
161 if (p->status != USB_RET_SUCCESS) {
162 return;
163 }
164
165 if (p->actual_length < s->setup_len) {
166 s->setup_len = p->actual_length;
167 }
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;
174 }
175
176 p->actual_length = 8;
177 }
178
179 static void do_token_in(USBDevice *s, USBPacket *p)
180 {
181 int request, value, index;
182
183 assert(p->ep->nr == 0);
184
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];
188
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;
196 }
197 s->setup_state = SETUP_STATE_IDLE;
198 p->actual_length = 0;
199 }
200 break;
201
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;
207 }
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;
212 }
213 return;
214 }
215 s->setup_state = SETUP_STATE_IDLE;
216 p->status = USB_RET_STALL;
217 break;
218
219 default:
220 p->status = USB_RET_STALL;
221 }
222 }
223
224 static void do_token_out(USBDevice *s, USBPacket *p)
225 {
226 assert(p->ep->nr == 0);
227
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 */
235 }
236 break;
237
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;
243 }
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;
248 }
249 return;
250 }
251 s->setup_state = SETUP_STATE_IDLE;
252 p->status = USB_RET_STALL;
253 break;
254
255 default:
256 p->status = USB_RET_STALL;
257 }
258 }
259
260 static void do_parameter(USBDevice *s, USBPacket *p)
261 {
262 int i, request, value, index;
263
264 for (i = 0; i < 8; i++) {
265 s->setup_buf[i] = p->parameter >> (i*8);
266 }
267
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;
271
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];
275
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;
282 }
283
284 if (p->pid == USB_TOKEN_OUT) {
285 usb_packet_copy(p, s->data_buf, s->setup_len);
286 }
287
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;
292 }
293
294 if (p->actual_length < s->setup_len) {
295 s->setup_len = p->actual_length;
296 }
297 if (p->pid == USB_TOKEN_IN) {
298 p->actual_length = 0;
299 usb_packet_copy(p, s->data_buf, s->setup_len);
300 }
301 }
302
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)
308 {
309 if (p->status < 0) {
310 s->setup_state = SETUP_STATE_IDLE;
311 }
312
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;
317 }
318 s->setup_state = SETUP_STATE_DATA;
319 p->actual_length = 8;
320 break;
321
322 case SETUP_STATE_ACK:
323 s->setup_state = SETUP_STATE_IDLE;
324 p->actual_length = 0;
325 break;
326
327 case SETUP_STATE_PARAM:
328 if (p->actual_length < s->setup_len) {
329 s->setup_len = p->actual_length;
330 }
331 if (p->pid == USB_TOKEN_IN) {
332 p->actual_length = 0;
333 usb_packet_copy(p, s->data_buf, s->setup_len);
334 }
335 break;
336
337 default:
338 break;
339 }
340 usb_packet_complete(s, p);
341 }
342
343 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
344 {
345 USBDevice *dev = port->dev;
346
347 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
348 return NULL;
349 }
350 if (dev->addr == addr) {
351 return dev;
352 }
353 return usb_device_find_device(dev, addr);
354 }
355
356 static void usb_process_one(USBPacket *p)
357 {
358 USBDevice *dev = p->ep->dev;
359
360 /*
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().
364 */
365 p->status = USB_RET_SUCCESS;
366
367 if (p->ep->nr == 0) {
368 /* control pipe */
369 if (p->parameter) {
370 do_parameter(dev, p);
371 return;
372 }
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;
385 }
386 } else {
387 /* data pipe */
388 usb_device_handle_data(dev, p);
389 }
390 }
391
392 static void usb_queue_one(USBPacket *p)
393 {
394 usb_packet_set_state(p, USB_PACKET_QUEUED);
395 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
396 p->status = USB_RET_ASYNC;
397 }
398
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)
403 {
404 if (dev == NULL) {
405 p->status = USB_RET_NODEV;
406 return;
407 }
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);
412
413 /* Submitting a new packet clears halt */
414 if (p->ep->halted) {
415 assert(QTAILQ_EMPTY(&p->ep->queue));
416 p->ep->halted = false;
417 }
418
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 {
432 /*
433 * When pipelining is enabled usb-devices must always return async,
434 * otherwise packets can complete out of order!
435 */
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);
440 }
441 }
442 } else {
443 usb_queue_one(p);
444 }
445 }
446
447 void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
448 {
449 USBEndpoint *ep = p->ep;
450
451 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
452 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
453
454 if (p->status != USB_RET_SUCCESS ||
455 (p->short_not_ok && (p->actual_length < p->iov.size))) {
456 ep->halted = true;
457 }
458 usb_packet_set_state(p, USB_PACKET_COMPLETE);
459 QTAILQ_REMOVE(&ep->queue, p, queue);
460 dev->port->ops->complete(dev->port, p);
461 }
462
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)
467 {
468 USBEndpoint *ep = p->ep;
469
470 usb_packet_check_state(p, USB_PACKET_ASYNC);
471 usb_packet_complete_one(dev, p);
472
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;
480 }
481 if (p->state == USB_PACKET_ASYNC) {
482 break;
483 }
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;
489 }
490 usb_packet_complete_one(ep->dev, p);
491 }
492 }
493
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)
498 {
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);
505 }
506 }
507
508
509 void usb_packet_init(USBPacket *p)
510 {
511 qemu_iovec_init(&p->iov, 1);
512 }
513
514 static const char *usb_packet_state_name(USBPacketState state)
515 {
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",
523 };
524 if (state < ARRAY_SIZE(name)) {
525 return name[state];
526 }
527 return "INVALID";
528 }
529
530 void usb_packet_check_state(USBPacket *p, USBPacketState expected)
531 {
532 USBDevice *dev;
533 USBBus *bus;
534
535 if (p->state == expected) {
536 return;
537 }
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");
544 }
545
546 void usb_packet_set_state(USBPacket *p, USBPacketState state)
547 {
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));
558 }
559 p->state = state;
560 }
561
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)
565 {
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);
580 }
581
582 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
583 {
584 qemu_iovec_add(&p->iov, ptr, len);
585 }
586
587 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
588 {
589 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
590
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();
604 }
605 p->actual_length += bytes;
606 }
607
608 void usb_packet_skip(USBPacket *p, size_t bytes)
609 {
610 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
611
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);
616 }
617 p->actual_length += bytes;
618 }
619
620 size_t usb_packet_size(USBPacket *p)
621 {
622 return p->combined ? p->combined->iov.size : p->iov.size;
623 }
624
625 void usb_packet_cleanup(USBPacket *p)
626 {
627 assert(!usb_packet_is_inflight(p));
628 qemu_iovec_destroy(&p->iov);
629 }
630
631 void usb_ep_reset(USBDevice *dev)
632 {
633 int ep;
634
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;
659 }
660 }
661
662 void usb_ep_init(USBDevice *dev)
663 {
664 int ep;
665
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);
671 }
672 }
673
674 void usb_ep_dump(USBDevice *dev)
675 {
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",
681 };
682 int ifnum, ep, first;
683
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]);
695 }
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);
699 }
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]);
706 }
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);
710 }
711 }
712 }
713 fprintf(stderr, "--\n");
714 }
715
716 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
717 {
718 struct USBEndpoint *eps;
719
720 if (dev == NULL) {
721 return NULL;
722 }
723 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
724 if (ep == 0) {
725 return &dev->ep_ctl;
726 }
727 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
728 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
729 return eps + ep - 1;
730 }
731
732 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
733 {
734 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
735 return uep->type;
736 }
737
738 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
739 {
740 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
741 uep->type = type;
742 }
743
744 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
745 {
746 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
747 uep->ifnum = ifnum;
748 }
749
750 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
751 uint16_t raw)
752 {
753 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
754 int size, microframes;
755
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;
767 }
768 uep->max_packet_size = size * microframes;
769 }
770
771 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
772 {
773 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
774 int MaxStreams;
775
776 MaxStreams = raw & 0x1f;
777 if (MaxStreams) {
778 uep->max_streams = 1 << MaxStreams;
779 } else {
780 uep->max_streams = 0;
781 }
782 }
783
784 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
785 {
786 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
787 uep->halted = halted;
788 }
789
790 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
791 uint64_t id)
792 {
793 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
794 USBPacket *p;
795
796 QTAILQ_FOREACH(p, &uep->queue, queue) {
797 if (p->id == id) {
798 return p;
799 }
800 }
801
802 return NULL;
803 }