Subversion Repositories Kolibri OS

Rev

Rev 2434 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1340 tsdima 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2465 Serge 3
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;;
1340 tsdima 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
format MS COFF
9
 
2434 Serge 10
DEBUG           equ 1
11
DEBUG_IRQ       equ 0
1340 tsdima 12
 
13
include 'proc32.inc'
14
include 'imports.inc'
15
 
2434 Serge 16
API_VERSION     equ 0x01000100
1340 tsdima 17
 
2434 Serge 18
USE_COM_IRQ     equ 0    ;make irq 3 and irq 4 available for PCI devices
1340 tsdima 19
 
20
;irq 0,1,2,8,12,13 недоступны
21
;                   FEDCBA9876543210
2434 Serge 22
VALID_IRQ       equ 1100111011111000b
23
ATTCH_IRQ       equ 0000111010100000b
1340 tsdima 24
 
25
if USE_COM_IRQ
2434 Serge 26
ATTCH_IRQ       equ 0000111010111000b
1340 tsdima 27
end if
28
 
2434 Serge 29
CPU_FREQ        equ  2000d
1340 tsdima 30
 
31
BIT0  EQU 0x00000001
32
BIT1  EQU 0x00000002
33
BIT2  EQU 0x00000004
34
BIT3  EQU 0x00000008
35
BIT4  EQU 0x00000010
36
BIT5  EQU 0x00000020
37
BIT6  EQU 0x00000040
38
BIT7  EQU 0x00000080
39
BIT8  EQU 0x00000100
40
BIT9  EQU 0x00000200
41
BIT10 EQU 0x00000400
42
BIT11 EQU 0x00000800
43
BIT12 EQU 0x00001000
44
BIT13 EQU 0x00002000
45
BIT14 EQU 0x00004000
46
BIT15 EQU 0x00008000
47
BIT16 EQU 0x00010000
48
BIT17 EQU 0x00020000
49
BIT18 EQU 0x00040000
50
BIT19 EQU 0x00080000
51
BIT20 EQU 0x00100000
52
BIT21 EQU 0x00200000
53
BIT22 EQU 0x00400000
54
BIT23 EQU 0x00800000
55
BIT24 EQU 0x00100000
56
BIT25 EQU 0x02000000
57
BIT26 EQU 0x04000000
58
BIT27 EQU 0x08000000
59
BIT28 EQU 0x10000000
60
BIT29 EQU 0x20000000
61
BIT30 EQU 0x40000000
62
BIT31 EQU 0x80000000
63
 
2434 Serge 64
VID_FM801         equ 0x1319
65
CTRL_FM801        equ 0x0801
1340 tsdima 66
 
67
FM_PCM_VOLUME         equ 0x00
68
FM_FM_VOLUME          equ 0x02
69
FM_I2S_VOLUME         equ 0x04
70
FM_RECORD_SOURCE      equ 0x06
71
 
72
FM_PLAY_CTL           equ 0x08
73
FM_PLAY_RATE_MASK     equ 0x0f00
74
FM_PLAY_BUF1_LAST     equ 0x0001
75
FM_PLAY_BUF2_LAST     equ 0x0002
76
FM_PLAY_START         equ 0x0020
77
FM_PLAY_PAUSE         equ 0x0040
78
FM_PLAY_STOPNOW       equ 0x0080
79
FM_PLAY_16BIT         equ 0x4000
80
FM_PLAY_STEREO        equ 0x8000
81
 
82
FM_PLAY_DMALEN        equ 0x0a
83
FM_PLAY_DMABUF1       equ 0x0c
84
FM_PLAY_DMABUF2       equ 0x10
85
 
86
FM_REC_CTL            equ 0x14
87
FM_REC_RATE_MASK      equ 0x0f00
88
FM_REC_BUF1_LAST      equ 0x0001
89
FM_REC_BUF2_LAST      equ 0x0002
90
FM_REC_START          equ 0x0020
91
FM_REC_PAUSE          equ 0x0040
92
FM_REC_STOPNOW        equ 0x0080
93
FM_REC_16BIT          equ 0x4000
94
FM_REC_STEREO         equ 0x8000
95
 
96
FM_REC_DMALEN         equ 0x16
97
FM_REC_DMABUF1        equ 0x18
98
FM_REC_DMABUF2        equ 0x1c
99
 
100
FM_CODEC_CTL          equ 0x22
101
FM_VOLUME             equ 0x26
102
FM_VOLUME_MUTE        equ 0x8000
103
 
104
FM_CODEC_CMD          equ 0x2a
105
FM_CODEC_CMD_READ     equ 0x0080
106
FM_CODEC_CMD_VALID    equ 0x0100
107
FM_CODEC_CMD_BUSY     equ 0x0200
108
 
109
FM_CODEC_DATA         equ 0x2c
110
 
111
FM_IO_CTL             equ 0x52
112
FM_CARD_CTL           equ 0x54
113
 
114
FM_INTMASK            equ 0x56
115
FM_INTMASK_PLAY       equ 0x0001
116
FM_INTMASK_REC        equ 0x0002
117
FM_INTMASK_VOL        equ 0x0040
118
FM_INTMASK_MPU        equ 0x0080
119
 
120
FM_INTSTATUS          equ 0x5a
121
FM_INTSTATUS_PLAY     equ 0x0100
122
FM_INTSTATUS_REC      equ 0x0200
123
FM_INTSTATUS_VOL      equ 0x4000
124
FM_INTSTATUS_MPU      equ 0x8000
125
 
