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