xref: /haiku/src/add-ons/kernel/busses/usb/xhci.h (revision 541ff51a6ef4c47f8ab105ba6ff895cdbba83aca)
1 /*
2  * Copyright 2006-2011, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Lotz <mmlr@mlotz.ch>
7  *		Jian Chiang <j.jian.chiang@gmail.com>
8  */
9 #ifndef XHCI_H
10 #define XHCI_H
11 
12 
13 #include "usb_private.h"
14 #include "xhci_hardware.h"
15 
16 
17 #define MAX_EVENTS		(16 * 13)
18 #define MAX_COMMANDS		(16 * 1)
19 #define XHCI_MAX_SLOTS		256
20 #define XHCI_MAX_PORTS		127
21 
22 
23 struct pci_info;
24 struct pci_module_info;
25 class XHCIRootHub;
26 
27 
28 struct xhci_trb {
29 	uint64	qwtrb0;
30 	uint32	dwtrb2;
31 	uint32	dwtrb3;
32 };
33 
34 
35 struct xhci_segment {
36 	xhci_trb *		trbs;
37 	xhci_segment *	next;
38 };
39 
40 
41 struct xhci_ring {
42 	xhci_segment *	first_seg;
43 	xhci_trb *		enqueue;
44 	xhci_trb *		dequeue;
45 };
46 
47 
48 // Section 6.5
49 struct xhci_erst_element {
50 	uint64	rs_addr;
51 	uint32	rs_size;
52 	uint32	rsvdz;
53 } __attribute__((__aligned__(64)));
54 
55 
56 class XHCI : public BusManager {
57 public:
58 								XHCI(pci_info *info, Stack *stack);
59 								~XHCI();
60 
61 			status_t			Start();
62 	virtual	status_t			SubmitTransfer(Transfer *transfer);
63 	virtual	status_t			CancelQueuedTransfers(Pipe *pipe, bool force);
64 
65 	virtual	status_t			NotifyPipeChange(Pipe *pipe,
66 									usb_change change);
67 
68 	static	status_t			AddTo(Stack *stack);
69 
70 			// Port operations for root hub
71 			uint8				PortCount() { return fPortCount; };
72 			status_t			GetPortStatus(uint8 index, usb_port_status *status);
73 			status_t			SetPortFeature(uint8 index, uint16 feature);
74 			status_t			ClearPortFeature(uint8 index, uint16 feature);
75 
76 	virtual	const char *		TypeName() const { return "xhci"; };
77 
78 private:
79 			// Controller resets
80 			status_t			ControllerReset();
81 			status_t			ControllerHalt();
82 
83 			// Interrupt functions
84 	static	int32				InterruptHandler(void *data);
85 			int32				Interrupt();
86 
87 			// Transfer management
88 	static	int32				FinishThread(void *data);
89 			void				FinishTransfers();
90 
91 			// Command
92 			void				QueueCommand(xhci_trb *trb);
93 			void				HandleCmdComplete(xhci_trb *trb);
94 
95 			//Doorbell
96 			void				Ring();
97 
98 			//no-op
99 			void				QueueNoop();
100 	static	int32				CmdCompThread(void *data);
101 			void				CmdComplete();
102 
103 			// Operational register functions
104 	inline	void				WriteOpReg(uint32 reg, uint32 value);
105 	inline	uint32				ReadOpReg(uint32 reg);
106 
107 			// Capability register functions
108 	inline	uint8				ReadCapReg8(uint32 reg);
109 	inline	uint16				ReadCapReg16(uint32 reg);
110 	inline	uint32				ReadCapReg32(uint32 reg);
111 	inline	void				WriteCapReg32(uint32 reg, uint32 value);
112 
113 			// Runtime register functions
114 	inline	uint32				ReadRunReg32(uint32 reg);
115 	inline	void				WriteRunReg32(uint32 reg, uint32 value);
116 
117 			// Doorbell register functions
118 	inline	uint32				ReadDoorReg32(uint32 reg);
119 	inline	void				WriteDoorReg32(uint32 reg, uint32 value);
120 
121 	static	pci_module_info *	sPCIModule;
122 
123 			uint8 *				fCapabilityRegisters;
124 			uint8 *				fOperationalRegisters;
125 			uint8 *				fRuntimeRegisters;
126 			uint8 *				fDoorbellRegisters;
127 			area_id				fRegisterArea;
128 			pci_info *			fPCIInfo;
129 			Stack *				fStack;
130 
131 			area_id				fErstArea;
132 			xhci_erst_element *	fErst;
133 			xhci_trb *			fEventRing;
134 			xhci_trb *			fCmdRing;
135 			uint64				fCmdAddr;
136 			uint32				fCmdResult[2];
137 
138 			area_id				fDcbaArea;
139 			uint8 *				fDcba;
140 
141 			spinlock			fSpinlock;
142 
143 			sem_id				fCmdCompSem;
144 			thread_id			fCmdCompThread;
145 			sem_id				fFinishTransfersSem;
146 			thread_id			fFinishThread;
147 			bool				fStopThreads;
148 
149 			// Root Hub
150 			XHCIRootHub *		fRootHub;
151 			uint8				fRootHubAddress;
152 
153 			// Port management
154 			uint8				fPortCount;
155 			uint8				fSlotCount;
156 
157 			uint16				fEventIdx;
158 			uint16				fCmdIdx;
159 			uint8				fEventCcs;
160 			uint8				fCmdCcs;
161 };
162 
163 
164 class XHCIRootHub : public Hub {
165 public:
166 									XHCIRootHub(Object *rootObject,
167 										int8 deviceAddress);
168 
169 static	status_t					ProcessTransfer(XHCI *ehci,
170 										Transfer *transfer);
171 };
172 
173 
174 #endif // !XHCI_H
175