Subversion Repositories Kolibri OS

Rev

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