Subversion Repositories Kolibri OS

Rev

Rev 224 | 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.           call reset_codec
  697.           and eax, eax
  698.           jz .err
  699.  
  700.           xor edx, edx     ;ac_reg_0
  701.           call [ctrl.codec_write16]
  702.  
  703.           xor eax, eax
  704.           mov edx, CODEC_REG_POWERDOWN
  705.           call [ctrl.codec_write16]
  706.  
  707.           mov [counter], 200     ; total 200*5 ms = 1s
  708. .wait:
  709.           mov edx, CODEC_REG_POWERDOWN
  710.           call [ctrl.codec_read16]
  711.           and eax, 0x0F
  712.           cmp eax, 0x0F
  713.           jz .ready
  714.  
  715.           mov eax, 5000          ; wait 5 ms
  716.           call StallExec
  717.           sub [counter] , 1
  718.           jnz .wait
  719. .err:
  720.           xor eax, eax        ; timeout error
  721.           ret
  722. .ready:
  723.           call detect_codec
  724.  
  725.           xor eax, eax
  726.           inc eax
  727.           ret
  728. endp
  729.  
  730. align 4
  731. proc reset_codec
  732.            mov edx, GLOB_CTRL
  733.            call [ctrl.ctrl_read32]
  734.  
  735.            test eax, 0x02
  736.            jz .cold
  737.  
  738.            call warm_reset
  739.            jnc .ok
  740. .cold:
  741.            call cold_reset
  742.            jnc .ok
  743.  
  744.      if DEBUG
  745.            mov esi, msgCFail
  746.            call SysMsgBoardStr
  747.      end if
  748.            xor eax, eax     ; timeout error
  749.            ret
  750. .ok:
  751.            xor eax, eax
  752.            inc eax
  753.            ret
  754. endp
  755.  
  756. align 4
  757. proc warm_reset
  758.            locals
  759.              counter dd ?
  760.            endl
  761.  
  762.            mov eax, 0x06
  763.            mov edx, GLOB_CTRL
  764.            call [ctrl.ctrl_write32]
  765.  
  766.      if DEBUG
  767.            mov esi, msgWarm
  768.            call SysMsgBoardStr
  769.      end if
  770.  
  771.            mov [counter], 10    ; total 10*100 ms = 1s
  772. .wait:
  773.            mov eax, 100000    ; wait 100 ms
  774.            call StallExec
  775.  
  776.            mov edx, GLOB_CTRL
  777.            call [ctrl.ctrl_read32]
  778.            test eax, 4
  779.            jz .ok
  780.            sub [counter], 1
  781.            jnz .wait
  782.  
  783.      if DEBUG
  784.            mov esi, msgWRFail
  785.            call SysMsgBoardStr
  786.      end if
  787.            stc
  788.            ret
  789. .ok:
  790.            mov edx, CTRL_STAT
  791.            call [ctrl.ctrl_read32]
  792.            and eax, CTRL_ST_CREADY
  793.            jz .fail
  794.            clc
  795.            ret
  796. .fail:
  797.            stc
  798.            ret
  799. endp
  800.  
  801. align 4
  802. proc cold_reset
  803.            locals
  804.              counter dd ?
  805.             endl
  806.  
  807.            xor eax, eax
  808.            mov edx, GLOB_CTRL
  809.            call [ctrl.ctrl_write32]
  810.  
  811.      if DEBUG
  812.            mov esi, msgCold
  813.            call SysMsgBoardStr
  814.      end if
  815.  
  816.            mov eax, 1000000    ; wait 1 s
  817.            call StallExec
  818.  
  819.            mov eax, 2
  820.            mov edx, GLOB_CTRL
  821.            call [ctrl.ctrl_write32]
  822.  
  823.            mov [counter], 10    ; total 10*100 ms = 1s
  824. .wait:
  825.            mov eax, 100000    ; wait 100 ms
  826.            call StallExec
  827.  
  828.            mov edx, GLOB_CTRL
  829.            call [ctrl.ctrl_read32]
  830.            test eax, 4
  831.            jz .ok
  832.            sub [counter], 1
  833.            jnz .wait
  834.  
  835.      if DEBUG
  836.            mov esi, msgCRFail
  837.            call SysMsgBoardStr
  838.      end if
  839.            stc
  840.            ret
  841. .ok:
  842.            mov edx, CTRL_STAT
  843.            call [ctrl.ctrl_read32]
  844.            and eax, CTRL_ST_CREADY
  845.            jz .fail
  846.            clc
  847.            ret
  848. .fail:
  849.            stc
  850.            ret
  851. endp
  852.  
  853. align 4
  854. proc play
  855.            xor eax, eax
  856.            mov [civ_val], eax
  857.            mov edx, PCM_OUT_CIV_REG
  858.            call [ctrl.ctrl_write8]
  859.  
  860.            mov eax, 16
  861.            mov [ctrl.lvi_reg], eax
  862.            mov edx, PCM_OUT_LVI_REG
  863.            call [ctrl.ctrl_write8]
  864.  
  865.            mov edx, PCM_OUT_CR_REG
  866.            mov ax, 0x1D
  867.            call [ctrl.ctrl_write8]
  868.            ret
  869. endp
  870.  
  871. align 4
  872. proc stop
  873.            mov edx, PCM_OUT_CR_REG
  874.            mov ax, 0x0
  875.            call [ctrl.ctrl_write8]
  876.  
  877.            mov ax, 0x1c
  878.            mov edx, PCM_OUT_SR_REG
  879.            call [ctrl.ctrl_write16]
  880.            ret
  881. endp
  882.  
  883. align 4
  884. proc get_dev_info stdcall, p_info:dword
  885.            virtual at esi
  886.              CTRL_INFO CTRL_INFO
  887.            end virtual
  888.  
  889.            mov esi, [p_info]
  890.            mov eax, [ctrl.int_line]
  891.            mov ebx, [ctrl.codec_io_base]
  892.            mov ecx, [ctrl.ctrl_io_base]
  893.            mov edx, [ctrl.codec_mem_base]
  894.            mov edi, [ctrl.ctrl_mem_base]
  895.  
  896.            mov [CTRL_INFO.irq], eax
  897.            mov [CTRL_INFO.codec_io_base], ebx
  898.            mov [CTRL_INFO.ctrl_io_base], ecx
  899.            mov [CTRL_INFO.codec_mem_base], edx
  900.            mov [CTRL_INFO.ctrl_mem_base], edi
  901.  
  902.            mov eax, [codec.chip_id]
  903.            mov [CTRL_INFO.codec_id], eax
  904.  
  905.            mov edx, GLOB_CTRL
  906.            call [ctrl.ctrl_read32]
  907.            mov [CTRL_INFO.glob_cntrl], eax
  908.  
  909.            mov edx, CTRL_STAT
  910.            call [ctrl.ctrl_read32]
  911.            mov [CTRL_INFO.glob_sta], eax
  912.  
  913.            mov ebx, [ctrl.pci_cmd]
  914.            mov [CTRL_INFO.pci_cmd], ebx
  915.  
  916.            ret
  917. endp
  918.  
  919. align 4
  920. proc set_callback stdcall, handler:dword
  921.            mov eax, [handler]
  922.            mov [ctrl.user_callback], eax
  923.            ret
  924. endp
  925.  
  926. align 4
  927. proc codec_read stdcall, ac_reg:dword      ; reg = edx, reval = eax
  928.  
  929.            mov edx, [ac_reg]
  930.  
  931.            mov ebx, edx
  932.            shr ebx, 1
  933.            bt [codec.shadow_flag], ebx
  934.            jc .use_shadow
  935.  
  936.            call [ctrl.codec_read16]  ;change edx !!!
  937.            mov ecx, eax
  938.  
  939.            mov edx, CTRL_STAT
  940.            call [ctrl.ctrl_read32]
  941.            test eax, CTRL_ST_RCS
  942.            jz .read_ok
  943.  
  944.            mov edx, CTRL_STAT
  945.            call [ctrl.ctrl_write32]
  946.            xor eax,eax
  947.            not eax  ;timeout
  948.            ret
  949. .read_ok:
  950.            mov edx, [ac_reg]
  951.            mov [codec.regs+edx], cx
  952.            bts [codec.shadow_flag], ebx
  953.            mov eax, ecx
  954.            ret
  955. .use_shadow:
  956.            movzx eax, word [codec.regs+edx]
  957.            ret
  958. endp
  959.  
  960. align 4
  961. proc codec_write stdcall, ac_reg:dword
  962.            push eax
  963.            call check_semafore
  964.            and eax, eax
  965.            jz .err
  966.            pop eax
  967.  
  968.            mov esi, [ac_reg]
  969.            mov edx, esi
  970.            call [ctrl.codec_write16]
  971.            mov [codec.regs+esi], ax
  972.            shr esi, 1
  973.            bts [codec.shadow_flag], esi
  974.            ret
  975. .err:
  976.            pop eax
  977.            ret
  978. endp
  979.  
  980.  
  981. align 4
  982. proc codec_check_ready
  983.  
  984.            mov edx, CTRL_ST
  985.            call [ctrl.ctrl_read32]
  986.            and eax, CTRL_ST_CREADY
  987.            jz .not_ready
  988.  
  989.            xor eax, wax
  990.            inc eax
  991.            ret
  992.  
  993. align 4
  994. .not_ready:
  995.            xor eax, eax
  996.            ret
  997. endp
  998.  
  999.  
  1000. align 4
  1001. proc check_semafore
  1002.            local counter:DWORD
  1003.  
  1004.            mov [counter], 100
  1005. .l1:
  1006.            mov edx, CTRL_CAS
  1007.            call [ctrl.ctrl_read8]
  1008.            and eax, CAS_FLAG
  1009.            jz .ok
  1010.  
  1011.            mov eax, 1
  1012.            call StallExec
  1013.            sub [counter], 1
  1014.            jnz .l1
  1015.            xor eax, eax
  1016.            ret
  1017. align 4
  1018. .ok:
  1019.            xor eax,eax
  1020.            inc eax
  1021.            ret
  1022. endp
  1023.  
  1024. align 4
  1025. proc StallExec
  1026.            push ecx
  1027.            push edx
  1028.            push ebx
  1029.            push eax
  1030.  
  1031.            mov ecx, CPU_FREQ
  1032.            mul ecx
  1033.            mov ebx, eax       ;low
  1034.            mov ecx, edx       ;high
  1035.            rdtsc
  1036.            add ebx, eax
  1037.            adc ecx,edx
  1038. @@:
  1039.            rdtsc
  1040.            sub eax, ebx
  1041.            sbb edx, ecx
  1042.            jb @B
  1043.  
  1044.            pop eax
  1045.            pop ebx
  1046.            pop edx
  1047.            pop ecx
  1048.            ret
  1049. endp
  1050.  
  1051. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1052. ;          CONTROLLER IO functions
  1053. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1054.  
  1055. align 4
  1056. proc codec_io_r16
  1057.            add edx, [ctrl.codec_io_base]
  1058.            in  ax, dx
  1059.            ret
  1060. endp
  1061.  
  1062. align 4
  1063. proc codec_io_w16
  1064.            add edx, [ctrl.codec_io_base]
  1065.            out dx, ax
  1066.            ret
  1067. endp
  1068.  
  1069. align 4
  1070. proc ctrl_io_r8
  1071.            add edx, [ctrl.ctrl_io_base]
  1072.            in  al, dx
  1073.            ret
  1074. endp
  1075.  
  1076. align 4
  1077. proc ctrl_io_r16
  1078.            add edx, [ctrl.ctrl_io_base]
  1079.            in  ax, dx
  1080.            ret
  1081. endp
  1082.  
  1083. align 4
  1084. proc ctrl_io_r32
  1085.            add edx, [ctrl.ctrl_io_base]
  1086.            in  eax, dx
  1087.            ret
  1088. endp
  1089.  
  1090. align 4
  1091. proc ctrl_io_w8
  1092.            add edx, [ctrl.ctrl_io_base]
  1093.            out dx, al
  1094.            ret
  1095. endp
  1096.  
  1097. align 4
  1098. proc ctrl_io_w16
  1099.            add edx, [ctrl.ctrl_io_base]
  1100.            out dx, ax
  1101.            ret
  1102. endp
  1103.  
  1104. align 4
  1105. proc ctrl_io_w32
  1106.            add edx, [ctrl.ctrl_io_base]
  1107.            out dx, eax
  1108.            ret
  1109. endp
  1110.  
  1111. include "codec.inc"
  1112.  
  1113. align 4
  1114. devices dd (CTRL_SIS  shl 16)+VID_SIS,msg_AC, set_SIS
  1115.         dd 0
  1116.  
  1117. version      dd 0x00010001
  1118.  
  1119. msg_AC       db '7012 AC97 controller',13,10, 0
  1120. msg_SIS      db 'Silicon Integrated Systems',13,10, 0
  1121.  
  1122. sz_sound_srv        db 'SOUND',0
  1123.  
  1124. msgInit      db 'detect hardware...',13,10,0
  1125. msgFail      db 'device not found',13,10,0
  1126. msgPlay      db 'start play', 13,10,0
  1127. msgStop      db 'stop play',  13,10,0
  1128. msgNotify    db 'call notify',13,10,0
  1129. msgIRQ       db 'AC97 IRQ', 13,10,0
  1130. msgInitCtrl  db 'init controller',13,10,0
  1131. msgInitCodec db 'init codec',13,10,0
  1132. msgPrimBuff  db 'create primary buffer',13,10,0
  1133. msgReg       db 'set service handler',13,10,0
  1134. msgOk        db 'service installed',13,10,0
  1135. msgCold      db 'cold resret',13,10,0
  1136. msgWarm      db 'warm reset',13,10,0
  1137. msgWRFail    db 'warm reset failed',13,10,0
  1138. msgCRFail    db 'cold reset failed',13,10,0
  1139. msgCFail     db 'codec not ready',13,10,0
  1140.  
  1141. section '.data' data readable writable align 16
  1142.  
  1143. pcmout_bdl       rq 32
  1144. buff_list        rd 32
  1145.  
  1146. codec CODEC
  1147. ctrl AC_CNTRL
  1148.  
  1149. lpc_bus  rd 1
  1150. civ_val  rd 1
  1151.