1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
|
/*
* Support the ENSONIQ AudioPCI board based on the ES1370 and Codec
* AK4531.
*
* Copyright (c) 1998 by Joachim Kuebart. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgement:
* This product includes software developed by Joachim Kuebart.
*
* 4. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: es1370.c,v 1.3 1999/05/09 10:43:54 peter Exp $
*/
#include "pci.h"
#include "pcm.h"
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/kernel.h>
#include <machine/bus_pio.h>
#include <machine/bus_memio.h>
#include <machine/bus.h>
#include <pci/pcireg.h>
#include <pci/pcivar.h>
#include <pci/es1370_reg.h>
#include <i386/isa/snd/sound.h>
#define DSP_ULAW_NOT_WANTED
#include <i386/isa/snd/ulaw.h>
#if NPCI != 0
/* -------------------------------------------------------------------- */
/*
* #defines
*/
#ifdef __alpha__
#define IO_SPACE_MAPPING ALPHA_BUS_SPACE_IO
#define MEM_SPACE_MAPPING ALPHA_BUS_SPACE_MEM
#else /* not __alpha__ */
#define IO_SPACE_MAPPING I386_BUS_SPACE_IO
#define MEM_SPACE_MAPPING I386_BUS_SPACE_MEM
#endif /* not __alpha__ */
#define DMA_ALIGN_THRESHOLD 4
#define DMA_ALIGN_MASK (~(DMA_ALIGN_THRESHOLD - 1))
#define DMA_READ_THRESHOLD 0x200
#define MEM_MAP_REG 0x14
#define UNIT(minor) ((minor) >> 4)
#define DEV(minor) ((minor) & 0xf)
/* -------------------------------------------------------------------- */
/*
* PCI IDs of supported chips
*/
#define ES1370_PCI_ID 0x50001274
/* -------------------------------------------------------------------- */
/*
* device private data
*/
struct es_info {
bus_space_tag_t st;
bus_space_handle_t sh;
bus_dma_tag_t parent_dmat;
bus_dmamap_t dmam_in, dmam_out;
/* Contents of board's registers */
u_long ctrl;
u_long sctrl;
};
/* -------------------------------------------------------------------- */
/*
* prototypes
*/
static void dma_wrintr(snddev_info *);
static void dma_rdintr(snddev_info *);
static int es_init(snddev_info *);
static snd_callback_t es_callback;
static d_open_t es_dsp_open;
static d_close_t es_dsp_close;
static d_ioctl_t es_dsp_ioctl;
static d_read_t es_dsp_read;
static d_write_t es_dsp_write;
static void es_intr(void *);
static int es_rdabort(snddev_info *);
static void es_rd_map(void *, bus_dma_segment_t *, int, int);
static int es_wrabort(snddev_info *);
static void es_wr_map(void *, bus_dma_segment_t *, int, int);
static const char *es_pci_probe __P((pcici_t, pcidi_t));
static void es_pci_attach __P((pcici_t, int));
static int es_rd_dmaupdate(snddev_info *);
static d_select_t es_select;
static int es_wr_dmaupdate(snddev_info *);
static int alloc_dmabuf(snddev_info *, int);
static int write_codec(snddev_info *, u_char, u_char);
/* -------------------------------------------------------------------- */
/*
* PCI driver and PCM driver method tables
*/
static struct pci_device es_pci_driver = {
"es",
es_pci_probe,
es_pci_attach,
&nsnd,
NULL
};
COMPAT_PCI_DRIVER(es_pci, es_pci_driver);
static snddev_info es_op_desc = {
"ENSONIQ AudioPCI",
0, /* type, apparently unused */
NULL, /* ISA probe */
NULL, /* ISA attach */
es_dsp_open,
es_dsp_close,
es_dsp_read,
es_dsp_write,
es_dsp_ioctl,
es_select,
NULL, /* Interrupt Service Routine */
es_callback,
ES_BUFFSIZE,
AFMT_FULLDUPLEX | AFMT_STEREO | AFMT_U8 | AFMT_S16_LE, /* brag :-) */
};
/* -------------------------------------------------------------------- */
/*
* The mixer interface
*/
static const struct {
unsigned volidx:4;
unsigned left:4;
unsigned right:4;
unsigned stereo:1;
unsigned recmask:13;
unsigned avail:1;
} mixtable[SOUND_MIXER_NRDEVICES] = {
[SOUND_MIXER_VOLUME] = { 0, 0x0, 0x1, 1, 0x0000, 1 },
[SOUND_MIXER_PCM] = { 1, 0x2, 0x3, 1, 0x0400, 1 },
[SOUND_MIXER_SYNTH] = { 2, 0x4, 0x5, 1, 0x0060, 1 },
[SOUND_MIXER_CD] = { 3, 0x6, 0x7, 1, 0x0006, 1 },
[SOUND_MIXER_LINE] = { 4, 0x8, 0x9, 1, 0x0018, 1 },
[SOUND_MIXER_LINE1] = { 5, 0xa, 0xb, 1, 0x1800, 1 },
[SOUND_MIXER_LINE2] = { 6, 0xc, 0x0, 0, 0x0100, 1 },
[SOUND_MIXER_LINE3] = { 7, 0xd, 0x0, 0, 0x0200, 1 },
[SOUND_MIXER_MIC] = { 8, 0xe, 0x0, 0, 0x0001, 1 },
[SOUND_MIXER_OGAIN] = { 9, 0xf, 0x0, 0, 0x0000, 1 } };
static int
mixer_ioctl(snddev_info *d, u_long cmd, caddr_t data, int fflag, struct proc *p)
{
int i, j, *val, ret = 0;
val = (int *)data;
i = cmd & 0xff;
switch (cmd & IOC_DIRMASK) {
case IOC_IN | IOC_OUT: /* _IOWR */
switch (i) {
case SOUND_MIXER_RECSRC:
for (i = j = 0; i != SOUND_MIXER_NRDEVICES; i++)
if ((*val & (1 << i)) != 0) {
if (!mixtable[i].recmask)
*val &= ~(1 << i);
else
j |= mixtable[i].recmask;
}
d->mix_recsrc = *val;
write_codec(d, CODEC_LIMIX1, j & 0x55);
write_codec(d, CODEC_RIMIX1, j & 0xaa);
write_codec(d, CODEC_LIMIX2, (j >> 8) & 0x17);
write_codec(d, CODEC_RIMIX2, (j >> 8) & 0x0f);
write_codec(d, CODEC_OMIX1, 0x7f);
write_codec(d, CODEC_OMIX2, 0x3f);
break;
default:
if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].avail)
ret = EINVAL;
else {
int l, r, rl, rr;
l = *val & 0xff;
if (l > 100)
l = 100;
if (mixtable[i].left == 0xf) {
if (l < 2)
rl = 0x80;
else
rl = 7 - (l - 2) / 14;
} else {
if (l < 10)
rl = 0x80;
else
rl = 15 - (l - 10) / 6;
}
if (mixtable[i].stereo) {
r = (*val >> 8) & 0xff;
if (r > 100)
r = 100;
if (r < 10)
rr = 0x80;
else
rr = 15 - (r - 10) / 6;
write_codec(d, mixtable[i].right, rr);
} else
r = l;
write_codec(d, mixtable[i].left, rl);
*val = d->mix_levels[i] = ((u_int) r << 8) | l;
}
break;
}
break;
default:
ret = ENOSYS;
break;
}
return (ret);
}
/* -------------------------------------------------------------------- */
/*
* File operations
*/
static int
es_dsp_open(dev_t dev, int oflags, int devtype, struct proc *p)
{
int unit = UNIT(minor(dev));
snddev_info *d = &pcm_info[unit];
if (d->flags & SND_F_BUSY)
return (EBUSY);
d->flags = 0;
d->dbuf_out.total = d->dbuf_out.prev_total =
d->dbuf_in.total = d->dbuf_in.prev_total = 0;
switch (DEV(minor(dev))) {
case SND_DEV_DSP16:
d->play_fmt = d->rec_fmt = AFMT_S16_LE;
break;
case SND_DEV_DSP:
d->play_fmt = d->rec_fmt = AFMT_U8;
break;
case SND_DEV_AUDIO:
d->play_fmt = d->rec_fmt = AFMT_MU_LAW;
break;
default:
return (ENXIO);
}
if ((oflags & FREAD) == 0)
d->rec_fmt = 0;
else if ((oflags & FWRITE) == 0)
d->play_fmt = 0;
d->play_speed = d->rec_speed = DSP_DEFAULT_SPEED;
d->flags |= SND_F_BUSY;
if (oflags & O_NONBLOCK)
d->flags |= SND_F_NBIO;
ask_init(d);
return (0);
}
static int
es_dsp_close(dev_t dev, int cflags, int devtype, struct proc *p)
{
int unit = UNIT(minor(dev));
snddev_info *d = &pcm_info[unit];
d->flags &= ~SND_F_BUSY;
es_rdabort(d);
return (0);
}
static int
es_dsp_read(dev_t dev, struct uio *buf, int flag)
{
int l, l1, limit, ret = 0, unit = UNIT(minor(dev));
long s;
snddev_info *d = &pcm_info[unit];
snd_dbuf *b = &d->dbuf_in;
if (d->flags & SND_F_READING) {
/* This shouldn't happen and is actually silly */
tsleep(&s, PZERO, "sndar", hz);
return (EBUSY);
}
d->flags |= SND_F_READING;
/*
* XXX Check for SND_F_INIT. If set, wait for DMA to run empty and
* re-initialize the board
*/
if (buf->uio_resid - d->rec_blocksize > 0)
limit = buf->uio_resid - d->rec_blocksize;
else
limit = 0;
while ((l = buf->uio_resid) > limit) {
s = spltty();
es_rd_dmaupdate(d);
if ((l = min(l, b->rl)) == 0) {
int timeout;
if (b->dl == 0)
dma_rdintr(d);
if (d->flags & SND_F_NBIO) {
splx(s);
break;
}
if (buf->uio_resid - limit > b->dl)
timeout = hz;
else
timeout = 1;
splx(s);
switch (ret = tsleep((caddr_t)b, PRIBIO | PCATCH,
"dsprd", timeout)) {
case EINTR:
es_rdabort(d);
/* FALLTHROUGH */
case ERESTART:
break;
default:
continue;
}
break;
}
splx(s);
if ((l1 = b->bufsize - b->rp) < l) {
if (d->flags & SND_F_XLAT8) {
translate_bytes(ulaw_dsp, b->buf + b->rp, l1);
translate_bytes(ulaw_dsp, b->buf, l - l1);
}
uiomove(b->buf + b->rp, l1, buf);
uiomove(b->buf, l - l1, buf);
} else {
if (d->flags & SND_F_XLAT8)
translate_bytes(ulaw_dsp, b->buf + b->rp, l);
uiomove(b->buf + b->rp, l, buf);
}
s = spltty();
b->fl += l;
b->rl -= l;
b->rp = (b->rp + l) % b->bufsize;
splx(s);
}
d->flags &= ~SND_F_READING;
return (ret);
}
static int
es_dsp_write(dev_t dev, struct uio *buf, int flag)
{
int l, l1, ret = 0, unit = UNIT(minor(dev));
long s;
snddev_info *d = &pcm_info[unit];
snd_dbuf *b = &d->dbuf_out;
if (d->flags & SND_F_WRITING) {
/* This shouldn't happen and is actually silly */
tsleep(&s, PZERO, "sndaw", hz);
return (EBUSY);
}
d->flags |= SND_F_WRITING;
/*
* XXX Check for SND_F_INIT. If set, wait for DMA to run empty and
* re-initialize the board
*/
while ((l = buf->uio_resid) != 0) {
s = spltty();
es_wr_dmaupdate(d);
if ((l = min(l, b->fl)) == 0) {
int timeout;
if (d->flags & SND_F_NBIO) {
splx(s);
break;
}
if (buf->uio_resid >= b->dl)
timeout = hz;
else
timeout = 1;
splx(s);
switch (ret = tsleep((caddr_t)b, PRIBIO | PCATCH,
"dspwr", timeout)) {
case EINTR:
es_wrabort(d);
/* FALLTHROUGH */
case ERESTART:
break;
default:
continue;
}
break;
}
splx(s);
if ((l1 = b->bufsize - b->fp) < l) {
uiomove(b->buf + b->fp, l1, buf);
uiomove(b->buf, l - l1, buf);
if (d->flags & SND_F_XLAT8) {
translate_bytes(ulaw_dsp, b->buf + b->fp, l1);
translate_bytes(ulaw_dsp, b->buf, l - l1);
}
} else {
uiomove(b->buf + b->fp, l, buf);
if (d->flags & SND_F_XLAT8)
translate_bytes(ulaw_dsp, b->buf + b->fp, l);
}
s = spltty();
b->rl += l;
b->fl -= l;
b->fp = (b->fp + l) % b->bufsize;
if (b->dl == 0)
dma_wrintr(d);
splx(s);
}
d->flags &= ~SND_F_WRITING;
return (ret);
}
static int
es_dsp_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
{
int ret = 0, unit = UNIT(minor(dev));
snddev_info *d = &pcm_info[unit];
long s;
if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0))
return mixer_ioctl(d, cmd, data, fflag, p);
switch(cmd) {
case AIONWRITE:
if (d->dbuf_out.dl != 0) {
s = spltty();
es_wr_dmaupdate(d);
splx(s);
}
*(int *)data = d->dbuf_out.fl;
break;
case FIONREAD:
if (d->dbuf_in.dl != 0) {
s = spltty();
es_rd_dmaupdate(d);
splx(s);
}
*(int *)data = d->dbuf_in.rl;
break;
case SNDCTL_DSP_GETISPACE:
{
audio_buf_info *a = (audio_buf_info *)data;
snd_dbuf *b = &d->dbuf_in;
if (b->dl != 0) {
s = spltty();
es_rd_dmaupdate(d);
splx(s);
}
a->bytes = b->fl;
a->fragments = b->fl / d->rec_blocksize;
a->fragstotal = b->bufsize / d->rec_blocksize;
a->fragsize = d->rec_blocksize;
}
break;
case SNDCTL_DSP_GETOSPACE:
{
audio_buf_info *a = (audio_buf_info *)data;
snd_dbuf *b = &d->dbuf_out;
if (b->dl != 0) {
s = spltty();
es_wr_dmaupdate(d);
splx(s);
}
a->bytes = b->fl;
a->fragments = b->fl / d->rec_blocksize;
a->fragstotal = b->bufsize / d->play_blocksize;
a->fragsize = d->play_blocksize;
}
break;
case SNDCTL_DSP_GETIPTR:
{
count_info *c = (count_info *)data;
snd_dbuf *b = &d->dbuf_in;
if (b->dl != 0) {
s = spltty();
es_rd_dmaupdate(d);
splx(s);
}
c->bytes = b->total;
c->blocks = (b->total - b->prev_total +
d->rec_blocksize - 1) / d->rec_blocksize;
c->ptr = b->fp;
b->prev_total = b->total;
}
break;
case SNDCTL_DSP_GETOPTR:
{
count_info *c = (count_info *)data;
snd_dbuf *b = &d->dbuf_out;
if (b->dl != 0) {
s = spltty();
es_wr_dmaupdate(d);
splx(s);
}
c->bytes = b->total;
c->blocks = (b->total - b->prev_total +
d->play_blocksize - 1) / d->play_blocksize;
c->ptr = b->rp;
b->prev_total = b->total;
}
break;
case AIOSTOP:
case SNDCTL_DSP_RESET:
case SNDCTL_DSP_SYNC:
ret = EINVAL;
break;
default:
ret = ENOSYS;
break;
}
return (ret);
}
static int
es_select(dev_t i_dev, int rw, struct proc * p)
{
return (ENOSYS);
}
/* -------------------------------------------------------------------- */
/*
* The interrupt handler
*/
static void
es_intr (void *p)
{
snddev_info *d = (snddev_info *)p;
struct es_info *es = (struct es_info *)d->device_data;
unsigned intsrc, sctrl;
intsrc = bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS);
if ((intsrc & STAT_INTR) == 0)
return;
sctrl = es->sctrl;
if (intsrc & STAT_ADC)
sctrl &= ~SCTRL_R1INTEN;
if (intsrc & STAT_DAC1)
sctrl &= ~SCTRL_P1INTEN;
if (intsrc & STAT_DAC2) {
sctrl &= ~SCTRL_P2INTEN;
}
bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, sctrl);
bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL,
es->sctrl);
if (intsrc & STAT_DAC2)
dma_wrintr(d);
if (intsrc & STAT_ADC)
dma_rdintr(d);
}
/* -------------------------------------------------------------------- */
/*
* DMA hassle
*/
static int
alloc_dmabuf(snddev_info *d, int rd)
{
struct es_info *es = (struct es_info *)d->device_data;
snd_dbuf *b = rd ? &d->dbuf_in : &d->dbuf_out;
bus_dmamap_t *dmam = rd ? &es->dmam_in : &es->dmam_out;
if (bus_dmamem_alloc(es->parent_dmat, (void **)&b->buf, BUS_DMA_NOWAIT,
dmam) != 0 ||
bus_dmamap_load(es->parent_dmat, *dmam, b->buf, d->bufsize,
rd ? es_rd_map : es_wr_map, es, 0) != 0)
return -1;
b->rp = b->fp = b->dl = b->rl = 0;
b->fl = b->bufsize = d->bufsize;
return (0);
}
static int
es_wr_dmaupdate(snddev_info *d)
{
struct es_info *es = (struct es_info *)d->device_data;
unsigned hwptr, delta;
bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE,
ES1370_REG_DAC2_FRAMECNT >> 8);
hwptr = (bus_space_read_4(es->st, es->sh,
ES1370_REG_DAC2_FRAMECNT & 0xff) >> 14) & 0x3fffc;
delta = (d->dbuf_out.bufsize + hwptr - d->dbuf_out.rp) %
d->dbuf_out.bufsize;
d->dbuf_out.rp = hwptr;
d->dbuf_out.rl -= delta;
d->dbuf_out.fl += delta;
d->dbuf_out.total += delta;
return delta;
}
static int
es_rd_dmaupdate(snddev_info *d)
{
struct es_info *es = (struct es_info *)d->device_data;
unsigned hwptr, delta;
bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE,
ES1370_REG_ADC_FRAMECNT >> 8);
hwptr = (bus_space_read_4(es->st, es->sh,
ES1370_REG_ADC_FRAMECNT & 0xff) >> 14) & 0x3fffc;
delta = (d->dbuf_in.bufsize + hwptr - d->dbuf_in.fp) %
d->dbuf_in.bufsize;
d->dbuf_in.fp = hwptr;
d->dbuf_in.rl += delta;
d->dbuf_in.fl -= delta;
d->dbuf_in.total += delta;
return delta;
}
/* -------------------------------------------------------------------- */
/*
* Hardware
*/
static int
es_callback(snddev_info *d, int reason)
{
struct es_info *es = (struct es_info *)d->device_data;
int rd = reason & SND_CB_RD;
switch(reason & SND_CB_REASON_MASK) {
case SND_CB_INIT:
es->ctrl = (es->ctrl & ~CTRL_PCLKDIV) |
(DAC2_SRTODIV(d->play_speed) << CTRL_SH_PCLKDIV);
snd_set_blocksize(d);
es->sctrl &= ~(SCTRL_R1FMT | SCTRL_P2FMT);
d->flags &= ~SND_F_XLAT8;
switch(d->play_fmt) {
case 0:
case AFMT_U8:
break;
case AFMT_S16_LE:
es->sctrl |= SCTRL_P2SEB;
break;
case AFMT_MU_LAW:
d->flags |= SND_F_XLAT8;
break;
default:
return (-1);
}
switch(d->rec_fmt) {
case 0:
case AFMT_U8:
break;
case AFMT_S16_LE:
es->sctrl |= SCTRL_R1SEB;
break;
case AFMT_MU_LAW:
d->flags |= SND_F_XLAT8;
break;
default:
return (-1);
}
if (d->flags & SND_F_STEREO)
es->sctrl |= SCTRL_P2SMB | SCTRL_R1SMB;
bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL,
es->ctrl);
bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL,
es->sctrl);
break;
case SND_CB_START:
if (rd) {
es->ctrl |= CTRL_ADC_EN;
es->sctrl = (es->sctrl & ~SCTRL_R1LOOPSEL) |
SCTRL_R1INTEN;
bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_SCOUNT,
d->dbuf_in.dl / d->dbuf_in.sample_size - 1);
} else {
es->ctrl |= CTRL_DAC2_EN;
es->sctrl = (es->sctrl & ~(SCTRL_P2ENDINC |
SCTRL_P2STINC | SCTRL_P2LOOPSEL | SCTRL_P2PAUSE |
SCTRL_P2DACSEN)) | SCTRL_P2INTEN |
(((d->play_fmt == AFMT_S16_LE) ? 2 : 1)
<< SCTRL_SH_P2ENDINC);
bus_space_write_4(es->st, es->sh,
ES1370_REG_DAC2_SCOUNT,
d->dbuf_out.dl / d->dbuf_out.sample_size - 1);
}
bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL,
es->sctrl);
bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
break;
case SND_CB_ABORT:
case SND_CB_STOP:
if (rd)
es->ctrl &= ~CTRL_ADC_EN;
else
es->ctrl &= ~CTRL_DAC2_EN;
bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
break;
default:
return (-1);
}
return (0);
}
static int
write_codec(snddev_info *d, u_char i, u_char data)
{
struct es_info *es = (struct es_info *)d->device_data;
int wait = 100; /* 100 msec timeout */
do {
if ((bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS) &
STAT_CSTAT) == 0) {
bus_space_write_2(es->st, es->sh, ES1370_REG_CODEC,
((u_short)i << CODEC_INDEX_SHIFT) | data);
return (0);
}
DELAY(1000);
/* tsleep(&wait, PZERO, "sndaw", hz / 1000); */
} while (--wait);
printf("pcm: write_codec timed out\n");
return (-1);
}
static void
es_wr_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
struct es_info *es = (struct es_info *)arg;
bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE,
ES1370_REG_DAC2_FRAMEADR >> 8);
bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMEADR & 0xff,
segs->ds_addr);
bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMECNT & 0xff,
(segs->ds_len >> 2) - 1);
}
static void
es_rd_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
struct es_info *es = (struct es_info *)arg;
bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE,
ES1370_REG_ADC_FRAMEADR >> 8);
bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMEADR & 0xff,
segs->ds_addr);
bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMECNT & 0xff,
(segs->ds_len >> 2) - 1);
}
static void
dma_wrintr(snddev_info *d)
{
snd_dbuf *b = &d->dbuf_out;
/*
* According to Linux driver:
* dmaupdate()
* Bei underrun error++
* wake_up(dac2.wait)
*/
if (b->dl != 0) {
es_wr_dmaupdate(d);
wakeup(b);
}
if (b->rl >= DMA_ALIGN_THRESHOLD &&
!(d->flags & SND_F_ABORTING)) {
int l = min(b->rl, d->play_blocksize);
l &= DMA_ALIGN_MASK;
if (l != b->dl) {
if (b->dl != 0) {
d->callback(d, SND_CB_WR | SND_CB_STOP);
es_wr_dmaupdate(d);
l = min(b->rl, d->play_blocksize);
l &= DMA_ALIGN_MASK;
}
b->dl = l;
d->callback(d, SND_CB_WR | SND_CB_START);
}
} else if (b->dl != 0) {
b->dl = 0;
d->callback(d, SND_CB_WR | SND_CB_STOP);
es_wr_dmaupdate(d);
}
}
static void
dma_rdintr(snddev_info *d)
{
snd_dbuf *b = &d->dbuf_in;
if (b->dl != 0) {
es_rd_dmaupdate(d);
wakeup(b);
}
if (b->fl >= DMA_READ_THRESHOLD &&
!(d->flags & SND_F_ABORTING)) {
int l = min(b->fl, d->rec_blocksize);
l &= DMA_ALIGN_MASK;
if (l != b->dl) {
if (b->dl != 0) {
d->callback(d, SND_CB_RD | SND_CB_STOP);
es_rd_dmaupdate(d);
l = min(b->fl, d->rec_blocksize);
l &= DMA_ALIGN_MASK;
}
b->dl = l;
d->callback(d, SND_CB_RD | SND_CB_START);
}
} else {
if (b->dl != 0) {
b->dl = 0;
d->callback(d, SND_CB_RD | SND_CB_STOP);
es_rd_dmaupdate(d);
}
}
}
static int
es_wrabort(snddev_info *d)
{
snd_dbuf *b = &d->dbuf_out;
long s;
int missing;
s = spltty();
if (b->dl != 0) {
wakeup(b);
b->dl = 0;
d->callback(d, SND_CB_WR | SND_CB_ABORT);
}
es_wr_dmaupdate(d);
missing = b->rl;
b->rl = 0;
b->fp = b->rp;
b->fl = b->bufsize;
splx(s);
return missing;
}
static int
es_rdabort(snddev_info *d)
{
snd_dbuf *b = &d->dbuf_in;
long s;
int missing;
s = spltty();
if (b->dl != 0) {
wakeup(b);
b->dl = 0;
d->callback(d, SND_CB_RD | SND_CB_ABORT);
es_rd_dmaupdate(d);
}
missing = b->rl;
b->rl = 0;
b->fp = b->rp;
b->fl = b->bufsize;
splx(s);
return missing;
}
/* -------------------------------------------------------------------- */
/*
* Probe and attach the card
*/
static int
es_init(snddev_info *d)
{
struct es_info *es = (struct es_info *)d->device_data;
u_int i;
es->ctrl = CTRL_CDC_EN | CTRL_SERR_DIS |
(DAC2_SRTODIV(DSP_DEFAULT_SPEED) << CTRL_SH_PCLKDIV);
bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
es->sctrl = 0;
bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
write_codec(d, CODEC_RES_PD, 3);/* No RST, PD */
write_codec(d, CODEC_CSEL, 0); /* CODEC ADC and CODEC DAC use
* {LR,B}CLK2 and run off the LRCLK2
* PLL; program DAC_SYNC=0! */
write_codec(d, CODEC_ADSEL, 0); /* Recording source is mixer */
write_codec(d, CODEC_MGAIN, 0); /* MIC amp is 0db */
i = SOUND_MASK_MIC;
mixer_ioctl(d, SOUND_MIXER_WRITE_RECSRC, (caddr_t) &i, 0, NULL);
i = 0;
mixer_ioctl(d, SOUND_MIXER_WRITE_VOLUME, (caddr_t) &i, 0, NULL);
mixer_ioctl(d, SOUND_MIXER_WRITE_PCM, (caddr_t) &i, 0, NULL);
mixer_ioctl(d, SOUND_MIXER_WRITE_SYNTH, (caddr_t) &i, 0, NULL);
mixer_ioctl(d, SOUND_MIXER_WRITE_CD, (caddr_t) &i, 0, NULL);
mixer_ioctl(d, SOUND_MIXER_WRITE_LINE, (caddr_t) &i, 0, NULL);
mixer_ioctl(d, SOUND_MIXER_WRITE_LINE1, (caddr_t) &i, 0, NULL);
mixer_ioctl(d, SOUND_MIXER_WRITE_LINE2, (caddr_t) &i, 0, NULL);
mixer_ioctl(d, SOUND_MIXER_WRITE_LINE3, (caddr_t) &i, 0, NULL);
mixer_ioctl(d, SOUND_MIXER_WRITE_MIC, (caddr_t) &i, 0, NULL);
return (0);
}
static const char *
es_pci_probe(pcici_t tag, pcidi_t type)
{
if (type == ES1370_PCI_ID)
return ("AudioPCI ES1370");
return (NULL);
}
static void
es_pci_attach(pcici_t config_id, int unit)
{
snddev_info *d;
u_int32_t data;
struct es_info *es;
pci_port_t io_port;
int i, mapped;
vm_offset_t vaddr, paddr;
if (unit > NPCM_MAX)
return;
d = &pcm_info[unit];
*d = es_op_desc;
if ((es = malloc(sizeof(*es), M_DEVBUF, M_NOWAIT)) == NULL) {
printf("pcm%d: cannot allocate softc\n", unit);
return;
}
bzero(es, sizeof(*es));
d->device_data = es;
vaddr = paddr = NULL;
mapped = 0;
data = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG);
if (mapped == 0 && (data & PCI_COMMAND_MEM_ENABLE)) {
if (pci_map_mem(config_id, MEM_MAP_REG, &vaddr, &paddr)) {
es->st = MEM_SPACE_MAPPING;
es->sh = vaddr;
mapped++;
}
}
if (mapped == 0 && (data & PCI_COMMAND_IO_ENABLE)) {
if (pci_map_port(config_id, PCI_MAP_REG_START, &io_port)) {
es->st = IO_SPACE_MAPPING;
es->sh = io_port;
mapped++;
}
}
if (mapped == 0) {
printf("pcm%d: unable to map any ports\n", unit);
free(es, M_DEVBUF);
return;
}
printf("pcm%d: using %s space register mapping at %#x\n", unit,
es->st == IO_SPACE_MAPPING ? "I/O" : "Memory", es->sh);
d->io_base = es->sh;
d->mix_devs = 0;
for (i = 0; i != SOUND_MIXER_NRDEVICES; i++)
if (mixtable[i].avail)
d->mix_devs |= (1 << i);
d->mix_rec_devs = 0;
for (i = 0; i != SOUND_MIXER_NRDEVICES; i++)
if (mixtable[i].recmask)
d->mix_rec_devs |= (1 << i);
if (es_init(d) == -1) {
printf("pcm%d: unable to initialize the card\n", unit);
free(es, M_DEVBUF);
d->io_base = 0;
return;
}
if (pci_map_int(config_id, es_intr, d, &tty_imask) == 0) {
printf("pcm%d: unable to map interrupt\n", unit);
free(es, M_DEVBUF);
d->io_base = 0;
return;
}
if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
/*highaddr*/BUS_SPACE_MAXADDR,
/*filter*/NULL, /*filterarg*/NULL,
/*maxsize*/d->bufsize, /*nsegments*/1, /*maxsegz*/0x3ffff,
/*flags*/0, &es->parent_dmat) != 0) {
printf("pcm%d: unable to create dma tag\n", unit);
free(es, M_DEVBUF);
d->io_base = 0;
return;
}
if (alloc_dmabuf(d, 0) == -1 ||
alloc_dmabuf(d, 1) == -1) {
printf("pcm%d: unable to allocate dma buffers\n", unit);
free(es, M_DEVBUF);
d->io_base = 0;
return;
}
pcminit(d, unit);
return;
}
#endif /* NPCI != 0 */
|