blob: 4f900d8b92ce9a783f481309f6300807f766a1fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dummy soundcard
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
Takashi Iwai6e65c1c2005-11-17 16:01:56 +010022#include <linux/err.h>
23#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/jiffies.h>
25#include <linux/slab.h>
26#include <linux/time.h>
27#include <linux/wait.h>
28#include <linux/moduleparam.h>
29#include <sound/core.h>
30#include <sound/control.h>
Takashi Iwaifb567a82006-08-23 13:07:19 +020031#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/pcm.h>
33#include <sound/rawmidi.h>
34#include <sound/initval.h>
35
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020036MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
38MODULE_LICENSE("GPL");
39MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
40
41#define MAX_PCM_DEVICES 4
42#define MAX_PCM_SUBSTREAMS 16
43#define MAX_MIDI_DEVICES 2
44
45#if 0 /* emu10k1 emulation */
46#define MAX_BUFFER_SIZE (128 * 1024)
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010047static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 int err;
50 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
51 return err;
52 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0)
53 return err;
54 return 0;
55}
56#define add_playback_constraints emu10k1_playback_constraints
57#endif
58
59#if 0 /* RME9652 emulation */
60#define MAX_BUFFER_SIZE (26 * 64 * 1024)
61#define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
62#define USE_CHANNELS_MIN 26
63#define USE_CHANNELS_MAX 26
64#define USE_PERIODS_MIN 2
65#define USE_PERIODS_MAX 2
66#endif
67
68#if 0 /* ICE1712 emulation */
69#define MAX_BUFFER_SIZE (256 * 1024)
70#define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
71#define USE_CHANNELS_MIN 10
72#define USE_CHANNELS_MAX 10
73#define USE_PERIODS_MIN 1
74#define USE_PERIODS_MAX 1024
75#endif
76
77#if 0 /* UDA1341 emulation */
78#define MAX_BUFFER_SIZE (16380)
79#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
80#define USE_CHANNELS_MIN 2
81#define USE_CHANNELS_MAX 2
82#define USE_PERIODS_MIN 2
83#define USE_PERIODS_MAX 255
84#endif
85
86#if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */
87#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
88#define USE_CHANNELS_MIN 2
89#define USE_CHANNELS_MAX 2
90#define USE_RATE SNDRV_PCM_RATE_48000
91#define USE_RATE_MIN 48000
92#define USE_RATE_MAX 48000
93#endif
94
Jaroslav Kysela2ad5dd82006-01-03 14:27:21 +010095#if 0 /* CA0106 */
96#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
97#define USE_CHANNELS_MIN 2
98#define USE_CHANNELS_MAX 2
99#define USE_RATE (SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000)
100#define USE_RATE_MIN 48000
101#define USE_RATE_MAX 192000
102#define MAX_BUFFER_SIZE ((65536-64)*8)
103#define MAX_PERIOD_SIZE (65536-64)
104#define USE_PERIODS_MIN 2
105#define USE_PERIODS_MAX 8
106#endif
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* defaults */
110#ifndef MAX_BUFFER_SIZE
111#define MAX_BUFFER_SIZE (64*1024)
112#endif
Jaroslav Kysela2ad5dd82006-01-03 14:27:21 +0100113#ifndef MAX_PERIOD_SIZE
114#define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#ifndef USE_FORMATS
117#define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
118#endif
119#ifndef USE_RATE
120#define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
121#define USE_RATE_MIN 5500
122#define USE_RATE_MAX 48000
123#endif
124#ifndef USE_CHANNELS_MIN
125#define USE_CHANNELS_MIN 1
126#endif
127#ifndef USE_CHANNELS_MAX
128#define USE_CHANNELS_MAX 2
129#endif
130#ifndef USE_PERIODS_MIN
131#define USE_PERIODS_MIN 1
132#endif
133#ifndef USE_PERIODS_MAX
134#define USE_PERIODS_MAX 1024
135#endif
136#ifndef add_playback_constraints
137#define add_playback_constraints(x) 0
138#endif
139#ifndef add_capture_constraints
140#define add_capture_constraints(x) 0
141#endif
142
143static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
144static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
145static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
146static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
147static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
148//static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
149
150module_param_array(index, int, NULL, 0444);
151MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
152module_param_array(id, charp, NULL, 0444);
153MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
154module_param_array(enable, bool, NULL, 0444);
155MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
156module_param_array(pcm_devs, int, NULL, 0444);
157MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
158module_param_array(pcm_substreams, int, NULL, 0444);
159MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
160//module_param_array(midi_devs, int, NULL, 0444);
161//MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
162
Clemens Ladischf7a92752005-12-07 09:13:42 +0100163static struct platform_device *devices[SNDRV_CARDS];
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define MIXER_ADDR_MASTER 0
166#define MIXER_ADDR_LINE 1
167#define MIXER_ADDR_MIC 2
168#define MIXER_ADDR_SYNTH 3
169#define MIXER_ADDR_CD 4
170#define MIXER_ADDR_LAST 4
171
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100172struct snd_dummy {
173 struct snd_card *card;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100174 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 spinlock_t mixer_lock;
176 int mixer_volume[MIXER_ADDR_LAST+1][2];
177 int capture_source[MIXER_ADDR_LAST+1][2];
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100178};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100180struct snd_dummy_pcm {
181 struct snd_dummy *dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 spinlock_t lock;
183 struct timer_list timer;
Ahmet Ä°nan53463a82008-02-21 07:55:30 +0100184 unsigned int pcm_buffer_size;
185 unsigned int pcm_period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned int pcm_bps; /* bytes per second */
Ahmet Ä°nan53463a82008-02-21 07:55:30 +0100187 unsigned int pcm_hz; /* HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned int pcm_irq_pos; /* IRQ position */
189 unsigned int pcm_buf_pos; /* position in buffer */
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100190 struct snd_pcm_substream *substream;
191};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100194static inline void snd_card_dummy_pcm_timer_start(struct snd_dummy_pcm *dpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 dpcm->timer.expires = 1 + jiffies;
197 add_timer(&dpcm->timer);
198}
199
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100200static inline void snd_card_dummy_pcm_timer_stop(struct snd_dummy_pcm *dpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 del_timer(&dpcm->timer);
203}
204
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100205static int snd_card_dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100207 struct snd_pcm_runtime *runtime = substream->runtime;
208 struct snd_dummy_pcm *dpcm = runtime->private_data;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100209 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100211 spin_lock(&dpcm->lock);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100212 switch (cmd) {
213 case SNDRV_PCM_TRIGGER_START:
214 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100215 snd_card_dummy_pcm_timer_start(dpcm);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100216 break;
217 case SNDRV_PCM_TRIGGER_STOP:
218 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100219 snd_card_dummy_pcm_timer_stop(dpcm);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100220 break;
221 default:
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100222 err = -EINVAL;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100225 spin_unlock(&dpcm->lock);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100229static int snd_card_dummy_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100231 struct snd_pcm_runtime *runtime = substream->runtime;
232 struct snd_dummy_pcm *dpcm = runtime->private_data;
Roel Kluin369b2402008-04-16 19:30:30 +0200233 int bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Roel Kluin369b2402008-04-16 19:30:30 +0200235 bps = snd_pcm_format_width(runtime->format) * runtime->rate *
236 runtime->channels / 8;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (bps <= 0)
239 return -EINVAL;
Roel Kluin369b2402008-04-16 19:30:30 +0200240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 dpcm->pcm_bps = bps;
Ahmet Ä°nan53463a82008-02-21 07:55:30 +0100242 dpcm->pcm_hz = HZ;
243 dpcm->pcm_buffer_size = snd_pcm_lib_buffer_bytes(substream);
244 dpcm->pcm_period_size = snd_pcm_lib_period_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dpcm->pcm_irq_pos = 0;
246 dpcm->pcm_buf_pos = 0;
Ahmet Ä°nan53463a82008-02-21 07:55:30 +0100247
248 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
249 bytes_to_samples(runtime, runtime->dma_bytes));
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static void snd_card_dummy_pcm_timer_function(unsigned long data)
255{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100256 struct snd_dummy_pcm *dpcm = (struct snd_dummy_pcm *)data;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100257 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Takashi Iwaib32425a2005-11-18 18:52:14 +0100259 spin_lock_irqsave(&dpcm->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dpcm->timer.expires = 1 + jiffies;
261 add_timer(&dpcm->timer);
Ahmet Ä°nan53463a82008-02-21 07:55:30 +0100262 dpcm->pcm_irq_pos += dpcm->pcm_bps;
Ahmet Ä°nan470f23b2008-02-28 12:46:32 +0100263 dpcm->pcm_buf_pos += dpcm->pcm_bps;
264 dpcm->pcm_buf_pos %= dpcm->pcm_buffer_size * dpcm->pcm_hz;
Ahmet Ä°nan53463a82008-02-21 07:55:30 +0100265 if (dpcm->pcm_irq_pos >= dpcm->pcm_period_size * dpcm->pcm_hz) {
266 dpcm->pcm_irq_pos %= dpcm->pcm_period_size * dpcm->pcm_hz;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100267 spin_unlock_irqrestore(&dpcm->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100269 } else
270 spin_unlock_irqrestore(&dpcm->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100273static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100275 struct snd_pcm_runtime *runtime = substream->runtime;
276 struct snd_dummy_pcm *dpcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Ahmet Ä°nan470f23b2008-02-28 12:46:32 +0100278 return bytes_to_frames(runtime, dpcm->pcm_buf_pos / dpcm->pcm_hz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100281static struct snd_pcm_hardware snd_card_dummy_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100284 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 .formats = USE_FORMATS,
286 .rates = USE_RATE,
287 .rate_min = USE_RATE_MIN,
288 .rate_max = USE_RATE_MAX,
289 .channels_min = USE_CHANNELS_MIN,
290 .channels_max = USE_CHANNELS_MAX,
291 .buffer_bytes_max = MAX_BUFFER_SIZE,
292 .period_bytes_min = 64,
Takashi Iwaie05d6962006-08-17 17:12:19 +0200293 .period_bytes_max = MAX_PERIOD_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .periods_min = USE_PERIODS_MIN,
295 .periods_max = USE_PERIODS_MAX,
296 .fifo_size = 0,
297};
298
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100299static struct snd_pcm_hardware snd_card_dummy_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100302 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 .formats = USE_FORMATS,
304 .rates = USE_RATE,
305 .rate_min = USE_RATE_MIN,
306 .rate_max = USE_RATE_MAX,
307 .channels_min = USE_CHANNELS_MIN,
308 .channels_max = USE_CHANNELS_MAX,
309 .buffer_bytes_max = MAX_BUFFER_SIZE,
310 .period_bytes_min = 64,
Jaroslav Kysela2ad5dd82006-01-03 14:27:21 +0100311 .period_bytes_max = MAX_PERIOD_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .periods_min = USE_PERIODS_MIN,
313 .periods_max = USE_PERIODS_MAX,
314 .fifo_size = 0,
315};
316
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100317static void snd_card_dummy_runtime_free(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100319 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100322static int snd_card_dummy_hw_params(struct snd_pcm_substream *substream,
323 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
326}
327
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100328static int snd_card_dummy_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 return snd_pcm_lib_free_pages(substream);
331}
332
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100333static struct snd_dummy_pcm *new_pcm_stream(struct snd_pcm_substream *substream)
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100334{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100335 struct snd_dummy_pcm *dpcm;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100336
337 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
338 if (! dpcm)
339 return dpcm;
340 init_timer(&dpcm->timer);
341 dpcm->timer.data = (unsigned long) dpcm;
342 dpcm->timer.function = snd_card_dummy_pcm_timer_function;
343 spin_lock_init(&dpcm->lock);
344 dpcm->substream = substream;
345 return dpcm;
346}
347
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100348static int snd_card_dummy_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100350 struct snd_pcm_runtime *runtime = substream->runtime;
351 struct snd_dummy_pcm *dpcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int err;
353
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100354 if ((dpcm = new_pcm_stream(substream)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 runtime->private_data = dpcm;
Daniel THOMPSON54e8e212008-08-15 10:53:38 +0100357 /* makes the infrastructure responsible for freeing dpcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 runtime->private_free = snd_card_dummy_runtime_free;
359 runtime->hw = snd_card_dummy_playback;
360 if (substream->pcm->device & 1) {
361 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
362 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
363 }
364 if (substream->pcm->device & 2)
365 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
Daniel THOMPSON54e8e212008-08-15 10:53:38 +0100366 if ((err = add_playback_constraints(runtime)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 return 0;
370}
371
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100372static int snd_card_dummy_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100374 struct snd_pcm_runtime *runtime = substream->runtime;
375 struct snd_dummy_pcm *dpcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int err;
377
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100378 if ((dpcm = new_pcm_stream(substream)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 runtime->private_data = dpcm;
Daniel THOMPSON54e8e212008-08-15 10:53:38 +0100381 /* makes the infrastructure responsible for freeing dpcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 runtime->private_free = snd_card_dummy_runtime_free;
383 runtime->hw = snd_card_dummy_capture;
384 if (substream->pcm->device == 1) {
385 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
386 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
387 }
388 if (substream->pcm->device & 2)
389 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
Daniel THOMPSON54e8e212008-08-15 10:53:38 +0100390 if ((err = add_capture_constraints(runtime)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return 0;
394}
395
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100396static int snd_card_dummy_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 return 0;
399}
400
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100401static int snd_card_dummy_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 return 0;
404}
405
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100406static struct snd_pcm_ops snd_card_dummy_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 .open = snd_card_dummy_playback_open,
408 .close = snd_card_dummy_playback_close,
409 .ioctl = snd_pcm_lib_ioctl,
410 .hw_params = snd_card_dummy_hw_params,
411 .hw_free = snd_card_dummy_hw_free,
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100412 .prepare = snd_card_dummy_pcm_prepare,
413 .trigger = snd_card_dummy_pcm_trigger,
414 .pointer = snd_card_dummy_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415};
416
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100417static struct snd_pcm_ops snd_card_dummy_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 .open = snd_card_dummy_capture_open,
419 .close = snd_card_dummy_capture_close,
420 .ioctl = snd_pcm_lib_ioctl,
421 .hw_params = snd_card_dummy_hw_params,
422 .hw_free = snd_card_dummy_hw_free,
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100423 .prepare = snd_card_dummy_pcm_prepare,
424 .trigger = snd_card_dummy_pcm_trigger,
425 .pointer = snd_card_dummy_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
427
Prarit Bhargava788c6042007-02-13 13:11:11 +0100428static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
429 int substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100431 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int err;
433
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100434 if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device,
435 substreams, substreams, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return err;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100437 dummy->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
439 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
440 pcm->private_data = dummy;
441 pcm->info_flags = 0;
442 strcpy(pcm->name, "Dummy PCM");
443 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
444 snd_dma_continuous_data(GFP_KERNEL),
445 0, 64*1024);
446 return 0;
447}
448
449#define DUMMY_VOLUME(xname, xindex, addr) \
Takashi Iwaifb567a82006-08-23 13:07:19 +0200450{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
451 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
452 .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 .info = snd_dummy_volume_info, \
454 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
Takashi Iwaifb567a82006-08-23 13:07:19 +0200455 .private_value = addr, \
456 .tlv = { .p = db_scale_dummy } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100458static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
459 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
462 uinfo->count = 2;
463 uinfo->value.integer.min = -50;
464 uinfo->value.integer.max = 100;
465 return 0;
466}
467
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100468static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
469 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100471 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int addr = kcontrol->private_value;
473
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100474 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
476 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100477 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479}
480
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100481static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
482 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100484 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int change, addr = kcontrol->private_value;
486 int left, right;
487
488 left = ucontrol->value.integer.value[0];
489 if (left < -50)
490 left = -50;
491 if (left > 100)
492 left = 100;
493 right = ucontrol->value.integer.value[1];
494 if (right < -50)
495 right = -50;
496 if (right > 100)
497 right = 100;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100498 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 change = dummy->mixer_volume[addr][0] != left ||
500 dummy->mixer_volume[addr][1] != right;
501 dummy->mixer_volume[addr][0] = left;
502 dummy->mixer_volume[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100503 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return change;
505}
506
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100507static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
Takashi Iwaifb567a82006-08-23 13:07:19 +0200508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509#define DUMMY_CAPSRC(xname, xindex, addr) \
510{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
511 .info = snd_dummy_capsrc_info, \
512 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
513 .private_value = addr }
514
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200515#define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100517static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
518 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100520 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int addr = kcontrol->private_value;
522
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100523 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
525 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100526 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 0;
528}
529
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100530static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100532 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int change, addr = kcontrol->private_value;
534 int left, right;
535
536 left = ucontrol->value.integer.value[0] & 1;
537 right = ucontrol->value.integer.value[1] & 1;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100538 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 change = dummy->capture_source[addr][0] != left &&
540 dummy->capture_source[addr][1] != right;
541 dummy->capture_source[addr][0] = left;
542 dummy->capture_source[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100543 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return change;
545}
546
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100547static struct snd_kcontrol_new snd_dummy_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
549DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
550DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200551DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200553DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200555DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200557DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558};
559
Prarit Bhargava788c6042007-02-13 13:11:11 +0100560static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100562 struct snd_card *card = dummy->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 unsigned int idx;
564 int err;
565
Takashi Iwai5e246b82008-08-08 17:12:47 +0200566 if (snd_BUG_ON(!dummy))
567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 spin_lock_init(&dummy->mixer_lock);
569 strcpy(card->mixername, "Dummy Mixer");
570
571 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
572 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0)
573 return err;
574 }
575 return 0;
576}
577
Prarit Bhargava788c6042007-02-13 13:11:11 +0100578static int __devinit snd_dummy_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100580 struct snd_card *card;
581 struct snd_dummy *dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int idx, err;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100583 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100586 sizeof(struct snd_dummy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (card == NULL)
588 return -ENOMEM;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100589 dummy = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 dummy->card = card;
591 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
592 if (pcm_substreams[dev] < 1)
593 pcm_substreams[dev] = 1;
594 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
595 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
596 if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0)
597 goto __nodev;
598 }
599 if ((err = snd_card_dummy_new_mixer(dummy)) < 0)
600 goto __nodev;
601 strcpy(card->driver, "Dummy");
602 strcpy(card->shortname, "Dummy");
603 sprintf(card->longname, "Dummy %i", dev + 1);
Takashi Iwai16dab542005-09-05 17:17:58 +0200604
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100605 snd_card_set_dev(card, &devptr->dev);
Takashi Iwai16dab542005-09-05 17:17:58 +0200606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if ((err = snd_card_register(card)) == 0) {
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100608 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610 }
611 __nodev:
612 snd_card_free(card);
613 return err;
614}
615
Prarit Bhargava788c6042007-02-13 13:11:11 +0100616static int __devexit snd_dummy_remove(struct platform_device *devptr)
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100617{
618 snd_card_free(platform_get_drvdata(devptr));
619 platform_set_drvdata(devptr, NULL);
620 return 0;
621}
622
623#ifdef CONFIG_PM
624static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
625{
626 struct snd_card *card = platform_get_drvdata(pdev);
627 struct snd_dummy *dummy = card->private_data;
628
629 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
630 snd_pcm_suspend_all(dummy->pcm);
631 return 0;
632}
633
634static int snd_dummy_resume(struct platform_device *pdev)
635{
636 struct snd_card *card = platform_get_drvdata(pdev);
637
638 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
639 return 0;
640}
641#endif
642
643#define SND_DUMMY_DRIVER "snd_dummy"
644
645static struct platform_driver snd_dummy_driver = {
646 .probe = snd_dummy_probe,
Prarit Bhargava788c6042007-02-13 13:11:11 +0100647 .remove = __devexit_p(snd_dummy_remove),
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100648#ifdef CONFIG_PM
649 .suspend = snd_dummy_suspend,
650 .resume = snd_dummy_resume,
651#endif
652 .driver = {
653 .name = SND_DUMMY_DRIVER
654 },
655};
656
Randy Dunlapc12aad62007-06-25 12:08:01 +0200657static void snd_dummy_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100658{
659 int i;
660
661 for (i = 0; i < ARRAY_SIZE(devices); ++i)
662 platform_device_unregister(devices[i]);
663 platform_driver_unregister(&snd_dummy_driver);
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static int __init alsa_card_dummy_init(void)
667{
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100668 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100670 if ((err = platform_driver_register(&snd_dummy_driver)) < 0)
671 return err;
672
673 cards = 0;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100674 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100675 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100676 if (! enable[i])
677 continue;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100678 device = platform_device_register_simple(SND_DUMMY_DRIVER,
679 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +0200680 if (IS_ERR(device))
681 continue;
Rene Herman71524472006-04-13 12:58:06 +0200682 if (!platform_get_drvdata(device)) {
683 platform_device_unregister(device);
684 continue;
685 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100686 devices[i] = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 cards++;
688 }
689 if (!cards) {
690#ifdef MODULE
691 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
692#endif
Rene Hermana182ee92006-04-13 12:57:11 +0200693 snd_dummy_unregister_all();
694 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 return 0;
697}
698
699static void __exit alsa_card_dummy_exit(void)
700{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100701 snd_dummy_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704module_init(alsa_card_dummy_init)
705module_exit(alsa_card_dummy_exit)