2434 Serge 126
CODEC_MASTER_VOL_REG         equ 0x02 ;
127
CODEC_AUX_VOL                equ 0x04 ;
128
CODEC_PCM_OUT_REG            equ 0x18 ; PCM output volume
129
CODEC_EXT_AUDIO_REG          equ 0x28 ; extended audio
1340 tsdima 130
CODEC_EXT_AUDIO_CTRL_REG     equ 0x2a ; extended audio control
131
CODEC_PCM_FRONT_DACRATE_REG  equ 0x2c ; PCM out sample rate
132
CODEC_PCM_SURND_DACRATE_REG  equ 0x2e ; surround sound sample rate
133
CODEC_PCM_LFE_DACRATE_REG    equ 0x30 ; LFE sample rate
134
 
2434 Serge 135
SRV_GETVERSION        equ  0
136
DEV_PLAY              equ  1
137
DEV_STOP              equ  2
138
DEV_CALLBACK          equ  3
139
DEV_SET_BUFF          equ  4
140
DEV_NOTIFY            equ  5
1340 tsdima 141
DEV_SET_MASTERVOL     equ  6
142
DEV_GET_MASTERVOL     equ  7
2434 Serge 143
DEV_GET_INFO          equ  8
1340 tsdima 144
 
2434 Serge 145
struc AC_CNTRL    ;AC controller base class
146
{ .bus                dd ?
147
  .devfn              dd ?
1340 tsdima 148
 
2434 Serge 149
  .vendor             dd ?
150
  .dev_id             dd ?
151
  .pci_cmd            dd ?
152
  .pci_stat           dd ?
1340 tsdima 153
 
154
  .codec_io_base      dd ?
155
  .codec_mem_base     dd ?
156
 
157
  .ctrl_io_base       dd ?
158
  .ctrl_mem_base      dd ?
2434 Serge 159
  .cfg_reg            dd ?
160
  .int_line           dd ?
1340 tsdima 161
 
2434 Serge 162
  .vendor_ids         dd ?    ;vendor id string
163
  .ctrl_ids           dd ?    ;hub id string
1340 tsdima 164
 
2434 Serge 165
  .buffer             dd ?
1340 tsdima 166
 
2434 Serge 167
  .notify_pos         dd ?
168
  .notify_task        dd ?
1340 tsdima 169
 
2434 Serge 170
  .lvi_reg            dd ?
171
  .ctrl_setup         dd ?
1340 tsdima 172
  .user_callback      dd ?
173
  .codec_read16       dd ?
174
  .codec_write16      dd ?
175
 
2434 Serge 176
  .ctrl_read8         dd ?
177
  .ctrl_read16        dd ?
178
  .ctrl_read32        dd ?
1340 tsdima 179
 
2434 Serge 180
  .ctrl_write8        dd ?
1340 tsdima 181
  .ctrl_write16       dd ?
182
  .ctrl_write32       dd ?
183
}
184
 
185
struc CODEC   ;Audio Chip base class
186
{
2434 Serge 187
  .chip_id            dd ?
188
  .flags              dd ?
189
  .status             dd ?
1340 tsdima 190
 
191
  .ac_vendor_ids      dd ?    ;ac vendor id string
2434 Serge 192
  .chip_ids           dd ?    ;chip model string
1340 tsdima 193
 
2434 Serge 194
  .shadow_flag        dd ?
195
                      dd ?
1340 tsdima 196
 
2434 Serge 197
  .regs               dw ?     ; codec registers
1340 tsdima 198
  .reg_master_vol     dw ?     ;0x02
199
  .reg_aux_out_vol    dw ?     ;0x04
200
  .reg_mone_vol       dw ?     ;0x06
201
  .reg_master_tone    dw ?     ;0x08
202
  .reg_beep_vol       dw ?     ;0x0A
203
  .reg_phone_vol      dw ?     ;0x0C
2434 Serge 204
  .reg_mic_vol        dw ?     ;0x0E
1340 tsdima 205
  .reg_line_in_vol    dw ?     ;0x10
2434 Serge 206
  .reg_cd_vol         dw ?     ;0x12
1340 tsdima 207
  .reg_video_vol      dw ?     ;0x14
208
  .reg_aux_in_vol     dw ?     ;0x16
209
  .reg_pcm_out_vol    dw ?     ;0x18
210
  .reg_rec_select     dw ?     ;0x1A
211
  .reg_rec_gain       dw ?     ;0x1C
212
  .reg_rec_gain_mic   dw ?     ;0x1E
2434 Serge 213
  .reg_gen            dw ?     ;0x20
214
  .reg_3d_ctrl        dw ?     ;0X22
215
  .reg_page           dw ?     ;0X24
1340 tsdima 216
  .reg_powerdown      dw ?     ;0x26
217
  .reg_ext_audio      dw ?     ;0x28
2434 Serge 218
  .reg_ext_st         dw ?     ;0x2a
1340 tsdima 219
  .reg_pcm_front_rate dw ?     ;0x2c
220
  .reg_pcm_surr_rate  dw ?     ;0x2e
221
  .reg_lfe_rate       dw ?     ;0x30
222
  .reg_pcm_in_rate    dw ?     ;0x32
2434 Serge 223
                      dw ?     ;0x34
1340 tsdima 224
  .reg_cent_lfe_vol   dw ?     ;0x36
225
  .reg_surr_vol       dw ?     ;0x38
226
  .reg_spdif_ctrl     dw ?     ;0x3A
2434 Serge 227
                      dw ?     ;0x3C
228
                      dw ?     ;0x3E
229
                      dw ?     ;0x40
230
                      dw ?     ;0x42
231
                      dw ?     ;0x44
232
                      dw ?     ;0x46
233
                      dw ?     ;0x48
234
                      dw ?     ;0x4A
235
                      dw ?     ;0x4C
236
                      dw ?     ;0x4E
237
                      dw ?     ;0x50
238
                      dw ?     ;0x52
239
                      dw ?     ;0x54
240
                      dw ?     ;0x56
241
                      dw ?     ;0x58
242
                      dw ?     ;0x5A
243
                      dw ?     ;0x5C
244
                      dw ?     ;0x5E
245
  .reg_page_0         dw ?     ;0x60
246
  .reg_page_1         dw ?     ;0x62
247
  .reg_page_2         dw ?     ;0x64
248
  .reg_page_3         dw ?     ;0x66
249
  .reg_page_4         dw ?     ;0x68
250
  .reg_page_5         dw ?     ;0x6A
251
  .reg_page_6         dw ?     ;0x6C
252
  .reg_page_7         dw ?     ;0x6E
253
                      dw ?     ;0x70
254
                      dw ?     ;0x72
255
                      dw ?     ;0x74
256
                      dw ?     ;0x76
257
                      dw ?     ;0x78
258
                      dw ?     ;0x7A
1340 tsdima 259
  .reg_vendor_id_1    dw ?     ;0x7C
260
  .reg_vendor_id_2    dw ?     ;0x7E
261
 
262
 
2434 Serge 263
  .reset              dd ?    ;virual
1340 tsdima 264
  .set_master_vol     dd ?
265
}
266
 
