aboutsummaryrefslogtreecommitdiff
path: root/sound/oss/dmasound/dmasound_atari.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/oss/dmasound/dmasound_atari.c')
-rw-r--r--sound/oss/dmasound/dmasound_atari.c1600
1 files changed, 1600 insertions, 0 deletions
diff --git a/sound/oss/dmasound/dmasound_atari.c b/sound/oss/dmasound/dmasound_atari.c
new file mode 100644
index 00000000000..8daaf87664b
--- /dev/null
+++ b/sound/oss/dmasound/dmasound_atari.c
@@ -0,0 +1,1600 @@
+/*
+ * 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, int 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;
+ *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;
+ *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;
+ 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;
+ *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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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, int 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