blob: 0a798bde0d03315df82c49bd1bcb76f1b22374da [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>
Takashi Iwaic631d032009-09-03 15:59:26 +020028#include <linux/hrtimer.h>
29#include <linux/math64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/moduleparam.h>
31#include <sound/core.h>
32#include <sound/control.h>
Takashi Iwaifb567a82006-08-23 13:07:19 +020033#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <sound/pcm.h>
35#include <sound/rawmidi.h>
36#include <sound/initval.h>
37
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020038MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
40MODULE_LICENSE("GPL");
41MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
42
43#define MAX_PCM_DEVICES 4
44#define MAX_PCM_SUBSTREAMS 16
45#define MAX_MIDI_DEVICES 2
46
47#if 0 /* emu10k1 emulation */
48#define MAX_BUFFER_SIZE (128 * 1024)
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010049static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 int err;
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +020052 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
53 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return err;
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +020055 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
Mariusz Kozlowskie78521f2008-10-19 10:34:22 +020056 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return err;
58 return 0;
59}
60#define add_playback_constraints emu10k1_playback_constraints
61#endif
62
63#if 0 /* RME9652 emulation */
64#define MAX_BUFFER_SIZE (26 * 64 * 1024)
65#define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
66#define USE_CHANNELS_MIN 26
67#define USE_CHANNELS_MAX 26
68#define USE_PERIODS_MIN 2
69#define USE_PERIODS_MAX 2
70#endif
71
72#if 0 /* ICE1712 emulation */
73#define MAX_BUFFER_SIZE (256 * 1024)
74#define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
75#define USE_CHANNELS_MIN 10
76#define USE_CHANNELS_MAX 10
77#define USE_PERIODS_MIN 1
78#define USE_PERIODS_MAX 1024
79#endif
80
81#if 0 /* UDA1341 emulation */
82#define MAX_BUFFER_SIZE (16380)
83#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
84#define USE_CHANNELS_MIN 2
85#define USE_CHANNELS_MAX 2
86#define USE_PERIODS_MIN 2
87#define USE_PERIODS_MAX 255
88#endif
89
90#if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */
91#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
92#define USE_CHANNELS_MIN 2
93#define USE_CHANNELS_MAX 2
94#define USE_RATE SNDRV_PCM_RATE_48000
95#define USE_RATE_MIN 48000
96#define USE_RATE_MAX 48000
97#endif
98
Jaroslav Kysela2ad5dd82006-01-03 14:27:21 +010099#if 0 /* CA0106 */
100#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
101#define USE_CHANNELS_MIN 2
102#define USE_CHANNELS_MAX 2
103#define USE_RATE (SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000)
104#define USE_RATE_MIN 48000
105#define USE_RATE_MAX 192000
106#define MAX_BUFFER_SIZE ((65536-64)*8)
107#define MAX_PERIOD_SIZE (65536-64)
108#define USE_PERIODS_MIN 2
109#define USE_PERIODS_MAX 8
110#endif
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* defaults */
114#ifndef MAX_BUFFER_SIZE
115#define MAX_BUFFER_SIZE (64*1024)
116#endif
Jaroslav Kysela2ad5dd82006-01-03 14:27:21 +0100117#ifndef MAX_PERIOD_SIZE
118#define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
119#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#ifndef USE_FORMATS
121#define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
122#endif
123#ifndef USE_RATE
124#define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
125#define USE_RATE_MIN 5500
126#define USE_RATE_MAX 48000
127#endif
128#ifndef USE_CHANNELS_MIN
129#define USE_CHANNELS_MIN 1
130#endif
131#ifndef USE_CHANNELS_MAX
132#define USE_CHANNELS_MAX 2
133#endif
134#ifndef USE_PERIODS_MIN
135#define USE_PERIODS_MIN 1
136#endif
137#ifndef USE_PERIODS_MAX
138#define USE_PERIODS_MAX 1024
139#endif
140#ifndef add_playback_constraints
141#define add_playback_constraints(x) 0
142#endif
143#ifndef add_capture_constraints
144#define add_capture_constraints(x) 0
145#endif
146
147static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
148static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
149static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
150static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
151static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
152//static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
Takashi Iwaic631d032009-09-03 15:59:26 +0200153#ifdef CONFIG_HIGH_RES_TIMERS
154static int hrtimer = 1;
155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157module_param_array(index, int, NULL, 0444);
158MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
159module_param_array(id, charp, NULL, 0444);
160MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
161module_param_array(enable, bool, NULL, 0444);
162MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
163module_param_array(pcm_devs, int, NULL, 0444);
164MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
165module_param_array(pcm_substreams, int, NULL, 0444);
166MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
167//module_param_array(midi_devs, int, NULL, 0444);
168//MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
Takashi Iwaic631d032009-09-03 15:59:26 +0200169#ifdef CONFIG_HIGH_RES_TIMERS
170module_param(hrtimer, bool, 0644);
171MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Clemens Ladischf7a92752005-12-07 09:13:42 +0100174static struct platform_device *devices[SNDRV_CARDS];
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define MIXER_ADDR_MASTER 0
177#define MIXER_ADDR_LINE 1
178#define MIXER_ADDR_MIC 2
179#define MIXER_ADDR_SYNTH 3
180#define MIXER_ADDR_CD 4
181#define MIXER_ADDR_LAST 4
182
Takashi Iwaic631d032009-09-03 15:59:26 +0200183struct dummy_timer_ops {
184 int (*create)(struct snd_pcm_substream *);
185 void (*free)(struct snd_pcm_substream *);
186 int (*prepare)(struct snd_pcm_substream *);
187 int (*start)(struct snd_pcm_substream *);
188 int (*stop)(struct snd_pcm_substream *);
189 snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
190};
191
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100192struct snd_dummy {
193 struct snd_card *card;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100194 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 spinlock_t mixer_lock;
196 int mixer_volume[MIXER_ADDR_LAST+1][2];
197 int capture_source[MIXER_ADDR_LAST+1][2];
Takashi Iwaic631d032009-09-03 15:59:26 +0200198 const struct dummy_timer_ops *timer_ops;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100199};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Takashi Iwaic631d032009-09-03 15:59:26 +0200201/*
202 * system timer interface
203 */
204
205struct dummy_systimer_pcm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 spinlock_t lock;
207 struct timer_list timer;
Takashi Iwaib1420372009-09-03 16:01:06 +0200208 unsigned long base_time;
209 unsigned int frac_pos; /* fractional sample position (based HZ) */
210 unsigned int frac_buffer_size; /* buffer_size * HZ */
211 unsigned int frac_period_size; /* period_size * HZ */
212 unsigned int rate;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100213 struct snd_pcm_substream *substream;
214};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Takashi Iwaib1420372009-09-03 16:01:06 +0200216static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
217{
218 unsigned long frac;
219
220 frac = dpcm->frac_pos % dpcm->frac_period_size;
221 dpcm->timer.expires = jiffies +
222 (dpcm->frac_period_size + dpcm->rate - 1) / dpcm->rate;
223 add_timer(&dpcm->timer);
224}
225
226static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
227{
228 unsigned long delta;
229
230 delta = jiffies - dpcm->base_time;
231 if (!delta)
232 return;
233 dpcm->base_time = jiffies;
234 dpcm->frac_pos += delta * dpcm->rate;
235 while (dpcm->frac_pos >= dpcm->frac_buffer_size)
236 dpcm->frac_pos -= dpcm->frac_buffer_size;
237}
238
Takashi Iwaic631d032009-09-03 15:59:26 +0200239static int dummy_systimer_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Takashi Iwaic631d032009-09-03 15:59:26 +0200241 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
242 spin_lock(&dpcm->lock);
Takashi Iwaib1420372009-09-03 16:01:06 +0200243 dpcm->base_time = jiffies;
244 dummy_systimer_rearm(dpcm);
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100245 spin_unlock(&dpcm->lock);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Takashi Iwaic631d032009-09-03 15:59:26 +0200249static int dummy_systimer_stop(struct snd_pcm_substream *substream)
250{
251 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
252 spin_lock(&dpcm->lock);
253 del_timer(&dpcm->timer);
254 spin_unlock(&dpcm->lock);
255 return 0;
256}
257
258static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100260 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaic631d032009-09-03 15:59:26 +0200261 struct dummy_systimer_pcm *dpcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Takashi Iwaib1420372009-09-03 16:01:06 +0200263 dpcm->frac_pos = 0;
264 dpcm->rate = runtime->rate;
265 dpcm->frac_buffer_size = runtime->buffer_size * HZ;
266 dpcm->frac_period_size = runtime->period_size * HZ;
Ahmet Ä°nan53463a832008-02-21 07:55:30 +0100267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269}
270
Takashi Iwaic631d032009-09-03 15:59:26 +0200271static void dummy_systimer_callback(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Takashi Iwaic631d032009-09-03 15:59:26 +0200273 struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100274 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Takashi Iwaib32425a2005-11-18 18:52:14 +0100276 spin_lock_irqsave(&dpcm->lock, flags);
Takashi Iwaib1420372009-09-03 16:01:06 +0200277 dummy_systimer_update(dpcm);
278 dummy_systimer_rearm(dpcm);
279 spin_unlock_irqrestore(&dpcm->lock, flags);
280 snd_pcm_period_elapsed(dpcm->substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Takashi Iwaic631d032009-09-03 15:59:26 +0200283static snd_pcm_uframes_t
284dummy_systimer_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Takashi Iwaib1420372009-09-03 16:01:06 +0200286 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Takashi Iwaib1420372009-09-03 16:01:06 +0200288 spin_lock(&dpcm->lock);
289 dummy_systimer_update(dpcm);
290 spin_unlock(&dpcm->lock);
291 return dpcm->frac_pos / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Takashi Iwaic631d032009-09-03 15:59:26 +0200294static int dummy_systimer_create(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Takashi Iwaic631d032009-09-03 15:59:26 +0200296 struct dummy_systimer_pcm *dpcm;
297
298 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
299 if (!dpcm)
300 return -ENOMEM;
301 substream->runtime->private_data = dpcm;
302 init_timer(&dpcm->timer);
303 dpcm->timer.data = (unsigned long) dpcm;
304 dpcm->timer.function = dummy_systimer_callback;
305 spin_lock_init(&dpcm->lock);
306 dpcm->substream = substream;
307 return 0;
308}
309
310static void dummy_systimer_free(struct snd_pcm_substream *substream)
311{
312 kfree(substream->runtime->private_data);
313}
314
315static struct dummy_timer_ops dummy_systimer_ops = {
316 .create = dummy_systimer_create,
317 .free = dummy_systimer_free,
318 .prepare = dummy_systimer_prepare,
319 .start = dummy_systimer_start,
320 .stop = dummy_systimer_stop,
321 .pointer = dummy_systimer_pointer,
322};
323
324#ifdef CONFIG_HIGH_RES_TIMERS
325/*
326 * hrtimer interface
327 */
328
329struct dummy_hrtimer_pcm {
330 ktime_t base_time;
331 ktime_t period_time;
332 atomic_t running;
333 struct hrtimer timer;
334 struct tasklet_struct tasklet;
335 struct snd_pcm_substream *substream;
336};
337
338static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
339{
340 struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
341 if (atomic_read(&dpcm->running))
342 snd_pcm_period_elapsed(dpcm->substream);
343}
344
345static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
346{
347 struct dummy_hrtimer_pcm *dpcm;
348
349 dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
350 if (!atomic_read(&dpcm->running))
351 return HRTIMER_NORESTART;
352 tasklet_schedule(&dpcm->tasklet);
353 hrtimer_forward_now(timer, dpcm->period_time);
354 return HRTIMER_RESTART;
355}
356
357static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
358{
359 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
360
361 dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
362 hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL);
363 atomic_set(&dpcm->running, 1);
364 return 0;
365}
366
367static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
368{
369 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
370
371 atomic_set(&dpcm->running, 0);
372 hrtimer_cancel(&dpcm->timer);
373 return 0;
374}
375
376static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
377{
378 tasklet_kill(&dpcm->tasklet);
379}
380
381static snd_pcm_uframes_t
382dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
383{
384 struct snd_pcm_runtime *runtime = substream->runtime;
385 struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
386 u64 delta;
387 u32 pos;
388
389 delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
390 dpcm->base_time);
391 delta = div_u64(delta * runtime->rate + 999999, 1000000);
392 div_u64_rem(delta, runtime->buffer_size, &pos);
393 return pos;
394}
395
396static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
397{
398 struct snd_pcm_runtime *runtime = substream->runtime;
399 struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
400 unsigned int period, rate;
401 long sec;
402 unsigned long nsecs;
403
404 dummy_hrtimer_sync(dpcm);
405 period = runtime->period_size;
406 rate = runtime->rate;
407 sec = period / rate;
408 period %= rate;
409 nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
410 dpcm->period_time = ktime_set(sec, nsecs);
411
412 return 0;
413}
414
415static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
416{
417 struct dummy_hrtimer_pcm *dpcm;
418
419 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
420 if (!dpcm)
421 return -ENOMEM;
422 substream->runtime->private_data = dpcm;
423 hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
424 dpcm->timer.function = dummy_hrtimer_callback;
425 dpcm->substream = substream;
426 atomic_set(&dpcm->running, 0);
427 tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
428 (unsigned long)dpcm);
429 return 0;
430}
431
432static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
433{
434 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
435 dummy_hrtimer_sync(dpcm);
436 kfree(dpcm);
437}
438
439static struct dummy_timer_ops dummy_hrtimer_ops = {
440 .create = dummy_hrtimer_create,
441 .free = dummy_hrtimer_free,
442 .prepare = dummy_hrtimer_prepare,
443 .start = dummy_hrtimer_start,
444 .stop = dummy_hrtimer_stop,
445 .pointer = dummy_hrtimer_pointer,
446};
447
448#endif /* CONFIG_HIGH_RES_TIMERS */
449
450/*
451 * PCM interface
452 */
453
454static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
455{
456 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
457
458 switch (cmd) {
459 case SNDRV_PCM_TRIGGER_START:
460 case SNDRV_PCM_TRIGGER_RESUME:
461 return dummy->timer_ops->start(substream);
462 case SNDRV_PCM_TRIGGER_STOP:
463 case SNDRV_PCM_TRIGGER_SUSPEND:
464 return dummy->timer_ops->stop(substream);
465 }
466 return -EINVAL;
467}
468
469static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
470{
471 struct snd_pcm_runtime *runtime = substream->runtime;
472 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
473
474 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
475 bytes_to_samples(runtime, runtime->dma_bytes));
476 return dummy->timer_ops->prepare(substream);
477}
478
479static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
480{
481 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
482
483 return dummy->timer_ops->pointer(substream);
484}
485
486static struct snd_pcm_hardware dummy_pcm_hardware = {
487 .info = (SNDRV_PCM_INFO_MMAP |
488 SNDRV_PCM_INFO_INTERLEAVED |
489 SNDRV_PCM_INFO_RESUME |
490 SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 .formats = USE_FORMATS,
492 .rates = USE_RATE,
493 .rate_min = USE_RATE_MIN,
494 .rate_max = USE_RATE_MAX,
495 .channels_min = USE_CHANNELS_MIN,
496 .channels_max = USE_CHANNELS_MAX,
497 .buffer_bytes_max = MAX_BUFFER_SIZE,
498 .period_bytes_min = 64,
Takashi Iwaie05d6962006-08-17 17:12:19 +0200499 .period_bytes_max = MAX_PERIOD_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 .periods_min = USE_PERIODS_MIN,
501 .periods_max = USE_PERIODS_MAX,
502 .fifo_size = 0,
503};
504
Takashi Iwaic631d032009-09-03 15:59:26 +0200505static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
506 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Takashi Iwaic631d032009-09-03 15:59:26 +0200508 return snd_pcm_lib_malloc_pages(substream,
509 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Takashi Iwaic631d032009-09-03 15:59:26 +0200512static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 return snd_pcm_lib_free_pages(substream);
515}
516
Takashi Iwaic631d032009-09-03 15:59:26 +0200517static int dummy_pcm_open(struct snd_pcm_substream *substream)
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100518{
Takashi Iwaic631d032009-09-03 15:59:26 +0200519 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100520 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int err;
522
Takashi Iwaic631d032009-09-03 15:59:26 +0200523 dummy->timer_ops = &dummy_systimer_ops;
524#ifdef CONFIG_HIGH_RES_TIMERS
525 if (hrtimer)
526 dummy->timer_ops = &dummy_hrtimer_ops;
527#endif
528
529 err = dummy->timer_ops->create(substream);
530 if (err < 0)
531 return err;
532
533 runtime->hw = dummy_pcm_hardware;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (substream->pcm->device & 1) {
535 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
536 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
537 }
538 if (substream->pcm->device & 2)
Takashi Iwaic631d032009-09-03 15:59:26 +0200539 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
540 SNDRV_PCM_INFO_MMAP_VALID);
541
542 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
543 err = add_playback_constraints(substream->runtime);
544 else
545 err = add_capture_constraints(substream->runtime);
546 if (err < 0) {
547 dummy->timer_ops->free(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551}
552
Takashi Iwaic631d032009-09-03 15:59:26 +0200553static int dummy_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Takashi Iwaic631d032009-09-03 15:59:26 +0200555 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
556 dummy->timer_ops->free(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
558}
559
Takashi Iwaic631d032009-09-03 15:59:26 +0200560static struct snd_pcm_ops dummy_pcm_ops = {
561 .open = dummy_pcm_open,
562 .close = dummy_pcm_close,
563 .ioctl = snd_pcm_lib_ioctl,
564 .hw_params = dummy_pcm_hw_params,
565 .hw_free = dummy_pcm_hw_free,
566 .prepare = dummy_pcm_prepare,
567 .trigger = dummy_pcm_trigger,
568 .pointer = dummy_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569};
570
Prarit Bhargava788c6042007-02-13 13:11:11 +0100571static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
572 int substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100574 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int err;
576
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200577 err = snd_pcm_new(dummy->card, "Dummy PCM", device,
578 substreams, substreams, &pcm);
579 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return err;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100581 dummy->pcm = pcm;
Takashi Iwaic631d032009-09-03 15:59:26 +0200582 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &dummy_pcm_ops);
583 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &dummy_pcm_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 pcm->private_data = dummy;
585 pcm->info_flags = 0;
586 strcpy(pcm->name, "Dummy PCM");
587 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
588 snd_dma_continuous_data(GFP_KERNEL),
589 0, 64*1024);
590 return 0;
591}
592
593#define DUMMY_VOLUME(xname, xindex, addr) \
Takashi Iwaifb567a82006-08-23 13:07:19 +0200594{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
595 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
596 .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 .info = snd_dummy_volume_info, \
598 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
Takashi Iwaifb567a82006-08-23 13:07:19 +0200599 .private_value = addr, \
600 .tlv = { .p = db_scale_dummy } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100602static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
603 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
606 uinfo->count = 2;
607 uinfo->value.integer.min = -50;
608 uinfo->value.integer.max = 100;
609 return 0;
610}
611
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100612static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
613 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100615 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int addr = kcontrol->private_value;
617
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100618 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
620 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100621 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623}
624
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100625static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
626 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100628 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int change, addr = kcontrol->private_value;
630 int left, right;
631
632 left = ucontrol->value.integer.value[0];
633 if (left < -50)
634 left = -50;
635 if (left > 100)
636 left = 100;
637 right = ucontrol->value.integer.value[1];
638 if (right < -50)
639 right = -50;
640 if (right > 100)
641 right = 100;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100642 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 change = dummy->mixer_volume[addr][0] != left ||
644 dummy->mixer_volume[addr][1] != right;
645 dummy->mixer_volume[addr][0] = left;
646 dummy->mixer_volume[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100647 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return change;
649}
650
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100651static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
Takashi Iwaifb567a82006-08-23 13:07:19 +0200652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#define DUMMY_CAPSRC(xname, xindex, addr) \
654{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
655 .info = snd_dummy_capsrc_info, \
656 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
657 .private_value = addr }
658
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200659#define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100661static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
662 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100664 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int addr = kcontrol->private_value;
666
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100667 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
669 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100670 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return 0;
672}
673
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100674static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100676 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int change, addr = kcontrol->private_value;
678 int left, right;
679
680 left = ucontrol->value.integer.value[0] & 1;
681 right = ucontrol->value.integer.value[1] & 1;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100682 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 change = dummy->capture_source[addr][0] != left &&
684 dummy->capture_source[addr][1] != right;
685 dummy->capture_source[addr][0] = left;
686 dummy->capture_source[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100687 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return change;
689}
690
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100691static struct snd_kcontrol_new snd_dummy_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
693DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
694DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200695DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200697DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200699DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200701DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702};
703
Prarit Bhargava788c6042007-02-13 13:11:11 +0100704static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100706 struct snd_card *card = dummy->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 unsigned int idx;
708 int err;
709
Takashi Iwai5e246b82008-08-08 17:12:47 +0200710 if (snd_BUG_ON(!dummy))
711 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 spin_lock_init(&dummy->mixer_lock);
713 strcpy(card->mixername, "Dummy Mixer");
714
715 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200716 err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy));
717 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return err;
719 }
720 return 0;
721}
722
Prarit Bhargava788c6042007-02-13 13:11:11 +0100723static int __devinit snd_dummy_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100725 struct snd_card *card;
726 struct snd_dummy *dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int idx, err;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100728 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100730 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
731 sizeof(struct snd_dummy), &card);
732 if (err < 0)
733 return err;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100734 dummy = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 dummy->card = card;
736 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
737 if (pcm_substreams[dev] < 1)
738 pcm_substreams[dev] = 1;
739 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
740 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200741 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
742 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto __nodev;
744 }
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200745 err = snd_card_dummy_new_mixer(dummy);
746 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto __nodev;
748 strcpy(card->driver, "Dummy");
749 strcpy(card->shortname, "Dummy");
750 sprintf(card->longname, "Dummy %i", dev + 1);
Takashi Iwai16dab542005-09-05 17:17:58 +0200751
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100752 snd_card_set_dev(card, &devptr->dev);
Takashi Iwai16dab542005-09-05 17:17:58 +0200753
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200754 err = snd_card_register(card);
755 if (err == 0) {
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100756 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758 }
759 __nodev:
760 snd_card_free(card);
761 return err;
762}
763
Prarit Bhargava788c6042007-02-13 13:11:11 +0100764static int __devexit snd_dummy_remove(struct platform_device *devptr)
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100765{
766 snd_card_free(platform_get_drvdata(devptr));
767 platform_set_drvdata(devptr, NULL);
768 return 0;
769}
770
771#ifdef CONFIG_PM
772static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
773{
774 struct snd_card *card = platform_get_drvdata(pdev);
775 struct snd_dummy *dummy = card->private_data;
776
777 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
778 snd_pcm_suspend_all(dummy->pcm);
779 return 0;
780}
781
782static int snd_dummy_resume(struct platform_device *pdev)
783{
784 struct snd_card *card = platform_get_drvdata(pdev);
785
786 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
787 return 0;
788}
789#endif
790
791#define SND_DUMMY_DRIVER "snd_dummy"
792
793static struct platform_driver snd_dummy_driver = {
794 .probe = snd_dummy_probe,
Prarit Bhargava788c6042007-02-13 13:11:11 +0100795 .remove = __devexit_p(snd_dummy_remove),
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100796#ifdef CONFIG_PM
797 .suspend = snd_dummy_suspend,
798 .resume = snd_dummy_resume,
799#endif
800 .driver = {
801 .name = SND_DUMMY_DRIVER
802 },
803};
804
Randy Dunlapc12aad62007-06-25 12:08:01 +0200805static void snd_dummy_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100806{
807 int i;
808
809 for (i = 0; i < ARRAY_SIZE(devices); ++i)
810 platform_device_unregister(devices[i]);
811 platform_driver_unregister(&snd_dummy_driver);
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814static int __init alsa_card_dummy_init(void)
815{
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100816 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200818 err = platform_driver_register(&snd_dummy_driver);
819 if (err < 0)
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100820 return err;
821
822 cards = 0;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100823 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100824 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100825 if (! enable[i])
826 continue;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100827 device = platform_device_register_simple(SND_DUMMY_DRIVER,
828 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +0200829 if (IS_ERR(device))
830 continue;
Rene Herman71524472006-04-13 12:58:06 +0200831 if (!platform_get_drvdata(device)) {
832 platform_device_unregister(device);
833 continue;
834 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100835 devices[i] = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 cards++;
837 }
838 if (!cards) {
839#ifdef MODULE
840 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
841#endif
Rene Hermana182ee92006-04-13 12:57:11 +0200842 snd_dummy_unregister_all();
843 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 return 0;
846}
847
848static void __exit alsa_card_dummy_exit(void)
849{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100850 snd_dummy_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853module_init(alsa_card_dummy_init)
854module_exit(alsa_card_dummy_exit)