267
struc CTRL_INFO
2434 Serge 268
{   .pci_cmd          dd ?
269
    .irq              dd ?
1340 tsdima 270
    .glob_cntrl       dd ?
2434 Serge 271
    .glob_sta         dd ?
1340 tsdima 272
    .codec_io_base    dd ?
273
    .ctrl_io_base     dd ?
274
    .codec_mem_base   dd ?
275
    .ctrl_mem_base    dd ?
2434 Serge 276
    .codec_id         dd ?
1340 tsdima 277
}
278
 
279
struc IOCTL
2434 Serge 280
{  .handle            dd ?
281
   .io_code           dd ?
282
   .input             dd ?
283
   .inp_size          dd ?
284
   .output            dd ?
285
   .out_size          dd ?
1340 tsdima 286
}
287
 
288
virtual at 0
289
  IOCTL IOCTL
290
end virtual
291
 
2434 Serge 292
EVENT_NOTIFY      equ 0x00000200
1340 tsdima 293
 
294
public START
295
public service_proc
296
public version
297
 
298
section '.flat' code readable align 16
299
 
300
proc START stdcall, state:dword
301
 
2434 Serge 302
        cmp     [state], 1
303
        jne     .stop
1340 tsdima 304
 
305
     if DEBUG
2434 Serge 306
        mov     eax, START
307
        call    dword2str
308
        call    SysMsgBoardStr
309
        mov     esi, msgInit
310
        call    SysMsgBoardStr
1340 tsdima 311
     end if
312
 
2434 Serge 313
        call    detect_controller
314
        test    eax, eax
315
        jz      .fail
1340 tsdima 316
 
317
     if DEBUG
2434 Serge 318
        mov     esi, [ctrl.vendor_ids]
319
        call    SysMsgBoardStr
320
        mov     esi, [ctrl.ctrl_ids]
321
        call    SysMsgBoardStr
1340 tsdima 322
 
323
     end if
324
 
2434 Serge 325
        call    init_controller
326
        test    eax, eax
327
        jz      .fail
1340 tsdima 328
 
2434 Serge 329
        call    init_codec
330
        test    eax, eax
331
        jz      .fail
1340 tsdima 332
 
2434 Serge 333
        call    reset_controller
334
        call    setup_codec
1340 tsdima 335
 
2434 Serge 336
        mov     esi, msgPrimBuff
337
        call    SysMsgBoardStr
338
        call    create_primary_buff
1340 tsdima 339
 
2434 Serge 340
        mov     esi, msgDone
341
        call    SysMsgBoardStr
1340 tsdima 342
 
2434 Serge 343
        mov     eax, VALID_IRQ
344
        mov     ebx, [ctrl.int_line]
345
        mov     esi, msgInvIRQ
346
        bt      eax, ebx
347
        jnc     .fail_msg
348
        mov     eax, ATTCH_IRQ
349
        mov     esi, msgAttchIRQ
350
        bt      eax, ebx
351
        jnc     .fail_msg
1340 tsdima 352
 
2434 Serge 353
        stdcall AttachIntHandler, ebx, ac97_irq, dword 0
1340 tsdima 354
.reg:
355
 
2434 Serge 356
        stdcall RegService, sz_sound_srv, service_proc
357
        ret
1340 tsdima 358
.fail:
359
   if DEBUG
2434 Serge 360
        mov     esi, msgFail
361
        call    SysMsgBoardStr
1340 tsdima 362
   end if
2434 Serge 363
        xor     eax, eax
364
        ret
