Skip to content

Commit 94274c7

Browse files
Improved RTL card.
1 parent b063579 commit 94274c7

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

source/drivers/networking/ethernet/RTL/8139.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212

1313
struct rtl8139* RTL8139 = NULL;
1414

15-
// Function to read the MAC address from EEPROM
16-
void read_mac_address(struct rtl8139* nic) {
17-
// Read the MAC address from the EEPROM
15+
void read_mac_address() {
1816
for (int i = 0; i < 6; i++) {
19-
nic->mac_address[i] = inb(nic->io_base + RTL8139_REG_MAC + i);
17+
RTL8139->mac_address[i] = inb(RTL8139->io_base + RTL8139_REG_MAC + i);
2018
}
2119
}
2220

@@ -31,8 +29,10 @@ void rtl8139_init(struct rtl8139* nic) {
3129
outw(nic->io_base + RTL8139_REG_COMMAND, RTL8139_CMD_RESET);
3230
// while (inb(nic->io_base + RTL8139_REG_COMMAND) & RTL8139_CMD_RESET);
3331

32+
sleep(1); // for safety
33+
3434
// Initialize MAC address
35-
read_mac_address(nic);
35+
read_mac_address();
3636

3737
print("Mac Address: ");
3838
printf("%x:%x:%x:%x:%x:%x", nic->mac_address[0], nic->mac_address[1], nic->mac_address[2], nic->mac_address[3], nic->mac_address[4], nic->mac_address[5]);
@@ -43,45 +43,45 @@ void rtl8139_init(struct rtl8139* nic) {
4343
}
4444

4545
// Transmit a packet
46-
bool rtl8139_send_packet(struct rtl8139* nic, const int8* data, int16 length) {
46+
bool rtl8139_send_packet(const int8* data, int16 length) {
4747
if(RTL8139->io_base == null || RTL8139->io_base == 0){
4848
warn("RTL8139 Card is not detected but tried to send data to it. Skipping...", __FILE__);
4949
return no;
5050
}
5151
// Check if the NIC is ready for transmission (status checks)
52-
int16 status = inw(nic->io_base + RTL8139_REG_TX_STATUS);
52+
int16 status = inw(RTL8139->io_base + RTL8139_REG_TX_STATUS);
5353
if ((status & 0x8000) == 0) {
5454
return no; // Transmission is not ready
5555
}
5656

5757
// Write the packet to the transmit buffer
5858
int16 tx_buffer_offset = status >> 11;
59-
int16 tx_buffer_address = nic->io_base + RTL8139_REG_TX_ADDR + tx_buffer_offset;
59+
int16 tx_buffer_address = RTL8139->io_base + RTL8139_REG_TX_ADDR + tx_buffer_offset;
6060
for (int16 i = 0; i < length; i++) {
6161
outb(tx_buffer_address, data[i]);
6262
tx_buffer_address++;
6363
}
6464

6565
// Trigger transmission
66-
outw(nic->io_base + RTL8139_REG_TX_STATUS, (tx_buffer_offset << 11) | length);
66+
outw(RTL8139->io_base + RTL8139_REG_TX_STATUS, (tx_buffer_offset << 11) | length);
6767

6868
return yes; // Return yes if transmission was successful, no otherwise
6969
}
7070

7171
// Receives a packet
72-
bool rtl8139_receive_packet(struct rtl8139* nic, int8* buffer, int16* length) {
72+
bool rtl8139_receive_packet(int8* buffer, int16* length) {
7373
if(RTL8139->io_base == null || RTL8139->io_base == 0){
74-
// warn("RTL8139 Card is not detected but tried to initialize it. Skipping...", __FILE__);
74+
warn("RTL8139 Card is not detected but tried to receive data. Skipping...", __FILE__);
7575
return no;
7676
}
77-
int16 status = inw(nic->io_base + RTL8139_REG_RX_BUFFER);
77+
int16 status = inw(RTL8139->io_base + RTL8139_REG_RX_BUFFER);
7878
if ((status & 0x01) == 0) {
7979
return no; // No packet available
8080
}
8181

8282
// Copy the received packet to the buffer
8383
int16 rx_buffer_offset = status >> 1;
84-
int16 rx_buffer_address = nic->io_base + RTL8139_REG_RX_BUFFER + rx_buffer_offset;
84+
int16 rx_buffer_address = RTL8139->io_base + RTL8139_REG_RX_BUFFER + rx_buffer_offset;
8585
*length = inw(rx_buffer_address);
8686
rx_buffer_address += 4; // Skip status and reserved fields
8787
for (int16 i = 0; i < *length; i++) {
@@ -90,7 +90,7 @@ bool rtl8139_receive_packet(struct rtl8139* nic, int8* buffer, int16* length) {
9090
}
9191

9292
// Notify the NIC that the packet is read (update the RX buffer offset)
93-
outw(nic->io_base + RTL8139_REG_RX_BUFFER, rx_buffer_offset);
93+
outw(RTL8139->io_base + RTL8139_REG_RX_BUFFER, rx_buffer_offset);
9494

9595
return yes; // Return yes if a packet was received, no otherwise
9696
}

source/includes/drivers/rtl8139.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
#define RTL8139_IOADDR1 0x10
3434
#define RTL8139_CMD 0x37
3535

36+
// RTL8139 Status
37+
#define TOK 0x0001
38+
#define ROK 0x0002
39+
40+
#define RTL8139_IRQ_LINE 0x3C
41+
3642
struct rtl8139 {
3743
int16 io_base;
3844
int8 mac_address[6];
@@ -45,7 +51,7 @@ extern struct rtl8139* RTL8139;
4551
*
4652
* @param nic
4753
*/
48-
void read_mac_address(struct rtl8139* nic);
54+
void read_mac_address();
4955

5056
/**
5157
* @brief Initialize RTL8139 NIC
@@ -57,21 +63,19 @@ void rtl8139_init(struct rtl8139* nic);
5763
/**
5864
* @brief Transmit a packet from the RTL8139
5965
*
60-
* @param nic
6166
* @param data
6267
* @param length
6368
* @return true if successfully sent.
6469
* @return false if failed to sent.
6570
*/
66-
bool rtl8139_send_packet(struct rtl8139* nic, const int8* data, int16 length);
71+
bool rtl8139_send_packet(const int8* data, int16 length);
6772

6873
/**
6974
* @brief Receives a packet
7075
*
71-
* @param nic the pointer to RTL structure
7276
* @param buffer the received data
7377
* @param length the length of buffer
7478
* @return [true] Return yes if a packet was received
7579
* @return [false] Return false if a packet was not received
7680
*/
77-
bool rtl8139_receive_packet(struct rtl8139* nic, int8* buffer, int16* length);
81+
bool rtl8139_receive_packet(int8* buffer, int16* length);

source/includes/isr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ void irqHandler(InterruptFrame* frame);
3939
*/
4040
void registerInterruptHandler(uint8_t irq, irq_handler handler);
4141

42+
void rtl8139_handler(InterruptFrame* frame);
43+
4244
#endif

source/kernel/C/interrupts/isr.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <isr.h>
1313
#include <keyboard.h>
1414
#include <memory2.h>
15+
#include <drivers/rtl8139.h>
1516

1617
irq_handler interrupt_handlers[256];
1718

@@ -69,4 +70,15 @@ void irqHandler(InterruptFrame* frame)
6970
irq_handler handler = (irq_handler)interrupt_handlers[frame->int_no];
7071
if (handler != NULL)
7172
handler(frame);
72-
}
73+
}
74+
75+
void rtl8139_handler(InterruptFrame* frame) {
76+
uint16_t status = inw(RTL8139->io_base + 0x3e);
77+
outw(RTL8139->io_base + 0x3E, 0x05);
78+
if(status & TOK) {
79+
info("Successfully sent a packet!", __FILE__);
80+
}
81+
if (status & ROK) {
82+
info("Successfully recieved a packet!", __FILE__);
83+
}
84+
}

source/kernel/C/pci.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <ahci.h>
1313
#include <hal.h>
1414
#include <pci.h>
15+
#include <isr.h>
1516

1617
const char* display_adapter_name = "Frost Generic Display Adapter";
1718
const char* GPUName[1] = {"Frost Generic Display Driver for Graphics Processing Unit."}; //Max 2 GPUs allowed
@@ -395,6 +396,11 @@ void probe_pci(){
395396
if(device == 0x8139){ // Indeed it is an RTL8139 Card
396397
RTL8139->io_base = (int16)(pci_read_word(bus, slot, 0, RTL8139_IOADDR1) & 0xFFFC);
397398
deviceName = "RTL8139 Networking Card";
399+
int8 rtl8139_irq = pci_read_word(bus, slot, 0, RTL8139_IRQ_LINE);
400+
401+
printf("Handler number : 0x%x", rtl8139_irq);
402+
403+
registerInterruptHandler(rtl8139_irq, rtl8139_handler);
398404
}
399405
}
400406

0 commit comments

Comments
 (0)