dummy.c 20.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  Dummy soundcard
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <sound/driver.h>
#include <linux/init.h>
23 24
#include <linux/err.h>
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
25 26 27 28 29 30 31
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/wait.h>
#include <linux/moduleparam.h>
#include <sound/core.h>
#include <sound/control.h>
32
#include <sound/tlv.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <sound/pcm.h>
#include <sound/rawmidi.h>
#include <sound/initval.h>

MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");

#define MAX_PCM_DEVICES		4
#define MAX_PCM_SUBSTREAMS	16
#define MAX_MIDI_DEVICES	2

#if 0 /* emu10k1 emulation */
#define MAX_BUFFER_SIZE		(128 * 1024)
48
static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
	int err;
	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
		return err;
	if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0)
		return err;
	return 0;
}
#define add_playback_constraints emu10k1_playback_constraints
#endif

#if 0 /* RME9652 emulation */
#define MAX_BUFFER_SIZE		(26 * 64 * 1024)
#define USE_FORMATS		SNDRV_PCM_FMTBIT_S32_LE
#define USE_CHANNELS_MIN	26
#define USE_CHANNELS_MAX	26
#define USE_PERIODS_MIN		2
#define USE_PERIODS_MAX		2
#endif

#if 0 /* ICE1712 emulation */
#define MAX_BUFFER_SIZE		(256 * 1024)
#define USE_FORMATS		SNDRV_PCM_FMTBIT_S32_LE
#define USE_CHANNELS_MIN	10
#define USE_CHANNELS_MAX	10
#define USE_PERIODS_MIN		1
#define USE_PERIODS_MAX		1024
#endif

#if 0 /* UDA1341 emulation */
#define MAX_BUFFER_SIZE		(16380)
#define USE_FORMATS		SNDRV_PCM_FMTBIT_S16_LE
#define USE_CHANNELS_MIN	2
#define USE_CHANNELS_MAX	2
#define USE_PERIODS_MIN		2
#define USE_PERIODS_MAX		255
#endif

#if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */
#define USE_FORMATS		SNDRV_PCM_FMTBIT_S16_LE
#define USE_CHANNELS_MIN	2
#define USE_CHANNELS_MAX	2
#define USE_RATE		SNDRV_PCM_RATE_48000
#define USE_RATE_MIN		48000
#define USE_RATE_MAX		48000
#endif

96 97 98 99 100 101 102 103 104 105 106 107 108
#if 0 /* CA0106 */
#define USE_FORMATS		SNDRV_PCM_FMTBIT_S16_LE
#define USE_CHANNELS_MIN	2
#define USE_CHANNELS_MAX	2
#define USE_RATE		(SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000) 
#define USE_RATE_MIN		48000 
#define USE_RATE_MAX		192000
#define MAX_BUFFER_SIZE		((65536-64)*8)
#define MAX_PERIOD_SIZE		(65536-64)
#define USE_PERIODS_MIN		2
#define USE_PERIODS_MAX		8
#endif

L
Linus Torvalds 已提交
109 110 111 112 113

/* defaults */
#ifndef MAX_BUFFER_SIZE
#define MAX_BUFFER_SIZE		(64*1024)
#endif
114 115 116
#ifndef MAX_PERIOD_SIZE
#define MAX_PERIOD_SIZE		MAX_BUFFER_SIZE
#endif
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#ifndef USE_FORMATS
#define USE_FORMATS 		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
#endif
#ifndef USE_RATE
#define USE_RATE		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
#define USE_RATE_MIN		5500
#define USE_RATE_MAX		48000
#endif
#ifndef USE_CHANNELS_MIN
#define USE_CHANNELS_MIN 	1
#endif
#ifndef USE_CHANNELS_MAX
#define USE_CHANNELS_MAX 	2
#endif
#ifndef USE_PERIODS_MIN
#define USE_PERIODS_MIN 	1
#endif
#ifndef USE_PERIODS_MAX
#define USE_PERIODS_MAX 	1024
#endif
#ifndef add_playback_constraints
#define add_playback_constraints(x) 0
#endif
#ifndef add_capture_constraints
#define add_capture_constraints(x) 0
#endif

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
//static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
module_param_array(pcm_devs, int, NULL, 0444);
MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
module_param_array(pcm_substreams, int, NULL, 0444);
MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
//module_param_array(midi_devs, int, NULL, 0444);
//MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");

