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"
34 //#define DEBUG_DUMP_DATA
36 #define UHCI_CMD_FGR (1 << 4)
37 #define UHCI_CMD_EGSM (1 << 3)
38 #define UHCI_CMD_GRESET (1 << 2)
39 #define UHCI_CMD_HCRESET (1 << 1)
40 #define UHCI_CMD_RS (1 << 0)
42 #define UHCI_STS_HCHALTED (1 << 5)
43 #define UHCI_STS_HCPERR (1 << 4)
44 #define UHCI_STS_HSERR (1 << 3)
45 #define UHCI_STS_RD (1 << 2)
46 #define UHCI_STS_USBERR (1 << 1)
47 #define UHCI_STS_USBINT (1 << 0)
49 #define TD_CTRL_SPD (1 << 29)
50 #define TD_CTRL_ERROR_SHIFT 27
51 #define TD_CTRL_IOS (1 << 25)
52 #define TD_CTRL_IOC (1 << 24)
53 #define TD_CTRL_ACTIVE (1 << 23)
54 #define TD_CTRL_STALL (1 << 22)
55 #define TD_CTRL_BABBLE (1 << 20)
56 #define TD_CTRL_NAK (1 << 19)
57 #define TD_CTRL_TIMEOUT (1 << 18)
59 #define UHCI_PORT_RESET (1 << 9)
60 #define UHCI_PORT_LSDA (1 << 8)
61 #define UHCI_PORT_ENC (1 << 3)
62 #define UHCI_PORT_EN (1 << 2)
63 #define UHCI_PORT_CSC (1 << 1)
64 #define UHCI_PORT_CCS (1 << 0)
66 #define FRAME_TIMER_FREQ 1000
68 #define FRAME_MAX_LOOPS 100
73 #define dprintf printf
75 const char *pid2str(int pid
)
78 case USB_TOKEN_SETUP
: return "SETUP";
79 case USB_TOKEN_IN
: return "IN";
80 case USB_TOKEN_OUT
: return "OUT";
89 #ifdef DEBUG_DUMP_DATA
90 static void dump_data(const uint8_t *data
, int len
)
94 printf("uhci: data: ");
95 for(i
= 0; i
< len
; i
++)
96 printf(" %02x", data
[i
]);
100 static void dump_data(const uint8_t *data
, int len
) {}
104 * Pending async transaction.
105 * 'packet' must be the first field because completion
106 * handler does "(UHCIAsync *) pkt" cast.
108 typedef struct UHCIAsync
{
110 struct UHCIAsync
*next
;
115 uint8_t buffer
[2048];
118 typedef struct UHCIPort
{
123 typedef struct UHCIState
{
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 */
132 QEMUTimer
*frame_timer
;
133 UHCIPort ports
[NB_PORTS
];
135 /* Interrupts that should be raised at the end of the current frame. */
136 uint32_t pending_int_mask
;
139 UHCIAsync
*async_pending
;
140 UHCIAsync
*async_pool
;
143 typedef struct UHCI_TD
{
145 uint32_t ctrl
; /* see TD_CTRL_xxx */
150 typedef struct UHCI_QH
{
155 static UHCIAsync
*uhci_async_alloc(UHCIState
*s
)
157 UHCIAsync
*async
= qemu_malloc(sizeof(UHCIAsync
));
159 memset(&async
->packet
, 0, sizeof(async
->packet
));
169 static void uhci_async_free(UHCIState
*s
, UHCIAsync
*async
)
174 static void uhci_async_link(UHCIState
*s
, UHCIAsync
*async
)
176 async
->next
= s
->async_pending
;
177 s
->async_pending
= async
;
180 static void uhci_async_unlink(UHCIState
*s
, UHCIAsync
*async
)
182 UHCIAsync
*curr
= s
->async_pending
;
183 UHCIAsync
**prev
= &s
->async_pending
;
196 static void uhci_async_cancel(UHCIState
*s
, UHCIAsync
*async
)
198 dprintf("uhci: cancel td 0x%x token 0x%x done %u\n",
199 async
->td
, async
->token
, async
->done
);
202 usb_cancel_packet(&async
->packet
);
203 uhci_async_free(s
, async
);
207 * Mark all outstanding async packets as invalid.
208 * This is used for canceling them when TDs are removed by the HCD.
210 static UHCIAsync
*uhci_async_validate_begin(UHCIState
*s
)
212 UHCIAsync
*async
= s
->async_pending
;
222 * Cancel async packets that are no longer valid
224 static void uhci_async_validate_end(UHCIState
*s
)
226 UHCIAsync
*curr
= s
->async_pending
;
227 UHCIAsync
**prev
= &s
->async_pending
;
231 if (curr
->valid
> 0) {
242 uhci_async_cancel(s
, curr
);
248 static void uhci_async_cancel_all(UHCIState
*s
)
250 UHCIAsync
*curr
= s
->async_pending
;
256 uhci_async_cancel(s
, curr
);
261 s
->async_pending
= NULL
;
264 static UHCIAsync
*uhci_async_find_td(UHCIState
*s
, uint32_t addr
, uint32_t token
)
266 UHCIAsync
*async
= s
->async_pending
;
267 UHCIAsync
*match
= NULL
;
271 * We're looking for the best match here. ie both td addr and token.
272 * Otherwise we return last good match. ie just token.
273 * It's ok to match just token because it identifies the transaction
274 * rather well, token includes: device addr, endpoint, size, etc.
276 * Also since we queue async transactions in reverse order by returning
277 * last good match we restores the order.
279 * It's expected that we wont have a ton of outstanding transactions.
280 * If we ever do we'd want to optimize this algorithm.
284 if (async
->token
== token
) {
288 if (async
->td
== addr
) {
299 fprintf(stderr
, "uhci: warning lots of async transactions\n");
304 static void uhci_attach(USBPort
*port1
, USBDevice
*dev
);
306 static void uhci_update_irq(UHCIState
*s
)
309 if (((s
->status2
& 1) && (s
->intr
& (1 << 2))) ||
310 ((s
->status2
& 2) && (s
->intr
& (1 << 3))) ||
311 ((s
->status
& UHCI_STS_USBERR
) && (s
->intr
& (1 << 0))) ||
312 ((s
->status
& UHCI_STS_RD
) && (s
->intr
& (1 << 1))) ||
313 (s
->status
& UHCI_STS_HSERR
) ||
314 (s
->status
& UHCI_STS_HCPERR
)) {
319 qemu_set_irq(s
->dev
.irq
[3], level
);
322 static void uhci_reset(void *opaque
)
324 UHCIState
*s
= opaque
;
329 dprintf("uhci: full reset\n");
331 pci_conf
= s
->dev
.config
;
333 pci_conf
[0x6a] = 0x01; /* usb clock */
334 pci_conf
[0x6b] = 0x00;
342 for(i
= 0; i
< NB_PORTS
; i
++) {
346 uhci_attach(&port
->port
, port
->port
.dev
);
349 uhci_async_cancel_all(s
);
352 static void uhci_save(QEMUFile
*f
, void *opaque
)
354 UHCIState
*s
= opaque
;
355 uint8_t num_ports
= NB_PORTS
;
358 uhci_async_cancel_all(s
);
360 pci_device_save(&s
->dev
, f
);
362 qemu_put_8s(f
, &num_ports
);
363 for (i
= 0; i
< num_ports
; ++i
)
364 qemu_put_be16s(f
, &s
->ports
[i
].ctrl
);
365 qemu_put_be16s(f
, &s
->cmd
);
366 qemu_put_be16s(f
, &s
->status
);
367 qemu_put_be16s(f
, &s
->intr
);
368 qemu_put_be16s(f
, &s
->frnum
);
369 qemu_put_be32s(f
, &s
->fl_base_addr
);
370 qemu_put_8s(f
, &s
->sof_timing
);
371 qemu_put_8s(f
, &s
->status2
);
372 qemu_put_timer(f
, s
->frame_timer
);
375 static int uhci_load(QEMUFile
*f
, void *opaque
, int version_id
)
377 UHCIState
*s
= opaque
;
384 ret
= pci_device_load(&s
->dev
, f
);
388 qemu_get_8s(f
, &num_ports
);
389 if (num_ports
!= NB_PORTS
)
392 for (i
= 0; i
< num_ports
; ++i
)
393 qemu_get_be16s(f
, &s
->ports
[i
].ctrl
);
394 qemu_get_be16s(f
, &s
->cmd
);
395 qemu_get_be16s(f
, &s
->status
);
396 qemu_get_be16s(f
, &s
->intr
);
397 qemu_get_be16s(f
, &s
->frnum
);
398 qemu_get_be32s(f
, &s
->fl_base_addr
);
399 qemu_get_8s(f
, &s
->sof_timing
);
400 qemu_get_8s(f
, &s
->status2
);
401 qemu_get_timer(f
, s
->frame_timer
);
406 static void uhci_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
408 UHCIState
*s
= opaque
;
418 static uint32_t uhci_ioport_readb(void *opaque
, uint32_t addr
)
420 UHCIState
*s
= opaque
;
435 static void uhci_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
437 UHCIState
*s
= opaque
;
440 dprintf("uhci: writew port=0x%04x val=0x%04x\n", addr
, val
);
444 if ((val
& UHCI_CMD_RS
) && !(s
->cmd
& UHCI_CMD_RS
)) {
445 /* start frame processing */
446 qemu_mod_timer(s
->frame_timer
, qemu_get_clock(vm_clock
));
447 s
->status
&= ~UHCI_STS_HCHALTED
;
448 } else if (!(val
& UHCI_CMD_RS
)) {
449 s
->status
|= UHCI_STS_HCHALTED
;
451 if (val
& UHCI_CMD_GRESET
) {
456 /* send reset on the USB bus */
457 for(i
= 0; i
< NB_PORTS
; i
++) {
459 dev
= port
->port
.dev
;
461 usb_send_msg(dev
, USB_MSG_RESET
);
467 if (val
& UHCI_CMD_HCRESET
) {
475 /* XXX: the chip spec is not coherent, so we add a hidden
476 register to distinguish between IOC and SPD */
477 if (val
& UHCI_STS_USBINT
)
486 if (s
->status
& UHCI_STS_HCHALTED
)
487 s
->frnum
= val
& 0x7ff;
499 dev
= port
->port
.dev
;
502 if ( (val
& UHCI_PORT_RESET
) &&
503 !(port
->ctrl
& UHCI_PORT_RESET
) ) {
504 usb_send_msg(dev
, USB_MSG_RESET
);
507 port
->ctrl
= (port
->ctrl
& 0x01fb) | (val
& ~0x01fb);
508 /* some bits are reset when a '1' is written to them */
509 port
->ctrl
&= ~(val
& 0x000a);
515 static uint32_t uhci_ioport_readw(void *opaque
, uint32_t addr
)
517 UHCIState
*s
= opaque
;
547 val
= 0xff7f; /* disabled port */
551 dprintf("uhci: readw port=0x%04x val=0x%04x\n", addr
, val
);
556 static void uhci_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
558 UHCIState
*s
= opaque
;
561 dprintf("uhci: writel port=0x%04x val=0x%08x\n", addr
, val
);
565 s
->fl_base_addr
= val
& ~0xfff;
570 static uint32_t uhci_ioport_readl(void *opaque
, uint32_t addr
)
572 UHCIState
*s
= opaque
;
578 val
= s
->fl_base_addr
;
587 /* signal resume if controller suspended */
588 static void uhci_resume (void *opaque
)
590 UHCIState
*s
= (UHCIState
*)opaque
;
595 if (s
->cmd
& UHCI_CMD_EGSM
) {
596 s
->cmd
|= UHCI_CMD_FGR
;
597 s
->status
|= UHCI_STS_RD
;
602 static void uhci_attach(USBPort
*port1
, USBDevice
*dev
)
604 UHCIState
*s
= port1
->opaque
;
605 UHCIPort
*port
= &s
->ports
[port1
->index
];
608 if (port
->port
.dev
) {
609 usb_attach(port1
, NULL
);
611 /* set connect status */
612 port
->ctrl
|= UHCI_PORT_CCS
| UHCI_PORT_CSC
;
615 if (dev
->speed
== USB_SPEED_LOW
)
616 port
->ctrl
|= UHCI_PORT_LSDA
;
618 port
->ctrl
&= ~UHCI_PORT_LSDA
;
622 port
->port
.dev
= dev
;
623 /* send the attach message */
624 usb_send_msg(dev
, USB_MSG_ATTACH
);
626 /* set connect status */
627 if (port
->ctrl
& UHCI_PORT_CCS
) {
628 port
->ctrl
&= ~UHCI_PORT_CCS
;
629 port
->ctrl
|= UHCI_PORT_CSC
;
632 if (port
->ctrl
& UHCI_PORT_EN
) {
633 port
->ctrl
&= ~UHCI_PORT_EN
;
634 port
->ctrl
|= UHCI_PORT_ENC
;
639 dev
= port
->port
.dev
;
641 /* send the detach message */
642 usb_send_msg(dev
, USB_MSG_DETACH
);
644 port
->port
.dev
= NULL
;
648 static int uhci_broadcast_packet(UHCIState
*s
, USBPacket
*p
)
652 dprintf("uhci: packet enter. pid %s addr 0x%02x ep %d len %d\n",
653 pid2str(p
->pid
), p
->devaddr
, p
->devep
, p
->len
);
654 if (p
->pid
== USB_TOKEN_OUT
|| p
->pid
== USB_TOKEN_SETUP
)
655 dump_data(p
->data
, p
->len
);
658 for (i
= 0; i
< NB_PORTS
&& ret
== USB_RET_NODEV
; i
++) {
659 UHCIPort
*port
= &s
->ports
[i
];
660 USBDevice
*dev
= port
->port
.dev
;
662 if (dev
&& (port
->ctrl
& UHCI_PORT_EN
))
663 ret
= dev
->handle_packet(dev
, p
);
666 dprintf("uhci: packet exit. ret %d len %d\n", ret
, p
->len
);
667 if (p
->pid
== USB_TOKEN_IN
&& ret
> 0)
668 dump_data(p
->data
, ret
);
673 static void uhci_async_complete(USBPacket
* packet
, void *opaque
);
674 static void uhci_process_frame(UHCIState
*s
);
676 /* return -1 if fatal error (frame must be stopped)
678 1 if TD unsuccessful or inactive
680 static int uhci_complete_td(UHCIState
*s
, UHCI_TD
*td
, UHCIAsync
*async
, uint32_t *int_mask
)
682 int len
= 0, max_len
, err
, ret
;
685 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
686 pid
= td
->token
& 0xff;
688 ret
= async
->packet
.len
;
690 if (td
->ctrl
& TD_CTRL_IOC
)
693 if (td
->ctrl
& TD_CTRL_IOS
)
694 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
699 len
= async
->packet
.len
;
700 td
->ctrl
= (td
->ctrl
& ~0x7ff) | ((len
- 1) & 0x7ff);
702 /* The NAK bit may have been set by a previous frame, so clear it
703 here. The docs are somewhat unclear, but win2k relies on this
705 td
->ctrl
&= ~(TD_CTRL_ACTIVE
| TD_CTRL_NAK
);
707 if (pid
== USB_TOKEN_IN
) {
710 ret
= USB_RET_BABBLE
;
715 /* write the data back */
716 cpu_physical_memory_write(td
->buffer
, async
->buffer
, len
);
719 if ((td
->ctrl
& TD_CTRL_SPD
) && len
< max_len
) {
721 /* short packet: do not update QH */
722 dprintf("uhci: short packet. td 0x%x token 0x%x\n", async
->td
, async
->token
);
733 td
->ctrl
|= TD_CTRL_STALL
;
734 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
738 td
->ctrl
|= TD_CTRL_BABBLE
| TD_CTRL_STALL
;
739 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
740 /* frame interrupted */
744 td
->ctrl
|= TD_CTRL_NAK
;
745 if (pid
== USB_TOKEN_SETUP
)
754 /* Retry the TD if error count is not zero */
756 td
->ctrl
|= TD_CTRL_TIMEOUT
;
757 err
= (td
->ctrl
>> TD_CTRL_ERROR_SHIFT
) & 3;
761 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
762 s
->status
|= UHCI_STS_USBERR
;
766 td
->ctrl
= (td
->ctrl
& ~(3 << TD_CTRL_ERROR_SHIFT
)) |
767 (err
<< TD_CTRL_ERROR_SHIFT
);
771 static int uhci_handle_td(UHCIState
*s
, uint32_t addr
, UHCI_TD
*td
, uint32_t *int_mask
)
774 int len
= 0, max_len
;
778 if (!(td
->ctrl
& TD_CTRL_ACTIVE
))
781 async
= uhci_async_find_td(s
, addr
, td
->token
);
783 /* Already submitted */
789 uhci_async_unlink(s
, async
);
793 /* Allocate new packet */
794 async
= uhci_async_alloc(s
);
800 async
->token
= td
->token
;
802 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
803 pid
= td
->token
& 0xff;
805 async
->packet
.pid
= pid
;
806 async
->packet
.devaddr
= (td
->token
>> 8) & 0x7f;
807 async
->packet
.devep
= (td
->token
>> 15) & 0xf;
808 async
->packet
.data
= async
->buffer
;
809 async
->packet
.len
= max_len
;
810 async
->packet
.complete_cb
= uhci_async_complete
;
811 async
->packet
.complete_opaque
= s
;
815 case USB_TOKEN_SETUP
:
816 cpu_physical_memory_read(td
->buffer
, async
->buffer
, max_len
);
817 len
= uhci_broadcast_packet(s
, &async
->packet
);
823 len
= uhci_broadcast_packet(s
, &async
->packet
);
827 /* invalid pid : frame interrupted */
828 uhci_async_free(s
, async
);
829 s
->status
|= UHCI_STS_HCPERR
;
834 if (len
== USB_RET_ASYNC
) {
835 uhci_async_link(s
, async
);
839 async
->packet
.len
= len
;
842 len
= uhci_complete_td(s
, td
, async
, int_mask
);
843 uhci_async_free(s
, async
);
847 static void uhci_async_complete(USBPacket
*packet
, void *opaque
)
849 UHCIState
*s
= opaque
;
850 UHCIAsync
*async
= (UHCIAsync
*) packet
;
852 dprintf("uhci: async complete. td 0x%x token 0x%x\n", async
->td
, async
->token
);
856 uhci_process_frame(s
);
859 static int is_valid(uint32_t link
)
861 return (link
& 1) == 0;
864 static int is_qh(uint32_t link
)
866 return (link
& 2) != 0;
869 static int depth_first(uint32_t link
)
871 return (link
& 4) != 0;
874 /* QH DB used for detecting QH loops */
875 #define UHCI_MAX_QUEUES 128
877 uint32_t addr
[UHCI_MAX_QUEUES
];
881 static void qhdb_reset(QhDb
*db
)
886 /* Add QH to DB. Returns 1 if already present or DB is full. */
887 static int qhdb_insert(QhDb
*db
, uint32_t addr
)
890 for (i
= 0; i
< db
->count
; i
++)
891 if (db
->addr
[i
] == addr
)
894 if (db
->count
>= UHCI_MAX_QUEUES
)
897 db
->addr
[db
->count
++] = addr
;
901 static void uhci_process_frame(UHCIState
*s
)
903 uint32_t frame_addr
, link
, old_td_ctrl
, val
, int_mask
;
910 frame_addr
= s
->fl_base_addr
+ ((s
->frnum
& 0x3ff) << 2);
912 dprintf("uhci: processing frame %d addr 0x%x\n" , s
->frnum
, frame_addr
);
914 cpu_physical_memory_read(frame_addr
, (uint8_t *)&link
, 4);
922 for (cnt
= FRAME_MAX_LOOPS
; is_valid(link
) && cnt
; cnt
--) {
926 if (qhdb_insert(&qhdb
, link
)) {
928 * We're going in circles. Which is not a bug because
929 * HCD is allowed to do that as part of the BW management.
930 * In our case though it makes no sense to spin here. Sync transations
931 * are already done, and async completion handler will re-process
932 * the frame when something is ready.
934 dprintf("uhci: detected loop. qh 0x%x\n", link
);
938 cpu_physical_memory_read(link
& ~0xf, (uint8_t *) &qh
, sizeof(qh
));
939 le32_to_cpus(&qh
.link
);
940 le32_to_cpus(&qh
.el_link
);
942 dprintf("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
943 link
, qh
.link
, qh
.el_link
);
945 if (!is_valid(qh
.el_link
)) {
946 /* QH w/o elements */
950 /* QH with elements */
958 cpu_physical_memory_read(link
& ~0xf, (uint8_t *) &td
, sizeof(td
));
959 le32_to_cpus(&td
.link
);
960 le32_to_cpus(&td
.ctrl
);
961 le32_to_cpus(&td
.token
);
962 le32_to_cpus(&td
.buffer
);
964 dprintf("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
965 link
, td
.link
, td
.ctrl
, td
.token
, curr_qh
);
967 old_td_ctrl
= td
.ctrl
;
968 ret
= uhci_handle_td(s
, link
, &td
, &int_mask
);
969 if (old_td_ctrl
!= td
.ctrl
) {
970 /* update the status bits of the TD */
971 val
= cpu_to_le32(td
.ctrl
);
972 cpu_physical_memory_write((link
& ~0xf) + 4,
973 (const uint8_t *)&val
, sizeof(val
));
977 /* interrupted frame */
981 if (ret
== 2 || ret
== 1) {
982 dprintf("uhci: TD 0x%x %s. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
983 link
, ret
== 2 ? "pend" : "skip",
984 td
.link
, td
.ctrl
, td
.token
, curr_qh
);
986 link
= curr_qh
? qh
.link
: td
.link
;
992 dprintf("uhci: TD 0x%x done. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
993 link
, td
.link
, td
.ctrl
, td
.token
, curr_qh
);
998 /* update QH element link */
1000 val
= cpu_to_le32(qh
.el_link
);
1001 cpu_physical_memory_write((curr_qh
& ~0xf) + 4,
1002 (const uint8_t *)&val
, sizeof(val
));
1004 if (!depth_first(link
)) {
1005 /* done with this QH */
1007 dprintf("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
1008 curr_qh
, qh
.link
, qh
.el_link
);
1015 /* go to the next entry */
1018 s
->pending_int_mask
= int_mask
;
1021 static void uhci_frame_timer(void *opaque
)
1023 UHCIState
*s
= opaque
;
1024 int64_t expire_time
;
1026 if (!(s
->cmd
& UHCI_CMD_RS
)) {
1028 qemu_del_timer(s
->frame_timer
);
1029 /* set hchalted bit in status - UHCI11D 2.1.2 */
1030 s
->status
|= UHCI_STS_HCHALTED
;
1032 dprintf("uhci: halted\n");
1036 /* Complete the previous frame */
1037 if (s
->pending_int_mask
) {
1038 s
->status2
|= s
->pending_int_mask
;
1039 s
->status
|= UHCI_STS_USBINT
;
1043 /* Start new frame */
1044 s
->frnum
= (s
->frnum
+ 1) & 0x7ff;
1046 dprintf("uhci: new frame #%u\n" , s
->frnum
);
1048 uhci_async_validate_begin(s
);
1050 uhci_process_frame(s
);
1052 uhci_async_validate_end(s
);
1054 /* prepare the timer for the next frame */
1055 expire_time
= qemu_get_clock(vm_clock
) +
1056 (ticks_per_sec
/ FRAME_TIMER_FREQ
);
1057 qemu_mod_timer(s
->frame_timer
, expire_time
);
1060 static void uhci_map(PCIDevice
*pci_dev
, int region_num
,
1061 uint32_t addr
, uint32_t size
, int type
)
1063 UHCIState
*s
= (UHCIState
*)pci_dev
;
1065 register_ioport_write(addr
, 32, 2, uhci_ioport_writew
, s
);
1066 register_ioport_read(addr
, 32, 2, uhci_ioport_readw
, s
);
1067 register_ioport_write(addr
, 32, 4, uhci_ioport_writel
, s
);
1068 register_ioport_read(addr
, 32, 4, uhci_ioport_readl
, s
);
1069 register_ioport_write(addr
, 32, 1, uhci_ioport_writeb
, s
);
1070 register_ioport_read(addr
, 32, 1, uhci_ioport_readb
, s
);
1073 void usb_uhci_piix3_init(PCIBus
*bus
, int devfn
)
1079 s
= (UHCIState
*)pci_register_device(bus
,
1080 "USB-UHCI", sizeof(UHCIState
),
1082 pci_conf
= s
->dev
.config
;
1083 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
1084 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371SB_2
);
1085 pci_conf
[0x08] = 0x01; // revision number
1086 pci_conf
[0x09] = 0x00;
1087 pci_config_set_class(pci_conf
, PCI_CLASS_SERIAL_USB
);
1088 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
1089 pci_conf
[0x3d] = 4; // interrupt pin 3
1090 pci_conf
[0x60] = 0x10; // release number
1092 for(i
= 0; i
< NB_PORTS
; i
++) {
1093 qemu_register_usb_port(&s
->ports
[i
].port
, s
, i
, uhci_attach
);
1095 s
->frame_timer
= qemu_new_timer(vm_clock
, uhci_frame_timer
, s
);
1097 qemu_register_reset(uhci_reset
, s
);
1100 /* Use region 4 for consistency with real hardware. BSD guests seem
1102 pci_register_bar(&s
->dev
, 4, 0x20,
1103 PCI_ADDRESS_SPACE_IO
, uhci_map
);
1105 register_savevm("uhci", 0, 1, uhci_save
, uhci_load
, s
);
1108 void usb_uhci_piix4_init(PCIBus
*bus
, int devfn
)
1114 s
= (UHCIState
*)pci_register_device(bus
,
1115 "USB-UHCI", sizeof(UHCIState
),
1117 pci_conf
= s
->dev
.config
;
1118 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
1119 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB_2
);
1120 pci_conf
[0x08] = 0x01; // revision number
1121 pci_conf
[0x09] = 0x00;
1122 pci_config_set_class(pci_conf
, PCI_CLASS_SERIAL_USB
);
1123 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
1124 pci_conf
[0x3d] = 4; // interrupt pin 3
1125 pci_conf
[0x60] = 0x10; // release number
1127 for(i
= 0; i
< NB_PORTS
; i
++) {
1128 qemu_register_usb_port(&s
->ports
[i
].port
, s
, i
, uhci_attach
);
1130 s
->frame_timer
= qemu_new_timer(vm_clock
, uhci_frame_timer
, s
);
1132 qemu_register_reset(uhci_reset
, s
);
1135 /* Use region 4 for consistency with real hardware. BSD guests seem
1137 pci_register_bar(&s
->dev
, 4, 0x20,
1138 PCI_ADDRESS_SPACE_IO
, uhci_map
);
1140 register_savevm("uhci", 0, 1, uhci_save
, uhci_load
, s
);