Subversion Repositories Kolibri OS

Rev

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