Subversion Repositories Kolibri OS

Rev

Rev 3198 | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                  ;;
  3. ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved.     ;;
  4. ;; Distributed under terms of the GNU General Public License        ;;
  5. ;;                                                                  ;;
  6. ;;  AMD PCnet driver for KolibriOS                                  ;;
  7. ;;                                                                  ;;
  8. ;;  By hidnplayr & clevermouse                                      ;;
  9. ;;                                                                  ;;
  10. ;;  Based on the PCnet32 driver for MenuetOS, by Jarek Pelczar      ;;
  11. ;;                                                                  ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                              ;;
  13. ;;             Version 2, June 1991                                 ;;
  14. ;;                                                                  ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. format MS COFF
  18.  
  19.         API_VERSION             = 0x01000100
  20.  
  21.         DEBUG                   = 1
  22.         __DEBUG__               = 1
  23.         __DEBUG_LEVEL__         = 1
  24.  
  25.         MAX_DEVICES             = 4
  26.         MAX_ETH_FRAME_SIZE      = 1514
  27.  
  28.         TX_RING_SIZE            = 4
  29.         RX_RING_SIZE            = 4
  30.  
  31. include 'proc32.inc'
  32. include 'imports.inc'
  33. include 'fdo.inc'
  34. include 'netdrv.inc'
  35.  
  36. public START
  37. public service_proc
  38. public version
  39.  
  40.  
  41.         PORT_AUI                = 0x00
  42.         PORT_10BT               = 0x01
  43.         PORT_GPSI               = 0x02
  44.         PORT_MII                = 0x03
  45.         PORT_PORTSEL            = 0x03
  46.         PORT_ASEL               = 0x04
  47.         PORT_100                = 0x40
  48.         PORT_FD                 = 0x80
  49.  
  50.         DMA_MASK                = 0xffffffff
  51.  
  52.         LOG_TX_BUFFERS          = 2             ; FIXME
  53.         LOG_RX_BUFFERS          = 2
  54.  
  55.         TX_RING_MOD_MASK        = (TX_RING_SIZE-1)
  56.         TX_RING_LEN_BITS        = (LOG_TX_BUFFERS shl 12)
  57.  
  58.         RX_RING_MOD_MASK        = (RX_RING_SIZE-1)
  59.         RX_RING_LEN_BITS        = (LOG_RX_BUFFERS shl 4)
  60.  
  61.         PKT_BUF_SZ              = 1544
  62.  
  63.         WIO_RDP                 = 0x10
  64.         WIO_RAP                 = 0x12
  65.         WIO_RESET               = 0x14
  66.         WIO_BDP                 = 0x16
  67.  
  68.         DWIO_RDP                = 0x10
  69.         DWIO_RAP                = 0x14
  70.         DWIO_RESET              = 0x18
  71.         DWIO_BDP                = 0x1C
  72.  
  73. ; CSR registers
  74.  
  75.         CSR_CSR                 = 0x00
  76.         CSR_IAB0                = 0x01
  77.         CSR_IAB1                = 0x02
  78.         CSR_IMR                 = 0x03
  79.         CSR_TFEAT               = 0x04
  80.         CSR_EXTCTL1             = 0x05
  81.         CSR_DTBLLEN             = 0x06
  82.         CSR_EXTCTL2             = 0x07
  83.         CSR_MAR0                = 0x08
  84.         CSR_MAR1                = 0x09
  85.         CSR_MAR2                = 0x0A
  86.         CSR_MAR3                = 0x0B
  87.         CSR_PAR0                = 0x0C
  88.         CSR_PAR1                = 0x0D
  89.         CSR_PAR2                = 0x0E
  90.         CSR_MODE                = 0x0F
  91.         CSR_RXADDR0             = 0x18
  92.         CSR_RXADDR1             = 0x19
  93.         CSR_TXADDR0             = 0x1E
  94.         CSR_TXADDR1             = 0x1F
  95.         CSR_TXPOLL              = 0x2F
  96.         CSR_RXPOLL              = 0x31
  97.         CSR_RXRINGLEN           = 0x4C
  98.         CSR_TXRINGLEN           = 0x4E
  99.         CSR_DMACTL              = 0x50
  100.         CSR_BUSTIMER            = 0x52
  101.         CSR_MEMERRTIMEO         = 0x64
  102.         CSR_ONNOWMISC           = 0x74
  103.         CSR_ADVFEAT             = 0x7A
  104.         CSR_MACCFG              = 0x7D
  105.         CSR_CHIPID0             = 0x58
  106.         CSR_CHIPID1             = 0x59
  107.  
  108. ; Control and Status Register (CSR0)
  109.  
  110.         CSR_INIT                = 1 shl 0
  111.         CSR_START               = 1 shl 1
  112.         CSR_STOP                = 1 shl 2
  113.         CSR_TX                  = 1 shl 3
  114.         CSR_TXON                = 1 shl 4
  115.         CSR_RXON                = 1 shl 5
  116.         CSR_INTEN               = 1 shl 6
  117.         CSR_INTR                = 1 shl 7
  118.         CSR_IDONE               = 1 shl 8
  119.         CSR_TINT                = 1 shl 9
  120.         CSR_RINT                = 1 shl 10
  121.         CSR_MERR                = 1 shl 11
  122.         CSR_MISS                = 1 shl 12
  123.         CSR_CERR                = 1 shl 13
  124.  
  125. ; Interrupt masks and deferral control (CSR3)
  126.  
  127.         IMR_BSWAP               = 0x0004
  128.         IMR_ENMBA               = 0x0008  ; enable modified backoff alg
  129.         IMR_DXMT2PD             = 0x0010
  130.         IMR_LAPPEN              = 0x0020  ; lookahead packet processing enb
  131.         IMR_DXSUFLO             = 0x0040  ; disable TX stop on underflow
  132.         IMR_IDONE               = 0x0100
  133.         IMR_TINT                = 0x0200
  134.         IMR_RINT                = 0x0400
  135.         IMR_MERR                = 0x0800
  136.         IMR_MISS                = 0x1000
  137.  
  138.         IMR                     = IMR_IDONE ; IMR_TINT + IMR_RINT + IMR_MERR + IMR_MISS ;+ IMR_IDONE
  139.  
  140. ; Test and features control (CSR4)
  141.  
  142.         TFEAT_TXSTRTMASK        = 0x0004
  143.         TFEAT_TXSTRT            = 0x0008
  144.         TFEAT_RXCCOFLOWM        = 0x0010  ; Rx collision counter oflow
  145.         TFEAT_RXCCOFLOW         = 0x0020
  146.         TFEAT_UINT              = 0x0040
  147.         TFEAT_UINTREQ           = 0x0080
  148.         TFEAT_MISSOFLOWM        = 0x0100
  149.         TFEAT_MISSOFLOW         = 0x0200
  150.         TFEAT_STRIP_FCS         = 0x0400
  151.         TFEAT_PAD_TX            = 0x0800
  152.         TFEAT_TXDPOLL           = 0x1000
  153.         TFEAT_DMAPLUS           = 0x4000
  154.  
  155. ; Extended control and interrupt 1 (CSR5)
  156.  
  157.         EXTCTL1_SPND            = 0x0001  ; suspend
  158.         EXTCTL1_MPMODE          = 0x0002  ; magic packet mode
  159.         EXTCTL1_MPENB           = 0x0004  ; magic packet enable
  160.         EXTCTL1_MPINTEN         = 0x0008  ; magic packet interrupt enable
  161.         EXTCTL1_MPINT           = 0x0010  ; magic packet interrupt
  162.         EXTCTL1_MPPLBA          = 0x0020  ; magic packet phys. logical bcast
  163.         EXTCTL1_EXDEFEN         = 0x0040  ; excessive deferral interrupt enb.
  164.         EXTCTL1_EXDEF           = 0x0080  ; excessive deferral interrupt
  165.         EXTCTL1_SINTEN          = 0x0400  ; system interrupt enable
  166.         EXTCTL1_SINT            = 0x0800  ; system interrupt
  167.         EXTCTL1_LTINTEN         = 0x4000  ; last TX interrupt enb
  168.         EXTCTL1_TXOKINTD        = 0x8000  ; TX OK interrupt disable
  169.  
  170. ; RX/TX descriptor len (CSR6)
  171.  
  172.         DTBLLEN_RLEN            = 0x0F00
  173.         DTBLLEN_TLEN            = 0xF000
  174.  
  175. ; Extended control and interrupt 2 (CSR7)
  176.  
  177.         EXTCTL2_MIIPDTINTE      = 0x0001
  178.         EXTCTL2_MIIPDTINT       = 0x0002
  179.         EXTCTL2_MCCIINTE        = 0x0004
  180.         EXTCTL2_MCCIINT         = 0x0008
  181.         EXTCTL2_MCCINTE         = 0x0010
  182.         EXTCTL2_MCCINT          = 0x0020
  183.         EXTCTL2_MAPINTE         = 0x0040
  184.         EXTCTL2_MAPINT          = 0x0080
  185.         EXTCTL2_MREINTE         = 0x0100
  186.         EXTCTL2_MREINT          = 0x0200
  187.         EXTCTL2_STINTE          = 0x0400
  188.         EXTCTL2_STINT           = 0x0800
  189.         EXTCTL2_RXDPOLL         = 0x1000
  190.         EXTCTL2_RDMD            = 0x2000
  191.         EXTCTL2_RXFRTG          = 0x4000
  192.         EXTCTL2_FASTSPNDE       = 0x8000
  193.  
  194. ; Mode (CSR15)
  195.  
  196.         MODE_RXD                = 0x0001  ; RX disable
  197.         MODE_TXD                = 0x0002  ; TX disable
  198.         MODE_LOOP               = 0x0004  ; loopback enable
  199.         MODE_TXCRCD             = 0x0008
  200.         MODE_FORCECOLL          = 0x0010
  201.         MODE_RETRYD             = 0x0020
  202.         MODE_INTLOOP            = 0x0040
  203.         MODE_PORTSEL            = 0x0180
  204.         MODE_RXVPAD             = 0x2000
  205.         MODE_RXNOBROAD          = 0x4000
  206.         MODE_PROMISC            = 0x8000
  207.  
  208. ; BCR (Bus Control Registers)
  209.  
  210.         BCR_MMRA                = 0x00    ; Master Mode Read Active
  211.         BCR_MMW                 = 0x01    ; Master Mode Write Active
  212.         BCR_MISCCFG             = 0x02
  213.         BCR_LED0                = 0x04
  214.         BCR_LED1                = 0x05
  215.         BCR_LED2                = 0x06
  216.         BCR_LED3                = 0x07
  217.         BCR_DUPLEX              = 0x09
  218.         BCR_BUSCTL              = 0x12
  219.         BCR_EECTL               = 0x13
  220.         BCR_SSTYLE              = 0x14
  221.         BCR_PCILAT              = 0x16
  222.         BCR_PCISUBVENID         = 0x17
  223.         BCR_PCISUBSYSID         = 0x18
  224.         BCR_SRAMSIZE            = 0x19
  225.         BCR_SRAMBOUND           = 0x1A
  226.         BCR_SRAMCTL             = 0x1B
  227.         BCR_MIICTL              = 0x20
  228.         BCR_MIIADDR             = 0x21
  229.         BCR_MIIDATA             = 0x22
  230.         BCR_PCIVENID            = 0x23
  231.         BCR_PCIPCAP             = 0x24
  232.         BCR_DATA0               = 0x25
  233.         BCR_DATA1               = 0x26
  234.         BCR_DATA2               = 0x27
  235.         BCR_DATA3               = 0x28
  236.         BCR_DATA4               = 0x29
  237.         BCR_DATA5               = 0x2A
  238.         BCR_DATA6               = 0x2B
  239.         BCR_DATA7               = 0x2C
  240.         BCR_ONNOWPAT0           = 0x2D
  241.         BCR_ONNOWPAT1           = 0x2E
  242.         BCR_ONNOWPAT2           = 0x2F
  243.         BCR_PHYSEL              = 0x31
  244.  
  245. ; RX status register
  246.  
  247.         RXSTAT_BPE              = 0x0080        ; bus parity error
  248.         RXSTAT_ENP              = 0x0100        ; end of packet
  249.         RXSTAT_STP              = 0x0200        ; start of packet
  250.         RXSTAT_BUFF             = 0x0400        ; buffer error
  251.         RXSTAT_CRC              = 0x0800        ; CRC error
  252.         RXSTAT_OFLOW            = 0x1000        ; rx overrun
  253.         RXSTAT_FRAM             = 0x2000        ; framing error
  254.         RXSTAT_ERR              = 0x4000        ; error summary
  255.         RXSTAT_OWN              = 0x8000
  256.  
  257. ; TX status register
  258.  
  259.         TXSTAT_TRC              = 0x0000000F    ; transmit retries
  260.         TXSTAT_RTRY             = 0x04000000    ; retry
  261.         TXSTAT_LCAR             = 0x08000000    ; lost carrier
  262.         TXSTAT_LCOL             = 0x10000000    ; late collision
  263.         TXSTAT_EXDEF            = 0x20000000    ; excessive deferrals
  264.         TXSTAT_UFLOW            = 0x40000000    ; transmit underrun
  265.         TXSTAT_BUFF             = 0x80000000    ; buffer error
  266.  
  267.         TXCTL_OWN               = 0x8000
  268.         TXCTL_ERR               = 0x4000        ; error summary
  269.         TXCTL_ADD_FCS           = 0x2000        ; add FCS to pkt
  270.         TXCTL_MORE_LTINT        = 0x1000
  271.         TXCTL_ONE               = 0x0800
  272.         TXCTL_DEF               = 0x0400
  273.         TXCTL_STP               = 0x0200
  274.         TXCTL_ENP               = 0x0100
  275.         TXCTL_BPE               = 0x0080
  276.  
  277.         TXCTL_MBO               = 0x0000F000
  278.         TXCTL_BUFSZ             = 0x00000FFF
  279.  
  280.  
  281. virtual at ebx
  282.  
  283.         device:
  284.  
  285.         ETH_DEVICE
  286.  
  287. ; device specific
  288.  
  289.                         rb 0x100-(($ - device) and 0xff)        ;        align 256
  290.         .private:
  291.         .mode_          dw ?
  292.         .tlen_rlen      dw ?
  293.         .phys_addr      dp ?
  294.         .reserved       dw ?
  295.         .filter         dq ?
  296.         .rx_ring_phys   dd ?
  297.         .tx_ring_phys   dd ?
  298.  
  299.                         rb 0x100-(($ - device) and 0xff)        ;        align 256
  300.         .rx_ring        rb RX_RING_SIZE * descriptor.size
  301.  
  302.                         rb 0x100-(($ - device) and 0xff)        ;        align 256
  303.         .tx_ring        rb TX_RING_SIZE * descriptor.size
  304.  
  305.         .cur_rx         db ?
  306.         .cur_tx         db ?
  307.         .last_tx        db ?
  308.         .options        dd ?
  309.         .full_duplex    db ?
  310.         .chip_version   dw ?
  311.         .mii            db ?
  312.         .ltint          db ?
  313.         .dxsuflo        db ?
  314.         .fset           db ?
  315.         .fdx            db ?
  316.  
  317.         .io_addr        dd ?
  318.         .irq_line       db ?
  319.         .pci_bus        dd ?
  320.         .pci_dev        dd ?
  321.  
  322.         .read_csr       dd ?
  323.         .write_csr      dd ?
  324.         .read_bcr       dd ?
  325.         .write_bcr      dd ?
  326.         .read_rap       dd ?
  327.         .write_rap      dd ?
  328.         .sw_reset       dd ?
  329.  
  330.         device_size     = $ - device
  331.  
  332. end virtual
  333.  
  334. struc   descriptor {
  335.         .base           dd ?
  336.         .length         dw ?
  337.         .status         dw ?
  338.         .msg_length     dw ?
  339.         .misc           dw ?
  340.         .virtual        dd ?
  341.  
  342.         .size:
  343. }
  344.  
  345. virtual at 0
  346.  descriptor descriptor
  347. end virtual
  348.  
  349.  
  350.  
  351.  
  352. section '.flat' code readable align 16
  353.  
  354. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  355. ;;                        ;;
  356. ;; proc START             ;;
  357. ;;                        ;;
  358. ;; (standard driver proc) ;;
  359. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  360.  
  361. proc START stdcall, state:dword
  362.  
  363.         cmp [state], 1
  364.         jne .exit
  365.  
  366.   .entry:
  367.  
  368.         DEBUGF 1,"Loading %s driver\n", my_service
  369.         stdcall RegService, my_service, service_proc
  370.         ret
  371.  
  372.   .fail:
  373.   .exit:
  374.         xor eax, eax
  375.         ret
  376.  
  377. endp
  378.  
  379.  
  380. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  381. ;;                        ;;
  382. ;; proc SERVICE_PROC      ;;
  383. ;;                        ;;
  384. ;; (standard driver proc) ;;
  385. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  386.  
  387. align 4
  388. proc service_proc stdcall, ioctl:dword
  389.  
  390.         mov     edx, [ioctl]
  391.         mov     eax, [IOCTL.io_code]
  392.  
  393. ;------------------------------------------------------
  394.  
  395.         cmp     eax, 0 ;SRV_GETVERSION
  396.         jne     @F
  397.  
  398.         cmp     [IOCTL.out_size], 4
  399.         jb      .fail
  400.         mov     eax, [IOCTL.output]
  401.         mov     [eax], dword API_VERSION
  402.  
  403.         xor     eax, eax
  404.         ret
  405.  
  406. ;------------------------------------------------------
  407.   @@:
  408.         cmp     eax, 1 ;SRV_HOOK
  409.         jne     .fail
  410.  
  411.         cmp     [IOCTL.inp_size], 3                     ; Data input must be at least 3 bytes
  412.         jb      .fail
  413.  
  414.         mov     eax, [IOCTL.input]
  415.         cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
  416.         jne     .fail                                   ; other types arent supported for this card yet
  417.  
  418. ; check if the device is already listed
  419.  
  420.         mov     ecx, [devices]
  421.         test    ecx, ecx
  422.         jz      .firstdevice
  423.  
  424.         mov     esi, device_list
  425. ;        mov     eax, [IOCTL.input]                      ; get the pci bus and device numbers
  426.         mov     ax , [eax+1]                            ;
  427.   .nextdevice:
  428.         mov     ebx, [esi]
  429.         cmp     al, byte[device.pci_bus]
  430.         jne     @f
  431.         cmp     ah, byte[device.pci_dev]
  432.         je      .find_devicenum                         ; Device is already loaded, let's find it's device number
  433.        @@:
  434.         add     esi, 4
  435.         loop    .nextdevice
  436.  
  437. ; This device doesnt have its own eth_device structure yet, lets create one
  438.  
  439.   .firstdevice:
  440.         cmp     [devices], MAX_DEVICES                  ; First check if the driver can handle one more card
  441.         jae     .fail
  442.  
  443.         allocate_and_clear ebx, device_size, .fail
  444.  
  445. ; Fill in the direct call addresses into the struct
  446.  
  447.         mov     [device.reset], reset
  448.         mov     [device.transmit], transmit
  449.         mov     [device.get_MAC], read_mac
  450.         mov     [device.set_MAC], write_mac
  451.         mov     [device.unload], unload
  452.         mov     [device.name], my_service
  453.  
  454. ; save the pci bus and device numbers
  455.  
  456.         mov     eax, [IOCTL.input]
  457.         movzx   ecx, byte[eax+1]
  458.         mov     [device.pci_bus], ecx
  459.         movzx   ecx, byte[eax+2]
  460.         mov     [device.pci_dev], ecx
  461.  
  462. ; Now, it's time to find the base io addres of the PCI device
  463.  
  464.         PCI_find_io
  465.  
  466. ; We've found the io address, find IRQ now
  467.  
  468.         PCI_find_irq
  469.  
  470.         DEBUGF  1,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
  471.         [device.pci_dev]:1,[device.pci_bus]:1,[device.irq_line]:1,[device.io_addr]:4
  472.  
  473. ; Ok, the eth_device structure is ready, let's probe the device
  474. ; Because initialization fires IRQ, IRQ handler must be aware of this device
  475.         mov     eax, [devices]                                          ; Add the device structure to our device list
  476.         mov     [device_list+4*eax], ebx                                ; (IRQ handler uses this list to find device)
  477.         inc     [devices]                                               ;
  478.  
  479.         call    probe                                                   ; this function will output in eax
  480.         test    eax, eax
  481.         jnz     .destroy                                                ; If an error occured, exit
  482.  
  483.         mov     [device.type], NET_TYPE_ETH
  484.         call    NetRegDev
  485.         cmp     eax, -1
  486.         je      .destroy
  487.  
  488.         ret
  489.  
  490. ; If the device was already loaded, find the device number and return it in eax
  491.  
  492.   .find_devicenum:
  493.         DEBUGF  1,"Trying to find device number of already registered device\n"
  494.         call    NetPtrToNum                                             ; This kernel procedure converts a pointer to device struct in ebx
  495.                                                                         ; into a device number in edi
  496.         mov     eax, edi                                                ; Application wants it in eax instead
  497.         DEBUGF  1,"Kernel says: %u\n", eax
  498.         ret
  499.  
  500. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  501.  
  502.   .destroy:
  503.         ; todo: reset device into virgin state
  504.  
  505.         dec     [devices]
  506.   .err:
  507.         DEBUGF  1,"Error, removing all data !\n"
  508.         stdcall KernelFree, ebx
  509.  
  510.   .fail:
  511.         or      eax, -1
  512.         ret
  513.  
  514. ;------------------------------------------------------
  515. endp
  516.  
  517.  
  518. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  519. ;;                                                                        ;;
  520. ;;        Actual Hardware dependent code starts here                      ;;
  521. ;;                                                                        ;;
  522. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  523.  
  524. align 4
  525. unload:
  526.         ; TODO: (in this particular order)
  527.         ;
  528.         ; - Stop the device
  529.         ; - Detach int handler
  530.         ; - Remove device from local list (RTL8139_LIST)
  531.         ; - call unregister function in kernel
  532.         ; - Remove all allocated structures and buffers the card used
  533.  
  534.         or      eax,-1
  535.  
  536. ret
  537.  
  538.  
  539. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  540. ;;
  541. ;;  probe: enables the device (if it really is a PCnet device)
  542. ;;
  543. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  544.  
  545. align 4
  546. probe:
  547.  
  548.         mov     edx, [device.io_addr]
  549.  
  550.         call    wio_reset
  551.  
  552.         xor     ecx, ecx
  553.         call    wio_read_csr
  554.         cmp     eax, 4
  555.         jne     .try_dwio
  556.  
  557.         ; Try Word I/O
  558.         mov     ax, 88
  559.         add     edx, WIO_RAP
  560.         out     dx, ax
  561.         nop
  562.         nop
  563.         in      ax, dx
  564.         sub     edx, WIO_RAP
  565.         cmp     ax, 88
  566.         jne     .try_dwio
  567.  
  568.         call    switch_to_wio
  569.  
  570.         jmp     .L1
  571.  
  572.   .try_dwio:
  573.         call    dwio_reset
  574.  
  575.         xor     ecx, ecx
  576.         call    dwio_read_csr
  577.         cmp     eax, 4
  578.         jne     .no_dev
  579.  
  580.         ; Try Dword I/O
  581.         add     edx, DWIO_RAP
  582.         mov     eax, 88
  583.         out     dx, eax
  584.         nop
  585.         nop
  586.         in      eax, dx
  587.         sub     edx, DWIO_RAP
  588.         and     eax, 0xffff
  589.         cmp     eax, 88
  590.         jne     .no_dev
  591.  
  592.         call    switch_to_dwio
  593.  
  594.         jmp     .L1
  595.  
  596.   .no_dev:
  597.         DEBUGF 1,"PCnet device not found!\n"
  598.         mov     eax, 1
  599.         ret
  600.  
  601.   .L1:
  602.         mov     ecx, CSR_CHIPID0
  603.         call    [device.read_csr]
  604.  
  605.         mov     esi, eax
  606.         shr     esi, 12
  607.  
  608.         and     ax, 0xfff
  609.         cmp     ax, 3
  610.         jne     .no_dev
  611.  
  612.         mov     ecx, CSR_CHIPID1
  613.         call    [device.read_csr]
  614.         shl     eax, 4
  615.         or      eax, esi
  616.         mov     [device.chip_version], ax
  617.  
  618.         mov     [device.fdx], 0
  619.         mov     [device.mii], 0
  620.         mov     [device.fset], 0
  621.         mov     [device.dxsuflo], 0
  622.         mov     [device.ltint], 0
  623.  
  624.         cmp     ax, 0x2420
  625.         je      .L2
  626.         cmp     ax, 0x2430
  627.         je      .L2
  628.  
  629.         mov     [device.fdx], 1
  630.  
  631.         cmp     ax, 0x2621
  632.         je      .L4
  633.         cmp     ax, 0x2623
  634.         je      .L5
  635.         cmp     ax, 0x2624
  636.         je      .L6
  637.         cmp     ax, 0x2625
  638.         je      .L7
  639.         cmp     ax, 0x2626
  640.         je      .L8
  641.         cmp     ax, 0x2627
  642.         je      .L9
  643.  
  644.         DEBUGF 1,"Invalid chip rev\n"
  645.         jmp     .no_dev
  646.   .L2:
  647.         mov     [device.name], device_l2
  648.         jmp     .L10
  649.   .L4:
  650.         mov     [device.name], device_l4
  651. ;        mov     [device.fdx], 1
  652.         jmp     .L10
  653.   .L5:
  654.         mov     [device.name], device_l5
  655. ;        mov     [device.fdx], 1
  656.         mov     [device.mii], 1
  657.         mov     [device.fset], 1
  658.         mov     [device.ltint], 1
  659.         jmp     .L10
  660.   .L6:
  661.         mov     [device.name], device_l6
  662. ;        mov     [device.fdx], 1
  663.         mov     [device.mii], 1
  664.         mov     [device.fset], 1
  665.         jmp     .L10
  666.   .L7:
  667.         mov     [device.name], device_l7
  668. ;        mov     [device.fdx], 1
  669.         mov     [device.mii], 1
  670.         jmp     .L10
  671.   .L8:
  672.         mov     [device.name], device_l8
  673. ;        mov     [device.fdx], 1
  674.         mov     ecx, CSR_RXPOLL
  675.         call    dword [device.read_bcr]
  676.         call    dword [device.write_bcr]
  677.         jmp     .L10
  678.   .L9:
  679.         mov     [device.name], device_l9
  680. ;        mov     [device.fdx], 1
  681.         mov     [device.mii], 1
  682.   .L10:
  683.         DEBUGF 1,"device name: %s\n", [device.name]
  684.  
  685.         cmp     [device.fset], 1
  686.         jne     .L11
  687.         mov     ecx, BCR_BUSCTL
  688.         call    [device.read_bcr]
  689.         or      eax, 0x800
  690.         call    [device.write_bcr]
  691.  
  692.         mov     ecx, CSR_DMACTL
  693.         call    [device.read_csr]
  694. ;        and     eax, 0xc00
  695. ;        or      eax, 0xc00
  696.         mov     eax, 0xc00
  697.         call    [device.write_csr]
  698.  
  699.         mov     [device.dxsuflo],1
  700.         mov     [device.ltint],1
  701.   .L11:
  702.  
  703.         PCI_make_bus_master
  704.  
  705.         mov     [device.options], PORT_ASEL
  706.         mov     [device.mode_], MODE_RXD + MODE_TXD     ; disable receive and transmit
  707.         mov     [device.tlen_rlen], (TX_RING_LEN_BITS or RX_RING_LEN_BITS)
  708.  
  709.         mov     dword [device.filter], 0
  710.         mov     dword [device.filter+4], 0
  711.  
  712. align 4
  713. reset:
  714.  
  715. ; attach int handler
  716.  
  717.         movzx   eax, [device.irq_line]
  718.         DEBUGF  1,"Attaching int handler to irq %x\n", eax:1
  719.         stdcall AttachIntHandler, eax, int_handler, dword 0
  720.         test    eax, eax
  721.         jnz     @f
  722.         DEBUGF  1,"\nCould not attach int handler!\n"
  723. ;        or      eax, -1
  724. ;        ret
  725.   @@:
  726.  
  727.         mov     edx, [device.io_addr]
  728.  
  729.         call    [device.sw_reset]
  730.  
  731.         ; Switch pcnet32 to 32bit mode
  732.         mov     ecx, BCR_SSTYLE
  733.         mov     eax, 2
  734.         call    [device.write_bcr]
  735.  
  736.         ; set/reset autoselect bit
  737.         mov     ecx, BCR_MISCCFG
  738.         call    [device.read_bcr]
  739.         and     eax, not 2
  740.         test    [device.options], PORT_ASEL
  741.         jz      @f
  742.         or      eax, 2
  743.   @@:
  744.         call    [device.write_bcr]
  745.  
  746.         ; Handle full duplex setting
  747.         cmp     byte [device.full_duplex], 0
  748.         je      .duplex_ok
  749.         mov     ecx, BCR_DUPLEX
  750.         call    [device.read_bcr]
  751.         and     eax, not 3
  752.         test    [device.options], PORT_FD
  753.         jz      @f
  754.         or      eax, 1
  755.         cmp     [device.options], PORT_FD or PORT_AUI
  756.         jne     .set_duplex
  757.         or      eax, 2
  758.         jmp     .set_duplex
  759.   @@:
  760.         test    [device.options], PORT_ASEL
  761.         jz      .set_duplex
  762.         cmp     [device.chip_version], 0x2627
  763.         jne     .set_duplex
  764.         or      eax, 3
  765.   .set_duplex:
  766.         mov     ecx, BCR_DUPLEX
  767.         call    [device.write_bcr]
  768.   .duplex_ok:
  769.  
  770.         ; set/reset GPSI bit in test register
  771.         mov     ecx, 124
  772.         call    [device.read_csr]
  773.         mov     ecx, [device.options]
  774.         and     ecx, PORT_PORTSEL
  775.         cmp     ecx, PORT_GPSI
  776.         jne     @f
  777.         or      eax, 0x10
  778.   @@:
  779.         call    [device.write_csr]
  780.         cmp     [device.mii], 0
  781.         je      .L6
  782.         test    [device.options], PORT_ASEL
  783.         jnz     .L6
  784.         mov     ecx, BCR_MIICTL
  785.         call    [device.read_bcr]
  786.         and     eax, not 0x38
  787.         test    [device.options], PORT_FD
  788.         jz      @f
  789.         or      eax, 0x10
  790.   @@:
  791.         test    [device.options], PORT_100
  792.         jz      @f
  793.         or      eax, 0x08
  794.   @@:
  795.         call    [device.write_bcr]
  796.         jmp     .L9
  797.   .L6:
  798.         test    [device.options], PORT_ASEL
  799.         jz      .L9
  800.         mov     ecx, BCR_MIICTL
  801.         DEBUGF 1,"ASEL, enable auto-negotiation\n"
  802.         call    [device.read_bcr]
  803.         and     eax, not 0x98
  804.         or      eax, 0x20
  805.         call    [device.write_bcr]
  806.   .L9:
  807.         cmp     [device.ltint], 0
  808.         je      @f
  809.         mov     ecx, 5
  810.         call    [device.read_csr]
  811.         or      eax, (1 shl 14)
  812.         call    [device.write_csr]
  813.   @@:
  814.         mov     eax, [device.options]
  815.         and     eax, PORT_PORTSEL
  816.         shl     eax, 7
  817.         mov     [device.mode_], ax
  818.         mov     dword [device.filter], -1
  819.         mov     dword [device.filter+4], -1
  820.  
  821.         call    read_mac
  822.  
  823.         lea     esi, [device.mac]
  824.         lea     edi, [device.phys_addr]
  825.         movsd
  826.         movsw
  827.  
  828.         call    init_ring
  829.  
  830.         mov     edx, [device.io_addr]   ; init ring destroys edx
  831.  
  832.         lea     eax, [device.private]
  833.         GetRealAddr
  834.         push    eax
  835.         and     eax, 0xffff
  836.         mov     ecx, 1
  837.         call    [device.write_csr]
  838.         pop     eax
  839.         shr     eax, 16
  840.         mov     ecx, 2
  841.         call    [device.write_csr]
  842.  
  843.         mov     ecx, 4
  844.         mov     eax, 0x0915
  845.         call    [device.write_csr]
  846.  
  847. ; Set the interrupt mask
  848.         mov     ecx, CSR_IMR
  849.         mov     eax, IMR
  850.         call    [device.write_csr]
  851.  
  852. ; Initialise the device
  853.         xor     ecx, ecx
  854.         mov     eax, CSR_INIT
  855.         call    [device.write_csr]
  856.  
  857.         mov     esi, 100
  858. ;        xor     ecx, ecx
  859.   @@:
  860.         call    [device.read_csr]
  861.         test    ax, CSR_IDONE
  862.         jnz     @f
  863.  
  864.         dec     esi
  865.         jnz     @r
  866.         DEBUGF 1,"Initialize timeout!\n"
  867.   @@:
  868.  
  869. ; Start the device and enable interrupts
  870.         xor     ecx, ecx
  871.         mov     eax, CSR_START + CSR_INTEN
  872.         call    [device.write_csr]
  873.  
  874. ; Set the mtu, kernel will be able to send now
  875.         mov     [device.mtu], 1514
  876.  
  877.         DEBUGF 1,"reset complete\n"
  878.         xor     eax, eax
  879.         ret
  880.  
  881.  
  882. align 4
  883. init_ring:
  884.  
  885.         DEBUGF 1,"init ring\n"
  886.  
  887.         lea     edi, [device.rx_ring]
  888.         mov     eax, edi
  889.         GetRealAddr
  890.         mov     [device.rx_ring_phys], eax
  891.         mov     ecx, RX_RING_SIZE
  892.   .rx_init:
  893.         push    ecx
  894.         stdcall KernelAlloc, PKT_BUF_SZ
  895.         pop     ecx
  896.         mov     [edi + descriptor.virtual], eax
  897.         GetRealAddr
  898.         mov     [edi + descriptor.base], eax
  899.         mov     [edi + descriptor.length], - PKT_BUF_SZ
  900.         mov     [edi + descriptor.status], RXSTAT_OWN
  901.         mov     dword [edi + descriptor.msg_length], 0    ; also clears misc field
  902.         add     edi, descriptor.size
  903.         dec     ecx
  904.         jnz     .rx_init
  905.  
  906.         lea     edi, [device.tx_ring]
  907.         mov     eax, edi
  908.         GetRealAddr
  909.         mov     [device.tx_ring_phys], eax
  910.         mov     ecx, TX_RING_SIZE
  911.   .tx_init:
  912.         mov     [edi + descriptor.status], 0
  913.         add     edi, descriptor.size
  914.         dec     ecx
  915.         jnz     .tx_init
  916.  
  917.         mov     [device.tlen_rlen], (TX_RING_LEN_BITS or RX_RING_LEN_BITS)
  918.  
  919.         mov     [device.cur_tx], 0
  920.         mov     [device.last_tx], 0
  921.         mov     [device.cur_rx], 0
  922.  
  923.         ret
  924.  
  925.  
  926.  
  927.  
  928. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  929. ;;                                         ;;
  930. ;; Transmit                                ;;
  931. ;;                                         ;;
  932. ;; In: buffer pointer in [esp+4]           ;;
  933. ;;     size of buffer in [esp+8]           ;;
  934. ;;     pointer to device structure in ebx  ;;
  935. ;;                                         ;;
  936. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  937.  
  938. align 4
  939. transmit:
  940.         DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n", [esp+4], [esp+8]
  941.         mov     eax, [esp+4]
  942.         DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  943.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  944.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  945.         [eax+13]:2,[eax+12]:2
  946.  
  947.         cmp     dword [esp+8], 1514
  948.         ja      .nospace                        ; packet is too long
  949.         cmp     dword [esp+8], 60
  950.         jb      .nospace                        ; packet is too short
  951.  
  952. ; check descriptor
  953.         lea     edi, [device.tx_ring]
  954.         movzx   eax, [device.cur_tx]
  955.         shl     eax, 4
  956.         add     edi, eax
  957.  
  958.         test    [edi + descriptor.status], TXCTL_OWN
  959.         jnz     .nospace
  960. ; descriptor is free, use it
  961.         mov     eax, [esp+4]
  962.         mov     [edi + descriptor.virtual], eax
  963.         GetRealAddr
  964.         mov     [edi + descriptor.base], eax
  965. ; set length
  966.         mov     eax, [esp+8]
  967.         neg     eax
  968.         mov     [edi + descriptor.length], ax
  969. ; put to transfer queue
  970.         mov     [edi + descriptor.status], TXCTL_OWN + TXCTL_STP + TXCTL_ENP
  971.  
  972. ; trigger an immediate send
  973.         mov     edx, [device.io_addr]
  974.         xor     ecx, ecx                        ; CSR0
  975.         call    [device.read_csr]
  976.         or      eax, CSR_TX
  977.         call    [device.write_csr]
  978.  
  979. ; get next descriptor 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
  980.         inc     [device.cur_tx]
  981.         and     [device.cur_tx], TX_RING_SIZE - 1
  982.         DEBUGF  2," - Packet Sent! "
  983.  
  984. ; Update stats
  985.         inc     [device.packets_tx]
  986.         mov     eax, [esp+8]
  987.         add     dword [device.bytes_tx], eax
  988.         adc     dword [device.bytes_tx + 4], 0
  989.  
  990.   .finish:
  991.         DEBUGF  2," - Done!\n"
  992.         xor     eax, eax
  993.         ret     8
  994.  
  995.   .nospace:
  996.         DEBUGF  1, 'ERROR: no free transmit descriptors\n'
  997.         stdcall KernelFree, [esp+4]
  998.         or      eax, -1
  999.         ret     8
  1000.  
  1001.  
  1002.  
  1003. ;;;;;;;;;;;;;;;;;;;;;;;
  1004. ;;                   ;;
  1005. ;; Interrupt handler ;;
  1006. ;;                   ;;
  1007. ;;;;;;;;;;;;;;;;;;;;;;;
  1008.  
  1009. align 4
  1010. int_handler:
  1011.  
  1012.         DEBUGF  1,"\n%s int\n", my_service
  1013.  
  1014. ; find pointer of device wich made IRQ occur
  1015.  
  1016.         mov     ecx, [devices]
  1017.         test    ecx, ecx
  1018.         jz      .nothing
  1019.         mov     esi, device_list
  1020.   .nextdevice:
  1021.         mov     ebx, [esi]
  1022.  
  1023.         mov     edx, [device.io_addr]
  1024.         push    ecx
  1025.         xor     ecx, ecx                        ; CSR0
  1026.         call    [device.read_csr]               ; get IRQ reason
  1027.         call    [device.write_csr]              ; write it back to ACK
  1028.         pop     ecx
  1029.         test    ax, ax
  1030.         jnz     .got_it
  1031.   .continue:
  1032.         add     esi, 4
  1033.         dec     ecx
  1034.         jnz     .nextdevice
  1035.   .nothing:
  1036.         ret                                     ; If no device was found, abort (The int was probably for a device, not registered to this driver)
  1037.  
  1038.   .got_it:
  1039.         DEBUGF  1,"Device: %x status: %x\n", ebx, eax:2
  1040.  
  1041.         push    ax
  1042.         test    ax, CSR_RINT
  1043.         jz      .not_receive
  1044.  
  1045.         push    ebx
  1046.   .rx_loop:
  1047.         pop     ebx
  1048.         movzx   eax, [device.cur_rx]
  1049.         shl     eax, 4
  1050.         lea     edi, [device.rx_ring]
  1051.         add     edi, eax                        ; edi now points to current rx ring entry
  1052.  
  1053.         mov     ax, [edi + descriptor.status]
  1054.         DEBUGF  1,"RX packet status: %x\n", eax:4
  1055.  
  1056.         test    ax, RXSTAT_OWN                  ; If this bit is set, the controller OWN's the packet, if not, we do
  1057.         jnz     .not_receive
  1058.  
  1059.         test    ax, RXSTAT_ENP
  1060.         jz      .not_receive
  1061.  
  1062.         test    ax, RXSTAT_STP
  1063.         jz      .not_receive
  1064.  
  1065.         movzx   ecx, [edi + descriptor.msg_length]      ; get packet length in ecx
  1066.         sub     ecx, 4                                  ;
  1067.  
  1068. ; Set pointers for ETH_input
  1069.         push    ebx
  1070.  
  1071.         push    .rx_loop                                ; return address
  1072.         push    ecx                                     ; packet size
  1073.         push    [edi + descriptor.virtual]              ; packet address
  1074.  
  1075. ; Update stats
  1076.         add     dword [device.bytes_rx], ecx
  1077.         adc     dword [device.bytes_rx + 4], 0
  1078.         inc     [device.packets_rx]
  1079.  
  1080. ; now allocate a new buffer
  1081.         stdcall KernelAlloc, PKT_BUF_SZ                 ; Allocate a buffer for the next packet
  1082.         mov     [edi + descriptor.virtual], eax         ; set virtual address
  1083.         GetRealAddr
  1084.         mov     [edi + descriptor.base], eax            ; and real address
  1085.  
  1086. ;        mov     word [edi + descriptor.length], - PKT_BUF_SZ
  1087.         mov     [edi + descriptor.status], RXSTAT_OWN   ; give it back to PCnet controller
  1088.  
  1089.         inc     [device.cur_rx]                         ; set next receive descriptor
  1090.         and     [device.cur_rx], RX_RING_SIZE - 1
  1091.  
  1092.         jmp     Eth_input
  1093.  
  1094.   .not_receive:
  1095.         pop     ax
  1096.  
  1097.         test    ax, CSR_TINT
  1098.         jz      .not_transmit
  1099.  
  1100.   .tx_loop:
  1101.         lea     edi, [device.tx_ring]
  1102.         movzx   eax, [device.last_tx]
  1103.         shl     eax, 4
  1104.         add     edi, eax
  1105.  
  1106.         test    [edi + descriptor.status], TXCTL_OWN
  1107.         jnz     .not_transmit
  1108.  
  1109.         mov     eax, [edi + descriptor.virtual]
  1110.         test    eax, eax
  1111.         jz      .not_transmit
  1112.  
  1113.         mov     [edi + descriptor.virtual], 0
  1114.  
  1115.         DEBUGF  1,"Removing packet %x from memory\n", eax
  1116.  
  1117.         stdcall KernelFree, eax
  1118.  
  1119.         inc     [device.last_tx]
  1120.         and     [device.last_tx], TX_RING_SIZE - 1
  1121.         jmp     .tx_loop
  1122.  
  1123.   .not_transmit:
  1124.  
  1125.         ret
  1126.  
  1127.  
  1128.  
  1129.  
  1130. ;;;;;;;;;;;;;;;;;;;;;;;
  1131. ;;                   ;;
  1132. ;; Write MAC address ;;
  1133. ;;                   ;;
  1134. ;;;;;;;;;;;;;;;;;;;;;;;
  1135.  
  1136. align 4
  1137. write_mac:      ; in: mac pushed onto stack (as 3 words)
  1138.  
  1139.         DEBUGF  1,"Writing MAC: %x-%x-%x-%x-%x-%x",[esp+0]:2,[esp+1]:2,[esp+2]:2,[esp+3]:2,[esp+4]:2,[esp+5]:2
  1140.  
  1141.         mov     edx, [device.io_addr]
  1142.         add     dx, 2
  1143.         xor     eax, eax
  1144.  
  1145.         mov     ecx, CSR_PAR0
  1146.        @@:
  1147.         pop     ax
  1148.         call    [device.write_csr]
  1149.         DEBUGF  1,"."
  1150.         inc     ecx
  1151.         cmp     ecx, CSR_PAR2
  1152.         jb      @r
  1153.  
  1154.         DEBUGF  1,"\n"
  1155.  
  1156. ; Notice this procedure does not ret, but continues to read_mac instead.
  1157.  
  1158. ;;;;;;;;;;;;;;;;;;;;;;
  1159. ;;                  ;;
  1160. ;; Read MAC address ;;
  1161. ;;                  ;;
  1162. ;;;;;;;;;;;;;;;;;;;;;;
  1163. align 4
  1164. read_mac:
  1165.         DEBUGF  1,"Reading MAC"
  1166.  
  1167.         mov     edx, [device.io_addr]
  1168.         add     dx, 6
  1169.        @@:
  1170.         dec     dx
  1171.         dec     dx
  1172.         in      ax, dx
  1173.         push    ax
  1174.         DEBUGF  1,"."
  1175.         cmp     edx, [device.io_addr]
  1176.         ja      @r
  1177.  
  1178.         DEBUGF  1," %x-%x-%x-%x-%x-%x\n",[esp+0]:2,[esp+1]:2,[esp+2]:2,[esp+3]:2,[esp+4]:2,[esp+5]:2
  1179.  
  1180.         lea     edi, [device.mac]
  1181.         pop     ax
  1182.         stosw
  1183.         pop     ax
  1184.         stosw
  1185.         pop     ax
  1186.         stosw
  1187.  
  1188.         ret
  1189.  
  1190. align 4
  1191. switch_to_wio:
  1192.  
  1193.         DEBUGF  1,"Switching to 16-bit mode\n"
  1194.  
  1195.         mov     [device.read_csr], wio_read_csr
  1196.         mov     [device.write_csr], wio_write_csr
  1197.         mov     [device.read_bcr], wio_read_bcr
  1198.         mov     [device.write_bcr], wio_write_bcr
  1199.         mov     [device.read_rap], wio_read_rap
  1200.         mov     [device.write_rap], wio_write_rap
  1201.         mov     [device.sw_reset], wio_reset
  1202.  
  1203.         ret
  1204.  
  1205. align 4
  1206. switch_to_dwio:
  1207.  
  1208.         DEBUGF  1,"Switching to 32-bit mode\n"
  1209.  
  1210.         mov     [device.read_csr], dwio_read_csr
  1211.         mov     [device.write_csr], dwio_write_csr
  1212.         mov     [device.read_bcr], dwio_read_bcr
  1213.         mov     [device.write_bcr], dwio_write_bcr
  1214.         mov     [device.read_rap], dwio_read_rap
  1215.         mov     [device.write_rap], dwio_write_rap
  1216.         mov     [device.sw_reset], dwio_reset
  1217.  
  1218.         ret
  1219.  
  1220.  
  1221. ; ecx - index
  1222. ; return:
  1223. ; eax - data
  1224. align 4
  1225. wio_read_csr:
  1226.  
  1227.         add     edx, WIO_RAP
  1228.         mov     ax, cx
  1229.         out     dx, ax
  1230.         add     edx, WIO_RDP - WIO_RAP
  1231.         in      ax, dx
  1232.         and     eax, 0xffff
  1233.         sub     edx, WIO_RDP
  1234.  
  1235.         ret
  1236.  
  1237.  
  1238. ; eax - data
  1239. ; ecx - index
  1240. align 4
  1241. wio_write_csr:
  1242.  
  1243.         add     edx, WIO_RAP
  1244.         xchg    eax, ecx
  1245.         out     dx, ax
  1246.         xchg    eax, ecx
  1247.         add     edx, WIO_RDP - WIO_RAP
  1248.         out     dx, ax
  1249.         sub     edx, WIO_RDP
  1250.  
  1251.         ret
  1252.  
  1253.  
  1254. ; ecx - index
  1255. ; return:
  1256. ; eax - data
  1257. align 4
  1258. wio_read_bcr:
  1259.  
  1260.         add     edx, WIO_RAP
  1261.         mov     ax, cx
  1262.         out     dx, ax
  1263.         add     edx, WIO_BDP - WIO_RAP
  1264.         in      ax, dx
  1265.         and     eax, 0xffff
  1266.         sub     edx, WIO_BDP
  1267.  
  1268.         ret
  1269.  
  1270.  
  1271. ; eax - data
  1272. ; ecx - index
  1273. align 4
  1274. wio_write_bcr:
  1275.  
  1276.         add     edx, WIO_RAP
  1277.         xchg    eax, ecx
  1278.         out     dx, ax
  1279.         xchg    eax, ecx
  1280.         add     edx, WIO_BDP - WIO_RAP
  1281.         out     dx, ax
  1282.         sub     edx, WIO_BDP
  1283.  
  1284.         ret
  1285.  
  1286. align 4
  1287. wio_read_rap:
  1288.  
  1289.         add     edx, WIO_RAP
  1290.         in      ax, dx
  1291.         and     eax, 0xffff
  1292.         sub     edx, WIO_RAP
  1293.  
  1294.         ret
  1295.  
  1296. ; eax - val
  1297. align 4
  1298. wio_write_rap:
  1299.  
  1300.         add     edx, WIO_RAP
  1301.         out     dx, ax
  1302.         sub     edx, WIO_RAP
  1303.  
  1304.         ret
  1305.  
  1306. align 4
  1307. wio_reset:
  1308.  
  1309.         push    eax
  1310.         add     edx, WIO_RESET
  1311.         in      ax, dx
  1312.         pop     eax
  1313.         sub     edx, WIO_RESET
  1314.  
  1315.         ret
  1316.  
  1317.  
  1318.  
  1319. ; ecx - index
  1320. ; return:
  1321. ; eax - data
  1322. align 4
  1323. dwio_read_csr:
  1324.  
  1325.         add     edx, DWIO_RAP
  1326.         mov     eax, ecx
  1327.         out     dx, eax
  1328.         add     edx, DWIO_RDP - DWIO_RAP
  1329.         in      eax, dx
  1330.         and     eax, 0xffff
  1331.         sub     edx, DWIO_RDP
  1332.  
  1333.         ret
  1334.  
  1335.  
  1336. ; ecx - index
  1337. ; eax - data
  1338. align 4
  1339. dwio_write_csr:
  1340.  
  1341.         add     edx, DWIO_RAP
  1342.         xchg    eax, ecx
  1343.         out     dx, eax
  1344.         add     edx, DWIO_RDP - DWIO_RAP
  1345.         xchg    eax, ecx
  1346.         out     dx, eax
  1347.         sub     edx, DWIO_RDP
  1348.  
  1349.         ret
  1350.  
  1351. ; ecx - index
  1352. ; return:
  1353. ; eax - data
  1354. align 4
  1355. dwio_read_bcr:
  1356.  
  1357.         add     edx, DWIO_RAP
  1358.         mov     eax, ecx
  1359.         out     dx, eax
  1360.         add     edx, DWIO_BDP - DWIO_RAP
  1361.         in      eax, dx
  1362.         and     eax, 0xffff
  1363.         sub     edx, DWIO_BDP
  1364.  
  1365.         ret
  1366.  
  1367.  
  1368. ; ecx - index
  1369. ; eax - data
  1370. align 4
  1371. dwio_write_bcr:
  1372.  
  1373.         add     edx, DWIO_RAP
  1374.         xchg    eax, ecx
  1375.         out     dx, eax
  1376.         add     edx, DWIO_BDP - DWIO_RAP
  1377.         xchg    eax, ecx
  1378.         out     dx, eax
  1379.         sub     edx, DWIO_BDP
  1380.  
  1381.         ret
  1382.  
  1383. align 4
  1384. dwio_read_rap:
  1385.  
  1386.         add     edx, DWIO_RAP
  1387.         in      eax, dx
  1388.         and     eax, 0xffff
  1389.         sub     edx, DWIO_RAP
  1390.  
  1391.         ret
  1392.  
  1393.  
  1394. ; eax - val
  1395. align 4
  1396. dwio_write_rap:
  1397.  
  1398.         add     edx, DWIO_RAP
  1399.         out     dx, eax
  1400.         sub     edx, DWIO_RAP
  1401.  
  1402.         ret
  1403.  
  1404. align 4
  1405. dwio_reset:
  1406.  
  1407.         push    eax
  1408.         add     edx, DWIO_RESET
  1409.         in      eax, dx
  1410.         pop     eax
  1411.         sub     edx, DWIO_RESET
  1412.  
  1413.         ret
  1414.  
  1415.  
  1416.  
  1417. ; End of code
  1418.  
  1419. align 4                                       ; Place all initialised data here
  1420.  
  1421. devices     dd 0
  1422. version       dd (5 shl 16) or (API_VERSION and 0xFFFF)
  1423. my_service    db 'PCnet',0                    ; max 16 chars include zero
  1424.  
  1425. device_l2     db "PCnet/PCI 79C970",0
  1426. device_l4     db "PCnet/PCI II 79C970A",0
  1427. device_l5     db "PCnet/FAST 79C971",0
  1428. device_l6     db "PCnet/FAST+ 79C972",0
  1429. device_l7     db "PCnet/FAST III 79C973",0
  1430. device_l8     db "PCnet/Home 79C978",0
  1431. device_l9     db "PCnet/FAST III 79C975",0
  1432.  
  1433. options_mapping:
  1434. dd PORT_ASEL                            ; 0 Auto-select
  1435. dd PORT_AUI                             ; 1 BNC/AUI
  1436. dd PORT_AUI                             ; 2 AUI/BNC
  1437. dd PORT_ASEL                            ; 3 not supported
  1438. dd PORT_10BT or PORT_FD                 ; 4 10baseT-FD
  1439. dd PORT_ASEL                            ; 5 not supported
  1440. dd PORT_ASEL                            ; 6 not supported
  1441. dd PORT_ASEL                            ; 7 not supported
  1442. dd PORT_ASEL                            ; 8 not supported
  1443. dd PORT_MII                             ; 9 MII 10baseT
  1444. dd PORT_MII or PORT_FD                  ; 10 MII 10baseT-FD
  1445. dd PORT_MII                             ; 11 MII (autosel)
  1446. dd PORT_10BT                            ; 12 10BaseT
  1447. dd PORT_MII or PORT_100                 ; 13 MII 100BaseTx
  1448. dd PORT_MII or PORT_100 or PORT_FD      ; 14 MII 100BaseTx-FD
  1449. dd PORT_ASEL                            ; 15 not supported
  1450.  
  1451. include_debug_strings                                   ; All data wich FDO uses will be included here
  1452.  
  1453. section '.data' data readable writable align 16         ; place all uninitialized data place here
  1454.  
  1455. device_list rd MAX_DEVICES                              ; This list contains all pointers to device structures the driver is handling
  1456.