Modules/USB - Fiddling with USB
[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                 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, int bIOC, 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         if( bIOC ) {
153                 td->Control |= (1 << 24);
154                 Log_Warning("UHCI", "TODO: Support IOC... somehow");
155         }
156
157         UHCI_int_AppendTD(Cont, td);
158
159         return td;
160 }
161
162 void *UHCI_DataIN(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length)
163 {
164         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0x69, DataTgl, bIOC, Data, Length);
165 }
166
167 void *UHCI_DataOUT(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length)
168 {
169         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0xE1, DataTgl, bIOC, Data, Length);
170 }
171
172 void *UHCI_SendSetup(void *Ptr, int Fcn, int Endpt, int DataTgl, int bIOC, void *Data, size_t Length)
173 {
174         return UHCI_int_SendTransaction(Ptr, Fcn*16+Endpt, 0x2D, DataTgl, bIOC, Data, Length);
175 }
176
177 void UHCI_Hub_Poll(tUSBHub *Hub, tUSBDevice *Dev)
178 {
179         tUHCI_Controller        *cont = USB_GetDeviceDataPtr(Dev);
180         
181         UHCI_CheckPortUpdate(cont);
182 }
183
184 // === INTERNAL FUNCTIONS ===
185 /**
186  * \fn int UHCI_Int_InitHost(tUCHI_Controller *Host)
187  * \brief Initialises a UHCI host controller
188  * \param Host  Pointer - Host to initialise
189  */
190 int UHCI_Int_InitHost(tUHCI_Controller *Host)
191 {
192         ENTER("pHost", Host);
193
194         outw( Host->IOBase + USBCMD, 4 );       // GRESET
195         Time_Delay(10);
196         // TODO: Wait for at least 10ms
197         outw( Host->IOBase + USBCMD, 0 );       // GRESET
198         
199         // Allocate Frame List
200         // - 1 Page, 32-bit address
201         // - 1 page = 1024  4 byte entries
202         Host->FrameList = (void *) MM_AllocDMA(1, 32, &Host->PhysFrameList);
203         if( !Host->FrameList ) {
204                 Log_Warning("UHCI", "Unable to allocate frame list, aborting");
205                 LEAVE('i', -1);
206                 return -1;
207         }
208         LOG("Allocated frame list 0x%x (0x%x)", Host->FrameList, Host->PhysFrameList);
209         memsetd( Host->FrameList, 1, 1024 );    // Clear List (Disabling all entries)
210         
211         //! \todo Properly fill frame list
212         
213         // Set frame length to 1 ms
214         outb( Host->IOBase + SOFMOD, 64 );
215         
216         // Set Frame List
217         outd( Host->IOBase + FLBASEADD, Host->PhysFrameList );
218         outw( Host->IOBase + FRNUM, 0 );
219         
220         // Enable Interrupts
221         outw( Host->IOBase + USBINTR, 0x000F );
222         PCI_ConfigWrite( Host->PciId, 0xC0, 2, 0x2000 );
223
224         outw( Host->IOBase + USBCMD, 0x0001 );
225
226         LEAVE('i', 0);
227         return 0;
228 }
229
230 void UHCI_CheckPortUpdate(tUHCI_Controller *Host)
231 {
232         // Enable ports
233         for( int i = 0; i < 2; i ++ )
234         {
235                  int    port = Host->IOBase + PORTSC1 + i*2;
236                 // Check for port change
237                 if( !(inw(port) & 0x0002) )     continue;
238                 outw(port, 0x0002);
239                 
240                 // Check if the port is connected
241                 if( !(inw(port) & 1) )
242                 {
243                         // Tell the USB code it's gone.
244                         USB_DeviceDisconnected(Host->RootHub, i);
245                         continue;
246                 }
247                 else
248                 {
249                         LOG("Port %i has something", i);
250                         // Reset port (set bit 9)
251                         LOG("Reset");
252                         outw( port, 0x0100 );
253                         Time_Delay(50); // 50ms delay
254                         outw( port, inw(port) & ~0x0100 );
255                         // Enable port
256                         LOG("Enable");
257                         Time_Delay(50); // 50ms delay
258                         outw( port, inw(port) & 0x0004 );
259                         // Tell USB there's a new device
260                         USB_DeviceConnected(Host->RootHub, i);
261                 }
262         }
263 }
264
265 void UHCI_InterruptHandler(int IRQ, void *Ptr)
266 {
267         Log_Debug("UHCI", "UHIC Interrupt");
268 }

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