1340 tsdima 365
.fail_msg:
2434 Serge 366
        call    SysMsgBoardStr
367
        xor     eax, eax
368
        ret
1340 tsdima 369
.stop:
2434 Serge 370
        call    stop
371
        xor     eax, eax
372
        ret
1340 tsdima 373
endp
374
 
2434 Serge 375
handle     equ  IOCTL.handle
376
io_code    equ  IOCTL.io_code
377
input      equ  IOCTL.input
378
inp_size   equ  IOCTL.inp_size
379
output     equ  IOCTL.output
380
out_size   equ  IOCTL.out_size
1340 tsdima 381
 
382
align 4
383
proc service_proc stdcall, ioctl:dword
384
 
2434 Serge 385
        mov     edi, [ioctl]
386
        mov     eax, [edi+io_code]
1340 tsdima 387
 
2434 Serge 388
        cmp     eax, SRV_GETVERSION
389
        jne     @F
1340 tsdima 390
 
2434 Serge 391
        mov     eax, [edi+output]
392
        cmp     [edi+out_size], 4
393
        jne     .fail
1340 tsdima 394
 
2434 Serge 395
        mov     [eax], dword API_VERSION
396
        xor     eax, eax
397
        ret
1340 tsdima 398
@@:
2434 Serge 399
        cmp     eax, DEV_PLAY
400
        jne     @F
1340 tsdima 401
     if DEBUG
2434 Serge 402
        mov     esi, msgPlay
403
        call    SysMsgBoardStr
1340 tsdima 404
     end if
2434 Serge 405
        call    play
406
        ret
1340 tsdima 407
@@:
2434 Serge 408
        cmp     eax, DEV_STOP
409
        jne     @F
1340 tsdima 410
     if DEBUG
2434 Serge 411
        mov     esi, msgStop
412
        call    SysMsgBoardStr
1340 tsdima 413
     end if
2434 Serge 414
        call    stop
415
        ret
1340 tsdima 416
@@:
2434 Serge 417
        cmp     eax, DEV_CALLBACK
418
        jne     @F
419
        mov     ebx, [edi+input]
420
        stdcall set_callback, [ebx]
421
        ret
1340 tsdima 422
@@:
2434 Serge 423
        cmp     eax, DEV_SET_MASTERVOL
424
        jne     @F
425
        mov     eax, [edi+input]
426
        mov     eax, [eax]
427
        call    set_master_vol      ;eax= vol
428
        ret
1340 tsdima 429
@@:
2434 Serge 430
        cmp     eax, DEV_GET_MASTERVOL
431
        jne     @F
432
        mov     ebx, [edi+output]
433
        stdcall get_master_vol, ebx
434
        ret
1340 tsdima 435
;@@:
436
;           cmp eax, DEV_GET_INFO
437
;           jne @F
438
;           mov ebx, [edi+output]
439
;           stdcall get_dev_info, ebx
440
;           ret
441
@@:
442
.fail:
2434 Serge 443
        or      eax, -1
444
        ret
1340 tsdima 445
endp
446
 
447
restore   handle
448
restore   io_code
449
restore   input
450
restore   inp_size
451
restore   output
452
restore   out_size
453
 
454
align 4
455
proc fill_buffer
456
 
2434 Serge 457
        cmp     [ctrl.user_callback], 0
458
        je      .exit
1340 tsdima 459
 
2434 Serge 460
        mov     esi, [ctrl.buffer]
461
        mov     eax, int_flip_flop
462
        inc     dword [eax]
463
        test    dword [eax], 1
464
        je      @f
465
        add     esi, 0x4000
1340 tsdima 466
@@:
2434 Serge 467
        stdcall [ctrl.user_callback], esi
1340 tsdima 468
 
2434 Serge 469
        mov     edx, FM_PLAY_DMABUF1
470
        mov     eax, [buffer_pgaddr]
471
        mov     esi, int_flip_flop
472
        test    dword [esi], 1
473
        je      @f
474
        mov     edx, FM_PLAY_DMABUF2
475
        add     eax, 0x4000
1340 tsdima 476
@@:
2434 Serge 477
        call    [ctrl.ctrl_write32]
1340 tsdima 478
 
479
.exit:
2434 Serge 480
        ret
1340 tsdima 481
endp
482
 
483
align 4
484
proc ac97_irq
485
 
486
     if DEBUG_IRQ
2434 Serge 487
        mov     esi, msgIRQ
488
        call    SysMsgBoardStr
1340 tsdima 489
     end if
490
 
2434 Serge 491
        mov     edx, FM_INTSTATUS
492
        call    [ctrl.ctrl_read16]
1340 tsdima 493
 
2434 Serge 494
        test    eax, FM_INTSTATUS_PLAY
495
        je      .exit
1340 tsdima 496
 
2434 Serge 497
        push    eax
498
        call    fill_buffer
499
        pop     eax
1340 tsdima 500
 
501
.exit:
2434 Serge 502
        mov     edx, FM_INTSTATUS
503
        call    [ctrl.ctrl_write16]
1340 tsdima 504
 
2434 Serge 505
        ret
1340 tsdima 506
endp
507
 
508
align 4
509
proc create_primary_buff
510
 
2434 Serge 511
        stdcall KernelAlloc, 0x10000
512
        mov     [ctrl.buffer], eax
1340 tsdima 513
 
2434 Serge 514
        mov     edi, eax
515
        mov     ecx, 0x10000/4
