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 #define DPRINTF printf
83 static const char *pid2str(int pid
)
86 case USB_TOKEN_SETUP
: return "SETUP";
87 case USB_TOKEN_IN
: return "IN";
88 case USB_TOKEN_OUT
: return "OUT";
97 typedef struct UHCIState UHCIState
;
100 * Pending async transaction.
101 * 'packet' must be the first field because completion
102 * handler does "(UHCIAsync *) pkt" cast.
104 typedef struct UHCIAsync
{
108 QTAILQ_ENTRY(UHCIAsync
) next
;
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
;
134 UHCIPort ports
[NB_PORTS
];
136 /* Interrupts that should be raised at the end of the current frame. */
137 uint32_t pending_int_mask
;
140 QTAILQ_HEAD(,UHCIAsync
) async_pending
;
141 uint8_t num_ports_vmstate
;
148 typedef struct UHCI_TD
{
150 uint32_t ctrl
; /* see TD_CTRL_xxx */
155 typedef struct UHCI_QH
{
160 static UHCIAsync
*uhci_async_alloc(UHCIState
*s
)
162 UHCIAsync
*async
= g_malloc(sizeof(UHCIAsync
));
164 memset(&async
->packet
, 0, sizeof(async
->packet
));
171 usb_packet_init(&async
->packet
);
172 pci_dma_sglist_init(&async
->sgl
, &s
->dev
, 1);
177 static void uhci_async_free(UHCIState
*s
, UHCIAsync
*async
)
179 usb_packet_cleanup(&async
->packet
);
180 qemu_sglist_destroy(&async
->sgl
);
184 static void uhci_async_link(UHCIState
*s
, UHCIAsync
*async
)
186 QTAILQ_INSERT_HEAD(&s
->async_pending
, async
, next
);
189 static void uhci_async_unlink(UHCIState
*s
, UHCIAsync
*async
)
191 QTAILQ_REMOVE(&s
->async_pending
, async
, next
);
194 static void uhci_async_cancel(UHCIState
*s
, UHCIAsync
*async
)
196 DPRINTF("uhci: cancel td 0x%x token 0x%x done %u\n",
197 async
->td
, async
->token
, async
->done
);
200 usb_cancel_packet(&async
->packet
);
201 uhci_async_free(s
, async
);
205 * Mark all outstanding async packets as invalid.
206 * This is used for canceling them when TDs are removed by the HCD.
208 static UHCIAsync
*uhci_async_validate_begin(UHCIState
*s
)
212 QTAILQ_FOREACH(async
, &s
->async_pending
, next
) {
219 * Cancel async packets that are no longer valid
221 static void uhci_async_validate_end(UHCIState
*s
)
225 QTAILQ_FOREACH_SAFE(curr
, &s
->async_pending
, next
, n
) {
226 if (curr
->valid
> 0) {
229 uhci_async_unlink(s
, curr
);
230 uhci_async_cancel(s
, curr
);
234 static void uhci_async_cancel_device(UHCIState
*s
, USBDevice
*dev
)
238 QTAILQ_FOREACH_SAFE(curr
, &s
->async_pending
, next
, n
) {
239 if (!usb_packet_is_inflight(&curr
->packet
) ||
240 curr
->packet
.ep
->dev
!= dev
) {
243 uhci_async_unlink(s
, curr
);
244 uhci_async_cancel(s
, curr
);
248 static void uhci_async_cancel_all(UHCIState
*s
)
252 QTAILQ_FOREACH_SAFE(curr
, &s
->async_pending
, next
, n
) {
253 uhci_async_unlink(s
, curr
);
254 uhci_async_cancel(s
, curr
);
258 static UHCIAsync
*uhci_async_find_td(UHCIState
*s
, uint32_t addr
, uint32_t token
)
261 UHCIAsync
*match
= NULL
;
265 * We're looking for the best match here. ie both td addr and token.
266 * Otherwise we return last good match. ie just token.
267 * It's ok to match just token because it identifies the transaction
268 * rather well, token includes: device addr, endpoint, size, etc.
270 * Also since we queue async transactions in reverse order by returning
271 * last good match we restores the order.
273 * It's expected that we wont have a ton of outstanding transactions.
274 * If we ever do we'd want to optimize this algorithm.
277 QTAILQ_FOREACH(async
, &s
->async_pending
, next
) {
278 if (async
->token
== token
) {
282 if (async
->td
== addr
) {
291 fprintf(stderr
, "uhci: warning lots of async transactions\n");
296 static void uhci_update_irq(UHCIState
*s
)
299 if (((s
->status2
& 1) && (s
->intr
& (1 << 2))) ||
300 ((s
->status2
& 2) && (s
->intr
& (1 << 3))) ||
301 ((s
->status
& UHCI_STS_USBERR
) && (s
->intr
& (1 << 0))) ||
302 ((s
->status
& UHCI_STS_RD
) && (s
->intr
& (1 << 1))) ||
303 (s
->status
& UHCI_STS_HSERR
) ||
304 (s
->status
& UHCI_STS_HCPERR
)) {
309 qemu_set_irq(s
->dev
.irq
[3], level
);
312 static void uhci_reset(void *opaque
)
314 UHCIState
*s
= opaque
;
319 DPRINTF("uhci: full reset\n");
321 pci_conf
= s
->dev
.config
;
323 pci_conf
[0x6a] = 0x01; /* usb clock */
324 pci_conf
[0x6b] = 0x00;
332 for(i
= 0; i
< NB_PORTS
; i
++) {
335 if (port
->port
.dev
&& port
->port
.dev
->attached
) {
336 usb_port_reset(&port
->port
);
340 uhci_async_cancel_all(s
);
343 static void uhci_pre_save(void *opaque
)
345 UHCIState
*s
= opaque
;
347 uhci_async_cancel_all(s
);
350 static const VMStateDescription vmstate_uhci_port
= {
353 .minimum_version_id
= 1,
354 .minimum_version_id_old
= 1,
355 .fields
= (VMStateField
[]) {
356 VMSTATE_UINT16(ctrl
, UHCIPort
),
357 VMSTATE_END_OF_LIST()
361 static const VMStateDescription vmstate_uhci
= {
364 .minimum_version_id
= 1,
365 .minimum_version_id_old
= 1,
366 .pre_save
= uhci_pre_save
,
367 .fields
= (VMStateField
[]) {
368 VMSTATE_PCI_DEVICE(dev
, UHCIState
),
369 VMSTATE_UINT8_EQUAL(num_ports_vmstate
, UHCIState
),
370 VMSTATE_STRUCT_ARRAY(ports
, UHCIState
, NB_PORTS
, 1,
371 vmstate_uhci_port
, UHCIPort
),
372 VMSTATE_UINT16(cmd
, UHCIState
),
373 VMSTATE_UINT16(status
, UHCIState
),
374 VMSTATE_UINT16(intr
, UHCIState
),
375 VMSTATE_UINT16(frnum
, UHCIState
),
376 VMSTATE_UINT32(fl_base_addr
, UHCIState
),
377 VMSTATE_UINT8(sof_timing
, UHCIState
),
378 VMSTATE_UINT8(status2
, UHCIState
),
379 VMSTATE_TIMER(frame_timer
, UHCIState
),
380 VMSTATE_INT64_V(expire_time
, UHCIState
, 2),
381 VMSTATE_END_OF_LIST()
385 static void uhci_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
387 UHCIState
*s
= opaque
;
397 static uint32_t uhci_ioport_readb(void *opaque
, uint32_t addr
)
399 UHCIState
*s
= opaque
;
414 static void uhci_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
416 UHCIState
*s
= opaque
;
419 DPRINTF("uhci: writew port=0x%04x val=0x%04x\n", addr
, val
);
423 if ((val
& UHCI_CMD_RS
) && !(s
->cmd
& UHCI_CMD_RS
)) {
424 /* start frame processing */
425 s
->expire_time
= qemu_get_clock_ns(vm_clock
) +
426 (get_ticks_per_sec() / FRAME_TIMER_FREQ
);
427 qemu_mod_timer(s
->frame_timer
, qemu_get_clock_ns(vm_clock
));
428 s
->status
&= ~UHCI_STS_HCHALTED
;
429 } else if (!(val
& UHCI_CMD_RS
)) {
430 s
->status
|= UHCI_STS_HCHALTED
;
432 if (val
& UHCI_CMD_GRESET
) {
436 /* send reset on the USB bus */
437 for(i
= 0; i
< NB_PORTS
; i
++) {
439 usb_device_reset(port
->port
.dev
);
444 if (val
& UHCI_CMD_HCRESET
) {
452 /* XXX: the chip spec is not coherent, so we add a hidden
453 register to distinguish between IOC and SPD */
454 if (val
& UHCI_STS_USBINT
)
463 if (s
->status
& UHCI_STS_HCHALTED
)
464 s
->frnum
= val
& 0x7ff;
476 dev
= port
->port
.dev
;
477 if (dev
&& dev
->attached
) {
479 if ( (val
& UHCI_PORT_RESET
) &&
480 !(port
->ctrl
& UHCI_PORT_RESET
) ) {
481 usb_device_reset(dev
);
484 port
->ctrl
&= UHCI_PORT_READ_ONLY
;
485 port
->ctrl
|= (val
& ~UHCI_PORT_READ_ONLY
);
486 /* some bits are reset when a '1' is written to them */
487 port
->ctrl
&= ~(val
& UHCI_PORT_WRITE_CLEAR
);
493 static uint32_t uhci_ioport_readw(void *opaque
, uint32_t addr
)
495 UHCIState
*s
= opaque
;
525 val
= 0xff7f; /* disabled port */
529 DPRINTF("uhci: readw port=0x%04x val=0x%04x\n", addr
, val
);
534 static void uhci_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
536 UHCIState
*s
= opaque
;
539 DPRINTF("uhci: writel port=0x%04x val=0x%08x\n", addr
, val
);
543 s
->fl_base_addr
= val
& ~0xfff;
548 static uint32_t uhci_ioport_readl(void *opaque
, uint32_t addr
)
550 UHCIState
*s
= opaque
;
556 val
= s
->fl_base_addr
;
565 /* signal resume if controller suspended */
566 static void uhci_resume (void *opaque
)
568 UHCIState
*s
= (UHCIState
*)opaque
;
573 if (s
->cmd
& UHCI_CMD_EGSM
) {
574 s
->cmd
|= UHCI_CMD_FGR
;
575 s
->status
|= UHCI_STS_RD
;
580 static void uhci_attach(USBPort
*port1
)
582 UHCIState
*s
= port1
->opaque
;
583 UHCIPort
*port
= &s
->ports
[port1
->index
];
585 /* set connect status */
586 port
->ctrl
|= UHCI_PORT_CCS
| UHCI_PORT_CSC
;
589 if (port
->port
.dev
->speed
== USB_SPEED_LOW
) {
590 port
->ctrl
|= UHCI_PORT_LSDA
;
592 port
->ctrl
&= ~UHCI_PORT_LSDA
;
598 static void uhci_detach(USBPort
*port1
)
600 UHCIState
*s
= port1
->opaque
;
601 UHCIPort
*port
= &s
->ports
[port1
->index
];
603 uhci_async_cancel_device(s
, port1
->dev
);
605 /* set connect status */
606 if (port
->ctrl
& UHCI_PORT_CCS
) {
607 port
->ctrl
&= ~UHCI_PORT_CCS
;
608 port
->ctrl
|= UHCI_PORT_CSC
;
611 if (port
->ctrl
& UHCI_PORT_EN
) {
612 port
->ctrl
&= ~UHCI_PORT_EN
;
613 port
->ctrl
|= UHCI_PORT_ENC
;
619 static void uhci_child_detach(USBPort
*port1
, USBDevice
*child
)
621 UHCIState
*s
= port1
->opaque
;
623 uhci_async_cancel_device(s
, child
);
626 static void uhci_wakeup(USBPort
*port1
)
628 UHCIState
*s
= port1
->opaque
;
629 UHCIPort
*port
= &s
->ports
[port1
->index
];
631 if (port
->ctrl
& UHCI_PORT_SUSPEND
&& !(port
->ctrl
& UHCI_PORT_RD
)) {
632 port
->ctrl
|= UHCI_PORT_RD
;
637 static USBDevice
*uhci_find_device(UHCIState
*s
, uint8_t addr
)
642 for (i
= 0; i
< NB_PORTS
; i
++) {
643 UHCIPort
*port
= &s
->ports
[i
];
644 if (!(port
->ctrl
& UHCI_PORT_EN
)) {
647 dev
= usb_find_device(&port
->port
, addr
);
655 static void uhci_async_complete(USBPort
*port
, USBPacket
*packet
);
656 static void uhci_process_frame(UHCIState
*s
);
658 /* return -1 if fatal error (frame must be stopped)
660 1 if TD unsuccessful or inactive
662 static int uhci_complete_td(UHCIState
*s
, UHCI_TD
*td
, UHCIAsync
*async
, uint32_t *int_mask
)
664 int len
= 0, max_len
, err
, ret
;
667 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
668 pid
= td
->token
& 0xff;
670 ret
= async
->packet
.result
;
672 if (td
->ctrl
& TD_CTRL_IOS
)
673 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
678 len
= async
->packet
.result
;
679 td
->ctrl
= (td
->ctrl
& ~0x7ff) | ((len
- 1) & 0x7ff);
681 /* The NAK bit may have been set by a previous frame, so clear it
682 here. The docs are somewhat unclear, but win2k relies on this
684 td
->ctrl
&= ~(TD_CTRL_ACTIVE
| TD_CTRL_NAK
);
685 if (td
->ctrl
& TD_CTRL_IOC
)
688 if (pid
== USB_TOKEN_IN
) {
690 ret
= USB_RET_BABBLE
;
694 if ((td
->ctrl
& TD_CTRL_SPD
) && len
< max_len
) {
696 /* short packet: do not update QH */
697 DPRINTF("uhci: short packet. td 0x%x token 0x%x\n", async
->td
, async
->token
);
708 td
->ctrl
|= TD_CTRL_STALL
;
709 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
710 s
->status
|= UHCI_STS_USBERR
;
711 if (td
->ctrl
& TD_CTRL_IOC
) {
718 td
->ctrl
|= TD_CTRL_BABBLE
| TD_CTRL_STALL
;
719 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
720 s
->status
|= UHCI_STS_USBERR
;
721 if (td
->ctrl
& TD_CTRL_IOC
) {
725 /* frame interrupted */
729 td
->ctrl
|= TD_CTRL_NAK
;
730 if (pid
== USB_TOKEN_SETUP
)
739 /* Retry the TD if error count is not zero */
741 td
->ctrl
|= TD_CTRL_TIMEOUT
;
742 err
= (td
->ctrl
>> TD_CTRL_ERROR_SHIFT
) & 3;
746 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
747 s
->status
|= UHCI_STS_USBERR
;
748 if (td
->ctrl
& TD_CTRL_IOC
)
753 td
->ctrl
= (td
->ctrl
& ~(3 << TD_CTRL_ERROR_SHIFT
)) |
754 (err
<< TD_CTRL_ERROR_SHIFT
);
758 static int uhci_handle_td(UHCIState
*s
, uint32_t addr
, UHCI_TD
*td
, uint32_t *int_mask
)
761 int len
= 0, max_len
;
768 if (!(td
->ctrl
& TD_CTRL_ACTIVE
))
771 /* token field is not unique for isochronous requests,
772 * so use the destination buffer
774 if (td
->ctrl
& TD_CTRL_IOS
) {
782 async
= uhci_async_find_td(s
, addr
, token
);
784 /* Already submitted */
790 uhci_async_unlink(s
, async
);
794 /* Allocate new packet */
795 async
= uhci_async_alloc(s
);
799 /* valid needs to be large enough to handle 10 frame delay
800 * for initial isochronous requests
804 async
->token
= token
;
807 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
808 pid
= td
->token
& 0xff;
810 dev
= uhci_find_device(s
, (td
->token
>> 8) & 0x7f);
811 ep
= usb_ep_get(dev
, pid
, (td
->token
>> 15) & 0xf);
812 usb_packet_setup(&async
->packet
, pid
, ep
);
813 qemu_sglist_add(&async
->sgl
, td
->buffer
, max_len
);
814 usb_packet_map(&async
->packet
, &async
->sgl
);
818 case USB_TOKEN_SETUP
:
819 len
= usb_handle_packet(dev
, &async
->packet
);
825 len
= usb_handle_packet(dev
, &async
->packet
);
829 /* invalid pid : frame interrupted */
830 uhci_async_free(s
, async
);
831 s
->status
|= UHCI_STS_HCPERR
;
836 if (len
== USB_RET_ASYNC
) {
837 uhci_async_link(s
, async
);
841 async
->packet
.result
= len
;
844 len
= uhci_complete_td(s
, td
, async
, int_mask
);
845 usb_packet_unmap(&async
->packet
);
846 uhci_async_free(s
, async
);
850 static void uhci_async_complete(USBPort
*port
, USBPacket
*packet
)
852 UHCIAsync
*async
= container_of(packet
, UHCIAsync
, packet
);
853 UHCIState
*s
= async
->uhci
;
855 DPRINTF("uhci: async complete. td 0x%x token 0x%x\n", async
->td
, async
->token
);
859 uint32_t link
= async
->td
;
860 uint32_t int_mask
= 0, val
;
862 pci_dma_read(&s
->dev
, link
& ~0xf, &td
, sizeof(td
));
863 le32_to_cpus(&td
.link
);
864 le32_to_cpus(&td
.ctrl
);
865 le32_to_cpus(&td
.token
);
866 le32_to_cpus(&td
.buffer
);
868 uhci_async_unlink(s
, async
);
869 uhci_complete_td(s
, &td
, async
, &int_mask
);
870 s
->pending_int_mask
|= int_mask
;
872 /* update the status bits of the TD */
873 val
= cpu_to_le32(td
.ctrl
);
874 pci_dma_write(&s
->dev
, (link
& ~0xf) + 4, &val
, sizeof(val
));
875 uhci_async_free(s
, async
);
878 uhci_process_frame(s
);
882 static int is_valid(uint32_t link
)
884 return (link
& 1) == 0;
887 static int is_qh(uint32_t link
)
889 return (link
& 2) != 0;
892 static int depth_first(uint32_t link
)
894 return (link
& 4) != 0;
897 /* QH DB used for detecting QH loops */
898 #define UHCI_MAX_QUEUES 128
900 uint32_t addr
[UHCI_MAX_QUEUES
];
904 static void qhdb_reset(QhDb
*db
)
909 /* Add QH to DB. Returns 1 if already present or DB is full. */
910 static int qhdb_insert(QhDb
*db
, uint32_t addr
)
913 for (i
= 0; i
< db
->count
; i
++)
914 if (db
->addr
[i
] == addr
)
917 if (db
->count
>= UHCI_MAX_QUEUES
)
920 db
->addr
[db
->count
++] = addr
;
924 static void uhci_process_frame(UHCIState
*s
)
926 uint32_t frame_addr
, link
, old_td_ctrl
, val
, int_mask
;
927 uint32_t curr_qh
, td_count
= 0, bytes_count
= 0;
933 frame_addr
= s
->fl_base_addr
+ ((s
->frnum
& 0x3ff) << 2);
935 DPRINTF("uhci: processing frame %d addr 0x%x\n" , s
->frnum
, frame_addr
);
937 pci_dma_read(&s
->dev
, frame_addr
, &link
, 4);
945 for (cnt
= FRAME_MAX_LOOPS
; is_valid(link
) && cnt
; cnt
--) {
949 if (qhdb_insert(&qhdb
, link
)) {
951 * We're going in circles. Which is not a bug because
952 * HCD is allowed to do that as part of the BW management.
954 * Stop processing here if
955 * (a) no transaction has been done since we've been
957 * (b) we've reached the usb 1.1 bandwidth, which is
960 DPRINTF("uhci: detected loop. qh 0x%x\n", link
);
962 DPRINTF("uhci: no transaction last round, stop\n");
964 } else if (bytes_count
>= 1280) {
965 DPRINTF("uhci: bandwidth limit reached, stop\n");
970 qhdb_insert(&qhdb
, link
);
974 pci_dma_read(&s
->dev
, link
& ~0xf, &qh
, sizeof(qh
));
975 le32_to_cpus(&qh
.link
);
976 le32_to_cpus(&qh
.el_link
);
978 DPRINTF("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
979 link
, qh
.link
, qh
.el_link
);
981 if (!is_valid(qh
.el_link
)) {
982 /* QH w/o elements */
986 /* QH with elements */
994 pci_dma_read(&s
->dev
, link
& ~0xf, &td
, sizeof(td
));
995 le32_to_cpus(&td
.link
);
996 le32_to_cpus(&td
.ctrl
);
997 le32_to_cpus(&td
.token
);
998 le32_to_cpus(&td
.buffer
);
1000 DPRINTF("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1001 link
, td
.link
, td
.ctrl
, td
.token
, curr_qh
);
1003 old_td_ctrl
= td
.ctrl
;
1004 ret
= uhci_handle_td(s
, link
, &td
, &int_mask
);
1005 if (old_td_ctrl
!= td
.ctrl
) {
1006 /* update the status bits of the TD */
1007 val
= cpu_to_le32(td
.ctrl
);
1008 pci_dma_write(&s
->dev
, (link
& ~0xf) + 4, &val
, sizeof(val
));
1012 /* interrupted frame */
1016 if (ret
== 2 || ret
== 1) {
1017 DPRINTF("uhci: TD 0x%x %s. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1018 link
, ret
== 2 ? "pend" : "skip",
1019 td
.link
, td
.ctrl
, td
.token
, curr_qh
);
1021 link
= curr_qh
? qh
.link
: td
.link
;
1027 DPRINTF("uhci: TD 0x%x done. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1028 link
, td
.link
, td
.ctrl
, td
.token
, curr_qh
);
1032 bytes_count
+= (td
.ctrl
& 0x7ff) + 1;
1035 /* update QH element link */
1037 val
= cpu_to_le32(qh
.el_link
);
1038 pci_dma_write(&s
->dev
, (curr_qh
& ~0xf) + 4, &val
, sizeof(val
));
1040 if (!depth_first(link
)) {
1041 /* done with this QH */
1043 DPRINTF("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
1044 curr_qh
, qh
.link
, qh
.el_link
);
1051 /* go to the next entry */
1054 s
->pending_int_mask
|= int_mask
;
1057 static void uhci_frame_timer(void *opaque
)
1059 UHCIState
*s
= opaque
;
1061 /* prepare the timer for the next frame */
1062 s
->expire_time
+= (get_ticks_per_sec() / FRAME_TIMER_FREQ
);
1064 if (!(s
->cmd
& UHCI_CMD_RS
)) {
1066 qemu_del_timer(s
->frame_timer
);
1067 /* set hchalted bit in status - UHCI11D 2.1.2 */
1068 s
->status
|= UHCI_STS_HCHALTED
;
1070 DPRINTF("uhci: halted\n");
1074 /* Complete the previous frame */
1075 if (s
->pending_int_mask
) {
1076 s
->status2
|= s
->pending_int_mask
;
1077 s
->status
|= UHCI_STS_USBINT
;
1080 s
->pending_int_mask
= 0;
1082 /* Start new frame */
1083 s
->frnum
= (s
->frnum
+ 1) & 0x7ff;
1085 DPRINTF("uhci: new frame #%u\n" , s
->frnum
);
1087 uhci_async_validate_begin(s
);
1089 uhci_process_frame(s
);
1091 uhci_async_validate_end(s
);
1093 qemu_mod_timer(s
->frame_timer
, s
->expire_time
);
1096 static const MemoryRegionPortio uhci_portio
[] = {
1097 { 0, 32, 2, .write
= uhci_ioport_writew
, },
1098 { 0, 32, 2, .read
= uhci_ioport_readw
, },
1099 { 0, 32, 4, .write
= uhci_ioport_writel
, },
1100 { 0, 32, 4, .read
= uhci_ioport_readl
, },
1101 { 0, 32, 1, .write
= uhci_ioport_writeb
, },
1102 { 0, 32, 1, .read
= uhci_ioport_readb
, },
1103 PORTIO_END_OF_LIST()
1106 static const MemoryRegionOps uhci_ioport_ops
= {
1107 .old_portio
= uhci_portio
,
1110 static USBPortOps uhci_port_ops
= {
1111 .attach
= uhci_attach
,
1112 .detach
= uhci_detach
,
1113 .child_detach
= uhci_child_detach
,
1114 .wakeup
= uhci_wakeup
,
1115 .complete
= uhci_async_complete
,
1118 static USBBusOps uhci_bus_ops
= {
1121 static int usb_uhci_common_initfn(PCIDevice
*dev
)
1123 UHCIState
*s
= DO_UPCAST(UHCIState
, dev
, dev
);
1124 uint8_t *pci_conf
= s
->dev
.config
;
1127 pci_conf
[PCI_CLASS_PROG
] = 0x00;
1128 /* TODO: reset value should be 0. */
1129 pci_conf
[PCI_INTERRUPT_PIN
] = 4; /* interrupt pin D */
1130 pci_conf
[USB_SBRN
] = USB_RELEASE_1
; // release number
1133 USBPort
*ports
[NB_PORTS
];
1134 for(i
= 0; i
< NB_PORTS
; i
++) {
1135 ports
[i
] = &s
->ports
[i
].port
;
1137 if (usb_register_companion(s
->masterbus
, ports
, NB_PORTS
,
1138 s
->firstport
, s
, &uhci_port_ops
,
1139 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
) != 0) {
1143 usb_bus_new(&s
->bus
, &uhci_bus_ops
, &s
->dev
.qdev
);
1144 for (i
= 0; i
< NB_PORTS
; i
++) {
1145 usb_register_port(&s
->bus
, &s
->ports
[i
].port
, s
, i
, &uhci_port_ops
,
1146 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
);
1149 s
->frame_timer
= qemu_new_timer_ns(vm_clock
, uhci_frame_timer
, s
);
1150 s
->num_ports_vmstate
= NB_PORTS
;
1151 QTAILQ_INIT(&s
->async_pending
);
1153 qemu_register_reset(uhci_reset
, s
);
1155 memory_region_init_io(&s
->io_bar
, &uhci_ioport_ops
, s
, "uhci", 0x20);
1156 /* Use region 4 for consistency with real hardware. BSD guests seem
1158 pci_register_bar(&s
->dev
, 4, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_bar
);
1163 static int usb_uhci_vt82c686b_initfn(PCIDevice
*dev
)
1165 UHCIState
*s
= DO_UPCAST(UHCIState
, dev
, dev
);
1166 uint8_t *pci_conf
= s
->dev
.config
;
1168 /* USB misc control 1/2 */
1169 pci_set_long(pci_conf
+ 0x40,0x00001000);
1171 pci_set_long(pci_conf
+ 0x80,0x00020001);
1172 /* USB legacy support */
1173 pci_set_long(pci_conf
+ 0xc0,0x00002000);
1175 return usb_uhci_common_initfn(dev
);
1178 static int usb_uhci_exit(PCIDevice
*dev
)
1180 UHCIState
*s
= DO_UPCAST(UHCIState
, dev
, dev
);
1182 memory_region_destroy(&s
->io_bar
);
1186 static Property uhci_properties
[] = {
1187 DEFINE_PROP_STRING("masterbus", UHCIState
, masterbus
),
1188 DEFINE_PROP_UINT32("firstport", UHCIState
, firstport
, 0),
1189 DEFINE_PROP_END_OF_LIST(),
1192 static void piix3_uhci_class_init(ObjectClass
*klass
, void *data
)
1194 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1195 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1197 k
->init
= usb_uhci_common_initfn
;
1198 k
->exit
= usb_uhci_exit
;
1199 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1200 k
->device_id
= PCI_DEVICE_ID_INTEL_82371SB_2
;
1202 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1203 dc
->vmsd
= &vmstate_uhci
;
1204 dc
->props
= uhci_properties
;
1207 static TypeInfo piix3_uhci_info
= {
1208 .name
= "piix3-usb-uhci",
1209 .parent
= TYPE_PCI_DEVICE
,
1210 .instance_size
= sizeof(UHCIState
),
1211 .class_init
= piix3_uhci_class_init
,
1214 static void piix4_uhci_class_init(ObjectClass
*klass
, void *data
)
1216 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1217 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1219 k
->init
= usb_uhci_common_initfn
;
1220 k
->exit
= usb_uhci_exit
;
1221 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1222 k
->device_id
= PCI_DEVICE_ID_INTEL_82371AB_2
;
1224 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1225 dc
->vmsd
= &vmstate_uhci
;
1226 dc
->props
= uhci_properties
;
1229 static TypeInfo piix4_uhci_info
= {
1230 .name
= "piix4-usb-uhci",
1231 .parent
= TYPE_PCI_DEVICE
,
1232 .instance_size
= sizeof(UHCIState
),
1233 .class_init
= piix4_uhci_class_init
,
1236 static void vt82c686b_uhci_class_init(ObjectClass
*klass
, void *data
)
1238 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1239 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1241 k
->init
= usb_uhci_vt82c686b_initfn
;
1242 k
->exit
= usb_uhci_exit
;
1243 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
1244 k
->device_id
= PCI_DEVICE_ID_VIA_UHCI
;
1246 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1247 dc
->vmsd
= &vmstate_uhci
;
1248 dc
->props
= uhci_properties
;
1251 static TypeInfo vt82c686b_uhci_info
= {
1252 .name
= "vt82c686b-usb-uhci",
1253 .parent
= TYPE_PCI_DEVICE
,
1254 .instance_size
= sizeof(UHCIState
),
1255 .class_init
= vt82c686b_uhci_class_init
,
1258 static void ich9_uhci1_class_init(ObjectClass
*klass
, void *data
)
1260 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1261 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1263 k
->init
= usb_uhci_common_initfn
;
1264 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1265 k
->device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI1
;
1267 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1268 dc
->vmsd
= &vmstate_uhci
;
1269 dc
->props
= uhci_properties
;
1272 static TypeInfo ich9_uhci1_info
= {
1273 .name
= "ich9-usb-uhci1",
1274 .parent
= TYPE_PCI_DEVICE
,
1275 .instance_size
= sizeof(UHCIState
),
1276 .class_init
= ich9_uhci1_class_init
,
1279 static void ich9_uhci2_class_init(ObjectClass
*klass
, void *data
)
1281 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1282 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1284 k
->init
= usb_uhci_common_initfn
;
1285 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1286 k
->device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI2
;
1288 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1289 dc
->vmsd
= &vmstate_uhci
;
1290 dc
->props
= uhci_properties
;
1293 static TypeInfo ich9_uhci2_info
= {
1294 .name
= "ich9-usb-uhci2",
1295 .parent
= TYPE_PCI_DEVICE
,
1296 .instance_size
= sizeof(UHCIState
),
1297 .class_init
= ich9_uhci2_class_init
,
1300 static void ich9_uhci3_class_init(ObjectClass
*klass
, void *data
)
1302 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1303 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1305 k
->init
= usb_uhci_common_initfn
;
1306 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1307 k
->device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI3
;
1309 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1310 dc
->vmsd
= &vmstate_uhci
;
1311 dc
->props
= uhci_properties
;
1314 static TypeInfo ich9_uhci3_info
= {
1315 .name
= "ich9-usb-uhci3",
1316 .parent
= TYPE_PCI_DEVICE
,
1317 .instance_size
= sizeof(UHCIState
),
1318 .class_init
= ich9_uhci3_class_init
,
1321 static void uhci_register_types(void)
1323 type_register_static(&piix3_uhci_info
);
1324 type_register_static(&piix4_uhci_info
);
1325 type_register_static(&vt82c686b_uhci_info
);
1326 type_register_static(&ich9_uhci1_info
);
1327 type_register_static(&ich9_uhci2_info
);
1328 type_register_static(&ich9_uhci3_info
);
1331 type_init(uhci_register_types
)
1333 void usb_uhci_piix3_init(PCIBus
*bus
, int devfn
)
1335 pci_create_simple(bus
, devfn
, "piix3-usb-uhci");
1338 void usb_uhci_piix4_init(PCIBus
*bus
, int devfn
)
1340 pci_create_simple(bus
, devfn
, "piix4-usb-uhci");
1343 void usb_uhci_vt82c686b_init(PCIBus
*bus
, int devfn
)
1345 pci_create_simple(bus
, devfn
, "vt82c686b-usb-uhci");