]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - sound/drivers/dummy.c
Merge tag 'ext4_for_linus_fixes2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-hirsute-kernel.git] / sound / drivers / dummy.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Dummy soundcard
c1017a4c 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
5 */
6
1da177e4 7#include <linux/init.h>
6e65c1cc
TI
8#include <linux/err.h>
9#include <linux/platform_device.h>
1da177e4
LT
10#include <linux/jiffies.h>
11#include <linux/slab.h>
12#include <linux/time.h>
13#include <linux/wait.h>
c631d03c
TI
14#include <linux/hrtimer.h>
15#include <linux/math64.h>
65a77217 16#include <linux/module.h>
1da177e4
LT
17#include <sound/core.h>
18#include <sound/control.h>
fb567a8e 19#include <sound/tlv.h>
1da177e4
LT
20#include <sound/pcm.h>
21#include <sound/rawmidi.h>
9b151fec 22#include <sound/info.h>
1da177e4
LT
23#include <sound/initval.h>
24
c1017a4c 25MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4
LT
26MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
27MODULE_LICENSE("GPL");
28MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
29
30#define MAX_PCM_DEVICES 4
b888d1ce 31#define MAX_PCM_SUBSTREAMS 128
1da177e4
LT
32#define MAX_MIDI_DEVICES 2
33
1da177e4 34/* defaults */
1da177e4 35#define MAX_BUFFER_SIZE (64*1024)
d5e1ca05 36#define MIN_PERIOD_SIZE 64
2ad5dd8d 37#define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
1da177e4 38#define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
1da177e4
LT
39#define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
40#define USE_RATE_MIN 5500
41#define USE_RATE_MAX 48000
1da177e4 42#define USE_CHANNELS_MIN 1
1da177e4 43#define USE_CHANNELS_MAX 2
1da177e4 44#define USE_PERIODS_MIN 1
1da177e4 45#define USE_PERIODS_MAX 1024
1da177e4
LT
46
47static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
a67ff6a5 49static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
d5e1ca05 50static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL};
1da177e4
LT
51static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
52static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
53//static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
c631d03c 54#ifdef CONFIG_HIGH_RES_TIMERS
a67ff6a5 55static bool hrtimer = 1;
c631d03c 56#endif
a67ff6a5 57static bool fake_buffer = 1;
1da177e4
LT
58
59module_param_array(index, int, NULL, 0444);
60MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
61module_param_array(id, charp, NULL, 0444);
62MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
63module_param_array(enable, bool, NULL, 0444);
64MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
d5e1ca05
JK
65module_param_array(model, charp, NULL, 0444);
66MODULE_PARM_DESC(model, "Soundcard model.");
1da177e4
LT
67module_param_array(pcm_devs, int, NULL, 0444);
68MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
69module_param_array(pcm_substreams, int, NULL, 0444);
23aebca4 70MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver.");
1da177e4
LT
71//module_param_array(midi_devs, int, NULL, 0444);
72//MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
a68c4d11
TI
73module_param(fake_buffer, bool, 0444);
74MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
c631d03c 75#ifdef CONFIG_HIGH_RES_TIMERS
ddce57a6 76module_param(hrtimer, bool, 0644);
c631d03c
TI
77MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
78#endif
1da177e4 79
f7a9275d
CL
80static struct platform_device *devices[SNDRV_CARDS];
81
1da177e4
LT
82#define MIXER_ADDR_MASTER 0
83#define MIXER_ADDR_LINE 1
84#define MIXER_ADDR_MIC 2
85#define MIXER_ADDR_SYNTH 3
86#define MIXER_ADDR_CD 4
87#define MIXER_ADDR_LAST 4
88
c631d03c
TI
89struct dummy_timer_ops {
90 int (*create)(struct snd_pcm_substream *);
91 void (*free)(struct snd_pcm_substream *);
92 int (*prepare)(struct snd_pcm_substream *);
93 int (*start)(struct snd_pcm_substream *);
94 int (*stop)(struct snd_pcm_substream *);
95 snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
96};
97
ddce57a6
TI
98#define get_dummy_ops(substream) \
99 (*(const struct dummy_timer_ops **)(substream)->runtime->private_data)
100
d5e1ca05
JK
101struct dummy_model {
102 const char *name;
103 int (*playback_constraints)(struct snd_pcm_runtime *runtime);
104 int (*capture_constraints)(struct snd_pcm_runtime *runtime);
105 u64 formats;
106 size_t buffer_bytes_max;
107 size_t period_bytes_min;
108 size_t period_bytes_max;
109 unsigned int periods_min;
110 unsigned int periods_max;
111 unsigned int rates;
112 unsigned int rate_min;
113 unsigned int rate_max;
114 unsigned int channels_min;
115 unsigned int channels_max;
116};
117
4a4d2cfd
TI
118struct snd_dummy {
119 struct snd_card *card;
d64e7f7c 120 const struct dummy_model *model;
6e65c1cc 121 struct snd_pcm *pcm;
d5e1ca05 122 struct snd_pcm_hardware pcm_hw;
1da177e4
LT
123 spinlock_t mixer_lock;
124 int mixer_volume[MIXER_ADDR_LAST+1][2];
125 int capture_source[MIXER_ADDR_LAST+1][2];
16e43467
CL
126 int iobox;
127 struct snd_kcontrol *cd_volume_ctl;
128 struct snd_kcontrol *cd_switch_ctl;
4a4d2cfd 129};
1da177e4 130
d5e1ca05
JK
131/*
132 * card models
133 */
134
135static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
136{
137 int err;
138 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
139 if (err < 0)
140 return err;
141 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
142 if (err < 0)
143 return err;
144 return 0;
145}
146
d64e7f7c 147static const struct dummy_model model_emu10k1 = {
d5e1ca05
JK
148 .name = "emu10k1",
149 .playback_constraints = emu10k1_playback_constraints,
150 .buffer_bytes_max = 128 * 1024,
151};
152
d64e7f7c 153static const struct dummy_model model_rme9652 = {
d5e1ca05
JK
154 .name = "rme9652",
155 .buffer_bytes_max = 26 * 64 * 1024,
156 .formats = SNDRV_PCM_FMTBIT_S32_LE,
157 .channels_min = 26,
158 .channels_max = 26,
159 .periods_min = 2,
160 .periods_max = 2,
161};
162
d64e7f7c 163static const struct dummy_model model_ice1712 = {
d5e1ca05
JK
164 .name = "ice1712",
165 .buffer_bytes_max = 256 * 1024,
166 .formats = SNDRV_PCM_FMTBIT_S32_LE,
167 .channels_min = 10,
168 .channels_max = 10,
169 .periods_min = 1,
170 .periods_max = 1024,
171};
172
d64e7f7c 173static const struct dummy_model model_uda1341 = {
d5e1ca05
JK
174 .name = "uda1341",
175 .buffer_bytes_max = 16380,
176 .formats = SNDRV_PCM_FMTBIT_S16_LE,
177 .channels_min = 2,
178 .channels_max = 2,
179 .periods_min = 2,
180 .periods_max = 255,
181};
182
d64e7f7c 183static const struct dummy_model model_ac97 = {
d5e1ca05
JK
184 .name = "ac97",
185 .formats = SNDRV_PCM_FMTBIT_S16_LE,
186 .channels_min = 2,
187 .channels_max = 2,
188 .rates = SNDRV_PCM_RATE_48000,
189 .rate_min = 48000,
190 .rate_max = 48000,
191};
192
d64e7f7c 193static const struct dummy_model model_ca0106 = {
d5e1ca05
JK
194 .name = "ca0106",
195 .formats = SNDRV_PCM_FMTBIT_S16_LE,
196 .buffer_bytes_max = ((65536-64)*8),
197 .period_bytes_max = (65536-64),
198 .periods_min = 2,
199 .periods_max = 8,
200 .channels_min = 2,
201 .channels_max = 2,
202 .rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000,
203 .rate_min = 48000,
204 .rate_max = 192000,
205};
206
d64e7f7c 207static const struct dummy_model *dummy_models[] = {
d5e1ca05
JK
208 &model_emu10k1,
209 &model_rme9652,
210 &model_ice1712,
211 &model_uda1341,
212 &model_ac97,
213 &model_ca0106,
214 NULL
215};
216
c631d03c
TI
217/*
218 * system timer interface
219 */
220
221struct dummy_systimer_pcm {
ddce57a6
TI
222 /* ops must be the first item */
223 const struct dummy_timer_ops *timer_ops;
1da177e4
LT
224 spinlock_t lock;
225 struct timer_list timer;
b142037b
TI
226 unsigned long base_time;
227 unsigned int frac_pos; /* fractional sample position (based HZ) */
b5d10781 228 unsigned int frac_period_rest;
b142037b
TI
229 unsigned int frac_buffer_size; /* buffer_size * HZ */
230 unsigned int frac_period_size; /* period_size * HZ */
231 unsigned int rate;
b5d10781 232 int elapsed;
4a4d2cfd
TI
233 struct snd_pcm_substream *substream;
234};
1da177e4 235
b142037b
TI
236static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
237{
2a52b6ee
RK
238 mod_timer(&dpcm->timer, jiffies +
239 (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate);
b142037b
TI
240}
241
242static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
243{
244 unsigned long delta;
245
246 delta = jiffies - dpcm->base_time;
247 if (!delta)
248 return;
b5d10781
TI
249 dpcm->base_time += delta;
250 delta *= dpcm->rate;
251 dpcm->frac_pos += delta;
b142037b
TI
252 while (dpcm->frac_pos >= dpcm->frac_buffer_size)
253 dpcm->frac_pos -= dpcm->frac_buffer_size;
b5d10781
TI
254 while (dpcm->frac_period_rest <= delta) {
255 dpcm->elapsed++;
256 dpcm->frac_period_rest += dpcm->frac_period_size;
257 }
258 dpcm->frac_period_rest -= delta;
b142037b
TI
259}
260
c631d03c 261static int dummy_systimer_start(struct snd_pcm_substream *substream)
1da177e4 262{
c631d03c
TI
263 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
264 spin_lock(&dpcm->lock);
b142037b
TI
265 dpcm->base_time = jiffies;
266 dummy_systimer_rearm(dpcm);
c631d03c
TI
267 spin_unlock(&dpcm->lock);
268 return 0;
1da177e4
LT
269}
270
c631d03c 271static int dummy_systimer_stop(struct snd_pcm_substream *substream)
1da177e4 272{
c631d03c 273 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
c8eb6ba1 274 spin_lock(&dpcm->lock);
c631d03c 275 del_timer(&dpcm->timer);
c8eb6ba1 276 spin_unlock(&dpcm->lock);
6e65c1cc 277 return 0;
1da177e4
LT
278}
279
c631d03c 280static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
1da177e4 281{
4a4d2cfd 282 struct snd_pcm_runtime *runtime = substream->runtime;
c631d03c 283 struct dummy_systimer_pcm *dpcm = runtime->private_data;
369b240d 284
b142037b
TI
285 dpcm->frac_pos = 0;
286 dpcm->rate = runtime->rate;
287 dpcm->frac_buffer_size = runtime->buffer_size * HZ;
288 dpcm->frac_period_size = runtime->period_size * HZ;
b5d10781
TI
289 dpcm->frac_period_rest = dpcm->frac_period_size;
290 dpcm->elapsed = 0;
53463a83 291
1da177e4
LT
292 return 0;
293}
294
bc47ba90 295static void dummy_systimer_callback(struct timer_list *t)
1da177e4 296{
bc47ba90 297 struct dummy_systimer_pcm *dpcm = from_timer(dpcm, t, timer);
b32425ac 298 unsigned long flags;
b5d10781 299 int elapsed = 0;
1da177e4 300
b32425ac 301 spin_lock_irqsave(&dpcm->lock, flags);
b142037b
TI
302 dummy_systimer_update(dpcm);
303 dummy_systimer_rearm(dpcm);
b5d10781
TI
304 elapsed = dpcm->elapsed;
305 dpcm->elapsed = 0;
b142037b 306 spin_unlock_irqrestore(&dpcm->lock, flags);
b5d10781
TI
307 if (elapsed)
308 snd_pcm_period_elapsed(dpcm->substream);
1da177e4
LT
309}
310
c631d03c
TI
311static snd_pcm_uframes_t
312dummy_systimer_pointer(struct snd_pcm_substream *substream)
1da177e4 313{
b142037b 314 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
b5d10781 315 snd_pcm_uframes_t pos;
1da177e4 316
b142037b
TI
317 spin_lock(&dpcm->lock);
318 dummy_systimer_update(dpcm);
b5d10781 319 pos = dpcm->frac_pos / HZ;
b142037b 320 spin_unlock(&dpcm->lock);
b5d10781 321 return pos;
1da177e4
LT
322}
323
c631d03c 324static int dummy_systimer_create(struct snd_pcm_substream *substream)
1da177e4 325{
c631d03c
TI
326 struct dummy_systimer_pcm *dpcm;
327
328 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
329 if (!dpcm)
330 return -ENOMEM;
331 substream->runtime->private_data = dpcm;
bc47ba90 332 timer_setup(&dpcm->timer, dummy_systimer_callback, 0);
c631d03c
TI
333 spin_lock_init(&dpcm->lock);
334 dpcm->substream = substream;
335 return 0;
336}
337
338static void dummy_systimer_free(struct snd_pcm_substream *substream)
339{
340 kfree(substream->runtime->private_data);
341}
342
d8c5ed75 343static const struct dummy_timer_ops dummy_systimer_ops = {
c631d03c
TI
344 .create = dummy_systimer_create,
345 .free = dummy_systimer_free,
346 .prepare = dummy_systimer_prepare,
347 .start = dummy_systimer_start,
348 .stop = dummy_systimer_stop,
349 .pointer = dummy_systimer_pointer,
1da177e4
LT
350};
351
c631d03c
TI
352#ifdef CONFIG_HIGH_RES_TIMERS
353/*
354 * hrtimer interface
355 */
356
357struct dummy_hrtimer_pcm {
ddce57a6
TI
358 /* ops must be the first item */
359 const struct dummy_timer_ops *timer_ops;
c631d03c
TI
360 ktime_t base_time;
361 ktime_t period_time;
362 atomic_t running;
363 struct hrtimer timer;
c631d03c
TI
364 struct snd_pcm_substream *substream;
365};
366
c631d03c
TI
367static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
368{
369 struct dummy_hrtimer_pcm *dpcm;
370
371 dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
372 if (!atomic_read(&dpcm->running))
373 return HRTIMER_NORESTART;
b03bbbe0
TG
374 /*
375 * In cases of XRUN and draining, this calls .trigger to stop PCM
376 * substream.
377 */
378 snd_pcm_period_elapsed(dpcm->substream);
379 if (!atomic_read(&dpcm->running))
380 return HRTIMER_NORESTART;
381
c631d03c
TI
382 hrtimer_forward_now(timer, dpcm->period_time);
383 return HRTIMER_RESTART;
384}
385
386static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
387{
388 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
389
390 dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
b03bbbe0 391 hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL_SOFT);
c631d03c
TI
392 atomic_set(&dpcm->running, 1);
393 return 0;
394}
395
396static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
397{
398 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
399
400 atomic_set(&dpcm->running, 0);
b03bbbe0
TG
401 if (!hrtimer_callback_running(&dpcm->timer))
402 hrtimer_cancel(&dpcm->timer);
c631d03c
TI
403 return 0;
404}
405
406static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
407{
d5dbbe65 408 hrtimer_cancel(&dpcm->timer);
c631d03c
TI
409}
410
411static snd_pcm_uframes_t
412dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
413{
414 struct snd_pcm_runtime *runtime = substream->runtime;
415 struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
416 u64 delta;
417 u32 pos;
418
419 delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
420 dpcm->base_time);
421 delta = div_u64(delta * runtime->rate + 999999, 1000000);
422 div_u64_rem(delta, runtime->buffer_size, &pos);
423 return pos;
424}
425
426static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
1da177e4 427{
c631d03c
TI
428 struct snd_pcm_runtime *runtime = substream->runtime;
429 struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
430 unsigned int period, rate;
431 long sec;
432 unsigned long nsecs;
433
434 dummy_hrtimer_sync(dpcm);
435 period = runtime->period_size;
436 rate = runtime->rate;
437 sec = period / rate;
438 period %= rate;
439 nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
440 dpcm->period_time = ktime_set(sec, nsecs);
441
442 return 0;
443}
444
445static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
446{
447 struct dummy_hrtimer_pcm *dpcm;
448
449 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
450 if (!dpcm)
451 return -ENOMEM;
452 substream->runtime->private_data = dpcm;
b03bbbe0 453 hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
c631d03c
TI
454 dpcm->timer.function = dummy_hrtimer_callback;
455 dpcm->substream = substream;
456 atomic_set(&dpcm->running, 0);
c631d03c
TI
457 return 0;
458}
459
460static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
461{
462 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
463 dummy_hrtimer_sync(dpcm);
464 kfree(dpcm);
465}
466
d8c5ed75 467static const struct dummy_timer_ops dummy_hrtimer_ops = {
c631d03c
TI
468 .create = dummy_hrtimer_create,
469 .free = dummy_hrtimer_free,
470 .prepare = dummy_hrtimer_prepare,
471 .start = dummy_hrtimer_start,
472 .stop = dummy_hrtimer_stop,
473 .pointer = dummy_hrtimer_pointer,
474};
475
476#endif /* CONFIG_HIGH_RES_TIMERS */
477
478/*
479 * PCM interface
480 */
481
482static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
483{
c631d03c
TI
484 switch (cmd) {
485 case SNDRV_PCM_TRIGGER_START:
486 case SNDRV_PCM_TRIGGER_RESUME:
ddce57a6 487 return get_dummy_ops(substream)->start(substream);
c631d03c
TI
488 case SNDRV_PCM_TRIGGER_STOP:
489 case SNDRV_PCM_TRIGGER_SUSPEND:
ddce57a6 490 return get_dummy_ops(substream)->stop(substream);
c631d03c
TI
491 }
492 return -EINVAL;
493}
494
495static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
496{
ddce57a6 497 return get_dummy_ops(substream)->prepare(substream);
c631d03c
TI
498}
499
500static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
501{
ddce57a6 502 return get_dummy_ops(substream)->pointer(substream);
c631d03c
TI
503}
504
b6c0b715 505static const struct snd_pcm_hardware dummy_pcm_hardware = {
c631d03c
TI
506 .info = (SNDRV_PCM_INFO_MMAP |
507 SNDRV_PCM_INFO_INTERLEAVED |
508 SNDRV_PCM_INFO_RESUME |
509 SNDRV_PCM_INFO_MMAP_VALID),
1da177e4
LT
510 .formats = USE_FORMATS,
511 .rates = USE_RATE,
512 .rate_min = USE_RATE_MIN,
513 .rate_max = USE_RATE_MAX,
514 .channels_min = USE_CHANNELS_MIN,
515 .channels_max = USE_CHANNELS_MAX,
516 .buffer_bytes_max = MAX_BUFFER_SIZE,
d5e1ca05 517 .period_bytes_min = MIN_PERIOD_SIZE,
2ad5dd8d 518 .period_bytes_max = MAX_PERIOD_SIZE,
1da177e4
LT
519 .periods_min = USE_PERIODS_MIN,
520 .periods_max = USE_PERIODS_MAX,
521 .fifo_size = 0,
522};
523
c631d03c
TI
524static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
525 struct snd_pcm_hw_params *hw_params)
1da177e4 526{
a68c4d11
TI
527 if (fake_buffer) {
528 /* runtime->dma_bytes has to be set manually to allow mmap */
529 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
530 return 0;
531 }
ef1545b8 532 return 0;
1da177e4
LT
533}
534
c631d03c 535static int dummy_pcm_open(struct snd_pcm_substream *substream)
c8eb6ba1 536{
c631d03c 537 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
d64e7f7c 538 const struct dummy_model *model = dummy->model;
4a4d2cfd 539 struct snd_pcm_runtime *runtime = substream->runtime;
ddce57a6 540 const struct dummy_timer_ops *ops;
c8eb6ba1
TI
541 int err;
542
ddce57a6 543 ops = &dummy_systimer_ops;
c631d03c
TI
544#ifdef CONFIG_HIGH_RES_TIMERS
545 if (hrtimer)
ddce57a6 546 ops = &dummy_hrtimer_ops;
c631d03c
TI
547#endif
548
ddce57a6 549 err = ops->create(substream);
1a11cb64 550 if (err < 0)
1da177e4 551 return err;
ddce57a6 552 get_dummy_ops(substream) = ops;
1da177e4 553
d5e1ca05 554 runtime->hw = dummy->pcm_hw;
c631d03c 555 if (substream->pcm->device & 1) {
1da177e4
LT
556 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
557 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
558 }
559 if (substream->pcm->device & 2)
c631d03c
TI
560 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
561 SNDRV_PCM_INFO_MMAP_VALID);
562
d5e1ca05
JK
563 if (model == NULL)
564 return 0;
565
566 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
567 if (model->playback_constraints)
568 err = model->playback_constraints(substream->runtime);
569 } else {
570 if (model->capture_constraints)
571 err = model->capture_constraints(substream->runtime);
572 }
c631d03c 573 if (err < 0) {
ddce57a6 574 get_dummy_ops(substream)->free(substream);
1da177e4 575 return err;
c631d03c 576 }
1da177e4
LT
577 return 0;
578}
579
c631d03c 580static int dummy_pcm_close(struct snd_pcm_substream *substream)
1da177e4 581{
ddce57a6 582 get_dummy_ops(substream)->free(substream);
1da177e4
LT
583 return 0;
584}
585
a68c4d11
TI
586/*
587 * dummy buffer handling
588 */
589
590static void *dummy_page[2];
591
592static void free_fake_buffer(void)
593{
594 if (fake_buffer) {
595 int i;
596 for (i = 0; i < 2; i++)
597 if (dummy_page[i]) {
598 free_page((unsigned long)dummy_page[i]);
599 dummy_page[i] = NULL;
600 }
601 }
602}
603
604static int alloc_fake_buffer(void)
605{
606 int i;
607
608 if (!fake_buffer)
609 return 0;
610 for (i = 0; i < 2; i++) {
611 dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
612 if (!dummy_page[i]) {
613 free_fake_buffer();
614 return -ENOMEM;
615 }
616 }
617 return 0;
618}
619
620static int dummy_pcm_copy(struct snd_pcm_substream *substream,
d53611d2
TI
621 int channel, unsigned long pos,
622 void __user *dst, unsigned long bytes)
623{
624 return 0; /* do nothing */
625}
626
627static int dummy_pcm_copy_kernel(struct snd_pcm_substream *substream,
628 int channel, unsigned long pos,
629 void *dst, unsigned long bytes)
a68c4d11
TI
630{
631 return 0; /* do nothing */
632}
633
634static int dummy_pcm_silence(struct snd_pcm_substream *substream,
d53611d2
TI
635 int channel, unsigned long pos,
636 unsigned long bytes)
a68c4d11
TI
637{
638 return 0; /* do nothing */
639}
640
641static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
642 unsigned long offset)
643{
644 return virt_to_page(dummy_page[substream->stream]); /* the same page */
645}
646
1da7f0c5 647static const struct snd_pcm_ops dummy_pcm_ops = {
c631d03c
TI
648 .open = dummy_pcm_open,
649 .close = dummy_pcm_close,
c631d03c 650 .hw_params = dummy_pcm_hw_params,
c631d03c
TI
651 .prepare = dummy_pcm_prepare,
652 .trigger = dummy_pcm_trigger,
653 .pointer = dummy_pcm_pointer,
1da177e4
LT
654};
655
1da7f0c5 656static const struct snd_pcm_ops dummy_pcm_ops_no_buf = {
a68c4d11
TI
657 .open = dummy_pcm_open,
658 .close = dummy_pcm_close,
a68c4d11 659 .hw_params = dummy_pcm_hw_params,
a68c4d11
TI
660 .prepare = dummy_pcm_prepare,
661 .trigger = dummy_pcm_trigger,
662 .pointer = dummy_pcm_pointer,
d53611d2
TI
663 .copy_user = dummy_pcm_copy,
664 .copy_kernel = dummy_pcm_copy_kernel,
665 .fill_silence = dummy_pcm_silence,
a68c4d11
TI
666 .page = dummy_pcm_page,
667};
668
fbbb01a1
BP
669static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
670 int substreams)
1da177e4 671{
4a4d2cfd 672 struct snd_pcm *pcm;
1da7f0c5 673 const struct snd_pcm_ops *ops;
1da177e4
LT
674 int err;
675
1a11cb64
JK
676 err = snd_pcm_new(dummy->card, "Dummy PCM", device,
677 substreams, substreams, &pcm);
678 if (err < 0)
1da177e4 679 return err;
6e65c1cc 680 dummy->pcm = pcm;
a68c4d11
TI
681 if (fake_buffer)
682 ops = &dummy_pcm_ops_no_buf;
683 else
684 ops = &dummy_pcm_ops;
685 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
686 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
1da177e4
LT
687 pcm->private_data = dummy;
688 pcm->info_flags = 0;
689 strcpy(pcm->name, "Dummy PCM");
a68c4d11 690 if (!fake_buffer) {
ef1545b8 691 snd_pcm_set_managed_buffer_all(pcm,
a68c4d11 692 SNDRV_DMA_TYPE_CONTINUOUS,
8fd9da75 693 NULL,
a68c4d11
TI
694 0, 64*1024);
695 }
1da177e4
LT
696 return 0;
697}
698
9b151fec
TI
699/*
700 * mixer interface
701 */
702
1da177e4 703#define DUMMY_VOLUME(xname, xindex, addr) \
fb567a8e
TI
704{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
705 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
706 .name = xname, .index = xindex, \
1da177e4
LT
707 .info = snd_dummy_volume_info, \
708 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
fb567a8e
TI
709 .private_value = addr, \
710 .tlv = { .p = db_scale_dummy } }
1da177e4 711
4a4d2cfd
TI
712static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
713 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
714{
715 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
716 uinfo->count = 2;
717 uinfo->value.integer.min = -50;
718 uinfo->value.integer.max = 100;
719 return 0;
720}
721
4a4d2cfd
TI
722static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
723 struct snd_ctl_elem_value *ucontrol)
1da177e4 724{
4a4d2cfd 725 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
1da177e4
LT
726 int addr = kcontrol->private_value;
727
c8eb6ba1 728 spin_lock_irq(&dummy->mixer_lock);
1da177e4
LT
729 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
730 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
c8eb6ba1 731 spin_unlock_irq(&dummy->mixer_lock);
1da177e4
LT
732 return 0;
733}
734
4a4d2cfd
TI
735static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
736 struct snd_ctl_elem_value *ucontrol)
1da177e4 737{
4a4d2cfd 738 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
1da177e4
LT
739 int change, addr = kcontrol->private_value;
740 int left, right;
741
742 left = ucontrol->value.integer.value[0];
743 if (left < -50)
744 left = -50;
745 if (left > 100)
746 left = 100;
747 right = ucontrol->value.integer.value[1];
748 if (right < -50)
749 right = -50;
750 if (right > 100)
751 right = 100;
c8eb6ba1 752 spin_lock_irq(&dummy->mixer_lock);
1da177e4
LT
753 change = dummy->mixer_volume[addr][0] != left ||
754 dummy->mixer_volume[addr][1] != right;
755 dummy->mixer_volume[addr][0] = left;
756 dummy->mixer_volume[addr][1] = right;
c8eb6ba1 757 spin_unlock_irq(&dummy->mixer_lock);
1da177e4
LT
758 return change;
759}
760
0cb29ea0 761static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
fb567a8e 762
1da177e4
LT
763#define DUMMY_CAPSRC(xname, xindex, addr) \
764{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
765 .info = snd_dummy_capsrc_info, \
766 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
767 .private_value = addr }
768
a5ce8890 769#define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
1da177e4 770
4a4d2cfd
TI
771static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
772 struct snd_ctl_elem_value *ucontrol)
1da177e4 773{
4a4d2cfd 774 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
1da177e4
LT
775 int addr = kcontrol->private_value;
776
c8eb6ba1 777 spin_lock_irq(&dummy->mixer_lock);
1da177e4
LT
778 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
779 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
c8eb6ba1 780 spin_unlock_irq(&dummy->mixer_lock);
1da177e4
LT
781 return 0;
782}
783
4a4d2cfd 784static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 785{
4a4d2cfd 786 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
1da177e4
LT
787 int change, addr = kcontrol->private_value;
788 int left, right;
789
790 left = ucontrol->value.integer.value[0] & 1;
791 right = ucontrol->value.integer.value[1] & 1;
c8eb6ba1 792 spin_lock_irq(&dummy->mixer_lock);
1da177e4
LT
793 change = dummy->capture_source[addr][0] != left &&
794 dummy->capture_source[addr][1] != right;
795 dummy->capture_source[addr][0] = left;
796 dummy->capture_source[addr][1] = right;
c8eb6ba1 797 spin_unlock_irq(&dummy->mixer_lock);
1da177e4
LT
798 return change;
799}
800
16e43467
CL
801static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
802 struct snd_ctl_elem_info *info)
803{
a4a1b737 804 static const char *const names[] = { "None", "CD Player" };
16e43467
CL
805
806 return snd_ctl_enum_info(info, 1, 2, names);
807}
808
809static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
810 struct snd_ctl_elem_value *value)
811{
812 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
813
814 value->value.enumerated.item[0] = dummy->iobox;
815 return 0;
816}
817
818static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
819 struct snd_ctl_elem_value *value)
820{
821 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
822 int changed;
823
824 if (value->value.enumerated.item[0] > 1)
825 return -EINVAL;
826
827 changed = value->value.enumerated.item[0] != dummy->iobox;
828 if (changed) {
829 dummy->iobox = value->value.enumerated.item[0];
830
831 if (dummy->iobox) {
832 dummy->cd_volume_ctl->vd[0].access &=
833 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
834 dummy->cd_switch_ctl->vd[0].access &=
835 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
836 } else {
837 dummy->cd_volume_ctl->vd[0].access |=
838 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
839 dummy->cd_switch_ctl->vd[0].access |=
840 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
841 }
842
843 snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
844 &dummy->cd_volume_ctl->id);
845 snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
846 &dummy->cd_switch_ctl->id);
847 }
848
849 return changed;
850}
851
2eccd408 852static const struct snd_kcontrol_new snd_dummy_controls[] = {
1da177e4
LT
853DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
854DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
855DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
e05d6964 856DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
1da177e4 857DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
e05d6964 858DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
1da177e4 859DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
e05d6964 860DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
1da177e4 861DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
16e43467
CL
862DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
863{
864 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
865 .name = "External I/O Box",
866 .info = snd_dummy_iobox_info,
867 .get = snd_dummy_iobox_get,
868 .put = snd_dummy_iobox_put,
869},
1da177e4
LT
870};
871
fbbb01a1 872static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
1da177e4 873{
4a4d2cfd 874 struct snd_card *card = dummy->card;
16e43467 875 struct snd_kcontrol *kcontrol;
1da177e4
LT
876 unsigned int idx;
877 int err;
878
1da177e4
LT
879 spin_lock_init(&dummy->mixer_lock);
880 strcpy(card->mixername, "Dummy Mixer");
16e43467 881 dummy->iobox = 1;
1da177e4
LT
882
883 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
16e43467
CL
884 kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
885 err = snd_ctl_add(card, kcontrol);
1a11cb64 886 if (err < 0)
1da177e4 887 return err;
16e43467
CL
888 if (!strcmp(kcontrol->id.name, "CD Volume"))
889 dummy->cd_volume_ctl = kcontrol;
890 else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
891 dummy->cd_switch_ctl = kcontrol;
892
1da177e4
LT
893 }
894 return 0;
895}
896
129a4c9f 897#if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS)
9b151fec
TI
898/*
899 * proc interface
900 */
d5e1ca05
JK
901static void print_formats(struct snd_dummy *dummy,
902 struct snd_info_buffer *buffer)
9b151fec 903{
c5f72ef1 904 snd_pcm_format_t i;
9b151fec 905
c5f72ef1
TI
906 pcm_for_each_format(i) {
907 if (dummy->pcm_hw.formats & pcm_format_to_bits(i))
9b151fec
TI
908 snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
909 }
910}
911
d5e1ca05
JK
912static void print_rates(struct snd_dummy *dummy,
913 struct snd_info_buffer *buffer)
9b151fec 914{
d64e7f7c 915 static const int rates[] = {
9b151fec
TI
916 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
917 64000, 88200, 96000, 176400, 192000,
918 };
919 int i;
920
d5e1ca05 921 if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
9b151fec 922 snd_iprintf(buffer, " continuous");
d5e1ca05 923 if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
9b151fec
TI
924 snd_iprintf(buffer, " knot");
925 for (i = 0; i < ARRAY_SIZE(rates); i++)
d5e1ca05 926 if (dummy->pcm_hw.rates & (1 << i))
9b151fec
TI
927 snd_iprintf(buffer, " %d", rates[i]);
928}
929
d5e1ca05
JK
930#define get_dummy_int_ptr(dummy, ofs) \
931 (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
932#define get_dummy_ll_ptr(dummy, ofs) \
933 (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
9b151fec
TI
934
935struct dummy_hw_field {
936 const char *name;
937 const char *format;
938 unsigned int offset;
939 unsigned int size;
940};
941#define FIELD_ENTRY(item, fmt) { \
942 .name = #item, \
943 .format = fmt, \
944 .offset = offsetof(struct snd_pcm_hardware, item), \
945 .size = sizeof(dummy_pcm_hardware.item) }
946
d64e7f7c 947static const struct dummy_hw_field fields[] = {
9b151fec
TI
948 FIELD_ENTRY(formats, "%#llx"),
949 FIELD_ENTRY(rates, "%#x"),
950 FIELD_ENTRY(rate_min, "%d"),
951 FIELD_ENTRY(rate_max, "%d"),
952 FIELD_ENTRY(channels_min, "%d"),
953 FIELD_ENTRY(channels_max, "%d"),
954 FIELD_ENTRY(buffer_bytes_max, "%ld"),
955 FIELD_ENTRY(period_bytes_min, "%ld"),
956 FIELD_ENTRY(period_bytes_max, "%ld"),
957 FIELD_ENTRY(periods_min, "%d"),
958 FIELD_ENTRY(periods_max, "%d"),
959};
960
961static void dummy_proc_read(struct snd_info_entry *entry,
962 struct snd_info_buffer *buffer)
963{
d5e1ca05 964 struct snd_dummy *dummy = entry->private_data;
9b151fec
TI
965 int i;
966
967 for (i = 0; i < ARRAY_SIZE(fields); i++) {
968 snd_iprintf(buffer, "%s ", fields[i].name);
969 if (fields[i].size == sizeof(int))
970 snd_iprintf(buffer, fields[i].format,
d5e1ca05 971 *get_dummy_int_ptr(dummy, fields[i].offset));
9b151fec
TI
972 else
973 snd_iprintf(buffer, fields[i].format,
d5e1ca05 974 *get_dummy_ll_ptr(dummy, fields[i].offset));
9b151fec 975 if (!strcmp(fields[i].name, "formats"))
d5e1ca05 976 print_formats(dummy, buffer);
9b151fec 977 else if (!strcmp(fields[i].name, "rates"))
d5e1ca05 978 print_rates(dummy, buffer);
9b151fec
TI
979 snd_iprintf(buffer, "\n");
980 }
981}
982
983static void dummy_proc_write(struct snd_info_entry *entry,
984 struct snd_info_buffer *buffer)
985{
d5e1ca05 986 struct snd_dummy *dummy = entry->private_data;
9b151fec
TI
987 char line[64];
988
989 while (!snd_info_get_line(buffer, line, sizeof(line))) {
990 char item[20];
991 const char *ptr;
992 unsigned long long val;
993 int i;
994
995 ptr = snd_info_get_str(item, line, sizeof(item));
996 for (i = 0; i < ARRAY_SIZE(fields); i++) {
997 if (!strcmp(item, fields[i].name))
998 break;
999 }
1000 if (i >= ARRAY_SIZE(fields))
1001 continue;
1002 snd_info_get_str(item, ptr, sizeof(item));
b785a492 1003 if (kstrtoull(item, 0, &val))
9b151fec
TI
1004 continue;
1005 if (fields[i].size == sizeof(int))
d5e1ca05 1006 *get_dummy_int_ptr(dummy, fields[i].offset) = val;
9b151fec 1007 else
d5e1ca05 1008 *get_dummy_ll_ptr(dummy, fields[i].offset) = val;
9b151fec
TI
1009 }
1010}
1011
fbbb01a1 1012static void dummy_proc_init(struct snd_dummy *chip)
9b151fec 1013{
815d808c
TI
1014 snd_card_rw_proc_new(chip->card, "dummy_pcm", chip,
1015 dummy_proc_read, dummy_proc_write);
9b151fec
TI
1016}
1017#else
1018#define dummy_proc_init(x)
129a4c9f 1019#endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */
9b151fec 1020
fbbb01a1 1021static int snd_dummy_probe(struct platform_device *devptr)
1da177e4 1022{
4a4d2cfd
TI
1023 struct snd_card *card;
1024 struct snd_dummy *dummy;
d64e7f7c 1025 const struct dummy_model *m = NULL, **mdl;
1da177e4 1026 int idx, err;
6e65c1cc 1027 int dev = devptr->id;
1da177e4 1028
5872f3f6
TI
1029 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1030 sizeof(struct snd_dummy), &card);
bd7dd77c
TI
1031 if (err < 0)
1032 return err;
4a4d2cfd 1033 dummy = card->private_data;
1da177e4 1034 dummy->card = card;
d5e1ca05
JK
1035 for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
1036 if (strcmp(model[dev], (*mdl)->name) == 0) {
1037 printk(KERN_INFO
1038 "snd-dummy: Using model '%s' for card %i\n",
1039 (*mdl)->name, card->number);
1040 m = dummy->model = *mdl;
1041 break;
1042 }
1043 }
1da177e4
LT
1044 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
1045 if (pcm_substreams[dev] < 1)
1046 pcm_substreams[dev] = 1;
1047 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1048 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1a11cb64
JK
1049 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
1050 if (err < 0)
1da177e4
LT
1051 goto __nodev;
1052 }
d5e1ca05
JK
1053
1054 dummy->pcm_hw = dummy_pcm_hardware;
1055 if (m) {
1056 if (m->formats)
1057 dummy->pcm_hw.formats = m->formats;
1058 if (m->buffer_bytes_max)
1059 dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
1060 if (m->period_bytes_min)
1061 dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
1062 if (m->period_bytes_max)
1063 dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
1064 if (m->periods_min)
1065 dummy->pcm_hw.periods_min = m->periods_min;
1066 if (m->periods_max)
1067 dummy->pcm_hw.periods_max = m->periods_max;
1068 if (m->rates)
1069 dummy->pcm_hw.rates = m->rates;
1070 if (m->rate_min)
1071 dummy->pcm_hw.rate_min = m->rate_min;
1072 if (m->rate_max)
1073 dummy->pcm_hw.rate_max = m->rate_max;
1074 if (m->channels_min)
1075 dummy->pcm_hw.channels_min = m->channels_min;
1076 if (m->channels_max)
1077 dummy->pcm_hw.channels_max = m->channels_max;
1078 }
1079
1a11cb64
JK
1080 err = snd_card_dummy_new_mixer(dummy);
1081 if (err < 0)
1da177e4
LT
1082 goto __nodev;
1083 strcpy(card->driver, "Dummy");
1084 strcpy(card->shortname, "Dummy");
1085 sprintf(card->longname, "Dummy %i", dev + 1);
16dab54b 1086
9b151fec
TI
1087 dummy_proc_init(dummy);
1088
1a11cb64
JK
1089 err = snd_card_register(card);
1090 if (err == 0) {
6e65c1cc 1091 platform_set_drvdata(devptr, card);
1da177e4
LT
1092 return 0;
1093 }
1094 __nodev:
1095 snd_card_free(card);
1096 return err;
1097}
1098
fbbb01a1 1099static int snd_dummy_remove(struct platform_device *devptr)
6e65c1cc
TI
1100{
1101 snd_card_free(platform_get_drvdata(devptr));
6e65c1cc
TI
1102 return 0;
1103}
1104
d34e4e00 1105#ifdef CONFIG_PM_SLEEP
284e7ca7 1106static int snd_dummy_suspend(struct device *pdev)
1da177e4 1107{
284e7ca7 1108 struct snd_card *card = dev_get_drvdata(pdev);
1da177e4 1109
6e65c1cc 1110 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
6e65c1cc
TI
1111 return 0;
1112}
1113
284e7ca7 1114static int snd_dummy_resume(struct device *pdev)
6e65c1cc 1115{
284e7ca7 1116 struct snd_card *card = dev_get_drvdata(pdev);
6e65c1cc
TI
1117
1118 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1119 return 0;
1120}
284e7ca7
TI
1121
1122static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume);
1123#define SND_DUMMY_PM_OPS &snd_dummy_pm
1124#else
1125#define SND_DUMMY_PM_OPS NULL
6e65c1cc
TI
1126#endif
1127
1128#define SND_DUMMY_DRIVER "snd_dummy"
1129
1130static struct platform_driver snd_dummy_driver = {
1131 .probe = snd_dummy_probe,
fbbb01a1 1132 .remove = snd_dummy_remove,
6e65c1cc 1133 .driver = {
8bf01d8a 1134 .name = SND_DUMMY_DRIVER,
284e7ca7 1135 .pm = SND_DUMMY_PM_OPS,
6e65c1cc
TI
1136 },
1137};
1138
c12aad6e 1139static void snd_dummy_unregister_all(void)
f7a9275d
CL
1140{
1141 int i;
1142
1143 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1144 platform_device_unregister(devices[i]);
1145 platform_driver_unregister(&snd_dummy_driver);
a68c4d11 1146 free_fake_buffer();
f7a9275d
CL
1147}
1148
6e65c1cc
TI
1149static int __init alsa_card_dummy_init(void)
1150{
1151 int i, cards, err;
1152
1a11cb64
JK
1153 err = platform_driver_register(&snd_dummy_driver);
1154 if (err < 0)
6e65c1cc
TI
1155 return err;
1156
a68c4d11
TI
1157 err = alloc_fake_buffer();
1158 if (err < 0) {
1159 platform_driver_unregister(&snd_dummy_driver);
1160 return err;
1161 }
1162
6e65c1cc 1163 cards = 0;
8278ca8f 1164 for (i = 0; i < SNDRV_CARDS; i++) {
6e65c1cc 1165 struct platform_device *device;
8278ca8f
TI
1166 if (! enable[i])
1167 continue;
6e65c1cc
TI
1168 device = platform_device_register_simple(SND_DUMMY_DRIVER,
1169 i, NULL, 0);
a182ee98
RH
1170 if (IS_ERR(device))
1171 continue;
7152447d
RH
1172 if (!platform_get_drvdata(device)) {
1173 platform_device_unregister(device);
1174 continue;
1175 }
f7a9275d 1176 devices[i] = device;
1da177e4
LT
1177 cards++;
1178 }
1179 if (!cards) {
1180#ifdef MODULE
1181 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
1182#endif
a182ee98
RH
1183 snd_dummy_unregister_all();
1184 return -ENODEV;
1da177e4
LT
1185 }
1186 return 0;
1187}
1188
1189static void __exit alsa_card_dummy_exit(void)
1190{
f7a9275d 1191 snd_dummy_unregister_all();
1da177e4
LT
1192}
1193
1194module_init(alsa_card_dummy_init)
1195module_exit(alsa_card_dummy_exit)