516
        xor     eax, eax
517
        cld
518
        rep stosd
1340 tsdima 519
 
2434 Serge 520
        mov     eax, [ctrl.buffer]
521
        call    GetPgAddr
522
        mov     [buffer_pgaddr], eax
1340 tsdima 523
 
2434 Serge 524
        ret
1340 tsdima 525
endp
526
 
527
align 4
528
proc detect_controller
2434 Serge 529
           locals
530
             last_bus dd ?
531
             bus      dd ?
532
             devfn    dd ?
533
           endl
1340 tsdima 534
 
2434 Serge 535
        xor     eax, eax
536
        mov     [bus], eax
537
        inc     eax
538
        call    PciApi
539
        cmp     eax, -1
540
        je      .err
1340 tsdima 541
 
2434 Serge 542
        mov     [last_bus], eax
1340 tsdima 543
 
544
.next_bus:
2434 Serge 545
        and     [devfn], 0
1340 tsdima 546
.next_dev:
2434 Serge 547
        stdcall PciRead32, [bus], [devfn], dword 0
548
        test    eax, eax
549
        jz      .next
550
        cmp     eax, -1
551
        je      .next
1340 tsdima 552
 
2434 Serge 553
        push    eax
554
        stdcall PciRead32, [bus], [devfn], dword 0x09
555
        and     eax, 0xffffff
556
        cmp     eax, 0x060100 ;pci-isa
557
        jne     .no_bridge
1340 tsdima 558
 
2434 Serge 559
        mov     eax, [bus]
560
        mov     [brg_bus], eax
561
        mov     eax, [devfn]
562
        mov     [brg_devfn], eax
563
.no_bridge:
564
        pop     eax
1340 tsdima 565
 
2434 Serge 566
        mov     edi, devices
1340 tsdima 567
@@:
2434 Serge 568
        mov     ebx, [edi]
569
        test    ebx, ebx
570
        jz      .next
1340 tsdima 571
 
2434 Serge 572
        cmp     eax, ebx
573
        je      .found
574
        add     edi, 12
575
        jmp     @B
1340 tsdima 576
.next:
2434 Serge 577
        inc     [devfn]
578
        cmp     [devfn], 256
579
        jb      .next_dev
580
        mov     eax, [bus]
581
        inc     eax
582
        mov     [bus], eax
583
        cmp     eax, [last_bus]
584
        jna     .next_bus
585
        xor     eax, eax
586
        ret
1340 tsdima 587
.found:
2434 Serge 588
        mov     ebx, [bus]
589
        mov     [ctrl.bus], ebx
1340 tsdima 590
 
2434 Serge 591
        mov     ecx, [devfn]
592
        mov     [ctrl.devfn], ecx
1340 tsdima 593
 
2434 Serge 594
        mov     edx, eax
595
        and     edx, 0xFFFF
596
        mov     [ctrl.vendor], edx
597
        shr     eax, 16
598
        mov     [ctrl.dev_id], eax
1340 tsdima 599
 
2434 Serge 600
        mov     ebx, [edi+4]
601
        mov     [ctrl.ctrl_ids], ebx
602
        mov     [ctrl.vendor_ids], msg_FM
1340 tsdima 603
 
2434 Serge 604
        mov     esi, [edi+8]
605
        mov     [ctrl.ctrl_setup], esi
606
        ret
1340 tsdima 607
.err:
2434 Serge 608
        xor     eax, eax
609
        ret
1340 tsdima 610
endp
611
 
612
align 4
613
proc init_controller
614
 
2434 Serge 615
        stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 4
616
        mov     ebx, eax
617
        and     eax, 0xFFFF
618
        mov     [ctrl.pci_cmd], eax
619
        shr     ebx, 16
620
        mov     [ctrl.pci_stat], ebx
1340 tsdima 621
 
2434 Serge 622
        mov     esi, msgPciCmd
623
        call    SysMsgBoardStr
624
        call    dword2str
625
        call    SysMsgBoardStr
1340 tsdima 626
 
2434 Serge 627
        mov     esi, msgPciStat
628
        call    SysMsgBoardStr
629
        mov     eax, [ctrl.pci_stat]
630
        call    dword2str
631
        call    SysMsgBoardStr
1340 tsdima 632
 
2434 Serge 633
        mov     esi, msgCtrlIsaIo
634
        call    SysMsgBoardStr
1340 tsdima 635
 
2434 Serge 636
        stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x10
1340 tsdima 637
 
2434 Serge 638
        call    dword2str
639
        call    SysMsgBoardStr
1340 tsdima 640
 
2434 Serge 641
        and     eax, 0xFFFE
642
        mov     [ctrl.ctrl_io_base], eax
1340 tsdima 643
 
2434 Serge 644
        mov     esi, msgIrqNum
645
        call    SysMsgBoardStr
1340 tsdima 646
 
2434 Serge 647
        stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x3C
648
        and     eax, 0xFF
649
        mov     [ctrl.int_line], eax
1340 tsdima 650
 
2434 Serge 651
        call    dword2str
652
        call    SysMsgBoardStr
1340 tsdima 653
 
2434 Serge 654
        call    [ctrl.ctrl_setup]
655
        xor     eax, eax
656
        inc     eax
657
        ret
1340 tsdima 658
endp
659
 
660
align 4
661
proc set_FM
2434 Serge 662
        mov     [ctrl.codec_read16], codec_io_r16    ;virtual
