To: linux-m68k@lists.linux-m68k.org
Subject: L68K: stram.c, dmasound.c
X-Yow: Hey!!  Let's watch the' ELEVATOR go UP and DOWN at th' HILTON HOTEL!!
From: Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
Date: 29 Jun 1998 11:58:16 +0200
Sender: owner-linux-m68k@phil.uni-sb.de

Hi!

This patch is for dmasound.c and stram.c: more __initfunc's, optimized
sound format conversions, lock mmap semaphore around accesses to the process
memory map.

Andreas.

----------------------------------------------------------------------
--- linux/arch/m68k/atari/stram.c.~1~	Wed Jun 10 17:57:41 1998
+++ linux/arch/m68k/atari/stram.c	Sat Jun 27 13:08:02 1998
@@ -826,18 +826,23 @@
 						   unsigned long page, int isswap )
 {
 	struct vm_area_struct* vma;
+	int retval = 0;
 
 	/*
 	 * Go through process' page directory.
 	 */
 	if (!mm || mm == &init_mm)
 		return 0;
+	down(&mm->mmap_sem);
 	for( vma = mm->mmap; vma; vma = vma->vm_next ) {
 		pgd_t * pgd = pgd_offset(mm, vma->vm_start);
-		if (unswap_vma( vma, pgd, entry, page, isswap ))
-			return 1;
+		if (unswap_vma( vma, pgd, entry, page, isswap )) {
+			retval = 1;
+			break;
+		}
 	}
-	return 0;
+	up(&mm->mmap_sem);
+	return retval;
 }
 
 
@@ -1160,7 +1165,7 @@
 
 
 /* setup parameters from command line */
-void stram_swap_setup( char *str, int *ints )
+__initfunc(void stram_swap_setup(char *str, int *ints))
 {
 	if (ints[0] >= 1)
 		max_swap_size = ((ints[1] < 0 ? 0 : ints[1]) * 1024) & PAGE_MASK;
@@ -1256,7 +1261,7 @@
         block_fsync             /* fsync */
 };
 
