dummy.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 645 行 · 第 1/2 页
C
645 行
/* * Dummy soundcard * Copyright (c) by Jaroslav Kysela <perex@suse.cz> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#include <sound/driver.h>#include <linux/init.h>#include <linux/jiffies.h>#include <linux/slab.h>#include <linux/time.h>#include <linux/wait.h>#include <linux/moduleparam.h>#include <sound/core.h>#include <sound/control.h>#include <sound/pcm.h>#include <sound/rawmidi.h>#include <sound/initval.h>MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");MODULE_LICENSE("GPL");MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");#define MAX_PCM_DEVICES 4#define MAX_PCM_SUBSTREAMS 16#define MAX_MIDI_DEVICES 2#if 0 /* emu10k1 emulation */#define MAX_BUFFER_SIZE (128 * 1024)static int emu10k1_playback_constraints(snd_pcm_runtime_t *runtime){ int err; if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) return err; if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) return err; return 0;}#define add_playback_constraints emu10k1_playback_constraints#endif#if 0 /* RME9652 emulation */#define MAX_BUFFER_SIZE (26 * 64 * 1024)#define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE#define USE_CHANNELS_MIN 26#define USE_CHANNELS_MAX 26#define USE_PERIODS_MIN 2#define USE_PERIODS_MAX 2#endif#if 0 /* ICE1712 emulation */#define MAX_BUFFER_SIZE (256 * 1024)#define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE#define USE_CHANNELS_MIN 10#define USE_CHANNELS_MAX 10#define USE_PERIODS_MIN 1#define USE_PERIODS_MAX 1024#endif#if 0 /* UDA1341 emulation */#define MAX_BUFFER_SIZE (16380)#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE#define USE_CHANNELS_MIN 2#define USE_CHANNELS_MAX 2#define USE_PERIODS_MIN 2#define USE_PERIODS_MAX 255#endif#if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE#define USE_CHANNELS_MIN 2#define USE_CHANNELS_MAX 2#define USE_RATE SNDRV_PCM_RATE_48000#define USE_RATE_MIN 48000#define USE_RATE_MAX 48000#endif/* defaults */#ifndef MAX_BUFFER_SIZE#define MAX_BUFFER_SIZE (64*1024)#endif#ifndef USE_FORMATS#define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)#endif#ifndef USE_RATE#define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000#define USE_RATE_MIN 5500#define USE_RATE_MAX 48000#endif#ifndef USE_CHANNELS_MIN#define USE_CHANNELS_MIN 1#endif#ifndef USE_CHANNELS_MAX#define USE_CHANNELS_MAX 2#endif#ifndef USE_PERIODS_MIN#define USE_PERIODS_MIN 1#endif#ifndef USE_PERIODS_MAX#define USE_PERIODS_MAX 1024#endif#ifndef add_playback_constraints#define add_playback_constraints(x) 0#endif#ifndef add_capture_constraints#define add_capture_constraints(x) 0#endifstatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};//static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};static int boot_devs;module_param_array(index, int, boot_devs, 0444);MODULE_PARM_DESC(index, "Index value for dummy soundcard.");module_param_array(id, charp, boot_devs, 0444);MODULE_PARM_DESC(id, "ID string for dummy soundcard.");module_param_array(enable, bool, boot_devs, 0444);MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");module_param_array(pcm_devs, int, boot_devs, 0444);MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");module_param_array(pcm_substreams, int, boot_devs, 0444);MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");//module_param_array(midi_devs, int, boot_devs, 0444);//MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");#define MIXER_ADDR_MASTER 0#define MIXER_ADDR_LINE 1#define MIXER_ADDR_MIC 2#define MIXER_ADDR_SYNTH 3#define MIXER_ADDR_CD 4#define MIXER_ADDR_LAST 4typedef struct snd_card_dummy { snd_card_t *card; spinlock_t mixer_lock; int mixer_volume[MIXER_ADDR_LAST+1][2]; int capture_source[MIXER_ADDR_LAST+1][2];} snd_card_dummy_t;typedef struct snd_card_dummy_pcm { snd_card_dummy_t *dummy; spinlock_t lock; struct timer_list timer; unsigned int pcm_size; unsigned int pcm_count; unsigned int pcm_bps; /* bytes per second */ unsigned int pcm_jiffie; /* bytes per one jiffie */ unsigned int pcm_irq_pos; /* IRQ position */ unsigned int pcm_buf_pos; /* position in buffer */ snd_pcm_substream_t *substream;} snd_card_dummy_pcm_t;static snd_card_t *snd_dummy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;static void snd_card_dummy_pcm_timer_start(snd_pcm_substream_t * substream){ snd_pcm_runtime_t *runtime = substream->runtime; snd_card_dummy_pcm_t *dpcm = runtime->private_data; dpcm->timer.expires = 1 + jiffies; add_timer(&dpcm->timer);}static void snd_card_dummy_pcm_timer_stop(snd_pcm_substream_t * substream){ snd_pcm_runtime_t *runtime = substream->runtime; snd_card_dummy_pcm_t *dpcm = runtime->private_data; del_timer(&dpcm->timer);}static int snd_card_dummy_playback_trigger(snd_pcm_substream_t * substream, int cmd){ if (cmd == SNDRV_PCM_TRIGGER_START) { snd_card_dummy_pcm_timer_start(substream); } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { snd_card_dummy_pcm_timer_stop(substream); } else { return -EINVAL; } return 0;}static int snd_card_dummy_capture_trigger(snd_pcm_substream_t * substream, int cmd){ if (cmd == SNDRV_PCM_TRIGGER_START) { snd_card_dummy_pcm_timer_start(substream); } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { snd_card_dummy_pcm_timer_stop(substream); } else { return -EINVAL; } return 0;}static int snd_card_dummy_pcm_prepare(snd_pcm_substream_t * substream){ snd_pcm_runtime_t *runtime = substream->runtime; snd_card_dummy_pcm_t *dpcm = runtime->private_data; unsigned int bps; bps = runtime->rate * runtime->channels; bps *= snd_pcm_format_width(runtime->format); bps /= 8; if (bps <= 0) return -EINVAL; dpcm->pcm_bps = bps; dpcm->pcm_jiffie = bps / HZ; dpcm->pcm_size = snd_pcm_lib_buffer_bytes(substream); dpcm->pcm_count = snd_pcm_lib_period_bytes(substream); dpcm->pcm_irq_pos = 0; dpcm->pcm_buf_pos = 0; return 0;}static int snd_card_dummy_playback_prepare(snd_pcm_substream_t * substream){ return snd_card_dummy_pcm_prepare(substream);}static int snd_card_dummy_capture_prepare(snd_pcm_substream_t * substream){ return snd_card_dummy_pcm_prepare(substream);}static void snd_card_dummy_pcm_timer_function(unsigned long data){ snd_card_dummy_pcm_t *dpcm = (snd_card_dummy_pcm_t *)data; dpcm->timer.expires = 1 + jiffies; add_timer(&dpcm->timer); spin_lock_irq(&dpcm->lock); dpcm->pcm_irq_pos += dpcm->pcm_jiffie; dpcm->pcm_buf_pos += dpcm->pcm_jiffie; dpcm->pcm_buf_pos %= dpcm->pcm_size; if (dpcm->pcm_irq_pos >= dpcm->pcm_count) { dpcm->pcm_irq_pos %= dpcm->pcm_count; snd_pcm_period_elapsed(dpcm->substream); } spin_unlock_irq(&dpcm->lock); }static snd_pcm_uframes_t snd_card_dummy_playback_pointer(snd_pcm_substream_t * substream){ snd_pcm_runtime_t *runtime = substream->runtime; snd_card_dummy_pcm_t *dpcm = runtime->private_data; return bytes_to_frames(runtime, dpcm->pcm_buf_pos);}static snd_pcm_uframes_t snd_card_dummy_capture_pointer(snd_pcm_substream_t * substream){ snd_pcm_runtime_t *runtime = substream->runtime; snd_card_dummy_pcm_t *dpcm = runtime->private_data; return bytes_to_frames(runtime, dpcm->pcm_buf_pos);}static snd_pcm_hardware_t snd_card_dummy_playback ={ .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = USE_FORMATS, .rates = USE_RATE, .rate_min = USE_RATE_MIN, .rate_max = USE_RATE_MAX, .channels_min = USE_CHANNELS_MIN, .channels_max = USE_CHANNELS_MAX, .buffer_bytes_max = MAX_BUFFER_SIZE, .period_bytes_min = 64, .period_bytes_max = MAX_BUFFER_SIZE, .periods_min = USE_PERIODS_MIN, .periods_max = USE_PERIODS_MAX, .fifo_size = 0,};static snd_pcm_hardware_t snd_card_dummy_capture ={ .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = USE_FORMATS, .rates = USE_RATE, .rate_min = USE_RATE_MIN, .rate_max = USE_RATE_MAX, .channels_min = USE_CHANNELS_MIN, .channels_max = USE_CHANNELS_MAX, .buffer_bytes_max = MAX_BUFFER_SIZE, .period_bytes_min = 64, .period_bytes_max = MAX_BUFFER_SIZE, .periods_min = USE_PERIODS_MIN, .periods_max = USE_PERIODS_MAX, .fifo_size = 0,};static void snd_card_dummy_runtime_free(snd_pcm_runtime_t *runtime){ snd_card_dummy_pcm_t *dpcm = runtime->private_data; kfree(dpcm);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?