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"
22 #include <linux/init.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
27 * define the following if you want to use this pcm with non-interleaved mode
29 /* #define USE_NONINTERLEAVE */
31 /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set
32 * mmap_emulation flag to 1 in your .asoundrc, such like
44 * besides, for the time being, the non-interleaved mode doesn't work well on
49 typedef struct snd_emu8k_pcm emu8k_pcm_t;
51 struct snd_emu8k_pcm {
53 snd_pcm_substream_t *substream;
55 unsigned int allocated_bytes;
56 snd_util_memblk_t *block;
58 unsigned int buf_size;
59 unsigned int period_size;
60 unsigned int loop_start[2];
66 unsigned int dram_opened: 1;
67 unsigned int running: 1;
68 unsigned int timer_running: 1;
69 struct timer_list timer;
70 spinlock_t timer_lock;
73 #define LOOP_BLANK_SIZE 8
77 * open up channels for the simultaneous data transfer and playback
80 emu8k_open_dram_for_pcm(emu8000_t *emu, int channels)
84 /* reserve up to 2 voices for playback */
85 snd_emux_lock_voice(emu->emu, 0);
87 snd_emux_lock_voice(emu->emu, 1);
89 /* reserve 28 voices for loading */
90 for (i = channels + 1; i < EMU8000_DRAM_VOICES; i++) {
91 unsigned int mode = EMU8000_RAM_WRITE;
92 snd_emux_lock_voice(emu->emu, i);
93 #ifndef USE_NONINTERLEAVE
94 if (channels > 1 && (i & 1) != 0)
95 mode |= EMU8000_RAM_RIGHT;
97 snd_emu8000_dma_chan(emu, i, mode);
100 /* assign voice 31 and 32 to ROM */
101 EMU8000_VTFT_WRITE(emu, 30, 0);
102 EMU8000_PSST_WRITE(emu, 30, 0x1d8);
103 EMU8000_CSL_WRITE(emu, 30, 0x1e0);
104 EMU8000_CCCA_WRITE(emu, 30, 0x1d8);
105 EMU8000_VTFT_WRITE(emu, 31, 0);
106 EMU8000_PSST_WRITE(emu, 31, 0x1d8);
107 EMU8000_CSL_WRITE(emu, 31, 0x1e0);
108 EMU8000_CCCA_WRITE(emu, 31, 0x1d8);
116 snd_emu8000_write_wait(emu8000_t *emu, int can_schedule)
118 while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
120 set_current_state(TASK_INTERRUPTIBLE);
122 if (signal_pending(current))
132 emu8k_close_dram(emu8000_t *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 snd_pcm_hardware_t 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(emu8k_pcm_t *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(unsigned long data)
198 emu8k_pcm_t *rec = (emu8k_pcm_t *)data;
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 rec->timer.expires = jiffies + 1;
213 add_timer(&rec->timer);
216 if (rec->period_pos >= (int)rec->period_size) {
217 rec->period_pos %= rec->period_size;
218 spin_unlock(&rec->timer_lock);
219 snd_pcm_period_elapsed(rec->substream);
222 spin_unlock(&rec->timer_lock);
228 * creating an instance here
230 static int emu8k_pcm_open(snd_pcm_substream_t *subs)
232 emu8000_t *emu = snd_pcm_substream_chip(subs);
234 snd_pcm_runtime_t *runtime = subs->runtime;
236 rec = kzalloc(sizeof(*rec), GFP_KERNEL);
241 rec->substream = subs;
242 runtime->private_data = rec;
244 spin_lock_init(&rec->timer_lock);
245 init_timer(&rec->timer);
246 rec->timer.function = emu8k_pcm_timer_func;
247 rec->timer.data = (unsigned long)rec;
249 runtime->hw = emu8k_pcm_hw;
250 runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3;
251 runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2;
253 /* use timer to update periods.. (specified in msec) */
254 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
255 (1000000 + HZ - 1) / HZ, UINT_MAX);
260 static int emu8k_pcm_close(snd_pcm_substream_t *subs)
262 emu8k_pcm_t *rec = subs->runtime->private_data;
264 subs->runtime->private_data = NULL;
269 * calculate pitch target
271 static int calc_pitch_target(int pitch)
273 int ptarget = 1 << (pitch >> 12);
274 if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710;
275 if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710;
276 if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710;
277 ptarget += (ptarget >> 1);
278 if (ptarget > 0xffff) ptarget = 0xffff;
285 static void setup_voice(emu8k_pcm_t *rec, int ch)
287 emu8000_t *hw = rec->emu;
290 /* channel to be silent and idle */
291 EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
292 EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
293 EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
294 EMU8000_PTRX_WRITE(hw, ch, 0);
295 EMU8000_CPF_WRITE(hw, ch, 0);
298 EMU8000_IP_WRITE(hw, ch, rec->pitch);
299 /* set envelope parameters */
300 EMU8000_ENVVAL_WRITE(hw, ch, 0x8000);
301 EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f);
302 EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f);
303 EMU8000_ENVVOL_WRITE(hw, ch, 0x8000);
304 EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f);
305 /* decay/sustain parameter for volume envelope is used
306 for triggerg the voice */
307 /* modulation envelope heights */
308 EMU8000_PEFE_WRITE(hw, ch, 0x0);
310 EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000);
311 EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000);
312 /* lfo1 pitch & cutoff shift */
313 EMU8000_FMMOD_WRITE(hw, ch, 0);
314 /* lfo1 volume & freq */
315 EMU8000_TREMFRQ_WRITE(hw, ch, 0);
316 /* lfo2 pitch & freq */
317 EMU8000_FM2FRQ2_WRITE(hw, ch, 0);
318 /* pan & loop start */
319 temp = rec->panning[ch];
320 temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1);
321 EMU8000_PSST_WRITE(hw, ch, temp);
322 /* chorus & loop end (chorus 8bit, MSB) */
324 temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1);
325 EMU8000_CSL_WRITE(hw, ch, temp);
326 /* Q & current address (Q 4bit value, MSB) */
328 temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1);
329 EMU8000_CCCA_WRITE(hw, ch, temp);
330 /* clear unknown registers */
331 EMU8000_00A0_WRITE(hw, ch, 0);
332 EMU8000_0080_WRITE(hw, ch, 0);
338 static void start_voice(emu8k_pcm_t *rec, int ch)
341 emu8000_t *hw = rec->emu;
342 unsigned int temp, aux;
343 int pt = calc_pitch_target(rec->pitch);
345 /* cutoff and volume */
346 EMU8000_IFATN_WRITE(hw, ch, 0xff00);
347 EMU8000_VTFT_WRITE(hw, ch, 0xffff);
348 EMU8000_CVCF_WRITE(hw, ch, 0xffff);
349 /* trigger envelope */
350 EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f);
351 /* set reverb and pitch target */
353 if (rec->panning[ch] == 0)
356 aux = (-rec->panning[ch]) & 0xff;
357 temp = (temp << 8) | (pt << 16) | aux;
358 EMU8000_PTRX_WRITE(hw, ch, temp);
359 EMU8000_CPF_WRITE(hw, ch, pt << 16);
362 spin_lock_irqsave(&rec->timer_lock, flags);
363 if (! rec->timer_running) {
364 rec->timer.expires = jiffies + 1;
365 add_timer(&rec->timer);
366 rec->timer_running = 1;
368 spin_unlock_irqrestore(&rec->timer_lock, flags);
372 * stop the voice immediately
374 static void stop_voice(emu8k_pcm_t *rec, int ch)
377 emu8000_t *hw = rec->emu;
379 EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
382 spin_lock_irqsave(&rec->timer_lock, flags);
383 if (rec->timer_running) {
384 del_timer(&rec->timer);
385 rec->timer_running = 0;
387 spin_unlock_irqrestore(&rec->timer_lock, flags);
390 static int emu8k_pcm_trigger(snd_pcm_substream_t *subs, int cmd)
392 emu8k_pcm_t *rec = subs->runtime->private_data;
396 case SNDRV_PCM_TRIGGER_START:
397 for (ch = 0; ch < rec->voices; ch++)
398 start_voice(rec, ch);
401 case SNDRV_PCM_TRIGGER_STOP:
403 for (ch = 0; ch < rec->voices; ch++)
418 * this macro should be inserted in the copy/silence loops
419 * to reduce the latency. without this, the system will hang up
420 * during the whole loop.
422 #define CHECK_SCHEDULER() \
425 if (signal_pending(current))\
430 #ifdef USE_NONINTERLEAVE
431 /* copy one channel block */
432 static int emu8k_transfer_block(emu8000_t *emu, int offset, unsigned short *buf, int count)
434 EMU8000_SMALW_WRITE(emu, offset);
439 EMU8000_SMLD_WRITE(emu, sval);
446 static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
448 snd_pcm_uframes_t pos,
450 snd_pcm_uframes_t count)
452 emu8k_pcm_t *rec = subs->runtime->private_data;
453 emu8000_t *emu = rec->emu;
455 snd_emu8000_write_wait(emu, 1);
457 unsigned short *buf = src;
459 count /= rec->voices;
460 for (i = 0; i < rec->voices; i++) {
461 err = emu8k_transfer_block(emu, pos + rec->loop_start[i], buf, count);
468 return emu8k_transfer_block(emu, pos + rec->loop_start[voice], src, count);
472 /* make a channel block silence */
473 static int emu8k_silence_block(emu8000_t *emu, int offset, int count)
475 EMU8000_SMALW_WRITE(emu, offset);
478 EMU8000_SMLD_WRITE(emu, 0);
484 static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
486 snd_pcm_uframes_t pos,
487 snd_pcm_uframes_t count)
489 emu8k_pcm_t *rec = subs->runtime->private_data;
490 emu8000_t *emu = rec->emu;
492 snd_emu8000_write_wait(emu, 1);
493 if (voice == -1 && rec->voices == 1)
497 err = emu8k_silence_block(emu, pos + rec->loop_start[0], count / 2);
500 return emu8k_silence_block(emu, pos + rec->loop_start[1], count / 2);
502 return emu8k_silence_block(emu, pos + rec->loop_start[voice], count);
506 #else /* interleave */
509 * copy the interleaved data can be done easily by using
510 * DMA "left" and "right" channels on emu8k engine.
512 static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
514 snd_pcm_uframes_t pos,
516 snd_pcm_uframes_t count)
518 emu8k_pcm_t *rec = subs->runtime->private_data;
519 emu8000_t *emu = rec->emu;
520 unsigned short __user *buf = src;
522 snd_emu8000_write_wait(emu, 1);
523 EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]);
525 EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]);
527 while (count-- > 0) {
531 EMU8000_SMLD_WRITE(emu, sval);
533 if (rec->voices > 1) {
536 EMU8000_SMRD_WRITE(emu, sval);
543 static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
545 snd_pcm_uframes_t pos,
546 snd_pcm_uframes_t count)
548 emu8k_pcm_t *rec = subs->runtime->private_data;
549 emu8000_t *emu = rec->emu;
551 snd_emu8000_write_wait(emu, 1);
552 EMU8000_SMALW_WRITE(emu, rec->loop_start[0] + pos);
554 EMU8000_SMARW_WRITE(emu, rec->loop_start[1] + pos);
555 while (count-- > 0) {
557 EMU8000_SMLD_WRITE(emu, 0);
558 if (rec->voices > 1) {
560 EMU8000_SMRD_WRITE(emu, 0);
569 * allocate a memory block
571 static int emu8k_pcm_hw_params(snd_pcm_substream_t *subs,
572 snd_pcm_hw_params_t *hw_params)
574 emu8k_pcm_t *rec = subs->runtime->private_data;
577 /* reallocation - release the old block */
578 snd_util_mem_free(rec->emu->memhdr, rec->block);
582 rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4;
583 rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes);
586 rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */
587 /* at least dma_bytes must be set for non-interleaved mode */
588 subs->dma_buffer.bytes = params_buffer_bytes(hw_params);
594 * free the memory block
596 static int emu8k_pcm_hw_free(snd_pcm_substream_t *subs)
598 emu8k_pcm_t *rec = subs->runtime->private_data;
602 for (ch = 0; ch < rec->voices; ch++)
603 stop_voice(rec, ch); // to be sure
604 if (rec->dram_opened)
605 emu8k_close_dram(rec->emu);
606 snd_util_mem_free(rec->emu->memhdr, rec->block);
614 static int emu8k_pcm_prepare(snd_pcm_substream_t *subs)
616 emu8k_pcm_t *rec = subs->runtime->private_data;
618 rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate);
622 rec->buf_size = subs->runtime->buffer_size;
623 rec->period_size = subs->runtime->period_size;
624 rec->voices = subs->runtime->channels;
625 rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE;
627 rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE;
628 if (rec->voices > 1) {
629 rec->panning[0] = 0xff;
630 rec->panning[1] = 0x00;
632 rec->panning[0] = 0x80;
634 if (! rec->dram_opened) {
637 snd_emux_terminate_all(rec->emu->emu);
638 if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0)
640 rec->dram_opened = 1;
642 /* clear loop blanks */
643 snd_emu8000_write_wait(rec->emu, 0);
644 EMU8000_SMALW_WRITE(rec->emu, rec->offset);
645 for (i = 0; i < LOOP_BLANK_SIZE; i++)
646 EMU8000_SMLD_WRITE(rec->emu, 0);
647 for (ch = 0; ch < rec->voices; ch++) {
648 EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size);
649 for (i = 0; i < LOOP_BLANK_SIZE; i++)
650 EMU8000_SMLD_WRITE(rec->emu, 0);
660 static snd_pcm_uframes_t emu8k_pcm_pointer(snd_pcm_substream_t *subs)
662 emu8k_pcm_t *rec = subs->runtime->private_data;
664 return emu8k_get_curpos(rec, 0);
669 static snd_pcm_ops_t emu8k_pcm_ops = {
670 .open = emu8k_pcm_open,
671 .close = emu8k_pcm_close,
672 .ioctl = snd_pcm_lib_ioctl,
673 .hw_params = emu8k_pcm_hw_params,
674 .hw_free = emu8k_pcm_hw_free,
675 .prepare = emu8k_pcm_prepare,
676 .trigger = emu8k_pcm_trigger,
677 .pointer = emu8k_pcm_pointer,
678 .copy = emu8k_pcm_copy,
679 .silence = emu8k_pcm_silence,
683 static void snd_emu8000_pcm_free(snd_pcm_t *pcm)
685 emu8000_t *emu = pcm->private_data;
689 int snd_emu8000_pcm_new(snd_card_t *card, emu8000_t *emu, int index)
694 if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0)
696 pcm->private_data = emu;
697 pcm->private_free = snd_emu8000_pcm_free;
698 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops);
701 snd_device_register(card, pcm);