blob: 186117571745ead8a87a5b4270c2bd81d8e2dd9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dummy soundcard
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
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
21#include <sound/driver.h>
22#include <linux/init.h>
Takashi Iwai6e65c1c2005-11-17 16:01:56 +010023#include <linux/err.h>
24#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/jiffies.h>
26#include <linux/slab.h>
27#include <linux/time.h>
28#include <linux/wait.h>
29#include <linux/moduleparam.h>
30#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/pcm.h>
33#include <sound/rawmidi.h>
34#include <sound/initval.h>
35
36MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
37MODULE_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
95
96/* defaults */
97#ifndef MAX_BUFFER_SIZE
98#define MAX_BUFFER_SIZE (64*1024)
99#endif
100#ifndef USE_FORMATS
101#define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
102#endif
103#ifndef USE_RATE
104#define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
105#define USE_RATE_MIN 5500
106#define USE_RATE_MAX 48000
107#endif
108#ifndef USE_CHANNELS_MIN
109#define USE_CHANNELS_MIN 1
110#endif
111#ifndef USE_CHANNELS_MAX
112#define USE_CHANNELS_MAX 2
113#endif
114#ifndef USE_PERIODS_MIN
115#define USE_PERIODS_MIN 1
116#endif
117#ifndef USE_PERIODS_MAX
118#define USE_PERIODS_MAX 1024
119#endif
120#ifndef add_playback_constraints
121#define add_playback_constraints(x) 0
122#endif
123#ifndef add_capture_constraints
124#define add_capture_constraints(x) 0
125#endif
126
127static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
128static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
129static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
130static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
131static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
132//static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
133
134module_param_array(index, int, NULL, 0444);
135MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
136module_param_array(id, charp, NULL, 0444);
137MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
138module_param_array(enable, bool, NULL, 0444);
139MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
140module_param_array(pcm_devs, int, NULL, 0444);
141MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
142module_param_array(pcm_substreams, int, NULL, 0444);
143MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
144//module_param_array(midi_devs, int, NULL, 0444);
145//MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
146
147#define MIXER_ADDR_MASTER 0
148#define MIXER_ADDR_LINE 1
149#define MIXER_ADDR_MIC 2
150#define MIXER_ADDR_SYNTH 3
151#define MIXER_ADDR_CD 4
152#define MIXER_ADDR_LAST 4
153
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100154struct snd_dummy {
155 struct snd_card *card;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100156 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 spinlock_t mixer_lock;
158 int mixer_volume[MIXER_ADDR_LAST+1][2];
159 int capture_source[MIXER_ADDR_LAST+1][2];
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100160};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100162struct snd_dummy_pcm {
163 struct snd_dummy *dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 spinlock_t lock;
165 struct timer_list timer;
166 unsigned int pcm_size;
167 unsigned int pcm_count;
168 unsigned int pcm_bps; /* bytes per second */
169 unsigned int pcm_jiffie; /* bytes per one jiffie */
170 unsigned int pcm_irq_pos; /* IRQ position */
171 unsigned int pcm_buf_pos; /* position in buffer */
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100172 struct snd_pcm_substream *substream;
173};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100176static inline void snd_card_dummy_pcm_timer_start(struct snd_dummy_pcm *dpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 dpcm->timer.expires = 1 + jiffies;
179 add_timer(&dpcm->timer);
180}
181
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100182static inline void snd_card_dummy_pcm_timer_stop(struct snd_dummy_pcm *dpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 del_timer(&dpcm->timer);
185}
186
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100187static int snd_card_dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100189 struct snd_pcm_runtime *runtime = substream->runtime;
190 struct snd_dummy_pcm *dpcm = runtime->private_data;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100191 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100193 spin_lock(&dpcm->lock);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100194 switch (cmd) {
195 case SNDRV_PCM_TRIGGER_START:
196 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100197 snd_card_dummy_pcm_timer_start(dpcm);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100198 break;
199 case SNDRV_PCM_TRIGGER_STOP:
200 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100201 snd_card_dummy_pcm_timer_stop(dpcm);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100202 break;
203 default:
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100204 err = -EINVAL;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100207 spin_unlock(&dpcm->lock);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100211static int snd_card_dummy_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100213 struct snd_pcm_runtime *runtime = substream->runtime;
214 struct snd_dummy_pcm *dpcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 unsigned int bps;
216
217 bps = runtime->rate * runtime->channels;
218 bps *= snd_pcm_format_width(runtime->format);
219 bps /= 8;
220 if (bps <= 0)
221 return -EINVAL;
222 dpcm->pcm_bps = bps;
223 dpcm->pcm_jiffie = bps / HZ;
224 dpcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
225 dpcm->pcm_count = snd_pcm_lib_period_bytes(substream);
226 dpcm->pcm_irq_pos = 0;
227 dpcm->pcm_buf_pos = 0;
228 return 0;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static void snd_card_dummy_pcm_timer_function(unsigned long data)
232{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100233 struct snd_dummy_pcm *dpcm = (struct snd_dummy_pcm *)data;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100234 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Takashi Iwaib32425a2005-11-18 18:52:14 +0100236 spin_lock_irqsave(&dpcm->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 dpcm->timer.expires = 1 + jiffies;
238 add_timer(&dpcm->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 dpcm->pcm_irq_pos += dpcm->pcm_jiffie;
240 dpcm->pcm_buf_pos += dpcm->pcm_jiffie;
241 dpcm->pcm_buf_pos %= dpcm->pcm_size;
242 if (dpcm->pcm_irq_pos >= dpcm->pcm_count) {
243 dpcm->pcm_irq_pos %= dpcm->pcm_count;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100244 spin_unlock_irqrestore(&dpcm->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100246 } else
247 spin_unlock_irqrestore(&dpcm->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100250static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100252 struct snd_pcm_runtime *runtime = substream->runtime;
253 struct snd_dummy_pcm *dpcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
256}
257
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100258static struct snd_pcm_hardware snd_card_dummy_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100261 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .formats = USE_FORMATS,
263 .rates = USE_RATE,
264 .rate_min = USE_RATE_MIN,
265 .rate_max = USE_RATE_MAX,
266 .channels_min = USE_CHANNELS_MIN,
267 .channels_max = USE_CHANNELS_MAX,
268 .buffer_bytes_max = MAX_BUFFER_SIZE,
269 .period_bytes_min = 64,
270 .period_bytes_max = MAX_BUFFER_SIZE,
271 .periods_min = USE_PERIODS_MIN,
272 .periods_max = USE_PERIODS_MAX,
273 .fifo_size = 0,
274};
275
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100276static struct snd_pcm_hardware snd_card_dummy_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100279 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 .formats = USE_FORMATS,
281 .rates = USE_RATE,
282 .rate_min = USE_RATE_MIN,
283 .rate_max = USE_RATE_MAX,
284 .channels_min = USE_CHANNELS_MIN,
285 .channels_max = USE_CHANNELS_MAX,
286 .buffer_bytes_max = MAX_BUFFER_SIZE,
287 .period_bytes_min = 64,
288 .period_bytes_max = MAX_BUFFER_SIZE,
289 .periods_min = USE_PERIODS_MIN,
290 .periods_max = USE_PERIODS_MAX,
291 .fifo_size = 0,
292};
293
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100294static void snd_card_dummy_runtime_free(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100296 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100299static int snd_card_dummy_hw_params(struct snd_pcm_substream *substream,
300 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
303}
304
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100305static int snd_card_dummy_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 return snd_pcm_lib_free_pages(substream);
308}
309
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100310static struct snd_dummy_pcm *new_pcm_stream(struct snd_pcm_substream *substream)
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100311{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100312 struct snd_dummy_pcm *dpcm;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100313
314 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
315 if (! dpcm)
316 return dpcm;
317 init_timer(&dpcm->timer);
318 dpcm->timer.data = (unsigned long) dpcm;
319 dpcm->timer.function = snd_card_dummy_pcm_timer_function;
320 spin_lock_init(&dpcm->lock);
321 dpcm->substream = substream;
322 return dpcm;
323}
324
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100325static int snd_card_dummy_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100327 struct snd_pcm_runtime *runtime = substream->runtime;
328 struct snd_dummy_pcm *dpcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int err;
330
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100331 if ((dpcm = new_pcm_stream(substream)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 runtime->private_data = dpcm;
334 runtime->private_free = snd_card_dummy_runtime_free;
335 runtime->hw = snd_card_dummy_playback;
336 if (substream->pcm->device & 1) {
337 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
338 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
339 }
340 if (substream->pcm->device & 2)
341 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
342 if ((err = add_playback_constraints(runtime)) < 0) {
343 kfree(dpcm);
344 return err;
345 }
346
347 return 0;
348}
349
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100350static int snd_card_dummy_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100352 struct snd_pcm_runtime *runtime = substream->runtime;
353 struct snd_dummy_pcm *dpcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int err;
355
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100356 if ((dpcm = new_pcm_stream(substream)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 runtime->private_data = dpcm;
359 runtime->private_free = snd_card_dummy_runtime_free;
360 runtime->hw = snd_card_dummy_capture;
361 if (substream->pcm->device == 1) {
362 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
363 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
364 }
365 if (substream->pcm->device & 2)
366 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
367 if ((err = add_capture_constraints(runtime)) < 0) {
368 kfree(dpcm);
369 return err;
370 }
371
372 return 0;
373}
374
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100375static int snd_card_dummy_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 return 0;
378}
379
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100380static int snd_card_dummy_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 return 0;
383}
384
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100385static struct snd_pcm_ops snd_card_dummy_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .open = snd_card_dummy_playback_open,
387 .close = snd_card_dummy_playback_close,
388 .ioctl = snd_pcm_lib_ioctl,
389 .hw_params = snd_card_dummy_hw_params,
390 .hw_free = snd_card_dummy_hw_free,
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100391 .prepare = snd_card_dummy_pcm_prepare,
392 .trigger = snd_card_dummy_pcm_trigger,
393 .pointer = snd_card_dummy_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394};
395
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100396static struct snd_pcm_ops snd_card_dummy_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 .open = snd_card_dummy_capture_open,
398 .close = snd_card_dummy_capture_close,
399 .ioctl = snd_pcm_lib_ioctl,
400 .hw_params = snd_card_dummy_hw_params,
401 .hw_free = snd_card_dummy_hw_free,
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100402 .prepare = snd_card_dummy_pcm_prepare,
403 .trigger = snd_card_dummy_pcm_trigger,
404 .pointer = snd_card_dummy_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405};
406
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100407static int __init snd_card_dummy_pcm(struct snd_dummy *dummy, int device, int substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100409 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int err;
411
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100412 if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device,
413 substreams, substreams, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return err;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100415 dummy->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
417 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
418 pcm->private_data = dummy;
419 pcm->info_flags = 0;
420 strcpy(pcm->name, "Dummy PCM");
421 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
422 snd_dma_continuous_data(GFP_KERNEL),
423 0, 64*1024);
424 return 0;
425}
426
427#define DUMMY_VOLUME(xname, xindex, addr) \
428{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
429 .info = snd_dummy_volume_info, \
430 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
431 .private_value = addr }
432
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100433static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
434 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
437 uinfo->count = 2;
438 uinfo->value.integer.min = -50;
439 uinfo->value.integer.max = 100;
440 return 0;
441}
442
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100443static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
444 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100446 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int addr = kcontrol->private_value;
448
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100449 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
451 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100452 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
454}
455
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100456static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
457 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100459 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int change, addr = kcontrol->private_value;
461 int left, right;
462
463 left = ucontrol->value.integer.value[0];
464 if (left < -50)
465 left = -50;
466 if (left > 100)
467 left = 100;
468 right = ucontrol->value.integer.value[1];
469 if (right < -50)
470 right = -50;
471 if (right > 100)
472 right = 100;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100473 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 change = dummy->mixer_volume[addr][0] != left ||
475 dummy->mixer_volume[addr][1] != right;
476 dummy->mixer_volume[addr][0] = left;
477 dummy->mixer_volume[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100478 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return change;
480}
481
482#define DUMMY_CAPSRC(xname, xindex, addr) \
483{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
484 .info = snd_dummy_capsrc_info, \
485 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
486 .private_value = addr }
487
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100488static int snd_dummy_capsrc_info(struct snd_kcontrol *kcontrol,
489 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
492 uinfo->count = 2;
493 uinfo->value.integer.min = 0;
494 uinfo->value.integer.max = 1;
495 return 0;
496}
497
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100498static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
499 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100501 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int addr = kcontrol->private_value;
503
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100504 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
506 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100507 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return 0;
509}
510
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100511static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100513 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int change, addr = kcontrol->private_value;
515 int left, right;
516
517 left = ucontrol->value.integer.value[0] & 1;
518 right = ucontrol->value.integer.value[1] & 1;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100519 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 change = dummy->capture_source[addr][0] != left &&
521 dummy->capture_source[addr][1] != right;
522 dummy->capture_source[addr][0] = left;
523 dummy->capture_source[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100524 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return change;
526}
527
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100528static struct snd_kcontrol_new snd_dummy_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
530DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
531DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
532DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_MASTER),
533DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
534DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_MASTER),
535DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
536DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MASTER),
537DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
538DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_MASTER)
539};
540
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100541static int __init snd_card_dummy_new_mixer(struct snd_dummy *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100543 struct snd_card *card = dummy->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 unsigned int idx;
545 int err;
546
547 snd_assert(dummy != NULL, return -EINVAL);
548 spin_lock_init(&dummy->mixer_lock);
549 strcpy(card->mixername, "Dummy Mixer");
550
551 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
552 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0)
553 return err;
554 }
555 return 0;
556}
557
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100558static int __init snd_dummy_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100560 struct snd_card *card;
561 struct snd_dummy *dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int idx, err;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100563 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100566 sizeof(struct snd_dummy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (card == NULL)
568 return -ENOMEM;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100569 dummy = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 dummy->card = card;
571 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
572 if (pcm_substreams[dev] < 1)
573 pcm_substreams[dev] = 1;
574 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
575 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
576 if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0)
577 goto __nodev;
578 }
579 if ((err = snd_card_dummy_new_mixer(dummy)) < 0)
580 goto __nodev;
581 strcpy(card->driver, "Dummy");
582 strcpy(card->shortname, "Dummy");
583 sprintf(card->longname, "Dummy %i", dev + 1);
Takashi Iwai16dab542005-09-05 17:17:58 +0200584
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100585 snd_card_set_dev(card, &devptr->dev);
Takashi Iwai16dab542005-09-05 17:17:58 +0200586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if ((err = snd_card_register(card)) == 0) {
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100588 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590 }
591 __nodev:
592 snd_card_free(card);
593 return err;
594}
595
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100596static int snd_dummy_remove(struct platform_device *devptr)
597{
598 snd_card_free(platform_get_drvdata(devptr));
599 platform_set_drvdata(devptr, NULL);
600 return 0;
601}
602
603#ifdef CONFIG_PM
604static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
605{
606 struct snd_card *card = platform_get_drvdata(pdev);
607 struct snd_dummy *dummy = card->private_data;
608
609 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
610 snd_pcm_suspend_all(dummy->pcm);
611 return 0;
612}
613
614static int snd_dummy_resume(struct platform_device *pdev)
615{
616 struct snd_card *card = platform_get_drvdata(pdev);
617
618 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
619 return 0;
620}
621#endif
622
623#define SND_DUMMY_DRIVER "snd_dummy"
624
625static struct platform_driver snd_dummy_driver = {
626 .probe = snd_dummy_probe,
627 .remove = snd_dummy_remove,
628#ifdef CONFIG_PM
629 .suspend = snd_dummy_suspend,
630 .resume = snd_dummy_resume,
631#endif
632 .driver = {
633 .name = SND_DUMMY_DRIVER
634 },
635};
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static int __init alsa_card_dummy_init(void)
638{
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100639 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100641 if ((err = platform_driver_register(&snd_dummy_driver)) < 0)
642 return err;
643
644 cards = 0;
645 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
646 struct platform_device *device;
647 device = platform_device_register_simple(SND_DUMMY_DRIVER,
648 i, NULL, 0);
649 if (IS_ERR(device)) {
650 err = PTR_ERR(device);
651 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 cards++;
654 }
655 if (!cards) {
656#ifdef MODULE
657 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
658#endif
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100659 err = -ENODEV;
660 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 return 0;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100663
664 errout:
665 platform_driver_unregister(&snd_dummy_driver);
666 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static void __exit alsa_card_dummy_exit(void)
670{
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100671 platform_driver_unregister(&snd_dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674module_init(alsa_card_dummy_init)
675module_exit(alsa_card_dummy_exit)