Subversion Repositories Kolibri OS

Rev

Rev 1159 | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;; Realtek 8139 driver for KolibriOS                               ;;
  7. ;;                                                                 ;;
  8. ;;    Written by hidnplayr@kolibrios.org                           ;;
  9. ;;                                                                 ;;
  10. ;;    v0.1 - march 2009                                            ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. format MS COFF
  18.  
  19.         API_VERSION             equ 0x01000100
  20.  
  21.         DEBUG                   equ 1
  22.         __DEBUG__               equ 1
  23.         __DEBUG_LEVEL__         equ 1
  24.  
  25. include 'proc32.inc'
  26. include 'imports.inc'
  27. include 'fdo.inc'
  28.  
  29. OS_BASE         equ 0;
  30. new_app_base    equ 0x60400000
  31. PROC_BASE       equ OS_BASE+0x0080000
  32.  
  33. public START
  34. public service_proc
  35. public version
  36.  
  37. struc IOCTL {
  38.       .handle           dd ?
  39.       .io_code          dd ?
  40.       .input            dd ?
  41.       .inp_size         dd ?
  42.       .output           dd ?
  43.       .out_size         dd ?
  44. }
  45.  
  46. virtual at 0
  47.   IOCTL IOCTL
  48. end virtual
  49.  
  50. struc ETH_DEVICE {
  51. ; pointers to procedures
  52.       .unload           dd ?
  53.       .reset            dd ?
  54.       .transmit         dd ?
  55.       .set_MAC          dd ?
  56.       .get_MAC          dd ?
  57.       .set_mode         dd ?
  58.       .get_mode         dd ?
  59. ; status & variables
  60.       .bytes_tx         dq ?
  61.       .bytes_rx         dq ?
  62.       .packets_tx       dd ?
  63.       .packets_rx       dd ?
  64.       .mode             dd ?  ; This dword contains cable status (10mbit/100mbit, full/half duplex, auto negotiation or not,..)
  65.       .name             dd ?
  66.       .mac              dp ?
  67. ; device specific
  68.       .rx_buffer        dd ?
  69.       .tx_buffer        dd ?
  70.       .rx_data_offset   dd ?
  71.       .io_addr          dd ?
  72.       .curr_tx_desc     db ?
  73.       .pci_bus          db ?
  74.       .pci_dev          db ?
  75.       .irq_line         db ?
  76.       .hw_ver_id        db ?
  77.       .size:
  78.  
  79. }
  80.  
  81. virtual at 0
  82.   device ETH_DEVICE
  83. end virtual
  84.  
  85. ; PCI Bus defines
  86.  
  87.         PCI_HEADER_TYPE                 equ     0x0e  ;8 bit
  88.         PCI_BASE_ADDRESS_0              equ     0x10  ;32 bit
  89.         PCI_BASE_ADDRESS_5              equ     0x24  ;32 bits
  90.         PCI_BASE_ADDRESS_SPACE_IO       equ     0x01
  91.         PCI_VENDOR_ID                   equ     0x00  ;16 bit
  92.         PCI_BASE_ADDRESS_IO_MASK        equ     0xFFFFFFFC
  93.  
  94. ; RTL8139 specific defines
  95.  
  96.         MAX_RTL8139             equ 16   ; Max number of devices this driver may handle
  97.         TX_TIMEOUT              equ 30   ; 300 milliseconds timeout
  98.  
  99.         PCI_REG_CMD             equ 0x04 ; command register
  100.         PCI_BIT_PIO             equ 0    ; bit0: io space control
  101.         PCI_BIT_MMIO            equ 1    ; bit1: memory space control
  102.         PCI_BIT_MASTER          equ 2    ; bit2: device acts as a PCI master
  103.  
  104.         REG_IDR0                equ 0x00
  105.         REG_MAR0                equ 0x08 ; multicast filter register 0
  106.         REG_MAR4                equ 0x0c ; multicast filter register 4
  107.         REG_TSD0                equ 0x10 ; transmit status of descriptor
  108.         REG_TSAD0               equ 0x20 ; transmit start address of descriptor
  109.         REG_RBSTART             equ 0x30 ; RxBuffer start address
  110.         REG_COMMAND             equ 0x37 ; command register
  111.         REG_CAPR                equ 0x38 ; current address of packet read (word) R/W
  112.         REG_IMR                 equ 0x3c ; interrupt mask register
  113.         REG_ISR                 equ 0x3e ; interrupt status register
  114.         REG_TXCONFIG            equ 0x40 ; transmit configuration register
  115.         REG_RXCONFIG            equ 0x44 ; receive configuration register 0
  116.         REG_MPC                 equ 0x4c ; missed packet counter
  117.         REG_9346CR              equ 0x50 ; serial eeprom 93C46 command register
  118.         REG_CONFIG1             equ 0x52 ; configuration register 1
  119.         REG_MSR                 equ 0x58
  120.         REG_CONFIG4             equ 0x5a ; configuration register 4
  121.         REG_HLTCLK              equ 0x5b ; undocumented halt clock register
  122.         REG_BMCR                equ 0x62 ; basic mode control register
  123.         REG_ANAR                equ 0x66 ; auto negotiation advertisement register
  124.         REG_9346CR_WE           equ 11b SHL 6
  125.  
  126.         BIT_RUNT                equ 4 ; total packet length < 64 bytes
  127.         BIT_LONG                equ 3 ; total packet length > 4k
  128.         BIT_CRC                 equ 2 ; crc error occured
  129.         BIT_FAE                 equ 1 ; frame alignment error occured
  130.         BIT_ROK                 equ 0 ; received packet is ok
  131.  
  132.         BIT_RST                 equ 4 ; reset bit
  133.         BIT_RE                  equ 3 ; receiver enabled
  134.         BIT_TE                  equ 2 ; transmitter enabled
  135.         BUFE                    equ 1 ; rx buffer is empty, no packet stored
  136.  
  137.         BIT_ISR_TOK             equ 2 ; transmit ok
  138.         BIT_ISR_RER             equ 1 ; receive error interrupt
  139.         BIT_ISR_ROK             equ 0 ; receive ok
  140.  
  141.         BIT_TX_MXDMA            equ 8 ; Max DMA burst size per Tx DMA burst
  142.         BIT_TXRR                equ 4 ; Tx Retry count 16+(TXRR*16)
  143.  
  144.         BIT_RXFTH               equ 13 ; Rx fifo threshold
  145.         BIT_RBLEN               equ 11 ; Ring buffer length indicator
  146.         BIT_RX_MXDMA            equ 8 ; Max DMA burst size per Rx DMA burst
  147.         BIT_NOWRAP              equ 7 ; transfered data wrapping
  148.         BIT_9356SEL             equ 6 ; eeprom selector 9346/9356
  149.         BIT_AER                 equ 5 ; accept error packets
  150.         BIT_AR                  equ 4 ; accept runt packets
  151.         BIT_AB                  equ 3 ; accept broadcast packets
  152.         BIT_AM                  equ 2 ; accept multicast packets
  153.         BIT_APM                 equ 1 ; accept physical match packets
  154.         BIT_AAP                 equ 0 ; accept all packets
  155.  
  156.         BIT_93C46_EEM1          equ 7 ; RTL8139 eeprom operating mode1
  157.         BIT_93C46_EEM0          equ 6 ; RTL8139 eeprom operating mode0
  158.         BIT_93C46_EECS          equ 3 ; chip select
  159.         BIT_93C46_EESK          equ 2 ; serial data clock
  160.         BIT_93C46_EEDI          equ 1 ; serial data input
  161.         BIT_93C46_EEDO          equ 0 ; serial data output
  162.  
  163.         BIT_LWACT               equ 4 ; see REG_CONFIG1
  164.         BIT_SLEEP               equ 1 ; sleep bit at older chips
  165.         BIT_PWRDWN              equ 0 ; power down bit at older chips
  166.         BIT_PMEn                equ 0 ; power management enabled
  167.  
  168.         BIT_LWPTN               equ 2 ; see REG_CONFIG4
  169.  
  170.         BIT_ERTXTH              equ 16 ; early TX threshold
  171.         BIT_TOK                 equ 15 ; transmit ok
  172.         BIT_OWN                 equ 13 ; tx DMA operation is completed
  173.  
  174.         BIT_ANE                 equ 12 ; auto negotiation enable
  175.  
  176.         BIT_TXFD                equ 8 ; 100base-T full duplex
  177.         BIT_TX                  equ 7 ; 100base-T
  178.         BIT_10FD                equ 6 ; 10base-T full duplex
  179.         BIT_10                  equ 5 ; 10base-T
  180.         BIT_SELECTOR            equ 0 ; binary encoded selector CSMA/CD=00001
  181.  
  182.         BIT_IFG1                equ 25
  183.         BIT_IFG0                equ 24
  184.  
  185.         RBLEN                   equ 2 ; Receive buffer size: 0==8K 1==16k 2==32k 3==64k
  186.         TXRR                    equ 8 ; total retries = 16+(TXRR*16)
  187.         TX_MXDMA                equ 6 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=2048
  188.         ERTXTH                  equ 8 ; in unit of 32 bytes e.g:(8*32)=256
  189.         RX_MXDMA                equ 7 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=unlimited
  190.         RXFTH                   equ 7 ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=no threshold
  191.  
  192.         RX_CONFIG               equ (RBLEN shl BIT_RBLEN) or \
  193.                                     (RX_MXDMA shl BIT_RX_MXDMA) or \
  194.                                     (1 shl BIT_NOWRAP) or \
  195.                                     (RXFTH shl BIT_RXFTH) or\
  196.                                     (1 shl BIT_AB) or \
  197.                                     (1 shl BIT_APM) or \
  198.                                     (1 shl BIT_AER) or \
  199.                                     (1 shl BIT_AR) or \
  200.                                     (1 shl BIT_AM)
  201.  
  202.         RX_BUFFER_SIZE          equ (8192 shl RBLEN)
  203.         MAX_ETH_FRAME_SIZE      equ 1516 ; exactly 1514 wthout CRC
  204.         NUM_TX_DESC             equ 4
  205.         TX_BUF_SIZE             equ 4096 ; size of one tx buffer (set to 4kb because of KolibriOS's page size)
  206.  
  207.         EE_93C46_REG_ETH_ID     equ 7 ; MAC offset
  208.         EE_93C46_READ_CMD       equ (6 shl 6) ; 110b + 6bit address
  209.         EE_93C56_READ_CMD       equ (6 shl 8) ; 110b + 8bit address
  210.         EE_93C46_CMD_LENGTH     equ 9  ; start bit + cmd + 6bit address
  211.         EE_93C56_CMD_LENGTH     equ 11 ; start bit + cmd + 8bit ddress
  212.  
  213.         VER_RTL8139             equ 1100000b
  214.         VER_RTL8139A            equ 1110000b
  215.         VER_RTL8139AG           equ 1110100b
  216.         VER_RTL8139B            equ 1111000b
  217.         VER_RTL8130             equ VER_RTL8139B
  218.         VER_RTL8139C            equ 1110100b
  219.         VER_RTL8100             equ 1111010b
  220.         VER_RTL8100B            equ 1110101b
  221.         VER_RTL8139D            equ VER_RTL8100B
  222.         VER_RTL8139CP           equ 1110110b
  223.         VER_RTL8101             equ 1110111b
  224.  
  225.         IDX_RTL8139             equ 0
  226.         IDX_RTL8139A            equ 1
  227.         IDX_RTL8139B            equ 2
  228.         IDX_RTL8139C            equ 3
  229.         IDX_RTL8100             equ 4
  230.         IDX_RTL8139D            equ 5
  231.         IDX_RTL8139D            equ 6
  232.         IDX_RTL8101             equ 7
  233.  
  234.         ISR_SERR                equ 1 SHL 15
  235.         ISR_TIMEOUT             equ 1 SHL 14
  236.         ISR_LENCHG              equ 1 SHL 13
  237.         ISR_FIFOOVW             equ 1 SHL 6
  238.         ISR_PUN                 equ 1 SHL 5
  239.         ISR_RXOVW               equ 1 SHL 4
  240.         ISR_TER                 equ 1 SHL 3
  241.         ISR_TOK                 equ 1 SHL 2
  242.         ISR_RER                 equ 1 SHL 1
  243.         ISR_ROK                 equ 1 SHL 0
  244.  
  245.         INTERRUPT_MASK          equ ISR_ROK or \
  246.                                     ISR_RXOVW or \
  247.                                     ISR_PUN or \
  248.                                     ISR_FIFOOVW or \
  249.                                     ISR_LENCHG or \
  250.                                     ISR_TOK or \
  251.                                     ISR_TER
  252.  
  253.         TSR_OWN                 equ 1 SHL 13
  254.         TSR_TUN                 equ 1 SHL 14
  255.         TSR_TOK                 equ 1 SHL 15
  256.  
  257.         TSR_CDH                 equ 1 SHL 28
  258.         TSR_OWC                 equ 1 SHL 29
  259.         TSR_TABT                equ 1 SHL 30
  260.         TSR_CRS                 equ 1 SHL 31
  261.  
  262.  
  263.  
  264. section '.flat' code readable align 16
  265.  
  266. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  267. ;;                        ;;
  268. ;; proc START             ;;
  269. ;;                        ;;
  270. ;; (standard driver proc) ;;
  271. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  272.  
  273. align 4
  274. proc START stdcall, state:dword
  275.  
  276.         cmp [state], 1
  277.         jne .exit
  278.  
  279.   .entry:
  280.  
  281.         DEBUGF 1,"Loading rtl8139 driver\n"
  282.         stdcall RegService, my_service, service_proc
  283.         ret
  284.  
  285.   .fail:
  286.   .exit:
  287.         xor eax, eax
  288.         ret
  289.  
  290. endp
  291.  
  292.  
  293. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  294. ;;                        ;;
  295. ;; proc SERVICE_PROC      ;;
  296. ;;                        ;;
  297. ;; (standard driver proc) ;;
  298. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  299.  
  300. align 4
  301. proc service_proc stdcall, ioctl:dword
  302.  
  303.         mov     edx, [ioctl]
  304.         mov     eax, [ebx+IOCTL.io_code]
  305.  
  306. ;------------------------------------------------------
  307.  
  308.         cmp     eax, 0 ;SRV_GETVERSION
  309.         jne     @F
  310.  
  311.         cmp     [edx+IOCTL.out_size], 4
  312.         jl      .fail
  313.         mov     eax, [edx+IOCTL.output]
  314.         mov     [eax], dword API_VERSION
  315.  
  316.         xor     eax, eax
  317.         ret
  318.  
  319. ;------------------------------------------------------
  320.   @@:
  321.         cmp     eax, 1 ;SRV_HOOK
  322.         jne     .fail
  323.  
  324.         cmp     [edx + IOCTL.inp_size], 3               ; Data input must be at least 3 bytes
  325.         jl      .fail
  326.  
  327.         mov     eax, [edx + IOCTL.input]
  328.         cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
  329.         jne     .fail                                   ; other types arent supported for this card yet
  330.  
  331. ; check if the device is already listed
  332.  
  333.         mov     esi, RTL8139_LIST
  334.         mov     ecx, [RTL8139_DEV]
  335.         test    ecx, ecx
  336.         jz      .firstdevice
  337. ;        mov     eax, [edx+IOCTL.input]                  ; get the pci bus and device numbers
  338.         mov     bx , [eax+1]                            ;
  339.   .nextdevice:
  340.         lodsd
  341.         cmp     bx , word [eax + device.pci_bus]        ; compare with pci and device num in RTL8139 list (notice the usage of word instead of byte)
  342.         je      .find_devicenum                         ; Device is already loaded, let's find it's device number
  343.  
  344.         loop    .nextdevice
  345.  
  346. ; This device doesnt have its own eth_device structure yet, lets create one
  347.   .firstdevice:
  348.         cmp     [RTL8139_DEV], MAX_RTL8139              ; First check if the driver can handle one more card
  349.         jge     .fail
  350.  
  351.         push    edx
  352.         stdcall KernelAlloc, dword device.size          ; Allocate the buffer for eth_device structure
  353.         pop     edx
  354.         test    eax, eax
  355.         jz      .fail
  356.         mov     ebx, eax                                ; ebx is always used as a pointer to the structure (in driver, but also in kernel code)
  357.  
  358. ; Fill in the direct call addresses into the struct
  359.  
  360.         mov     dword [ebx+device.reset], reset
  361.         mov     dword [ebx+device.transmit], transmit
  362.         mov     dword [ebx+device.get_MAC], read_mac
  363.         mov     dword [ebx+device.set_MAC], write_mac
  364.         mov     dword [ebx+device.unload], unload
  365.         mov     dword [ebx+device.name], devicename
  366.  
  367. ; save the pci bus and device numbers
  368.  
  369.         mov     eax, [edx+IOCTL.input]
  370.         mov     cl , [eax+1]
  371.         mov     [ebx+device.pci_bus], cl
  372.         mov     cl , [eax+2]
  373.         mov     [ebx+device.pci_dev], cl
  374.  
  375. ; Now, it's time to find the base io addres of the PCI device
  376. ; TODO: implement check if bus and dev exist on this machine
  377.  
  378.         mov     edx, PCI_BASE_ADDRESS_0
  379.   .reg_check:
  380.         movzx   eax, byte [ebx+device.pci_bus]
  381.         movzx   ecx, byte [ebx+device.pci_dev]
  382.  
  383.         push    edx ecx
  384.         stdcall PciRead16, eax ,ecx ,edx
  385.         pop     ecx edx
  386.  
  387.         mov     [ebx+device.io_addr], eax
  388.         and     eax, PCI_BASE_ADDRESS_IO_MASK
  389.         test    eax, eax
  390.         jz      .inc_reg
  391.         mov     eax, [ebx+device.io_addr]
  392.         and     eax, PCI_BASE_ADDRESS_SPACE_IO
  393.         test    eax, eax
  394.         jz      .inc_reg
  395.  
  396.         mov     eax, [ebx+device.io_addr]
  397.         and     eax, PCI_BASE_ADDRESS_IO_MASK
  398.         mov     [ebx+device.io_addr], eax
  399.         jmp     .got_io
  400.  
  401.   .inc_reg:
  402.         add     edx, 4
  403.         cmp     edx, PCI_BASE_ADDRESS_5
  404.         jbe     .reg_check
  405.  
  406.   .got_io:
  407.  
  408. ; We've found the io address, find IRQ now
  409.  
  410.         movzx   eax, byte [ebx+device.pci_bus]
  411.         movzx   ecx, byte [ebx+device.pci_dev]
  412.         push    ebx
  413.         stdcall PciRead8, eax ,ecx ,0x3c                                ; 0x3c is the offset where irq can be found
  414.         pop     ebx
  415.         mov     byte [ebx+device.irq_line], al
  416.  
  417.         DEBUGF  1,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
  418.         [ebx+device.pci_dev]:1,[ebx+device.pci_bus]:1,[ebx+device.irq_line]:1,[ebx+device.io_addr]:4
  419.  
  420. ; Allocate the Receive buffer
  421.  
  422.         stdcall KernelAlloc, dword (RX_BUFFER_SIZE+MAX_ETH_FRAME_SIZE)
  423.         test    eax, eax
  424.         jz      .err
  425.         mov     [ebx+device.rx_buffer], eax                             ; Save the address to it into the device struct
  426.  
  427. ; Now, Clear the allocated buffer
  428.  
  429.         cld
  430.         mov     edi, eax
  431.         mov     ecx, (RX_BUFFER_SIZE)/4                                 ; divide by 4 because we are going to use DWORD
  432.         xor     eax, eax
  433.         rep     stosd
  434.  
  435. ; Allocate the Transmit Buffer
  436.  
  437.         stdcall KernelAlloc, dword (TX_BUF_SIZE*NUM_TX_DESC)
  438.         test    eax, eax
  439.         jz      .err
  440.         mov     [ebx+device.tx_buffer], eax
  441.  
  442. ; This one needs to be cleared too..
  443.  
  444.         mov     edi, eax
  445.         mov     ecx, (TX_BUF_SIZE*NUM_TX_DESC)/4
  446.         xor     eax, eax
  447.         rep     stosd
  448.  
  449. ; Ok, the eth_device structure is ready, let's probe the device
  450.  
  451.         call    probe                                                   ; this function will output in eax
  452.         test    eax, eax
  453.         jnz     .err                                                    ; If an error occured, exit
  454.  
  455.         mov     eax, [RTL8139_DEV]                                      ; Add the device structure to our device list
  456.         mov     [RTL8139_LIST+4*eax], ebx                               ; (IRQ handler uses this list to find device)
  457.         inc     [RTL8139_DEV]                                           ;
  458.  
  459.  
  460.         call    EthRegDev
  461.         cmp     eax, -1
  462.         je      .destroy
  463.  
  464.         ret
  465.  
  466. ; If the device was already loaded, find the device number and return it in eax
  467.  
  468.   .find_devicenum:
  469.         DEBUGF  1,"Trying to find device number of already registered device\n"
  470.         mov     ebx, eax
  471.         call    EthStruc2Dev                                            ; This kernel procedure converts a pointer to device struct in ebx
  472.                                                                         ; into a device number in edi
  473.         mov     eax, edi                                                ; Application wants it in eax instead
  474.         DEBUGF  1,"Kernel says: %u\n", eax
  475.         ret
  476.  
  477. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  478.  
  479.   .destroy:
  480.         ; todo: reset device into virgin state
  481.  
  482.   .err:
  483.         stdcall KernelFree, dword [ebx+device.rx_buffer]
  484.         stdcall KernelFree, dword [ebx+device.tx_buffer]
  485.         stdcall KernelFree, ebx
  486.  
  487.  
  488.   .fail:
  489.         or      eax, -1
  490.         ret
  491.  
  492. ;------------------------------------------------------
  493. endp
  494.  
  495.  
  496. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  497. ;;                                                                        ;;
  498. ;;        Actual Hardware dependent code starts here                      ;;
  499. ;;                                                                        ;;
  500. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  501.  
  502. align 4
  503. unload:
  504.         ; TODO: (in this particular order)
  505.         ;
  506.         ; - Stop the device
  507.         ; - Detach int handler
  508.         ; - Remove device from local list (RTL8139_LIST)
  509.         ; - call unregister function in kernel
  510.         ; - Remove all allocated structures and buffers the card used
  511.  
  512.         or      eax,-1
  513.  
  514. ret
  515.  
  516.  
  517. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  518. ;;
  519. ;;  probe: enables the device (if it really is RTL8139)
  520. ;;
  521. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  522.  
  523. align 4
  524. probe:
  525.         DEBUGF  2,"Probing rtl8139 device: "
  526.  
  527. ; enable the device
  528.  
  529.         movzx   eax, byte [ebx+device.pci_bus]
  530.         movzx   ecx, byte [ebx+device.pci_dev]
  531.         stdcall PciRead32, eax ,ecx ,PCI_REG_CMD
  532.  
  533.         mov     cx , ax
  534.         or      cl , (1 shl PCI_BIT_MASTER) or (1 shl PCI_BIT_PIO)
  535.         and     cl , not (1 shl PCI_BIT_MMIO)
  536.         movzx   eax, byte [ebx+device.pci_bus]
  537.         movzx   edx, byte [ebx+device.pci_dev]
  538.         stdcall PciWrite32, eax ,edx ,PCI_REG_CMD, ecx
  539.  
  540. ; get chip version
  541.  
  542.         mov     edx, [ebx+device.io_addr]
  543.         add     edx, REG_TXCONFIG + 2
  544.         in      ax , dx
  545.         shr     ah , 2
  546.         shr     ax , 6
  547.         and     al , 01111111b
  548.         mov     ecx, HW_VER_ARRAY_SIZE-1
  549.   .chip_ver_loop:
  550.         cmp     al , [hw_ver_array+ecx]
  551.         je      .chip_ver_found
  552.         dec     ecx
  553.         jns     .chip_ver_loop
  554.         xor     cl , cl ; default RTL8139
  555.   .chip_ver_found:
  556.         mov     [ebx+device.hw_ver_id], cl
  557.  
  558. ; wake up the chip
  559.  
  560.         mov     edx, [ebx+device.io_addr]
  561.         add     edx, REG_HLTCLK
  562.         mov     al , 'R' ; run the clock
  563.         out     dx , al
  564.  
  565. ; unlock config and BMCR registers
  566.  
  567.         add     edx, REG_9346CR - REG_HLTCLK
  568.         mov     al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  569.         out     dx , al
  570.  
  571. ; enable power management
  572.  
  573.         add     edx, REG_CONFIG1 - REG_9346CR
  574.         in      al , dx
  575.         cmp     byte [ebx+device.hw_ver_id], IDX_RTL8139B
  576.         jl      .old_chip
  577.  
  578. ; set LWAKE pin to active high (default value).
  579. ; it is for Wake-On-LAN functionality of some motherboards.
  580. ; this signal is used to inform the motherboard to execute a wake-up process.
  581. ; only at newer chips.
  582.  
  583.         or      al , (1 shl BIT_PMEn)
  584.         and     al , not (1 shl BIT_LWACT)
  585.         out     dx , al
  586.         add     edx, REG_CONFIG4 - REG_CONFIG1
  587.         in      al , dx
  588.         and     al , not (1 shl BIT_LWPTN)
  589.         out     dx , al
  590.         jmp     .finish_wake_up
  591.   .old_chip:
  592.  
  593. ; wake up older chips
  594.  
  595.         and     al , not ((1 shl BIT_SLEEP) or (1 shl BIT_PWRDWN))
  596.         out     dx , al
  597.   .finish_wake_up:
  598.  
  599. ; lock config and BMCR registers
  600.  
  601.         xor     al , al
  602.         mov     edx, [ebx+device.io_addr]
  603.         add     edx, REG_9346CR
  604.         out     dx , al
  605.         DEBUGF  2,"done!\n"
  606.  
  607.  
  608. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  609. ;;
  610. ;;   reset: Set up all registers and descriptors, clear some values
  611. ;;
  612. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  613.  
  614. reset:
  615.         DEBUGF  2,"Resetting rtl8139: "
  616.  
  617. ; attach int handler
  618.  
  619.         movzx   eax, [ebx+device.irq_line]
  620.         DEBUGF  1,"Attaching int handler to irq %x, ",eax:1
  621.         stdcall AttachIntHandler, eax, int_handler, dword 0
  622.         test    eax, eax
  623.         jnz     @f
  624.         DEBUGF  1,"\nCould not attach int handler!\n"
  625. ;        or      eax, -1
  626. ;        ret
  627.   @@:
  628.  
  629. ; reset chip
  630.  
  631.         DEBUGF  1,"Resetting chip\n"
  632.         mov     edx, [ebx+device.io_addr]
  633.         add     edx, REG_COMMAND
  634.         mov     al , 1 shl BIT_RST
  635.         out     dx , al
  636.         mov     cx , 1000               ; wait no longer for the reset
  637.   .wait_for_reset:
  638.         in      al , dx
  639.         test    al , 1 shl BIT_RST
  640.         jz      .reset_completed        ; RST remains 1 during reset
  641.         dec     cx
  642.         jns     .wait_for_reset
  643.   .reset_completed:
  644.  
  645. ; unlock config and BMCR registers
  646.  
  647.         mov     edx, [ebx+device.io_addr]
  648.         add     edx, REG_9346CR
  649.         mov     al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  650.         out     dx , al
  651.  
  652. ; initialize multicast registers (no filtering)
  653.  
  654.         mov     eax, 0xffffffff
  655.         add     edx, REG_MAR0 - REG_9346CR
  656.         out     dx , eax
  657.         add     edx, REG_MAR4 - REG_MAR0
  658.         out     dx , eax
  659.  
  660. ; enable Rx/Tx
  661.  
  662.         mov     al , (1 shl BIT_RE) or (1 shl BIT_TE)
  663.         add     edx, REG_COMMAND - REG_MAR4
  664.         out     dx , al
  665.  
  666. ; 32k Rxbuffer, unlimited dma burst, no wrapping, no rx threshold
  667. ; accept broadcast packets, accept physical match packets
  668.  
  669.         mov     ax , RX_CONFIG
  670.         add     edx, REG_RXCONFIG - REG_COMMAND
  671.         out     dx , ax
  672.  
  673. ; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
  674.  
  675.         mov     eax , (TX_MXDMA shl BIT_TX_MXDMA) or (TXRR shl BIT_TXRR) or BIT_IFG1 or BIT_IFG0
  676.         add     edx, REG_TXCONFIG - REG_RXCONFIG
  677.         out     dx , eax
  678.  
  679. ; enable auto negotiation
  680.  
  681.         add     edx, REG_BMCR - REG_TXCONFIG
  682.         in      ax , dx
  683.         or      ax , (1 shl BIT_ANE)
  684.         out     dx , ax
  685.  
  686. ; set auto negotiation advertisement
  687.  
  688.         add     edx, REG_ANAR - REG_BMCR
  689.         in      ax , dx
  690.         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)
  691.         out     dx , ax
  692.  
  693. ; lock config and BMCR registers
  694.  
  695.         xor     eax, eax
  696.         add     edx, REG_9346CR - REG_ANAR
  697.         out     dx , al
  698.  
  699. ; init RX/TX pointers
  700.  
  701.         mov     [ebx+device.rx_data_offset], eax
  702.         mov     [ebx+device.curr_tx_desc], al
  703.  
  704. ; clear missing packet counter
  705.  
  706.         add     edx, REG_MPC - REG_9346CR
  707.         out     dx , eax
  708.  
  709. ; Set up the 4 Txbuffer descriptors
  710.  
  711.         add     edx, REG_TSAD0 - REG_MPC
  712.         mov     eax, [ebx+device.tx_buffer]
  713.         mov     ecx, 4
  714.   .loop:
  715.         push    eax
  716.         call    GetPgAddr
  717.         DEBUGF  2,"Desc: %x ", eax
  718.         out     dx , eax
  719.         add     dx , 4
  720.         pop     eax
  721.         add     eax, TX_BUF_SIZE
  722.         loop    .loop
  723.  
  724. ; set RxBuffer address, init RX buffer offset, init TX ring
  725.  
  726.         mov     eax, [ebx+device.rx_buffer]
  727.         call    GetPgAddr
  728.         mov     edx, [ebx+device.io_addr]
  729.         add     edx, REG_RBSTART
  730.         out     dx , eax
  731.  
  732. ; enable interrupts
  733.  
  734.         mov     eax, INTERRUPT_MASK
  735.         add     edx, REG_IMR - REG_RBSTART
  736.         out     dx , ax
  737.  
  738. ; Read MAC address
  739.  
  740.         call    read_mac
  741.  
  742. ; Indicate that we have successfully reset the card
  743.  
  744.         DEBUGF  2,"Done!\n"
  745.         xor     eax, eax
  746.  
  747.         ret
  748.  
  749.  
  750. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  751. ;;                                         ;;
  752. ;; Transmit                                ;;
  753. ;;                                         ;;
  754. ;; In: buffer pointer in [esp]             ;;
  755. ;;     size of buffer in [esp+4]           ;;
  756. ;;     pointer to device structure in ebx  ;;
  757. ;;                                         ;;
  758. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  759.  
  760. align 4
  761. transmit:
  762.         DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n",[esp],[esp+4]
  763.         mov     eax, [esp]
  764.         DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  765.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  766.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  767.         [eax+13]:2,[eax+12]:2
  768.  
  769.         cmp     dword [esp+4], MAX_ETH_FRAME_SIZE
  770.         jg      .finish                         ; packet is too long
  771.         cmp     dword [esp+4], 60
  772.         jl      .finish                         ; packet is too short
  773.  
  774. ; check descriptor
  775. ;        DEBUGF  1,"Checking descriptor, "
  776.         movzx   ecx, [ebx+device.curr_tx_desc]
  777.         mov     edx, [ebx+device.io_addr]
  778.         lea     edx, [edx+ecx*4+REG_TSD0]
  779.         in      ax, dx
  780.         test    ax, 0x1fff ; or no size given
  781.         jz      .send_packet
  782.         and     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  783.         cmp     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  784.         jz      .send_packet
  785. ; wait for timeout
  786. ;        DEBUGF  1,"Waiting for timeout, "
  787.  
  788.         push    edx ebx                          ; TODO : rtl8139 internal timer should be used instead
  789.         stdcall Sleep, TX_TIMEOUT                ; ? What registers does this destroy ?
  790.         pop     ebx edx
  791.  
  792.         in      ax, dx
  793.         and     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  794.         cmp     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  795.         jz      .send_packet                     ; if chip hung, reset it
  796.         push    dx
  797.         call    reset                            ; reset the card
  798.         pop     dx
  799. .send_packet:
  800. ;        DEBUGF  1,"Sending packet, "
  801.  
  802.         push    edx
  803.         movzx   eax, [ebx+device.curr_tx_desc]   ; calculate the current tx_buffer address
  804.         mov     edx, TX_BUF_SIZE ;MAX_ETH_FRAME_SIZE          ;
  805.         mul     edx                              ;
  806.         mov     edi, [ebx+device.tx_buffer]      ;
  807.         add     edi, eax                         ; Store it in edi
  808.         pop     edx
  809.  
  810.         mov     esi, [esp]                       ; Copy data to that address
  811.         mov     ecx, [esp+4]                     ;
  812.         shr     ecx, 2                           ;
  813.         rep     movsd                            ;
  814.         mov     ecx, [esp+4]                     ;
  815.         and     ecx, 3                           ;
  816.         rep     movsb                            ;
  817.  
  818.         inc     [ebx+device.packets_tx]          ;
  819.         mov     eax, [esp+4]                     ; Get packet size in eax
  820.  
  821.         add     dword [ebx + device.bytes_tx], eax
  822.         adc     dword [ebx + device.bytes_tx + 4], 0
  823.  
  824. ;        or      eax, (ERTXTH shl BIT_ERTXTH)     ; Set descriptor size and the early tx treshold into the correct Transmission status register (TSD0, TSD1, TSD2 or TSD3)
  825.         out     dx , eax                         ;
  826.  
  827. ; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
  828.         inc     [ebx+device.curr_tx_desc]
  829.         and     [ebx+device.curr_tx_desc], 3
  830.  
  831.         DEBUGF  2," - Packet Sent! "
  832. .finish:
  833.         DEBUGF  2," - Done!\n"
  834.         call    KernelFree
  835.         add     esp, 4 ; pop (balance stack)
  836.  
  837.         ret
  838.  
  839.  
  840.  
  841.  
  842.  
  843. ;;;;;;;;;;;;;;;;;;;;;;;
  844. ;;                   ;;
  845. ;; Interrupt handler ;;
  846. ;;                   ;;
  847. ;;;;;;;;;;;;;;;;;;;;;;;
  848.  
  849. align 4
  850. int_handler:
  851.  
  852.         DEBUGF  1,"IRQ %x ",eax:2                   ; no, you cant replace 'eax:2' with 'al', this must be a bug in FDO
  853.  
  854. ; find pointer of device wich made IRQ occur
  855.  
  856.         mov     esi, RTL8139_LIST
  857.         mov     ecx, [RTL8139_DEV]
  858. .nextdevice:
  859.         mov     ebx, dword [esi]
  860.  
  861.         mov     edx, dword [ebx+device.io_addr]     ; get IRQ reason
  862.         add     edx, REG_ISR
  863.         in      ax , dx
  864.         out     dx , ax                             ; send it back to ACK
  865.  
  866.         add     esi, 4
  867.  
  868.         test    ax , ax
  869.         jnz     .got_it
  870.  
  871.         loop    .nextdevice
  872.  
  873.         ret                                         ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
  874.  
  875.   .got_it:
  876.  
  877. ; looks like we've found it!
  878.  
  879. ; Lets found out why the irq occured then..
  880.  
  881. ;----------------------------------------------------
  882. ; Received packet ok?
  883.  
  884.         test    ax, ISR_ROK
  885.         jz      @f
  886.         push    ax
  887.  
  888.   .receive:
  889.         mov     edx, dword [ebx+device.io_addr]     ; get IRQ reason
  890.         add     edx, REG_COMMAND                    ;
  891.         in      al , dx                             ;
  892.         test    al , BUFE                           ; test if RX buffer is empty
  893.         jnz     .finish                             ;
  894.  
  895.         DEBUGF  2,"RX: "
  896.  
  897.         mov     eax, dword [ebx+device.rx_buffer]
  898.         add     eax, dword [ebx+device.rx_data_offset]
  899.         test    byte [eax], (1 shl BIT_ROK)         ; check if packet is ok
  900.         jz      .reset_rx
  901.                                                     ; packet is ok, copy it
  902.         movzx   ecx, word [eax+2]                   ; packet length
  903.         add     dword [ebx + device.bytes_rx], ecx  ; Update stats
  904.         adc     dword [ebx + device.bytes_rx + 4], 0
  905.         inc     dword [ebx + device.packets_rx]     ;
  906.         sub     ecx, 4                              ; don't copy CRC
  907.         DEBUGF  1,"Received %u bytes\n", ecx
  908.  
  909.         push    ebx eax ecx
  910.         stdcall KernelAlloc, ecx                    ; Allocate a buffer to put packet into
  911.         pop     ecx
  912.         test    eax, eax                            ; Test if we allocated succesfully
  913.         jz      .abort                              ;
  914.  
  915.         mov     edi, eax                            ; Set up registers to copy the packet
  916.         mov     esi, [esp]                          ;
  917.         add     esi, 4                              ; Dont copy CRC
  918.  
  919.         push    dword .abort                        ; Kernel will return to this address after EthReceiver
  920.         push    ecx edi                             ; Save buffer pointer and size, to pass to kernel
  921.  
  922.         shr     ecx, 2
  923.         cld
  924.         rep     movsd                               ; copy the dwords
  925.         mov     ecx, [esp+4]
  926.         and     ecx, 3
  927.         rep     movsb                               ; copy the rest bytes
  928.  
  929.         jmp     EthReceiver                         ; Send it to kernel
  930.  
  931.   .abort:
  932.         pop     eax ebx
  933.                                                     ; update eth_data_start_offset
  934.         movzx   eax, word [eax+2]                   ; packet length
  935.         add     eax, [ebx+device.rx_data_offset]
  936.         add     eax, 4+3                            ; packet header is 4 bytes long + dword alignment
  937.         and     eax, not 3                          ; dword alignment
  938.         cmp     eax, RX_BUFFER_SIZE
  939.         jl      .no_wrap
  940.         sub     eax, RX_BUFFER_SIZE
  941.   .no_wrap:
  942.         mov     [ebx+device.rx_data_offset], eax
  943.         DEBUGF  1,"New RX ptr: %u", eax
  944.  
  945.         mov     edx, dword [ebx+device.io_addr]
  946.         add     edx, REG_CAPR                       ; update 'Current Address of Packet Read register'
  947.         sub     eax, 0x10                           ; value 0x10 is a constant for CAPR
  948.         out     dx , ax
  949.  
  950.         jmp     .receive                            ; check for multiple packets
  951.  
  952.   .reset_rx:
  953.         test    byte [eax], (1 shl BIT_CRC)
  954.         jz      .no_crc_error
  955.         DEBUGF  2,"\nCRC error!\n"
  956.  
  957.   .no_crc_error:
  958.         test    byte [eax], (1 shl BIT_FAE)
  959.         jz      .no_fae_error
  960.         DEBUGF  1,"\nFrame alignment error!\n"
  961.  
  962.   .no_fae_error:
  963.         DEBUGF  1,"Reset RX\n"
  964.         in      al , dx                             ; read command register
  965.         push    ax
  966.  
  967.         and     al , not (1 shl BIT_RE)             ; Clear the RE bit
  968.         out     dx , al
  969.  
  970.         pop     ax
  971.         out     dx , al                             ; write original command back
  972.  
  973.         add     edx, REG_RXCONFIG - REG_COMMAND     ; Restore RX configuration
  974.         mov     ax , RX_CONFIG
  975.         out     dx , ax
  976.  
  977.   .finish:
  978.         pop     ax
  979.  
  980. ;----------------------------------------------------
  981. ; Transmit error ?
  982.  
  983.   @@:
  984.         test    ax, ISR_TER
  985.         jz      @f
  986.  
  987.         push    ax
  988.         cmp     [ebx+device.curr_tx_desc], 4
  989.         jz      .notxd
  990.  
  991.         mov     edx, [ebx+device.io_addr]
  992.         movzx   ecx, [ebx+device.curr_tx_desc]
  993.         lea     edx, [edx+ecx*4+REG_TSD0]
  994.         in      eax, dx
  995.  
  996.   .notxd:
  997.         test    eax, TSR_TUN
  998.         jz      .nobun
  999.         DEBUGF  1, "TX: FIFO Buffer underrun!\n"
  1000.  
  1001.   .nobun:
  1002.         test    eax, TSR_OWC
  1003.         jz      .noowc
  1004.         DEBUGF  1, "TX: OWC!\n"
  1005.  
  1006.   .noowc:
  1007.         test    eax, TSR_TABT
  1008.         jz      .notabt
  1009.         DEBUGF  1, "TX: TABT!\n"
  1010.  
  1011.   .notabt:
  1012.         test    eax, TSR_CRS
  1013.         jz      .nocsl
  1014.         DEBUGF  1, "TX: Carrier Sense Lost!\n"
  1015.  
  1016.   .nocsl:
  1017. ;                test    eax, TSR_OWN or TSR_TOK
  1018. ;                jz      .nofd
  1019. ;                DEBUGF  1, "TX: Transmit OK (desc: %u)\n", ecx
  1020. ;
  1021. ;               .nofd:
  1022.         pop     ax
  1023.  
  1024. ;----------------------------------------------------
  1025. ; Transmit ok ?
  1026.  
  1027.   @@:
  1028.         test    ax, ISR_TOK
  1029.         jz      @f
  1030.  
  1031.         DEBUGF  1, "TX: Transmit OK (desc: %u)\n", [ebx+device.curr_tx_desc]:1
  1032.  
  1033. ;----------------------------------------------------
  1034. ; Rx buffer overflow ?
  1035.  
  1036.   @@:
  1037.         test    ax, ISR_RXOVW
  1038.         jz      @f
  1039.  
  1040.         push    ax
  1041.         DEBUGF  1,"RX-buffer overflow!\n"
  1042.  
  1043.         mov     edx, [ebx+device.io_addr]
  1044.         add     edx, REG_ISR
  1045.         mov     ax , ISR_FIFOOVW or ISR_RXOVW
  1046.         out     dx , ax
  1047.         pop     ax
  1048.  
  1049. ;----------------------------------------------------
  1050. ; Packet underrun? ?
  1051.  
  1052.  
  1053.   @@:
  1054.         test    ax, ISR_PUN
  1055.         jz      @f
  1056.  
  1057.         DEBUGF  1,"Packet underrun!\n"
  1058.  
  1059. ;----------------------------------------------------
  1060. ; Receive FIFO overflow ?
  1061.  
  1062.   @@:
  1063.         test    ax, ISR_FIFOOVW
  1064.         jz      @f
  1065.  
  1066.         push    ax
  1067.         DEBUGF  2,"RX fifo overflox!\n"
  1068.  
  1069.         mov     edx, [ebx+device.io_addr]
  1070.         add     edx, REG_ISR
  1071.         mov     ax , ISR_FIFOOVW or ISR_RXOVW
  1072.         out     dx , ax
  1073.         pop     ax
  1074.  
  1075. ;----------------------------------------------------
  1076. ; Something about Cable changed ?
  1077.  
  1078.   @@:
  1079.         test    ax, ISR_LENCHG
  1080.         jz      .fail
  1081.  
  1082.         DEBUGF  2,"Cable changed!\n"
  1083.         call    cable
  1084.  
  1085. ; If none of the above events happened, just exit clearing int
  1086.  
  1087.   .fail:
  1088.  
  1089.         DEBUGF  2,"\n"
  1090.         ret
  1091.  
  1092.  
  1093.  
  1094.  
  1095. ;;;;;;;;;;;;;;;;;;;;;;;;;
  1096. ;;                     ;;
  1097. ;; Update Cable status ;;
  1098. ;;                     ;;
  1099. ;;;;;;;;;;;;;;;;;;;;;;;;;
  1100.  
  1101. align 4
  1102. cable:
  1103.         DEBUGF  1,"Checking Cable status: "
  1104.  
  1105.         mov     edx, dword [ebx+device.io_addr]
  1106.         add     edx, REG_MSR
  1107.         in      al , dx
  1108.  
  1109. ;        test    al , 1 SHL 2     ; 0 = link ok 1 = link fail
  1110. ;        jnz     .notconnected
  1111.  
  1112. ;        test    al , 1 SHL 3     ; 0 = 100 Mbps 1 = 10 Mbps
  1113. ;        jnz     .10mbps
  1114.  
  1115.         shr     al, 2
  1116.         and     al, 3
  1117.  
  1118.         mov     byte [ebx+device.mode+3], al
  1119.         DEBUGF  1,"Done!\n"
  1120. ret
  1121.  
  1122.  
  1123.  
  1124. ;;;;;;;;;;;;;;;;;;;;;;;
  1125. ;;                   ;;
  1126. ;; Write MAC address ;;
  1127. ;;                   ;;
  1128. ;;;;;;;;;;;;;;;;;;;;;;;
  1129.  
  1130. align 4
  1131. write_mac:      ; in: mac pushed onto stack (as 3 words)
  1132.  
  1133.         DEBUGF  1,"Writing MAC: "
  1134.  
  1135. ; disable all in command registers
  1136.  
  1137.         mov     edx, [ebx+device.io_addr]
  1138.         add     edx, REG_9346CR
  1139.         xor     eax, eax
  1140.         out     dx , al
  1141.  
  1142.         add     edx, REG_IMR - REG_9346CR
  1143.         xor     eax, eax
  1144.         out     dx , ax
  1145.  
  1146.         add     edx, REG_ISR - REG_IMR
  1147.         mov     eax, -1
  1148.         out     dx , ax
  1149.  
  1150. ; enable writing
  1151.  
  1152.  
  1153.         add     edx, REG_9346CR - REG_ISR
  1154.         mov     eax, REG_9346CR_WE
  1155.         out     dx , al
  1156.  
  1157.  ; write the mac ...
  1158.  
  1159.         add     edx, REG_IDR0 - REG_9346CR
  1160.         pop     eax
  1161.         out     dx , eax
  1162.  
  1163.         add     edx, 4
  1164.         xor     eax, eax
  1165.         pop     ax
  1166.         out     dx , eax
  1167.  
  1168. ; disable writing
  1169.  
  1170.         add     edx, REG_9346CR -REG_IDR0
  1171.         xor     eax, eax
  1172.         out     dx , al
  1173.  
  1174.         DEBUGF  1,"ok!\n"
  1175.  
  1176. ; Notice this procedure does not ret, but continues to read_mac instead.
  1177.  
  1178.  
  1179. ;;;;;;;;;;;;;;;;;;;;;;
  1180. ;;                  ;;
  1181. ;; Read MAC address ;;
  1182. ;;                  ;;
  1183. ;;;;;;;;;;;;;;;;;;;;;;
  1184.  
  1185. read_mac:
  1186.         DEBUGF  1,"Reading MAC: "
  1187.  
  1188.         mov     edx, [ebx + device.io_addr]
  1189.  
  1190.         mov     ecx, 2
  1191.        @@:
  1192.         lea     eax, [EE_93C46_REG_ETH_ID+ecx]
  1193.         push    ecx
  1194.         call    read_eeprom
  1195.         pop     ecx
  1196.         push    ax
  1197.         dec     cl
  1198.         jns     @r
  1199.  
  1200.         DEBUGF  1,"%x-%x-%x-%x-%x-%x\n",[esp+0]:2,[esp+1]:2,[esp+2]:2,[esp+3]:2,[esp+4]:2,[esp+5]:2
  1201.  
  1202.         lea     edi, [ebx + device.mac]
  1203.         pop     eax
  1204.         stosd
  1205.         pop     ax
  1206.         stosw
  1207.  
  1208.         ret
  1209.  
  1210.  
  1211.  
  1212. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1213. ;;                                                                      ;;
  1214. ;; Read eeprom (type 93c46 and 93c56)                                   ;;
  1215. ;;                                                                      ;;
  1216. ;; In: word to be read in al (6bit in case of 93c46 and 8bit otherwise) ;;
  1217. ;;     pointer to device structure in ebx                               ;;
  1218. ;;                                                                      ;;
  1219. ;; OUT: word read in ax                                                 ;;
  1220. ;;                                                                      ;;
  1221. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1222.  
  1223. align 4
  1224. read_eeprom:
  1225.         DEBUGF  2,"Reading eeprom, "
  1226.  
  1227.         mov     edx, [ebx+device.io_addr]
  1228.         push    ebx
  1229.         movzx   ebx, al
  1230.         add     edx, REG_RXCONFIG
  1231.         in      al, dx
  1232.         test    al, (1 shl BIT_9356SEL)
  1233.         jz      .type_93c46
  1234. ;       and     bl, 01111111b ; don't care first bit
  1235.         or      bx, EE_93C56_READ_CMD           ; it contains start bit
  1236.         mov     cx, EE_93C56_CMD_LENGTH-1       ; cmd_loop counter
  1237.         jmp     .read_eeprom
  1238. .type_93c46:
  1239.         and     bl, 00111111b
  1240.         or      bx, EE_93C46_READ_CMD           ; it contains start bit
  1241.         mov     cx, EE_93C46_CMD_LENGTH-1       ; cmd_loop counter
  1242. .read_eeprom:
  1243.         add     edx, REG_9346CR - REG_RXCONFIG
  1244. ;       mov     al, (1 shl BIT_93C46_EEM1)
  1245. ;       out     dx, al
  1246.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) ; wake up the eeprom
  1247.         out     dx, al
  1248. .cmd_loop:
  1249.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1250.         bt      bx, cx
  1251.         jnc     .zero_bit
  1252.         or      al, (1 shl BIT_93C46_EEDI)
  1253. .zero_bit:
  1254.         out     dx, al
  1255. ;       push    eax
  1256. ;       in      eax, dx ; eeprom delay
  1257. ;       pop     eax
  1258.         or      al, (1 shl BIT_93C46_EESK)
  1259.         out     dx, al
  1260. ;       in      eax, dx ; eeprom delay
  1261.         dec     cx
  1262.         jns     .cmd_loop
  1263. ;       in      eax, dx ; eeprom delay
  1264.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1265.         out     dx, al
  1266.         mov     cl, 0xf
  1267. .read_loop:
  1268.         shl     ebx, 1
  1269.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) or (1 shl BIT_93C46_EESK)
  1270.         out     dx, al
  1271. ;       in      eax, dx ; eeprom delay
  1272.         in      al, dx
  1273.         and     al, (1 shl BIT_93C46_EEDO)
  1274.         jz      .dont_set
  1275.         inc     ebx
  1276. .dont_set:
  1277.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1278.         out     dx, al
  1279. ;       in      eax, dx ; eeprom delay
  1280.         dec     cl
  1281.         jns     .read_loop
  1282.         xor     al, al
  1283.         out     dx, al
  1284.         mov     ax, bx
  1285.         pop     ebx
  1286.  
  1287.         ret
  1288.  
  1289.  
  1290. ; End of code
  1291.  
  1292. align 4                                         ; Place all initialised data here
  1293.  
  1294. RTL8139_DEV   dd 0
  1295. version       dd (5 shl 16) or (API_VERSION and 0xFFFF)
  1296. my_service    db 'RTL8139',0                    ; max 16 chars include zero
  1297. devicename    db 'Realtek 8139',0
  1298.  
  1299. hw_ver_array  db VER_RTL8139                    ; This array is used by the probe routine to find out wich version of the RTL8139 we are working with
  1300.               db VER_RTL8139A
  1301.               db VER_RTL8139B
  1302.               db VER_RTL8139C
  1303.               db VER_RTL8100
  1304.               db VER_RTL8139D
  1305.               db VER_RTL8139CP
  1306.               db VER_RTL8101
  1307.  
  1308. HW_VER_ARRAY_SIZE = $-hw_ver_array
  1309.  
  1310. include_debug_strings                           ; All data wich FDO uses will be included here
  1311.  
  1312. section '.data' data readable writable align 16 ; place all uninitialized data place here
  1313.  
  1314. RTL8139_LIST rd MAX_RTL8139                     ; This list contains all pointers to device structures the driver is handling
  1315.  
  1316.