-int stram_device_init( void )
+__initfunc(int stram_device_init(void))
 {
 
     if (!MACH_IS_ATARI)
--- linux/drivers/sound/dmasound.c.~2~	Mon Jun 15 20:55:57 1998
+++ linux/drivers/sound/dmasound.c	Sat Jun 27 13:52:15 1998
@@ -88,6 +88,7 @@
 #include <linux/mm.h>
 #include <linux/malloc.h>
 #include <linux/sound.h>
+#include <linux/init.h>
 
 #ifdef __mc68000__
 #include <asm/setup.h>
@@ -124,10 +125,10 @@
 
 #define HAS_8BIT_TABLES
 
+#ifdef MODULE
 static int sq_unit = -1;
 static int mixer_unit = -1;
 static int state_unit = -1;
-#ifdef MODULE
 static int irq_installed = 0;
 #endif /* MODULE */
 static char **sound_buffers = NULL;
@@ -263,7 +264,9 @@
 #define MAX_BUFSIZE		128	/* Limit for Amiga */
 
 static int catchRadius = 0, numBufs = 4, bufSize = 32;
-
+MODULE_PARM(catchRadius, "i");
+MODULE_PARM(numBufs, "i");
+MODULE_PARM(bufSize, "i");
 
 #define arraysize(x)	(sizeof(x)/sizeof(*(x)))
 #define min(x, y)	((x) < (y) ? (x) : (y))
@@ -681,9 +684,9 @@
 
 
 #ifdef CONFIG_ATARI
-static void *AtaAlloc(unsigned int size, int flags);
-static void AtaFree(void *, unsigned int size);
-static int AtaIrqInit(void);
+static void *AtaAlloc(unsigned int size, int flags) __init;
+static void AtaFree(void *, unsigned int size) __init;
+static int AtaIrqInit(void) __init;
 #ifdef MODULE
 static void AtaIrqCleanUp(void);
 #endif /* MODULE */
@@ -704,9 +707,9 @@
 #endif /* CONFIG_ATARI */
 
 #ifdef CONFIG_AMIGA
-static void *AmiAlloc(unsigned int size, int flags);
-static void AmiFree(void *, unsigned int);
-static int AmiIrqInit(void);
+static void *AmiAlloc(unsigned int size, int flags) __init;
+static void AmiFree(void *, unsigned int) __init;
+static int AmiIrqInit(void) __init;
 #ifdef MODULE
 static void AmiIrqCleanUp(void);
 #endif /* MODULE */
@@ -721,9 +724,9 @@
 #endif /* CONFIG_AMIGA */
 
 #ifdef CONFIG_PMAC
-static void *PMacAlloc(unsigned int size, int flags);
-static void PMacFree(void *ptr, unsigned int size);
-static int PMacIrqInit(void);
+static void *PMacAlloc(unsigned int size, int flags) __init;
+static void PMacFree(void *ptr, unsigned int size) __init;
+static int PMacIrqInit(void) __init;
 #ifdef MODULE
 static void PMacIrqCleanup(void);
 #endif /* MODULE */
@@ -956,13 +959,13 @@
 			    ssize_t frameLeft)
 {
 	ssize_t count, used;
-	u_long data;
 
 	if (!sound.soft.stereo) {
 		u_short *p = (u_short *)&frame[*frameUsed];
 		count = min(userCount, frameLeft)>>1;
 		used = count*2;
 		while (count > 0) {
+			u_short data;
 			if (get_user(data, ((u_short *)userPtr)++))
 				return -EFAULT;
 			*p++ = data;
@@ -987,13 +990,13 @@
 			    ssize_t frameLeft)
 {
 	ssize_t count, used;
-	u_long data;
 
 	if (!sound.soft.stereo) {
 		u_short *p = (u_short *)&frame[*frameUsed];
 		count = min(userCount, frameLeft)>>1;
 		used = count*2;
 		while (count > 0) {
+			u_short data;
 			if (get_user(data, ((u_short *)userPtr)++))
 				return -EFAULT;
 			data ^= 0x8000;
@@ -1007,6 +1010,7 @@
 		count = min(userCount, frameLeft)>>2;
 		used = count*4;
 		while (count > 0) {
+			u_long data;
 			if (get_user(data, ((u_int *)userPtr)++))
 				return -EFAULT;
 			*p++ = data ^ 0x80008000;
@@ -1023,7 +1027,6 @@
 			    ssize_t frameLeft)
 {
 	ssize_t count, used;
-	u_long data;
 
 	count = frameLeft;
 	if (!sound.soft.stereo) {
@@ -1031,6 +1034,7 @@
 		count = min(userCount, frameLeft)>>1;
 		used = count*2;
 		while (count > 0) {
+			u_short data;
 			if (get_user(data, ((u_short *)userPtr)++))
 				return -EFAULT;
 			data = le2be16(data);
@@ -1044,6 +1048,7 @@
 		count = min(userCount, frameLeft)>>2;
 		used = count*4;
 		while (count > 0) {
+			u_long data;
 			if (get_user(data, ((u_int *)userPtr)++))
 				return -EFAULT;
 			data = le2be16dbl(data);
@@ -1061,7 +1066,6 @@
 			    ssize_t frameLeft)
 {
 	ssize_t count, used;
-	u_long data;
 
 	count = frameLeft;
 	if (!sound.soft.stereo) {
@@ -1069,6 +1073,7 @@
 		count = min(userCount, frameLeft)>>1;
 		used = count*2;
 		while (count > 0) {
+			u_short data;
 			if (get_user(data, ((u_short *)userPtr)++))
 				return -EFAULT;
 			data = le2be16(data) ^ 0x8000;
@@ -1081,6 +1086,7 @@
 		count = min(userCount, frameLeft)>>2;
 		used = count;
 		while (count > 0) {
+			u_long data;
 			if (get_user(data, ((u_int *)userPtr)++))
 				return -EFAULT;
 			data = le2be16dbl(data) ^ 0x80008000;
@@ -1099,7 +1105,6 @@
 {
 	char *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma8 : alaw2dma8;
 	/* this should help gcc to stuff everything into registers */
-	u_long data = sound.data;
 	long bal = sound.bal;
 	long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
 	ssize_t used, usedf;
@@ -1108,6 +1113,7 @@
 	usedf = frameLeft;
 	if (!sound.soft.stereo) {
 		u_char *p = &frame[*frameUsed];
+		u_char data = sound.data;
 		while (frameLeft) {
 			u_char c;
 			if (bal < 0) {
@@ -1123,8 +1129,10 @@
 			frameLeft--;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	} else {
 		u_short *p = (u_short *)&frame[*frameUsed];
+		u_short data = sound.data;
 		while (frameLeft >= 2) {
 			u_char c;
 			if (bal < 0) {
@@ -1143,9 +1151,9 @@
 			frameLeft -= 2;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	}
 	sound.bal = bal;
-	sound.data = data;
 	used -= userCount;
 	*frameUsed += usedf-frameLeft;
 	return(used);
@@ -1157,7 +1165,6 @@
 			  ssize_t frameLeft)
 {
 	/* this should help gcc to stuff everything into registers */
-	u_long data = sound.data;
 	long bal = sound.bal;
 	long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
 	ssize_t used, usedf;
@@ -1166,6 +1173,7 @@
 	usedf = frameLeft;
 	if (!sound.soft.stereo) {
 		u_char *p = &frame[*frameUsed];
+		u_char data = sound.data;
 		while (frameLeft) {
 			if (bal < 0) {
 				if (!userCount)
@@ -1179,8 +1187,10 @@
 			frameLeft--;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	} else {
 		u_short *p = (u_short *)&frame[*frameUsed];
+		u_short data = sound.data;
 		while (frameLeft >= 2) {
 			if (bal < 0) {
 				if (userCount < 2)
@@ -1194,9 +1204,9 @@
 			frameLeft -= 2;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	}
 	sound.bal = bal;
-	sound.data = data;
 	used -= userCount;
 	*frameUsed += usedf-frameLeft;
 	return(used);
@@ -1208,7 +1218,6 @@
 			  ssize_t frameLeft)
 {
 	/* this should help gcc to stuff everything into registers */
-	u_long data = sound.data;
 	long bal = sound.bal;
 	long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
 	ssize_t used, usedf;
@@ -1217,6 +1226,7 @@
 	usedf = frameLeft;
 	if (!sound.soft.stereo) {
 		u_char *p = &frame[*frameUsed];
+		u_char data = sound.data;
 		while (frameLeft) {
 			if (bal < 0) {
 				if (!userCount)
@@ -1231,8 +1241,10 @@
 			frameLeft--;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	} else {
 		u_short *p = (u_short *)&frame[*frameUsed];
+		u_short data = sound.data;
 		while (frameLeft >= 2) {
 			if (bal < 0) {
 				if (userCount < 2)
@@ -1247,9 +1259,9 @@
 			frameLeft -= 2;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	}
 	sound.bal = bal;
-	sound.data = data;
 	used -= userCount;
 	*frameUsed += usedf-frameLeft;
 	return(used);
@@ -1261,7 +1273,6 @@
 			     ssize_t frameLeft)
 {
 	/* this should help gcc to stuff everything into registers */
-	u_long data = sound.data;
 	long bal = sound.bal;
 	long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
 	ssize_t used, usedf;
@@ -1270,6 +1281,7 @@
 	usedf = frameLeft;
 	if (!sound.soft.stereo) {
 		u_short *p = (u_short *)&frame[*frameUsed];
+		u_short data = sound.data;
 		while (frameLeft >= 4) {
 			if (bal < 0) {
 				if (userCount < 2)
@@ -1284,8 +1296,10 @@
 			frameLeft -= 4;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	} else {
 		u_long *p = (u_long *)&frame[*frameUsed];
+		u_long data = sound.data;
 		while (frameLeft >= 4) {
 			if (bal < 0) {
 				if (userCount < 4)
@@ -1299,9 +1313,9 @@
 			frameLeft -= 4;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	}
 	sound.bal = bal;
-	sound.data = data;
 	used -= userCount;
 	*frameUsed += usedf-frameLeft;
 	return(used);
@@ -1313,7 +1327,6 @@
 			     ssize_t frameLeft)
 {
 	/* this should help gcc to stuff everything into registers */
-	u_long data = sound.data;
 	long bal = sound.bal;
 	long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
 	ssize_t used, usedf;
@@ -1322,6 +1335,7 @@
 	usedf = frameLeft;
 	if (!sound.soft.stereo) {
 		u_short *p = (u_short *)&frame[*frameUsed];
+		u_short data = sound.data;
 		while (frameLeft >= 4) {
 			if (bal < 0) {
 				if (userCount < 2)
@@ -1337,8 +1351,10 @@
 			frameLeft -= 4;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	} else {
 		u_long *p = (u_long *)&frame[*frameUsed];
+		u_long data = sound.data;
 		while (frameLeft >= 4) {
 			if (bal < 0) {
 				if (userCount < 4)
@@ -1353,9 +1369,9 @@
 			frameLeft -= 4;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	}
 	sound.bal = bal;
-	sound.data = data;
 	used -= userCount;
 	*frameUsed += usedf-frameLeft;
 	return(used);
@@ -1367,7 +1383,6 @@
 			     ssize_t frameLeft)
 {
 	/* this should help gcc to stuff everything into registers */
-	u_long data = sound.data;
 	long bal = sound.bal;
 	long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
 	ssize_t used, usedf;
@@ -1376,6 +1391,7 @@
 	usedf = frameLeft;
 	if (!sound.soft.stereo) {
 		u_short *p = (u_short *)&frame[*frameUsed];
+		u_short data = sound.data;
 		while (frameLeft >= 4) {
 			if (bal < 0) {
 				if (userCount < 2)
@@ -1391,8 +1407,10 @@
 			frameLeft -= 4;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	} else {
 		u_long *p = (u_long *)&frame[*frameUsed];
+		u_long data = sound.data;
 		while (frameLeft >= 4) {
 			if (bal < 0) {
 				if (userCount < 4)
@@ -1407,9 +1425,9 @@
 			frameLeft -= 4;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	}
 	sound.bal = bal;
-	sound.data = data;
 	used -= userCount;
 	*frameUsed += usedf-frameLeft;
 	return(used);
@@ -1421,7 +1439,6 @@
 			     ssize_t frameLeft)
 {
 	/* this should help gcc to stuff everything into registers */
-	u_long data = sound.data;
 	long bal = sound.bal;
 	long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
 	ssize_t used, usedf;
@@ -1430,6 +1447,7 @@
 	usedf = frameLeft;
 	if (!sound.soft.stereo) {
 		u_short *p = (u_short *)&frame[*frameUsed];
+		u_short data = sound.data;
 		while (frameLeft >= 4) {
 			if (bal < 0) {
 				if (userCount < 2)
@@ -1445,8 +1463,10 @@
 			frameLeft -= 4;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	} else {
 		u_long *p = (u_long *)&frame[*frameUsed];
+		u_long data = sound.data;
 		while (frameLeft >= 4) {
 			if (bal < 0) {
 				if (userCount < 4)
@@ -1461,9 +1481,9 @@
 			frameLeft -= 4;
 			bal -= sSpeed;
 		}
+		sound.data = data;
 	}
 	sound.bal = bal;
-	sound.data = data;
 	used -= userCount;
 	*frameUsed += usedf-frameLeft;
 	return(used);
@@ -1581,7 +1601,7 @@
 			    ssize_t frameLeft)
 {
 	ssize_t count, used;
-	u_long data;
+	u_short data;
 
 	if (!sound.soft.stereo) {
 		u_char *high = &frame[*frameUsed>>1];
@@ -1624,7 +1644,7 @@
 			    ssize_t frameLeft)
 {
 	ssize_t count, used;
-	u_long data;
+	u_short data;
 
 	if (!sound.soft.stereo) {
 		u_char *high = &frame[*frameUsed>>1];
@@ -1670,7 +1690,7 @@
 			    ssize_t frameLeft)
 {
 	ssize_t count, used;
-	u_long data;
+	u_short data;
 
 	if (!sound.soft.stereo) {
 		u_char *high = &frame[*frameUsed>>1];
@@ -1716,7 +1736,7 @@
 			    ssize_t frameLeft)
 {
 	ssize_t count, used;
-	u_long data;
+	u_short data;
 
 	if (!sound.soft.stereo) {
 		u_char *high = &frame[*frameUsed>>1];
@@ -1886,7 +1906,7 @@
 			   ssize_t frameLeft)
 {
 	ssize_t count, used;
-	int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
+	short mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
 	int stereo = sound.soft.stereo;
 	short *fp = (short *) &frame[*frameUsed];
 	short *up = (short *) userPtr;
@@ -1895,7 +1915,7 @@
 	userCount >>= (stereo? 2: 1);
 	used = count = min(userCount, frameLeft);
 	while (count > 0) {
-		int data;
+		short data;
 		if (get_user(data, up++))
 			return -EFAULT;
 		data ^= mask;
@@ -3791,8 +3811,11 @@
 };
 
 
-static void mixer_init(void)
+__initfunc(static void mixer_init(void))
 {
+#ifndef MODULE
+	int mixer_unit;
+#endif
 	mixer_unit = register_sound_mixer(&mixer_fops);
 	if (mixer_unit < 0)
 		return;
@@ -4149,8 +4172,11 @@
 };
 
 
-static void sq_init(void)
+__initfunc(static void sq_init(void))
 {
+#ifndef MODULE
+	int sq_unit;
+#endif
 	sq_unit = register_sound_dsp(&sq_fops);
 	if (sq_unit < 0)
 		return;
@@ -4340,8 +4366,11 @@
 };
 
 
-static void state_init(void)
+__initfunc(static void state_init(void))
 {
+#ifndef MODULE
+	int state_unit;
+#endif
 	state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
 	if (state_unit < 0)
 		return;
@@ -4360,7 +4389,7 @@
 /*** Config & Setup **********************************************************/
 
 
-void dmasound_init(void)
+__initfunc(void dmasound_init(void))
 {
 	int has_sound = 0;
 	int i;
@@ -4494,7 +4523,7 @@
 
 #define MAXARGS		8	/* Should be sufficient for now */
 
-void dmasound_setup(char *str, int *ints)
+__initfunc(void dmasound_setup(char *str, int *ints))
 {
 	/* check the bootstrap parameter for "dmasound=" */
 
@@ -4525,20 +4554,8 @@
 
 #ifdef MODULE
 
-static int dmasound[MAXARGS] = { 0 };
-
 int init_module(void)
 {
-	int err, i = 0;
-	int ints[MAXARGS+1];
-
-	while (i < MAXARGS && dmasound[i])
-		ints[i + 1] = dmasound[i++];
-	ints[0] = i;
-
-	if (i)
-		dmasound_setup("dmasound=", ints);
-
 	dmasound_init();
 	return 0;
 }
