Subversion Repositories Kolibri OS

Rev

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

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