Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. format MS COFF
  3.  
  4. include 'proc32.inc'
  5. include 'imports.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. DEV_PLAY              equ  1
  88. DEV_STOP              equ  2
  89. DEV_CALLBACK          equ  3
  90. DEV_SET_BUFF          equ  4
  91. DEV_NOTIFY            equ  5
  92. DEV_SET_MASTERVOL     equ  6
  93. DEV_GET_MASTERVOL     equ  7
  94. DEV_GET_INFO          equ  8
  95.  
  96. struc AC_CNTRL              ;AC controller base class
  97. { .bus                dd ?
  98.   .devfn              dd ?
  99.  
  100.   .vendor             dd ?
  101.   .dev_id             dd ?
  102.   .pci_cmd            dd ?
  103.   .pci_stat           dd ?
  104.  
  105.   .codec_io_base      dd ?
  106.   .codec_mem_base     dd ?
  107.  
  108.   .ctrl_io_base       dd ?
  109.   .ctrl_mem_base      dd ?
  110.   .cfg_reg            dd ?
  111.   .int_line           dd ?
  112.  
  113.   .vendor_ids         dd ?    ;vendor id string
  114.   .ctrl_ids           dd ?    ;hub id string
  115.  
  116.   .buffer             dd ?
  117.  
  118.   .notify_pos         dd ?
  119.   .notify_task        dd ?
  120.  
  121.   .lvi_reg            dd ?
  122.   .ctrl_setup         dd ?
  123.   .user_callback      dd ?
  124.   .codec_read16       dd ?
  125.   .codec_write16      dd ?
  126.  
  127.   .ctrl_read8         dd ?
  128.   .ctrl_read16        dd ?
  129.   .ctrl_read32        dd ?
  130.  
  131.   .ctrl_write8        dd ?
  132.   .ctrl_write16       dd ?
  133.   .ctrl_write32       dd ?
  134. }
  135.  
  136. struc CODEC                ;Audio Chip base class
  137. {
  138.   .chip_id            dd ?
  139.   .flags              dd ?
  140.   .status             dd ?
  141.  
  142.   .ac_vendor_ids      dd ?    ;ac vendor id string
  143.   .chip_ids           dd ?    ;chip model string
  144.  
  145.   .shadow_flag        dd ?
  146.                       dd ?
  147.  
  148.   .regs               dw ?     ; codec registers
  149.   .reg_master_vol     dw ?     ;0x02
  150.   .reg_aux_out_vol    dw ?     ;0x04
  151.   .reg_mone_vol       dw ?     ;0x06
  152.   .reg_master_tone    dw ?     ;0x08
  153.   .reg_beep_vol       dw ?     ;0x0A
  154.   .reg_phone_vol      dw ?     ;0x0C
  155.   .reg_mic_vol        dw ?     ;0x0E
  156.   .reg_line_in_vol    dw ?     ;0x10
  157.   .reg_cd_vol         dw ?     ;0x12
  158.   .reg_video_vol      dw ?     ;0x14
  159.   .reg_aux_in_vol     dw ?     ;0x16
  160.   .reg_pcm_out_vol    dw ?     ;0x18
  161.   .reg_rec_select     dw ?     ;0x1A
  162.   .reg_rec_gain       dw ?     ;0x1C
  163.   .reg_rec_gain_mic   dw ?     ;0x1E
  164.   .reg_gen            dw ?     ;0x20
  165.   .reg_3d_ctrl        dw ?     ;0X22
  166.   .reg_page           dw ?     ;0X24
  167.   .reg_powerdown      dw ?     ;0x26
  168.   .reg_ext_audio      dw ?     ;0x28
  169.   .reg_ext_st         dw ?     ;0x2a
  170.   .reg_pcm_front_rate dw ?     ;0x2c
  171.   .reg_pcm_surr_rate  dw ?     ;0x2e
  172.   .reg_lfe_rate       dw ?     ;0x30
  173.   .reg_pcm_in_rate    dw ?     ;0x32
  174.                       dw ?     ;0x34
  175.   .reg_cent_lfe_vol   dw ?     ;0x36
  176.   .reg_surr_vol       dw ?     ;0x38
  177.   .reg_spdif_ctrl     dw ?     ;0x3A
  178.                       dw ?     ;0x3C
  179.                       dw ?     ;0x3E
  180.                       dw ?     ;0x40
  181.                       dw ?     ;0x42
  182.                       dw ?     ;0x44
  183.                       dw ?     ;0x46
  184.                       dw ?     ;0x48
  185.                       dw ?     ;0x4A
  186.                       dw ?     ;0x4C
  187.                       dw ?     ;0x4E
  188.                       dw ?     ;0x50
  189.                       dw ?     ;0x52
  190.                       dw ?     ;0x54
  191.                       dw ?     ;0x56
  192.                       dw ?     ;0x58
  193.                       dw ?     ;0x5A
  194.                       dw ?     ;0x5C
  195.                       dw ?     ;0x5E
  196.   .reg_page_0         dw ?     ;0x60
  197.   .reg_page_1         dw ?     ;0x62
  198.   .reg_page_2         dw ?     ;0x64
  199.   .reg_page_3         dw ?     ;0x66
  200.   .reg_page_4         dw ?     ;0x68
  201.   .reg_page_5         dw ?     ;0x6A
  202.   .reg_page_6         dw ?     ;0x6C
  203.   .reg_page_7         dw ?     ;0x6E
  204.                       dw ?     ;0x70
  205.                       dw ?     ;0x72
  206.                       dw ?     ;0x74
  207.                       dw ?     ;0x76
  208.                       dw ?     ;0x78
  209.                       dw ?     ;0x7A
  210.   .reg_vendor_id_1    dw ?     ;0x7C
  211.   .reg_vendor_id_2    dw ?     ;0x7E
  212.  
  213.  
  214.   .reset              dd ?    ;virual
  215.   .set_master_vol     dd ?
  216. }
  217.  
  218. struc CTRL_INFO
  219. {   .pci_cmd          dd ?
  220.     .irq              dd ?
  221.     .glob_cntrl       dd ?
  222.     .glob_sta         dd ?
  223.     .codec_io_base    dd ?
  224.     .ctrl_io_base     dd ?
  225.     .codec_mem_base   dd ?
  226.     .ctrl_mem_base    dd ?
  227.     .codec_id         dd ?
  228. }
  229.  
  230. struc IOCTL
  231. {  .handle            dd ?
  232.    .io_code           dd ?
  233.    .input             dd ?
  234.    .inp_size          dd ?
  235.    .output            dd ?
  236.    .out_size          dd ?
  237. }
  238.  
  239. virtual at 0
  240.   IOCTL IOCTL
  241. end virtual
  242.  
  243. EVENT_NOTIFY          equ 0x00000200
  244.  
  245. OS_BASE         equ 0x80000000
  246. SLOT_BASE          equ OS_BASE+0x0080000
  247.  
  248. public START
  249. public service_proc
  250. public version
  251.  
  252. section '.flat' code readable align 16
  253.  
  254. proc START stdcall, state:dword
  255.  
  256.            cmp [state], 1
  257.            jne .stop
  258.  
  259.      if DEBUG
  260.            mov esi, msgInit
  261.            call SysMsgBoardStr
  262.      end if
  263.  
  264.            call detect_controller
  265.            test eax, eax
  266.            jz .fail
  267.  
  268.      if DEBUG
  269.            mov esi,[ctrl.vendor_ids]
  270.            call SysMsgBoardStr
  271.            mov  esi, [ctrl.ctrl_ids]
  272.            call SysMsgBoardStr
  273.      end if
  274.  
  275.            call init_controller
  276.            test eax, eax
  277.            jz .fail
  278.  
  279.      if DEBUG
  280.            mov esi, msgInitCodec
  281.            call SysMsgBoardStr
  282.      end if
  283.  
  284.            call init_codec
  285.            test eax, eax
  286.            jz .fail
  287.  
  288.      if DEBUG
  289.            mov esi, [codec.ac_vendor_ids]
  290.            call SysMsgBoardStr
  291.  
  292.            mov esi, [codec.chip_ids]
  293.            call SysMsgBoardStr
  294.      end if
  295.  
  296.            call reset_controller
  297.            call setup_codec
  298.  
  299.            mov esi, msgPrimBuff
  300.            call SysMsgBoardStr
  301.  
  302.            call create_primary_buff
  303.  
  304.            stdcall AttachIntHandler, [ctrl.int_line], ac97_irq
  305.  
  306.            stdcall RegService, sz_sound_srv, service_proc
  307.  
  308.            mov esi, msgOk
  309.            call SysMsgBoardStr
  310.            ret
  311. .fail:
  312.    if DEBUG
  313.            mov esi, msgFail
  314.            call SysMsgBoardStr
  315.    end if
  316.            xor eax, eax
  317.            ret
  318. .stop:
  319.            call stop
  320.            xor eax, eax
  321.            ret
  322. endp
  323.  
  324. handle     equ  IOCTL.handle
  325. io_code    equ  IOCTL.io_code
  326. input      equ  IOCTL.input
  327. inp_size   equ  IOCTL.inp_size
  328. output     equ  IOCTL.output
  329. out_size   equ  IOCTL.out_size
  330.  
  331. align 4
  332. proc service_proc stdcall, ioctl:dword
  333.  
  334.            mov edi, [ioctl]
  335.            mov eax, [edi+io_code]
  336.            cmp eax, DEV_PLAY
  337.            jne @F
  338.      if DEBUG
  339.            mov esi, msgPlay
  340.            call SysMsgBoardStr
  341.      end if
  342.            call play
  343.            ret
  344. @@:
  345.            cmp eax, DEV_STOP
  346.            jne @F
  347.      if DEBUG
  348.            mov esi, msgStop
  349.            call SysMsgBoardStr
  350.      end if
  351.            call stop
  352.            ret
  353. @@:
  354.            cmp eax, DEV_CALLBACK
  355.            jne @F
  356.            mov ebx, [edi+input]
  357.            stdcall set_callback, [ebx]
  358.            ret
  359. @@:
  360.            cmp eax, DEV_SET_MASTERVOL
  361.            jne @F
  362.            mov eax, [edi+input]
  363.            mov eax, [eax]
  364.            call set_master_vol      ;eax= vol
  365.            ret
  366. @@:
  367.            cmp eax, DEV_GET_MASTERVOL
  368.            jne @F
  369.            mov ebx, [edi+output]
  370.            stdcall get_master_vol, ebx
  371.            ret
  372. ;@@:
  373. ;           cmp eax, DEV_GET_INFO
  374. ;           jne @F
  375. ;           mov ebx, [edi+output]
  376. ;           stdcall get_dev_info, ebx
  377. ;           ret
  378. @@:
  379. .fail:
  380.            or eax, -1
  381.            ret
  382. endp
  383.  
  384. restore   handle
  385. restore   io_code
  386. restore   input
  387. restore   inp_size
  388. restore   output
  389. restore   out_size
  390.  
  391. align 4
  392. proc ac97_irq
  393.  
  394. ;     if DEBUG
  395. ;           mov esi, msgIRQ
  396. ;           call SysMsgBoardStr
  397. ;     end if
  398.  
  399.            mov edx, PCM_OUT_CR_REG
  400.            mov al, 0x10
  401.            call [ctrl.ctrl_write8]
  402.  
  403.            mov ax, 0x1c
  404.            mov edx, PCM_OUT_SR_REG
  405.            call [ctrl.ctrl_write16]
  406.  
  407.            mov edx, PCM_OUT_CIV_REG
  408.            call [ctrl.ctrl_read8]
  409.  
  410.            and eax, 0x1F
  411.            cmp eax, [civ_val]
  412.            je .skip
  413.  
  414.            mov [civ_val], eax
  415.            dec eax
  416.            and eax, 0x1F
  417.            mov [ctrl.lvi_reg], eax
  418.  
  419.            mov edx, PCM_OUT_LVI_REG
  420.            call [ctrl.ctrl_write8]
  421.  
  422.            mov edx, PCM_OUT_CR_REG
  423.            mov ax, 0x11
  424.            call [ctrl.ctrl_write8]
  425.  
  426.            mov eax, [civ_val]
  427.            add eax, 1
  428.            and eax, 31
  429.            mov ebx, dword [buff_list+eax*4]
  430.  
  431.            cmp [ctrl.user_callback], 0
  432.            je @f
  433.  
  434.            stdcall [ctrl.user_callback], ebx
  435. @@:
  436.            ret
  437.  
  438. .skip:
  439.            mov edx, PCM_OUT_CR_REG
  440.            mov ax, 0x11
  441.            call [ctrl.ctrl_write8]
  442.            ret
  443. endp
  444.  
  445. align 4
  446. proc create_primary_buff
  447.  
  448.            stdcall KernelAlloc, 0x10000
  449.            mov [ctrl.buffer], eax
  450.  
  451.            mov edi, eax
  452.            mov ecx, 0x10000/4
  453.            xor eax, eax
  454.            cld
  455.            rep stosd
  456.  
  457.            mov eax, [ctrl.buffer]
  458.            call GetPgAddr
  459.  
  460.            mov ebx, 0xC0004000
  461.            mov ecx, 4
  462.            mov edi, pcmout_bdl
  463. @@:
  464.            mov [edi], eax
  465.            mov [edi+4], ebx
  466.            mov [edi+32], eax
  467.            mov [edi+4+32], ebx
  468.            mov [edi+64], eax
  469.            mov [edi+4+64], ebx
  470.            mov [edi+96], eax
  471.            mov [edi+4+96], ebx
  472.            mov [edi+128], eax
  473.            mov [edi+4+128], ebx
  474.            mov [edi+160], eax
  475.            mov [edi+4+160], ebx
  476.            mov [edi+192], eax
  477.            mov [edi+4+192], ebx
  478.            mov [edi+224], eax
  479.            mov [edi+4+224], ebx
  480.  
  481.            add eax, 0x4000
  482.            add edi, 8
  483.            loop @B
  484.  
  485.            mov edi, buff_list
  486.            mov eax, [ctrl.buffer]
  487.            mov ecx, 4
  488. @@:
  489.            mov [edi], eax
  490.            mov [edi+16], eax
  491.            mov [edi+32], eax
  492.            mov [edi+48], eax
  493.            mov [edi+64], eax
  494.            mov [edi+80], eax
  495.            mov [edi+96], eax
  496.            mov [edi+112], eax
  497.  
  498.            add eax, 0x4000
  499.            add edi, 4
  500.            loop @B
  501.  
  502.            mov eax, pcmout_bdl
  503.            mov ebx, eax
  504.            call GetPgAddr     ;eax
  505.            and ebx, 0xFFF
  506.            add eax, ebx
  507.  
  508.            mov edx, PCM_OUT_BDL
  509.            call [ctrl.ctrl_write32]
  510.  
  511.            mov eax, 16
  512.            mov [ctrl.lvi_reg], eax
  513.            mov edx, PCM_OUT_LVI_REG
  514.            call [ctrl.ctrl_write8]
  515.  
  516.            mov edx, GLOB_CTRL
  517.            call [ctrl.ctrl_read32]
  518.            and eax, not 0x000000C0
  519.            mov edx, GLOB_CTRL
  520.            call [ctrl.ctrl_write32]
  521.  
  522.  
  523.            ret
  524. endp
  525.  
  526. align 4
  527. proc detect_controller
  528.            locals
  529.              last_bus dd ?
  530.              bus      dd ?
  531.              devfn    dd ?
  532.            endl
  533.  
  534.            xor eax, eax
  535.            mov [bus], eax
  536.            inc eax
  537.            call PciApi
  538.            cmp eax, -1
  539.            je .err
  540.  
  541.            mov [last_bus], eax
  542.  
  543. .next_bus:
  544.            and [devfn], 0
  545. .next_dev:
  546.            stdcall PciRead32, [bus], [devfn], dword 0
  547.            test eax, eax
  548.            jz .next
  549.            cmp eax, -1
  550.            je .next
  551.            mov edi, devices
  552. @@:
  553.            mov ebx, [edi]
  554.            test ebx, ebx
  555.            jz .next
  556.  
  557.            cmp eax, ebx
  558.            je .found
  559.            add edi, 12
  560.            jmp @B
  561.  
  562. .next:
  563.            inc [devfn]
  564.            cmp [devfn], 256
  565.            jb  .next_dev
  566.            mov eax, [bus]
  567.            inc eax
  568.            mov [bus], eax
  569.            cmp eax, [last_bus]
  570.            jna .next_bus
  571.            xor eax, eax
  572.            ret
  573. .found:
  574.            mov ebx, [bus]
  575.            mov [ctrl.bus], ebx
  576.  
  577.            mov ecx, [devfn]
  578.            mov [ctrl.devfn], ecx
  579.  
  580.            mov edx, eax
  581.            and edx, 0xFFFF
  582.            mov [ctrl.vendor], edx
  583.            shr eax, 16
  584.            mov [ctrl.dev_id], eax
  585.  
  586.            mov ebx, [edi+4]
  587.            mov [ctrl.ctrl_ids], ebx
  588.            mov [ctrl.vendor_ids], msg_SIS
  589.  
  590.            mov esi, [edi+8]
  591.            mov [ctrl.ctrl_setup], esi
  592.            ret
  593. .err:
  594.            xor eax, eax
  595.            ret
  596. endp
  597.  
  598. align 4
  599. proc init_controller
  600.  
  601.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 4
  602.            mov ebx, eax
  603.            and eax, 0xFFFF
  604.            mov [ctrl.pci_cmd], eax
  605.            shr ebx, 16
  606.            mov [ctrl.pci_stat], ebx
  607.  
  608.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x10
  609.            and eax,0xFFFE
  610.            mov [ctrl.codec_io_base], eax
  611.  
  612.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x14
  613.            and eax, 0xFFC0
  614.            mov [ctrl.ctrl_io_base], eax
  615.  
  616.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x18
  617.            mov [ctrl.codec_mem_base], eax
  618.  
  619.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x1C
  620.            mov [ctrl.ctrl_mem_base], eax
  621.  
  622.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x3C
  623.            and eax, 0xFF
  624.            mov [ctrl.int_line], eax
  625.  
  626.            stdcall PciRead8, [ctrl.bus], [ctrl.devfn], dword 0x41
  627.            and eax, 0xFF
  628.            mov [ctrl.cfg_reg], eax
  629.  
  630.            call [ctrl.ctrl_setup]
  631.            xor eax, eax
  632.            inc eax
  633.            ret
  634. endp
  635.  
  636. align 4
  637. proc set_SIS
  638.            mov [ctrl.codec_read16],  codec_io_r16    ;virtual
  639.            mov [ctrl.codec_write16], codec_io_w16    ;virtual
  640.  
  641.            mov [ctrl.ctrl_read8 ],  ctrl_io_r8      ;virtual
  642.            mov [ctrl.ctrl_read16],  ctrl_io_r16      ;virtual
  643.            mov [ctrl.ctrl_read32],  ctrl_io_r32      ;virtual
  644.  
  645.            mov [ctrl.ctrl_write8 ], ctrl_io_w8      ;virtual
  646.            mov [ctrl.ctrl_write16], ctrl_io_w16      ;virtual
  647.            mov [ctrl.ctrl_write32], ctrl_io_w32      ;virtual
  648.            ret
  649. endp
  650.  
  651. align 4
  652. proc reset_controller
  653.  
  654.            xor eax, eax
  655.            mov edx, PCM_IN_CR_REG
  656.            call [ctrl.ctrl_write8]
  657.  
  658.            mov edx, PCM_OUT_CR_REG
  659.            call [ctrl.ctrl_write8]
  660.  
  661.            mov edx, MC_IN_CR_REG
  662.            call [ctrl.ctrl_write8]
  663.  
  664.            mov eax, RR
  665.            mov edx, PCM_IN_CR_REG
  666.            call [ctrl.ctrl_write8]
  667.  
  668.            mov edx, PCM_OUT_CR_REG
  669.            call [ctrl.ctrl_write8]
  670.  
  671.            mov edx, MC_IN_CR_REG
  672.            call [ctrl.ctrl_write8]
  673.            ret
  674. endp
  675.  
  676. align 4
  677. proc init_codec
  678.            locals
  679.              counter dd ?
  680.            endl
  681.  
  682.            mov esi, msgControl
  683.            call SysMsgBoardStr
  684.  
  685.            mov edx, GLOB_CTRL
  686.            call [ctrl.ctrl_read32]
  687.            call dword2str
  688.            call SysMsgBoardStr
  689.  
  690.            mov esi, msgStatus
  691.            call SysMsgBoardStr
  692.  
  693.            mov edx, CTRL_STAT
  694.            call [ctrl.ctrl_read32]
  695.  
  696.            call dword2str
  697.            call SysMsgBoardStr
  698.  
  699.            test eax, CTRL_ST_CREADY
  700.            jnz .ready
  701.  
  702.            call reset_codec
  703.            and eax, eax
  704.            jz .err
  705.  
  706.            xor edx, edx     ;ac_reg_0
  707.            call [ctrl.codec_write16]
  708.  
  709.            xor eax, eax
  710.            mov edx, CODEC_REG_POWERDOWN
  711.            call [ctrl.codec_write16]
  712.  
  713.            mov [counter], 200     ; total 200*5 ms = 1s
  714. .wait:
  715.            mov edx, CODEC_REG_POWERDOWN
  716.            call [ctrl.codec_read16]
  717.            and eax, 0x0F
  718.            cmp eax, 0x0F
  719.            je .ready
  720.  
  721.            mov eax, 5000          ; wait 5 ms
  722.            call StallExec
  723.            sub [counter] , 1
  724.            jnz .wait
  725. .err:
  726.            xor eax, eax        ; timeout error
  727.            ret
  728. .ready:
  729.            call detect_codec
  730.  
  731.            xor eax, eax
  732.            inc eax
  733.            ret
  734. endp
  735.  
  736. align 4
  737. proc reset_codec
  738.            mov edx, GLOB_CTRL
  739.            call [ctrl.ctrl_read32]
  740.  
  741.            test eax, 0x02
  742.            jz .cold
  743.  
  744.            call warm_reset
  745.            jnc .ok
  746. .cold:
  747.            call cold_reset
  748.            jnc .ok
  749.  
  750.      if DEBUG
  751.            mov esi, msgCFail
  752.            call SysMsgBoardStr
  753.      end if
  754.            xor eax, eax     ; timeout error
  755.            ret
  756. .ok:
  757.            xor eax, eax
  758.            inc eax
  759.            ret
  760. endp
  761.  
  762. align 4
  763. proc warm_reset
  764.            locals
  765.              counter dd ?
  766.            endl
  767.  
  768.            mov eax, 0x06
  769.            mov edx, GLOB_CTRL
  770.            call [ctrl.ctrl_write32]
  771.  
  772.      if DEBUG
  773.            mov esi, msgWarm
  774.            call SysMsgBoardStr
  775.      end if
  776.  
  777.            mov [counter], 10    ; total 10*100 ms = 1s
  778. .wait:
  779.            mov eax, 100000    ; wait 100 ms
  780.            call StallExec
  781.  
  782.            mov edx, GLOB_CTRL
  783.            call [ctrl.ctrl_read32]
  784.            test eax, 4
  785.            jz .ok
  786.            sub [counter], 1
  787.            jnz .wait
  788.  
  789.      if DEBUG
  790.            mov esi, msgWRFail
  791.            call SysMsgBoardStr
  792.      end if
  793.            stc
  794.            ret
  795. .ok:
  796.            mov edx, CTRL_STAT
  797.            call [ctrl.ctrl_read32]
  798.            and eax, CTRL_ST_CREADY
  799.            jz .fail
  800.            clc
  801.            ret
  802. .fail:
  803.            stc
  804.            ret
  805. endp
  806.  
  807. align 4
  808. proc cold_reset
  809.            locals
  810.              counter dd ?
  811.             endl
  812.  
  813.            mov edx, GLOB_CTRL
  814.            call [ctrl.ctrl_read32]
  815.            and eax, not 0x08
  816.            or eax, 0x02
  817.            mov edx, GLOB_CTRL
  818.            call [ctrl.ctrl_write32]
  819.  
  820.      if DEBUG
  821.            mov esi, msgCold
  822.            call SysMsgBoardStr
  823.      end if
  824.  
  825.            mov [counter], 10    ; total 10*100 ms = 1s
  826. .wait:
  827.            mov eax, 100000    ; wait 100 ms
  828.            call StallExec
  829.  
  830.            mov edx, GLOB_CTRL
  831.            call [ctrl.ctrl_read32]
  832.            test eax, 4
  833.            jz .ok
  834.            sub [counter], 1
  835.            jnz .wait
  836.  
  837.      if DEBUG
  838.            mov esi, msgCRFail
  839.            call SysMsgBoardStr
  840.      end if
  841. .fail:
  842.            stc
  843.            ret
  844. .ok:
  845.            mov edx, CTRL_STAT
  846.            call [ctrl.ctrl_read32]
  847.            and eax, CTRL_ST_CREADY
  848.            jz .fail
  849.            clc
  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.            js @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. align 4
  1112. dword2str:
  1113.       mov  esi, hex_buff
  1114.       mov ecx, -8
  1115. @@:
  1116.       rol eax, 4
  1117.       mov ebx, eax
  1118.       and ebx, 0x0F
  1119.       mov bl, [ebx+hexletters]
  1120.       mov [8+esi+ecx], bl
  1121.       inc ecx
  1122.       jnz @B
  1123.       ret
  1124.  
  1125.  
  1126. include "codec.inc"
  1127.  
  1128. align 4
  1129. devices dd (CTRL_SIS  shl 16)+VID_SIS,msg_AC, set_SIS
  1130.         dd 0
  1131.  
  1132. version      dd 0x00040004
  1133.  
  1134. msg_AC       db '7012 AC97 controller',13,10, 0
  1135. msg_SIS      db 'Silicon Integrated Systems',13,10, 0
  1136.  
  1137. sz_sound_srv        db 'SOUND',0
  1138.  
  1139. msgInit      db 'detect hardware...',13,10,0
  1140. msgFail      db 'device not found',13,10,0
  1141. msgPlay      db 'start play', 13,10,0
  1142. msgStop      db 'stop play',  13,10,0
  1143. msgNotify    db 'call notify',13,10,0
  1144. msgIRQ       db 'AC97 IRQ', 13,10,0
  1145. msgInitCtrl  db 'init controller',13,10,0
  1146. msgInitCodec db 'init codec',13,10,0
  1147. msgPrimBuff  db 'create primary buffer',13,10,0
  1148. msgReg       db 'set service handler',13,10,0
  1149. msgOk        db 'service installed',13,10,0
  1150. msgCold      db 'cold resret',13,10,0
  1151. msgWarm      db 'warm reset',13,10,0
  1152. msgWRFail    db 'warm reset failed',13,10,0
  1153. msgCRFail    db 'cold reset failed',13,10,0
  1154. msgCFail     db 'codec not ready',13,10,0
  1155. msgStatus    db 'global status   ',0
  1156. msgControl   db 'global control  ',0
  1157.  
  1158. hexletters   db '0123456789ABCDEF'
  1159. hex_buff     db 8 dup(0),13,10,0
  1160.  
  1161.  
  1162. section '.data' data readable writable align 16
  1163.  
  1164. pcmout_bdl       rq 32
  1165. buff_list        rd 32
  1166.  
  1167. codec CODEC
  1168. ctrl AC_CNTRL
  1169.  
  1170. lpc_bus  rd 1
  1171. civ_val  rd 1
  1172.