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"
31 void usb_pick_speed(USBPort *port)
33 static const int speeds[] = {
39 USBDevice *udev = port->dev;
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];
51 void usb_attach(USBPort *port)
53 USBDevice *dev = port->dev;
56 assert(dev->attached);
57 assert(dev->state == USB_STATE_NOTATTACHED);
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;
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;
81 usb_device_reset(dev);
84 void usb_device_reset(USBDevice *dev)
86 if (dev == NULL || !dev->attached) {
89 usb_device_handle_reset(dev);
90 dev->remote_wakeup = 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);
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.
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
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;
133 if (p->iov.size != 8) {
134 p->status = USB_RET_STALL;
138 usb_packet_copy(p, s->setup_buf, p->iov.size);
140 p->actual_length = 0;
141 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
142 if (s->setup_len > sizeof(s->data_buf)) {
144 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
145 s->setup_len, sizeof(s->data_buf));
146 p->status = USB_RET_STALL;
150 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
151 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
152 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
154 if (s->setup_buf[0] & USB_DIR_IN) {
155 usb_device_handle_control(s, p, request, value, index,
156 s->setup_len, s->data_buf);
157 if (p->status == USB_RET_ASYNC) {
158 s->setup_state = SETUP_STATE_SETUP;
160 if (p->status != USB_RET_SUCCESS) {
164 if (p->actual_length < s->setup_len) {
165 s->setup_len = p->actual_length;
167 s->setup_state = SETUP_STATE_DATA;
169 if (s->setup_len == 0)
170 s->setup_state = SETUP_STATE_ACK;
172 s->setup_state = SETUP_STATE_DATA;
175 p->actual_length = 8;
178 static void do_token_in(USBDevice *s, USBPacket *p)
180 int request, value, index;
182 assert(p->ep->nr == 0);
184 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
185 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
186 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
188 switch(s->setup_state) {
189 case SETUP_STATE_ACK:
190 if (!(s->setup_buf[0] & USB_DIR_IN)) {
191 usb_device_handle_control(s, p, request, value, index,
192 s->setup_len, s->data_buf);
193 if (p->status == USB_RET_ASYNC) {
196 s->setup_state = SETUP_STATE_IDLE;
197 p->actual_length = 0;
201 case SETUP_STATE_DATA:
202 if (s->setup_buf[0] & USB_DIR_IN) {
203 int len = s->setup_len - s->setup_index;
204 if (len > p->iov.size) {
207 usb_packet_copy(p, s->data_buf + s->setup_index, len);
208 s->setup_index += len;
209 if (s->setup_index >= s->setup_len) {
210 s->setup_state = SETUP_STATE_ACK;
214 s->setup_state = SETUP_STATE_IDLE;
215 p->status = USB_RET_STALL;
219 p->status = USB_RET_STALL;
223 static void do_token_out(USBDevice *s, USBPacket *p)
225 assert(p->ep->nr == 0);
227 switch(s->setup_state) {
228 case SETUP_STATE_ACK:
229 if (s->setup_buf[0] & USB_DIR_IN) {
230 s->setup_state = SETUP_STATE_IDLE;
233 /* ignore additional output */
237 case SETUP_STATE_DATA:
238 if (!(s->setup_buf[0] & USB_DIR_IN)) {
239 int len = s->setup_len - s->setup_index;
240 if (len > p->iov.size) {
243 usb_packet_copy(p, s->data_buf + s->setup_index, len);
244 s->setup_index += len;
245 if (s->setup_index >= s->setup_len) {
246 s->setup_state = SETUP_STATE_ACK;
250 s->setup_state = SETUP_STATE_IDLE;
251 p->status = USB_RET_STALL;
255 p->status = USB_RET_STALL;
259 static void do_parameter(USBDevice *s, USBPacket *p)
261 int i, request, value, index;
263 for (i = 0; i < 8; i++) {
264 s->setup_buf[i] = p->parameter >> (i*8);
267 s->setup_state = SETUP_STATE_PARAM;
268 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
271 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
272 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
273 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
275 if (s->setup_len > sizeof(s->data_buf)) {
277 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
278 s->setup_len, sizeof(s->data_buf));
279 p->status = USB_RET_STALL;
283 if (p->pid == USB_TOKEN_OUT) {
284 usb_packet_copy(p, s->data_buf, s->setup_len);
287 usb_device_handle_control(s, p, request, value, index,
288 s->setup_len, s->data_buf);
289 if (p->status == USB_RET_ASYNC) {
293 if (p->actual_length < s->setup_len) {
294 s->setup_len = p->actual_length;
296 if (p->pid == USB_TOKEN_IN) {
297 p->actual_length = 0;
298 usb_packet_copy(p, s->data_buf, s->setup_len);
302 /* ctrl complete function for devices which use usb_generic_handle_packet and
303 may return USB_RET_ASYNC from their handle_control callback. Device code
304 which does this *must* call this function instead of the normal
305 usb_packet_complete to complete their async control packets. */
306 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
309 s->setup_state = SETUP_STATE_IDLE;
312 switch (s->setup_state) {
313 case SETUP_STATE_SETUP:
314 if (p->actual_length < s->setup_len) {
315 s->setup_len = p->actual_length;
317 s->setup_state = SETUP_STATE_DATA;
318 p->actual_length = 8;
321 case SETUP_STATE_ACK:
322 s->setup_state = SETUP_STATE_IDLE;
323 p->actual_length = 0;
326 case SETUP_STATE_PARAM:
327 if (p->actual_length < s->setup_len) {
328 s->setup_len = p->actual_length;
330 if (p->pid == USB_TOKEN_IN) {
331 p->actual_length = 0;
332 usb_packet_copy(p, s->data_buf, s->setup_len);
339 usb_packet_complete(s, p);
342 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
344 USBDevice *dev = port->dev;
346 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
349 if (dev->addr == addr) {
352 return usb_device_find_device(dev, addr);
355 static void usb_process_one(USBPacket *p)
357 USBDevice *dev = p->ep->dev;
360 * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
361 * can be USB_RET_NAK here from a previous usb_process_one() call,
362 * or USB_RET_ASYNC from going through usb_queue_one().
364 p->status = USB_RET_SUCCESS;
366 if (p->ep->nr == 0) {
369 do_parameter(dev, p);
373 case USB_TOKEN_SETUP:
374 do_token_setup(dev, p);
380 do_token_out(dev, p);
383 p->status = USB_RET_STALL;
387 usb_device_handle_data(dev, p);
391 static void usb_queue_one(USBPacket *p)
393 usb_packet_set_state(p, USB_PACKET_QUEUED);
394 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
395 p->status = USB_RET_ASYNC;
398 /* Hand over a packet to a device for processing. p->status ==
399 USB_RET_ASYNC indicates the processing isn't finished yet, the
400 driver will call usb_packet_complete() when done processing it. */
401 void usb_handle_packet(USBDevice *dev, USBPacket *p)
404 p->status = USB_RET_NODEV;
407 assert(dev == p->ep->dev);
408 assert(dev->state == USB_STATE_DEFAULT);
409 usb_packet_check_state(p, USB_PACKET_SETUP);
410 assert(p->ep != NULL);
412 /* Submitting a new packet clears halt */
414 assert(QTAILQ_EMPTY(&p->ep->queue));
415 p->ep->halted = false;
418 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
420 if (p->status == USB_RET_ASYNC) {
421 /* hcd drivers cannot handle async for isoc */
422 assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
423 /* using async for interrupt packets breaks migration */
424 assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
425 (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
426 usb_packet_set_state(p, USB_PACKET_ASYNC);
427 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
428 } else if (p->status == USB_RET_ADD_TO_QUEUE) {
432 * When pipelining is enabled usb-devices must always return async,
433 * otherwise packets can complete out of order!
435 assert(p->stream || !p->ep->pipeline ||
436 QTAILQ_EMPTY(&p->ep->queue));
437 if (p->status != USB_RET_NAK) {
438 usb_packet_set_state(p, USB_PACKET_COMPLETE);
446 void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
448 USBEndpoint *ep = p->ep;
450 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
451 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
453 if (p->status != USB_RET_SUCCESS ||
454 (p->short_not_ok && (p->actual_length < p->iov.size))) {
457 usb_packet_set_state(p, USB_PACKET_COMPLETE);
458 QTAILQ_REMOVE(&ep->queue, p, queue);
459 dev->port->ops->complete(dev->port, p);
462 /* Notify the controller that an async packet is complete. This should only
463 be called for packets previously deferred by returning USB_RET_ASYNC from
465 void usb_packet_complete(USBDevice *dev, USBPacket *p)
467 USBEndpoint *ep = p->ep;
469 usb_packet_check_state(p, USB_PACKET_ASYNC);
470 usb_packet_complete_one(dev, p);
472 while (!QTAILQ_EMPTY(&ep->queue)) {
473 p = QTAILQ_FIRST(&ep->queue);
475 /* Empty the queue on a halt */
476 p->status = USB_RET_REMOVE_FROM_QUEUE;
477 dev->port->ops->complete(dev->port, p);
480 if (p->state == USB_PACKET_ASYNC) {
483 usb_packet_check_state(p, USB_PACKET_QUEUED);
485 if (p->status == USB_RET_ASYNC) {
486 usb_packet_set_state(p, USB_PACKET_ASYNC);
489 usb_packet_complete_one(ep->dev, p);
493 /* Cancel an active packet. The packed must have been deferred by
494 returning USB_RET_ASYNC from handle_packet, and not yet
496 void usb_cancel_packet(USBPacket * p)
498 bool callback = (p->state == USB_PACKET_ASYNC);
499 assert(usb_packet_is_inflight(p));
500 usb_packet_set_state(p, USB_PACKET_CANCELED);
501 QTAILQ_REMOVE(&p->ep->queue, p, queue);
503 usb_device_cancel_packet(p->ep->dev, p);
508 void usb_packet_init(USBPacket *p)
510 qemu_iovec_init(&p->iov, 1);
513 static const char *usb_packet_state_name(USBPacketState state)
515 static const char *name[] = {
516 [USB_PACKET_UNDEFINED] = "undef",
517 [USB_PACKET_SETUP] = "setup",
518 [USB_PACKET_QUEUED] = "queued",
519 [USB_PACKET_ASYNC] = "async",
520 [USB_PACKET_COMPLETE] = "complete",
521 [USB_PACKET_CANCELED] = "canceled",
523 if (state < ARRAY_SIZE(name)) {
529 void usb_packet_check_state(USBPacket *p, USBPacketState expected)
534 if (p->state == expected) {
538 bus = usb_bus_from_device(dev);
539 trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
540 usb_packet_state_name(p->state),
541 usb_packet_state_name(expected));
542 assert(!"usb packet state check failed");
545 void usb_packet_set_state(USBPacket *p, USBPacketState state)
548 USBDevice *dev = p->ep->dev;
549 USBBus *bus = usb_bus_from_device(dev);
550 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
551 usb_packet_state_name(p->state),
552 usb_packet_state_name(state));
554 trace_usb_packet_state_change(-1, "", -1, p,
555 usb_packet_state_name(p->state),
556 usb_packet_state_name(state));
561 void usb_packet_setup(USBPacket *p, int pid,
562 USBEndpoint *ep, unsigned int stream,
563 uint64_t id, bool short_not_ok, bool int_req)
565 assert(!usb_packet_is_inflight(p));
566 assert(p->iov.iov != NULL);
571 p->status = USB_RET_SUCCESS;
572 p->actual_length = 0;
574 p->short_not_ok = short_not_ok;
575 p->int_req = int_req;
577 qemu_iovec_reset(&p->iov);
578 usb_packet_set_state(p, USB_PACKET_SETUP);
581 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
583 qemu_iovec_add(&p->iov, ptr, len);
586 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
588 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
590 assert(p->actual_length >= 0);
591 assert(p->actual_length + bytes <= iov->size);
593 case USB_TOKEN_SETUP:
595 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
598 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
601 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
604 p->actual_length += bytes;
607 void usb_packet_skip(USBPacket *p, size_t bytes)
609 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
611 assert(p->actual_length >= 0);
612 assert(p->actual_length + bytes <= iov->size);
613 if (p->pid == USB_TOKEN_IN) {
614 iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
616 p->actual_length += bytes;
619 size_t usb_packet_size(USBPacket *p)
621 return p->combined ? p->combined->iov.size : p->iov.size;
624 void usb_packet_cleanup(USBPacket *p)
626 assert(!usb_packet_is_inflight(p));
627 qemu_iovec_destroy(&p->iov);
630 void usb_ep_reset(USBDevice *dev)
635 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
636 dev->ep_ctl.ifnum = 0;
637 dev->ep_ctl.max_packet_size = 64;
638 dev->ep_ctl.max_streams = 0;
639 dev->ep_ctl.dev = dev;
640 dev->ep_ctl.pipeline = false;
641 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
642 dev->ep_in[ep].nr = ep + 1;
643 dev->ep_out[ep].nr = ep + 1;
644 dev->ep_in[ep].pid = USB_TOKEN_IN;
645 dev->ep_out[ep].pid = USB_TOKEN_OUT;
646 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
647 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
648 dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
649 dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
650 dev->ep_in[ep].max_packet_size = 0;
651 dev->ep_out[ep].max_packet_size = 0;
652 dev->ep_in[ep].max_streams = 0;
653 dev->ep_out[ep].max_streams = 0;
654 dev->ep_in[ep].dev = dev;
655 dev->ep_out[ep].dev = dev;
656 dev->ep_in[ep].pipeline = false;
657 dev->ep_out[ep].pipeline = false;
661 void usb_ep_init(USBDevice *dev)
666 QTAILQ_INIT(&dev->ep_ctl.queue);
667 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
668 QTAILQ_INIT(&dev->ep_in[ep].queue);
669 QTAILQ_INIT(&dev->ep_out[ep].queue);
673 void usb_ep_dump(USBDevice *dev)
675 static const char *tname[] = {
676 [USB_ENDPOINT_XFER_CONTROL] = "control",
677 [USB_ENDPOINT_XFER_ISOC] = "isoc",
678 [USB_ENDPOINT_XFER_BULK] = "bulk",
679 [USB_ENDPOINT_XFER_INT] = "int",
681 int ifnum, ep, first;
683 fprintf(stderr, "Device \"%s\", config %d\n",
684 dev->product_desc, dev->configuration);
685 for (ifnum = 0; ifnum < 16; ifnum++) {
687 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
688 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
689 dev->ep_in[ep].ifnum == ifnum) {
692 fprintf(stderr, " Interface %d, alternative %d\n",
693 ifnum, dev->altsetting[ifnum]);
695 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
696 tname[dev->ep_in[ep].type],
697 dev->ep_in[ep].max_packet_size);
699 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
700 dev->ep_out[ep].ifnum == ifnum) {
703 fprintf(stderr, " Interface %d, alternative %d\n",
704 ifnum, dev->altsetting[ifnum]);
706 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
707 tname[dev->ep_out[ep].type],
708 dev->ep_out[ep].max_packet_size);
712 fprintf(stderr, "--\n");
715 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
717 struct USBEndpoint *eps;
723 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
724 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
725 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
729 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
731 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
735 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
737 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
741 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
743 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
747 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
750 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
751 int size, microframes;
754 switch ((raw >> 11) & 3) {
765 uep->max_packet_size = size * microframes;
768 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
770 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
773 MaxStreams = raw & 0x1f;
775 uep->max_streams = 1 << MaxStreams;
777 uep->max_streams = 0;
781 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
783 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
784 uep->halted = halted;
787 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
790 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
793 QTAILQ_FOREACH(p, &uep->queue, queue) {