Modules/UHCI - Fixed interrupt bug that caused keyboard to not work
[tpg/acess2.git] / KernelLand / Modules / USB / UHCI / uhci.h
1 /*
2  * AcessOS Version 1
3  * USB Stack
4  * - Universal Host Controller Interface
5  */
6 #ifndef _UHCI_H_
7 #define _UHCI_H_
8
9 // === TYPES ===
10 typedef struct sUHCI_Controller tUHCI_Controller;
11 typedef struct sUHCI_ExtraTDInfo        tUHCI_ExtraTDInfo;
12
13 typedef struct sUHCI_TD tUHCI_TD;
14 typedef struct sUHCI_QH tUHCI_QH;
15
16 // === STRUCTURES ===
17 struct sUHCI_ExtraTDInfo
18 {
19          int    Offset;
20         tPAddr  FirstPage;
21         tPAddr  SecondPage;
22         
23         tUSBHostCb      Callback;
24         void    *CallbackPtr;
25 };
26
27 struct sUHCI_TD
28 {
29         /**
30          * \brief Next Entry in list
31          * 
32          * 31:4 - Address
33          * 3 - Reserved
34          * 2 - Depth/Breadth Select
35          * 1 - QH/TD Select
36          * 0 - Terminate (Last in List)
37          */
38         Uint32  Link;
39         
40         /**
41          * \brief Control and Status Field
42          * 
43          * 31:30 - Reserved
44          * 29 - Short Packet Detect (Input Only)
45          * 28:27 - Number of Errors Allowed
46          * 26 - Low Speed Device (Communicating with a low speed device)
47          * 25 - Isynchonious Select
48          * 24 - Interrupt on Completion (IOC)
49          * 23:16 - Status
50          *     23 - Active
51          *     22 - Stalled
52          *     21 - Data Buffer Error
53          *     20 - Babble Detected
54          *     19 - NAK Detected
55          *     18 - CRC/Timout Error
56          *     17 - Bitstuff Error
57          *     16 - Reserved
58          * 15:11 - Reserved
59          * 10:0 - Actual Length (Number of bytes transfered)
60          */
61         Uint32  Control;
62         
63         /**
64          * \brief Packet Header
65          * 
66          * 31:21 - Maximum Length (0=1, Max 0x4FF, 0x7FF=0)
67          * 20 - Reserved
68          * 19 - Data Toggle
69          * 18:15 - Endpoint
70          * 14:8 - Device Address
71          * 7:0 - PID (Packet Identifcation) - Only 96, E1, 2D allowed
72          *
73          * 0x96 = Data IN
74          * 0xE1 = Data Out
75          * 0x2D = Setup
76          */
77         Uint32  Token;
78         
79         /**
80          * \brief Pointer to the data to send
81          */
82         Uint32  BufferPointer;
83
84         struct
85         {
86                 tUHCI_ExtraTDInfo       *ExtraInfo;
87                 char    bActive;        // Allocated
88                 Uint8   QueueIndex;     // QH, 0-127 are interrupt, 128 undef, 129 Control, 130 Bulk
89                 char    bFreePointer;   // Free \a BufferPointer once done
90         } _info;
91 } __attribute__((aligned(16)));
92
93 struct sUHCI_QH
94 {
95         /**
96          * \brief Next Entry in list
97          * 
98          * 31:4 - Address
99          * 3:2 - Reserved
100          * 1 - QH/TD Select
101          * 0 - Terminate (Last in List)
102          */
103         Uint32  Next;
104
105         
106         /**
107          * \brief Next Entry in list
108          * 
109          * 31:4 - Address
110          * 3:2 - Reserved
111          * 1 - QH/TD Select
112          * 0 - Terminate (Last in List)
113          */
114         Uint32  Child;
115         
116         /*
117          * \note Area for software use
118          * \brief Last TD in this list, used to add things to the end
119          */
120         tUHCI_TD        *_LastItem;
121 } __attribute__((aligned(16)));
122
123 struct sUHCI_Controller
124 {
125         /**
126          * \brief PCI Device ID
127          */
128         Uint16  PciId;
129         
130         /**
131          * \brief IO Base Address
132          */
133         Uint16  IOBase;
134         
135         /**
136          * \brief Memory Mapped-IO base address
137          */
138         Uint16  *MemIOMap;
139
140         /**
141          * \brief IRQ Number assigned to the device
142          */
143          int    IRQNum;
144
145         /**
146          * \brief Number of the last frame to be cleaned
147          */
148          int    LastCleanedFrame;
149         
150         /**
151          * \brief Frame list
152          * 
153          * 31:4 - Frame Pointer
154          * 3:2 - Reserved
155          * 1 - QH/TD Selector
156          * 0 - Terminate (Empty Pointer)
157          */
158         Uint32  *FrameList;
159         
160         /**
161          * \brief Physical Address of the Frame List
162          */
163         tPAddr  PhysFrameList;
164
165         tUSBHub *RootHub;
166
167         /**
168          * \brief Load in bytes on each interrupt queue
169          */
170          int    InterruptLoad[128];
171
172         tPAddr          PhysTDQHPage;
173         struct
174         {
175                 // 127 Interrupt Queue Heads
176                 // - 4ms -> 256ms range of periods
177                 tUHCI_QH        InterruptQHs[0];
178                 tUHCI_QH        InterruptQHs_256ms[64];
179                 tUHCI_QH        InterruptQHs_128ms[32];
180                 tUHCI_QH        InterruptQHs_64ms [16];
181                 tUHCI_QH        InterruptQHs_32ms [ 8];
182                 tUHCI_QH        InterruptQHs_16ms [ 4];
183                 tUHCI_QH        InterruptQHs_8ms  [ 2];
184                 tUHCI_QH        InterruptQHs_4ms  [ 1];
185                 tUHCI_QH        _padding;
186         
187                 tUHCI_QH        ControlQH;
188                 tUHCI_QH        BulkQH;
189                 
190                 tUHCI_TD        LocalTDPool[ (4096-(128+2)*sizeof(tUHCI_QH)) / sizeof(tUHCI_TD) ];
191         }       *TDQHPage;
192 };
193
194 // === ENUMERATIONS ===
195 enum eUHCI_IOPorts {
196         /**
197          * \brief USB Command Register
198          * 
199          * 15:8 - Reserved
200          * 7 - Maximum Packet Size selector (1: 64 bytes, 0: 32 bytes)
201          * 6 - Configure Flag (No Hardware Effect)
202          * 5 - Software Debug (Don't think it will be needed)
203          * 4 - Force Global Resume
204          * 3 - Enter Global Suspend Mode
205          * 2 - Global Reset (Resets all devices on the bus)
206          * 1 - Host Controller Reset (Reset just the controller)
207          * 0 - Run/Stop
208          */
209         USBCMD  = 0x00,
210         /**
211          * \brief USB Status Register
212          * 
213          * 15:6 - Reserved
214          * 5 - HC Halted, set to 1 when USBCMD:RS is set to 0
215          * 4 - Host Controller Process Error (Errors related to the bus)
216          * 3 - Host System Error (Errors related to the OS/PCI Bus)
217          * 2 - Resume Detect (Set if a RESUME command is sent to the Controller)
218          * 1 - USB Error Interrupt
219          * 0 - USB Interrupts (Set if a transaction with the IOC bit set is completed)
220          */
221         USBSTS  = 0x02,
222         /**
223          * \brief USB Interrupt Enable Register
224          * 
225          * 15:4 - Reserved
226          * 3 - Short Packet Interrupt Enable
227          * 2 - Interrupt on Complete (IOC) Enable
228          * 1 - Resume Interrupt Enable
229          * 0 - Timout / CRC Error Interrupt Enable
230          */
231         USBINTR = 0x04,
232         /**
233          * \brief Frame Number (Index into the Frame List)
234          * 
235          * 15:11 - Reserved
236          * 10:0 - Index (Incremented each approx 1ms)
237          */
238         FRNUM   = 0x06,
239         /**
240          * \brief Frame List Base Address
241          * 
242          * 31:12 - Pysical Address >> 12
243          * 11:0 - Reserved (Set to Zero)
244          */
245         FLBASEADD = 0x08,       // 32-bit
246         /**
247          * \brief Start-of-frame Modify Register
248          * \note 8-bits only
249          * 
250          * Sets the size of a frame
251          * Frequency = (11936+n)/12000 kHz
252          * 
253          * 7 - Reserved
254          * 6:0 -
255          */
256         SOFMOD = 0x0C,  // 8bit
257         /**
258          * \brief Port Status and Controll Register (Port 1)
259          * 
260          * 15:13 - Reserved
261          * 12 - Suspend
262          * 11:10 - Reserved
263          * 9 - Port Reset
264          * 8 - Low Speed Device Attached
265          * 5:4 - Line Status
266          * 3 - Port Enable/Disable Change - Used for detecting device removal
267          * 2 - Port Enable/Disable
268          * 1 - Connect Status Change
269          * 0 - Current Connect Status
270          */
271         PORTSC1 = 0x10,
272         /**
273          * \brief Port Status and Controll Register (Port 2)
274          * 
275          * See ::PORTSC1
276          */
277         PORTSC2 = 0x12
278 };
279
280 #endif

UCC git Repository :: git.ucc.asn.au