summaryrefslogtreecommitdiff
path: root/sys/dev/ic/interwave.c
blob: eb7adefe5769b4d1343dfca65d90e7fb2f3404e6 (plain)
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
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
/*	$NetBSD: interwave.c,v 1.43 2020/02/29 05:51:11 isaki Exp $	*/

/*
 * Copyright (c) 1997, 1999, 2008 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Author: Kari Mettinen
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: interwave.c,v 1.43 2020/02/29 05:51:11 isaki Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/fcntl.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/cpu.h>
#include <sys/intr.h>
#include <sys/audioio.h>

#include <machine/pio.h>

#include <dev/audio/audio_if.h>

#include <dev/isa/isavar.h>
#include <dev/isa/isadmavar.h>

#include <dev/ic/interwavereg.h>
#include <dev/ic/interwavevar.h>


static void iwreset(struct iw_softc *, int);

static int iw_set_speed(struct iw_softc *, u_long, char);
static u_long iw_set_format(struct iw_softc *, u_long, int);
static void iw_mixer_line_level(struct iw_softc *, int, int, int);
static void iw_trigger_dma(struct iw_softc *, u_char);
static void iw_stop_dma(struct iw_softc *, u_char, u_char);
static void iw_dma_count(struct iw_softc *, u_short, int);
static int iwintr(void *);
static void iw_meminit(struct iw_softc *);
static void iw_mempoke(struct iw_softc *, u_long, u_char);
static u_char iw_mempeek(struct iw_softc *, u_long);

#ifdef USE_WAVETABLE
static void iw_set_voice_place(struct iw_softc *, u_char, u_long);
static void iw_voice_pan(struct iw_softc *, u_char, u_short, u_short);
static void iw_voice_freq(struct iw_softc *, u_char, u_long);
static void iw_set_loopmode(struct iw_softc *, u_char, u_char, u_char);
static void iw_set_voice_pos(struct iw_softc *, u_short, u_long, u_long);
static void iw_start_voice(struct iw_softc *, u_char);
static void iw_play_voice(struct iw_softc *, u_long, u_long, u_short);
static void iw_stop_voice(struct iw_softc *, u_char);
static void iw_move_voice_end(struct iw_softc *, u_short, u_long);
static void iw_initvoices(struct iw_softc *);
#endif

struct audio_device iw_device = {
	"Am78C201",
	"0.1",
	"guspnp"
};

/* The HW supports more formats but only SLINEAR_NE/16/2ch is enough. */
static const struct audio_format iw_formats[] = {
	{
		.mode		= AUMODE_PLAY | AUMODE_RECORD,
		.encoding	= AUDIO_ENCODING_SLINEAR_NE,
		.validbits	= 16,
		.precision	= 16,
		.channels	= 2,
		.channel_mask	= AUFMT_STEREO,
		.frequency_type	= 16,
		.frequency	= {
			 5510,  6620,  8000,  9600, 11025,
			16000, 18900, 22050, 27420, 32000,
			33075, 37800, 38400, 44100, 44800, 48000,
		},
	},
};
#define IW_NFORMATS __arraycount(iw_formats)

#ifdef AUDIO_DEBUG
int iw_debug;
#define DPRINTF(p)       if (iw_debug) printf p
#else
#define DPRINTF(p)
#endif

static int      iw_cc = 1;
#ifdef DIAGNOSTIC
static int      outputs = 0;
static int      iw_ints = 0;
static int      inputs = 0;
static int      iw_inints = 0;
#endif

int
iwintr(void *arg)
{
	struct	iw_softc *sc;
	int	val;
	u_char	intrs;

	sc = arg;
	val = 0;
	intrs = 0;

	mutex_spin_enter(&sc->sc_intr_lock);

	IW_READ_DIRECT_1(6, sc->p2xr_h, intrs);	/* UISR */

	/* codec ints */

	/*
	 * The proper order to do this seems to be to read CSR3 to get the
	 * int cause and fifo over underrrun status, then deal with the ints
	 * (new DMA set up), and to clear ints by writing the respective bit
	 * to 0.
	 */

	/* read what ints happened */

	IW_READ_CODEC_1(CSR3I, intrs);

	/* clear them */

	IW_WRITE_DIRECT_1(2, sc->codec_index_h, 0x00);

	/* and process them */

	if (intrs & 0x20) {
#ifdef DIAGNOSTIC
		iw_inints++;
#endif
		if (sc->sc_recintr != 0)
			sc->sc_recintr(sc->sc_recarg);
		val = 1;
	}
	if (intrs & 0x10) {
#ifdef DIAGNOSTIC
		iw_ints++;
#endif
		if (sc->sc_playintr != 0)
			sc->sc_playintr(sc->sc_playarg);
		val = 1;
	}

	mutex_spin_exit(&sc->sc_intr_lock);

	return val;
}

