99#include <print.h>
1010
1111#include <device.h>
12- #include <devices/pci.h>
1312#include <devices/netdev.h>
13+ #include <devices/pci.h>
1414#include <net/eth.h>
1515
1616struct rtl8139_regs {
@@ -34,20 +34,14 @@ struct rtl8139 {
3434bool send_packet (struct netdev * this , u8 packet [], usize length );
3535struct mac get_mac (struct netdev * this );
3636
37- struct netdev_ops rtl8139_ops = {
38- .send_packet = send_packet ,
39- .get_mac = get_mac
40- };
37+ struct netdev_ops rtl8139_ops = {.send_packet = send_packet ,
38+ .get_mac = get_mac };
4139
4240constexpr usize TX_BUFF_SIZE = 0x1700 ;
4341constexpr u16 rtl8139_vid = 0x10ec ;
4442constexpr u16 rtl8139_did = 0x8139 ;
4543
46- enum rtl8139_registers {
47- REG_COM = 0x37 ,
48- REG_TCR = 0x40 ,
49- REG_CONFIG1 = 0x52
50- };
44+ enum rtl8139_registers { REG_COM = 0x37 , REG_TCR = 0x40 , REG_CONFIG1 = 0x52 };
5145
5246enum rtl8139_flags {
5347 TSD_TOK = (1 << 15 ),
@@ -60,10 +54,10 @@ enum rtl8139_flags {
6054
6155u8 rtl8139_tbuff [4 ][TX_BUFF_SIZE ] __attribute__((aligned (256 )));
6256
63- static_assert ( offsetof(struct rtl8139_regs , tsad ) == 0x20 );
64- static_assert ( offsetof(struct rtl8139_regs , tsd ) == 0x10 );
65- static_assert ( offsetof(struct rtl8139_regs , com ) == 0x37 );
66- static_assert ( offsetof(struct rtl8139_regs , config1 ) == 0x52 );
57+ static_assert (offsetof(struct rtl8139_regs , tsad ) == 0x20 );
58+ static_assert (offsetof(struct rtl8139_regs , tsd ) == 0x10 );
59+ static_assert (offsetof(struct rtl8139_regs , com ) == 0x37 );
60+ static_assert (offsetof(struct rtl8139_regs , config1 ) == 0x52 );
6761
6862bool mm_paging_walk (uaddr va , paddr * pte , bool alloc );
6963
@@ -81,15 +75,15 @@ u32 walkaddr(uaddr addr) {
8175
8276 pte = (pte >> 10 ) << 12 ;
8377 assert (pte < U32_MAX );
84- return (u32 ) pte | off ;
78+ return (u32 )pte | off ;
8579}
8680
8781struct mac get_mac (struct netdev * this ) {
8882 struct rtl8139 * rtl_this = (struct rtl8139 * )(this -> device );
8983 volatile struct rtl8139_regs * rtl_regs = rtl_this -> regs ;
9084 struct mac mac ;
9185
92- for (usize i = 0 ; i < 6 ; ++ i ) {
86+ for (usize i = 0 ; i < 6 ; ++ i ) {
9387 mac .addr [i ] = rtl_regs -> mac [i ];
9488 }
9589 return mac ;
@@ -98,10 +92,10 @@ struct mac get_mac(struct netdev *this) {
9892bool send_packet (struct netdev * this , u8 packet [], usize length ) {
9993 struct rtl8139 * rtl_this = container_of (this , struct rtl8139 , netdev );
10094 volatile struct rtl8139_regs * rtl_regs = rtl_this -> regs ;
101- u32 phys_buffer = walkaddr ((uaddr ) (& rtl8139_tbuff [0 ]));
95+ u32 phys_buffer = walkaddr ((uaddr )(& rtl8139_tbuff [0 ]));
10296 u64 i ;
10397
104- for (i = 0 ; i < length ; ++ i ) {
98+ for (i = 0 ; i < length ; ++ i ) {
10599 rtl8139_tbuff [rtl_this -> current_buffer ][i ] = packet [i ];
106100 }
107101
@@ -110,26 +104,23 @@ bool send_packet(struct netdev *this, u8 packet[], usize length) {
110104 }
111105
112106 rtl_regs -> tsad [rtl_this -> current_buffer ] = phys_buffer ;
113- rtl_regs -> tsd [rtl_this -> current_buffer ] = (u32 ) length ;
107+ rtl_regs -> tsd [rtl_this -> current_buffer ] = (u32 )length ;
114108
115109 ++ rtl_this -> current_buffer ;
116110 return true;
117111}
118112
119-
120113void rtl8139_test (struct rtl8139 * rtl_device ) {
121114 volatile struct rtl8139_regs * rtl_regs = rtl_device -> regs ;
122115 struct mac mac ;
123- memcpy (mac .addr , (const u8 * ) "\xff\xff\xff\xff\xff\xff" , 6 );
116+ memcpy (mac .addr , (const u8 * ) "\xff\xff\xff\xff\xff\xff" , 6 );
124117
125- eth_send_packet (& rtl_device -> netdev , mac ,
126- (u8 * ) "yellow submarine" , 16 );
118+ eth_send_packet (& rtl_device -> netdev , mac , (u8 * )"yellow submarine" , 16 );
127119
128120 assert (rtl_regs -> tsad [0 ] != 0 );
129121 assert ((rtl_regs -> tsd [0 ] & TSD_TOK ) != 0 );
130122}
131123
132-
133124static struct device * add_device (paddr reg_addr , usize reg_size ) {
134125 struct rtl8139 * device = nullptr ;
135126
@@ -164,10 +155,9 @@ static struct device *add_device(paddr reg_addr, usize reg_size) {
164155 }
165156 free (device );
166157 return nullptr ;
167-
168158}
169159
170- void rtl8139_init (struct pci * pci , struct pci_regs * pci_device ) {
160+ void rtl8139_init (struct pci * pci , struct pci_regs * pci_device ) {
171161 print ("initializing rtl8139" );
172162
173163 volatile struct rtl8139_regs * rtl_regs ;
@@ -179,10 +169,11 @@ void rtl8139_init(struct pci* pci, struct pci_regs *pci_device) {
179169
180170 /* Should always be true, see mmio_alloc */
181171 assert (low < U32_MAX );
182- reg_addr = (u32 ) low ;
172+ reg_addr = (u32 )low ;
183173
184- struct device * device = add_device (paddr_of_bits (reg_addr ), sizeof (struct rtl8139_regs ));
185- struct rtl8139 * rtl_device = (struct rtl8139 * ) device ;
174+ struct device * device =
175+ add_device (paddr_of_bits (reg_addr ), sizeof (struct rtl8139_regs ));
176+ struct rtl8139 * rtl_device = (struct rtl8139 * )device ;
186177 rtl_regs = rtl_device -> regs ;
187178
188179 /* TODO: Maybe move PCI init to pci.c? */
@@ -193,7 +184,8 @@ void rtl8139_init(struct pci* pci, struct pci_regs *pci_device) {
193184 rtl_regs -> config1 = 0 ;
194185
195186 rtl_regs -> com = COM_RST ;
196- while (rtl_regs -> com & COM_RST ) ;
187+ while (rtl_regs -> com & COM_RST )
188+ ;
197189 rtl_regs -> com = COM_RE | COM_TE ;
198190
199191 rtl_regs -> tsad [0 ] = 0x12 ;
0 commit comments