Misc Changes, Added Logging Subsystem, Fixes to InitRD, Working on RTL8139 driver
[tpg/acess2.git] / Modules / Network / RTL8139 / main.c
1 /* Acess2 RTL8139 Driver
2  * - By John Hodge (thePowersGang)
3  * 
4  * main.c - Driver Core
5  */
6 #define DEBUG   0
7 #define VERSION ((0<<8)|50)
8 #include <acess.h>
9 #include <modules.h>
10 #include <fs_devfs.h>
11 #include <drv_pci.h>
12 #include <tpl_drv_network.h>
13
14 // === CONSTANTS ===
15 enum eRTL8139_Regs
16 {
17         MAC0, MAC1, MAC2,
18         MAC3, MAC4, MAC5,
19         MAR0    = 0x08,
20         MAR1, MAR2, MAR3,
21         MAR4, MAR5, MAR6, MAR7,
22         
23         RBSTART = 0x30, //!< Recieve Buffer Start
24         // ??, ??, ??, RST, RE, TE, ??, ??
25         CMD     = 0x37,
26         IMR     = 0x3C,
27         ISR     = 0x3E,
28         
29         RCR     = 0x44,
30         
31         CONFIG1 = 0x52
32 };
33
34 // === TYPES ===
35 typedef struct sCard
36 {
37         Uint16  IOBase;
38         Uint8   IRQ;
39         
40          int    NumWaitingPackets;
41         
42         void    *ReceiveBuffer;
43         tPAddr  PhysReceiveBuffer;
44         
45         char    Name[2];
46         tVFS_Node       Node;
47         Uint8   MacAddr[6];
48 }       tCard;
49
50 // === PROTOTYPES ===
51
52 // === GLOBALS ===
53 MODULE_DEFINE(0, VERSION, RTL8139, RTL8139_Install, NULL, NULL);
54  int    giRTL8139_CardCount;
55 tCard   gpRTL8139_Cards;
56
57 // === CODE ===
58 /**
59  * \brief Installs the RTL8139 Driver
60  */
61 int RTL8139_Install(char **Options)
62 {
63          int    id = -1;
64          int    i = 0;
65         Uint16  base;
66         
67         giRTL8139_CardCount = PCI_CountDevices( 0x10EC, 0x8139, 0 );
68         
69         gpRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
70         
71         
72         while( (id = PCI_GetDevice(0x10EC, 0x8139, 0, id)) != -1 )
73         {
74                 base = PCI_AssignPort( id, 0, 0x100 );
75                 gpRTL8139_Cards[i].IOBase = base;
76                 gpRTL8139_Cards[i].IRQ = PCI_GetIRQ( id );
77                 
78                 // Install IRQ Handler
79                 IRQ_AddHandler(gpRTL8139_Cards[ k ].IRQ, RTL8136_IRQHandler);
80                 
81                 // Power on
82                 outb( base + CONFIG1, 0x00 );
83                 // Reset (0x10 to CMD)
84                 outb( base + CMD, 0x10 );
85                 
86                 gpRTL8139_Cards[i].ReceiveBuffer = MM_AllocDMA( 2, 32, &gpRTL8139_Cards[i].PhysReceiveBuffer );
87                 // Set up recieve buffer
88                 outl(base + RBSTART, (Uint32)gpRTL8139_Cards[i].PhysReceiveBuffer);
89                 // Set IMR to Transmit OK and Receive OK
90                 outw(base + IMR, 0x5);
91                 
92                 // Set recieve buffer size, buffer wrap and recieve mask
93                 outl(base + RCR, 0x8F);
94                 
95                 outb(base + CMD, 0x0C); // Recive Enable and Transmit Enable
96                 
97                 gpRTL8139_Cards[ i ].MacAddr[0] = inb(base+MAC0);
98                 gpRTL8139_Cards[ i ].MacAddr[1] = inb(base+MAC1);
99                 gpRTL8139_Cards[ i ].MacAddr[2] = inb(base+MAC2);
100                 gpRTL8139_Cards[ i ].MacAddr[3] = inb(base+MAC3);
101                 gpRTL8139_Cards[ i ].MacAddr[4] = inb(base+MAC4);
102                 gpRTL8139_Cards[ i ].MacAddr[5] = inb(base+MAC5);
103                 
104                 // Set VFS Node
105                 gpRTL8139_Cards[ i ].Name[0] = '0'+i;
106                 gpRTL8139_Cards[ i ].Name[1] = '\0';
107                 gpRTL8139_Cards[ i ].Node.ImplPtr = &gpRTL8139_Cards[ i ];
108                 gpRTL8139_Cards[ i ].Node.NumACLs = 0;
109                 gpRTL8139_Cards[ i ].Node.CTime = now();
110                 gpRTL8139_Cards[ i ].Node.Write = RTL8139_Write;
111                 gpRTL8139_Cards[ i ].Node.Read = RTL8139_Read;
112                 gpRTL8139_Cards[ i ].Node.IOCtl = RTL8139_IOCtl;
113                 
114                 Log_Log("RTL8139", "Card %i 0x%04x %02x:%02x:%02x:%02x:%02x:%02x",
115                         i, base,
116                         gpRTL8139_Cards[ i ].MacAddr[0], gpRTL8139_Cards[ i ].MacAddr[1],
117                         gpRTL8139_Cards[ i ].MacAddr[2], gpRTL8139_Cards[ i ].MacAddr[3],
118                         gpRTL8139_Cards[ i ].MacAddr[4], gpRTL8139_Cards[ i ].MacAddr[5]
119                         );
120                 
121                 i ++;
122         }
123         return MODULE_ERR_OK;
124 }

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