2 * Patch routines for the emu8000 (AWE32/64)
4 * Copyright (C) 1999 Steve Ratcliffe
5 * Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
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.
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.
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
22 #include "emu8000_local.h"
24 #include <linux/sched/signal.h>
25 #include <linux/uaccess.h>
26 #include <linux/moduleparam.h>
28 static int emu8000_reset_addr
;
29 module_param(emu8000_reset_addr
, int, 0444);
30 MODULE_PARM_DESC(emu8000_reset_addr
, "reset write address at each time (makes slowdown)");
37 snd_emu8000_open_dma(struct snd_emu8000
*emu
, int write
)
41 /* reserve all 30 voices for loading */
42 for (i
= 0; i
< EMU8000_DRAM_VOICES
; i
++) {
43 snd_emux_lock_voice(emu
->emu
, i
);
44 snd_emu8000_dma_chan(emu
, i
, write
);
47 /* assign voice 31 and 32 to ROM */
48 EMU8000_VTFT_WRITE(emu
, 30, 0);
49 EMU8000_PSST_WRITE(emu
, 30, 0x1d8);
50 EMU8000_CSL_WRITE(emu
, 30, 0x1e0);
51 EMU8000_CCCA_WRITE(emu
, 30, 0x1d8);
52 EMU8000_VTFT_WRITE(emu
, 31, 0);
53 EMU8000_PSST_WRITE(emu
, 31, 0x1d8);
54 EMU8000_CSL_WRITE(emu
, 31, 0x1e0);
55 EMU8000_CCCA_WRITE(emu
, 31, 0x1d8);
61 * Close all dram channels.
64 snd_emu8000_close_dma(struct snd_emu8000
*emu
)
68 for (i
= 0; i
< EMU8000_DRAM_VOICES
; i
++) {
69 snd_emu8000_dma_chan(emu
, i
, EMU8000_RAM_CLOSE
);
70 snd_emux_unlock_voice(emu
->emu
, i
);
77 #define BLANK_LOOP_START 4
78 #define BLANK_LOOP_END 8
79 #define BLANK_LOOP_SIZE 12
80 #define BLANK_HEAD_SIZE 48
83 * Read a word from userland, taking care of conversions from
87 read_word(const void __user
*buf
, int offset
, int mode
)
90 if (mode
& SNDRV_SFNT_SAMPLE_8BITS
) {
92 get_user(cc
, (unsigned char __user
*)buf
+ offset
);
93 c
= cc
<< 8; /* convert 8bit -> 16bit */
95 #ifdef SNDRV_LITTLE_ENDIAN
96 get_user(c
, (unsigned short __user
*)buf
+ offset
);
99 get_user(cc
, (unsigned short __user
*)buf
+ offset
);
103 if (mode
& SNDRV_SFNT_SAMPLE_UNSIGNED
)
104 c
^= 0x8000; /* unsigned -> signed */
111 snd_emu8000_write_wait(struct snd_emu8000
*emu
)
113 while ((EMU8000_SMALW_READ(emu
) & 0x80000000) != 0) {
114 schedule_timeout_interruptible(1);
115 if (signal_pending(current
))
121 * write sample word data
123 * You should not have to keep resetting the address each time
124 * as the chip is supposed to step on the next address automatically.
125 * It mostly does, but during writes of some samples at random it
126 * completely loses words (every one in 16 roughly but with no
129 * This is therefore much slower than need be, but is at least
133 write_word(struct snd_emu8000
*emu
, int *offset
, unsigned short data
)
135 if (emu8000_reset_addr
) {
136 if (emu8000_reset_addr
> 1)
137 snd_emu8000_write_wait(emu
);
138 EMU8000_SMALW_WRITE(emu
, *offset
);
140 EMU8000_SMLD_WRITE(emu
, data
);
145 * Write the sample to EMU800 memory. This routine is invoked out of
146 * the generic soundfont routines as a callback.
149 snd_emu8000_sample_new(struct snd_emux
*rec
, struct snd_sf_sample
*sp
,
150 struct snd_util_memhdr
*hdr
,
151 const void __user
*data
, long count
)
157 int dram_offset
, dram_start
;
158 struct snd_emu8000
*emu
;
167 /* be sure loop points start < end */
168 if (sp
->v
.loopstart
> sp
->v
.loopend
) {
169 int tmp
= sp
->v
.loopstart
;
170 sp
->v
.loopstart
= sp
->v
.loopend
;
174 /* compute true data size to be loaded */
175 truesize
= sp
->v
.size
;
176 if (sp
->v
.mode_flags
& (SNDRV_SFNT_SAMPLE_BIDIR_LOOP
|SNDRV_SFNT_SAMPLE_REVERSE_LOOP
))
177 truesize
+= sp
->v
.loopend
- sp
->v
.loopstart
;
178 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_NO_BLANK
)
179 truesize
+= BLANK_LOOP_SIZE
;
181 sp
->block
= snd_util_mem_alloc(hdr
, truesize
* 2);
182 if (sp
->block
== NULL
) {
183 /*snd_printd("EMU8000: out of memory\n");*/
184 /* not ENOMEM (for compatibility) */
188 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_8BITS
) {
189 if (!access_ok(VERIFY_READ
, data
, sp
->v
.size
))
192 if (!access_ok(VERIFY_READ
, data
, sp
->v
.size
* 2))
196 /* recalculate address offset */
197 sp
->v
.end
-= sp
->v
.start
;
198 sp
->v
.loopstart
-= sp
->v
.start
;
199 sp
->v
.loopend
-= sp
->v
.start
;
202 /* dram position (in word) -- mem_offset is byte */
203 dram_offset
= EMU8000_DRAM_OFFSET
+ (sp
->block
->offset
>> 1);
204 dram_start
= dram_offset
;
206 /* set the total size (store onto obsolete checksum value) */
207 sp
->v
.truesize
= truesize
* 2; /* in bytes */
209 snd_emux_terminate_all(emu
->emu
);
210 if ((rc
= snd_emu8000_open_dma(emu
, EMU8000_RAM_WRITE
)) != 0)
213 /* Set the address to start writing at */
214 snd_emu8000_write_wait(emu
);
215 EMU8000_SMALW_WRITE(emu
, dram_offset
);
217 /*snd_emu8000_init_fm(emu);*/
220 /* first block - write 48 samples for silence */
221 if (! sp
->block
->offset
) {
222 for (i
= 0; i
< BLANK_HEAD_SIZE
; i
++) {
223 write_word(emu
, &dram_offset
, 0);
229 for (i
= 0; i
< sp
->v
.size
; i
++) {
232 s
= read_word(data
, offset
, sp
->v
.mode_flags
);
234 write_word(emu
, &dram_offset
, s
);
236 /* we may take too long time in this loop.
237 * so give controls back to kernel if needed.
241 if (i
== sp
->v
.loopend
&&
242 (sp
->v
.mode_flags
& (SNDRV_SFNT_SAMPLE_BIDIR_LOOP
|SNDRV_SFNT_SAMPLE_REVERSE_LOOP
)))
244 int looplen
= sp
->v
.loopend
- sp
->v
.loopstart
;
247 /* copy reverse loop */
248 for (k
= 1; k
<= looplen
; k
++) {
249 s
= read_word(data
, offset
- k
, sp
->v
.mode_flags
);
250 write_word(emu
, &dram_offset
, s
);
252 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_BIDIR_LOOP
) {
253 sp
->v
.loopend
+= looplen
;
255 sp
->v
.loopstart
+= looplen
;
256 sp
->v
.loopend
+= looplen
;
258 sp
->v
.end
+= looplen
;
262 /* if no blank loop is attached in the sample, add it */
263 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_NO_BLANK
) {
264 for (i
= 0; i
< BLANK_LOOP_SIZE
; i
++) {
265 write_word(emu
, &dram_offset
, 0);
267 if (sp
->v
.mode_flags
& SNDRV_SFNT_SAMPLE_SINGLESHOT
) {
268 sp
->v
.loopstart
= sp
->v
.end
+ BLANK_LOOP_START
;
269 sp
->v
.loopend
= sp
->v
.end
+ BLANK_LOOP_END
;
273 /* add dram offset */
274 sp
->v
.start
+= dram_start
;
275 sp
->v
.end
+= dram_start
;
276 sp
->v
.loopstart
+= dram_start
;
277 sp
->v
.loopend
+= dram_start
;
279 snd_emu8000_close_dma(emu
);
280 snd_emu8000_init_fm(emu
);
286 * free a sample block
289 snd_emu8000_sample_free(struct snd_emux
*rec
, struct snd_sf_sample
*sp
,
290 struct snd_util_memhdr
*hdr
)
293 snd_util_mem_free(hdr
, sp
->block
);
301 * sample_reset callback - terminate voices
304 snd_emu8000_sample_reset(struct snd_emux
*rec
)
306 snd_emux_terminate_all(rec
);