Subversion Repositories Kolibri OS

Rev

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