2 * USB UHCI controller emulation
4 * Copyright (c) 2005 Fabrice Bellard
6 * Copyright (c) 2008 Max Krasnyansky
7 * Magor rewrite of the UHCI data structures parser and frame processor
8 * Support for fully async operation and multiple outstanding transactions
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 #include "qemu-timer.h"
37 //#define DEBUG_DUMP_DATA
39 #define UHCI_CMD_FGR (1 << 4)
40 #define UHCI_CMD_EGSM (1 << 3)
41 #define UHCI_CMD_GRESET (1 << 2)
42 #define UHCI_CMD_HCRESET (1 << 1)
43 #define UHCI_CMD_RS (1 << 0)
45 #define UHCI_STS_HCHALTED (1 << 5)
46 #define UHCI_STS_HCPERR (1 << 4)
47 #define UHCI_STS_HSERR (1 << 3)
48 #define UHCI_STS_RD (1 << 2)
49 #define UHCI_STS_USBERR (1 << 1)
50 #define UHCI_STS_USBINT (1 << 0)
52 #define TD_CTRL_SPD (1 << 29)
53 #define TD_CTRL_ERROR_SHIFT 27
54 #define TD_CTRL_IOS (1 << 25)
55 #define TD_CTRL_IOC (1 << 24)
56 #define TD_CTRL_ACTIVE (1 << 23)
57 #define TD_CTRL_STALL (1 << 22)
58 #define TD_CTRL_BABBLE (1 << 20)
59 #define TD_CTRL_NAK (1 << 19)
60 #define TD_CTRL_TIMEOUT (1 << 18)
62 #define UHCI_PORT_SUSPEND (1 << 12)
63 #define UHCI_PORT_RESET (1 << 9)
64 #define UHCI_PORT_LSDA (1 << 8)
65 #define UHCI_PORT_RD (1 << 6)
66 #define UHCI_PORT_ENC (1 << 3)
67 #define UHCI_PORT_EN (1 << 2)
68 #define UHCI_PORT_CSC (1 << 1)
69 #define UHCI_PORT_CCS (1 << 0)
71 #define UHCI_PORT_READ_ONLY (0x1bb)
72 #define UHCI_PORT_WRITE_CLEAR (UHCI_PORT_CSC | UHCI_PORT_ENC)
74 #define FRAME_TIMER_FREQ 1000
76 #define FRAME_MAX_LOOPS 256
81 TD_RESULT_STOP_FRAME = 10,
84 TD_RESULT_ASYNC_START,
88 typedef struct UHCIState UHCIState;
89 typedef struct UHCIAsync UHCIAsync;
90 typedef struct UHCIQueue UHCIQueue;
91 typedef struct UHCIInfo UHCIInfo;
92 typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass;
100 int (*initfn)(PCIDevice *dev);
104 struct UHCIPCIDeviceClass {
105 PCIDeviceClass parent_class;
110 * Pending async transaction.
111 * 'packet' must be the first field because completion
112 * handler does "(UHCIAsync *) pkt" cast.
119 QTAILQ_ENTRY(UHCIAsync) next;
129 QTAILQ_ENTRY(UHCIQueue) next;
130 QTAILQ_HEAD(asyncs_head, UHCIAsync) asyncs;
134 typedef struct UHCIPort {
142 USBBus bus; /* Note unused when we're a companion controller */
143 uint16_t cmd; /* cmd register */
145 uint16_t intr; /* interrupt enable register */
146 uint16_t frnum; /* frame number */
147 uint32_t fl_base_addr; /* frame list base address */
149 uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
151 QEMUTimer *frame_timer;
153 uint32_t frame_bytes;
154 uint32_t frame_bandwidth;
155 UHCIPort ports[NB_PORTS];
157 /* Interrupts that should be raised at the end of the current frame. */
158 uint32_t pending_int_mask;
162 QTAILQ_HEAD(, UHCIQueue) queues;
163 uint8_t num_ports_vmstate;
170 typedef struct UHCI_TD {
172 uint32_t ctrl; /* see TD_CTRL_xxx */
177 typedef struct UHCI_QH {
182 static void uhci_async_cancel(UHCIAsync *async);
183 static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
185 static inline int32_t uhci_queue_token(UHCI_TD *td)
187 if ((td->token & (0xf << 15)) == 0) {
188 /* ctrl ep, cover ep and dev, not pid! */
189 return td->token & 0x7ff00;
191 /* covers ep, dev, pid -> identifies the endpoint */
192 return td->token & 0x7ffff;
196 static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td,
201 queue = g_new0(UHCIQueue, 1);
203 queue->qh_addr = qh_addr;
204 queue->token = uhci_queue_token(td);
206 QTAILQ_INIT(&queue->asyncs);
207 QTAILQ_INSERT_HEAD(&s->queues, queue, next);
208 /* valid needs to be large enough to handle 10 frame delay
209 * for initial isochronous requests */
211 trace_usb_uhci_queue_add(queue->token);
215 static void uhci_queue_free(UHCIQueue *queue, const char *reason)
217 UHCIState *s = queue->uhci;
220 while (!QTAILQ_EMPTY(&queue->asyncs)) {
221 async = QTAILQ_FIRST(&queue->asyncs);
222 uhci_async_cancel(async);
225 trace_usb_uhci_queue_del(queue->token, reason);
226 QTAILQ_REMOVE(&s->queues, queue, next);
230 static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td)
232 uint32_t token = uhci_queue_token(td);
235 QTAILQ_FOREACH(queue, &s->queues, next) {
236 if (queue->token == token) {
243 static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td,
244 uint32_t td_addr, bool queuing)
246 UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs);
248 return queue->qh_addr == qh_addr &&
249 queue->token == uhci_queue_token(td) &&
250 (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL ||
251 first->td_addr == td_addr);
254 static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
256 UHCIAsync *async = g_new0(UHCIAsync, 1);
258 async->queue = queue;
259 async->td_addr = td_addr;
260 usb_packet_init(&async->packet);
261 pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
262 trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
267 static void uhci_async_free(UHCIAsync *async)
269 trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
270 usb_packet_cleanup(&async->packet);
271 qemu_sglist_destroy(&async->sgl);
275 static void uhci_async_link(UHCIAsync *async)
277 UHCIQueue *queue = async->queue;
278 QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
279 trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
282 static void uhci_async_unlink(UHCIAsync *async)
284 UHCIQueue *queue = async->queue;
285 QTAILQ_REMOVE(&queue->asyncs, async, next);
286 trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
289 static void uhci_async_cancel(UHCIAsync *async)
291 uhci_async_unlink(async);
292 trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
295 usb_cancel_packet(&async->packet);
296 usb_packet_unmap(&async->packet, &async->sgl);
297 uhci_async_free(async);
301 * Mark all outstanding async packets as invalid.
302 * This is used for canceling them when TDs are removed by the HCD.
304 static void uhci_async_validate_begin(UHCIState *s)
308 QTAILQ_FOREACH(queue, &s->queues, next) {
314 * Cancel async packets that are no longer valid
316 static void uhci_async_validate_end(UHCIState *s)
318 UHCIQueue *queue, *n;
320 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
322 uhci_queue_free(queue, "validate-end");
327 static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
329 UHCIQueue *queue, *n;
331 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
332 if (queue->ep->dev == dev) {
333 uhci_queue_free(queue, "cancel-device");
338 static void uhci_async_cancel_all(UHCIState *s)
340 UHCIQueue *queue, *nq;
342 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
343 uhci_queue_free(queue, "cancel-all");
347 static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr)
352 QTAILQ_FOREACH(queue, &s->queues, next) {
353 QTAILQ_FOREACH(async, &queue->asyncs, next) {
354 if (async->td_addr == td_addr) {
362 static void uhci_update_irq(UHCIState *s)
365 if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
366 ((s->status2 & 2) && (s->intr & (1 << 3))) ||
367 ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
368 ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
369 (s->status & UHCI_STS_HSERR) ||
370 (s->status & UHCI_STS_HCPERR)) {
375 qemu_set_irq(s->dev.irq[s->irq_pin], level);
378 static void uhci_reset(void *opaque)
380 UHCIState *s = opaque;
385 trace_usb_uhci_reset();
387 pci_conf = s->dev.config;
389 pci_conf[0x6a] = 0x01; /* usb clock */
390 pci_conf[0x6b] = 0x00;
398 for(i = 0; i < NB_PORTS; i++) {
401 if (port->port.dev && port->port.dev->attached) {
402 usb_port_reset(&port->port);
406 uhci_async_cancel_all(s);
407 qemu_bh_cancel(s->bh);
411 static const VMStateDescription vmstate_uhci_port = {
414 .minimum_version_id = 1,
415 .minimum_version_id_old = 1,
416 .fields = (VMStateField []) {
417 VMSTATE_UINT16(ctrl, UHCIPort),
418 VMSTATE_END_OF_LIST()
422 static int uhci_post_load(void *opaque, int version_id)
424 UHCIState *s = opaque;
426 if (version_id < 2) {
427 s->expire_time = qemu_get_clock_ns(vm_clock) +
428 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
433 static const VMStateDescription vmstate_uhci = {
436 .minimum_version_id = 1,
437 .minimum_version_id_old = 1,
438 .post_load = uhci_post_load,
439 .fields = (VMStateField []) {
440 VMSTATE_PCI_DEVICE(dev, UHCIState),
441 VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
442 VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
443 vmstate_uhci_port, UHCIPort),
444 VMSTATE_UINT16(cmd, UHCIState),
445 VMSTATE_UINT16(status, UHCIState),
446 VMSTATE_UINT16(intr, UHCIState),
447 VMSTATE_UINT16(frnum, UHCIState),
448 VMSTATE_UINT32(fl_base_addr, UHCIState),
449 VMSTATE_UINT8(sof_timing, UHCIState),
450 VMSTATE_UINT8(status2, UHCIState),
451 VMSTATE_TIMER(frame_timer, UHCIState),
452 VMSTATE_INT64_V(expire_time, UHCIState, 2),
453 VMSTATE_END_OF_LIST()
457 static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
459 UHCIState *s = opaque;
469 static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
471 UHCIState *s = opaque;
486 static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
488 UHCIState *s = opaque;
491 trace_usb_uhci_mmio_writew(addr, val);
495 if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
496 /* start frame processing */
497 trace_usb_uhci_schedule_start();
498 s->expire_time = qemu_get_clock_ns(vm_clock) +
499 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
500 qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
501 s->status &= ~UHCI_STS_HCHALTED;
502 } else if (!(val & UHCI_CMD_RS)) {
503 s->status |= UHCI_STS_HCHALTED;
505 if (val & UHCI_CMD_GRESET) {
509 /* send reset on the USB bus */
510 for(i = 0; i < NB_PORTS; i++) {
512 usb_device_reset(port->port.dev);
517 if (val & UHCI_CMD_HCRESET) {
525 /* XXX: the chip spec is not coherent, so we add a hidden
526 register to distinguish between IOC and SPD */
527 if (val & UHCI_STS_USBINT)
536 if (s->status & UHCI_STS_HCHALTED)
537 s->frnum = val & 0x7ff;
549 dev = port->port.dev;
550 if (dev && dev->attached) {
552 if ( (val & UHCI_PORT_RESET) &&
553 !(port->ctrl & UHCI_PORT_RESET) ) {
554 usb_device_reset(dev);
557 port->ctrl &= UHCI_PORT_READ_ONLY;
558 port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
559 /* some bits are reset when a '1' is written to them */
560 port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
566 static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
568 UHCIState *s = opaque;
598 val = 0xff7f; /* disabled port */
602 trace_usb_uhci_mmio_readw(addr, val);
607 static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
609 UHCIState *s = opaque;
612 trace_usb_uhci_mmio_writel(addr, val);
616 s->fl_base_addr = val & ~0xfff;
621 static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
623 UHCIState *s = opaque;
629 val = s->fl_base_addr;
635 trace_usb_uhci_mmio_readl(addr, val);
639 /* signal resume if controller suspended */
640 static void uhci_resume (void *opaque)
642 UHCIState *s = (UHCIState *)opaque;
647 if (s->cmd & UHCI_CMD_EGSM) {
648 s->cmd |= UHCI_CMD_FGR;
649 s->status |= UHCI_STS_RD;
654 static void uhci_attach(USBPort *port1)
656 UHCIState *s = port1->opaque;
657 UHCIPort *port = &s->ports[port1->index];
659 /* set connect status */
660 port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
663 if (port->port.dev->speed == USB_SPEED_LOW) {
664 port->ctrl |= UHCI_PORT_LSDA;
666 port->ctrl &= ~UHCI_PORT_LSDA;
672 static void uhci_detach(USBPort *port1)
674 UHCIState *s = port1->opaque;
675 UHCIPort *port = &s->ports[port1->index];
677 uhci_async_cancel_device(s, port1->dev);
679 /* set connect status */
680 if (port->ctrl & UHCI_PORT_CCS) {
681 port->ctrl &= ~UHCI_PORT_CCS;
682 port->ctrl |= UHCI_PORT_CSC;
685 if (port->ctrl & UHCI_PORT_EN) {
686 port->ctrl &= ~UHCI_PORT_EN;
687 port->ctrl |= UHCI_PORT_ENC;
693 static void uhci_child_detach(USBPort *port1, USBDevice *child)
695 UHCIState *s = port1->opaque;
697 uhci_async_cancel_device(s, child);
700 static void uhci_wakeup(USBPort *port1)
702 UHCIState *s = port1->opaque;
703 UHCIPort *port = &s->ports[port1->index];
705 if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
706 port->ctrl |= UHCI_PORT_RD;
711 static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
716 for (i = 0; i < NB_PORTS; i++) {
717 UHCIPort *port = &s->ports[i];
718 if (!(port->ctrl & UHCI_PORT_EN)) {
721 dev = usb_find_device(&port->port, addr);
729 static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
731 pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
732 le32_to_cpus(&td->link);
733 le32_to_cpus(&td->ctrl);
734 le32_to_cpus(&td->token);
735 le32_to_cpus(&td->buffer);
738 static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
740 int len = 0, max_len, err, ret;
743 max_len = ((td->token >> 21) + 1) & 0x7ff;
744 pid = td->token & 0xff;
746 ret = async->packet.result;
748 if (td->ctrl & TD_CTRL_IOS)
749 td->ctrl &= ~TD_CTRL_ACTIVE;
754 len = async->packet.result;
755 td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
757 /* The NAK bit may have been set by a previous frame, so clear it
758 here. The docs are somewhat unclear, but win2k relies on this
760 td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
761 if (td->ctrl & TD_CTRL_IOC)
764 if (pid == USB_TOKEN_IN) {
765 if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
767 /* short packet: do not update QH */
768 trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
770 return TD_RESULT_NEXT_QH;
775 trace_usb_uhci_packet_complete_success(async->queue->token,
777 return TD_RESULT_COMPLETE;
782 td->ctrl |= TD_CTRL_NAK;
783 return TD_RESULT_NEXT_QH;
786 td->ctrl |= TD_CTRL_STALL;
787 trace_usb_uhci_packet_complete_stall(async->queue->token,
789 err = TD_RESULT_NEXT_QH;
793 td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
794 /* frame interrupted */
795 trace_usb_uhci_packet_complete_babble(async->queue->token,
797 err = TD_RESULT_STOP_FRAME;
800 case USB_RET_IOERROR:
803 td->ctrl |= TD_CTRL_TIMEOUT;
804 td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
805 trace_usb_uhci_packet_complete_error(async->queue->token,
807 err = TD_RESULT_NEXT_QH;
811 td->ctrl &= ~TD_CTRL_ACTIVE;
812 s->status |= UHCI_STS_USBERR;
813 if (td->ctrl & TD_CTRL_IOC) {
820 static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr,
821 UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
823 int len = 0, max_len;
825 bool queuing = (q != NULL);
826 uint8_t pid = td->token & 0xff;
827 UHCIAsync *async = uhci_async_find_td(s, td_addr);
830 if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) {
831 assert(q == NULL || q == async->queue);
834 uhci_queue_free(async->queue, "guest re-used pending td");
840 q = uhci_queue_find(s, td);
841 if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) {
842 uhci_queue_free(q, "guest re-used qh");
852 if (!(td->ctrl & TD_CTRL_ACTIVE)) {
854 /* Guest marked a pending td non-active, cancel the queue */
855 uhci_queue_free(async->queue, "pending td non-active");
858 * ehci11d spec page 22: "Even if the Active bit in the TD is already
859 * cleared when the TD is fetched ... an IOC interrupt is generated"
861 if (td->ctrl & TD_CTRL_IOC) {
864 return TD_RESULT_NEXT_QH;
869 /* we are busy filling the queue, we are not prepared
870 to consume completed packages then, just leave them
872 return TD_RESULT_ASYNC_CONT;
876 UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs, asyncs_head);
878 * While we are waiting for the current td to complete, the guest
879 * may have added more tds to the queue. Note we re-read the td
880 * rather then caching it, as we want to see guest made changes!
882 uhci_read_td(s, &last_td, last->td_addr);
883 uhci_queue_fill(async->queue, &last_td);
885 return TD_RESULT_ASYNC_CONT;
887 uhci_async_unlink(async);
891 /* Allocate new packet */
893 USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
894 USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
895 q = uhci_queue_new(s, qh_addr, td, ep);
897 async = uhci_async_alloc(q, td_addr);
899 max_len = ((td->token >> 21) + 1) & 0x7ff;
900 spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
901 usb_packet_setup(&async->packet, pid, q->ep, td_addr, spd,
902 (td->ctrl & TD_CTRL_IOC) != 0);
903 qemu_sglist_add(&async->sgl, td->buffer, max_len);
904 usb_packet_map(&async->packet, &async->sgl);
908 case USB_TOKEN_SETUP:
909 len = usb_handle_packet(q->ep->dev, &async->packet);
915 len = usb_handle_packet(q->ep->dev, &async->packet);
919 /* invalid pid : frame interrupted */
920 usb_packet_unmap(&async->packet, &async->sgl);
921 uhci_async_free(async);
922 s->status |= UHCI_STS_HCPERR;
924 return TD_RESULT_STOP_FRAME;
927 if (len == USB_RET_ASYNC) {
928 uhci_async_link(async);
930 uhci_queue_fill(q, td);
932 return TD_RESULT_ASYNC_START;
935 async->packet.result = len;
938 len = uhci_complete_td(s, td, async, int_mask);
939 usb_packet_unmap(&async->packet, &async->sgl);
940 uhci_async_free(async);
944 static void uhci_async_complete(USBPort *port, USBPacket *packet)
946 UHCIAsync *async = container_of(packet, UHCIAsync, packet);
947 UHCIState *s = async->queue->uhci;
949 if (packet->result == USB_RET_REMOVE_FROM_QUEUE) {
950 uhci_async_unlink(async);
951 uhci_async_cancel(async);
956 if (s->frame_bytes < s->frame_bandwidth) {
957 qemu_bh_schedule(s->bh);
961 static int is_valid(uint32_t link)
963 return (link & 1) == 0;
966 static int is_qh(uint32_t link)
968 return (link & 2) != 0;
971 static int depth_first(uint32_t link)
973 return (link & 4) != 0;
976 /* QH DB used for detecting QH loops */
977 #define UHCI_MAX_QUEUES 128
979 uint32_t addr[UHCI_MAX_QUEUES];
983 static void qhdb_reset(QhDb *db)
988 /* Add QH to DB. Returns 1 if already present or DB is full. */
989 static int qhdb_insert(QhDb *db, uint32_t addr)
992 for (i = 0; i < db->count; i++)
993 if (db->addr[i] == addr)
996 if (db->count >= UHCI_MAX_QUEUES)
999 db->addr[db->count++] = addr;
1003 static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
1005 uint32_t int_mask = 0;
1006 uint32_t plink = td->link;
1010 while (is_valid(plink)) {
1011 uhci_read_td(q->uhci, &ptd, plink);
1012 if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
1015 if (uhci_queue_token(&ptd) != q->token) {
1018 trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
1019 ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask);
1020 if (ret == TD_RESULT_ASYNC_CONT) {
1023 assert(ret == TD_RESULT_ASYNC_START);
1024 assert(int_mask == 0);
1027 usb_device_flush_ep_queue(q->ep->dev, q->ep);
1030 static void uhci_process_frame(UHCIState *s)
1032 uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
1033 uint32_t curr_qh, td_count = 0;
1039 frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
1041 pci_dma_read(&s->dev, frame_addr, &link, 4);
1042 le32_to_cpus(&link);
1049 for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
1050 if (s->frame_bytes >= s->frame_bandwidth) {
1051 /* We've reached the usb 1.1 bandwidth, which is
1052 1280 bytes/frame, stop processing */
1053 trace_usb_uhci_frame_stop_bandwidth();
1058 trace_usb_uhci_qh_load(link & ~0xf);
1060 if (qhdb_insert(&qhdb, link)) {
1062 * We're going in circles. Which is not a bug because
1063 * HCD is allowed to do that as part of the BW management.
1065 * Stop processing here if no transaction has been done
1066 * since we've been here last time.
1068 if (td_count == 0) {
1069 trace_usb_uhci_frame_loop_stop_idle();
1072 trace_usb_uhci_frame_loop_continue();
1075 qhdb_insert(&qhdb, link);
1079 pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
1080 le32_to_cpus(&qh.link);
1081 le32_to_cpus(&qh.el_link);
1083 if (!is_valid(qh.el_link)) {
1084 /* QH w/o elements */
1088 /* QH with elements */
1096 uhci_read_td(s, &td, link);
1097 trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
1099 old_td_ctrl = td.ctrl;
1100 ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask);
1101 if (old_td_ctrl != td.ctrl) {
1102 /* update the status bits of the TD */
1103 val = cpu_to_le32(td.ctrl);
1104 pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
1108 case TD_RESULT_STOP_FRAME: /* interrupted frame */
1111 case TD_RESULT_NEXT_QH:
1112 case TD_RESULT_ASYNC_CONT:
1113 trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
1114 link = curr_qh ? qh.link : td.link;
1117 case TD_RESULT_ASYNC_START:
1118 trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
1119 link = curr_qh ? qh.link : td.link;
1122 case TD_RESULT_COMPLETE:
1123 trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
1126 s->frame_bytes += (td.ctrl & 0x7ff) + 1;
1129 /* update QH element link */
1131 val = cpu_to_le32(qh.el_link);
1132 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
1134 if (!depth_first(link)) {
1135 /* done with this QH */
1143 assert(!"unknown return code");
1146 /* go to the next entry */
1150 s->pending_int_mask |= int_mask;
1153 static void uhci_bh(void *opaque)
1155 UHCIState *s = opaque;
1156 uhci_process_frame(s);
1159 static void uhci_frame_timer(void *opaque)
1161 UHCIState *s = opaque;
1163 /* prepare the timer for the next frame */
1164 s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
1166 qemu_bh_cancel(s->bh);
1168 if (!(s->cmd & UHCI_CMD_RS)) {
1170 trace_usb_uhci_schedule_stop();
1171 qemu_del_timer(s->frame_timer);
1172 uhci_async_cancel_all(s);
1173 /* set hchalted bit in status - UHCI11D 2.1.2 */
1174 s->status |= UHCI_STS_HCHALTED;
1178 /* Complete the previous frame */
1179 if (s->pending_int_mask) {
1180 s->status2 |= s->pending_int_mask;
1181 s->status |= UHCI_STS_USBINT;
1184 s->pending_int_mask = 0;
1186 /* Start new frame */
1187 s->frnum = (s->frnum + 1) & 0x7ff;
1189 trace_usb_uhci_frame_start(s->frnum);
1191 uhci_async_validate_begin(s);
1193 uhci_process_frame(s);
1195 uhci_async_validate_end(s);
1197 qemu_mod_timer(s->frame_timer, s->expire_time);
1200 static const MemoryRegionPortio uhci_portio[] = {
1201 { 0, 32, 2, .write = uhci_ioport_writew, },
1202 { 0, 32, 2, .read = uhci_ioport_readw, },
1203 { 0, 32, 4, .write = uhci_ioport_writel, },
1204 { 0, 32, 4, .read = uhci_ioport_readl, },
1205 { 0, 32, 1, .write = uhci_ioport_writeb, },
1206 { 0, 32, 1, .read = uhci_ioport_readb, },
1207 PORTIO_END_OF_LIST()
1210 static const MemoryRegionOps uhci_ioport_ops = {
1211 .old_portio = uhci_portio,
1214 static USBPortOps uhci_port_ops = {
1215 .attach = uhci_attach,
1216 .detach = uhci_detach,
1217 .child_detach = uhci_child_detach,
1218 .wakeup = uhci_wakeup,
1219 .complete = uhci_async_complete,
1222 static USBBusOps uhci_bus_ops = {
1225 static int usb_uhci_common_initfn(PCIDevice *dev)
1227 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1228 UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class);
1229 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1230 uint8_t *pci_conf = s->dev.config;
1233 pci_conf[PCI_CLASS_PROG] = 0x00;
1234 /* TODO: reset value should be 0. */
1235 pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
1237 s->irq_pin = u->info.irq_pin;
1238 pci_config_set_interrupt_pin(pci_conf, s->irq_pin + 1);
1241 USBPort *ports[NB_PORTS];
1242 for(i = 0; i < NB_PORTS; i++) {
1243 ports[i] = &s->ports[i].port;
1245 if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1246 s->firstport, s, &uhci_port_ops,
1247 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1251 usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1252 for (i = 0; i < NB_PORTS; i++) {
1253 usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1254 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1257 s->bh = qemu_bh_new(uhci_bh, s);
1258 s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
1259 s->num_ports_vmstate = NB_PORTS;
1260 QTAILQ_INIT(&s->queues);
1262 qemu_register_reset(uhci_reset, s);
1264 memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
1265 /* Use region 4 for consistency with real hardware. BSD guests seem
1267 pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1272 static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1274 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1275 uint8_t *pci_conf = s->dev.config;
1277 /* USB misc control 1/2 */
1278 pci_set_long(pci_conf + 0x40,0x00001000);
1280 pci_set_long(pci_conf + 0x80,0x00020001);
1281 /* USB legacy support */
1282 pci_set_long(pci_conf + 0xc0,0x00002000);
1284 return usb_uhci_common_initfn(dev);
1287 static void usb_uhci_exit(PCIDevice *dev)
1289 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1291 memory_region_destroy(&s->io_bar);
1294 static Property uhci_properties[] = {
1295 DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1296 DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
1297 DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
1298 DEFINE_PROP_END_OF_LIST(),
1301 static void uhci_class_init(ObjectClass *klass, void *data)
1303 DeviceClass *dc = DEVICE_CLASS(klass);
1304 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1305 UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class);
1306 UHCIInfo *info = data;
1308 k->init = info->initfn ? info->initfn : usb_uhci_common_initfn;
1309 k->exit = info->unplug ? usb_uhci_exit : NULL;
1310 k->vendor_id = info->vendor_id;
1311 k->device_id = info->device_id;
1312 k->revision = info->revision;
1313 k->class_id = PCI_CLASS_SERIAL_USB;
1314 dc->vmsd = &vmstate_uhci;
1315 dc->props = uhci_properties;
1319 static UHCIInfo uhci_info[] = {
1321 .name = "piix3-usb-uhci",
1322 .vendor_id = PCI_VENDOR_ID_INTEL,
1323 .device_id = PCI_DEVICE_ID_INTEL_82371SB_2,
1328 .name = "piix4-usb-uhci",
1329 .vendor_id = PCI_VENDOR_ID_INTEL,
1330 .device_id = PCI_DEVICE_ID_INTEL_82371AB_2,
1335 .name = "vt82c686b-usb-uhci",
1336 .vendor_id = PCI_VENDOR_ID_VIA,
1337 .device_id = PCI_DEVICE_ID_VIA_UHCI,
1340 .initfn = usb_uhci_vt82c686b_initfn,
1343 .name = "ich9-usb-uhci1", /* 00:1d.0 */
1344 .vendor_id = PCI_VENDOR_ID_INTEL,
1345 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1,
1350 .name = "ich9-usb-uhci2", /* 00:1d.1 */
1351 .vendor_id = PCI_VENDOR_ID_INTEL,
1352 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2,
1357 .name = "ich9-usb-uhci3", /* 00:1d.2 */
1358 .vendor_id = PCI_VENDOR_ID_INTEL,
1359 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3,
1364 .name = "ich9-usb-uhci4", /* 00:1a.0 */
1365 .vendor_id = PCI_VENDOR_ID_INTEL,
1366 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4,
1371 .name = "ich9-usb-uhci5", /* 00:1a.1 */
1372 .vendor_id = PCI_VENDOR_ID_INTEL,
1373 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5,
1378 .name = "ich9-usb-uhci6", /* 00:1a.2 */
1379 .vendor_id = PCI_VENDOR_ID_INTEL,
1380 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6,
1387 static void uhci_register_types(void)
1389 TypeInfo uhci_type_info = {
1390 .parent = TYPE_PCI_DEVICE,
1391 .instance_size = sizeof(UHCIState),
1392 .class_size = sizeof(UHCIPCIDeviceClass),
1393 .class_init = uhci_class_init,
1397 for (i = 0; i < ARRAY_SIZE(uhci_info); i++) {
1398 uhci_type_info.name = uhci_info[i].name;
1399 uhci_type_info.class_data = uhci_info + i;
1400 type_register(&uhci_type_info);
1404 type_init(uhci_register_types)