Modules/USB - Working on a structure for the USB subsystem
[tpg/acess2.git] / Modules / USB / UHCI / uhci.c
1 /*
2  * Acess 2 USB Stack
3  * Universal Host Controller Interface
4  */
5 #define DEBUG   1
6 #define VERSION VER2(0,5)
7 #include <acess.h>
8 #include <vfs.h>
9 #include <drv_pci.h>
10 #include <modules.h>
11 #include <usb_host.h>
12 #include "uhci.h"
13
14 // === CONSTANTS ===
15 #define MAX_CONTROLLERS 4
16 #define NUM_TDs 1024
17
18 // === PROTOTYPES ===
19  int    UHCI_Initialise();
20 void    UHCI_Cleanup();
21 tUHCI_TD        *UHCI_int_AllocateTD(tUHCI_Controller *Cont);
22 void    UHCI_int_AppendTD(tUHCI_Controller *Cont, tUHCI_TD *TD);
23 void    *UHCI_int_SendTransaction(tUHCI_Controller *Cont, int Addr, Uint8 Type, int bTgl, int bIOC, void *Data, size_t Length);
24 void    *UHCI_DataIN(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length);
25 void    *UHCI_DataOUT(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length);
26 void    *UHCI_SendSetup(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length);
27 void    UHCI_Hub_Poll(tUSBHub *Hub, tUSBDevice *Dev);
28  int    UHCI_Int_InitHost(tUHCI_Controller *Host);
29 void    UHCI_CheckPortUpdate(tUHCI_Controller *Host);
30 void    UHCI_InterruptHandler(int IRQ, void *Ptr);
31
32 // === GLOBALS ===
33 MODULE_DEFINE(0, VERSION, USB_UHCI, UHCI_Initialise, NULL, "USB_Core", NULL);
34 tUHCI_TD        gaUHCI_TDPool[NUM_TDs];
35 tUHCI_Controller        gUHCI_Controllers[MAX_CONTROLLERS];
36 tUSBHostDef     gUHCI_HostDef = {
37         .SendIN = UHCI_DataIN,
38         .SendOUT = UHCI_DataOUT,
39         .SendSETUP = UHCI_SendSetup,
40         .CheckPorts = (void*)UHCI_CheckPortUpdate
41         };
42
43 // === CODE ===
44 /**
45  * \fn int UHCI_Initialise()
46  * \brief Called to initialise the UHCI Driver
47  */
48 int UHCI_Initialise(const char **Arguments)
49 {
50          int    i=0, id=-1;
51          int    ret;
52         
53         ENTER("");
54         
55         // Enumerate PCI Bus, getting a maximum of `MAX_CONTROLLERS` devices
56         while( (id = PCI_GetDeviceByClass(0x0C03, 0xFFFF, id)) >= 0 && i < MAX_CONTROLLERS )
57         {
58                 tUHCI_Controller        *cinfo = &gUHCI_Controllers[i];
59                 // NOTE: Check "protocol" from PCI?
60                 
61                 cinfo->PciId = id;
62                 // Assign a port range (BAR4, Reserve 32 ports)
63                 cinfo->IOBase = PCI_GetBAR(id, 4);
64                 if( !(cinfo->IOBase & 1) ) {
65                         Log_Warning("UHCI", "MMIO is not supported");
66                         continue ;
67                 }
68                 cinfo->IRQNum = PCI_GetIRQ(id);
69                 
70                 Log_Debug("UHCI", "Controller PCI #%i: IO Base = 0x%x, IRQ %i",
71                         id, cinfo->IOBase, cinfo->IRQNum);
72                 
73                 IRQ_AddHandler(cinfo->IRQNum, UHCI_InterruptHandler, cinfo);
74         
75                 // Initialise Host
76                 ret = UHCI_Int_InitHost(&gUHCI_Controllers[i]);
77                 // Detect an error
78                 if(ret != 0) {
79                         LEAVE('i', ret);
80                         return ret;
81                 }
82                 
83                 cinfo->RootHub = USB_RegisterHost(&gUHCI_HostDef, cinfo, 2);
84
85                 i ++;
86         }
87         if(i == MAX_CONTROLLERS) {
88                 Log_Warning("UHCI", "Over "EXPAND_STR(MAX_CONTROLLERS)" UHCI controllers detected, ignoring rest");
89         }
90         LEAVE('i', MODULE_ERR_OK);
91         return MODULE_ERR_OK;
92 }
93
94 /**
95  * \fn void UHCI_Cleanup()
96  * \brief Called just before module is unloaded
97  */
98 void UHCI_Cleanup()
99 {
100 }
101
102 tUHCI_TD *UHCI_int_AllocateTD(tUHCI_Controller *Cont)
103 {
104          int    i;
105         for(i = 0; i < NUM_TDs; i ++)
106         {
107                 if(gaUHCI_TDPool[i].Link == 0) {
108                         gaUHCI_TDPool[i].Link = 1;
109                         return &gaUHCI_TDPool[i];
110                 }
111         }
112         return NULL;
113 }
114
115 void UHCI_int_AppendTD(tUHCI_Controller *Cont, tUHCI_TD *TD)
116 {
117         Log_Warning("UHCI", "TODO: Implement AppendTD");
118 }
119
120 /**
121  * \brief Send a transaction to the USB bus
122  * \param Cont  Controller pointer
123  * \param Addr  Function Address * 16 + Endpoint
124  * \param bTgl  Data toggle value
125  */
126 void *UHCI_int_SendTransaction(tUHCI_Controller *Cont, int Addr, Uint8 Type, int bTgl, int bIOC, void *Data, size_t Length)
127 {
128         tUHCI_TD        *td;
129
130         if( Length > 0x400 )    return NULL;    // Controller allows up to 0x500, but USB doesn't
131
132         td = UHCI_int_AllocateTD(Cont);
133
134         td->Link = 1;
135         td->Control = (Length - 1) & 0x7FF;
136         td->Token  = ((Length - 1) & 0x7FF) << 21;
137         td->Token |= (bTgl & 1) << 19;
138         td->Token |= (Addr & 0xF) << 15;
139         td->Token |= ((Addr/16) & 0xFF) << 8;
140         td->Token |= Type;
141
142         // TODO: Ensure 32-bit paddr
143         if( ((tVAddr)Data & PAGE_SIZE) + Length > PAGE_SIZE ) {
144                 Log_Warning("UHCI", "TODO: Support non single page transfers");
145 //              td->BufferPointer = 
146                 return NULL;
147         }
148         else {
149                 td->BufferPointer = MM_GetPhysAddr( (tVAddr)Data );
150         }
151
152         if( bIOC ) {
153 //              td->Control
154         }
155
156         UHCI_int_AppendTD(Cont, td);
157
158         return td;
159 }
160
161 void *UHCI_DataIN(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length)
162 {
163         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0x69, DataTgl, bIOC, Data, Length);
164 }
165
166 void *UHCI_DataOUT(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length)
167 {
168         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0xE1, DataTgl, bIOC, Data, Length);
169 }
170
171 void *UHCI_SendSetup(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length)
172 {
173         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0x2D, DataTgl, bIOC, Data, Length);
174 }
175
176 void UHCI_Hub_Poll(tUSBHub *Hub, tUSBDevice *Dev)
177 {
178         tUHCI_Controller        *cont = USB_GetDeviceDataPtr(Dev);
179         
180         UHCI_CheckPortUpdate(cont);
181 }
182
183 // === INTERNAL FUNCTIONS ===
184 /**
185  * \fn int UHCI_Int_InitHost(tUCHI_Controller *Host)
186  * \brief Initialises a UHCI host controller
187  * \param Host  Pointer - Host to initialise
188  */
189 int UHCI_Int_InitHost(tUHCI_Controller *Host)
190 {
191         ENTER("pHost", Host);
192
193         outw( Host->IOBase + USBCMD, 4 );       // GRESET
194         Time_Delay(10);
195         // TODO: Wait for at least 10ms
196         outw( Host->IOBase + USBCMD, 0 );       // GRESET
197         
198         // Allocate Frame List
199         // - 1 Page, 32-bit address
200         // - 1 page = 1024  4 byte entries
201         Host->FrameList = (void *) MM_AllocDMA(1, 32, &Host->PhysFrameList);
202         if( !Host->FrameList ) {
203                 Log_Warning("UHCI", "Unable to allocate frame list, aborting");
204                 LEAVE('i', -1);
205                 return -1;
206         }
207         LOG("Allocated frame list 0x%x (0x%x)", Host->FrameList, Host->PhysFrameList);
208         memsetd( Host->FrameList, 1, 1024 );    // Clear List (Disabling all entries)
209         
210         //! \todo Properly fill frame list
211         
212         // Set frame length to 1 ms
213         outb( Host->IOBase + SOFMOD, 64 );
214         
215         // Set Frame List
216         outd( Host->IOBase + FLBASEADD, Host->PhysFrameList );
217         outw( Host->IOBase + FRNUM, 0 );
218         
219         // Enable Interrupts
220         outw( Host->IOBase + USBINTR, 0x000F );
221         PCI_ConfigWrite( Host->PciId, 0xC0, 2, 0x2000 );
222
223         outw( Host->IOBase + USBCMD, 0x0001 );
224
225         LEAVE('i', 0);
226         return 0;
227 }
228
229 void UHCI_CheckPortUpdate(tUHCI_Controller *Host)
230 {
231         // Enable ports
232         for( int i = 0; i < 2; i ++ )
233         {
234                  int    port = Host->IOBase + PORTSC1 + i*2;
235                 // Check for port change
236                 if( !(inw(port) & 0x0002) )     continue;
237                 outw(port, 0x0002);
238                 
239                 // Check if the port is connected
240                 if( !(inw(port) & 1) )
241                 {
242                         // Tell the USB code it's gone.
243                         USB_DeviceDisconnected(Host->RootHub, i);
244                         continue;
245                 }
246                 else
247                 {
248                         LOG("Port %i has something", i);
249                         // Reset port (set bit 9)
250                         outw( port, 0x0100 );
251                         Time_Delay(50); // 50ms delay
252                         outw( port, inw(port) & ~0x0100 );
253                         // Enable port
254                         Time_Delay(50); // 50ms delay
255                         outw( port, inw(port) & 0x0004 );
256                         // Tell USB there's a new device
257                         USB_DeviceConnected(Host->RootHub, i);
258                 }
259         }
260 }
261
262 void UHCI_InterruptHandler(int IRQ, void *Ptr)
263 {
264         Log_Debug("UHCI", "UHIC Interrupt");
265 }

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