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