164 165
static struct platform_device *devices[SNDRV_CARDS];

L
Linus Torvalds 已提交
166 167 168 169 170 171 172
#define MIXER_ADDR_MASTER	0
#define MIXER_ADDR_LINE		1
#define MIXER_ADDR_MIC		2
#define MIXER_ADDR_SYNTH	3
#define MIXER_ADDR_CD		4
#define MIXER_ADDR_LAST		4

173 174
struct snd_dummy {
	struct snd_card *card;
175
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
176 177 178
	spinlock_t mixer_lock;
	int mixer_volume[MIXER_ADDR_LAST+1][2];
	int capture_source[MIXER_ADDR_LAST+1][2];
179
};
L
Linus Torvalds 已提交
180

181 182
struct snd_dummy_pcm {
	struct snd_dummy *dummy;
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190
	spinlock_t lock;
	struct timer_list timer;
	unsigned int pcm_size;
	unsigned int pcm_count;
	unsigned int pcm_bps;		/* bytes per second */
	unsigned int pcm_jiffie;	/* bytes per one jiffie */
	unsigned int pcm_irq_pos;	/* IRQ position */
	unsigned int pcm_buf_pos;	/* position in buffer */
191 192
	struct snd_pcm_substream *substream;
};
L
Linus Torvalds 已提交
193 194


195
static inline void snd_card_dummy_pcm_timer_start(struct snd_dummy_pcm *dpcm)
L
Linus Torvalds 已提交
196 197 198 199 200
{
	dpcm->timer.expires = 1 + jiffies;
	add_timer(&dpcm->timer);
}

201
static inline void snd_card_dummy_pcm_timer_stop(struct snd_dummy_pcm *dpcm)
L
Linus Torvalds 已提交
202 203 204 205
{
	del_timer(&dpcm->timer);
}

206
static int snd_card_dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
L
Linus Torvalds 已提交
207
{
208 209
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_dummy_pcm *dpcm = runtime->private_data;
T
Takashi Iwai 已提交
210
	int err = 0;
L
Linus Torvalds 已提交
211

T
Takashi Iwai 已提交
212
	spin_lock(&dpcm->lock);
213 214 215
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
T
Takashi Iwai 已提交
216
		snd_card_dummy_pcm_timer_start(dpcm);
217 218 219
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
T
Takashi Iwai 已提交
220
		snd_card_dummy_pcm_timer_stop(dpcm);
221 222
		break;
	default:
T
Takashi Iwai 已提交
223
		err = -EINVAL;
224
		break;
L
Linus Torvalds 已提交
225
	}
T
Takashi Iwai 已提交
226
	spin_unlock(&dpcm->lock);
227
	return 0;
L
Linus Torvalds 已提交
228 229
}

230
static int snd_card_dummy_pcm_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
231
{
232 233
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_dummy_pcm *dpcm = runtime->private_data;
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	unsigned int bps;

	bps = runtime->rate * runtime->channels;
	bps *= snd_pcm_format_width(runtime->format);
	bps /= 8;
	if (bps <= 0)
		return -EINVAL;
	dpcm->pcm_bps = bps;
	dpcm->pcm_jiffie = bps / HZ;
	dpcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
	dpcm->pcm_count = snd_pcm_lib_period_bytes(substream);
	dpcm->pcm_irq_pos = 0;
	dpcm->pcm_buf_pos = 0;
	return 0;
}

