83059ad2c6cc64f0a22342f7455cef8ce5ae04b0
[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, tUSBHostCb Cb, void *Data, size_t Length);
24 void    *UHCI_DataIN(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *Data, size_t Length);
25 void    *UHCI_DataOUT(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *Data, size_t Length);
26 void    *UHCI_SendSetup(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, 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                 cinfo->IOBase = PCI_GetBAR(id, 4);
63                 if( !(cinfo->IOBase & 1) ) {
64                         Log_Warning("UHCI", "MMIO is not supported");
65                         continue ;
66                 }
67                 cinfo->IRQNum = PCI_GetIRQ(id);
68                 
69                 Log_Debug("UHCI", "Controller PCI #%i: IO Base = 0x%x, IRQ %i",
70                         id, cinfo->IOBase, cinfo->IRQNum);
71                 
72                 IRQ_AddHandler(cinfo->IRQNum, UHCI_InterruptHandler, cinfo);
73         
74                 // Initialise Host
75                 ret = UHCI_Int_InitHost(&gUHCI_Controllers[i]);
76                 // Detect an error
77                 if(ret != 0) {
78                         LEAVE('i', ret);
79                         return ret;
80                 }
81                 
82                 cinfo->RootHub = USB_RegisterHost(&gUHCI_HostDef, cinfo, 2);
83
84                 i ++;
85         }
86         if(i == MAX_CONTROLLERS) {
87                 Log_Warning("UHCI", "Over "EXPAND_STR(MAX_CONTROLLERS)" UHCI controllers detected, ignoring rest");
88         }
89         LEAVE('i', MODULE_ERR_OK);
90         return MODULE_ERR_OK;
91 }
92
93 /**
94  * \fn void UHCI_Cleanup()
95  * \brief Called just before module is unloaded
96  */
97 void UHCI_Cleanup()
98 {
99 }
100
101 tUHCI_TD *UHCI_int_AllocateTD(tUHCI_Controller *Cont)
102 {
103          int    i;
104         for(i = 0; i < NUM_TDs; i ++)
105         {
106                 if(gaUHCI_TDPool[i].Link == 0) {
107                         gaUHCI_TDPool[i].Link = 1;
108                         return &gaUHCI_TDPool[i];
109                 }
110         }
111         return NULL;
112 }
113
114 void UHCI_int_AppendTD(tUHCI_Controller *Cont, tUHCI_TD *TD)
115 {
116         Log_Warning("UHCI", "TODO: Implement AppendTD");
117 }
118
119 /**
120  * \brief Send a transaction to the USB bus
121  * \param Cont  Controller pointer
122  * \param Addr  Function Address * 16 + Endpoint
123  * \param bTgl  Data toggle value
124  */
125 void *UHCI_int_SendTransaction(tUHCI_Controller *Cont, int Addr, Uint8 Type, int bTgl, tUSBHostCb Cb, void *Data, size_t Length)
126 {
127         tUHCI_TD        *td;
128
129         if( Length > 0x400 )    return NULL;    // Controller allows up to 0x500, but USB doesn't
130
131         td = UHCI_int_AllocateTD(Cont);
132
133         td->Link = 1;
134         td->Control = (Length - 1) & 0x7FF;
135         td->Token  = ((Length - 1) & 0x7FF) << 21;
136         td->Token |= (bTgl & 1) << 19;
137         td->Token |= (Addr & 0xF) << 15;
138         td->Token |= ((Addr/16) & 0xFF) << 8;
139         td->Token |= Type;
140
141         // TODO: Ensure 32-bit paddr
142         if( ((tVAddr)Data & PAGE_SIZE) + Length > PAGE_SIZE ) {
143                 Log_Warning("UHCI", "TODO: Support non single page transfers");
144                 // TODO: Need to enable IOC to copy the data back
145 //              td->BufferPointer = 
146                 return NULL;
147         }
148         else {
149                 td->BufferPointer = MM_GetPhysAddr( (tVAddr)Data );
150         }
151
152         // Interrupt on completion
153         if( Cb ) {
154                 td->Control |= (1 << 24);
155                 td->Avaliable[0] = (tVAddr)Cb;  // NOTE: if ERRPTR then the TD is kept allocated until checked
156                 #if BITS > 32
157                 td->Avaliable[1] = (tVAddr)Cb >> 32;
158                 #endif
159                 Log_Warning("UHCI", "TODO: Support IOC... somehow");
160         }
161
162         UHCI_int_AppendTD(Cont, td);
163
164         return td;
165 }
166
167 void *UHCI_DataIN(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *Data, size_t Length)
168 {
169         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0x69, DataTgl, Cb, Data, Length);
170 }
171
172 void *UHCI_DataOUT(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *Data, size_t Length)
173 {
174         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0xE1, DataTgl, Cb, Data, Length);
175 }
176
177 void *UHCI_SendSetup(void *Ptr, int Fcn, int Endpt, int DataTgl, tUSBHostCb Cb, void *Data, size_t Length)
178 {
179         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0x2D, DataTgl, Cb, Data, Length);
180 }
181
182 void UHCI_Hub_Poll(tUSBHub *Hub, tUSBDevice *Dev)
183 {
184         tUHCI_Controller *cont = USB_GetDeviceDataPtr(Dev);
185         
186         UHCI_CheckPortUpdate(cont);
187 }
188
189 // === INTERNAL FUNCTIONS ===
190 /**
191  * \fn int UHCI_Int_InitHost(tUCHI_Controller *Host)
192  * \brief Initialises a UHCI host controller
193  * \param Host  Pointer - Host to initialise
194  */
195 int UHCI_Int_InitHost(tUHCI_Controller *Host)
196 {
197         ENTER("pHost", Host);
198
199         outw( Host->IOBase + USBCMD, 4 );       // GRESET
200         Time_Delay(10);
201         // TODO: Wait for at least 10ms
202         outw( Host->IOBase + USBCMD, 0 );       // GRESET
203         
204         // Allocate Frame List
205         // - 1 Page, 32-bit address
206         // - 1 page = 1024  4 byte entries
207         Host->FrameList = (void *) MM_AllocDMA(1, 32, &Host->PhysFrameList);
208         if( !Host->FrameList ) {
209                 Log_Warning("UHCI", "Unable to allocate frame list, aborting");
210                 LEAVE('i', -1);
211                 return -1;
212         }
213         LOG("Allocated frame list 0x%x (0x%x)", Host->FrameList, Host->PhysFrameList);
214         memsetd( Host->FrameList, 1, 1024 );    // Clear List (Disabling all entries)
215         
216         //! \todo Properly fill frame list
217         
218         // Set frame length to 1 ms
219         outb( Host->IOBase + SOFMOD, 64 );
220         
221         // Set Frame List
222         outd( Host->IOBase + FLBASEADD, Host->PhysFrameList );
223         outw( Host->IOBase + FRNUM, 0 );
224         
225         // Enable Interrupts
226         outw( Host->IOBase + USBINTR, 0x000F );
227         PCI_ConfigWrite( Host->PciId, 0xC0, 2, 0x2000 );
228
229         outw( Host->IOBase + USBCMD, 0x0001 );
230
231         LEAVE('i', 0);
232         return 0;
233 }
234
235 void UHCI_CheckPortUpdate(tUHCI_Controller *Host)
236 {
237         // Enable ports
238         for( int i = 0; i < 2; i ++ )
239         {
240                  int    port = Host->IOBase + PORTSC1 + i*2;
241                 // Check for port change
242                 if( !(inw(port) & 0x0002) )     continue;
243                 outw(port, 0x0002);
244                 
245                 // Check if the port is connected
246                 if( !(inw(port) & 1) )
247                 {
248                         // Tell the USB code it's gone.
249                         USB_DeviceDisconnected(Host->RootHub, i);
250                         continue;
251                 }
252                 else
253                 {
254                         LOG("Port %i has something", i);
255                         // Reset port (set bit 9)
256                         LOG("Reset");
257                         outw( port, 0x0100 );
258                         Time_Delay(50); // 50ms delay
259                         outw( port, inw(port) & ~0x0100 );
260                         // Enable port
261                         LOG("Enable");
262                         Time_Delay(50); // 50ms delay
263                         outw( port, inw(port) & 0x0004 );
264                         // Tell USB there's a new device
265                         USB_DeviceConnected(Host->RootHub, i);
266                 }
267         }
268 }
269
270 void UHCI_InterruptHandler(int IRQ, void *Ptr)
271 {
272         Log_Debug("UHCI", "UHIC Interrupt");
273 }

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