Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. format MS COFF
  3.  
  4.  
  5. include 'proc32.inc'
  6.  
  7. DEBUG       equ 1
  8.  
  9. CPU_FREQ    equ  2000d   ;cpu freq in MHz
  10.  
  11. BIT0  EQU 0x00000001
  12. BIT1  EQU 0x00000002
  13. BIT2  EQU 0x00000004
  14. BIT3  EQU 0x00000008
  15. BIT4  EQU 0x00000010
  16. BIT5  EQU 0x00000020
  17. BIT6  EQU 0x00000040
  18. BIT7  EQU 0x00000080
  19. BIT8  EQU 0x00000100
  20. BIT9  EQU 0x00000200
  21. BIT10 EQU 0x00000400
  22. BIT11 EQU 0x00000800
  23. BIT12 EQU 0x00001000
  24. BIT13 EQU 0x00002000
  25. BIT14 EQU 0x00004000
  26. BIT15 EQU 0x00008000
  27. BIT16 EQU 0x00010000
  28. BIT17 EQU 0x00020000
  29. BIT18 EQU 0x00040000
  30. BIT19 EQU 0x00080000
  31. BIT20 EQU 0x00100000
  32. BIT21 EQU 0x00200000
  33. BIT22 EQU 0x00400000
  34. BIT23 EQU 0x00800000
  35. BIT24 EQU 0x00100000
  36. BIT25 EQU 0x02000000
  37. BIT26 EQU 0x04000000
  38. BIT27 EQU 0x08000000
  39. BIT28 EQU 0x10000000
  40. BIT29 EQU 0x20000000
  41. BIT30 EQU 0x40000000
  42. BIT31 EQU 0x80000000
  43.  
  44. VID_SIS           equ 0x1039
  45. CTRL_SIS          equ 0x7012
  46.  
  47. PCM_OUT_BDL       equ  0x10      ; PCM out buffer descriptors list
  48. PCM_OUT_CR_REG    equ  0x1b      ; PCM out Control Register
  49. PCM_OUT_LVI_REG   equ  0x15      ; PCM last valid index
  50. PCM_OUT_SR_REG    equ  0x18      ; PCM out Status register
  51. PCM_OUT_PIV_REG   equ  0x1a      ; PCM out prefetched index
  52. PCM_OUT_CIV_REG   equ  0x14      ; PCM out current index
  53.  
  54. PCM_IN_CR_REG     equ  0x0b      ; PCM in Control Register
  55. MC_IN_CR_REG      equ  0x2b      ; MIC in Control Register
  56. RR                equ  BIT1      ; reset registers.  Nukes all regs
  57.  
  58. CODEC_MASTER_VOL_REG            equ     0x02
  59. CODEC_AUX_VOL                   equ     0x04    ;
  60. CODEC_PCM_OUT_REG               equ     18h     ; PCM output volume
  61. CODEC_EXT_AUDIO_REG             equ     28h     ; extended audio
  62. CODEC_EXT_AUDIO_CTRL_REG        equ     2ah     ; extended audio control
  63. CODEC_PCM_FRONT_DACRATE_REG     equ     2ch     ; PCM out sample rate
  64. CODEC_PCM_SURND_DACRATE_REG     equ     2eh     ; surround sound sample rate
  65. CODEC_PCM_LFE_DACRATE_REG       equ     30h     ; LFE sample rate
  66.  
  67.  
  68. GLOB_CTRL      equ  0x2C        ;   Global Control
  69. CTRL_STAT       equ  0x30        ;   Global Status
  70. CTRL_CAS        equ  0x34        ;   Codec Access Semiphore
  71.  
  72. CAS_FLAG       equ  0x01        ;   Codec Access Semiphore Bit
  73.  
  74. CTRL_ST_CREADY  equ  BIT8+BIT9+BIT28 ;   Primary Codec Ready
  75.  
  76. CTRL_ST_RCS     equ  0x00008000  ;   Read Completion Status
  77.  
  78. CTRL_CNT_CRIE   equ  BIT4+BIT5+BIT6  ;   Codecs Resume Interrupt Enable
  79. CTRL_CNT_AC_OFF equ  0x00000008  ;   ACLINK Off
  80. CTRL_CNT_WARM   equ  0x00000004  ;   AC97 Warm Reset
  81. CTRL_CNT_COLD   equ  0x00000002  ;   AC97 Cold Reset
  82. CTRL_CNT_GIE    equ  0x00000001  ;   GPI Interrupt Enable
  83.  
  84. CODEC_REG_POWERDOWN   equ 0x26
  85. CODEC_REG_ST          equ 0x26
  86.  
  87.  
  88. DEV_PLAY              equ  1
  89. DEV_STOP              equ  2
  90. DEV_CALLBACK          equ  3
  91. DEV_SET_BUFF          equ  4
  92. DEV_NOTIFY            equ  5
  93. DEV_SET_MASTERVOL     equ  6
  94. DEV_GET_MASTERVOL     equ  7
  95. DEV_GET_INFO          equ  8
  96.  
  97. struc AC_CNTRL              ;AC controller base class
  98. { .bus                dd ?
  99.   .devfn              dd ?
  100.  
  101.   .vendor             dd ?
  102.   .dev_id             dd ?
  103.   .pci_cmd            dd ?
  104.   .pci_stat           dd ?
  105.  
  106.   .codec_io_base      dd ?
  107.   .codec_mem_base     dd ?
  108.  
  109.   .ctrl_io_base       dd ?
  110.   .ctrl_mem_base      dd ?
  111.   .cfg_reg            dd ?
  112.   .int_line           dd ?
  113.  
  114.   .vendor_ids         dd ?    ;vendor id string
  115.   .ctrl_ids           dd ?    ;hub id string
  116.  
  117.   .buffer             dd ?
  118.  
  119.   .notify_pos         dd ?
  120.   .notify_task        dd ?
  121.  
  122.   .lvi_reg            dd ?
  123.   .ctrl_setup         dd ?
  124.   .user_callback      dd ?
  125.   .codec_read16       dd ?
  126.   .codec_write16      dd ?
  127.  
  128.   .ctrl_read8         dd ?
  129.   .ctrl_read16        dd ?
  130.   .ctrl_read32        dd ?
  131.  
  132.   .ctrl_write8        dd ?
  133.   .ctrl_write16       dd ?
  134.   .ctrl_write32       dd ?
  135. }
  136.  
  137. struc CODEC                ;Audio Chip base class
  138. {
  139.   .chip_id            dd ?
  140.   .flags              dd ?
  141.   .status             dd ?
  142.  
  143.   .ac_vendor_ids      dd ?    ;ac vendor id string
  144.   .chip_ids           dd ?    ;chip model string
  145.  
  146.   .shadow_flag        dd ?
  147.                       dd ?
  148.  
  149.   .regs               dw ?     ; codec registers
  150.   .reg_master_vol     dw ?     ;0x02
  151.   .reg_aux_out_vol    dw ?     ;0x04
  152.   .reg_mone_vol       dw ?     ;0x06
  153.   .reg_master_tone    dw ?     ;0x08
  154.   .reg_beep_vol       dw ?     ;0x0A
  155.   .reg_phone_vol      dw ?     ;0x0C
  156.   .reg_mic_vol        dw ?     ;0x0E
  157.   .reg_line_in_vol    dw ?     ;0x10
  158.   .reg_cd_vol         dw ?     ;0x12
  159.   .reg_video_vol      dw ?     ;0x14
  160.   .reg_aux_in_vol     dw ?     ;0x16
  161.   .reg_pcm_out_vol    dw ?     ;0x18
  162.   .reg_rec_select     dw ?     ;0x1A
  163.   .reg_rec_gain       dw ?     ;0x1C
  164.   .reg_rec_gain_mic   dw ?     ;0x1E
  165.   .reg_gen            dw ?     ;0x20
  166.   .reg_3d_ctrl        dw ?     ;0X22
  167.   .reg_page           dw ?     ;0X24
  168.   .reg_powerdown      dw ?     ;0x26
  169.   .reg_ext_audio      dw ?     ;0x28
  170.   .reg_ext_st         dw ?     ;0x2a
  171.   .reg_pcm_front_rate dw ?     ;0x2c
  172.   .reg_pcm_surr_rate  dw ?     ;0x2e
  173.   .reg_lfe_rate       dw ?     ;0x30
  174.   .reg_pcm_in_rate    dw ?     ;0x32
  175.                       dw ?     ;0x34
  176.   .reg_cent_lfe_vol   dw ?     ;0x36
  177.   .reg_surr_vol       dw ?     ;0x38
  178.   .reg_spdif_ctrl     dw ?     ;0x3A
  179.                       dw ?     ;0x3C
  180.                       dw ?     ;0x3E
  181.                       dw ?     ;0x40
  182.                       dw ?     ;0x42
  183.                       dw ?     ;0x44
  184.                       dw ?     ;0x46
  185.                       dw ?     ;0x48
  186.                       dw ?     ;0x4A
  187.                       dw ?     ;0x4C
  188.                       dw ?     ;0x4E
  189.                       dw ?     ;0x50
  190.                       dw ?     ;0x52
  191.                       dw ?     ;0x54
  192.                       dw ?     ;0x56
  193.                       dw ?     ;0x58
  194.                       dw ?     ;0x5A
  195.                       dw ?     ;0x5C
  196.                       dw ?     ;0x5E
  197.   .reg_page_0         dw ?     ;0x60
  198.   .reg_page_1         dw ?     ;0x62
  199.   .reg_page_2         dw ?     ;0x64
  200.   .reg_page_3         dw ?     ;0x66
  201.   .reg_page_4         dw ?     ;0x68
  202.   .reg_page_5         dw ?     ;0x6A
  203.   .reg_page_6         dw ?     ;0x6C
  204.   .reg_page_7         dw ?     ;0x6E
  205.                       dw ?     ;0x70
  206.                       dw ?     ;0x72
  207.                       dw ?     ;0x74
  208.                       dw ?     ;0x76
  209.                       dw ?     ;0x78
  210.                       dw ?     ;0x7A
  211.   .reg_vendor_id_1    dw ?     ;0x7C
  212.   .reg_vendor_id_2    dw ?     ;0x7E
  213.  
  214.  
  215.   .reset              dd ?    ;virual
  216.   .set_master_vol     dd ?
  217. }
  218.  
  219. struc CTRL_INFO
  220. {   .pci_cmd        dd  ?
  221.     .irq            dd  ?
  222.     .glob_cntrl     dd  ?
  223.     .glob_sta       dd  ?
  224.     .codec_io_base  dd  ?
  225.     .ctrl_io_base   dd  ?
  226.     .codec_mem_base dd  ?
  227.     .ctrl_mem_base  dd  ?
  228.     .codec_id       dd  ?
  229. }
  230.  
  231. struc IOCTL
  232. {  .handle           dd ?
  233.    .io_code          dd ?
  234.    .input            dd ?
  235.    .inp_size         dd ?
  236.    .output           dd ?
  237.    .out_size         dd ?
  238. }
  239.  
  240. virtual at 0
  241.   IOCTL IOCTL
  242. end virtual
  243.  
  244. EVENT_NOTIFY          equ 0x00000200
  245.  
  246. OS_BASE               equ 0;  0x80400000
  247. new_app_base          equ 0x60400000;   0x01000000
  248. PROC_BASE             equ OS_BASE+0x0080000
  249.  
  250. public START
  251. public service_proc
  252. public version
  253.  
  254. extrn AttachIntHandler
  255. extrn SysMsgBoardStr
  256. extrn PciApi
  257. extrn PciRead32
  258. extrn PciRead8
  259. extrn PciWrite8
  260. extrn AllocKernelSpace
  261. extrn MapPage
  262. extrn RegService
  263. extrn KernelAlloc
  264. extrn GetPgAddr
  265. extrn GetCurrentTask
  266.  
  267. section '.flat' code readable align 16
  268.  
  269. proc START stdcall, state:dword
  270.  
  271.            cmp [state], 1
  272.            jne .stop
  273.  
  274.      if DEBUG
  275.            mov esi, msgInit
  276.            call SysMsgBoardStr
  277.      end if
  278.  
  279.            call detect_controller
  280.            test eax, eax
  281.            jz .fail
  282.  
  283.      if DEBUG
  284.            mov esi,[ctrl.vendor_ids]
  285.            call SysMsgBoardStr
  286.            mov  esi, [ctrl.ctrl_ids]
  287.            call SysMsgBoardStr
  288.      end if
  289.  
  290.            call init_controller
  291.            test eax, eax
  292.            jz .fail
  293.  
  294.      if DEBUG
  295.            mov esi, msgInitCodec
  296.            call SysMsgBoardStr
  297.      end if
  298.  
  299.            call init_codec
  300.            test eax, eax
  301.            jz .fail
  302.  
  303.      if DEBUG
  304.            mov esi, [codec.ac_vendor_ids]
  305.            call SysMsgBoardStr
  306.  
  307.            mov esi, [codec.chip_ids]
  308.            call SysMsgBoardStr
  309.      end if
  310.  
  311.            call reset_controller
  312.            call setup_codec
  313.  
  314.            mov esi, msgPrimBuff
  315.            call SysMsgBoardStr
  316.  
  317.            call create_primary_buff
  318.  
  319.            stdcall AttachIntHandler, [ctrl.int_line], ac97_irq
  320.  
  321.            stdcall RegService, sz_sound_srv, service_proc
  322.  
  323.            mov esi, msgOk
  324.            call SysMsgBoardStr
  325.            ret
  326. .fail:
  327.    if DEBUG
  328.            mov esi, msgFail
  329.            call SysMsgBoardStr
  330.    end if
  331.            xor eax, eax
  332.            ret
  333. .stop:
  334.            call stop
  335.            xor eax, eax
  336.            ret
  337. endp
  338.  
  339. handle     equ  IOCTL.handle
  340. io_code    equ  IOCTL.io_code
  341. input      equ  IOCTL.input
  342. inp_size   equ  IOCTL.inp_size
  343. output     equ  IOCTL.output
  344. out_size   equ  IOCTL.out_size
  345.  
  346. align 4
  347. proc service_proc stdcall, ioctl:dword
  348.  
  349.            mov edi, [ioctl]
  350.            mov eax, [edi+io_code]
  351.            cmp eax, DEV_PLAY
  352.            jne @F
  353.      if DEBUG
  354.            mov esi, msgPlay
  355.            call SysMsgBoardStr
  356.      end if
  357.            call play
  358.            ret
  359. @@:
  360.            cmp eax, DEV_STOP
  361.            jne @F
  362.      if DEBUG
  363.            mov esi, msgStop
  364.            call SysMsgBoardStr
  365.      end if
  366.            call stop
  367.            ret
  368. @@:
  369.            cmp eax, DEV_CALLBACK
  370.            jne @F
  371.            mov ebx, [edi+input]
  372.            stdcall set_callback, [ebx]
  373.            ret
  374. @@:
  375.            cmp eax, DEV_SET_MASTERVOL
  376.            jne @F
  377.            mov ebx, [edi+input]
  378.            stdcall set_master_vol, [ebx]
  379.            ret
  380. @@:
  381.            cmp eax, DEV_GET_MASTERVOL
  382.            jne @F
  383.            mov ebx, [edi+output]
  384.            test ebx, ebx
  385.            jz .fail
  386.  
  387.            stdcall get_master_vol, ebx
  388.            ret
  389. @@:
  390.            cmp eax, DEV_GET_INFO
  391.            jne @F
  392.            mov ebx, [edi+output]
  393.            stdcall get_dev_info, ebx
  394.            ret
  395. @@:
  396. .fail:
  397.            xor eax, eax
  398.            ret
  399. endp
  400.  
  401. restore   handle
  402. restore   io_code
  403. restore   input
  404. restore   inp_size
  405. restore   output
  406. restore   out_size
  407.  
  408. align 4
  409. proc ac97_irq
  410.  
  411. ;     if DEBUG
  412. ;           mov esi, msgIRQ
  413. ;           call SysMsgBoardStr
  414. ;     end if
  415.  
  416.            mov edx, PCM_OUT_CR_REG
  417.            mov al, 0x14
  418.            call [ctrl.ctrl_write8]
  419.  
  420.            mov ax, 0x1c
  421.            mov edx, PCM_OUT_SR_REG
  422.            call [ctrl.ctrl_write16]
  423.  
  424.            mov edx, PCM_OUT_CIV_REG
  425.            call [ctrl.ctrl_read8]
  426.  
  427.            and eax, 0x1F
  428.            cmp eax, [civ_val]
  429.            je .skip
  430.  
  431.            mov [civ_val], eax
  432.            dec eax
  433.            and eax, 0x1F
  434.            mov [ctrl.lvi_reg], eax
  435.  
  436.            mov edx, PCM_OUT_LVI_REG
  437.            call [ctrl.ctrl_write8]
  438.  
  439.            mov edx, PCM_OUT_CR_REG
  440.            mov ax, 0x1D
  441.            call [ctrl.ctrl_write8]
  442.  
  443.            mov eax, [civ_val]
  444.            add eax, 2
  445.            and eax, 31
  446.            mov ebx, dword [buff_list+eax*4]
  447.  
  448.            cmp [ctrl.user_callback], 0
  449.            je @f
  450.  
  451.            stdcall [ctrl.user_callback], ebx
  452. @@:
  453.            ret
  454.  
  455. .skip:
  456.            mov edx, PCM_OUT_CR_REG
  457.            mov ax, 0x1D
  458.            call [ctrl.ctrl_write8]
  459.            ret
  460. endp
  461.  
  462. align 4
  463. proc create_primary_buff
  464.  
  465.            stdcall KernelAlloc, 0x10000
  466.            mov [ctrl.buffer], eax
  467.  
  468.            mov edi, eax
  469.            mov ecx, 0x10000/4
  470.            xor eax, eax
  471.            rep stosd
  472.  
  473.            stdcall GetPgAddr, [ctrl.buffer]
  474.  
  475.            mov ebx, 0xC0004000
  476.            mov ecx, 4
  477.            mov edi, pcmout_bdl
  478. @@:
  479.            mov [edi], eax
  480.            mov [edi+4], ebx
  481.            mov [edi+32], eax
  482.            mov [edi+4+32], ebx
  483.            mov [edi+64], eax
  484.            mov [edi+4+64], ebx
  485.            mov [edi+96], eax
  486.            mov [edi+4+96], ebx
  487.            mov [edi+128], eax
  488.            mov [edi+4+128], ebx
  489.            mov [edi+160], eax
  490.            mov [edi+4+160], ebx
  491.            mov [edi+192], eax
  492.            mov [edi+4+192], ebx
  493.            mov [edi+224], eax
  494.            mov [edi+4+224], ebx
  495.  
  496.            add eax, 0x4000
  497.            add edi, 8
  498.            loop @B
  499.  
  500.            mov edi, buff_list
  501.            mov eax, [ctrl.buffer]
  502.            mov ecx, 4
  503. @@:
  504.            mov [edi], eax
  505.            mov [edi+16], eax
  506.            mov [edi+32], eax
  507.            mov [edi+48], eax
  508.            mov [edi+64], eax
  509.            mov [edi+80], eax
  510.            mov [edi+96], eax
  511.            mov [edi+112], eax
  512.  
  513.            add eax, 0x4000
  514.            add edi, 4
  515.            loop @B
  516.  
  517.            mov ecx, pcmout_bdl
  518.            stdcall GetPgAddr, ecx
  519.            and ecx, 0xFFF
  520.            add eax, ecx
  521.  
  522.            mov edx, PCM_OUT_BDL
  523.            call [ctrl.ctrl_write32]
  524.  
  525.            mov eax, 16
  526.            mov [ctrl.lvi_reg], eax
  527.            mov edx, PCM_OUT_LVI_REG
  528.            call [ctrl.ctrl_write8]
  529.  
  530.            mov edx, GLOB_CTRL
  531.            call [ctrl.ctrl_read32]
  532.            and eax, not 0x000000C0
  533.            mov edx, GLOB_CTRL
  534.            call [ctrl.ctrl_write32]
  535.  
  536.  
  537.            ret
  538. endp
  539.  
  540. align 4
  541. proc detect_controller
  542.            locals
  543.              last_bus dd ?
  544.              bus      dd ?
  545.              devfn    dd ?
  546.            endl
  547.  
  548.            xor eax, eax
  549.            mov [bus], eax
  550.            inc eax
  551.            call PciApi
  552.            cmp eax, -1
  553.            je .err
  554.  
  555.            mov [last_bus], eax
  556.  
  557. .next_bus:
  558.            and [devfn], 0
  559. .next_dev:
  560.            stdcall PciRead32, [bus], [devfn], dword 0
  561.            test eax, eax
  562.            jz .next
  563.            cmp eax, -1
  564.            je .next
  565.            mov edi, devices
  566. @@:
  567.            mov ebx, [edi]
  568.            test ebx, ebx
  569.            jz .next
  570.  
  571.            cmp eax, ebx
  572.            je .found
  573.            add edi, 12
  574.            jmp @B
  575.  
  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_SIS
  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.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x10
  623.            and eax,0xFFFE
  624.            mov [ctrl.codec_io_base], eax
  625.  
  626.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x14
  627.            and eax, 0xFFC0
  628.            mov [ctrl.ctrl_io_base], eax
  629.  
  630.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x18
  631.            mov [ctrl.codec_mem_base], eax
  632.  
  633.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x1C
  634.            mov [ctrl.ctrl_mem_base], eax
  635.  
  636.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x3C
  637.            and eax, 0xFF
  638.            mov [ctrl.int_line], eax
  639.  
  640.            stdcall PciRead8, [ctrl.bus], [ctrl.devfn], dword 0x41
  641.            and eax, 0xFF
  642.            mov [ctrl.cfg_reg], eax
  643.  
  644.            call [ctrl.ctrl_setup]
  645.            xor eax, eax
  646.            inc eax
  647.            ret
  648. endp
  649.  
  650. align 4
  651. proc set_SIS
  652.            mov [ctrl.codec_read16],  codec_io_r16    ;virtual
  653.            mov [ctrl.codec_write16], codec_io_w16    ;virtual
  654.  
  655.            mov [ctrl.ctrl_read8 ],  ctrl_io_r8      ;virtual
  656.            mov [ctrl.ctrl_read16],  ctrl_io_r16      ;virtual
  657.            mov [ctrl.ctrl_read32],  ctrl_io_r32      ;virtual
  658.  
  659.            mov [ctrl.ctrl_write8 ], ctrl_io_w8      ;virtual
  660.            mov [ctrl.ctrl_write16], ctrl_io_w16      ;virtual
  661.            mov [ctrl.ctrl_write32], ctrl_io_w32      ;virtual
  662.            ret
  663. endp
  664.  
  665. align 4
  666. proc reset_controller
  667.  
  668.            xor eax, eax
  669.            mov edx, PCM_IN_CR_REG
  670.            call [ctrl.ctrl_write8]
  671.  
  672.            mov edx, PCM_OUT_CR_REG
  673.            call [ctrl.ctrl_write8]
  674.  
  675.            mov edx, MC_IN_CR_REG
  676.            call [ctrl.ctrl_write8]
  677.  
  678.            mov eax, RR
  679.            mov edx, PCM_IN_CR_REG
  680.            call [ctrl.ctrl_write8]
  681.  
  682.            mov edx, PCM_OUT_CR_REG
  683.            call [ctrl.ctrl_write8]
  684.  
  685.            mov edx, MC_IN_CR_REG
  686.            call [ctrl.ctrl_write8]
  687.            ret
  688. endp
  689.  
  690. align 4
  691. proc init_codec
  692.            locals
  693.              counter dd ?
  694.            endl
  695.  
  696.            mov edx, CTRL_STAT
  697.            call [ctrl.ctrl_read32]
  698.            test eax, CTRL_ST_CREADY
  699.            jnz .ready
  700.  
  701.            call reset_codec
  702.            and eax, eax
  703.            jz .err
  704.  
  705.            xor edx, edx     ;ac_reg_0
  706.            call [ctrl.codec_write16]
  707.  
  708.            xor eax, eax
  709.            mov edx, CODEC_REG_POWERDOWN
  710.            call [ctrl.codec_write16]
  711.  
  712.            mov [counter], 200     ; total 200*5 ms = 1s
  713. .wait:
  714.            mov edx, CODEC_REG_POWERDOWN
  715.            call [ctrl.codec_read16]
  716.            and eax, 0x0F
  717.            cmp eax, 0x0F
  718.            je .ready
  719.  
  720.            mov eax, 5000          ; wait 5 ms
  721.            call StallExec
  722.            sub [counter] , 1
  723.            jnz .wait
  724. .err:
  725.            xor eax, eax        ; timeout error
  726.            ret
  727. .ready:
  728.            call detect_codec
  729.  
  730.            xor eax, eax
  731.            inc eax
  732.            ret
  733. endp
  734.  
  735. align 4
  736. proc reset_codec
  737.            mov edx, GLOB_CTRL
  738.            call [ctrl.ctrl_read32]
  739.  
  740.            test eax, 0x02
  741.            jz .cold
  742.  
  743.            call warm_reset
  744.            jnc .ok
  745. .cold:
  746.            call cold_reset
  747.            jnc .ok
  748.  
  749.      if DEBUG
  750.            mov esi, msgCFail
  751.            call SysMsgBoardStr
  752.      end if
  753.            xor eax, eax     ; timeout error
  754.            ret
  755. .ok:
  756.            xor eax, eax
  757.            inc eax
  758.            ret
  759. endp
  760.  
  761. align 4
  762. proc warm_reset
  763.            locals
  764.              counter dd ?
  765.            endl
  766.  
  767.            mov eax, 0x06
  768.            mov edx, GLOB_CTRL
  769.            call [ctrl.ctrl_write32]
  770.  
  771.      if DEBUG
  772.            mov esi, msgWarm
  773.            call SysMsgBoardStr
  774.      end if
  775.  
  776.            mov [counter], 10    ; total 10*100 ms = 1s
  777. .wait:
  778.            mov eax, 100000    ; wait 100 ms
  779.            call StallExec
  780.  
  781.            mov edx, GLOB_CTRL
  782.            call [ctrl.ctrl_read32]
  783.            test eax, 4
  784.            jz .ok
  785.            sub [counter], 1
  786.            jnz .wait
  787.  
  788.      if DEBUG
  789.            mov esi, msgWRFail
  790.            call SysMsgBoardStr
  791.      end if
  792.            stc
  793.            ret
  794. .ok:
  795.            mov edx, CTRL_STAT
  796.            call [ctrl.ctrl_read32]
  797.            and eax, CTRL_ST_CREADY
  798.            jz .fail
  799.            clc
  800.            ret
  801. .fail:
  802.            stc
  803.            ret
  804. endp
  805.  
  806. align 4
  807. proc cold_reset
  808.            locals
  809.              counter dd ?
  810.             endl
  811.  
  812.            mov edx, GLOB_CTRL
  813.            call [ctrl.ctrl_read32]
  814.            and eax, not 0x08
  815.            or eax, 0x02
  816.            mov edx, GLOB_CTRL
  817.            call [ctrl.ctrl_write32]
  818.  
  819.      if DEBUG
  820.            mov esi, msgCold
  821.            call SysMsgBoardStr
  822.      end if
  823.  
  824.            mov [counter], 10    ; total 10*100 ms = 1s
  825. .wait:
  826.            mov eax, 100000    ; wait 100 ms
  827.            call StallExec
  828.  
  829.            mov edx, GLOB_CTRL
  830.            call [ctrl.ctrl_read32]
  831.            test eax, 4
  832.            jz .ok
  833.            sub [counter], 1
  834.            jnz .wait
  835.  
  836.      if DEBUG
  837.            mov esi, msgCRFail
  838.            call SysMsgBoardStr
  839.      end if
  840. .fail:
  841.            stc
  842.            ret
  843. .ok:
  844.            mov edx, CTRL_STAT
  845.            call [ctrl.ctrl_read32]
  846.            and eax, CTRL_ST_CREADY
  847.            jz .fail
  848.            clc
  849.            ret
  850. endp
  851.  
  852. align 4
  853. proc play
  854.            xor eax, eax
  855.            mov [civ_val], eax
  856.            mov edx, PCM_OUT_CIV_REG
  857.            call [ctrl.ctrl_write8]
  858.  
  859.            mov eax, 16
  860.            mov [ctrl.lvi_reg], eax
  861.            mov edx, PCM_OUT_LVI_REG
  862.            call [ctrl.ctrl_write8]
  863.  
  864.            mov edx, PCM_OUT_CR_REG
  865.            mov ax, 0x1D
  866.            call [ctrl.ctrl_write8]
  867.            ret
  868. endp
  869.  
  870. align 4
  871. proc stop
  872.            mov edx, PCM_OUT_CR_REG
  873.            mov ax, 0x0
  874.            call [ctrl.ctrl_write8]
  875.  
  876.            mov ax, 0x1c
  877.            mov edx, PCM_OUT_SR_REG
  878.            call [ctrl.ctrl_write16]
  879.            ret
  880. endp
  881.  
  882. align 4
  883. proc get_dev_info stdcall, p_info:dword
  884.            virtual at esi
  885.              CTRL_INFO CTRL_INFO
  886.            end virtual
  887.  
  888.            mov esi, [p_info]
  889.            mov eax, [ctrl.int_line]
  890.            mov ebx, [ctrl.codec_io_base]
  891.            mov ecx, [ctrl.ctrl_io_base]
  892.            mov edx, [ctrl.codec_mem_base]
  893.            mov edi, [ctrl.ctrl_mem_base]
  894.  
  895.            mov [CTRL_INFO.irq], eax
  896.            mov [CTRL_INFO.codec_io_base], ebx
  897.            mov [CTRL_INFO.ctrl_io_base], ecx
  898.            mov [CTRL_INFO.codec_mem_base], edx
  899.            mov [CTRL_INFO.ctrl_mem_base], edi
  900.  
  901.            mov eax, [codec.chip_id]
  902.            mov [CTRL_INFO.codec_id], eax
  903.  
  904.            mov edx, GLOB_CTRL
  905.            call [ctrl.ctrl_read32]
  906.            mov [CTRL_INFO.glob_cntrl], eax
  907.  
  908.            mov edx, CTRL_STAT
  909.            call [ctrl.ctrl_read32]
  910.            mov [CTRL_INFO.glob_sta], eax
  911.  
  912.            mov ebx, [ctrl.pci_cmd]
  913.            mov [CTRL_INFO.pci_cmd], ebx
  914.  
  915.            ret
  916. endp
  917.  
  918. align 4
  919. proc set_callback stdcall, handler:dword
  920.            mov eax, [handler]
  921.            mov [ctrl.user_callback], eax
  922.            ret
  923. endp
  924.  
  925. align 4
  926. proc codec_read stdcall, ac_reg:dword      ; reg = edx, reval = eax
  927.  
  928.            mov edx, [ac_reg]
  929.  
  930.            mov ebx, edx
  931.            shr ebx, 1
  932.            bt [codec.shadow_flag], ebx
  933.            jc .use_shadow
  934.  
  935.            call [ctrl.codec_read16]  ;change edx !!!
  936.            mov ecx, eax
  937.  
  938.            mov edx, CTRL_STAT
  939.            call [ctrl.ctrl_read32]
  940.            test eax, CTRL_ST_RCS
  941.            jz .read_ok
  942.  
  943.            mov edx, CTRL_STAT
  944.            call [ctrl.ctrl_write32]
  945.            xor eax,eax
  946.            not eax  ;timeout
  947.            ret
  948. .read_ok:
  949.            mov edx, [ac_reg]
  950.            mov [codec.regs+edx], cx
  951.            bts [codec.shadow_flag], ebx
  952.            mov eax, ecx
  953.            ret
  954. .use_shadow:
  955.            movzx eax, word [codec.regs+edx]
  956.            ret
  957. endp
  958.  
  959. align 4
  960. proc codec_write stdcall, ac_reg:dword
  961.            push eax
  962.            call check_semafore
  963.            and eax, eax
  964.            jz .err
  965.            pop eax
  966.  
  967.            mov esi, [ac_reg]
  968.            mov edx, esi
  969.            call [ctrl.codec_write16]
  970.            mov [codec.regs+esi], ax
  971.            shr esi, 1
  972.            bts [codec.shadow_flag], esi
  973.            ret
  974. .err:
  975.            pop eax
  976.            ret
  977. endp
  978.  
  979.  
  980. align 4
  981. proc codec_check_ready
  982.  
  983.            mov edx, CTRL_ST
  984.            call [ctrl.ctrl_read32]
  985.            and eax, CTRL_ST_CREADY
  986.            jz .not_ready
  987.  
  988.            xor eax, wax
  989.            inc eax
  990.            ret
  991.  
  992. align 4
  993. .not_ready:
  994.            xor eax, eax
  995.            ret
  996. endp
  997.  
  998.  
  999. align 4
  1000. proc check_semafore
  1001.            local counter:DWORD
  1002.  
  1003.            mov [counter], 100
  1004. .l1:
  1005.            mov edx, CTRL_CAS
  1006.            call [ctrl.ctrl_read8]
  1007.            and eax, CAS_FLAG
  1008.            jz .ok
  1009.  
  1010.            mov eax, 1
  1011.            call StallExec
  1012.            sub [counter], 1
  1013.            jnz .l1
  1014.            xor eax, eax
  1015.            ret
  1016. align 4
  1017. .ok:
  1018.            xor eax,eax
  1019.            inc eax
  1020.            ret
  1021. endp
  1022.  
  1023. align 4
  1024. proc StallExec
  1025.            push ecx
  1026.            push edx
  1027.            push ebx
  1028.            push eax
  1029.  
  1030.            mov ecx, CPU_FREQ
  1031.            mul ecx
  1032.            mov ebx, eax       ;low
  1033.            mov ecx, edx       ;high
  1034.            rdtsc
  1035.            add ebx, eax
  1036.            adc ecx, edx
  1037. @@:
  1038.            rdtsc
  1039.            sub eax, ebx
  1040.            sbb edx, ecx
  1041.            js @B
  1042.  
  1043.            pop eax
  1044.            pop ebx
  1045.            pop edx
  1046.            pop ecx
  1047.            ret
  1048. endp
  1049.  
  1050. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1051. ;          CONTROLLER IO functions
  1052. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1053.  
  1054. align 4
  1055. proc codec_io_r16
  1056.            add edx, [ctrl.codec_io_base]
  1057.            in  ax, dx
  1058.            ret
  1059. endp
  1060.  
  1061. align 4
  1062. proc codec_io_w16
  1063.            add edx, [ctrl.codec_io_base]
  1064.            out dx, ax
  1065.            ret
  1066. endp
  1067.  
  1068. align 4
  1069. proc ctrl_io_r8
  1070.            add edx, [ctrl.ctrl_io_base]
  1071.            in  al, dx
  1072.            ret
  1073. endp
  1074.  
  1075. align 4
  1076. proc ctrl_io_r16
  1077.            add edx, [ctrl.ctrl_io_base]
  1078.            in  ax, dx
  1079.            ret
  1080. endp
  1081.  
  1082. align 4
  1083. proc ctrl_io_r32
  1084.            add edx, [ctrl.ctrl_io_base]
  1085.            in  eax, dx
  1086.            ret
  1087. endp
  1088.  
  1089. align 4
  1090. proc ctrl_io_w8
  1091.            add edx, [ctrl.ctrl_io_base]
  1092.            out dx, al
  1093.            ret
  1094. endp
  1095.  
  1096. align 4
  1097. proc ctrl_io_w16
  1098.            add edx, [ctrl.ctrl_io_base]
  1099.            out dx, ax
  1100.            ret
  1101. endp
  1102.  
  1103. align 4
  1104. proc ctrl_io_w32
  1105.            add edx, [ctrl.ctrl_io_base]
  1106.            out dx, eax
  1107.            ret
  1108. endp
  1109.  
  1110. include "codec.inc"
  1111.  
  1112. align 4
  1113. devices dd (CTRL_SIS  shl 16)+VID_SIS,msg_AC, set_SIS
  1114.         dd 0
  1115.  
  1116. version      dd 0x00010001
  1117.  
  1118. msg_AC       db '7012 AC97 controller',13,10, 0
  1119. msg_SIS      db 'Silicon Integrated Systems',13,10, 0
  1120.  
  1121. sz_sound_srv        db 'SOUND',0
  1122.  
  1123. msgInit      db 'detect hardware...',13,10,0
  1124. msgFail      db 'device not found',13,10,0
  1125. msgPlay      db 'start play', 13,10,0
  1126. msgStop      db 'stop play',  13,10,0
  1127. msgNotify    db 'call notify',13,10,0
  1128. msgIRQ       db 'AC97 IRQ', 13,10,0
  1129. msgInitCtrl  db 'init controller',13,10,0
  1130. msgInitCodec db 'init codec',13,10,0
  1131. msgPrimBuff  db 'create primary buffer',13,10,0
  1132. msgReg       db 'set service handler',13,10,0
  1133. msgOk        db 'service installed',13,10,0
  1134. msgCold      db 'cold resret',13,10,0
  1135. msgWarm      db 'warm reset',13,10,0
  1136. msgWRFail    db 'warm reset failed',13,10,0
  1137. msgCRFail    db 'cold reset failed',13,10,0
  1138. msgCFail     db 'codec not ready',13,10,0
  1139.  
  1140. section '.data' data readable writable align 16
  1141.  
  1142. pcmout_bdl       rq 32
  1143. buff_list        rd 32
  1144.  
  1145. codec CODEC
  1146. ctrl AC_CNTRL
  1147.  
  1148. lpc_bus  rd 1
  1149. civ_val  rd 1
  1150.