static void snd_card_dummy_pcm_timer_function(unsigned long data)
{
252
	struct snd_dummy_pcm *dpcm = (struct snd_dummy_pcm *)data;
253
	unsigned long flags;
L
Linus Torvalds 已提交
254
	
255
	spin_lock_irqsave(&dpcm->lock, flags);
L
Linus Torvalds 已提交
256 257 258 259 260 261 262
	dpcm->timer.expires = 1 + jiffies;
	add_timer(&dpcm->timer);
	dpcm->pcm_irq_pos += dpcm->pcm_jiffie;
	dpcm->pcm_buf_pos += dpcm->pcm_jiffie;
	dpcm->pcm_buf_pos %= dpcm->pcm_size;
	if (dpcm->pcm_irq_pos >= dpcm->pcm_count) {
		dpcm->pcm_irq_pos %= dpcm->pcm_count;
263
		spin_unlock_irqrestore(&dpcm->lock, flags);
L
Linus Torvalds 已提交
264
		snd_pcm_period_elapsed(dpcm->substream);
265 266
	} else
		spin_unlock_irqrestore(&dpcm->lock, flags);
L
Linus Torvalds 已提交
267 268
}

269
static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
270
{
271 272
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_dummy_pcm *dpcm = runtime->private_data;
L
Linus Torvalds 已提交
273 274 275 276

	return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
}

277
static struct snd_pcm_hardware snd_card_dummy_playback =
L
Linus Torvalds 已提交
278 279
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
280
				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
L
Linus Torvalds 已提交
281 282 283 284 285 286 287 288
	.formats =		USE_FORMATS,
	.rates =		USE_RATE,
	.rate_min =		USE_RATE_MIN,
	.rate_max =		USE_RATE_MAX,
	.channels_min =		USE_CHANNELS_MIN,
	.channels_max =		USE_CHANNELS_MAX,
	.buffer_bytes_max =	MAX_BUFFER_SIZE,
	.period_bytes_min =	64,
289
	.period_bytes_max =	MAX_PERIOD_SIZE,
L
Linus Torvalds 已提交
290 291 292 293 294
	.periods_min =		USE_PERIODS_MIN,
	.periods_max =		USE_PERIODS_MAX,
	.fifo_size =		0,
};

295
static struct snd_pcm_hardware snd_card_dummy_capture =
L
Linus Torvalds 已提交
296 297
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
298
				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306
	.formats =		USE_FORMATS,
	.rates =		USE_RATE,
	.rate_min =		USE_RATE_MIN,
	.rate_max =		USE_RATE_MAX,
	.channels_min =		USE_CHANNELS_MIN,
	.channels_max =		USE_CHANNELS_MAX,
	.buffer_bytes_max =	MAX_BUFFER_SIZE,
	.period_bytes_min =	64,
307
	.period_bytes_max =	MAX_PERIOD_SIZE,
L
Linus Torvalds 已提交
308 309 310 311 312
	.periods_min =		USE_PERIODS_MIN,
	.periods_max =		USE_PERIODS_MAX,
	.fifo_size =		0,
};