void
iwattach(struct iw_softc *sc)
{
	int	got_irq;

	DPRINTF(("iwattach sc %p\n", sc));
	got_irq = 0;

	sc->cdatap = 1;		/* relative offsets in region */
	sc->csr1r = 2;
	sc->cxdr = 3;		/* CPDR or CRDR */

	sc->gmxr = 0;		/* sc->p3xr */
	sc->gmxdr = 1;		/* GMTDR or GMRDR */
	sc->svsr = 2;
	sc->igidxr = 3;
	sc->i16dp = 4;
	sc->i8dp = 5;
	sc->lmbdr = 7;

	sc->rec_precision = sc->play_precision = 8;
	sc->rec_channels = sc->play_channels = 1;
	sc->rec_encoding = sc->play_encoding = AUDIO_ENCODING_ULAW;
	sc->sc_irate = 8000;
	sc->sc_orate = 8000;

	sc->sc_fullduplex = 1;

	sc->sc_dma_flags = 0;

	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);

	aprint_naive("\n");
	/*
	 * We can only use a few selected irqs, see if we got one from pnp
	 * code that suits us.
	 */

	if (sc->sc_irq > 0) {
		sc->sc_ih = isa_intr_establish(sc->sc_p2xr_ic,
		    sc->sc_irq, IST_EDGE, IPL_AUDIO, iwintr, sc);
		got_irq = 1;
	}
	if (!got_irq) {
		aprint_error("\niwattach: couldn't get a suitable irq\n");
		mutex_destroy(&sc->sc_lock);
		mutex_destroy(&sc->sc_intr_lock);
		return;
	}
	aprint_normal("\n");
	iwreset(sc, 0);
	iw_set_format(sc, AUDIO_ENCODING_ULAW, 0);
	iw_set_format(sc, AUDIO_ENCODING_ULAW, 1);
	aprint_normal("%s: interwave version %s\n",
	    device_xname(sc->sc_dev), iw_device.version);
	audio_attach_mi(sc->iw_hw_if, sc, sc->sc_dev);
}

int
iwopen(struct iw_softc *sc, int flags)
{

	DPRINTF(("iwopen: sc %p\n", sc));

#ifdef DIAGNOSTIC
	outputs = 0;
	iw_ints = 0;
	inputs = 0;
	iw_inints = 0;
#endif

	iwreset(sc, 1);

	return 0;
}

void
iwclose(void *addr)
{

	DPRINTF(("iwclose sc %p\n", addr));
#ifdef DIAGNOSTIC
	DPRINTF(("iwclose: outputs %d ints %d inputs %d in_ints %d\n",
		outputs, iw_ints, inputs, iw_inints));
#endif
}

#define RAM_STEP	64*1024

static void
iw_mempoke(struct iw_softc *sc, u_long addy, u_char val)
{

	IW_WRITE_GENERAL_2(LMALI, (u_short) addy);
	IW_WRITE_GENERAL_1(LMAHI, (u_char) (addy >> 16));

	/* Write byte to LMBDR */
	IW_WRITE_DIRECT_1(sc->p3xr + 7, sc->p3xr_h, val);
}

static u_char
iw_mempeek(struct iw_softc *sc, u_long addy)
{
	u_char	ret;

	IW_WRITE_GENERAL_2(LMALI, (u_short) addy);
	IW_WRITE_GENERAL_1(LMAHI, (u_char) (addy >> 16));

	IW_READ_DIRECT_1(sc->p3xr + 7, sc->p3xr_h, ret);
	return ret;		/* return byte from LMBDR */
}

static void
iw_meminit(struct iw_softc *sc)
{
	u_long	bank[4] = {0L, 0L, 0L, 0L};
	u_long	addr, base, cnt;
	u_char	i, ram /* ,memval=0 */ ;
	u_short	lmcfi;
	u_long	temppi;
	u_long	*lpbanks;

	addr = 0L;
	base = 0L;
	cnt = 0L;
	ram = 0;
	lpbanks = &temppi;

	IW_WRITE_GENERAL_1(LDMACI, 0x00);

	IW_READ_GENERAL_2(LMCFI, lmcfi);	/* 0x52 */
	lmcfi |= 0x0A0C;
	IW_WRITE_GENERAL_2(LMCFI, lmcfi);	/* max addr span */
	IW_WRITE_GENERAL_1(LMCI, 0x00);

	/* fifo addresses */

	IW_WRITE_GENERAL_2(LMRFAI, ((4 * 1024 * 1024) >> 8));
	IW_WRITE_GENERAL_2(LMPFAI, ((4 * 1024 * 1024 + 16 * 1024) >> 8));

	IW_WRITE_GENERAL_2(LMFSI, 0x000);

	IW_WRITE_GENERAL_2(LDICI, 0x0000);

	while (addr < (16 * 1024 * 1024)) {
		iw_mempoke(sc, addr, 0x00);
		addr += RAM_STEP;
	}

	printf("%s:", device_xname(sc->sc_dev));

	for (i = 0; i < 4; i++) {
		iw_mempoke(sc, base, 0xAA);	/* mark start of bank */
		iw_mempoke(sc, base + 1L, 0x55);
		if (iw_mempeek(sc, base) == 0xAA  &&
		    iw_mempeek(sc, base + 1L) == 0x55)
			ram = 1;
		if (ram) {
			while (cnt < (4 * 1024 * 1024)) {
				bank[i] += RAM_STEP;
				cnt += RAM_STEP;
				addr = base + cnt;
				if (iw_mempeek(sc, addr) == 0xAA)
					break;
			}
		}
		if (lpbanks != NULL) {
			*lpbanks = bank[i];
			lpbanks++;
		}
		bank[i] = bank[i] >> 10;
		printf("%s bank[%d]: %ldK", i ? "," : "", i, bank[i]);
		base += 4 * 1024 * 1024;
		cnt = 0L;
		ram = 0;
	}

	printf("\n");

	/*
	 * this is not really useful since GUS PnP supports memory
	 * configurations that aren't really supported by Interwave...beware
	 * of holes! Also, we don't use the memory for anything in this
	 * version of the driver.
	 *
	 * we've configured for 4M-4M-4M-4M
	 */
}