663
        mov     [ctrl.codec_write16], codec_io_w16   ;virtual
1340 tsdima 664
 
2434 Serge 665
        mov     [ctrl.ctrl_read8 ], ctrl_io_r8      ;virtual
666
        mov     [ctrl.ctrl_read16], ctrl_io_r16      ;virtual
667
        mov     [ctrl.ctrl_read32], ctrl_io_r32      ;virtual
1340 tsdima 668
 
2434 Serge 669
        mov     [ctrl.ctrl_write8 ], ctrl_io_w8     ;virtual
670
        mov     [ctrl.ctrl_write16], ctrl_io_w16     ;virtual
671
        mov     [ctrl.ctrl_write32], ctrl_io_w32     ;virtual
672
        ret
1340 tsdima 673
endp
674
 
675
align 4
676
proc reset_controller
677
 
2434 Serge 678
        mov     esi, msgInitCtrl
679
        call    SysMsgBoardStr
1340 tsdima 680
 
2434 Serge 681
        mov     edx, FM_CARD_CTL
682
        call    [ctrl.ctrl_read8]
683
        push    eax
684
        or      al, 1
685
        mov     edx, FM_CARD_CTL
686
        call    [ctrl.ctrl_write8]
687
        mov     eax, 10
688
        call    StallExec
689
        pop     eax
690
        and     al, 0xFE
691
        mov     edx, FM_CARD_CTL
692
        call    [ctrl.ctrl_write8]
693
        mov     eax, 10
694
        call    StallExec
1340 tsdima 695
 
2434 Serge 696
        mov     eax, 0x0404
697
        mov     edx, FM_PCM_VOLUME
698
        call    [ctrl.ctrl_write16]
699
        mov     edx, FM_FM_VOLUME
700
        call    [ctrl.ctrl_write16]
701
        mov     edx, FM_I2S_VOLUME
702
        call    [ctrl.ctrl_write16]
1340 tsdima 703
 
2434 Serge 704
        mov     edx, FM_INTMASK
705
        call    [ctrl.ctrl_read16]
706
        and     eax, not FM_INTMASK_PLAY
707
        or      eax, FM_INTMASK_REC or FM_INTMASK_MPU or FM_INTMASK_VOL
708
        mov     edx, FM_INTMASK
709
        call    [ctrl.ctrl_write16]
1340 tsdima 710
 
2434 Serge 711
        mov     eax, FM_INTMASK_PLAY or FM_INTMASK_REC or FM_INTMASK_MPU or FM_INTMASK_VOL
712
        mov     edx, FM_INTSTATUS
713
        call    [ctrl.ctrl_write16]
1340 tsdima 714
 
2434 Serge 715
        ret
1340 tsdima 716
endp
717
 
718
align 4
719
proc init_codec
720
 
2434 Serge 721
        mov     esi, msgInitCodec
722
        call    SysMsgBoardStr
1340 tsdima 723
 
2434 Serge 724
        mov     al, FM_CODEC_CMD_READ
725
        mov     edx, FM_CODEC_CMD
726
        call    [ctrl.ctrl_write8]
1340 tsdima 727
 
2434 Serge 728
        call    reset_codec
1340 tsdima 729
 
2434 Serge 730
        call    detect_codec
1340 tsdima 731
 
2434 Serge 732
        xor     eax, eax
733
        inc     eax
734
        ret
1340 tsdima 735
endp
736
 
737
align 4
738
proc reset_codec
739
 
2434 Serge 740
        mov     ecx, 255
1340 tsdima 741
.L1:
2434 Serge 742
        mov     edx, FM_CODEC_CMD
743
        call    [ctrl.ctrl_read16]
744
        test    ah, FM_CODEC_CMD_VALID shr 8
745
        jne     .L2
746
        loop    .L1
1340 tsdima 747
.L2:
2434 Serge 748
        mov     edx, FM_CODEC_CTL
749
        call    [ctrl.ctrl_read8]
750
        push    eax
751
        or      al, 0x20
752
        mov     edx, FM_CODEC_CTL
753
        call    [ctrl.ctrl_write8]
754
        pop     eax
755
        and     al, 0xDF
756
        mov     edx, FM_CODEC_CTL
757
        call    [ctrl.ctrl_write8]
1340 tsdima 758
 
2434 Serge 759
        xor     eax, eax
760
        inc     eax
761
        ret
1340 tsdima 762
endp
763
 
764
align 4
765
play:
2434 Serge 766
        mov     eax, 0x4000-1
767
        mov     edx, FM_PLAY_DMALEN
768
        call    [ctrl.ctrl_write16]
1340 tsdima 769
 
2434 Serge 770
        call    fill_buffer
1340 tsdima 771
 
2434 Serge 772
        mov     eax, FM_PLAY_START or FM_PLAY_STOPNOW or FM_PLAY_STEREO or FM_PLAY_16BIT or 0xA00
773
        mov     edx, FM_PLAY_CTL
774
        call    [ctrl.ctrl_write16]
1340 tsdima 775
 
2434 Serge 776
        xor     eax, eax
777
        ret
1340 tsdima 778
 
779
align 4
780
stop:
2434 Serge 781
        mov     edx, FM_PLAY_CTL
782
        call    [ctrl.ctrl_read16]
783
        and     eax, not (FM_PLAY_START or FM_PLAY_STOPNOW)
784
        or      eax, FM_PLAY_BUF1_LAST or FM_PLAY_BUF2_LAST
