Subversion Repositories Kolibri OS

Rev

Rev 1492 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2010. 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.         mov     [device.type], NET_TYPE_ETH
  372.         call    NetRegDev
  373.  
  374.         cmp     eax, -1
  375.         je      .destroy
  376.  
  377.         ret
  378.  
  379. ; If the device was already loaded, find the device number and return it in eax
  380.  
  381.   .find_devicenum:
  382.         DEBUGF  2,"Trying to find device number of already registered device\n"
  383.         call    NetPtrToNum                                             ; This kernel procedure converts a pointer to device struct in ebx
  384.                                                                         ; into a device number in edi
  385.         mov     eax, edi                                                ; Application wants it in eax instead
  386.         DEBUGF  2,"Kernel says: %u\n", eax
  387.         ret
  388.  
  389. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  390.  
  391.   .destroy:
  392.         ; todo: reset device into virgin state
  393.  
  394.   .err:
  395.         stdcall KernelFree, dword [device.rx_buffer]
  396.         stdcall KernelFree, dword [device.tx_buffer]
  397.         stdcall KernelFree, ebx
  398.  
  399.  
  400.   .fail:
  401.         or      eax, -1
  402.         ret
  403.  
  404. ;------------------------------------------------------
  405. endp
  406.  
  407.  
  408. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  409. ;;                                                                        ;;
  410. ;;        Actual Hardware dependent code starts here                      ;;
  411. ;;                                                                        ;;
  412. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  413.  
  414. align 4
  415. unload:
  416.         ; TODO: (in this particular order)
  417.         ;
  418.         ; - Stop the device
  419.         ; - Detach int handler
  420.         ; - Remove device from local list (RTL8139_LIST)
  421.         ; - call unregister function in kernel
  422.         ; - Remove all allocated structures and buffers the card used
  423.  
  424.         or      eax,-1
  425.  
  426. ret
  427.  
  428.  
  429. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  430. ;;
  431. ;;  probe: enables the device (if it really is RTL8139)
  432. ;;
  433. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  434.  
  435. align 4
  436. probe:
  437.         DEBUGF  2,"Probing rtl8139 device: "
  438.  
  439.         make_bus_master [device.pci_bus], [device.pci_dev]
  440.  
  441. ; get chip version
  442.  
  443.         set_io  0
  444.         set_io  REG_TXCONFIG + 2
  445.         in      ax , dx
  446.         shr     ah , 2
  447.         shr     ax , 6
  448.         and     al , 01111111b
  449.  
  450.         mov     ecx, HW_VER_ARRAY_SIZE-1
  451.   .chip_ver_loop:
  452.         cmp     al , [hw_ver_array + ecx]
  453.         je      .chip_ver_found
  454.         dec     ecx
  455.         jns     .chip_ver_loop
  456.   .unknown:
  457.         mov     ecx, 8
  458.   .chip_ver_found:
  459.         cmp     ecx, 8
  460.         jg      .unknown
  461.  
  462.         mov     [device.hw_ver_id], cl
  463.  
  464.         mov     ecx, [crosslist + ecx*4]
  465.         mov     [device.name], ecx
  466.  
  467.         DEBUGF  2,"Chip version: %s\n", ecx
  468.  
  469. ; wake up the chip
  470.  
  471.         set_io  0
  472.         set_io  REG_HLTCLK
  473.         mov     al , 'R' ; run the clock
  474.         out     dx , al
  475.  
  476. ; unlock config and BMCR registers
  477.  
  478.         set_io  REG_9346CR
  479.         mov     al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  480.         out     dx , al
  481.  
  482. ; enable power management
  483.  
  484.         set_io  REG_CONFIG1
  485.         in      al , dx
  486.         cmp     [device.hw_ver_id], IDX_RTL8139B
  487.         jl      .old_chip
  488.  
  489. ; set LWAKE pin to active high (default value).
  490. ; it is for Wake-On-LAN functionality of some motherboards.
  491. ; this signal is used to inform the motherboard to execute a wake-up process.
  492. ; only at newer chips.
  493.  
  494.         or      al , (1 shl BIT_PMEn)
  495.         and     al , not (1 shl BIT_LWACT)
  496.         out     dx , al
  497.  
  498.         set_io  REG_CONFIG4
  499.         in      al , dx
  500.         and     al , not (1 shl BIT_LWPTN)
  501.         out     dx , al
  502.  
  503.         jmp     .finish_wake_up
  504.   .old_chip:
  505.  
  506. ; wake up older chips
  507.  
  508.         and     al , not ((1 shl BIT_SLEEP) or (1 shl BIT_PWRDWN))
  509.         out     dx , al
  510.   .finish_wake_up:
  511.  
  512. ; lock config and BMCR registers
  513.  
  514.         xor     al , al
  515.         set_io  0
  516.         set_io  REG_9346CR
  517.         out     dx , al
  518.         DEBUGF  2,"done!\n"
  519.  
  520.  
  521. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  522. ;;
  523. ;;   reset: Set up all registers and descriptors, clear some values
  524. ;;
  525. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  526.  
  527. reset:
  528.         DEBUGF  2,"Resetting rtl8139: "
  529.  
  530. ; attach int handler
  531.  
  532.         movzx   eax, [device.irq_line]
  533.         DEBUGF  1,"Attaching int handler to irq %x, ",eax:1
  534.         stdcall AttachIntHandler, eax, int_handler, dword 0
  535.         test    eax, eax
  536.         jnz     @f
  537.         DEBUGF  1,"\nCould not attach int handler!\n"
  538. ;        or      eax, -1
  539. ;        ret
  540.   @@:
  541.  
  542. ; reset chip
  543.  
  544.         DEBUGF  1,"Resetting chip\n"
  545.         set_io  0
  546.         set_io  REG_COMMAND
  547.         mov     al , 1 shl BIT_RST
  548.         out     dx , al
  549.         mov     cx , 1000               ; wait no longer for the reset
  550.   .wait_for_reset:
  551.         in      al , dx
  552.         test    al , 1 shl BIT_RST
  553.         jz      .reset_completed        ; RST remains 1 during reset
  554.         dec     cx
  555.         jns     .wait_for_reset
  556.   .reset_completed:
  557.  
  558. ; unlock config and BMCR registers
  559.  
  560.         set_io  REG_9346CR
  561.         mov     al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  562.         out     dx , al
  563.  
  564. ; initialize multicast registers (no filtering)
  565.  
  566.         mov     eax, 0xffffffff
  567.         set_io  REG_MAR0
  568.         out     dx , eax
  569.         set_io  REG_MAR4
  570.         out     dx , eax
  571.  
  572. ; enable Rx/Tx
  573.  
  574.         mov     al , (1 shl BIT_RE) or (1 shl BIT_TE)
  575.         set_io  REG_COMMAND
  576.         out     dx , al
  577.  
  578. ; 32k Rxbuffer, unlimited dma burst, no wrapping, no rx threshold
  579. ; accept broadcast packets, accept physical match packets
  580.  
  581.         mov     ax , RX_CONFIG
  582.         set_io  REG_RXCONFIG
  583.         out     dx , ax
  584.  
  585.  
  586. ; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
  587.  
  588.         mov     eax , (TX_MXDMA shl BIT_TX_MXDMA) or (TXRR shl BIT_TXRR) or BIT_IFG1 or BIT_IFG0
  589.         set_io  REG_TXCONFIG
  590.         out     dx , eax
  591.  
  592. ; enable auto negotiation
  593.  
  594.         set_io  REG_BMCR
  595.         in      ax , dx
  596.         or      ax , (1 shl BIT_ANE)
  597.         out     dx , ax
  598.  
  599. ; set auto negotiation advertisement
  600.  
  601.         set_io  REG_ANAR
  602.         in      ax , dx
  603.         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)
  604.         out     dx , ax
  605.  
  606. ; lock config and BMCR registers
  607.  
  608.         xor     eax, eax
  609.         set_io  REG_9346CR
  610.         out     dx , al
  611.  
  612. ; init RX/TX pointers
  613.  
  614.         mov     [device.rx_data_offset], eax
  615.         mov     [device.curr_tx_desc], al
  616.  
  617. ; clear packet/byte counters
  618.  
  619.         lea     edi, [device.bytes_tx]
  620.         mov     ecx, 6
  621.         rep     stosd
  622.  
  623. ; clear missing packet counter
  624.  
  625.         set_io  REG_MPC
  626.         out     dx , eax
  627.  
  628. ; Set up the 4 Txbuffer descriptors
  629.  
  630.         set_io  REG_TSAD0
  631.         mov     eax, [device.tx_buffer]
  632.         mov     ecx, 4
  633.   .loop:
  634.         push    eax
  635.         call    GetPgAddr
  636.         DEBUGF  1,"Desc: %x ", eax
  637.         out     dx , eax
  638.         add     dx , 4
  639.         pop     eax
  640.         add     eax, TX_BUF_SIZE
  641.         loop    .loop
  642.  
  643. ; set RxBuffer address, init RX buffer offset
  644.  
  645.         mov     eax, [device.rx_buffer]
  646.         call    GetPgAddr
  647.         set_io  0
  648.         set_io  REG_RBSTART
  649.         out     dx , eax
  650.  
  651. ; enable interrupts
  652.  
  653.         mov     eax, INTERRUPT_MASK
  654.         set_io  REG_IMR
  655.         out     dx , ax
  656.  
  657. ; Read MAC address
  658.  
  659.         call    read_mac
  660.  
  661. ; Indicate that we have successfully reset the card
  662.  
  663.         DEBUGF  2,"Done!\n"
  664.         xor     eax, eax
  665.  
  666.         ret
  667.  
  668.  
  669. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  670. ;;                                         ;;
  671. ;; Transmit                                ;;
  672. ;;                                         ;;
  673. ;; In: buffer pointer in [esp+4]           ;;
  674. ;;     size of buffer in [esp+8]           ;;
  675. ;;     pointer to device structure in ebx  ;;
  676. ;;                                         ;;
  677. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  678.  
  679. align 4
  680. transmit:
  681.         DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n",[esp+4],[esp+8]
  682.         mov     eax, [esp+4]
  683.         DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  684.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  685.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  686.         [eax+13]:2,[eax+12]:2
  687.  
  688.         cmp     dword [esp+8], MAX_ETH_FRAME_SIZE
  689.         jg      .finish                         ; packet is too long
  690.         cmp     dword [esp+8], 60
  691.         jl      .finish                         ; packet is too short
  692.  
  693. ; check descriptor
  694.         DEBUGF  1,"Checking descriptor, "
  695.         movzx   ecx, [device.curr_tx_desc]
  696.         mov     edx, [device.io_addr]
  697.         lea     edx, [edx+ecx*4+REG_TSD0]
  698.         in      ax, dx
  699.         test    ax, 0x1fff ; or no size given
  700.         jz      .send_packet
  701.         and     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  702.         cmp     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  703.         jz      .send_packet
  704. ; wait for timeout
  705.         DEBUGF  1,"Waiting for timeout, "
  706.  
  707.         push    edx ebx                          ; TODO : rtl8139 internal timer should be used instead
  708.         stdcall Sleep, TX_TIMEOUT                ; ? What registers does this destroy ?
  709.         pop     ebx edx
  710.  
  711.         in      ax, dx
  712.         and     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  713.         cmp     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  714.         jz      .send_packet                     ; if chip hung, reset it
  715.         push    dx
  716.         call    reset                            ; reset the card
  717.         pop     dx
  718. .send_packet:
  719.         DEBUGF  1,"Sending packet, "
  720.  
  721.         push    edx
  722.         movzx   eax, [device.curr_tx_desc]   ; calculate the current tx_buffer address
  723.         mov     edx, TX_BUF_SIZE ;MAX_ETH_FRAME_SIZE          ;
  724.         mul     edx                              ;
  725.         mov     edi, [device.tx_buffer]      ;
  726.         add     edi, eax                         ; Store it in edi
  727.         pop     edx
  728.  
  729.         mov     esi, [esp+4]                     ; Copy data to that address
  730.         mov     ecx, [esp+8]                     ;
  731.         shr     ecx, 2                           ;
  732.         rep     movsd                            ;
  733.         mov     ecx, [esp+8]                     ;
  734.         and     ecx, 3                           ;
  735.         rep     movsb                            ;
  736.  
  737.         inc     [device.packets_tx]          ;
  738.         mov     eax, [esp+8]                     ; Get packet size in eax
  739.  
  740.         add     dword [device.bytes_tx], eax
  741.         adc     dword [device.bytes_tx + 4], 0
  742.  
  743. ;        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)
  744.         out     dx , eax                         ;
  745.  
  746. ; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
  747.         inc     [device.curr_tx_desc]
  748.         and     [device.curr_tx_desc], 3
  749.  
  750.         DEBUGF  1," - Packet Sent! "
  751. .finish:
  752.         DEBUGF  1," - Done!\n"
  753.         ret
  754.  
  755.  
  756.  
  757.  
  758.  
  759. ;;;;;;;;;;;;;;;;;;;;;;;
  760. ;;                   ;;
  761. ;; Interrupt handler ;;
  762. ;;                   ;;
  763. ;;;;;;;;;;;;;;;;;;;;;;;
  764.  
  765. align 4
  766. int_handler:
  767.  
  768.         DEBUGF  1,"IRQ %x ",eax:2                   ; no, you cant replace 'eax:2' with 'al', this must be a bug in FDO
  769.  
  770. ; find pointer of device wich made IRQ occur
  771.  
  772.         mov     esi, RTL8139_LIST
  773.         mov     ecx, [RTL8139_DEV]
  774.         test    ecx, ecx
  775.         jz      .fail
  776. .nextdevice:
  777.         mov     ebx, dword [esi]
  778.  
  779.         set_io  0
  780.         set_io  REG_ISR
  781.         in      ax , dx
  782.         out     dx , ax                             ; send it back to ACK
  783.  
  784.         add     esi, 4
  785.  
  786.         test    ax , ax
  787.         jnz     .got_it
  788.  
  789.         dec     ecx
  790.         jnz     .nextdevice
  791.  
  792.         ret                                         ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
  793.  
  794.   .got_it:
  795.  
  796. ; looks like we've found it!
  797.  
  798. ; Lets found out why the irq occured then..
  799.  
  800. ;----------------------------------------------------
  801. ; Received packet ok?
  802.  
  803.         test    ax, ISR_ROK
  804.         jz      @f
  805.         push    ax
  806.  
  807.   .receive:
  808.         set_io  0
  809.         set_io  REG_COMMAND
  810.         in      al , dx
  811.         test    al , BUFE                           ; test if RX buffer is empty
  812.         jnz     .finish                             ;
  813.  
  814.         DEBUGF  1,"RX: "
  815.  
  816.         mov     eax, [device.rx_buffer]
  817.         add     eax, [device.rx_data_offset]
  818.         test    byte [eax], (1 shl BIT_ROK)         ; check if packet is ok
  819.         jz      .reset_rx
  820.  
  821. ; packet is ok, copy it
  822.         movzx   ecx, word [eax+2]                   ; packet length
  823.  
  824. ; Update stats
  825.         add     dword [device.bytes_rx], ecx
  826.         adc     dword [device.bytes_rx + 4], 0
  827.         inc     dword [device.packets_rx]
  828.  
  829.         sub     ecx, 4                              ; don't copy CRC
  830.  
  831.         DEBUGF  1,"Received %u bytes\n", ecx
  832.  
  833.         push    ebx eax ecx
  834.         stdcall KernelAlloc, ecx                    ; Allocate a buffer to put packet into
  835.         pop     ecx
  836.         test    eax, eax                            ; Test if we allocated succesfully
  837.         jz      .abort
  838.  
  839.  
  840.         mov     edi, eax                            ; Where we will copy too
  841.  
  842.         mov     esi, [esp]                          ; The buffer we will copy from
  843.         add     esi, 4                              ; Dont copy CRC
  844.  
  845.         push    dword .abort                        ; Kernel will return to this address after EthReceiver
  846.         push    ecx edi                             ; Save buffer pointer and size, to pass to kernel
  847.  
  848.   .copy:
  849.         shr     ecx, 1
  850.         jnc     .nb
  851.         movsb
  852.   .nb:
  853.         shr     ecx, 1
  854.         jnc     .nw
  855.         movsw
  856.   .nw:
  857.         jz      .nd
  858.         rep     movsd
  859.   .nd:
  860.  
  861.         jmp     EthReceiver                         ; Send it to kernel
  862.  
  863.  
  864.   .abort:
  865.         pop     eax ebx
  866.                                                     ; update eth_data_start_offset
  867.         movzx   eax, word [eax+2]                   ; packet length
  868.         add     eax, [device.rx_data_offset]
  869.         add     eax, 4+3                            ; packet header is 4 bytes long + dword alignment
  870.         and     eax, not 3                          ; dword alignment
  871.  
  872.         cmp     eax, RX_BUFFER_SIZE
  873.         jl      .no_wrap
  874.         DEBUGF  2,"Wrapping"
  875.         sub     eax, RX_BUFFER_SIZE
  876.   .no_wrap:
  877.         mov     [device.rx_data_offset], eax
  878.         DEBUGF  1,"New RX ptr: %d ", eax
  879.  
  880.         set_io  0
  881.         set_io  REG_CAPR                            ; update 'Current Address of Packet Read register'
  882.         sub     eax, 0x10                           ; value 0x10 is a constant for CAPR
  883.         out     dx , ax
  884.  
  885.         jmp     .receive                            ; check for multiple packets
  886.  
  887.   .reset_rx:
  888.         test    byte [eax], (1 shl BIT_CRC)
  889.         jz      .no_crc_error
  890.         DEBUGF  2,"\nCRC error!\n"
  891.  
  892.   .no_crc_error:
  893.         test    byte [eax], (1 shl BIT_FAE)
  894.         jz      .no_fae_error
  895.         DEBUGF  1,"\nFrame alignment error!\n"
  896.  
  897.   .no_fae_error:
  898.         DEBUGF  1,"Reset RX\n"
  899.         in      al , dx                             ; read command register
  900.         push    ax
  901.  
  902.         and     al , not (1 shl BIT_RE)             ; Clear the RE bit
  903.         out     dx , al
  904.  
  905.         pop     ax
  906.         out     dx , al                             ; write original command back
  907.  
  908.         add     edx, REG_RXCONFIG - REG_COMMAND     ; Restore RX configuration
  909.         mov     ax , RX_CONFIG
  910.         out     dx , ax
  911.  
  912.   .finish:
  913.         pop     ax
  914.  
  915. ;----------------------------------------------------
  916. ; Transmit error ?
  917.  
  918.   @@:
  919.         test    ax, ISR_TER
  920.         jz      @f
  921.  
  922.         push    ax
  923.         cmp     [device.curr_tx_desc], 4
  924.         jz      .notxd
  925.  
  926.         set_io  0
  927.         movzx   ecx, [device.curr_tx_desc]
  928.         lea     edx, [edx+ecx*4+REG_TSD0]
  929.         in      eax, dx
  930.  
  931.   .notxd:
  932.         test    eax, TSR_TUN
  933.         jz      .nobun
  934.         DEBUGF  2, "TX: FIFO Buffer underrun!\n"
  935.  
  936.   .nobun:
  937.         test    eax, TSR_OWC
  938.         jz      .noowc
  939.         DEBUGF  2, "TX: OWC!\n"
  940.  
  941.   .noowc:
  942.         test    eax, TSR_TABT
  943.         jz      .notabt
  944.         DEBUGF  2, "TX: TABT!\n"
  945.  
  946.   .notabt:
  947.         test    eax, TSR_CRS
  948.         jz      .nocsl
  949.         DEBUGF  2, "TX: Carrier Sense Lost!\n"
  950.  
  951.   .nocsl:
  952. ;                test    eax, TSR_OWN or TSR_TOK
  953. ;                jz      .nofd
  954. ;                DEBUGF  1, "TX: Transmit OK (desc: %u)\n", ecx
  955. ;
  956. ;               .nofd:
  957.         pop     ax
  958.  
  959. ;----------------------------------------------------
  960. ; Transmit ok ?
  961.  
  962.   @@:
  963.         test    ax, ISR_TOK
  964.         jz      @f
  965.  
  966.         DEBUGF  1, "TX: Transmit OK (desc: %u)\n", [device.curr_tx_desc]:1
  967.  
  968. ;----------------------------------------------------
  969. ; Rx buffer overflow ?
  970.  
  971.   @@:
  972.         test    ax, ISR_RXOVW
  973.         jz      @f
  974.  
  975.         push    ax
  976.         DEBUGF  2,"RX-buffer overflow!\n"
  977.  
  978.         mov     edx, [device.io_addr]
  979.         add     edx, REG_ISR
  980.         mov     ax , ISR_FIFOOVW or ISR_RXOVW
  981.         out     dx , ax
  982.         pop     ax
  983.  
  984. ;----------------------------------------------------
  985. ; Packet underrun? ?
  986.  
  987.  
  988.   @@:
  989.         test    ax, ISR_PUN
  990.         jz      @f
  991.  
  992.         DEBUGF  2,"Packet underrun!\n"
  993.  
  994. ;----------------------------------------------------
  995. ; Receive FIFO overflow ?
  996.  
  997.   @@:
  998.         test    ax, ISR_FIFOOVW
  999.         jz      @f
  1000.  
  1001.         push    ax
  1002.         DEBUGF  2,"RX fifo overflox!\n"
  1003.  
  1004.         mov     edx, [device.io_addr]
  1005.         add     edx, REG_ISR
  1006.         mov     ax , ISR_FIFOOVW or ISR_RXOVW
  1007.         out     dx , ax
  1008.         pop     ax
  1009.  
  1010. ;----------------------------------------------------
  1011. ; Something about Cable changed ?
  1012.  
  1013.   @@:
  1014.         test    ax, ISR_LENCHG
  1015.         jz      .fail
  1016.  
  1017.         DEBUGF  2,"Cable changed!\n"
  1018.         call    cable
  1019.  
  1020. ; If none of the above events happened, just exit clearing int
  1021.  
  1022.   .fail:
  1023.  
  1024.         DEBUGF  1,"\n"
  1025.         ret
  1026.  
  1027.  
  1028.  
  1029.  
  1030. ;;;;;;;;;;;;;;;;;;;;;;;;;
  1031. ;;                     ;;
  1032. ;; Update Cable status ;;
  1033. ;;                     ;;
  1034. ;;;;;;;;;;;;;;;;;;;;;;;;;
  1035.  
  1036. align 4
  1037. cable:
  1038.         DEBUGF  1,"Checking Cable status: "
  1039.  
  1040.         mov     edx, dword [device.io_addr]
  1041.         add     edx, REG_MSR
  1042.         in      al , dx
  1043.  
  1044. ;        test    al , 1 SHL 2     ; 0 = link ok 1 = link fail
  1045. ;        jnz     .notconnected
  1046.  
  1047. ;        test    al , 1 SHL 3     ; 0 = 100 Mbps 1 = 10 Mbps
  1048. ;        jnz     .10mbps
  1049.  
  1050.         shr     al, 2
  1051.         and     al, 3
  1052.  
  1053.         mov     byte [device.mode+3], al
  1054.         DEBUGF  1,"Done!\n"
  1055. ret
  1056.  
  1057.  
  1058.  
  1059. ;;;;;;;;;;;;;;;;;;;;;;;
  1060. ;;                   ;;
  1061. ;; Write MAC address ;;
  1062. ;;                   ;;
  1063. ;;;;;;;;;;;;;;;;;;;;;;;
  1064.  
  1065. align 4
  1066. write_mac:      ; in: mac pushed onto stack (as 3 words)
  1067.  
  1068.         DEBUGF  2,"Writing MAC: "
  1069.  
  1070. ; disable all in command registers
  1071.  
  1072.         set_io  0
  1073.         set_io  REG_9346CR
  1074.         xor     eax, eax
  1075.         out     dx , al
  1076.  
  1077.         set_io  REG_IMR
  1078.         xor     eax, eax
  1079.         out     dx , ax
  1080.  
  1081.         set_io  REG_ISR
  1082.         mov     eax, -1
  1083.         out     dx , ax
  1084.  
  1085. ; enable writing
  1086.  
  1087.         set_io  REG_9346CR
  1088.         mov     eax, REG_9346CR_WE
  1089.         out     dx , al
  1090.  
  1091.  ; write the mac ...
  1092.  
  1093.         set_io  REG_IDR0
  1094.         pop     eax
  1095.         out     dx , eax
  1096.  
  1097.         set_io  REG_IDR0+4
  1098.         xor     eax, eax
  1099.         pop     ax
  1100.         out     dx , eax
  1101.  
  1102. ; disable writing
  1103.  
  1104.         set_io  REG_9346CR
  1105.         xor     eax, eax
  1106.         out     dx , al
  1107.  
  1108.         DEBUGF  2,"ok!\n"
  1109.  
  1110. ; Notice this procedure does not ret, but continues to read_mac instead.
  1111.  
  1112.  
  1113. ;;;;;;;;;;;;;;;;;;;;;;
  1114. ;;                  ;;
  1115. ;; Read MAC address ;;
  1116. ;;                  ;;
  1117. ;;;;;;;;;;;;;;;;;;;;;;
  1118.  
  1119. read_mac:
  1120.         DEBUGF  2,"Reading MAC: "
  1121.  
  1122.         set_io  0
  1123.         lea     edi, [device.mac]
  1124.         in      eax, dx
  1125.         stosd
  1126.         add     edx, 4
  1127.         in      ax, dx
  1128.         stosw
  1129.  
  1130.         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
  1131.  
  1132.         ret
  1133.  
  1134.  
  1135.  
  1136. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1137. ;;                                                                      ;;
  1138. ;; Read eeprom (type 93c46 and 93c56)                                   ;;
  1139. ;;                                                                      ;;
  1140. ;; In: word to be read in al (6bit in case of 93c46 and 8bit otherwise) ;;
  1141. ;;     pointer to device structure in ebx                               ;;
  1142. ;;                                                                      ;;
  1143. ;; OUT: word read in ax                                                 ;;
  1144. ;;                                                                      ;;
  1145. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1146.  
  1147. align 4
  1148. read_eeprom:
  1149.         DEBUGF  2,"Reading eeprom, "
  1150.  
  1151.         set_io  0
  1152.         push    ebx
  1153.         movzx   ebx, al
  1154.         set_io  REG_RXCONFIG
  1155.         in      al, dx
  1156.         test    al, (1 shl BIT_9356SEL)
  1157.         jz      .type_93c46
  1158. ;       and     bl, 01111111b ; don't care first bit
  1159.         or      bx, EE_93C56_READ_CMD           ; it contains start bit
  1160.         mov     cx, EE_93C56_CMD_LENGTH-1       ; cmd_loop counter
  1161.         jmp     .read_eeprom
  1162. .type_93c46:
  1163.         and     bl, 00111111b
  1164.         or      bx, EE_93C46_READ_CMD           ; it contains start bit
  1165.         mov     cx, EE_93C46_CMD_LENGTH-1       ; cmd_loop counter
  1166. .read_eeprom:
  1167.         set_io  REG_9346CR
  1168. ;       mov     al, (1 shl BIT_93C46_EEM1)
  1169. ;       out     dx, al
  1170.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) ; wake up the eeprom
  1171.         out     dx, al
  1172. .cmd_loop:
  1173.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1174.         bt      bx, cx
  1175.         jnc     .zero_bit
  1176.         or      al, (1 shl BIT_93C46_EEDI)
  1177. .zero_bit:
  1178.         out     dx, al
  1179. ;       push    eax
  1180. ;       in      eax, dx ; eeprom delay
  1181. ;       pop     eax
  1182.         or      al, (1 shl BIT_93C46_EESK)
  1183.         out     dx, al
  1184. ;       in      eax, dx ; eeprom delay
  1185.         dec     cx
  1186.         jns     .cmd_loop
  1187. ;       in      eax, dx ; eeprom delay
  1188.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1189.         out     dx, al
  1190.         mov     cl, 0xf
  1191. .read_loop:
  1192.         shl     ebx, 1
  1193.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS) or (1 shl BIT_93C46_EESK)
  1194.         out     dx, al
  1195. ;       in      eax, dx ; eeprom delay
  1196.         in      al, dx
  1197.         and     al, (1 shl BIT_93C46_EEDO)
  1198.         jz      .dont_set
  1199.         inc     ebx
  1200. .dont_set:
  1201.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EECS)
  1202.         out     dx, al
  1203. ;       in      eax, dx ; eeprom delay
  1204.         dec     cl
  1205.         jns     .read_loop
  1206.         xor     al, al
  1207.         out     dx, al
  1208.         mov     ax, bx
  1209.         pop     ebx
  1210.  
  1211.         ret
  1212.  
  1213.  
  1214. ; End of code
  1215.  
  1216. align 4                                         ; Place all initialised data here
  1217.  
  1218. RTL8139_DEV   dd 0
  1219. version       dd (5 shl 16) or (API_VERSION and 0xFFFF)
  1220. my_service    db 'RTL8139',0                    ; max 16 chars include zero
  1221.  
  1222. device_1      db 'Realtek 8139',0
  1223. device_2      db 'Realtek 8139A',0
  1224. device_3      db 'Realtek 8139B',0
  1225. device_4      db 'Realtek 8139C',0
  1226. device_5      db 'Realtek 8100',0
  1227. device_6      db 'Realtek 8139D',0
  1228. device_7      db 'Realtek 8139CP',0
  1229. device_8      db 'Realtek 8101',0
  1230. device_unknown db 'Unknown RTL8139 clone', 0
  1231.  
  1232. crosslist     dd device_1
  1233.               dd device_2
  1234.               dd device_3
  1235.               dd device_4
  1236.               dd device_5
  1237.               dd device_6
  1238.               dd device_7
  1239.               dd device_8
  1240.               dd device_unknown
  1241.  
  1242. 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
  1243.               db VER_RTL8139A
  1244.               db VER_RTL8139B
  1245.               db VER_RTL8139C
  1246.               db VER_RTL8100
  1247.               db VER_RTL8139D
  1248.               db VER_RTL8139CP
  1249.               db VER_RTL8101
  1250.               db 0
  1251.  
  1252. HW_VER_ARRAY_SIZE = $-hw_ver_array
  1253.  
  1254. include_debug_strings                           ; All data wich FDO uses will be included here
  1255.  
  1256. section '.data' data readable writable align 16 ; place all uninitialized data place here
  1257.  
  1258. RTL8139_LIST rd MAX_RTL8139                     ; This list contains all pointers to device structures the driver is handling
  1259.  
  1260.