static void
iwreset(struct iw_softc *sc, int warm)
{
	u_char	reg, cmode, val, mixer_image;

	val = 0;
	mixer_image = 0;
	reg = 0;		/* XXX gcc -Wall */

	cmode = 0x6c;		/* enhanced codec mode (full duplex) */

	/* reset */

	IW_WRITE_GENERAL_1(URSTI, 0x00);
	delay(10);
	IW_WRITE_GENERAL_1(URSTI, 0x07);
	IW_WRITE_GENERAL_1(ICMPTI, 0x1f);	/* disable DSP and uici and
						 * udci writes */
	IW_WRITE_GENERAL_1(IDECI, 0x7f);	/* enable ints to ISA and
						 * codec access */
	IW_READ_GENERAL_1(IVERI, reg);
	IW_WRITE_GENERAL_1(IVERI, reg | 0x01);	/* hidden reg lock disable */
	IW_WRITE_GENERAL_1(UASBCI, 0x00);

	/* synth enhanced mode (default), 0 active voices, disable ints */

	IW_WRITE_GENERAL_1(SGMI_WR, 0x01);	/* enhanced mode, LFOs
						 * disabled */
	for (val = 0; val < 32; val++) {
		/* set each synth sound volume to 0 */
		IW_WRITE_DIRECT_1(sc->p3xr + 2, sc->p3xr_h, val);
		IW_WRITE_GENERAL_1(SVSI_WR, 0x00);
		IW_WRITE_GENERAL_2(SASLI_WR, 0x0000);
		IW_WRITE_GENERAL_2(SASHI_WR, 0x0000);
		IW_WRITE_GENERAL_2(SAELI_WR, 0x0000);
		IW_WRITE_GENERAL_2(SAEHI_WR, 0x0000);
		IW_WRITE_GENERAL_2(SFCI_WR, 0x0000);
		IW_WRITE_GENERAL_1(SACI_WR, 0x02);
		IW_WRITE_GENERAL_1(SVSI_WR, 0x00);
		IW_WRITE_GENERAL_1(SVEI_WR, 0x00);
		IW_WRITE_GENERAL_2(SVLI_WR, 0x0000);
		IW_WRITE_GENERAL_1(SVCI_WR, 0x02);
		IW_WRITE_GENERAL_1(SMSI_WR, 0x02);
	}

	IW_WRITE_GENERAL_1(SAVI_WR, 0x00);

	/* codec mode/init */

	/* first change mode to 1 */

	IW_WRITE_CODEC_1(CMODEI, 0x00);

	/* and mode 3 */

	IW_WRITE_CODEC_1(CMODEI, cmode);

	IW_READ_CODEC_1(CMODEI, reg);

	DPRINTF(("cmode %x\n", reg));

	sc->revision = ((reg & 0x80) >> 3) | (reg & 0x0f);

	IW_WRITE_DIRECT_1(sc->codec_index + 2, sc->p2xr_h, 0x00);

	IW_WRITE_CODEC_1(CFIG1I | IW_MCE, 0x00);	/* DMA 2 chan access */
	IW_WRITE_CODEC_1(CEXTI, 0x00);	/* disable ints for now */


	IW_WRITE_CODEC_1(CLPCTI, 0x00);	/* reset playback sample counters */
	IW_WRITE_CODEC_1(CUPCTI, 0x00);	/* always upper byte last */
	IW_WRITE_CODEC_1(CFIG2I, 0x80);	/* full voltage range, enable record
					 * and playback sample counters, and
					 * don't center output in case or
					 * FIFO underrun */
	IW_WRITE_CODEC_1(CFIG3I, 0xc0);	/* enable record/playback irq (still
					 * turned off from CEXTI), max DMA
					 * rate */
	IW_WRITE_CODEC_1(CSR3I, 0x00);	/* clear status 3 reg */


	IW_WRITE_CODEC_1(CLRCTI, 0x00);	/* reset record sample counters */
	IW_WRITE_CODEC_1(CURCTI, 0x00);	/* always upper byte last */


	IW_READ_GENERAL_1(IVERI, reg);

	sc->vers = reg >> 4;
	if (!warm)
		snprintf(iw_device.version, sizeof(iw_device.version), "%d.%d",
		    sc->vers, sc->revision);

	IW_WRITE_GENERAL_1(IDECI, 0x7f);	/* irqs and codec decode
						 * enable */


	/* ports */

	if (!warm) {
		iw_mixer_line_level(sc, IW_LINE_OUT, 255, 255);
		iw_mixer_line_level(sc, IW_LINE_IN, 0, 0);
		iw_mixer_line_level(sc, IW_AUX1, 0, 0);
		iw_mixer_line_level(sc, IW_AUX2, 200, 200); /* CD */
		sc->sc_dac.off = 0;
		iw_mixer_line_level(sc, IW_DAC, 200, 200);

		iw_mixer_line_level(sc, IW_MIC_IN, 0, 0);
		iw_mixer_line_level(sc, IW_REC, 0, 0);
		iw_mixer_line_level(sc, IW_LOOPBACK, 0, 0);
		iw_mixer_line_level(sc, IW_MONO_IN, 0, 0);

		/* mem stuff */
		iw_meminit(sc);

	}
	IW_WRITE_CODEC_1(CEXTI, 0x02);	/* codec int enable */

	/* clear _LDMACI */

	IW_WRITE_GENERAL_1(LDMACI, 0x00);

	/* enable mixer paths */
	mixer_image = 0x0c;
	IW_WRITE_DIRECT_1(sc->p2xr, sc->p2xr_h, mixer_image);
	/*
	 * enable output, line in. disable mic in bit 0 = 0 -> line in on
	 * (from codec?) bit 1 = 0 -> output on bit 2 = 1 -> mic in on bit 3
	 * = 1 -> irq&drq pin enable bit 4 = 1 -> channel interrupts to chan
	 * 1 bit 5 = 1 -> enable midi loop back bit 6 = 0 -> irq latches
	 * URCR[2:0] bit 6 = 1 -> DMA latches URCR[2:0]
	 */


	IW_READ_DIRECT_1(sc->p2xr, sc->p2xr_h, mixer_image);
#ifdef AUDIO_DEBUG
	if (!warm)
		DPRINTF(("mix image %x \n", mixer_image));
#endif
}