785
        mov     edx, FM_PLAY_CTL
786
        call    [ctrl.ctrl_write16]
1340 tsdima 787
 
2434 Serge 788
        xor     eax, eax
789
        ret
1340 tsdima 790
 
791
align 4
792
proc get_dev_info stdcall, p_info:dword
2434 Serge 793
           virtual at esi
794
             CTRL_INFO CTRL_INFO
795
           end virtual
1340 tsdima 796
 
2434 Serge 797
        mov     esi, [p_info]
798
        mov     eax, [ctrl.int_line]
799
        mov     ebx, [ctrl.codec_io_base]
800
        mov     ecx, [ctrl.ctrl_io_base]
801
        mov     edx, [ctrl.codec_mem_base]
802
        mov     edi, [ctrl.ctrl_mem_base]
1340 tsdima 803
 
2434 Serge 804
        mov     [CTRL_INFO.irq], eax
805
        mov     [CTRL_INFO.codec_io_base], ebx
806
        mov     [CTRL_INFO.ctrl_io_base], ecx
807
        mov     [CTRL_INFO.codec_mem_base], edx
808
        mov     [CTRL_INFO.ctrl_mem_base], edi
1340 tsdima 809
 
2434 Serge 810
        mov     eax, [codec.chip_id]
811
        mov     [CTRL_INFO.codec_id], eax
1340 tsdima 812
 
2434 Serge 813
        mov     ebx, [ctrl.pci_cmd]
814
        mov     [CTRL_INFO.pci_cmd], ebx
815
        ret
1340 tsdima 816
endp
817
 
818
align 4
819
proc set_callback stdcall, handler:dword
2434 Serge 820
        mov     eax, [handler]
821
        mov     [ctrl.user_callback], eax
822
        ret
1340 tsdima 823
endp
824
 
825
align 4
2434 Serge 826
proc codec_read stdcall, ac_reg:dword   ; reg = edx, reval = eax
1340 tsdima 827
 
2434 Serge 828
        mov     edx, [ac_reg]
1340 tsdima 829
 
2434 Serge 830
        mov     ebx, edx
831
        shr     ebx, 1
832
        bt      [codec.shadow_flag], ebx
833
        jc      .use_shadow
1340 tsdima 834
 
2434 Serge 835
        call    [ctrl.codec_read16]  ;change edx !!!
836
        mov     ecx, eax
1340 tsdima 837
 
838
.read_ok:
2434 Serge 839
        mov     edx, [ac_reg]
840
        mov     [codec.regs+edx], cx
841
        bts     [codec.shadow_flag], ebx
842
        mov     eax, ecx
843
        ret
1340 tsdima 844
.use_shadow:
2434 Serge 845
        movzx   eax, word [codec.regs+edx]
846
        ret
1340 tsdima 847
 
848
endp
849
 
850
align 4
851
proc codec_write stdcall, ac_reg:dword
852
 
2434 Serge 853
        mov     esi, [ac_reg]
1340 tsdima 854
 
2434 Serge 855
        mov     edx, esi
1340 tsdima 856
 
2434 Serge 857
        call    [ctrl.codec_write16]
1340 tsdima 858
 
2434 Serge 859
        mov     [codec.regs+esi], ax
860
        shr     esi, 1
861
        bts     [codec.shadow_flag], esi
1340 tsdima 862
 
2434 Serge 863
        ret
1340 tsdima 864
endp
865
 
866
align 4
867
proc check_semafore
868
align 4
869
.ok:
2434 Serge 870
        xor     eax, eax
871
        inc     eax
872
        ret
1340 tsdima 873
endp
874
 
875
align 4
876
proc StallExec
2434 Serge 877
        push    ecx
878
        push    edx
879
        push    ebx
880
        push    eax
1340 tsdima 881
 
2434 Serge 882
        mov     ecx, CPU_FREQ
883
        mul     ecx
884
        mov     ebx, eax      ;low
885
        mov     ecx, edx      ;high
886
        rdtsc
887
        add     ebx, eax
888
        adc     ecx, edx
1340 tsdima 889
@@:
2434 Serge 890
        rdtsc
891
        sub     eax, ebx
892
        sbb     edx, ecx
893
        js      @B
1340 tsdima 894
 
2434 Serge 895
        pop     eax
896
        pop     ebx
897
        pop     edx
898
        pop     ecx
899
        ret
1340 tsdima 900
endp
901
 
902
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
903
;          CONTROLLER IO functions
904
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
905
 
906
align 4
907
proc codec_io_r16
908
 
2434 Serge 909
        push    edx
910
        mov     ecx, 255
1340 tsdima 911
.L1:
2434 Serge 912
        mov     edx, FM_CODEC_CMD
913
        call    [ctrl.ctrl_read16]
914
        test    ah, FM_CODEC_CMD_BUSY shr 8
915
        je      .L2
916
        loop    .L1
1340 tsdima 917
.L2:
2434 Serge 918
        pop     eax
919
        or      al, FM_CODEC_CMD_READ
920
        mov     edx, FM_CODEC_CMD
921
        call    [ctrl.ctrl_write8]
1340 tsdima 922
 
2434 Serge 923
        mov     ecx, 255
1340 tsdima 924
.L3:
2434 Serge 925
        mov     edx, FM_CODEC_CMD
926
        call    [ctrl.ctrl_read16]
