2 * linux/sound/oss/dmasound/dmasound_atari.c
4 * Atari TT and Falcon DMA Sound Driver
6 * See linux/sound/oss/dmasound/dmasound_core.c for copyright and credits
9 * 28/01/2001 [0.1] Iain Sandoe
11 * - put in and populated the hardware_afmts field.
12 * [0.2] - put in SNDCTL_DSP_GETCAPS value.
13 * 01/02/2001 [0.3] - put in default hard/soft settings.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/soundcard.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
25 #include <asm/uaccess.h>
26 #include <asm/atariints.h>
27 #include <asm/atari_stram.h>
31 #define DMASOUND_ATARI_REVISION 0
32 #define DMASOUND_ATARI_EDITION 3
34 extern void atari_microwire_cmd(int cmd
);
37 static int write_sq_ignore_int
; /* ++TeSche: used for Falcon */
39 static int expand_bal
; /* Balance factor for expanding (not volume!) */
40 static int expand_data
; /* Data for expanding */
43 /*** Translations ************************************************************/
46 /* ++TeSche: radically changed for new expanding purposes...
48 * These two routines now deal with copying/expanding/translating the samples
49 * from user space into our buffer at the right frequency. They take care about
50 * how much data there's actually to read, how much buffer space there is and
51 * to convert samples into the right frequency/encoding. They will only work on
52 * complete samples so it may happen they leave some bytes in the input stream
53 * if the user didn't write a multiple of the current sample size. They both
54 * return the number of bytes they've used from both streams so you may detect
55 * such a situation. Luckily all programs should be able to cope with that.
57 * I think I've optimized anything as far as one can do in plain C, all
58 * variables should fit in registers and the loops are really short. There's
59 * one loop for every possible situation. Writing a more generalized and thus
60 * parameterized loop would only produce slower code. Feel free to optimize
61 * this in assembler if you like. :)
63 * I think these routines belong here because they're not yet really hardware
64 * independent, especially the fact that the Falcon can play 16bit samples
65 * only in stereo is hardcoded in both of them!
67 * ++geert: split in even more functions (one per format)
70 static ssize_t
ata_ct_law(const u_char __user
*userPtr
, size_t userCount
,
71 u_char frame
[], ssize_t
*frameUsed
,
73 static ssize_t
ata_ct_s8(const u_char __user
*userPtr
, size_t userCount
,
74 u_char frame
[], ssize_t
*frameUsed
,
76 static ssize_t
ata_ct_u8(const u_char __user
*userPtr
, size_t userCount
,
77 u_char frame
[], ssize_t
*frameUsed
,
79 static ssize_t
ata_ct_s16be(const u_char __user
*userPtr
, size_t userCount
,
80 u_char frame
[], ssize_t
*frameUsed
,
82 static ssize_t
ata_ct_u16be(const u_char __user
*userPtr
, size_t userCount
,
83 u_char frame
[], ssize_t
*frameUsed
,
85 static ssize_t
ata_ct_s16le(const u_char __user
*userPtr
, size_t userCount
,
86 u_char frame
[], ssize_t
*frameUsed
,
88 static ssize_t
ata_ct_u16le(const u_char __user
*userPtr
, size_t userCount
,
89 u_char frame
[], ssize_t
*frameUsed
,
91 static ssize_t
ata_ctx_law(const u_char __user
*userPtr
, size_t userCount
,
92 u_char frame
[], ssize_t
*frameUsed
,
94 static ssize_t
ata_ctx_s8(const u_char __user
*userPtr
, size_t userCount
,
95 u_char frame
[], ssize_t
*frameUsed
,
97 static ssize_t
ata_ctx_u8(const u_char __user
*userPtr
, size_t userCount
,
98 u_char frame
[], ssize_t
*frameUsed
,
100 static ssize_t
ata_ctx_s16be(const u_char __user
*userPtr
, size_t userCount
,
101 u_char frame
[], ssize_t
*frameUsed
,
103 static ssize_t
ata_ctx_u16be(const u_char __user
*userPtr
, size_t userCount
,
104 u_char frame
[], ssize_t
*frameUsed
,
106 static ssize_t
ata_ctx_s16le(const u_char __user
*userPtr
, size_t userCount
,
107 u_char frame
[], ssize_t
*frameUsed
,
109 static ssize_t
ata_ctx_u16le(const u_char __user
*userPtr
, size_t userCount
,
110 u_char frame
[], ssize_t
*frameUsed
,
114 /*** Low level stuff *********************************************************/
117 static void *AtaAlloc(unsigned int size
, gfp_t flags
);
118 static void AtaFree(void *, unsigned int size
);
119 static int AtaIrqInit(void);
121 static void AtaIrqCleanUp(void);
123 static int AtaSetBass(int bass
);
124 static int AtaSetTreble(int treble
);
125 static void TTSilence(void);
126 static void TTInit(void);
127 static int TTSetFormat(int format
);
128 static int TTSetVolume(int volume
);
129 static int TTSetGain(int gain
);
130 static void FalconSilence(void);
131 static void FalconInit(void);
132 static int FalconSetFormat(int format
);
133 static int FalconSetVolume(int volume
);
134 static void AtaPlayNextFrame(int index
);
135 static void AtaPlay(void);
136 static irqreturn_t
AtaInterrupt(int irq
, void *dummy
);
138 /*** Mid level stuff *********************************************************/
140 static void TTMixerInit(void);
141 static void FalconMixerInit(void);
142 static int AtaMixerIoctl(u_int cmd
, u_long arg
);
143 static int TTMixerIoctl(u_int cmd
, u_long arg
);
144 static int FalconMixerIoctl(u_int cmd
, u_long arg
);
145 static int AtaWriteSqSetup(void);
146 static int AtaSqOpen(fmode_t mode
);
147 static int TTStateInfo(char *buffer
, size_t space
);
148 static int FalconStateInfo(char *buffer
, size_t space
);
151 /*** Translations ************************************************************/
154 static ssize_t
ata_ct_law(const u_char __user
*userPtr
, size_t userCount
,
155 u_char frame
[], ssize_t
*frameUsed
,
158 char *table
= dmasound
.soft
.format
== AFMT_MU_LAW
? dmasound_ulaw2dma8
159 : dmasound_alaw2dma8
;
161 u_char
*p
= &frame
[*frameUsed
];
163 count
= min_t(unsigned long, userCount
, frameLeft
);
164 if (dmasound
.soft
.stereo
)
169 if (get_user(data
, userPtr
++))
179 static ssize_t
ata_ct_s8(const u_char __user
*userPtr
, size_t userCount
,
180 u_char frame
[], ssize_t
*frameUsed
,
184 void *p
= &frame
[*frameUsed
];
186 count
= min_t(unsigned long, userCount
, frameLeft
);
187 if (dmasound
.soft
.stereo
)
190 if (copy_from_user(p
, userPtr
, count
))
197 static ssize_t
ata_ct_u8(const u_char __user
*userPtr
, size_t userCount
,
198 u_char frame
[], ssize_t
*frameUsed
,
203 if (!dmasound
.soft
.stereo
) {
204 u_char
*p
= &frame
[*frameUsed
];
205 count
= min_t(unsigned long, userCount
, frameLeft
);
209 if (get_user(data
, userPtr
++))
215 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
216 count
= min_t(unsigned long, userCount
, frameLeft
)>>1;
220 if (get_user(data
, (u_short __user
*)userPtr
))
223 *p
++ = data
^ 0x8080;
232 static ssize_t
ata_ct_s16be(const u_char __user
*userPtr
, size_t userCount
,
233 u_char frame
[], ssize_t
*frameUsed
,
238 if (!dmasound
.soft
.stereo
) {
239 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
240 count
= min_t(unsigned long, userCount
, frameLeft
)>>1;
244 if (get_user(data
, (u_short __user
*)userPtr
))
251 *frameUsed
+= used
*2;
253 void *p
= (u_short
*)&frame
[*frameUsed
];
254 count
= min_t(unsigned long, userCount
, frameLeft
) & ~3;
256 if (copy_from_user(p
, userPtr
, count
))
264 static ssize_t
ata_ct_u16be(const u_char __user
*userPtr
, size_t userCount
,
265 u_char frame
[], ssize_t
*frameUsed
,
270 if (!dmasound
.soft
.stereo
) {
271 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
272 count
= min_t(unsigned long, userCount
, frameLeft
)>>1;
276 if (get_user(data
, (u_short __user
*)userPtr
))
284 *frameUsed
+= used
*2;
286 u_long
*p
= (u_long
*)&frame
[*frameUsed
];
287 count
= min_t(unsigned long, userCount
, frameLeft
)>>2;
291 if (get_user(data
, (u_int __user
*)userPtr
))
294 *p
++ = data
^ 0x80008000;
303 static ssize_t
ata_ct_s16le(const u_char __user
*userPtr
, size_t userCount
,
304 u_char frame
[], ssize_t
*frameUsed
,
310 if (!dmasound
.soft
.stereo
) {
311 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
312 count
= min_t(unsigned long, userCount
, frameLeft
)>>1;
316 if (get_user(data
, (u_short __user
*)userPtr
))
319 data
= le2be16(data
);
324 *frameUsed
+= used
*2;
326 u_long
*p
= (u_long
*)&frame
[*frameUsed
];
327 count
= min_t(unsigned long, userCount
, frameLeft
)>>2;
331 if (get_user(data
, (u_int __user
*)userPtr
))
334 data
= le2be16dbl(data
);
344 static ssize_t
ata_ct_u16le(const u_char __user
*userPtr
, size_t userCount
,
345 u_char frame
[], ssize_t
*frameUsed
,
351 if (!dmasound
.soft
.stereo
) {
352 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
353 count
= min_t(unsigned long, userCount
, frameLeft
)>>1;
357 if (get_user(data
, (u_short __user
*)userPtr
))
360 data
= le2be16(data
) ^ 0x8000;
364 *frameUsed
+= used
*2;
366 u_long
*p
= (u_long
*)&frame
[*frameUsed
];
367 count
= min_t(unsigned long, userCount
, frameLeft
)>>2;
371 if (get_user(data
, (u_int __user
*)userPtr
))
374 data
= le2be16dbl(data
) ^ 0x80008000;
384 static ssize_t
ata_ctx_law(const u_char __user
*userPtr
, size_t userCount
,
385 u_char frame
[], ssize_t
*frameUsed
,
388 char *table
= dmasound
.soft
.format
== AFMT_MU_LAW
? dmasound_ulaw2dma8
389 : dmasound_alaw2dma8
;
390 /* this should help gcc to stuff everything into registers */
391 long bal
= expand_bal
;
392 long hSpeed
= dmasound
.hard
.speed
, sSpeed
= dmasound
.soft
.speed
;
397 if (!dmasound
.soft
.stereo
) {
398 u_char
*p
= &frame
[*frameUsed
];
399 u_char data
= expand_data
;
405 if (get_user(c
, userPtr
++))
417 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
418 u_short data
= expand_data
;
419 while (frameLeft
>= 2) {
424 if (get_user(c
, userPtr
++))
426 data
= table
[c
] << 8;
427 if (get_user(c
, userPtr
++))
441 *frameUsed
+= usedf
-frameLeft
;
446 static ssize_t
ata_ctx_s8(const u_char __user
*userPtr
, size_t userCount
,
447 u_char frame
[], ssize_t
*frameUsed
,
450 /* this should help gcc to stuff everything into registers */
451 long bal
= expand_bal
;
452 long hSpeed
= dmasound
.hard
.speed
, sSpeed
= dmasound
.soft
.speed
;
457 if (!dmasound
.soft
.stereo
) {
458 u_char
*p
= &frame
[*frameUsed
];
459 u_char data
= expand_data
;
464 if (get_user(data
, userPtr
++))
475 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
476 u_short data
= expand_data
;
477 while (frameLeft
>= 2) {
481 if (get_user(data
, (u_short __user
*)userPtr
))
495 *frameUsed
+= usedf
-frameLeft
;
500 static ssize_t
ata_ctx_u8(const u_char __user
*userPtr
, size_t userCount
,
501 u_char frame
[], ssize_t
*frameUsed
,
504 /* this should help gcc to stuff everything into registers */
505 long bal
= expand_bal
;
506 long hSpeed
= dmasound
.hard
.speed
, sSpeed
= dmasound
.soft
.speed
;
511 if (!dmasound
.soft
.stereo
) {
512 u_char
*p
= &frame
[*frameUsed
];
513 u_char data
= expand_data
;
518 if (get_user(data
, userPtr
++))
530 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
531 u_short data
= expand_data
;
532 while (frameLeft
>= 2) {
536 if (get_user(data
, (u_short __user
*)userPtr
))
551 *frameUsed
+= usedf
-frameLeft
;
556 static ssize_t
ata_ctx_s16be(const u_char __user
*userPtr
, size_t userCount
,
557 u_char frame
[], ssize_t
*frameUsed
,
560 /* this should help gcc to stuff everything into registers */
561 long bal
= expand_bal
;
562 long hSpeed
= dmasound
.hard
.speed
, sSpeed
= dmasound
.soft
.speed
;
567 if (!dmasound
.soft
.stereo
) {
568 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
569 u_short data
= expand_data
;
570 while (frameLeft
>= 4) {
574 if (get_user(data
, (u_short __user
*)userPtr
))
587 u_long
*p
= (u_long
*)&frame
[*frameUsed
];
588 u_long data
= expand_data
;
589 while (frameLeft
>= 4) {
593 if (get_user(data
, (u_int __user
*)userPtr
))
607 *frameUsed
+= usedf
-frameLeft
;
612 static ssize_t
ata_ctx_u16be(const u_char __user
*userPtr
, size_t userCount
,
613 u_char frame
[], ssize_t
*frameUsed
,
616 /* this should help gcc to stuff everything into registers */
617 long bal
= expand_bal
;
618 long hSpeed
= dmasound
.hard
.speed
, sSpeed
= dmasound
.soft
.speed
;
623 if (!dmasound
.soft
.stereo
) {
624 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
625 u_short data
= expand_data
;
626 while (frameLeft
>= 4) {
630 if (get_user(data
, (u_short __user
*)userPtr
))
644 u_long
*p
= (u_long
*)&frame
[*frameUsed
];
645 u_long data
= expand_data
;
646 while (frameLeft
>= 4) {
650 if (get_user(data
, (u_int __user
*)userPtr
))
665 *frameUsed
+= usedf
-frameLeft
;
670 static ssize_t
ata_ctx_s16le(const u_char __user
*userPtr
, size_t userCount
,
671 u_char frame
[], ssize_t
*frameUsed
,
674 /* this should help gcc to stuff everything into registers */
675 long bal
= expand_bal
;
676 long hSpeed
= dmasound
.hard
.speed
, sSpeed
= dmasound
.soft
.speed
;
681 if (!dmasound
.soft
.stereo
) {
682 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
683 u_short data
= expand_data
;
684 while (frameLeft
>= 4) {
688 if (get_user(data
, (u_short __user
*)userPtr
))
691 data
= le2be16(data
);
702 u_long
*p
= (u_long
*)&frame
[*frameUsed
];
703 u_long data
= expand_data
;
704 while (frameLeft
>= 4) {
708 if (get_user(data
, (u_int __user
*)userPtr
))
711 data
= le2be16dbl(data
);
723 *frameUsed
+= usedf
-frameLeft
;
728 static ssize_t
ata_ctx_u16le(const u_char __user
*userPtr
, size_t userCount
,
729 u_char frame
[], ssize_t
*frameUsed
,
732 /* this should help gcc to stuff everything into registers */
733 long bal
= expand_bal
;
734 long hSpeed
= dmasound
.hard
.speed
, sSpeed
= dmasound
.soft
.speed
;
739 if (!dmasound
.soft
.stereo
) {
740 u_short
*p
= (u_short
*)&frame
[*frameUsed
];
741 u_short data
= expand_data
;
742 while (frameLeft
>= 4) {
746 if (get_user(data
, (u_short __user
*)userPtr
))
749 data
= le2be16(data
) ^ 0x8000;
760 u_long
*p
= (u_long
*)&frame
[*frameUsed
];
761 u_long data
= expand_data
;
762 while (frameLeft
>= 4) {
766 if (get_user(data
, (u_int __user
*)userPtr
))
769 data
= le2be16dbl(data
) ^ 0x80008000;
781 *frameUsed
+= usedf
-frameLeft
;
786 static TRANS transTTNormal
= {
787 .ct_ulaw
= ata_ct_law
,
788 .ct_alaw
= ata_ct_law
,
793 static TRANS transTTExpanding
= {
794 .ct_ulaw
= ata_ctx_law
,
795 .ct_alaw
= ata_ctx_law
,
800 static TRANS transFalconNormal
= {
801 .ct_ulaw
= ata_ct_law
,
802 .ct_alaw
= ata_ct_law
,
805 .ct_s16be
= ata_ct_s16be
,
806 .ct_u16be
= ata_ct_u16be
,
807 .ct_s16le
= ata_ct_s16le
,
808 .ct_u16le
= ata_ct_u16le
811 static TRANS transFalconExpanding
= {
812 .ct_ulaw
= ata_ctx_law
,
813 .ct_alaw
= ata_ctx_law
,
816 .ct_s16be
= ata_ctx_s16be
,
817 .ct_u16be
= ata_ctx_u16be
,
818 .ct_s16le
= ata_ctx_s16le
,
819 .ct_u16le
= ata_ctx_u16le
,
823 /*** Low level stuff *********************************************************/
831 static void *AtaAlloc(unsigned int size
, gfp_t flags
)
833 return atari_stram_alloc(size
, "dmasound");
836 static void AtaFree(void *obj
, unsigned int size
)
838 atari_stram_free( obj
);
841 static int __init
AtaIrqInit(void)
843 /* Set up timer A. Timer A
844 will receive a signal upon end of playing from the sound
845 hardware. Furthermore Timer A is able to count events
846 and will cause an interrupt after a programmed number
847 of events. So all we need to keep the music playing is
848 to provide the sound hardware with new data upon
849 an interrupt from timer A. */
850 st_mfp
.tim_ct_a
= 0; /* ++roman: Stop timer before programming! */
851 st_mfp
.tim_dt_a
= 1; /* Cause interrupt after first event. */
852 st_mfp
.tim_ct_a
= 8; /* Turn on event counting. */
853 /* Register interrupt handler. */
854 if (request_irq(IRQ_MFP_TIMA
, AtaInterrupt
, IRQ_TYPE_SLOW
, "DMA sound",
857 st_mfp
.int_en_a
|= 0x20; /* Turn interrupt on. */
858 st_mfp
.int_mk_a
|= 0x20;
863 static void AtaIrqCleanUp(void)
865 st_mfp
.tim_ct_a
= 0; /* stop timer */
866 st_mfp
.int_en_a
&= ~0x20; /* turn interrupt off */
867 free_irq(IRQ_MFP_TIMA
, AtaInterrupt
);
872 #define TONE_VOXWARE_TO_DB(v) \
873 (((v) < 0) ? -12 : ((v) > 100) ? 12 : ((v) - 50) * 6 / 25)
874 #define TONE_DB_TO_VOXWARE(v) (((v) * 25 + ((v) > 0 ? 5 : -5)) / 6 + 50)
877 static int AtaSetBass(int bass
)
879 dmasound
.bass
= TONE_VOXWARE_TO_DB(bass
);
880 atari_microwire_cmd(MW_LM1992_BASS(dmasound
.bass
));
881 return TONE_DB_TO_VOXWARE(dmasound
.bass
);
885 static int AtaSetTreble(int treble
)
887 dmasound
.treble
= TONE_VOXWARE_TO_DB(treble
);
888 atari_microwire_cmd(MW_LM1992_TREBLE(dmasound
.treble
));
889 return TONE_DB_TO_VOXWARE(dmasound
.treble
);
899 static void TTSilence(void)
901 tt_dmasnd
.ctrl
= DMASND_CTRL_OFF
;
902 atari_microwire_cmd(MW_LM1992_PSG_HIGH
); /* mix in PSG signal 1:1 */
906 static void TTInit(void)
909 const int freq
[4] = {50066, 25033, 12517, 6258};
911 /* search a frequency that fits into the allowed error range */
914 for (i
= 0; i
< ARRAY_SIZE(freq
); i
++)
915 /* this isn't as much useful for a TT than for a Falcon, but
916 * then it doesn't hurt very much to implement it for a TT too.
918 if ((100 * abs(dmasound
.soft
.speed
- freq
[i
]) / freq
[i
]) < catchRadius
)
921 dmasound
.soft
.speed
= freq
[idx
];
922 dmasound
.trans_write
= &transTTNormal
;
924 dmasound
.trans_write
= &transTTExpanding
;
927 dmasound
.hard
= dmasound
.soft
;
929 if (dmasound
.hard
.speed
> 50066) {
930 /* we would need to squeeze the sound, but we won't do that */
931 dmasound
.hard
.speed
= 50066;
932 mode
= DMASND_MODE_50KHZ
;
933 dmasound
.trans_write
= &transTTNormal
;
934 } else if (dmasound
.hard
.speed
> 25033) {
935 dmasound
.hard
.speed
= 50066;
936 mode
= DMASND_MODE_50KHZ
;
937 } else if (dmasound
.hard
.speed
> 12517) {
938 dmasound
.hard
.speed
= 25033;
939 mode
= DMASND_MODE_25KHZ
;
940 } else if (dmasound
.hard
.speed
> 6258) {
941 dmasound
.hard
.speed
= 12517;
942 mode
= DMASND_MODE_12KHZ
;
944 dmasound
.hard
.speed
= 6258;
945 mode
= DMASND_MODE_6KHZ
;
948 tt_dmasnd
.mode
= (dmasound
.hard
.stereo
?
949 DMASND_MODE_STEREO
: DMASND_MODE_MONO
) |
950 DMASND_MODE_8BIT
| mode
;
952 expand_bal
= -dmasound
.soft
.speed
;
956 static int TTSetFormat(int format
)
958 /* TT sound DMA supports only 8bit modes */
962 return dmasound
.soft
.format
;
972 dmasound
.soft
.format
= format
;
973 dmasound
.soft
.size
= 8;
974 if (dmasound
.minDev
== SND_DEV_DSP
) {
975 dmasound
.dsp
.format
= format
;
976 dmasound
.dsp
.size
= 8;
984 #define VOLUME_VOXWARE_TO_DB(v) \
985 (((v) < 0) ? -40 : ((v) > 100) ? 0 : ((v) * 2) / 5 - 40)
986 #define VOLUME_DB_TO_VOXWARE(v) ((((v) + 40) * 5 + 1) / 2)
989 static int TTSetVolume(int volume
)
991 dmasound
.volume_left
= VOLUME_VOXWARE_TO_DB(volume
& 0xff);
992 atari_microwire_cmd(MW_LM1992_BALLEFT(dmasound
.volume_left
));
993 dmasound
.volume_right
= VOLUME_VOXWARE_TO_DB((volume
& 0xff00) >> 8);
994 atari_microwire_cmd(MW_LM1992_BALRIGHT(dmasound
.volume_right
));
995 return VOLUME_DB_TO_VOXWARE(dmasound
.volume_left
) |
996 (VOLUME_DB_TO_VOXWARE(dmasound
.volume_right
) << 8);
1000 #define GAIN_VOXWARE_TO_DB(v) \
1001 (((v) < 0) ? -80 : ((v) > 100) ? 0 : ((v) * 4) / 5 - 80)
1002 #define GAIN_DB_TO_VOXWARE(v) ((((v) + 80) * 5 + 1) / 4)
1004 static int TTSetGain(int gain
)
1006 dmasound
.gain
= GAIN_VOXWARE_TO_DB(gain
);
1007 atari_microwire_cmd(MW_LM1992_VOLUME(dmasound
.gain
));
1008 return GAIN_DB_TO_VOXWARE(dmasound
.gain
);
1018 static void FalconSilence(void)
1020 /* stop playback, set sample rate 50kHz for PSG sound */
1021 tt_dmasnd
.ctrl
= DMASND_CTRL_OFF
;
1022 tt_dmasnd
.mode
= DMASND_MODE_50KHZ
| DMASND_MODE_STEREO
| DMASND_MODE_8BIT
;
1023 tt_dmasnd
.int_div
= 0; /* STE compatible divider */
1024 tt_dmasnd
.int_ctrl
= 0x0;
1025 tt_dmasnd
.cbar_src
= 0x0000; /* no matrix inputs */
1026 tt_dmasnd
.cbar_dst
= 0x0000; /* no matrix outputs */
1027 tt_dmasnd
.dac_src
= 1; /* connect ADC to DAC, disconnect matrix */
1028 tt_dmasnd
.adc_src
= 3; /* ADC Input = PSG */
1032 static void FalconInit(void)
1034 int divider
, i
, idx
;
1035 const int freq
[8] = {49170, 32780, 24585, 19668, 16390, 12292, 9834, 8195};
1037 /* search a frequency that fits into the allowed error range */
1040 for (i
= 0; i
< ARRAY_SIZE(freq
); i
++)
1041 /* if we will tolerate 3% error 8000Hz->8195Hz (2.38%) would
1042 * be playable without expanding, but that now a kernel runtime
1045 if ((100 * abs(dmasound
.soft
.speed
- freq
[i
]) / freq
[i
]) < catchRadius
)
1048 dmasound
.soft
.speed
= freq
[idx
];
1049 dmasound
.trans_write
= &transFalconNormal
;
1051 dmasound
.trans_write
= &transFalconExpanding
;
1054 dmasound
.hard
= dmasound
.soft
;
1056 if (dmasound
.hard
.size
== 16) {
1057 /* the Falcon can play 16bit samples only in stereo */
1058 dmasound
.hard
.stereo
= 1;
1061 if (dmasound
.hard
.speed
> 49170) {
1062 /* we would need to squeeze the sound, but we won't do that */
1063 dmasound
.hard
.speed
= 49170;
1065 dmasound
.trans_write
= &transFalconNormal
;
1066 } else if (dmasound
.hard
.speed
> 32780) {
1067 dmasound
.hard
.speed
= 49170;
1069 } else if (dmasound
.hard
.speed
> 24585) {
1070 dmasound
.hard
.speed
= 32780;
1072 } else if (dmasound
.hard
.speed
> 19668) {
1073 dmasound
.hard
.speed
= 24585;
1075 } else if (dmasound
.hard
.speed
> 16390) {
1076 dmasound
.hard
.speed
= 19668;
1078 } else if (dmasound
.hard
.speed
> 12292) {
1079 dmasound
.hard
.speed
= 16390;
1081 } else if (dmasound
.hard
.speed
> 9834) {
1082 dmasound
.hard
.speed
= 12292;
1084 } else if (dmasound
.hard
.speed
> 8195) {
1085 dmasound
.hard
.speed
= 9834;
1088 dmasound
.hard
.speed
= 8195;
1091 tt_dmasnd
.int_div
= divider
;
1093 /* Setup Falcon sound DMA for playback */
1094 tt_dmasnd
.int_ctrl
= 0x4; /* Timer A int at play end */
1095 tt_dmasnd
.track_select
= 0x0; /* play 1 track, track 1 */
1096 tt_dmasnd
.cbar_src
= 0x0001; /* DMA(25MHz) --> DAC */
1097 tt_dmasnd
.cbar_dst
= 0x0000;
1098 tt_dmasnd
.rec_track_select
= 0;
1099 tt_dmasnd
.dac_src
= 2; /* connect matrix to DAC */
1100 tt_dmasnd
.adc_src
= 0; /* ADC Input = Mic */
1102 tt_dmasnd
.mode
= (dmasound
.hard
.stereo
?
1103 DMASND_MODE_STEREO
: DMASND_MODE_MONO
) |
1104 ((dmasound
.hard
.size
== 8) ?
1105 DMASND_MODE_8BIT
: DMASND_MODE_16BIT
) |
1108 expand_bal
= -dmasound
.soft
.speed
;
1112 static int FalconSetFormat(int format
)
1115 /* Falcon sound DMA supports 8bit and 16bit modes */
1119 return dmasound
.soft
.format
;
1137 dmasound
.soft
.format
= format
;
1138 dmasound
.soft
.size
= size
;
1139 if (dmasound
.minDev
== SND_DEV_DSP
) {
1140 dmasound
.dsp
.format
= format
;
1141 dmasound
.dsp
.size
= dmasound
.soft
.size
;
1150 /* This is for the Falcon output *attenuation* in 1.5dB steps,
1151 * i.e. output level from 0 to -22.5dB in -1.5dB steps.
1153 #define VOLUME_VOXWARE_TO_ATT(v) \
1154 ((v) < 0 ? 15 : (v) > 100 ? 0 : 15 - (v) * 3 / 20)
1155 #define VOLUME_ATT_TO_VOXWARE(v) (100 - (v) * 20 / 3)
1158 static int FalconSetVolume(int volume
)
1160 dmasound
.volume_left
= VOLUME_VOXWARE_TO_ATT(volume
& 0xff);
1161 dmasound
.volume_right
= VOLUME_VOXWARE_TO_ATT((volume
& 0xff00) >> 8);
1162 tt_dmasnd
.output_atten
= dmasound
.volume_left
<< 8 | dmasound
.volume_right
<< 4;
1163 return VOLUME_ATT_TO_VOXWARE(dmasound
.volume_left
) |
1164 VOLUME_ATT_TO_VOXWARE(dmasound
.volume_right
) << 8;
1168 static void AtaPlayNextFrame(int index
)
1172 /* used by AtaPlay() if all doubts whether there really is something
1173 * to be played are already wiped out.
1175 start
= write_sq
.buffers
[write_sq
.front
];
1176 end
= start
+((write_sq
.count
== index
) ? write_sq
.rear_size
1177 : write_sq
.block_size
);
1178 /* end might not be a legal virtual address. */
1179 DMASNDSetEnd(virt_to_phys(end
- 1) + 1);
1180 DMASNDSetBase(virt_to_phys(start
));
1181 /* Since only an even number of samples per frame can
1182 be played, we might lose one byte here. (TO DO) */
1183 write_sq
.front
= (write_sq
.front
+1) % write_sq
.max_count
;
1185 tt_dmasnd
.ctrl
= DMASND_CTRL_ON
| DMASND_CTRL_REPEAT
;
1189 static void AtaPlay(void)
1191 /* ++TeSche: Note that write_sq.active is no longer just a flag but
1192 * holds the number of frames the DMA is currently programmed for
1193 * instead, may be 0, 1 (currently being played) or 2 (pre-programmed).
1195 * Changes done to write_sq.count and write_sq.active are a bit more
1196 * subtle again so now I must admit I also prefer disabling the irq
1197 * here rather than considering all possible situations. But the point
1198 * is that disabling the irq doesn't have any bad influence on this
1199 * version of the driver as we benefit from having pre-programmed the
1200 * DMA wherever possible: There's no need to reload the DMA at the
1201 * exact time of an interrupt but only at some time while the
1202 * pre-programmed frame is playing!
1204 atari_disable_irq(IRQ_MFP_TIMA
);
1206 if (write_sq
.active
== 2 || /* DMA is 'full' */
1207 write_sq
.count
<= 0) { /* nothing to do */
1208 atari_enable_irq(IRQ_MFP_TIMA
);
1212 if (write_sq
.active
== 0) {
1213 /* looks like there's nothing 'in' the DMA yet, so try
1214 * to put two frames into it (at least one is available).
1216 if (write_sq
.count
== 1 &&
1217 write_sq
.rear_size
< write_sq
.block_size
&&
1218 !write_sq
.syncing
) {
1219 /* hmmm, the only existing frame is not
1220 * yet filled and we're not syncing?
1222 atari_enable_irq(IRQ_MFP_TIMA
);
1225 AtaPlayNextFrame(1);
1226 if (write_sq
.count
== 1) {
1227 /* no more frames */
1228 atari_enable_irq(IRQ_MFP_TIMA
);
1231 if (write_sq
.count
== 2 &&
1232 write_sq
.rear_size
< write_sq
.block_size
&&
1233 !write_sq
.syncing
) {
1234 /* hmmm, there were two frames, but the second
1235 * one is not yet filled and we're not syncing?
1237 atari_enable_irq(IRQ_MFP_TIMA
);
1240 AtaPlayNextFrame(2);
1242 /* there's already a frame being played so we may only stuff
1243 * one new into the DMA, but even if this may be the last
1244 * frame existing the previous one is still on write_sq.count.
1246 if (write_sq
.count
== 2 &&
1247 write_sq
.rear_size
< write_sq
.block_size
&&
1248 !write_sq
.syncing
) {
1249 /* hmmm, the only existing frame is not
1250 * yet filled and we're not syncing?
1252 atari_enable_irq(IRQ_MFP_TIMA
);
1255 AtaPlayNextFrame(2);
1257 atari_enable_irq(IRQ_MFP_TIMA
);
1261 static irqreturn_t
AtaInterrupt(int irq
, void *dummy
)
1264 /* ++TeSche: if you should want to test this... */
1266 if (write_sq
.active
== 2)
1268 /* simulate losing an interrupt */
1273 spin_lock(&dmasound
.lock
);
1274 if (write_sq_ignore_int
&& is_falcon
) {
1275 /* ++TeSche: Falcon only: ignore first irq because it comes
1276 * immediately after starting a frame. after that, irqs come
1277 * (almost) like on the TT.
1279 write_sq_ignore_int
= 0;
1283 if (!write_sq
.active
) {
1284 /* playing was interrupted and sq_reset() has already cleared
1285 * the sq variables, so better don't do anything here.
1287 WAKE_UP(write_sq
.sync_queue
);
1291 /* Probably ;) one frame is finished. Well, in fact it may be that a
1292 * pre-programmed one is also finished because there has been a long
1293 * delay in interrupt delivery and we've completely lost one, but
1294 * there's no way to detect such a situation. In such a case the last
1295 * frame will be played more than once and the situation will recover
1296 * as soon as the irq gets through.
1301 if (!write_sq
.active
) {
1302 tt_dmasnd
.ctrl
= DMASND_CTRL_OFF
;
1303 write_sq_ignore_int
= 1;
1306 WAKE_UP(write_sq
.action_queue
);
1307 /* At least one block of the queue is free now
1308 so wake up a writing process blocked because
1311 if ((write_sq
.active
!= 1) || (write_sq
.count
!= 1))
1312 /* We must be a bit carefully here: write_sq.count indicates the
1313 * number of buffers used and not the number of frames to be
1314 * played. If write_sq.count==1 and write_sq.active==1 that
1315 * means the only remaining frame was already programmed
1316 * earlier (and is currently running) so we mustn't call
1317 * AtaPlay() here, otherwise we'll play one frame too much.
1321 if (!write_sq
.active
) WAKE_UP(write_sq
.sync_queue
);
1322 /* We are not playing after AtaPlay(), so there
1323 is nothing to play any more. Wake up a process
1324 waiting for audio output to drain. */
1325 spin_unlock(&dmasound
.lock
);
1330 /*** Mid level stuff *********************************************************/
1334 * /dev/mixer abstraction
1337 #define RECLEVEL_VOXWARE_TO_GAIN(v) \
1338 ((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
1339 #define RECLEVEL_GAIN_TO_VOXWARE(v) (((v) * 20 + 2) / 3)
1342 static void __init
TTMixerInit(void)
1344 atari_microwire_cmd(MW_LM1992_VOLUME(0));
1345 dmasound
.volume_left
= 0;
1346 atari_microwire_cmd(MW_LM1992_BALLEFT(0));
1347 dmasound
.volume_right
= 0;
1348 atari_microwire_cmd(MW_LM1992_BALRIGHT(0));
1349 atari_microwire_cmd(MW_LM1992_TREBLE(0));
1350 atari_microwire_cmd(MW_LM1992_BASS(0));
1353 static void __init
FalconMixerInit(void)
1355 dmasound
.volume_left
= (tt_dmasnd
.output_atten
& 0xf00) >> 8;
1356 dmasound
.volume_right
= (tt_dmasnd
.output_atten
& 0xf0) >> 4;
1359 static int AtaMixerIoctl(u_int cmd
, u_long arg
)
1362 unsigned long flags
;
1364 case SOUND_MIXER_READ_SPEAKER
:
1365 if (is_falcon
|| MACH_IS_TT
) {
1367 spin_lock_irqsave(&dmasound
.lock
, flags
);
1368 sound_ym
.rd_data_reg_sel
= 14;
1369 porta
= sound_ym
.rd_data_reg_sel
;
1370 spin_unlock_irqrestore(&dmasound
.lock
, flags
);
1371 return IOCTL_OUT(arg
, porta
& 0x40 ? 0 : 100);
1374 case SOUND_MIXER_WRITE_VOLUME
:
1375 IOCTL_IN(arg
, data
);
1376 return IOCTL_OUT(arg
, dmasound_set_volume(data
));
1377 case SOUND_MIXER_WRITE_SPEAKER
:
1378 if (is_falcon
|| MACH_IS_TT
) {
1380 IOCTL_IN(arg
, data
);
1381 spin_lock_irqsave(&dmasound
.lock
, flags
);
1382 sound_ym
.rd_data_reg_sel
= 14;
1383 porta
= (sound_ym
.rd_data_reg_sel
& ~0x40) |
1384 (data
< 50 ? 0x40 : 0);
1385 sound_ym
.wd_data
= porta
;
1386 spin_unlock_irqrestore(&dmasound
.lock
, flags
);
1387 return IOCTL_OUT(arg
, porta
& 0x40 ? 0 : 100);
1394 static int TTMixerIoctl(u_int cmd
, u_long arg
)
1398 case SOUND_MIXER_READ_RECMASK
:
1399 return IOCTL_OUT(arg
, 0);
1400 case SOUND_MIXER_READ_DEVMASK
:
1401 return IOCTL_OUT(arg
,
1402 SOUND_MASK_VOLUME
| SOUND_MASK_TREBLE
| SOUND_MASK_BASS
|
1403 (MACH_IS_TT
? SOUND_MASK_SPEAKER
: 0));
1404 case SOUND_MIXER_READ_STEREODEVS
:
1405 return IOCTL_OUT(arg
, SOUND_MASK_VOLUME
);
1406 case SOUND_MIXER_READ_VOLUME
:
1407 return IOCTL_OUT(arg
,
1408 VOLUME_DB_TO_VOXWARE(dmasound
.volume_left
) |
1409 (VOLUME_DB_TO_VOXWARE(dmasound
.volume_right
) << 8));
1410 case SOUND_MIXER_READ_BASS
:
1411 return IOCTL_OUT(arg
, TONE_DB_TO_VOXWARE(dmasound
.bass
));
1412 case SOUND_MIXER_READ_TREBLE
:
1413 return IOCTL_OUT(arg
, TONE_DB_TO_VOXWARE(dmasound
.treble
));
1414 case SOUND_MIXER_READ_OGAIN
:
1415 return IOCTL_OUT(arg
, GAIN_DB_TO_VOXWARE(dmasound
.gain
));
1416 case SOUND_MIXER_WRITE_BASS
:
1417 IOCTL_IN(arg
, data
);
1418 return IOCTL_OUT(arg
, dmasound_set_bass(data
));
1419 case SOUND_MIXER_WRITE_TREBLE
:
1420 IOCTL_IN(arg
, data
);
1421 return IOCTL_OUT(arg
, dmasound_set_treble(data
));
1422 case SOUND_MIXER_WRITE_OGAIN
:
1423 IOCTL_IN(arg
, data
);
1424 return IOCTL_OUT(arg
, dmasound_set_gain(data
));
1426 return AtaMixerIoctl(cmd
, arg
);
1429 static int FalconMixerIoctl(u_int cmd
, u_long arg
)
1433 case SOUND_MIXER_READ_RECMASK
:
1434 return IOCTL_OUT(arg
, SOUND_MASK_MIC
);
1435 case SOUND_MIXER_READ_DEVMASK
:
1436 return IOCTL_OUT(arg
, SOUND_MASK_VOLUME
| SOUND_MASK_MIC
| SOUND_MASK_SPEAKER
);
1437 case SOUND_MIXER_READ_STEREODEVS
:
1438 return IOCTL_OUT(arg
, SOUND_MASK_VOLUME
| SOUND_MASK_MIC
);
1439 case SOUND_MIXER_READ_VOLUME
:
1440 return IOCTL_OUT(arg
,
1441 VOLUME_ATT_TO_VOXWARE(dmasound
.volume_left
) |
1442 VOLUME_ATT_TO_VOXWARE(dmasound
.volume_right
) << 8);
1443 case SOUND_MIXER_READ_CAPS
:
1444 return IOCTL_OUT(arg
, SOUND_CAP_EXCL_INPUT
);
1445 case SOUND_MIXER_WRITE_MIC
:
1446 IOCTL_IN(arg
, data
);
1447 tt_dmasnd
.input_gain
=
1448 RECLEVEL_VOXWARE_TO_GAIN(data
& 0xff) << 4 |
1449 RECLEVEL_VOXWARE_TO_GAIN(data
>> 8 & 0xff);
1450 /* fall thru, return set value */
1451 case SOUND_MIXER_READ_MIC
:
1452 return IOCTL_OUT(arg
,
1453 RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd
.input_gain
>> 4 & 0xf) |
1454 RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd
.input_gain
& 0xf) << 8);
1456 return AtaMixerIoctl(cmd
, arg
);
1459 static int AtaWriteSqSetup(void)
1461 write_sq_ignore_int
= 0;
1465 static int AtaSqOpen(fmode_t mode
)
1467 write_sq_ignore_int
= 1;
1471 static int TTStateInfo(char *buffer
, size_t space
)
1474 len
+= sprintf(buffer
+len
, "\tvol left %ddB [-40... 0]\n",
1475 dmasound
.volume_left
);
1476 len
+= sprintf(buffer
+len
, "\tvol right %ddB [-40... 0]\n",
1477 dmasound
.volume_right
);
1478 len
+= sprintf(buffer
+len
, "\tbass %ddB [-12...+12]\n",
1480 len
+= sprintf(buffer
+len
, "\ttreble %ddB [-12...+12]\n",
1483 printk(KERN_ERR
"dmasound_atari: overflowed state buffer alloc.\n") ;
1489 static int FalconStateInfo(char *buffer
, size_t space
)
1492 len
+= sprintf(buffer
+len
, "\tvol left %ddB [-22.5 ... 0]\n",
1493 dmasound
.volume_left
);
1494 len
+= sprintf(buffer
+len
, "\tvol right %ddB [-22.5 ... 0]\n",
1495 dmasound
.volume_right
);
1497 printk(KERN_ERR
"dmasound_atari: overflowed state buffer alloc.\n") ;
1504 /*** Machine definitions *****************************************************/
1506 static SETTINGS def_hard_falcon
= {
1513 static SETTINGS def_hard_tt
= {
1520 static SETTINGS def_soft
= {
1527 static MACHINE machTT
= {
1530 .owner
= THIS_MODULE
,
1531 .dma_alloc
= AtaAlloc
,
1532 .dma_free
= AtaFree
,
1533 .irqinit
= AtaIrqInit
,
1535 .irqcleanup
= AtaIrqCleanUp
,
1538 .silence
= TTSilence
,
1539 .setFormat
= TTSetFormat
,
1540 .setVolume
= TTSetVolume
,
1541 .setBass
= AtaSetBass
,
1542 .setTreble
= AtaSetTreble
,
1543 .setGain
= TTSetGain
,
1545 .mixer_init
= TTMixerInit
,
1546 .mixer_ioctl
= TTMixerIoctl
,
1547 .write_sq_setup
= AtaWriteSqSetup
,
1548 .sq_open
= AtaSqOpen
,
1549 .state_info
= TTStateInfo
,
1550 .min_dsp_speed
= 6258,
1551 .version
= ((DMASOUND_ATARI_REVISION
<<8) | DMASOUND_ATARI_EDITION
),
1552 .hardware_afmts
= AFMT_S8
, /* h'ware-supported formats *only* here */
1553 .capabilities
= DSP_CAP_BATCH
/* As per SNDCTL_DSP_GETCAPS */
1556 static MACHINE machFalcon
= {
1559 .dma_alloc
= AtaAlloc
,
1560 .dma_free
= AtaFree
,
1561 .irqinit
= AtaIrqInit
,
1563 .irqcleanup
= AtaIrqCleanUp
,
1566 .silence
= FalconSilence
,
1567 .setFormat
= FalconSetFormat
,
1568 .setVolume
= FalconSetVolume
,
1569 .setBass
= AtaSetBass
,
1570 .setTreble
= AtaSetTreble
,
1572 .mixer_init
= FalconMixerInit
,
1573 .mixer_ioctl
= FalconMixerIoctl
,
1574 .write_sq_setup
= AtaWriteSqSetup
,
1575 .sq_open
= AtaSqOpen
,
1576 .state_info
= FalconStateInfo
,
1577 .min_dsp_speed
= 8195,
1578 .version
= ((DMASOUND_ATARI_REVISION
<<8) | DMASOUND_ATARI_EDITION
),
1579 .hardware_afmts
= (AFMT_S8
| AFMT_S16_BE
), /* h'ware-supported formats *only* here */
1580 .capabilities
= DSP_CAP_BATCH
/* As per SNDCTL_DSP_GETCAPS */
1584 /*** Config & Setup **********************************************************/
1587 static int __init
dmasound_atari_init(void)
1589 if (MACH_IS_ATARI
&& ATARIHW_PRESENT(PCM_8BIT
)) {
1590 if (ATARIHW_PRESENT(CODEC
)) {
1591 dmasound
.mach
= machFalcon
;
1592 dmasound
.mach
.default_soft
= def_soft
;
1593 dmasound
.mach
.default_hard
= def_hard_falcon
;
1595 } else if (ATARIHW_PRESENT(MICROWIRE
)) {
1596 dmasound
.mach
= machTT
;
1597 dmasound
.mach
.default_soft
= def_soft
;
1598 dmasound
.mach
.default_hard
= def_hard_tt
;
1602 if ((st_mfp
.int_en_a
& st_mfp
.int_mk_a
& 0x20) == 0)
1603 return dmasound_init();
1605 printk("DMA sound driver: Timer A interrupt already in use\n");
1612 static void __exit
dmasound_atari_cleanup(void)
1617 module_init(dmasound_atari_init
);
1618 module_exit(dmasound_atari_cleanup
);
1619 MODULE_LICENSE("GPL");