2 * USB xHCI controller emulation
4 * Copyright (c) 2011 Securiforest
5 * Date: 2011-05-11 ; Author: Hector Martin <hector@marcansoft.com>
6 * Based on usb-ohci.c, emulates Renesas NEC USB 3.0
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu-timer.h"
32 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
34 #define DPRINTF(...) do {} while (0)
36 #define FIXME() do { fprintf(stderr, "FIXME %s:%d\n", \
37 __func__, __LINE__); abort(); } while (0)
45 #define MAXPORTS (USB2_PORTS+USB3_PORTS)
51 /* Very pessimistic, let's hope it's enough for all cases */
52 #define EV_QUEUE (((3*TD_QUEUE)+16)*MAXSLOTS)
53 /* Do not deliver ER Full events. NEC's driver does some things not bound
54 * to the specs when it gets them */
58 #define OFF_OPER LEN_CAP
59 #define LEN_OPER (0x400 + 0x10 * MAXPORTS)
60 #define OFF_RUNTIME ((OFF_OPER + LEN_OPER + 0x20) & ~0x1f)
61 #define LEN_RUNTIME (0x20 + MAXINTRS * 0x20)
62 #define OFF_DOORBELL (OFF_RUNTIME + LEN_RUNTIME)
63 #define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20)
65 /* must be power of 2 */
66 #define LEN_REGS 0x2000
68 #if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS
69 # error Increase LEN_REGS
73 # error TODO: only one interrupter supported
77 #define USBCMD_RS (1<<0)
78 #define USBCMD_HCRST (1<<1)
79 #define USBCMD_INTE (1<<2)
80 #define USBCMD_HSEE (1<<3)
81 #define USBCMD_LHCRST (1<<7)
82 #define USBCMD_CSS (1<<8)
83 #define USBCMD_CRS (1<<9)
84 #define USBCMD_EWE (1<<10)
85 #define USBCMD_EU3S (1<<11)
87 #define USBSTS_HCH (1<<0)
88 #define USBSTS_HSE (1<<2)
89 #define USBSTS_EINT (1<<3)
90 #define USBSTS_PCD (1<<4)
91 #define USBSTS_SSS (1<<8)
92 #define USBSTS_RSS (1<<9)
93 #define USBSTS_SRE (1<<10)
94 #define USBSTS_CNR (1<<11)
95 #define USBSTS_HCE (1<<12)
98 #define PORTSC_CCS (1<<0)
99 #define PORTSC_PED (1<<1)
100 #define PORTSC_OCA (1<<3)
101 #define PORTSC_PR (1<<4)
102 #define PORTSC_PLS_SHIFT 5
103 #define PORTSC_PLS_MASK 0xf
104 #define PORTSC_PP (1<<9)
105 #define PORTSC_SPEED_SHIFT 10
106 #define PORTSC_SPEED_MASK 0xf
107 #define PORTSC_SPEED_FULL (1<<10)
108 #define PORTSC_SPEED_LOW (2<<10)
109 #define PORTSC_SPEED_HIGH (3<<10)
110 #define PORTSC_SPEED_SUPER (4<<10)
111 #define PORTSC_PIC_SHIFT 14
112 #define PORTSC_PIC_MASK 0x3
113 #define PORTSC_LWS (1<<16)
114 #define PORTSC_CSC (1<<17)
115 #define PORTSC_PEC (1<<18)
116 #define PORTSC_WRC (1<<19)
117 #define PORTSC_OCC (1<<20)
118 #define PORTSC_PRC (1<<21)
119 #define PORTSC_PLC (1<<22)
120 #define PORTSC_CEC (1<<23)
121 #define PORTSC_CAS (1<<24)
122 #define PORTSC_WCE (1<<25)
123 #define PORTSC_WDE (1<<26)
124 #define PORTSC_WOE (1<<27)
125 #define PORTSC_DR (1<<30)
126 #define PORTSC_WPR (1<<31)
128 #define CRCR_RCS (1<<0)
129 #define CRCR_CS (1<<1)
130 #define CRCR_CA (1<<2)
131 #define CRCR_CRR (1<<3)
133 #define IMAN_IP (1<<0)
134 #define IMAN_IE (1<<1)
136 #define ERDP_EHB (1<<3)
139 typedef struct XHCITRB
{
148 typedef enum TRBType
{
161 CR_CONFIGURE_ENDPOINT
,
169 CR_SET_LATENCY_TOLERANCE
,
170 CR_GET_PORT_BANDWIDTH
,
175 ER_PORT_STATUS_CHANGE
,
176 ER_BANDWIDTH_REQUEST
,
179 ER_DEVICE_NOTIFICATION
,
181 /* vendor specific bits */
182 CR_VENDOR_VIA_CHALLENGE_RESPONSE
= 48,
183 CR_VENDOR_NEC_FIRMWARE_REVISION
= 49,
184 CR_VENDOR_NEC_CHALLENGE_RESPONSE
= 50,
187 #define CR_LINK TR_LINK
189 typedef enum TRBCCode
{
192 CC_DATA_BUFFER_ERROR
,
194 CC_USB_TRANSACTION_ERROR
,
200 CC_INVALID_STREAM_TYPE_ERROR
,
201 CC_SLOT_NOT_ENABLED_ERROR
,
202 CC_EP_NOT_ENABLED_ERROR
,
208 CC_BANDWIDTH_OVERRUN
,
209 CC_CONTEXT_STATE_ERROR
,
210 CC_NO_PING_RESPONSE_ERROR
,
211 CC_EVENT_RING_FULL_ERROR
,
212 CC_INCOMPATIBLE_DEVICE_ERROR
,
213 CC_MISSED_SERVICE_ERROR
,
214 CC_COMMAND_RING_STOPPED
,
217 CC_STOPPED_LENGTH_INVALID
,
218 CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
= 29,
219 CC_ISOCH_BUFFER_OVERRUN
= 31,
222 CC_INVALID_STREAM_ID_ERROR
,
223 CC_SECONDARY_BANDWIDTH_ERROR
,
224 CC_SPLIT_TRANSACTION_ERROR
228 #define TRB_TYPE_SHIFT 10
229 #define TRB_TYPE_MASK 0x3f
230 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
232 #define TRB_EV_ED (1<<2)
234 #define TRB_TR_ENT (1<<1)
235 #define TRB_TR_ISP (1<<2)
236 #define TRB_TR_NS (1<<3)
237 #define TRB_TR_CH (1<<4)
238 #define TRB_TR_IOC (1<<5)
239 #define TRB_TR_IDT (1<<6)
240 #define TRB_TR_TBC_SHIFT 7
241 #define TRB_TR_TBC_MASK 0x3
242 #define TRB_TR_BEI (1<<9)
243 #define TRB_TR_TLBPC_SHIFT 16
244 #define TRB_TR_TLBPC_MASK 0xf
245 #define TRB_TR_FRAMEID_SHIFT 20
246 #define TRB_TR_FRAMEID_MASK 0x7ff
247 #define TRB_TR_SIA (1<<31)
249 #define TRB_TR_DIR (1<<16)
251 #define TRB_CR_SLOTID_SHIFT 24
252 #define TRB_CR_SLOTID_MASK 0xff
253 #define TRB_CR_EPID_SHIFT 16
254 #define TRB_CR_EPID_MASK 0x1f
256 #define TRB_CR_BSR (1<<9)
257 #define TRB_CR_DC (1<<9)
259 #define TRB_LK_TC (1<<1)
261 #define EP_TYPE_MASK 0x7
262 #define EP_TYPE_SHIFT 3
264 #define EP_STATE_MASK 0x7
265 #define EP_DISABLED (0<<0)
266 #define EP_RUNNING (1<<0)
267 #define EP_HALTED (2<<0)
268 #define EP_STOPPED (3<<0)
269 #define EP_ERROR (4<<0)
271 #define SLOT_STATE_MASK 0x1f
272 #define SLOT_STATE_SHIFT 27
273 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
274 #define SLOT_ENABLED 0
275 #define SLOT_DEFAULT 1
276 #define SLOT_ADDRESSED 2
277 #define SLOT_CONFIGURED 3
279 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
280 #define SLOT_CONTEXT_ENTRIES_SHIFT 27
282 typedef enum EPType
{
293 typedef struct XHCIRing
{
299 typedef struct XHCIPort
{
305 typedef struct XHCIState XHCIState
;
307 typedef struct XHCITransfer
{
315 unsigned int iso_pkts
;
322 unsigned int trb_count
;
323 unsigned int trb_alloced
;
326 unsigned int data_length
;
327 unsigned int data_alloced
;
333 unsigned int pktsize
;
334 unsigned int cur_pkt
;
337 typedef struct XHCIEPContext
{
339 unsigned int next_xfer
;
340 unsigned int comp_xfer
;
341 XHCITransfer transfers
[TD_QUEUE
];
345 unsigned int next_bg
;
346 XHCITransfer bg_transfers
[BG_XFERS
];
349 unsigned int max_psize
;
354 typedef struct XHCISlot
{
358 unsigned int devaddr
;
359 XHCIEPContext
* eps
[31];
362 typedef struct XHCIEvent
{
379 unsigned int devaddr
;
381 /* Operational Registers */
388 uint32_t dcbaap_high
;
391 XHCIPort ports
[MAXPORTS
];
392 XHCISlot slots
[MAXSLOTS
];
394 /* Runtime Registers */
396 /* note: we only support one interrupter */
401 uint32_t erstba_high
;
408 unsigned int er_ep_idx
;
411 XHCIEvent ev_buffer
[EV_QUEUE
];
412 unsigned int ev_buffer_put
;
413 unsigned int ev_buffer_get
;
418 typedef struct XHCIEvRingSeg
{
425 static const char *TRBType_names
[] = {
426 [TRB_RESERVED
] = "TRB_RESERVED",
427 [TR_NORMAL
] = "TR_NORMAL",
428 [TR_SETUP
] = "TR_SETUP",
429 [TR_DATA
] = "TR_DATA",
430 [TR_STATUS
] = "TR_STATUS",
431 [TR_ISOCH
] = "TR_ISOCH",
432 [TR_LINK
] = "TR_LINK",
433 [TR_EVDATA
] = "TR_EVDATA",
434 [TR_NOOP
] = "TR_NOOP",
435 [CR_ENABLE_SLOT
] = "CR_ENABLE_SLOT",
436 [CR_DISABLE_SLOT
] = "CR_DISABLE_SLOT",
437 [CR_ADDRESS_DEVICE
] = "CR_ADDRESS_DEVICE",
438 [CR_CONFIGURE_ENDPOINT
] = "CR_CONFIGURE_ENDPOINT",
439 [CR_EVALUATE_CONTEXT
] = "CR_EVALUATE_CONTEXT",
440 [CR_RESET_ENDPOINT
] = "CR_RESET_ENDPOINT",
441 [CR_STOP_ENDPOINT
] = "CR_STOP_ENDPOINT",
442 [CR_SET_TR_DEQUEUE
] = "CR_SET_TR_DEQUEUE",
443 [CR_RESET_DEVICE
] = "CR_RESET_DEVICE",
444 [CR_FORCE_EVENT
] = "CR_FORCE_EVENT",
445 [CR_NEGOTIATE_BW
] = "CR_NEGOTIATE_BW",
446 [CR_SET_LATENCY_TOLERANCE
] = "CR_SET_LATENCY_TOLERANCE",
447 [CR_GET_PORT_BANDWIDTH
] = "CR_GET_PORT_BANDWIDTH",
448 [CR_FORCE_HEADER
] = "CR_FORCE_HEADER",
449 [CR_NOOP
] = "CR_NOOP",
450 [ER_TRANSFER
] = "ER_TRANSFER",
451 [ER_COMMAND_COMPLETE
] = "ER_COMMAND_COMPLETE",
452 [ER_PORT_STATUS_CHANGE
] = "ER_PORT_STATUS_CHANGE",
453 [ER_BANDWIDTH_REQUEST
] = "ER_BANDWIDTH_REQUEST",
454 [ER_DOORBELL
] = "ER_DOORBELL",
455 [ER_HOST_CONTROLLER
] = "ER_HOST_CONTROLLER",
456 [ER_DEVICE_NOTIFICATION
] = "ER_DEVICE_NOTIFICATION",
457 [ER_MFINDEX_WRAP
] = "ER_MFINDEX_WRAP",
458 [CR_VENDOR_VIA_CHALLENGE_RESPONSE
] = "CR_VENDOR_VIA_CHALLENGE_RESPONSE",
459 [CR_VENDOR_NEC_FIRMWARE_REVISION
] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
460 [CR_VENDOR_NEC_CHALLENGE_RESPONSE
] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
463 static const char *lookup_name(uint32_t index
, const char **list
, uint32_t llen
)
465 if (index
>= llen
|| list
[index
] == NULL
) {
471 static const char *trb_name(XHCITRB
*trb
)
473 return lookup_name(TRB_TYPE(*trb
), TRBType_names
,
474 ARRAY_SIZE(TRBType_names
));
477 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
480 static inline dma_addr_t
xhci_addr64(uint32_t low
, uint32_t high
)
482 if (sizeof(dma_addr_t
) == 4) {
485 return low
| (((dma_addr_t
)high
<< 16) << 16);
489 static inline dma_addr_t
xhci_mask64(uint64_t addr
)
491 if (sizeof(dma_addr_t
) == 4) {
492 return addr
& 0xffffffff;
498 static void xhci_irq_update(XHCIState
*xhci
)
502 if (xhci
->iman
& IMAN_IP
&& xhci
->iman
& IMAN_IE
&&
503 xhci
->usbcmd
& USBCMD_INTE
) {
507 if (xhci
->msi
&& msi_enabled(&xhci
->pci_dev
)) {
509 trace_usb_xhci_irq_msi(0);
510 msi_notify(&xhci
->pci_dev
, 0);
513 trace_usb_xhci_irq_intx(level
);
514 qemu_set_irq(xhci
->irq
, level
);
518 static inline int xhci_running(XHCIState
*xhci
)
520 return !(xhci
->usbsts
& USBSTS_HCH
) && !xhci
->er_full
;
523 static void xhci_die(XHCIState
*xhci
)
525 xhci
->usbsts
|= USBSTS_HCE
;
526 fprintf(stderr
, "xhci: asserted controller error\n");
529 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
)
534 ev_trb
.parameter
= cpu_to_le64(event
->ptr
);
535 ev_trb
.status
= cpu_to_le32(event
->length
| (event
->ccode
<< 24));
536 ev_trb
.control
= (event
->slotid
<< 24) | (event
->epid
<< 16) |
537 event
->flags
| (event
->type
<< TRB_TYPE_SHIFT
);
539 ev_trb
.control
|= TRB_C
;
541 ev_trb
.control
= cpu_to_le32(ev_trb
.control
);
543 trace_usb_xhci_queue_event(xhci
->er_ep_idx
, trb_name(&ev_trb
),
544 ev_trb
.parameter
, ev_trb
.status
, ev_trb
.control
);
546 addr
= xhci
->er_start
+ TRB_SIZE
*xhci
->er_ep_idx
;
547 pci_dma_write(&xhci
->pci_dev
, addr
, &ev_trb
, TRB_SIZE
);
550 if (xhci
->er_ep_idx
>= xhci
->er_size
) {
552 xhci
->er_pcs
= !xhci
->er_pcs
;
556 static void xhci_events_update(XHCIState
*xhci
)
562 if (xhci
->usbsts
& USBSTS_HCH
) {
566 erdp
= xhci_addr64(xhci
->erdp_low
, xhci
->erdp_high
);
567 if (erdp
< xhci
->er_start
||
568 erdp
>= (xhci
->er_start
+ TRB_SIZE
*xhci
->er_size
)) {
569 fprintf(stderr
, "xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
570 fprintf(stderr
, "xhci: ER at "DMA_ADDR_FMT
" len %d\n",
571 xhci
->er_start
, xhci
->er_size
);
575 dp_idx
= (erdp
- xhci
->er_start
) / TRB_SIZE
;
576 assert(dp_idx
< xhci
->er_size
);
578 /* NEC didn't read section 4.9.4 of the spec (v1.0 p139 top Note) and thus
579 * deadlocks when the ER is full. Hack it by holding off events until
580 * the driver decides to free at least half of the ring */
582 int er_free
= dp_idx
- xhci
->er_ep_idx
;
584 er_free
+= xhci
->er_size
;
586 if (er_free
< (xhci
->er_size
/2)) {
587 DPRINTF("xhci_events_update(): event ring still "
588 "more than half full (hack)\n");
593 while (xhci
->ev_buffer_put
!= xhci
->ev_buffer_get
) {
594 assert(xhci
->er_full
);
595 if (((xhci
->er_ep_idx
+1) % xhci
->er_size
) == dp_idx
) {
596 DPRINTF("xhci_events_update(): event ring full again\n");
598 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
599 xhci_write_event(xhci
, &full
);
604 XHCIEvent
*event
= &xhci
->ev_buffer
[xhci
->ev_buffer_get
];
605 xhci_write_event(xhci
, event
);
606 xhci
->ev_buffer_get
++;
608 if (xhci
->ev_buffer_get
== EV_QUEUE
) {
609 xhci
->ev_buffer_get
= 0;
614 xhci
->erdp_low
|= ERDP_EHB
;
615 xhci
->iman
|= IMAN_IP
;
616 xhci
->usbsts
|= USBSTS_EINT
;
617 xhci_irq_update(xhci
);
620 if (xhci
->er_full
&& xhci
->ev_buffer_put
== xhci
->ev_buffer_get
) {
621 DPRINTF("xhci_events_update(): event ring no longer full\n");
627 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
)
633 DPRINTF("xhci_event(): ER full, queueing\n");
634 if (((xhci
->ev_buffer_put
+1) % EV_QUEUE
) == xhci
->ev_buffer_get
) {
635 fprintf(stderr
, "xhci: event queue full, dropping event!\n");
638 xhci
->ev_buffer
[xhci
->ev_buffer_put
++] = *event
;
639 if (xhci
->ev_buffer_put
== EV_QUEUE
) {
640 xhci
->ev_buffer_put
= 0;
645 erdp
= xhci_addr64(xhci
->erdp_low
, xhci
->erdp_high
);
646 if (erdp
< xhci
->er_start
||
647 erdp
>= (xhci
->er_start
+ TRB_SIZE
*xhci
->er_size
)) {
648 fprintf(stderr
, "xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
649 fprintf(stderr
, "xhci: ER at "DMA_ADDR_FMT
" len %d\n",
650 xhci
->er_start
, xhci
->er_size
);
655 dp_idx
= (erdp
- xhci
->er_start
) / TRB_SIZE
;
656 assert(dp_idx
< xhci
->er_size
);
658 if ((xhci
->er_ep_idx
+1) % xhci
->er_size
== dp_idx
) {
659 DPRINTF("xhci_event(): ER full, queueing\n");
661 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
662 xhci_write_event(xhci
, &full
);
665 if (((xhci
->ev_buffer_put
+1) % EV_QUEUE
) == xhci
->ev_buffer_get
) {
666 fprintf(stderr
, "xhci: event queue full, dropping event!\n");
669 xhci
->ev_buffer
[xhci
->ev_buffer_put
++] = *event
;
670 if (xhci
->ev_buffer_put
== EV_QUEUE
) {
671 xhci
->ev_buffer_put
= 0;
674 xhci_write_event(xhci
, event
);
677 xhci
->erdp_low
|= ERDP_EHB
;
678 xhci
->iman
|= IMAN_IP
;
679 xhci
->usbsts
|= USBSTS_EINT
;
681 xhci_irq_update(xhci
);
684 static void xhci_ring_init(XHCIState
*xhci
, XHCIRing
*ring
,
688 ring
->dequeue
= base
;
692 static TRBType
xhci_ring_fetch(XHCIState
*xhci
, XHCIRing
*ring
, XHCITRB
*trb
,
697 pci_dma_read(&xhci
->pci_dev
, ring
->dequeue
, trb
, TRB_SIZE
);
698 trb
->addr
= ring
->dequeue
;
699 trb
->ccs
= ring
->ccs
;
700 le64_to_cpus(&trb
->parameter
);
701 le32_to_cpus(&trb
->status
);
702 le32_to_cpus(&trb
->control
);
704 trace_usb_xhci_fetch_trb(ring
->dequeue
, trb_name(trb
),
705 trb
->parameter
, trb
->status
, trb
->control
);
707 if ((trb
->control
& TRB_C
) != ring
->ccs
) {
711 type
= TRB_TYPE(*trb
);
713 if (type
!= TR_LINK
) {
715 *addr
= ring
->dequeue
;
717 ring
->dequeue
+= TRB_SIZE
;
720 ring
->dequeue
= xhci_mask64(trb
->parameter
);
721 if (trb
->control
& TRB_LK_TC
) {
722 ring
->ccs
= !ring
->ccs
;
728 static int xhci_ring_chain_length(XHCIState
*xhci
, const XHCIRing
*ring
)
732 dma_addr_t dequeue
= ring
->dequeue
;
733 bool ccs
= ring
->ccs
;
734 /* hack to bundle together the two/three TDs that make a setup transfer */
735 bool control_td_set
= 0;
739 pci_dma_read(&xhci
->pci_dev
, dequeue
, &trb
, TRB_SIZE
);
740 le64_to_cpus(&trb
.parameter
);
741 le32_to_cpus(&trb
.status
);
742 le32_to_cpus(&trb
.control
);
744 if ((trb
.control
& TRB_C
) != ccs
) {
748 type
= TRB_TYPE(trb
);
750 if (type
== TR_LINK
) {
751 dequeue
= xhci_mask64(trb
.parameter
);
752 if (trb
.control
& TRB_LK_TC
) {
761 if (type
== TR_SETUP
) {
763 } else if (type
== TR_STATUS
) {
767 if (!control_td_set
&& !(trb
.control
& TRB_TR_CH
)) {
773 static void xhci_er_reset(XHCIState
*xhci
)
777 /* cache the (sole) event ring segment location */
778 if (xhci
->erstsz
!= 1) {
779 fprintf(stderr
, "xhci: invalid value for ERSTSZ: %d\n", xhci
->erstsz
);
783 dma_addr_t erstba
= xhci_addr64(xhci
->erstba_low
, xhci
->erstba_high
);
784 pci_dma_read(&xhci
->pci_dev
, erstba
, &seg
, sizeof(seg
));
785 le32_to_cpus(&seg
.addr_low
);
786 le32_to_cpus(&seg
.addr_high
);
787 le32_to_cpus(&seg
.size
);
788 if (seg
.size
< 16 || seg
.size
> 4096) {
789 fprintf(stderr
, "xhci: invalid value for segment size: %d\n", seg
.size
);
793 xhci
->er_start
= xhci_addr64(seg
.addr_low
, seg
.addr_high
);
794 xhci
->er_size
= seg
.size
;
800 DPRINTF("xhci: event ring:" DMA_ADDR_FMT
" [%d]\n",
801 xhci
->er_start
, xhci
->er_size
);
804 static void xhci_run(XHCIState
*xhci
)
806 trace_usb_xhci_run();
807 xhci
->usbsts
&= ~USBSTS_HCH
;
810 static void xhci_stop(XHCIState
*xhci
)
812 trace_usb_xhci_stop();
813 xhci
->usbsts
|= USBSTS_HCH
;
814 xhci
->crcr_low
&= ~CRCR_CRR
;
817 static void xhci_set_ep_state(XHCIState
*xhci
, XHCIEPContext
*epctx
,
821 if (epctx
->state
== state
) {
825 pci_dma_read(&xhci
->pci_dev
, epctx
->pctx
, ctx
, sizeof(ctx
));
826 ctx
[0] &= ~EP_STATE_MASK
;
828 ctx
[2] = epctx
->ring
.dequeue
| epctx
->ring
.ccs
;
829 ctx
[3] = (epctx
->ring
.dequeue
>> 16) >> 16;
830 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT
" state=%d dequeue=%08x%08x\n",
831 epctx
->pctx
, state
, ctx
[3], ctx
[2]);
832 pci_dma_write(&xhci
->pci_dev
, epctx
->pctx
, ctx
, sizeof(ctx
));
833 epctx
->state
= state
;
836 static TRBCCode
xhci_enable_ep(XHCIState
*xhci
, unsigned int slotid
,
837 unsigned int epid
, dma_addr_t pctx
,
841 XHCIEPContext
*epctx
;
845 trace_usb_xhci_ep_enable(slotid
, epid
);
846 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
847 assert(epid
>= 1 && epid
<= 31);
849 slot
= &xhci
->slots
[slotid
-1];
850 if (slot
->eps
[epid
-1]) {
851 fprintf(stderr
, "xhci: slot %d ep %d already enabled!\n", slotid
, epid
);
855 epctx
= g_malloc(sizeof(XHCIEPContext
));
856 memset(epctx
, 0, sizeof(XHCIEPContext
));
858 slot
->eps
[epid
-1] = epctx
;
860 dequeue
= xhci_addr64(ctx
[2] & ~0xf, ctx
[3]);
861 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
);
862 epctx
->ring
.ccs
= ctx
[2] & 1;
864 epctx
->type
= (ctx
[1] >> EP_TYPE_SHIFT
) & EP_TYPE_MASK
;
865 DPRINTF("xhci: endpoint %d.%d type is %d\n", epid
/2, epid
%2, epctx
->type
);
867 epctx
->max_psize
= ctx
[1]>>16;
868 epctx
->max_psize
*= 1+((ctx
[1]>>8)&0xff);
869 epctx
->has_bg
= false;
870 if (epctx
->type
== ET_ISO_IN
) {
871 epctx
->has_bg
= true;
873 DPRINTF("xhci: endpoint %d.%d max transaction (burst) size is %d\n",
874 epid
/2, epid
%2, epctx
->max_psize
);
875 for (i
= 0; i
< ARRAY_SIZE(epctx
->transfers
); i
++) {
876 usb_packet_init(&epctx
->transfers
[i
].packet
);
879 epctx
->state
= EP_RUNNING
;
880 ctx
[0] &= ~EP_STATE_MASK
;
881 ctx
[0] |= EP_RUNNING
;
886 static int xhci_ep_nuke_xfers(XHCIState
*xhci
, unsigned int slotid
,
890 XHCIEPContext
*epctx
;
891 int i
, xferi
, killed
= 0;
892 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
893 assert(epid
>= 1 && epid
<= 31);
895 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid
, epid
);
897 slot
= &xhci
->slots
[slotid
-1];
899 if (!slot
->eps
[epid
-1]) {
903 epctx
= slot
->eps
[epid
-1];
905 xferi
= epctx
->next_xfer
;
906 for (i
= 0; i
< TD_QUEUE
; i
++) {
907 XHCITransfer
*t
= &epctx
->transfers
[xferi
];
908 if (t
->running_async
) {
909 usb_cancel_packet(&t
->packet
);
910 t
->running_async
= 0;
912 DPRINTF("xhci: cancelling transfer %d, waiting for it to complete...\n", i
);
915 if (t
->running_retry
) {
916 t
->running_retry
= 0;
919 if (t
->backgrounded
) {
931 t
->trb_count
= t
->trb_alloced
= 0;
932 t
->data_length
= t
->data_alloced
= 0;
933 xferi
= (xferi
+ 1) % TD_QUEUE
;
936 xferi
= epctx
->next_bg
;
937 for (i
= 0; i
< BG_XFERS
; i
++) {
938 XHCITransfer
*t
= &epctx
->bg_transfers
[xferi
];
939 if (t
->running_async
) {
940 usb_cancel_packet(&t
->packet
);
941 t
->running_async
= 0;
943 DPRINTF("xhci: cancelling bg transfer %d, waiting for it to complete...\n", i
);
951 xferi
= (xferi
+ 1) % BG_XFERS
;
957 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
961 XHCIEPContext
*epctx
;
963 trace_usb_xhci_ep_disable(slotid
, epid
);
964 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
965 assert(epid
>= 1 && epid
<= 31);
967 slot
= &xhci
->slots
[slotid
-1];
969 if (!slot
->eps
[epid
-1]) {
970 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid
, epid
);
974 xhci_ep_nuke_xfers(xhci
, slotid
, epid
);
976 epctx
= slot
->eps
[epid
-1];
978 xhci_set_ep_state(xhci
, epctx
, EP_DISABLED
);
981 slot
->eps
[epid
-1] = NULL
;
986 static TRBCCode
xhci_stop_ep(XHCIState
*xhci
, unsigned int slotid
,
990 XHCIEPContext
*epctx
;
992 trace_usb_xhci_ep_stop(slotid
, epid
);
993 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
995 if (epid
< 1 || epid
> 31) {
996 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
1000 slot
= &xhci
->slots
[slotid
-1];
1002 if (!slot
->eps
[epid
-1]) {
1003 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1004 return CC_EP_NOT_ENABLED_ERROR
;
1007 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
) > 0) {
1008 fprintf(stderr
, "xhci: FIXME: endpoint stopped w/ xfers running, "
1009 "data might be lost\n");
1012 epctx
= slot
->eps
[epid
-1];
1014 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
1019 static TRBCCode
xhci_reset_ep(XHCIState
*xhci
, unsigned int slotid
,
1023 XHCIEPContext
*epctx
;
1026 trace_usb_xhci_ep_reset(slotid
, epid
);
1027 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1029 if (epid
< 1 || epid
> 31) {
1030 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
1031 return CC_TRB_ERROR
;
1034 slot
= &xhci
->slots
[slotid
-1];
1036 if (!slot
->eps
[epid
-1]) {
1037 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1038 return CC_EP_NOT_ENABLED_ERROR
;
1041 epctx
= slot
->eps
[epid
-1];
1043 if (epctx
->state
!= EP_HALTED
) {
1044 fprintf(stderr
, "xhci: reset EP while EP %d not halted (%d)\n",
1045 epid
, epctx
->state
);
1046 return CC_CONTEXT_STATE_ERROR
;
1049 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
) > 0) {
1050 fprintf(stderr
, "xhci: FIXME: endpoint reset w/ xfers running, "
1051 "data might be lost\n");
1054 uint8_t ep
= epid
>>1;
1060 dev
= xhci
->ports
[xhci
->slots
[slotid
-1].port
-1].port
.dev
;
1062 return CC_USB_TRANSACTION_ERROR
;
1065 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
1070 static TRBCCode
xhci_set_ep_dequeue(XHCIState
*xhci
, unsigned int slotid
,
1071 unsigned int epid
, uint64_t pdequeue
)
1074 XHCIEPContext
*epctx
;
1077 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1079 if (epid
< 1 || epid
> 31) {
1080 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
1081 return CC_TRB_ERROR
;
1084 DPRINTF("xhci_set_ep_dequeue(%d, %d, %016"PRIx64
")\n", slotid
, epid
, pdequeue
);
1085 dequeue
= xhci_mask64(pdequeue
);
1087 slot
= &xhci
->slots
[slotid
-1];
1089 if (!slot
->eps
[epid
-1]) {
1090 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1091 return CC_EP_NOT_ENABLED_ERROR
;
1094 epctx
= slot
->eps
[epid
-1];
1097 if (epctx
->state
!= EP_STOPPED
) {
1098 fprintf(stderr
, "xhci: set EP dequeue pointer while EP %d not stopped\n", epid
);
1099 return CC_CONTEXT_STATE_ERROR
;
1102 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
& ~0xF);
1103 epctx
->ring
.ccs
= dequeue
& 1;
1105 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
1110 static int xhci_xfer_data(XHCITransfer
*xfer
, uint8_t *data
,
1111 unsigned int length
, bool in_xfer
, bool out_xfer
,
1116 unsigned int transferred
= 0;
1117 unsigned int left
= length
;
1120 XHCIEvent event
= {ER_TRANSFER
, CC_SUCCESS
};
1121 XHCIState
*xhci
= xfer
->xhci
;
1123 DPRINTF("xhci_xfer_data(len=%d, in_xfer=%d, out_xfer=%d, report=%d)\n",
1124 length
, in_xfer
, out_xfer
, report
);
1126 assert(!(in_xfer
&& out_xfer
));
1128 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1129 XHCITRB
*trb
= &xfer
->trbs
[i
];
1131 unsigned int chunk
= 0;
1133 switch (TRB_TYPE(*trb
)) {
1135 if ((!(trb
->control
& TRB_TR_DIR
)) != (!in_xfer
)) {
1136 fprintf(stderr
, "xhci: data direction mismatch for TR_DATA\n");
1143 addr
= xhci_mask64(trb
->parameter
);
1144 chunk
= trb
->status
& 0x1ffff;
1149 if (in_xfer
|| out_xfer
) {
1150 if (trb
->control
& TRB_TR_IDT
) {
1152 if (chunk
> 8 || in_xfer
) {
1153 fprintf(stderr
, "xhci: invalid immediate data TRB\n");
1157 idata
= le64_to_cpu(trb
->parameter
);
1158 memcpy(data
, &idata
, chunk
);
1160 DPRINTF("xhci_xfer_data: r/w(%d) %d bytes at "
1161 DMA_ADDR_FMT
"\n", in_xfer
, chunk
, addr
);
1163 pci_dma_write(&xhci
->pci_dev
, addr
, data
, chunk
);
1165 pci_dma_read(&xhci
->pci_dev
, addr
, data
, chunk
);
1168 unsigned int count
= chunk
;
1174 for (i
= 0; i
< count
; i
++) {
1175 DPRINTF(" %02x", data
[i
]);
1184 transferred
+= chunk
;
1192 if (report
&& !reported
&& (trb
->control
& TRB_TR_IOC
||
1193 (shortpkt
&& (trb
->control
& TRB_TR_ISP
)))) {
1194 event
.slotid
= xfer
->slotid
;
1195 event
.epid
= xfer
->epid
;
1196 event
.length
= (trb
->status
& 0x1ffff) - chunk
;
1198 event
.ptr
= trb
->addr
;
1199 if (xfer
->status
== CC_SUCCESS
) {
1200 event
.ccode
= shortpkt
? CC_SHORT_PACKET
: CC_SUCCESS
;
1202 event
.ccode
= xfer
->status
;
1204 if (TRB_TYPE(*trb
) == TR_EVDATA
) {
1205 event
.ptr
= trb
->parameter
;
1206 event
.flags
|= TRB_EV_ED
;
1207 event
.length
= edtla
& 0xffffff;
1208 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event
.length
);
1211 xhci_event(xhci
, &event
);
1218 static void xhci_stall_ep(XHCITransfer
*xfer
)
1220 XHCIState
*xhci
= xfer
->xhci
;
1221 XHCISlot
*slot
= &xhci
->slots
[xfer
->slotid
-1];
1222 XHCIEPContext
*epctx
= slot
->eps
[xfer
->epid
-1];
1224 epctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1225 epctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1226 xhci_set_ep_state(xhci
, epctx
, EP_HALTED
);
1227 DPRINTF("xhci: stalled slot %d ep %d\n", xfer
->slotid
, xfer
->epid
);
1228 DPRINTF("xhci: will continue at "DMA_ADDR_FMT
"\n", epctx
->ring
.dequeue
);
1231 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
,
1232 XHCIEPContext
*epctx
);
1234 static void xhci_bg_update(XHCIState
*xhci
, XHCIEPContext
*epctx
)
1236 if (epctx
->bg_updating
) {
1239 DPRINTF("xhci_bg_update(%p, %p)\n", xhci
, epctx
);
1240 assert(epctx
->has_bg
);
1241 DPRINTF("xhci: fg=%d bg=%d\n", epctx
->comp_xfer
, epctx
->next_bg
);
1242 epctx
->bg_updating
= 1;
1243 while (epctx
->transfers
[epctx
->comp_xfer
].backgrounded
&&
1244 epctx
->bg_transfers
[epctx
->next_bg
].complete
) {
1245 XHCITransfer
*fg
= &epctx
->transfers
[epctx
->comp_xfer
];
1246 XHCITransfer
*bg
= &epctx
->bg_transfers
[epctx
->next_bg
];
1248 DPRINTF("xhci: completing fg %d from bg %d.%d (stat: %d)\n",
1249 epctx
->comp_xfer
, epctx
->next_bg
, bg
->cur_pkt
,
1250 bg
->usbxfer
->iso_packet_desc
[bg
->cur_pkt
].status
1253 assert(epctx
->type
== ET_ISO_IN
);
1254 assert(bg
->iso_xfer
);
1255 assert(bg
->in_xfer
);
1256 uint8_t *p
= bg
->data
+ bg
->cur_pkt
* bg
->pktsize
;
1258 int len
= bg
->usbxfer
->iso_packet_desc
[bg
->cur_pkt
].actual_length
;
1259 fg
->status
= libusb_to_ccode(bg
->usbxfer
->iso_packet_desc
[bg
->cur_pkt
].status
);
1265 fg
->backgrounded
= 0;
1267 if (fg
->status
== CC_STALL_ERROR
) {
1271 xhci_xfer_data(fg
, p
, len
, 1, 0, 1);
1274 if (epctx
->comp_xfer
== TD_QUEUE
) {
1275 epctx
->comp_xfer
= 0;
1277 DPRINTF("next fg xfer: %d\n", epctx
->comp_xfer
);
1279 if (bg
->cur_pkt
== bg
->pkts
) {
1281 if (xhci_submit(xhci
, bg
, epctx
) < 0) {
1282 fprintf(stderr
, "xhci: bg resubmit failed\n");
1285 if (epctx
->next_bg
== BG_XFERS
) {
1288 DPRINTF("next bg xfer: %d\n", epctx
->next_bg
);
1290 xhci_kick_ep(xhci
, fg
->slotid
, fg
->epid
);
1293 epctx
->bg_updating
= 0;
1297 static void xhci_xfer_cb(struct libusb_transfer
*transfer
)
1302 xfer
= (XHCITransfer
*)transfer
->user_data
;
1305 DPRINTF("xhci_xfer_cb(slot=%d, ep=%d, status=%d)\n", xfer
->slotid
,
1306 xfer
->epid
, transfer
->status
);
1308 assert(xfer
->slotid
>= 1 && xfer
->slotid
<= MAXSLOTS
);
1309 assert(xfer
->epid
>= 1 && xfer
->epid
<= 31);
1311 if (xfer
->cancelled
) {
1312 DPRINTF("xhci: transfer cancelled, not reporting anything\n");
1317 XHCIEPContext
*epctx
;
1319 slot
= &xhci
->slots
[xfer
->slotid
-1];
1320 assert(slot
->eps
[xfer
->epid
-1]);
1321 epctx
= slot
->eps
[xfer
->epid
-1];
1323 if (xfer
->bg_xfer
) {
1324 DPRINTF("xhci: background transfer, updating\n");
1327 xhci_bg_update(xhci
, epctx
);
1331 if (xfer
->iso_xfer
) {
1332 transfer
->status
= transfer
->iso_packet_desc
[0].status
;
1333 transfer
->actual_length
= transfer
->iso_packet_desc
[0].actual_length
;
1336 xfer
->status
= libusb_to_ccode(transfer
->status
);
1341 if (transfer
->status
== LIBUSB_TRANSFER_STALL
)
1342 xhci_stall_ep(xhci
, epctx
, xfer
);
1344 DPRINTF("xhci: transfer actual length = %d\n", transfer
->actual_length
);
1346 if (xfer
->in_xfer
) {
1347 if (xfer
->epid
== 1) {
1348 xhci_xfer_data(xhci
, xfer
, xfer
->data
+ 8,
1349 transfer
->actual_length
, 1, 0, 1);
1351 xhci_xfer_data(xhci
, xfer
, xfer
->data
,
1352 transfer
->actual_length
, 1, 0, 1);
1355 xhci_xfer_data(xhci
, xfer
, NULL
, transfer
->actual_length
, 0, 0, 1);
1358 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
);
1361 static int xhci_hle_control(XHCIState
*xhci
, XHCITransfer
*xfer
,
1362 uint8_t bmRequestType
, uint8_t bRequest
,
1363 uint16_t wValue
, uint16_t wIndex
, uint16_t wLength
)
1365 uint16_t type_req
= (bmRequestType
<< 8) | bRequest
;
1368 case 0x0000 | USB_REQ_SET_CONFIGURATION
:
1369 DPRINTF("xhci: HLE switch configuration\n");
1370 return xhci_switch_config(xhci
, xfer
->slotid
, wValue
) == 0;
1371 case 0x0100 | USB_REQ_SET_INTERFACE
:
1372 DPRINTF("xhci: HLE set interface altsetting\n");
1373 return xhci_set_iface_alt(xhci
, xfer
->slotid
, wIndex
, wValue
) == 0;
1374 case 0x0200 | USB_REQ_CLEAR_FEATURE
:
1375 if (wValue
== 0) { // endpoint halt
1376 DPRINTF("xhci: HLE clear halt\n");
1377 return xhci_clear_halt(xhci
, xfer
->slotid
, wIndex
);
1379 case 0x0000 | USB_REQ_SET_ADDRESS
:
1380 fprintf(stderr
, "xhci: warn: illegal SET_ADDRESS request\n");
1388 static int xhci_setup_packet(XHCITransfer
*xfer
, USBDevice
*dev
)
1393 dir
= xfer
->in_xfer
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1394 ep
= usb_ep_get(dev
, dir
, xfer
->epid
>> 1);
1395 usb_packet_setup(&xfer
->packet
, dir
, ep
);
1396 usb_packet_addbuf(&xfer
->packet
, xfer
->data
, xfer
->data_length
);
1397 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
1398 xfer
->packet
.pid
, dev
->addr
, ep
->nr
);
1402 static int xhci_complete_packet(XHCITransfer
*xfer
, int ret
)
1404 if (ret
== USB_RET_ASYNC
) {
1405 trace_usb_xhci_xfer_async(xfer
);
1406 xfer
->running_async
= 1;
1407 xfer
->running_retry
= 0;
1409 xfer
->cancelled
= 0;
1411 } else if (ret
== USB_RET_NAK
) {
1412 trace_usb_xhci_xfer_nak(xfer
);
1413 xfer
->running_async
= 0;
1414 xfer
->running_retry
= 1;
1416 xfer
->cancelled
= 0;
1419 xfer
->running_async
= 0;
1420 xfer
->running_retry
= 0;
1425 xfer
->status
= CC_SUCCESS
;
1426 xhci_xfer_data(xfer
, xfer
->data
, ret
, xfer
->in_xfer
, 0, 1);
1427 trace_usb_xhci_xfer_success(xfer
, ret
);
1432 trace_usb_xhci_xfer_error(xfer
, ret
);
1435 xfer
->status
= CC_USB_TRANSACTION_ERROR
;
1436 xhci_xfer_data(xfer
, xfer
->data
, 0, xfer
->in_xfer
, 0, 1);
1437 xhci_stall_ep(xfer
);
1440 xfer
->status
= CC_STALL_ERROR
;
1441 xhci_xfer_data(xfer
, xfer
->data
, 0, xfer
->in_xfer
, 0, 1);
1442 xhci_stall_ep(xfer
);
1445 fprintf(stderr
, "%s: FIXME: ret = %d\n", __FUNCTION__
, ret
);
1451 static USBDevice
*xhci_find_device(XHCIPort
*port
, uint8_t addr
)
1453 if (!(port
->portsc
& PORTSC_PED
)) {
1456 return usb_find_device(&port
->port
, addr
);
1459 static int xhci_fire_ctl_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
)
1461 XHCITRB
*trb_setup
, *trb_status
;
1462 uint8_t bmRequestType
;
1468 trb_setup
= &xfer
->trbs
[0];
1469 trb_status
= &xfer
->trbs
[xfer
->trb_count
-1];
1471 trace_usb_xhci_xfer_start(xfer
, xfer
->slotid
, xfer
->epid
,
1472 trb_setup
->parameter
>> 48);
1474 /* at most one Event Data TRB allowed after STATUS */
1475 if (TRB_TYPE(*trb_status
) == TR_EVDATA
&& xfer
->trb_count
> 2) {
1479 /* do some sanity checks */
1480 if (TRB_TYPE(*trb_setup
) != TR_SETUP
) {
1481 fprintf(stderr
, "xhci: ep0 first TD not SETUP: %d\n",
1482 TRB_TYPE(*trb_setup
));
1485 if (TRB_TYPE(*trb_status
) != TR_STATUS
) {
1486 fprintf(stderr
, "xhci: ep0 last TD not STATUS: %d\n",
1487 TRB_TYPE(*trb_status
));
1490 if (!(trb_setup
->control
& TRB_TR_IDT
)) {
1491 fprintf(stderr
, "xhci: Setup TRB doesn't have IDT set\n");
1494 if ((trb_setup
->status
& 0x1ffff) != 8) {
1495 fprintf(stderr
, "xhci: Setup TRB has bad length (%d)\n",
1496 (trb_setup
->status
& 0x1ffff));
1500 bmRequestType
= trb_setup
->parameter
;
1501 wLength
= trb_setup
->parameter
>> 48;
1503 if (xfer
->data
&& xfer
->data_alloced
< wLength
) {
1504 xfer
->data_alloced
= 0;
1509 DPRINTF("xhci: alloc %d bytes data\n", wLength
);
1510 xfer
->data
= g_malloc(wLength
+1);
1511 xfer
->data_alloced
= wLength
;
1513 xfer
->data_length
= wLength
;
1515 port
= &xhci
->ports
[xhci
->slots
[xfer
->slotid
-1].port
-1];
1516 dev
= xhci_find_device(port
, xhci
->slots
[xfer
->slotid
-1].devaddr
);
1518 fprintf(stderr
, "xhci: slot %d port %d has no device\n", xfer
->slotid
,
1519 xhci
->slots
[xfer
->slotid
-1].port
);
1523 xfer
->in_xfer
= bmRequestType
& USB_DIR_IN
;
1524 xfer
->iso_xfer
= false;
1526 xhci_setup_packet(xfer
, dev
);
1527 xfer
->packet
.parameter
= trb_setup
->parameter
;
1528 if (!xfer
->in_xfer
) {
1529 xhci_xfer_data(xfer
, xfer
->data
, wLength
, 0, 1, 0);
1532 ret
= usb_handle_packet(dev
, &xfer
->packet
);
1534 xhci_complete_packet(xfer
, ret
);
1535 if (!xfer
->running_async
&& !xfer
->running_retry
) {
1536 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
);
1541 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1547 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer
->slotid
, xfer
->epid
);
1549 xfer
->in_xfer
= epctx
->type
>>2;
1551 if (xfer
->data
&& xfer
->data_alloced
< xfer
->data_length
) {
1552 xfer
->data_alloced
= 0;
1556 if (!xfer
->data
&& xfer
->data_length
) {
1557 DPRINTF("xhci: alloc %d bytes data\n", xfer
->data_length
);
1558 xfer
->data
= g_malloc(xfer
->data_length
);
1559 xfer
->data_alloced
= xfer
->data_length
;
1561 if (epctx
->type
== ET_ISO_IN
|| epctx
->type
== ET_ISO_OUT
) {
1562 if (!xfer
->bg_xfer
) {
1569 port
= &xhci
->ports
[xhci
->slots
[xfer
->slotid
-1].port
-1];
1570 dev
= xhci_find_device(port
, xhci
->slots
[xfer
->slotid
-1].devaddr
);
1572 fprintf(stderr
, "xhci: slot %d port %d has no device\n", xfer
->slotid
,
1573 xhci
->slots
[xfer
->slotid
-1].port
);
1577 xhci_setup_packet(xfer
, dev
);
1579 switch(epctx
->type
) {
1590 fprintf(stderr
, "xhci: unknown or unhandled EP "
1591 "(type %d, in %d, ep %02x)\n",
1592 epctx
->type
, xfer
->in_xfer
, xfer
->epid
);
1596 if (!xfer
->in_xfer
) {
1597 xhci_xfer_data(xfer
, xfer
->data
, xfer
->data_length
, 0, 1, 0);
1599 ret
= usb_handle_packet(dev
, &xfer
->packet
);
1601 xhci_complete_packet(xfer
, ret
);
1602 if (!xfer
->running_async
&& !xfer
->running_retry
) {
1603 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
);
1608 static int xhci_fire_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1611 unsigned int length
= 0;
1614 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1615 trb
= &xfer
->trbs
[i
];
1616 if (TRB_TYPE(*trb
) == TR_NORMAL
|| TRB_TYPE(*trb
) == TR_ISOCH
) {
1617 length
+= trb
->status
& 0x1ffff;
1621 trace_usb_xhci_xfer_start(xfer
, xfer
->slotid
, xfer
->epid
, length
);
1623 if (!epctx
->has_bg
) {
1624 xfer
->data_length
= length
;
1625 xfer
->backgrounded
= 0;
1626 return xhci_submit(xhci
, xfer
, epctx
);
1628 if (!epctx
->bg_running
) {
1629 for (i
= 0; i
< BG_XFERS
; i
++) {
1630 XHCITransfer
*t
= &epctx
->bg_transfers
[i
];
1632 t
->epid
= xfer
->epid
;
1633 t
->slotid
= xfer
->slotid
;
1635 t
->pktsize
= epctx
->max_psize
;
1636 t
->data_length
= t
->pkts
* t
->pktsize
;
1638 if (xhci_submit(xhci
, t
, epctx
) < 0) {
1639 fprintf(stderr
, "xhci: bg submit failed\n");
1643 epctx
->bg_running
= 1;
1645 xfer
->backgrounded
= 1;
1646 xhci_bg_update(xhci
, epctx
);
1651 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
, unsigned int epid
)
1653 XHCIEPContext
*epctx
;
1657 trace_usb_xhci_ep_kick(slotid
, epid
);
1658 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1659 assert(epid
>= 1 && epid
<= 31);
1661 if (!xhci
->slots
[slotid
-1].enabled
) {
1662 fprintf(stderr
, "xhci: xhci_kick_ep for disabled slot %d\n", slotid
);
1665 epctx
= xhci
->slots
[slotid
-1].eps
[epid
-1];
1667 fprintf(stderr
, "xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
1673 /* retry nak'ed transfer */
1674 XHCITransfer
*xfer
= epctx
->retry
;
1677 trace_usb_xhci_xfer_retry(xfer
);
1678 assert(xfer
->running_retry
);
1679 xhci_setup_packet(xfer
, xfer
->packet
.ep
->dev
);
1680 result
= usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1681 if (result
== USB_RET_NAK
) {
1684 xhci_complete_packet(xfer
, result
);
1685 assert(!xfer
->running_retry
);
1686 epctx
->retry
= NULL
;
1689 if (epctx
->state
== EP_HALTED
) {
1690 DPRINTF("xhci: ep halted, not running schedule\n");
1694 xhci_set_ep_state(xhci
, epctx
, EP_RUNNING
);
1697 XHCITransfer
*xfer
= &epctx
->transfers
[epctx
->next_xfer
];
1698 if (xfer
->running_async
|| xfer
->running_retry
|| xfer
->backgrounded
) {
1701 length
= xhci_ring_chain_length(xhci
, &epctx
->ring
);
1704 } else if (length
== 0) {
1707 if (xfer
->trbs
&& xfer
->trb_alloced
< length
) {
1708 xfer
->trb_count
= 0;
1709 xfer
->trb_alloced
= 0;
1714 xfer
->trbs
= g_malloc(sizeof(XHCITRB
) * length
);
1715 xfer
->trb_alloced
= length
;
1717 xfer
->trb_count
= length
;
1719 for (i
= 0; i
< length
; i
++) {
1720 assert(xhci_ring_fetch(xhci
, &epctx
->ring
, &xfer
->trbs
[i
], NULL
));
1724 xfer
->slotid
= slotid
;
1727 if (xhci_fire_ctl_transfer(xhci
, xfer
) >= 0) {
1728 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
1730 fprintf(stderr
, "xhci: error firing CTL transfer\n");
1733 if (xhci_fire_transfer(xhci
, xfer
, epctx
) >= 0) {
1734 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
1736 fprintf(stderr
, "xhci: error firing data transfer\n");
1740 if (epctx
->state
== EP_HALTED
) {
1743 if (xfer
->running_retry
) {
1744 DPRINTF("xhci: xfer nacked, stopping schedule\n");
1745 epctx
->retry
= xfer
;
1751 static TRBCCode
xhci_enable_slot(XHCIState
*xhci
, unsigned int slotid
)
1753 trace_usb_xhci_slot_enable(slotid
);
1754 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1755 xhci
->slots
[slotid
-1].enabled
= 1;
1756 xhci
->slots
[slotid
-1].port
= 0;
1757 memset(xhci
->slots
[slotid
-1].eps
, 0, sizeof(XHCIEPContext
*)*31);
1762 static TRBCCode
xhci_disable_slot(XHCIState
*xhci
, unsigned int slotid
)
1766 trace_usb_xhci_slot_disable(slotid
);
1767 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1769 for (i
= 1; i
<= 31; i
++) {
1770 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
1771 xhci_disable_ep(xhci
, slotid
, i
);
1775 xhci
->slots
[slotid
-1].enabled
= 0;
1779 static TRBCCode
xhci_address_slot(XHCIState
*xhci
, unsigned int slotid
,
1780 uint64_t pictx
, bool bsr
)
1784 dma_addr_t ictx
, octx
, dcbaap
;
1786 uint32_t ictl_ctx
[2];
1787 uint32_t slot_ctx
[4];
1788 uint32_t ep0_ctx
[5];
1793 trace_usb_xhci_slot_address(slotid
);
1794 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1796 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
1797 pci_dma_read(&xhci
->pci_dev
, dcbaap
+ 8*slotid
, &poctx
, sizeof(poctx
));
1798 ictx
= xhci_mask64(pictx
);
1799 octx
= xhci_mask64(le64_to_cpu(poctx
));
1801 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
1802 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
1804 pci_dma_read(&xhci
->pci_dev
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
1806 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] != 0x3) {
1807 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1808 ictl_ctx
[0], ictl_ctx
[1]);
1809 return CC_TRB_ERROR
;
1812 pci_dma_read(&xhci
->pci_dev
, ictx
+32, slot_ctx
, sizeof(slot_ctx
));
1813 pci_dma_read(&xhci
->pci_dev
, ictx
+64, ep0_ctx
, sizeof(ep0_ctx
));
1815 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
1816 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1818 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
1819 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
1821 port
= (slot_ctx
[1]>>16) & 0xFF;
1822 dev
= xhci
->ports
[port
-1].port
.dev
;
1824 if (port
< 1 || port
> MAXPORTS
) {
1825 fprintf(stderr
, "xhci: bad port %d\n", port
);
1826 return CC_TRB_ERROR
;
1828 fprintf(stderr
, "xhci: port %d not connected\n", port
);
1829 return CC_USB_TRANSACTION_ERROR
;
1832 for (i
= 0; i
< MAXSLOTS
; i
++) {
1833 if (xhci
->slots
[i
].port
== port
) {
1834 fprintf(stderr
, "xhci: port %d already assigned to slot %d\n",
1836 return CC_TRB_ERROR
;
1840 slot
= &xhci
->slots
[slotid
-1];
1845 slot_ctx
[3] = SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
1847 slot
->devaddr
= xhci
->devaddr
++;
1848 slot_ctx
[3] = (SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
) | slot
->devaddr
;
1849 DPRINTF("xhci: device address is %d\n", slot
->devaddr
);
1850 usb_device_handle_control(dev
, NULL
,
1851 DeviceOutRequest
| USB_REQ_SET_ADDRESS
,
1852 slot
->devaddr
, 0, 0, NULL
);
1855 res
= xhci_enable_ep(xhci
, slotid
, 1, octx
+32, ep0_ctx
);
1857 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1858 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1859 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
1860 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
1862 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1863 pci_dma_write(&xhci
->pci_dev
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
1869 static TRBCCode
xhci_configure_slot(XHCIState
*xhci
, unsigned int slotid
,
1870 uint64_t pictx
, bool dc
)
1872 dma_addr_t ictx
, octx
;
1873 uint32_t ictl_ctx
[2];
1874 uint32_t slot_ctx
[4];
1875 uint32_t islot_ctx
[4];
1880 trace_usb_xhci_slot_configure(slotid
);
1881 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1883 ictx
= xhci_mask64(pictx
);
1884 octx
= xhci
->slots
[slotid
-1].ctx
;
1886 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
1887 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
1890 for (i
= 2; i
<= 31; i
++) {
1891 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
1892 xhci_disable_ep(xhci
, slotid
, i
);
1896 pci_dma_read(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1897 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
1898 slot_ctx
[3] |= SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
;
1899 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1900 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1901 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1906 pci_dma_read(&xhci
->pci_dev
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
1908 if ((ictl_ctx
[0] & 0x3) != 0x0 || (ictl_ctx
[1] & 0x3) != 0x1) {
1909 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1910 ictl_ctx
[0], ictl_ctx
[1]);
1911 return CC_TRB_ERROR
;
1914 pci_dma_read(&xhci
->pci_dev
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
1915 pci_dma_read(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1917 if (SLOT_STATE(slot_ctx
[3]) < SLOT_ADDRESSED
) {
1918 fprintf(stderr
, "xhci: invalid slot state %08x\n", slot_ctx
[3]);
1919 return CC_CONTEXT_STATE_ERROR
;
1922 for (i
= 2; i
<= 31; i
++) {
1923 if (ictl_ctx
[0] & (1<<i
)) {
1924 xhci_disable_ep(xhci
, slotid
, i
);
1926 if (ictl_ctx
[1] & (1<<i
)) {
1927 pci_dma_read(&xhci
->pci_dev
, ictx
+32+(32*i
), ep_ctx
,
1929 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
1930 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
1931 ep_ctx
[3], ep_ctx
[4]);
1932 xhci_disable_ep(xhci
, slotid
, i
);
1933 res
= xhci_enable_ep(xhci
, slotid
, i
, octx
+(32*i
), ep_ctx
);
1934 if (res
!= CC_SUCCESS
) {
1937 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
1938 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
1939 ep_ctx
[3], ep_ctx
[4]);
1940 pci_dma_write(&xhci
->pci_dev
, octx
+(32*i
), ep_ctx
, sizeof(ep_ctx
));
1944 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
1945 slot_ctx
[3] |= SLOT_CONFIGURED
<< SLOT_STATE_SHIFT
;
1946 slot_ctx
[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK
<< SLOT_CONTEXT_ENTRIES_SHIFT
);
1947 slot_ctx
[0] |= islot_ctx
[0] & (SLOT_CONTEXT_ENTRIES_MASK
<<
1948 SLOT_CONTEXT_ENTRIES_SHIFT
);
1949 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1950 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1952 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1958 static TRBCCode
xhci_evaluate_slot(XHCIState
*xhci
, unsigned int slotid
,
1961 dma_addr_t ictx
, octx
;
1962 uint32_t ictl_ctx
[2];
1963 uint32_t iep0_ctx
[5];
1964 uint32_t ep0_ctx
[5];
1965 uint32_t islot_ctx
[4];
1966 uint32_t slot_ctx
[4];
1968 trace_usb_xhci_slot_evaluate(slotid
);
1969 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1971 ictx
= xhci_mask64(pictx
);
1972 octx
= xhci
->slots
[slotid
-1].ctx
;
1974 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
1975 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
1977 pci_dma_read(&xhci
->pci_dev
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
1979 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] & ~0x3) {
1980 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1981 ictl_ctx
[0], ictl_ctx
[1]);
1982 return CC_TRB_ERROR
;
1985 if (ictl_ctx
[1] & 0x1) {
1986 pci_dma_read(&xhci
->pci_dev
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
1988 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
1989 islot_ctx
[0], islot_ctx
[1], islot_ctx
[2], islot_ctx
[3]);
1991 pci_dma_read(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1993 slot_ctx
[1] &= ~0xFFFF; /* max exit latency */
1994 slot_ctx
[1] |= islot_ctx
[1] & 0xFFFF;
1995 slot_ctx
[2] &= ~0xFF00000; /* interrupter target */
1996 slot_ctx
[2] |= islot_ctx
[2] & 0xFF000000;
1998 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1999 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2001 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
2004 if (ictl_ctx
[1] & 0x2) {
2005 pci_dma_read(&xhci
->pci_dev
, ictx
+64, iep0_ctx
, sizeof(iep0_ctx
));
2007 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2008 iep0_ctx
[0], iep0_ctx
[1], iep0_ctx
[2],
2009 iep0_ctx
[3], iep0_ctx
[4]);
2011 pci_dma_read(&xhci
->pci_dev
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2013 ep0_ctx
[1] &= ~0xFFFF0000; /* max packet size*/
2014 ep0_ctx
[1] |= iep0_ctx
[1] & 0xFFFF0000;
2016 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2017 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2019 pci_dma_write(&xhci
->pci_dev
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2025 static TRBCCode
xhci_reset_slot(XHCIState
*xhci
, unsigned int slotid
)
2027 uint32_t slot_ctx
[4];
2031 trace_usb_xhci_slot_reset(slotid
);
2032 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
2034 octx
= xhci
->slots
[slotid
-1].ctx
;
2036 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2038 for (i
= 2; i
<= 31; i
++) {
2039 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2040 xhci_disable_ep(xhci
, slotid
, i
);
2044 pci_dma_read(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
2045 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2046 slot_ctx
[3] |= SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2047 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2048 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2049 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
2054 static unsigned int xhci_get_slot(XHCIState
*xhci
, XHCIEvent
*event
, XHCITRB
*trb
)
2056 unsigned int slotid
;
2057 slotid
= (trb
->control
>> TRB_CR_SLOTID_SHIFT
) & TRB_CR_SLOTID_MASK
;
2058 if (slotid
< 1 || slotid
> MAXSLOTS
) {
2059 fprintf(stderr
, "xhci: bad slot id %d\n", slotid
);
2060 event
->ccode
= CC_TRB_ERROR
;
2062 } else if (!xhci
->slots
[slotid
-1].enabled
) {
2063 fprintf(stderr
, "xhci: slot id %d not enabled\n", slotid
);
2064 event
->ccode
= CC_SLOT_NOT_ENABLED_ERROR
;
2070 static TRBCCode
xhci_get_port_bandwidth(XHCIState
*xhci
, uint64_t pctx
)
2073 uint8_t bw_ctx
[MAXPORTS
+1];
2075 DPRINTF("xhci_get_port_bandwidth()\n");
2077 ctx
= xhci_mask64(pctx
);
2079 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT
"\n", ctx
);
2081 /* TODO: actually implement real values here */
2083 memset(&bw_ctx
[1], 80, MAXPORTS
); /* 80% */
2084 pci_dma_write(&xhci
->pci_dev
, ctx
, bw_ctx
, sizeof(bw_ctx
));
2089 static uint32_t rotl(uint32_t v
, unsigned count
)
2092 return (v
<< count
) | (v
>> (32 - count
));
2096 static uint32_t xhci_nec_challenge(uint32_t hi
, uint32_t lo
)
2099 val
= rotl(lo
- 0x49434878, 32 - ((hi
>>8) & 0x1F));
2100 val
+= rotl(lo
+ 0x49434878, hi
& 0x1F);
2101 val
-= rotl(hi
^ 0x49434878, (lo
>> 16) & 0x1F);
2105 static void xhci_via_challenge(XHCIState
*xhci
, uint64_t addr
)
2109 dma_addr_t paddr
= xhci_mask64(addr
);
2111 pci_dma_read(&xhci
->pci_dev
, paddr
, &buf
, 32);
2113 memcpy(obuf
, buf
, sizeof(obuf
));
2115 if ((buf
[0] & 0xff) == 2) {
2116 obuf
[0] = 0x49932000 + 0x54dc200 * buf
[2] + 0x7429b578 * buf
[3];
2117 obuf
[0] |= (buf
[2] * buf
[3]) & 0xff;
2118 obuf
[1] = 0x0132bb37 + 0xe89 * buf
[2] + 0xf09 * buf
[3];
2119 obuf
[2] = 0x0066c2e9 + 0x2091 * buf
[2] + 0x19bd * buf
[3];
2120 obuf
[3] = 0xd5281342 + 0x2cc9691 * buf
[2] + 0x2367662 * buf
[3];
2121 obuf
[4] = 0x0123c75c + 0x1595 * buf
[2] + 0x19ec * buf
[3];
2122 obuf
[5] = 0x00f695de + 0x26fd * buf
[2] + 0x3e9 * buf
[3];
2123 obuf
[6] = obuf
[2] ^ obuf
[3] ^ 0x29472956;
2124 obuf
[7] = obuf
[2] ^ obuf
[3] ^ 0x65866593;
2127 pci_dma_write(&xhci
->pci_dev
, paddr
, &obuf
, 32);
2130 static void xhci_process_commands(XHCIState
*xhci
)
2134 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_SUCCESS
};
2136 unsigned int i
, slotid
= 0;
2138 DPRINTF("xhci_process_commands()\n");
2139 if (!xhci_running(xhci
)) {
2140 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
2144 xhci
->crcr_low
|= CRCR_CRR
;
2146 while ((type
= xhci_ring_fetch(xhci
, &xhci
->cmd_ring
, &trb
, &addr
))) {
2149 case CR_ENABLE_SLOT
:
2150 for (i
= 0; i
< MAXSLOTS
; i
++) {
2151 if (!xhci
->slots
[i
].enabled
) {
2155 if (i
>= MAXSLOTS
) {
2156 fprintf(stderr
, "xhci: no device slots available\n");
2157 event
.ccode
= CC_NO_SLOTS_ERROR
;
2160 event
.ccode
= xhci_enable_slot(xhci
, slotid
);
2163 case CR_DISABLE_SLOT
:
2164 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2166 event
.ccode
= xhci_disable_slot(xhci
, slotid
);
2169 case CR_ADDRESS_DEVICE
:
2170 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2172 event
.ccode
= xhci_address_slot(xhci
, slotid
, trb
.parameter
,
2173 trb
.control
& TRB_CR_BSR
);
2176 case CR_CONFIGURE_ENDPOINT
:
2177 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2179 event
.ccode
= xhci_configure_slot(xhci
, slotid
, trb
.parameter
,
2180 trb
.control
& TRB_CR_DC
);
2183 case CR_EVALUATE_CONTEXT
:
2184 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2186 event
.ccode
= xhci_evaluate_slot(xhci
, slotid
, trb
.parameter
);
2189 case CR_STOP_ENDPOINT
:
2190 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2192 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2194 event
.ccode
= xhci_stop_ep(xhci
, slotid
, epid
);
2197 case CR_RESET_ENDPOINT
:
2198 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2200 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2202 event
.ccode
= xhci_reset_ep(xhci
, slotid
, epid
);
2205 case CR_SET_TR_DEQUEUE
:
2206 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2208 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2210 event
.ccode
= xhci_set_ep_dequeue(xhci
, slotid
, epid
,
2214 case CR_RESET_DEVICE
:
2215 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2217 event
.ccode
= xhci_reset_slot(xhci
, slotid
);
2220 case CR_GET_PORT_BANDWIDTH
:
2221 event
.ccode
= xhci_get_port_bandwidth(xhci
, trb
.parameter
);
2223 case CR_VENDOR_VIA_CHALLENGE_RESPONSE
:
2224 xhci_via_challenge(xhci
, trb
.parameter
);
2226 case CR_VENDOR_NEC_FIRMWARE_REVISION
:
2227 event
.type
= 48; /* NEC reply */
2228 event
.length
= 0x3025;
2230 case CR_VENDOR_NEC_CHALLENGE_RESPONSE
:
2232 uint32_t chi
= trb
.parameter
>> 32;
2233 uint32_t clo
= trb
.parameter
;
2234 uint32_t val
= xhci_nec_challenge(chi
, clo
);
2235 event
.length
= val
& 0xFFFF;
2236 event
.epid
= val
>> 16;
2238 event
.type
= 48; /* NEC reply */
2242 fprintf(stderr
, "xhci: unimplemented command %d\n", type
);
2243 event
.ccode
= CC_TRB_ERROR
;
2246 event
.slotid
= slotid
;
2247 xhci_event(xhci
, &event
);
2251 static void xhci_update_port(XHCIState
*xhci
, XHCIPort
*port
, int is_detach
)
2253 int nr
= port
->port
.index
+ 1;
2255 port
->portsc
= PORTSC_PP
;
2256 if (port
->port
.dev
&& port
->port
.dev
->attached
&& !is_detach
) {
2257 port
->portsc
|= PORTSC_CCS
;
2258 switch (port
->port
.dev
->speed
) {
2260 port
->portsc
|= PORTSC_SPEED_LOW
;
2262 case USB_SPEED_FULL
:
2263 port
->portsc
|= PORTSC_SPEED_FULL
;
2265 case USB_SPEED_HIGH
:
2266 port
->portsc
|= PORTSC_SPEED_HIGH
;
2271 if (xhci_running(xhci
)) {
2272 port
->portsc
|= PORTSC_CSC
;
2273 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
, nr
<< 24};
2274 xhci_event(xhci
, &ev
);
2275 DPRINTF("xhci: port change event for port %d\n", nr
);
2279 static void xhci_reset(DeviceState
*dev
)
2281 XHCIState
*xhci
= DO_UPCAST(XHCIState
, pci_dev
.qdev
, dev
);
2284 trace_usb_xhci_reset();
2285 if (!(xhci
->usbsts
& USBSTS_HCH
)) {
2286 fprintf(stderr
, "xhci: reset while running!\n");
2290 xhci
->usbsts
= USBSTS_HCH
;
2293 xhci
->crcr_high
= 0;
2294 xhci
->dcbaap_low
= 0;
2295 xhci
->dcbaap_high
= 0;
2299 for (i
= 0; i
< MAXSLOTS
; i
++) {
2300 xhci_disable_slot(xhci
, i
+1);
2303 for (i
= 0; i
< MAXPORTS
; i
++) {
2304 xhci_update_port(xhci
, xhci
->ports
+ i
, 0);
2311 xhci
->erstba_low
= 0;
2312 xhci
->erstba_high
= 0;
2314 xhci
->erdp_high
= 0;
2316 xhci
->er_ep_idx
= 0;
2319 xhci
->ev_buffer_put
= 0;
2320 xhci
->ev_buffer_get
= 0;
2323 static uint32_t xhci_cap_read(XHCIState
*xhci
, uint32_t reg
)
2328 case 0x00: /* HCIVERSION, CAPLENGTH */
2329 ret
= 0x01000000 | LEN_CAP
;
2331 case 0x04: /* HCSPARAMS 1 */
2332 ret
= (MAXPORTS
<<24) | (MAXINTRS
<<8) | MAXSLOTS
;
2334 case 0x08: /* HCSPARAMS 2 */
2337 case 0x0c: /* HCSPARAMS 3 */
2340 case 0x10: /* HCCPARAMS */
2341 if (sizeof(dma_addr_t
) == 4) {
2347 case 0x14: /* DBOFF */
2350 case 0x18: /* RTSOFF */
2354 /* extended capabilities */
2355 case 0x20: /* Supported Protocol:00 */
2356 ret
= 0x02000402; /* USB 2.0 */
2358 case 0x24: /* Supported Protocol:04 */
2359 ret
= 0x20425455; /* "USB " */
2361 case 0x28: /* Supported Protocol:08 */
2362 ret
= 0x00000001 | (USB2_PORTS
<<8);
2364 case 0x2c: /* Supported Protocol:0c */
2365 ret
= 0x00000000; /* reserved */
2367 case 0x30: /* Supported Protocol:00 */
2368 ret
= 0x03000002; /* USB 3.0 */
2370 case 0x34: /* Supported Protocol:04 */
2371 ret
= 0x20425455; /* "USB " */
2373 case 0x38: /* Supported Protocol:08 */
2374 ret
= 0x00000000 | (USB2_PORTS
+1) | (USB3_PORTS
<<8);
2376 case 0x3c: /* Supported Protocol:0c */
2377 ret
= 0x00000000; /* reserved */
2380 fprintf(stderr
, "xhci_cap_read: reg %d unimplemented\n", reg
);
2384 trace_usb_xhci_cap_read(reg
, ret
);
2388 static uint32_t xhci_port_read(XHCIState
*xhci
, uint32_t reg
)
2390 uint32_t port
= reg
>> 4;
2393 if (port
>= MAXPORTS
) {
2394 fprintf(stderr
, "xhci_port_read: port %d out of bounds\n", port
);
2399 switch (reg
& 0xf) {
2400 case 0x00: /* PORTSC */
2401 ret
= xhci
->ports
[port
].portsc
;
2403 case 0x04: /* PORTPMSC */
2404 case 0x08: /* PORTLI */
2407 case 0x0c: /* reserved */
2409 fprintf(stderr
, "xhci_port_read (port %d): reg 0x%x unimplemented\n",
2415 trace_usb_xhci_port_read(port
, reg
& 0x0f, ret
);
2419 static void xhci_port_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2421 uint32_t port
= reg
>> 4;
2424 trace_usb_xhci_port_write(port
, reg
& 0x0f, val
);
2426 if (port
>= MAXPORTS
) {
2427 fprintf(stderr
, "xhci_port_read: port %d out of bounds\n", port
);
2431 switch (reg
& 0xf) {
2432 case 0x00: /* PORTSC */
2433 portsc
= xhci
->ports
[port
].portsc
;
2434 /* write-1-to-clear bits*/
2435 portsc
&= ~(val
& (PORTSC_CSC
|PORTSC_PEC
|PORTSC_WRC
|PORTSC_OCC
|
2436 PORTSC_PRC
|PORTSC_PLC
|PORTSC_CEC
));
2437 if (val
& PORTSC_LWS
) {
2438 /* overwrite PLS only when LWS=1 */
2439 portsc
&= ~(PORTSC_PLS_MASK
<< PORTSC_PLS_SHIFT
);
2440 portsc
|= val
& (PORTSC_PLS_MASK
<< PORTSC_PLS_SHIFT
);
2442 /* read/write bits */
2443 portsc
&= ~(PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
);
2444 portsc
|= (val
& (PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
));
2445 /* write-1-to-start bits */
2446 if (val
& PORTSC_PR
) {
2447 DPRINTF("xhci: port %d reset\n", port
);
2448 usb_device_reset(xhci
->ports
[port
].port
.dev
);
2449 portsc
|= PORTSC_PRC
| PORTSC_PED
;
2451 xhci
->ports
[port
].portsc
= portsc
;
2453 case 0x04: /* PORTPMSC */
2454 case 0x08: /* PORTLI */
2456 fprintf(stderr
, "xhci_port_write (port %d): reg 0x%x unimplemented\n",
2461 static uint32_t xhci_oper_read(XHCIState
*xhci
, uint32_t reg
)
2466 return xhci_port_read(xhci
, reg
- 0x400);
2470 case 0x00: /* USBCMD */
2473 case 0x04: /* USBSTS */
2476 case 0x08: /* PAGESIZE */
2479 case 0x14: /* DNCTRL */
2482 case 0x18: /* CRCR low */
2483 ret
= xhci
->crcr_low
& ~0xe;
2485 case 0x1c: /* CRCR high */
2486 ret
= xhci
->crcr_high
;
2488 case 0x30: /* DCBAAP low */
2489 ret
= xhci
->dcbaap_low
;
2491 case 0x34: /* DCBAAP high */
2492 ret
= xhci
->dcbaap_high
;
2494 case 0x38: /* CONFIG */
2498 fprintf(stderr
, "xhci_oper_read: reg 0x%x unimplemented\n", reg
);
2502 trace_usb_xhci_oper_read(reg
, ret
);
2506 static void xhci_oper_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2509 xhci_port_write(xhci
, reg
- 0x400, val
);
2513 trace_usb_xhci_oper_write(reg
, val
);
2516 case 0x00: /* USBCMD */
2517 if ((val
& USBCMD_RS
) && !(xhci
->usbcmd
& USBCMD_RS
)) {
2519 } else if (!(val
& USBCMD_RS
) && (xhci
->usbcmd
& USBCMD_RS
)) {
2522 xhci
->usbcmd
= val
& 0xc0f;
2523 if (val
& USBCMD_HCRST
) {
2524 xhci_reset(&xhci
->pci_dev
.qdev
);
2526 xhci_irq_update(xhci
);
2529 case 0x04: /* USBSTS */
2530 /* these bits are write-1-to-clear */
2531 xhci
->usbsts
&= ~(val
& (USBSTS_HSE
|USBSTS_EINT
|USBSTS_PCD
|USBSTS_SRE
));
2532 xhci_irq_update(xhci
);
2535 case 0x14: /* DNCTRL */
2536 xhci
->dnctrl
= val
& 0xffff;
2538 case 0x18: /* CRCR low */
2539 xhci
->crcr_low
= (val
& 0xffffffcf) | (xhci
->crcr_low
& CRCR_CRR
);
2541 case 0x1c: /* CRCR high */
2542 xhci
->crcr_high
= val
;
2543 if (xhci
->crcr_low
& (CRCR_CA
|CRCR_CS
) && (xhci
->crcr_low
& CRCR_CRR
)) {
2544 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_COMMAND_RING_STOPPED
};
2545 xhci
->crcr_low
&= ~CRCR_CRR
;
2546 xhci_event(xhci
, &event
);
2547 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci
->crcr_low
);
2549 dma_addr_t base
= xhci_addr64(xhci
->crcr_low
& ~0x3f, val
);
2550 xhci_ring_init(xhci
, &xhci
->cmd_ring
, base
);
2552 xhci
->crcr_low
&= ~(CRCR_CA
| CRCR_CS
);
2554 case 0x30: /* DCBAAP low */
2555 xhci
->dcbaap_low
= val
& 0xffffffc0;
2557 case 0x34: /* DCBAAP high */
2558 xhci
->dcbaap_high
= val
;
2560 case 0x38: /* CONFIG */
2561 xhci
->config
= val
& 0xff;
2564 fprintf(stderr
, "xhci_oper_write: reg 0x%x unimplemented\n", reg
);
2568 static uint32_t xhci_runtime_read(XHCIState
*xhci
, uint32_t reg
)
2573 case 0x00: /* MFINDEX */
2574 fprintf(stderr
, "xhci_runtime_read: MFINDEX not yet implemented\n");
2575 ret
= xhci
->mfindex
;
2577 case 0x20: /* IMAN */
2580 case 0x24: /* IMOD */
2583 case 0x28: /* ERSTSZ */
2586 case 0x30: /* ERSTBA low */
2587 ret
= xhci
->erstba_low
;
2589 case 0x34: /* ERSTBA high */
2590 ret
= xhci
->erstba_high
;
2592 case 0x38: /* ERDP low */
2593 ret
= xhci
->erdp_low
;
2595 case 0x3c: /* ERDP high */
2596 ret
= xhci
->erdp_high
;
2599 fprintf(stderr
, "xhci_runtime_read: reg 0x%x unimplemented\n", reg
);
2603 trace_usb_xhci_runtime_read(reg
, ret
);
2607 static void xhci_runtime_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2609 trace_usb_xhci_runtime_read(reg
, val
);
2612 case 0x20: /* IMAN */
2613 if (val
& IMAN_IP
) {
2614 xhci
->iman
&= ~IMAN_IP
;
2616 xhci
->iman
&= ~IMAN_IE
;
2617 xhci
->iman
|= val
& IMAN_IE
;
2618 xhci_irq_update(xhci
);
2620 case 0x24: /* IMOD */
2623 case 0x28: /* ERSTSZ */
2624 xhci
->erstsz
= val
& 0xffff;
2626 case 0x30: /* ERSTBA low */
2627 /* XXX NEC driver bug: it doesn't align this to 64 bytes
2628 xhci->erstba_low = val & 0xffffffc0; */
2629 xhci
->erstba_low
= val
& 0xfffffff0;
2631 case 0x34: /* ERSTBA high */
2632 xhci
->erstba_high
= val
;
2633 xhci_er_reset(xhci
);
2635 case 0x38: /* ERDP low */
2636 if (val
& ERDP_EHB
) {
2637 xhci
->erdp_low
&= ~ERDP_EHB
;
2639 xhci
->erdp_low
= (val
& ~ERDP_EHB
) | (xhci
->erdp_low
& ERDP_EHB
);
2641 case 0x3c: /* ERDP high */
2642 xhci
->erdp_high
= val
;
2643 xhci_events_update(xhci
);
2646 fprintf(stderr
, "xhci_oper_write: reg 0x%x unimplemented\n", reg
);
2650 static uint32_t xhci_doorbell_read(XHCIState
*xhci
, uint32_t reg
)
2652 /* doorbells always read as 0 */
2653 trace_usb_xhci_doorbell_read(reg
, 0);
2657 static void xhci_doorbell_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2659 trace_usb_xhci_doorbell_write(reg
, val
);
2661 if (!xhci_running(xhci
)) {
2662 fprintf(stderr
, "xhci: wrote doorbell while xHC stopped or paused\n");
2670 xhci_process_commands(xhci
);
2672 fprintf(stderr
, "xhci: bad doorbell 0 write: 0x%x\n", val
);
2675 if (reg
> MAXSLOTS
) {
2676 fprintf(stderr
, "xhci: bad doorbell %d\n", reg
);
2677 } else if (val
> 31) {
2678 fprintf(stderr
, "xhci: bad doorbell %d write: 0x%x\n", reg
, val
);
2680 xhci_kick_ep(xhci
, reg
, val
);
2685 static uint64_t xhci_mem_read(void *ptr
, target_phys_addr_t addr
,
2688 XHCIState
*xhci
= ptr
;
2690 /* Only aligned reads are allowed on xHCI */
2692 fprintf(stderr
, "xhci_mem_read: Mis-aligned read\n");
2696 if (addr
< LEN_CAP
) {
2697 return xhci_cap_read(xhci
, addr
);
2698 } else if (addr
>= OFF_OPER
&& addr
< (OFF_OPER
+ LEN_OPER
)) {
2699 return xhci_oper_read(xhci
, addr
- OFF_OPER
);
2700 } else if (addr
>= OFF_RUNTIME
&& addr
< (OFF_RUNTIME
+ LEN_RUNTIME
)) {
2701 return xhci_runtime_read(xhci
, addr
- OFF_RUNTIME
);
2702 } else if (addr
>= OFF_DOORBELL
&& addr
< (OFF_DOORBELL
+ LEN_DOORBELL
)) {
2703 return xhci_doorbell_read(xhci
, addr
- OFF_DOORBELL
);
2705 fprintf(stderr
, "xhci_mem_read: Bad offset %x\n", (int)addr
);
2710 static void xhci_mem_write(void *ptr
, target_phys_addr_t addr
,
2711 uint64_t val
, unsigned size
)
2713 XHCIState
*xhci
= ptr
;
2715 /* Only aligned writes are allowed on xHCI */
2717 fprintf(stderr
, "xhci_mem_write: Mis-aligned write\n");
2721 if (addr
>= OFF_OPER
&& addr
< (OFF_OPER
+ LEN_OPER
)) {
2722 xhci_oper_write(xhci
, addr
- OFF_OPER
, val
);
2723 } else if (addr
>= OFF_RUNTIME
&& addr
< (OFF_RUNTIME
+ LEN_RUNTIME
)) {
2724 xhci_runtime_write(xhci
, addr
- OFF_RUNTIME
, val
);
2725 } else if (addr
>= OFF_DOORBELL
&& addr
< (OFF_DOORBELL
+ LEN_DOORBELL
)) {
2726 xhci_doorbell_write(xhci
, addr
- OFF_DOORBELL
, val
);
2728 fprintf(stderr
, "xhci_mem_write: Bad offset %x\n", (int)addr
);
2732 static const MemoryRegionOps xhci_mem_ops
= {
2733 .read
= xhci_mem_read
,
2734 .write
= xhci_mem_write
,
2735 .valid
.min_access_size
= 4,
2736 .valid
.max_access_size
= 4,
2737 .endianness
= DEVICE_LITTLE_ENDIAN
,
2740 static void xhci_attach(USBPort
*usbport
)
2742 XHCIState
*xhci
= usbport
->opaque
;
2743 XHCIPort
*port
= &xhci
->ports
[usbport
->index
];
2745 xhci_update_port(xhci
, port
, 0);
2748 static void xhci_detach(USBPort
*usbport
)
2750 XHCIState
*xhci
= usbport
->opaque
;
2751 XHCIPort
*port
= &xhci
->ports
[usbport
->index
];
2753 xhci_update_port(xhci
, port
, 1);
2756 static void xhci_wakeup(USBPort
*usbport
)
2758 XHCIState
*xhci
= usbport
->opaque
;
2759 XHCIPort
*port
= &xhci
->ports
[usbport
->index
];
2760 int nr
= port
->port
.index
+ 1;
2761 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
, nr
<< 24};
2764 pls
= (port
->portsc
>> PORTSC_PLS_SHIFT
) & PORTSC_PLS_MASK
;
2768 port
->portsc
|= 0xf << PORTSC_PLS_SHIFT
;
2769 if (port
->portsc
& PORTSC_PLC
) {
2772 port
->portsc
|= PORTSC_PLC
;
2773 xhci_event(xhci
, &ev
);
2776 static void xhci_complete(USBPort
*port
, USBPacket
*packet
)
2778 XHCITransfer
*xfer
= container_of(packet
, XHCITransfer
, packet
);
2780 xhci_complete_packet(xfer
, packet
->result
);
2781 xhci_kick_ep(xfer
->xhci
, xfer
->slotid
, xfer
->epid
);
2784 static void xhci_child_detach(USBPort
*port
, USBDevice
*child
)
2789 static USBPortOps xhci_port_ops
= {
2790 .attach
= xhci_attach
,
2791 .detach
= xhci_detach
,
2792 .wakeup
= xhci_wakeup
,
2793 .complete
= xhci_complete
,
2794 .child_detach
= xhci_child_detach
,
2797 static int xhci_find_slotid(XHCIState
*xhci
, USBDevice
*dev
)
2802 for (slotid
= 1; slotid
<= MAXSLOTS
; slotid
++) {
2803 slot
= &xhci
->slots
[slotid
-1];
2804 if (slot
->devaddr
== dev
->addr
) {
2811 static int xhci_find_epid(USBEndpoint
*ep
)
2816 if (ep
->pid
== USB_TOKEN_IN
) {
2817 return ep
->nr
* 2 + 1;
2823 static void xhci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
)
2825 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
2828 DPRINTF("%s\n", __func__
);
2829 slotid
= xhci_find_slotid(xhci
, ep
->dev
);
2830 if (slotid
== 0 || !xhci
->slots
[slotid
-1].enabled
) {
2831 DPRINTF("%s: oops, no slot for dev %d\n", __func__
, ep
->dev
->addr
);
2834 xhci_kick_ep(xhci
, slotid
, xhci_find_epid(ep
));
2837 static USBBusOps xhci_bus_ops
= {
2838 .wakeup_endpoint
= xhci_wakeup_endpoint
,
2841 static void usb_xhci_init(XHCIState
*xhci
, DeviceState
*dev
)
2845 xhci
->usbsts
= USBSTS_HCH
;
2847 usb_bus_new(&xhci
->bus
, &xhci_bus_ops
, &xhci
->pci_dev
.qdev
);
2849 for (i
= 0; i
< MAXPORTS
; i
++) {
2850 memset(&xhci
->ports
[i
], 0, sizeof(xhci
->ports
[i
]));
2851 usb_register_port(&xhci
->bus
, &xhci
->ports
[i
].port
, xhci
, i
,
2853 USB_SPEED_MASK_LOW
|
2854 USB_SPEED_MASK_FULL
|
2855 USB_SPEED_MASK_HIGH
);
2857 for (i
= 0; i
< MAXSLOTS
; i
++) {
2858 xhci
->slots
[i
].enabled
= 0;
2862 static int usb_xhci_initfn(struct PCIDevice
*dev
)
2866 XHCIState
*xhci
= DO_UPCAST(XHCIState
, pci_dev
, dev
);
2868 xhci
->pci_dev
.config
[PCI_CLASS_PROG
] = 0x30; /* xHCI */
2869 xhci
->pci_dev
.config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin 1 */
2870 xhci
->pci_dev
.config
[PCI_CACHE_LINE_SIZE
] = 0x10;
2871 xhci
->pci_dev
.config
[0x60] = 0x30; /* release number */
2873 usb_xhci_init(xhci
, &dev
->qdev
);
2875 xhci
->irq
= xhci
->pci_dev
.irq
[0];
2877 memory_region_init_io(&xhci
->mem
, &xhci_mem_ops
, xhci
,
2879 pci_register_bar(&xhci
->pci_dev
, 0,
2880 PCI_BASE_ADDRESS_SPACE_MEMORY
|PCI_BASE_ADDRESS_MEM_TYPE_64
,
2883 ret
= pcie_cap_init(&xhci
->pci_dev
, 0xa0, PCI_EXP_TYPE_ENDPOINT
, 0);
2887 ret
= msi_init(&xhci
->pci_dev
, 0x70, 1, true, false);
2894 static void xhci_write_config(PCIDevice
*dev
, uint32_t addr
, uint32_t val
,
2897 XHCIState
*xhci
= DO_UPCAST(XHCIState
, pci_dev
, dev
);
2899 pci_default_write_config(dev
, addr
, val
, len
);
2901 msi_write_config(dev
, addr
, val
, len
);
2905 static const VMStateDescription vmstate_xhci
= {
2910 static Property xhci_properties
[] = {
2911 DEFINE_PROP_UINT32("msi", XHCIState
, msi
, 0),
2912 DEFINE_PROP_END_OF_LIST(),
2915 static void xhci_class_init(ObjectClass
*klass
, void *data
)
2917 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2918 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2920 dc
->vmsd
= &vmstate_xhci
;
2921 dc
->props
= xhci_properties
;
2922 dc
->reset
= xhci_reset
;
2923 k
->init
= usb_xhci_initfn
;
2924 k
->vendor_id
= PCI_VENDOR_ID_NEC
;
2925 k
->device_id
= PCI_DEVICE_ID_NEC_UPD720200
;
2926 k
->class_id
= PCI_CLASS_SERIAL_USB
;
2929 k
->config_write
= xhci_write_config
;
2932 static TypeInfo xhci_info
= {
2933 .name
= "nec-usb-xhci",
2934 .parent
= TYPE_PCI_DEVICE
,
2935 .instance_size
= sizeof(XHCIState
),
2936 .class_init
= xhci_class_init
,
2939 static void xhci_register_types(void)
2941 type_register_static(&xhci_info
);
2944 type_init(xhci_register_types
)