Modules/EHCI - Planning out, might need USB HC API changes
[tpg/acess2.git] / KernelLand / Modules / USB / EHCI / ehci.c
1 /*
2  * Acess2 EHCI Driver
3  * - By John Hodge (thePowersGang)
4  * 
5  * ehci.c
6  * - Driver Core
7  */
8 #define DEBUG   1
9 #define VERSION VER2(0,1)
10 #include <acess.h>
11 #include <modules.h>
12 #include <usb_host.h>
13 #include "ehci.h"
14 #include <drv_pci.h>
15
16 // === CONSTANTS ===
17 #define EHCI_MAX_CONTROLLERS    4
18
19 // === PROTOTYPES ===
20  int    EHCI_Initialise(char **Arguments);
21  int    EHCI_Cleanup(void);
22  int    EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum);
23 void    EHCI_InterruptHandler(int IRQ, void *Ptr);
24 // -- API ---
25 void    *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bInput, int Period, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
26 void    *EHCI_InitIsoch  (void *Ptr, int Endpoint);
27 void    *EHCI_InitControl(void *Ptr, int Endpoint);
28 void    *EHCI_InitBulk   (void *Ptr, int Endpoint);
29 void    *EHCI_RemEndpoint(void *Ptr, void *Handle);
30 void    *EHCI_SETUP(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, void *Data, size_t Length);
31 void    *EHCI_IN   (void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, void *Data, size_t Length);
32 void    *EHCI_OUT  (void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, void *Data, size_t Length);
33
34 // === GLOBALS ===
35 MODULE_DEFINE(0, VERSION, USB_EHCI, EHCI_Initialise, NULL, "USB_Core", NULL);
36 tEHCI_Controller        gaEHCI_Controllers[EHCI_MAX_CONTROLLERS];
37
38 // === CODE ===
39 int EHCI_Initialise(char **Arguments)
40 {
41         for( int id = -1; (id = PCI_GetDeviceByClass(0x0C0320, 0xFFFFFF, id)) >= 0;  )
42         {
43                 Uint32  addr = PCI_GetBAR(id, 0);
44                 if( addr == 0 ) {
45                         // Oops, PCI BIOS emulation time
46                 }
47                 Uint8   irq = PCI_GetIRQ(id);
48                 if( irq == 0 ) {
49                         // TODO: The same
50                 }
51
52                 if( EHCI_InitController(addr, irq) ) {
53                         // TODO: Detect other forms of failure than "out of slots"
54                         break ;
55                 }
56         }
57         return 0;
58 }
59
60 int EHCI_Cleanup(void)
61 {
62         return 0;
63 }
64
65 // --- Driver Init ---
66 int EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum)
67 {
68         tEHCI_Controller        *cont = NULL;
69
70         for( int i = 0; i < EHCI_MAX_CONTROLLERS; i ++ )
71         {
72                 if( gaEHCI_Controllers[i].PhysBase == 0 ) {
73                         cont = &gaEHCI_Controllers[i];
74                         cont->PhysBase = BaseAddress;
75                         break;
76                 }
77         }
78
79         if(!cont) {
80                 return 1;
81         }
82
83         // -- Build up structure --
84         cont->CapRegs = (void*)MM_MapHWPages(BaseAddress, 1);
85         // TODO: Error check
86         cont->OpRegs = (void*)( (Uint32*)cont->CapRegs + cont->CapRegs->CapLength / 4 );
87         // - Allocate periodic queue
88         cont->PeriodicQueue = (void*)MM_AllocDMA(1, 32, NULL);
89         // TODO: Error check
90
91         // -- Bind IRQ --
92         IRQ_AddHandler(InterruptNum, EHCI_InterruptHandler, cont);
93
94         // -- Initialisation procedure (from ehci-r10) --
95         // - Reset controller
96         cont->OpRegs->USBCmd = USBCMD_HCReset;
97         // - Set CTRLDSSEGMENT (TODO: 64-bit support)
98         // - Set USBINTR
99         cont->OpRegs->USBIntr = USBINTR_IOC|USBINTR_PortChange|USBINTR_FrameRollover;
100         // - Set PERIODICLIST BASE
101         cont->OpRegs->PeridocListBase = MM_GetPhysAddr( (tVAddr) cont->PeriodicQueue );
102         // - Enable controller
103         cont->OpRegs->USBCmd = (0x40 << 16) | USBCMD_PeriodicEnable | USBCMD_Run;
104         // - Route all ports
105         cont->OpRegs->ConfigFlag = 1;
106         
107         return 0;
108 }
109
110 void EHCI_InterruptHandler(int IRQ, void *Ptr)
111 {
112
113 }

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