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