xref: /linux/sound/isa/sb/emu8000_patch.c (revision f2ee442115c9b6219083c019939a9cc0c9abb2f8)
1 /*
2  *  Patch routines for the emu8000 (AWE32/64)
3  *
4  *  Copyright (C) 1999 Steve Ratcliffe
5  *  Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21 
22 #include "emu8000_local.h"
23 #include <asm/uaccess.h>
24 #include <linux/moduleparam.h>
25 #include <linux/moduleparam.h>
26 
27 static int emu8000_reset_addr;
28 module_param(emu8000_reset_addr, int, 0444);
29 MODULE_PARM_DESC(emu8000_reset_addr, "reset write address at each time (makes slowdown)");
30 
31 
32 /*
33  * Open up channels.
34  */
35 static int
36 snd_emu8000_open_dma(struct snd_emu8000 *emu, int write)
37 {
38 	int i;
39 
40 	/* reserve all 30 voices for loading */
41 	for (i = 0; i < EMU8000_DRAM_VOICES; i++) {
42 		snd_emux_lock_voice(emu->emu, i);
43 		snd_emu8000_dma_chan(emu, i, write);
44 	}
45 
46 	/* assign voice 31 and 32 to ROM */
47 	EMU8000_VTFT_WRITE(emu, 30, 0);
48 	EMU8000_PSST_WRITE(emu, 30, 0x1d8);
49 	EMU8000_CSL_WRITE(emu, 30, 0x1e0);
50 	EMU8000_CCCA_WRITE(emu, 30, 0x1d8);
51 	EMU8000_VTFT_WRITE(emu, 31, 0);
52 	EMU8000_PSST_WRITE(emu, 31, 0x1d8);
53 	EMU8000_CSL_WRITE(emu, 31, 0x1e0);
54 	EMU8000_CCCA_WRITE(emu, 31, 0x1d8);
55 
56 	return 0;
57 }
58 
59 /*
60  * Close all dram channels.
61  */
62 static void
63 snd_emu8000_close_dma(struct snd_emu8000 *emu)
64 {
65 	int i;
66 
67 	for (i = 0; i < EMU8000_DRAM_VOICES; i++) {
68 		snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE);
69 		snd_emux_unlock_voice(emu->emu, i);
70 	}
71 }
72 
73 /*
74  */
75 
76 #define BLANK_LOOP_START	4
77 #define BLANK_LOOP_END		8
78 #define BLANK_LOOP_SIZE		12
79 #define BLANK_HEAD_SIZE		48
80 
81 /*
82  * Read a word from userland, taking care of conversions from
83  * 8bit samples etc.
84  */
85 static unsigned short
86 read_word(const void __user *buf, int offset, int mode)
87 {
88 	unsigned short c;
89 	if (mode & SNDRV_SFNT_SAMPLE_8BITS) {
90 		unsigned char cc;
91 		get_user(cc, (unsigned char __user *)buf + offset);
92 		c = cc << 8; /* convert 8bit -> 16bit */
93 	} else {
94 #ifdef SNDRV_LITTLE_ENDIAN
95 		get_user(c, (unsigned short __user *)buf + offset);
96 #else
97 		unsigned short cc;
98 		get_user(cc, (unsigned short __user *)buf + offset);
99 		c = swab16(cc);
100 #endif
101 	}
102 	if (mode & SNDRV_SFNT_SAMPLE_UNSIGNED)
103 		c ^= 0x8000; /* unsigned -> signed */
104 	return c;
105 }
106 
107 /*
108  */
109 static void
110 snd_emu8000_write_wait(struct snd_emu8000 *emu)
111 {
112 	while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
113 		schedule_timeout_interruptible(1);
114 		if (signal_pending(current))
115 			break;
116 	}
117 }
118 
119 /*
120  * write sample word data
121  *
122  * You should not have to keep resetting the address each time
123  * as the chip is supposed to step on the next address automatically.
124  * It mostly does, but during writes of some samples at random it
125  * completely loses words (every one in 16 roughly but with no
126  * obvious pattern).
127  *
128  * This is therefore much slower than need be, but is at least
129  * working.
130  */
131 static inline void
132 write_word(struct snd_emu8000 *emu, int *offset, unsigned short data)
133 {
134 	if (emu8000_reset_addr) {
135 		if (emu8000_reset_addr > 1)
136 			snd_emu8000_write_wait(emu);
137 		EMU8000_SMALW_WRITE(emu, *offset);
138 	}
139 	EMU8000_SMLD_WRITE(emu, data);
140 	*offset += 1;
141 }
142 
143 /*
144  * Write the sample to EMU800 memory.  This routine is invoked out of
145  * the generic soundfont routines as a callback.
146  */
147 int
148 snd_emu8000_sample_new(struct snd_emux *rec, struct snd_sf_sample *sp,
149 		       struct snd_util_memhdr *hdr,
150 		       const void __user *data, long count)
151 {
152 	int  i;
153 	int  rc;
154 	int  offset;
155 	int  truesize;
156 	int  dram_offset, dram_start;
157 	struct snd_emu8000 *emu;
158 
159 	emu = rec->hw;
160 	if (snd_BUG_ON(!sp))
161 		return -EINVAL;
162 
163 	if (sp->v.size == 0)
164 		return 0;
165 
166 	/* be sure loop points start < end */
167 	if (sp->v.loopstart > sp->v.loopend) {
168 		int tmp = sp->v.loopstart;
169 		sp->v.loopstart = sp->v.loopend;
170 		sp->v.loopend = tmp;
171 	}
172 
173 	/* compute true data size to be loaded */
174 	truesize = sp->v.size;
175 	if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP))
176 		truesize += sp->v.loopend - sp->v.loopstart;
177 	if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK)
178 		truesize += BLANK_LOOP_SIZE;
179 
180 	sp->block = snd_util_mem_alloc(hdr, truesize * 2);
181 	if (sp->block == NULL) {
182 		/*snd_printd("EMU8000: out of memory\n");*/
183 		/* not ENOMEM (for compatibility) */
184 		return -ENOSPC;
185 	}
186 
187 	if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS) {
188 		if (!access_ok(VERIFY_READ, data, sp->v.size))
189 			return -EFAULT;
190 	} else {
191 		if (!access_ok(VERIFY_READ, data, sp->v.size * 2))
192 			return -EFAULT;
193 	}
194 
195 	/* recalculate address offset */
196 	sp->v.end -= sp->v.start;
197 	sp->v.loopstart -= sp->v.start;
198 	sp->v.loopend -= sp->v.start;
199 	sp->v.start = 0;
200 
201 	/* dram position (in word) -- mem_offset is byte */
202 	dram_offset = EMU8000_DRAM_OFFSET + (sp->block->offset >> 1);
203 	dram_start = dram_offset;
204 
205 	/* set the total size (store onto obsolete checksum value) */
206 	sp->v.truesize = truesize * 2; /* in bytes */
207 
208 	snd_emux_terminate_all(emu->emu);
209 	if ((rc = snd_emu8000_open_dma(emu, EMU8000_RAM_WRITE)) != 0)
210 		return rc;
211 
212 	/* Set the address to start writing at */
213 	snd_emu8000_write_wait(emu);
214 	EMU8000_SMALW_WRITE(emu, dram_offset);
215 
216 	/*snd_emu8000_init_fm(emu);*/
217 
218 #if 0
219 	/* first block - write 48 samples for silence */
220 	if (! sp->block->offset) {
221 		for (i = 0; i < BLANK_HEAD_SIZE; i++) {
222 			write_word(emu, &dram_offset, 0);
223 		}
224 	}
225 #endif
226 
227 	offset = 0;
228 	for (i = 0; i < sp->v.size; i++) {
229 		unsigned short s;
230 
231 		s = read_word(data, offset, sp->v.mode_flags);
232 		offset++;
233 		write_word(emu, &dram_offset, s);
234 
235 		/* we may take too long time in this loop.
236 		 * so give controls back to kernel if needed.
237 		 */
238 		cond_resched();
239 
240 		if (i == sp->v.loopend &&
241 		    (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP)))
242 		{
243 			int looplen = sp->v.loopend - sp->v.loopstart;
244 			int k;
245 
246 			/* copy reverse loop */
247 			for (k = 1; k <= looplen; k++) {
248 				s = read_word(data, offset - k, sp->v.mode_flags);
249 				write_word(emu, &dram_offset, s);
250 			}
251 			if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_BIDIR_LOOP) {
252 				sp->v.loopend += looplen;
253 			} else {
254 				sp->v.loopstart += looplen;
255 				sp->v.loopend += looplen;
256 			}
257 			sp->v.end += looplen;
258 		}
259 	}
260 
261 	/* if no blank loop is attached in the sample, add it */
262 	if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) {
263 		for (i = 0; i < BLANK_LOOP_SIZE; i++) {
264 			write_word(emu, &dram_offset, 0);
265 		}
266 		if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT) {
267 			sp->v.loopstart = sp->v.end + BLANK_LOOP_START;
268 			sp->v.loopend = sp->v.end + BLANK_LOOP_END;
269 		}
270 	}
271 
272 	/* add dram offset */
273 	sp->v.start += dram_start;
274 	sp->v.end += dram_start;
275 	sp->v.loopstart += dram_start;
276 	sp->v.loopend += dram_start;
277 
278 	snd_emu8000_close_dma(emu);
279 	snd_emu8000_init_fm(emu);
280 
281 	return 0;
282 }
283 
284 /*
285  * free a sample block
286  */
287 int
288 snd_emu8000_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp,
289 			struct snd_util_memhdr *hdr)
290 {
291 	if (sp->block) {
292 		snd_util_mem_free(hdr, sp->block);
293 		sp->block = NULL;
294 	}
295 	return 0;
296 }
297 
298 
299 /*
300  * sample_reset callback - terminate voices
301  */
302 void
303 snd_emu8000_sample_reset(struct snd_emux *rec)
304 {
305 	snd_emux_terminate_all(rec);
306 }
307