313
static void snd_card_dummy_runtime_free(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
314
{
T
Takashi Iwai 已提交
315
	kfree(runtime->private_data);
L
Linus Torvalds 已提交
316 317
}

318 319
static int snd_card_dummy_hw_params(struct snd_pcm_substream *substream,
				    struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
320 321 322 323
{
	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
}

324
static int snd_card_dummy_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
325 326 327 328
{
	return snd_pcm_lib_free_pages(substream);
}

329
static struct snd_dummy_pcm *new_pcm_stream(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
330
{
331
	struct snd_dummy_pcm *dpcm;
L
Linus Torvalds 已提交
332

333
	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
T
Takashi Iwai 已提交
334 335
	if (! dpcm)
		return dpcm;
L
Linus Torvalds 已提交
336 337 338 339 340
	init_timer(&dpcm->timer);
	dpcm->timer.data = (unsigned long) dpcm;
	dpcm->timer.function = snd_card_dummy_pcm_timer_function;
	spin_lock_init(&dpcm->lock);
	dpcm->substream = substream;
T
Takashi Iwai 已提交
341 342 343
	return dpcm;
}

344
static int snd_card_dummy_playback_open(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
345
{
346 347
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_dummy_pcm *dpcm;
T
Takashi Iwai 已提交
348 349 350 351
	int err;

	if ((dpcm = new_pcm_stream(substream)) == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	runtime->private_data = dpcm;
	runtime->private_free = snd_card_dummy_runtime_free;
	runtime->hw = snd_card_dummy_playback;
	if (substream->pcm->device & 1) {
		runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
		runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
	}
	if (substream->pcm->device & 2)
		runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
	if ((err = add_playback_constraints(runtime)) < 0) {
		kfree(dpcm);
		return err;
	}

	return 0;
}

369
static int snd_card_dummy_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
370
{
371 372
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_dummy_pcm *dpcm;
L
Linus Torvalds 已提交
373 374
	int err;

T
Takashi Iwai 已提交
375
	if ((dpcm = new_pcm_stream(substream)) == NULL)
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
		return -ENOMEM;
	runtime->private_data = dpcm;
	runtime->private_free = snd_card_dummy_runtime_free;
	runtime->hw = snd_card_dummy_capture;
	if (substream->pcm->device == 1) {
		runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
		runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
	}
	if (substream->pcm->device & 2)
		runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
	if ((err = add_capture_constraints(runtime)) < 0) {
		kfree(dpcm);
		return err;
	}

	return 0;
}

394
static int snd_card_dummy_playback_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
395 396 397 398
{
	return 0;
}

399
static int snd_card_dummy_capture_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
400 401 402 403
{
	return 0;
}

404
static struct snd_pcm_ops snd_card_dummy_playback_ops = {
L
Linus Torvalds 已提交
405 406 407 408 409
	.open =			snd_card_dummy_playback_open,
	.close =		snd_card_dummy_playback_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_card_dummy_hw_params,
	.hw_free =		snd_card_dummy_hw_free,
T
Takashi Iwai 已提交
410 411 412
	.prepare =		snd_card_dummy_pcm_prepare,
	.trigger =		snd_card_dummy_pcm_trigger,
	.pointer =		snd_card_dummy_pcm_pointer,
L
Linus Torvalds 已提交
413 414
};

415
static struct snd_pcm_ops snd_card_dummy_capture_ops = {
L
Linus Torvalds 已提交
416 417 418 419 420
	.open =			snd_card_dummy_capture_open,
	.close =		snd_card_dummy_capture_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_card_dummy_hw_params,
	.hw_free =		snd_card_dummy_hw_free,
T
Takashi Iwai 已提交
421 422 423
	.prepare =		snd_card_dummy_pcm_prepare,
	.trigger =		snd_card_dummy_pcm_trigger,
	.pointer =		snd_card_dummy_pcm_pointer,
L
Linus Torvalds 已提交
424 425
};

426 427
static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
					int substreams)
L
Linus Torvalds 已提交
428
{
429
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
430 431
	int err;

432 433
	if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device,
			       substreams, substreams, &pcm)) < 0)
L
Linus Torvalds 已提交
434
		return err;
435
	dummy->pcm = pcm;
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446 447
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
	pcm->private_data = dummy;
	pcm->info_flags = 0;
	strcpy(pcm->name, "Dummy PCM");
	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
					      snd_dma_continuous_data(GFP_KERNEL),
					      0, 64*1024);
	return 0;
}

#define DUMMY_VOLUME(xname, xindex, addr) \
448 449 450
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  .name = xname, .index = xindex, \
L
Linus Torvalds 已提交
451 452
  .info = snd_dummy_volume_info, \
  .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
453 454
  .private_value = addr, \
  .tlv = { .p = db_scale_dummy } }
L
Linus Torvalds 已提交
455

456 457
static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = -50;
	uinfo->value.integer.max = 100;
	return 0;
}
 
