12
12
13
13
struct rtl8139 * RTL8139 = NULL ;
14
14
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 () {
18
16
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 );
20
18
}
21
19
}
22
20
@@ -31,8 +29,10 @@ void rtl8139_init(struct rtl8139* nic) {
31
29
outw (nic -> io_base + RTL8139_REG_COMMAND , RTL8139_CMD_RESET );
32
30
// while (inb(nic->io_base + RTL8139_REG_COMMAND) & RTL8139_CMD_RESET);
33
31
32
+ sleep (1 ); // for safety
33
+
34
34
// Initialize MAC address
35
- read_mac_address (nic );
35
+ read_mac_address ();
36
36
37
37
print ("Mac Address: " );
38
38
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) {
43
43
}
44
44
45
45
// 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 ) {
47
47
if (RTL8139 -> io_base == null || RTL8139 -> io_base == 0 ){
48
48
warn ("RTL8139 Card is not detected but tried to send data to it. Skipping..." , __FILE__ );
49
49
return no ;
50
50
}
51
51
// 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 );
53
53
if ((status & 0x8000 ) == 0 ) {
54
54
return no ; // Transmission is not ready
55
55
}
56
56
57
57
// Write the packet to the transmit buffer
58
58
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 ;
60
60
for (int16 i = 0 ; i < length ; i ++ ) {
61
61
outb (tx_buffer_address , data [i ]);
62
62
tx_buffer_address ++ ;
63
63
}
64
64
65
65
// 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 );
67
67
68
68
return yes ; // Return yes if transmission was successful, no otherwise
69
69
}
70
70
71
71
// Receives a packet
72
- bool rtl8139_receive_packet (struct rtl8139 * nic , int8 * buffer , int16 * length ) {
72
+ bool rtl8139_receive_packet (int8 * buffer , int16 * length ) {
73
73
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__ );
75
75
return no ;
76
76
}
77
- int16 status = inw (nic -> io_base + RTL8139_REG_RX_BUFFER );
77
+ int16 status = inw (RTL8139 -> io_base + RTL8139_REG_RX_BUFFER );
78
78
if ((status & 0x01 ) == 0 ) {
79
79
return no ; // No packet available
80
80
}
81
81
82
82
// Copy the received packet to the buffer
83
83
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 ;
85
85
* length = inw (rx_buffer_address );
86
86
rx_buffer_address += 4 ; // Skip status and reserved fields
87
87
for (int16 i = 0 ; i < * length ; i ++ ) {
@@ -90,7 +90,7 @@ bool rtl8139_receive_packet(struct rtl8139* nic, int8* buffer, int16* length) {
90
90
}
91
91
92
92
// 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 );
94
94
95
95
return yes ; // Return yes if a packet was received, no otherwise
96
96
}
0 commit comments