Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Dummy soundcard | |
c1017a4c | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | * | |
19 | */ | |
20 | ||
1da177e4 | 21 | #include <linux/init.h> |
6e65c1cc TI |
22 | #include <linux/err.h> |
23 | #include <linux/platform_device.h> | |
1da177e4 LT |
24 | #include <linux/jiffies.h> |
25 | #include <linux/slab.h> | |
26 | #include <linux/time.h> | |
27 | #include <linux/wait.h> | |
28 | #include <linux/moduleparam.h> | |
29 | #include <sound/core.h> | |
30 | #include <sound/control.h> | |
fb567a8e | 31 | #include <sound/tlv.h> |
1da177e4 LT |
32 | #include <sound/pcm.h> |
33 | #include <sound/rawmidi.h> | |
34 | #include <sound/initval.h> | |
35 | ||
c1017a4c | 36 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
1da177e4 LT |
37 | MODULE_DESCRIPTION("Dummy soundcard (/dev/null)"); |
38 | MODULE_LICENSE("GPL"); | |
39 | MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}"); | |
40 | ||
41 | #define MAX_PCM_DEVICES 4 | |
42 | #define MAX_PCM_SUBSTREAMS 16 | |
43 | #define MAX_MIDI_DEVICES 2 | |
44 | ||
45 | #if 0 /* emu10k1 emulation */ | |
46 | #define MAX_BUFFER_SIZE (128 * 1024) | |
4a4d2cfd | 47 | static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime) |
1da177e4 LT |
48 | { |
49 | int err; | |
50 | if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) | |
51 | return err; | |
52 | if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) | |
53 | return err; | |
54 | return 0; | |
55 | } | |
56 | #define add_playback_constraints emu10k1_playback_constraints | |
57 | #endif | |
58 | ||
59 | #if 0 /* RME9652 emulation */ | |
60 | #define MAX_BUFFER_SIZE (26 * 64 * 1024) | |
61 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE | |
62 | #define USE_CHANNELS_MIN 26 | |
63 | #define USE_CHANNELS_MAX 26 | |
64 | #define USE_PERIODS_MIN 2 | |
65 | #define USE_PERIODS_MAX 2 | |
66 | #endif | |
67 | ||
68 | #if 0 /* ICE1712 emulation */ | |
69 | #define MAX_BUFFER_SIZE (256 * 1024) | |
70 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE | |
71 | #define USE_CHANNELS_MIN 10 | |
72 | #define USE_CHANNELS_MAX 10 | |
73 | #define USE_PERIODS_MIN 1 | |
74 | #define USE_PERIODS_MAX 1024 | |
75 | #endif | |
76 | ||
77 | #if 0 /* UDA1341 emulation */ | |
78 | #define MAX_BUFFER_SIZE (16380) | |
79 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE | |
80 | #define USE_CHANNELS_MIN 2 | |
81 | #define USE_CHANNELS_MAX 2 | |
82 | #define USE_PERIODS_MIN 2 | |
83 | #define USE_PERIODS_MAX 255 | |
84 | #endif | |
85 | ||
86 | #if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */ | |
87 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE | |
88 | #define USE_CHANNELS_MIN 2 | |
89 | #define USE_CHANNELS_MAX 2 | |
90 | #define USE_RATE SNDRV_PCM_RATE_48000 | |
91 | #define USE_RATE_MIN 48000 | |
92 | #define USE_RATE_MAX 48000 | |
93 | #endif | |
94 | ||
2ad5dd8d JK |
95 | #if 0 /* CA0106 */ |
96 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE | |
97 | #define USE_CHANNELS_MIN 2 | |
98 | #define USE_CHANNELS_MAX 2 | |
99 | #define USE_RATE (SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000) | |
100 | #define USE_RATE_MIN 48000 | |
101 | #define USE_RATE_MAX 192000 | |
102 | #define MAX_BUFFER_SIZE ((65536-64)*8) | |
103 | #define MAX_PERIOD_SIZE (65536-64) | |
104 | #define USE_PERIODS_MIN 2 | |
105 | #define USE_PERIODS_MAX 8 | |
106 | #endif | |
107 | ||
1da177e4 LT |
108 | |
109 | /* defaults */ | |
110 | #ifndef MAX_BUFFER_SIZE | |
111 | #define MAX_BUFFER_SIZE (64*1024) | |
112 | #endif | |
2ad5dd8d JK |
113 | #ifndef MAX_PERIOD_SIZE |
114 | #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE | |
115 | #endif | |
1da177e4 LT |
116 | #ifndef USE_FORMATS |
117 | #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE) | |
118 | #endif | |
119 | #ifndef USE_RATE | |
120 | #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000 | |
121 | #define USE_RATE_MIN 5500 | |
122 | #define USE_RATE_MAX 48000 | |
123 | #endif | |
124 | #ifndef USE_CHANNELS_MIN | |
125 | #define USE_CHANNELS_MIN 1 | |
126 | #endif | |
127 | #ifndef USE_CHANNELS_MAX | |
128 | #define USE_CHANNELS_MAX 2 | |
129 | #endif | |
130 | #ifndef USE_PERIODS_MIN | |
131 | #define USE_PERIODS_MIN 1 | |
132 | #endif | |
133 | #ifndef USE_PERIODS_MAX | |
134 | #define USE_PERIODS_MAX 1024 | |
135 | #endif | |
136 | #ifndef add_playback_constraints | |
137 | #define add_playback_constraints(x) 0 | |
138 | #endif | |
139 | #ifndef add_capture_constraints | |
140 | #define add_capture_constraints(x) 0 | |
141 | #endif | |
142 | ||
143 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ | |
144 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ | |
145 | static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; | |
146 | static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; | |
147 | static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; | |
148 | //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; | |
149 | ||
150 | module_param_array(index, int, NULL, 0444); | |
151 | MODULE_PARM_DESC(index, "Index value for dummy soundcard."); | |
152 | module_param_array(id, charp, NULL, 0444); | |
153 | MODULE_PARM_DESC(id, "ID string for dummy soundcard."); | |
154 | module_param_array(enable, bool, NULL, 0444); | |
155 | MODULE_PARM_DESC(enable, "Enable this dummy soundcard."); | |
156 | module_param_array(pcm_devs, int, NULL, 0444); | |
157 | MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver."); | |
158 | module_param_array(pcm_substreams, int, NULL, 0444); | |
159 | MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver."); | |
160 | //module_param_array(midi_devs, int, NULL, 0444); | |
161 | //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver."); | |
162 | ||
f7a9275d CL |
163 | static struct platform_device *devices[SNDRV_CARDS]; |
164 | ||
1da177e4 LT |
165 | #define MIXER_ADDR_MASTER 0 |
166 | #define MIXER_ADDR_LINE 1 | |
167 | #define MIXER_ADDR_MIC 2 | |
168 | #define MIXER_ADDR_SYNTH 3 | |
169 | #define MIXER_ADDR_CD 4 | |
170 | #define MIXER_ADDR_LAST 4 | |
171 | ||
4a4d2cfd TI |
172 | struct snd_dummy { |
173 | struct snd_card *card; | |
6e65c1cc | 174 | struct snd_pcm *pcm; |
1da177e4 LT |
175 | spinlock_t mixer_lock; |
176 | int mixer_volume[MIXER_ADDR_LAST+1][2]; | |
177 | int capture_source[MIXER_ADDR_LAST+1][2]; | |
4a4d2cfd | 178 | }; |
1da177e4 | 179 | |
4a4d2cfd TI |
180 | struct snd_dummy_pcm { |
181 | struct snd_dummy *dummy; | |
1da177e4 LT |
182 | spinlock_t lock; |
183 | struct timer_list timer; | |
53463a83 Aİ |
184 | unsigned int pcm_buffer_size; |
185 | unsigned int pcm_period_size; | |
1da177e4 | 186 | unsigned int pcm_bps; /* bytes per second */ |
53463a83 | 187 | unsigned int pcm_hz; /* HZ */ |
1da177e4 LT |
188 | unsigned int pcm_irq_pos; /* IRQ position */ |
189 | unsigned int pcm_buf_pos; /* position in buffer */ | |
4a4d2cfd TI |
190 | struct snd_pcm_substream *substream; |
191 | }; | |
1da177e4 | 192 | |
1da177e4 | 193 | |
4a4d2cfd | 194 | static inline void snd_card_dummy_pcm_timer_start(struct snd_dummy_pcm *dpcm) |
1da177e4 | 195 | { |
1da177e4 LT |
196 | dpcm->timer.expires = 1 + jiffies; |
197 | add_timer(&dpcm->timer); | |
198 | } | |
199 | ||
4a4d2cfd | 200 | static inline void snd_card_dummy_pcm_timer_stop(struct snd_dummy_pcm *dpcm) |
1da177e4 | 201 | { |
1da177e4 LT |
202 | del_timer(&dpcm->timer); |
203 | } | |
204 | ||
4a4d2cfd | 205 | static int snd_card_dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
1da177e4 | 206 | { |
4a4d2cfd TI |
207 | struct snd_pcm_runtime *runtime = substream->runtime; |
208 | struct snd_dummy_pcm *dpcm = runtime->private_data; | |
c8eb6ba1 | 209 | int err = 0; |
1da177e4 | 210 | |
c8eb6ba1 | 211 | spin_lock(&dpcm->lock); |
6e65c1cc TI |
212 | switch (cmd) { |
213 | case SNDRV_PCM_TRIGGER_START: | |
214 | case SNDRV_PCM_TRIGGER_RESUME: | |
c8eb6ba1 | 215 | snd_card_dummy_pcm_timer_start(dpcm); |
6e65c1cc TI |
216 | break; |
217 | case SNDRV_PCM_TRIGGER_STOP: | |
218 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
c8eb6ba1 | 219 | snd_card_dummy_pcm_timer_stop(dpcm); |
6e65c1cc TI |
220 | break; |
221 | default: | |
c8eb6ba1 | 222 | err = -EINVAL; |
6e65c1cc | 223 | break; |
1da177e4 | 224 | } |
c8eb6ba1 | 225 | spin_unlock(&dpcm->lock); |
6e65c1cc | 226 | return 0; |
1da177e4 LT |
227 | } |
228 | ||
4a4d2cfd | 229 | static int snd_card_dummy_pcm_prepare(struct snd_pcm_substream *substream) |
1da177e4 | 230 | { |
4a4d2cfd TI |
231 | struct snd_pcm_runtime *runtime = substream->runtime; |
232 | struct snd_dummy_pcm *dpcm = runtime->private_data; | |
369b240d RK |
233 | int bps; |
234 | ||
235 | bps = snd_pcm_format_width(runtime->format) * runtime->rate * | |
236 | runtime->channels / 8; | |
1da177e4 | 237 | |
1da177e4 LT |
238 | if (bps <= 0) |
239 | return -EINVAL; | |
369b240d | 240 | |
1da177e4 | 241 | dpcm->pcm_bps = bps; |
53463a83 Aİ |
242 | dpcm->pcm_hz = HZ; |
243 | dpcm->pcm_buffer_size = snd_pcm_lib_buffer_bytes(substream); | |
244 | dpcm->pcm_period_size = snd_pcm_lib_period_bytes(substream); | |
1da177e4 LT |
245 | dpcm->pcm_irq_pos = 0; |
246 | dpcm->pcm_buf_pos = 0; | |
53463a83 Aİ |
247 | |
248 | snd_pcm_format_set_silence(runtime->format, runtime->dma_area, | |
249 | bytes_to_samples(runtime, runtime->dma_bytes)); | |
250 | ||
1da177e4 LT |
251 | return 0; |
252 | } | |
253 | ||
1da177e4 LT |
254 | static void snd_card_dummy_pcm_timer_function(unsigned long data) |
255 | { | |
4a4d2cfd | 256 | struct snd_dummy_pcm *dpcm = (struct snd_dummy_pcm *)data; |
b32425ac | 257 | unsigned long flags; |
1da177e4 | 258 | |
b32425ac | 259 | spin_lock_irqsave(&dpcm->lock, flags); |
1da177e4 LT |
260 | dpcm->timer.expires = 1 + jiffies; |
261 | add_timer(&dpcm->timer); | |
53463a83 | 262 | dpcm->pcm_irq_pos += dpcm->pcm_bps; |
470f23b8 Aİ |
263 | dpcm->pcm_buf_pos += dpcm->pcm_bps; |
264 | dpcm->pcm_buf_pos %= dpcm->pcm_buffer_size * dpcm->pcm_hz; | |
53463a83 Aİ |
265 | if (dpcm->pcm_irq_pos >= dpcm->pcm_period_size * dpcm->pcm_hz) { |
266 | dpcm->pcm_irq_pos %= dpcm->pcm_period_size * dpcm->pcm_hz; | |
b32425ac | 267 | spin_unlock_irqrestore(&dpcm->lock, flags); |
1da177e4 | 268 | snd_pcm_period_elapsed(dpcm->substream); |
b32425ac TI |
269 | } else |
270 | spin_unlock_irqrestore(&dpcm->lock, flags); | |
1da177e4 LT |
271 | } |
272 | ||
4a4d2cfd | 273 | static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(struct snd_pcm_substream *substream) |
1da177e4 | 274 | { |
4a4d2cfd TI |
275 | struct snd_pcm_runtime *runtime = substream->runtime; |
276 | struct snd_dummy_pcm *dpcm = runtime->private_data; | |
1da177e4 | 277 | |
470f23b8 | 278 | return bytes_to_frames(runtime, dpcm->pcm_buf_pos / dpcm->pcm_hz); |
1da177e4 LT |
279 | } |
280 | ||
4a4d2cfd | 281 | static struct snd_pcm_hardware snd_card_dummy_playback = |
1da177e4 LT |
282 | { |
283 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | | |
6e65c1cc | 284 | SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID), |
1da177e4 LT |
285 | .formats = USE_FORMATS, |
286 | .rates = USE_RATE, | |
287 | .rate_min = USE_RATE_MIN, | |
288 | .rate_max = USE_RATE_MAX, | |
289 | .channels_min = USE_CHANNELS_MIN, | |
290 | .channels_max = USE_CHANNELS_MAX, | |
291 | .buffer_bytes_max = MAX_BUFFER_SIZE, | |
292 | .period_bytes_min = 64, | |
e05d6964 | 293 | .period_bytes_max = MAX_PERIOD_SIZE, |
1da177e4 LT |
294 | .periods_min = USE_PERIODS_MIN, |
295 | .periods_max = USE_PERIODS_MAX, | |
296 | .fifo_size = 0, | |
297 | }; | |
298 | ||
4a4d2cfd | 299 | static struct snd_pcm_hardware snd_card_dummy_capture = |
1da177e4 LT |
300 | { |
301 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | | |
6e65c1cc | 302 | SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID), |
1da177e4 LT |
303 | .formats = USE_FORMATS, |
304 | .rates = USE_RATE, | |
305 | .rate_min = USE_RATE_MIN, | |
306 | .rate_max = USE_RATE_MAX, | |
307 | .channels_min = USE_CHANNELS_MIN, | |
308 | .channels_max = USE_CHANNELS_MAX, | |
309 | .buffer_bytes_max = MAX_BUFFER_SIZE, | |
310 | .period_bytes_min = 64, | |
2ad5dd8d | 311 | .period_bytes_max = MAX_PERIOD_SIZE, |
1da177e4 LT |
312 | .periods_min = USE_PERIODS_MIN, |
313 | .periods_max = USE_PERIODS_MAX, | |
314 | .fifo_size = 0, | |
315 | }; | |
316 | ||
4a4d2cfd | 317 | static void snd_card_dummy_runtime_free(struct snd_pcm_runtime *runtime) |
1da177e4 | 318 | { |
c8eb6ba1 | 319 | kfree(runtime->private_data); |
1da177e4 LT |
320 | } |
321 | ||
4a4d2cfd TI |
322 | static int snd_card_dummy_hw_params(struct snd_pcm_substream *substream, |
323 | struct snd_pcm_hw_params *hw_params) | |
1da177e4 LT |
324 | { |
325 | return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); | |
326 | } | |
327 | ||
4a4d2cfd | 328 | static int snd_card_dummy_hw_free(struct snd_pcm_substream *substream) |
1da177e4 LT |
329 | { |
330 | return snd_pcm_lib_free_pages(substream); | |
331 | } | |
332 | ||
4a4d2cfd | 333 | static struct snd_dummy_pcm *new_pcm_stream(struct snd_pcm_substream *substream) |
1da177e4 | 334 | { |
4a4d2cfd | 335 | struct snd_dummy_pcm *dpcm; |
1da177e4 | 336 | |
561b220a | 337 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
c8eb6ba1 TI |
338 | if (! dpcm) |
339 | return dpcm; | |
1da177e4 LT |
340 | init_timer(&dpcm->timer); |
341 | dpcm->timer.data = (unsigned long) dpcm; | |
342 | dpcm->timer.function = snd_card_dummy_pcm_timer_function; | |
343 | spin_lock_init(&dpcm->lock); | |
344 | dpcm->substream = substream; | |
c8eb6ba1 TI |
345 | return dpcm; |
346 | } | |
347 | ||
4a4d2cfd | 348 | static int snd_card_dummy_playback_open(struct snd_pcm_substream *substream) |
c8eb6ba1 | 349 | { |
4a4d2cfd TI |
350 | struct snd_pcm_runtime *runtime = substream->runtime; |
351 | struct snd_dummy_pcm *dpcm; | |
c8eb6ba1 TI |
352 | int err; |
353 | ||
354 | if ((dpcm = new_pcm_stream(substream)) == NULL) | |
355 | return -ENOMEM; | |
1da177e4 LT |
356 | runtime->private_data = dpcm; |
357 | runtime->private_free = snd_card_dummy_runtime_free; | |
358 | runtime->hw = snd_card_dummy_playback; | |
359 | if (substream->pcm->device & 1) { | |
360 | runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; | |
361 | runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; | |
362 | } | |
363 | if (substream->pcm->device & 2) | |
364 | runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID); | |
365 | if ((err = add_playback_constraints(runtime)) < 0) { | |
366 | kfree(dpcm); | |
367 | return err; | |
368 | } | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
4a4d2cfd | 373 | static int snd_card_dummy_capture_open(struct snd_pcm_substream *substream) |
1da177e4 | 374 | { |
4a4d2cfd TI |
375 | struct snd_pcm_runtime *runtime = substream->runtime; |
376 | struct snd_dummy_pcm *dpcm; | |
1da177e4 LT |
377 | int err; |
378 | ||
c8eb6ba1 | 379 | if ((dpcm = new_pcm_stream(substream)) == NULL) |
1da177e4 | 380 | return -ENOMEM; |
1da177e4 LT |
381 | runtime->private_data = dpcm; |
382 | runtime->private_free = snd_card_dummy_runtime_free; | |
383 | runtime->hw = snd_card_dummy_capture; | |
384 | if (substream->pcm->device == 1) { | |
385 | runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; | |
386 | runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; | |
387 | } | |
388 | if (substream->pcm->device & 2) | |
389 | runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID); | |
390 | if ((err = add_capture_constraints(runtime)) < 0) { | |
391 | kfree(dpcm); | |
392 | return err; | |
393 | } | |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
4a4d2cfd | 398 | static int snd_card_dummy_playback_close(struct snd_pcm_substream *substream) |
1da177e4 LT |
399 | { |
400 | return 0; | |
401 | } | |
402 | ||
4a4d2cfd | 403 | static int snd_card_dummy_capture_close(struct snd_pcm_substream *substream) |
1da177e4 LT |
404 | { |
405 | return 0; | |
406 | } | |
407 | ||
4a4d2cfd | 408 | static struct snd_pcm_ops snd_card_dummy_playback_ops = { |
1da177e4 LT |
409 | .open = snd_card_dummy_playback_open, |
410 | .close = snd_card_dummy_playback_close, | |
411 | .ioctl = snd_pcm_lib_ioctl, | |
412 | .hw_params = snd_card_dummy_hw_params, | |
413 | .hw_free = snd_card_dummy_hw_free, | |
c8eb6ba1 TI |
414 | .prepare = snd_card_dummy_pcm_prepare, |
415 | .trigger = snd_card_dummy_pcm_trigger, | |
416 | .pointer = snd_card_dummy_pcm_pointer, | |
1da177e4 LT |
417 | }; |
418 | ||
4a4d2cfd | 419 | static struct snd_pcm_ops snd_card_dummy_capture_ops = { |
1da177e4 LT |
420 | .open = snd_card_dummy_capture_open, |
421 | .close = snd_card_dummy_capture_close, | |
422 | .ioctl = snd_pcm_lib_ioctl, | |
423 | .hw_params = snd_card_dummy_hw_params, | |
424 | .hw_free = snd_card_dummy_hw_free, | |
c8eb6ba1 TI |
425 | .prepare = snd_card_dummy_pcm_prepare, |
426 | .trigger = snd_card_dummy_pcm_trigger, | |
427 | .pointer = snd_card_dummy_pcm_pointer, | |
1da177e4 LT |
428 | }; |
429 | ||
788c6043 PB |
430 | static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device, |
431 | int substreams) | |
1da177e4 | 432 | { |
4a4d2cfd | 433 | struct snd_pcm *pcm; |
1da177e4 LT |
434 | int err; |
435 | ||
4a4d2cfd TI |
436 | if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device, |
437 | substreams, substreams, &pcm)) < 0) | |
1da177e4 | 438 | return err; |
6e65c1cc | 439 | dummy->pcm = pcm; |
1da177e4 LT |
440 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops); |
441 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops); | |
442 | pcm->private_data = dummy; | |
443 | pcm->info_flags = 0; | |
444 | strcpy(pcm->name, "Dummy PCM"); | |
445 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, | |
446 | snd_dma_continuous_data(GFP_KERNEL), | |
447 | 0, 64*1024); | |
448 | return 0; | |
449 | } | |
450 | ||
451 | #define DUMMY_VOLUME(xname, xindex, addr) \ | |
fb567a8e TI |
452 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ |
453 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ | |
454 | .name = xname, .index = xindex, \ | |
1da177e4 LT |
455 | .info = snd_dummy_volume_info, \ |
456 | .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \ | |
fb567a8e TI |
457 | .private_value = addr, \ |
458 | .tlv = { .p = db_scale_dummy } } | |
1da177e4 | 459 | |
4a4d2cfd TI |
460 | static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol, |
461 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
462 | { |
463 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
464 | uinfo->count = 2; | |
465 | uinfo->value.integer.min = -50; | |
466 | uinfo->value.integer.max = 100; | |
467 | return 0; | |
468 | } | |
469 | ||
4a4d2cfd TI |
470 | static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol, |
471 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 472 | { |
4a4d2cfd | 473 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
474 | int addr = kcontrol->private_value; |
475 | ||
c8eb6ba1 | 476 | spin_lock_irq(&dummy->mixer_lock); |
1da177e4 LT |
477 | ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0]; |
478 | ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1]; | |
c8eb6ba1 | 479 | spin_unlock_irq(&dummy->mixer_lock); |
1da177e4 LT |
480 | return 0; |
481 | } | |
482 | ||
4a4d2cfd TI |
483 | static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol, |
484 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 485 | { |
4a4d2cfd | 486 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
487 | int change, addr = kcontrol->private_value; |
488 | int left, right; | |
489 | ||
490 | left = ucontrol->value.integer.value[0]; | |
491 | if (left < -50) | |
492 | left = -50; | |
493 | if (left > 100) | |
494 | left = 100; | |
495 | right = ucontrol->value.integer.value[1]; | |
496 | if (right < -50) | |
497 | right = -50; | |
498 | if (right > 100) | |
499 | right = 100; | |
c8eb6ba1 | 500 | spin_lock_irq(&dummy->mixer_lock); |
1da177e4 LT |
501 | change = dummy->mixer_volume[addr][0] != left || |
502 | dummy->mixer_volume[addr][1] != right; | |
503 | dummy->mixer_volume[addr][0] = left; | |
504 | dummy->mixer_volume[addr][1] = right; | |
c8eb6ba1 | 505 | spin_unlock_irq(&dummy->mixer_lock); |
1da177e4 LT |
506 | return change; |
507 | } | |
508 | ||
0cb29ea0 | 509 | static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0); |
fb567a8e | 510 | |
1da177e4 LT |
511 | #define DUMMY_CAPSRC(xname, xindex, addr) \ |
512 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ | |
513 | .info = snd_dummy_capsrc_info, \ | |
514 | .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \ | |
515 | .private_value = addr } | |
516 | ||
a5ce8890 | 517 | #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info |
1da177e4 | 518 | |
4a4d2cfd TI |
519 | static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol, |
520 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 521 | { |
4a4d2cfd | 522 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
523 | int addr = kcontrol->private_value; |
524 | ||
c8eb6ba1 | 525 | spin_lock_irq(&dummy->mixer_lock); |
1da177e4 LT |
526 | ucontrol->value.integer.value[0] = dummy->capture_source[addr][0]; |
527 | ucontrol->value.integer.value[1] = dummy->capture_source[addr][1]; | |
c8eb6ba1 | 528 | spin_unlock_irq(&dummy->mixer_lock); |
1da177e4 LT |
529 | return 0; |
530 | } | |
531 | ||
4a4d2cfd | 532 | static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 533 | { |
4a4d2cfd | 534 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
535 | int change, addr = kcontrol->private_value; |
536 | int left, right; | |
537 | ||
538 | left = ucontrol->value.integer.value[0] & 1; | |
539 | right = ucontrol->value.integer.value[1] & 1; | |
c8eb6ba1 | 540 | spin_lock_irq(&dummy->mixer_lock); |
1da177e4 LT |
541 | change = dummy->capture_source[addr][0] != left && |
542 | dummy->capture_source[addr][1] != right; | |
543 | dummy->capture_source[addr][0] = left; | |
544 | dummy->capture_source[addr][1] = right; | |
c8eb6ba1 | 545 | spin_unlock_irq(&dummy->mixer_lock); |
1da177e4 LT |
546 | return change; |
547 | } | |
548 | ||
4a4d2cfd | 549 | static struct snd_kcontrol_new snd_dummy_controls[] = { |
1da177e4 LT |
550 | DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER), |
551 | DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER), | |
552 | DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH), | |
e05d6964 | 553 | DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH), |
1da177e4 | 554 | DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE), |
e05d6964 | 555 | DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE), |
1da177e4 | 556 | DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC), |
e05d6964 | 557 | DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC), |
1da177e4 | 558 | DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD), |
e05d6964 | 559 | DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD) |
1da177e4 LT |
560 | }; |
561 | ||
788c6043 | 562 | static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy) |
1da177e4 | 563 | { |
4a4d2cfd | 564 | struct snd_card *card = dummy->card; |
1da177e4 LT |
565 | unsigned int idx; |
566 | int err; | |
567 | ||
5e246b85 TI |
568 | if (snd_BUG_ON(!dummy)) |
569 | return -EINVAL; | |
1da177e4 LT |
570 | spin_lock_init(&dummy->mixer_lock); |
571 | strcpy(card->mixername, "Dummy Mixer"); | |
572 | ||
573 | for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) { | |
574 | if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0) | |
575 | return err; | |
576 | } | |
577 | return 0; | |
578 | } | |
579 | ||
788c6043 | 580 | static int __devinit snd_dummy_probe(struct platform_device *devptr) |
1da177e4 | 581 | { |
4a4d2cfd TI |
582 | struct snd_card *card; |
583 | struct snd_dummy *dummy; | |
1da177e4 | 584 | int idx, err; |
6e65c1cc | 585 | int dev = devptr->id; |
1da177e4 | 586 | |
1da177e4 | 587 | card = snd_card_new(index[dev], id[dev], THIS_MODULE, |
4a4d2cfd | 588 | sizeof(struct snd_dummy)); |
1da177e4 LT |
589 | if (card == NULL) |
590 | return -ENOMEM; | |
4a4d2cfd | 591 | dummy = card->private_data; |
1da177e4 LT |
592 | dummy->card = card; |
593 | for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) { | |
594 | if (pcm_substreams[dev] < 1) | |
595 | pcm_substreams[dev] = 1; | |
596 | if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) | |
597 | pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; | |
598 | if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0) | |
599 | goto __nodev; | |
600 | } | |
601 | if ((err = snd_card_dummy_new_mixer(dummy)) < 0) | |
602 | goto __nodev; | |
603 | strcpy(card->driver, "Dummy"); | |
604 | strcpy(card->shortname, "Dummy"); | |
605 | sprintf(card->longname, "Dummy %i", dev + 1); | |
16dab54b | 606 | |
6e65c1cc | 607 | snd_card_set_dev(card, &devptr->dev); |
16dab54b | 608 | |
1da177e4 | 609 | if ((err = snd_card_register(card)) == 0) { |
6e65c1cc | 610 | platform_set_drvdata(devptr, card); |
1da177e4 LT |
611 | return 0; |
612 | } | |
613 | __nodev: | |
614 | snd_card_free(card); | |
615 | return err; | |
616 | } | |
617 | ||
788c6043 | 618 | static int __devexit snd_dummy_remove(struct platform_device *devptr) |
6e65c1cc TI |
619 | { |
620 | snd_card_free(platform_get_drvdata(devptr)); | |
621 | platform_set_drvdata(devptr, NULL); | |
622 | return 0; | |
623 | } | |
624 | ||
625 | #ifdef CONFIG_PM | |
626 | static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state) | |
1da177e4 | 627 | { |
6e65c1cc TI |
628 | struct snd_card *card = platform_get_drvdata(pdev); |
629 | struct snd_dummy *dummy = card->private_data; | |
1da177e4 | 630 | |
6e65c1cc TI |
631 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
632 | snd_pcm_suspend_all(dummy->pcm); | |
633 | return 0; | |
634 | } | |
635 | ||
636 | static int snd_dummy_resume(struct platform_device *pdev) | |
637 | { | |
638 | struct snd_card *card = platform_get_drvdata(pdev); | |
639 | ||
640 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); | |
641 | return 0; | |
642 | } | |
643 | #endif | |
644 | ||
645 | #define SND_DUMMY_DRIVER "snd_dummy" | |
646 | ||
647 | static struct platform_driver snd_dummy_driver = { | |
648 | .probe = snd_dummy_probe, | |
788c6043 | 649 | .remove = __devexit_p(snd_dummy_remove), |
6e65c1cc TI |
650 | #ifdef CONFIG_PM |
651 | .suspend = snd_dummy_suspend, | |
652 | .resume = snd_dummy_resume, | |
1da177e4 | 653 | #endif |
6e65c1cc TI |
654 | .driver = { |
655 | .name = SND_DUMMY_DRIVER | |
656 | }, | |
657 | }; | |
658 | ||
c12aad6e | 659 | static void snd_dummy_unregister_all(void) |
f7a9275d CL |
660 | { |
661 | int i; | |
662 | ||
663 | for (i = 0; i < ARRAY_SIZE(devices); ++i) | |
664 | platform_device_unregister(devices[i]); | |
665 | platform_driver_unregister(&snd_dummy_driver); | |
666 | } | |
667 | ||
6e65c1cc TI |
668 | static int __init alsa_card_dummy_init(void) |
669 | { | |
670 | int i, cards, err; | |
671 | ||
672 | if ((err = platform_driver_register(&snd_dummy_driver)) < 0) | |
673 | return err; | |
674 | ||
675 | cards = 0; | |
8278ca8f | 676 | for (i = 0; i < SNDRV_CARDS; i++) { |
6e65c1cc | 677 | struct platform_device *device; |
8278ca8f TI |
678 | if (! enable[i]) |
679 | continue; | |
6e65c1cc TI |
680 | device = platform_device_register_simple(SND_DUMMY_DRIVER, |
681 | i, NULL, 0); | |
a182ee98 RH |
682 | if (IS_ERR(device)) |
683 | continue; | |
7152447d RH |
684 | if (!platform_get_drvdata(device)) { |
685 | platform_device_unregister(device); | |
686 | continue; | |
687 | } | |
f7a9275d | 688 | devices[i] = device; |
1da177e4 LT |
689 | cards++; |
690 | } | |
691 | if (!cards) { | |
692 | #ifdef MODULE | |
693 | printk(KERN_ERR "Dummy soundcard not found or device busy\n"); | |
694 | #endif | |
a182ee98 RH |
695 | snd_dummy_unregister_all(); |
696 | return -ENODEV; | |
1da177e4 LT |
697 | } |
698 | return 0; | |
699 | } | |
700 | ||
701 | static void __exit alsa_card_dummy_exit(void) | |
702 | { | |
f7a9275d | 703 | snd_dummy_unregister_all(); |
1da177e4 LT |
704 | } |
705 | ||
706 | module_init(alsa_card_dummy_init) | |
707 | module_exit(alsa_card_dummy_exit) |