2 * pcm emulation on emu8000 wavetable
4 * Copyright (C) 2002 Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "emu8000_local.h"
23 #include <linux/sched/signal.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
30 * define the following if you want to use this pcm with non-interleaved mode
32 /* #define USE_NONINTERLEAVE */
34 /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set
35 * mmap_emulation flag to 1 in your .asoundrc, such like
47 * besides, for the time being, the non-interleaved mode doesn't work well on
52 struct snd_emu8k_pcm {
53 struct snd_emu8000 *emu;
54 struct snd_pcm_substream *substream;
56 unsigned int allocated_bytes;
57 struct snd_util_memblk *block;
59 unsigned int buf_size;
60 unsigned int period_size;
61 unsigned int loop_start[2];
67 unsigned int dram_opened: 1;
68 unsigned int running: 1;
69 unsigned int timer_running: 1;
70 struct timer_list timer;
71 spinlock_t timer_lock;
74 #define LOOP_BLANK_SIZE 8
78 * open up channels for the simultaneous data transfer and playback
81 emu8k_open_dram_for_pcm(struct snd_emu8000 *emu, int channels)
85 /* reserve up to 2 voices for playback */
86 snd_emux_lock_voice(emu->emu, 0);
88 snd_emux_lock_voice(emu->emu, 1);
90 /* reserve 28 voices for loading */
91 for (i = channels + 1; i < EMU8000_DRAM_VOICES; i++) {
92 unsigned int mode = EMU8000_RAM_WRITE;
93 snd_emux_lock_voice(emu->emu, i);
94 #ifndef USE_NONINTERLEAVE
95 if (channels > 1 && (i & 1) != 0)
96 mode |= EMU8000_RAM_RIGHT;
98 snd_emu8000_dma_chan(emu, i, mode);
101 /* assign voice 31 and 32 to ROM */
102 EMU8000_VTFT_WRITE(emu, 30, 0);
103 EMU8000_PSST_WRITE(emu, 30, 0x1d8);
104 EMU8000_CSL_WRITE(emu, 30, 0x1e0);
105 EMU8000_CCCA_WRITE(emu, 30, 0x1d8);
106 EMU8000_VTFT_WRITE(emu, 31, 0);
107 EMU8000_PSST_WRITE(emu, 31, 0x1d8);
108 EMU8000_CSL_WRITE(emu, 31, 0x1e0);
109 EMU8000_CCCA_WRITE(emu, 31, 0x1d8);
117 snd_emu8000_write_wait(struct snd_emu8000 *emu, int can_schedule)
119 while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
121 schedule_timeout_interruptible(1);
122 if (signal_pending(current))
132 emu8k_close_dram(struct snd_emu8000 *emu)
136 for (i = 0; i < 2; i++)
137 snd_emux_unlock_voice(emu->emu, i);
138 for (; i < EMU8000_DRAM_VOICES; i++) {
139 snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE);
140 snd_emux_unlock_voice(emu->emu, i);
145 * convert Hz to AWE32 rate offset (see emux/soundfont.c)
148 #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */
149 #define SAMPLERATE_RATIO 4096
151 static int calc_rate_offset(int hz)
153 return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO);
160 static const struct snd_pcm_hardware emu8k_pcm_hw = {
161 #ifdef USE_NONINTERLEAVE
162 .info = SNDRV_PCM_INFO_NONINTERLEAVED,
164 .info = SNDRV_PCM_INFO_INTERLEAVED,
166 .formats = SNDRV_PCM_FMTBIT_S16_LE,
167 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
172 .buffer_bytes_max = (128*1024),
173 .period_bytes_min = 1024,
174 .period_bytes_max = (128*1024),
182 * get the current position at the given channel from CCCA register
184 static inline int emu8k_get_curpos(struct snd_emu8k_pcm *rec, int ch)
186 int val = EMU8000_CCCA_READ(rec->emu, ch) & 0xfffffff;
187 val -= rec->loop_start[ch] - 1;
193 * timer interrupt handler
194 * check the current position and update the period if necessary.
196 static void emu8k_pcm_timer_func(struct timer_list *t)
198 struct snd_emu8k_pcm *rec = from_timer(rec, t, timer);
201 spin_lock(&rec->timer_lock);
202 /* update the current pointer */
203 ptr = emu8k_get_curpos(rec, 0);
204 if (ptr < rec->last_ptr)
205 delta = ptr + rec->buf_size - rec->last_ptr;
207 delta = ptr - rec->last_ptr;
208 rec->period_pos += delta;
211 /* reprogram timer */
212 mod_timer(&rec->timer, jiffies + 1);
215 if (rec->period_pos >= (int)rec->period_size) {
216 rec->period_pos %= rec->period_size;
217 spin_unlock(&rec->timer_lock);
218 snd_pcm_period_elapsed(rec->substream);
221 spin_unlock(&rec->timer_lock);
227 * creating an instance here
229 static int emu8k_pcm_open(struct snd_pcm_substream *subs)
231 struct snd_emu8000 *emu = snd_pcm_substream_chip(subs);
232 struct snd_emu8k_pcm *rec;
233 struct snd_pcm_runtime *runtime = subs->runtime;
235 rec = kzalloc(sizeof(*rec), GFP_KERNEL);
240 rec->substream = subs;
241 runtime->private_data = rec;
243 spin_lock_init(&rec->timer_lock);
244 timer_setup(&rec->timer, emu8k_pcm_timer_func, 0);
246 runtime->hw = emu8k_pcm_hw;
247 runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3;
248 runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2;
250 /* use timer to update periods.. (specified in msec) */
251 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
252 (1000000 + HZ - 1) / HZ, UINT_MAX);
257 static int emu8k_pcm_close(struct snd_pcm_substream *subs)
259 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
261 subs->runtime->private_data = NULL;
266 * calculate pitch target
268 static int calc_pitch_target(int pitch)
270 int ptarget = 1 << (pitch >> 12);
271 if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710;
272 if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710;
273 if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710;
274 ptarget += (ptarget >> 1);
275 if (ptarget > 0xffff) ptarget = 0xffff;
282 static void setup_voice(struct snd_emu8k_pcm *rec, int ch)
284 struct snd_emu8000 *hw = rec->emu;
287 /* channel to be silent and idle */
288 EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
289 EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
290 EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
291 EMU8000_PTRX_WRITE(hw, ch, 0);
292 EMU8000_CPF_WRITE(hw, ch, 0);
295 EMU8000_IP_WRITE(hw, ch, rec->pitch);
296 /* set envelope parameters */
297 EMU8000_ENVVAL_WRITE(hw, ch, 0x8000);
298 EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f);
299 EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f);
300 EMU8000_ENVVOL_WRITE(hw, ch, 0x8000);
301 EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f);
302 /* decay/sustain parameter for volume envelope is used
303 for triggerg the voice */
304 /* modulation envelope heights */
305 EMU8000_PEFE_WRITE(hw, ch, 0x0);
307 EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000);
308 EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000);
309 /* lfo1 pitch & cutoff shift */
310 EMU8000_FMMOD_WRITE(hw, ch, 0);
311 /* lfo1 volume & freq */
312 EMU8000_TREMFRQ_WRITE(hw, ch, 0);
313 /* lfo2 pitch & freq */
314 EMU8000_FM2FRQ2_WRITE(hw, ch, 0);
315 /* pan & loop start */
316 temp = rec->panning[ch];
317 temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1);
318 EMU8000_PSST_WRITE(hw, ch, temp);
319 /* chorus & loop end (chorus 8bit, MSB) */
321 temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1);
322 EMU8000_CSL_WRITE(hw, ch, temp);
323 /* Q & current address (Q 4bit value, MSB) */
325 temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1);
326 EMU8000_CCCA_WRITE(hw, ch, temp);
327 /* clear unknown registers */
328 EMU8000_00A0_WRITE(hw, ch, 0);
329 EMU8000_0080_WRITE(hw, ch, 0);
335 static void start_voice(struct snd_emu8k_pcm *rec, int ch)
338 struct snd_emu8000 *hw = rec->emu;
339 unsigned int temp, aux;
340 int pt = calc_pitch_target(rec->pitch);
342 /* cutoff and volume */
343 EMU8000_IFATN_WRITE(hw, ch, 0xff00);
344 EMU8000_VTFT_WRITE(hw, ch, 0xffff);
345 EMU8000_CVCF_WRITE(hw, ch, 0xffff);
346 /* trigger envelope */
347 EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f);
348 /* set reverb and pitch target */
350 if (rec->panning[ch] == 0)
353 aux = (-rec->panning[ch]) & 0xff;
354 temp = (temp << 8) | (pt << 16) | aux;
355 EMU8000_PTRX_WRITE(hw, ch, temp);
356 EMU8000_CPF_WRITE(hw, ch, pt << 16);
359 spin_lock_irqsave(&rec->timer_lock, flags);
360 if (! rec->timer_running) {
361 mod_timer(&rec->timer, jiffies + 1);
362 rec->timer_running = 1;
364 spin_unlock_irqrestore(&rec->timer_lock, flags);
368 * stop the voice immediately
370 static void stop_voice(struct snd_emu8k_pcm *rec, int ch)
373 struct snd_emu8000 *hw = rec->emu;
375 EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
378 spin_lock_irqsave(&rec->timer_lock, flags);
379 if (rec->timer_running) {
380 del_timer(&rec->timer);
381 rec->timer_running = 0;
383 spin_unlock_irqrestore(&rec->timer_lock, flags);
386 static int emu8k_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
388 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
392 case SNDRV_PCM_TRIGGER_START:
393 for (ch = 0; ch < rec->voices; ch++)
394 start_voice(rec, ch);
397 case SNDRV_PCM_TRIGGER_STOP:
399 for (ch = 0; ch < rec->voices; ch++)
414 * this macro should be inserted in the copy/silence loops
415 * to reduce the latency. without this, the system will hang up
416 * during the whole loop.
418 #define CHECK_SCHEDULER() \
421 if (signal_pending(current))\
426 COPY_USER, COPY_KERNEL, FILL_SILENCE,
429 #define GET_VAL(sval, buf, mode) \
439 if (get_user(sval, (unsigned short __user *)buf)) \
446 #ifdef USE_NONINTERLEAVE
448 #define LOOP_WRITE(rec, offset, _buf, count, mode) \
450 struct snd_emu8000 *emu = (rec)->emu; \
451 unsigned short *buf = (unsigned short *)(_buf); \
452 snd_emu8000_write_wait(emu, 1); \
453 EMU8000_SMALW_WRITE(emu, offset); \
454 while (count > 0) { \
455 unsigned short sval; \
457 GET_VAL(sval, buf, mode); \
458 EMU8000_SMLD_WRITE(emu, sval); \
463 /* copy one channel block */
464 static int emu8k_pcm_copy(struct snd_pcm_substream *subs,
465 int voice, unsigned long pos,
466 void __user *src, unsigned long count)
468 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
470 /* convert to word unit */
471 pos = (pos << 1) + rec->loop_start[voice];
473 LOOP_WRITE(rec, pos, src, count, COPY_USER);
477 static int emu8k_pcm_copy_kernel(struct snd_pcm_substream *subs,
478 int voice, unsigned long pos,
479 void *src, unsigned long count)
481 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
483 /* convert to word unit */
484 pos = (pos << 1) + rec->loop_start[voice];
486 LOOP_WRITE(rec, pos, src, count, COPY_KERNEL);
490 /* make a channel block silence */
491 static int emu8k_pcm_silence(struct snd_pcm_substream *subs,
492 int voice, unsigned long pos, unsigned long count)
494 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
496 /* convert to word unit */
497 pos = (pos << 1) + rec->loop_start[voice];
499 LOOP_WRITE(rec, pos, NULL, count, FILL_SILENCE);
503 #else /* interleave */
505 #define LOOP_WRITE(rec, pos, _buf, count, mode) \
507 struct snd_emu8000 *emu = rec->emu; \
508 unsigned short *buf = (unsigned short *)(_buf); \
509 snd_emu8000_write_wait(emu, 1); \
510 EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]); \
511 if (rec->voices > 1) \
512 EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]); \
513 while (count > 0) { \
514 unsigned short sval; \
516 GET_VAL(sval, buf, mode); \
517 EMU8000_SMLD_WRITE(emu, sval); \
518 if (rec->voices > 1) { \
520 GET_VAL(sval, buf, mode); \
521 EMU8000_SMRD_WRITE(emu, sval); \
529 * copy the interleaved data can be done easily by using
530 * DMA "left" and "right" channels on emu8k engine.
532 static int emu8k_pcm_copy(struct snd_pcm_substream *subs,
533 int voice, unsigned long pos,
534 void __user *src, unsigned long count)
536 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
538 /* convert to frames */
539 pos = bytes_to_frames(subs->runtime, pos);
540 count = bytes_to_frames(subs->runtime, count);
541 LOOP_WRITE(rec, pos, src, count, COPY_USER);
545 static int emu8k_pcm_copy_kernel(struct snd_pcm_substream *subs,
546 int voice, unsigned long pos,
547 void *src, unsigned long count)
549 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
551 /* convert to frames */
552 pos = bytes_to_frames(subs->runtime, pos);
553 count = bytes_to_frames(subs->runtime, count);
554 LOOP_WRITE(rec, pos, src, count, COPY_KERNEL);
558 static int emu8k_pcm_silence(struct snd_pcm_substream *subs,
559 int voice, unsigned long pos, unsigned long count)
561 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
563 /* convert to frames */
564 pos = bytes_to_frames(subs->runtime, pos);
565 count = bytes_to_frames(subs->runtime, count);
566 LOOP_WRITE(rec, pos, NULL, count, FILL_SILENCE);
573 * allocate a memory block
575 static int emu8k_pcm_hw_params(struct snd_pcm_substream *subs,
576 struct snd_pcm_hw_params *hw_params)
578 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
581 /* reallocation - release the old block */
582 snd_util_mem_free(rec->emu->memhdr, rec->block);
586 rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4;
587 rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes);
590 rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */
591 /* at least dma_bytes must be set for non-interleaved mode */
592 subs->dma_buffer.bytes = params_buffer_bytes(hw_params);
598 * free the memory block
600 static int emu8k_pcm_hw_free(struct snd_pcm_substream *subs)
602 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
606 for (ch = 0; ch < rec->voices; ch++)
607 stop_voice(rec, ch); // to be sure
608 if (rec->dram_opened)
609 emu8k_close_dram(rec->emu);
610 snd_util_mem_free(rec->emu->memhdr, rec->block);
618 static int emu8k_pcm_prepare(struct snd_pcm_substream *subs)
620 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
622 rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate);
626 rec->buf_size = subs->runtime->buffer_size;
627 rec->period_size = subs->runtime->period_size;
628 rec->voices = subs->runtime->channels;
629 rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE;
631 rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE;
632 if (rec->voices > 1) {
633 rec->panning[0] = 0xff;
634 rec->panning[1] = 0x00;
636 rec->panning[0] = 0x80;
638 if (! rec->dram_opened) {
641 snd_emux_terminate_all(rec->emu->emu);
642 if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0)
644 rec->dram_opened = 1;
646 /* clear loop blanks */
647 snd_emu8000_write_wait(rec->emu, 0);
648 EMU8000_SMALW_WRITE(rec->emu, rec->offset);
649 for (i = 0; i < LOOP_BLANK_SIZE; i++)
650 EMU8000_SMLD_WRITE(rec->emu, 0);
651 for (ch = 0; ch < rec->voices; ch++) {
652 EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size);
653 for (i = 0; i < LOOP_BLANK_SIZE; i++)
654 EMU8000_SMLD_WRITE(rec->emu, 0);
664 static snd_pcm_uframes_t emu8k_pcm_pointer(struct snd_pcm_substream *subs)
666 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
668 return emu8k_get_curpos(rec, 0);
673 static const struct snd_pcm_ops emu8k_pcm_ops = {
674 .open = emu8k_pcm_open,
675 .close = emu8k_pcm_close,
676 .ioctl = snd_pcm_lib_ioctl,
677 .hw_params = emu8k_pcm_hw_params,
678 .hw_free = emu8k_pcm_hw_free,
679 .prepare = emu8k_pcm_prepare,
680 .trigger = emu8k_pcm_trigger,
681 .pointer = emu8k_pcm_pointer,
682 .copy_user = emu8k_pcm_copy,
683 .copy_kernel = emu8k_pcm_copy_kernel,
684 .fill_silence = emu8k_pcm_silence,
688 static void snd_emu8000_pcm_free(struct snd_pcm *pcm)
690 struct snd_emu8000 *emu = pcm->private_data;
694 int snd_emu8000_pcm_new(struct snd_card *card, struct snd_emu8000 *emu, int index)
699 if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0)
701 pcm->private_data = emu;
702 pcm->private_free = snd_emu8000_pcm_free;
703 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops);
706 snd_device_register(card, pcm);