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

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