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;
93 * Pending async transaction.
94 * 'packet' must be the first field because completion
95 * handler does "(UHCIAsync *) pkt" cast.
102 QTAILQ_ENTRY(UHCIAsync) next;
111 QTAILQ_ENTRY(UHCIQueue) next;
112 QTAILQ_HEAD(, UHCIAsync) asyncs;
116 typedef struct UHCIPort {
124 USBBus bus; /* Note unused when we're a companion controller */
125 uint16_t cmd; /* cmd register */
127 uint16_t intr; /* interrupt enable register */
128 uint16_t frnum; /* frame number */
129 uint32_t fl_base_addr; /* frame list base address */
131 uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
133 QEMUTimer *frame_timer;
135 uint32_t frame_bytes;
136 uint32_t frame_bandwidth;
137 UHCIPort ports[NB_PORTS];
139 /* Interrupts that should be raised at the end of the current frame. */
140 uint32_t pending_int_mask;
144 QTAILQ_HEAD(, UHCIQueue) queues;
145 uint8_t num_ports_vmstate;
152 typedef struct UHCI_TD {
154 uint32_t ctrl; /* see TD_CTRL_xxx */
159 typedef struct UHCI_QH {
164 static void uhci_async_cancel(UHCIAsync *async);
165 static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
167 static inline int32_t uhci_queue_token(UHCI_TD *td)
169 /* covers ep, dev, pid -> identifies the endpoint */
170 return td->token & 0x7ffff;
173 static UHCIQueue *uhci_queue_get(UHCIState *s, UHCI_TD *td, USBEndpoint *ep)
175 uint32_t token = uhci_queue_token(td);
178 QTAILQ_FOREACH(queue, &s->queues, next) {
179 if (queue->token == token) {
184 queue = g_new0(UHCIQueue, 1);
186 queue->token = token;
188 QTAILQ_INIT(&queue->asyncs);
189 QTAILQ_INSERT_HEAD(&s->queues, queue, next);
190 trace_usb_uhci_queue_add(queue->token);
194 static void uhci_queue_free(UHCIQueue *queue)
196 UHCIState *s = queue->uhci;
199 while (!QTAILQ_EMPTY(&queue->asyncs)) {
200 async = QTAILQ_FIRST(&queue->asyncs);
201 uhci_async_cancel(async);
204 trace_usb_uhci_queue_del(queue->token);
205 QTAILQ_REMOVE(&s->queues, queue, next);
209 static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
211 UHCIAsync *async = g_new0(UHCIAsync, 1);
213 async->queue = queue;
214 async->td_addr = td_addr;
215 usb_packet_init(&async->packet);
216 pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
217 trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
222 static void uhci_async_free(UHCIAsync *async)
224 trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
225 usb_packet_cleanup(&async->packet);
226 qemu_sglist_destroy(&async->sgl);
230 static void uhci_async_link(UHCIAsync *async)
232 UHCIQueue *queue = async->queue;
233 QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
234 trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
237 static void uhci_async_unlink(UHCIAsync *async)
239 UHCIQueue *queue = async->queue;
240 QTAILQ_REMOVE(&queue->asyncs, async, next);
241 trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
244 static void uhci_async_cancel(UHCIAsync *async)
246 uhci_async_unlink(async);
247 trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
250 usb_cancel_packet(&async->packet);
251 usb_packet_unmap(&async->packet, &async->sgl);
252 uhci_async_free(async);
256 * Mark all outstanding async packets as invalid.
257 * This is used for canceling them when TDs are removed by the HCD.
259 static void uhci_async_validate_begin(UHCIState *s)
263 QTAILQ_FOREACH(queue, &s->queues, next) {
269 * Cancel async packets that are no longer valid
271 static void uhci_async_validate_end(UHCIState *s)
273 UHCIQueue *queue, *n;
275 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
277 uhci_queue_free(queue);
282 static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
284 UHCIQueue *queue, *n;
286 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
287 if (queue->ep->dev == dev) {
288 uhci_queue_free(queue, "cancel-device");
293 static void uhci_async_cancel_all(UHCIState *s)
295 UHCIQueue *queue, *nq;
297 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
298 uhci_queue_free(queue);
302 static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr,
305 uint32_t token = uhci_queue_token(td);
309 QTAILQ_FOREACH(queue, &s->queues, next) {
310 if (queue->token == token) {
318 QTAILQ_FOREACH(async, &queue->asyncs, next) {
319 if (async->td_addr == td_addr) {
327 static void uhci_update_irq(UHCIState *s)
330 if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
331 ((s->status2 & 2) && (s->intr & (1 << 3))) ||
332 ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
333 ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
334 (s->status & UHCI_STS_HSERR) ||
335 (s->status & UHCI_STS_HCPERR)) {
340 qemu_set_irq(s->dev.irq[s->irq_pin], level);
343 static void uhci_reset(void *opaque)
345 UHCIState *s = opaque;
350 trace_usb_uhci_reset();
352 pci_conf = s->dev.config;
354 pci_conf[0x6a] = 0x01; /* usb clock */
355 pci_conf[0x6b] = 0x00;
363 for(i = 0; i < NB_PORTS; i++) {
366 if (port->port.dev && port->port.dev->attached) {
367 usb_port_reset(&port->port);
371 uhci_async_cancel_all(s);
372 qemu_bh_cancel(s->bh);
376 static const VMStateDescription vmstate_uhci_port = {
379 .minimum_version_id = 1,
380 .minimum_version_id_old = 1,
381 .fields = (VMStateField []) {
382 VMSTATE_UINT16(ctrl, UHCIPort),
383 VMSTATE_END_OF_LIST()
387 static int uhci_post_load(void *opaque, int version_id)
389 UHCIState *s = opaque;
391 if (version_id < 2) {
392 s->expire_time = qemu_get_clock_ns(vm_clock) +
393 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
398 static const VMStateDescription vmstate_uhci = {
401 .minimum_version_id = 1,
402 .minimum_version_id_old = 1,
403 .post_load = uhci_post_load,
404 .fields = (VMStateField []) {
405 VMSTATE_PCI_DEVICE(dev, UHCIState),
406 VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
407 VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
408 vmstate_uhci_port, UHCIPort),
409 VMSTATE_UINT16(cmd, UHCIState),
410 VMSTATE_UINT16(status, UHCIState),
411 VMSTATE_UINT16(intr, UHCIState),
412 VMSTATE_UINT16(frnum, UHCIState),
413 VMSTATE_UINT32(fl_base_addr, UHCIState),
414 VMSTATE_UINT8(sof_timing, UHCIState),
415 VMSTATE_UINT8(status2, UHCIState),
416 VMSTATE_TIMER(frame_timer, UHCIState),
417 VMSTATE_INT64_V(expire_time, UHCIState, 2),
418 VMSTATE_END_OF_LIST()
422 static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
424 UHCIState *s = opaque;
434 static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
436 UHCIState *s = opaque;
451 static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
453 UHCIState *s = opaque;
456 trace_usb_uhci_mmio_writew(addr, val);
460 if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
461 /* start frame processing */
462 trace_usb_uhci_schedule_start();
463 s->expire_time = qemu_get_clock_ns(vm_clock) +
464 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
465 qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
466 s->status &= ~UHCI_STS_HCHALTED;
467 } else if (!(val & UHCI_CMD_RS)) {
468 s->status |= UHCI_STS_HCHALTED;
470 if (val & UHCI_CMD_GRESET) {
474 /* send reset on the USB bus */
475 for(i = 0; i < NB_PORTS; i++) {
477 usb_device_reset(port->port.dev);
482 if (val & UHCI_CMD_HCRESET) {
490 /* XXX: the chip spec is not coherent, so we add a hidden
491 register to distinguish between IOC and SPD */
492 if (val & UHCI_STS_USBINT)
501 if (s->status & UHCI_STS_HCHALTED)
502 s->frnum = val & 0x7ff;
514 dev = port->port.dev;
515 if (dev && dev->attached) {
517 if ( (val & UHCI_PORT_RESET) &&
518 !(port->ctrl & UHCI_PORT_RESET) ) {
519 usb_device_reset(dev);
522 port->ctrl &= UHCI_PORT_READ_ONLY;
523 port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
524 /* some bits are reset when a '1' is written to them */
525 port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
531 static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
533 UHCIState *s = opaque;
563 val = 0xff7f; /* disabled port */
567 trace_usb_uhci_mmio_readw(addr, val);
572 static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
574 UHCIState *s = opaque;
577 trace_usb_uhci_mmio_writel(addr, val);
581 s->fl_base_addr = val & ~0xfff;
586 static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
588 UHCIState *s = opaque;
594 val = s->fl_base_addr;
600 trace_usb_uhci_mmio_readl(addr, val);
604 /* signal resume if controller suspended */
605 static void uhci_resume (void *opaque)
607 UHCIState *s = (UHCIState *)opaque;
612 if (s->cmd & UHCI_CMD_EGSM) {
613 s->cmd |= UHCI_CMD_FGR;
614 s->status |= UHCI_STS_RD;
619 static void uhci_attach(USBPort *port1)
621 UHCIState *s = port1->opaque;
622 UHCIPort *port = &s->ports[port1->index];
624 /* set connect status */
625 port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
628 if (port->port.dev->speed == USB_SPEED_LOW) {
629 port->ctrl |= UHCI_PORT_LSDA;
631 port->ctrl &= ~UHCI_PORT_LSDA;
637 static void uhci_detach(USBPort *port1)
639 UHCIState *s = port1->opaque;
640 UHCIPort *port = &s->ports[port1->index];
642 uhci_async_cancel_device(s, port1->dev);
644 /* set connect status */
645 if (port->ctrl & UHCI_PORT_CCS) {
646 port->ctrl &= ~UHCI_PORT_CCS;
647 port->ctrl |= UHCI_PORT_CSC;
650 if (port->ctrl & UHCI_PORT_EN) {
651 port->ctrl &= ~UHCI_PORT_EN;
652 port->ctrl |= UHCI_PORT_ENC;
658 static void uhci_child_detach(USBPort *port1, USBDevice *child)
660 UHCIState *s = port1->opaque;
662 uhci_async_cancel_device(s, child);
665 static void uhci_wakeup(USBPort *port1)
667 UHCIState *s = port1->opaque;
668 UHCIPort *port = &s->ports[port1->index];
670 if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
671 port->ctrl |= UHCI_PORT_RD;
676 static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
681 for (i = 0; i < NB_PORTS; i++) {
682 UHCIPort *port = &s->ports[i];
683 if (!(port->ctrl & UHCI_PORT_EN)) {
686 dev = usb_find_device(&port->port, addr);
694 static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
696 pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
697 le32_to_cpus(&td->link);
698 le32_to_cpus(&td->ctrl);
699 le32_to_cpus(&td->token);
700 le32_to_cpus(&td->buffer);
703 static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
705 int len = 0, max_len, err, ret;
708 max_len = ((td->token >> 21) + 1) & 0x7ff;
709 pid = td->token & 0xff;
711 ret = async->packet.result;
713 if (td->ctrl & TD_CTRL_IOS)
714 td->ctrl &= ~TD_CTRL_ACTIVE;
719 len = async->packet.result;
720 td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
722 /* The NAK bit may have been set by a previous frame, so clear it
723 here. The docs are somewhat unclear, but win2k relies on this
725 td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
726 if (td->ctrl & TD_CTRL_IOC)
729 if (pid == USB_TOKEN_IN) {
730 if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
732 /* short packet: do not update QH */
733 trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
735 return TD_RESULT_NEXT_QH;
740 trace_usb_uhci_packet_complete_success(async->queue->token,
742 return TD_RESULT_COMPLETE;
747 td->ctrl |= TD_CTRL_NAK;
748 return TD_RESULT_NEXT_QH;
751 td->ctrl |= TD_CTRL_STALL;
752 trace_usb_uhci_packet_complete_stall(async->queue->token,
754 err = TD_RESULT_NEXT_QH;
758 td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
759 /* frame interrupted */
760 trace_usb_uhci_packet_complete_babble(async->queue->token,
762 err = TD_RESULT_STOP_FRAME;
765 case USB_RET_IOERROR:
768 td->ctrl |= TD_CTRL_TIMEOUT;
769 td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
770 trace_usb_uhci_packet_complete_error(async->queue->token,
772 err = TD_RESULT_NEXT_QH;
776 td->ctrl &= ~TD_CTRL_ACTIVE;
777 s->status |= UHCI_STS_USBERR;
778 if (td->ctrl & TD_CTRL_IOC) {
785 static int uhci_handle_td(UHCIState *s, UHCIQueue *q,
786 UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
789 int len = 0, max_len;
791 bool queuing = (q != NULL);
792 uint8_t pid = td->token & 0xff;
795 if (!(td->ctrl & TD_CTRL_ACTIVE)) {
797 * ehci11d spec page 22: "Even if the Active bit in the TD is already
798 * cleared when the TD is fetched ... an IOC interrupt is generated"
800 if (td->ctrl & TD_CTRL_IOC) {
803 return TD_RESULT_NEXT_QH;
806 async = uhci_async_find_td(s, td_addr, td);
808 /* Already submitted */
809 async->queue->valid = 32;
812 return TD_RESULT_ASYNC_CONT;
814 /* we are busy filling the queue, we are not prepared
815 to consume completed packages then, just leave them
817 return TD_RESULT_ASYNC_CONT;
820 uhci_async_unlink(async);
824 /* Allocate new packet */
826 USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
827 USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
828 q = uhci_queue_get(s, td, ep);
830 async = uhci_async_alloc(q, td_addr);
832 /* valid needs to be large enough to handle 10 frame delay
833 * for initial isochronous requests
835 async->queue->valid = 32;
837 max_len = ((td->token >> 21) + 1) & 0x7ff;
838 spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
839 usb_packet_setup(&async->packet, pid, q->ep, td_addr, spd,
840 (td->ctrl & TD_CTRL_IOC) != 0);
841 qemu_sglist_add(&async->sgl, td->buffer, max_len);
842 usb_packet_map(&async->packet, &async->sgl);
846 case USB_TOKEN_SETUP:
847 len = usb_handle_packet(q->ep->dev, &async->packet);
853 len = usb_handle_packet(q->ep->dev, &async->packet);
857 /* invalid pid : frame interrupted */
858 usb_packet_unmap(&async->packet, &async->sgl);
859 uhci_async_free(async);
860 s->status |= UHCI_STS_HCPERR;
862 return TD_RESULT_STOP_FRAME;
865 if (len == USB_RET_ASYNC) {
866 uhci_async_link(async);
868 uhci_queue_fill(q, td);
870 return TD_RESULT_ASYNC_START;
873 async->packet.result = len;
876 len = uhci_complete_td(s, td, async, int_mask);
877 usb_packet_unmap(&async->packet, &async->sgl);
878 uhci_async_free(async);
882 static void uhci_async_complete(USBPort *port, USBPacket *packet)
884 UHCIAsync *async = container_of(packet, UHCIAsync, packet);
885 UHCIState *s = async->queue->uhci;
887 if (packet->result == USB_RET_REMOVE_FROM_QUEUE) {
888 uhci_async_unlink(async);
889 uhci_async_cancel(async);
894 if (s->frame_bytes < s->frame_bandwidth) {
895 qemu_bh_schedule(s->bh);
899 static int is_valid(uint32_t link)
901 return (link & 1) == 0;
904 static int is_qh(uint32_t link)
906 return (link & 2) != 0;
909 static int depth_first(uint32_t link)
911 return (link & 4) != 0;
914 /* QH DB used for detecting QH loops */
915 #define UHCI_MAX_QUEUES 128
917 uint32_t addr[UHCI_MAX_QUEUES];
921 static void qhdb_reset(QhDb *db)
926 /* Add QH to DB. Returns 1 if already present or DB is full. */
927 static int qhdb_insert(QhDb *db, uint32_t addr)
930 for (i = 0; i < db->count; i++)
931 if (db->addr[i] == addr)
934 if (db->count >= UHCI_MAX_QUEUES)
937 db->addr[db->count++] = addr;
941 static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
943 uint32_t int_mask = 0;
944 uint32_t plink = td->link;
948 while (is_valid(plink)) {
949 uhci_read_td(q->uhci, &ptd, plink);
950 if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
953 if (uhci_queue_token(&ptd) != q->token) {
956 trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
957 ret = uhci_handle_td(q->uhci, q, &ptd, plink, &int_mask);
958 if (ret == TD_RESULT_ASYNC_CONT) {
961 assert(ret == TD_RESULT_ASYNC_START);
962 assert(int_mask == 0);
965 usb_device_flush_ep_queue(q->ep->dev, q->ep);
968 static void uhci_process_frame(UHCIState *s)
970 uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
971 uint32_t curr_qh, td_count = 0;
977 frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
979 pci_dma_read(&s->dev, frame_addr, &link, 4);
987 for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
988 if (s->frame_bytes >= s->frame_bandwidth) {
989 /* We've reached the usb 1.1 bandwidth, which is
990 1280 bytes/frame, stop processing */
991 trace_usb_uhci_frame_stop_bandwidth();
996 trace_usb_uhci_qh_load(link & ~0xf);
998 if (qhdb_insert(&qhdb, link)) {
1000 * We're going in circles. Which is not a bug because
1001 * HCD is allowed to do that as part of the BW management.
1003 * Stop processing here if no transaction has been done
1004 * since we've been here last time.
1006 if (td_count == 0) {
1007 trace_usb_uhci_frame_loop_stop_idle();
1010 trace_usb_uhci_frame_loop_continue();
1013 qhdb_insert(&qhdb, link);
1017 pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
1018 le32_to_cpus(&qh.link);
1019 le32_to_cpus(&qh.el_link);
1021 if (!is_valid(qh.el_link)) {
1022 /* QH w/o elements */
1026 /* QH with elements */
1034 uhci_read_td(s, &td, link);
1035 trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
1037 old_td_ctrl = td.ctrl;
1038 ret = uhci_handle_td(s, NULL, &td, link, &int_mask);
1039 if (old_td_ctrl != td.ctrl) {
1040 /* update the status bits of the TD */
1041 val = cpu_to_le32(td.ctrl);
1042 pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
1046 case TD_RESULT_STOP_FRAME: /* interrupted frame */
1049 case TD_RESULT_NEXT_QH:
1050 case TD_RESULT_ASYNC_CONT:
1051 trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
1052 link = curr_qh ? qh.link : td.link;
1055 case TD_RESULT_ASYNC_START:
1056 trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
1057 link = curr_qh ? qh.link : td.link;
1060 case TD_RESULT_COMPLETE:
1061 trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
1064 s->frame_bytes += (td.ctrl & 0x7ff) + 1;
1067 /* update QH element link */
1069 val = cpu_to_le32(qh.el_link);
1070 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
1072 if (!depth_first(link)) {
1073 /* done with this QH */
1081 assert(!"unknown return code");
1084 /* go to the next entry */
1088 s->pending_int_mask |= int_mask;
1091 static void uhci_bh(void *opaque)
1093 UHCIState *s = opaque;
1094 uhci_process_frame(s);
1097 static void uhci_frame_timer(void *opaque)
1099 UHCIState *s = opaque;
1101 /* prepare the timer for the next frame */
1102 s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
1104 qemu_bh_cancel(s->bh);
1106 if (!(s->cmd & UHCI_CMD_RS)) {
1108 trace_usb_uhci_schedule_stop();
1109 qemu_del_timer(s->frame_timer);
1110 uhci_async_cancel_all(s);
1111 /* set hchalted bit in status - UHCI11D 2.1.2 */
1112 s->status |= UHCI_STS_HCHALTED;
1116 /* Complete the previous frame */
1117 if (s->pending_int_mask) {
1118 s->status2 |= s->pending_int_mask;
1119 s->status |= UHCI_STS_USBINT;
1122 s->pending_int_mask = 0;
1124 /* Start new frame */
1125 s->frnum = (s->frnum + 1) & 0x7ff;
1127 trace_usb_uhci_frame_start(s->frnum);
1129 uhci_async_validate_begin(s);
1131 uhci_process_frame(s);
1133 uhci_async_validate_end(s);
1135 qemu_mod_timer(s->frame_timer, s->expire_time);
1138 static const MemoryRegionPortio uhci_portio[] = {
1139 { 0, 32, 2, .write = uhci_ioport_writew, },
1140 { 0, 32, 2, .read = uhci_ioport_readw, },
1141 { 0, 32, 4, .write = uhci_ioport_writel, },
1142 { 0, 32, 4, .read = uhci_ioport_readl, },
1143 { 0, 32, 1, .write = uhci_ioport_writeb, },
1144 { 0, 32, 1, .read = uhci_ioport_readb, },
1145 PORTIO_END_OF_LIST()
1148 static const MemoryRegionOps uhci_ioport_ops = {
1149 .old_portio = uhci_portio,
1152 static USBPortOps uhci_port_ops = {
1153 .attach = uhci_attach,
1154 .detach = uhci_detach,
1155 .child_detach = uhci_child_detach,
1156 .wakeup = uhci_wakeup,
1157 .complete = uhci_async_complete,
1160 static USBBusOps uhci_bus_ops = {
1163 static int usb_uhci_common_initfn(PCIDevice *dev)
1165 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1166 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1167 uint8_t *pci_conf = s->dev.config;
1170 pci_conf[PCI_CLASS_PROG] = 0x00;
1171 /* TODO: reset value should be 0. */
1172 pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
1174 switch (pc->device_id) {
1175 case PCI_DEVICE_ID_INTEL_82801I_UHCI1:
1176 s->irq_pin = 0; /* A */
1178 case PCI_DEVICE_ID_INTEL_82801I_UHCI2:
1179 s->irq_pin = 1; /* B */
1181 case PCI_DEVICE_ID_INTEL_82801I_UHCI3:
1182 s->irq_pin = 2; /* C */
1185 s->irq_pin = 3; /* D */
1188 pci_config_set_interrupt_pin(pci_conf, s->irq_pin + 1);
1191 USBPort *ports[NB_PORTS];
1192 for(i = 0; i < NB_PORTS; i++) {
1193 ports[i] = &s->ports[i].port;
1195 if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1196 s->firstport, s, &uhci_port_ops,
1197 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1201 usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1202 for (i = 0; i < NB_PORTS; i++) {
1203 usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1204 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1207 s->bh = qemu_bh_new(uhci_bh, s);
1208 s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
1209 s->num_ports_vmstate = NB_PORTS;
1210 QTAILQ_INIT(&s->queues);
1212 qemu_register_reset(uhci_reset, s);
1214 memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
1215 /* Use region 4 for consistency with real hardware. BSD guests seem
1217 pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1222 static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1224 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1225 uint8_t *pci_conf = s->dev.config;
1227 /* USB misc control 1/2 */
1228 pci_set_long(pci_conf + 0x40,0x00001000);
1230 pci_set_long(pci_conf + 0x80,0x00020001);
1231 /* USB legacy support */
1232 pci_set_long(pci_conf + 0xc0,0x00002000);
1234 return usb_uhci_common_initfn(dev);
1237 static void usb_uhci_exit(PCIDevice *dev)
1239 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1241 memory_region_destroy(&s->io_bar);
1244 static Property uhci_properties[] = {
1245 DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1246 DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
1247 DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
1248 DEFINE_PROP_END_OF_LIST(),
1251 static void piix3_uhci_class_init(ObjectClass *klass, void *data)
1253 DeviceClass *dc = DEVICE_CLASS(klass);
1254 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1256 k->init = usb_uhci_common_initfn;
1257 k->exit = usb_uhci_exit;
1258 k->vendor_id = PCI_VENDOR_ID_INTEL;
1259 k->device_id = PCI_DEVICE_ID_INTEL_82371SB_2;
1261 k->class_id = PCI_CLASS_SERIAL_USB;
1262 dc->vmsd = &vmstate_uhci;
1263 dc->props = uhci_properties;
1266 static TypeInfo piix3_uhci_info = {
1267 .name = "piix3-usb-uhci",
1268 .parent = TYPE_PCI_DEVICE,
1269 .instance_size = sizeof(UHCIState),
1270 .class_init = piix3_uhci_class_init,
1273 static void piix4_uhci_class_init(ObjectClass *klass, void *data)
1275 DeviceClass *dc = DEVICE_CLASS(klass);
1276 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1278 k->init = usb_uhci_common_initfn;
1279 k->exit = usb_uhci_exit;
1280 k->vendor_id = PCI_VENDOR_ID_INTEL;
1281 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_2;
1283 k->class_id = PCI_CLASS_SERIAL_USB;
1284 dc->vmsd = &vmstate_uhci;
1285 dc->props = uhci_properties;
1288 static TypeInfo piix4_uhci_info = {
1289 .name = "piix4-usb-uhci",
1290 .parent = TYPE_PCI_DEVICE,
1291 .instance_size = sizeof(UHCIState),
1292 .class_init = piix4_uhci_class_init,
1295 static void vt82c686b_uhci_class_init(ObjectClass *klass, void *data)
1297 DeviceClass *dc = DEVICE_CLASS(klass);
1298 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1300 k->init = usb_uhci_vt82c686b_initfn;
1301 k->exit = usb_uhci_exit;
1302 k->vendor_id = PCI_VENDOR_ID_VIA;
1303 k->device_id = PCI_DEVICE_ID_VIA_UHCI;
1305 k->class_id = PCI_CLASS_SERIAL_USB;
1306 dc->vmsd = &vmstate_uhci;
1307 dc->props = uhci_properties;
1310 static TypeInfo vt82c686b_uhci_info = {
1311 .name = "vt82c686b-usb-uhci",
1312 .parent = TYPE_PCI_DEVICE,
1313 .instance_size = sizeof(UHCIState),
1314 .class_init = vt82c686b_uhci_class_init,
1317 static void ich9_uhci1_class_init(ObjectClass *klass, void *data)
1319 DeviceClass *dc = DEVICE_CLASS(klass);
1320 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1322 k->init = usb_uhci_common_initfn;
1323 k->vendor_id = PCI_VENDOR_ID_INTEL;
1324 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1;
1326 k->class_id = PCI_CLASS_SERIAL_USB;
1327 dc->vmsd = &vmstate_uhci;
1328 dc->props = uhci_properties;
1331 static TypeInfo ich9_uhci1_info = {
1332 .name = "ich9-usb-uhci1",
1333 .parent = TYPE_PCI_DEVICE,
1334 .instance_size = sizeof(UHCIState),
1335 .class_init = ich9_uhci1_class_init,
1338 static void ich9_uhci2_class_init(ObjectClass *klass, void *data)
1340 DeviceClass *dc = DEVICE_CLASS(klass);
1341 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1343 k->init = usb_uhci_common_initfn;
1344 k->vendor_id = PCI_VENDOR_ID_INTEL;
1345 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2;
1347 k->class_id = PCI_CLASS_SERIAL_USB;
1348 dc->vmsd = &vmstate_uhci;
1349 dc->props = uhci_properties;
1352 static TypeInfo ich9_uhci2_info = {
1353 .name = "ich9-usb-uhci2",
1354 .parent = TYPE_PCI_DEVICE,
1355 .instance_size = sizeof(UHCIState),
1356 .class_init = ich9_uhci2_class_init,
1359 static void ich9_uhci3_class_init(ObjectClass *klass, void *data)
1361 DeviceClass *dc = DEVICE_CLASS(klass);
1362 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1364 k->init = usb_uhci_common_initfn;
1365 k->vendor_id = PCI_VENDOR_ID_INTEL;
1366 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3;
1368 k->class_id = PCI_CLASS_SERIAL_USB;
1369 dc->vmsd = &vmstate_uhci;
1370 dc->props = uhci_properties;
1373 static TypeInfo ich9_uhci3_info = {
1374 .name = "ich9-usb-uhci3",
1375 .parent = TYPE_PCI_DEVICE,
1376 .instance_size = sizeof(UHCIState),
1377 .class_init = ich9_uhci3_class_init,
1380 static void uhci_register_types(void)
1382 type_register_static(&piix3_uhci_info);
1383 type_register_static(&piix4_uhci_info);
1384 type_register_static(&vt82c686b_uhci_info);
1385 type_register_static(&ich9_uhci1_info);
1386 type_register_static(&ich9_uhci2_info);
1387 type_register_static(&ich9_uhci3_info);
1390 type_init(uhci_register_types)