2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2014-01-14 15:11:06 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2014. All rights reserved. ;;
|
2013-05-28 19:34:26 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; Realtek 8139 driver for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; based on RTL8139.asm driver for menuetos ;;
|
|
|
|
;; and realtek8139.asm for SolarOS by Eugen Brasoveanu ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
format PE DLL native
|
|
|
|
entry START
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
CURRENT_API = 0x0200
|
|
|
|
COMPATIBLE_API = 0x0100
|
|
|
|
API_VERSION = (COMPATIBLE_API shl 16) + CURRENT_API
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
MAX_DEVICES = 16
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
RBLEN = 3 ; Receive buffer size: 0==8K 1==16k 2==32k 3==64k
|
2013-08-06 14:46:35 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
TXRR = 8 ; total retries = 16+(TXRR*16)
|
|
|
|
TX_MXDMA = 6 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=2048
|
|
|
|
ERTXTH = 8 ; in unit of 32 bytes e.g:(8*32)=256
|
|
|
|
RX_MXDMA = 7 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=unlimited
|
|
|
|
RXFTH = 7 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=no threshold
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
__DEBUG__ = 1
|
2014-08-20 14:06:41 +02:00
|
|
|
__DEBUG_LEVEL__ = 2 ; 1 = verbose, 2 = errors only
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
section '.flat' readable writable executable
|
|
|
|
|
|
|
|
include '../proc32.inc'
|
2014-01-17 13:51:32 +01:00
|
|
|
include '../struct.inc'
|
|
|
|
include '../macros.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
include '../fdo.inc'
|
2014-08-31 16:09:14 +02:00
|
|
|
include '../netdrv.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
REG_IDR0 = 0x00
|
|
|
|
REG_MAR0 = 0x08 ; multicast filter register 0
|
|
|
|
REG_MAR4 = 0x0c ; multicast filter register 4
|
|
|
|
REG_TSD0 = 0x10 ; transmit status of descriptor
|
|
|
|
REG_TSAD0 = 0x20 ; transmit start address of descriptor
|
|
|
|
REG_RBSTART = 0x30 ; RxBuffer start address
|
|
|
|
REG_COMMAND = 0x37 ; command register
|
|
|
|
REG_CAPR = 0x38 ; current address of packet read (word) R/W
|
|
|
|
REG_IMR = 0x3c ; interrupt mask register
|
|
|
|
REG_ISR = 0x3e ; interrupt status register
|
|
|
|
REG_TXCONFIG = 0x40 ; transmit configuration register
|
|
|
|
REG_RXCONFIG = 0x44 ; receive configuration register 0
|
|
|
|
REG_MPC = 0x4c ; missed packet counter
|
|
|
|
REG_9346CR = 0x50 ; serial eeprom 93C46 command register
|
|
|
|
REG_CONFIG1 = 0x52 ; configuration register 1
|
|
|
|
REG_MSR = 0x58
|
|
|
|
REG_CONFIG4 = 0x5a ; configuration register 4
|
|
|
|
REG_HLTCLK = 0x5b ; undocumented halt clock register
|
|
|
|
REG_BMCR = 0x62 ; basic mode control register
|
|
|
|
REG_ANAR = 0x66 ; auto negotiation advertisement register
|
|
|
|
REG_9346CR_WE = 11b shl 6
|
|
|
|
|
|
|
|
BIT_RUNT = 4 ; total packet length < 64 bytes
|
|
|
|
BIT_LONG = 3 ; total packet length > 4k
|
|
|
|
BIT_CRC = 2 ; crc error occured
|
|
|
|
BIT_FAE = 1 ; frame alignment error occured
|
|
|
|
BIT_ROK = 0 ; received packet is ok
|
|
|
|
|
|
|
|
BIT_RST = 4 ; reset bit
|
|
|
|
BIT_RE = 3 ; receiver enabled
|
|
|
|
BIT_TE = 2 ; transmitter enabled
|
|
|
|
BUFE = 1 ; rx buffer is empty, no packet stored
|
|
|
|
|
|
|
|
BIT_ISR_TOK = 2 ; transmit ok
|
|
|
|
BIT_ISR_RER = 1 ; receive error interrupt
|
|
|
|
BIT_ISR_ROK = 0 ; receive ok
|
|
|
|
|
|
|
|
BIT_TX_MXDMA = 8 ; Max DMA burst size per Tx DMA burst
|
|
|
|
BIT_TXRR = 4 ; Tx Retry count 16+(TXRR*16)
|
|
|
|
|
|
|
|
BIT_RXFTH = 13 ; Rx fifo threshold
|
|
|
|
BIT_RBLEN = 11 ; Ring buffer length indicator
|
|
|
|
BIT_RX_MXDMA = 8 ; Max DMA burst size per Rx DMA burst
|
|
|
|
BIT_NOWRAP = 7 ; transfered data wrapping
|
|
|
|
BIT_9356SEL = 6 ; eeprom selector 9346/9356
|
|
|
|
BIT_AER = 5 ; accept error packets
|
|
|
|
BIT_AR = 4 ; accept runt packets
|
|
|
|
BIT_AB = 3 ; accept broadcast packets
|
|
|
|
BIT_AM = 2 ; accept multicast packets
|
|
|
|
BIT_APM = 1 ; accept physical match packets
|
|
|
|
BIT_AAP = 0 ; accept all packets
|
|
|
|
|
|
|
|
BIT_93C46_EEM1 = 7 ; RTL8139 eeprom operating mode1
|
|
|
|
BIT_93C46_EEM0 = 6 ; RTL8139 eeprom operating mode0
|
|
|
|
BIT_93C46_EECS = 3 ; chip select
|
|
|
|
BIT_93C46_EESK = 2 ; serial data clock
|
|
|
|
BIT_93C46_EEDI = 1 ; serial data input
|
|
|
|
BIT_93C46_EEDO = 0 ; serial data output
|
|
|
|
|
|
|
|
BIT_LWACT = 4 ; see REG_CONFIG1
|
|
|
|
BIT_SLEEP = 1 ; sleep bit at older chips
|
|
|
|
BIT_PWRDWN = 0 ; power down bit at older chips
|
|
|
|
BIT_PMEn = 0 ; power management enabled
|
|
|
|
|
|
|
|
BIT_LWPTN = 2 ; see REG_CONFIG4
|
|
|
|
|
|
|
|
BIT_ERTXTH = 16 ; early TX threshold
|
|
|
|
BIT_TOK = 15 ; transmit ok
|
|
|
|
BIT_OWN = 13 ; tx DMA operation is completed
|
|
|
|
|
|
|
|
BIT_ANE = 12 ; auto negotiation enable
|
|
|
|
|
|
|
|
BIT_TXFD = 8 ; 100base-T full duplex
|
|
|
|
BIT_TX = 7 ; 100base-T
|
|
|
|
BIT_10FD = 6 ; 10base-T full duplex
|
|
|
|
BIT_10 = 5 ; 10base-T
|
|
|
|
BIT_SELECTOR = 0 ; binary encoded selector CSMA/CD=00001
|
|
|
|
|
|
|
|
BIT_IFG1 = 25
|
|
|
|
BIT_IFG0 = 24
|
|
|
|
|
|
|
|
RX_CONFIG = (RBLEN shl BIT_RBLEN) or \
|
|
|
|
(RX_MXDMA shl BIT_RX_MXDMA) or \
|
|
|
|
(1 shl BIT_NOWRAP) or \
|
|
|
|
(RXFTH shl BIT_RXFTH) or\
|
2013-08-06 14:46:35 +02:00
|
|
|
(1 shl BIT_AB) or \ ; Accept broadcast packets
|
|
|
|
(1 shl BIT_APM) or \ ; Accept physical match packets
|
|
|
|
(1 shl BIT_AER) or \ ; Accept error packets
|
|
|
|
(1 shl BIT_AR) or \ ; Accept Runt packets (smaller then 64 bytes)
|
|
|
|
(1 shl BIT_AM) ; Accept multicast packets
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-08-06 14:46:35 +02:00
|
|
|
RX_BUFFER_SIZE = (8192 shl RBLEN);+16+1500
|
|
|
|
NUM_TX_DESC = 4 ; not user selectable
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
EE_93C46_REG_ETH_ID = 7 ; MAC offset
|
|
|
|
EE_93C46_READ_CMD = (6 shl 6) ; 110b + 6bit address
|
|
|
|
EE_93C56_READ_CMD = (6 shl 8) ; 110b + 8bit address
|
|
|
|
EE_93C46_CMD_LENGTH = 9 ; start bit + cmd + 6bit address
|
|
|
|
EE_93C56_CMD_LENGTH = 11 ; start bit + cmd + 8bit ddress
|
|
|
|
|
|
|
|
VER_RTL8139 = 1100000b
|
|
|
|
VER_RTL8139A = 1110000b
|
|
|
|
VER_RTL8139AG = 1110100b
|
|
|
|
VER_RTL8139B = 1111000b
|
|
|
|
VER_RTL8130 = VER_RTL8139B
|
|
|
|
VER_RTL8139C = 1110100b
|
|
|
|
VER_RTL8100 = 1111010b
|
|
|
|
VER_RTL8100B = 1110101b
|
|
|
|
VER_RTL8139D = VER_RTL8100B
|
|
|
|
VER_RTL8139CP = 1110110b
|
|
|
|
VER_RTL8101 = 1110111b
|
|
|
|
|
|
|
|
IDX_RTL8139 = 0
|
|
|
|
IDX_RTL8139A = 1
|
|
|
|
IDX_RTL8139B = 2
|
|
|
|
IDX_RTL8139C = 3
|
|
|
|
IDX_RTL8100 = 4
|
|
|
|
IDX_RTL8139D = 5
|
|
|
|
IDX_RTL8139D = 6
|
|
|
|
IDX_RTL8101 = 7
|
|
|
|
|
|
|
|
ISR_SERR = 1 shl 15
|
|
|
|
ISR_TIMEOUT = 1 shl 14
|
|
|
|
ISR_LENCHG = 1 shl 13
|
|
|
|
ISR_FIFOOVW = 1 shl 6
|
|
|
|
ISR_PUN = 1 shl 5
|
|
|
|
ISR_RXOVW = 1 shl 4
|
|
|
|
ISR_TER = 1 shl 3
|
|
|
|
ISR_TOK = 1 shl 2
|
|
|
|
ISR_RER = 1 shl 1
|
|
|
|
ISR_ROK = 1 shl 0
|
|
|
|
|
|
|
|
INTERRUPT_MASK = ISR_ROK or \
|
2013-08-06 14:46:35 +02:00
|
|
|
ISR_RER or \
|
|
|
|
ISR_TOK or \
|
|
|
|
ISR_TER or \
|
2013-05-28 19:34:26 +02:00
|
|
|
ISR_RXOVW or \
|
|
|
|
ISR_PUN or \
|
|
|
|
ISR_FIFOOVW or \
|
|
|
|
ISR_LENCHG or \
|
2013-08-06 14:46:35 +02:00
|
|
|
ISR_TIMEOUT or \
|
|
|
|
ISR_SERR
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
TSR_OWN = 1 shl 13
|
|
|
|
TSR_TUN = 1 shl 14
|
|
|
|
TSR_TOK = 1 shl 15
|
|
|
|
|
|
|
|
TSR_CDH = 1 shl 28
|
|
|
|
TSR_OWC = 1 shl 29
|
|
|
|
TSR_TABT = 1 shl 30
|
|
|
|
TSR_CRS = 1 shl 31
|
|
|
|
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
struct device ETH_DEVICE
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
io_addr dd ?
|
|
|
|
pci_bus dd ?
|
|
|
|
pci_dev dd ?
|
|
|
|
irq_line db ?
|
|
|
|
rb 3 ; align 4
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
rx_buffer dd ?
|
|
|
|
rx_data_offset dd ?
|
|
|
|
curr_tx_desc db ?
|
|
|
|
hw_ver_id db ?
|
|
|
|
rb 2 ; align 4
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
TX_DESC rd NUM_TX_DESC
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
ends
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; proc START ;;
|
|
|
|
;; ;;
|
|
|
|
;; (standard driver proc) ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
proc START c, reason:dword, cmdline:dword
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
cmp [reason], DRV_ENTRY
|
|
|
|
jne .fail
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
DEBUGF 2,"Loading driver\n"
|
|
|
|
invoke RegService, my_service, service_proc
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; proc SERVICE_PROC ;;
|
|
|
|
;; ;;
|
|
|
|
;; (standard driver proc) ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
proc service_proc stdcall, ioctl:dword
|
|
|
|
|
|
|
|
mov edx, [ioctl]
|
2014-01-17 16:07:07 +01:00
|
|
|
mov eax, [edx + IOCTL.io_code]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;------------------------------------------------------
|
|
|
|
|
|
|
|
cmp eax, 0 ;SRV_GETVERSION
|
|
|
|
jne @F
|
|
|
|
|
2014-01-17 16:07:07 +01:00
|
|
|
cmp [edx + IOCTL.out_size], 4
|
2013-05-28 19:34:26 +02:00
|
|
|
jb .fail
|
2014-01-17 16:07:07 +01:00
|
|
|
mov eax, [edx + IOCTL.output]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [eax], dword API_VERSION
|
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
;------------------------------------------------------
|
|
|
|
@@:
|
|
|
|
cmp eax, 1 ;SRV_HOOK
|
|
|
|
jne .fail
|
|
|
|
|
2014-01-17 16:07:07 +01:00
|
|
|
cmp [edx + IOCTL.inp_size], 3 ; Data input must be at least 3 bytes
|
2013-05-28 19:34:26 +02:00
|
|
|
jb .fail
|
|
|
|
|
2014-01-17 16:07:07 +01:00
|
|
|
mov eax, [edx + IOCTL.input]
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp byte [eax], 1 ; 1 means device number and bus number (pci) are given
|
|
|
|
jne .fail ; other types arent supported for this card yet
|
|
|
|
|
|
|
|
; check if the device is already listed
|
|
|
|
|
|
|
|
mov esi, device_list
|
|
|
|
mov ecx, [devices]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .firstdevice
|
|
|
|
|
|
|
|
mov ax, [eax+1] ; get the pci bus and device numbers
|
|
|
|
.nextdevice:
|
|
|
|
mov ebx, [esi]
|
2014-08-20 14:06:41 +02:00
|
|
|
cmp al, byte[ebx + device.pci_bus]
|
2013-05-28 19:34:26 +02:00
|
|
|
jne @f
|
2014-08-20 14:06:41 +02:00
|
|
|
cmp ah, byte[ebx + device.pci_dev]
|
2013-05-28 19:34:26 +02:00
|
|
|
je .find_devicenum ; Device is already loaded, let's find it's device number
|
|
|
|
@@:
|
|
|
|
add esi, 4
|
|
|
|
loop .nextdevice
|
|
|
|
|
|
|
|
|
|
|
|
; This device doesnt have its own eth_device structure yet, lets create one
|
|
|
|
.firstdevice:
|
|
|
|
cmp [devices], MAX_DEVICES ; First check if the driver can handle one more card
|
|
|
|
jae .fail
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
allocate_and_clear ebx, sizeof.device, .fail ; Allocate the buffer for device structure
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Fill in the direct call addresses into the struct
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.reset], reset
|
|
|
|
mov [ebx + device.transmit], transmit
|
|
|
|
mov [ebx + device.unload], unload
|
|
|
|
mov [ebx + device.name], my_service
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; save the pci bus and device numbers
|
|
|
|
|
2014-01-17 16:07:07 +01:00
|
|
|
mov eax, [edx + IOCTL.input]
|
2013-05-28 19:34:26 +02:00
|
|
|
movzx ecx, byte[eax+1]
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.pci_bus], ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
movzx ecx, byte[eax+2]
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.pci_dev], ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Now, it's time to find the base io addres of the PCI device
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
stdcall PCI_find_io, [ebx + device.pci_bus], [ebx + device.pci_dev]
|
|
|
|
mov [ebx + device.io_addr], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; We've found the io address, find IRQ now
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke PciRead8, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.interrupt_line
|
|
|
|
mov [ebx + device.irq_line], al
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "Hooking into device, dev:%x, bus:%x, irq:%x, I/O addr:%x\n",\
|
2014-08-20 14:06:41 +02:00
|
|
|
[ebx + device.pci_dev]:1,[ebx + device.pci_bus]:1,[ebx + device.irq_line]:1,[ebx + device.io_addr]:4
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Allocate the receive buffer
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke CreateRingBuffer, dword (RX_BUFFER_SIZE), dword PG_SW
|
2013-05-28 19:34:26 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .err
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.rx_buffer], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Ok, the eth_device structure is ready, let's probe the device
|
|
|
|
|
|
|
|
call probe ; this function will output in eax
|
|
|
|
test eax, eax
|
|
|
|
jnz .err ; If an error occured, exit
|
|
|
|
|
|
|
|
mov eax, [devices] ; Add the device structure to our device list
|
|
|
|
mov [device_list+4*eax], ebx ; (IRQ handler uses this list to find device)
|
|
|
|
inc [devices] ;
|
|
|
|
|
2013-06-19 11:27:02 +02:00
|
|
|
call reset
|
|
|
|
test eax, eax
|
|
|
|
jnz .destroy
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.type], NET_TYPE_ETH
|
|
|
|
invoke NetRegDev
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
cmp eax, -1
|
|
|
|
je .destroy
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
; If the device was already loaded, find the device number and return it in eax
|
|
|
|
|
|
|
|
.find_devicenum:
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "Trying to find device number of already registered device\n"
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke NetPtrToNum ; This kernel procedure converts a pointer to device struct in ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
; into a device number in edi
|
|
|
|
mov eax, edi ; Application wants it in eax instead
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "Kernel says: %u\n", eax
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
; If an error occured, remove all allocated data and exit (returning -1 in eax)
|
|
|
|
|
|
|
|
.destroy:
|
2013-06-19 11:27:02 +02:00
|
|
|
; todo: unregister device from device_list
|
2013-05-28 19:34:26 +02:00
|
|
|
; todo: reset device into virgin state
|
|
|
|
|
|
|
|
.err:
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 2, "Error, removing all data !\n"
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke KernelFree, [ebx + device.rx_buffer]
|
|
|
|
invoke KernelFree, ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.fail:
|
|
|
|
or eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
;------------------------------------------------------
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
|
|
|
|
;; ;;
|
|
|
|
;; Actual Hardware dependent code starts here ;;
|
|
|
|
;; ;;
|
|
|
|
;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
|
|
|
|
|
|
|
|
align 4
|
|
|
|
unload:
|
|
|
|
; TODO: (in this particular order)
|
|
|
|
;
|
|
|
|
; - Stop the device
|
|
|
|
; - Detach int handler
|
|
|
|
; - Remove device from local list (RTL8139_LIST)
|
|
|
|
; - call unregister function in kernel
|
|
|
|
; - Remove all allocated structures and buffers the card used
|
|
|
|
|
|
|
|
or eax, -1
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;
|
|
|
|
;; probe: enables the device (if it really is RTL8139)
|
|
|
|
;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
align 4
|
|
|
|
probe:
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "Probing\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
; Make the device a bus master
|
|
|
|
invoke PciRead32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command
|
|
|
|
or al, PCI_CMD_MASTER
|
|
|
|
invoke PciWrite32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; get chip version
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_TXCONFIG + 2
|
2013-05-28 19:34:26 +02:00
|
|
|
in ax, dx
|
|
|
|
shr ah, 2
|
|
|
|
shr ax, 6
|
|
|
|
and al, 01111111b
|
|
|
|
|
|
|
|
; now find it in our array
|
|
|
|
mov ecx, HW_VER_ARRAY_SIZE-1
|
|
|
|
.chip_ver_loop:
|
|
|
|
cmp al, [hw_ver_array + ecx]
|
|
|
|
je .chip_ver_found
|
|
|
|
dec ecx
|
|
|
|
jns .chip_ver_loop
|
|
|
|
.unknown:
|
|
|
|
mov ecx, 8
|
|
|
|
.chip_ver_found:
|
|
|
|
cmp ecx, 8
|
|
|
|
ja .unknown
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.hw_ver_id], cl
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov ecx, [crosslist+ecx*4]
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.name], ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "Chip version: %s\n", ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; wake up the chip
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_HLTCLK
|
2013-05-28 19:34:26 +02:00
|
|
|
mov al, 'R' ; run the clock
|
|
|
|
out dx, al
|
|
|
|
|
|
|
|
; unlock config and BMCR registers
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_9346CR
|
2013-05-28 19:34:26 +02:00
|
|
|
mov al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
|
|
|
|
out dx, al
|
|
|
|
|
|
|
|
; enable power management
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_CONFIG1
|
2013-05-28 19:34:26 +02:00
|
|
|
in al, dx
|
2014-08-20 14:06:41 +02:00
|
|
|
cmp [ebx + device.hw_ver_id], IDX_RTL8139B
|
2013-05-28 19:34:26 +02:00
|
|
|
jae .new_chip
|
|
|
|
; wake up older chips
|
|
|
|
and al, not ((1 shl BIT_SLEEP) or (1 shl BIT_PWRDWN))
|
|
|
|
out dx, al
|
|
|
|
jmp .finish_wake_up
|
|
|
|
|
|
|
|
; set LWAKE pin to active high (default value).
|
|
|
|
; it is for Wake-On-LAN functionality of some motherboards.
|
|
|
|
; this signal is used to inform the motherboard to execute a wake-up process.
|
|
|
|
; only at newer chips.
|
|
|
|
.new_chip:
|
|
|
|
or al, (1 shl BIT_PMEn)
|
|
|
|
and al, not (1 shl BIT_LWACT)
|
|
|
|
out dx, al
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_CONFIG4
|
2013-05-28 19:34:26 +02:00
|
|
|
in al, dx
|
|
|
|
and al, not (1 shl BIT_LWPTN)
|
|
|
|
out dx, al
|
|
|
|
|
|
|
|
; lock config and BMCR registers
|
|
|
|
.finish_wake_up:
|
|
|
|
xor al, al
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_9346CR
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, al
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "probing done!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-19 11:27:02 +02:00
|
|
|
xor eax, eax
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;
|
|
|
|
;; reset: Set up all registers and descriptors, clear some values
|
|
|
|
;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
reset:
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "Reset\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; attach int handler
|
2014-08-20 14:06:41 +02:00
|
|
|
movzx eax, [ebx + device.irq_line]
|
2013-05-28 19:34:26 +02:00
|
|
|
DEBUGF 1, "Attaching int handler to irq %x\n", eax:1
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke AttachIntHandler, eax, int_handler, ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
test eax, eax
|
|
|
|
jnz @f
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 2, "Could not attach int handler!\n"
|
2014-08-20 14:06:41 +02:00
|
|
|
or eax, -1
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
@@:
|
|
|
|
|
|
|
|
; reset chip
|
|
|
|
DEBUGF 1, "Resetting chip\n"
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_COMMAND
|
2013-05-28 19:34:26 +02:00
|
|
|
mov al, 1 shl BIT_RST
|
|
|
|
out dx, al
|
|
|
|
mov cx, 1000 ; wait no longer for the reset
|
|
|
|
.wait_for_reset:
|
|
|
|
in al, dx
|
|
|
|
test al, 1 shl BIT_RST
|
|
|
|
jz .reset_completed ; RST remains 1 during reset
|
|
|
|
dec cx
|
|
|
|
jns .wait_for_reset
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 2, "Reset timeout!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
.reset_completed:
|
|
|
|
|
2013-08-06 16:27:21 +02:00
|
|
|
; Read MAC address
|
|
|
|
call read_mac
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; unlock config and BMCR registers
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_9346CR
|
2013-05-28 19:34:26 +02:00
|
|
|
mov al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
|
|
|
|
out dx, al
|
|
|
|
|
|
|
|
; initialize multicast registers (no filtering)
|
|
|
|
mov eax, 0xffffffff
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_MAR0
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, eax
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_MAR4
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, eax
|
|
|
|
|
|
|
|
; enable Rx/Tx
|
|
|
|
mov al, (1 shl BIT_RE) or (1 shl BIT_TE)
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_COMMAND
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, al
|
|
|
|
|
|
|
|
; Rxbuffer size, unlimited dma burst, no wrapping, no rx threshold
|
|
|
|
; accept broadcast packets, accept physical match packets
|
2013-08-06 16:27:21 +02:00
|
|
|
mov eax, RX_CONFIG
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_RXCONFIG
|
2013-08-06 16:27:21 +02:00
|
|
|
out dx, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
|
|
|
|
mov eax, (TX_MXDMA shl BIT_TX_MXDMA) or (TXRR shl BIT_TXRR) or BIT_IFG1 or BIT_IFG0
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_TXCONFIG
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, eax
|
|
|
|
|
|
|
|
; enable auto negotiation
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_BMCR
|
2013-05-28 19:34:26 +02:00
|
|
|
in ax, dx
|
|
|
|
or ax, (1 shl BIT_ANE)
|
|
|
|
out dx, ax
|
|
|
|
|
|
|
|
; set auto negotiation advertisement
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_ANAR
|
2013-05-28 19:34:26 +02:00
|
|
|
in ax, dx
|
|
|
|
or ax, (1 shl BIT_SELECTOR) or (1 shl BIT_10) or (1 shl BIT_10FD) or (1 shl BIT_TX) or (1 shl BIT_TXFD)
|
|
|
|
out dx, ax
|
|
|
|
|
|
|
|
; lock config and BMCR registers
|
|
|
|
xor eax, eax
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_9346CR
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, al
|
|
|
|
|
|
|
|
; init RX/TX pointers
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.rx_data_offset], eax
|
|
|
|
mov [ebx + device.curr_tx_desc], al
|
|
|
|
; set_io [ebx + device.io_addr], REG_CAPR
|
2013-05-28 19:34:26 +02:00
|
|
|
; out dx, ax
|
|
|
|
|
|
|
|
; clear packet/byte counters
|
2014-08-20 14:06:41 +02:00
|
|
|
lea edi, [ebx + device.bytes_tx]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ecx, 6
|
|
|
|
rep stosd
|
|
|
|
|
|
|
|
; clear missing packet counter
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_MPC
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, eax
|
|
|
|
|
|
|
|
; set RxBuffer address, init RX buffer offset
|
2014-08-20 14:06:41 +02:00
|
|
|
mov eax, [ebx + device.rx_buffer]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov dword[eax], 0 ; clear receive flags for first packet (really needed??)
|
2013-06-19 11:27:02 +02:00
|
|
|
DEBUGF 1, "RX buffer virtual addr=0x%x\n", eax
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke GetPhysAddr
|
2013-08-06 14:46:35 +02:00
|
|
|
DEBUGF 1, "RX buffer physical addr=0x%x\n", eax
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_RBSTART
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, eax
|
|
|
|
|
|
|
|
; enable interrupts
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_IMR
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ax, INTERRUPT_MASK
|
|
|
|
out dx, ax
|
|
|
|
|
|
|
|
; Set the mtu, kernel will be able to send now
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.mtu], 1514
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
call cable
|
|
|
|
|
|
|
|
; Indicate that we have successfully reset the card
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Transmit ;;
|
|
|
|
;; ;;
|
|
|
|
;; In: buffer pointer in [esp+4] ;;
|
|
|
|
;; size of buffer in [esp+8] ;;
|
|
|
|
;; pointer to device structure in ebx ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2014-08-20 14:06:41 +02:00
|
|
|
|
|
|
|
proc transmit stdcall bufferptr, buffersize
|
|
|
|
|
|
|
|
pushf
|
|
|
|
cli
|
|
|
|
|
|
|
|
DEBUGF 1,"Transmitting packet, buffer:%x, size:%u\n", [bufferptr], [buffersize]
|
|
|
|
mov eax, [bufferptr]
|
|
|
|
DEBUGF 1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
|
2013-05-28 19:34:26 +02:00
|
|
|
[eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
|
|
|
|
[eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
|
|
|
|
[eax+13]:2,[eax+12]:2
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
cmp [buffersize], 1514
|
2013-05-28 19:34:26 +02:00
|
|
|
ja .fail
|
2014-08-20 14:06:41 +02:00
|
|
|
cmp [buffersize], 60
|
2013-05-28 19:34:26 +02:00
|
|
|
jb .fail
|
|
|
|
|
|
|
|
; check if we own the current discriptor
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_TSD0
|
|
|
|
movzx ecx, [ebx + device.curr_tx_desc]
|
2013-05-28 19:34:26 +02:00
|
|
|
shl ecx, 2
|
|
|
|
add edx, ecx
|
|
|
|
in eax, dx
|
|
|
|
test eax, (1 shl BIT_OWN)
|
|
|
|
jz .wait_to_send
|
|
|
|
|
|
|
|
.send_packet:
|
|
|
|
; get next descriptor
|
2014-08-20 14:06:41 +02:00
|
|
|
inc [ebx + device.curr_tx_desc]
|
|
|
|
and [ebx + device.curr_tx_desc], NUM_TX_DESC-1
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Update stats
|
2014-08-20 14:06:41 +02:00
|
|
|
inc [ebx + device.packets_tx]
|
|
|
|
mov eax, [buffersize]
|
|
|
|
add dword [ebx + device.bytes_tx], eax
|
|
|
|
adc dword [ebx + device.bytes_tx+4], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Set the buffer address
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_TSAD0
|
|
|
|
mov eax, [bufferptr]
|
|
|
|
mov [ebx + device.TX_DESC+ecx], eax
|
|
|
|
invoke GetPhysAddr
|
2013-05-28 19:34:26 +02:00
|
|
|
out dx, eax
|
|
|
|
|
|
|
|
; And the size of the buffer
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_TSD0
|
|
|
|
mov eax, [buffersize]
|
2013-05-28 19:34:26 +02:00
|
|
|
or eax, (ERTXTH shl BIT_ERTXTH) ; Early threshold
|
|
|
|
out dx, eax
|
|
|
|
|
|
|
|
DEBUGF 1, "Packet Sent!\n"
|
2014-08-20 14:06:41 +02:00
|
|
|
popf
|
2014-09-15 13:28:35 +02:00
|
|
|
xor eax, eax
|
2014-08-20 14:06:41 +02:00
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.wait_to_send:
|
|
|
|
DEBUGF 1, "Waiting for timeout\n"
|
|
|
|
|
|
|
|
push edx
|
|
|
|
mov esi, 30
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke Sleep
|
2013-05-28 19:34:26 +02:00
|
|
|
pop edx
|
|
|
|
|
|
|
|
in ax, dx
|
|
|
|
test ax, (1 shl BIT_OWN)
|
|
|
|
jnz .send_packet
|
|
|
|
|
|
|
|
pusha
|
|
|
|
call reset ; if chip hung, reset it
|
|
|
|
popa
|
|
|
|
|
|
|
|
jmp .send_packet
|
|
|
|
|
|
|
|
.fail:
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 2, "transmit failed!\n"
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke KernelFree, [bufferptr]
|
|
|
|
popf
|
2013-05-28 19:34:26 +02:00
|
|
|
or eax, -1
|
2014-08-20 14:06:41 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Interrupt handler ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
align 4
|
|
|
|
int_handler:
|
|
|
|
|
|
|
|
push ebx esi edi
|
|
|
|
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "INT\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; find pointer of device wich made IRQ occur
|
|
|
|
mov ecx, [devices]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .nothing
|
|
|
|
mov esi, device_list
|
|
|
|
.nextdevice:
|
|
|
|
mov ebx, [esi]
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_ISR
|
2013-05-28 19:34:26 +02:00
|
|
|
in ax, dx ; Get interrupt status
|
|
|
|
out dx, ax ; send it back to ACK
|
|
|
|
test ax, ax
|
|
|
|
jnz .got_it
|
|
|
|
.continue:
|
|
|
|
add esi, 4
|
|
|
|
dec ecx
|
|
|
|
jnz .nextdevice
|
|
|
|
.nothing:
|
|
|
|
pop edi esi ebx
|
|
|
|
xor eax, eax
|
|
|
|
|
|
|
|
ret ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
|
|
|
|
|
|
|
|
.got_it:
|
|
|
|
|
|
|
|
DEBUGF 1, "Device: %x Status: %x\n", ebx, ax
|
|
|
|
|
|
|
|
;----------------------------------------------------
|
|
|
|
; Received packet ok?
|
|
|
|
|
|
|
|
test ax, ISR_ROK
|
|
|
|
jz @f
|
|
|
|
push ax
|
|
|
|
|
|
|
|
.receive:
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_COMMAND
|
2013-05-28 19:34:26 +02:00
|
|
|
in al, dx
|
|
|
|
test al, BUFE ; test if RX buffer is empty
|
|
|
|
jnz .finish
|
|
|
|
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "RX:\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
mov eax, [ebx + device.rx_buffer]
|
|
|
|
add eax, [ebx + device.rx_data_offset]
|
2013-05-28 19:34:26 +02:00
|
|
|
test byte [eax], (1 shl BIT_ROK) ; check if packet is ok
|
|
|
|
jz .reset_rx
|
|
|
|
|
|
|
|
; packet is ok, copy it
|
|
|
|
movzx ecx, word [eax+2] ; packet length
|
|
|
|
sub cx, 4 ; don't copy CRC
|
|
|
|
|
|
|
|
; Update stats
|
2014-08-20 14:06:41 +02:00
|
|
|
add dword [ebx + device.bytes_rx], ecx
|
|
|
|
adc dword [ebx + device.bytes_rx + 4], 0
|
|
|
|
inc [ebx + device.packets_rx]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
DEBUGF 1, "Received %u bytes\n", ecx
|
|
|
|
|
|
|
|
push ebx eax ecx
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke KernelAlloc, ecx ; Allocate a buffer to put packet into
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ecx
|
|
|
|
test eax, eax ; Test if we allocated succesfully
|
|
|
|
jz .abort
|
|
|
|
|
|
|
|
mov edi, eax ; Where we will copy too
|
|
|
|
|
|
|
|
mov esi, [esp] ; The buffer we will copy from
|
|
|
|
add esi, 4 ; Dont copy CRC
|
|
|
|
|
|
|
|
push dword .abort ; Kernel will return to this address after EthReceiver
|
|
|
|
push ecx edi ; Save buffer pointer and size, to pass to kernel
|
|
|
|
|
|
|
|
.copy:
|
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
|
|
|
.nb:
|
|
|
|
shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
|
|
|
.nw:
|
|
|
|
jz .nd
|
|
|
|
rep movsd
|
|
|
|
.nd:
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
jmp [Eth_input] ; Send it to kernel
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.abort:
|
|
|
|
pop eax ebx
|
|
|
|
; update eth_data_start_offset
|
|
|
|
movzx eax, word [eax+2] ; packet length
|
2014-08-20 14:06:41 +02:00
|
|
|
add eax, [ebx + device.rx_data_offset]
|
2013-05-28 19:34:26 +02:00
|
|
|
add eax, 4+3 ; packet header is 4 bytes long + dword alignment
|
|
|
|
and eax, not 3 ; dword alignment
|
|
|
|
|
|
|
|
cmp eax, RX_BUFFER_SIZE
|
|
|
|
jb .no_wrap
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "Wrapping\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
sub eax, RX_BUFFER_SIZE
|
|
|
|
.no_wrap:
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.rx_data_offset], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
DEBUGF 1, "New RX ptr: %d\n", eax
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_CAPR ; update 'Current Address of Packet Read register'
|
2013-05-28 19:34:26 +02:00
|
|
|
sub eax, 0x10 ; value 0x10 is a constant for CAPR
|
2013-08-06 14:46:35 +02:00
|
|
|
out dx, ax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
jmp .receive ; check for multiple packets
|
|
|
|
|
|
|
|
.reset_rx:
|
|
|
|
test byte [eax], (1 shl BIT_CRC)
|
|
|
|
jz .no_crc_error
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 2, "RX: CRC error!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.no_crc_error:
|
|
|
|
test byte [eax], (1 shl BIT_FAE)
|
|
|
|
jz .no_fae_error
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 2, "RX: Frame alignment error!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.no_fae_error:
|
|
|
|
DEBUGF 1, "Reset RX\n"
|
|
|
|
in al, dx ; read command register
|
|
|
|
push ax
|
|
|
|
and al, not (1 shl BIT_RE) ; Clear the RE bit
|
|
|
|
out dx, al
|
|
|
|
pop ax
|
|
|
|
out dx, al ; write original command back
|
|
|
|
|
|
|
|
add edx, REG_RXCONFIG - REG_COMMAND ; Restore RX configuration
|
|
|
|
mov ax, RX_CONFIG
|
|
|
|
out dx, ax
|
|
|
|
|
|
|
|
.finish:
|
|
|
|
pop ax
|
|
|
|
|
|
|
|
;----------------------------------------------------
|
|
|
|
; Transmit ok / Transmit error
|
|
|
|
@@:
|
|
|
|
test ax, ISR_TOK + ISR_TER
|
|
|
|
jz @f
|
|
|
|
|
2013-08-06 14:46:35 +02:00
|
|
|
DEBUGF 1, "Transmit done!\n"
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
push ax
|
|
|
|
mov ecx, (NUM_TX_DESC-1)*4
|
|
|
|
.txdescloop:
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_TSD0
|
2013-05-28 19:34:26 +02:00
|
|
|
add edx, ecx
|
|
|
|
in eax, dx
|
|
|
|
|
|
|
|
test eax, TSR_OWN ; DMA operation completed
|
|
|
|
jz .notthisone
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
cmp [ebx + device.TX_DESC+ecx], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
je .notthisone
|
|
|
|
|
2013-08-06 14:46:35 +02:00
|
|
|
DEBUGF 1, "TSD: 0x%x\n", eax
|
|
|
|
|
|
|
|
test eax, TSR_TUN
|
|
|
|
jz .no_bun
|
|
|
|
DEBUGF 2, "TX: FIFO Buffer underrun!\n"
|
|
|
|
|
|
|
|
.no_bun:
|
|
|
|
test eax, TSR_OWC
|
|
|
|
jz .no_owc
|
|
|
|
DEBUGF 2, "TX: OWC!\n"
|
|
|
|
|
|
|
|
.no_owc:
|
|
|
|
test eax, TSR_TABT
|
|
|
|
jz .no_tabt
|
|
|
|
DEBUGF 2, "TX: TABT!\n"
|
|
|
|
|
|
|
|
.no_tabt:
|
|
|
|
test eax, TSR_CRS
|
|
|
|
jz .no_csl
|
|
|
|
DEBUGF 2, "TX: Carrier Sense Lost!\n"
|
|
|
|
|
|
|
|
.no_csl:
|
|
|
|
test eax, TSR_TOK
|
|
|
|
jz .no_tok
|
|
|
|
DEBUGF 1, "TX: Transmit OK!\n"
|
|
|
|
|
|
|
|
.no_tok:
|
2014-08-20 14:06:41 +02:00
|
|
|
DEBUGF 1, "free transmit buffer 0x%x\n", [ebx + device.TX_DESC+ecx]:8
|
2013-05-28 19:34:26 +02:00
|
|
|
push ecx ebx
|
2014-08-20 14:06:41 +02:00
|
|
|
invoke KernelFree, [ebx + device.TX_DESC+ecx]
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ebx ecx
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.TX_DESC+ecx], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.notthisone:
|
|
|
|
sub ecx, 4
|
2013-08-06 14:46:35 +02:00
|
|
|
jae .txdescloop
|
2013-05-28 19:34:26 +02:00
|
|
|
pop ax
|
|
|
|
|
|
|
|
;----------------------------------------------------
|
|
|
|
; Rx buffer overflow ?
|
|
|
|
@@:
|
|
|
|
test ax, ISR_RXOVW
|
|
|
|
jz @f
|
|
|
|
|
|
|
|
push ax
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 2, "RX:buffer overflow!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_ISR
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ax, ISR_FIFOOVW or ISR_RXOVW
|
|
|
|
out dx, ax
|
|
|
|
pop ax
|
|
|
|
|
|
|
|
;----------------------------------------------------
|
|
|
|
; Packet underrun?
|
|
|
|
@@:
|
|
|
|
test ax, ISR_PUN
|
|
|
|
jz @f
|
|
|
|
|
2014-01-14 15:11:06 +01:00
|
|
|
DEBUGF 1, "Packet underrun or link changed!\n"
|
|
|
|
|
|
|
|
call cable
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;----------------------------------------------------
|
|
|
|
; Receive FIFO overflow ?
|
|
|
|
@@:
|
|
|
|
test ax, ISR_FIFOOVW
|
|
|
|
jz @f
|
|
|
|
|
|
|
|
push ax
|
2014-01-14 15:11:06 +01:00
|
|
|
DEBUGF 2, "RX fifo overflow!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_ISR
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ax, ISR_FIFOOVW or ISR_RXOVW
|
|
|
|
out dx, ax
|
|
|
|
pop ax
|
|
|
|
|
|
|
|
;----------------------------------------------------
|
2014-01-14 15:11:06 +01:00
|
|
|
; cable length changed ?
|
2013-05-28 19:34:26 +02:00
|
|
|
@@:
|
|
|
|
test ax, ISR_LENCHG
|
|
|
|
jz .fail
|
|
|
|
|
2014-01-14 15:11:06 +01:00
|
|
|
DEBUGF 2, "Cable length changed!\n"
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
call cable
|
|
|
|
|
|
|
|
.fail:
|
|
|
|
pop edi esi ebx
|
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Update Cable status ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
align 4
|
|
|
|
cable:
|
2013-08-06 14:46:35 +02:00
|
|
|
DEBUGF 1, "Checking link status:\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_MSR
|
2013-05-28 19:34:26 +02:00
|
|
|
in al, dx
|
|
|
|
|
|
|
|
test al, 1 shl 2 ; 0 = link ok 1 = link fail
|
|
|
|
jnz .notconnected
|
|
|
|
|
|
|
|
test al, 1 shl 3 ; 0 = 100 Mbps 1 = 10 Mbps
|
|
|
|
jnz .10mbps
|
|
|
|
|
|
|
|
.100mbps:
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.state], ETH_LINK_100M
|
|
|
|
invoke NetLinkChanged
|
2014-01-14 15:11:06 +01:00
|
|
|
DEBUGF 2, "link changed to 100 mbit\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.10mbps:
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.state], ETH_LINK_10M
|
|
|
|
invoke NetLinkChanged
|
2014-01-14 15:11:06 +01:00
|
|
|
DEBUGF 2, "link changed to 10 mbit\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.notconnected:
|
2014-08-20 14:06:41 +02:00
|
|
|
mov [ebx + device.state], ETH_LINK_DOWN
|
|
|
|
invoke NetLinkChanged
|
2014-01-14 15:11:06 +01:00
|
|
|
DEBUGF 2, "no link\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Write MAC address ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
align 4
|
|
|
|
write_mac: ; in: mac pushed onto stack (as 3 words)
|
|
|
|
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "Writing MAC\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; disable all in command registers
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
set_io [ebx + device.io_addr], REG_9346CR
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
out dx, al
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_IMR
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
out dx, ax
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_ISR
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, -1
|
|
|
|
out dx, ax
|
|
|
|
|
|
|
|
; enable writing
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_9346CR
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, REG_9346CR_WE
|
|
|
|
out dx, al
|
|
|
|
|
|
|
|
; write the mac ...
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_IDR0
|
2013-05-28 19:34:26 +02:00
|
|
|
pop eax
|
|
|
|
out dx, eax
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_IDR0+4
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
pop ax
|
|
|
|
out dx, eax
|
|
|
|
|
|
|
|
; disable writing
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], REG_9346CR
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
out dx, al
|
|
|
|
|
2013-08-05 13:16:08 +02:00
|
|
|
DEBUGF 1, "MAC write ok!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Notice this procedure does not ret, but continues to read_mac instead.
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Read MAC address ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
read_mac:
|
2013-08-06 14:46:35 +02:00
|
|
|
DEBUGF 1, "Reading MAC:\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
set_io [ebx + device.io_addr], 0
|
|
|
|
lea edi, [ebx + device.mac]
|
2013-05-28 19:34:26 +02:00
|
|
|
in eax, dx
|
|
|
|
stosd
|
|
|
|
add edx, 4
|
|
|
|
in ax, dx
|
|
|
|
stosw
|
|
|
|
|
2013-08-06 14:46:35 +02:00
|
|
|
DEBUGF 1, "%x-%x-%x-%x-%x-%x\n",[edi-6]:2,[edi-5]:2,[edi-4]:2,[edi-3]:2,[edi-2]:2,[edi-1]:2
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
; End of code
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
data fixups
|
|
|
|
end data
|
|
|
|
|
|
|
|
include '../peimport.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
my_service db 'RTL8139',0 ; max 16 chars include zero
|
|
|
|
|
|
|
|
device_1 db 'Realtek 8139',0
|
|
|
|
device_2 db 'Realtek 8139A',0
|
|
|
|
device_3 db 'Realtek 8139B',0
|
|
|
|
device_4 db 'Realtek 8139C',0
|
|
|
|
device_5 db 'Realtek 8100',0
|
|
|
|
device_6 db 'Realtek 8139D',0
|
|
|
|
device_7 db 'Realtek 8139CP',0
|
|
|
|
device_8 db 'Realtek 8101',0
|
|
|
|
device_unknown db 'Unknown RTL8139 clone', 0
|
|
|
|
|
|
|
|
crosslist:
|
|
|
|
dd device_1
|
|
|
|
dd device_2
|
|
|
|
dd device_3
|
|
|
|
dd device_4
|
|
|
|
dd device_5
|
|
|
|
dd device_6
|
|
|
|
dd device_7
|
|
|
|
dd device_8
|
|
|
|
dd device_unknown
|
|
|
|
|
|
|
|
hw_ver_array: ; This array is used by the probe routine to find out wich version of the RTL8139 we are working with
|
|
|
|
db VER_RTL8139
|
|
|
|
db VER_RTL8139A
|
|
|
|
db VER_RTL8139B
|
|
|
|
db VER_RTL8139C
|
|
|
|
db VER_RTL8100
|
|
|
|
db VER_RTL8139D
|
|
|
|
db VER_RTL8139CP
|
|
|
|
db VER_RTL8101
|
|
|
|
db 0
|
|
|
|
|
|
|
|
HW_VER_ARRAY_SIZE = $-hw_ver_array
|
|
|
|
|
|
|
|
include_debug_strings ; All data wich FDO uses will be included here
|
|
|
|
|
2014-08-20 14:06:41 +02:00
|
|
|
align 4
|
|
|
|
devices dd 0
|
2013-05-28 19:34:26 +02:00
|
|
|
device_list rd MAX_DEVICES ; This list contains all pointers to device structures the driver is handling
|
|
|
|
|