initial commit with v2.6.9
[linux-2.6.9-moxart.git] / sound / isa / sb / emu8000_pcm.c
bloba6fd8d7ca4558f26106aff9667c0ae22e2858aae
1 /*
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
34 * pcm.emu8k {
35 * type plug
36 * slave.pcm {
37 * type hw
38 * card 0
39 * device 1
40 * mmap_emulation 1
41 * }
42 * }
44 * besides, for the time being, the non-interleaved mode doesn't work well on
45 * alsa-lib...
49 typedef struct snd_emu8k_pcm emu8k_pcm_t;
51 struct snd_emu8k_pcm {
52 emu8000_t *emu;
53 snd_pcm_substream_t *substream;
55 unsigned int allocated_bytes;
56 snd_util_memblk_t *block;
57 unsigned int offset;
58 unsigned int buf_size;
59 unsigned int period_size;
60 unsigned int loop_start[2];
61 unsigned int pitch;
62 int panning[2];
63 int last_ptr;
64 int period_pos;
65 int voices;
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
79 static int
80 emu8k_open_dram_for_pcm(emu8000_t *emu, int channels)
82 int i;
84 /* reserve up to 2 voices for playback */
85 snd_emux_lock_voice(emu->emu, 0);
86 if (channels > 1)
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;
96 #endif
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);
110 return 0;
115 static void
116 snd_emu8000_write_wait(emu8000_t *emu, int can_schedule)
118 while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
119 if (can_schedule) {
120 set_current_state(TASK_INTERRUPTIBLE);
121 schedule_timeout(1);
122 if (signal_pending(current))
123 break;
129 * close all channels
131 static void
132 emu8k_close_dram(emu8000_t *emu)
134 int i;
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,
163 #else
164 .info = SNDRV_PCM_INFO_INTERLEAVED,
165 #endif
166 .formats = SNDRV_PCM_FMTBIT_S16_LE,
167 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
168 .rate_min = 4000,
169 .rate_max = 48000,
170 .channels_min = 1,
171 .channels_max = 2,
172 .buffer_bytes_max = (128*1024),
173 .period_bytes_min = 1024,
174 .period_bytes_max = (128*1024),
175 .periods_min = 2,
176 .periods_max = 1024,
177 .fifo_size = 0,
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;
188 return val;
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;
199 int ptr, delta;
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;
206 else
207 delta = ptr - rec->last_ptr;
208 rec->period_pos += delta;
209 rec->last_ptr = ptr;
211 /* reprogram timer */
212 rec->timer.expires = jiffies + 1;
213 add_timer(&rec->timer);
215 /* update period */
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);
220 return;
222 spin_unlock(&rec->timer_lock);
227 * open pcm
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);
233 emu8k_pcm_t *rec;
234 snd_pcm_runtime_t *runtime = subs->runtime;
236 rec = kcalloc(1, sizeof(*rec), GFP_KERNEL);
237 if (! rec)
238 return -ENOMEM;
240 rec->emu = emu;
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);
257 return 0;
260 static int emu8k_pcm_close(snd_pcm_substream_t *subs)
262 emu8k_pcm_t *rec = subs->runtime->private_data;
263 if (rec)
264 kfree(rec);
265 subs->runtime->private_data = NULL;
266 return 0;
270 * calculate pitch target
272 static int calc_pitch_target(int pitch)
274 int ptarget = 1 << (pitch >> 12);
275 if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710;
276 if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710;
277 if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710;
278 ptarget += (ptarget >> 1);
279 if (ptarget > 0xffff) ptarget = 0xffff;
280 return ptarget;
284 * set up the voice
286 static void setup_voice(emu8k_pcm_t *rec, int ch)
288 emu8000_t *hw = rec->emu;
289 unsigned int temp;
291 /* channel to be silent and idle */
292 EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
293 EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
294 EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
295 EMU8000_PTRX_WRITE(hw, ch, 0);
296 EMU8000_CPF_WRITE(hw, ch, 0);
298 /* pitch offset */
299 EMU8000_IP_WRITE(hw, ch, rec->pitch);
300 /* set envelope parameters */
301 EMU8000_ENVVAL_WRITE(hw, ch, 0x8000);
302 EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f);
303 EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f);
304 EMU8000_ENVVOL_WRITE(hw, ch, 0x8000);
305 EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f);
306 /* decay/sustain parameter for volume envelope is used
307 for triggerg the voice */
308 /* modulation envelope heights */
309 EMU8000_PEFE_WRITE(hw, ch, 0x0);
310 /* lfo1/2 delay */
311 EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000);
312 EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000);
313 /* lfo1 pitch & cutoff shift */
314 EMU8000_FMMOD_WRITE(hw, ch, 0);
315 /* lfo1 volume & freq */
316 EMU8000_TREMFRQ_WRITE(hw, ch, 0);
317 /* lfo2 pitch & freq */
318 EMU8000_FM2FRQ2_WRITE(hw, ch, 0);
319 /* pan & loop start */
320 temp = rec->panning[ch];
321 temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1);
322 EMU8000_PSST_WRITE(hw, ch, temp);
323 /* chorus & loop end (chorus 8bit, MSB) */
324 temp = 0; // chorus
325 temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1);
326 EMU8000_CSL_WRITE(hw, ch, temp);
327 /* Q & current address (Q 4bit value, MSB) */
328 temp = 0; // filterQ
329 temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1);
330 EMU8000_CCCA_WRITE(hw, ch, temp);
331 /* clear unknown registers */
332 EMU8000_00A0_WRITE(hw, ch, 0);
333 EMU8000_0080_WRITE(hw, ch, 0);
337 * trigger the voice
339 static void start_voice(emu8k_pcm_t *rec, int ch)
341 unsigned long flags;
342 emu8000_t *hw = rec->emu;
343 unsigned int temp, aux;
344 int pt = calc_pitch_target(rec->pitch);
346 /* cutoff and volume */
347 EMU8000_IFATN_WRITE(hw, ch, 0xff00);
348 EMU8000_VTFT_WRITE(hw, ch, 0xffff);
349 EMU8000_CVCF_WRITE(hw, ch, 0xffff);
350 /* trigger envelope */
351 EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f);
352 /* set reverb and pitch target */
353 temp = 0; // reverb
354 if (rec->panning[ch] == 0)
355 aux = 0xff;
356 else
357 aux = (-rec->panning[ch]) & 0xff;
358 temp = (temp << 8) | (pt << 16) | aux;
359 EMU8000_PTRX_WRITE(hw, ch, temp);
360 EMU8000_CPF_WRITE(hw, ch, pt << 16);
362 /* start timer */
363 spin_lock_irqsave(&rec->timer_lock, flags);
364 if (! rec->timer_running) {
365 rec->timer.expires = jiffies + 1;
366 add_timer(&rec->timer);
367 rec->timer_running = 1;
369 spin_unlock_irqrestore(&rec->timer_lock, flags);
373 * stop the voice immediately
375 static void stop_voice(emu8k_pcm_t *rec, int ch)
377 unsigned long flags;
378 emu8000_t *hw = rec->emu;
380 EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
382 /* stop timer */
383 spin_lock_irqsave(&rec->timer_lock, flags);
384 if (rec->timer_running) {
385 del_timer(&rec->timer);
386 rec->timer_running = 0;
388 spin_unlock_irqrestore(&rec->timer_lock, flags);
391 static int emu8k_pcm_trigger(snd_pcm_substream_t *subs, int cmd)
393 emu8k_pcm_t *rec = subs->runtime->private_data;
394 int ch;
396 switch (cmd) {
397 case SNDRV_PCM_TRIGGER_START:
398 for (ch = 0; ch < rec->voices; ch++)
399 start_voice(rec, ch);
400 rec->running = 1;
401 break;
402 case SNDRV_PCM_TRIGGER_STOP:
403 rec->running = 0;
404 for (ch = 0; ch < rec->voices; ch++)
405 stop_voice(rec, ch);
406 break;
407 default:
408 return -EINVAL;
410 return 0;
415 * copy / silence ops
419 * this macro should be inserted in the copy/silence loops
420 * to reduce the latency. without this, the system will hang up
421 * during the whole loop.
423 #define CHECK_SCHEDULER() \
424 do { \
425 cond_resched();\
426 if (signal_pending(current))\
427 return -EAGAIN;\
428 } while (0)
431 #ifdef USE_NONINTERLEAVE
432 /* copy one channel block */
433 static int emu8k_transfer_block(emu8000_t *emu, int offset, unsigned short *buf, int count)
435 EMU8000_SMALW_WRITE(emu, offset);
436 while (count > 0) {
437 unsigned short sval;
438 CHECK_SCHEDULER();
439 get_user(sval, buf);
440 EMU8000_SMLD_WRITE(emu, sval);
441 buf++;
442 count--;
444 return 0;
447 static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
448 int voice,
449 snd_pcm_uframes_t pos,
450 void *src,
451 snd_pcm_uframes_t count)
453 emu8k_pcm_t *rec = subs->runtime->private_data;
454 emu8000_t *emu = rec->emu;
456 snd_emu8000_write_wait(emu, 1);
457 if (voice == -1) {
458 unsigned short *buf = src;
459 int i, err;
460 count /= rec->voices;
461 for (i = 0; i < rec->voices; i++) {
462 err = emu8k_transfer_block(emu, pos + rec->loop_start[i], buf, count);
463 if (err < 0)
464 return err;
465 buf += count;
467 return 0;
468 } else {
469 return emu8k_transfer_block(emu, pos + rec->loop_start[voice], src, count);
473 /* make a channel block silence */
474 static int emu8k_silence_block(emu8000_t *emu, int offset, int count)
476 EMU8000_SMALW_WRITE(emu, offset);
477 while (count > 0) {
478 CHECK_SCHEDULER();
479 EMU8000_SMLD_WRITE(emu, 0);
480 count--;
482 return 0;
485 static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
486 int voice,
487 snd_pcm_uframes_t pos,
488 snd_pcm_uframes_t count)
490 emu8k_pcm_t *rec = subs->runtime->private_data;
491 emu8000_t *emu = rec->emu;
493 snd_emu8000_write_wait(emu, 1);
494 if (voice == -1 && rec->voices == 1)
495 voice = 0;
496 if (voice == -1) {
497 int err;
498 err = emu8k_silence_block(emu, pos + rec->loop_start[0], count / 2);
499 if (err < 0)
500 return err;
501 return emu8k_silence_block(emu, pos + rec->loop_start[1], count / 2);
502 } else {
503 return emu8k_silence_block(emu, pos + rec->loop_start[voice], count);
507 #else /* interleave */
510 * copy the interleaved data can be done easily by using
511 * DMA "left" and "right" channels on emu8k engine.
513 static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
514 int voice,
515 snd_pcm_uframes_t pos,
516 void __user *src,
517 snd_pcm_uframes_t count)
519 emu8k_pcm_t *rec = subs->runtime->private_data;
520 emu8000_t *emu = rec->emu;
521 unsigned short __user *buf = src;
523 snd_emu8000_write_wait(emu, 1);
524 EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]);
525 if (rec->voices > 1)
526 EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]);
528 while (count-- > 0) {
529 unsigned short sval;
530 CHECK_SCHEDULER();
531 get_user(sval, buf);
532 EMU8000_SMLD_WRITE(emu, sval);
533 buf++;
534 if (rec->voices > 1) {
535 CHECK_SCHEDULER();
536 get_user(sval, buf);
537 EMU8000_SMRD_WRITE(emu, sval);
538 buf++;
541 return 0;
544 static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
545 int voice,
546 snd_pcm_uframes_t pos,
547 snd_pcm_uframes_t count)
549 emu8k_pcm_t *rec = subs->runtime->private_data;
550 emu8000_t *emu = rec->emu;
552 snd_emu8000_write_wait(emu, 1);
553 EMU8000_SMALW_WRITE(emu, rec->loop_start[0] + pos);
554 if (rec->voices > 1)
555 EMU8000_SMARW_WRITE(emu, rec->loop_start[1] + pos);
556 while (count-- > 0) {
557 CHECK_SCHEDULER();
558 EMU8000_SMLD_WRITE(emu, 0);
559 if (rec->voices > 1) {
560 CHECK_SCHEDULER();
561 EMU8000_SMRD_WRITE(emu, 0);
564 return 0;
566 #endif
570 * allocate a memory block
572 static int emu8k_pcm_hw_params(snd_pcm_substream_t *subs,
573 snd_pcm_hw_params_t *hw_params)
575 emu8k_pcm_t *rec = subs->runtime->private_data;
577 if (rec->block) {
578 /* reallocation - release the old block */
579 snd_util_mem_free(rec->emu->memhdr, rec->block);
580 rec->block = NULL;
583 rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4;
584 rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes);
585 if (! rec->block)
586 return -ENOMEM;
587 rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */
588 /* at least dma_bytes must be set for non-interleaved mode */
589 subs->dma_buffer.bytes = params_buffer_bytes(hw_params);
591 return 0;
595 * free the memory block
597 static int emu8k_pcm_hw_free(snd_pcm_substream_t *subs)
599 emu8k_pcm_t *rec = subs->runtime->private_data;
601 if (rec->block) {
602 int ch;
603 for (ch = 0; ch < rec->voices; ch++)
604 stop_voice(rec, ch); // to be sure
605 if (rec->dram_opened)
606 emu8k_close_dram(rec->emu);
607 snd_util_mem_free(rec->emu->memhdr, rec->block);
608 rec->block = NULL;
610 return 0;
615 static int emu8k_pcm_prepare(snd_pcm_substream_t *subs)
617 emu8k_pcm_t *rec = subs->runtime->private_data;
619 rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate);
620 rec->last_ptr = 0;
621 rec->period_pos = 0;
623 rec->buf_size = subs->runtime->buffer_size;
624 rec->period_size = subs->runtime->period_size;
625 rec->voices = subs->runtime->channels;
626 rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE;
627 if (rec->voices > 1)
628 rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE;
629 if (rec->voices > 1) {
630 rec->panning[0] = 0xff;
631 rec->panning[1] = 0x00;
632 } else
633 rec->panning[0] = 0x80;
635 if (! rec->dram_opened) {
636 int err, i, ch;
638 snd_emux_terminate_all(rec->emu->emu);
639 if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0)
640 return err;
641 rec->dram_opened = 1;
643 /* clear loop blanks */
644 snd_emu8000_write_wait(rec->emu, 0);
645 EMU8000_SMALW_WRITE(rec->emu, rec->offset);
646 for (i = 0; i < LOOP_BLANK_SIZE; i++)
647 EMU8000_SMLD_WRITE(rec->emu, 0);
648 for (ch = 0; ch < rec->voices; ch++) {
649 EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size);
650 for (i = 0; i < LOOP_BLANK_SIZE; i++)
651 EMU8000_SMLD_WRITE(rec->emu, 0);
655 setup_voice(rec, 0);
656 if (rec->voices > 1)
657 setup_voice(rec, 1);
658 return 0;
661 static snd_pcm_uframes_t emu8k_pcm_pointer(snd_pcm_substream_t *subs)
663 emu8k_pcm_t *rec = subs->runtime->private_data;
664 if (rec->running)
665 return emu8k_get_curpos(rec, 0);
666 return 0;
670 static snd_pcm_ops_t emu8k_pcm_ops = {
671 .open = emu8k_pcm_open,
672 .close = emu8k_pcm_close,
673 .ioctl = snd_pcm_lib_ioctl,
674 .hw_params = emu8k_pcm_hw_params,
675 .hw_free = emu8k_pcm_hw_free,
676 .prepare = emu8k_pcm_prepare,
677 .trigger = emu8k_pcm_trigger,
678 .pointer = emu8k_pcm_pointer,
679 .copy = emu8k_pcm_copy,
680 .silence = emu8k_pcm_silence,
684 static void snd_emu8000_pcm_free(snd_pcm_t *pcm)
686 emu8000_t *emu = pcm->private_data;
687 emu->pcm = NULL;
690 int snd_emu8000_pcm_new(snd_card_t *card, emu8000_t *emu, int index)
692 snd_pcm_t *pcm;
693 int err;
695 if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0)
696 return err;
697 pcm->private_data = emu;
698 pcm->private_free = snd_emu8000_pcm_free;
699 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops);
700 emu->pcm = pcm;
702 snd_device_register(card, pcm);
704 return 0;