summaryrefslogtreecommitdiffstats
path: root/sound/oss/dmasound/dmasound_atari.c
blob: b747e77238f827c91afe389b4dd1fc1bca35db42 (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
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
/*
 *  linux/sound/oss/dmasound/dmasound_atari.c
 *
 *  Atari TT and Falcon DMA Sound Driver
 *
 *  See linux/sound/oss/dmasound/dmasound_core.c for copyright and credits
 *  prior to 28/01/2001
 *
 *  28/01/2001 [0.1] Iain Sandoe
 *		     - added versioning
 *		     - put in and populated the hardware_afmts field.
 *             [0.2] - put in SNDCTL_DSP_GETCAPS value.
 *  01/02/2001 [0.3] - put in default hard/soft settings.
 */


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/soundcard.h>
#include <linux/mm.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>

#include <asm/uaccess.h>
#include <asm/atariints.h>
#include <asm/atari_stram.h>

#include "dmasound.h"

#define DMASOUND_ATARI_REVISION 0
#define DMASOUND_ATARI_EDITION 3

extern void atari_microwire_cmd(int cmd);

static int is_falcon;
static int write_sq_ignore_int;	/* ++TeSche: used for Falcon */

static int expand_bal;	/* Balance factor for expanding (not volume!) */
static int expand_data;	/* Data for expanding */


/*** Translations ************************************************************/


/* ++TeSche: radically changed for new expanding purposes...
 *
 * These two routines now deal with copying/expanding/translating the samples
 * from user space into our buffer at the right frequency. They take care about
 * how much data there's actually to read, how much buffer space there is and
 * to convert samples into the right frequency/encoding. They will only work on
 * complete samples so it may happen they leave some bytes in the input stream
 * if the user didn't write a multiple of the current sample size. They both
 * return the number of bytes they've used from both streams so you may detect
 * such a situation. Luckily all programs should be able to cope with that.
 *
 * I think I've optimized anything as far as one can do in plain C, all
 * variables should fit in registers and the loops are really short. There's
 * one loop for every possible situation. Writing a more generalized and thus
 * parameterized loop would only produce slower code. Feel free to optimize
 * this in assembler if you like. :)
 *
 * I think these routines belong here because they're not yet really hardware
 * independent, especially the fact that the Falcon can play 16bit samples
 * only in stereo is hardcoded in both of them!
 *
 * ++geert: split in even more functions (one per format)
 */

static ssize_t ata_ct_law(const u_char *userPtr, size_t userCount,
			  u_char frame[], ssize_t *frameUsed,
			  ssize_t frameLeft);
static ssize_t ata_ct_s8(const u_char *userPtr, size_t userCount,
			 u_char frame[], ssize_t *frameUsed,
			 ssize_t frameLeft);
static ssize_t ata_ct_u8(const u_char *userPtr, size_t userCount,
			 u_char frame[], ssize_t *frameUsed,
			 ssize_t frameLeft);
static ssize_t ata_ct_s16be(const u_char *userPtr, size_t userCount,
			    u_char frame[], ssize_t *frameUsed,
			    ssize_t frameLeft);
static ssize_t ata_ct_u16be(const u_char *userPtr, size_t userCount,
			    u_char frame[], ssize_t *frameUsed,
			    ssize_t frameLeft);
static ssize_t ata_ct_s16le(const u_char *userPtr, size_t userCount,
			    u_char frame[], ssize_t *frameUsed,
			    ssize_t frameLeft);
static ssize_t ata_ct_u16le(const u_char *userPtr, size_t userCount,
			    u_char frame[], ssize_t *frameUsed,
			    ssize_t frameLeft);
static ssize_t ata_ctx_law(const u_char *userPtr, size_t userCount,
			   u_char frame[], ssize_t *frameUsed,
			   ssize_t frameLeft);
static ssize_t ata_ctx_s8(const u_char *userPtr, size_t userCount,
			  u_char frame[], ssize_t *frameUsed,
			  ssize_t frameLeft);
static ssize_t ata_ctx_u8(const u_char *userPtr, size_t userCount,
			  u_char frame[], ssize_t *frameUsed,
			  ssize_t frameLeft);
static ssize_t ata_ctx_s16be(const u_char *userPtr, size_t userCount,
			     u_char frame[], ssize_t *frameUsed,
			     ssize_t frameLeft);
static ssize_t ata_ctx_u16be(const u_char *userPtr, size_t userCount,
			     u_char frame[], ssize_t *frameUsed,
			     ssize_t frameLeft);
static ssize_t ata_ctx_s16le(const u_char *userPtr, size_t userCount,
			     u_char frame[], ssize_t *frameUsed,
			     ssize_t frameLeft);
static ssize_t ata_ctx_u16le(const u_char *userPtr, size_t userCount,
			     u_char frame[], ssize_t *frameUsed,
			     ssize_t frameLeft);


/*** Low level stuff *********************************************************/


static void *AtaAlloc(unsigned int size, gfp_t flags);
static void AtaFree(void *, unsigned int size);
static int AtaIrqInit(void);
#ifdef MODULE
static void AtaIrqCleanUp(void);
#endif /* MODULE */
static int AtaSetBass(int bass);
static int AtaSetTreble(int treble);
static void TTSilence(void);
static void TTInit(void);
static int TTSetFormat(int format);
static int TTSetVolume(int volume);
static int TTSetGain(int gain);
static void FalconSilence(void);
static void FalconInit(void);
static int FalconSetFormat(int format);
static int FalconSetVolume(int volume);
static void AtaPlayNextFrame(int index);
static void AtaPlay(void);
static irqreturn_t AtaInterrupt(int irq, void *dummy, struct pt_regs *fp);

/*** Mid level stuff *********************************************************/

static void TTMixerInit(void);
static void FalconMixerInit(void);
static int AtaMixerIoctl(u_int cmd, u_long arg);
static int TTMixerIoctl(u_int cmd, u_long arg);
static int FalconMixerIoctl(u_int cmd, u_long arg);
static int AtaWriteSqSetup(void);
static int AtaSqOpen(mode_t mode);
static int TTStateInfo(char *buffer, size_t space);
static int FalconStateInfo(char *buffer, size_t space);


/*** Translations ************************************************************/


static ssize_t ata_ct_law(const u_char *userPtr, size_t userCount,
			  u_char frame[], ssize_t *frameUsed,
			  ssize_t frameLeft)
{
	char *table = dmasound.soft.format == AFMT_MU_LAW ? dmasound_ulaw2dma8
							  : dmasound_alaw2dma8;
	ssize_t count, used;
	u_char *p = &frame[*frameUsed];

	count = min_t(unsigned long, userCount, frameLeft);
	if (dmasound.soft.stereo)
		count &= ~1;
	used = count;
	while (count > 0) {
		u_char data;
		if (get_user(data, userPtr++))
			return -EFAULT;
		*p++ = table[data];
		count--;
	}
	*frameUsed += used;
	return used;
}


static ssize_t ata_ct_s8(const u_char *userPtr, size_t userCount,
			 u_char frame[], ssize_t *frameUsed,
			 ssize_t frameLeft)
{
	ssize_t count, used;
	void *p = &frame[*frameUsed];

	count = min_t(unsigned long, userCount, frameLeft);
	if (dmasound.soft.stereo)
		count &= ~1;
	used = count;
	if (copy_from_user(p, userPtr, count))
		return -EFAULT;
	*frameUsed += used;
	return used;
}


static ssize_t ata_ct_u8(const u_char *userPtr, size_t userCount,
			 u_char frame[], ssize_t *frameUsed,
			 ssize_t frameLeft)
{
	ssize_t count, used;

	if (!dmasound.soft.stereo) {
		u_char *p = &frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft);
		used = count;
		while (count > 0) {
			u_char data;
			if (get_user(data, userPtr++))
				return -EFAULT;
			*p++ = data ^ 0x80;
			count--;
		}
	} else {
		u_short *p = (u_short *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft)>>1;
		used = count*2;
		while (count > 0) {
			u_short data;
			if (get_user(data, (u_short *)userPtr))
				return -EFAULT;
			userPtr += 2;
			*p++ = data ^ 0x8080;
			count--;
		}
	}
	*frameUsed += used;
	return used;
}