struct iw_codec_freq {
	u_long	freq;
	u_char	bits;
};

int
iw_set_speed(struct iw_softc *sc, u_long freq, char in)
{
	u_char	var, cfig3, reg;

	static struct iw_codec_freq iw_cf[17] = {
#define FREQ_1 24576000
#define FREQ_2 16934400
#define XTAL1 0
#define XTAL2 1
		{5510, 0x00 | XTAL2}, {6620, 0x0E | XTAL2},
		{8000, 0x00 | XTAL1}, {9600, 0x0E | XTAL1},
		{11025, 0x02 | XTAL2}, {16000, 0x02 | XTAL1},
		{18900, 0x04 | XTAL2}, {22050, 0x06 | XTAL2},
		{27420, 0x04 | XTAL1}, {32000, 0x06 | XTAL1},
		{33075, 0x0C | XTAL2}, {37800, 0x08 | XTAL2},
		{38400, 0x0A | XTAL1}, {44100, 0x0A | XTAL2},
		{44800, 0x08 | XTAL1}, {48000, 0x0C | XTAL1},
		{48000, 0x0C | XTAL1}	/* really a dummy for indexing later */
#undef XTAL1
#undef XTAL2
	};

	cfig3 = 0;		/* XXX gcc -Wall */

	/*
	 * if the frequency is between 3493 Hz and 32 kHz we can use a more
	 * accurate frequency than the ones listed above base on the formula
	 * FREQ/((16*(48+x))) where FREQ is either FREQ_1 (24576000Hz) or
	 * FREQ_2 (16934400Hz) and x is the value to be written to either
	 * CPVFI or CRVFI. To enable this option, bit 2 in CFIG3 needs to be
	 * set high
	 *
	 * NOT IMPLEMENTED!
	 *
	 * Note that if you have a 'bad' XTAL_1 (higher than 18.5 MHz), 44.8 kHz
	 * and 38.4 kHz modes will provide wrong frequencies to output.
	 */


	if (freq > 48000)
		freq = 48000;
	if (freq < 5510)
		freq = 5510;

	/* reset CFIG3[2] */

	IW_READ_CODEC_1(CFIG3I, cfig3);

	cfig3 |= 0xc0;		/* not full fifo treshhold */

	DPRINTF(("cfig3i = %x -> ", cfig3));

	cfig3 &= ~0x04;
	IW_WRITE_CODEC_1(CFIG3I, cfig3);
	IW_READ_CODEC_1(CFIG3I, cfig3);

	DPRINTF(("%x\n", cfig3));

	for (var = 0; var < 16; var++)	/* select closest frequency */
		if (freq <= iw_cf[var].freq)
			break;
	if (var != 16)
		if (abs(freq - iw_cf[var].freq) > abs(iw_cf[var + 1].freq - freq))
			var++;

	if (in)
		IW_WRITE_CODEC_1(CRDFI | IW_MCE, sc->recfmtbits | iw_cf[var].bits);
	else
		IW_WRITE_CODEC_1(CPDFI | IW_MCE, sc->playfmtbits | iw_cf[var].bits);
	freq = iw_cf[var].freq;
	DPRINTF(("setting %s frequency to %d bits %x \n",
	       in ? "in" : "out", (int) freq, iw_cf[var].bits));

	IW_READ_CODEC_1(CPDFI, reg);

	DPRINTF((" CPDFI %x ", reg));

	IW_READ_CODEC_1(CRDFI, reg);

	DPRINTF((" CRDFI %x ", reg));
	__USE(reg);

	return freq;
}

int
iw_query_format(void *addr, audio_format_query_t *afp)
{

	return audio_query_format(iw_formats, IW_NFORMATS, afp);
}

static u_long
iw_set_format(struct iw_softc *sc, u_long precision, int in)
{
	u_char	data;
	int	encoding, channels;

	encoding = in ? sc->rec_encoding : sc->play_encoding;
	channels = in ? sc->rec_channels : sc->play_channels;

	DPRINTF(("iw_set_format\n"));

	switch (encoding) {
	case AUDIO_ENCODING_ULAW:
		data = 0x00;
		break;

	case AUDIO_ENCODING_ALAW:
		data = 0x00;
		break;

	case AUDIO_ENCODING_SLINEAR_LE:
		if (precision == 16)
			data = 0x40;	/* little endian. 0xc0 is big endian */
		else
			data = 0x00;
		break;

	case AUDIO_ENCODING_SLINEAR_BE:
		if (precision == 16)
			data = 0xc0;
		else
			data = 0x00;
		break;

	case AUDIO_ENCODING_ADPCM:
		data = 0xa0;
		break;

	default:
		return -1;
	}

	if (channels == 2)
		data |= 0x10;	/* stereo */

	if (in) {
		/* in */
		sc->recfmtbits = data;
		/* This will zero the normal codec frequency,
		 * iw_set_speed should always be called afterwards.
		 */
		IW_WRITE_CODEC_1(CRDFI | IW_MCE, data);
	} else {
		/* out */
		sc->playfmtbits = data;
		IW_WRITE_CODEC_1(CPDFI | IW_MCE, data);
	}

	DPRINTF(("formatbits %s %x", in ? "in" : "out", data));

	return encoding;
}

