ALSA: Add const prefix to proc helper functions
[linux-2.6-block.git] / sound / drivers / dummy.c
CommitLineData
1da177e4
LT
1/*
2 * Dummy soundcard
c1017a4c 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
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
1da177e4 21#include <linux/init.h>
6e65c1cc
TI
22#include <linux/err.h>
23#include <linux/platform_device.h>
1da177e4
LT
24#include <linux/jiffies.h>
25#include <linux/slab.h>
26#include <linux/time.h>
27#include <linux/wait.h>
c631d03c
TI
28#include <linux/hrtimer.h>
29#include <linux/math64.h>
1da177e4
LT
30#include <linux/moduleparam.h>
31#include <sound/core.h>
32#include <sound/control.h>
fb567a8e 33#include <sound/tlv.h>
1da177e4
LT
34#include <sound/pcm.h>
35#include <sound/rawmidi.h>
36#include <sound/initval.h>
37
c1017a4c 38MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4
LT
39MODULE_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)
4a4d2cfd 49static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
1da177e4
LT
50{
51 int err;
1a11cb64
JK
52 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
53 if (err < 0)
1da177e4 54 return err;
1a11cb64 55 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
e78521f3 56 if (err < 0)
1da177e4
LT
57 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
2ad5dd8d
JK
99#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
1da177e4
LT
112
113/* defaults */
114#ifndef MAX_BUFFER_SIZE
115#define MAX_BUFFER_SIZE (64*1024)
116#endif
2ad5dd8d
JK
117#ifndef MAX_PERIOD_SIZE
118#define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
119#endif
1da177e4
LT
120#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};
c631d03c
TI
153#ifdef CONFIG_HIGH_RES_TIMERS
154static int hrtimer = 1;
155#endif
a68c4d11 156static int fake_buffer = 1;
1da177e4
LT
157
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.");
a68c4d11
TI
170module_param(fake_buffer, bool, 0444);
171MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
c631d03c
TI
172#ifdef CONFIG_HIGH_RES_TIMERS
173module_param(hrtimer, bool, 0644);
174MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
175#endif
1da177e4 176
f7a9275d
CL
177static struct platform_device *devices[SNDRV_CARDS];
178
1da177e4
LT
179#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
c631d03c
TI
186struct 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
4a4d2cfd
TI
195struct snd_dummy {
196 struct snd_card *card;
6e65c1cc 197 struct snd_pcm *pcm;
1da177e4
LT
198 spinlock_t mixer_lock;
199 int mixer_volume[MIXER_ADDR_LAST+1][2];
200 int capture_source[MIXER_ADDR_LAST+1][2];
c631d03c 201 const struct dummy_timer_ops *timer_ops;
4a4d2cfd 202};
1da177e4 203
c631d03c
TI
204/*
205 * system timer interface
206 */
207
208struct dummy_systimer_pcm {
1da177e4
LT
209 spinlock_t lock;
210 struct timer_list timer;
b142037b
TI
211 unsigned long base_time;
212 unsigned int frac_pos; /* fractional sample position (based HZ) */
b5d10781 213 unsigned int frac_period_rest;
b142037b
TI
214 unsigned int frac_buffer_size; /* buffer_size * HZ */
215 unsigned int frac_period_size; /* period_size * HZ */
216 unsigned int rate;
b5d10781 217 int elapsed;
4a4d2cfd
TI
218 struct snd_pcm_substream *substream;
219};
1da177e4 220
b142037b
TI
221static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
222{
b142037b 223 dpcm->timer.expires = jiffies +
b5d10781 224 (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate;
b142037b
TI
225 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;
b5d10781
TI
235 dpcm->base_time += delta;
236 delta *= dpcm->rate;
237 dpcm->frac_pos += delta;
b142037b
TI
238 while (dpcm->frac_pos >= dpcm->frac_buffer_size)
239 dpcm->frac_pos -= dpcm->frac_buffer_size;
b5d10781
TI
240 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;
b142037b
TI
245}
246
c631d03c 247static int dummy_systimer_start(struct snd_pcm_substream *substream)
1da177e4 248{
c631d03c
TI
249 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
250 spin_lock(&dpcm->lock);
b142037b
TI
251 dpcm->base_time = jiffies;
252 dummy_systimer_rearm(dpcm);
c631d03c
TI
253 spin_unlock(&dpcm->lock);
254 return 0;
1da177e4
LT
255}
256
c631d03c 257static int dummy_systimer_stop(struct snd_pcm_substream *substream)
1da177e4 258{
c631d03c 259 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
c8eb6ba1 260 spin_lock(&dpcm->lock);
c631d03c 261 del_timer(&dpcm->timer);
c8eb6ba1 262 spin_unlock(&dpcm->lock);
6e65c1cc 263 return 0;
1da177e4
LT
264}
265
c631d03c 266static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
1da177e4 267{
4a4d2cfd 268 struct snd_pcm_runtime *runtime = substream->runtime;
c631d03c 269 struct dummy_systimer_pcm *dpcm = runtime->private_data;
369b240d 270
b142037b
TI
271 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;
b5d10781
TI
275 dpcm->frac_period_rest = dpcm->frac_period_size;
276 dpcm->elapsed = 0;
53463a83 277
1da177e4
LT
278 return 0;
279}
280
c631d03c 281static void dummy_systimer_callback(unsigned long data)
1da177e4 282{
c631d03c 283 struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
b32425ac 284 unsigned long flags;
b5d10781 285 int elapsed = 0;
1da177e4 286
b32425ac 287 spin_lock_irqsave(&dpcm->lock, flags);
b142037b
TI
288 dummy_systimer_update(dpcm);
289 dummy_systimer_rearm(dpcm);
b5d10781
TI
290 elapsed = dpcm->elapsed;
291 dpcm->elapsed = 0;
b142037b 292 spin_unlock_irqrestore(&dpcm->lock, flags);
b5d10781
TI
293 if (elapsed)
294 snd_pcm_period_elapsed(dpcm->substream);
1da177e4
LT
295}
296
c631d03c
TI
297static snd_pcm_uframes_t
298dummy_systimer_pointer(struct snd_pcm_substream *substream)
1da177e4 299{
b142037b 300 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
b5d10781 301 snd_pcm_uframes_t pos;
1da177e4 302
b142037b
TI
303 spin_lock(&dpcm->lock);
304 dummy_systimer_update(dpcm);
b5d10781 305 pos = dpcm->frac_pos / HZ;
b142037b 306 spin_unlock(&dpcm->lock);
b5d10781 307 return pos;
1da177e4
LT
308}
309
c631d03c 310static int dummy_systimer_create(struct snd_pcm_substream *substream)
1da177e4 311{
c631d03c
TI
312 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,
1da177e4
LT
338};
339
c631d03c
TI
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)
1da177e4 413{
c631d03c
TI
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{
c631d03c
TI
487 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
488
c631d03c
TI
489 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),
1da177e4
LT
504 .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,
2ad5dd8d 512 .period_bytes_max = MAX_PERIOD_SIZE,
1da177e4
LT
513 .periods_min = USE_PERIODS_MIN,
514 .periods_max = USE_PERIODS_MAX,
515 .fifo_size = 0,
516};
517
c631d03c
TI
518static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
519 struct snd_pcm_hw_params *hw_params)
1da177e4 520{
a68c4d11
TI
521 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 }
c631d03c
TI
526 return snd_pcm_lib_malloc_pages(substream,
527 params_buffer_bytes(hw_params));
1da177e4
LT
528}
529
c631d03c 530static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
1da177e4 531{
a68c4d11
TI
532 if (fake_buffer)
533 return 0;
1da177e4
LT
534 return snd_pcm_lib_free_pages(substream);
535}
536
c631d03c 537static int dummy_pcm_open(struct snd_pcm_substream *substream)
c8eb6ba1 538{
c631d03c 539 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
4a4d2cfd 540 struct snd_pcm_runtime *runtime = substream->runtime;
c8eb6ba1
TI
541 int err;
542
c631d03c
TI
543 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);
1a11cb64 550 if (err < 0)
1da177e4 551 return err;
1da177e4 552
c631d03c
TI
553 runtime->hw = dummy_pcm_hardware;
554 if (substream->pcm->device & 1) {
1da177e4
LT
555 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
556 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
557 }
558 if (substream->pcm->device & 2)
c631d03c
TI
559 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);
1da177e4 568 return err;
c631d03c 569 }
1da177e4
LT
570 return 0;
571}
572
c631d03c 573static int dummy_pcm_close(struct snd_pcm_substream *substream)
1da177e4 574{
c631d03c
TI
575 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
576 dummy->timer_ops->free(substream);
1da177e4
LT
577 return 0;
578}
579
a68c4d11
TI
580/*
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
c631d03c
TI
634static 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,
1da177e4
LT
643};
644
a68c4d11
TI
645static 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
788c6043
PB
659static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
660 int substreams)
1da177e4 661{
4a4d2cfd 662 struct snd_pcm *pcm;
a68c4d11 663 struct snd_pcm_ops *ops;
1da177e4
LT
664 int err;
665
1a11cb64
JK
666 err = snd_pcm_new(dummy->card, "Dummy PCM", device,
667 substreams, substreams, &pcm);
668 if (err < 0)
1da177e4 669 return err;
6e65c1cc 670 dummy->pcm = pcm;
a68c4d11
TI
671 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);
1da177e4
LT
677 pcm->private_data = dummy;
678 pcm->info_flags = 0;
679 strcpy(pcm->name, "Dummy PCM");
a68c4d11
TI
680 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 }
1da177e4
LT
686 return 0;
687}
688
689#define DUMMY_VOLUME(xname, xindex, addr) \
fb567a8e
TI
690{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
691 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
692 .name = xname, .index = xindex, \
1da177e4
LT
693 .info = snd_dummy_volume_info, \
694 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
fb567a8e
TI
695 .private_value = addr, \
696 .tlv = { .p = db_scale_dummy } }
1da177e4 697
4a4d2cfd
TI
698static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
699 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
700{
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
4a4d2cfd
TI
708static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
709 struct snd_ctl_elem_value *ucontrol)
1da177e4 710{
4a4d2cfd 711 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
1da177e4
LT
712 int addr = kcontrol->private_value;
713
c8eb6ba1 714 spin_lock_irq(&dummy->mixer_lock);
1da177e4
LT
715 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
716 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
c8eb6ba1 717 spin_unlock_irq(&dummy->mixer_lock);
1da177e4
LT
718 return 0;
719}
720
4a4d2cfd
TI
721static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
722 struct snd_ctl_elem_value *ucontrol)
1da177e4 723{
4a4d2cfd 724 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
1da177e4
LT
725 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;
c8eb6ba1 738 spin_lock_irq(&dummy->mixer_lock);
1da177e4
LT
739 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;
c8eb6ba1 743 spin_unlock_irq(&dummy->mixer_lock);
1da177e4
LT
744 return change;
745}
746
0cb29ea0 747static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
fb567a8e 748
1da177e4
LT
749#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
a5ce8890 755#define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
1da177e4 756
4a4d2cfd
TI
757static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
758 struct snd_ctl_elem_value *ucontrol)
1da177e4 759{
4a4d2cfd 760 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
1da177e4
LT
761 int addr = kcontrol->private_value;
762
c8eb6ba1 763 spin_lock_irq(&dummy->mixer_lock);
1da177e4
LT
764 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
765 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
c8eb6ba1 766 spin_unlock_irq(&dummy->mixer_lock);
1da177e4
LT
767 return 0;
768}
769
4a4d2cfd 770static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 771{
4a4d2cfd 772 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
1da177e4
LT
773 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;
c8eb6ba1 778 spin_lock_irq(&dummy->mixer_lock);
1da177e4
LT
779 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;
c8eb6ba1 783 spin_unlock_irq(&dummy->mixer_lock);
1da177e4
LT
784 return change;
785}
786
4a4d2cfd 787static struct snd_kcontrol_new snd_dummy_controls[] = {
1da177e4
LT
788DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
789DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
790DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
e05d6964 791DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
1da177e4 792DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
e05d6964 793DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
1da177e4 794DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
e05d6964 795DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
1da177e4 796DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
e05d6964 797DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
1da177e4
LT
798};
799
788c6043 800static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
1da177e4 801{
4a4d2cfd 802 struct snd_card *card = dummy->card;
1da177e4
LT
803 unsigned int idx;
804 int err;
805
5e246b85
TI
806 if (snd_BUG_ON(!dummy))
807 return -EINVAL;
1da177e4
LT
808 spin_lock_init(&dummy->mixer_lock);
809 strcpy(card->mixername, "Dummy Mixer");
810
811 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
1a11cb64
JK
812 err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy));
813 if (err < 0)
1da177e4
LT
814 return err;
815 }
816 return 0;
817}
818
788c6043 819static int __devinit snd_dummy_probe(struct platform_device *devptr)
1da177e4 820{
4a4d2cfd
TI
821 struct snd_card *card;
822 struct snd_dummy *dummy;
1da177e4 823 int idx, err;
6e65c1cc 824 int dev = devptr->id;
1da177e4 825
bd7dd77c
TI
826 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
827 sizeof(struct snd_dummy), &card);
828 if (err < 0)
829 return err;
4a4d2cfd 830 dummy = card->private_data;
1da177e4
LT
831 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;
1a11cb64
JK
837 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
838 if (err < 0)
1da177e4
LT
839 goto __nodev;
840 }
1a11cb64
JK
841 err = snd_card_dummy_new_mixer(dummy);
842 if (err < 0)
1da177e4
LT
843 goto __nodev;
844 strcpy(card->driver, "Dummy");
845 strcpy(card->shortname, "Dummy");
846 sprintf(card->longname, "Dummy %i", dev + 1);
16dab54b 847
6e65c1cc 848 snd_card_set_dev(card, &devptr->dev);
16dab54b 849
1a11cb64
JK
850 err = snd_card_register(card);
851 if (err == 0) {
6e65c1cc 852 platform_set_drvdata(devptr, card);
1da177e4
LT
853 return 0;
854 }
855 __nodev:
856 snd_card_free(card);
857 return err;
858}
859
788c6043 860static int __devexit snd_dummy_remove(struct platform_device *devptr)
6e65c1cc
TI
861{
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)
1da177e4 869{
6e65c1cc
TI
870 struct snd_card *card = platform_get_drvdata(pdev);
871 struct snd_dummy *dummy = card->private_data;
1da177e4 872
6e65c1cc
TI
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,
788c6043 891 .remove = __devexit_p(snd_dummy_remove),
6e65c1cc
TI
892#ifdef CONFIG_PM
893 .suspend = snd_dummy_suspend,
894 .resume = snd_dummy_resume,
1da177e4 895#endif
6e65c1cc
TI
896 .driver = {
897 .name = SND_DUMMY_DRIVER
898 },
899};
900
c12aad6e 901static void snd_dummy_unregister_all(void)
f7a9275d
CL
902{
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);
a68c4d11 908 free_fake_buffer();
f7a9275d
CL
909}
910
6e65c1cc
TI
911static int __init alsa_card_dummy_init(void)
912{
913 int i, cards, err;
914
1a11cb64
JK
915 err = platform_driver_register(&snd_dummy_driver);
916 if (err < 0)
6e65c1cc
TI
917 return err;
918
a68c4d11
TI
919 err = alloc_fake_buffer();
920 if (err < 0) {
921 platform_driver_unregister(&snd_dummy_driver);
922 return err;
923 }
924
6e65c1cc 925 cards = 0;
8278ca8f 926 for (i = 0; i < SNDRV_CARDS; i++) {
6e65c1cc 927 struct platform_device *device;
8278ca8f
TI
928 if (! enable[i])
929 continue;
6e65c1cc
TI
930 device = platform_device_register_simple(SND_DUMMY_DRIVER,
931 i, NULL, 0);
a182ee98
RH
932 if (IS_ERR(device))
933 continue;
7152447d
RH
934 if (!platform_get_drvdata(device)) {
935 platform_device_unregister(device);
936 continue;
937 }
f7a9275d 938 devices[i] = device;
1da177e4
LT
939 cards++;
940 }
941 if (!cards) {
942#ifdef MODULE
943 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
944#endif
a182ee98
RH
945 snd_dummy_unregister_all();
946 return -ENODEV;
1da177e4
LT
947 }
948 return 0;
949}
950
951static void __exit alsa_card_dummy_exit(void)
952{
f7a9275d 953 snd_dummy_unregister_all();
1da177e4
LT
954}
955
956module_init(alsa_card_dummy_init)
957module_exit(alsa_card_dummy_exit)