Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. format MS COFF
  9.  
  10. DEBUG           equ 1
  11. DEBUG_IRQ       equ 0
  12.  
  13. include 'proc32.inc'
  14. include 'imports.inc'
  15.  
  16. API_VERSION     equ 0x01000100
  17.  
  18. USE_COM_IRQ     equ 0    ;make irq 3 and irq 4 available for PCI devices
  19.  
  20. ;irq 0,1,2,8,12,13 íåäîñòóïíû
  21. ;                   FEDCBA9876543210
  22. VALID_IRQ       equ 1100111011111000b
  23. ATTCH_IRQ       equ 0000111010100000b
  24.  
  25. if USE_COM_IRQ
  26. ATTCH_IRQ       equ 0000111010111000b
  27. end if
  28.  
  29. CPU_FREQ        equ  2000d
  30.  
  31. BIT0  EQU 0x00000001
  32. BIT1  EQU 0x00000002
  33. BIT2  EQU 0x00000004
  34. BIT3  EQU 0x00000008
  35. BIT4  EQU 0x00000010
  36. BIT5  EQU 0x00000020
  37. BIT6  EQU 0x00000040
  38. BIT7  EQU 0x00000080
  39. BIT8  EQU 0x00000100
  40. BIT9  EQU 0x00000200
  41. BIT10 EQU 0x00000400
  42. BIT11 EQU 0x00000800
  43. BIT12 EQU 0x00001000
  44. BIT13 EQU 0x00002000
  45. BIT14 EQU 0x00004000
  46. BIT15 EQU 0x00008000
  47. BIT16 EQU 0x00010000
  48. BIT17 EQU 0x00020000
  49. BIT18 EQU 0x00040000
  50. BIT19 EQU 0x00080000
  51. BIT20 EQU 0x00100000
  52. BIT21 EQU 0x00200000
  53. BIT22 EQU 0x00400000
  54. BIT23 EQU 0x00800000
  55. BIT24 EQU 0x00100000
  56. BIT25 EQU 0x02000000
  57. BIT26 EQU 0x04000000
  58. BIT27 EQU 0x08000000
  59. BIT28 EQU 0x10000000
  60. BIT29 EQU 0x20000000
  61. BIT30 EQU 0x40000000
  62. BIT31 EQU 0x80000000
  63.  
  64. VID_FM801         equ 0x1319
  65. CTRL_FM801        equ 0x0801
  66.  
  67. FM_PCM_VOLUME         equ 0x00
  68. FM_FM_VOLUME          equ 0x02
  69. FM_I2S_VOLUME         equ 0x04
  70. FM_RECORD_SOURCE      equ 0x06
  71.  
  72. FM_PLAY_CTL           equ 0x08
  73. FM_PLAY_RATE_MASK     equ 0x0f00
  74. FM_PLAY_BUF1_LAST     equ 0x0001
  75. FM_PLAY_BUF2_LAST     equ 0x0002
  76. FM_PLAY_START         equ 0x0020
  77. FM_PLAY_PAUSE         equ 0x0040
  78. FM_PLAY_STOPNOW       equ 0x0080
  79. FM_PLAY_16BIT         equ 0x4000
  80. FM_PLAY_STEREO        equ 0x8000
  81.  
  82. FM_PLAY_DMALEN        equ 0x0a
  83. FM_PLAY_DMABUF1       equ 0x0c
  84. FM_PLAY_DMABUF2       equ 0x10
  85.  
  86. FM_REC_CTL            equ 0x14
  87. FM_REC_RATE_MASK      equ 0x0f00
  88. FM_REC_BUF1_LAST      equ 0x0001
  89. FM_REC_BUF2_LAST      equ 0x0002
  90. FM_REC_START          equ 0x0020
  91. FM_REC_PAUSE          equ 0x0040
  92. FM_REC_STOPNOW        equ 0x0080
  93. FM_REC_16BIT          equ 0x4000
  94. FM_REC_STEREO         equ 0x8000
  95.  
  96. FM_REC_DMALEN         equ 0x16
  97. FM_REC_DMABUF1        equ 0x18
  98. FM_REC_DMABUF2        equ 0x1c
  99.  
  100. FM_CODEC_CTL          equ 0x22
  101. FM_VOLUME             equ 0x26
  102. FM_VOLUME_MUTE        equ 0x8000
  103.  
  104. FM_CODEC_CMD          equ 0x2a
  105. FM_CODEC_CMD_READ     equ 0x0080
  106. FM_CODEC_CMD_VALID    equ 0x0100
  107. FM_CODEC_CMD_BUSY     equ 0x0200
  108.  
  109. FM_CODEC_DATA         equ 0x2c
  110.  
  111. FM_IO_CTL             equ 0x52
  112. FM_CARD_CTL           equ 0x54
  113.  
  114. FM_INTMASK            equ 0x56
  115. FM_INTMASK_PLAY       equ 0x0001
  116. FM_INTMASK_REC        equ 0x0002
  117. FM_INTMASK_VOL        equ 0x0040
  118. FM_INTMASK_MPU        equ 0x0080
  119.  
  120. FM_INTSTATUS          equ 0x5a
  121. FM_INTSTATUS_PLAY     equ 0x0100
  122. FM_INTSTATUS_REC      equ 0x0200
  123. FM_INTSTATUS_VOL      equ 0x4000
  124. FM_INTSTATUS_MPU      equ 0x8000
  125.  
  126. CODEC_MASTER_VOL_REG         equ 0x02 ;
  127. CODEC_AUX_VOL                equ 0x04 ;
  128. CODEC_PCM_OUT_REG            equ 0x18 ; PCM output volume
  129. CODEC_EXT_AUDIO_REG          equ 0x28 ; extended audio
  130. CODEC_EXT_AUDIO_CTRL_REG     equ 0x2a ; extended audio control
  131. CODEC_PCM_FRONT_DACRATE_REG  equ 0x2c ; PCM out sample rate
  132. CODEC_PCM_SURND_DACRATE_REG  equ 0x2e ; surround sound sample rate
  133. CODEC_PCM_LFE_DACRATE_REG    equ 0x30 ; LFE sample rate
  134.  
  135. SRV_GETVERSION        equ  0
  136. DEV_PLAY              equ  1
  137. DEV_STOP              equ  2
  138. DEV_CALLBACK          equ  3
  139. DEV_SET_BUFF          equ  4
  140. DEV_NOTIFY            equ  5
  141. DEV_SET_MASTERVOL     equ  6
  142. DEV_GET_MASTERVOL     equ  7
  143. DEV_GET_INFO          equ  8
  144.  
  145. struc AC_CNTRL    ;AC controller base class
  146. { .bus                dd ?
  147.   .devfn              dd ?
  148.  
  149.   .vendor             dd ?
  150.   .dev_id             dd ?
  151.   .pci_cmd            dd ?
  152.   .pci_stat           dd ?
  153.  
  154.   .codec_io_base      dd ?
  155.   .codec_mem_base     dd ?
  156.  
  157.   .ctrl_io_base       dd ?
  158.   .ctrl_mem_base      dd ?
  159.   .cfg_reg            dd ?
  160.   .int_line           dd ?
  161.  
  162.   .vendor_ids         dd ?    ;vendor id string
  163.   .ctrl_ids           dd ?    ;hub id string
  164.  
  165.   .buffer             dd ?
  166.  
  167.   .notify_pos         dd ?
  168.   .notify_task        dd ?
  169.  
  170.   .lvi_reg            dd ?
  171.   .ctrl_setup         dd ?
  172.   .user_callback      dd ?
  173.   .codec_read16       dd ?
  174.   .codec_write16      dd ?
  175.  
  176.   .ctrl_read8         dd ?
  177.   .ctrl_read16        dd ?
  178.   .ctrl_read32        dd ?
  179.  
  180.   .ctrl_write8        dd ?
  181.   .ctrl_write16       dd ?
  182.   .ctrl_write32       dd ?
  183. }
  184.  
  185. struc CODEC   ;Audio Chip base class
  186. {
  187.   .chip_id            dd ?
  188.   .flags              dd ?
  189.   .status             dd ?
  190.  
  191.   .ac_vendor_ids      dd ?    ;ac vendor id string
  192.   .chip_ids           dd ?    ;chip model string
  193.  
  194.   .shadow_flag        dd ?
  195.                       dd ?
  196.  
  197.   .regs               dw ?     ; codec registers
  198.   .reg_master_vol     dw ?     ;0x02
  199.   .reg_aux_out_vol    dw ?     ;0x04
  200.   .reg_mone_vol       dw ?     ;0x06
  201.   .reg_master_tone    dw ?     ;0x08
  202.   .reg_beep_vol       dw ?     ;0x0A
  203.   .reg_phone_vol      dw ?     ;0x0C
  204.   .reg_mic_vol        dw ?     ;0x0E
  205.   .reg_line_in_vol    dw ?     ;0x10
  206.   .reg_cd_vol         dw ?     ;0x12
  207.   .reg_video_vol      dw ?     ;0x14
  208.   .reg_aux_in_vol     dw ?     ;0x16
  209.   .reg_pcm_out_vol    dw ?     ;0x18
  210.   .reg_rec_select     dw ?     ;0x1A
  211.   .reg_rec_gain       dw ?     ;0x1C
  212.   .reg_rec_gain_mic   dw ?     ;0x1E
  213.   .reg_gen            dw ?     ;0x20
  214.   .reg_3d_ctrl        dw ?     ;0X22
  215.   .reg_page           dw ?     ;0X24
  216.   .reg_powerdown      dw ?     ;0x26
  217.   .reg_ext_audio      dw ?     ;0x28
  218.   .reg_ext_st         dw ?     ;0x2a
  219.   .reg_pcm_front_rate dw ?     ;0x2c
  220.   .reg_pcm_surr_rate  dw ?     ;0x2e
  221.   .reg_lfe_rate       dw ?     ;0x30
  222.   .reg_pcm_in_rate    dw ?     ;0x32
  223.                       dw ?     ;0x34
  224.   .reg_cent_lfe_vol   dw ?     ;0x36
  225.   .reg_surr_vol       dw ?     ;0x38
  226.   .reg_spdif_ctrl     dw ?     ;0x3A
  227.                       dw ?     ;0x3C
  228.                       dw ?     ;0x3E
  229.                       dw ?     ;0x40
  230.                       dw ?     ;0x42
  231.                       dw ?     ;0x44
  232.                       dw ?     ;0x46
  233.                       dw ?     ;0x48
  234.                       dw ?     ;0x4A
  235.                       dw ?     ;0x4C
  236.                       dw ?     ;0x4E
  237.                       dw ?     ;0x50
  238.                       dw ?     ;0x52
  239.                       dw ?     ;0x54
  240.                       dw ?     ;0x56
  241.                       dw ?     ;0x58
  242.                       dw ?     ;0x5A
  243.                       dw ?     ;0x5C
  244.                       dw ?     ;0x5E
  245.   .reg_page_0         dw ?     ;0x60
  246.   .reg_page_1         dw ?     ;0x62
  247.   .reg_page_2         dw ?     ;0x64
  248.   .reg_page_3         dw ?     ;0x66
  249.   .reg_page_4         dw ?     ;0x68
  250.   .reg_page_5         dw ?     ;0x6A
  251.   .reg_page_6         dw ?     ;0x6C
  252.   .reg_page_7         dw ?     ;0x6E
  253.                       dw ?     ;0x70
  254.                       dw ?     ;0x72
  255.                       dw ?     ;0x74
  256.                       dw ?     ;0x76
  257.                       dw ?     ;0x78
  258.                       dw ?     ;0x7A
  259.   .reg_vendor_id_1    dw ?     ;0x7C
  260.   .reg_vendor_id_2    dw ?     ;0x7E
  261.  
  262.  
  263.   .reset              dd ?    ;virual
  264.   .set_master_vol     dd ?
  265. }
  266.  
  267. struc CTRL_INFO
  268. {   .pci_cmd          dd ?
  269.     .irq              dd ?
  270.     .glob_cntrl       dd ?
  271.     .glob_sta         dd ?
  272.     .codec_io_base    dd ?
  273.     .ctrl_io_base     dd ?
  274.     .codec_mem_base   dd ?
  275.     .ctrl_mem_base    dd ?
  276.     .codec_id         dd ?
  277. }
  278.  
  279. struc IOCTL
  280. {  .handle            dd ?
  281.    .io_code           dd ?
  282.    .input             dd ?
  283.    .inp_size          dd ?
  284.    .output            dd ?
  285.    .out_size          dd ?
  286. }
  287.  
  288. virtual at 0
  289.   IOCTL IOCTL
  290. end virtual
  291.  
  292. EVENT_NOTIFY      equ 0x00000200
  293.  
  294. public START
  295. public service_proc
  296. public version
  297.  
  298. section '.flat' code readable align 16
  299.  
  300. proc START stdcall, state:dword
  301.  
  302.         cmp     [state], 1
  303.         jne     .stop
  304.  
  305.      if DEBUG
  306.         mov     eax, START
  307.         call    dword2str
  308.         call    SysMsgBoardStr
  309.         mov     esi, msgInit
  310.         call    SysMsgBoardStr
  311.      end if
  312.  
  313.         call    detect_controller
  314.         test    eax, eax
  315.         jz      .fail
  316.  
  317.      if DEBUG
  318.         mov     esi, [ctrl.vendor_ids]
  319.         call    SysMsgBoardStr
  320.         mov     esi, [ctrl.ctrl_ids]
  321.         call    SysMsgBoardStr
  322.  
  323.      end if
  324.  
  325.         call    init_controller
  326.         test    eax, eax
  327.         jz      .fail
  328.  
  329.         call    init_codec
  330.         test    eax, eax
  331.         jz      .fail
  332.  
  333.         call    reset_controller
  334.         call    setup_codec
  335.  
  336.         mov     esi, msgPrimBuff
  337.         call    SysMsgBoardStr
  338.         call    create_primary_buff
  339.  
  340.         mov     esi, msgDone
  341.         call    SysMsgBoardStr
  342.  
  343.         mov     eax, VALID_IRQ
  344.         mov     ebx, [ctrl.int_line]
  345.         mov     esi, msgInvIRQ
  346.         bt      eax, ebx
  347.         jnc     .fail_msg
  348.         mov     eax, ATTCH_IRQ
  349.         mov     esi, msgAttchIRQ
  350.         bt      eax, ebx
  351.         jnc     .fail_msg
  352.  
  353.         stdcall AttachIntHandler, ebx, ac97_irq, dword 0
  354. .reg:
  355.  
  356.         stdcall RegService, sz_sound_srv, service_proc
  357.         ret
  358. .fail:
  359.    if DEBUG
  360.         mov     esi, msgFail
  361.         call    SysMsgBoardStr
  362.    end if
  363.         xor     eax, eax
  364.         ret
  365. .fail_msg:
  366.         call    SysMsgBoardStr
  367.         xor     eax, eax
  368.         ret
  369. .stop:
  370.         call    stop
  371.         xor     eax, eax
  372.         ret
  373. endp
  374.  
  375. handle     equ  IOCTL.handle
  376. io_code    equ  IOCTL.io_code
  377. input      equ  IOCTL.input
  378. inp_size   equ  IOCTL.inp_size
  379. output     equ  IOCTL.output
  380. out_size   equ  IOCTL.out_size
  381.  
  382. align 4
  383. proc service_proc stdcall, ioctl:dword
  384.  
  385.         mov     edi, [ioctl]
  386.         mov     eax, [edi+io_code]
  387.  
  388.         cmp     eax, SRV_GETVERSION
  389.         jne     @F
  390.  
  391.         mov     eax, [edi+output]
  392.         cmp     [edi+out_size], 4
  393.         jne     .fail
  394.  
  395.         mov     [eax], dword API_VERSION
  396.         xor     eax, eax
  397.         ret
  398. @@:
  399.         cmp     eax, DEV_PLAY
  400.         jne     @F
  401.      if DEBUG
  402.         mov     esi, msgPlay
  403.         call    SysMsgBoardStr
  404.      end if
  405.         call    play
  406.         ret
  407. @@:
  408.         cmp     eax, DEV_STOP
  409.         jne     @F
  410.      if DEBUG
  411.         mov     esi, msgStop
  412.         call    SysMsgBoardStr
  413.      end if
  414.         call    stop
  415.         ret
  416. @@:
  417.         cmp     eax, DEV_CALLBACK
  418.         jne     @F
  419.         mov     ebx, [edi+input]
  420.         stdcall set_callback, [ebx]
  421.         ret
  422. @@:
  423.         cmp     eax, DEV_SET_MASTERVOL
  424.         jne     @F
  425.         mov     eax, [edi+input]
  426.         mov     eax, [eax]
  427.         call    set_master_vol      ;eax= vol
  428.         ret
  429. @@:
  430.         cmp     eax, DEV_GET_MASTERVOL
  431.         jne     @F
  432.         mov     ebx, [edi+output]
  433.         stdcall get_master_vol, ebx
  434.         ret
  435. ;@@:
  436. ;           cmp eax, DEV_GET_INFO
  437. ;           jne @F
  438. ;           mov ebx, [edi+output]
  439. ;           stdcall get_dev_info, ebx
  440. ;           ret
  441. @@:
  442. .fail:
  443.         or      eax, -1
  444.         ret
  445. endp
  446.  
  447. restore   handle
  448. restore   io_code
  449. restore   input
  450. restore   inp_size
  451. restore   output
  452. restore   out_size
  453.  
  454. align 4
  455. proc fill_buffer
  456.  
  457.         cmp     [ctrl.user_callback], 0
  458.         je      .exit
  459.  
  460.         mov     esi, [ctrl.buffer]
  461.         mov     eax, int_flip_flop
  462.         inc     dword [eax]
  463.         test    dword [eax], 1
  464.         je      @f
  465.         add     esi, 0x4000
  466. @@:
  467.         stdcall [ctrl.user_callback], esi
  468.  
  469.         mov     edx, FM_PLAY_DMABUF1
  470.         mov     eax, [buffer_pgaddr]
  471.         mov     esi, int_flip_flop
  472.         test    dword [esi], 1
  473.         je      @f
  474.         mov     edx, FM_PLAY_DMABUF2
  475.         add     eax, 0x4000
  476. @@:
  477.         call    [ctrl.ctrl_write32]
  478.  
  479. .exit:
  480.         ret
  481. endp
  482.  
  483. align 4
  484. proc ac97_irq
  485.  
  486.      if DEBUG_IRQ
  487.         mov     esi, msgIRQ
  488.         call    SysMsgBoardStr
  489.      end if
  490.  
  491.         mov     edx, FM_INTSTATUS
  492.         call    [ctrl.ctrl_read16]
  493.  
  494.         test    eax, FM_INTSTATUS_PLAY
  495.         je      .exit
  496.  
  497.         push    eax
  498.         call    fill_buffer
  499.         pop     eax
  500.  
  501. .exit:
  502.         mov     edx, FM_INTSTATUS
  503.         call    [ctrl.ctrl_write16]
  504.  
  505.         ret
  506. endp
  507.  
  508. align 4
  509. proc create_primary_buff
  510.  
  511.         stdcall KernelAlloc, 0x10000
  512.         mov     [ctrl.buffer], eax
  513.  
  514.         mov     edi, eax
  515.         mov     ecx, 0x10000/4
  516.         xor     eax, eax
  517.         cld
  518.         rep stosd
  519.  
  520.         mov     eax, [ctrl.buffer]
  521.         call    GetPgAddr
  522.         mov     [buffer_pgaddr], eax
  523.  
  524.         ret
  525. endp
  526.  
  527. align 4
  528. proc detect_controller
  529.            locals
  530.              last_bus dd ?
  531.              bus      dd ?
  532.              devfn    dd ?
  533.            endl
  534.  
  535.         xor     eax, eax
  536.         mov     [bus], eax
  537.         inc     eax
  538.         call    PciApi
  539.         cmp     eax, -1
  540.         je      .err
  541.  
  542.         mov     [last_bus], eax
  543.  
  544. .next_bus:
  545.         and     [devfn], 0
  546. .next_dev:
  547.         stdcall PciRead32, [bus], [devfn], dword 0
  548.         test    eax, eax
  549.         jz      .next
  550.         cmp     eax, -1
  551.         je      .next
  552.  
  553.         push    eax
  554.         stdcall PciRead32, [bus], [devfn], dword 0x09
  555.         and     eax, 0xffffff
  556.         cmp     eax, 0x060100 ;pci-isa
  557.         jne     .no_bridge
  558.  
  559.         mov     eax, [bus]
  560.         mov     [brg_bus], eax
  561.         mov     eax, [devfn]
  562.         mov     [brg_devfn], eax
  563. .no_bridge:
  564.         pop     eax
  565.  
  566.         mov     edi, devices
  567. @@:
  568.         mov     ebx, [edi]
  569.         test    ebx, ebx
  570.         jz      .next
  571.  
  572.         cmp     eax, ebx
  573.         je      .found
  574.         add     edi, 12
  575.         jmp     @B
  576. .next:
  577.         inc     [devfn]
  578.         cmp     [devfn], 256
  579.         jb      .next_dev
  580.         mov     eax, [bus]
  581.         inc     eax
  582.         mov     [bus], eax
  583.         cmp     eax, [last_bus]
  584.         jna     .next_bus
  585.         xor     eax, eax
  586.         ret
  587. .found:
  588.         mov     ebx, [bus]
  589.         mov     [ctrl.bus], ebx
  590.  
  591.         mov     ecx, [devfn]
  592.         mov     [ctrl.devfn], ecx
  593.  
  594.         mov     edx, eax
  595.         and     edx, 0xFFFF
  596.         mov     [ctrl.vendor], edx
  597.         shr     eax, 16
  598.         mov     [ctrl.dev_id], eax
  599.  
  600.         mov     ebx, [edi+4]
  601.         mov     [ctrl.ctrl_ids], ebx
  602.         mov     [ctrl.vendor_ids], msg_FM
  603.  
  604.         mov     esi, [edi+8]
  605.         mov     [ctrl.ctrl_setup], esi
  606.         ret
  607. .err:
  608.         xor     eax, eax
  609.         ret
  610. endp
  611.  
  612. align 4
  613. proc init_controller
  614.  
  615.         stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 4
  616.         mov     ebx, eax
  617.         and     eax, 0xFFFF
  618.         mov     [ctrl.pci_cmd], eax
  619.         shr     ebx, 16
  620.         mov     [ctrl.pci_stat], ebx
  621.  
  622.         mov     esi, msgPciCmd
  623.         call    SysMsgBoardStr
  624.         call    dword2str
  625.         call    SysMsgBoardStr
  626.  
  627.         mov     esi, msgPciStat
  628.         call    SysMsgBoardStr
  629.         mov     eax, [ctrl.pci_stat]
  630.         call    dword2str
  631.         call    SysMsgBoardStr
  632.  
  633.         mov     esi, msgCtrlIsaIo
  634.         call    SysMsgBoardStr
  635.  
  636.         stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x10
  637.  
  638.         call    dword2str
  639.         call    SysMsgBoardStr
  640.  
  641.         and     eax, 0xFFFE
  642.         mov     [ctrl.ctrl_io_base], eax
  643.  
  644.         mov     esi, msgIrqNum
  645.         call    SysMsgBoardStr
  646.  
  647.         stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x3C
  648.         and     eax, 0xFF
  649.         mov     [ctrl.int_line], eax
  650.  
  651.         call    dword2str
  652.         call    SysMsgBoardStr
  653.  
  654.         call    [ctrl.ctrl_setup]
  655.         xor     eax, eax
  656.         inc     eax
  657.         ret
  658. endp
  659.  
  660. align 4
  661. proc set_FM
  662.         mov     [ctrl.codec_read16], codec_io_r16    ;virtual
  663.         mov     [ctrl.codec_write16], codec_io_w16   ;virtual
  664.  
  665.         mov     [ctrl.ctrl_read8 ], ctrl_io_r8      ;virtual
  666.         mov     [ctrl.ctrl_read16], ctrl_io_r16      ;virtual
  667.         mov     [ctrl.ctrl_read32], ctrl_io_r32      ;virtual
  668.  
  669.         mov     [ctrl.ctrl_write8 ], ctrl_io_w8     ;virtual
  670.         mov     [ctrl.ctrl_write16], ctrl_io_w16     ;virtual
  671.         mov     [ctrl.ctrl_write32], ctrl_io_w32     ;virtual
  672.         ret
  673. endp
  674.  
  675. align 4
  676. proc reset_controller
  677.  
  678.         mov     esi, msgInitCtrl
  679.         call    SysMsgBoardStr
  680.  
  681.         mov     edx, FM_CARD_CTL
  682.         call    [ctrl.ctrl_read8]
  683.         push    eax
  684.         or      al, 1
  685.         mov     edx, FM_CARD_CTL
  686.         call    [ctrl.ctrl_write8]
  687.         mov     eax, 10
  688.         call    StallExec
  689.         pop     eax
  690.         and     al, 0xFE
  691.         mov     edx, FM_CARD_CTL
  692.         call    [ctrl.ctrl_write8]
  693.         mov     eax, 10
  694.         call    StallExec
  695.  
  696.         mov     eax, 0x0404
  697.         mov     edx, FM_PCM_VOLUME
  698.         call    [ctrl.ctrl_write16]
  699.         mov     edx, FM_FM_VOLUME
  700.         call    [ctrl.ctrl_write16]
  701.         mov     edx, FM_I2S_VOLUME
  702.         call    [ctrl.ctrl_write16]
  703.  
  704.         mov     edx, FM_INTMASK
  705.         call    [ctrl.ctrl_read16]
  706.         and     eax, not FM_INTMASK_PLAY
  707.         or      eax, FM_INTMASK_REC or FM_INTMASK_MPU or FM_INTMASK_VOL
  708.         mov     edx, FM_INTMASK
  709.         call    [ctrl.ctrl_write16]
  710.  
  711.         mov     eax, FM_INTMASK_PLAY or FM_INTMASK_REC or FM_INTMASK_MPU or FM_INTMASK_VOL
  712.         mov     edx, FM_INTSTATUS
  713.         call    [ctrl.ctrl_write16]
  714.  
  715.         ret
  716. endp
  717.  
  718. align 4
  719. proc init_codec
  720.  
  721.         mov     esi, msgInitCodec
  722.         call    SysMsgBoardStr
  723.  
  724.         mov     al, FM_CODEC_CMD_READ
  725.         mov     edx, FM_CODEC_CMD
  726.         call    [ctrl.ctrl_write8]
  727.  
  728.         call    reset_codec
  729.  
  730.         call    detect_codec
  731.  
  732.         xor     eax, eax
  733.         inc     eax
  734.         ret
  735. endp
  736.  
  737. align 4
  738. proc reset_codec
  739.  
  740.         mov     ecx, 255
  741. .L1:
  742.         mov     edx, FM_CODEC_CMD
  743.         call    [ctrl.ctrl_read16]
  744.         test    ah, FM_CODEC_CMD_VALID shr 8
  745.         jne     .L2
  746.         loop    .L1
  747. .L2:
  748.         mov     edx, FM_CODEC_CTL
  749.         call    [ctrl.ctrl_read8]
  750.         push    eax
  751.         or      al, 0x20
  752.         mov     edx, FM_CODEC_CTL
  753.         call    [ctrl.ctrl_write8]
  754.         pop     eax
  755.         and     al, 0xDF
  756.         mov     edx, FM_CODEC_CTL
  757.         call    [ctrl.ctrl_write8]
  758.  
  759.         xor     eax, eax
  760.         inc     eax
  761.         ret
  762. endp
  763.  
  764. align 4
  765. play:
  766.         mov     eax, 0x4000-1
  767.         mov     edx, FM_PLAY_DMALEN
  768.         call    [ctrl.ctrl_write16]
  769.  
  770.         call    fill_buffer
  771.  
  772.         mov     eax, FM_PLAY_START or FM_PLAY_STOPNOW or FM_PLAY_STEREO or FM_PLAY_16BIT or 0xA00
  773.         mov     edx, FM_PLAY_CTL
  774.         call    [ctrl.ctrl_write16]
  775.  
  776.         xor     eax, eax
  777.         ret
  778.  
  779. align 4
  780. stop:
  781.         mov     edx, FM_PLAY_CTL
  782.         call    [ctrl.ctrl_read16]
  783.         and     eax, not (FM_PLAY_START or FM_PLAY_STOPNOW)
  784.         or      eax, FM_PLAY_BUF1_LAST or FM_PLAY_BUF2_LAST
  785.         mov     edx, FM_PLAY_CTL
  786.         call    [ctrl.ctrl_write16]
  787.  
  788.         xor     eax, eax
  789.         ret
  790.  
  791. align 4
  792. proc get_dev_info stdcall, p_info:dword
  793.            virtual at esi
  794.              CTRL_INFO CTRL_INFO
  795.            end virtual
  796.  
  797.         mov     esi, [p_info]
  798.         mov     eax, [ctrl.int_line]
  799.         mov     ebx, [ctrl.codec_io_base]
  800.         mov     ecx, [ctrl.ctrl_io_base]
  801.         mov     edx, [ctrl.codec_mem_base]
  802.         mov     edi, [ctrl.ctrl_mem_base]
  803.  
  804.         mov     [CTRL_INFO.irq], eax
  805.         mov     [CTRL_INFO.codec_io_base], ebx
  806.         mov     [CTRL_INFO.ctrl_io_base], ecx
  807.         mov     [CTRL_INFO.codec_mem_base], edx
  808.         mov     [CTRL_INFO.ctrl_mem_base], edi
  809.  
  810.         mov     eax, [codec.chip_id]
  811.         mov     [CTRL_INFO.codec_id], eax
  812.  
  813.         mov     ebx, [ctrl.pci_cmd]
  814.         mov     [CTRL_INFO.pci_cmd], ebx
  815.         ret
  816. endp
  817.  
  818. align 4
  819. proc set_callback stdcall, handler:dword
  820.         mov     eax, [handler]
  821.         mov     [ctrl.user_callback], eax
  822.         ret
  823. endp
  824.  
  825. align 4
  826. proc codec_read stdcall, ac_reg:dword   ; reg = edx, reval = eax
  827.  
  828.         mov     edx, [ac_reg]
  829.  
  830.         mov     ebx, edx
  831.         shr     ebx, 1
  832.         bt      [codec.shadow_flag], ebx
  833.         jc      .use_shadow
  834.  
  835.         call    [ctrl.codec_read16]  ;change edx !!!
  836.         mov     ecx, eax
  837.  
  838. .read_ok:
  839.         mov     edx, [ac_reg]
  840.         mov     [codec.regs+edx], cx
  841.         bts     [codec.shadow_flag], ebx
  842.         mov     eax, ecx
  843.         ret
  844. .use_shadow:
  845.         movzx   eax, word [codec.regs+edx]
  846.         ret
  847.  
  848. endp
  849.  
  850. align 4
  851. proc codec_write stdcall, ac_reg:dword
  852.  
  853.         mov     esi, [ac_reg]
  854.  
  855.         mov     edx, esi
  856.  
  857.         call    [ctrl.codec_write16]
  858.  
  859.         mov     [codec.regs+esi], ax
  860.         shr     esi, 1
  861.         bts     [codec.shadow_flag], esi
  862.  
  863.         ret
  864. endp
  865.  
  866. align 4
  867. proc check_semafore
  868. align 4
  869. .ok:
  870.         xor     eax, eax
  871.         inc     eax
  872.         ret
  873. endp
  874.  
  875. align 4
  876. proc StallExec
  877.         push    ecx
  878.         push    edx
  879.         push    ebx
  880.         push    eax
  881.  
  882.         mov     ecx, CPU_FREQ
  883.         mul     ecx
  884.         mov     ebx, eax      ;low
  885.         mov     ecx, edx      ;high
  886.         rdtsc
  887.         add     ebx, eax
  888.         adc     ecx, edx
  889. @@:
  890.         rdtsc
  891.         sub     eax, ebx
  892.         sbb     edx, ecx
  893.         js      @B
  894.  
  895.         pop     eax
  896.         pop     ebx
  897.         pop     edx
  898.         pop     ecx
  899.         ret
  900. endp
  901.  
  902. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  903. ;          CONTROLLER IO functions
  904. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  905.  
  906. align 4
  907. proc codec_io_r16
  908.  
  909.         push    edx
  910.         mov     ecx, 255
  911. .L1:
  912.         mov     edx, FM_CODEC_CMD
  913.         call    [ctrl.ctrl_read16]
  914.         test    ah, FM_CODEC_CMD_BUSY shr 8
  915.         je      .L2
  916.         loop    .L1
  917. .L2:
  918.         pop     eax
  919.         or      al, FM_CODEC_CMD_READ
  920.         mov     edx, FM_CODEC_CMD
  921.         call    [ctrl.ctrl_write8]
  922.  
  923.         mov     ecx, 255
  924. .L3:
  925.         mov     edx, FM_CODEC_CMD
  926.         call    [ctrl.ctrl_read16]
  927.         test    ah, FM_CODEC_CMD_VALID shr 8
  928.         jne     .L4
  929.         loop    .L3
  930. .L4:
  931.         mov     edx, FM_CODEC_DATA
  932.         call    [ctrl.ctrl_read16]
  933.  
  934.         ret
  935. endp
  936.  
  937. align 4
  938. proc codec_io_w16
  939.  
  940.         push    edx
  941.         push    eax
  942.         mov     ecx, 255
  943. .L1:
  944.         mov     edx, FM_CODEC_CMD
  945.         call    [ctrl.ctrl_read16]
  946.         test    ah, FM_CODEC_CMD_BUSY shr 8
  947.         je      .L2
  948.         loop    .L1
  949. .L2:
  950.         pop     eax
  951.         mov     edx, FM_CODEC_DATA
  952.         call    [ctrl.ctrl_write16]
  953.  
  954.         pop     eax
  955.         mov     edx, FM_CODEC_CMD
  956.         call    [ctrl.ctrl_write16]
  957.  
  958.         ret
  959. endp
  960.  
  961. align 4
  962. proc ctrl_io_r8
  963.         add     edx, [ctrl.ctrl_io_base]
  964.         in      al, dx
  965.         ret
  966. endp
  967.  
  968. align 4
  969. proc ctrl_io_r16
  970.         add     edx, [ctrl.ctrl_io_base]
  971.         in      ax, dx
  972.         ret
  973. endp
  974.  
  975. align 4
  976. proc ctrl_io_r32
  977.         add     edx, [ctrl.ctrl_io_base]
  978.         in      eax, dx
  979.         ret
  980. endp
  981.  
  982. align 4
  983. proc ctrl_io_w8
  984.         add     edx, [ctrl.ctrl_io_base]
  985.         out     dx, al
  986.         ret
  987. endp
  988.  
  989. align 4
  990. proc ctrl_io_w16
  991.         add     edx, [ctrl.ctrl_io_base]
  992.         out     dx, ax
  993.         ret
  994. endp
  995.  
  996. align 4
  997. proc ctrl_io_w32
  998.         add     edx, [ctrl.ctrl_io_base]
  999.         out     dx, eax
  1000.         ret
  1001. endp
  1002.  
  1003. align 4
  1004. dword2str:
  1005.         mov     esi, hex_buff
  1006.         mov     ecx, -8
  1007. @@:
  1008.         rol     eax, 4
  1009.         mov     ebx, eax
  1010.         and     ebx, 0x0F
  1011.         mov     bl, [ebx+hexletters]
  1012.         mov     [8+esi+ecx], bl
  1013.         inc     ecx
  1014.         jnz     @B
  1015.         ret
  1016.  
  1017. hexletters   db '0123456789ABCDEF'
  1018. hex_buff     db 8 dup(0),13,10,0
  1019. brg_bus      dd ?
  1020. brg_devfn    dd ?
  1021. include "codec.inc"
  1022.  
  1023. align 4
  1024. devices dd (CTRL_FM801 shl 16)+VID_FM801, msg_FM801, set_FM
  1025.         dd 0
  1026.  
  1027. version      dd (5 shl 16) or (API_VERSION and 0xFFFF)
  1028.  
  1029. msg_FM801    db 'FM801 AC97 controller',13,10, 0
  1030. msg_FM       db 'Forte Media',13,10, 0
  1031.  
  1032. sz_sound_srv db 'SOUND',0
  1033.  
  1034. msgInit       db 'detect hardware...',13,10,0
  1035. msgFail       db 'device not found',13,10,0
  1036. msgAttchIRQ   db 'IRQ line not supported', 13,10, 0
  1037. msgInvIRQ     db 'IRQ line not assigned or invalid', 13,10, 0
  1038. msgPlay       db 'start play', 13,10,0
  1039. msgStop       db 'stop play',  13,10,0
  1040. ;msgNotify    db 'call notify',13,10,0
  1041. msgIRQ        db 'AC97 IRQ', 13,10,0
  1042. msgInitCtrl  db 'init controller',13,10,0
  1043. msgInitCodec db 'init codec',13,10,0
  1044. msgPrimBuff   db 'create primary buffer ...',0
  1045. msgDone       db 'done',13,10,0
  1046. ;msgReg       db 'set service handler',13,10,0
  1047. ;msgOk        db 'service installed',13,10,0
  1048. ;msgStatus    db 'global status   ',0
  1049. ;msgControl   db 'global control  ',0
  1050. msgPciCmd     db 'PCI command     ',0
  1051. msgPciStat    db 'PCI status      ',0
  1052. msgCtrlIsaIo  db 'controller io base   ',0
  1053. msgIrqNum     db 'IRQ default          ',0
  1054. ;msgIrqMap    db 'AC97 irq map as      ',0
  1055.  
  1056. section '.data' data readable writable align 16
  1057.  
  1058. codec CODEC
  1059. ctrl AC_CNTRL
  1060.  
  1061. int_flip_flop      rd 1
  1062. buffer_pgaddr      rd 1
  1063.