Subversion Repositories Kolibri OS

Rev

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