Subversion Repositories Kolibri OS

Rev

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