static ssize_t ata_ct_s16be(const u_char *userPtr, size_t userCount,
			    u_char frame[], ssize_t *frameUsed,
			    ssize_t frameLeft)
{
	ssize_t count, used;

	if (!dmasound.soft.stereo) {
		u_short *p = (u_short *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft)>>1;
		used = count*2;
		while (count > 0) {
			u_short data;
			if (get_user(data, (u_short *)userPtr))
				return -EFAULT;
			userPtr += 2;
			*p++ = data;
			*p++ = data;
			count--;
		}
		*frameUsed += used*2;
	} else {
		void *p = (u_short *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft) & ~3;
		used = count;
		if (copy_from_user(p, userPtr, count))
			return -EFAULT;
		*frameUsed += used;
	}
	return used;
}


static ssize_t ata_ct_u16be(const u_char *userPtr, size_t userCount,
			    u_char frame[], ssize_t *frameUsed,
			    ssize_t frameLeft)
{
	ssize_t count, used;

	if (!dmasound.soft.stereo) {
		u_short *p = (u_short *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft)>>1;
		used = count*2;
		while (count > 0) {
			u_short data;
			if (get_user(data, (u_short *)userPtr))
				return -EFAULT;
			userPtr += 2;
			data ^= 0x8000;
			*p++ = data;
			*p++ = data;
			count--;
		}
		*frameUsed += used*2;
	} else {
		u_long *p = (u_long *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft)>>2;
		used = count*4;
		while (count > 0) {
			u_long data;
			if (get_user(data, (u_int *)userPtr))
				return -EFAULT;
			userPtr += 4;
			*p++ = data ^ 0x80008000;
			count--;
		}
		*frameUsed += used;
	}
	return used;
}


static ssize_t ata_ct_s16le(const u_char *userPtr, size_t userCount,
			    u_char frame[], ssize_t *frameUsed,
			    ssize_t frameLeft)
{
	ssize_t count, used;

	count = frameLeft;
	if (!dmasound.soft.stereo) {
		u_short *p = (u_short *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft)>>1;
		used = count*2;
		while (count > 0) {
			u_short data;
			if (get_user(data, (u_short *)userPtr))
				return -EFAULT;
			userPtr += 2;
			data = le2be16(data);
			*p++ = data;
			*p++ = data;
			count--;
		}
		*frameUsed += used*2;
	} else {
		u_long *p = (u_long *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft)>>2;
		used = count*4;
		while (count > 0) {
			u_long data;
			if (get_user(data, (u_int *)userPtr))
				return -EFAULT;
			userPtr += 4;
			data = le2be16dbl(data);
			*p++ = data;
			count--;
		}
		*frameUsed += used;
	}
	return used;
}


static ssize_t ata_ct_u16le(const u_char *userPtr, size_t userCount,
			    u_char frame[], ssize_t *frameUsed,
			    ssize_t frameLeft)
{
	ssize_t count, used;

	count = frameLeft;
	if (!dmasound.soft.stereo) {
		u_short *p = (u_short *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft)>>1;
		used = count*2;
		while (count > 0) {
			u_short data;
			if (get_user(data, (u_short *)userPtr))
				return -EFAULT;
			userPtr += 2;
			data = le2be16(data) ^ 0x8000;
			*p++ = data;
			*p++ = data;
		}
		*frameUsed += used*2;
	} else {
		u_long *p = (u_long *)&frame[*frameUsed];
		count = min_t(unsigned long, userCount, frameLeft)>>2;
		used = count;
		while (count > 0) {
			u_long data;
			if (get_user(data, (u_int *)userPtr))
				return -EFAULT;
			userPtr += 4;
			data = le2be16dbl(data) ^ 0x80008000;
			*p++ = data;
			count--;
		}
		*frameUsed += used;
	}
	return used;
}