int
iw_audio_set_format(void *addr, int setmode,
	const audio_params_t *p, const audio_params_t *q,
	audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
{
	struct iw_softc *sc;

	DPRINTF(("%s: code %u, prec %u, rate %u, chan %u\n", __func__,
	    p->encoding, p->precision, p->sample_rate, p->channels));
	sc = addr;

	if (setmode & AUMODE_PLAY) {
		sc->play_channels = p->channels;
		sc->play_encoding = p->encoding;
		sc->play_precision = p->precision;
		iw_set_format(sc, p->precision, 0);
		sc->sc_orate = iw_set_speed(sc, p->sample_rate, 0);
	} else {
		sc->rec_channels = q->channels;
		sc->rec_encoding = q->encoding;
		sc->rec_precision = q->precision;
		/* XXX Is this 'p' a typo of 'q' ? */
		iw_set_format(sc, p->precision, 1);
		sc->sc_irate = iw_set_speed(sc, q->sample_rate, 1);
	}
	return 0;
}

void
iw_mixer_line_level(struct iw_softc *sc, int line, int levl, int levr)
{
	u_char	gainl, gainr, attenl, attenr;

	switch (line) {
	case IW_REC:
		gainl = sc->sc_recsrcbits | (levl >> 4);
		gainr = sc->sc_recsrcbits | (levr >> 4);
		DPRINTF(("recording with %x", gainl));
		IW_WRITE_CODEC_1(CLICI, gainl);
		IW_WRITE_CODEC_1(CRICI, gainr);
		sc->sc_rec.voll = levl & 0xf0;
		sc->sc_rec.volr = levr & 0xf0;
		break;

	case IW_AUX1:

		gainl = (255 - levl) >> 3;
		gainr = (255 - levr) >> 3;

		/* mute if 0 level */
		if (levl == 0)
			gainl |= 0x80;
		if (levr == 0)
			gainr |= 0x80;

		IW_WRITE_CODEC_1(IW_LEFT_AUX1_PORT, gainl);
		IW_WRITE_CODEC_1(IW_RIGHT_AUX1_PORT, gainr);
		sc->sc_aux1.voll = levl & 0xf8;
		sc->sc_aux1.volr = levr & 0xf8;

		break;

	case IW_AUX2:

		gainl = (255 - levl) >> 3;
		gainr = (255 - levr) >> 3;

		/* mute if 0 level */
		if (levl == 0)
			gainl |= 0x80;
		if (levr == 0)
			gainr |= 0x80;

		IW_WRITE_CODEC_1(IW_LEFT_AUX2_PORT, gainl);
		IW_WRITE_CODEC_1(IW_RIGHT_AUX2_PORT, gainr);
		sc->sc_aux2.voll = levl & 0xf8;
		sc->sc_aux2.volr = levr & 0xf8;
		break;
	case IW_DAC:
		attenl = ((255 - levl) >> 2) | ((levl && !sc->sc_dac.off) ? 0 : 0x80);
		attenr = ((255 - levr) >> 2) | ((levr && !sc->sc_dac.off) ? 0 : 0x80);
		IW_WRITE_CODEC_1(CLDACI, attenl);
		IW_WRITE_CODEC_1(CRDACI, attenr);
		sc->sc_dac.voll = levl & 0xfc;
		sc->sc_dac.volr = levr & 0xfc;
		break;
	case IW_LOOPBACK:
		attenl = ((255 - levl) & 0xfc) | (levl ? 0x01 : 0);
		IW_WRITE_CODEC_1(CLCI, attenl);
		sc->sc_loopback.voll = levl & 0xfc;
		break;
	case IW_LINE_IN:
		gainl = (levl >> 3) | (levl ? 0 : 0x80);
		gainr = (levr >> 3) | (levr ? 0 : 0x80);
		IW_WRITE_CODEC_1(CLLICI, gainl);
		IW_WRITE_CODEC_1(CRLICI, gainr);
		sc->sc_linein.voll = levl & 0xf8;
		sc->sc_linein.volr = levr & 0xf8;
		break;
	case IW_MIC_IN:
		gainl = ((255 - levl) >> 3) | (levl ? 0 : 0x80);
		gainr = ((255 - levr) >> 3) | (levr ? 0 : 0x80);
		IW_WRITE_CODEC_1(CLMICI, gainl);
		IW_WRITE_CODEC_1(CRMICI, gainr);
		sc->sc_mic.voll = levl & 0xf8;
		sc->sc_mic.volr = levr & 0xf8;
		break;
	case IW_LINE_OUT:
		attenl = ((255 - levl) >> 3) | (levl ? 0 : 0x80);
		attenr = ((255 - levr) >> 3) | (levr ? 0 : 0x80);
		IW_WRITE_CODEC_1(CLOAI, attenl);
		IW_WRITE_CODEC_1(CROAI, attenr);
		sc->sc_lineout.voll = levl & 0xf8;
		sc->sc_lineout.volr = levr & 0xf8;
		break;
	case IW_MONO_IN:
		attenl = ((255 - levl) >> 4) | (levl ? 0 : 0xc0);	/* in/out mute */
		IW_WRITE_CODEC_1(CMONOI, attenl);
		sc->sc_monoin.voll = levl & 0xf0;
		break;
	}
}

int
iw_commit_settings(void *addr)
{

	return 0;
}

void
iw_trigger_dma(struct iw_softc *sc, u_char io)
{
	u_char	reg;

	IW_READ_CODEC_1(CSR3I, reg);
	IW_WRITE_CODEC_1(CSR3I, reg & ~(io == IW_DMA_PLAYBACK ? 0x10 : 0x20));

	IW_READ_CODEC_1(CFIG1I, reg);

	IW_WRITE_CODEC_1(CFIG1I, reg | io);

	/* let the counter run */
	IW_READ_CODEC_1(CFIG2I, reg);
	IW_WRITE_CODEC_1(CFIG2I, reg & ~(io << 4));
}

void
iw_stop_dma(struct iw_softc *sc, u_char io, u_char hard)
{
	u_char	reg;

	/* just stop the counter, no need to flush the fifo */
	IW_READ_CODEC_1(CFIG2I, reg);
	IW_WRITE_CODEC_1(CFIG2I, (reg | (io << 4)));

	if (hard) {
		/* unless we're closing the device */
		IW_READ_CODEC_1(CFIG1I, reg);
		IW_WRITE_CODEC_1(CFIG1I, reg & ~io);
	}
}

void
iw_dma_count(struct iw_softc *sc, u_short count, int io)
{

	if (io == IW_DMA_PLAYBACK) {
		IW_WRITE_CODEC_1(CLPCTI, (u_char) (count & 0x00ff));
		IW_WRITE_CODEC_1(CUPCTI, (u_char) ((count >> 8) & 0x00ff));
	} else {
		IW_WRITE_CODEC_1(CLRCTI, (u_char) (count & 0x00ff));
		IW_WRITE_CODEC_1(CURCTI, (u_char) ((count >> 8) & 0x00ff));
	}
}

int
iw_init_output(void *addr, void *sbuf, int cc)
{
	struct iw_softc *sc = (struct iw_softc *) addr;

	DPRINTF(("iw_init_output\n"));

	isa_dmastart(sc->sc_ic, sc->sc_playdrq, sbuf,
		     cc, NULL, DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
	return 0;
}

int
iw_init_input(void *addr, void *sbuf, int cc)
{
	struct	iw_softc *sc;

	DPRINTF(("iw_init_input\n"));
	sc = (struct iw_softc *) addr;
	isa_dmastart(sc->sc_ic, sc->sc_recdrq, sbuf,
		     cc, NULL, DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
	return 0;
}


int
iw_start_output(void *addr, void *p, int cc, void (*intr)(void *), void *arg)
{
	struct	iw_softc *sc;

#ifdef DIAGNOSTIC
	if (!intr) {
		printf("iw_start_output: no callback!\n");
		return 1;
	}
#endif
	sc = addr;
	sc->sc_playintr = intr;
	sc->sc_playarg = arg;
	sc->sc_dma_flags |= DMAMODE_WRITE;
	sc->sc_playdma_bp = p;

	isa_dmastart(sc->sc_ic, sc->sc_playdrq, sc->sc_playdma_bp,
	    cc, NULL, DMAMODE_WRITE, BUS_DMA_NOWAIT);


	if (sc->play_encoding == AUDIO_ENCODING_ADPCM)
		cc >>= 2;
	if (sc->play_precision == 16)
		cc >>= 1;

	if (sc->play_channels == 2 && sc->play_encoding != AUDIO_ENCODING_ADPCM)
		cc >>= 1;

	cc -= iw_cc;

	/* iw_dma_access(sc,1); */
	if (cc != sc->sc_playdma_cnt) {
		iw_dma_count(sc, (u_short) cc, IW_DMA_PLAYBACK);
		sc->sc_playdma_cnt = cc;

		iw_trigger_dma(sc, IW_DMA_PLAYBACK);
	}

#ifdef DIAGNOSTIC
	if (outputs != iw_ints)
		printf("iw_start_output: out %d, int %d\n", outputs, iw_ints);
	outputs++;
#endif

	return 0;
}


int
iw_start_input(void *addr, void *p, int cc, void (*intr)(void *), void *arg)
{
	struct	iw_softc *sc;

#ifdef DIAGNOSTIC
	if (!intr) {
		printf("iw_start_input: no callback!\n");
		return 1;
	}
#endif
	sc = addr;
	sc->sc_recintr = intr;
	sc->sc_recarg = arg;
	sc->sc_dma_flags |= DMAMODE_READ;
	sc->sc_recdma_bp = p;

	isa_dmastart(sc->sc_ic, sc->sc_recdrq, sc->sc_recdma_bp,
	    cc, NULL, DMAMODE_READ, BUS_DMA_NOWAIT);


	if (sc->rec_encoding == AUDIO_ENCODING_ADPCM)
		cc >>= 2;
	if (sc->rec_precision == 16)
		cc >>= 1;

	if (sc->rec_channels == 2 && sc->rec_encoding != AUDIO_ENCODING_ADPCM)
		cc >>= 1;

	cc -= iw_cc;

	/* iw_dma_access(sc,0); */
	if (sc->sc_recdma_cnt != cc) {
		iw_dma_count(sc, (u_short) cc, IW_DMA_RECORD);
		sc->sc_recdma_cnt = cc;
		/* iw_dma_ctrl(sc, IW_DMA_RECORD); */
		iw_trigger_dma(sc, IW_DMA_RECORD);
	}

#ifdef DIAGNOSTIC
	if ((inputs != iw_inints))
		printf("iw_start_input: in %d, inints %d\n", inputs, iw_inints);
	inputs++;
#endif

	return 0;
}


int
iw_halt_output(void *addr)
{
	struct	iw_softc *sc;

	sc = addr;
	iw_stop_dma(sc, IW_DMA_PLAYBACK, 0);
	return 0;
}


int
iw_halt_input(void *addr)
{
	struct	iw_softc *sc;

	sc = addr;
	iw_stop_dma(sc, IW_DMA_RECORD, 0);
	return 0;
}

int
iw_speaker_ctl(void *addr, int newstate)
{
	struct iw_softc *sc;
	u_char reg;

	sc = addr;
	if (newstate == SPKR_ON) {
		sc->sc_dac.off = 0;
		IW_READ_CODEC_1(CLDACI, reg);
		IW_WRITE_CODEC_1(CLDACI, reg & 0x7f);
		IW_READ_CODEC_1(CRDACI, reg);
		IW_WRITE_CODEC_1(CRDACI, reg & 0x7f);
	} else {
		/* SPKR_OFF */
		sc->sc_dac.off = 1;
		IW_READ_CODEC_1(CLDACI, reg);
		IW_WRITE_CODEC_1(CLDACI, reg | 0x80);
		IW_READ_CODEC_1(CRDACI, reg);
		IW_WRITE_CODEC_1(CRDACI, reg | 0x80);
	}
	return 0;
}

int
iw_getdev(void *addr, struct audio_device *retp)
{

	*retp = iw_device;
	return 0;
}

/* Mixer (in/out ports) */
int
iw_set_port(void *addr, mixer_ctrl_t *cp)
{
	struct iw_softc *sc;
	u_char vall, valr;
	int error;

	sc = addr;
	vall = 0;
	valr = 0;
	error = EINVAL;
	switch (cp->dev) {
	case IW_MIC_IN_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels == 1) {
				vall = valr = cp->un.value.level[0];
			} else {
				vall = cp->un.value.level[0];
				valr = cp->un.value.level[1];
			}
			sc->sc_mic.voll = vall;
			sc->sc_mic.volr = valr;
			iw_mixer_line_level(sc, IW_MIC_IN, vall, valr);
		}
		break;
	case IW_AUX1_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels == 1) {
				vall = valr = cp->un.value.level[0];
			} else {
				vall = cp->un.value.level[0];
				valr = cp->un.value.level[1];
			}
			sc->sc_aux1.voll = vall;
			sc->sc_aux1.volr = valr;
			iw_mixer_line_level(sc, IW_AUX1, vall, valr);
		}
		break;
	case IW_AUX2_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels == 1) {
				vall = valr = cp->un.value.level[0];
			} else {
				vall = cp->un.value.level[0];
				valr = cp->un.value.level[1];
			}
			sc->sc_aux2.voll = vall;
			sc->sc_aux2.volr = valr;
			iw_mixer_line_level(sc, IW_AUX2, vall, valr);
		}
		break;
	case IW_LINE_IN_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels == 1) {
				vall = valr = cp->un.value.level[0];
			} else {
				vall = cp->un.value.level[0];
				valr = cp->un.value.level[1];
			}
			sc->sc_linein.voll = vall;
			sc->sc_linein.volr = valr;
			iw_mixer_line_level(sc, IW_LINE_IN, vall, valr);
		}
		break;
	case IW_LINE_OUT_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels == 1) {
				vall = valr = cp->un.value.level[0];
			} else {
				vall = cp->un.value.level[0];
				valr = cp->un.value.level[1];
			}
			sc->sc_lineout.voll = vall;
			sc->sc_lineout.volr = valr;
			iw_mixer_line_level(sc, IW_LINE_OUT, vall, valr);
		}
		break;
	case IW_REC_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels == 1) {
				vall = valr = cp->un.value.level[0];
			} else {
				vall = cp->un.value.level[0];
				valr = cp->un.value.level[1];
			}
			sc->sc_rec.voll = vall;
			sc->sc_rec.volr = valr;
			iw_mixer_line_level(sc, IW_REC, vall, valr);
		}
		break;

	case IW_DAC_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels == 1) {
				vall = valr = cp->un.value.level[0];
			} else {
				vall = cp->un.value.level[0];
				valr = cp->un.value.level[1];
			}
			sc->sc_dac.voll = vall;
			sc->sc_dac.volr = valr;
			iw_mixer_line_level(sc, IW_DAC, vall, valr);
		}
		break;

	case IW_LOOPBACK_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels != 1) {
				return EINVAL;
			} else {
				valr = vall = cp->un.value.level[0];
			}
			sc->sc_loopback.voll = vall;
			sc->sc_loopback.volr = valr;
			iw_mixer_line_level(sc, IW_LOOPBACK, vall, valr);
		}
		break;

	case IW_MONO_IN_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			error = 0;
			if (cp->un.value.num_channels != 1) {
				return EINVAL;
			} else {
				valr = vall = cp->un.value.level[0];
			}
			sc->sc_monoin.voll = vall;
			sc->sc_monoin.volr = valr;
			iw_mixer_line_level(sc, IW_MONO_IN, vall, valr);
		}
		break;
	case IW_RECORD_SOURCE:
		error = 0;
		sc->sc_recsrcbits = cp->un.ord << 6;
		DPRINTF(("record source %d bits %x\n", cp->un.ord,
		    sc->sc_recsrcbits));
		iw_mixer_line_level(sc, IW_REC, sc->sc_rec.voll,
		    sc->sc_rec.volr);
		break;
	}

	return error;
}


