blob: 2ee6c8ebe25af7adfbf271be5e97844a5c81cfb9 [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
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200156static int fake_buffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158module_param_array(index, int, NULL, 0444);
159MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
160module_param_array(id, charp, NULL, 0444);
161MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
162module_param_array(enable, bool, NULL, 0444);
163MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
164module_param_array(pcm_devs, int, NULL, 0444);
165MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
166module_param_array(pcm_substreams, int, NULL, 0444);
167MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
168//module_param_array(midi_devs, int, NULL, 0444);
169//MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200170module_param(fake_buffer, bool, 0444);
171MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
Takashi Iwaic631d032009-09-03 15:59:26 +0200172#ifdef CONFIG_HIGH_RES_TIMERS
173module_param(hrtimer, bool, 0644);
174MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Clemens Ladischf7a92752005-12-07 09:13:42 +0100177static struct platform_device *devices[SNDRV_CARDS];
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#define MIXER_ADDR_MASTER 0
180#define MIXER_ADDR_LINE 1
181#define MIXER_ADDR_MIC 2
182#define MIXER_ADDR_SYNTH 3
183#define MIXER_ADDR_CD 4
184#define MIXER_ADDR_LAST 4
185
Takashi Iwaic631d032009-09-03 15:59:26 +0200186struct dummy_timer_ops {
187 int (*create)(struct snd_pcm_substream *);
188 void (*free)(struct snd_pcm_substream *);
189 int (*prepare)(struct snd_pcm_substream *);
190 int (*start)(struct snd_pcm_substream *);
191 int (*stop)(struct snd_pcm_substream *);
192 snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
193};
194
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100195struct snd_dummy {
196 struct snd_card *card;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100197 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 spinlock_t mixer_lock;
199 int mixer_volume[MIXER_ADDR_LAST+1][2];
200 int capture_source[MIXER_ADDR_LAST+1][2];
Takashi Iwaic631d032009-09-03 15:59:26 +0200201 const struct dummy_timer_ops *timer_ops;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100202};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Takashi Iwaic631d032009-09-03 15:59:26 +0200204/*
205 * system timer interface
206 */
207
208struct dummy_systimer_pcm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 spinlock_t lock;
210 struct timer_list timer;
Takashi Iwaib1420372009-09-03 16:01:06 +0200211 unsigned long base_time;
212 unsigned int frac_pos; /* fractional sample position (based HZ) */
Takashi Iwaib5d10782009-09-04 08:45:11 +0200213 unsigned int frac_period_rest;
Takashi Iwaib1420372009-09-03 16:01:06 +0200214 unsigned int frac_buffer_size; /* buffer_size * HZ */
215 unsigned int frac_period_size; /* period_size * HZ */
216 unsigned int rate;
Takashi Iwaib5d10782009-09-04 08:45:11 +0200217 int elapsed;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100218 struct snd_pcm_substream *substream;
219};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Takashi Iwaib1420372009-09-03 16:01:06 +0200221static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
222{
Takashi Iwaib1420372009-09-03 16:01:06 +0200223 dpcm->timer.expires = jiffies +
Takashi Iwaib5d10782009-09-04 08:45:11 +0200224 (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate;
Takashi Iwaib1420372009-09-03 16:01:06 +0200225 add_timer(&dpcm->timer);
226}
227
228static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
229{
230 unsigned long delta;
231
232 delta = jiffies - dpcm->base_time;
233 if (!delta)
234 return;
Takashi Iwaib5d10782009-09-04 08:45:11 +0200235 dpcm->base_time += delta;
236 delta *= dpcm->rate;
237 dpcm->frac_pos += delta;
Takashi Iwaib1420372009-09-03 16:01:06 +0200238 while (dpcm->frac_pos >= dpcm->frac_buffer_size)
239 dpcm->frac_pos -= dpcm->frac_buffer_size;
Takashi Iwaib5d10782009-09-04 08:45:11 +0200240 while (dpcm->frac_period_rest <= delta) {
241 dpcm->elapsed++;
242 dpcm->frac_period_rest += dpcm->frac_period_size;
243 }
244 dpcm->frac_period_rest -= delta;
Takashi Iwaib1420372009-09-03 16:01:06 +0200245}
246
Takashi Iwaic631d032009-09-03 15:59:26 +0200247static int dummy_systimer_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Takashi Iwaic631d032009-09-03 15:59:26 +0200249 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
250 spin_lock(&dpcm->lock);
Takashi Iwaib1420372009-09-03 16:01:06 +0200251 dpcm->base_time = jiffies;
252 dummy_systimer_rearm(dpcm);
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100253 spin_unlock(&dpcm->lock);
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Takashi Iwaic631d032009-09-03 15:59:26 +0200257static int dummy_systimer_stop(struct snd_pcm_substream *substream)
258{
259 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
260 spin_lock(&dpcm->lock);
261 del_timer(&dpcm->timer);
262 spin_unlock(&dpcm->lock);
263 return 0;
264}
265
266static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100268 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaic631d032009-09-03 15:59:26 +0200269 struct dummy_systimer_pcm *dpcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Takashi Iwaib1420372009-09-03 16:01:06 +0200271 dpcm->frac_pos = 0;
272 dpcm->rate = runtime->rate;
273 dpcm->frac_buffer_size = runtime->buffer_size * HZ;
274 dpcm->frac_period_size = runtime->period_size * HZ;
Takashi Iwaib5d10782009-09-04 08:45:11 +0200275 dpcm->frac_period_rest = dpcm->frac_period_size;
276 dpcm->elapsed = 0;
Ahmet Ä°nan53463a82008-02-21 07:55:30 +0100277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
280
Takashi Iwaic631d032009-09-03 15:59:26 +0200281static void dummy_systimer_callback(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Takashi Iwaic631d032009-09-03 15:59:26 +0200283 struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100284 unsigned long flags;
Takashi Iwaib5d10782009-09-04 08:45:11 +0200285 int elapsed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Takashi Iwaib32425a2005-11-18 18:52:14 +0100287 spin_lock_irqsave(&dpcm->lock, flags);
Takashi Iwaib1420372009-09-03 16:01:06 +0200288 dummy_systimer_update(dpcm);
289 dummy_systimer_rearm(dpcm);
Takashi Iwaib5d10782009-09-04 08:45:11 +0200290 elapsed = dpcm->elapsed;
291 dpcm->elapsed = 0;
Takashi Iwaib1420372009-09-03 16:01:06 +0200292 spin_unlock_irqrestore(&dpcm->lock, flags);
Takashi Iwaib5d10782009-09-04 08:45:11 +0200293 if (elapsed)
294 snd_pcm_period_elapsed(dpcm->substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Takashi Iwaic631d032009-09-03 15:59:26 +0200297static snd_pcm_uframes_t
298dummy_systimer_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Takashi Iwaib1420372009-09-03 16:01:06 +0200300 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
Takashi Iwaib5d10782009-09-04 08:45:11 +0200301 snd_pcm_uframes_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Takashi Iwaib1420372009-09-03 16:01:06 +0200303 spin_lock(&dpcm->lock);
304 dummy_systimer_update(dpcm);
Takashi Iwaib5d10782009-09-04 08:45:11 +0200305 pos = dpcm->frac_pos / HZ;
Takashi Iwaib1420372009-09-03 16:01:06 +0200306 spin_unlock(&dpcm->lock);
Takashi Iwaib5d10782009-09-04 08:45:11 +0200307 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Takashi Iwaic631d032009-09-03 15:59:26 +0200310static int dummy_systimer_create(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwaic631d032009-09-03 15:59:26 +0200312 struct dummy_systimer_pcm *dpcm;
313
314 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
315 if (!dpcm)
316 return -ENOMEM;
317 substream->runtime->private_data = dpcm;
318 init_timer(&dpcm->timer);
319 dpcm->timer.data = (unsigned long) dpcm;
320 dpcm->timer.function = dummy_systimer_callback;
321 spin_lock_init(&dpcm->lock);
322 dpcm->substream = substream;
323 return 0;
324}
325
326static void dummy_systimer_free(struct snd_pcm_substream *substream)
327{
328 kfree(substream->runtime->private_data);
329}
330
331static struct dummy_timer_ops dummy_systimer_ops = {
332 .create = dummy_systimer_create,
333 .free = dummy_systimer_free,
334 .prepare = dummy_systimer_prepare,
335 .start = dummy_systimer_start,
336 .stop = dummy_systimer_stop,
337 .pointer = dummy_systimer_pointer,
338};
339
340#ifdef CONFIG_HIGH_RES_TIMERS
341/*
342 * hrtimer interface
343 */
344
345struct dummy_hrtimer_pcm {
346 ktime_t base_time;
347 ktime_t period_time;
348 atomic_t running;
349 struct hrtimer timer;
350 struct tasklet_struct tasklet;
351 struct snd_pcm_substream *substream;
352};
353
354static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
355{
356 struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
357 if (atomic_read(&dpcm->running))
358 snd_pcm_period_elapsed(dpcm->substream);
359}
360
361static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
362{
363 struct dummy_hrtimer_pcm *dpcm;
364
365 dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
366 if (!atomic_read(&dpcm->running))
367 return HRTIMER_NORESTART;
368 tasklet_schedule(&dpcm->tasklet);
369 hrtimer_forward_now(timer, dpcm->period_time);
370 return HRTIMER_RESTART;
371}
372
373static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
374{
375 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
376
377 dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
378 hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL);
379 atomic_set(&dpcm->running, 1);
380 return 0;
381}
382
383static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
384{
385 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
386
387 atomic_set(&dpcm->running, 0);
388 hrtimer_cancel(&dpcm->timer);
389 return 0;
390}
391
392static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
393{
394 tasklet_kill(&dpcm->tasklet);
395}
396
397static snd_pcm_uframes_t
398dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
399{
400 struct snd_pcm_runtime *runtime = substream->runtime;
401 struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
402 u64 delta;
403 u32 pos;
404
405 delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
406 dpcm->base_time);
407 delta = div_u64(delta * runtime->rate + 999999, 1000000);
408 div_u64_rem(delta, runtime->buffer_size, &pos);
409 return pos;
410}
411
412static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
413{
414 struct snd_pcm_runtime *runtime = substream->runtime;
415 struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
416 unsigned int period, rate;
417 long sec;
418 unsigned long nsecs;
419
420 dummy_hrtimer_sync(dpcm);
421 period = runtime->period_size;
422 rate = runtime->rate;
423 sec = period / rate;
424 period %= rate;
425 nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
426 dpcm->period_time = ktime_set(sec, nsecs);
427
428 return 0;
429}
430
431static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
432{
433 struct dummy_hrtimer_pcm *dpcm;
434
435 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
436 if (!dpcm)
437 return -ENOMEM;
438 substream->runtime->private_data = dpcm;
439 hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
440 dpcm->timer.function = dummy_hrtimer_callback;
441 dpcm->substream = substream;
442 atomic_set(&dpcm->running, 0);
443 tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
444 (unsigned long)dpcm);
445 return 0;
446}
447
448static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
449{
450 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
451 dummy_hrtimer_sync(dpcm);
452 kfree(dpcm);
453}
454
455static struct dummy_timer_ops dummy_hrtimer_ops = {
456 .create = dummy_hrtimer_create,
457 .free = dummy_hrtimer_free,
458 .prepare = dummy_hrtimer_prepare,
459 .start = dummy_hrtimer_start,
460 .stop = dummy_hrtimer_stop,
461 .pointer = dummy_hrtimer_pointer,
462};
463
464#endif /* CONFIG_HIGH_RES_TIMERS */
465
466/*
467 * PCM interface
468 */
469
470static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
471{
472 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
473
474 switch (cmd) {
475 case SNDRV_PCM_TRIGGER_START:
476 case SNDRV_PCM_TRIGGER_RESUME:
477 return dummy->timer_ops->start(substream);
478 case SNDRV_PCM_TRIGGER_STOP:
479 case SNDRV_PCM_TRIGGER_SUSPEND:
480 return dummy->timer_ops->stop(substream);
481 }
482 return -EINVAL;
483}
484
485static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
486{
Takashi Iwaic631d032009-09-03 15:59:26 +0200487 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
488
Takashi Iwaic631d032009-09-03 15:59:26 +0200489 return dummy->timer_ops->prepare(substream);
490}
491
492static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
493{
494 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
495
496 return dummy->timer_ops->pointer(substream);
497}
498
499static struct snd_pcm_hardware dummy_pcm_hardware = {
500 .info = (SNDRV_PCM_INFO_MMAP |
501 SNDRV_PCM_INFO_INTERLEAVED |
502 SNDRV_PCM_INFO_RESUME |
503 SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 .formats = USE_FORMATS,
505 .rates = USE_RATE,
506 .rate_min = USE_RATE_MIN,
507 .rate_max = USE_RATE_MAX,
508 .channels_min = USE_CHANNELS_MIN,
509 .channels_max = USE_CHANNELS_MAX,
510 .buffer_bytes_max = MAX_BUFFER_SIZE,
511 .period_bytes_min = 64,
Takashi Iwaie05d6962006-08-17 17:12:19 +0200512 .period_bytes_max = MAX_PERIOD_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 .periods_min = USE_PERIODS_MIN,
514 .periods_max = USE_PERIODS_MAX,
515 .fifo_size = 0,
516};
517
Takashi Iwaic631d032009-09-03 15:59:26 +0200518static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
519 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200521 if (fake_buffer) {
522 /* runtime->dma_bytes has to be set manually to allow mmap */
523 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
524 return 0;
525 }
Takashi Iwaic631d032009-09-03 15:59:26 +0200526 return snd_pcm_lib_malloc_pages(substream,
527 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Takashi Iwaic631d032009-09-03 15:59:26 +0200530static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200532 if (fake_buffer)
533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return snd_pcm_lib_free_pages(substream);
535}
536
Takashi Iwaic631d032009-09-03 15:59:26 +0200537static int dummy_pcm_open(struct snd_pcm_substream *substream)
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100538{
Takashi Iwaic631d032009-09-03 15:59:26 +0200539 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100540 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int err;
542
Takashi Iwaic631d032009-09-03 15:59:26 +0200543 dummy->timer_ops = &dummy_systimer_ops;
544#ifdef CONFIG_HIGH_RES_TIMERS
545 if (hrtimer)
546 dummy->timer_ops = &dummy_hrtimer_ops;
547#endif
548
549 err = dummy->timer_ops->create(substream);
550 if (err < 0)
551 return err;
552
553 runtime->hw = dummy_pcm_hardware;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (substream->pcm->device & 1) {
555 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
556 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
557 }
558 if (substream->pcm->device & 2)
Takashi Iwaic631d032009-09-03 15:59:26 +0200559 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
560 SNDRV_PCM_INFO_MMAP_VALID);
561
562 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
563 err = add_playback_constraints(substream->runtime);
564 else
565 err = add_capture_constraints(substream->runtime);
566 if (err < 0) {
567 dummy->timer_ops->free(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571}
572
Takashi Iwaic631d032009-09-03 15:59:26 +0200573static int dummy_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Takashi Iwaic631d032009-09-03 15:59:26 +0200575 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
576 dummy->timer_ops->free(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578}
579
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200580/*
581 * dummy buffer handling
582 */
583
584static void *dummy_page[2];
585
586static void free_fake_buffer(void)
587{
588 if (fake_buffer) {
589 int i;
590 for (i = 0; i < 2; i++)
591 if (dummy_page[i]) {
592 free_page((unsigned long)dummy_page[i]);
593 dummy_page[i] = NULL;
594 }
595 }
596}
597
598static int alloc_fake_buffer(void)
599{
600 int i;
601
602 if (!fake_buffer)
603 return 0;
604 for (i = 0; i < 2; i++) {
605 dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
606 if (!dummy_page[i]) {
607 free_fake_buffer();
608 return -ENOMEM;
609 }
610 }
611 return 0;
612}
613
614static int dummy_pcm_copy(struct snd_pcm_substream *substream,
615 int channel, snd_pcm_uframes_t pos,
616 void __user *dst, snd_pcm_uframes_t count)
617{
618 return 0; /* do nothing */
619}
620
621static int dummy_pcm_silence(struct snd_pcm_substream *substream,
622 int channel, snd_pcm_uframes_t pos,
623 snd_pcm_uframes_t count)
624{
625 return 0; /* do nothing */
626}
627
628static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
629 unsigned long offset)
630{
631 return virt_to_page(dummy_page[substream->stream]); /* the same page */
632}
633
Takashi Iwaic631d032009-09-03 15:59:26 +0200634static struct snd_pcm_ops dummy_pcm_ops = {
635 .open = dummy_pcm_open,
636 .close = dummy_pcm_close,
637 .ioctl = snd_pcm_lib_ioctl,
638 .hw_params = dummy_pcm_hw_params,
639 .hw_free = dummy_pcm_hw_free,
640 .prepare = dummy_pcm_prepare,
641 .trigger = dummy_pcm_trigger,
642 .pointer = dummy_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643};
644
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200645static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
646 .open = dummy_pcm_open,
647 .close = dummy_pcm_close,
648 .ioctl = snd_pcm_lib_ioctl,
649 .hw_params = dummy_pcm_hw_params,
650 .hw_free = dummy_pcm_hw_free,
651 .prepare = dummy_pcm_prepare,
652 .trigger = dummy_pcm_trigger,
653 .pointer = dummy_pcm_pointer,
654 .copy = dummy_pcm_copy,
655 .silence = dummy_pcm_silence,
656 .page = dummy_pcm_page,
657};
658
Prarit Bhargava788c6042007-02-13 13:11:11 +0100659static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
660 int substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100662 struct snd_pcm *pcm;
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200663 struct snd_pcm_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int err;
665
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200666 err = snd_pcm_new(dummy->card, "Dummy PCM", device,
667 substreams, substreams, &pcm);
668 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return err;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100670 dummy->pcm = pcm;
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200671 if (fake_buffer)
672 ops = &dummy_pcm_ops_no_buf;
673 else
674 ops = &dummy_pcm_ops;
675 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
676 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 pcm->private_data = dummy;
678 pcm->info_flags = 0;
679 strcpy(pcm->name, "Dummy PCM");
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200680 if (!fake_buffer) {
681 snd_pcm_lib_preallocate_pages_for_all(pcm,
682 SNDRV_DMA_TYPE_CONTINUOUS,
683 snd_dma_continuous_data(GFP_KERNEL),
684 0, 64*1024);
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687}
688
689#define DUMMY_VOLUME(xname, xindex, addr) \
Takashi Iwaifb567a82006-08-23 13:07:19 +0200690{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
691 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
692 .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .info = snd_dummy_volume_info, \
694 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
Takashi Iwaifb567a82006-08-23 13:07:19 +0200695 .private_value = addr, \
696 .tlv = { .p = db_scale_dummy } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100698static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
699 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
702 uinfo->count = 2;
703 uinfo->value.integer.min = -50;
704 uinfo->value.integer.max = 100;
705 return 0;
706}
707
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100708static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
709 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100711 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int addr = kcontrol->private_value;
713
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100714 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
716 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100717 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719}
720
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100721static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
722 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100724 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int change, addr = kcontrol->private_value;
726 int left, right;
727
728 left = ucontrol->value.integer.value[0];
729 if (left < -50)
730 left = -50;
731 if (left > 100)
732 left = 100;
733 right = ucontrol->value.integer.value[1];
734 if (right < -50)
735 right = -50;
736 if (right > 100)
737 right = 100;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100738 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 change = dummy->mixer_volume[addr][0] != left ||
740 dummy->mixer_volume[addr][1] != right;
741 dummy->mixer_volume[addr][0] = left;
742 dummy->mixer_volume[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100743 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return change;
745}
746
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100747static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
Takashi Iwaifb567a82006-08-23 13:07:19 +0200748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749#define DUMMY_CAPSRC(xname, xindex, addr) \
750{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
751 .info = snd_dummy_capsrc_info, \
752 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
753 .private_value = addr }
754
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200755#define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100757static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
758 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100760 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int addr = kcontrol->private_value;
762
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100763 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
765 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100766 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return 0;
768}
769
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100770static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100772 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int change, addr = kcontrol->private_value;
774 int left, right;
775
776 left = ucontrol->value.integer.value[0] & 1;
777 right = ucontrol->value.integer.value[1] & 1;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100778 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 change = dummy->capture_source[addr][0] != left &&
780 dummy->capture_source[addr][1] != right;
781 dummy->capture_source[addr][0] = left;
782 dummy->capture_source[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100783 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return change;
785}
786
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100787static struct snd_kcontrol_new snd_dummy_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
789DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
790DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200791DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200793DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200795DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
Takashi Iwaie05d6962006-08-17 17:12:19 +0200797DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798};
799
Prarit Bhargava788c6042007-02-13 13:11:11 +0100800static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100802 struct snd_card *card = dummy->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 unsigned int idx;
804 int err;
805
Takashi Iwai5e246b82008-08-08 17:12:47 +0200806 if (snd_BUG_ON(!dummy))
807 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 spin_lock_init(&dummy->mixer_lock);
809 strcpy(card->mixername, "Dummy Mixer");
810
811 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200812 err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy));
813 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return err;
815 }
816 return 0;
817}
818
Prarit Bhargava788c6042007-02-13 13:11:11 +0100819static int __devinit snd_dummy_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100821 struct snd_card *card;
822 struct snd_dummy *dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int idx, err;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100824 int dev = devptr->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100826 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
827 sizeof(struct snd_dummy), &card);
828 if (err < 0)
829 return err;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100830 dummy = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 dummy->card = card;
832 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
833 if (pcm_substreams[dev] < 1)
834 pcm_substreams[dev] = 1;
835 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
836 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200837 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
838 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 goto __nodev;
840 }
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200841 err = snd_card_dummy_new_mixer(dummy);
842 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 goto __nodev;
844 strcpy(card->driver, "Dummy");
845 strcpy(card->shortname, "Dummy");
846 sprintf(card->longname, "Dummy %i", dev + 1);
Takashi Iwai16dab542005-09-05 17:17:58 +0200847
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100848 snd_card_set_dev(card, &devptr->dev);
Takashi Iwai16dab542005-09-05 17:17:58 +0200849
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200850 err = snd_card_register(card);
851 if (err == 0) {
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100852 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 0;
854 }
855 __nodev:
856 snd_card_free(card);
857 return err;
858}
859
Prarit Bhargava788c6042007-02-13 13:11:11 +0100860static int __devexit snd_dummy_remove(struct platform_device *devptr)
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100861{
862 snd_card_free(platform_get_drvdata(devptr));
863 platform_set_drvdata(devptr, NULL);
864 return 0;
865}
866
867#ifdef CONFIG_PM
868static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
869{
870 struct snd_card *card = platform_get_drvdata(pdev);
871 struct snd_dummy *dummy = card->private_data;
872
873 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
874 snd_pcm_suspend_all(dummy->pcm);
875 return 0;
876}
877
878static int snd_dummy_resume(struct platform_device *pdev)
879{
880 struct snd_card *card = platform_get_drvdata(pdev);
881
882 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
883 return 0;
884}
885#endif
886
887#define SND_DUMMY_DRIVER "snd_dummy"
888
889static struct platform_driver snd_dummy_driver = {
890 .probe = snd_dummy_probe,
Prarit Bhargava788c6042007-02-13 13:11:11 +0100891 .remove = __devexit_p(snd_dummy_remove),
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100892#ifdef CONFIG_PM
893 .suspend = snd_dummy_suspend,
894 .resume = snd_dummy_resume,
895#endif
896 .driver = {
897 .name = SND_DUMMY_DRIVER
898 },
899};
900
Randy Dunlapc12aad62007-06-25 12:08:01 +0200901static void snd_dummy_unregister_all(void)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100902{
903 int i;
904
905 for (i = 0; i < ARRAY_SIZE(devices); ++i)
906 platform_device_unregister(devices[i]);
907 platform_driver_unregister(&snd_dummy_driver);
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200908 free_fake_buffer();
Clemens Ladischf7a92752005-12-07 09:13:42 +0100909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static int __init alsa_card_dummy_init(void)
912{
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100913 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jaroslav Kysela1a11cb62008-08-15 12:59:02 +0200915 err = platform_driver_register(&snd_dummy_driver);
916 if (err < 0)
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100917 return err;
918
Takashi Iwaia68c4d12009-09-04 12:19:36 +0200919 err = alloc_fake_buffer();
920 if (err < 0) {
921 platform_driver_unregister(&snd_dummy_driver);
922 return err;
923 }
924
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100925 cards = 0;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100926 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100927 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +0100928 if (! enable[i])
929 continue;
Takashi Iwai6e65c1c2005-11-17 16:01:56 +0100930 device = platform_device_register_simple(SND_DUMMY_DRIVER,
931 i, NULL, 0);
Rene Hermana182ee92006-04-13 12:57:11 +0200932 if (IS_ERR(device))
933 continue;
Rene Herman71524472006-04-13 12:58:06 +0200934 if (!platform_get_drvdata(device)) {
935 platform_device_unregister(device);
936 continue;
937 }
Clemens Ladischf7a92752005-12-07 09:13:42 +0100938 devices[i] = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 cards++;
940 }
941 if (!cards) {
942#ifdef MODULE
943 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
944#endif
Rene Hermana182ee92006-04-13 12:57:11 +0200945 snd_dummy_unregister_all();
946 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 return 0;
949}
950
951static void __exit alsa_card_dummy_exit(void)
952{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100953 snd_dummy_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956module_init(alsa_card_dummy_init)
957module_exit(alsa_card_dummy_exit)