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
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
32 void usb_pick_speed(USBPort *port)
34 static const int speeds[] = {
40 USBDevice *udev = port->dev;
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];
52 void usb_attach(USBPort *port)
54 USBDevice *dev = port->dev;
57 assert(dev->attached);
58 assert(dev->state == USB_STATE_NOTATTACHED);
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;
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;
82 usb_device_reset(dev);
85 void usb_device_reset(USBDevice *dev)
87 if (dev == NULL || !dev->attached) {
90 dev->remote_wakeup = 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);
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.
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
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;
139 usb_packet_copy(p, s->setup_buf, p->iov.size);
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)) {
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;
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) {
165 if (p->actual_length < s->setup_len) {
166 s->setup_len = p->actual_length;
168 s->setup_state = SETUP_STATE_DATA;
170 if (s->setup_len == 0)
171 s->setup_state = SETUP_STATE_ACK;
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) {
197 s->setup_state = SETUP_STATE_IDLE;
198 p->actual_length = 0;
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) {
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;
215 s->setup_state = SETUP_STATE_IDLE;
216 p->status = USB_RET_STALL;
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;
234 /* ignore additional output */
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) {
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;
251 s->setup_state = SETUP_STATE_IDLE;
252 p->status = USB_RET_STALL;
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];
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)) {
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;
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) {
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)
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;
322 case SETUP_STATE_ACK:
323 s->setup_state = SETUP_STATE_IDLE;
324 p->actual_length = 0;
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);
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) {
350 if (dev->addr == addr) {
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) {
370 do_parameter(dev, p);
374 case USB_TOKEN_SETUP:
375 do_token_setup(dev, p);
381 do_token_out(dev, p);
384 p->status = USB_RET_STALL;
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)
405 p->status = USB_RET_NODEV;
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 */
415 assert(QTAILQ_EMPTY(&p->ep->queue));
416 p->ep->halted = false;
419 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
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) {
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);
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))) {
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
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);
476 /* Empty the queue on a halt */
477 p->status = USB_RET_REMOVE_FROM_QUEUE;
478 dev->port->ops->complete(dev->port, p);
481 if (p->state == USB_PACKET_ASYNC) {
484 usb_packet_check_state(p, USB_PACKET_QUEUED);
486 if (p->status == USB_RET_ASYNC) {
487 usb_packet_set_state(p, USB_PACKET_ASYNC);
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
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);
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)) {
530 void usb_packet_check_state(USBPacket *p, USBPacketState expected)
535 if (p->state == expected) {
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)
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));
555 trace_usb_packet_state_change(-1, "", -1, p,
556 usb_packet_state_name(p->state),
557 usb_packet_state_name(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);
572 p->status = USB_RET_SUCCESS;
573 p->actual_length = 0;
575 p->short_not_ok = short_not_ok;
576 p->int_req = int_req;
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);
594 case USB_TOKEN_SETUP:
596 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
599 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
602 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
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)
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)
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++) {
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) {
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) {
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;
724 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
725 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
726 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
730 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
732 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
736 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
738 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
742 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
744 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
748 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
751 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
752 int size, microframes;
755 switch ((raw >> 11) & 3) {
766 uep->max_packet_size = size * microframes;
769 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
771 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
774 MaxStreams = raw & 0x1f;
776 uep->max_streams = 1 << MaxStreams;
778 uep->max_streams = 0;
782 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
784 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
785 uep->halted = halted;
788 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
791 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
794 QTAILQ_FOREACH(p, &uep->queue, queue) {