2 * QEMU USB EHCI Emulation
4 * Copyright(c) 2008 Emutex Ltd. (address@hidden)
5 * Copyright(c) 2011-2012 Red Hat, Inc.
11 * EHCI project was started by Mark Burkley, with contributions by
12 * Niels de Vos. David S. Ahern continued working on it. Kevin Wolf,
13 * Jan Kiszka and Vincent Palatin contributed bugfixes.
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2 of the License, or(at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 #include "qemu/osdep.h"
31 #include "hw/usb/ehci-regs.h"
32 #include "hw/usb/hcd-ehci.h"
35 #define FRAME_TIMER_FREQ 1000
36 #define FRAME_TIMER_NS (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ)
37 #define UFRAME_TIMER_NS (FRAME_TIMER_NS / 8)
39 #define NB_MAXINTRATE 8 // Max rate at which controller issues ints
40 #define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction
41 #define MAX_QH 100 // Max allowable queue heads in a chain
42 #define MIN_UFR_PER_TICK 24 /* Min frames to process when catching up */
43 #define PERIODIC_ACTIVE 512 /* Micro-frames */
45 /* Internal periodic / asynchronous schedule state machine states
52 /* The following states are internal to the state machine function
66 /* macros for accessing fields within next link pointer entry */
67 #define NLPTR_GET(x) ((x) & 0xffffffe0)
68 #define NLPTR_TYPE_GET(x) (((x) >> 1) & 3)
69 #define NLPTR_TBIT(x) ((x) & 1) // 1=invalid, 0=valid
71 /* link pointer types */
72 #define NLPTR_TYPE_ITD 0 // isoc xfer descriptor
73 #define NLPTR_TYPE_QH 1 // queue head
74 #define NLPTR_TYPE_STITD 2 // split xaction, isoc xfer descriptor
75 #define NLPTR_TYPE_FSTN 3 // frame span traversal node
77 #define SET_LAST_RUN_CLOCK(s) \
78 (s)->last_run_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
80 /* nifty macros from Arnon's EHCI version */
81 #define get_field(data, field) \
82 (((data) & field##_MASK) >> field##_SH)
84 #define set_field(data, newval, field) do { \
85 uint32_t val = *data; \
86 val &= ~ field##_MASK; \
87 val |= ((newval) << field##_SH) & field##_MASK; \
91 static const char *ehci_state_names[] = {
92 [EST_INACTIVE] = "INACTIVE",
93 [EST_ACTIVE] = "ACTIVE",
94 [EST_EXECUTING] = "EXECUTING",
95 [EST_SLEEPING] = "SLEEPING",
96 [EST_WAITLISTHEAD] = "WAITLISTHEAD",
97 [EST_FETCHENTRY] = "FETCH ENTRY",
98 [EST_FETCHQH] = "FETCH QH",
99 [EST_FETCHITD] = "FETCH ITD",
100 [EST_ADVANCEQUEUE] = "ADVANCEQUEUE",
101 [EST_FETCHQTD] = "FETCH QTD",
102 [EST_EXECUTE] = "EXECUTE",
103 [EST_WRITEBACK] = "WRITEBACK",
104 [EST_HORIZONTALQH] = "HORIZONTALQH",
107 static const char *ehci_mmio_names[] = {
110 [USBINTR] = "USBINTR",
111 [FRINDEX] = "FRINDEX",
112 [PERIODICLISTBASE] = "P-LIST BASE",
113 [ASYNCLISTADDR] = "A-LIST ADDR",
114 [CONFIGFLAG] = "CONFIGFLAG",
117 static int ehci_state_executing(EHCIQueue *q);
118 static int ehci_state_writeback(EHCIQueue *q);
119 static int ehci_state_advqueue(EHCIQueue *q);
120 static int ehci_fill_queue(EHCIPacket *p);
121 static void ehci_free_packet(EHCIPacket *p);
123 static const char *nr2str(const char **n, size_t len, uint32_t nr)
125 if (nr < len && n[nr] != NULL) {
132 static const char *state2str(uint32_t state)
134 return nr2str(ehci_state_names, ARRAY_SIZE(ehci_state_names), state);
137 static const char *addr2str(hwaddr addr)
139 return nr2str(ehci_mmio_names, ARRAY_SIZE(ehci_mmio_names), addr);
142 static void ehci_trace_usbsts(uint32_t mask, int state)
145 if (mask & USBSTS_INT) {
146 trace_usb_ehci_usbsts("INT", state);
148 if (mask & USBSTS_ERRINT) {
149 trace_usb_ehci_usbsts("ERRINT", state);
151 if (mask & USBSTS_PCD) {
152 trace_usb_ehci_usbsts("PCD", state);
154 if (mask & USBSTS_FLR) {
155 trace_usb_ehci_usbsts("FLR", state);
157 if (mask & USBSTS_HSE) {
158 trace_usb_ehci_usbsts("HSE", state);
160 if (mask & USBSTS_IAA) {
161 trace_usb_ehci_usbsts("IAA", state);
165 if (mask & USBSTS_HALT) {
166 trace_usb_ehci_usbsts("HALT", state);
168 if (mask & USBSTS_REC) {
169 trace_usb_ehci_usbsts("REC", state);
171 if (mask & USBSTS_PSS) {
172 trace_usb_ehci_usbsts("PSS", state);
174 if (mask & USBSTS_ASS) {
175 trace_usb_ehci_usbsts("ASS", state);
179 static inline void ehci_set_usbsts(EHCIState *s, int mask)
181 if ((s->usbsts & mask) == mask) {
184 ehci_trace_usbsts(mask, 1);
188 static inline void ehci_clear_usbsts(EHCIState *s, int mask)
190 if ((s->usbsts & mask) == 0) {
193 ehci_trace_usbsts(mask, 0);
197 /* update irq line */
198 static inline void ehci_update_irq(EHCIState *s)
202 if ((s->usbsts & USBINTR_MASK) & s->usbintr) {
206 trace_usb_ehci_irq(level, s->frindex, s->usbsts, s->usbintr);
207 qemu_set_irq(s->irq, level);
210 /* flag interrupt condition */
211 static inline void ehci_raise_irq(EHCIState *s, int intr)
213 if (intr & (USBSTS_PCD | USBSTS_FLR | USBSTS_HSE)) {
217 s->usbsts_pending |= intr;
222 * Commit pending interrupts (added via ehci_raise_irq),
223 * at the rate allowed by "Interrupt Threshold Control".
225 static inline void ehci_commit_irq(EHCIState *s)
229 if (!s->usbsts_pending) {
232 if (s->usbsts_frindex > s->frindex) {
236 itc = (s->usbcmd >> 16) & 0xff;
237 s->usbsts |= s->usbsts_pending;
238 s->usbsts_pending = 0;
239 s->usbsts_frindex = s->frindex + itc;
243 static void ehci_update_halt(EHCIState *s)
245 if (s->usbcmd & USBCMD_RUNSTOP) {
246 ehci_clear_usbsts(s, USBSTS_HALT);
248 if (s->astate == EST_INACTIVE && s->pstate == EST_INACTIVE) {
249 ehci_set_usbsts(s, USBSTS_HALT);
254 static void ehci_set_state(EHCIState *s, int async, int state)
257 trace_usb_ehci_state("async", state2str(state));
259 if (s->astate == EST_INACTIVE) {
260 ehci_clear_usbsts(s, USBSTS_ASS);
263 ehci_set_usbsts(s, USBSTS_ASS);
266 trace_usb_ehci_state("periodic", state2str(state));
268 if (s->pstate == EST_INACTIVE) {
269 ehci_clear_usbsts(s, USBSTS_PSS);
272 ehci_set_usbsts(s, USBSTS_PSS);
277 static int ehci_get_state(EHCIState *s, int async)
279 return async ? s->astate : s->pstate;
282 static void ehci_set_fetch_addr(EHCIState *s, int async, uint32_t addr)
285 s->a_fetch_addr = addr;
287 s->p_fetch_addr = addr;
291 static int ehci_get_fetch_addr(EHCIState *s, int async)
293 return async ? s->a_fetch_addr : s->p_fetch_addr;
296 static void ehci_trace_qh(EHCIQueue *q, hwaddr addr, EHCIqh *qh)
298 /* need three here due to argument count limits */
299 trace_usb_ehci_qh_ptrs(q, addr, qh->next,
300 qh->current_qtd, qh->next_qtd, qh->altnext_qtd);
301 trace_usb_ehci_qh_fields(addr,
302 get_field(qh->epchar, QH_EPCHAR_RL),
303 get_field(qh->epchar, QH_EPCHAR_MPLEN),
304 get_field(qh->epchar, QH_EPCHAR_EPS),
305 get_field(qh->epchar, QH_EPCHAR_EP),
306 get_field(qh->epchar, QH_EPCHAR_DEVADDR));
307 trace_usb_ehci_qh_bits(addr,
308 (bool)(qh->epchar & QH_EPCHAR_C),
309 (bool)(qh->epchar & QH_EPCHAR_H),
310 (bool)(qh->epchar & QH_EPCHAR_DTC),
311 (bool)(qh->epchar & QH_EPCHAR_I));
314 static void ehci_trace_qtd(EHCIQueue *q, hwaddr addr, EHCIqtd *qtd)
316 /* need three here due to argument count limits */
317 trace_usb_ehci_qtd_ptrs(q, addr, qtd->next, qtd->altnext);
318 trace_usb_ehci_qtd_fields(addr,
319 get_field(qtd->token, QTD_TOKEN_TBYTES),
320 get_field(qtd->token, QTD_TOKEN_CPAGE),
321 get_field(qtd->token, QTD_TOKEN_CERR),
322 get_field(qtd->token, QTD_TOKEN_PID));
323 trace_usb_ehci_qtd_bits(addr,
324 (bool)(qtd->token & QTD_TOKEN_IOC),
325 (bool)(qtd->token & QTD_TOKEN_ACTIVE),
326 (bool)(qtd->token & QTD_TOKEN_HALT),
327 (bool)(qtd->token & QTD_TOKEN_BABBLE),
328 (bool)(qtd->token & QTD_TOKEN_XACTERR));
331 static void ehci_trace_itd(EHCIState *s, hwaddr addr, EHCIitd *itd)
333 trace_usb_ehci_itd(addr, itd->next,
334 get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT),
335 get_field(itd->bufptr[2], ITD_BUFPTR_MULT),
336 get_field(itd->bufptr[0], ITD_BUFPTR_EP),
337 get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR));
340 static void ehci_trace_sitd(EHCIState *s, hwaddr addr,
343 trace_usb_ehci_sitd(addr, sitd->next,
344 (bool)(sitd->results & SITD_RESULTS_ACTIVE));
347 static void ehci_trace_guest_bug(EHCIState *s, const char *message)
349 trace_usb_ehci_guest_bug(message);
350 fprintf(stderr, "ehci warning: %s\n", message);
353 static inline bool ehci_enabled(EHCIState *s)
355 return s->usbcmd & USBCMD_RUNSTOP;
358 static inline bool ehci_async_enabled(EHCIState *s)
360 return ehci_enabled(s) && (s->usbcmd & USBCMD_ASE);
363 static inline bool ehci_periodic_enabled(EHCIState *s)
365 return ehci_enabled(s) && (s->usbcmd & USBCMD_PSE);
368 /* Get an array of dwords from main memory */
369 static inline int get_dwords(EHCIState *ehci, uint32_t addr,
370 uint32_t *buf, int num)
375 ehci_raise_irq(ehci, USBSTS_HSE);
376 ehci->usbcmd &= ~USBCMD_RUNSTOP;
377 trace_usb_ehci_dma_error();
381 for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
382 dma_memory_read(ehci->as, addr, buf, sizeof(*buf));
383 *buf = le32_to_cpu(*buf);
389 /* Put an array of dwords in to main memory */
390 static inline int put_dwords(EHCIState *ehci, uint32_t addr,
391 uint32_t *buf, int num)
396 ehci_raise_irq(ehci, USBSTS_HSE);
397 ehci->usbcmd &= ~USBCMD_RUNSTOP;
398 trace_usb_ehci_dma_error();
402 for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
403 uint32_t tmp = cpu_to_le32(*buf);
404 dma_memory_write(ehci->as, addr, &tmp, sizeof(tmp));
410 static int ehci_get_pid(EHCIqtd *qtd)
412 switch (get_field(qtd->token, QTD_TOKEN_PID)) {
414 return USB_TOKEN_OUT;
418 return USB_TOKEN_SETUP;
420 fprintf(stderr, "bad token\n");
425 static bool ehci_verify_qh(EHCIQueue *q, EHCIqh *qh)
427 uint32_t devaddr = get_field(qh->epchar, QH_EPCHAR_DEVADDR);
428 uint32_t endp = get_field(qh->epchar, QH_EPCHAR_EP);
429 if ((devaddr != get_field(q->qh.epchar, QH_EPCHAR_DEVADDR)) ||
430 (endp != get_field(q->qh.epchar, QH_EPCHAR_EP)) ||
431 (qh->current_qtd != q->qh.current_qtd) ||
432 (q->async && qh->next_qtd != q->qh.next_qtd) ||
433 (memcmp(&qh->altnext_qtd, &q->qh.altnext_qtd,
434 7 * sizeof(uint32_t)) != 0) ||
435 (q->dev != NULL && q->dev->addr != devaddr)) {
442 static bool ehci_verify_qtd(EHCIPacket *p, EHCIqtd *qtd)
444 if (p->qtdaddr != p->queue->qtdaddr ||
445 (p->queue->async && !NLPTR_TBIT(p->qtd.next) &&
446 (p->qtd.next != qtd->next)) ||
447 (!NLPTR_TBIT(p->qtd.altnext) && (p->qtd.altnext != qtd->altnext)) ||
448 p->qtd.token != qtd->token ||
449 p->qtd.bufptr[0] != qtd->bufptr[0]) {
456 static bool ehci_verify_pid(EHCIQueue *q, EHCIqtd *qtd)
458 int ep = get_field(q->qh.epchar, QH_EPCHAR_EP);
459 int pid = ehci_get_pid(qtd);
461 /* Note the pid changing is normal for ep 0 (the control ep) */
462 if (q->last_pid && ep != 0 && pid != q->last_pid) {
469 /* Finish executing and writeback a packet outside of the regular
470 fetchqh -> fetchqtd -> execute -> writeback cycle */
471 static void ehci_writeback_async_complete_packet(EHCIPacket *p)
473 EHCIQueue *q = p->queue;
478 /* Verify the qh + qtd, like we do when going through fetchqh & fetchqtd */
479 get_dwords(q->ehci, NLPTR_GET(q->qhaddr),
480 (uint32_t *) &qh, sizeof(EHCIqh) >> 2);
481 get_dwords(q->ehci, NLPTR_GET(q->qtdaddr),
482 (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2);
483 if (!ehci_verify_qh(q, &qh) || !ehci_verify_qtd(p, &qtd)) {
484 p->async = EHCI_ASYNC_INITIALIZED;
489 state = ehci_get_state(q->ehci, q->async);
490 ehci_state_executing(q);
491 ehci_state_writeback(q); /* Frees the packet! */
492 if (!(q->qh.token & QTD_TOKEN_HALT)) {
493 ehci_state_advqueue(q);
495 ehci_set_state(q->ehci, q->async, state);
498 /* packet management */
500 static EHCIPacket *ehci_alloc_packet(EHCIQueue *q)
504 p = g_new0(EHCIPacket, 1);
506 usb_packet_init(&p->packet);
507 QTAILQ_INSERT_TAIL(&q->packets, p, next);
508 trace_usb_ehci_packet_action(p->queue, p, "alloc");
512 static void ehci_free_packet(EHCIPacket *p)
514 if (p->async == EHCI_ASYNC_FINISHED &&
515 !(p->queue->qh.token & QTD_TOKEN_HALT)) {
516 ehci_writeback_async_complete_packet(p);
519 trace_usb_ehci_packet_action(p->queue, p, "free");
520 if (p->async == EHCI_ASYNC_INFLIGHT) {
521 usb_cancel_packet(&p->packet);
523 if (p->async == EHCI_ASYNC_FINISHED &&
524 p->packet.status == USB_RET_SUCCESS) {
526 "EHCI: Dropping completed packet from halted %s ep %02X\n",
527 (p->pid == USB_TOKEN_IN) ? "in" : "out",
528 get_field(p->queue->qh.epchar, QH_EPCHAR_EP));
530 if (p->async != EHCI_ASYNC_NONE) {
531 usb_packet_unmap(&p->packet, &p->sgl);
532 qemu_sglist_destroy(&p->sgl);
534 QTAILQ_REMOVE(&p->queue->packets, p, next);
535 usb_packet_cleanup(&p->packet);
539 /* queue management */
541 static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, uint32_t addr, int async)
543 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
546 q = g_malloc0(sizeof(*q));
550 QTAILQ_INIT(&q->packets);
551 QTAILQ_INSERT_HEAD(head, q, next);
552 trace_usb_ehci_queue_action(q, "alloc");
556 static void ehci_queue_stopped(EHCIQueue *q)
558 int endp = get_field(q->qh.epchar, QH_EPCHAR_EP);
560 if (!q->last_pid || !q->dev) {
564 usb_device_ep_stopped(q->dev, usb_ep_get(q->dev, q->last_pid, endp));
567 static int ehci_cancel_queue(EHCIQueue *q)
572 p = QTAILQ_FIRST(&q->packets);
577 trace_usb_ehci_queue_action(q, "cancel");
581 } while ((p = QTAILQ_FIRST(&q->packets)) != NULL);
584 ehci_queue_stopped(q);
588 static int ehci_reset_queue(EHCIQueue *q)
592 trace_usb_ehci_queue_action(q, "reset");
593 packets = ehci_cancel_queue(q);
600 static void ehci_free_queue(EHCIQueue *q, const char *warn)
602 EHCIQueueHead *head = q->async ? &q->ehci->aqueues : &q->ehci->pqueues;
605 trace_usb_ehci_queue_action(q, "free");
606 cancelled = ehci_cancel_queue(q);
607 if (warn && cancelled > 0) {
608 ehci_trace_guest_bug(q->ehci, warn);
610 QTAILQ_REMOVE(head, q, next);
614 static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr,
617 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
620 QTAILQ_FOREACH(q, head, next) {
621 if (addr == q->qhaddr) {
628 static void ehci_queues_rip_unused(EHCIState *ehci, int async)
630 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
631 const char *warn = async ? "guest unlinked busy QH" : NULL;
632 uint64_t maxage = FRAME_TIMER_NS * ehci->maxframes * 4;
635 QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
638 q->ts = ehci->last_run_ns;
641 if (ehci->last_run_ns < q->ts + maxage) {
644 ehci_free_queue(q, warn);
648 static void ehci_queues_rip_unseen(EHCIState *ehci, int async)
650 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
653 QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
655 ehci_free_queue(q, NULL);
660 static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async)
662 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
665 QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
669 ehci_free_queue(q, NULL);
673 static void ehci_queues_rip_all(EHCIState *ehci, int async)
675 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
676 const char *warn = async ? "guest stopped busy async schedule" : NULL;
679 QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
680 ehci_free_queue(q, warn);
684 /* Attach or detach a device on root hub */
686 static void ehci_attach(USBPort *port)
688 EHCIState *s = port->opaque;
689 uint32_t *portsc = &s->portsc[port->index];
690 const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci";
692 trace_usb_ehci_port_attach(port->index, owner, port->dev->product_desc);
694 if (*portsc & PORTSC_POWNER) {
695 USBPort *companion = s->companion_ports[port->index];
696 companion->dev = port->dev;
697 companion->ops->attach(companion);
701 *portsc |= PORTSC_CONNECT;
702 *portsc |= PORTSC_CSC;
704 ehci_raise_irq(s, USBSTS_PCD);
707 static void ehci_detach(USBPort *port)
709 EHCIState *s = port->opaque;
710 uint32_t *portsc = &s->portsc[port->index];
711 const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci";
713 trace_usb_ehci_port_detach(port->index, owner);
715 if (*portsc & PORTSC_POWNER) {
716 USBPort *companion = s->companion_ports[port->index];
717 companion->ops->detach(companion);
718 companion->dev = NULL;
720 * EHCI spec 4.2.2: "When a disconnect occurs... On the event,
721 * the port ownership is returned immediately to the EHCI controller."
723 *portsc &= ~PORTSC_POWNER;
727 ehci_queues_rip_device(s, port->dev, 0);
728 ehci_queues_rip_device(s, port->dev, 1);
730 *portsc &= ~(PORTSC_CONNECT|PORTSC_PED|PORTSC_SUSPEND);
731 *portsc |= PORTSC_CSC;
733 ehci_raise_irq(s, USBSTS_PCD);
736 static void ehci_child_detach(USBPort *port, USBDevice *child)
738 EHCIState *s = port->opaque;
739 uint32_t portsc = s->portsc[port->index];
741 if (portsc & PORTSC_POWNER) {
742 USBPort *companion = s->companion_ports[port->index];
743 companion->ops->child_detach(companion, child);
747 ehci_queues_rip_device(s, child, 0);
748 ehci_queues_rip_device(s, child, 1);
751 static void ehci_wakeup(USBPort *port)
753 EHCIState *s = port->opaque;
754 uint32_t *portsc = &s->portsc[port->index];
756 if (*portsc & PORTSC_POWNER) {
757 USBPort *companion = s->companion_ports[port->index];
758 if (companion->ops->wakeup) {
759 companion->ops->wakeup(companion);
764 if (*portsc & PORTSC_SUSPEND) {
765 trace_usb_ehci_port_wakeup(port->index);
766 *portsc |= PORTSC_FPRES;
767 ehci_raise_irq(s, USBSTS_PCD);
770 qemu_bh_schedule(s->async_bh);
773 static void ehci_register_companion(USBBus *bus, USBPort *ports[],
774 uint32_t portcount, uint32_t firstport,
777 EHCIState *s = container_of(bus, EHCIState, bus);
780 if (firstport + portcount > NB_PORTS) {
781 error_setg(errp, "firstport must be between 0 and %u",
782 NB_PORTS - portcount);
786 for (i = 0; i < portcount; i++) {
787 if (s->companion_ports[firstport + i]) {
788 error_setg(errp, "firstport %u asks for ports %u-%u,"
789 " but port %u has a companion assigned already",
790 firstport, firstport, firstport + portcount - 1,
796 for (i = 0; i < portcount; i++) {
797 s->companion_ports[firstport + i] = ports[i];
798 s->ports[firstport + i].speedmask |=
799 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL;
800 /* Ensure devs attached before the initial reset go to the companion */
801 s->portsc[firstport + i] = PORTSC_POWNER;
804 s->companion_count++;
805 s->caps[0x05] = (s->companion_count << 4) | portcount;
808 static void ehci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep,
811 EHCIState *s = container_of(bus, EHCIState, bus);
812 uint32_t portsc = s->portsc[ep->dev->port->index];
814 if (portsc & PORTSC_POWNER) {
818 s->periodic_sched_active = PERIODIC_ACTIVE;
819 qemu_bh_schedule(s->async_bh);
822 static USBDevice *ehci_find_device(EHCIState *ehci, uint8_t addr)
828 for (i = 0; i < NB_PORTS; i++) {
829 port = &ehci->ports[i];
830 if (!(ehci->portsc[i] & PORTSC_PED)) {
831 DPRINTF("Port %d not enabled\n", i);
834 dev = usb_find_device(port, addr);
842 /* 4.1 host controller initialization */
843 void ehci_reset(void *opaque)
845 EHCIState *s = opaque;
847 USBDevice *devs[NB_PORTS];
849 trace_usb_ehci_reset();
852 * Do the detach before touching portsc, so that it correctly gets send to
853 * us or to our companion based on PORTSC_POWNER before the reset.
855 for(i = 0; i < NB_PORTS; i++) {
856 devs[i] = s->ports[i].dev;
857 if (devs[i] && devs[i]->attached) {
858 usb_detach(&s->ports[i]);
862 memset(&s->opreg, 0x00, sizeof(s->opreg));
863 memset(&s->portsc, 0x00, sizeof(s->portsc));
865 s->usbcmd = NB_MAXINTRATE << USBCMD_ITC_SH;
866 s->usbsts = USBSTS_HALT;
867 s->usbsts_pending = 0;
868 s->usbsts_frindex = 0;
871 s->astate = EST_INACTIVE;
872 s->pstate = EST_INACTIVE;
874 for(i = 0; i < NB_PORTS; i++) {
875 if (s->companion_ports[i]) {
876 s->portsc[i] = PORTSC_POWNER | PORTSC_PPOWER;
878 s->portsc[i] = PORTSC_PPOWER;
880 if (devs[i] && devs[i]->attached) {
881 usb_attach(&s->ports[i]);
882 usb_device_reset(devs[i]);
885 ehci_queues_rip_all(s, 0);
886 ehci_queues_rip_all(s, 1);
887 timer_del(s->frame_timer);
888 qemu_bh_cancel(s->async_bh);
891 static uint64_t ehci_caps_read(void *ptr, hwaddr addr,
895 return s->caps[addr];
898 static void ehci_caps_write(void *ptr, hwaddr addr,
899 uint64_t val, unsigned size)
903 static uint64_t ehci_opreg_read(void *ptr, hwaddr addr,
911 /* Round down to mult of 8, else it can go backwards on migration */
912 val = s->frindex & ~7;
915 val = s->opreg[addr >> 2];
918 trace_usb_ehci_opreg_read(addr + s->opregbase, addr2str(addr), val);
922 static uint64_t ehci_port_read(void *ptr, hwaddr addr,
928 val = s->portsc[addr >> 2];
929 trace_usb_ehci_portsc_read(addr + s->portscbase, addr >> 2, val);
933 static void handle_port_owner_write(EHCIState *s, int port, uint32_t owner)
935 USBDevice *dev = s->ports[port].dev;
936 uint32_t *portsc = &s->portsc[port];
939 if (s->companion_ports[port] == NULL)
942 owner = owner & PORTSC_POWNER;
943 orig = *portsc & PORTSC_POWNER;
945 if (!(owner ^ orig)) {
949 if (dev && dev->attached) {
950 usb_detach(&s->ports[port]);
953 *portsc &= ~PORTSC_POWNER;
956 if (dev && dev->attached) {
957 usb_attach(&s->ports[port]);
961 static void ehci_port_write(void *ptr, hwaddr addr,
962 uint64_t val, unsigned size)
965 int port = addr >> 2;
966 uint32_t *portsc = &s->portsc[port];
967 uint32_t old = *portsc;
968 USBDevice *dev = s->ports[port].dev;
970 trace_usb_ehci_portsc_write(addr + s->portscbase, addr >> 2, val);
973 *portsc &= ~(val & PORTSC_RWC_MASK);
974 /* The guest may clear, but not set the PED bit */
975 *portsc &= val | ~PORTSC_PED;
976 /* POWNER is masked out by RO_MASK as it is RO when we've no companion */
977 handle_port_owner_write(s, port, val);
978 /* And finally apply RO_MASK */
979 val &= PORTSC_RO_MASK;
981 if ((val & PORTSC_PRESET) && !(*portsc & PORTSC_PRESET)) {
982 trace_usb_ehci_port_reset(port, 1);
985 if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) {
986 trace_usb_ehci_port_reset(port, 0);
987 if (dev && dev->attached) {
988 usb_port_reset(&s->ports[port]);
989 *portsc &= ~PORTSC_CSC;
993 * Table 2.16 Set the enable bit(and enable bit change) to indicate
994 * to SW that this port has a high speed device attached
996 if (dev && dev->attached && (dev->speedmask & USB_SPEED_MASK_HIGH)) {
1001 if ((val & PORTSC_SUSPEND) && !(*portsc & PORTSC_SUSPEND)) {
1002 trace_usb_ehci_port_suspend(port);
1004 if (!(val & PORTSC_FPRES) && (*portsc & PORTSC_FPRES)) {
1005 trace_usb_ehci_port_resume(port);
1006 val &= ~PORTSC_SUSPEND;
1009 *portsc &= ~PORTSC_RO_MASK;
1011 trace_usb_ehci_portsc_change(addr + s->portscbase, addr >> 2, *portsc, old);
1014 static void ehci_opreg_write(void *ptr, hwaddr addr,
1015 uint64_t val, unsigned size)
1018 uint32_t *mmio = s->opreg + (addr >> 2);
1019 uint32_t old = *mmio;
1022 trace_usb_ehci_opreg_write(addr + s->opregbase, addr2str(addr), val);
1026 if (val & USBCMD_HCRESET) {
1032 /* not supporting dynamic frame list size at the moment */
1033 if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) {
1034 fprintf(stderr, "attempt to set frame list size -- value %d\n",
1035 (int)val & USBCMD_FLS);
1039 if (val & USBCMD_IAAD) {
1041 * Process IAAD immediately, otherwise the Linux IAAD watchdog may
1042 * trigger and re-use a qh without us seeing the unlink.
1044 s->async_stepdown = 0;
1045 qemu_bh_schedule(s->async_bh);
1046 trace_usb_ehci_doorbell_ring();
1049 if (((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & val) !=
1050 ((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & s->usbcmd)) {
1051 if (s->pstate == EST_INACTIVE) {
1052 SET_LAST_RUN_CLOCK(s);
1054 s->usbcmd = val; /* Set usbcmd for ehci_update_halt() */
1055 ehci_update_halt(s);
1056 s->async_stepdown = 0;
1057 qemu_bh_schedule(s->async_bh);
1062 val &= USBSTS_RO_MASK; // bits 6 through 31 are RO
1063 ehci_clear_usbsts(s, val); // bits 0 through 5 are R/WC
1069 val &= USBINTR_MASK;
1070 if (ehci_enabled(s) && (USBSTS_FLR & val)) {
1071 qemu_bh_schedule(s->async_bh);
1076 val &= 0x00003fff; /* frindex is 14bits */
1077 s->usbsts_frindex = val;
1083 for(i = 0; i < NB_PORTS; i++)
1084 handle_port_owner_write(s, i, 0);
1088 case PERIODICLISTBASE:
1089 if (ehci_periodic_enabled(s)) {
1091 "ehci: PERIODIC list base register set while periodic schedule\n"
1092 " is enabled and HC is enabled\n");
1097 if (ehci_async_enabled(s)) {
1099 "ehci: ASYNC list address register set while async schedule\n"
1100 " is enabled and HC is enabled\n");
1106 trace_usb_ehci_opreg_change(addr + s->opregbase, addr2str(addr),
1111 * Write the qh back to guest physical memory. This step isn't
1112 * in the EHCI spec but we need to do it since we don't share
1113 * physical memory with our guest VM.
1115 * The first three dwords are read-only for the EHCI, so skip them
1116 * when writing back the qh.
1118 static void ehci_flush_qh(EHCIQueue *q)
1120 uint32_t *qh = (uint32_t *) &q->qh;
1121 uint32_t dwords = sizeof(EHCIqh) >> 2;
1122 uint32_t addr = NLPTR_GET(q->qhaddr);
1124 put_dwords(q->ehci, addr + 3 * sizeof(uint32_t), qh + 3, dwords - 3);
1129 static int ehci_qh_do_overlay(EHCIQueue *q)
1131 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
1139 assert(p->qtdaddr == q->qtdaddr);
1141 // remember values in fields to preserve in qh after overlay
1143 dtoggle = q->qh.token & QTD_TOKEN_DTOGGLE;
1144 ping = q->qh.token & QTD_TOKEN_PING;
1146 q->qh.current_qtd = p->qtdaddr;
1147 q->qh.next_qtd = p->qtd.next;
1148 q->qh.altnext_qtd = p->qtd.altnext;
1149 q->qh.token = p->qtd.token;
1152 eps = get_field(q->qh.epchar, QH_EPCHAR_EPS);
1153 if (eps == EHCI_QH_EPS_HIGH) {
1154 q->qh.token &= ~QTD_TOKEN_PING;
1155 q->qh.token |= ping;
1158 reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
1159 set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
1161 for (i = 0; i < 5; i++) {
1162 q->qh.bufptr[i] = p->qtd.bufptr[i];
1165 if (!(q->qh.epchar & QH_EPCHAR_DTC)) {
1166 // preserve QH DT bit
1167 q->qh.token &= ~QTD_TOKEN_DTOGGLE;
1168 q->qh.token |= dtoggle;
1171 q->qh.bufptr[1] &= ~BUFPTR_CPROGMASK_MASK;
1172 q->qh.bufptr[2] &= ~BUFPTR_FRAMETAG_MASK;
1179 static int ehci_init_transfer(EHCIPacket *p)
1181 uint32_t cpage, offset, bytes, plen;
1184 cpage = get_field(p->qtd.token, QTD_TOKEN_CPAGE);
1185 bytes = get_field(p->qtd.token, QTD_TOKEN_TBYTES);
1186 offset = p->qtd.bufptr[0] & ~QTD_BUFPTR_MASK;
1187 qemu_sglist_init(&p->sgl, p->queue->ehci->device, 5, p->queue->ehci->as);
1191 fprintf(stderr, "cpage out of range (%d)\n", cpage);
1195 page = p->qtd.bufptr[cpage] & QTD_BUFPTR_MASK;
1198 if (plen > 4096 - offset) {
1199 plen = 4096 - offset;
1204 qemu_sglist_add(&p->sgl, page, plen);
1210 static void ehci_finish_transfer(EHCIQueue *q, int len)
1212 uint32_t cpage, offset;
1215 /* update cpage & offset */
1216 cpage = get_field(q->qh.token, QTD_TOKEN_CPAGE);
1217 offset = q->qh.bufptr[0] & ~QTD_BUFPTR_MASK;
1220 cpage += offset >> QTD_BUFPTR_SH;
1221 offset &= ~QTD_BUFPTR_MASK;
1223 set_field(&q->qh.token, cpage, QTD_TOKEN_CPAGE);
1224 q->qh.bufptr[0] &= QTD_BUFPTR_MASK;
1225 q->qh.bufptr[0] |= offset;
1229 static void ehci_async_complete_packet(USBPort *port, USBPacket *packet)
1232 EHCIState *s = port->opaque;
1233 uint32_t portsc = s->portsc[port->index];
1235 if (portsc & PORTSC_POWNER) {
1236 USBPort *companion = s->companion_ports[port->index];
1237 companion->ops->complete(companion, packet);
1241 p = container_of(packet, EHCIPacket, packet);
1242 assert(p->async == EHCI_ASYNC_INFLIGHT);
1244 if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
1245 trace_usb_ehci_packet_action(p->queue, p, "remove");
1246 ehci_free_packet(p);
1250 trace_usb_ehci_packet_action(p->queue, p, "wakeup");
1251 p->async = EHCI_ASYNC_FINISHED;
1253 if (!p->queue->async) {
1254 s->periodic_sched_active = PERIODIC_ACTIVE;
1256 qemu_bh_schedule(s->async_bh);
1259 static void ehci_execute_complete(EHCIQueue *q)
1261 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
1265 assert(p->qtdaddr == q->qtdaddr);
1266 assert(p->async == EHCI_ASYNC_INITIALIZED ||
1267 p->async == EHCI_ASYNC_FINISHED);
1269 DPRINTF("execute_complete: qhaddr 0x%x, next 0x%x, qtdaddr 0x%x, "
1270 "status %d, actual_length %d\n",
1271 q->qhaddr, q->qh.next, q->qtdaddr,
1272 p->packet.status, p->packet.actual_length);
1274 switch (p->packet.status) {
1275 case USB_RET_SUCCESS:
1277 case USB_RET_IOERROR:
1279 q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR);
1280 set_field(&q->qh.token, 0, QTD_TOKEN_CERR);
1281 ehci_raise_irq(q->ehci, USBSTS_ERRINT);
1284 q->qh.token |= QTD_TOKEN_HALT;
1285 ehci_raise_irq(q->ehci, USBSTS_ERRINT);
1288 set_field(&q->qh.altnext_qtd, 0, QH_ALTNEXT_NAKCNT);
1289 return; /* We're not done yet with this transaction */
1290 case USB_RET_BABBLE:
1291 q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE);
1292 ehci_raise_irq(q->ehci, USBSTS_ERRINT);
1295 /* should not be triggerable */
1296 fprintf(stderr, "USB invalid response %d\n", p->packet.status);
1297 g_assert_not_reached();
1301 /* TODO check 4.12 for splits */
1302 tbytes = get_field(q->qh.token, QTD_TOKEN_TBYTES);
1303 if (tbytes && p->pid == USB_TOKEN_IN) {
1304 tbytes -= p->packet.actual_length;
1306 /* 4.15.1.2 must raise int on a short input packet */
1307 ehci_raise_irq(q->ehci, USBSTS_INT);
1309 q->ehci->int_req_by_async = true;
1315 DPRINTF("updating tbytes to %d\n", tbytes);
1316 set_field(&q->qh.token, tbytes, QTD_TOKEN_TBYTES);
1318 ehci_finish_transfer(q, p->packet.actual_length);
1319 usb_packet_unmap(&p->packet, &p->sgl);
1320 qemu_sglist_destroy(&p->sgl);
1321 p->async = EHCI_ASYNC_NONE;
1323 q->qh.token ^= QTD_TOKEN_DTOGGLE;
1324 q->qh.token &= ~QTD_TOKEN_ACTIVE;
1326 if (q->qh.token & QTD_TOKEN_IOC) {
1327 ehci_raise_irq(q->ehci, USBSTS_INT);
1329 q->ehci->int_req_by_async = true;
1334 /* 4.10.3 returns "again" */
1335 static int ehci_execute(EHCIPacket *p, const char *action)
1341 assert(p->async == EHCI_ASYNC_NONE ||
1342 p->async == EHCI_ASYNC_INITIALIZED);
1344 if (!(p->qtd.token & QTD_TOKEN_ACTIVE)) {
1345 fprintf(stderr, "Attempting to execute inactive qtd\n");
1349 if (get_field(p->qtd.token, QTD_TOKEN_TBYTES) > BUFF_SIZE) {
1350 ehci_trace_guest_bug(p->queue->ehci,
1351 "guest requested more bytes than allowed");
1355 if (!ehci_verify_pid(p->queue, &p->qtd)) {
1356 ehci_queue_stopped(p->queue); /* Mark the ep in the prev dir stopped */
1358 p->pid = ehci_get_pid(&p->qtd);
1359 p->queue->last_pid = p->pid;
1360 endp = get_field(p->queue->qh.epchar, QH_EPCHAR_EP);
1361 ep = usb_ep_get(p->queue->dev, p->pid, endp);
1363 if (p->async == EHCI_ASYNC_NONE) {
1364 if (ehci_init_transfer(p) != 0) {
1368 spd = (p->pid == USB_TOKEN_IN && NLPTR_TBIT(p->qtd.altnext) == 0);
1369 usb_packet_setup(&p->packet, p->pid, ep, 0, p->qtdaddr, spd,
1370 (p->qtd.token & QTD_TOKEN_IOC) != 0);
1371 usb_packet_map(&p->packet, &p->sgl);
1372 p->async = EHCI_ASYNC_INITIALIZED;
1375 trace_usb_ehci_packet_action(p->queue, p, action);
1376 usb_handle_packet(p->queue->dev, &p->packet);
1377 DPRINTF("submit: qh 0x%x next 0x%x qtd 0x%x pid 0x%x len %zd endp 0x%x "
1378 "status %d actual_length %d\n", p->queue->qhaddr, p->qtd.next,
1379 p->qtdaddr, p->pid, p->packet.iov.size, endp, p->packet.status,
1380 p->packet.actual_length);
1382 if (p->packet.actual_length > BUFF_SIZE) {
1383 fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n");
1393 static int ehci_process_itd(EHCIState *ehci,
1399 uint32_t i, len, pid, dir, devaddr, endp, xfers = 0;
1400 uint32_t pg, off, ptr1, ptr2, max, mult;
1402 ehci->periodic_sched_active = PERIODIC_ACTIVE;
1404 dir =(itd->bufptr[1] & ITD_BUFPTR_DIRECTION);
1405 devaddr = get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR);
1406 endp = get_field(itd->bufptr[0], ITD_BUFPTR_EP);
1407 max = get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT);
1408 mult = get_field(itd->bufptr[2], ITD_BUFPTR_MULT);
1410 for(i = 0; i < 8; i++) {
1411 if (itd->transact[i] & ITD_XACT_ACTIVE) {
1412 pg = get_field(itd->transact[i], ITD_XACT_PGSEL);
1413 off = itd->transact[i] & ITD_XACT_OFFSET_MASK;
1414 len = get_field(itd->transact[i], ITD_XACT_LENGTH);
1416 if (len > max * mult) {
1419 if (len > BUFF_SIZE || pg > 6) {
1423 ptr1 = (itd->bufptr[pg] & ITD_BUFPTR_MASK);
1424 qemu_sglist_init(&ehci->isgl, ehci->device, 2, ehci->as);
1425 if (off + len > 4096) {
1426 /* transfer crosses page border */
1428 return -1; /* avoid page pg + 1 */
1430 ptr2 = (itd->bufptr[pg + 1] & ITD_BUFPTR_MASK);
1431 uint32_t len2 = off + len - 4096;
1432 uint32_t len1 = len - len2;
1433 qemu_sglist_add(&ehci->isgl, ptr1 + off, len1);
1434 qemu_sglist_add(&ehci->isgl, ptr2, len2);
1436 qemu_sglist_add(&ehci->isgl, ptr1 + off, len);
1439 pid = dir ? USB_TOKEN_IN : USB_TOKEN_OUT;
1441 dev = ehci_find_device(ehci, devaddr);
1442 ep = usb_ep_get(dev, pid, endp);
1443 if (ep && ep->type == USB_ENDPOINT_XFER_ISOC) {
1444 usb_packet_setup(&ehci->ipacket, pid, ep, 0, addr, false,
1445 (itd->transact[i] & ITD_XACT_IOC) != 0);
1446 usb_packet_map(&ehci->ipacket, &ehci->isgl);
1447 usb_handle_packet(dev, &ehci->ipacket);
1448 usb_packet_unmap(&ehci->ipacket, &ehci->isgl);
1450 DPRINTF("ISOCH: attempt to addess non-iso endpoint\n");
1451 ehci->ipacket.status = USB_RET_NAK;
1452 ehci->ipacket.actual_length = 0;
1454 qemu_sglist_destroy(&ehci->isgl);
1456 switch (ehci->ipacket.status) {
1457 case USB_RET_SUCCESS:
1460 fprintf(stderr, "Unexpected iso usb result: %d\n",
1461 ehci->ipacket.status);
1463 case USB_RET_IOERROR:
1465 /* 3.3.2: XACTERR is only allowed on IN transactions */
1467 itd->transact[i] |= ITD_XACT_XACTERR;
1468 ehci_raise_irq(ehci, USBSTS_ERRINT);
1471 case USB_RET_BABBLE:
1472 itd->transact[i] |= ITD_XACT_BABBLE;
1473 ehci_raise_irq(ehci, USBSTS_ERRINT);
1476 /* no data for us, so do a zero-length transfer */
1477 ehci->ipacket.actual_length = 0;
1481 set_field(&itd->transact[i], len - ehci->ipacket.actual_length,
1482 ITD_XACT_LENGTH); /* OUT */
1484 set_field(&itd->transact[i], ehci->ipacket.actual_length,
1485 ITD_XACT_LENGTH); /* IN */
1487 if (itd->transact[i] & ITD_XACT_IOC) {
1488 ehci_raise_irq(ehci, USBSTS_INT);
1490 itd->transact[i] &= ~ITD_XACT_ACTIVE;
1494 return xfers ? 0 : -1;
1498 /* This state is the entry point for asynchronous schedule
1499 * processing. Entry here consitutes a EHCI start event state (4.8.5)
1501 static int ehci_state_waitlisthead(EHCIState *ehci, int async)
1506 uint32_t entry = ehci->asynclistaddr;
1508 /* set reclamation flag at start event (4.8.6) */
1510 ehci_set_usbsts(ehci, USBSTS_REC);
1513 ehci_queues_rip_unused(ehci, async);
1515 /* Find the head of the list (4.9.1.1) */
1516 for(i = 0; i < MAX_QH; i++) {
1517 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &qh,
1518 sizeof(EHCIqh) >> 2) < 0) {
1521 ehci_trace_qh(NULL, NLPTR_GET(entry), &qh);
1523 if (qh.epchar & QH_EPCHAR_H) {
1525 entry |= (NLPTR_TYPE_QH << 1);
1528 ehci_set_fetch_addr(ehci, async, entry);
1529 ehci_set_state(ehci, async, EST_FETCHENTRY);
1535 if (entry == ehci->asynclistaddr) {
1540 /* no head found for list. */
1542 ehci_set_state(ehci, async, EST_ACTIVE);
1549 /* This state is the entry point for periodic schedule processing as
1550 * well as being a continuation state for async processing.
1552 static int ehci_state_fetchentry(EHCIState *ehci, int async)
1555 uint32_t entry = ehci_get_fetch_addr(ehci, async);
1557 if (NLPTR_TBIT(entry)) {
1558 ehci_set_state(ehci, async, EST_ACTIVE);
1562 /* section 4.8, only QH in async schedule */
1563 if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) {
1564 fprintf(stderr, "non queue head request in async schedule\n");
1568 switch (NLPTR_TYPE_GET(entry)) {
1570 ehci_set_state(ehci, async, EST_FETCHQH);
1574 case NLPTR_TYPE_ITD:
1575 ehci_set_state(ehci, async, EST_FETCHITD);
1579 case NLPTR_TYPE_STITD:
1580 ehci_set_state(ehci, async, EST_FETCHSITD);
1585 /* TODO: handle FSTN type */
1586 fprintf(stderr, "FETCHENTRY: entry at %X is of type %d "
1587 "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry));
1595 static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async)
1601 entry = ehci_get_fetch_addr(ehci, async);
1602 q = ehci_find_queue_by_qh(ehci, entry, async);
1604 q = ehci_alloc_queue(ehci, entry, async);
1609 /* we are going in circles -- stop processing */
1610 ehci_set_state(ehci, async, EST_ACTIVE);
1615 if (get_dwords(ehci, NLPTR_GET(q->qhaddr),
1616 (uint32_t *) &qh, sizeof(EHCIqh) >> 2) < 0) {
1620 ehci_trace_qh(q, NLPTR_GET(q->qhaddr), &qh);
1623 * The overlay area of the qh should never be changed by the guest,
1624 * except when idle, in which case the reset is a nop.
1626 if (!ehci_verify_qh(q, &qh)) {
1627 if (ehci_reset_queue(q) > 0) {
1628 ehci_trace_guest_bug(ehci, "guest updated active QH");
1633 q->transact_ctr = get_field(q->qh.epcap, QH_EPCAP_MULT);
1634 if (q->transact_ctr == 0) { /* Guest bug in some versions of windows */
1635 q->transact_ctr = 4;
1638 if (q->dev == NULL) {
1639 q->dev = ehci_find_device(q->ehci,
1640 get_field(q->qh.epchar, QH_EPCHAR_DEVADDR));
1643 if (async && (q->qh.epchar & QH_EPCHAR_H)) {
1645 /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */
1646 if (ehci->usbsts & USBSTS_REC) {
1647 ehci_clear_usbsts(ehci, USBSTS_REC);
1649 DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset"
1650 " - done processing\n", q->qhaddr);
1651 ehci_set_state(ehci, async, EST_ACTIVE);
1658 if (q->qhaddr != q->qh.next) {
1659 DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n",
1661 q->qh.epchar & QH_EPCHAR_H,
1662 q->qh.token & QTD_TOKEN_HALT,
1663 q->qh.token & QTD_TOKEN_ACTIVE,
1668 if (q->qh.token & QTD_TOKEN_HALT) {
1669 ehci_set_state(ehci, async, EST_HORIZONTALQH);
1671 } else if ((q->qh.token & QTD_TOKEN_ACTIVE) &&
1672 (NLPTR_TBIT(q->qh.current_qtd) == 0)) {
1673 q->qtdaddr = q->qh.current_qtd;
1674 ehci_set_state(ehci, async, EST_FETCHQTD);
1677 /* EHCI spec version 1.0 Section 4.10.2 */
1678 ehci_set_state(ehci, async, EST_ADVANCEQUEUE);
1685 static int ehci_state_fetchitd(EHCIState *ehci, int async)
1691 entry = ehci_get_fetch_addr(ehci, async);
1693 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
1694 sizeof(EHCIitd) >> 2) < 0) {
1697 ehci_trace_itd(ehci, entry, &itd);
1699 if (ehci_process_itd(ehci, &itd, entry) != 0) {
1703 put_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
1704 sizeof(EHCIitd) >> 2);
1705 ehci_set_fetch_addr(ehci, async, itd.next);
1706 ehci_set_state(ehci, async, EST_FETCHENTRY);
1711 static int ehci_state_fetchsitd(EHCIState *ehci, int async)
1717 entry = ehci_get_fetch_addr(ehci, async);
1719 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *)&sitd,
1720 sizeof(EHCIsitd) >> 2) < 0) {
1723 ehci_trace_sitd(ehci, entry, &sitd);
1725 if (!(sitd.results & SITD_RESULTS_ACTIVE)) {
1726 /* siTD is not active, nothing to do */;
1728 /* TODO: split transfers are not implemented */
1729 fprintf(stderr, "WARNING: Skipping active siTD\n");
1732 ehci_set_fetch_addr(ehci, async, sitd.next);
1733 ehci_set_state(ehci, async, EST_FETCHENTRY);
1737 /* Section 4.10.2 - paragraph 3 */
1738 static int ehci_state_advqueue(EHCIQueue *q)
1741 /* TO-DO: 4.10.2 - paragraph 2
1742 * if I-bit is set to 1 and QH is not active
1743 * go to horizontal QH
1746 ehci_set_state(ehci, async, EST_HORIZONTALQH);
1752 * want data and alt-next qTD is valid
1754 if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) &&
1755 (NLPTR_TBIT(q->qh.altnext_qtd) == 0)) {
1756 q->qtdaddr = q->qh.altnext_qtd;
1757 ehci_set_state(q->ehci, q->async, EST_FETCHQTD);
1762 } else if (NLPTR_TBIT(q->qh.next_qtd) == 0) {
1763 q->qtdaddr = q->qh.next_qtd;
1764 ehci_set_state(q->ehci, q->async, EST_FETCHQTD);
1767 * no valid qTD, try next QH
1770 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
1776 /* Section 4.10.2 - paragraph 4 */
1777 static int ehci_state_fetchqtd(EHCIQueue *q)
1783 if (get_dwords(q->ehci, NLPTR_GET(q->qtdaddr), (uint32_t *) &qtd,
1784 sizeof(EHCIqtd) >> 2) < 0) {
1787 ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), &qtd);
1789 p = QTAILQ_FIRST(&q->packets);
1791 if (!ehci_verify_qtd(p, &qtd)) {
1792 ehci_cancel_queue(q);
1793 if (qtd.token & QTD_TOKEN_ACTIVE) {
1794 ehci_trace_guest_bug(q->ehci, "guest updated active qTD");
1799 ehci_qh_do_overlay(q);
1803 if (!(qtd.token & QTD_TOKEN_ACTIVE)) {
1804 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
1805 } else if (p != NULL) {
1807 case EHCI_ASYNC_NONE:
1808 case EHCI_ASYNC_INITIALIZED:
1809 /* Not yet executed (MULT), or previously nacked (int) packet */
1810 ehci_set_state(q->ehci, q->async, EST_EXECUTE);
1812 case EHCI_ASYNC_INFLIGHT:
1813 /* Check if the guest has added new tds to the queue */
1814 again = ehci_fill_queue(QTAILQ_LAST(&q->packets, pkts_head));
1815 /* Unfinished async handled packet, go horizontal */
1816 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
1818 case EHCI_ASYNC_FINISHED:
1819 /* Complete executing of the packet */
1820 ehci_set_state(q->ehci, q->async, EST_EXECUTING);
1824 p = ehci_alloc_packet(q);
1825 p->qtdaddr = q->qtdaddr;
1827 ehci_set_state(q->ehci, q->async, EST_EXECUTE);
1833 static int ehci_state_horizqh(EHCIQueue *q)
1837 if (ehci_get_fetch_addr(q->ehci, q->async) != q->qh.next) {
1838 ehci_set_fetch_addr(q->ehci, q->async, q->qh.next);
1839 ehci_set_state(q->ehci, q->async, EST_FETCHENTRY);
1842 ehci_set_state(q->ehci, q->async, EST_ACTIVE);
1848 /* Returns "again" */
1849 static int ehci_fill_queue(EHCIPacket *p)
1851 USBEndpoint *ep = p->packet.ep;
1852 EHCIQueue *q = p->queue;
1853 EHCIqtd qtd = p->qtd;
1857 if (NLPTR_TBIT(qtd.next) != 0) {
1862 * Detect circular td lists, Windows creates these, counting on the
1863 * active bit going low after execution to make the queue stop.
1865 QTAILQ_FOREACH(p, &q->packets, next) {
1866 if (p->qtdaddr == qtdaddr) {
1870 if (get_dwords(q->ehci, NLPTR_GET(qtdaddr),
1871 (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2) < 0) {
1874 ehci_trace_qtd(q, NLPTR_GET(qtdaddr), &qtd);
1875 if (!(qtd.token & QTD_TOKEN_ACTIVE)) {
1878 if (!ehci_verify_pid(q, &qtd)) {
1879 ehci_trace_guest_bug(q->ehci, "guest queued token with wrong pid");
1882 p = ehci_alloc_packet(q);
1883 p->qtdaddr = qtdaddr;
1885 if (ehci_execute(p, "queue") == -1) {
1888 assert(p->packet.status == USB_RET_ASYNC);
1889 p->async = EHCI_ASYNC_INFLIGHT;
1892 usb_device_flush_ep_queue(ep->dev, ep);
1896 static int ehci_state_execute(EHCIQueue *q)
1898 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
1902 assert(p->qtdaddr == q->qtdaddr);
1904 if (ehci_qh_do_overlay(q) != 0) {
1908 // TODO verify enough time remains in the uframe as in 4.4.1.1
1909 // TODO write back ptr to async list when done or out of time
1911 /* 4.10.3, bottom of page 82, go horizontal on transaction counter == 0 */
1912 if (!q->async && q->transact_ctr == 0) {
1913 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
1919 ehci_set_usbsts(q->ehci, USBSTS_REC);
1922 again = ehci_execute(p, "process");
1926 if (p->packet.status == USB_RET_ASYNC) {
1928 trace_usb_ehci_packet_action(p->queue, p, "async");
1929 p->async = EHCI_ASYNC_INFLIGHT;
1930 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
1932 again = ehci_fill_queue(p);
1939 ehci_set_state(q->ehci, q->async, EST_EXECUTING);
1946 static int ehci_state_executing(EHCIQueue *q)
1948 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
1951 assert(p->qtdaddr == q->qtdaddr);
1953 ehci_execute_complete(q);
1956 if (!q->async && q->transact_ctr > 0) {
1961 if (p->packet.status == USB_RET_NAK) {
1962 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
1964 ehci_set_state(q->ehci, q->async, EST_WRITEBACK);
1972 static int ehci_state_writeback(EHCIQueue *q)
1974 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
1975 uint32_t *qtd, addr;
1978 /* Write back the QTD from the QH area */
1980 assert(p->qtdaddr == q->qtdaddr);
1982 ehci_trace_qtd(q, NLPTR_GET(p->qtdaddr), (EHCIqtd *) &q->qh.next_qtd);
1983 qtd = (uint32_t *) &q->qh.next_qtd;
1984 addr = NLPTR_GET(p->qtdaddr);
1985 put_dwords(q->ehci, addr + 2 * sizeof(uint32_t), qtd + 2, 2);
1986 ehci_free_packet(p);
1989 * EHCI specs say go horizontal here.
1991 * We can also advance the queue here for performance reasons. We
1992 * need to take care to only take that shortcut in case we've
1993 * processed the qtd just written back without errors, i.e. halt
1996 if (q->qh.token & QTD_TOKEN_HALT) {
1997 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
2000 ehci_set_state(q->ehci, q->async, EST_ADVANCEQUEUE);
2007 * This is the state machine that is common to both async and periodic
2010 static void ehci_advance_state(EHCIState *ehci, int async)
2012 EHCIQueue *q = NULL;
2016 switch(ehci_get_state(ehci, async)) {
2017 case EST_WAITLISTHEAD:
2018 again = ehci_state_waitlisthead(ehci, async);
2021 case EST_FETCHENTRY:
2022 again = ehci_state_fetchentry(ehci, async);
2026 q = ehci_state_fetchqh(ehci, async);
2028 assert(q->async == async);
2036 again = ehci_state_fetchitd(ehci, async);
2040 again = ehci_state_fetchsitd(ehci, async);
2043 case EST_ADVANCEQUEUE:
2045 again = ehci_state_advqueue(q);
2050 again = ehci_state_fetchqtd(q);
2053 case EST_HORIZONTALQH:
2055 again = ehci_state_horizqh(q);
2060 again = ehci_state_execute(q);
2062 ehci->async_stepdown = 0;
2069 ehci->async_stepdown = 0;
2071 again = ehci_state_executing(q);
2076 again = ehci_state_writeback(q);
2078 ehci->periodic_sched_active = PERIODIC_ACTIVE;
2083 fprintf(stderr, "Bad state!\n");
2085 g_assert_not_reached();
2090 fprintf(stderr, "processing error - resetting ehci HC\n");
2098 static void ehci_advance_async_state(EHCIState *ehci)
2100 const int async = 1;
2102 switch(ehci_get_state(ehci, async)) {
2104 if (!ehci_async_enabled(ehci)) {
2107 ehci_set_state(ehci, async, EST_ACTIVE);
2108 // No break, fall through to ACTIVE
2111 if (!ehci_async_enabled(ehci)) {
2112 ehci_queues_rip_all(ehci, async);
2113 ehci_set_state(ehci, async, EST_INACTIVE);
2117 /* make sure guest has acknowledged the doorbell interrupt */
2118 /* TO-DO: is this really needed? */
2119 if (ehci->usbsts & USBSTS_IAA) {
2120 DPRINTF("IAA status bit still set.\n");
2124 /* check that address register has been set */
2125 if (ehci->asynclistaddr == 0) {
2129 ehci_set_state(ehci, async, EST_WAITLISTHEAD);
2130 ehci_advance_state(ehci, async);
2132 /* If the doorbell is set, the guest wants to make a change to the
2133 * schedule. The host controller needs to release cached data.
2136 if (ehci->usbcmd & USBCMD_IAAD) {
2137 /* Remove all unseen qhs from the async qhs queue */
2138 ehci_queues_rip_unseen(ehci, async);
2139 trace_usb_ehci_doorbell_ack();
2140 ehci->usbcmd &= ~USBCMD_IAAD;
2141 ehci_raise_irq(ehci, USBSTS_IAA);
2146 /* this should only be due to a developer mistake */
2147 fprintf(stderr, "ehci: Bad asynchronous state %d. "
2148 "Resetting to active\n", ehci->astate);
2149 g_assert_not_reached();
2153 static void ehci_advance_periodic_state(EHCIState *ehci)
2157 const int async = 0;
2161 switch(ehci_get_state(ehci, async)) {
2163 if (!(ehci->frindex & 7) && ehci_periodic_enabled(ehci)) {
2164 ehci_set_state(ehci, async, EST_ACTIVE);
2165 // No break, fall through to ACTIVE
2170 if (!(ehci->frindex & 7) && !ehci_periodic_enabled(ehci)) {
2171 ehci_queues_rip_all(ehci, async);
2172 ehci_set_state(ehci, async, EST_INACTIVE);
2176 list = ehci->periodiclistbase & 0xfffff000;
2177 /* check that register has been set */
2181 list |= ((ehci->frindex & 0x1ff8) >> 1);
2183 if (get_dwords(ehci, list, &entry, 1) < 0) {
2187 DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n",
2188 ehci->frindex / 8, list, entry);
2189 ehci_set_fetch_addr(ehci, async,entry);
2190 ehci_set_state(ehci, async, EST_FETCHENTRY);
2191 ehci_advance_state(ehci, async);
2192 ehci_queues_rip_unused(ehci, async);
2196 /* this should only be due to a developer mistake */
2197 fprintf(stderr, "ehci: Bad periodic state %d. "
2198 "Resetting to active\n", ehci->pstate);
2199 g_assert_not_reached();
2203 static void ehci_update_frindex(EHCIState *ehci, int uframes)
2207 if (!ehci_enabled(ehci) && ehci->pstate == EST_INACTIVE) {
2211 for (i = 0; i < uframes; i++) {
2214 if (ehci->frindex == 0x00002000) {
2215 ehci_raise_irq(ehci, USBSTS_FLR);
2218 if (ehci->frindex == 0x00004000) {
2219 ehci_raise_irq(ehci, USBSTS_FLR);
2221 if (ehci->usbsts_frindex >= 0x00004000) {
2222 ehci->usbsts_frindex -= 0x00004000;
2224 ehci->usbsts_frindex = 0;
2230 static void ehci_frame_timer(void *opaque)
2232 EHCIState *ehci = opaque;
2234 int64_t expire_time, t_now;
2235 uint64_t ns_elapsed;
2236 int uframes, skipped_uframes;
2239 t_now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2240 ns_elapsed = t_now - ehci->last_run_ns;
2241 uframes = ns_elapsed / UFRAME_TIMER_NS;
2243 if (ehci_periodic_enabled(ehci) || ehci->pstate != EST_INACTIVE) {
2246 if (uframes > (ehci->maxframes * 8)) {
2247 skipped_uframes = uframes - (ehci->maxframes * 8);
2248 ehci_update_frindex(ehci, skipped_uframes);
2249 ehci->last_run_ns += UFRAME_TIMER_NS * skipped_uframes;
2250 uframes -= skipped_uframes;
2251 DPRINTF("WARNING - EHCI skipped %d uframes\n", skipped_uframes);
2254 for (i = 0; i < uframes; i++) {
2256 * If we're running behind schedule, we should not catch up
2257 * too fast, as that will make some guests unhappy:
2258 * 1) We must process a minimum of MIN_UFR_PER_TICK frames,
2259 * otherwise we will never catch up
2260 * 2) Process frames until the guest has requested an irq (IOC)
2262 if (i >= MIN_UFR_PER_TICK) {
2263 ehci_commit_irq(ehci);
2264 if ((ehci->usbsts & USBINTR_MASK) & ehci->usbintr) {
2268 if (ehci->periodic_sched_active) {
2269 ehci->periodic_sched_active--;
2271 ehci_update_frindex(ehci, 1);
2272 if ((ehci->frindex & 7) == 0) {
2273 ehci_advance_periodic_state(ehci);
2275 ehci->last_run_ns += UFRAME_TIMER_NS;
2278 ehci->periodic_sched_active = 0;
2279 ehci_update_frindex(ehci, uframes);
2280 ehci->last_run_ns += UFRAME_TIMER_NS * uframes;
2283 if (ehci->periodic_sched_active) {
2284 ehci->async_stepdown = 0;
2285 } else if (ehci->async_stepdown < ehci->maxframes / 2) {
2286 ehci->async_stepdown++;
2289 /* Async is not inside loop since it executes everything it can once
2292 if (ehci_async_enabled(ehci) || ehci->astate != EST_INACTIVE) {
2294 ehci_advance_async_state(ehci);
2297 ehci_commit_irq(ehci);
2298 if (ehci->usbsts_pending) {
2300 ehci->async_stepdown = 0;
2303 if (ehci_enabled(ehci) && (ehci->usbintr & USBSTS_FLR)) {
2308 /* If we've raised int, we speed up the timer, so that we quickly
2309 * notice any new packets queued up in response */
2310 if (ehci->int_req_by_async && (ehci->usbsts & USBSTS_INT)) {
2311 expire_time = t_now + get_ticks_per_sec() / (FRAME_TIMER_FREQ * 4);
2312 ehci->int_req_by_async = false;
2314 expire_time = t_now + (get_ticks_per_sec()
2315 * (ehci->async_stepdown+1) / FRAME_TIMER_FREQ);
2317 timer_mod(ehci->frame_timer, expire_time);
2321 static const MemoryRegionOps ehci_mmio_caps_ops = {
2322 .read = ehci_caps_read,
2323 .write = ehci_caps_write,
2324 .valid.min_access_size = 1,
2325 .valid.max_access_size = 4,
2326 .impl.min_access_size = 1,
2327 .impl.max_access_size = 1,
2328 .endianness = DEVICE_LITTLE_ENDIAN,
2331 static const MemoryRegionOps ehci_mmio_opreg_ops = {
2332 .read = ehci_opreg_read,
2333 .write = ehci_opreg_write,
2334 .valid.min_access_size = 4,
2335 .valid.max_access_size = 4,
2336 .endianness = DEVICE_LITTLE_ENDIAN,
2339 static const MemoryRegionOps ehci_mmio_port_ops = {
2340 .read = ehci_port_read,
2341 .write = ehci_port_write,
2342 .valid.min_access_size = 4,
2343 .valid.max_access_size = 4,
2344 .endianness = DEVICE_LITTLE_ENDIAN,
2347 static USBPortOps ehci_port_ops = {
2348 .attach = ehci_attach,
2349 .detach = ehci_detach,
2350 .child_detach = ehci_child_detach,
2351 .wakeup = ehci_wakeup,
2352 .complete = ehci_async_complete_packet,
2355 static USBBusOps ehci_bus_ops_companion = {
2356 .register_companion = ehci_register_companion,
2357 .wakeup_endpoint = ehci_wakeup_endpoint,
2359 static USBBusOps ehci_bus_ops_standalone = {
2360 .wakeup_endpoint = ehci_wakeup_endpoint,
2363 static void usb_ehci_pre_save(void *opaque)
2365 EHCIState *ehci = opaque;
2366 uint32_t new_frindex;
2368 /* Round down frindex to a multiple of 8 for migration compatibility */
2369 new_frindex = ehci->frindex & ~7;
2370 ehci->last_run_ns -= (ehci->frindex - new_frindex) * UFRAME_TIMER_NS;
2371 ehci->frindex = new_frindex;
2374 static int usb_ehci_post_load(void *opaque, int version_id)
2376 EHCIState *s = opaque;
2379 for (i = 0; i < NB_PORTS; i++) {
2380 USBPort *companion = s->companion_ports[i];
2381 if (companion == NULL) {
2384 if (s->portsc[i] & PORTSC_POWNER) {
2385 companion->dev = s->ports[i].dev;
2387 companion->dev = NULL;
2394 static void usb_ehci_vm_state_change(void *opaque, int running, RunState state)
2396 EHCIState *ehci = opaque;
2399 * We don't migrate the EHCIQueue-s, instead we rebuild them for the
2400 * schedule in guest memory. We must do the rebuilt ASAP, so that
2401 * USB-devices which have async handled packages have a packet in the
2402 * ep queue to match the completion with.
2404 if (state == RUN_STATE_RUNNING) {
2405 ehci_advance_async_state(ehci);
2409 * The schedule rebuilt from guest memory could cause the migration dest
2410 * to miss a QH unlink, and fail to cancel packets, since the unlinked QH
2411 * will never have existed on the destination. Therefor we must flush the
2412 * async schedule on savevm to catch any not yet noticed unlinks.
2414 if (state == RUN_STATE_SAVE_VM) {
2415 ehci_advance_async_state(ehci);
2416 ehci_queues_rip_unseen(ehci, 1);
2420 const VMStateDescription vmstate_ehci = {
2421 .name = "ehci-core",
2423 .minimum_version_id = 1,
2424 .pre_save = usb_ehci_pre_save,
2425 .post_load = usb_ehci_post_load,
2426 .fields = (VMStateField[]) {
2427 /* mmio registers */
2428 VMSTATE_UINT32(usbcmd, EHCIState),
2429 VMSTATE_UINT32(usbsts, EHCIState),
2430 VMSTATE_UINT32_V(usbsts_pending, EHCIState, 2),
2431 VMSTATE_UINT32_V(usbsts_frindex, EHCIState, 2),
2432 VMSTATE_UINT32(usbintr, EHCIState),
2433 VMSTATE_UINT32(frindex, EHCIState),
2434 VMSTATE_UINT32(ctrldssegment, EHCIState),
2435 VMSTATE_UINT32(periodiclistbase, EHCIState),
2436 VMSTATE_UINT32(asynclistaddr, EHCIState),
2437 VMSTATE_UINT32(configflag, EHCIState),
2438 VMSTATE_UINT32(portsc[0], EHCIState),
2439 VMSTATE_UINT32(portsc[1], EHCIState),
2440 VMSTATE_UINT32(portsc[2], EHCIState),
2441 VMSTATE_UINT32(portsc[3], EHCIState),
2442 VMSTATE_UINT32(portsc[4], EHCIState),
2443 VMSTATE_UINT32(portsc[5], EHCIState),
2445 VMSTATE_TIMER_PTR(frame_timer, EHCIState),
2446 VMSTATE_UINT64(last_run_ns, EHCIState),
2447 VMSTATE_UINT32(async_stepdown, EHCIState),
2448 /* schedule state */
2449 VMSTATE_UINT32(astate, EHCIState),
2450 VMSTATE_UINT32(pstate, EHCIState),
2451 VMSTATE_UINT32(a_fetch_addr, EHCIState),
2452 VMSTATE_UINT32(p_fetch_addr, EHCIState),
2453 VMSTATE_END_OF_LIST()
2457 void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp)
2461 if (s->portnr > NB_PORTS) {
2462 error_setg(errp, "Too many ports! Max. port number is %d.",
2467 usb_bus_new(&s->bus, sizeof(s->bus), s->companion_enable ?
2468 &ehci_bus_ops_companion : &ehci_bus_ops_standalone, dev);
2469 for (i = 0; i < s->portnr; i++) {
2470 usb_register_port(&s->bus, &s->ports[i], s, i, &ehci_port_ops,
2471 USB_SPEED_MASK_HIGH);
2472 s->ports[i].dev = 0;
2475 s->frame_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ehci_frame_timer, s);
2476 s->async_bh = qemu_bh_new(ehci_frame_timer, s);
2479 s->vmstate = qemu_add_vm_change_state_handler(usb_ehci_vm_state_change, s);
2482 void usb_ehci_unrealize(EHCIState *s, DeviceState *dev, Error **errp)
2484 trace_usb_ehci_unrealize();
2486 if (s->frame_timer) {
2487 timer_del(s->frame_timer);
2488 timer_free(s->frame_timer);
2489 s->frame_timer = NULL;
2492 qemu_bh_delete(s->async_bh);
2495 ehci_queues_rip_all(s, 0);
2496 ehci_queues_rip_all(s, 1);
2498 memory_region_del_subregion(&s->mem, &s->mem_caps);
2499 memory_region_del_subregion(&s->mem, &s->mem_opreg);
2500 memory_region_del_subregion(&s->mem, &s->mem_ports);
2502 usb_bus_release(&s->bus);
2505 qemu_del_vm_change_state_handler(s->vmstate);
2509 void usb_ehci_init(EHCIState *s, DeviceState *dev)
2511 /* 2.2 host controller interface version */
2512 s->caps[0x00] = (uint8_t)(s->opregbase - s->capsbase);
2513 s->caps[0x01] = 0x00;
2514 s->caps[0x02] = 0x00;
2515 s->caps[0x03] = 0x01; /* HC version */
2516 s->caps[0x04] = s->portnr; /* Number of downstream ports */
2517 s->caps[0x05] = 0x00; /* No companion ports at present */
2518 s->caps[0x06] = 0x00;
2519 s->caps[0x07] = 0x00;
2520 s->caps[0x08] = 0x80; /* We can cache whole frame, no 64-bit */
2521 s->caps[0x0a] = 0x00;
2522 s->caps[0x0b] = 0x00;
2524 QTAILQ_INIT(&s->aqueues);
2525 QTAILQ_INIT(&s->pqueues);
2526 usb_packet_init(&s->ipacket);
2528 memory_region_init(&s->mem, OBJECT(dev), "ehci", MMIO_SIZE);
2529 memory_region_init_io(&s->mem_caps, OBJECT(dev), &ehci_mmio_caps_ops, s,
2530 "capabilities", CAPA_SIZE);
2531 memory_region_init_io(&s->mem_opreg, OBJECT(dev), &ehci_mmio_opreg_ops, s,
2532 "operational", s->portscbase);
2533 memory_region_init_io(&s->mem_ports, OBJECT(dev), &ehci_mmio_port_ops, s,
2534 "ports", 4 * s->portnr);
2536 memory_region_add_subregion(&s->mem, s->capsbase, &s->mem_caps);
2537 memory_region_add_subregion(&s->mem, s->opregbase, &s->mem_opreg);
2538 memory_region_add_subregion(&s->mem, s->opregbase + s->portscbase,
2543 * vim: expandtab ts=4