Subversion Repositories Kolibri OS

Rev

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

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