int
iw_get_port(void *addr, mixer_ctrl_t *cp)
{
	struct iw_softc *sc;
	int error;

	sc = addr;
	error = EINVAL;
	switch (cp->dev) {
	case IW_MIC_IN_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 2;
			cp->un.value.level[0] = sc->sc_mic.voll;
			cp->un.value.level[1] = sc->sc_mic.volr;
			error = 0;
		}
		break;
	case IW_AUX1_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 2;
			cp->un.value.level[0] = sc->sc_aux1.voll;
			cp->un.value.level[1] = sc->sc_aux1.volr;
			error = 0;
		}
		break;
	case IW_AUX2_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 2;
			cp->un.value.level[0] = sc->sc_aux2.voll;
			cp->un.value.level[1] = sc->sc_aux2.volr;
			error = 0;
		}
		break;
	case IW_LINE_OUT_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 2;
			cp->un.value.level[0] = sc->sc_lineout.voll;
			cp->un.value.level[1] = sc->sc_lineout.volr;
			error = 0;
		}
		break;
	case IW_LINE_IN_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 2;
			cp->un.value.level[0] = sc->sc_linein.voll;
			cp->un.value.level[1] = sc->sc_linein.volr;
			error = 0;
		}
		break;
	case IW_REC_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 2;
			cp->un.value.level[0] = sc->sc_rec.voll;
			cp->un.value.level[1] = sc->sc_rec.volr;
			error = 0;
		}
		break;

	case IW_DAC_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 2;
			cp->un.value.level[0] = sc->sc_dac.voll;
			cp->un.value.level[1] = sc->sc_dac.volr;
			error = 0;
		}
		break;

	case IW_LOOPBACK_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 1;
			cp->un.value.level[0] = sc->sc_loopback.voll;
			error = 0;
		}
		break;

	case IW_MONO_IN_LVL:
		if (cp->type == AUDIO_MIXER_VALUE) {
			cp->un.value.num_channels = 1;
			cp->un.value.level[0] = sc->sc_monoin.voll;
			error = 0;
		}
		break;
	case IW_RECORD_SOURCE:
		cp->un.ord = sc->sc_recsrcbits >> 6;
		error = 0;
		break;
	}

	return error;
}



