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 <linux/slab.h>
24 #include <sound/initval.h>
25 #include <sound/pcm.h>
28 * define the following if you want to use this pcm with non-interleaved mode
30 /* #define USE_NONINTERLEAVE */
32 /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set
33 * mmap_emulation flag to 1 in your .asoundrc, such like
45 * besides, for the time being, the non-interleaved mode doesn't work well on
50 struct snd_emu8k_pcm
{
51 struct snd_emu8000
*emu
;
52 struct snd_pcm_substream
*substream
;
54 unsigned int allocated_bytes
;
55 struct snd_util_memblk
*block
;
57 unsigned int buf_size
;
58 unsigned int period_size
;
59 unsigned int loop_start
[2];
65 unsigned int dram_opened
: 1;
66 unsigned int running
: 1;
67 unsigned int timer_running
: 1;
68 struct timer_list timer
;
69 spinlock_t timer_lock
;
72 #define LOOP_BLANK_SIZE 8
76 * open up channels for the simultaneous data transfer and playback
79 emu8k_open_dram_for_pcm(struct snd_emu8000
*emu
, int channels
)
83 /* reserve up to 2 voices for playback */
84 snd_emux_lock_voice(emu
->emu
, 0);
86 snd_emux_lock_voice(emu
->emu
, 1);
88 /* reserve 28 voices for loading */
89 for (i
= channels
+ 1; i
< EMU8000_DRAM_VOICES
; i
++) {
90 unsigned int mode
= EMU8000_RAM_WRITE
;
91 snd_emux_lock_voice(emu
->emu
, i
);
92 #ifndef USE_NONINTERLEAVE
93 if (channels
> 1 && (i
& 1) != 0)
94 mode
|= EMU8000_RAM_RIGHT
;
96 snd_emu8000_dma_chan(emu
, i
, mode
);
99 /* assign voice 31 and 32 to ROM */
100 EMU8000_VTFT_WRITE(emu
, 30, 0);
101 EMU8000_PSST_WRITE(emu
, 30, 0x1d8);
102 EMU8000_CSL_WRITE(emu
, 30, 0x1e0);
103 EMU8000_CCCA_WRITE(emu
, 30, 0x1d8);
104 EMU8000_VTFT_WRITE(emu
, 31, 0);
105 EMU8000_PSST_WRITE(emu
, 31, 0x1d8);
106 EMU8000_CSL_WRITE(emu
, 31, 0x1e0);
107 EMU8000_CCCA_WRITE(emu
, 31, 0x1d8);
115 snd_emu8000_write_wait(struct snd_emu8000
*emu
, int can_schedule
)
117 while ((EMU8000_SMALW_READ(emu
) & 0x80000000) != 0) {
119 schedule_timeout_interruptible(1);
120 if (signal_pending(current
))
130 emu8k_close_dram(struct snd_emu8000
*emu
)
134 for (i
= 0; i
< 2; i
++)
135 snd_emux_unlock_voice(emu
->emu
, i
);
136 for (; i
< EMU8000_DRAM_VOICES
; i
++) {
137 snd_emu8000_dma_chan(emu
, i
, EMU8000_RAM_CLOSE
);
138 snd_emux_unlock_voice(emu
->emu
, i
);
143 * convert Hz to AWE32 rate offset (see emux/soundfont.c)
146 #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */
147 #define SAMPLERATE_RATIO 4096
149 static int calc_rate_offset(int hz
)
151 return snd_sf_linear_to_log(hz
, OFFSET_SAMPLERATE
, SAMPLERATE_RATIO
);
158 static struct snd_pcm_hardware emu8k_pcm_hw
= {
159 #ifdef USE_NONINTERLEAVE
160 .info
= SNDRV_PCM_INFO_NONINTERLEAVED
,
162 .info
= SNDRV_PCM_INFO_INTERLEAVED
,
164 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
165 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
170 .buffer_bytes_max
= (128*1024),
171 .period_bytes_min
= 1024,
172 .period_bytes_max
= (128*1024),
180 * get the current position at the given channel from CCCA register
182 static inline int emu8k_get_curpos(struct snd_emu8k_pcm
*rec
, int ch
)
184 int val
= EMU8000_CCCA_READ(rec
->emu
, ch
) & 0xfffffff;
185 val
-= rec
->loop_start
[ch
] - 1;
191 * timer interrupt handler
192 * check the current position and update the period if necessary.
194 static void emu8k_pcm_timer_func(unsigned long data
)
196 struct snd_emu8k_pcm
*rec
= (struct snd_emu8k_pcm
*)data
;
199 spin_lock(&rec
->timer_lock
);
200 /* update the current pointer */
201 ptr
= emu8k_get_curpos(rec
, 0);
202 if (ptr
< rec
->last_ptr
)
203 delta
= ptr
+ rec
->buf_size
- rec
->last_ptr
;
205 delta
= ptr
- rec
->last_ptr
;
206 rec
->period_pos
+= delta
;
209 /* reprogram timer */
210 mod_timer(&rec
->timer
, jiffies
+ 1);
213 if (rec
->period_pos
>= (int)rec
->period_size
) {
214 rec
->period_pos
%= rec
->period_size
;
215 spin_unlock(&rec
->timer_lock
);
216 snd_pcm_period_elapsed(rec
->substream
);
219 spin_unlock(&rec
->timer_lock
);
225 * creating an instance here
227 static int emu8k_pcm_open(struct snd_pcm_substream
*subs
)
229 struct snd_emu8000
*emu
= snd_pcm_substream_chip(subs
);
230 struct snd_emu8k_pcm
*rec
;
231 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
233 rec
= kzalloc(sizeof(*rec
), GFP_KERNEL
);
238 rec
->substream
= subs
;
239 runtime
->private_data
= rec
;
241 spin_lock_init(&rec
->timer_lock
);
242 setup_timer(&rec
->timer
, emu8k_pcm_timer_func
, (unsigned long)rec
);
244 runtime
->hw
= emu8k_pcm_hw
;
245 runtime
->hw
.buffer_bytes_max
= emu
->mem_size
- LOOP_BLANK_SIZE
* 3;
246 runtime
->hw
.period_bytes_max
= runtime
->hw
.buffer_bytes_max
/ 2;
248 /* use timer to update periods.. (specified in msec) */
249 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
250 (1000000 + HZ
- 1) / HZ
, UINT_MAX
);
255 static int emu8k_pcm_close(struct snd_pcm_substream
*subs
)
257 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
259 subs
->runtime
->private_data
= NULL
;
264 * calculate pitch target
266 static int calc_pitch_target(int pitch
)
268 int ptarget
= 1 << (pitch
>> 12);
269 if (pitch
& 0x800) ptarget
+= (ptarget
* 0x102e) / 0x2710;
270 if (pitch
& 0x400) ptarget
+= (ptarget
* 0x764) / 0x2710;
271 if (pitch
& 0x200) ptarget
+= (ptarget
* 0x389) / 0x2710;
272 ptarget
+= (ptarget
>> 1);
273 if (ptarget
> 0xffff) ptarget
= 0xffff;
280 static void setup_voice(struct snd_emu8k_pcm
*rec
, int ch
)
282 struct snd_emu8000
*hw
= rec
->emu
;
285 /* channel to be silent and idle */
286 EMU8000_DCYSUSV_WRITE(hw
, ch
, 0x0080);
287 EMU8000_VTFT_WRITE(hw
, ch
, 0x0000FFFF);
288 EMU8000_CVCF_WRITE(hw
, ch
, 0x0000FFFF);
289 EMU8000_PTRX_WRITE(hw
, ch
, 0);
290 EMU8000_CPF_WRITE(hw
, ch
, 0);
293 EMU8000_IP_WRITE(hw
, ch
, rec
->pitch
);
294 /* set envelope parameters */
295 EMU8000_ENVVAL_WRITE(hw
, ch
, 0x8000);
296 EMU8000_ATKHLD_WRITE(hw
, ch
, 0x7f7f);
297 EMU8000_DCYSUS_WRITE(hw
, ch
, 0x7f7f);
298 EMU8000_ENVVOL_WRITE(hw
, ch
, 0x8000);
299 EMU8000_ATKHLDV_WRITE(hw
, ch
, 0x7f7f);
300 /* decay/sustain parameter for volume envelope is used
301 for triggerg the voice */
302 /* modulation envelope heights */
303 EMU8000_PEFE_WRITE(hw
, ch
, 0x0);
305 EMU8000_LFO1VAL_WRITE(hw
, ch
, 0x8000);
306 EMU8000_LFO2VAL_WRITE(hw
, ch
, 0x8000);
307 /* lfo1 pitch & cutoff shift */
308 EMU8000_FMMOD_WRITE(hw
, ch
, 0);
309 /* lfo1 volume & freq */
310 EMU8000_TREMFRQ_WRITE(hw
, ch
, 0);
311 /* lfo2 pitch & freq */
312 EMU8000_FM2FRQ2_WRITE(hw
, ch
, 0);
313 /* pan & loop start */
314 temp
= rec
->panning
[ch
];
315 temp
= (temp
<<24) | ((unsigned int)rec
->loop_start
[ch
] - 1);
316 EMU8000_PSST_WRITE(hw
, ch
, temp
);
317 /* chorus & loop end (chorus 8bit, MSB) */
319 temp
= (temp
<< 24) | ((unsigned int)rec
->loop_start
[ch
] + rec
->buf_size
- 1);
320 EMU8000_CSL_WRITE(hw
, ch
, temp
);
321 /* Q & current address (Q 4bit value, MSB) */
323 temp
= (temp
<< 28) | ((unsigned int)rec
->loop_start
[ch
] - 1);
324 EMU8000_CCCA_WRITE(hw
, ch
, temp
);
325 /* clear unknown registers */
326 EMU8000_00A0_WRITE(hw
, ch
, 0);
327 EMU8000_0080_WRITE(hw
, ch
, 0);
333 static void start_voice(struct snd_emu8k_pcm
*rec
, int ch
)
336 struct snd_emu8000
*hw
= rec
->emu
;
337 unsigned int temp
, aux
;
338 int pt
= calc_pitch_target(rec
->pitch
);
340 /* cutoff and volume */
341 EMU8000_IFATN_WRITE(hw
, ch
, 0xff00);
342 EMU8000_VTFT_WRITE(hw
, ch
, 0xffff);
343 EMU8000_CVCF_WRITE(hw
, ch
, 0xffff);
344 /* trigger envelope */
345 EMU8000_DCYSUSV_WRITE(hw
, ch
, 0x7f7f);
346 /* set reverb and pitch target */
348 if (rec
->panning
[ch
] == 0)
351 aux
= (-rec
->panning
[ch
]) & 0xff;
352 temp
= (temp
<< 8) | (pt
<< 16) | aux
;
353 EMU8000_PTRX_WRITE(hw
, ch
, temp
);
354 EMU8000_CPF_WRITE(hw
, ch
, pt
<< 16);
357 spin_lock_irqsave(&rec
->timer_lock
, flags
);
358 if (! rec
->timer_running
) {
359 mod_timer(&rec
->timer
, jiffies
+ 1);
360 rec
->timer_running
= 1;
362 spin_unlock_irqrestore(&rec
->timer_lock
, flags
);
366 * stop the voice immediately
368 static void stop_voice(struct snd_emu8k_pcm
*rec
, int ch
)
371 struct snd_emu8000
*hw
= rec
->emu
;
373 EMU8000_DCYSUSV_WRITE(hw
, ch
, 0x807F);
376 spin_lock_irqsave(&rec
->timer_lock
, flags
);
377 if (rec
->timer_running
) {
378 del_timer(&rec
->timer
);
379 rec
->timer_running
= 0;
381 spin_unlock_irqrestore(&rec
->timer_lock
, flags
);
384 static int emu8k_pcm_trigger(struct snd_pcm_substream
*subs
, int cmd
)
386 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
390 case SNDRV_PCM_TRIGGER_START
:
391 for (ch
= 0; ch
< rec
->voices
; ch
++)
392 start_voice(rec
, ch
);
395 case SNDRV_PCM_TRIGGER_STOP
:
397 for (ch
= 0; ch
< rec
->voices
; ch
++)
412 * this macro should be inserted in the copy/silence loops
413 * to reduce the latency. without this, the system will hang up
414 * during the whole loop.
416 #define CHECK_SCHEDULER() \
419 if (signal_pending(current))\
424 #ifdef USE_NONINTERLEAVE
425 /* copy one channel block */
426 static int emu8k_transfer_block(struct snd_emu8000
*emu
, int offset
, unsigned short *buf
, int count
)
428 EMU8000_SMALW_WRITE(emu
, offset
);
432 if (get_user(sval
, buf
))
434 EMU8000_SMLD_WRITE(emu
, sval
);
441 static int emu8k_pcm_copy(struct snd_pcm_substream
*subs
,
443 snd_pcm_uframes_t pos
,
445 snd_pcm_uframes_t count
)
447 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
448 struct snd_emu8000
*emu
= rec
->emu
;
450 snd_emu8000_write_wait(emu
, 1);
452 unsigned short *buf
= src
;
454 count
/= rec
->voices
;
455 for (i
= 0; i
< rec
->voices
; i
++) {
456 err
= emu8k_transfer_block(emu
, pos
+ rec
->loop_start
[i
], buf
, count
);
463 return emu8k_transfer_block(emu
, pos
+ rec
->loop_start
[voice
], src
, count
);
467 /* make a channel block silence */
468 static int emu8k_silence_block(struct snd_emu8000
*emu
, int offset
, int count
)
470 EMU8000_SMALW_WRITE(emu
, offset
);
473 EMU8000_SMLD_WRITE(emu
, 0);
479 static int emu8k_pcm_silence(struct snd_pcm_substream
*subs
,
481 snd_pcm_uframes_t pos
,
482 snd_pcm_uframes_t count
)
484 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
485 struct snd_emu8000
*emu
= rec
->emu
;
487 snd_emu8000_write_wait(emu
, 1);
488 if (voice
== -1 && rec
->voices
== 1)
492 err
= emu8k_silence_block(emu
, pos
+ rec
->loop_start
[0], count
/ 2);
495 return emu8k_silence_block(emu
, pos
+ rec
->loop_start
[1], count
/ 2);
497 return emu8k_silence_block(emu
, pos
+ rec
->loop_start
[voice
], count
);
501 #else /* interleave */
504 * copy the interleaved data can be done easily by using
505 * DMA "left" and "right" channels on emu8k engine.
507 static int emu8k_pcm_copy(struct snd_pcm_substream
*subs
,
509 snd_pcm_uframes_t pos
,
511 snd_pcm_uframes_t count
)
513 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
514 struct snd_emu8000
*emu
= rec
->emu
;
515 unsigned short __user
*buf
= src
;
517 snd_emu8000_write_wait(emu
, 1);
518 EMU8000_SMALW_WRITE(emu
, pos
+ rec
->loop_start
[0]);
520 EMU8000_SMARW_WRITE(emu
, pos
+ rec
->loop_start
[1]);
522 while (count
-- > 0) {
525 if (get_user(sval
, buf
))
527 EMU8000_SMLD_WRITE(emu
, sval
);
529 if (rec
->voices
> 1) {
531 if (get_user(sval
, buf
))
533 EMU8000_SMRD_WRITE(emu
, sval
);
540 static int emu8k_pcm_silence(struct snd_pcm_substream
*subs
,
542 snd_pcm_uframes_t pos
,
543 snd_pcm_uframes_t count
)
545 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
546 struct snd_emu8000
*emu
= rec
->emu
;
548 snd_emu8000_write_wait(emu
, 1);
549 EMU8000_SMALW_WRITE(emu
, rec
->loop_start
[0] + pos
);
551 EMU8000_SMARW_WRITE(emu
, rec
->loop_start
[1] + pos
);
552 while (count
-- > 0) {
554 EMU8000_SMLD_WRITE(emu
, 0);
555 if (rec
->voices
> 1) {
557 EMU8000_SMRD_WRITE(emu
, 0);
566 * allocate a memory block
568 static int emu8k_pcm_hw_params(struct snd_pcm_substream
*subs
,
569 struct snd_pcm_hw_params
*hw_params
)
571 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
574 /* reallocation - release the old block */
575 snd_util_mem_free(rec
->emu
->memhdr
, rec
->block
);
579 rec
->allocated_bytes
= params_buffer_bytes(hw_params
) + LOOP_BLANK_SIZE
* 4;
580 rec
->block
= snd_util_mem_alloc(rec
->emu
->memhdr
, rec
->allocated_bytes
);
583 rec
->offset
= EMU8000_DRAM_OFFSET
+ (rec
->block
->offset
>> 1); /* in word */
584 /* at least dma_bytes must be set for non-interleaved mode */
585 subs
->dma_buffer
.bytes
= params_buffer_bytes(hw_params
);
591 * free the memory block
593 static int emu8k_pcm_hw_free(struct snd_pcm_substream
*subs
)
595 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
599 for (ch
= 0; ch
< rec
->voices
; ch
++)
600 stop_voice(rec
, ch
); // to be sure
601 if (rec
->dram_opened
)
602 emu8k_close_dram(rec
->emu
);
603 snd_util_mem_free(rec
->emu
->memhdr
, rec
->block
);
611 static int emu8k_pcm_prepare(struct snd_pcm_substream
*subs
)
613 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
615 rec
->pitch
= 0xe000 + calc_rate_offset(subs
->runtime
->rate
);
619 rec
->buf_size
= subs
->runtime
->buffer_size
;
620 rec
->period_size
= subs
->runtime
->period_size
;
621 rec
->voices
= subs
->runtime
->channels
;
622 rec
->loop_start
[0] = rec
->offset
+ LOOP_BLANK_SIZE
;
624 rec
->loop_start
[1] = rec
->loop_start
[0] + rec
->buf_size
+ LOOP_BLANK_SIZE
;
625 if (rec
->voices
> 1) {
626 rec
->panning
[0] = 0xff;
627 rec
->panning
[1] = 0x00;
629 rec
->panning
[0] = 0x80;
631 if (! rec
->dram_opened
) {
634 snd_emux_terminate_all(rec
->emu
->emu
);
635 if ((err
= emu8k_open_dram_for_pcm(rec
->emu
, rec
->voices
)) != 0)
637 rec
->dram_opened
= 1;
639 /* clear loop blanks */
640 snd_emu8000_write_wait(rec
->emu
, 0);
641 EMU8000_SMALW_WRITE(rec
->emu
, rec
->offset
);
642 for (i
= 0; i
< LOOP_BLANK_SIZE
; i
++)
643 EMU8000_SMLD_WRITE(rec
->emu
, 0);
644 for (ch
= 0; ch
< rec
->voices
; ch
++) {
645 EMU8000_SMALW_WRITE(rec
->emu
, rec
->loop_start
[ch
] + rec
->buf_size
);
646 for (i
= 0; i
< LOOP_BLANK_SIZE
; i
++)
647 EMU8000_SMLD_WRITE(rec
->emu
, 0);
657 static snd_pcm_uframes_t
emu8k_pcm_pointer(struct snd_pcm_substream
*subs
)
659 struct snd_emu8k_pcm
*rec
= subs
->runtime
->private_data
;
661 return emu8k_get_curpos(rec
, 0);
666 static struct snd_pcm_ops emu8k_pcm_ops
= {
667 .open
= emu8k_pcm_open
,
668 .close
= emu8k_pcm_close
,
669 .ioctl
= snd_pcm_lib_ioctl
,
670 .hw_params
= emu8k_pcm_hw_params
,
671 .hw_free
= emu8k_pcm_hw_free
,
672 .prepare
= emu8k_pcm_prepare
,
673 .trigger
= emu8k_pcm_trigger
,
674 .pointer
= emu8k_pcm_pointer
,
675 .copy
= emu8k_pcm_copy
,
676 .silence
= emu8k_pcm_silence
,
680 static void snd_emu8000_pcm_free(struct snd_pcm
*pcm
)
682 struct snd_emu8000
*emu
= pcm
->private_data
;
686 int snd_emu8000_pcm_new(struct snd_card
*card
, struct snd_emu8000
*emu
, int index
)
691 if ((err
= snd_pcm_new(card
, "Emu8000 PCM", index
, 1, 0, &pcm
)) < 0)
693 pcm
->private_data
= emu
;
694 pcm
->private_free
= snd_emu8000_pcm_free
;
695 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &emu8k_pcm_ops
);
698 snd_device_register(card
, pcm
);