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"
36 //#define DEBUG_DUMP_DATA
38 #define UHCI_CMD_FGR (1 << 4)
39 #define UHCI_CMD_EGSM (1 << 3)
40 #define UHCI_CMD_GRESET (1 << 2)
41 #define UHCI_CMD_HCRESET (1 << 1)
42 #define UHCI_CMD_RS (1 << 0)
44 #define UHCI_STS_HCHALTED (1 << 5)
45 #define UHCI_STS_HCPERR (1 << 4)
46 #define UHCI_STS_HSERR (1 << 3)
47 #define UHCI_STS_RD (1 << 2)
48 #define UHCI_STS_USBERR (1 << 1)
49 #define UHCI_STS_USBINT (1 << 0)
51 #define TD_CTRL_SPD (1 << 29)
52 #define TD_CTRL_ERROR_SHIFT 27
53 #define TD_CTRL_IOS (1 << 25)
54 #define TD_CTRL_IOC (1 << 24)
55 #define TD_CTRL_ACTIVE (1 << 23)
56 #define TD_CTRL_STALL (1 << 22)
57 #define TD_CTRL_BABBLE (1 << 20)
58 #define TD_CTRL_NAK (1 << 19)
59 #define TD_CTRL_TIMEOUT (1 << 18)
61 #define UHCI_PORT_SUSPEND (1 << 12)
62 #define UHCI_PORT_RESET (1 << 9)
63 #define UHCI_PORT_LSDA (1 << 8)
64 #define UHCI_PORT_RD (1 << 6)
65 #define UHCI_PORT_ENC (1 << 3)
66 #define UHCI_PORT_EN (1 << 2)
67 #define UHCI_PORT_CSC (1 << 1)
68 #define UHCI_PORT_CCS (1 << 0)
70 #define UHCI_PORT_READ_ONLY (0x1bb)
71 #define UHCI_PORT_WRITE_CLEAR (UHCI_PORT_CSC | UHCI_PORT_ENC)
73 #define FRAME_TIMER_FREQ 1000
75 #define FRAME_MAX_LOOPS 256
80 #define DPRINTF printf
82 static const char *pid2str(int pid)
85 case USB_TOKEN_SETUP: return "SETUP";
86 case USB_TOKEN_IN: return "IN";
87 case USB_TOKEN_OUT: return "OUT";
96 typedef struct UHCIState UHCIState;
97 typedef struct UHCIAsync UHCIAsync;
98 typedef struct UHCIQueue UHCIQueue;
101 * Pending async transaction.
102 * 'packet' must be the first field because completion
103 * handler does "(UHCIAsync *) pkt" cast.
110 QTAILQ_ENTRY(UHCIAsync) next;
119 QTAILQ_ENTRY(UHCIQueue) next;
120 QTAILQ_HEAD(, UHCIAsync) asyncs;
124 typedef struct UHCIPort {
132 USBBus bus; /* Note unused when we're a companion controller */
133 uint16_t cmd; /* cmd register */
135 uint16_t intr; /* interrupt enable register */
136 uint16_t frnum; /* frame number */
137 uint32_t fl_base_addr; /* frame list base address */
139 uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
141 QEMUTimer *frame_timer;
142 UHCIPort ports[NB_PORTS];
144 /* Interrupts that should be raised at the end of the current frame. */
145 uint32_t pending_int_mask;
148 QTAILQ_HEAD(, UHCIQueue) queues;
149 uint8_t num_ports_vmstate;
156 typedef struct UHCI_TD {
158 uint32_t ctrl; /* see TD_CTRL_xxx */
163 typedef struct UHCI_QH {
168 static inline int32_t uhci_queue_token(UHCI_TD *td)
170 /* covers ep, dev, pid -> identifies the endpoint */
171 return td->token & 0x7ffff;
174 static UHCIQueue *uhci_queue_get(UHCIState *s, UHCI_TD *td)
176 uint32_t token = uhci_queue_token(td);
179 QTAILQ_FOREACH(queue, &s->queues, next) {
180 if (queue->token == token) {
185 queue = g_new0(UHCIQueue, 1);
187 queue->token = token;
188 QTAILQ_INIT(&queue->asyncs);
189 QTAILQ_INSERT_HEAD(&s->queues, queue, next);
193 static void uhci_queue_free(UHCIQueue *queue)
195 UHCIState *s = queue->uhci;
197 QTAILQ_REMOVE(&s->queues, queue, next);
201 static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t addr)
203 UHCIAsync *async = g_new0(UHCIAsync, 1);
205 async->queue = queue;
207 usb_packet_init(&async->packet);
208 pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
213 static void uhci_async_free(UHCIAsync *async)
215 usb_packet_cleanup(&async->packet);
216 qemu_sglist_destroy(&async->sgl);
220 static void uhci_async_link(UHCIAsync *async)
222 UHCIQueue *queue = async->queue;
223 QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
226 static void uhci_async_unlink(UHCIAsync *async)
228 UHCIQueue *queue = async->queue;
229 QTAILQ_REMOVE(&queue->asyncs, async, next);
232 static void uhci_async_cancel(UHCIAsync *async)
234 DPRINTF("uhci: cancel td 0x%x token 0x%x done %u\n",
235 async->td, async->token, async->done);
238 usb_cancel_packet(&async->packet);
239 uhci_async_free(async);
243 * Mark all outstanding async packets as invalid.
244 * This is used for canceling them when TDs are removed by the HCD.
246 static void uhci_async_validate_begin(UHCIState *s)
250 QTAILQ_FOREACH(queue, &s->queues, next) {
256 * Cancel async packets that are no longer valid
258 static void uhci_async_validate_end(UHCIState *s)
260 UHCIQueue *queue, *n;
263 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
264 if (queue->valid > 0) {
267 while (!QTAILQ_EMPTY(&queue->asyncs)) {
268 async = QTAILQ_FIRST(&queue->asyncs);
269 uhci_async_unlink(async);
270 uhci_async_cancel(async);
272 uhci_queue_free(queue);
276 static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
281 QTAILQ_FOREACH(queue, &s->queues, next) {
282 QTAILQ_FOREACH_SAFE(curr, &queue->asyncs, next, n) {
283 if (!usb_packet_is_inflight(&curr->packet) ||
284 curr->packet.ep->dev != dev) {
287 uhci_async_unlink(curr);
288 uhci_async_cancel(curr);
293 static void uhci_async_cancel_all(UHCIState *s)
298 QTAILQ_FOREACH(queue, &s->queues, next) {
299 QTAILQ_FOREACH_SAFE(curr, &queue->asyncs, next, n) {
300 uhci_async_unlink(curr);
301 uhci_async_cancel(curr);
303 uhci_queue_free(queue);
307 static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t addr, UHCI_TD *td)
309 uint32_t token = uhci_queue_token(td);
313 QTAILQ_FOREACH(queue, &s->queues, next) {
314 if (queue->token == token) {
322 QTAILQ_FOREACH(async, &queue->asyncs, next) {
323 if (async->td == addr) {
331 static void uhci_update_irq(UHCIState *s)
334 if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
335 ((s->status2 & 2) && (s->intr & (1 << 3))) ||
336 ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
337 ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
338 (s->status & UHCI_STS_HSERR) ||
339 (s->status & UHCI_STS_HCPERR)) {
344 qemu_set_irq(s->dev.irq[3], level);
347 static void uhci_reset(void *opaque)
349 UHCIState *s = opaque;
354 DPRINTF("uhci: full reset\n");
356 pci_conf = s->dev.config;
358 pci_conf[0x6a] = 0x01; /* usb clock */
359 pci_conf[0x6b] = 0x00;
367 for(i = 0; i < NB_PORTS; i++) {
370 if (port->port.dev && port->port.dev->attached) {
371 usb_port_reset(&port->port);
375 uhci_async_cancel_all(s);
378 static void uhci_pre_save(void *opaque)
380 UHCIState *s = opaque;
382 uhci_async_cancel_all(s);
385 static const VMStateDescription vmstate_uhci_port = {
388 .minimum_version_id = 1,
389 .minimum_version_id_old = 1,
390 .fields = (VMStateField []) {
391 VMSTATE_UINT16(ctrl, UHCIPort),
392 VMSTATE_END_OF_LIST()
396 static const VMStateDescription vmstate_uhci = {
399 .minimum_version_id = 1,
400 .minimum_version_id_old = 1,
401 .pre_save = uhci_pre_save,
402 .fields = (VMStateField []) {
403 VMSTATE_PCI_DEVICE(dev, UHCIState),
404 VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
405 VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
406 vmstate_uhci_port, UHCIPort),
407 VMSTATE_UINT16(cmd, UHCIState),
408 VMSTATE_UINT16(status, UHCIState),
409 VMSTATE_UINT16(intr, UHCIState),
410 VMSTATE_UINT16(frnum, UHCIState),
411 VMSTATE_UINT32(fl_base_addr, UHCIState),
412 VMSTATE_UINT8(sof_timing, UHCIState),
413 VMSTATE_UINT8(status2, UHCIState),
414 VMSTATE_TIMER(frame_timer, UHCIState),
415 VMSTATE_INT64_V(expire_time, UHCIState, 2),
416 VMSTATE_END_OF_LIST()
420 static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
422 UHCIState *s = opaque;
432 static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
434 UHCIState *s = opaque;
449 static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
451 UHCIState *s = opaque;
454 DPRINTF("uhci: writew port=0x%04x val=0x%04x\n", addr, val);
458 if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
459 /* start frame processing */
460 s->expire_time = qemu_get_clock_ns(vm_clock) +
461 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
462 qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
463 s->status &= ~UHCI_STS_HCHALTED;
464 } else if (!(val & UHCI_CMD_RS)) {
465 s->status |= UHCI_STS_HCHALTED;
467 if (val & UHCI_CMD_GRESET) {
471 /* send reset on the USB bus */
472 for(i = 0; i < NB_PORTS; i++) {
474 usb_device_reset(port->port.dev);
479 if (val & UHCI_CMD_HCRESET) {
487 /* XXX: the chip spec is not coherent, so we add a hidden
488 register to distinguish between IOC and SPD */
489 if (val & UHCI_STS_USBINT)
498 if (s->status & UHCI_STS_HCHALTED)
499 s->frnum = val & 0x7ff;
511 dev = port->port.dev;
512 if (dev && dev->attached) {
514 if ( (val & UHCI_PORT_RESET) &&
515 !(port->ctrl & UHCI_PORT_RESET) ) {
516 usb_device_reset(dev);
519 port->ctrl &= UHCI_PORT_READ_ONLY;
520 port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
521 /* some bits are reset when a '1' is written to them */
522 port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
528 static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
530 UHCIState *s = opaque;
560 val = 0xff7f; /* disabled port */
564 DPRINTF("uhci: readw port=0x%04x val=0x%04x\n", addr, val);
569 static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
571 UHCIState *s = opaque;
574 DPRINTF("uhci: writel port=0x%04x val=0x%08x\n", addr, val);
578 s->fl_base_addr = val & ~0xfff;
583 static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
585 UHCIState *s = opaque;
591 val = s->fl_base_addr;
600 /* signal resume if controller suspended */
601 static void uhci_resume (void *opaque)
603 UHCIState *s = (UHCIState *)opaque;
608 if (s->cmd & UHCI_CMD_EGSM) {
609 s->cmd |= UHCI_CMD_FGR;
610 s->status |= UHCI_STS_RD;
615 static void uhci_attach(USBPort *port1)
617 UHCIState *s = port1->opaque;
618 UHCIPort *port = &s->ports[port1->index];
620 /* set connect status */
621 port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
624 if (port->port.dev->speed == USB_SPEED_LOW) {
625 port->ctrl |= UHCI_PORT_LSDA;
627 port->ctrl &= ~UHCI_PORT_LSDA;
633 static void uhci_detach(USBPort *port1)
635 UHCIState *s = port1->opaque;
636 UHCIPort *port = &s->ports[port1->index];
638 uhci_async_cancel_device(s, port1->dev);
640 /* set connect status */
641 if (port->ctrl & UHCI_PORT_CCS) {
642 port->ctrl &= ~UHCI_PORT_CCS;
643 port->ctrl |= UHCI_PORT_CSC;
646 if (port->ctrl & UHCI_PORT_EN) {
647 port->ctrl &= ~UHCI_PORT_EN;
648 port->ctrl |= UHCI_PORT_ENC;
654 static void uhci_child_detach(USBPort *port1, USBDevice *child)
656 UHCIState *s = port1->opaque;
658 uhci_async_cancel_device(s, child);
661 static void uhci_wakeup(USBPort *port1)
663 UHCIState *s = port1->opaque;
664 UHCIPort *port = &s->ports[port1->index];
666 if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
667 port->ctrl |= UHCI_PORT_RD;
672 static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
677 for (i = 0; i < NB_PORTS; i++) {
678 UHCIPort *port = &s->ports[i];
679 if (!(port->ctrl & UHCI_PORT_EN)) {
682 dev = usb_find_device(&port->port, addr);
690 static void uhci_async_complete(USBPort *port, USBPacket *packet);
691 static void uhci_process_frame(UHCIState *s);
693 /* return -1 if fatal error (frame must be stopped)
695 1 if TD unsuccessful or inactive
697 static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
699 int len = 0, max_len, err, ret;
702 max_len = ((td->token >> 21) + 1) & 0x7ff;
703 pid = td->token & 0xff;
705 ret = async->packet.result;
707 if (td->ctrl & TD_CTRL_IOS)
708 td->ctrl &= ~TD_CTRL_ACTIVE;
713 len = async->packet.result;
714 td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
716 /* The NAK bit may have been set by a previous frame, so clear it
717 here. The docs are somewhat unclear, but win2k relies on this
719 td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
720 if (td->ctrl & TD_CTRL_IOC)
723 if (pid == USB_TOKEN_IN) {
725 ret = USB_RET_BABBLE;
729 if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
731 /* short packet: do not update QH */
732 DPRINTF("uhci: short packet. td 0x%x token 0x%x\n", async->td, async->token);
743 td->ctrl |= TD_CTRL_STALL;
744 td->ctrl &= ~TD_CTRL_ACTIVE;
745 s->status |= UHCI_STS_USBERR;
746 if (td->ctrl & TD_CTRL_IOC) {
753 td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
754 td->ctrl &= ~TD_CTRL_ACTIVE;
755 s->status |= UHCI_STS_USBERR;
756 if (td->ctrl & TD_CTRL_IOC) {
760 /* frame interrupted */
764 td->ctrl |= TD_CTRL_NAK;
765 if (pid == USB_TOKEN_SETUP)
769 case USB_RET_IOERROR:
775 /* Retry the TD if error count is not zero */
777 td->ctrl |= TD_CTRL_TIMEOUT;
778 err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
782 td->ctrl &= ~TD_CTRL_ACTIVE;
783 s->status |= UHCI_STS_USBERR;
784 if (td->ctrl & TD_CTRL_IOC)
789 td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
790 (err << TD_CTRL_ERROR_SHIFT);
794 static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *int_mask)
797 int len = 0, max_len;
803 if (!(td->ctrl & TD_CTRL_ACTIVE))
806 async = uhci_async_find_td(s, addr, td);
808 /* Already submitted */
809 async->queue->valid = 32;
814 uhci_async_unlink(async);
818 /* Allocate new packet */
819 async = uhci_async_alloc(uhci_queue_get(s, td), addr);
823 /* valid needs to be large enough to handle 10 frame delay
824 * for initial isochronous requests
826 async->queue->valid = 32;
827 async->isoc = td->ctrl & TD_CTRL_IOS;
829 max_len = ((td->token >> 21) + 1) & 0x7ff;
830 pid = td->token & 0xff;
832 dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
833 ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
834 usb_packet_setup(&async->packet, pid, ep);
835 qemu_sglist_add(&async->sgl, td->buffer, max_len);
836 usb_packet_map(&async->packet, &async->sgl);
840 case USB_TOKEN_SETUP:
841 len = usb_handle_packet(dev, &async->packet);
847 len = usb_handle_packet(dev, &async->packet);
851 /* invalid pid : frame interrupted */
852 uhci_async_free(async);
853 s->status |= UHCI_STS_HCPERR;
858 if (len == USB_RET_ASYNC) {
859 uhci_async_link(async);
863 async->packet.result = len;
866 len = uhci_complete_td(s, td, async, int_mask);
867 usb_packet_unmap(&async->packet);
868 uhci_async_free(async);
872 static void uhci_async_complete(USBPort *port, USBPacket *packet)
874 UHCIAsync *async = container_of(packet, UHCIAsync, packet);
875 UHCIState *s = async->queue->uhci;
877 DPRINTF("uhci: async complete. td 0x%x token 0x%x\n", async->td, async->token);
881 uint32_t link = async->td;
882 uint32_t int_mask = 0, val;
884 pci_dma_read(&s->dev, link & ~0xf, &td, sizeof(td));
885 le32_to_cpus(&td.link);
886 le32_to_cpus(&td.ctrl);
887 le32_to_cpus(&td.token);
888 le32_to_cpus(&td.buffer);
890 uhci_async_unlink(async);
891 uhci_complete_td(s, &td, async, &int_mask);
892 s->pending_int_mask |= int_mask;
894 /* update the status bits of the TD */
895 val = cpu_to_le32(td.ctrl);
896 pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
897 uhci_async_free(async);
900 uhci_process_frame(s);
904 static int is_valid(uint32_t link)
906 return (link & 1) == 0;
909 static int is_qh(uint32_t link)
911 return (link & 2) != 0;
914 static int depth_first(uint32_t link)
916 return (link & 4) != 0;
919 /* QH DB used for detecting QH loops */
920 #define UHCI_MAX_QUEUES 128
922 uint32_t addr[UHCI_MAX_QUEUES];
926 static void qhdb_reset(QhDb *db)
931 /* Add QH to DB. Returns 1 if already present or DB is full. */
932 static int qhdb_insert(QhDb *db, uint32_t addr)
935 for (i = 0; i < db->count; i++)
936 if (db->addr[i] == addr)
939 if (db->count >= UHCI_MAX_QUEUES)
942 db->addr[db->count++] = addr;
946 static void uhci_fill_queue(UHCIState *s, UHCI_TD *td)
948 uint32_t int_mask = 0;
949 uint32_t plink = td->link;
950 uint32_t token = uhci_queue_token(td);
954 while (is_valid(plink)) {
955 pci_dma_read(&s->dev, plink & ~0xf, &ptd, sizeof(ptd));
956 le32_to_cpus(&ptd.link);
957 le32_to_cpus(&ptd.ctrl);
958 le32_to_cpus(&ptd.token);
959 le32_to_cpus(&ptd.buffer);
960 if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
963 if (uhci_queue_token(&ptd) != token) {
966 ret = uhci_handle_td(s, plink, &ptd, &int_mask);
967 assert(ret == 2); /* got USB_RET_ASYNC */
968 assert(int_mask == 0);
973 static void uhci_process_frame(UHCIState *s)
975 uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
976 uint32_t curr_qh, td_count = 0, bytes_count = 0;
982 frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
984 DPRINTF("uhci: processing frame %d addr 0x%x\n" , s->frnum, frame_addr);
986 pci_dma_read(&s->dev, frame_addr, &link, 4);
994 for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
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
1004 * (a) no transaction has been done since we've been
1005 * here last time, or
1006 * (b) we've reached the usb 1.1 bandwidth, which is
1009 DPRINTF("uhci: detected loop. qh 0x%x\n", link);
1010 if (td_count == 0) {
1011 DPRINTF("uhci: no transaction last round, stop\n");
1013 } else if (bytes_count >= 1280) {
1014 DPRINTF("uhci: bandwidth limit reached, stop\n");
1019 qhdb_insert(&qhdb, link);
1023 pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
1024 le32_to_cpus(&qh.link);
1025 le32_to_cpus(&qh.el_link);
1027 DPRINTF("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
1028 link, qh.link, qh.el_link);
1030 if (!is_valid(qh.el_link)) {
1031 /* QH w/o elements */
1035 /* QH with elements */
1043 pci_dma_read(&s->dev, link & ~0xf, &td, sizeof(td));
1044 le32_to_cpus(&td.link);
1045 le32_to_cpus(&td.ctrl);
1046 le32_to_cpus(&td.token);
1047 le32_to_cpus(&td.buffer);
1049 DPRINTF("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1050 link, td.link, td.ctrl, td.token, curr_qh);
1052 old_td_ctrl = td.ctrl;
1053 ret = uhci_handle_td(s, link, &td, &int_mask);
1054 if (old_td_ctrl != td.ctrl) {
1055 /* update the status bits of the TD */
1056 val = cpu_to_le32(td.ctrl);
1057 pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
1061 case -1: /* interrupted frame */
1064 case 1: /* goto next queue */
1065 DPRINTF("uhci: TD 0x%x skip. "
1066 "link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1067 link, td.link, td.ctrl, td.token, curr_qh);
1068 link = curr_qh ? qh.link : td.link;
1071 case 2: /* got USB_RET_ASYNC */
1072 DPRINTF("uhci: TD 0x%x async. "
1073 "link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1074 link, td.link, td.ctrl, td.token, curr_qh);
1075 if (is_valid(td.link)) {
1076 uhci_fill_queue(s, &td);
1078 link = curr_qh ? qh.link : td.link;
1081 case 0: /* completed TD */
1082 DPRINTF("uhci: TD 0x%x done. "
1083 "link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1084 link, td.link, td.ctrl, td.token, curr_qh);
1088 bytes_count += (td.ctrl & 0x7ff) + 1;
1091 /* update QH element link */
1093 val = cpu_to_le32(qh.el_link);
1094 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
1096 if (!depth_first(link)) {
1097 /* done with this QH */
1099 DPRINTF("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
1100 curr_qh, qh.link, qh.el_link);
1109 assert(!"unknown return code");
1112 /* go to the next entry */
1116 s->pending_int_mask |= int_mask;
1119 static void uhci_frame_timer(void *opaque)
1121 UHCIState *s = opaque;
1123 /* prepare the timer for the next frame */
1124 s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
1126 if (!(s->cmd & UHCI_CMD_RS)) {
1128 qemu_del_timer(s->frame_timer);
1129 uhci_async_cancel_all(s);
1130 /* set hchalted bit in status - UHCI11D 2.1.2 */
1131 s->status |= UHCI_STS_HCHALTED;
1133 DPRINTF("uhci: halted\n");
1137 /* Complete the previous frame */
1138 if (s->pending_int_mask) {
1139 s->status2 |= s->pending_int_mask;
1140 s->status |= UHCI_STS_USBINT;
1143 s->pending_int_mask = 0;
1145 /* Start new frame */
1146 s->frnum = (s->frnum + 1) & 0x7ff;
1148 DPRINTF("uhci: new frame #%u\n" , s->frnum);
1150 uhci_async_validate_begin(s);
1152 uhci_process_frame(s);
1154 uhci_async_validate_end(s);
1156 qemu_mod_timer(s->frame_timer, s->expire_time);
1159 static const MemoryRegionPortio uhci_portio[] = {
1160 { 0, 32, 2, .write = uhci_ioport_writew, },
1161 { 0, 32, 2, .read = uhci_ioport_readw, },
1162 { 0, 32, 4, .write = uhci_ioport_writel, },
1163 { 0, 32, 4, .read = uhci_ioport_readl, },
1164 { 0, 32, 1, .write = uhci_ioport_writeb, },
1165 { 0, 32, 1, .read = uhci_ioport_readb, },
1166 PORTIO_END_OF_LIST()
1169 static const MemoryRegionOps uhci_ioport_ops = {
1170 .old_portio = uhci_portio,
1173 static USBPortOps uhci_port_ops = {
1174 .attach = uhci_attach,
1175 .detach = uhci_detach,
1176 .child_detach = uhci_child_detach,
1177 .wakeup = uhci_wakeup,
1178 .complete = uhci_async_complete,
1181 static USBBusOps uhci_bus_ops = {
1184 static int usb_uhci_common_initfn(PCIDevice *dev)
1186 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1187 uint8_t *pci_conf = s->dev.config;
1190 pci_conf[PCI_CLASS_PROG] = 0x00;
1191 /* TODO: reset value should be 0. */
1192 pci_conf[PCI_INTERRUPT_PIN] = 4; /* interrupt pin D */
1193 pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
1196 USBPort *ports[NB_PORTS];
1197 for(i = 0; i < NB_PORTS; i++) {
1198 ports[i] = &s->ports[i].port;
1200 if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1201 s->firstport, s, &uhci_port_ops,
1202 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1206 usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1207 for (i = 0; i < NB_PORTS; i++) {
1208 usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1209 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1212 s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
1213 s->num_ports_vmstate = NB_PORTS;
1214 QTAILQ_INIT(&s->queues);
1216 qemu_register_reset(uhci_reset, s);
1218 memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
1219 /* Use region 4 for consistency with real hardware. BSD guests seem
1221 pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1226 static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1228 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1229 uint8_t *pci_conf = s->dev.config;
1231 /* USB misc control 1/2 */
1232 pci_set_long(pci_conf + 0x40,0x00001000);
1234 pci_set_long(pci_conf + 0x80,0x00020001);
1235 /* USB legacy support */
1236 pci_set_long(pci_conf + 0xc0,0x00002000);
1238 return usb_uhci_common_initfn(dev);
1241 static int usb_uhci_exit(PCIDevice *dev)
1243 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1245 memory_region_destroy(&s->io_bar);
1249 static Property uhci_properties[] = {
1250 DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1251 DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
1252 DEFINE_PROP_END_OF_LIST(),
1255 static void piix3_uhci_class_init(ObjectClass *klass, void *data)
1257 DeviceClass *dc = DEVICE_CLASS(klass);
1258 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1260 k->init = usb_uhci_common_initfn;
1261 k->exit = usb_uhci_exit;
1262 k->vendor_id = PCI_VENDOR_ID_INTEL;
1263 k->device_id = PCI_DEVICE_ID_INTEL_82371SB_2;
1265 k->class_id = PCI_CLASS_SERIAL_USB;
1266 dc->vmsd = &vmstate_uhci;
1267 dc->props = uhci_properties;
1270 static TypeInfo piix3_uhci_info = {
1271 .name = "piix3-usb-uhci",
1272 .parent = TYPE_PCI_DEVICE,
1273 .instance_size = sizeof(UHCIState),
1274 .class_init = piix3_uhci_class_init,
1277 static void piix4_uhci_class_init(ObjectClass *klass, void *data)
1279 DeviceClass *dc = DEVICE_CLASS(klass);
1280 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1282 k->init = usb_uhci_common_initfn;
1283 k->exit = usb_uhci_exit;
1284 k->vendor_id = PCI_VENDOR_ID_INTEL;
1285 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_2;
1287 k->class_id = PCI_CLASS_SERIAL_USB;
1288 dc->vmsd = &vmstate_uhci;
1289 dc->props = uhci_properties;
1292 static TypeInfo piix4_uhci_info = {
1293 .name = "piix4-usb-uhci",
1294 .parent = TYPE_PCI_DEVICE,
1295 .instance_size = sizeof(UHCIState),
1296 .class_init = piix4_uhci_class_init,
1299 static void vt82c686b_uhci_class_init(ObjectClass *klass, void *data)
1301 DeviceClass *dc = DEVICE_CLASS(klass);
1302 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1304 k->init = usb_uhci_vt82c686b_initfn;
1305 k->exit = usb_uhci_exit;
1306 k->vendor_id = PCI_VENDOR_ID_VIA;
1307 k->device_id = PCI_DEVICE_ID_VIA_UHCI;
1309 k->class_id = PCI_CLASS_SERIAL_USB;
1310 dc->vmsd = &vmstate_uhci;
1311 dc->props = uhci_properties;
1314 static TypeInfo vt82c686b_uhci_info = {
1315 .name = "vt82c686b-usb-uhci",
1316 .parent = TYPE_PCI_DEVICE,
1317 .instance_size = sizeof(UHCIState),
1318 .class_init = vt82c686b_uhci_class_init,
1321 static void ich9_uhci1_class_init(ObjectClass *klass, void *data)
1323 DeviceClass *dc = DEVICE_CLASS(klass);
1324 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1326 k->init = usb_uhci_common_initfn;
1327 k->vendor_id = PCI_VENDOR_ID_INTEL;
1328 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1;
1330 k->class_id = PCI_CLASS_SERIAL_USB;
1331 dc->vmsd = &vmstate_uhci;
1332 dc->props = uhci_properties;
1335 static TypeInfo ich9_uhci1_info = {
1336 .name = "ich9-usb-uhci1",
1337 .parent = TYPE_PCI_DEVICE,
1338 .instance_size = sizeof(UHCIState),
1339 .class_init = ich9_uhci1_class_init,
1342 static void ich9_uhci2_class_init(ObjectClass *klass, void *data)
1344 DeviceClass *dc = DEVICE_CLASS(klass);
1345 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1347 k->init = usb_uhci_common_initfn;
1348 k->vendor_id = PCI_VENDOR_ID_INTEL;
1349 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2;
1351 k->class_id = PCI_CLASS_SERIAL_USB;
1352 dc->vmsd = &vmstate_uhci;
1353 dc->props = uhci_properties;
1356 static TypeInfo ich9_uhci2_info = {
1357 .name = "ich9-usb-uhci2",
1358 .parent = TYPE_PCI_DEVICE,
1359 .instance_size = sizeof(UHCIState),
1360 .class_init = ich9_uhci2_class_init,
1363 static void ich9_uhci3_class_init(ObjectClass *klass, void *data)
1365 DeviceClass *dc = DEVICE_CLASS(klass);
1366 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1368 k->init = usb_uhci_common_initfn;
1369 k->vendor_id = PCI_VENDOR_ID_INTEL;
1370 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3;
1372 k->class_id = PCI_CLASS_SERIAL_USB;
1373 dc->vmsd = &vmstate_uhci;
1374 dc->props = uhci_properties;
1377 static TypeInfo ich9_uhci3_info = {
1378 .name = "ich9-usb-uhci3",
1379 .parent = TYPE_PCI_DEVICE,
1380 .instance_size = sizeof(UHCIState),
1381 .class_init = ich9_uhci3_class_init,
1384 static void uhci_register_types(void)
1386 type_register_static(&piix3_uhci_info);
1387 type_register_static(&piix4_uhci_info);
1388 type_register_static(&vt82c686b_uhci_info);
1389 type_register_static(&ich9_uhci1_info);
1390 type_register_static(&ich9_uhci2_info);
1391 type_register_static(&ich9_uhci3_info);
1394 type_init(uhci_register_types)