Subversion Repositories Kolibri OS

Rev

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

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