Subversion Repositories Kolibri OS

Rev

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