927
        test    ah, FM_CODEC_CMD_VALID shr 8
928
        jne     .L4
929
        loop    .L3
1340 tsdima 930
.L4:
2434 Serge 931
        mov     edx, FM_CODEC_DATA
932
        call    [ctrl.ctrl_read16]
1340 tsdima 933
 
2434 Serge 934
        ret
1340 tsdima 935
endp
936
 
937
align 4
938
proc codec_io_w16
939
 
2434 Serge 940
        push    edx
941
        push    eax
942
        mov     ecx, 255
1340 tsdima 943
.L1:
2434 Serge 944
        mov     edx, FM_CODEC_CMD
945
        call    [ctrl.ctrl_read16]
946
        test    ah, FM_CODEC_CMD_BUSY shr 8
947
        je      .L2
948
        loop    .L1
1340 tsdima 949
.L2:
2434 Serge 950
        pop     eax
951
        mov     edx, FM_CODEC_DATA
952
        call    [ctrl.ctrl_write16]
1340 tsdima 953
 
2434 Serge 954
        pop     eax
955
        mov     edx, FM_CODEC_CMD
956
        call    [ctrl.ctrl_write16]
1340 tsdima 957
 
2434 Serge 958
        ret
1340 tsdima 959
endp
960
 
961
align 4
962
proc ctrl_io_r8
2434 Serge 963
        add     edx, [ctrl.ctrl_io_base]
964
        in      al, dx
965
        ret
1340 tsdima 966
endp
967
 
968
align 4
969
proc ctrl_io_r16
2434 Serge 970
        add     edx, [ctrl.ctrl_io_base]
971
        in      ax, dx
972
        ret
1340 tsdima 973
endp
974
 
975
align 4
976
proc ctrl_io_r32
2434 Serge 977
        add     edx, [ctrl.ctrl_io_base]
978
        in      eax, dx
979
        ret
1340 tsdima 980
endp
981
 
982
align 4
983
proc ctrl_io_w8
2434 Serge 984
        add     edx, [ctrl.ctrl_io_base]
985
        out     dx, al
986
        ret
1340 tsdima 987
endp
988
 
989
align 4
990
proc ctrl_io_w16
2434 Serge 991
        add     edx, [ctrl.ctrl_io_base]
992
        out     dx, ax
993
        ret
1340 tsdima 994
endp
995
 
996
align 4
997
proc ctrl_io_w32
2434 Serge 998
        add     edx, [ctrl.ctrl_io_base]
999
        out     dx, eax
1000
        ret
1340 tsdima 1001
endp
1002
 
1003
align 4
1004
dword2str:
2434 Serge 1005
        mov     esi, hex_buff
1006
        mov     ecx, -8
1340 tsdima 1007
@@:
2434 Serge 1008
        rol     eax, 4
1009
        mov     ebx, eax
1010
        and     ebx, 0x0F
1011
        mov     bl, [ebx+hexletters]
1012
        mov     [8+esi+ecx], bl
1013
        inc     ecx
1014
        jnz     @B
1015
        ret
1340 tsdima 1016
 
1017
hexletters   db '0123456789ABCDEF'
1018
hex_buff     db 8 dup(0),13,10,0
1019
brg_bus      dd ?
1020
brg_devfn    dd ?
1021
include "codec.inc"
1022
 
1023
align 4
1024
devices dd (CTRL_FM801 shl 16)+VID_FM801, msg_FM801, set_FM
2434 Serge 1025
        dd 0
1340 tsdima 1026
 
1027
version      dd (5 shl 16) or (API_VERSION and 0xFFFF)
1028
 
1029
msg_FM801    db 'FM801 AC97 controller',13,10, 0
1030
msg_FM       db 'Forte Media',13,10, 0
1031
 
1032
sz_sound_srv db 'SOUND',0
1033
 
1034
msgInit       db 'detect hardware...',13,10,0
1035
msgFail       db 'device not found',13,10,0
1036
msgAttchIRQ   db 'IRQ line not supported', 13,10, 0
1037
msgInvIRQ     db 'IRQ line not assigned or invalid', 13,10, 0
1038
msgPlay       db 'start play', 13,10,0
1039
msgStop       db 'stop play',  13,10,0
1040
;msgNotify    db 'call notify',13,10,0
2434 Serge 1041
msgIRQ        db 'AC97 IRQ', 13,10,0
1340 tsdima 1042
msgInitCtrl  db 'init controller',13,10,0
1043
msgInitCodec db 'init codec',13,10,0
1044
msgPrimBuff   db 'create primary buffer ...',0
1045
msgDone       db 'done',13,10,0
1046
;msgReg       db 'set service handler',13,10,0
2434 Serge 1047
;msgOk        db 'service installed',13,10,0
1340 tsdima 1048
;msgStatus    db 'global status   ',0
1049
;msgControl   db 'global control  ',0
1050
msgPciCmd     db 'PCI command     ',0
1051
msgPciStat    db 'PCI status      ',0
1052
msgCtrlIsaIo  db 'controller io base   ',0
1053
msgIrqNum     db 'IRQ default          ',0
1054
;msgIrqMap    db 'AC97 irq map as      ',0
1055
 
1056
section '.data' data readable writable align 16
1057
 
1058
codec CODEC
1059
ctrl AC_CNTRL
1060
 
2434 Serge 1061
int_flip_flop      rd 1
1340 tsdima 1062
buffer_pgaddr      rd 1