#include "rtl8139.h" #include "string.h" #include "stdio.h" #define DEBUG /* Offset (from IO base) Size Name 0x08 8 MAR0-7 0x30 4 RBSTART 0x37 1 CMD 0x3C 2 IMR 0x3E 2 ISR */ static rtl8139_vtable_t const rtl8139_vtable = { { rtl8139_activate, rtl8139_deactivate, rtl8139_deinit, rtl8139_print_info } }; // registers #define REG_MAC0 0x00 #define REG_CMD 0x37 // commands for REG_CMD register #define CR_RESET 0x08 void rtl8139_init( rtl8139_t *rtl8139, pci_device_descriptor_t *descriptor, interrupt_t *interrupt, void *context ) { memset( rtl8139, 0, sizeof( rtl8139_t ) ); network_init( (network_t *)rtl8139, interrupt, context ); puts( "Initializing driver for Realtek 8139 network card.." ); for( int i = 0; i < NOF_MAC_REGISTERS; i++ ) { port8_init( &rtl8139->MAC_port[i], descriptor->port_base + REG_MAC0 + i ); } port8_init( &rtl8139->CMD_port, descriptor->port_base + REG_CMD ); // software reset port8_write( &rtl8139->CMD_port, CR_RESET ); // get MAC for( int i = 0; i < NOF_MAC_REGISTERS; i++ ) { rtl8139->base.mac_address.byte[i] = port8_read( &rtl8139->MAC_port[i] ); } #ifdef DEBUG char buf[20]; network_mac_to_string( rtl8139->base.mac_address, buf, 20 ); printf( "rtl8139 NIC at I/O base 0x%X, interrupt 0x%X, MAC: %s\n", descriptor->port_base, descriptor->interrupt, buf ); #endif interrupt_handler_init( &rtl8139->interrupt_handler, IRQ_BASE + descriptor->interrupt, interrupt, rtl8139_handle_interrupt, rtl8139 ); interrupts_register_interrupt_handler( rtl8139->interrupt_handler ); ((driver_t *)rtl8139)->vtable = (driver_vtable_t *)&rtl8139_vtable; } void rtl8139_activate( void *obj ) { puts( "Activating driver for Realtek 8139 network card.." ); rtl8139_t *rtl8139 = obj; } void rtl8139_deactivate( void *obj ) { puts( "Dectivating driver for Realtek 8139 network card.." ); rtl8139_t *rtl8139 = obj; } void rtl8139_deinit( void *obj ) { // nothing to do } uint32_t rtl8139_handle_interrupt( interrupt_handler_t *handler, uint32_t esp ) { rtl8139_t *rtl8139 = (rtl8139_t *)handler->driver; #ifdef DEBUG printf( "RTL INTERRUPT\n" ); #endif return esp; } void rtl8139_print_info( void *obj ) { puts( "Realtek 8139 network card driver" ); }