// SPDX-License-Identifier: GPL-2.0-only /* * 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.
*/
/* ++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_ctx_law(const u_char __user *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 __user *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 __user *)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 __user *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 __user *)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 __user *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 __user *)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 __user *)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 __user *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 __user *)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 __user *)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 __user *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 __user *)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 __user *)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 __user *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 __user *)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 __user *)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;
}
staticint __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. */
st_mfp.tim_ct_a = 0; /* ++roman: Stop timer before programming! */
st_mfp.tim_dt_a = 1; /* Cause interrupt after first event. */
st_mfp.tim_ct_a = 8; /* Turn on event counting. */ /* Register interrupt handler. */ if (request_irq(IRQ_MFP_TIMA, AtaInterrupt, 0, "DMA sound",
AtaInterrupt)) return 0;
st_mfp.int_en_a |= 0x20; /* Turn interrupt on. */
st_mfp.int_mk_a |= 0x20; return 1;
}
staticvoid TTSilence(void)
{
tt_dmasnd.ctrl = DMASND_CTRL_OFF;
atari_microwire_cmd(MW_LM1992_PSG_HIGH); /* mix in PSG signal 1:1 */
}
staticvoid TTInit(void)
{ int mode, i, idx; constint 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;
} elseif (dmasound.hard.speed > 25033) {
dmasound.hard.speed = 50066;
mode = DMASND_MODE_50KHZ;
} elseif (dmasound.hard.speed > 12517) {
dmasound.hard.speed = 25033;
mode = DMASND_MODE_25KHZ;
} elseif (dmasound.hard.speed > 6258) {
dmasound.hard.speed = 12517;
mode = DMASND_MODE_12KHZ;
} else {
dmasound.hard.speed = 6258;
mode = DMASND_MODE_6KHZ;
}
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;
}
staticvoid FalconInit(void)
{ int divider, i, idx; constint 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;
}
staticint 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;
}
/* 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;
}
staticvoid 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)
{ #if 0 /* ++TeSche: if you should want to test this... */ staticint 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; goto out;
}
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); goto out;
}
/* 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. */
out:
spin_unlock(&dmasound.lock); return IRQ_HANDLED;
}
staticint 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;
}
staticint 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;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.