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> | |
c631d03c TI |
28 | #include <linux/hrtimer.h> |
29 | #include <linux/math64.h> | |
65a77217 | 30 | #include <linux/module.h> |
1da177e4 LT |
31 | #include <sound/core.h> |
32 | #include <sound/control.h> | |
fb567a8e | 33 | #include <sound/tlv.h> |
1da177e4 LT |
34 | #include <sound/pcm.h> |
35 | #include <sound/rawmidi.h> | |
9b151fec | 36 | #include <sound/info.h> |
1da177e4 LT |
37 | #include <sound/initval.h> |
38 | ||
c1017a4c | 39 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
1da177e4 LT |
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 | |
b888d1ce | 45 | #define MAX_PCM_SUBSTREAMS 128 |
1da177e4 LT |
46 | #define MAX_MIDI_DEVICES 2 |
47 | ||
1da177e4 | 48 | /* defaults */ |
1da177e4 | 49 | #define MAX_BUFFER_SIZE (64*1024) |
d5e1ca05 | 50 | #define MIN_PERIOD_SIZE 64 |
2ad5dd8d | 51 | #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE |
1da177e4 | 52 | #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE) |
1da177e4 LT |
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 | |
1da177e4 | 56 | #define USE_CHANNELS_MIN 1 |
1da177e4 | 57 | #define USE_CHANNELS_MAX 2 |
1da177e4 | 58 | #define USE_PERIODS_MIN 1 |
1da177e4 | 59 | #define USE_PERIODS_MAX 1024 |
1da177e4 LT |
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 */ | |
a67ff6a5 | 63 | static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; |
d5e1ca05 | 64 | static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL}; |
1da177e4 LT |
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}; | |
c631d03c | 68 | #ifdef CONFIG_HIGH_RES_TIMERS |
a67ff6a5 | 69 | static bool hrtimer = 1; |
c631d03c | 70 | #endif |
a67ff6a5 | 71 | static bool fake_buffer = 1; |
1da177e4 LT |
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."); | |
d5e1ca05 JK |
79 | module_param_array(model, charp, NULL, 0444); |
80 | MODULE_PARM_DESC(model, "Soundcard model."); | |
1da177e4 LT |
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); | |
23aebca4 | 84 | MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver."); |
1da177e4 LT |
85 | //module_param_array(midi_devs, int, NULL, 0444); |
86 | //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver."); | |
a68c4d11 TI |
87 | module_param(fake_buffer, bool, 0444); |
88 | MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations."); | |
c631d03c | 89 | #ifdef CONFIG_HIGH_RES_TIMERS |
e063b1a0 | 90 | module_param(hrtimer, bool, 0644); |
c631d03c TI |
91 | MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source."); |
92 | #endif | |
1da177e4 | 93 | |
f7a9275d CL |
94 | static struct platform_device *devices[SNDRV_CARDS]; |
95 | ||
1da177e4 LT |
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 | ||
c631d03c TI |
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 | ||
e063b1a0 TI |
112 | #define get_dummy_ops(substream) \ |
113 | (*(const struct dummy_timer_ops **)(substream)->runtime->private_data) | |
114 | ||
d5e1ca05 JK |
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 | ||
4a4d2cfd TI |
132 | struct snd_dummy { |
133 | struct snd_card *card; | |
d5e1ca05 | 134 | struct dummy_model *model; |
6e65c1cc | 135 | struct snd_pcm *pcm; |
d5e1ca05 | 136 | struct snd_pcm_hardware pcm_hw; |
1da177e4 LT |
137 | spinlock_t mixer_lock; |
138 | int mixer_volume[MIXER_ADDR_LAST+1][2]; | |
139 | int capture_source[MIXER_ADDR_LAST+1][2]; | |
16e43467 CL |
140 | int iobox; |
141 | struct snd_kcontrol *cd_volume_ctl; | |
142 | struct snd_kcontrol *cd_switch_ctl; | |
4a4d2cfd | 143 | }; |
1da177e4 | 144 | |
d5e1ca05 JK |
145 | /* |
146 | * card models | |
147 | */ | |
148 | ||
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 | ||
c631d03c TI |
231 | /* |
232 | * system timer interface | |
233 | */ | |
234 | ||
235 | struct dummy_systimer_pcm { | |
e063b1a0 TI |
236 | /* ops must be the first item */ |
237 | const struct dummy_timer_ops *timer_ops; | |
1da177e4 LT |
238 | spinlock_t lock; |
239 | struct timer_list timer; | |
b142037b TI |
240 | unsigned long base_time; |
241 | unsigned int frac_pos; /* fractional sample position (based HZ) */ | |
b5d10781 | 242 | unsigned int frac_period_rest; |
b142037b TI |
243 | unsigned int frac_buffer_size; /* buffer_size * HZ */ |
244 | unsigned int frac_period_size; /* period_size * HZ */ | |
245 | unsigned int rate; | |
b5d10781 | 246 | int elapsed; |
4a4d2cfd TI |
247 | struct snd_pcm_substream *substream; |
248 | }; | |
1da177e4 | 249 | |
b142037b TI |
250 | static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm) |
251 | { | |
b142037b | 252 | dpcm->timer.expires = jiffies + |
b5d10781 | 253 | (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate; |
b142037b TI |
254 | add_timer(&dpcm->timer); |
255 | } | |
256 | ||
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; | |
b5d10781 TI |
264 | dpcm->base_time += delta; |
265 | delta *= dpcm->rate; | |
266 | dpcm->frac_pos += delta; | |
b142037b TI |
267 | while (dpcm->frac_pos >= dpcm->frac_buffer_size) |
268 | dpcm->frac_pos -= dpcm->frac_buffer_size; | |
b5d10781 TI |
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; | |
b142037b TI |
274 | } |
275 | ||
c631d03c | 276 | static int dummy_systimer_start(struct snd_pcm_substream *substream) |
1da177e4 | 277 | { |
c631d03c TI |
278 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
279 | spin_lock(&dpcm->lock); | |
b142037b TI |
280 | dpcm->base_time = jiffies; |
281 | dummy_systimer_rearm(dpcm); | |
c631d03c TI |
282 | spin_unlock(&dpcm->lock); |
283 | return 0; | |
1da177e4 LT |
284 | } |
285 | ||
c631d03c | 286 | static int dummy_systimer_stop(struct snd_pcm_substream *substream) |
1da177e4 | 287 | { |
c631d03c | 288 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
c8eb6ba1 | 289 | spin_lock(&dpcm->lock); |
c631d03c | 290 | del_timer(&dpcm->timer); |
c8eb6ba1 | 291 | spin_unlock(&dpcm->lock); |
6e65c1cc | 292 | return 0; |
1da177e4 LT |
293 | } |
294 | ||
c631d03c | 295 | static int dummy_systimer_prepare(struct snd_pcm_substream *substream) |
1da177e4 | 296 | { |
4a4d2cfd | 297 | struct snd_pcm_runtime *runtime = substream->runtime; |
c631d03c | 298 | struct dummy_systimer_pcm *dpcm = runtime->private_data; |
369b240d | 299 | |
b142037b TI |
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; | |
b5d10781 TI |
304 | dpcm->frac_period_rest = dpcm->frac_period_size; |
305 | dpcm->elapsed = 0; | |
53463a83 | 306 | |
1da177e4 LT |
307 | return 0; |
308 | } | |
309 | ||
c631d03c | 310 | static void dummy_systimer_callback(unsigned long data) |
1da177e4 | 311 | { |
c631d03c | 312 | struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data; |
b32425ac | 313 | unsigned long flags; |
b5d10781 | 314 | int elapsed = 0; |
1da177e4 | 315 | |
b32425ac | 316 | spin_lock_irqsave(&dpcm->lock, flags); |
b142037b TI |
317 | dummy_systimer_update(dpcm); |
318 | dummy_systimer_rearm(dpcm); | |
b5d10781 TI |
319 | elapsed = dpcm->elapsed; |
320 | dpcm->elapsed = 0; | |
b142037b | 321 | spin_unlock_irqrestore(&dpcm->lock, flags); |
b5d10781 TI |
322 | if (elapsed) |
323 | snd_pcm_period_elapsed(dpcm->substream); | |
1da177e4 LT |
324 | } |
325 | ||
c631d03c TI |
326 | static snd_pcm_uframes_t |
327 | dummy_systimer_pointer(struct snd_pcm_substream *substream) | |
1da177e4 | 328 | { |
b142037b | 329 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
b5d10781 | 330 | snd_pcm_uframes_t pos; |
1da177e4 | 331 | |
b142037b TI |
332 | spin_lock(&dpcm->lock); |
333 | dummy_systimer_update(dpcm); | |
b5d10781 | 334 | pos = dpcm->frac_pos / HZ; |
b142037b | 335 | spin_unlock(&dpcm->lock); |
b5d10781 | 336 | return pos; |
1da177e4 LT |
337 | } |
338 | ||
c631d03c | 339 | static int dummy_systimer_create(struct snd_pcm_substream *substream) |
1da177e4 | 340 | { |
c631d03c TI |
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 | ||
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, | |
1da177e4 LT |
367 | }; |
368 | ||
c631d03c TI |
369 | #ifdef CONFIG_HIGH_RES_TIMERS |
370 | /* | |
371 | * hrtimer interface | |
372 | */ | |
373 | ||
374 | struct dummy_hrtimer_pcm { | |
e063b1a0 TI |
375 | /* ops must be the first item */ |
376 | const struct dummy_timer_ops *timer_ops; | |
c631d03c TI |
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
423 | static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm) | |
424 | { | |
c3fdd7d3 | 425 | hrtimer_cancel(&dpcm->timer); |
c631d03c TI |
426 | tasklet_kill(&dpcm->tasklet); |
427 | } | |
428 | ||
429 | static snd_pcm_uframes_t | |
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 | ||
444 | static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream) | |
1da177e4 | 445 | { |
c631d03c TI |
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 | ||
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 | ||
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 | ||
502 | static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
503 | { | |
c631d03c TI |
504 | switch (cmd) { |
505 | case SNDRV_PCM_TRIGGER_START: | |
506 | case SNDRV_PCM_TRIGGER_RESUME: | |
e063b1a0 | 507 | return get_dummy_ops(substream)->start(substream); |
c631d03c TI |
508 | case SNDRV_PCM_TRIGGER_STOP: |
509 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
e063b1a0 | 510 | return get_dummy_ops(substream)->stop(substream); |
c631d03c TI |
511 | } |
512 | return -EINVAL; | |
513 | } | |
514 | ||
515 | static int dummy_pcm_prepare(struct snd_pcm_substream *substream) | |
516 | { | |
e063b1a0 | 517 | return get_dummy_ops(substream)->prepare(substream); |
c631d03c TI |
518 | } |
519 | ||
520 | static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream) | |
521 | { | |
e063b1a0 | 522 | return get_dummy_ops(substream)->pointer(substream); |
c631d03c TI |
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), | |
1da177e4 LT |
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, | |
d5e1ca05 | 537 | .period_bytes_min = MIN_PERIOD_SIZE, |
2ad5dd8d | 538 | .period_bytes_max = MAX_PERIOD_SIZE, |
1da177e4 LT |
539 | .periods_min = USE_PERIODS_MIN, |
540 | .periods_max = USE_PERIODS_MAX, | |
541 | .fifo_size = 0, | |
542 | }; | |
543 | ||
c631d03c TI |
544 | static int dummy_pcm_hw_params(struct snd_pcm_substream *substream, |
545 | struct snd_pcm_hw_params *hw_params) | |
1da177e4 | 546 | { |
a68c4d11 TI |
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 | } | |
c631d03c TI |
552 | return snd_pcm_lib_malloc_pages(substream, |
553 | params_buffer_bytes(hw_params)); | |
1da177e4 LT |
554 | } |
555 | ||
c631d03c | 556 | static int dummy_pcm_hw_free(struct snd_pcm_substream *substream) |
1da177e4 | 557 | { |
a68c4d11 TI |
558 | if (fake_buffer) |
559 | return 0; | |
1da177e4 LT |
560 | return snd_pcm_lib_free_pages(substream); |
561 | } | |
562 | ||
c631d03c | 563 | static int dummy_pcm_open(struct snd_pcm_substream *substream) |
c8eb6ba1 | 564 | { |
c631d03c | 565 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
d5e1ca05 | 566 | struct dummy_model *model = dummy->model; |
4a4d2cfd | 567 | struct snd_pcm_runtime *runtime = substream->runtime; |
e063b1a0 | 568 | const struct dummy_timer_ops *ops; |
c8eb6ba1 TI |
569 | int err; |
570 | ||
e063b1a0 | 571 | ops = &dummy_systimer_ops; |
c631d03c TI |
572 | #ifdef CONFIG_HIGH_RES_TIMERS |
573 | if (hrtimer) | |
e063b1a0 | 574 | ops = &dummy_hrtimer_ops; |
c631d03c TI |
575 | #endif |
576 | ||
e063b1a0 | 577 | err = ops->create(substream); |
1a11cb64 | 578 | if (err < 0) |
1da177e4 | 579 | return err; |
e063b1a0 | 580 | get_dummy_ops(substream) = ops; |
1da177e4 | 581 | |
d5e1ca05 | 582 | runtime->hw = dummy->pcm_hw; |
c631d03c | 583 | if (substream->pcm->device & 1) { |
1da177e4 LT |
584 | runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; |
585 | runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; | |
586 | } | |
587 | if (substream->pcm->device & 2) | |
c631d03c TI |
588 | runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP | |
589 | SNDRV_PCM_INFO_MMAP_VALID); | |
590 | ||
d5e1ca05 JK |
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 | } | |
c631d03c | 601 | if (err < 0) { |
e063b1a0 | 602 | get_dummy_ops(substream)->free(substream); |
1da177e4 | 603 | return err; |
c631d03c | 604 | } |
1da177e4 LT |
605 | return 0; |
606 | } | |
607 | ||
c631d03c | 608 | static int dummy_pcm_close(struct snd_pcm_substream *substream) |
1da177e4 | 609 | { |
e063b1a0 | 610 | get_dummy_ops(substream)->free(substream); |
1da177e4 LT |
611 | return 0; |
612 | } | |
613 | ||
a68c4d11 TI |
614 | /* |
615 | * dummy buffer handling | |
616 | */ | |
617 | ||
618 | static void *dummy_page[2]; | |
619 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
c631d03c TI |
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, | |
1da177e4 LT |
677 | }; |
678 | ||
a68c4d11 TI |
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 | ||
fbbb01a1 BP |
693 | static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device, |
694 | int substreams) | |
1da177e4 | 695 | { |
4a4d2cfd | 696 | struct snd_pcm *pcm; |
a68c4d11 | 697 | struct snd_pcm_ops *ops; |
1da177e4 LT |
698 | int err; |
699 | ||
1a11cb64 JK |
700 | err = snd_pcm_new(dummy->card, "Dummy PCM", device, |
701 | substreams, substreams, &pcm); | |
702 | if (err < 0) | |
1da177e4 | 703 | return err; |
6e65c1cc | 704 | dummy->pcm = pcm; |
a68c4d11 TI |
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); | |
1da177e4 LT |
711 | pcm->private_data = dummy; |
712 | pcm->info_flags = 0; | |
713 | strcpy(pcm->name, "Dummy PCM"); | |
a68c4d11 TI |
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 | } | |
1da177e4 LT |
720 | return 0; |
721 | } | |
722 | ||
9b151fec TI |
723 | /* |
724 | * mixer interface | |
725 | */ | |
726 | ||
1da177e4 | 727 | #define DUMMY_VOLUME(xname, xindex, addr) \ |
fb567a8e TI |
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, \ | |
1da177e4 LT |
731 | .info = snd_dummy_volume_info, \ |
732 | .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \ | |
fb567a8e TI |
733 | .private_value = addr, \ |
734 | .tlv = { .p = db_scale_dummy } } | |
1da177e4 | 735 | |
4a4d2cfd TI |
736 | static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol, |
737 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
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 | ||
4a4d2cfd TI |
746 | static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol, |
747 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 748 | { |
4a4d2cfd | 749 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
750 | int addr = kcontrol->private_value; |
751 | ||
c8eb6ba1 | 752 | spin_lock_irq(&dummy->mixer_lock); |
1da177e4 LT |
753 | ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0]; |
754 | ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1]; | |
c8eb6ba1 | 755 | spin_unlock_irq(&dummy->mixer_lock); |
1da177e4 LT |
756 | return 0; |
757 | } | |
758 | ||
4a4d2cfd TI |
759 | static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol, |
760 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 761 | { |
4a4d2cfd | 762 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
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; | |
c8eb6ba1 | 776 | spin_lock_irq(&dummy->mixer_lock); |
1da177e4 LT |
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; | |
c8eb6ba1 | 781 | spin_unlock_irq(&dummy->mixer_lock); |
1da177e4 LT |
782 | return change; |
783 | } | |
784 | ||
0cb29ea0 | 785 | static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0); |
fb567a8e | 786 | |
1da177e4 LT |
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 | ||
a5ce8890 | 793 | #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info |
1da177e4 | 794 | |
4a4d2cfd TI |
795 | static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol, |
796 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 797 | { |
4a4d2cfd | 798 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
799 | int addr = kcontrol->private_value; |
800 | ||
c8eb6ba1 | 801 | spin_lock_irq(&dummy->mixer_lock); |
1da177e4 LT |
802 | ucontrol->value.integer.value[0] = dummy->capture_source[addr][0]; |
803 | ucontrol->value.integer.value[1] = dummy->capture_source[addr][1]; | |
c8eb6ba1 | 804 | spin_unlock_irq(&dummy->mixer_lock); |
1da177e4 LT |
805 | return 0; |
806 | } | |
807 | ||
4a4d2cfd | 808 | static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 | 809 | { |
4a4d2cfd | 810 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
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; | |
c8eb6ba1 | 816 | spin_lock_irq(&dummy->mixer_lock); |
1da177e4 LT |
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; | |
c8eb6ba1 | 821 | spin_unlock_irq(&dummy->mixer_lock); |
1da177e4 LT |
822 | return change; |
823 | } | |
824 | ||
16e43467 CL |
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 | ||
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 | ||
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 | ||
4a4d2cfd | 876 | static struct snd_kcontrol_new snd_dummy_controls[] = { |
1da177e4 LT |
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), | |
e05d6964 | 880 | DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH), |
1da177e4 | 881 | DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE), |
e05d6964 | 882 | DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE), |
1da177e4 | 883 | DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC), |
e05d6964 | 884 | DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC), |
1da177e4 | 885 | DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD), |
16e43467 CL |
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 | }, | |
1da177e4 LT |
894 | }; |
895 | ||
fbbb01a1 | 896 | static int snd_card_dummy_new_mixer(struct snd_dummy *dummy) |
1da177e4 | 897 | { |
4a4d2cfd | 898 | struct snd_card *card = dummy->card; |
16e43467 | 899 | struct snd_kcontrol *kcontrol; |
1da177e4 LT |
900 | unsigned int idx; |
901 | int err; | |
902 | ||
1da177e4 LT |
903 | spin_lock_init(&dummy->mixer_lock); |
904 | strcpy(card->mixername, "Dummy Mixer"); | |
16e43467 | 905 | dummy->iobox = 1; |
1da177e4 LT |
906 | |
907 | for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) { | |
16e43467 CL |
908 | kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy); |
909 | err = snd_ctl_add(card, kcontrol); | |
1a11cb64 | 910 | if (err < 0) |
1da177e4 | 911 | return err; |
16e43467 CL |
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 | ||
1da177e4 LT |
917 | } |
918 | return 0; | |
919 | } | |
920 | ||
9b151fec TI |
921 | #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_PROC_FS) |
922 | /* | |
923 | * proc interface | |
924 | */ | |
d5e1ca05 JK |
925 | static void print_formats(struct snd_dummy *dummy, |
926 | struct snd_info_buffer *buffer) | |
9b151fec TI |
927 | { |
928 | int i; | |
929 | ||
930 | for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) { | |
d5e1ca05 | 931 | if (dummy->pcm_hw.formats & (1ULL << i)) |
9b151fec TI |
932 | snd_iprintf(buffer, " %s", snd_pcm_format_name(i)); |
933 | } | |
934 | } | |
935 | ||
d5e1ca05 JK |
936 | static void print_rates(struct snd_dummy *dummy, |
937 | struct snd_info_buffer *buffer) | |
9b151fec TI |
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 | ||
d5e1ca05 | 945 | if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS) |
9b151fec | 946 | snd_iprintf(buffer, " continuous"); |
d5e1ca05 | 947 | if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT) |
9b151fec TI |
948 | snd_iprintf(buffer, " knot"); |
949 | for (i = 0; i < ARRAY_SIZE(rates); i++) | |
d5e1ca05 | 950 | if (dummy->pcm_hw.rates & (1 << i)) |
9b151fec TI |
951 | snd_iprintf(buffer, " %d", rates[i]); |
952 | } | |
953 | ||
d5e1ca05 JK |
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)) | |
9b151fec TI |
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 | ||
985 | static void dummy_proc_read(struct snd_info_entry *entry, | |
986 | struct snd_info_buffer *buffer) | |
987 | { | |
d5e1ca05 | 988 | struct snd_dummy *dummy = entry->private_data; |
9b151fec TI |
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, | |
d5e1ca05 | 995 | *get_dummy_int_ptr(dummy, fields[i].offset)); |
9b151fec TI |
996 | else |
997 | snd_iprintf(buffer, fields[i].format, | |
d5e1ca05 | 998 | *get_dummy_ll_ptr(dummy, fields[i].offset)); |
9b151fec | 999 | if (!strcmp(fields[i].name, "formats")) |
d5e1ca05 | 1000 | print_formats(dummy, buffer); |
9b151fec | 1001 | else if (!strcmp(fields[i].name, "rates")) |
d5e1ca05 | 1002 | print_rates(dummy, buffer); |
9b151fec TI |
1003 | snd_iprintf(buffer, "\n"); |
1004 | } | |
1005 | } | |
1006 | ||
1007 | static void dummy_proc_write(struct snd_info_entry *entry, | |
1008 | struct snd_info_buffer *buffer) | |
1009 | { | |
d5e1ca05 | 1010 | struct snd_dummy *dummy = entry->private_data; |
9b151fec TI |
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 (strict_strtoull(item, 0, &val)) | |
1028 | continue; | |
1029 | if (fields[i].size == sizeof(int)) | |
d5e1ca05 | 1030 | *get_dummy_int_ptr(dummy, fields[i].offset) = val; |
9b151fec | 1031 | else |
d5e1ca05 | 1032 | *get_dummy_ll_ptr(dummy, fields[i].offset) = val; |
9b151fec TI |
1033 | } |
1034 | } | |
1035 | ||
fbbb01a1 | 1036 | static void dummy_proc_init(struct snd_dummy *chip) |
9b151fec TI |
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; | |
d5e1ca05 | 1044 | entry->private_data = chip; |
9b151fec TI |
1045 | } |
1046 | } | |
1047 | #else | |
1048 | #define dummy_proc_init(x) | |
1049 | #endif /* CONFIG_SND_DEBUG && CONFIG_PROC_FS */ | |
1050 | ||
fbbb01a1 | 1051 | static int snd_dummy_probe(struct platform_device *devptr) |
1da177e4 | 1052 | { |
4a4d2cfd TI |
1053 | struct snd_card *card; |
1054 | struct snd_dummy *dummy; | |
d5e1ca05 | 1055 | struct dummy_model *m = NULL, **mdl; |
1da177e4 | 1056 | int idx, err; |
6e65c1cc | 1057 | int dev = devptr->id; |
1da177e4 | 1058 | |
bd7dd77c TI |
1059 | err = snd_card_create(index[dev], id[dev], THIS_MODULE, |
1060 | sizeof(struct snd_dummy), &card); | |
1061 | if (err < 0) | |
1062 | return err; | |
4a4d2cfd | 1063 | dummy = card->private_data; |
1da177e4 | 1064 | dummy->card = card; |
d5e1ca05 JK |
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 | } | |
1da177e4 LT |
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; | |
1a11cb64 JK |
1079 | err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]); |
1080 | if (err < 0) | |
1da177e4 LT |
1081 | goto __nodev; |
1082 | } | |
d5e1ca05 JK |
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 | ||
1a11cb64 JK |
1110 | err = snd_card_dummy_new_mixer(dummy); |
1111 | if (err < 0) | |
1da177e4 LT |
1112 | goto __nodev; |
1113 | strcpy(card->driver, "Dummy"); | |
1114 | strcpy(card->shortname, "Dummy"); | |
1115 | sprintf(card->longname, "Dummy %i", dev + 1); | |
16dab54b | 1116 | |
9b151fec TI |
1117 | dummy_proc_init(dummy); |
1118 | ||
6e65c1cc | 1119 | snd_card_set_dev(card, &devptr->dev); |
16dab54b | 1120 | |
1a11cb64 JK |
1121 | err = snd_card_register(card); |
1122 | if (err == 0) { | |
6e65c1cc | 1123 | platform_set_drvdata(devptr, card); |
1da177e4 LT |
1124 | return 0; |
1125 | } | |
1126 | __nodev: | |
1127 | snd_card_free(card); | |
1128 | return err; | |
1129 | } | |
1130 | ||
fbbb01a1 | 1131 | static int snd_dummy_remove(struct platform_device *devptr) |
6e65c1cc TI |
1132 | { |
1133 | snd_card_free(platform_get_drvdata(devptr)); | |
1134 | platform_set_drvdata(devptr, NULL); | |
1135 | return 0; | |
1136 | } | |
1137 | ||
d34e4e00 | 1138 | #ifdef CONFIG_PM_SLEEP |
284e7ca7 | 1139 | static int snd_dummy_suspend(struct device *pdev) |
1da177e4 | 1140 | { |
284e7ca7 | 1141 | struct snd_card *card = dev_get_drvdata(pdev); |
6e65c1cc | 1142 | struct snd_dummy *dummy = card->private_data; |
1da177e4 | 1143 | |
6e65c1cc TI |
1144 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
1145 | snd_pcm_suspend_all(dummy->pcm); | |
1146 | return 0; | |
1147 | } | |
1148 | ||
284e7ca7 | 1149 | static int snd_dummy_resume(struct device *pdev) |
6e65c1cc | 1150 | { |
284e7ca7 | 1151 | struct snd_card *card = dev_get_drvdata(pdev); |
6e65c1cc TI |
1152 | |
1153 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); | |
1154 | return 0; | |
1155 | } | |
284e7ca7 TI |
1156 | |
1157 | static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume); | |
1158 | #define SND_DUMMY_PM_OPS &snd_dummy_pm | |
1159 | #else | |
1160 | #define SND_DUMMY_PM_OPS NULL | |
6e65c1cc TI |
1161 | #endif |
1162 | ||
1163 | #define SND_DUMMY_DRIVER "snd_dummy" | |
1164 | ||
1165 | static struct platform_driver snd_dummy_driver = { | |
1166 | .probe = snd_dummy_probe, | |
fbbb01a1 | 1167 | .remove = snd_dummy_remove, |
6e65c1cc | 1168 | .driver = { |
8bf01d8a TI |
1169 | .name = SND_DUMMY_DRIVER, |
1170 | .owner = THIS_MODULE, | |
284e7ca7 | 1171 | .pm = SND_DUMMY_PM_OPS, |
6e65c1cc TI |
1172 | }, |
1173 | }; | |
1174 | ||
c12aad6e | 1175 | static void snd_dummy_unregister_all(void) |
f7a9275d CL |
1176 | { |
1177 | int i; | |
1178 | ||
1179 | for (i = 0; i < ARRAY_SIZE(devices); ++i) | |
1180 | platform_device_unregister(devices[i]); | |
1181 | platform_driver_unregister(&snd_dummy_driver); | |
a68c4d11 | 1182 | free_fake_buffer(); |
f7a9275d CL |
1183 | } |
1184 | ||
6e65c1cc TI |
1185 | static int __init alsa_card_dummy_init(void) |
1186 | { | |
1187 | int i, cards, err; | |
1188 | ||
1a11cb64 JK |
1189 | err = platform_driver_register(&snd_dummy_driver); |
1190 | if (err < 0) | |
6e65c1cc TI |
1191 | return err; |
1192 | ||
a68c4d11 TI |
1193 | err = alloc_fake_buffer(); |
1194 | if (err < 0) { | |
1195 | platform_driver_unregister(&snd_dummy_driver); | |
1196 | return err; | |
1197 | } | |
1198 | ||
6e65c1cc | 1199 | cards = 0; |
8278ca8f | 1200 | for (i = 0; i < SNDRV_CARDS; i++) { |
6e65c1cc | 1201 | struct platform_device *device; |
8278ca8f TI |
1202 | if (! enable[i]) |
1203 | continue; | |
6e65c1cc TI |
1204 | device = platform_device_register_simple(SND_DUMMY_DRIVER, |
1205 | i, NULL, 0); | |
a182ee98 RH |
1206 | if (IS_ERR(device)) |
1207 | continue; | |
7152447d RH |
1208 | if (!platform_get_drvdata(device)) { |
1209 | platform_device_unregister(device); | |
1210 | continue; | |
1211 | } | |
f7a9275d | 1212 | devices[i] = device; |
1da177e4 LT |
1213 | cards++; |
1214 | } | |
1215 | if (!cards) { | |
1216 | #ifdef MODULE | |
1217 | printk(KERN_ERR "Dummy soundcard not found or device busy\n"); | |
1218 | #endif | |
a182ee98 RH |
1219 | snd_dummy_unregister_all(); |
1220 | return -ENODEV; | |
1da177e4 LT |
1221 | } |
1222 | return 0; | |
1223 | } | |
1224 | ||
1225 | static void __exit alsa_card_dummy_exit(void) | |
1226 | { | |
f7a9275d | 1227 | snd_dummy_unregister_all(); |
1da177e4 LT |
1228 | } |
1229 | ||
1230 | module_init(alsa_card_dummy_init) | |
1231 | module_exit(alsa_card_dummy_exit) |