1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "emu8000_local.h"
23#include <linux/uaccess.h>
24#include <linux/moduleparam.h>
25
26static int emu8000_reset_addr;
27module_param(emu8000_reset_addr, int, 0444);
28MODULE_PARM_DESC(emu8000_reset_addr, "reset write address at each time (makes slowdown)");
29
30
31
32
33
34static int
35snd_emu8000_open_dma(struct snd_emu8000 *emu, int write)
36{
37 int i;
38
39
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
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
60
61static void
62snd_emu8000_close_dma(struct snd_emu8000 *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
82
83
84static unsigned short
85read_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;
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;
103 return c;
104}
105
106
107
108static void
109snd_emu8000_write_wait(struct snd_emu8000 *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
120
121
122
123
124
125
126
127
128
129
130static inline void
131write_word(struct snd_emu8000 *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
144
145
146int
147snd_emu8000_sample_new(struct snd_emux *rec, struct snd_sf_sample *sp,
148 struct snd_util_memhdr *hdr,
149 const void __user *data, long count)
150{
151 int i;
152 int rc;
153 int offset;
154 int truesize;
155 int dram_offset, dram_start;
156 struct snd_emu8000 *emu;
157
158 emu = rec->hw;
159 if (snd_BUG_ON(!sp))
160 return -EINVAL;
161
162 if (sp->v.size == 0)
163 return 0;
164
165
166 if (sp->v.loopstart > sp->v.loopend) {
167 int tmp = sp->v.loopstart;
168 sp->v.loopstart = sp->v.loopend;
169 sp->v.loopend = tmp;
170 }
171
172
173 truesize = sp->v.size;
174 if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP))
175 truesize += sp->v.loopend - sp->v.loopstart;
176 if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK)
177 truesize += BLANK_LOOP_SIZE;
178
179 sp->block = snd_util_mem_alloc(hdr, truesize * 2);
180 if (sp->block == NULL) {
181
182
183 return -ENOSPC;
184 }
185
186 if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS) {
187 if (!access_ok(VERIFY_READ, data, sp->v.size))
188 return -EFAULT;
189 } else {
190 if (!access_ok(VERIFY_READ, data, sp->v.size * 2))
191 return -EFAULT;
192 }
193
194
195 sp->v.end -= sp->v.start;
196 sp->v.loopstart -= sp->v.start;
197 sp->v.loopend -= sp->v.start;
198 sp->v.start = 0;
199
200
201 dram_offset = EMU8000_DRAM_OFFSET + (sp->block->offset >> 1);
202 dram_start = dram_offset;
203
204
205 sp->v.truesize = truesize * 2;
206
207 snd_emux_terminate_all(emu->emu);
208 if ((rc = snd_emu8000_open_dma(emu, EMU8000_RAM_WRITE)) != 0)
209 return rc;
210
211
212 snd_emu8000_write_wait(emu);
213 EMU8000_SMALW_WRITE(emu, dram_offset);
214
215
216
217#if 0
218
219 if (! sp->block->offset) {
220 for (i = 0; i < BLANK_HEAD_SIZE; i++) {
221 write_word(emu, &dram_offset, 0);
222 }
223 }
224#endif
225
226 offset = 0;
227 for (i = 0; i < sp->v.size; i++) {
228 unsigned short s;
229
230 s = read_word(data, offset, sp->v.mode_flags);
231 offset++;
232 write_word(emu, &dram_offset, s);
233
234
235
236
237 cond_resched();
238
239 if (i == sp->v.loopend &&
240 (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP)))
241 {
242 int looplen = sp->v.loopend - sp->v.loopstart;
243 int k;
244
245
246 for (k = 1; k <= looplen; k++) {
247 s = read_word(data, offset - k, sp->v.mode_flags);
248 write_word(emu, &dram_offset, s);
249 }
250 if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_BIDIR_LOOP) {
251 sp->v.loopend += looplen;
252 } else {
253 sp->v.loopstart += looplen;
254 sp->v.loopend += looplen;
255 }
256 sp->v.end += looplen;
257 }
258 }
259
260
261 if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) {
262 for (i = 0; i < BLANK_LOOP_SIZE; i++) {
263 write_word(emu, &dram_offset, 0);
264 }
265 if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT) {
266 sp->v.loopstart = sp->v.end + BLANK_LOOP_START;
267 sp->v.loopend = sp->v.end + BLANK_LOOP_END;
268 }
269 }
270
271
272 sp->v.start += dram_start;
273 sp->v.end += dram_start;
274 sp->v.loopstart += dram_start;
275 sp->v.loopend += dram_start;
276
277 snd_emu8000_close_dma(emu);
278 snd_emu8000_init_fm(emu);
279
280 return 0;
281}
282
283
284
285
286int
287snd_emu8000_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp,
288 struct snd_util_memhdr *hdr)
289{
290 if (sp->block) {
291 snd_util_mem_free(hdr, sp->block);
292 sp->block = NULL;
293 }
294 return 0;
295}
296
297
298
299
300
301void
302snd_emu8000_sample_reset(struct snd_emux *rec)
303{
304 snd_emux_terminate_all(rec);
305}
306