466 467
static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
468
{
469
	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
470 471
	int addr = kcontrol->private_value;

T
Takashi Iwai 已提交
472
	spin_lock_irq(&dummy->mixer_lock);
L
Linus Torvalds 已提交
473 474
	ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
	ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
T
Takashi Iwai 已提交
475
	spin_unlock_irq(&dummy->mixer_lock);
L
Linus Torvalds 已提交
476 477 478
	return 0;
}

479 480
static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
481
{
482
	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495
	int change, addr = kcontrol->private_value;
	int left, right;

	left = ucontrol->value.integer.value[0];
	if (left < -50)
		left = -50;
	if (left > 100)
		left = 100;
	right = ucontrol->value.integer.value[1];
	if (right < -50)
		right = -50;
	if (right > 100)
		right = 100;
T
Takashi Iwai 已提交
496
	spin_lock_irq(&dummy->mixer_lock);
L
Linus Torvalds 已提交
497 498 499 500
	change = dummy->mixer_volume[addr][0] != left ||
	         dummy->mixer_volume[addr][1] != right;
	dummy->mixer_volume[addr][0] = left;
	dummy->mixer_volume[addr][1] = right;
T
Takashi Iwai 已提交
501
	spin_unlock_irq(&dummy->mixer_lock);
L
Linus Torvalds 已提交
502 503 504
	return change;
}

505
static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
506

L
Linus Torvalds 已提交
507 508 509 510 511 512
#define DUMMY_CAPSRC(xname, xindex, addr) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  .info = snd_dummy_capsrc_info, \
  .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
  .private_value = addr }

513 514
static int snd_dummy_capsrc_info(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;
	return 0;
}
 
523 524
static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
525
{
526
	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
527 528
	int addr = kcontrol->private_value;

T
Takashi Iwai 已提交
529
	spin_lock_irq(&dummy->mixer_lock);
L
Linus Torvalds 已提交
530 531
	ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
	ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
T
Takashi Iwai 已提交
532
	spin_unlock_irq(&dummy->mixer_lock);
L
Linus Torvalds 已提交
533 534 535
	return 0;
}

536
static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
537
{
538
	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
539 540 541 542 543
	int change, addr = kcontrol->private_value;
	int left, right;

	left = ucontrol->value.integer.value[0] & 1;
	right = ucontrol->value.integer.value[1] & 1;
T
Takashi Iwai 已提交
544
	spin_lock_irq(&dummy->mixer_lock);
L
Linus Torvalds 已提交
545 546 547 548
	change = dummy->capture_source[addr][0] != left &&
	         dummy->capture_source[addr][1] != right;
	dummy->capture_source[addr][0] = left;
	dummy->capture_source[addr][1] = right;
T
Takashi Iwai 已提交
549
	spin_unlock_irq(&dummy->mixer_lock);
L
Linus Torvalds 已提交
550 551 552
	return change;
}

553
static struct snd_kcontrol_new snd_dummy_controls[] = {
L
Linus Torvalds 已提交
554 555 556
DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
557
DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
L
Linus Torvalds 已提交
558
DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
559
DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
L
Linus Torvalds 已提交
560
DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
561
DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
L
Linus Torvalds 已提交
562
DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
563
DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
L
Linus Torvalds 已提交
564 565
};

566
static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
L
Linus Torvalds 已提交
567
{
568
	struct snd_card *card = dummy->card;
L
Linus Torvalds 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582
	unsigned int idx;
	int err;

	snd_assert(dummy != NULL, return -EINVAL);
	spin_lock_init(&dummy->mixer_lock);
	strcpy(card->mixername, "Dummy Mixer");

	for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0)
			return err;
	}
	return 0;
}

