Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;; Realtek 8139 driver for KolibriOS                               ;;
  7. ;;                                                                 ;;
  8. ;;    Written by hidnplayr@kolibrios.org                           ;;
  9. ;;                                                                 ;;
  10. ;;     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. <<<<<<< .mine
  287.         mov     eax, [IOCTL.io_code]
  288. =======
  289.         mov     eax, [edx+IOCTL.io_code]
  290. >>>>>>> .r1471
  291.  
  292. ;------------------------------------------------------
  293.  
  294.         cmp     eax, 0 ;SRV_GETVERSION
  295.         jne     @F
  296.  
  297.         cmp     [IOCTL.out_size], 4
  298.         jl      .fail
  299.         mov     eax, [IOCTL.output]
  300.         mov     [eax], dword API_VERSION
  301.  
  302.         xor     eax, eax
  303.         ret
  304.  
  305. ;------------------------------------------------------
  306.   @@:
  307.         cmp     eax, 1 ;SRV_HOOK
  308.         jne     .fail
  309.  
  310.         cmp     [IOCTL.inp_size], 3               ; Data input must be at least 3 bytes
  311.         jl      .fail
  312.  
  313.         mov     eax, [IOCTL.input]
  314.         cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
  315.         jne     .fail                                   ; other types arent supported for this card yet
  316.  
  317. ; check if the device is already listed
  318.  
  319.         mov     esi, RTL8139_LIST
  320.         mov     ecx, [RTL8139_DEV]
  321.         test    ecx, ecx
  322.         jz      .firstdevice
  323.  
  324. ;        mov     eax, [IOCTL.input]                      ; get the pci bus and device numbers
  325.         mov     ax , [eax+1]                            ;
  326.   .nextdevice:
  327.         mov     ebx, [esi]
  328.         cmp     ax , word [device.pci_bus]              ; compare with pci and device num in device list (notice the usage of word instead of byte)
  329.         je      .find_devicenum                         ; Device is already loaded, let's find it's device number
  330.         add     esi, 4
  331.         loop    .nextdevice
  332.  
  333.  
  334. ; This device doesnt have its own eth_device structure yet, lets create one
  335.   .firstdevice:
  336.         cmp     [RTL8139_DEV], MAX_RTL8139              ; First check if the driver can handle one more card
  337.         jge     .fail
  338.  
  339.         push    edx
  340.         stdcall KernelAlloc, dword device.size          ; Allocate the buffer for eth_device structure
  341.         pop     edx
  342.         test    eax, eax
  343.         jz      .fail
  344.         mov     ebx, eax                                ; ebx is always used as a pointer to the structure (in driver, but also in kernel code)
  345.  
  346. ; Fill in the direct call addresses into the struct
  347.  
  348.         mov     dword [device.reset], reset
  349.         mov     dword [device.transmit], transmit
  350.         mov     dword [device.get_MAC], read_mac
  351.         mov     dword [device.set_MAC], write_mac
  352.         mov     dword [device.unload], unload
  353.         mov     dword [device.name], my_service
  354.  
  355. ; save the pci bus and device numbers
  356.  
  357.         mov     eax, [IOCTL.input]
  358.         mov     cl , [eax+1]
  359.         mov     [device.pci_bus], cl
  360.         mov     cl , [eax+2]
  361.         mov     [device.pci_dev], cl
  362.  
  363. ; Now, it's time to find the base io addres of the PCI device
  364. ; TODO: implement check if bus and dev exist on this machine
  365.  
  366.         find_io [device.pci_bus], [device.pci_dev], [device.io_addr]
  367.  
  368. ; We've found the io address, find IRQ now
  369.  
  370.         movzx   eax, byte [device.pci_bus]
  371.         movzx   ecx, byte [device.pci_dev]
  372.         push    ebx
  373.         stdcall PciRead8, eax ,ecx ,0x3c                                ; 0x3c is the offset where irq can be found
  374.         pop     ebx
  375.         mov     byte [device.irq_line], al
  376.  
  377.         DEBUGF  2,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
  378.         [device.pci_dev]:1,[device.pci_bus]:1,[device.irq_line]:1,[device.io_addr]:4
  379.  
  380.  
  381.         allocate_and_clear [device.rx_buffer], (RX_BUFFER_SIZE+MAX_ETH_FRAME_SIZE), .err
  382.         allocate_and_clear [device.tx_buffer], (TX_BUF_SIZE*NUM_TX_DESC), .err
  383.  
  384. ; Ok, the eth_device structure is ready, let's probe the device
  385.  
  386.         call    probe                                                   ; this function will output in eax
  387.         test    eax, eax
  388.         jnz     .err                                                    ; If an error occured, exit
  389.  
  390.         mov     eax, [RTL8139_DEV]                                      ; Add the device structure to our device list
  391.         mov     [RTL8139_LIST+4*eax], ebx                               ; (IRQ handler uses this list to find device)
  392.         inc     [RTL8139_DEV]                                           ;
  393.  
  394.  
  395.         call    EthRegDev
  396.         cmp     eax, -1
  397.         je      .destroy
  398.  
  399.         ret
  400.  
  401. ; If the device was already loaded, find the device number and return it in eax
  402.  
  403.   .find_devicenum:
  404.         DEBUGF  2,"Trying to find device number of already registered device\n"
  405.         mov     ebx, eax
  406.         call    EthStruc2Dev                                            ; This kernel procedure converts a pointer to device struct in ebx
  407.                                                                         ; into a device number in edi
  408.         mov     eax, edi                                                ; Application wants it in eax instead
  409.         DEBUGF  2,"Kernel says: %u\n", eax
  410.         ret
  411.  
  412. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  413.  
  414.   .destroy:
  415.         ; todo: reset device into virgin state
  416.  
  417.   .err:
  418.         stdcall KernelFree, dword [device.rx_buffer]
  419.         stdcall KernelFree, dword [device.tx_buffer]
  420.         stdcall KernelFree, ebx
  421.  
  422.  
  423.   .fail:
  424.         or      eax, -1
  425.         ret
  426.  
  427. ;------------------------------------------------------
  428. endp
  429.  
  430.  
  431. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  432. ;;                                                                        ;;
  433. ;;        Actual Hardware dependent code starts here                      ;;
  434. ;;                                                                        ;;
  435. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  436.  
  437. align 4
  438. unload:
  439.         ; TODO: (in this particular order)
  440.         ;
  441.         ; - Stop the device
  442.         ; - Detach int handler
  443.         ; - Remove device from local list (RTL8139_LIST)
  444.         ; - call unregister function in kernel
  445.         ; - Remove all allocated structures and buffers the card used
  446.  
  447.         or      eax,-1
  448.  
  449. ret
  450.  
  451.  
  452. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  453. ;;
  454. ;;  probe: enables the device (if it really is RTL8139)
  455. ;;
  456. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  457.  
  458. align 4
  459. probe:
  460.         DEBUGF  2,"Probing rtl8139 device: "
  461.  
  462.         make_bus_master [device.pci_bus], [device.pci_dev]
  463.  
  464. ; get chip version
  465.  
  466.         set_io  0
  467.         set_io  REG_TXCONFIG + 2
  468.         in      ax , dx
  469.         shr     ah , 2
  470.         shr     ax , 6
  471.         and     al , 01111111b
  472.         mov     ecx, HW_VER_ARRAY_SIZE-1
  473.   .chip_ver_loop:
  474.         cmp     al , [hw_ver_array+ecx]
  475.         je      .chip_ver_found
  476.         dec     ecx
  477.         jns     .chip_ver_loop
  478.         xor     cl , cl ; default RTL8139
  479.   .chip_ver_found:
  480.         mov     [device.hw_ver_id], cl
  481.  
  482.         shl     ecx, 2
  483.         add     ecx, crosslist
  484.         mov     ecx, [ecx]
  485.         mov     [device.name], ecx
  486.  
  487.         DEBUGF  2,"Chip version: %s\n",ecx
  488.  
  489. ; wake up the chip
  490.  
  491.         set_io  0
  492.         set_io  REG_HLTCLK
  493.         mov     al , 'R' ; run the clock
  494.         out     dx , al
  495.  
  496. ; unlock config and BMCR registers
  497.  
  498.         set_io  REG_9346CR
  499.         mov     al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  500.         out     dx , al
  501.  
  502. ; enable power management
  503.  
  504.         set_io  REG_CONFIG1
  505.         in      al , dx
  506.         cmp     [device.hw_ver_id], IDX_RTL8139B
  507.         jl      .old_chip
  508.  
  509. ; set LWAKE pin to active high (default value).
  510. ; it is for Wake-On-LAN functionality of some motherboards.
  511. ; this signal is used to inform the motherboard to execute a wake-up process.
  512. ; only at newer chips.
  513.  
  514.         or      al , (1 shl BIT_PMEn)
  515.         and     al , not (1 shl BIT_LWACT)
  516.         out     dx , al
  517.  
  518.         set_io  REG_CONFIG4
  519.         in      al , dx
  520.         and     al , not (1 shl BIT_LWPTN)
  521.         out     dx , al
  522.  
  523.         jmp     .finish_wake_up
  524.   .old_chip:
  525.  
  526. ; wake up older chips
  527.  
  528.         and     al , not ((1 shl BIT_SLEEP) or (1 shl BIT_PWRDWN))
  529.         out     dx , al
  530.   .finish_wake_up:
  531.  
  532. ; lock config and BMCR registers
  533.  
  534.         xor     al , al
  535.         set_io  0
  536.         set_io  REG_9346CR
  537.         out     dx , al
  538.         DEBUGF  2,"done!\n"
  539.  
  540.  
  541. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  542. ;;
  543. ;;   reset: Set up all registers and descriptors, clear some values
  544. ;;
  545. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  546.  
  547. reset:
  548.         DEBUGF  2,"Resetting rtl8139: "
  549.  
  550. ; attach int handler
  551.  
  552.         movzx   eax, [device.irq_line]
  553.         DEBUGF  1,"Attaching int handler to irq %x, ",eax:1
  554.         stdcall AttachIntHandler, eax, int_handler, dword 0
  555.         test    eax, eax
  556.         jnz     @f
  557.         DEBUGF  1,"\nCould not attach int handler!\n"
  558. ;        or      eax, -1
  559. ;        ret
  560.   @@:
  561.  
  562. ; reset chip
  563.  
  564.         DEBUGF  1,"Resetting chip\n"
  565.         set_io  0
  566.         set_io  REG_COMMAND
  567.         mov     al , 1 shl BIT_RST
  568.         out     dx , al
  569.         mov     cx , 1000               ; wait no longer for the reset
  570.   .wait_for_reset:
  571.         in      al , dx
  572.         test    al , 1 shl BIT_RST
  573.         jz      .reset_completed        ; RST remains 1 during reset
  574.         dec     cx
  575.         jns     .wait_for_reset
  576.   .reset_completed:
  577.  
  578. ; unlock config and BMCR registers
  579.  
  580.         set_io  REG_9346CR
  581.         mov     al , (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  582.         out     dx , al
  583.  
  584. ; initialize multicast registers (no filtering)
  585.  
  586.         mov     eax, 0xffffffff
  587.         set_io  REG_MAR0
  588.         out     dx , eax
  589.         set_io  REG_MAR4
  590.         out     dx , eax
  591.  
  592. ; enable Rx/Tx
  593.  
  594.         mov     al , (1 shl BIT_RE) or (1 shl BIT_TE)
  595.         set_io  REG_COMMAND
  596.         out     dx , al
  597.  
  598. ; 32k Rxbuffer, unlimited dma burst, no wrapping, no rx threshold
  599. ; accept broadcast packets, accept physical match packets
  600.  
  601.         mov     ax , RX_CONFIG
  602.         set_io  REG_RXCONFIG
  603.         out     dx , ax
  604.  
  605.  
  606. ; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
  607.  
  608.         mov     eax , (TX_MXDMA shl BIT_TX_MXDMA) or (TXRR shl BIT_TXRR) or BIT_IFG1 or BIT_IFG0
  609.         set_io  REG_TXCONFIG
  610.         out     dx , eax
  611.  
  612. ; enable auto negotiation
  613.  
  614.         set_io  REG_BMCR
  615.         in      ax , dx
  616.         or      ax , (1 shl BIT_ANE)
  617.         out     dx , ax
  618.  
  619. ; set auto negotiation advertisement
  620.  
  621.         set_io  REG_ANAR
  622.         in      ax , dx
  623.         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)
  624.         out     dx , ax
  625.  
  626. ; lock config and BMCR registers
  627.  
  628.         xor     eax, eax
  629.         set_io  REG_9346CR
  630.         out     dx , al
  631.  
  632. ; init RX/TX pointers
  633.  
  634.         mov     [device.rx_data_offset], eax
  635.         mov     [device.curr_tx_desc], al
  636.  
  637. ; clear packet/byte counters
  638.  
  639.         lea     edi, [device.bytes_tx]
  640.         mov     ecx, 6
  641.         rep     stosd
  642.  
  643. ; clear missing packet counter
  644.  
  645.         set_io  REG_MPC
  646.         out     dx , eax
  647.  
  648. ; Set up the 4 Txbuffer descriptors
  649.  
  650.         set_io  REG_TSAD0
  651.         mov     eax, [device.tx_buffer]
  652.         mov     ecx, 4
  653.   .loop:
  654.         push    eax
  655.         call    GetPgAddr
  656.         DEBUGF  1,"Desc: %x ", eax
  657.         out     dx , eax
  658.         add     dx , 4
  659.         pop     eax
  660.         add     eax, TX_BUF_SIZE
  661.         loop    .loop
  662.  
  663. ; set RxBuffer address, init RX buffer offset
  664.  
  665.         mov     eax, [device.rx_buffer]
  666.         call    GetPgAddr
  667.         set_io  0
  668.         set_io  REG_RBSTART
  669.         out     dx , eax
  670.  
  671. ; enable interrupts
  672.  
  673.         mov     eax, INTERRUPT_MASK
  674.         set_io  REG_IMR
  675.         out     dx , ax
  676.  
  677. ; Read MAC address
  678.  
  679.         call    read_mac
  680.  
  681. ; Indicate that we have successfully reset the card
  682.  
  683.         DEBUGF  2,"Done!\n"
  684.         xor     eax, eax
  685.  
  686.         ret
  687.  
  688.  
  689. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  690. ;;                                         ;;
  691. ;; Transmit                                ;;
  692. ;;                                         ;;
  693. ;; In: buffer pointer in [esp+4]           ;;
  694. ;;     size of buffer in [esp+8]           ;;
  695. ;;     pointer to device structure in ebx  ;;
  696. ;;                                         ;;
  697. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  698.  
  699. align 4
  700. transmit:
  701.         DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n",[esp+4],[esp+8]
  702.         mov     eax, [esp+4]
  703.         DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  704.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  705.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  706.         [eax+13]:2,[eax+12]:2
  707.  
  708.         cmp     dword [esp+8], MAX_ETH_FRAME_SIZE
  709.         jg      .finish                         ; packet is too long
  710.         cmp     dword [esp+8], 60
  711.         jl      .finish                         ; packet is too short
  712.  
  713. ; check descriptor
  714.         DEBUGF  1,"Checking descriptor, "
  715.         movzx   ecx, [device.curr_tx_desc]
  716.         mov     edx, [device.io_addr]
  717.         lea     edx, [edx+ecx*4+REG_TSD0]
  718.         in      ax, dx
  719.         test    ax, 0x1fff ; or no size given
  720.         jz      .send_packet
  721.         and     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  722.         cmp     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  723.         jz      .send_packet
  724. ; wait for timeout
  725.         DEBUGF  1,"Waiting for timeout, "
  726.  
  727.         push    edx ebx                          ; TODO : rtl8139 internal timer should be used instead
  728.         stdcall Sleep, TX_TIMEOUT                ; ? What registers does this destroy ?
  729.         pop     ebx edx
  730.  
  731.         in      ax, dx
  732.         and     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  733.         cmp     ax, (1 shl BIT_TOK) or (1 shl BIT_OWN)
  734.         jz      .send_packet                     ; if chip hung, reset it
  735.         push    dx
  736.         call    reset                            ; reset the card
  737.         pop     dx
  738. .send_packet:
  739.         DEBUGF  1,"Sending packet, "
  740.  
  741.         push    edx
  742.         movzx   eax, [device.curr_tx_desc]   ; calculate the current tx_buffer address
  743.         mov     edx, TX_BUF_SIZE ;MAX_ETH_FRAME_SIZE          ;
  744.         mul     edx                              ;
  745.         mov     edi, [device.tx_buffer]      ;
  746.         add     edi, eax                         ; Store it in edi
  747.         pop     edx
  748.  
  749.         mov     esi, [esp+4]                     ; Copy data to that address
  750.         mov     ecx, [esp+8]                     ;
  751.         shr     ecx, 2                           ;
  752.         rep     movsd                            ;
  753.         mov     ecx, [esp+8]                     ;
  754.         and     ecx, 3                           ;
  755.         rep     movsb                            ;
  756.  
  757.         inc     [device.packets_tx]          ;
  758.         mov     eax, [esp+8]                     ; Get packet size in eax
  759.  
  760.         add     dword [device.bytes_tx], eax
  761.         adc     dword [device.bytes_tx + 4], 0
  762.  
  763. ;        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)
  764.         out     dx , eax                         ;
  765.  
  766. ; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
  767.         inc     [device.curr_tx_desc]
  768.         and     [device.curr_tx_desc], 3
  769.  
  770.         DEBUGF  1," - Packet Sent! "
  771. .finish:
  772.         DEBUGF  1," - Done!\n"
  773.         ret
  774.  
  775.  
  776.  
  777.  
  778.  
  779. ;;;;;;;;;;;;;;;;;;;;;;;
  780. ;;                   ;;
  781. ;; Interrupt handler ;;
  782. ;;                   ;;
  783. ;;;;;;;;;;;;;;;;;;;;;;;
  784.  
  785. align 4
  786. int_handler:
  787.  
  788.         DEBUGF  1,"IRQ %x ",eax:2                   ; no, you cant replace 'eax:2' with 'al', this must be a bug in FDO
  789.  
  790. ; find pointer of device wich made IRQ occur
  791.  
  792.         mov     esi, RTL8139_LIST
  793.         mov     ecx, [RTL8139_DEV]
  794. .nextdevice:
  795.         mov     ebx, dword [esi]
  796.  
  797.         set_io  0
  798.         set_io  REG_ISR
  799.         in      ax , dx
  800.         out     dx , ax                             ; send it back to ACK
  801.  
  802.         add     esi, 4
  803.  
  804.         test    ax , ax
  805.         jnz     .got_it
  806.  
  807.         loop    .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.