2 * USB UHCI controller emulation
4 * Copyright (c) 2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 //#define DEBUG_PACKET
30 #define UHCI_CMD_FGR (1 << 4)
31 #define UHCI_CMD_EGSM (1 << 3)
32 #define UHCI_CMD_GRESET (1 << 2)
33 #define UHCI_CMD_HCRESET (1 << 1)
34 #define UHCI_CMD_RS (1 << 0)
36 #define UHCI_STS_HCHALTED (1 << 5)
37 #define UHCI_STS_HCPERR (1 << 4)
38 #define UHCI_STS_HSERR (1 << 3)
39 #define UHCI_STS_RD (1 << 2)
40 #define UHCI_STS_USBERR (1 << 1)
41 #define UHCI_STS_USBINT (1 << 0)
43 #define TD_CTRL_SPD (1 << 29)
44 #define TD_CTRL_ERROR_SHIFT 27
45 #define TD_CTRL_IOS (1 << 25)
46 #define TD_CTRL_IOC (1 << 24)
47 #define TD_CTRL_ACTIVE (1 << 23)
48 #define TD_CTRL_STALL (1 << 22)
49 #define TD_CTRL_BABBLE (1 << 20)
50 #define TD_CTRL_NAK (1 << 19)
51 #define TD_CTRL_TIMEOUT (1 << 18)
53 #define UHCI_PORT_RESET (1 << 9)
54 #define UHCI_PORT_LSDA (1 << 8)
55 #define UHCI_PORT_ENC (1 << 3)
56 #define UHCI_PORT_EN (1 << 2)
57 #define UHCI_PORT_CSC (1 << 1)
58 #define UHCI_PORT_CCS (1 << 0)
60 #define FRAME_TIMER_FREQ 1000
62 #define FRAME_MAX_LOOPS 100
66 typedef struct UHCIPort {
71 typedef struct UHCIState {
73 uint16_t cmd; /* cmd register */
75 uint16_t intr; /* interrupt enable register */
76 uint16_t frnum; /* frame number */
77 uint32_t fl_base_addr; /* frame list base address */
79 uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
80 QEMUTimer *frame_timer;
81 UHCIPort ports[NB_PORTS];
83 /* Interrupts that should be raised at the end of the current frame. */
84 uint32_t pending_int_mask;
85 /* For simplicity of implementation we only allow a single pending USB
86 request. This means all usb traffic on this controller is effectively
87 suspended until that transfer completes. When the transfer completes
88 the next transfer from that queue will be processed. However
89 other queues will not be processed until the next frame. The solution
90 is to allow multiple pending requests. */
92 uint32_t async_frame_addr;
94 uint8_t usb_buf[2048];
97 typedef struct UHCI_TD {
99 uint32_t ctrl; /* see TD_CTRL_xxx */
104 typedef struct UHCI_QH {
109 static void uhci_attach(USBPort *port1, USBDevice *dev);
111 static void uhci_update_irq(UHCIState *s)
114 if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
115 ((s->status2 & 2) && (s->intr & (1 << 3))) ||
116 ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
117 ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
118 (s->status & UHCI_STS_HSERR) ||
119 (s->status & UHCI_STS_HCPERR)) {
124 qemu_set_irq(s->dev.irq[3], level);
127 static void uhci_reset(UHCIState *s)
133 pci_conf = s->dev.config;
135 pci_conf[0x6a] = 0x01; /* usb clock */
136 pci_conf[0x6b] = 0x00;
143 for(i = 0; i < NB_PORTS; i++) {
147 uhci_attach(&port->port, port->port.dev);
151 static void uhci_save(QEMUFile *f, void *opaque)
153 UHCIState *s = opaque;
154 uint8_t num_ports = NB_PORTS;
157 pci_device_save(&s->dev, f);
159 qemu_put_8s(f, &num_ports);
160 for (i = 0; i < num_ports; ++i)
161 qemu_put_be16s(f, &s->ports[i].ctrl);
162 qemu_put_be16s(f, &s->cmd);
163 qemu_put_be16s(f, &s->status);
164 qemu_put_be16s(f, &s->intr);
165 qemu_put_be16s(f, &s->frnum);
166 qemu_put_be32s(f, &s->fl_base_addr);
167 qemu_put_8s(f, &s->sof_timing);
168 qemu_put_8s(f, &s->status2);
169 qemu_put_timer(f, s->frame_timer);
172 static int uhci_load(QEMUFile *f, void *opaque, int version_id)
174 UHCIState *s = opaque;
181 ret = pci_device_load(&s->dev, f);
185 qemu_get_8s(f, &num_ports);
186 if (num_ports != NB_PORTS)
189 for (i = 0; i < num_ports; ++i)
190 qemu_get_be16s(f, &s->ports[i].ctrl);
191 qemu_get_be16s(f, &s->cmd);
192 qemu_get_be16s(f, &s->status);
193 qemu_get_be16s(f, &s->intr);
194 qemu_get_be16s(f, &s->frnum);
195 qemu_get_be32s(f, &s->fl_base_addr);
196 qemu_get_8s(f, &s->sof_timing);
197 qemu_get_8s(f, &s->status2);
198 qemu_get_timer(f, s->frame_timer);
203 static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
205 UHCIState *s = opaque;
215 static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
217 UHCIState *s = opaque;
232 static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
234 UHCIState *s = opaque;
238 printf("uhci writew port=0x%04x val=0x%04x\n", addr, val);
242 if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
243 /* start frame processing */
244 qemu_mod_timer(s->frame_timer, qemu_get_clock(vm_clock));
245 s->status &= ~UHCI_STS_HCHALTED;
246 } else if (!(val & UHCI_CMD_RS)) {
247 s->status |= UHCI_STS_HCHALTED;
249 if (val & UHCI_CMD_GRESET) {
254 /* send reset on the USB bus */
255 for(i = 0; i < NB_PORTS; i++) {
257 dev = port->port.dev;
259 usb_send_msg(dev, USB_MSG_RESET);
265 if (val & UHCI_CMD_HCRESET) {
273 /* XXX: the chip spec is not coherent, so we add a hidden
274 register to distinguish between IOC and SPD */
275 if (val & UHCI_STS_USBINT)
284 if (s->status & UHCI_STS_HCHALTED)
285 s->frnum = val & 0x7ff;
297 dev = port->port.dev;
300 if ( (val & UHCI_PORT_RESET) &&
301 !(port->ctrl & UHCI_PORT_RESET) ) {
302 usb_send_msg(dev, USB_MSG_RESET);
305 port->ctrl = (port->ctrl & 0x01fb) | (val & ~0x01fb);
306 /* some bits are reset when a '1' is written to them */
307 port->ctrl &= ~(val & 0x000a);
313 static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
315 UHCIState *s = opaque;
345 val = 0xff7f; /* disabled port */
349 printf("uhci readw port=0x%04x val=0x%04x\n", addr, val);
354 static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
356 UHCIState *s = opaque;
360 printf("uhci writel port=0x%04x val=0x%08x\n", addr, val);
364 s->fl_base_addr = val & ~0xfff;
369 static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
371 UHCIState *s = opaque;
377 val = s->fl_base_addr;
386 /* signal resume if controller suspended */
387 static void uhci_resume (void *opaque)
389 UHCIState *s = (UHCIState *)opaque;
394 if (s->cmd & UHCI_CMD_EGSM) {
395 s->cmd |= UHCI_CMD_FGR;
396 s->status |= UHCI_STS_RD;
401 static void uhci_attach(USBPort *port1, USBDevice *dev)
403 UHCIState *s = port1->opaque;
404 UHCIPort *port = &s->ports[port1->index];
407 if (port->port.dev) {
408 usb_attach(port1, NULL);
410 /* set connect status */
411 port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
414 if (dev->speed == USB_SPEED_LOW)
415 port->ctrl |= UHCI_PORT_LSDA;
417 port->ctrl &= ~UHCI_PORT_LSDA;
421 port->port.dev = dev;
422 /* send the attach message */
423 usb_send_msg(dev, USB_MSG_ATTACH);
425 /* set connect status */
426 if (port->ctrl & UHCI_PORT_CCS) {
427 port->ctrl &= ~UHCI_PORT_CCS;
428 port->ctrl |= UHCI_PORT_CSC;
431 if (port->ctrl & UHCI_PORT_EN) {
432 port->ctrl &= ~UHCI_PORT_EN;
433 port->ctrl |= UHCI_PORT_ENC;
438 dev = port->port.dev;
440 /* send the detach message */
441 usb_send_msg(dev, USB_MSG_DETACH);
443 port->port.dev = NULL;
447 static int uhci_broadcast_packet(UHCIState *s, USBPacket *p)
457 case USB_TOKEN_SETUP: pidstr = "SETUP"; break;
458 case USB_TOKEN_IN: pidstr = "IN"; break;
459 case USB_TOKEN_OUT: pidstr = "OUT"; break;
460 default: pidstr = "?"; break;
462 printf("frame %d: pid=%s addr=0x%02x ep=%d len=%d\n",
463 s->frnum, pidstr, p->devaddr, p->devep, p->len);
464 if (p->pid != USB_TOKEN_IN) {
465 printf(" data_out=");
466 for(i = 0; i < p->len; i++) {
467 printf(" %02x", p->data[i]);
473 for(i = 0; i < NB_PORTS; i++) {
475 dev = port->port.dev;
476 if (dev && (port->ctrl & UHCI_PORT_EN)) {
477 ret = dev->handle_packet(dev, p);
478 if (ret != USB_RET_NODEV) {
480 if (ret == USB_RET_ASYNC) {
481 printf("usb-uhci: Async packet\n");
483 printf(" ret=%d ", ret);
484 if (p->pid == USB_TOKEN_IN && ret > 0) {
486 for(i = 0; i < ret; i++) {
487 printf(" %02x", p->data[i]);
497 return USB_RET_NODEV;
500 static void uhci_async_complete_packet(USBPacket * packet, void *opaque);
502 /* return -1 if fatal error (frame must be stopped)
504 1 if TD unsuccessful or inactive
506 static int uhci_handle_td(UHCIState *s, UHCI_TD *td, int *int_mask,
510 int len = 0, max_len, err, ret = 0;
512 /* ??? This is wrong for async completion. */
513 if (td->ctrl & TD_CTRL_IOC) {
517 if (!(td->ctrl & TD_CTRL_ACTIVE))
521 max_len = ((td->token >> 21) + 1) & 0x7ff;
522 pid = td->token & 0xff;
524 if (completion && (s->async_qh || s->async_frame_addr)) {
525 ret = s->usb_packet.len;
530 ret = USB_RET_BABBLE;
533 /* write the data back */
534 cpu_physical_memory_write(td->buffer, s->usb_buf, len);
540 s->async_frame_addr = 0;
541 } else if (!completion) {
542 s->usb_packet.pid = pid;
543 s->usb_packet.devaddr = (td->token >> 8) & 0x7f;
544 s->usb_packet.devep = (td->token >> 15) & 0xf;
545 s->usb_packet.data = s->usb_buf;
546 s->usb_packet.len = max_len;
547 s->usb_packet.complete_cb = uhci_async_complete_packet;
548 s->usb_packet.complete_opaque = s;
551 case USB_TOKEN_SETUP:
552 cpu_physical_memory_read(td->buffer, s->usb_buf, max_len);
553 ret = uhci_broadcast_packet(s, &s->usb_packet);
557 ret = uhci_broadcast_packet(s, &s->usb_packet);
562 ret = USB_RET_BABBLE;
565 /* write the data back */
566 cpu_physical_memory_write(td->buffer, s->usb_buf, len);
573 /* invalid pid : frame interrupted */
574 s->status |= UHCI_STS_HCPERR;
580 if (ret == USB_RET_ASYNC) {
583 if (td->ctrl & TD_CTRL_IOS)
584 td->ctrl &= ~TD_CTRL_ACTIVE;
586 td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
587 /* The NAK bit may have been set by a previous frame, so clear it
588 here. The docs are somewhat unclear, but win2k relies on this
590 td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
591 if (pid == USB_TOKEN_IN &&
592 (td->ctrl & TD_CTRL_SPD) &&
595 /* short packet: do not update QH */
606 td->ctrl |= TD_CTRL_TIMEOUT;
607 err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
611 td->ctrl &= ~TD_CTRL_ACTIVE;
612 s->status |= UHCI_STS_USBERR;
616 td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
617 (err << TD_CTRL_ERROR_SHIFT);
620 td->ctrl |= TD_CTRL_NAK;
621 if (pid == USB_TOKEN_SETUP)
625 td->ctrl |= TD_CTRL_STALL;
626 td->ctrl &= ~TD_CTRL_ACTIVE;
629 td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
630 td->ctrl &= ~TD_CTRL_ACTIVE;
631 /* frame interrupted */
637 static void uhci_async_complete_packet(USBPacket * packet, void *opaque)
639 UHCIState *s = opaque;
643 uint32_t old_td_ctrl;
648 /* Handle async isochronous packet completion */
649 frame_addr = s->async_frame_addr;
651 cpu_physical_memory_read(frame_addr, (uint8_t *)&link, 4);
654 cpu_physical_memory_read(link & ~0xf, (uint8_t *)&td, sizeof(td));
655 le32_to_cpus(&td.link);
656 le32_to_cpus(&td.ctrl);
657 le32_to_cpus(&td.token);
658 le32_to_cpus(&td.buffer);
659 old_td_ctrl = td.ctrl;
660 ret = uhci_handle_td(s, &td, &s->pending_int_mask, 1);
662 /* update the status bits of the TD */
663 if (old_td_ctrl != td.ctrl) {
664 val = cpu_to_le32(td.ctrl);
665 cpu_physical_memory_write((link & ~0xf) + 4,
666 (const uint8_t *)&val,
670 s->async_frame_addr = frame_addr;
671 } else if (ret == 0) {
672 /* update qh element link */
673 val = cpu_to_le32(td.link);
674 cpu_physical_memory_write(frame_addr,
675 (const uint8_t *)&val,
683 /* This should never happen. It means a TD somehow got removed
684 without cancelling the associated async IO request. */
687 cpu_physical_memory_read(link & ~0xf, (uint8_t *)&qh, sizeof(qh));
688 le32_to_cpus(&qh.link);
689 le32_to_cpus(&qh.el_link);
690 /* Re-process the queue containing the async packet. */
692 cpu_physical_memory_read(qh.el_link & ~0xf,
693 (uint8_t *)&td, sizeof(td));
694 le32_to_cpus(&td.link);
695 le32_to_cpus(&td.ctrl);
696 le32_to_cpus(&td.token);
697 le32_to_cpus(&td.buffer);
698 old_td_ctrl = td.ctrl;
699 ret = uhci_handle_td(s, &td, &s->pending_int_mask, 1);
701 /* update the status bits of the TD */
702 if (old_td_ctrl != td.ctrl) {
703 val = cpu_to_le32(td.ctrl);
704 cpu_physical_memory_write((qh.el_link & ~0xf) + 4,
705 (const uint8_t *)&val,
709 break; /* interrupted frame */
713 } else if (ret == 0) {
714 /* update qh element link */
715 qh.el_link = td.link;
716 val = cpu_to_le32(qh.el_link);
717 cpu_physical_memory_write((link & ~0xf) + 4,
718 (const uint8_t *)&val,
720 if (!(qh.el_link & 4))
727 static void uhci_frame_timer(void *opaque)
729 UHCIState *s = opaque;
731 uint32_t frame_addr, link, old_td_ctrl, val;
732 int int_mask, cnt, ret;
735 uint32_t old_async_qh;
737 if (!(s->cmd & UHCI_CMD_RS)) {
738 qemu_del_timer(s->frame_timer);
739 /* set hchalted bit in status - UHCI11D 2.1.2 */
740 s->status |= UHCI_STS_HCHALTED;
743 /* Complete the previous frame. */
744 s->frnum = (s->frnum + 1) & 0x7ff;
745 if (s->pending_int_mask) {
746 s->status2 |= s->pending_int_mask;
747 s->status |= UHCI_STS_USBINT;
750 old_async_qh = s->async_qh;
751 frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
752 cpu_physical_memory_read(frame_addr, (uint8_t *)&link, 4);
755 cnt = FRAME_MAX_LOOPS;
756 while ((link & 1) == 0) {
762 if (link == s->async_qh) {
763 /* We've found a previously issues packet.
764 Nothing else to do. */
768 cpu_physical_memory_read(link & ~0xf, (uint8_t *)&qh, sizeof(qh));
769 le32_to_cpus(&qh.link);
770 le32_to_cpus(&qh.el_link);
772 if (qh.el_link & 1) {
773 /* no element : go to next entry */
775 } else if (qh.el_link & 2) {
778 } else if (s->async_qh) {
779 /* We can only cope with one pending packet. Keep looking
780 for the previously issued packet. */
786 cpu_physical_memory_read(qh.el_link & ~0xf,
787 (uint8_t *)&td, sizeof(td));
788 le32_to_cpus(&td.link);
789 le32_to_cpus(&td.ctrl);
790 le32_to_cpus(&td.token);
791 le32_to_cpus(&td.buffer);
792 old_td_ctrl = td.ctrl;
793 ret = uhci_handle_td(s, &td, &int_mask, 0);
795 /* update the status bits of the TD */
796 if (old_td_ctrl != td.ctrl) {
797 val = cpu_to_le32(td.ctrl);
798 cpu_physical_memory_write((qh.el_link & ~0xf) + 4,
799 (const uint8_t *)&val,
803 break; /* interrupted frame */
806 } else if (ret == 0) {
807 /* update qh element link */
808 qh.el_link = td.link;
809 val = cpu_to_le32(qh.el_link);
810 cpu_physical_memory_write((link & ~0xf) + 4,
811 (const uint8_t *)&val,
813 if (qh.el_link & 4) {
818 /* go to next entry */
823 cpu_physical_memory_read(link & ~0xf, (uint8_t *)&td, sizeof(td));
824 le32_to_cpus(&td.link);
825 le32_to_cpus(&td.ctrl);
826 le32_to_cpus(&td.token);
827 le32_to_cpus(&td.buffer);
829 /* Handle isochonous transfer. */
830 /* FIXME: might be more than one isoc in frame */
831 old_td_ctrl = td.ctrl;
832 ret = uhci_handle_td(s, &td, &int_mask, 0);
834 /* update the status bits of the TD */
835 if (old_td_ctrl != td.ctrl) {
836 val = cpu_to_le32(td.ctrl);
837 cpu_physical_memory_write((link & ~0xf) + 4,
838 (const uint8_t *)&val,
842 break; /* interrupted frame */
844 s->async_frame_addr = frame_addr;
849 s->pending_int_mask = int_mask;
851 /* A previously started transfer has disappeared from the transfer
852 list. There's nothing useful we can do with it now, so just
853 discard the packet and hope it wasn't too important. */
855 printf("Discarding USB packet\n");
857 usb_cancel_packet(&s->usb_packet);
861 /* prepare the timer for the next frame */
862 expire_time = qemu_get_clock(vm_clock) +
863 (ticks_per_sec / FRAME_TIMER_FREQ);
864 qemu_mod_timer(s->frame_timer, expire_time);
867 static void uhci_map(PCIDevice *pci_dev, int region_num,
868 uint32_t addr, uint32_t size, int type)
870 UHCIState *s = (UHCIState *)pci_dev;
872 register_ioport_write(addr, 32, 2, uhci_ioport_writew, s);
873 register_ioport_read(addr, 32, 2, uhci_ioport_readw, s);
874 register_ioport_write(addr, 32, 4, uhci_ioport_writel, s);
875 register_ioport_read(addr, 32, 4, uhci_ioport_readl, s);
876 register_ioport_write(addr, 32, 1, uhci_ioport_writeb, s);
877 register_ioport_read(addr, 32, 1, uhci_ioport_readb, s);
880 void usb_uhci_piix3_init(PCIBus *bus, int devfn)
886 s = (UHCIState *)pci_register_device(bus,
887 "USB-UHCI", sizeof(UHCIState),
889 pci_conf = s->dev.config;
890 pci_conf[0x00] = 0x86;
891 pci_conf[0x01] = 0x80;
892 pci_conf[0x02] = 0x20;
893 pci_conf[0x03] = 0x70;
894 pci_conf[0x08] = 0x01; // revision number
895 pci_conf[0x09] = 0x00;
896 pci_conf[0x0a] = 0x03;
897 pci_conf[0x0b] = 0x0c;
898 pci_conf[0x0e] = 0x00; // header_type
899 pci_conf[0x3d] = 4; // interrupt pin 3
900 pci_conf[0x60] = 0x10; // release number
902 for(i = 0; i < NB_PORTS; i++) {
903 qemu_register_usb_port(&s->ports[i].port, s, i, uhci_attach);
905 s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s);
909 /* Use region 4 for consistency with real hardware. BSD guests seem
911 pci_register_io_region(&s->dev, 4, 0x20,
912 PCI_ADDRESS_SPACE_IO, uhci_map);
915 void usb_uhci_piix4_init(PCIBus *bus, int devfn)
921 s = (UHCIState *)pci_register_device(bus,
922 "USB-UHCI", sizeof(UHCIState),
924 pci_conf = s->dev.config;
925 pci_conf[0x00] = 0x86;
926 pci_conf[0x01] = 0x80;
927 pci_conf[0x02] = 0x12;
928 pci_conf[0x03] = 0x71;
929 pci_conf[0x08] = 0x01; // revision number
930 pci_conf[0x09] = 0x00;
931 pci_conf[0x0a] = 0x03;
932 pci_conf[0x0b] = 0x0c;
933 pci_conf[0x0e] = 0x00; // header_type
934 pci_conf[0x3d] = 4; // interrupt pin 3
935 pci_conf[0x60] = 0x10; // release number
937 for(i = 0; i < NB_PORTS; i++) {
938 qemu_register_usb_port(&s->ports[i].port, s, i, uhci_attach);
940 s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s);
944 /* Use region 4 for consistency with real hardware. BSD guests seem
946 pci_register_io_region(&s->dev, 4, 0x20,
947 PCI_ADDRESS_SPACE_IO, uhci_map);