int
iw_query_devinfo(void *addr, mixer_devinfo_t *dip)
{

	switch (dip->index) {
	case IW_MIC_IN_LVL:	/* Microphone */
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_INPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNmicrophone);
		dip->un.v.num_channels = 2;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;
	case IW_AUX1_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_INPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNline);
		dip->un.v.num_channels = 2;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;
	case IW_AUX2_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_INPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNcd);
		dip->un.v.num_channels = 2;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;
	case IW_LINE_OUT_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_OUTPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNline);
		dip->un.v.num_channels = 2;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;
	case IW_DAC_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_OUTPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNdac);
		dip->un.v.num_channels = 2;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;
	case IW_LINE_IN_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_INPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNinput);
		dip->un.v.num_channels = 2;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;
	case IW_MONO_IN_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_INPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNmono);
		dip->un.v.num_channels = 1;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;

	case IW_REC_LVL:	/* record level */
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_RECORD_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNrecord);
		dip->un.v.num_channels = 2;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;

	case IW_LOOPBACK_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = IW_RECORD_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, "filter");
		dip->un.v.num_channels = 1;
		strcpy(dip->un.v.units.name, AudioNvolume);
		break;

	case IW_RECORD_SOURCE:
		dip->mixer_class = IW_RECORD_CLASS;
		dip->type = AUDIO_MIXER_ENUM;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioNsource);
		dip->un.e.num_mem = 4;
		strcpy(dip->un.e.member[0].label.name, AudioNline);
		dip->un.e.member[0].ord = IW_LINE_IN_SRC;
		strcpy(dip->un.e.member[1].label.name, "aux1");
		dip->un.e.member[1].ord = IW_AUX1_SRC;
		strcpy(dip->un.e.member[2].label.name, AudioNmicrophone);
		dip->un.e.member[2].ord = IW_MIC_IN_SRC;
		strcpy(dip->un.e.member[3].label.name, AudioNmixerout);
		dip->un.e.member[3].ord = IW_MIX_OUT_SRC;
		break;
	case IW_INPUT_CLASS:
		dip->type = AUDIO_MIXER_CLASS;
		dip->mixer_class = IW_INPUT_CLASS;
		dip->next = dip->prev = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioCinputs);
		break;
	case IW_OUTPUT_CLASS:
		dip->type = AUDIO_MIXER_CLASS;
		dip->mixer_class = IW_OUTPUT_CLASS;
		dip->next = dip->prev = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioCoutputs);
		break;
	case IW_RECORD_CLASS:	/* record source class */
		dip->type = AUDIO_MIXER_CLASS;
		dip->mixer_class = IW_RECORD_CLASS;
		dip->next = dip->prev = AUDIO_MIXER_LAST;
		strcpy(dip->label.name, AudioCrecord);
		return 0;
	default:
		return ENXIO;
	}
	return 0;
}


void *
iw_malloc(void *addr, int direction, size_t size)
{
	struct iw_softc *sc;
	int drq;

	sc = addr;
	if (direction == AUMODE_PLAY)
		drq = sc->sc_playdrq;
	else
		drq = sc->sc_recdrq;
	return isa_malloc(sc->sc_ic, drq, size, M_DEVBUF, M_WAITOK);
}

void
iw_free(void *addr, void *ptr, size_t size)
{

	isa_free(ptr, M_DEVBUF);
}

size_t
iw_round_buffersize(void *addr, int direction, size_t size)
{
	struct iw_softc *sc;
	bus_size_t maxsize;

	sc = addr;
	if (direction == AUMODE_PLAY)
		maxsize = sc->sc_play_maxsize;
	else
		maxsize = sc->sc_rec_maxsize;

	if (size > maxsize)
		size = maxsize;
	return size;
}

int
iw_get_props(void *addr)
{
	struct iw_softc *sc;

	sc = addr;
	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
		(sc->sc_fullduplex ? AUDIO_PROP_FULLDUPLEX : 0);
}

void
iw_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
{
	struct iw_softc *sc;

	sc = addr;
	*intr = &sc->sc_intr_lock;
	*thread = &sc->sc_lock;
}