2 * Acess2 RTL8139 Driver
3 * - By John Hodge (thePowersGang)
8 #define VERSION ((0<<8)|50)
13 #include <tpl_drv_network.h>
22 MAR4, MAR5, MAR6, MAR7,
24 RBSTART = 0x30, //!< Recieve Buffer Start
25 // ??, ??, ??, RST, RE, TE, ??, ??
41 int NumWaitingPackets;
44 tPAddr PhysReceiveBuffer;
52 int RTL8139_Install(char **Options);
53 char *RTL8139_ReadDir(tVFS_Node *Node, int Pos);
54 tVFS_Node *RTL8139_FindDir(tVFS_Node *Node, const char *Filename);
55 int RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
56 Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
57 Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
58 void RTL8139_IRQHandler(int Num);
61 MODULE_DEFINE(0, VERSION, RTL8139, RTL8139_Install, NULL, NULL);
62 tDevFS_Driver gRTL8139_DriverInfo = {
66 .ACLs = &gVFS_ACL_EveryoneRX,
67 .Flags = VFS_FFLAG_DIRECTORY,
68 .ReadDir = RTL8139_ReadDir,
69 .FindDir = RTL8139_FindDir,
70 .IOCtl = RTL8139_RootIOCtl
73 int giRTL8139_CardCount;
74 tCard *gpRTL8139_Cards;
78 * \brief Installs the RTL8139 Driver
80 int RTL8139_Install(char **Options)
86 giRTL8139_CardCount = PCI_CountDevices( 0x10EC, 0x8139, 0 );
88 gpRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
91 while( (id = PCI_GetDevice(0x10EC, 0x8139, 0, id)) != -1 )
93 base = PCI_AssignPort( id, 0, 0x100 );
94 gpRTL8139_Cards[i].IOBase = base;
95 gpRTL8139_Cards[i].IRQ = PCI_GetIRQ( id );
97 // Install IRQ Handler
98 IRQ_AddHandler(gpRTL8139_Cards[ k ].IRQ, RTL8136_IRQHandler);
101 outb( base + CONFIG1, 0x00 );
102 // Reset (0x10 to CMD)
103 outb( base + CMD, 0x10 );
105 while( inb(base + CMD) & 0x10 ) ;
107 // Allocate 3 pages below 4GiB for the recieve buffer (Allows 8k+16+1500)
108 gpRTL8139_Cards[i].ReceiveBuffer = MM_AllocDMA( 3, 32, &gpRTL8139_Cards[i].PhysReceiveBuffer );
109 // Set up recieve buffer
110 outl(base + RBSTART, (Uint32)gpRTL8139_Cards[i].PhysReceiveBuffer);
111 // Set IMR to Transmit OK and Receive OK
112 outw(base + IMR, 0x5);
114 // Set recieve buffer size and recieve mask
115 outl(base + RCR, 0x0F);
117 outb(base + CMD, 0x0C); // Recive Enable and Transmit Enable
119 // Get the card's MAC address
120 gpRTL8139_Cards[ i ].MacAddr[0] = inb(base+MAC0);
121 gpRTL8139_Cards[ i ].MacAddr[1] = inb(base+MAC1);
122 gpRTL8139_Cards[ i ].MacAddr[2] = inb(base+MAC2);
123 gpRTL8139_Cards[ i ].MacAddr[3] = inb(base+MAC3);
124 gpRTL8139_Cards[ i ].MacAddr[4] = inb(base+MAC4);
125 gpRTL8139_Cards[ i ].MacAddr[5] = inb(base+MAC5);
128 gpRTL8139_Cards[ i ].Name[0] = '0'+i;
129 gpRTL8139_Cards[ i ].Name[1] = '\0';
130 gpRTL8139_Cards[ i ].Node.ImplPtr = &gpRTL8139_Cards[ i ];
131 gpRTL8139_Cards[ i ].Node.NumACLs = 0;
132 gpRTL8139_Cards[ i ].Node.CTime = now();
133 gpRTL8139_Cards[ i ].Node.Write = RTL8139_Write;
134 gpRTL8139_Cards[ i ].Node.Read = RTL8139_Read;
135 gpRTL8139_Cards[ i ].Node.IOCtl = RTL8139_IOCtl;
137 Log_Log("RTL8139", "Card %i 0x%04x %02x:%02x:%02x:%02x:%02x:%02x",
139 gpRTL8139_Cards[ i ].MacAddr[0], gpRTL8139_Cards[ i ].MacAddr[1],
140 gpRTL8139_Cards[ i ].MacAddr[2], gpRTL8139_Cards[ i ].MacAddr[3],
141 gpRTL8139_Cards[ i ].MacAddr[4], gpRTL8139_Cards[ i ].MacAddr[5]
146 return MODULE_ERR_OK;
149 // --- Root Functions ---
150 char *RTL8139_ReadDir(tVFS_Node *Node, int Pos)
155 tVFS_Node *RTL8139_FindDir(tVFS_Node *Node, const char *Filename)
160 int RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg)
165 // --- File Functions ---
166 Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
171 Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
176 int RTL8139_IOCtl(tVFS_Node *Node, int ID, void *Arg)