xref: /openbmc/linux/sound/oss/dmasound/dmasound_atari.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/sound/oss/dmasound/dmasound_atari.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Atari TT and Falcon DMA Sound Driver
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  See linux/sound/oss/dmasound/dmasound_core.c for copyright and credits
81da177e4SLinus Torvalds  *  prior to 28/01/2001
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  28/01/2001 [0.1] Iain Sandoe
111da177e4SLinus Torvalds  *		     - added versioning
121da177e4SLinus Torvalds  *		     - put in and populated the hardware_afmts field.
131da177e4SLinus Torvalds  *             [0.2] - put in SNDCTL_DSP_GETCAPS value.
141da177e4SLinus Torvalds  *  01/02/2001 [0.3] - put in default hard/soft settings.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/soundcard.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
231da177e4SLinus Torvalds #include <linux/spinlock.h>
241da177e4SLinus Torvalds #include <linux/interrupt.h>
251da177e4SLinus Torvalds 
267c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
271da177e4SLinus Torvalds #include <asm/atariints.h>
281da177e4SLinus Torvalds #include <asm/atari_stram.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include "dmasound.h"
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define DMASOUND_ATARI_REVISION 0
331da177e4SLinus Torvalds #define DMASOUND_ATARI_EDITION 3
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds extern void atari_microwire_cmd(int cmd);
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds static int is_falcon;
381da177e4SLinus Torvalds static int write_sq_ignore_int;	/* ++TeSche: used for Falcon */
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds static int expand_bal;	/* Balance factor for expanding (not volume!) */
411da177e4SLinus Torvalds static int expand_data;	/* Data for expanding */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /*** Translations ************************************************************/
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /* ++TeSche: radically changed for new expanding purposes...
481da177e4SLinus Torvalds  *
491da177e4SLinus Torvalds  * These two routines now deal with copying/expanding/translating the samples
501da177e4SLinus Torvalds  * from user space into our buffer at the right frequency. They take care about
511da177e4SLinus Torvalds  * how much data there's actually to read, how much buffer space there is and
521da177e4SLinus Torvalds  * to convert samples into the right frequency/encoding. They will only work on
531da177e4SLinus Torvalds  * complete samples so it may happen they leave some bytes in the input stream
541da177e4SLinus Torvalds  * if the user didn't write a multiple of the current sample size. They both
551da177e4SLinus Torvalds  * return the number of bytes they've used from both streams so you may detect
561da177e4SLinus Torvalds  * such a situation. Luckily all programs should be able to cope with that.
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  * I think I've optimized anything as far as one can do in plain C, all
591da177e4SLinus Torvalds  * variables should fit in registers and the loops are really short. There's
601da177e4SLinus Torvalds  * one loop for every possible situation. Writing a more generalized and thus
611da177e4SLinus Torvalds  * parameterized loop would only produce slower code. Feel free to optimize
621da177e4SLinus Torvalds  * this in assembler if you like. :)
631da177e4SLinus Torvalds  *
641da177e4SLinus Torvalds  * I think these routines belong here because they're not yet really hardware
651da177e4SLinus Torvalds  * independent, especially the fact that the Falcon can play 16bit samples
661da177e4SLinus Torvalds  * only in stereo is hardcoded in both of them!
671da177e4SLinus Torvalds  *
681da177e4SLinus Torvalds  * ++geert: split in even more functions (one per format)
691da177e4SLinus Torvalds  */
701da177e4SLinus Torvalds 
71031eb4cdSAl Viro static ssize_t ata_ct_law(const u_char __user *userPtr, size_t userCount,
721da177e4SLinus Torvalds 			  u_char frame[], ssize_t *frameUsed,
731da177e4SLinus Torvalds 			  ssize_t frameLeft);
74031eb4cdSAl Viro static ssize_t ata_ct_s8(const u_char __user *userPtr, size_t userCount,
751da177e4SLinus Torvalds 			 u_char frame[], ssize_t *frameUsed,
761da177e4SLinus Torvalds 			 ssize_t frameLeft);
77031eb4cdSAl Viro static ssize_t ata_ct_u8(const u_char __user *userPtr, size_t userCount,
781da177e4SLinus Torvalds 			 u_char frame[], ssize_t *frameUsed,
791da177e4SLinus Torvalds 			 ssize_t frameLeft);
80031eb4cdSAl Viro static ssize_t ata_ct_s16be(const u_char __user *userPtr, size_t userCount,
811da177e4SLinus Torvalds 			    u_char frame[], ssize_t *frameUsed,
821da177e4SLinus Torvalds 			    ssize_t frameLeft);
83031eb4cdSAl Viro static ssize_t ata_ct_u16be(const u_char __user *userPtr, size_t userCount,
841da177e4SLinus Torvalds 			    u_char frame[], ssize_t *frameUsed,
851da177e4SLinus Torvalds 			    ssize_t frameLeft);
86031eb4cdSAl Viro static ssize_t ata_ct_s16le(const u_char __user *userPtr, size_t userCount,
871da177e4SLinus Torvalds 			    u_char frame[], ssize_t *frameUsed,
881da177e4SLinus Torvalds 			    ssize_t frameLeft);
89031eb4cdSAl Viro static ssize_t ata_ct_u16le(const u_char __user *userPtr, size_t userCount,
901da177e4SLinus Torvalds 			    u_char frame[], ssize_t *frameUsed,
911da177e4SLinus Torvalds 			    ssize_t frameLeft);
92031eb4cdSAl Viro static ssize_t ata_ctx_law(const u_char __user *userPtr, size_t userCount,
931da177e4SLinus Torvalds 			   u_char frame[], ssize_t *frameUsed,
941da177e4SLinus Torvalds 			   ssize_t frameLeft);
95031eb4cdSAl Viro static ssize_t ata_ctx_s8(const u_char __user *userPtr, size_t userCount,
961da177e4SLinus Torvalds 			  u_char frame[], ssize_t *frameUsed,
971da177e4SLinus Torvalds 			  ssize_t frameLeft);
98031eb4cdSAl Viro static ssize_t ata_ctx_u8(const u_char __user *userPtr, size_t userCount,
991da177e4SLinus Torvalds 			  u_char frame[], ssize_t *frameUsed,
1001da177e4SLinus Torvalds 			  ssize_t frameLeft);
101031eb4cdSAl Viro static ssize_t ata_ctx_s16be(const u_char __user *userPtr, size_t userCount,
1021da177e4SLinus Torvalds 			     u_char frame[], ssize_t *frameUsed,
1031da177e4SLinus Torvalds 			     ssize_t frameLeft);
104031eb4cdSAl Viro static ssize_t ata_ctx_u16be(const u_char __user *userPtr, size_t userCount,
1051da177e4SLinus Torvalds 			     u_char frame[], ssize_t *frameUsed,
1061da177e4SLinus Torvalds 			     ssize_t frameLeft);
107031eb4cdSAl Viro static ssize_t ata_ctx_s16le(const u_char __user *userPtr, size_t userCount,
1081da177e4SLinus Torvalds 			     u_char frame[], ssize_t *frameUsed,
1091da177e4SLinus Torvalds 			     ssize_t frameLeft);
110031eb4cdSAl Viro static ssize_t ata_ctx_u16le(const u_char __user *userPtr, size_t userCount,
1111da177e4SLinus Torvalds 			     u_char frame[], ssize_t *frameUsed,
1121da177e4SLinus Torvalds 			     ssize_t frameLeft);
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*** Low level stuff *********************************************************/
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 
1181ef64e67SAl Viro static void *AtaAlloc(unsigned int size, gfp_t flags);
1191da177e4SLinus Torvalds static void AtaFree(void *, unsigned int size);
1201da177e4SLinus Torvalds static int AtaIrqInit(void);
1211da177e4SLinus Torvalds #ifdef MODULE
1221da177e4SLinus Torvalds static void AtaIrqCleanUp(void);
1231da177e4SLinus Torvalds #endif /* MODULE */
1241da177e4SLinus Torvalds static int AtaSetBass(int bass);
1251da177e4SLinus Torvalds static int AtaSetTreble(int treble);
1261da177e4SLinus Torvalds static void TTSilence(void);
1271da177e4SLinus Torvalds static void TTInit(void);
1281da177e4SLinus Torvalds static int TTSetFormat(int format);
1291da177e4SLinus Torvalds static int TTSetVolume(int volume);
1301da177e4SLinus Torvalds static int TTSetGain(int gain);
1311da177e4SLinus Torvalds static void FalconSilence(void);
1321da177e4SLinus Torvalds static void FalconInit(void);
1331da177e4SLinus Torvalds static int FalconSetFormat(int format);
1341da177e4SLinus Torvalds static int FalconSetVolume(int volume);
1351da177e4SLinus Torvalds static void AtaPlayNextFrame(int index);
1361da177e4SLinus Torvalds static void AtaPlay(void);
1377d12e780SDavid Howells static irqreturn_t AtaInterrupt(int irq, void *dummy);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*** Mid level stuff *********************************************************/
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static void TTMixerInit(void);
1421da177e4SLinus Torvalds static void FalconMixerInit(void);
1431da177e4SLinus Torvalds static int AtaMixerIoctl(u_int cmd, u_long arg);
1441da177e4SLinus Torvalds static int TTMixerIoctl(u_int cmd, u_long arg);
1451da177e4SLinus Torvalds static int FalconMixerIoctl(u_int cmd, u_long arg);
1461da177e4SLinus Torvalds static int AtaWriteSqSetup(void);
147aeb5d727SAl Viro static int AtaSqOpen(fmode_t mode);
1481da177e4SLinus Torvalds static int TTStateInfo(char *buffer, size_t space);
1491da177e4SLinus Torvalds static int FalconStateInfo(char *buffer, size_t space);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds /*** Translations ************************************************************/
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 
ata_ct_law(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)155031eb4cdSAl Viro static ssize_t ata_ct_law(const u_char __user *userPtr, size_t userCount,
1561da177e4SLinus Torvalds 			  u_char frame[], ssize_t *frameUsed,
1571da177e4SLinus Torvalds 			  ssize_t frameLeft)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	char *table = dmasound.soft.format == AFMT_MU_LAW ? dmasound_ulaw2dma8
1601da177e4SLinus Torvalds 							  : dmasound_alaw2dma8;
1611da177e4SLinus Torvalds 	ssize_t count, used;
1621da177e4SLinus Torvalds 	u_char *p = &frame[*frameUsed];
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	count = min_t(unsigned long, userCount, frameLeft);
1651da177e4SLinus Torvalds 	if (dmasound.soft.stereo)
1661da177e4SLinus Torvalds 		count &= ~1;
1671da177e4SLinus Torvalds 	used = count;
1681da177e4SLinus Torvalds 	while (count > 0) {
1691da177e4SLinus Torvalds 		u_char data;
1701da177e4SLinus Torvalds 		if (get_user(data, userPtr++))
1711da177e4SLinus Torvalds 			return -EFAULT;
1721da177e4SLinus Torvalds 		*p++ = table[data];
1731da177e4SLinus Torvalds 		count--;
1741da177e4SLinus Torvalds 	}
1751da177e4SLinus Torvalds 	*frameUsed += used;
1761da177e4SLinus Torvalds 	return used;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 
ata_ct_s8(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)180031eb4cdSAl Viro static ssize_t ata_ct_s8(const u_char __user *userPtr, size_t userCount,
1811da177e4SLinus Torvalds 			 u_char frame[], ssize_t *frameUsed,
1821da177e4SLinus Torvalds 			 ssize_t frameLeft)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	ssize_t count, used;
1851da177e4SLinus Torvalds 	void *p = &frame[*frameUsed];
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	count = min_t(unsigned long, userCount, frameLeft);
1881da177e4SLinus Torvalds 	if (dmasound.soft.stereo)
1891da177e4SLinus Torvalds 		count &= ~1;
1901da177e4SLinus Torvalds 	used = count;
1911da177e4SLinus Torvalds 	if (copy_from_user(p, userPtr, count))
1921da177e4SLinus Torvalds 		return -EFAULT;
1931da177e4SLinus Torvalds 	*frameUsed += used;
1941da177e4SLinus Torvalds 	return used;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 
ata_ct_u8(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)198031eb4cdSAl Viro static ssize_t ata_ct_u8(const u_char __user *userPtr, size_t userCount,
1991da177e4SLinus Torvalds 			 u_char frame[], ssize_t *frameUsed,
2001da177e4SLinus Torvalds 			 ssize_t frameLeft)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	ssize_t count, used;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
2051da177e4SLinus Torvalds 		u_char *p = &frame[*frameUsed];
2061da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft);
2071da177e4SLinus Torvalds 		used = count;
2081da177e4SLinus Torvalds 		while (count > 0) {
2091da177e4SLinus Torvalds 			u_char data;
2101da177e4SLinus Torvalds 			if (get_user(data, userPtr++))
2111da177e4SLinus Torvalds 				return -EFAULT;
2121da177e4SLinus Torvalds 			*p++ = data ^ 0x80;
2131da177e4SLinus Torvalds 			count--;
2141da177e4SLinus Torvalds 		}
2151da177e4SLinus Torvalds 	} else {
2161da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
2171da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft)>>1;
2181da177e4SLinus Torvalds 		used = count*2;
2191da177e4SLinus Torvalds 		while (count > 0) {
2201da177e4SLinus Torvalds 			u_short data;
221031eb4cdSAl Viro 			if (get_user(data, (u_short __user *)userPtr))
2221da177e4SLinus Torvalds 				return -EFAULT;
2233756513dSAl Viro 			userPtr += 2;
2241da177e4SLinus Torvalds 			*p++ = data ^ 0x8080;
2251da177e4SLinus Torvalds 			count--;
2261da177e4SLinus Torvalds 		}
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 	*frameUsed += used;
2291da177e4SLinus Torvalds 	return used;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 
ata_ct_s16be(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)233031eb4cdSAl Viro static ssize_t ata_ct_s16be(const u_char __user *userPtr, size_t userCount,
2341da177e4SLinus Torvalds 			    u_char frame[], ssize_t *frameUsed,
2351da177e4SLinus Torvalds 			    ssize_t frameLeft)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	ssize_t count, used;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
2401da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
2411da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft)>>1;
2421da177e4SLinus Torvalds 		used = count*2;
2431da177e4SLinus Torvalds 		while (count > 0) {
2441da177e4SLinus Torvalds 			u_short data;
245031eb4cdSAl Viro 			if (get_user(data, (u_short __user *)userPtr))
2461da177e4SLinus Torvalds 				return -EFAULT;
2473756513dSAl Viro 			userPtr += 2;
2481da177e4SLinus Torvalds 			*p++ = data;
2491da177e4SLinus Torvalds 			*p++ = data;
2501da177e4SLinus Torvalds 			count--;
2511da177e4SLinus Torvalds 		}
2521da177e4SLinus Torvalds 		*frameUsed += used*2;
2531da177e4SLinus Torvalds 	} else {
2541da177e4SLinus Torvalds 		void *p = (u_short *)&frame[*frameUsed];
2551da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft) & ~3;
2561da177e4SLinus Torvalds 		used = count;
2571da177e4SLinus Torvalds 		if (copy_from_user(p, userPtr, count))
2581da177e4SLinus Torvalds 			return -EFAULT;
2591da177e4SLinus Torvalds 		*frameUsed += used;
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 	return used;
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 
ata_ct_u16be(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)265031eb4cdSAl Viro static ssize_t ata_ct_u16be(const u_char __user *userPtr, size_t userCount,
2661da177e4SLinus Torvalds 			    u_char frame[], ssize_t *frameUsed,
2671da177e4SLinus Torvalds 			    ssize_t frameLeft)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	ssize_t count, used;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
2721da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
2731da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft)>>1;
2741da177e4SLinus Torvalds 		used = count*2;
2751da177e4SLinus Torvalds 		while (count > 0) {
2761da177e4SLinus Torvalds 			u_short data;
277031eb4cdSAl Viro 			if (get_user(data, (u_short __user *)userPtr))
2781da177e4SLinus Torvalds 				return -EFAULT;
2793756513dSAl Viro 			userPtr += 2;
2801da177e4SLinus Torvalds 			data ^= 0x8000;
2811da177e4SLinus Torvalds 			*p++ = data;
2821da177e4SLinus Torvalds 			*p++ = data;
2831da177e4SLinus Torvalds 			count--;
2841da177e4SLinus Torvalds 		}
2851da177e4SLinus Torvalds 		*frameUsed += used*2;
2861da177e4SLinus Torvalds 	} else {
2871da177e4SLinus Torvalds 		u_long *p = (u_long *)&frame[*frameUsed];
2881da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft)>>2;
2891da177e4SLinus Torvalds 		used = count*4;
2901da177e4SLinus Torvalds 		while (count > 0) {
291031eb4cdSAl Viro 			u_int data;
292031eb4cdSAl Viro 			if (get_user(data, (u_int __user *)userPtr))
2931da177e4SLinus Torvalds 				return -EFAULT;
2943756513dSAl Viro 			userPtr += 4;
2951da177e4SLinus Torvalds 			*p++ = data ^ 0x80008000;
2961da177e4SLinus Torvalds 			count--;
2971da177e4SLinus Torvalds 		}
2981da177e4SLinus Torvalds 		*frameUsed += used;
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 	return used;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 
ata_ct_s16le(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)304031eb4cdSAl Viro static ssize_t ata_ct_s16le(const u_char __user *userPtr, size_t userCount,
3051da177e4SLinus Torvalds 			    u_char frame[], ssize_t *frameUsed,
3061da177e4SLinus Torvalds 			    ssize_t frameLeft)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	ssize_t count, used;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	count = frameLeft;
3111da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
3121da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
3131da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft)>>1;
3141da177e4SLinus Torvalds 		used = count*2;
3151da177e4SLinus Torvalds 		while (count > 0) {
3161da177e4SLinus Torvalds 			u_short data;
317031eb4cdSAl Viro 			if (get_user(data, (u_short __user *)userPtr))
3181da177e4SLinus Torvalds 				return -EFAULT;
3193756513dSAl Viro 			userPtr += 2;
3201da177e4SLinus Torvalds 			data = le2be16(data);
3211da177e4SLinus Torvalds 			*p++ = data;
3221da177e4SLinus Torvalds 			*p++ = data;
3231da177e4SLinus Torvalds 			count--;
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 		*frameUsed += used*2;
3261da177e4SLinus Torvalds 	} else {
3271da177e4SLinus Torvalds 		u_long *p = (u_long *)&frame[*frameUsed];
3281da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft)>>2;
3291da177e4SLinus Torvalds 		used = count*4;
3301da177e4SLinus Torvalds 		while (count > 0) {
3311da177e4SLinus Torvalds 			u_long data;
332031eb4cdSAl Viro 			if (get_user(data, (u_int __user *)userPtr))
3331da177e4SLinus Torvalds 				return -EFAULT;
3343756513dSAl Viro 			userPtr += 4;
3351da177e4SLinus Torvalds 			data = le2be16dbl(data);
3361da177e4SLinus Torvalds 			*p++ = data;
3371da177e4SLinus Torvalds 			count--;
3381da177e4SLinus Torvalds 		}
3391da177e4SLinus Torvalds 		*frameUsed += used;
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 	return used;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 
ata_ct_u16le(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)345031eb4cdSAl Viro static ssize_t ata_ct_u16le(const u_char __user *userPtr, size_t userCount,
3461da177e4SLinus Torvalds 			    u_char frame[], ssize_t *frameUsed,
3471da177e4SLinus Torvalds 			    ssize_t frameLeft)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	ssize_t count, used;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	count = frameLeft;
3521da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
3531da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
3541da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft)>>1;
3551da177e4SLinus Torvalds 		used = count*2;
3561da177e4SLinus Torvalds 		while (count > 0) {
3571da177e4SLinus Torvalds 			u_short data;
358031eb4cdSAl Viro 			if (get_user(data, (u_short __user *)userPtr))
3591da177e4SLinus Torvalds 				return -EFAULT;
3603756513dSAl Viro 			userPtr += 2;
3611da177e4SLinus Torvalds 			data = le2be16(data) ^ 0x8000;
3621da177e4SLinus Torvalds 			*p++ = data;
3631da177e4SLinus Torvalds 			*p++ = data;
3641da177e4SLinus Torvalds 		}
3651da177e4SLinus Torvalds 		*frameUsed += used*2;
3661da177e4SLinus Torvalds 	} else {
3671da177e4SLinus Torvalds 		u_long *p = (u_long *)&frame[*frameUsed];
3681da177e4SLinus Torvalds 		count = min_t(unsigned long, userCount, frameLeft)>>2;
3691da177e4SLinus Torvalds 		used = count;
3701da177e4SLinus Torvalds 		while (count > 0) {
3711da177e4SLinus Torvalds 			u_long data;
372031eb4cdSAl Viro 			if (get_user(data, (u_int __user *)userPtr))
3731da177e4SLinus Torvalds 				return -EFAULT;
3743756513dSAl Viro 			userPtr += 4;
3751da177e4SLinus Torvalds 			data = le2be16dbl(data) ^ 0x80008000;
3761da177e4SLinus Torvalds 			*p++ = data;
3771da177e4SLinus Torvalds 			count--;
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 		*frameUsed += used;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 	return used;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 
ata_ctx_law(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)385031eb4cdSAl Viro static ssize_t ata_ctx_law(const u_char __user *userPtr, size_t userCount,
3861da177e4SLinus Torvalds 			   u_char frame[], ssize_t *frameUsed,
3871da177e4SLinus Torvalds 			   ssize_t frameLeft)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	char *table = dmasound.soft.format == AFMT_MU_LAW ? dmasound_ulaw2dma8
3901da177e4SLinus Torvalds 							  : dmasound_alaw2dma8;
3911da177e4SLinus Torvalds 	/* this should help gcc to stuff everything into registers */
3921da177e4SLinus Torvalds 	long bal = expand_bal;
3931da177e4SLinus Torvalds 	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
3941da177e4SLinus Torvalds 	ssize_t used, usedf;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	used = userCount;
3971da177e4SLinus Torvalds 	usedf = frameLeft;
3981da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
3991da177e4SLinus Torvalds 		u_char *p = &frame[*frameUsed];
4001da177e4SLinus Torvalds 		u_char data = expand_data;
4011da177e4SLinus Torvalds 		while (frameLeft) {
4021da177e4SLinus Torvalds 			u_char c;
4031da177e4SLinus Torvalds 			if (bal < 0) {
4041da177e4SLinus Torvalds 				if (!userCount)
4051da177e4SLinus Torvalds 					break;
4061da177e4SLinus Torvalds 				if (get_user(c, userPtr++))
4071da177e4SLinus Torvalds 					return -EFAULT;
4081da177e4SLinus Torvalds 				data = table[c];
4091da177e4SLinus Torvalds 				userCount--;
4101da177e4SLinus Torvalds 				bal += hSpeed;
4111da177e4SLinus Torvalds 			}
4121da177e4SLinus Torvalds 			*p++ = data;
4131da177e4SLinus Torvalds 			frameLeft--;
4141da177e4SLinus Torvalds 			bal -= sSpeed;
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 		expand_data = data;
4171da177e4SLinus Torvalds 	} else {
4181da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
4191da177e4SLinus Torvalds 		u_short data = expand_data;
4201da177e4SLinus Torvalds 		while (frameLeft >= 2) {
4211da177e4SLinus Torvalds 			u_char c;
4221da177e4SLinus Torvalds 			if (bal < 0) {
4231da177e4SLinus Torvalds 				if (userCount < 2)
4241da177e4SLinus Torvalds 					break;
4251da177e4SLinus Torvalds 				if (get_user(c, userPtr++))
4261da177e4SLinus Torvalds 					return -EFAULT;
4271da177e4SLinus Torvalds 				data = table[c] << 8;
4281da177e4SLinus Torvalds 				if (get_user(c, userPtr++))
4291da177e4SLinus Torvalds 					return -EFAULT;
4301da177e4SLinus Torvalds 				data |= table[c];
4311da177e4SLinus Torvalds 				userCount -= 2;
4321da177e4SLinus Torvalds 				bal += hSpeed;
4331da177e4SLinus Torvalds 			}
4341da177e4SLinus Torvalds 			*p++ = data;
4351da177e4SLinus Torvalds 			frameLeft -= 2;
4361da177e4SLinus Torvalds 			bal -= sSpeed;
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 		expand_data = data;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 	expand_bal = bal;
4411da177e4SLinus Torvalds 	used -= userCount;
4421da177e4SLinus Torvalds 	*frameUsed += usedf-frameLeft;
4431da177e4SLinus Torvalds 	return used;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 
ata_ctx_s8(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)447031eb4cdSAl Viro static ssize_t ata_ctx_s8(const u_char __user *userPtr, size_t userCount,
4481da177e4SLinus Torvalds 			  u_char frame[], ssize_t *frameUsed,
4491da177e4SLinus Torvalds 			  ssize_t frameLeft)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	/* this should help gcc to stuff everything into registers */
4521da177e4SLinus Torvalds 	long bal = expand_bal;
4531da177e4SLinus Torvalds 	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
4541da177e4SLinus Torvalds 	ssize_t used, usedf;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	used = userCount;
4571da177e4SLinus Torvalds 	usedf = frameLeft;
4581da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
4591da177e4SLinus Torvalds 		u_char *p = &frame[*frameUsed];
4601da177e4SLinus Torvalds 		u_char data = expand_data;
4611da177e4SLinus Torvalds 		while (frameLeft) {
4621da177e4SLinus Torvalds 			if (bal < 0) {
4631da177e4SLinus Torvalds 				if (!userCount)
4641da177e4SLinus Torvalds 					break;
4651da177e4SLinus Torvalds 				if (get_user(data, userPtr++))
4661da177e4SLinus Torvalds 					return -EFAULT;
4671da177e4SLinus Torvalds 				userCount--;
4681da177e4SLinus Torvalds 				bal += hSpeed;
4691da177e4SLinus Torvalds 			}
4701da177e4SLinus Torvalds 			*p++ = data;
4711da177e4SLinus Torvalds 			frameLeft--;
4721da177e4SLinus Torvalds 			bal -= sSpeed;
4731da177e4SLinus Torvalds 		}
4741da177e4SLinus Torvalds 		expand_data = data;
4751da177e4SLinus Torvalds 	} else {
4761da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
4771da177e4SLinus Torvalds 		u_short data = expand_data;
4781da177e4SLinus Torvalds 		while (frameLeft >= 2) {
4791da177e4SLinus Torvalds 			if (bal < 0) {
4801da177e4SLinus Torvalds 				if (userCount < 2)
4811da177e4SLinus Torvalds 					break;
482031eb4cdSAl Viro 				if (get_user(data, (u_short __user *)userPtr))
4831da177e4SLinus Torvalds 					return -EFAULT;
4843756513dSAl Viro 				userPtr += 2;
4851da177e4SLinus Torvalds 				userCount -= 2;
4861da177e4SLinus Torvalds 				bal += hSpeed;
4871da177e4SLinus Torvalds 			}
4881da177e4SLinus Torvalds 			*p++ = data;
4891da177e4SLinus Torvalds 			frameLeft -= 2;
4901da177e4SLinus Torvalds 			bal -= sSpeed;
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 		expand_data = data;
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 	expand_bal = bal;
4951da177e4SLinus Torvalds 	used -= userCount;
4961da177e4SLinus Torvalds 	*frameUsed += usedf-frameLeft;
4971da177e4SLinus Torvalds 	return used;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 
ata_ctx_u8(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)501031eb4cdSAl Viro static ssize_t ata_ctx_u8(const u_char __user *userPtr, size_t userCount,
5021da177e4SLinus Torvalds 			  u_char frame[], ssize_t *frameUsed,
5031da177e4SLinus Torvalds 			  ssize_t frameLeft)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	/* this should help gcc to stuff everything into registers */
5061da177e4SLinus Torvalds 	long bal = expand_bal;
5071da177e4SLinus Torvalds 	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
5081da177e4SLinus Torvalds 	ssize_t used, usedf;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	used = userCount;
5111da177e4SLinus Torvalds 	usedf = frameLeft;
5121da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
5131da177e4SLinus Torvalds 		u_char *p = &frame[*frameUsed];
5141da177e4SLinus Torvalds 		u_char data = expand_data;
5151da177e4SLinus Torvalds 		while (frameLeft) {
5161da177e4SLinus Torvalds 			if (bal < 0) {
5171da177e4SLinus Torvalds 				if (!userCount)
5181da177e4SLinus Torvalds 					break;
5191da177e4SLinus Torvalds 				if (get_user(data, userPtr++))
5201da177e4SLinus Torvalds 					return -EFAULT;
5211da177e4SLinus Torvalds 				data ^= 0x80;
5221da177e4SLinus Torvalds 				userCount--;
5231da177e4SLinus Torvalds 				bal += hSpeed;
5241da177e4SLinus Torvalds 			}
5251da177e4SLinus Torvalds 			*p++ = data;
5261da177e4SLinus Torvalds 			frameLeft--;
5271da177e4SLinus Torvalds 			bal -= sSpeed;
5281da177e4SLinus Torvalds 		}
5291da177e4SLinus Torvalds 		expand_data = data;
5301da177e4SLinus Torvalds 	} else {
5311da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
5321da177e4SLinus Torvalds 		u_short data = expand_data;
5331da177e4SLinus Torvalds 		while (frameLeft >= 2) {
5341da177e4SLinus Torvalds 			if (bal < 0) {
5351da177e4SLinus Torvalds 				if (userCount < 2)
5361da177e4SLinus Torvalds 					break;
537031eb4cdSAl Viro 				if (get_user(data, (u_short __user *)userPtr))
5381da177e4SLinus Torvalds 					return -EFAULT;
5393756513dSAl Viro 				userPtr += 2;
5401da177e4SLinus Torvalds 				data ^= 0x8080;
5411da177e4SLinus Torvalds 				userCount -= 2;
5421da177e4SLinus Torvalds 				bal += hSpeed;
5431da177e4SLinus Torvalds 			}
5441da177e4SLinus Torvalds 			*p++ = data;
5451da177e4SLinus Torvalds 			frameLeft -= 2;
5461da177e4SLinus Torvalds 			bal -= sSpeed;
5471da177e4SLinus Torvalds 		}
5481da177e4SLinus Torvalds 		expand_data = data;
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds 	expand_bal = bal;
5511da177e4SLinus Torvalds 	used -= userCount;
5521da177e4SLinus Torvalds 	*frameUsed += usedf-frameLeft;
5531da177e4SLinus Torvalds 	return used;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 
ata_ctx_s16be(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)557031eb4cdSAl Viro static ssize_t ata_ctx_s16be(const u_char __user *userPtr, size_t userCount,
5581da177e4SLinus Torvalds 			     u_char frame[], ssize_t *frameUsed,
5591da177e4SLinus Torvalds 			     ssize_t frameLeft)
5601da177e4SLinus Torvalds {
5611da177e4SLinus Torvalds 	/* this should help gcc to stuff everything into registers */
5621da177e4SLinus Torvalds 	long bal = expand_bal;
5631da177e4SLinus Torvalds 	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
5641da177e4SLinus Torvalds 	ssize_t used, usedf;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	used = userCount;
5671da177e4SLinus Torvalds 	usedf = frameLeft;
5681da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
5691da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
5701da177e4SLinus Torvalds 		u_short data = expand_data;
5711da177e4SLinus Torvalds 		while (frameLeft >= 4) {
5721da177e4SLinus Torvalds 			if (bal < 0) {
5731da177e4SLinus Torvalds 				if (userCount < 2)
5741da177e4SLinus Torvalds 					break;
575031eb4cdSAl Viro 				if (get_user(data, (u_short __user *)userPtr))
5761da177e4SLinus Torvalds 					return -EFAULT;
5773756513dSAl Viro 				userPtr += 2;
5781da177e4SLinus Torvalds 				userCount -= 2;
5791da177e4SLinus Torvalds 				bal += hSpeed;
5801da177e4SLinus Torvalds 			}
5811da177e4SLinus Torvalds 			*p++ = data;
5821da177e4SLinus Torvalds 			*p++ = data;
5831da177e4SLinus Torvalds 			frameLeft -= 4;
5841da177e4SLinus Torvalds 			bal -= sSpeed;
5851da177e4SLinus Torvalds 		}
5861da177e4SLinus Torvalds 		expand_data = data;
5871da177e4SLinus Torvalds 	} else {
5881da177e4SLinus Torvalds 		u_long *p = (u_long *)&frame[*frameUsed];
5891da177e4SLinus Torvalds 		u_long data = expand_data;
5901da177e4SLinus Torvalds 		while (frameLeft >= 4) {
5911da177e4SLinus Torvalds 			if (bal < 0) {
5921da177e4SLinus Torvalds 				if (userCount < 4)
5931da177e4SLinus Torvalds 					break;
594031eb4cdSAl Viro 				if (get_user(data, (u_int __user *)userPtr))
5951da177e4SLinus Torvalds 					return -EFAULT;
5963756513dSAl Viro 				userPtr += 4;
5971da177e4SLinus Torvalds 				userCount -= 4;
5981da177e4SLinus Torvalds 				bal += hSpeed;
5991da177e4SLinus Torvalds 			}
6001da177e4SLinus Torvalds 			*p++ = data;
6011da177e4SLinus Torvalds 			frameLeft -= 4;
6021da177e4SLinus Torvalds 			bal -= sSpeed;
6031da177e4SLinus Torvalds 		}
6041da177e4SLinus Torvalds 		expand_data = data;
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 	expand_bal = bal;
6071da177e4SLinus Torvalds 	used -= userCount;
6081da177e4SLinus Torvalds 	*frameUsed += usedf-frameLeft;
6091da177e4SLinus Torvalds 	return used;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 
ata_ctx_u16be(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)613031eb4cdSAl Viro static ssize_t ata_ctx_u16be(const u_char __user *userPtr, size_t userCount,
6141da177e4SLinus Torvalds 			     u_char frame[], ssize_t *frameUsed,
6151da177e4SLinus Torvalds 			     ssize_t frameLeft)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	/* this should help gcc to stuff everything into registers */
6181da177e4SLinus Torvalds 	long bal = expand_bal;
6191da177e4SLinus Torvalds 	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
6201da177e4SLinus Torvalds 	ssize_t used, usedf;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	used = userCount;
6231da177e4SLinus Torvalds 	usedf = frameLeft;
6241da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
6251da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
6261da177e4SLinus Torvalds 		u_short data = expand_data;
6271da177e4SLinus Torvalds 		while (frameLeft >= 4) {
6281da177e4SLinus Torvalds 			if (bal < 0) {
6291da177e4SLinus Torvalds 				if (userCount < 2)
6301da177e4SLinus Torvalds 					break;
631031eb4cdSAl Viro 				if (get_user(data, (u_short __user *)userPtr))
6321da177e4SLinus Torvalds 					return -EFAULT;
6333756513dSAl Viro 				userPtr += 2;
6341da177e4SLinus Torvalds 				data ^= 0x8000;
6351da177e4SLinus Torvalds 				userCount -= 2;
6361da177e4SLinus Torvalds 				bal += hSpeed;
6371da177e4SLinus Torvalds 			}
6381da177e4SLinus Torvalds 			*p++ = data;
6391da177e4SLinus Torvalds 			*p++ = data;
6401da177e4SLinus Torvalds 			frameLeft -= 4;
6411da177e4SLinus Torvalds 			bal -= sSpeed;
6421da177e4SLinus Torvalds 		}
6431da177e4SLinus Torvalds 		expand_data = data;
6441da177e4SLinus Torvalds 	} else {
6451da177e4SLinus Torvalds 		u_long *p = (u_long *)&frame[*frameUsed];
6461da177e4SLinus Torvalds 		u_long data = expand_data;
6471da177e4SLinus Torvalds 		while (frameLeft >= 4) {
6481da177e4SLinus Torvalds 			if (bal < 0) {
6491da177e4SLinus Torvalds 				if (userCount < 4)
6501da177e4SLinus Torvalds 					break;
651031eb4cdSAl Viro 				if (get_user(data, (u_int __user *)userPtr))
6521da177e4SLinus Torvalds 					return -EFAULT;
6533756513dSAl Viro 				userPtr += 4;
6541da177e4SLinus Torvalds 				data ^= 0x80008000;
6551da177e4SLinus Torvalds 				userCount -= 4;
6561da177e4SLinus Torvalds 				bal += hSpeed;
6571da177e4SLinus Torvalds 			}
6581da177e4SLinus Torvalds 			*p++ = data;
6591da177e4SLinus Torvalds 			frameLeft -= 4;
6601da177e4SLinus Torvalds 			bal -= sSpeed;
6611da177e4SLinus Torvalds 		}
6621da177e4SLinus Torvalds 		expand_data = data;
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 	expand_bal = bal;
6651da177e4SLinus Torvalds 	used -= userCount;
6661da177e4SLinus Torvalds 	*frameUsed += usedf-frameLeft;
6671da177e4SLinus Torvalds 	return used;
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 
ata_ctx_s16le(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)671031eb4cdSAl Viro static ssize_t ata_ctx_s16le(const u_char __user *userPtr, size_t userCount,
6721da177e4SLinus Torvalds 			     u_char frame[], ssize_t *frameUsed,
6731da177e4SLinus Torvalds 			     ssize_t frameLeft)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds 	/* this should help gcc to stuff everything into registers */
6761da177e4SLinus Torvalds 	long bal = expand_bal;
6771da177e4SLinus Torvalds 	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
6781da177e4SLinus Torvalds 	ssize_t used, usedf;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	used = userCount;
6811da177e4SLinus Torvalds 	usedf = frameLeft;
6821da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
6831da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
6841da177e4SLinus Torvalds 		u_short data = expand_data;
6851da177e4SLinus Torvalds 		while (frameLeft >= 4) {
6861da177e4SLinus Torvalds 			if (bal < 0) {
6871da177e4SLinus Torvalds 				if (userCount < 2)
6881da177e4SLinus Torvalds 					break;
689031eb4cdSAl Viro 				if (get_user(data, (u_short __user *)userPtr))
6901da177e4SLinus Torvalds 					return -EFAULT;
6913756513dSAl Viro 				userPtr += 2;
6921da177e4SLinus Torvalds 				data = le2be16(data);
6931da177e4SLinus Torvalds 				userCount -= 2;
6941da177e4SLinus Torvalds 				bal += hSpeed;
6951da177e4SLinus Torvalds 			}
6961da177e4SLinus Torvalds 			*p++ = data;
6971da177e4SLinus Torvalds 			*p++ = data;
6981da177e4SLinus Torvalds 			frameLeft -= 4;
6991da177e4SLinus Torvalds 			bal -= sSpeed;
7001da177e4SLinus Torvalds 		}
7011da177e4SLinus Torvalds 		expand_data = data;
7021da177e4SLinus Torvalds 	} else {
7031da177e4SLinus Torvalds 		u_long *p = (u_long *)&frame[*frameUsed];
7041da177e4SLinus Torvalds 		u_long data = expand_data;
7051da177e4SLinus Torvalds 		while (frameLeft >= 4) {
7061da177e4SLinus Torvalds 			if (bal < 0) {
7071da177e4SLinus Torvalds 				if (userCount < 4)
7081da177e4SLinus Torvalds 					break;
709031eb4cdSAl Viro 				if (get_user(data, (u_int __user *)userPtr))
7101da177e4SLinus Torvalds 					return -EFAULT;
7113756513dSAl Viro 				userPtr += 4;
7121da177e4SLinus Torvalds 				data = le2be16dbl(data);
7131da177e4SLinus Torvalds 				userCount -= 4;
7141da177e4SLinus Torvalds 				bal += hSpeed;
7151da177e4SLinus Torvalds 			}
7161da177e4SLinus Torvalds 			*p++ = data;
7171da177e4SLinus Torvalds 			frameLeft -= 4;
7181da177e4SLinus Torvalds 			bal -= sSpeed;
7191da177e4SLinus Torvalds 		}
7201da177e4SLinus Torvalds 		expand_data = data;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 	expand_bal = bal;
7231da177e4SLinus Torvalds 	used -= userCount;
7241da177e4SLinus Torvalds 	*frameUsed += usedf-frameLeft;
7251da177e4SLinus Torvalds 	return used;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 
ata_ctx_u16le(const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)729031eb4cdSAl Viro static ssize_t ata_ctx_u16le(const u_char __user *userPtr, size_t userCount,
7301da177e4SLinus Torvalds 			     u_char frame[], ssize_t *frameUsed,
7311da177e4SLinus Torvalds 			     ssize_t frameLeft)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	/* this should help gcc to stuff everything into registers */
7341da177e4SLinus Torvalds 	long bal = expand_bal;
7351da177e4SLinus Torvalds 	long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
7361da177e4SLinus Torvalds 	ssize_t used, usedf;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	used = userCount;
7391da177e4SLinus Torvalds 	usedf = frameLeft;
7401da177e4SLinus Torvalds 	if (!dmasound.soft.stereo) {
7411da177e4SLinus Torvalds 		u_short *p = (u_short *)&frame[*frameUsed];
7421da177e4SLinus Torvalds 		u_short data = expand_data;
7431da177e4SLinus Torvalds 		while (frameLeft >= 4) {
7441da177e4SLinus Torvalds 			if (bal < 0) {
7451da177e4SLinus Torvalds 				if (userCount < 2)
7461da177e4SLinus Torvalds 					break;
747031eb4cdSAl Viro 				if (get_user(data, (u_short __user *)userPtr))
7481da177e4SLinus Torvalds 					return -EFAULT;
7493756513dSAl Viro 				userPtr += 2;
7501da177e4SLinus Torvalds 				data = le2be16(data) ^ 0x8000;
7511da177e4SLinus Torvalds 				userCount -= 2;
7521da177e4SLinus Torvalds 				bal += hSpeed;
7531da177e4SLinus Torvalds 			}
7541da177e4SLinus Torvalds 			*p++ = data;
7551da177e4SLinus Torvalds 			*p++ = data;
7561da177e4SLinus Torvalds 			frameLeft -= 4;
7571da177e4SLinus Torvalds 			bal -= sSpeed;
7581da177e4SLinus Torvalds 		}
7591da177e4SLinus Torvalds 		expand_data = data;
7601da177e4SLinus Torvalds 	} else {
7611da177e4SLinus Torvalds 		u_long *p = (u_long *)&frame[*frameUsed];
7621da177e4SLinus Torvalds 		u_long data = expand_data;
7631da177e4SLinus Torvalds 		while (frameLeft >= 4) {
7641da177e4SLinus Torvalds 			if (bal < 0) {
7651da177e4SLinus Torvalds 				if (userCount < 4)
7661da177e4SLinus Torvalds 					break;
767031eb4cdSAl Viro 				if (get_user(data, (u_int __user *)userPtr))
7681da177e4SLinus Torvalds 					return -EFAULT;
7693756513dSAl Viro 				userPtr += 4;
7701da177e4SLinus Torvalds 				data = le2be16dbl(data) ^ 0x80008000;
7711da177e4SLinus Torvalds 				userCount -= 4;
7721da177e4SLinus Torvalds 				bal += hSpeed;
7731da177e4SLinus Torvalds 			}
7741da177e4SLinus Torvalds 			*p++ = data;
7751da177e4SLinus Torvalds 			frameLeft -= 4;
7761da177e4SLinus Torvalds 			bal -= sSpeed;
7771da177e4SLinus Torvalds 		}
7781da177e4SLinus Torvalds 		expand_data = data;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	expand_bal = bal;
7811da177e4SLinus Torvalds 	used -= userCount;
7821da177e4SLinus Torvalds 	*frameUsed += usedf-frameLeft;
7831da177e4SLinus Torvalds 	return used;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds static TRANS transTTNormal = {
7881da177e4SLinus Torvalds 	.ct_ulaw	= ata_ct_law,
7891da177e4SLinus Torvalds 	.ct_alaw	= ata_ct_law,
7901da177e4SLinus Torvalds 	.ct_s8		= ata_ct_s8,
7911da177e4SLinus Torvalds 	.ct_u8		= ata_ct_u8,
7921da177e4SLinus Torvalds };
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds static TRANS transTTExpanding = {
7951da177e4SLinus Torvalds 	.ct_ulaw	= ata_ctx_law,
7961da177e4SLinus Torvalds 	.ct_alaw	= ata_ctx_law,
7971da177e4SLinus Torvalds 	.ct_s8		= ata_ctx_s8,
7981da177e4SLinus Torvalds 	.ct_u8		= ata_ctx_u8,
7991da177e4SLinus Torvalds };
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds static TRANS transFalconNormal = {
8021da177e4SLinus Torvalds 	.ct_ulaw	= ata_ct_law,
8031da177e4SLinus Torvalds 	.ct_alaw	= ata_ct_law,
8041da177e4SLinus Torvalds 	.ct_s8		= ata_ct_s8,
8051da177e4SLinus Torvalds 	.ct_u8		= ata_ct_u8,
8061da177e4SLinus Torvalds 	.ct_s16be	= ata_ct_s16be,
8071da177e4SLinus Torvalds 	.ct_u16be	= ata_ct_u16be,
8081da177e4SLinus Torvalds 	.ct_s16le	= ata_ct_s16le,
8091da177e4SLinus Torvalds 	.ct_u16le	= ata_ct_u16le
8101da177e4SLinus Torvalds };
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds static TRANS transFalconExpanding = {
8131da177e4SLinus Torvalds 	.ct_ulaw	= ata_ctx_law,
8141da177e4SLinus Torvalds 	.ct_alaw	= ata_ctx_law,
8151da177e4SLinus Torvalds 	.ct_s8		= ata_ctx_s8,
8161da177e4SLinus Torvalds 	.ct_u8		= ata_ctx_u8,
8171da177e4SLinus Torvalds 	.ct_s16be	= ata_ctx_s16be,
8181da177e4SLinus Torvalds 	.ct_u16be	= ata_ctx_u16be,
8191da177e4SLinus Torvalds 	.ct_s16le	= ata_ctx_s16le,
8201da177e4SLinus Torvalds 	.ct_u16le	= ata_ctx_u16le,
8211da177e4SLinus Torvalds };
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds /*** Low level stuff *********************************************************/
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds /*
8291da177e4SLinus Torvalds  * Atari (TT/Falcon)
8301da177e4SLinus Torvalds  */
8311da177e4SLinus Torvalds 
AtaAlloc(unsigned int size,gfp_t flags)8321ef64e67SAl Viro static void *AtaAlloc(unsigned int size, gfp_t flags)
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds 	return atari_stram_alloc(size, "dmasound");
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
AtaFree(void * obj,unsigned int size)8371da177e4SLinus Torvalds static void AtaFree(void *obj, unsigned int size)
8381da177e4SLinus Torvalds {
8391da177e4SLinus Torvalds 	atari_stram_free( obj );
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
AtaIrqInit(void)8421da177e4SLinus Torvalds static int __init AtaIrqInit(void)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	/* Set up timer A. Timer A
8451da177e4SLinus Torvalds 	   will receive a signal upon end of playing from the sound
8461da177e4SLinus Torvalds 	   hardware. Furthermore Timer A is able to count events
8471da177e4SLinus Torvalds 	   and will cause an interrupt after a programmed number
8481da177e4SLinus Torvalds 	   of events. So all we need to keep the music playing is
8491da177e4SLinus Torvalds 	   to provide the sound hardware with new data upon
8501da177e4SLinus Torvalds 	   an interrupt from timer A. */
8513d92e8f3SGeert Uytterhoeven 	st_mfp.tim_ct_a = 0;	/* ++roman: Stop timer before programming! */
8523d92e8f3SGeert Uytterhoeven 	st_mfp.tim_dt_a = 1;	/* Cause interrupt after first event. */
8533d92e8f3SGeert Uytterhoeven 	st_mfp.tim_ct_a = 8;	/* Turn on event counting. */
8541da177e4SLinus Torvalds 	/* Register interrupt handler. */
85580dbb01aSGeert Uytterhoeven 	if (request_irq(IRQ_MFP_TIMA, AtaInterrupt, 0, "DMA sound",
85689bde7b8SGeert Uytterhoeven 			AtaInterrupt))
85789bde7b8SGeert Uytterhoeven 		return 0;
8583d92e8f3SGeert Uytterhoeven 	st_mfp.int_en_a |= 0x20;	/* Turn interrupt on. */
8593d92e8f3SGeert Uytterhoeven 	st_mfp.int_mk_a |= 0x20;
8601da177e4SLinus Torvalds 	return 1;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds #ifdef MODULE
AtaIrqCleanUp(void)8641da177e4SLinus Torvalds static void AtaIrqCleanUp(void)
8651da177e4SLinus Torvalds {
8663d92e8f3SGeert Uytterhoeven 	st_mfp.tim_ct_a = 0;		/* stop timer */
8673d92e8f3SGeert Uytterhoeven 	st_mfp.int_en_a &= ~0x20;	/* turn interrupt off */
8681da177e4SLinus Torvalds 	free_irq(IRQ_MFP_TIMA, AtaInterrupt);
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds #endif /* MODULE */
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds #define TONE_VOXWARE_TO_DB(v) \
8741da177e4SLinus Torvalds 	(((v) < 0) ? -12 : ((v) > 100) ? 12 : ((v) - 50) * 6 / 25)
8751da177e4SLinus Torvalds #define TONE_DB_TO_VOXWARE(v) (((v) * 25 + ((v) > 0 ? 5 : -5)) / 6 + 50)
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 
AtaSetBass(int bass)8781da177e4SLinus Torvalds static int AtaSetBass(int bass)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	dmasound.bass = TONE_VOXWARE_TO_DB(bass);
8811da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_BASS(dmasound.bass));
8821da177e4SLinus Torvalds 	return TONE_DB_TO_VOXWARE(dmasound.bass);
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 
AtaSetTreble(int treble)8861da177e4SLinus Torvalds static int AtaSetTreble(int treble)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	dmasound.treble = TONE_VOXWARE_TO_DB(treble);
8891da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_TREBLE(dmasound.treble));
8901da177e4SLinus Torvalds 	return TONE_DB_TO_VOXWARE(dmasound.treble);
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds /*
8961da177e4SLinus Torvalds  * TT
8971da177e4SLinus Torvalds  */
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 
TTSilence(void)9001da177e4SLinus Torvalds static void TTSilence(void)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	tt_dmasnd.ctrl = DMASND_CTRL_OFF;
9031da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_PSG_HIGH); /* mix in PSG signal 1:1 */
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 
TTInit(void)9071da177e4SLinus Torvalds static void TTInit(void)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	int mode, i, idx;
9101da177e4SLinus Torvalds 	const int freq[4] = {50066, 25033, 12517, 6258};
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	/* search a frequency that fits into the allowed error range */
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	idx = -1;
9151da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(freq); i++)
9161da177e4SLinus Torvalds 		/* this isn't as much useful for a TT than for a Falcon, but
9171da177e4SLinus Torvalds 		 * then it doesn't hurt very much to implement it for a TT too.
9181da177e4SLinus Torvalds 		 */
9191da177e4SLinus Torvalds 		if ((100 * abs(dmasound.soft.speed - freq[i]) / freq[i]) < catchRadius)
9201da177e4SLinus Torvalds 			idx = i;
9211da177e4SLinus Torvalds 	if (idx > -1) {
9221da177e4SLinus Torvalds 		dmasound.soft.speed = freq[idx];
9231da177e4SLinus Torvalds 		dmasound.trans_write = &transTTNormal;
9241da177e4SLinus Torvalds 	} else
9251da177e4SLinus Torvalds 		dmasound.trans_write = &transTTExpanding;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	TTSilence();
9281da177e4SLinus Torvalds 	dmasound.hard = dmasound.soft;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	if (dmasound.hard.speed > 50066) {
9311da177e4SLinus Torvalds 		/* we would need to squeeze the sound, but we won't do that */
9321da177e4SLinus Torvalds 		dmasound.hard.speed = 50066;
9331da177e4SLinus Torvalds 		mode = DMASND_MODE_50KHZ;
9341da177e4SLinus Torvalds 		dmasound.trans_write = &transTTNormal;
9351da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 25033) {
9361da177e4SLinus Torvalds 		dmasound.hard.speed = 50066;
9371da177e4SLinus Torvalds 		mode = DMASND_MODE_50KHZ;
9381da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 12517) {
9391da177e4SLinus Torvalds 		dmasound.hard.speed = 25033;
9401da177e4SLinus Torvalds 		mode = DMASND_MODE_25KHZ;
9411da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 6258) {
9421da177e4SLinus Torvalds 		dmasound.hard.speed = 12517;
9431da177e4SLinus Torvalds 		mode = DMASND_MODE_12KHZ;
9441da177e4SLinus Torvalds 	} else {
9451da177e4SLinus Torvalds 		dmasound.hard.speed = 6258;
9461da177e4SLinus Torvalds 		mode = DMASND_MODE_6KHZ;
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	tt_dmasnd.mode = (dmasound.hard.stereo ?
9501da177e4SLinus Torvalds 			  DMASND_MODE_STEREO : DMASND_MODE_MONO) |
9511da177e4SLinus Torvalds 		DMASND_MODE_8BIT | mode;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	expand_bal = -dmasound.soft.speed;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 
TTSetFormat(int format)9571da177e4SLinus Torvalds static int TTSetFormat(int format)
9581da177e4SLinus Torvalds {
9591da177e4SLinus Torvalds 	/* TT sound DMA supports only 8bit modes */
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	switch (format) {
9621da177e4SLinus Torvalds 	case AFMT_QUERY:
9631da177e4SLinus Torvalds 		return dmasound.soft.format;
9641da177e4SLinus Torvalds 	case AFMT_MU_LAW:
9651da177e4SLinus Torvalds 	case AFMT_A_LAW:
9661da177e4SLinus Torvalds 	case AFMT_S8:
9671da177e4SLinus Torvalds 	case AFMT_U8:
9681da177e4SLinus Torvalds 		break;
9691da177e4SLinus Torvalds 	default:
9701da177e4SLinus Torvalds 		format = AFMT_S8;
9711da177e4SLinus Torvalds 	}
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	dmasound.soft.format = format;
9741da177e4SLinus Torvalds 	dmasound.soft.size = 8;
9751da177e4SLinus Torvalds 	if (dmasound.minDev == SND_DEV_DSP) {
9761da177e4SLinus Torvalds 		dmasound.dsp.format = format;
9771da177e4SLinus Torvalds 		dmasound.dsp.size = 8;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 	TTInit();
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	return format;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds #define VOLUME_VOXWARE_TO_DB(v) \
9861da177e4SLinus Torvalds 	(((v) < 0) ? -40 : ((v) > 100) ? 0 : ((v) * 2) / 5 - 40)
9871da177e4SLinus Torvalds #define VOLUME_DB_TO_VOXWARE(v) ((((v) + 40) * 5 + 1) / 2)
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 
TTSetVolume(int volume)9901da177e4SLinus Torvalds static int TTSetVolume(int volume)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	dmasound.volume_left = VOLUME_VOXWARE_TO_DB(volume & 0xff);
9931da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_BALLEFT(dmasound.volume_left));
9941da177e4SLinus Torvalds 	dmasound.volume_right = VOLUME_VOXWARE_TO_DB((volume & 0xff00) >> 8);
9951da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_BALRIGHT(dmasound.volume_right));
9961da177e4SLinus Torvalds 	return VOLUME_DB_TO_VOXWARE(dmasound.volume_left) |
9971da177e4SLinus Torvalds 	       (VOLUME_DB_TO_VOXWARE(dmasound.volume_right) << 8);
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds #define GAIN_VOXWARE_TO_DB(v) \
10021da177e4SLinus Torvalds 	(((v) < 0) ? -80 : ((v) > 100) ? 0 : ((v) * 4) / 5 - 80)
10031da177e4SLinus Torvalds #define GAIN_DB_TO_VOXWARE(v) ((((v) + 80) * 5 + 1) / 4)
10041da177e4SLinus Torvalds 
TTSetGain(int gain)10051da177e4SLinus Torvalds static int TTSetGain(int gain)
10061da177e4SLinus Torvalds {
10071da177e4SLinus Torvalds 	dmasound.gain = GAIN_VOXWARE_TO_DB(gain);
10081da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_VOLUME(dmasound.gain));
10091da177e4SLinus Torvalds 	return GAIN_DB_TO_VOXWARE(dmasound.gain);
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds /*
10151da177e4SLinus Torvalds  * Falcon
10161da177e4SLinus Torvalds  */
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 
FalconSilence(void)10191da177e4SLinus Torvalds static void FalconSilence(void)
10201da177e4SLinus Torvalds {
10211da177e4SLinus Torvalds 	/* stop playback, set sample rate 50kHz for PSG sound */
10221da177e4SLinus Torvalds 	tt_dmasnd.ctrl = DMASND_CTRL_OFF;
10231da177e4SLinus Torvalds 	tt_dmasnd.mode = DMASND_MODE_50KHZ | DMASND_MODE_STEREO | DMASND_MODE_8BIT;
10241da177e4SLinus Torvalds 	tt_dmasnd.int_div = 0; /* STE compatible divider */
10251da177e4SLinus Torvalds 	tt_dmasnd.int_ctrl = 0x0;
10261da177e4SLinus Torvalds 	tt_dmasnd.cbar_src = 0x0000; /* no matrix inputs */
10271da177e4SLinus Torvalds 	tt_dmasnd.cbar_dst = 0x0000; /* no matrix outputs */
10281da177e4SLinus Torvalds 	tt_dmasnd.dac_src = 1; /* connect ADC to DAC, disconnect matrix */
10291da177e4SLinus Torvalds 	tt_dmasnd.adc_src = 3; /* ADC Input = PSG */
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 
FalconInit(void)10331da177e4SLinus Torvalds static void FalconInit(void)
10341da177e4SLinus Torvalds {
10351da177e4SLinus Torvalds 	int divider, i, idx;
10361da177e4SLinus Torvalds 	const int freq[8] = {49170, 32780, 24585, 19668, 16390, 12292, 9834, 8195};
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	/* search a frequency that fits into the allowed error range */
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	idx = -1;
10411da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(freq); i++)
10421da177e4SLinus Torvalds 		/* if we will tolerate 3% error 8000Hz->8195Hz (2.38%) would
10431da177e4SLinus Torvalds 		 * be playable without expanding, but that now a kernel runtime
10441da177e4SLinus Torvalds 		 * option
10451da177e4SLinus Torvalds 		 */
10461da177e4SLinus Torvalds 		if ((100 * abs(dmasound.soft.speed - freq[i]) / freq[i]) < catchRadius)
10471da177e4SLinus Torvalds 			idx = i;
10481da177e4SLinus Torvalds 	if (idx > -1) {
10491da177e4SLinus Torvalds 		dmasound.soft.speed = freq[idx];
10501da177e4SLinus Torvalds 		dmasound.trans_write = &transFalconNormal;
10511da177e4SLinus Torvalds 	} else
10521da177e4SLinus Torvalds 		dmasound.trans_write = &transFalconExpanding;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	FalconSilence();
10551da177e4SLinus Torvalds 	dmasound.hard = dmasound.soft;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	if (dmasound.hard.size == 16) {
10581da177e4SLinus Torvalds 		/* the Falcon can play 16bit samples only in stereo */
10591da177e4SLinus Torvalds 		dmasound.hard.stereo = 1;
10601da177e4SLinus Torvalds 	}
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	if (dmasound.hard.speed > 49170) {
10631da177e4SLinus Torvalds 		/* we would need to squeeze the sound, but we won't do that */
10641da177e4SLinus Torvalds 		dmasound.hard.speed = 49170;
10651da177e4SLinus Torvalds 		divider = 1;
10661da177e4SLinus Torvalds 		dmasound.trans_write = &transFalconNormal;
10671da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 32780) {
10681da177e4SLinus Torvalds 		dmasound.hard.speed = 49170;
10691da177e4SLinus Torvalds 		divider = 1;
10701da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 24585) {
10711da177e4SLinus Torvalds 		dmasound.hard.speed = 32780;
10721da177e4SLinus Torvalds 		divider = 2;
10731da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 19668) {
10741da177e4SLinus Torvalds 		dmasound.hard.speed = 24585;
10751da177e4SLinus Torvalds 		divider = 3;
10761da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 16390) {
10771da177e4SLinus Torvalds 		dmasound.hard.speed = 19668;
10781da177e4SLinus Torvalds 		divider = 4;
10791da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 12292) {
10801da177e4SLinus Torvalds 		dmasound.hard.speed = 16390;
10811da177e4SLinus Torvalds 		divider = 5;
10821da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 9834) {
10831da177e4SLinus Torvalds 		dmasound.hard.speed = 12292;
10841da177e4SLinus Torvalds 		divider = 7;
10851da177e4SLinus Torvalds 	} else if (dmasound.hard.speed > 8195) {
10861da177e4SLinus Torvalds 		dmasound.hard.speed = 9834;
10871da177e4SLinus Torvalds 		divider = 9;
10881da177e4SLinus Torvalds 	} else {
10891da177e4SLinus Torvalds 		dmasound.hard.speed = 8195;
10901da177e4SLinus Torvalds 		divider = 11;
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds 	tt_dmasnd.int_div = divider;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	/* Setup Falcon sound DMA for playback */
10951da177e4SLinus Torvalds 	tt_dmasnd.int_ctrl = 0x4; /* Timer A int at play end */
10961da177e4SLinus Torvalds 	tt_dmasnd.track_select = 0x0; /* play 1 track, track 1 */
10971da177e4SLinus Torvalds 	tt_dmasnd.cbar_src = 0x0001; /* DMA(25MHz) --> DAC */
10981da177e4SLinus Torvalds 	tt_dmasnd.cbar_dst = 0x0000;
10991da177e4SLinus Torvalds 	tt_dmasnd.rec_track_select = 0;
11001da177e4SLinus Torvalds 	tt_dmasnd.dac_src = 2; /* connect matrix to DAC */
11011da177e4SLinus Torvalds 	tt_dmasnd.adc_src = 0; /* ADC Input = Mic */
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	tt_dmasnd.mode = (dmasound.hard.stereo ?
11041da177e4SLinus Torvalds 			  DMASND_MODE_STEREO : DMASND_MODE_MONO) |
11051da177e4SLinus Torvalds 		((dmasound.hard.size == 8) ?
11061da177e4SLinus Torvalds 		 DMASND_MODE_8BIT : DMASND_MODE_16BIT) |
11071da177e4SLinus Torvalds 		DMASND_MODE_6KHZ;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	expand_bal = -dmasound.soft.speed;
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 
FalconSetFormat(int format)11131da177e4SLinus Torvalds static int FalconSetFormat(int format)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	int size;
11161da177e4SLinus Torvalds 	/* Falcon sound DMA supports 8bit and 16bit modes */
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	switch (format) {
11191da177e4SLinus Torvalds 	case AFMT_QUERY:
11201da177e4SLinus Torvalds 		return dmasound.soft.format;
11211da177e4SLinus Torvalds 	case AFMT_MU_LAW:
11221da177e4SLinus Torvalds 	case AFMT_A_LAW:
11231da177e4SLinus Torvalds 	case AFMT_U8:
11241da177e4SLinus Torvalds 	case AFMT_S8:
11251da177e4SLinus Torvalds 		size = 8;
11261da177e4SLinus Torvalds 		break;
11271da177e4SLinus Torvalds 	case AFMT_S16_BE:
11281da177e4SLinus Torvalds 	case AFMT_U16_BE:
11291da177e4SLinus Torvalds 	case AFMT_S16_LE:
11301da177e4SLinus Torvalds 	case AFMT_U16_LE:
11311da177e4SLinus Torvalds 		size = 16;
11321da177e4SLinus Torvalds 		break;
11331da177e4SLinus Torvalds 	default: /* :-) */
11341da177e4SLinus Torvalds 		size = 8;
11351da177e4SLinus Torvalds 		format = AFMT_S8;
11361da177e4SLinus Torvalds 	}
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	dmasound.soft.format = format;
11391da177e4SLinus Torvalds 	dmasound.soft.size = size;
11401da177e4SLinus Torvalds 	if (dmasound.minDev == SND_DEV_DSP) {
11411da177e4SLinus Torvalds 		dmasound.dsp.format = format;
11421da177e4SLinus Torvalds 		dmasound.dsp.size = dmasound.soft.size;
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	FalconInit();
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	return format;
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds /* This is for the Falcon output *attenuation* in 1.5dB steps,
11521da177e4SLinus Torvalds  * i.e. output level from 0 to -22.5dB in -1.5dB steps.
11531da177e4SLinus Torvalds  */
11541da177e4SLinus Torvalds #define VOLUME_VOXWARE_TO_ATT(v) \
11551da177e4SLinus Torvalds 	((v) < 0 ? 15 : (v) > 100 ? 0 : 15 - (v) * 3 / 20)
11561da177e4SLinus Torvalds #define VOLUME_ATT_TO_VOXWARE(v) (100 - (v) * 20 / 3)
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 
FalconSetVolume(int volume)11591da177e4SLinus Torvalds static int FalconSetVolume(int volume)
11601da177e4SLinus Torvalds {
11611da177e4SLinus Torvalds 	dmasound.volume_left = VOLUME_VOXWARE_TO_ATT(volume & 0xff);
11621da177e4SLinus Torvalds 	dmasound.volume_right = VOLUME_VOXWARE_TO_ATT((volume & 0xff00) >> 8);
11631da177e4SLinus Torvalds 	tt_dmasnd.output_atten = dmasound.volume_left << 8 | dmasound.volume_right << 4;
11641da177e4SLinus Torvalds 	return VOLUME_ATT_TO_VOXWARE(dmasound.volume_left) |
11651da177e4SLinus Torvalds 	       VOLUME_ATT_TO_VOXWARE(dmasound.volume_right) << 8;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 
AtaPlayNextFrame(int index)11691da177e4SLinus Torvalds static void AtaPlayNextFrame(int index)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds 	char *start, *end;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	/* used by AtaPlay() if all doubts whether there really is something
11741da177e4SLinus Torvalds 	 * to be played are already wiped out.
11751da177e4SLinus Torvalds 	 */
11761da177e4SLinus Torvalds 	start = write_sq.buffers[write_sq.front];
11771da177e4SLinus Torvalds 	end = start+((write_sq.count == index) ? write_sq.rear_size
11781da177e4SLinus Torvalds 					       : write_sq.block_size);
11791da177e4SLinus Torvalds 	/* end might not be a legal virtual address. */
11801da177e4SLinus Torvalds 	DMASNDSetEnd(virt_to_phys(end - 1) + 1);
11811da177e4SLinus Torvalds 	DMASNDSetBase(virt_to_phys(start));
11821da177e4SLinus Torvalds 	/* Since only an even number of samples per frame can
11831da177e4SLinus Torvalds 	   be played, we might lose one byte here. (TO DO) */
11841da177e4SLinus Torvalds 	write_sq.front = (write_sq.front+1) % write_sq.max_count;
11851da177e4SLinus Torvalds 	write_sq.active++;
11861da177e4SLinus Torvalds 	tt_dmasnd.ctrl = DMASND_CTRL_ON | DMASND_CTRL_REPEAT;
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 
AtaPlay(void)11901da177e4SLinus Torvalds static void AtaPlay(void)
11911da177e4SLinus Torvalds {
11921da177e4SLinus Torvalds 	/* ++TeSche: Note that write_sq.active is no longer just a flag but
11931da177e4SLinus Torvalds 	 * holds the number of frames the DMA is currently programmed for
11941da177e4SLinus Torvalds 	 * instead, may be 0, 1 (currently being played) or 2 (pre-programmed).
11951da177e4SLinus Torvalds 	 *
11961da177e4SLinus Torvalds 	 * Changes done to write_sq.count and write_sq.active are a bit more
11971da177e4SLinus Torvalds 	 * subtle again so now I must admit I also prefer disabling the irq
11981da177e4SLinus Torvalds 	 * here rather than considering all possible situations. But the point
11991da177e4SLinus Torvalds 	 * is that disabling the irq doesn't have any bad influence on this
12001da177e4SLinus Torvalds 	 * version of the driver as we benefit from having pre-programmed the
12011da177e4SLinus Torvalds 	 * DMA wherever possible: There's no need to reload the DMA at the
12021da177e4SLinus Torvalds 	 * exact time of an interrupt but only at some time while the
12031da177e4SLinus Torvalds 	 * pre-programmed frame is playing!
12041da177e4SLinus Torvalds 	 */
12051da177e4SLinus Torvalds 	atari_disable_irq(IRQ_MFP_TIMA);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	if (write_sq.active == 2 ||	/* DMA is 'full' */
12081da177e4SLinus Torvalds 	    write_sq.count <= 0) {	/* nothing to do */
12091da177e4SLinus Torvalds 		atari_enable_irq(IRQ_MFP_TIMA);
12101da177e4SLinus Torvalds 		return;
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	if (write_sq.active == 0) {
12141da177e4SLinus Torvalds 		/* looks like there's nothing 'in' the DMA yet, so try
12151da177e4SLinus Torvalds 		 * to put two frames into it (at least one is available).
12161da177e4SLinus Torvalds 		 */
12171da177e4SLinus Torvalds 		if (write_sq.count == 1 &&
12181da177e4SLinus Torvalds 		    write_sq.rear_size < write_sq.block_size &&
12191da177e4SLinus Torvalds 		    !write_sq.syncing) {
12201da177e4SLinus Torvalds 			/* hmmm, the only existing frame is not
12211da177e4SLinus Torvalds 			 * yet filled and we're not syncing?
12221da177e4SLinus Torvalds 			 */
12231da177e4SLinus Torvalds 			atari_enable_irq(IRQ_MFP_TIMA);
12241da177e4SLinus Torvalds 			return;
12251da177e4SLinus Torvalds 		}
12261da177e4SLinus Torvalds 		AtaPlayNextFrame(1);
12271da177e4SLinus Torvalds 		if (write_sq.count == 1) {
12281da177e4SLinus Torvalds 			/* no more frames */
12291da177e4SLinus Torvalds 			atari_enable_irq(IRQ_MFP_TIMA);
12301da177e4SLinus Torvalds 			return;
12311da177e4SLinus Torvalds 		}
12321da177e4SLinus Torvalds 		if (write_sq.count == 2 &&
12331da177e4SLinus Torvalds 		    write_sq.rear_size < write_sq.block_size &&
12341da177e4SLinus Torvalds 		    !write_sq.syncing) {
12351da177e4SLinus Torvalds 			/* hmmm, there were two frames, but the second
12361da177e4SLinus Torvalds 			 * one is not yet filled and we're not syncing?
12371da177e4SLinus Torvalds 			 */
12381da177e4SLinus Torvalds 			atari_enable_irq(IRQ_MFP_TIMA);
12391da177e4SLinus Torvalds 			return;
12401da177e4SLinus Torvalds 		}
12411da177e4SLinus Torvalds 		AtaPlayNextFrame(2);
12421da177e4SLinus Torvalds 	} else {
12431da177e4SLinus Torvalds 		/* there's already a frame being played so we may only stuff
12441da177e4SLinus Torvalds 		 * one new into the DMA, but even if this may be the last
12451da177e4SLinus Torvalds 		 * frame existing the previous one is still on write_sq.count.
12461da177e4SLinus Torvalds 		 */
12471da177e4SLinus Torvalds 		if (write_sq.count == 2 &&
12481da177e4SLinus Torvalds 		    write_sq.rear_size < write_sq.block_size &&
12491da177e4SLinus Torvalds 		    !write_sq.syncing) {
12501da177e4SLinus Torvalds 			/* hmmm, the only existing frame is not
12511da177e4SLinus Torvalds 			 * yet filled and we're not syncing?
12521da177e4SLinus Torvalds 			 */
12531da177e4SLinus Torvalds 			atari_enable_irq(IRQ_MFP_TIMA);
12541da177e4SLinus Torvalds 			return;
12551da177e4SLinus Torvalds 		}
12561da177e4SLinus Torvalds 		AtaPlayNextFrame(2);
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds 	atari_enable_irq(IRQ_MFP_TIMA);
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 
AtaInterrupt(int irq,void * dummy)12627d12e780SDavid Howells static irqreturn_t AtaInterrupt(int irq, void *dummy)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds #if 0
12651da177e4SLinus Torvalds 	/* ++TeSche: if you should want to test this... */
12661da177e4SLinus Torvalds 	static int cnt;
12671da177e4SLinus Torvalds 	if (write_sq.active == 2)
12681da177e4SLinus Torvalds 		if (++cnt == 10) {
12691da177e4SLinus Torvalds 			/* simulate losing an interrupt */
12701da177e4SLinus Torvalds 			cnt = 0;
12711da177e4SLinus Torvalds 			return IRQ_HANDLED;
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds #endif
12741da177e4SLinus Torvalds 	spin_lock(&dmasound.lock);
12751da177e4SLinus Torvalds 	if (write_sq_ignore_int && is_falcon) {
12761da177e4SLinus Torvalds 		/* ++TeSche: Falcon only: ignore first irq because it comes
12771da177e4SLinus Torvalds 		 * immediately after starting a frame. after that, irqs come
12781da177e4SLinus Torvalds 		 * (almost) like on the TT.
12791da177e4SLinus Torvalds 		 */
12801da177e4SLinus Torvalds 		write_sq_ignore_int = 0;
12811efddcc9SJulia Lawall 		goto out;
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	if (!write_sq.active) {
12851da177e4SLinus Torvalds 		/* playing was interrupted and sq_reset() has already cleared
12861da177e4SLinus Torvalds 		 * the sq variables, so better don't do anything here.
12871da177e4SLinus Torvalds 		 */
12881da177e4SLinus Torvalds 		WAKE_UP(write_sq.sync_queue);
12891efddcc9SJulia Lawall 		goto out;
12901da177e4SLinus Torvalds 	}
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	/* Probably ;) one frame is finished. Well, in fact it may be that a
12931da177e4SLinus Torvalds 	 * pre-programmed one is also finished because there has been a long
12941da177e4SLinus Torvalds 	 * delay in interrupt delivery and we've completely lost one, but
12951da177e4SLinus Torvalds 	 * there's no way to detect such a situation. In such a case the last
12961da177e4SLinus Torvalds 	 * frame will be played more than once and the situation will recover
12971da177e4SLinus Torvalds 	 * as soon as the irq gets through.
12981da177e4SLinus Torvalds 	 */
12991da177e4SLinus Torvalds 	write_sq.count--;
13001da177e4SLinus Torvalds 	write_sq.active--;
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	if (!write_sq.active) {
13031da177e4SLinus Torvalds 		tt_dmasnd.ctrl = DMASND_CTRL_OFF;
13041da177e4SLinus Torvalds 		write_sq_ignore_int = 1;
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	WAKE_UP(write_sq.action_queue);
13081da177e4SLinus Torvalds 	/* At least one block of the queue is free now
13091da177e4SLinus Torvalds 	   so wake up a writing process blocked because
13101da177e4SLinus Torvalds 	   of a full queue. */
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	if ((write_sq.active != 1) || (write_sq.count != 1))
13131da177e4SLinus Torvalds 		/* We must be a bit carefully here: write_sq.count indicates the
13141da177e4SLinus Torvalds 		 * number of buffers used and not the number of frames to be
13151da177e4SLinus Torvalds 		 * played. If write_sq.count==1 and write_sq.active==1 that
13161da177e4SLinus Torvalds 		 * means the only remaining frame was already programmed
13171da177e4SLinus Torvalds 		 * earlier (and is currently running) so we mustn't call
13181da177e4SLinus Torvalds 		 * AtaPlay() here, otherwise we'll play one frame too much.
13191da177e4SLinus Torvalds 		 */
13201da177e4SLinus Torvalds 		AtaPlay();
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	if (!write_sq.active) WAKE_UP(write_sq.sync_queue);
13231da177e4SLinus Torvalds 	/* We are not playing after AtaPlay(), so there
13241da177e4SLinus Torvalds 	   is nothing to play any more. Wake up a process
13251da177e4SLinus Torvalds 	   waiting for audio output to drain. */
13261efddcc9SJulia Lawall out:
13271da177e4SLinus Torvalds 	spin_unlock(&dmasound.lock);
13281da177e4SLinus Torvalds 	return IRQ_HANDLED;
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /*** Mid level stuff *********************************************************/
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds /*
13361da177e4SLinus Torvalds  * /dev/mixer abstraction
13371da177e4SLinus Torvalds  */
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds #define RECLEVEL_VOXWARE_TO_GAIN(v)	\
13401da177e4SLinus Torvalds 	((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
13411da177e4SLinus Torvalds #define RECLEVEL_GAIN_TO_VOXWARE(v)	(((v) * 20 + 2) / 3)
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 
TTMixerInit(void)13441da177e4SLinus Torvalds static void __init TTMixerInit(void)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_VOLUME(0));
13471da177e4SLinus Torvalds 	dmasound.volume_left = 0;
13481da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_BALLEFT(0));
13491da177e4SLinus Torvalds 	dmasound.volume_right = 0;
13501da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_BALRIGHT(0));
13511da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_TREBLE(0));
13521da177e4SLinus Torvalds 	atari_microwire_cmd(MW_LM1992_BASS(0));
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds 
FalconMixerInit(void)13551da177e4SLinus Torvalds static void __init FalconMixerInit(void)
13561da177e4SLinus Torvalds {
13571da177e4SLinus Torvalds 	dmasound.volume_left = (tt_dmasnd.output_atten & 0xf00) >> 8;
13581da177e4SLinus Torvalds 	dmasound.volume_right = (tt_dmasnd.output_atten & 0xf0) >> 4;
13591da177e4SLinus Torvalds }
13601da177e4SLinus Torvalds 
AtaMixerIoctl(u_int cmd,u_long arg)13611da177e4SLinus Torvalds static int AtaMixerIoctl(u_int cmd, u_long arg)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	int data;
13641da177e4SLinus Torvalds 	unsigned long flags;
13651da177e4SLinus Torvalds 	switch (cmd) {
13661da177e4SLinus Torvalds 	    case SOUND_MIXER_READ_SPEAKER:
13671da177e4SLinus Torvalds 		    if (is_falcon || MACH_IS_TT) {
13681da177e4SLinus Torvalds 			    int porta;
13691da177e4SLinus Torvalds 			    spin_lock_irqsave(&dmasound.lock, flags);
13701da177e4SLinus Torvalds 			    sound_ym.rd_data_reg_sel = 14;
13711da177e4SLinus Torvalds 			    porta = sound_ym.rd_data_reg_sel;
13721da177e4SLinus Torvalds 			    spin_unlock_irqrestore(&dmasound.lock, flags);
13731da177e4SLinus Torvalds 			    return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
13741da177e4SLinus Torvalds 		    }
13751da177e4SLinus Torvalds 		    break;
13761da177e4SLinus Torvalds 	    case SOUND_MIXER_WRITE_VOLUME:
13771da177e4SLinus Torvalds 		    IOCTL_IN(arg, data);
13781da177e4SLinus Torvalds 		    return IOCTL_OUT(arg, dmasound_set_volume(data));
13791da177e4SLinus Torvalds 	    case SOUND_MIXER_WRITE_SPEAKER:
13801da177e4SLinus Torvalds 		    if (is_falcon || MACH_IS_TT) {
13811da177e4SLinus Torvalds 			    int porta;
13821da177e4SLinus Torvalds 			    IOCTL_IN(arg, data);
13831da177e4SLinus Torvalds 			    spin_lock_irqsave(&dmasound.lock, flags);
13841da177e4SLinus Torvalds 			    sound_ym.rd_data_reg_sel = 14;
13851da177e4SLinus Torvalds 			    porta = (sound_ym.rd_data_reg_sel & ~0x40) |
13861da177e4SLinus Torvalds 				    (data < 50 ? 0x40 : 0);
13871da177e4SLinus Torvalds 			    sound_ym.wd_data = porta;
13881da177e4SLinus Torvalds 			    spin_unlock_irqrestore(&dmasound.lock, flags);
13891da177e4SLinus Torvalds 			    return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
13901da177e4SLinus Torvalds 		    }
13911da177e4SLinus Torvalds 	}
13921da177e4SLinus Torvalds 	return -EINVAL;
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 
TTMixerIoctl(u_int cmd,u_long arg)13961da177e4SLinus Torvalds static int TTMixerIoctl(u_int cmd, u_long arg)
13971da177e4SLinus Torvalds {
13981da177e4SLinus Torvalds 	int data;
13991da177e4SLinus Torvalds 	switch (cmd) {
14001da177e4SLinus Torvalds 	    case SOUND_MIXER_READ_RECMASK:
14011da177e4SLinus Torvalds 		return IOCTL_OUT(arg, 0);
14021da177e4SLinus Torvalds 	    case SOUND_MIXER_READ_DEVMASK:
14031da177e4SLinus Torvalds 		return IOCTL_OUT(arg,
14041da177e4SLinus Torvalds 				 SOUND_MASK_VOLUME | SOUND_MASK_TREBLE | SOUND_MASK_BASS |
14051da177e4SLinus Torvalds 				 (MACH_IS_TT ? SOUND_MASK_SPEAKER : 0));
14061da177e4SLinus Torvalds 	    case SOUND_MIXER_READ_STEREODEVS:
14071da177e4SLinus Torvalds 		return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
14081da177e4SLinus Torvalds 	    case SOUND_MIXER_READ_VOLUME:
14091da177e4SLinus Torvalds 		return IOCTL_OUT(arg,
14101da177e4SLinus Torvalds 				 VOLUME_DB_TO_VOXWARE(dmasound.volume_left) |
14111da177e4SLinus Torvalds 				 (VOLUME_DB_TO_VOXWARE(dmasound.volume_right) << 8));
14121da177e4SLinus Torvalds 	    case SOUND_MIXER_READ_BASS:
14131da177e4SLinus Torvalds 		return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(dmasound.bass));
14141da177e4SLinus Torvalds 	    case SOUND_MIXER_READ_TREBLE:
14151da177e4SLinus Torvalds 		return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(dmasound.treble));
14161da177e4SLinus Torvalds 	    case SOUND_MIXER_READ_OGAIN:
14171da177e4SLinus Torvalds 		return IOCTL_OUT(arg, GAIN_DB_TO_VOXWARE(dmasound.gain));
14181da177e4SLinus Torvalds 	    case SOUND_MIXER_WRITE_BASS:
14191da177e4SLinus Torvalds 		IOCTL_IN(arg, data);
14201da177e4SLinus Torvalds 		return IOCTL_OUT(arg, dmasound_set_bass(data));
14211da177e4SLinus Torvalds 	    case SOUND_MIXER_WRITE_TREBLE:
14221da177e4SLinus Torvalds 		IOCTL_IN(arg, data);
14231da177e4SLinus Torvalds 		return IOCTL_OUT(arg, dmasound_set_treble(data));
14241da177e4SLinus Torvalds 	    case SOUND_MIXER_WRITE_OGAIN:
14251da177e4SLinus Torvalds 		IOCTL_IN(arg, data);
14261da177e4SLinus Torvalds 		return IOCTL_OUT(arg, dmasound_set_gain(data));
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 	return AtaMixerIoctl(cmd, arg);
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
FalconMixerIoctl(u_int cmd,u_long arg)14311da177e4SLinus Torvalds static int FalconMixerIoctl(u_int cmd, u_long arg)
14321da177e4SLinus Torvalds {
14331da177e4SLinus Torvalds 	int data;
14341da177e4SLinus Torvalds 	switch (cmd) {
14351da177e4SLinus Torvalds 	case SOUND_MIXER_READ_RECMASK:
14361da177e4SLinus Torvalds 		return IOCTL_OUT(arg, SOUND_MASK_MIC);
14371da177e4SLinus Torvalds 	case SOUND_MIXER_READ_DEVMASK:
14381da177e4SLinus Torvalds 		return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC | SOUND_MASK_SPEAKER);
14391da177e4SLinus Torvalds 	case SOUND_MIXER_READ_STEREODEVS:
14401da177e4SLinus Torvalds 		return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC);
14411da177e4SLinus Torvalds 	case SOUND_MIXER_READ_VOLUME:
14421da177e4SLinus Torvalds 		return IOCTL_OUT(arg,
14431da177e4SLinus Torvalds 			VOLUME_ATT_TO_VOXWARE(dmasound.volume_left) |
14441da177e4SLinus Torvalds 			VOLUME_ATT_TO_VOXWARE(dmasound.volume_right) << 8);
14451da177e4SLinus Torvalds 	case SOUND_MIXER_READ_CAPS:
14461da177e4SLinus Torvalds 		return IOCTL_OUT(arg, SOUND_CAP_EXCL_INPUT);
14471da177e4SLinus Torvalds 	case SOUND_MIXER_WRITE_MIC:
14481da177e4SLinus Torvalds 		IOCTL_IN(arg, data);
14491da177e4SLinus Torvalds 		tt_dmasnd.input_gain =
14501da177e4SLinus Torvalds 			RECLEVEL_VOXWARE_TO_GAIN(data & 0xff) << 4 |
14511da177e4SLinus Torvalds 			RECLEVEL_VOXWARE_TO_GAIN(data >> 8 & 0xff);
1452*c0dbbdadSGustavo A. R. Silva 		fallthrough;	/* return set value */
14531da177e4SLinus Torvalds 	case SOUND_MIXER_READ_MIC:
14541da177e4SLinus Torvalds 		return IOCTL_OUT(arg,
14551da177e4SLinus Torvalds 			RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain >> 4 & 0xf) |
14561da177e4SLinus Torvalds 			RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain & 0xf) << 8);
14571da177e4SLinus Torvalds 	}
14581da177e4SLinus Torvalds 	return AtaMixerIoctl(cmd, arg);
14591da177e4SLinus Torvalds }
14601da177e4SLinus Torvalds 
AtaWriteSqSetup(void)14611da177e4SLinus Torvalds static int AtaWriteSqSetup(void)
14621da177e4SLinus Torvalds {
14631da177e4SLinus Torvalds 	write_sq_ignore_int = 0;
14641da177e4SLinus Torvalds 	return 0 ;
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds 
AtaSqOpen(fmode_t mode)1467aeb5d727SAl Viro static int AtaSqOpen(fmode_t mode)
14681da177e4SLinus Torvalds {
14691da177e4SLinus Torvalds 	write_sq_ignore_int = 1;
14701da177e4SLinus Torvalds 	return 0 ;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
TTStateInfo(char * buffer,size_t space)14731da177e4SLinus Torvalds static int TTStateInfo(char *buffer, size_t space)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	int len = 0;
14761da177e4SLinus Torvalds 	len += sprintf(buffer+len, "\tvol left  %ddB [-40...  0]\n",
14771da177e4SLinus Torvalds 		       dmasound.volume_left);
14781da177e4SLinus Torvalds 	len += sprintf(buffer+len, "\tvol right %ddB [-40...  0]\n",
14791da177e4SLinus Torvalds 		       dmasound.volume_right);
14801da177e4SLinus Torvalds 	len += sprintf(buffer+len, "\tbass      %ddB [-12...+12]\n",
14811da177e4SLinus Torvalds 		       dmasound.bass);
14821da177e4SLinus Torvalds 	len += sprintf(buffer+len, "\ttreble    %ddB [-12...+12]\n",
14831da177e4SLinus Torvalds 		       dmasound.treble);
14841da177e4SLinus Torvalds 	if (len >= space) {
14851da177e4SLinus Torvalds 		printk(KERN_ERR "dmasound_atari: overflowed state buffer alloc.\n") ;
14861da177e4SLinus Torvalds 		len = space ;
14871da177e4SLinus Torvalds 	}
14881da177e4SLinus Torvalds 	return len;
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
FalconStateInfo(char * buffer,size_t space)14911da177e4SLinus Torvalds static int FalconStateInfo(char *buffer, size_t space)
14921da177e4SLinus Torvalds {
14931da177e4SLinus Torvalds 	int len = 0;
14941da177e4SLinus Torvalds 	len += sprintf(buffer+len, "\tvol left  %ddB [-22.5 ... 0]\n",
14951da177e4SLinus Torvalds 		       dmasound.volume_left);
14961da177e4SLinus Torvalds 	len += sprintf(buffer+len, "\tvol right %ddB [-22.5 ... 0]\n",
14971da177e4SLinus Torvalds 		       dmasound.volume_right);
14981da177e4SLinus Torvalds 	if (len >= space) {
14991da177e4SLinus Torvalds 		printk(KERN_ERR "dmasound_atari: overflowed state buffer alloc.\n") ;
15001da177e4SLinus Torvalds 		len = space ;
15011da177e4SLinus Torvalds 	}
15021da177e4SLinus Torvalds 	return len;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds /*** Machine definitions *****************************************************/
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds static SETTINGS def_hard_falcon = {
15091da177e4SLinus Torvalds 	.format		= AFMT_S8,
15101da177e4SLinus Torvalds 	.stereo		= 0,
15111da177e4SLinus Torvalds 	.size		= 8,
15121da177e4SLinus Torvalds 	.speed		= 8195
15131da177e4SLinus Torvalds } ;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds static SETTINGS def_hard_tt = {
15161da177e4SLinus Torvalds 	.format	= AFMT_S8,
15171da177e4SLinus Torvalds 	.stereo	= 0,
15181da177e4SLinus Torvalds 	.size	= 8,
15191da177e4SLinus Torvalds 	.speed	= 12517
15201da177e4SLinus Torvalds } ;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds static SETTINGS def_soft = {
15231da177e4SLinus Torvalds 	.format	= AFMT_U8,
15241da177e4SLinus Torvalds 	.stereo	= 0,
15251da177e4SLinus Torvalds 	.size	= 8,
15261da177e4SLinus Torvalds 	.speed	= 8000
15271da177e4SLinus Torvalds } ;
15281da177e4SLinus Torvalds 
1529d497e3abSMichael Schmitz static __initdata MACHINE machTT = {
15301da177e4SLinus Torvalds 	.name		= "Atari",
15311da177e4SLinus Torvalds 	.name2		= "TT",
15321da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
15331da177e4SLinus Torvalds 	.dma_alloc	= AtaAlloc,
15341da177e4SLinus Torvalds 	.dma_free	= AtaFree,
15351da177e4SLinus Torvalds 	.irqinit	= AtaIrqInit,
15361da177e4SLinus Torvalds #ifdef MODULE
15371da177e4SLinus Torvalds 	.irqcleanup	= AtaIrqCleanUp,
15381da177e4SLinus Torvalds #endif /* MODULE */
15391da177e4SLinus Torvalds 	.init		= TTInit,
15401da177e4SLinus Torvalds 	.silence	= TTSilence,
15411da177e4SLinus Torvalds 	.setFormat	= TTSetFormat,
15421da177e4SLinus Torvalds 	.setVolume	= TTSetVolume,
15431da177e4SLinus Torvalds 	.setBass	= AtaSetBass,
15441da177e4SLinus Torvalds 	.setTreble	= AtaSetTreble,
15451da177e4SLinus Torvalds 	.setGain	= TTSetGain,
15461da177e4SLinus Torvalds 	.play		= AtaPlay,
15471da177e4SLinus Torvalds 	.mixer_init	= TTMixerInit,
15481da177e4SLinus Torvalds 	.mixer_ioctl	= TTMixerIoctl,
15491da177e4SLinus Torvalds 	.write_sq_setup	= AtaWriteSqSetup,
15501da177e4SLinus Torvalds 	.sq_open	= AtaSqOpen,
15511da177e4SLinus Torvalds 	.state_info	= TTStateInfo,
15521da177e4SLinus Torvalds 	.min_dsp_speed	= 6258,
15531da177e4SLinus Torvalds 	.version	= ((DMASOUND_ATARI_REVISION<<8) | DMASOUND_ATARI_EDITION),
15541da177e4SLinus Torvalds 	.hardware_afmts	= AFMT_S8,  /* h'ware-supported formats *only* here */
15551da177e4SLinus Torvalds 	.capabilities	=  DSP_CAP_BATCH	/* As per SNDCTL_DSP_GETCAPS */
15561da177e4SLinus Torvalds };
15571da177e4SLinus Torvalds 
1558d497e3abSMichael Schmitz static __initdata MACHINE machFalcon = {
15591da177e4SLinus Torvalds 	.name		= "Atari",
15601da177e4SLinus Torvalds 	.name2		= "FALCON",
15611da177e4SLinus Torvalds 	.dma_alloc	= AtaAlloc,
15621da177e4SLinus Torvalds 	.dma_free	= AtaFree,
15631da177e4SLinus Torvalds 	.irqinit	= AtaIrqInit,
15641da177e4SLinus Torvalds #ifdef MODULE
15651da177e4SLinus Torvalds 	.irqcleanup	= AtaIrqCleanUp,
15661da177e4SLinus Torvalds #endif /* MODULE */
15671da177e4SLinus Torvalds 	.init		= FalconInit,
15681da177e4SLinus Torvalds 	.silence	= FalconSilence,
15691da177e4SLinus Torvalds 	.setFormat	= FalconSetFormat,
15701da177e4SLinus Torvalds 	.setVolume	= FalconSetVolume,
15711da177e4SLinus Torvalds 	.setBass	= AtaSetBass,
15721da177e4SLinus Torvalds 	.setTreble	= AtaSetTreble,
15731da177e4SLinus Torvalds 	.play		= AtaPlay,
15741da177e4SLinus Torvalds 	.mixer_init	= FalconMixerInit,
15751da177e4SLinus Torvalds 	.mixer_ioctl	= FalconMixerIoctl,
15761da177e4SLinus Torvalds 	.write_sq_setup	= AtaWriteSqSetup,
15771da177e4SLinus Torvalds 	.sq_open	= AtaSqOpen,
15781da177e4SLinus Torvalds 	.state_info	= FalconStateInfo,
15791da177e4SLinus Torvalds 	.min_dsp_speed	= 8195,
15801da177e4SLinus Torvalds 	.version	= ((DMASOUND_ATARI_REVISION<<8) | DMASOUND_ATARI_EDITION),
15811da177e4SLinus Torvalds 	.hardware_afmts	= (AFMT_S8 | AFMT_S16_BE), /* h'ware-supported formats *only* here */
15821da177e4SLinus Torvalds 	.capabilities	=  DSP_CAP_BATCH	/* As per SNDCTL_DSP_GETCAPS */
15831da177e4SLinus Torvalds };
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds /*** Config & Setup **********************************************************/
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 
dmasound_atari_init(void)15891da177e4SLinus Torvalds static int __init dmasound_atari_init(void)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	if (MACH_IS_ATARI && ATARIHW_PRESENT(PCM_8BIT)) {
15921da177e4SLinus Torvalds 	    if (ATARIHW_PRESENT(CODEC)) {
15931da177e4SLinus Torvalds 		dmasound.mach = machFalcon;
15941da177e4SLinus Torvalds 		dmasound.mach.default_soft = def_soft ;
15951da177e4SLinus Torvalds 		dmasound.mach.default_hard = def_hard_falcon ;
15961da177e4SLinus Torvalds 		is_falcon = 1;
15971da177e4SLinus Torvalds 	    } else if (ATARIHW_PRESENT(MICROWIRE)) {
15981da177e4SLinus Torvalds 		dmasound.mach = machTT;
15991da177e4SLinus Torvalds 		dmasound.mach.default_soft = def_soft ;
16001da177e4SLinus Torvalds 		dmasound.mach.default_hard = def_hard_tt ;
16011da177e4SLinus Torvalds 		is_falcon = 0;
16021da177e4SLinus Torvalds 	    } else
16031da177e4SLinus Torvalds 		return -ENODEV;
16043d92e8f3SGeert Uytterhoeven 	    if ((st_mfp.int_en_a & st_mfp.int_mk_a & 0x20) == 0)
16051da177e4SLinus Torvalds 		return dmasound_init();
16061da177e4SLinus Torvalds 	    else {
16071da177e4SLinus Torvalds 		printk("DMA sound driver: Timer A interrupt already in use\n");
16081da177e4SLinus Torvalds 		return -EBUSY;
16091da177e4SLinus Torvalds 	    }
16101da177e4SLinus Torvalds 	}
16111da177e4SLinus Torvalds 	return -ENODEV;
16121da177e4SLinus Torvalds }
16131da177e4SLinus Torvalds 
dmasound_atari_cleanup(void)16141da177e4SLinus Torvalds static void __exit dmasound_atari_cleanup(void)
16151da177e4SLinus Torvalds {
16161da177e4SLinus Torvalds 	dmasound_deinit();
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds module_init(dmasound_atari_init);
16201da177e4SLinus Torvalds module_exit(dmasound_atari_cleanup);
16211da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1622