static ssize_t ata_ctx_law(const u_char *userPtr, size_t userCount,
			   u_char frame[], ssize_t *frameUsed,
			   ssize_t frameLeft)
{
	char *table = dmasound.soft.format == AFMT_MU_LAW ? dmasound_ulaw2dma8
							  : dmasound_alaw2dma8;
	/* this should help gcc to stuff everything into registers */
	long bal = expand_bal;
	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
	ssize_t used, usedf;

	used = userCount;
	usedf = frameLeft;
	if (!dmasound.soft.stereo) {
		u_char *p = &frame[*frameUsed];
		u_char data = expand_data;
		while (frameLeft) {
			u_char c;
			if (bal < 0) {
				if (!userCount)
					break;
				if (get_user(c, userPtr++))
					return -EFAULT;
				data = table[c];
				userCount--;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft--;
			bal -= sSpeed;
		}
		expand_data = data;
	} else {
		u_short *p = (u_short *)&frame[*frameUsed];
		u_short data = expand_data;
		while (frameLeft >= 2) {
			u_char c;
			if (bal < 0) {
				if (userCount < 2)
					break;
				if (get_user(c, userPtr++))
					return -EFAULT;
				data = table[c] << 8;
				if (get_user(c, userPtr++))
					return -EFAULT;
				data |= table[c];
				userCount -= 2;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft -= 2;
			bal -= sSpeed;
		}
		expand_data = data;
	}
	expand_bal = bal;
	used -= userCount;
	*frameUsed += usedf-frameLeft;
	return used;
}


static ssize_t ata_ctx_s8(const u_char *userPtr, size_t userCount,
			  u_char frame[], ssize_t *frameUsed,
			  ssize_t frameLeft)
{
	/* this should help gcc to stuff everything into registers */
	long bal = expand_bal;
	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
	ssize_t used, usedf;

	used = userCount;
	usedf = frameLeft;
	if (!dmasound.soft.stereo) {
		u_char *p = &frame[*frameUsed];
		u_char data = expand_data;
		while (frameLeft) {
			if (bal < 0) {
				if (!userCount)
					break;
				if (get_user(data, userPtr++))
					return -EFAULT;
				userCount--;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft--;
			bal -= sSpeed;
		}
		expand_data = data;
	} else {
		u_short *p = (u_short *)&frame[*frameUsed];
		u_short data = expand_data;
		while (frameLeft >= 2) {
			if (bal < 0) {
				if (userCount < 2)
					break;
				if (get_user(data, (u_short *)userPtr))
					return -EFAULT;
				userPtr += 2;
				userCount -= 2;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft -= 2;
			bal -= sSpeed;
		}
		expand_data = data;
	}
	expand_bal = bal;
	used -= userCount;
	*frameUsed += usedf-frameLeft;
	return used;
}


static ssize_t ata_ctx_u8(const u_char *userPtr, size_t userCount,
			  u_char frame[], ssize_t *frameUsed,
			  ssize_t frameLeft)
{
	/* this should help gcc to stuff everything into registers */
	long bal = expand_bal;
	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
	ssize_t used, usedf;

	used = userCount;
	usedf = frameLeft;
	if (!dmasound.soft.stereo) {
		u_char *p = &frame[*frameUsed];
		u_char data = expand_data;
		while (frameLeft) {
			if (bal < 0) {
				if (!userCount)
					break;
				if (get_user(data, userPtr++))
					return -EFAULT;
				data ^= 0x80;
				userCount--;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft--;
			bal -= sSpeed;
		}
		expand_data = data;
	} else {
		u_short *p = (u_short *)&frame[*frameUsed];
		u_short data = expand_data;
		while (frameLeft >= 2) {
			if (bal < 0) {
				if (userCount < 2)
					break;
				if (get_user(data, (u_short *)userPtr))
					return -EFAULT;
				userPtr += 2;
				data ^= 0x8080;
				userCount -= 2;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft -= 2;
			bal -= sSpeed;
		}
		expand_data = data;
	}
	expand_bal = bal;
	used -= userCount;
	*frameUsed += usedf-frameLeft;
	return used;
}


static ssize_t ata_ctx_s16be(const u_char *userPtr, size_t userCount,
			     u_char frame[], ssize_t *frameUsed,
			     ssize_t frameLeft)
{
	/* this should help gcc to stuff everything into registers */
	long bal = expand_bal;
	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
	ssize_t used, usedf;

	used = userCount;
	usedf = frameLeft;
	if (!dmasound.soft.stereo) {
		u_short *p = (u_short *)&frame[*frameUsed];
		u_short data = expand_data;
		while (frameLeft >= 4) {
			if (bal < 0) {
				if (userCount < 2)
					break;
				if (get_user(data, (u_short *)userPtr))
					return -EFAULT;
				userPtr += 2;
				userCount -= 2;
				bal += hSpeed;
			}
			*p++ = data;
			*p++ = data;
			frameLeft -= 4;
			bal -= sSpeed;
		}
		expand_data = data;
	} else {
		u_long *p = (u_long *)&frame[*frameUsed];
		u_long data = expand_data;
		while (frameLeft >= 4) {
			if (bal < 0) {
				if (userCount < 4)
					break;
				if (get_user(data, (u_int *)userPtr))
					return -EFAULT;
				userPtr += 4;
				userCount -= 4;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft -= 4;
			bal -= sSpeed;
		}
		expand_data = data;
	}
	expand_bal = bal;
	used -= userCount;
	*frameUsed += usedf-frameLeft;
	return used;
}


static ssize_t ata_ctx_u16be(const u_char *userPtr, size_t userCount,
			     u_char frame[], ssize_t *frameUsed,
			     ssize_t frameLeft)
{
	/* this should help gcc to stuff everything into registers */
	long bal = expand_bal;
	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
	ssize_t used, usedf;

	used = userCount;
	usedf = frameLeft;
	if (!dmasound.soft.stereo) {
		u_short *p = (u_short *)&frame[*frameUsed];
		u_short data = expand_data;
		while (frameLeft >= 4) {
			if (bal < 0) {
				if (userCount < 2)
					break;
				if (get_user(data, (u_short *)userPtr))
					return -EFAULT;
				userPtr += 2;
				data ^= 0x8000;
				userCount -= 2;
				bal += hSpeed;
			}
			*p++ = data;
			*p++ = data;
			frameLeft -= 4;
			bal -= sSpeed;
		}
		expand_data = data;
	} else {
		u_long *p = (u_long *)&frame[*frameUsed];
		u_long data = expand_data;
		while (frameLeft >= 4) {
			if (bal < 0) {
				if (userCount < 4)
					break;
				if (get_user(data, (u_int *)userPtr))
					return -EFAULT;
				userPtr += 4;
				data ^= 0x80008000;
				userCount -= 4;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft -= 4;
			bal -= sSpeed;
		}
		expand_data = data;
	}
	expand_bal = bal;
	used -= userCount;
	*frameUsed += usedf-frameLeft;
	return used;
}


static ssize_t ata_ctx_s16le(const u_char *userPtr, size_t userCount,
			     u_char frame[], ssize_t *frameUsed,
			     ssize_t frameLeft)
{
	/* this should help gcc to stuff everything into registers */
	long bal = expand_bal;
	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
	ssize_t used, usedf;

	used = userCount;
	usedf = frameLeft;
	if (!dmasound.soft.stereo) {
		u_short *p = (u_short *)&frame[*frameUsed];
		u_short data = expand_data;
		while (frameLeft >= 4) {
			if (bal < 0) {
				if (userCount < 2)
					break;
				if (get_user(data, (u_short *)userPtr))
					return -EFAULT;
				userPtr += 2;
				data = le2be16(data);
				userCount -= 2;
				bal += hSpeed;
			}
			*p++ = data;
			*p++ = data;
			frameLeft -= 4;
			bal -= sSpeed;
		}
		expand_data = data;
	} else {
		u_long *p = (u_long *)&frame[*frameUsed];
		u_long data = expand_data;
		while (frameLeft >= 4) {
			if (bal < 0) {
				if (userCount < 4)
					break;
				if (get_user(data, (u_int *)userPtr))
					return -EFAULT;
				userPtr += 4;
				data = le2be16dbl(data);
				userCount -= 4;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft -= 4;
			bal -= sSpeed;
		}
		expand_data = data;
	}
	expand_bal = bal;
	used -= userCount;
	*frameUsed += usedf-frameLeft;
	return used;
}


static ssize_t ata_ctx_u16le(const u_char *userPtr, size_t userCount,
			     u_char frame[], ssize_t *frameUsed,
			     ssize_t frameLeft)
{
	/* this should help gcc to stuff everything into registers */
	long bal = expand_bal;
	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
	ssize_t used, usedf;

	used = userCount;
	usedf = frameLeft;
	if (!dmasound.soft.stereo) {
		u_short *p = (u_short *)&frame[*frameUsed];
		u_short data = expand_data;
		while (frameLeft >= 4) {
			if (bal < 0) {
				if (userCount < 2)
					break;
				if (get_user(data, (u_short *)userPtr))
					return -EFAULT;
				userPtr += 2;
				data = le2be16(data) ^ 0x8000;
				userCount -= 2;
				bal += hSpeed;
			}
			*p++ = data;
			*p++ = data;
			frameLeft -= 4;
			bal -= sSpeed;
		}
		expand_data = data;
	} else {
		u_long *p = (u_long *)&frame[*frameUsed];
		u_long data = expand_data;
		while (frameLeft >= 4) {
			if (bal < 0) {
				if (userCount < 4)
					break;
				if (get_user(data, (u_int *)userPtr))
					return -EFAULT;
				userPtr += 4;
				data = le2be16dbl(data) ^ 0x80008000;
				userCount -= 4;
				bal += hSpeed;
			}
			*p++ = data;
			frameLeft -= 4;
			bal -= sSpeed;
		}
		expand_data = data;
	}
	expand_bal = bal;
	used -= userCount;
	*frameUsed += usedf-frameLeft;
	return used;
}


static TRANS transTTNormal = {
	.ct_ulaw	= ata_ct_law,
	.ct_alaw	= ata_ct_law,
	.ct_s8		= ata_ct_s8,
	.ct_u8		= ata_ct_u8,
};

static TRANS transTTExpanding = {
	.ct_ulaw	= ata_ctx_law,
	.ct_alaw	= ata_ctx_law,
	.ct_s8		= ata_ctx_s8,
	.ct_u8		= ata_ctx_u8,
};

static TRANS transFalconNormal = {
	.ct_ulaw	= ata_ct_law,
	.ct_alaw	= ata_ct_law,
	.ct_s8		= ata_ct_s8,
	.ct_u8		= ata_ct_u8,
	.ct_s16be	= ata_ct_s16be,
	.ct_u16be	= ata_ct_u16be,
	.ct_s16le	= ata_ct_s16le,
	.ct_u16le	= ata_ct_u16le
};

static TRANS transFalconExpanding = {
	.ct_ulaw	= ata_ctx_law,
	.ct_alaw	= ata_ctx_law,
	.ct_s8		= ata_ctx_s8,
	.ct_u8		= ata_ctx_u8,
	.ct_s16be	= ata_ctx_s16be,
	.ct_u16be	= ata_ctx_u16be,
	.ct_s16le	= ata_ctx_s16le,
	.ct_u16le	= ata_ctx_u16le,
};


/*** Low level stuff *********************************************************/



/*
 * Atari (TT/Falcon)
 */

static void *AtaAlloc(unsigned int size, gfp_t flags)
{
	return atari_stram_alloc(size, "dmasound");
}

static void AtaFree(void *obj, unsigned int size)
{
	atari_stram_free( obj );
}

static int __init AtaIrqInit(void)
{
	/* Set up timer A. Timer A
	   will receive a signal upon end of playing from the sound
	   hardware. Furthermore Timer A is able to count events
	   and will cause an interrupt after a programmed number
	   of events. So all we need to keep the music playing is
	   to provide the sound hardware with new data upon
	   an interrupt from timer A. */
	mfp.tim_ct_a = 0;	/* ++roman: Stop timer before programming! */
	mfp.tim_dt_a = 1;	/* Cause interrupt after first event. */
	mfp.tim_ct_a = 8;	/* Turn on event counting. */
	/* Register interrupt handler. */
	request_irq(IRQ_MFP_TIMA, AtaInterrupt, IRQ_TYPE_SLOW, "DMA sound",
		    AtaInterrupt);
	mfp.int_en_a |= 0x20;	/* Turn interrupt on. */
	mfp.int_mk_a |= 0x20;
	return 1;
}

#ifdef MODULE
static void AtaIrqCleanUp(void)
{
	mfp.tim_ct_a = 0;	/* stop timer */
	mfp.int_en_a &= ~0x20;	/* turn interrupt off */
	free_irq(IRQ_MFP_TIMA, AtaInterrupt);
}
#endif /* MODULE */


#define TONE_VOXWARE_TO_DB(v) \
	(((v) < 0) ? -12 : ((v) > 100) ? 12 : ((v) - 50) * 6 / 25)
#define TONE_DB_TO_VOXWARE(v) (((v) * 25 + ((v) > 0 ? 5 : -5)) / 6 + 50)


static int AtaSetBass(int bass)
{
	dmasound.bass = TONE_VOXWARE_TO_DB(bass);
	atari_microwire_cmd(MW_LM1992_BASS(dmasound.bass));
	return TONE_DB_TO_VOXWARE(dmasound.bass);
}


static int AtaSetTreble(int treble)
{
	dmasound.treble = TONE_VOXWARE_TO_DB(treble);
	atari_microwire_cmd(MW_LM1992_TREBLE(dmasound.treble));
	return TONE_DB_TO_VOXWARE(dmasound.treble);
}



/*
 * TT
 */


static void TTSilence(void)
{
	tt_dmasnd.ctrl = DMASND_CTRL_OFF;
	atari_microwire_cmd(MW_LM1992_PSG_HIGH); /* mix in PSG signal 1:1 */
}


static void TTInit(void)
{
	int mode, i, idx;
	const int freq[4] = {50066, 25033, 12517, 6258};

	/* search a frequency that fits into the allowed error range */

	idx = -1;
	for (i = 0; i < ARRAY_SIZE(freq); i++)
		/* this isn't as much useful for a TT than for a Falcon, but
		 * then it doesn't hurt very much to implement it for a TT too.
		 */
		if ((100 * abs(dmasound.soft.speed - freq[i]) / freq[i]) < catchRadius)
			idx = i;
	if (idx > -1) {
		dmasound.soft.speed = freq[idx];
		dmasound.trans_write = &transTTNormal;
	} else
		dmasound.trans_write = &transTTExpanding;

	TTSilence();
	dmasound.hard = dmasound.soft;

	if (dmasound.hard.speed > 50066) {
		/* we would need to squeeze the sound, but we won't do that */
		dmasound.hard.speed = 50066;
		mode = DMASND_MODE_50KHZ;
		dmasound.trans_write = &transTTNormal;
	} else if (dmasound.hard.speed > 25033) {
		dmasound.hard.speed = 50066;
		mode = DMASND_MODE_50KHZ;
	} else if (dmasound.hard.speed > 12517) {
		dmasound.hard.speed = 25033;
		mode = DMASND_MODE_25KHZ;
	} else if (dmasound.hard.speed > 6258) {
		dmasound.hard.speed = 12517;
		mode = DMASND_MODE_12KHZ;
	} else {
		dmasound.hard.speed = 6258;
		mode = DMASND_MODE_6KHZ;
	}

	tt_dmasnd.mode = (dmasound.hard.stereo ?
			  DMASND_MODE_STEREO : DMASND_MODE_MONO) |
		DMASND_MODE_8BIT | mode;

	expand_bal = -dmasound.soft.speed;
}


static int TTSetFormat(int format)
{
	/* TT sound DMA supports only 8bit modes */

	switch (format) {
	case AFMT_QUERY:
		return dmasound.soft.format;
	case AFMT_MU_LAW:
	case AFMT_A_LAW:
	case AFMT_S8:
	case AFMT_U8:
		break;
	default:
		format = AFMT_S8;
	}

	dmasound.soft.format = format;
	dmasound.soft.size = 8;
	if (dmasound.minDev == SND_DEV_DSP) {
		dmasound.dsp.format = format;
		dmasound.dsp.size = 8;
	}
	TTInit();

	return format;
}


#define VOLUME_VOXWARE_TO_DB(v) \
	(((v) < 0) ? -40 : ((v) > 100) ? 0 : ((v) * 2) / 5 - 40)
#define VOLUME_DB_TO_VOXWARE(v) ((((v) + 40) * 5 + 1) / 2)


static int TTSetVolume(int volume)
{
	dmasound.volume_left = VOLUME_VOXWARE_TO_DB(volume & 0xff);
	atari_microwire_cmd(MW_LM1992_BALLEFT(dmasound.volume_left));
	dmasound.volume_right = VOLUME_VOXWARE_TO_DB((volume & 0xff00) >> 8);
	atari_microwire_cmd(MW_LM1992_BALRIGHT(dmasound.volume_right));
	return VOLUME_DB_TO_VOXWARE(dmasound.volume_left) |
	       (VOLUME_DB_TO_VOXWARE(dmasound.volume_right) << 8);
}


#define GAIN_VOXWARE_TO_DB(v) \
	(((v) < 0) ? -80 : ((v) > 100) ? 0 : ((v) * 4) / 5 - 80)
#define GAIN_DB_TO_VOXWARE(v) ((((v) + 80) * 5 + 1) / 4)

static int TTSetGain(int gain)
{
	dmasound.gain = GAIN_VOXWARE_TO_DB(gain);
	atari_microwire_cmd(MW_LM1992_VOLUME(dmasound.gain));
	return GAIN_DB_TO_VOXWARE(dmasound.gain);
}



/*
 * Falcon
 */


static void FalconSilence(void)
{
	/* stop playback, set sample rate 50kHz for PSG sound */
	tt_dmasnd.ctrl = DMASND_CTRL_OFF;
	tt_dmasnd.mode = DMASND_MODE_50KHZ | DMASND_MODE_STEREO | DMASND_MODE_8BIT;
	tt_dmasnd.int_div = 0; /* STE compatible divider */
	tt_dmasnd.int_ctrl = 0x0;
	tt_dmasnd.cbar_src = 0x0000; /* no matrix inputs */
	tt_dmasnd.cbar_dst = 0x0000; /* no matrix outputs */
	tt_dmasnd.dac_src = 1; /* connect ADC to DAC, disconnect matrix */
	tt_dmasnd.adc_src = 3; /* ADC Input = PSG */
}


static void FalconInit(void)
{
	int divider, i, idx;
	const int freq[8] = {49170, 32780, 24585, 19668, 16390, 12292, 9834, 8195};

	/* search a frequency that fits into the allowed error range */

	idx = -1;
	for (i = 0; i < ARRAY_SIZE(freq); i++)
		/* if we will tolerate 3% error 8000Hz->8195Hz (2.38%) would
		 * be playable without expanding, but that now a kernel runtime
		 * option
		 */
		if ((100 * abs(dmasound.soft.speed - freq[i]) / freq[i]) < catchRadius)
			idx = i;
	if (idx > -1) {
		dmasound.soft.speed = freq[idx];
		dmasound.trans_write = &transFalconNormal;
	} else
		dmasound.trans_write = &transFalconExpanding;

	FalconSilence();
	dmasound.hard = dmasound.soft;

	if (dmasound.hard.size == 16) {
		/* the Falcon can play 16bit samples only in stereo */
		dmasound.hard.stereo = 1;
	}

	if (dmasound.hard.speed > 49170) {
		/* we would need to squeeze the sound, but we won't do that */
		dmasound.hard.speed = 49170;
		divider = 1;
		dmasound.trans_write = &transFalconNormal;
	} else if (dmasound.hard.speed > 32780) {
		dmasound.hard.speed = 49170;
		divider = 1;
	} else if (dmasound.hard.speed > 24585) {
		dmasound.hard.speed = 32780;
		divider = 2;
	} else if (dmasound.hard.speed > 19668) {
		dmasound.hard.speed = 24585;
		divider = 3;
	} else if (dmasound.hard.speed > 16390) {
		dmasound.hard.speed = 19668;
		divider = 4;
	} else if (dmasound.hard.speed > 12292) {
		dmasound.hard.speed = 16390;
		divider = 5;
	} else if (dmasound.hard.speed > 9834) {
		dmasound.hard.speed = 12292;
		divider = 7;
	} else if (dmasound.hard.speed > 8195) {
		dmasound.hard.speed = 9834;
		divider = 9;
	} else {
		dmasound.hard.speed = 8195;
		divider = 11;
	}
	tt_dmasnd.int_div = divider;

	/* Setup Falcon sound DMA for playback */
	tt_dmasnd.int_ctrl = 0x4; /* Timer A int at play end */
	tt_dmasnd.track_select = 0x0; /* play 1 track, track 1 */
	tt_dmasnd.cbar_src = 0x0001; /* DMA(25MHz) --> DAC */
	tt_dmasnd.cbar_dst = 0x0000;
	tt_dmasnd.rec_track_select = 0;
	tt_dmasnd.dac_src = 2; /* connect matrix to DAC */
	tt_dmasnd.adc_src = 0; /* ADC Input = Mic */

	tt_dmasnd.mode = (dmasound.hard.stereo ?
			  DMASND_MODE_STEREO : DMASND_MODE_MONO) |
		((dmasound.hard.size == 8) ?
		 DMASND_MODE_8BIT : DMASND_MODE_16BIT) |
		DMASND_MODE_6KHZ;

	expand_bal = -dmasound.soft.speed;
}


static int FalconSetFormat(int format)
{
	int size;
	/* Falcon sound DMA supports 8bit and 16bit modes */

	switch (format) {
	case AFMT_QUERY:
		return dmasound.soft.format;
	case AFMT_MU_LAW:
	case AFMT_A_LAW:
	case AFMT_U8:
	case AFMT_S8:
		size = 8;
		break;
	case AFMT_S16_BE:
	case AFMT_U16_BE:
	case AFMT_S16_LE:
	case AFMT_U16_LE:
		size = 16;
		break;
	default: /* :-) */
		size = 8;
		format = AFMT_S8;
	}

	dmasound.soft.format = format;
	dmasound.soft.size = size;
	if (dmasound.minDev == SND_DEV_DSP) {
		dmasound.dsp.format = format;
		dmasound.dsp.size = dmasound.soft.size;
	}

	FalconInit();

	return format;
}


/* This is for the Falcon output *attenuation* in 1.5dB steps,
 * i.e. output level from 0 to -22.5dB in -1.5dB steps.
 */
#define VOLUME_VOXWARE_TO_ATT(v) \
	((v) < 0 ? 15 : (v) > 100 ? 0 : 15 - (v) * 3 / 20)
#define VOLUME_ATT_TO_VOXWARE(v) (100 - (v) * 20 / 3)


static int FalconSetVolume(int volume)
{
	dmasound.volume_left = VOLUME_VOXWARE_TO_ATT(volume & 0xff);
	dmasound.volume_right = VOLUME_VOXWARE_TO_ATT((volume & 0xff00) >> 8);
	tt_dmasnd.output_atten = dmasound.volume_left << 8 | dmasound.volume_right << 4;
	return VOLUME_ATT_TO_VOXWARE(dmasound.volume_left) |
	       VOLUME_ATT_TO_VOXWARE(dmasound.volume_right) << 8;
}


static void AtaPlayNextFrame(int index)
{
	char *start, *end;

	/* used by AtaPlay() if all doubts whether there really is something
	 * to be played are already wiped out.
	 */
	start = write_sq.buffers[write_sq.front];
	end = start+((write_sq.count == index) ? write_sq.rear_size
					       : write_sq.block_size);
	/* end might not be a legal virtual address. */
	DMASNDSetEnd(virt_to_phys(end - 1) + 1);
	DMASNDSetBase(virt_to_phys(start));
	/* Since only an even number of samples per frame can
	   be played, we might lose one byte here. (TO DO) */
	write_sq.front = (write_sq.front+1) % write_sq.max_count;
	write_sq.active++;
	tt_dmasnd.ctrl = DMASND_CTRL_ON | DMASND_CTRL_REPEAT;
}


static void AtaPlay(void)
{
	/* ++TeSche: Note that write_sq.active is no longer just a flag but
	 * holds the number of frames the DMA is currently programmed for
	 * instead, may be 0, 1 (currently being played) or 2 (pre-programmed).
	 *
	 * Changes done to write_sq.count and write_sq.active are a bit more
	 * subtle again so now I must admit I also prefer disabling the irq
	 * here rather than considering all possible situations. But the point
	 * is that disabling the irq doesn't have any bad influence on this
	 * version of the driver as we benefit from having pre-programmed the
	 * DMA wherever possible: There's no need to reload the DMA at the
	 * exact time of an interrupt but only at some time while the
	 * pre-programmed frame is playing!
	 */
	atari_disable_irq(IRQ_MFP_TIMA);

	if (write_sq.active == 2 ||	/* DMA is 'full' */
	    write_sq.count <= 0) {	/* nothing to do */
		atari_enable_irq(IRQ_MFP_TIMA);
		return;
	}

	if (write_sq.active == 0) {
		/* looks like there's nothing 'in' the DMA yet, so try
		 * to put two frames into it (at least one is available).
		 */
		if (write_sq.count == 1 &&
		    write_sq.rear_size < write_sq.block_size &&
		    !write_sq.syncing) {
			/* hmmm, the only existing frame is not
			 * yet filled and we're not syncing?
			 */
			atari_enable_irq(IRQ_MFP_TIMA);
			return;
		}
		AtaPlayNextFrame(1);
		if (write_sq.count == 1) {
			/* no more frames */
			atari_enable_irq(IRQ_MFP_TIMA);
			return;
		}
		if (write_sq.count == 2 &&
		    write_sq.rear_size < write_sq.block_size &&
		    !write_sq.syncing) {
			/* hmmm, there were two frames, but the second
			 * one is not yet filled and we're not syncing?
			 */
			atari_enable_irq(IRQ_MFP_TIMA);
			return;
		}
		AtaPlayNextFrame(2);
	} else {
		/* there's already a frame being played so we may only stuff
		 * one new into the DMA, but even if this may be the last
		 * frame existing the previous one is still on write_sq.count.
		 */
		if (write_sq.count == 2 &&
		    write_sq.rear_size < write_sq.block_size &&
		    !write_sq.syncing) {
			/* hmmm, the only existing frame is not
			 * yet filled and we're not syncing?
			 */
			atari_enable_irq(IRQ_MFP_TIMA);
			return;
		}
		AtaPlayNextFrame(2);
	}
	atari_enable_irq(IRQ_MFP_TIMA);
}


static irqreturn_t AtaInterrupt(int irq, void *dummy, struct pt_regs *fp)
{
#if 0
	/* ++TeSche: if you should want to test this... */
	static int cnt;
	if (write_sq.active == 2)
		if (++cnt == 10) {
			/* simulate losing an interrupt */
			cnt = 0;
			return IRQ_HANDLED;
		}
#endif
	spin_lock(&dmasound.lock);
	if (write_sq_ignore_int && is_falcon) {
		/* ++TeSche: Falcon only: ignore first irq because it comes
		 * immediately after starting a frame. after that, irqs come
		 * (almost) like on the TT.
		 */
		write_sq_ignore_int = 0;
		return IRQ_HANDLED;
	}

	if (!write_sq.active) {
		/* playing was interrupted and sq_reset() has already cleared
		 * the sq variables, so better don't do anything here.
		 */
		WAKE_UP(write_sq.sync_queue);
		return IRQ_HANDLED;
	}

	/* Probably ;) one frame is finished. Well, in fact it may be that a
	 * pre-programmed one is also finished because there has been a long
	 * delay in interrupt delivery and we've completely lost one, but
	 * there's no way to detect such a situation. In such a case the last
	 * frame will be played more than once and the situation will recover
	 * as soon as the irq gets through.
	 */
	write_sq.count--;
	write_sq.active--;

	if (!write_sq.active) {
		tt_dmasnd.ctrl = DMASND_CTRL_OFF;
		write_sq_ignore_int = 1;
	}

	WAKE_UP(write_sq.action_queue);
	/* At least one block of the queue is free now
	   so wake up a writing process blocked because
	   of a full queue. */

	if ((write_sq.active != 1) || (write_sq.count != 1))
		/* We must be a bit carefully here: write_sq.count indicates the
		 * number of buffers used and not the number of frames to be
		 * played. If write_sq.count==1 and write_sq.active==1 that
		 * means the only remaining frame was already programmed
		 * earlier (and is currently running) so we mustn't call
		 * AtaPlay() here, otherwise we'll play one frame too much.
		 */
		AtaPlay();

	if (!write_sq.active) WAKE_UP(write_sq.sync_queue);
	/* We are not playing after AtaPlay(), so there
	   is nothing to play any more. Wake up a process
	   waiting for audio output to drain. */
	spin_unlock(&dmasound.lock);
	return IRQ_HANDLED;
}


/*** Mid level stuff *********************************************************/


/*
 * /dev/mixer abstraction
 */

#define RECLEVEL_VOXWARE_TO_GAIN(v)	\
	((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
#define RECLEVEL_GAIN_TO_VOXWARE(v)	(((v) * 20 + 2) / 3)


static void __init TTMixerInit(void)
{
	atari_microwire_cmd(MW_LM1992_VOLUME(0));
	dmasound.volume_left = 0;
	atari_microwire_cmd(MW_LM1992_BALLEFT(0));
	dmasound.volume_right = 0;
	atari_microwire_cmd(MW_LM1992_BALRIGHT(0));
	atari_microwire_cmd(MW_LM1992_TREBLE(0));
	atari_microwire_cmd(MW_LM1992_BASS(0));
}

static void __init FalconMixerInit(void)
{
	dmasound.volume_left = (tt_dmasnd.output_atten & 0xf00) >> 8;
	dmasound.volume_right = (tt_dmasnd.output_atten & 0xf0) >> 4;
}

static int AtaMixerIoctl(u_int cmd, u_long arg)
{
	int data;
	unsigned long flags;
	switch (cmd) {
	    case SOUND_MIXER_READ_SPEAKER:
		    if (is_falcon || MACH_IS_TT) {
			    int porta;
			    spin_lock_irqsave(&dmasound.lock, flags);
			    sound_ym.rd_data_reg_sel = 14;
			    porta = sound_ym.rd_data_reg_sel;
			    spin_unlock_irqrestore(&dmasound.lock, flags);
			    return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
		    }
		    break;
	    case SOUND_MIXER_WRITE_VOLUME:
		    IOCTL_IN(arg, data);
		    return IOCTL_OUT(arg, dmasound_set_volume(data));
	    case SOUND_MIXER_WRITE_SPEAKER:
		    if (is_falcon || MACH_IS_TT) {
			    int porta;
			    IOCTL_IN(arg, data);
			    spin_lock_irqsave(&dmasound.lock, flags);
			    sound_ym.rd_data_reg_sel = 14;
			    porta = (sound_ym.rd_data_reg_sel & ~0x40) |
				    (data < 50 ? 0x40 : 0);
			    sound_ym.wd_data = porta;
			    spin_unlock_irqrestore(&dmasound.lock, flags);
			    return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
		    }
	}
	return -EINVAL;
}


static int TTMixerIoctl(u_int cmd, u_long arg)
{
	int data;
	switch (cmd) {
	    case SOUND_MIXER_READ_RECMASK:
		return IOCTL_OUT(arg, 0);
	    case SOUND_MIXER_READ_DEVMASK:
		return IOCTL_OUT(arg,
				 SOUND_MASK_VOLUME | SOUND_MASK_TREBLE | SOUND_MASK_BASS |
				 (MACH_IS_TT ? SOUND_MASK_SPEAKER : 0));
	    case SOUND_MIXER_READ_STEREODEVS:
		return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
	    case SOUND_MIXER_READ_VOLUME:
		return IOCTL_OUT(arg,
				 VOLUME_DB_TO_VOXWARE(dmasound.volume_left) |
				 (VOLUME_DB_TO_VOXWARE(dmasound.volume_right) << 8));
	    case SOUND_MIXER_READ_BASS:
		return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(dmasound.bass));
	    case SOUND_MIXER_READ_TREBLE:
		return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(dmasound.treble));
	    case SOUND_MIXER_READ_OGAIN:
		return IOCTL_OUT(arg, GAIN_DB_TO_VOXWARE(dmasound.gain));
	    case SOUND_MIXER_WRITE_BASS:
		IOCTL_IN(arg, data);
		return IOCTL_OUT(arg, dmasound_set_bass(data));
	    case SOUND_MIXER_WRITE_TREBLE:
		IOCTL_IN(arg, data);
		return IOCTL_OUT(arg, dmasound_set_treble(data));
	    case SOUND_MIXER_WRITE_OGAIN:
		IOCTL_IN(arg, data);
		return IOCTL_OUT(arg, dmasound_set_gain(data));
	}
	return AtaMixerIoctl(cmd, arg);
}

static int FalconMixerIoctl(u_int cmd, u_long arg)
{
	int data;
	switch (cmd) {
	    case SOUND_MIXER_READ_RECMASK:
		return IOCTL_OUT(arg, SOUND_MASK_MIC);
	    case SOUND_MIXER_READ_DEVMASK:
		return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC | SOUND_MASK_SPEAKER);
	    case SOUND_MIXER_READ_STEREODEVS:
		return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC);
	    case SOUND_MIXER_READ_VOLUME:
		return IOCTL_OUT(arg,
			VOLUME_ATT_TO_VOXWARE(dmasound.volume_left) |
			VOLUME_ATT_TO_VOXWARE(dmasound.volume_right) << 8);
	    case SOUND_MIXER_READ_CAPS:
		return IOCTL_OUT(arg, SOUND_CAP_EXCL_INPUT);
	    case SOUND_MIXER_WRITE_MIC:
		IOCTL_IN(arg, data);
		tt_dmasnd.input_gain =
			RECLEVEL_VOXWARE_TO_GAIN(data & 0xff) << 4 |
			RECLEVEL_VOXWARE_TO_GAIN(data >> 8 & 0xff);
		/* fall thru, return set value */
	    case SOUND_MIXER_READ_MIC:
		return IOCTL_OUT(arg,
			RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain >> 4 & 0xf) |
			RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain & 0xf) << 8);
	}
	return AtaMixerIoctl(cmd, arg);
}

static int AtaWriteSqSetup(void)
{
	write_sq_ignore_int = 0;
	return 0 ;
}

static int AtaSqOpen(mode_t mode)
{
	write_sq_ignore_int = 1;
	return 0 ;
}

static int TTStateInfo(char *buffer, size_t space)
{
	int len = 0;
	len += sprintf(buffer+len, "\tvol left  %ddB [-40...  0]\n",
		       dmasound.volume_left);
	len += sprintf(buffer+len, "\tvol right %ddB [-40...  0]\n",
		       dmasound.volume_right);
	len += sprintf(buffer+len, "\tbass      %ddB [-12...+12]\n",
		       dmasound.bass);
	len += sprintf(buffer+len, "\ttreble    %ddB [-12...+12]\n",
		       dmasound.treble);
	if (len >= space) {
		printk(KERN_ERR "dmasound_atari: overflowed state buffer alloc.\n") ;
		len = space ;
	}
	return len;
}

static int FalconStateInfo(char *buffer, size_t space)
{
	int len = 0;
	len += sprintf(buffer+len, "\tvol left  %ddB [-22.5 ... 0]\n",
		       dmasound.volume_left);
	len += sprintf(buffer+len, "\tvol right %ddB [-22.5 ... 0]\n",
		       dmasound.volume_right);
	if (len >= space) {
		printk(KERN_ERR "dmasound_atari: overflowed state buffer alloc.\n") ;
		len = space ;
	}
	return len;
}


/*** Machine definitions *****************************************************/

static SETTINGS def_hard_falcon = {
	.format		= AFMT_S8,
	.stereo		= 0,
	.size		= 8,
	.speed		= 8195
} ;

static SETTINGS def_hard_tt = {
	.format	= AFMT_S8,
	.stereo	= 0,
	.size	= 8,
	.speed	= 12517
} ;

static SETTINGS def_soft = {
	.format	= AFMT_U8,
	.stereo	= 0,
	.size	= 8,
	.speed	= 8000
} ;

static MACHINE machTT = {
	.name		= "Atari",
	.name2		= "TT",
	.owner		= THIS_MODULE,
	.dma_alloc	= AtaAlloc,
	.dma_free	= AtaFree,
	.irqinit	= AtaIrqInit,
#ifdef MODULE
	.irqcleanup	= AtaIrqCleanUp,
#endif /* MODULE */
	.init		= TTInit,
	.silence	= TTSilence,
	.setFormat	= TTSetFormat,
	.setVolume	= TTSetVolume,
	.setBass	= AtaSetBass,
	.setTreble	= AtaSetTreble,
	.setGain	= TTSetGain,
	.play		= AtaPlay,
	.mixer_init	= TTMixerInit,
	.mixer_ioctl	= TTMixerIoctl,
	.write_sq_setup	= AtaWriteSqSetup,
	.sq_open	= AtaSqOpen,
	.state_info	= TTStateInfo,
	.min_dsp_speed	= 6258,
	.version	= ((DMASOUND_ATARI_REVISION<<8) | DMASOUND_ATARI_EDITION),
	.hardware_afmts	= AFMT_S8,  /* h'ware-supported formats *only* here */
	.capabilities	=  DSP_CAP_BATCH	/* As per SNDCTL_DSP_GETCAPS */
};

static MACHINE machFalcon = {
	.name		= "Atari",
	.name2		= "FALCON",
	.dma_alloc	= AtaAlloc,
	.dma_free	= AtaFree,
	.irqinit	= AtaIrqInit,
#ifdef MODULE
	.irqcleanup	= AtaIrqCleanUp,
#endif /* MODULE */
	.init		= FalconInit,
	.silence	= FalconSilence,
	.setFormat	= FalconSetFormat,
	.setVolume	= FalconSetVolume,
	.setBass	= AtaSetBass,
	.setTreble	= AtaSetTreble,
	.play		= AtaPlay,
	.mixer_init	= FalconMixerInit,
	.mixer_ioctl	= FalconMixerIoctl,
	.write_sq_setup	= AtaWriteSqSetup,
	.sq_open	= AtaSqOpen,
	.state_info	= FalconStateInfo,
	.min_dsp_speed	= 8195,
	.version	= ((DMASOUND_ATARI_REVISION<<8) | DMASOUND_ATARI_EDITION),
	.hardware_afmts	= (AFMT_S8 | AFMT_S16_BE), /* h'ware-supported formats *only* here */
	.capabilities	=  DSP_CAP_BATCH	/* As per SNDCTL_DSP_GETCAPS */
};


/*** Config & Setup **********************************************************/


static int __init dmasound_atari_init(void)
{
	if (MACH_IS_ATARI && ATARIHW_PRESENT(PCM_8BIT)) {
	    if (ATARIHW_PRESENT(CODEC)) {
		dmasound.mach = machFalcon;
		dmasound.mach.default_soft = def_soft ;
		dmasound.mach.default_hard = def_hard_falcon ;
		is_falcon = 1;
	    } else if (ATARIHW_PRESENT(MICROWIRE)) {
		dmasound.mach = machTT;
		dmasound.mach.default_soft = def_soft ;
		dmasound.mach.default_hard = def_hard_tt ;
		is_falcon = 0;
	    } else
		return -ENODEV;
	    if ((mfp.int_en_a & mfp.int_mk_a & 0x20) == 0)
		return dmasound_init();
	    else {
		printk("DMA sound driver: Timer A interrupt already in use\n");
		return -EBUSY;
	    }
	}
	return -ENODEV;
}

static void __exit dmasound_atari_cleanup(void)
{
	dmasound_deinit();
}

module_init(dmasound_atari_init);
module_exit(dmasound_atari_cleanup);
MODULE_LICENSE("GPL");