Fixing bugs and removing debug statements
[tpg/acess2.git] / Modules / Network / RTL8139 / main.c
1 /*
2  * Acess2 RTL8139 Driver
3  * - By John Hodge (thePowersGang)
4  * 
5  * main.c - Driver Core
6  */
7 #define DEBUG   0
8 #define VERSION ((0<<8)|50)
9 #include <acess.h>
10 #include <modules.h>
11 #include <fs_devfs.h>
12 #include <drv_pci.h>
13 #include <tpl_drv_network.h>
14
15 // === CONSTANTS ===
16 enum eRTL8139_Regs
17 {
18         MAC0, MAC1, MAC2,
19         MAC3, MAC4, MAC5,
20         MAR0    = 0x08,
21         MAR1, MAR2, MAR3,
22         MAR4, MAR5, MAR6, MAR7,
23         
24         RBSTART = 0x30, //!< Recieve Buffer Start (DWord)
25         // 0x31, 0x32, 0x33
26         
27         // ??, ??, ??, RST, RE, TE, ??, ??
28         CMD     = 0x37,
29         IMR     = 0x3C,
30         ISR     = 0x3E,
31         
32         RCR     = 0x44,
33         
34         CONFIG1 = 0x52
35 };
36
37 // === TYPES ===
38 typedef struct sCard
39 {
40         Uint16  IOBase;
41         Uint8   IRQ;
42         
43          int    NumWaitingPackets;
44         
45         void    *ReceiveBuffer;
46         tPAddr  PhysReceiveBuffer;
47         
48         char    Name[2];
49         tVFS_Node       Node;
50         Uint8   MacAddr[6];
51 }       tCard;
52
53 // === PROTOTYPES ===
54  int    RTL8139_Install(char **Options);
55 char    *RTL8139_ReadDir(tVFS_Node *Node, int Pos);
56 tVFS_Node       *RTL8139_FindDir(tVFS_Node *Node, const char *Filename);
57  int    RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
58 Uint64  RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
59 Uint64  RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
60 void    RTL8139_IRQHandler(int Num);
61
62 // === GLOBALS ===
63 MODULE_DEFINE(0, VERSION, RTL8139, RTL8139_Install, NULL, NULL);
64 tDevFS_Driver   gRTL8139_DriverInfo = {
65         NULL, "RTL8139",
66         {
67         .NumACLs = 1,
68         .ACLs = &gVFS_ACL_EveryoneRX,
69         .Flags = VFS_FFLAG_DIRECTORY,
70         .ReadDir = RTL8139_ReadDir,
71         .FindDir = RTL8139_FindDir,
72         .IOCtl = RTL8139_RootIOCtl
73         }
74 };
75  int    giRTL8139_CardCount;
76 tCard   *gpRTL8139_Cards;
77
78 // === CODE ===
79 /**
80  * \brief Installs the RTL8139 Driver
81  */
82 int RTL8139_Install(char **Options)
83 {
84          int    id = -1;
85          int    i = 0;
86         Uint16  base;
87         
88         giRTL8139_CardCount = PCI_CountDevices( 0x10EC, 0x8139, 0 );
89         
90         gpRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
91         
92         
93         while( (id = PCI_GetDevice(0x10EC, 0x8139, 0, id)) != -1 )
94         {
95                 base = PCI_AssignPort( id, 0, 0x100 );
96                 gpRTL8139_Cards[i].IOBase = base;
97                 gpRTL8139_Cards[i].IRQ = PCI_GetIRQ( id );
98                 
99                 // Install IRQ Handler
100                 IRQ_AddHandler(gpRTL8139_Cards[ k ].IRQ, RTL8136_IRQHandler);
101                 
102                 // Power on
103                 outb( base + CONFIG1, 0x00 );
104                 // Reset (0x10 to CMD)
105                 outb( base + CMD, 0x10 );
106                 
107                 while( inb(base + CMD) & 0x10 ) ;
108                 
109                 // Allocate 3 pages below 4GiB for the recieve buffer (Allows 8k+16+1500)
110                 gpRTL8139_Cards[i].ReceiveBuffer = MM_AllocDMA( 3, 32, &gpRTL8139_Cards[i].PhysReceiveBuffer );
111                 // Set up recieve buffer
112                 outl(base + RBSTART, (Uint32)gpRTL8139_Cards[i].PhysReceiveBuffer);
113                 // Set IMR to Transmit OK and Receive OK
114                 outw(base + IMR, 0x5);
115                 
116                 // Set recieve buffer size and recieve mask
117                 outl(base + RCR, 0x0F);
118                 
119                 outb(base + CMD, 0x0C); // Recive Enable and Transmit Enable
120                 
121                 // Get the card's MAC address
122                 gpRTL8139_Cards[ i ].MacAddr[0] = inb(base+MAC0);
123                 gpRTL8139_Cards[ i ].MacAddr[1] = inb(base+MAC1);
124                 gpRTL8139_Cards[ i ].MacAddr[2] = inb(base+MAC2);
125                 gpRTL8139_Cards[ i ].MacAddr[3] = inb(base+MAC3);
126                 gpRTL8139_Cards[ i ].MacAddr[4] = inb(base+MAC4);
127                 gpRTL8139_Cards[ i ].MacAddr[5] = inb(base+MAC5);
128                 
129                 // Set VFS Node
130                 gpRTL8139_Cards[ i ].Name[0] = '0'+i;
131                 gpRTL8139_Cards[ i ].Name[1] = '\0';
132                 gpRTL8139_Cards[ i ].Node.ImplPtr = &gpRTL8139_Cards[ i ];
133                 gpRTL8139_Cards[ i ].Node.NumACLs = 0;
134                 gpRTL8139_Cards[ i ].Node.CTime = now();
135                 gpRTL8139_Cards[ i ].Node.Write = RTL8139_Write;
136                 gpRTL8139_Cards[ i ].Node.Read = RTL8139_Read;
137                 gpRTL8139_Cards[ i ].Node.IOCtl = RTL8139_IOCtl;
138                 
139                 Log_Log("RTL8139", "Card %i 0x%04x %02x:%02x:%02x:%02x:%02x:%02x",
140                         i, base,
141                         gpRTL8139_Cards[ i ].MacAddr[0], gpRTL8139_Cards[ i ].MacAddr[1],
142                         gpRTL8139_Cards[ i ].MacAddr[2], gpRTL8139_Cards[ i ].MacAddr[3],
143                         gpRTL8139_Cards[ i ].MacAddr[4], gpRTL8139_Cards[ i ].MacAddr[5]
144                         );
145                 
146                 i ++;
147         }
148         return MODULE_ERR_OK;
149 }
150
151 // --- Root Functions ---
152 char *RTL8139_ReadDir(tVFS_Node *Node, int Pos)
153 {
154         if( Pos < 0 || Pos > giRTL8139_CardCount )      return NULL;
155         
156         return strdup( gpRTL8139_Cards[Pos].Name );
157 }
158
159 tVFS_Node *RTL8139_FindDir(tVFS_Node *Node, const char *Filename)
160 {
161         //TODO: It might be an idea to supprt >10 cards
162         if(Filename[0] == '\0' || Filename[0] != '\0')  return NULL;
163         if(Filename[0] < '0' || Filename[0] > '9')      return NULL;
164         return &gpRTL8139_Cards[ Filename[0]-'0' ].Node;
165 }
166
167 const char *csaRTL8139_RootIOCtls[] = {DRV_IOCTLNAMES, NULL};
168 int RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg)
169 {
170         switch(ID)
171         {
172         BASE_IOCTLS(DRV_TYPE_NETWORK, "RTL8139", VERSION, csaRTL8139_RootIOCtls);
173         }
174         return 0;
175 }
176
177 // --- File Functions ---
178 Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
179 {
180         return 0;
181 }
182
183 Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
184 {
185         return 0;
186 }
187
188 const char *csaRTL8139_NodeIOCtls[] = {DRV_IOCTLNAMES, NULL};
189 int RTL8139_IOCtl(tVFS_Node *Node, int ID, void *Arg)
190 {
191         switch(ID)
192         {
193         BASE_IOCTLS(DRV_TYPE_NETWORK, "RTL8139", VERSION, csaRTL8139_NodeIOCtls);
194         }
195         return 0;
196 }

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