583
static int __devinit snd_dummy_probe(struct platform_device *devptr)
L
Linus Torvalds 已提交
584
{
585 586
	struct snd_card *card;
	struct snd_dummy *dummy;
L
Linus Torvalds 已提交
587
	int idx, err;
588
	int dev = devptr->id;
L
Linus Torvalds 已提交
589 590

	card = snd_card_new(index[dev], id[dev], THIS_MODULE,
591
			    sizeof(struct snd_dummy));
L
Linus Torvalds 已提交
592 593
	if (card == NULL)
		return -ENOMEM;
594
	dummy = card->private_data;
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608
	dummy->card = card;
	for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
		if (pcm_substreams[dev] < 1)
			pcm_substreams[dev] = 1;
		if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
			pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
		if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0)
			goto __nodev;
	}
	if ((err = snd_card_dummy_new_mixer(dummy)) < 0)
		goto __nodev;
	strcpy(card->driver, "Dummy");
	strcpy(card->shortname, "Dummy");
	sprintf(card->longname, "Dummy %i", dev + 1);
609

610
	snd_card_set_dev(card, &devptr->dev);
611

L
Linus Torvalds 已提交
612
	if ((err = snd_card_register(card)) == 0) {
613
		platform_set_drvdata(devptr, card);
L
Linus Torvalds 已提交
614 615 616 617 618 619 620
		return 0;
	}
      __nodev:
	snd_card_free(card);
	return err;
}

621
static int __devexit snd_dummy_remove(struct platform_device *devptr)
622 623 624 625 626 627 628 629
{
	snd_card_free(platform_get_drvdata(devptr));
	platform_set_drvdata(devptr, NULL);
	return 0;
}

#ifdef CONFIG_PM
static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
L
Linus Torvalds 已提交
630
{
631 632
	struct snd_card *card = platform_get_drvdata(pdev);
	struct snd_dummy *dummy = card->private_data;
L
Linus Torvalds 已提交
633

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
	snd_pcm_suspend_all(dummy->pcm);
	return 0;
}
	
static int snd_dummy_resume(struct platform_device *pdev)
{
	struct snd_card *card = platform_get_drvdata(pdev);

	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
	return 0;
}
#endif

#define SND_DUMMY_DRIVER	"snd_dummy"

static struct platform_driver snd_dummy_driver = {
	.probe		= snd_dummy_probe,
652
	.remove		= __devexit_p(snd_dummy_remove),
653 654 655
#ifdef CONFIG_PM
	.suspend	= snd_dummy_suspend,
	.resume		= snd_dummy_resume,
L
Linus Torvalds 已提交
656
#endif
657 658 659 660 661
	.driver		= {
		.name	= SND_DUMMY_DRIVER
	},
};

662 663 664 665 666 667 668 669 670
static void __init_or_module snd_dummy_unregister_all(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(devices); ++i)
		platform_device_unregister(devices[i]);
	platform_driver_unregister(&snd_dummy_driver);
}

671 672 673 674 675 676 677 678
static int __init alsa_card_dummy_init(void)
{
	int i, cards, err;

	if ((err = platform_driver_register(&snd_dummy_driver)) < 0)
		return err;

	cards = 0;
679
	for (i = 0; i < SNDRV_CARDS; i++) {
680
		struct platform_device *device;
681 682
		if (! enable[i])
			continue;
683 684
		device = platform_device_register_simple(SND_DUMMY_DRIVER,
							 i, NULL, 0);
685 686
		if (IS_ERR(device))
			continue;
687 688 689 690
		if (!platform_get_drvdata(device)) {
			platform_device_unregister(device);
			continue;
		}
691
		devices[i] = device;
L
Linus Torvalds 已提交
692 693 694 695 696 697
		cards++;
	}
	if (!cards) {
#ifdef MODULE
		printk(KERN_ERR "Dummy soundcard not found or device busy\n");
#endif
698 699
		snd_dummy_unregister_all();
		return -ENODEV;
L
Linus Torvalds 已提交
700 701 702 703 704 705
	}
	return 0;
}

static void __exit alsa_card_dummy_exit(void)
{
706
	snd_dummy_unregister_all();
L
Linus Torvalds 已提交
707 708 709 710
}

module_init(alsa_card_dummy_init)
module_exit(alsa_card_dummy_exit)