]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - sound/drivers/aloop.c
ALSA: snd-aloop - fix issue in the timer start function
[mirror_ubuntu-artful-kernel.git] / sound / drivers / aloop.c
CommitLineData
597603d6
JK
1/*
2 * Loopback soundcard
3 *
4 * Original code:
5 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 *
7 * More accurate positioning and full-duplex support:
8 * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
9 *
10 * Major (almost complete) rewrite:
11 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
12 *
13 * A next major update in 2010 (separate timers for playback and capture):
14 * Copyright (c) Jaroslav Kysela <perex@perex.cz>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 */
31
32#include <linux/init.h>
33#include <linux/jiffies.h>
34#include <linux/slab.h>
35#include <linux/time.h>
36#include <linux/wait.h>
37#include <linux/moduleparam.h>
38#include <linux/platform_device.h>
39#include <sound/core.h>
40#include <sound/control.h>
41#include <sound/pcm.h>
42#include <sound/initval.h>
43
44MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
45MODULE_DESCRIPTION("A loopback soundcard");
46MODULE_LICENSE("GPL");
47MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
48
49#define MAX_PCM_SUBSTREAMS 8
50
51static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
52static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
53static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
54static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
55static int pcm_notify[SNDRV_CARDS];
56
57module_param_array(index, int, NULL, 0444);
58MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
59module_param_array(id, charp, NULL, 0444);
60MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
61module_param_array(enable, bool, NULL, 0444);
62MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
63module_param_array(pcm_substreams, int, NULL, 0444);
64MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
65module_param_array(pcm_notify, int, NULL, 0444);
66MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
67
68#define NO_PITCH 100000
69
70struct loopback_pcm;
71
72struct loopback_cable {
73 spinlock_t lock;
74 struct loopback_pcm *streams[2];
75 struct snd_pcm_hardware hw;
76 /* flags */
77 unsigned int valid;
78 unsigned int running;
79};
80
81struct loopback_setup {
82 unsigned int notify: 1;
83 unsigned int rate_shift;
84 unsigned int format;
85 unsigned int rate;
86 unsigned int channels;
87 struct snd_ctl_elem_id active_id;
88 struct snd_ctl_elem_id format_id;
89 struct snd_ctl_elem_id rate_id;
90 struct snd_ctl_elem_id channels_id;
91};
92
93struct loopback {
94 struct snd_card *card;
95 struct mutex cable_lock;
96 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
97 struct snd_pcm *pcm[2];
98 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
99};
100
101struct loopback_pcm {
102 struct loopback *loopback;
103 struct snd_pcm_substream *substream;
104 struct loopback_cable *cable;
105 unsigned int pcm_buffer_size;
106 unsigned int buf_pos; /* position in buffer */
107 unsigned int silent_size;
108 /* PCM parameters */
109 unsigned int pcm_period_size;
110 unsigned int pcm_bps; /* bytes per second */
111 unsigned int pcm_salign; /* bytes per sample * channels */
112 unsigned int pcm_rate_shift; /* rate shift value */
113 /* flags */
114 unsigned int period_update_pending :1;
115 /* timer stuff */
116 unsigned int irq_pos; /* fractional IRQ position */
117 unsigned int period_size_frac;
118 unsigned long last_jiffies;
119 struct timer_list timer;
120};
121
122static struct platform_device *devices[SNDRV_CARDS];
123
124static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
125{
126 if (dpcm->pcm_rate_shift == NO_PITCH) {
127 x /= HZ;
128 } else {
129 x = div_u64(NO_PITCH * (unsigned long long)x,
130 HZ * (unsigned long long)dpcm->pcm_rate_shift);
131 }
132 return x - (x % dpcm->pcm_salign);
133}
134
135static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
136{
137 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
138 return x * HZ;
139 } else {
140 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
141 NO_PITCH);
142 }
143 return x;
144}
145
146static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
147{
148 int device = dpcm->substream->pstr->pcm->device;
149
150 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
151 device ^= 1;
152 return &dpcm->loopback->setup[dpcm->substream->number][device];
153}
154
155static inline unsigned int get_notify(struct loopback_pcm *dpcm)
156{
157 return get_setup(dpcm)->notify;
158}
159
160static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
161{
162 return get_setup(dpcm)->rate_shift;
163}
164
165static void loopback_timer_start(struct loopback_pcm *dpcm)
166{
167 unsigned long tick;
168 unsigned int rate_shift = get_rate_shift(dpcm);
169
170 if (rate_shift != dpcm->pcm_rate_shift) {
171 dpcm->pcm_rate_shift = rate_shift;
172 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
173 }
0db71023
JK
174 if (dpcm->period_size_frac <= dpcm->irq_pos) {
175 dpcm->irq_pos %= dpcm->period_size_frac;
176 dpcm->period_update_pending = 1;
177 }
597603d6
JK
178 tick = dpcm->period_size_frac - dpcm->irq_pos;
179 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
180 dpcm->timer.expires = jiffies + tick;
181 add_timer(&dpcm->timer);
182}
183
184static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
185{
186 del_timer(&dpcm->timer);
187}
188
189#define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
190#define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
191#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
192
193static int loopback_check_format(struct loopback_cable *cable, int stream)
194{
b1c73fc8 195 struct snd_pcm_runtime *runtime, *cruntime;
597603d6
JK
196 struct loopback_setup *setup;
197 struct snd_card *card;
198 int check;
199
200 if (cable->valid != CABLE_VALID_BOTH) {
201 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
202 goto __notify;
203 return 0;
204 }
205 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
206 substream->runtime;
b1c73fc8
JK
207 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
208 substream->runtime;
209 check = runtime->format != cruntime->format ||
210 runtime->rate != cruntime->rate ||
211 runtime->channels != cruntime->channels;
597603d6
JK
212 if (!check)
213 return 0;
214 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
215 return -EIO;
216 } else {
217 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
218 substream, SNDRV_PCM_STATE_DRAINING);
219 __notify:
220 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
221 substream->runtime;
222 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
223 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
224 if (setup->format != runtime->format) {
225 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
226 &setup->format_id);
227 setup->format = runtime->format;
228 }
229 if (setup->rate != runtime->rate) {
230 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
231 &setup->rate_id);
232 setup->rate = runtime->rate;
233 }
234 if (setup->channels != runtime->channels) {
235 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
236 &setup->channels_id);
237 setup->channels = runtime->channels;
238 }
239 }
240 return 0;
241}
242
243static void loopback_active_notify(struct loopback_pcm *dpcm)
244{
245 snd_ctl_notify(dpcm->loopback->card,
246 SNDRV_CTL_EVENT_MASK_VALUE,
247 &get_setup(dpcm)->active_id);
248}
249
250static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
251{
252 struct snd_pcm_runtime *runtime = substream->runtime;
253 struct loopback_pcm *dpcm = runtime->private_data;
254 struct loopback_cable *cable = dpcm->cable;
255 int err;
256
257 switch (cmd) {
258 case SNDRV_PCM_TRIGGER_START:
259 err = loopback_check_format(cable, substream->stream);
260 if (err < 0)
261 return err;
262 dpcm->last_jiffies = jiffies;
263 dpcm->pcm_rate_shift = 0;
264 loopback_timer_start(dpcm);
265 cable->running |= (1 << substream->stream);
266 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
267 loopback_active_notify(dpcm);
268 break;
269 case SNDRV_PCM_TRIGGER_STOP:
270 cable->running &= ~(1 << substream->stream);
271 loopback_timer_stop(dpcm);
272 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
273 loopback_active_notify(dpcm);
274 break;
275 default:
276 return -EINVAL;
277 }
278 return 0;
279}
280
b1c73fc8
JK
281static void params_change_substream(struct loopback_pcm *dpcm,
282 struct snd_pcm_runtime *runtime)
283{
284 struct snd_pcm_runtime *dst_runtime;
285
286 if (dpcm == NULL || dpcm->substream == NULL)
287 return;
288 dst_runtime = dpcm->substream->runtime;
289 if (dst_runtime == NULL)
290 return;
291 dst_runtime->hw = dpcm->cable->hw;
292}
293
294static void params_change(struct snd_pcm_substream *substream)
295{
296 struct snd_pcm_runtime *runtime = substream->runtime;
297 struct loopback_pcm *dpcm = runtime->private_data;
298 struct loopback_cable *cable = dpcm->cable;
299
300 cable->hw.formats = (1ULL << runtime->format);
301 cable->hw.rate_min = runtime->rate;
302 cable->hw.rate_max = runtime->rate;
303 cable->hw.channels_min = runtime->channels;
304 cable->hw.channels_max = runtime->channels;
305 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
306 runtime);
307 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
308 runtime);
309}
310
597603d6
JK
311static int loopback_prepare(struct snd_pcm_substream *substream)
312{
313 struct snd_pcm_runtime *runtime = substream->runtime;
314 struct loopback_pcm *dpcm = runtime->private_data;
315 struct loopback_cable *cable = dpcm->cable;
b1c73fc8 316 int bps, salign;
597603d6
JK
317
318 salign = (snd_pcm_format_width(runtime->format) *
319 runtime->channels) / 8;
320 bps = salign * runtime->rate;
321 if (bps <= 0 || salign <= 0)
322 return -EINVAL;
323
324 dpcm->buf_pos = 0;
325 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
326 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
327 /* clear capture buffer */
328 dpcm->silent_size = dpcm->pcm_buffer_size;
329 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
330 runtime->buffer_size * runtime->channels);
331 }
332
333 dpcm->irq_pos = 0;
334 dpcm->period_update_pending = 0;
335 dpcm->pcm_bps = bps;
336 dpcm->pcm_salign = salign;
337 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
338
339 mutex_lock(&dpcm->loopback->cable_lock);
b1c73fc8
JK
340 if (!(cable->valid & ~(1 << substream->stream)) ||
341 (get_setup(dpcm)->notify &&
342 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
343 params_change(substream);
597603d6
JK
344 cable->valid |= 1 << substream->stream;
345 mutex_unlock(&dpcm->loopback->cable_lock);
346
347 return 0;
348}
349
350static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
351{
352 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
353 char *dst = runtime->dma_area;
354 unsigned int dst_off = dpcm->buf_pos;
355
356 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
357 return;
358 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
359 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
360
361 for (;;) {
362 unsigned int size = bytes;
363 if (dst_off + size > dpcm->pcm_buffer_size)
364 size = dpcm->pcm_buffer_size - dst_off;
365 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
366 bytes_to_frames(runtime, size) *
367 runtime->channels);
368 dpcm->silent_size += size;
369 bytes -= size;
370 if (!bytes)
371 break;
372 dst_off = 0;
373 }
374}
375
376static void copy_play_buf(struct loopback_pcm *play,
377 struct loopback_pcm *capt,
378 unsigned int bytes)
379{
380 struct snd_pcm_runtime *runtime = play->substream->runtime;
20d9a26d 381 char *src = runtime->dma_area;
597603d6
JK
382 char *dst = capt->substream->runtime->dma_area;
383 unsigned int src_off = play->buf_pos;
384 unsigned int dst_off = capt->buf_pos;
385 unsigned int clear_bytes = 0;
386
387 /* check if playback is draining, trim the capture copy size
388 * when our pointer is at the end of playback ring buffer */
389 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
390 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
391 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
392 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
393 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
394 appl_ptr1 += play->buf_pos / play->pcm_salign;
395 if (appl_ptr < appl_ptr1)
396 appl_ptr1 -= runtime->buffer_size;
397 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
398 if (diff < bytes) {
399 clear_bytes = bytes - diff;
400 bytes = diff;
401 }
402 }
403
404 for (;;) {
405 unsigned int size = bytes;
406 if (src_off + size > play->pcm_buffer_size)
407 size = play->pcm_buffer_size - src_off;
408 if (dst_off + size > capt->pcm_buffer_size)
409 size = capt->pcm_buffer_size - dst_off;
410 memcpy(dst + dst_off, src + src_off, size);
411 capt->silent_size = 0;
412 bytes -= size;
413 if (!bytes)
414 break;
415 src_off = (src_off + size) % play->pcm_buffer_size;
416 dst_off = (dst_off + size) % capt->pcm_buffer_size;
417 }
418
20d9a26d 419 if (clear_bytes > 0) {
597603d6 420 clear_capture_buf(capt, clear_bytes);
20d9a26d
JK
421 capt->silent_size = 0;
422 }
597603d6
JK
423}
424
425#define BYTEPOS_UPDATE_POSONLY 0
426#define BYTEPOS_UPDATE_CLEAR 1
427#define BYTEPOS_UPDATE_COPY 2
428
429static void loopback_bytepos_update(struct loopback_pcm *dpcm,
430 unsigned int delta,
431 unsigned int cmd)
432{
433 unsigned int count;
434 unsigned long last_pos;
435
436 last_pos = byte_pos(dpcm, dpcm->irq_pos);
437 dpcm->irq_pos += delta * dpcm->pcm_bps;
438 count = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
439 if (!count)
440 return;
441 if (cmd == BYTEPOS_UPDATE_CLEAR)
442 clear_capture_buf(dpcm, count);
443 else if (cmd == BYTEPOS_UPDATE_COPY)
444 copy_play_buf(dpcm->cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
445 dpcm->cable->streams[SNDRV_PCM_STREAM_CAPTURE],
446 count);
447 dpcm->buf_pos += count;
448 dpcm->buf_pos %= dpcm->pcm_buffer_size;
449 if (dpcm->irq_pos >= dpcm->period_size_frac) {
450 dpcm->irq_pos %= dpcm->period_size_frac;
451 dpcm->period_update_pending = 1;
452 }
453}
454
455static void loopback_pos_update(struct loopback_cable *cable)
456{
457 struct loopback_pcm *dpcm_play =
458 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
459 struct loopback_pcm *dpcm_capt =
460 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
461 unsigned long delta_play = 0, delta_capt = 0;
462
463 spin_lock(&cable->lock);
464 if (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
465 delta_play = jiffies - dpcm_play->last_jiffies;
466 dpcm_play->last_jiffies += delta_play;
467 }
468
469 if (cable->running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
470 delta_capt = jiffies - dpcm_capt->last_jiffies;
471 dpcm_capt->last_jiffies += delta_capt;
472 }
473
474 if (delta_play == 0 && delta_capt == 0) {
475 spin_unlock(&cable->lock);
476 return;
477 }
478
479 if (delta_play > delta_capt) {
480 loopback_bytepos_update(dpcm_play, delta_play - delta_capt,
481 BYTEPOS_UPDATE_POSONLY);
482 delta_play = delta_capt;
483 } else if (delta_play < delta_capt) {
484 loopback_bytepos_update(dpcm_capt, delta_capt - delta_play,
485 BYTEPOS_UPDATE_CLEAR);
486 delta_capt = delta_play;
487 }
488
489 if (delta_play == 0 && delta_capt == 0) {
490 spin_unlock(&cable->lock);
491 return;
492 }
493 /* note delta_capt == delta_play at this moment */
494 loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
495 loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
496 spin_unlock(&cable->lock);
497}
498
499static void loopback_timer_function(unsigned long data)
500{
501 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
502 int stream;
503
504 loopback_pos_update(dpcm->cable);
505 stream = dpcm->substream->stream;
506 if (dpcm->cable->running & (1 << stream))
507 loopback_timer_start(dpcm);
508 if (dpcm->period_update_pending) {
509 dpcm->period_update_pending = 0;
510 if (dpcm->cable->running & (1 << stream))
511 snd_pcm_period_elapsed(dpcm->substream);
512 }
513}
514
515static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
516{
517 struct snd_pcm_runtime *runtime = substream->runtime;
518 struct loopback_pcm *dpcm = runtime->private_data;
519
520 loopback_pos_update(dpcm->cable);
521 return bytes_to_frames(runtime, dpcm->buf_pos);
522}
523
524static struct snd_pcm_hardware loopback_pcm_hardware =
525{
526 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
527 SNDRV_PCM_INFO_MMAP_VALID),
528 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
529 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
530 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
531 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
532 .rate_min = 8000,
533 .rate_max = 192000,
534 .channels_min = 1,
535 .channels_max = 32,
536 .buffer_bytes_max = 2 * 1024 * 1024,
537 .period_bytes_min = 64,
0db71023
JK
538 /* note check overflow in frac_pos() using pcm_rate_shift before
539 changing period_bytes_max value */
540 .period_bytes_max = 1024 * 1024,
597603d6
JK
541 .periods_min = 1,
542 .periods_max = 1024,
543 .fifo_size = 0,
544};
545
546static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
547{
548 struct loopback_pcm *dpcm = runtime->private_data;
549 kfree(dpcm);
550}
551
552static int loopback_hw_params(struct snd_pcm_substream *substream,
553 struct snd_pcm_hw_params *params)
554{
555 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
556}
557
558static int loopback_hw_free(struct snd_pcm_substream *substream)
559{
560 struct snd_pcm_runtime *runtime = substream->runtime;
561 struct loopback_pcm *dpcm = runtime->private_data;
562 struct loopback_cable *cable = dpcm->cable;
563
564 mutex_lock(&dpcm->loopback->cable_lock);
565 cable->valid &= ~(1 << substream->stream);
566 mutex_unlock(&dpcm->loopback->cable_lock);
567 return snd_pcm_lib_free_pages(substream);
568}
569
570static unsigned int get_cable_index(struct snd_pcm_substream *substream)
571{
572 if (!substream->pcm->device)
573 return substream->stream;
574 else
575 return !substream->stream;
576}
577
b1c73fc8
JK
578static int rule_format(struct snd_pcm_hw_params *params,
579 struct snd_pcm_hw_rule *rule)
580{
581
582 struct snd_pcm_hardware *hw = rule->private;
583 struct snd_mask *maskp = hw_param_mask(params, rule->var);
584
585 maskp->bits[0] &= (u_int32_t)hw->formats;
586 maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
587 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
588 if (! maskp->bits[0] && ! maskp->bits[1])
589 return -EINVAL;
590 return 0;
591}
592
593static int rule_rate(struct snd_pcm_hw_params *params,
594 struct snd_pcm_hw_rule *rule)
595{
596 struct snd_pcm_hardware *hw = rule->private;
597 struct snd_interval t;
598
599 t.min = hw->rate_min;
600 t.max = hw->rate_max;
601 t.openmin = t.openmax = 0;
602 t.integer = 0;
603 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
604}
605
606static int rule_channels(struct snd_pcm_hw_params *params,
607 struct snd_pcm_hw_rule *rule)
608{
609 struct snd_pcm_hardware *hw = rule->private;
610 struct snd_interval t;
611
612 t.min = hw->channels_min;
613 t.max = hw->channels_max;
614 t.openmin = t.openmax = 0;
615 t.integer = 0;
616 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
617}
618
597603d6
JK
619static int loopback_open(struct snd_pcm_substream *substream)
620{
621 struct snd_pcm_runtime *runtime = substream->runtime;
622 struct loopback *loopback = substream->private_data;
623 struct loopback_pcm *dpcm;
624 struct loopback_cable *cable;
625 int err = 0;
626 int dev = get_cable_index(substream);
627
628 mutex_lock(&loopback->cable_lock);
629 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
630 if (!dpcm) {
631 err = -ENOMEM;
632 goto unlock;
633 }
634 dpcm->loopback = loopback;
635 dpcm->substream = substream;
636 setup_timer(&dpcm->timer, loopback_timer_function,
637 (unsigned long)dpcm);
638
639 cable = loopback->cables[substream->number][dev];
640 if (!cable) {
641 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
642 if (!cable) {
643 kfree(dpcm);
644 err = -ENOMEM;
645 goto unlock;
646 }
647 spin_lock_init(&cable->lock);
648 cable->hw = loopback_pcm_hardware;
649 loopback->cables[substream->number][dev] = cable;
650 }
651 dpcm->cable = cable;
652 cable->streams[substream->stream] = dpcm;
653
654 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
655
b1c73fc8
JK
656 /* use dynamic rules based on actual runtime->hw values */
657 /* note that the default rules created in the PCM midlevel code */
658 /* are cached -> they do not reflect the actual state */
659 err = snd_pcm_hw_rule_add(runtime, 0,
660 SNDRV_PCM_HW_PARAM_FORMAT,
661 rule_format, &runtime->hw,
662 SNDRV_PCM_HW_PARAM_FORMAT, -1);
663 if (err < 0)
664 goto unlock;
665 err = snd_pcm_hw_rule_add(runtime, 0,
666 SNDRV_PCM_HW_PARAM_RATE,
667 rule_rate, &runtime->hw,
668 SNDRV_PCM_HW_PARAM_RATE, -1);
669 if (err < 0)
670 goto unlock;
671 err = snd_pcm_hw_rule_add(runtime, 0,
672 SNDRV_PCM_HW_PARAM_CHANNELS,
673 rule_channels, &runtime->hw,
674 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
675 if (err < 0)
676 goto unlock;
677
597603d6
JK
678 runtime->private_data = dpcm;
679 runtime->private_free = loopback_runtime_free;
b1c73fc8 680 if (get_notify(dpcm))
597603d6 681 runtime->hw = loopback_pcm_hardware;
b1c73fc8 682 else
597603d6 683 runtime->hw = cable->hw;
597603d6
JK
684 unlock:
685 mutex_unlock(&loopback->cable_lock);
686 return err;
687}
688
689static int loopback_close(struct snd_pcm_substream *substream)
690{
691 struct loopback *loopback = substream->private_data;
692 struct loopback_pcm *dpcm = substream->runtime->private_data;
693 struct loopback_cable *cable;
694 int dev = get_cable_index(substream);
695
696 loopback_timer_stop(dpcm);
697 mutex_lock(&loopback->cable_lock);
698 cable = loopback->cables[substream->number][dev];
699 if (cable->streams[!substream->stream]) {
700 /* other stream is still alive */
701 cable->streams[substream->stream] = NULL;
702 } else {
703 /* free the cable */
704 loopback->cables[substream->number][dev] = NULL;
705 kfree(cable);
706 }
707 mutex_unlock(&loopback->cable_lock);
708 return 0;
709}
710
711static struct snd_pcm_ops loopback_playback_ops = {
712 .open = loopback_open,
713 .close = loopback_close,
714 .ioctl = snd_pcm_lib_ioctl,
715 .hw_params = loopback_hw_params,
716 .hw_free = loopback_hw_free,
717 .prepare = loopback_prepare,
718 .trigger = loopback_trigger,
719 .pointer = loopback_pointer,
720};
721
722static struct snd_pcm_ops loopback_capture_ops = {
723 .open = loopback_open,
724 .close = loopback_close,
725 .ioctl = snd_pcm_lib_ioctl,
726 .hw_params = loopback_hw_params,
727 .hw_free = loopback_hw_free,
728 .prepare = loopback_prepare,
729 .trigger = loopback_trigger,
730 .pointer = loopback_pointer,
731};
732
733static int __devinit loopback_pcm_new(struct loopback *loopback,
734 int device, int substreams)
735{
736 struct snd_pcm *pcm;
737 int err;
738
739 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
740 substreams, substreams, &pcm);
741 if (err < 0)
742 return err;
743 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
744 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
745
746 pcm->private_data = loopback;
747 pcm->info_flags = 0;
748 strcpy(pcm->name, "Loopback PCM");
749
750 loopback->pcm[device] = pcm;
751
752 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
753 snd_dma_continuous_data(GFP_KERNEL),
754 0, 2 * 1024 * 1024);
755 return 0;
756}
757
758static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
759 struct snd_ctl_elem_info *uinfo)
760{
761 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
762 uinfo->count = 1;
763 uinfo->value.integer.min = 80000;
764 uinfo->value.integer.max = 120000;
765 uinfo->value.integer.step = 1;
766 return 0;
767}
768
769static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
770 struct snd_ctl_elem_value *ucontrol)
771{
772 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
773
774 ucontrol->value.integer.value[0] =
775 loopback->setup[kcontrol->id.subdevice]
776 [kcontrol->id.device].rate_shift;
777 return 0;
778}
779
780static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
781 struct snd_ctl_elem_value *ucontrol)
782{
783 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
784 unsigned int val;
785 int change = 0;
786
787 val = ucontrol->value.integer.value[0];
788 if (val < 80000)
789 val = 80000;
790 if (val > 120000)
791 val = 120000;
792 mutex_lock(&loopback->cable_lock);
793 if (val != loopback->setup[kcontrol->id.subdevice]
794 [kcontrol->id.device].rate_shift) {
795 loopback->setup[kcontrol->id.subdevice]
796 [kcontrol->id.device].rate_shift = val;
797 change = 1;
798 }
799 mutex_unlock(&loopback->cable_lock);
800 return change;
801}
802
803static int loopback_notify_get(struct snd_kcontrol *kcontrol,
804 struct snd_ctl_elem_value *ucontrol)
805{
806 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
807
808 ucontrol->value.integer.value[0] =
809 loopback->setup[kcontrol->id.subdevice]
810 [kcontrol->id.device].notify;
811 return 0;
812}
813
814static int loopback_notify_put(struct snd_kcontrol *kcontrol,
815 struct snd_ctl_elem_value *ucontrol)
816{
817 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
818 unsigned int val;
819 int change = 0;
820
821 val = ucontrol->value.integer.value[0] ? 1 : 0;
822 if (val != loopback->setup[kcontrol->id.subdevice]
823 [kcontrol->id.device].notify) {
824 loopback->setup[kcontrol->id.subdevice]
825 [kcontrol->id.device].notify = val;
826 change = 1;
827 }
828 return change;
829}
830
831static int loopback_active_get(struct snd_kcontrol *kcontrol,
832 struct snd_ctl_elem_value *ucontrol)
833{
834 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
835 struct loopback_cable *cable = loopback->cables
ac446fb7 836 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
597603d6
JK
837 unsigned int val = 0;
838
839 if (cable != NULL)
840 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
841 1 : 0;
842 ucontrol->value.integer.value[0] = val;
843 return 0;
844}
845
846static int loopback_format_info(struct snd_kcontrol *kcontrol,
847 struct snd_ctl_elem_info *uinfo)
848{
849 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
850 uinfo->count = 1;
851 uinfo->value.integer.min = 0;
852 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
853 uinfo->value.integer.step = 1;
854 return 0;
855}
856
857static int loopback_format_get(struct snd_kcontrol *kcontrol,
858 struct snd_ctl_elem_value *ucontrol)
859{
860 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
861
862 ucontrol->value.integer.value[0] =
863 loopback->setup[kcontrol->id.subdevice]
864 [kcontrol->id.device].format;
865 return 0;
866}
867
868static int loopback_rate_info(struct snd_kcontrol *kcontrol,
869 struct snd_ctl_elem_info *uinfo)
870{
871 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
872 uinfo->count = 1;
873 uinfo->value.integer.min = 0;
874 uinfo->value.integer.max = 192000;
875 uinfo->value.integer.step = 1;
876 return 0;
877}
878
879static int loopback_rate_get(struct snd_kcontrol *kcontrol,
880 struct snd_ctl_elem_value *ucontrol)
881{
882 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
883
884 ucontrol->value.integer.value[0] =
885 loopback->setup[kcontrol->id.subdevice]
886 [kcontrol->id.device].rate;
887 return 0;
888}
889
890static int loopback_channels_info(struct snd_kcontrol *kcontrol,
891 struct snd_ctl_elem_info *uinfo)
892{
893 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
894 uinfo->count = 1;
895 uinfo->value.integer.min = 1;
896 uinfo->value.integer.max = 1024;
897 uinfo->value.integer.step = 1;
898 return 0;
899}
900
901static int loopback_channels_get(struct snd_kcontrol *kcontrol,
902 struct snd_ctl_elem_value *ucontrol)
903{
904 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
905
906 ucontrol->value.integer.value[0] =
907 loopback->setup[kcontrol->id.subdevice]
1446c5fb 908 [kcontrol->id.device].channels;
597603d6
JK
909 return 0;
910}
911
912static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
913{
914 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
915 .name = "PCM Rate Shift 100000",
916 .info = loopback_rate_shift_info,
917 .get = loopback_rate_shift_get,
918 .put = loopback_rate_shift_put,
919},
920{
921 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
922 .name = "PCM Notify",
923 .info = snd_ctl_boolean_mono_info,
924 .get = loopback_notify_get,
925 .put = loopback_notify_put,
926},
927#define ACTIVE_IDX 2
928{
929 .access = SNDRV_CTL_ELEM_ACCESS_READ,
930 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
931 .name = "PCM Slave Active",
932 .info = snd_ctl_boolean_mono_info,
933 .get = loopback_active_get,
934},
935#define FORMAT_IDX 3
936{
937 .access = SNDRV_CTL_ELEM_ACCESS_READ,
938 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
939 .name = "PCM Slave Format",
940 .info = loopback_format_info,
941 .get = loopback_format_get
942},
943#define RATE_IDX 4
944{
945 .access = SNDRV_CTL_ELEM_ACCESS_READ,
946 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
947 .name = "PCM Slave Rate",
948 .info = loopback_rate_info,
949 .get = loopback_rate_get
950},
951#define CHANNELS_IDX 5
952{
953 .access = SNDRV_CTL_ELEM_ACCESS_READ,
954 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
955 .name = "PCM Slave Channels",
956 .info = loopback_channels_info,
957 .get = loopback_channels_get
958}
959};
960
961static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
962{
963 struct snd_card *card = loopback->card;
964 struct snd_pcm *pcm;
965 struct snd_kcontrol *kctl;
966 struct loopback_setup *setup;
967 int err, dev, substr, substr_count, idx;
968
969 strcpy(card->mixername, "Loopback Mixer");
970 for (dev = 0; dev < 2; dev++) {
971 pcm = loopback->pcm[dev];
972 substr_count =
973 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
974 for (substr = 0; substr < substr_count; substr++) {
975 setup = &loopback->setup[substr][dev];
976 setup->notify = notify;
977 setup->rate_shift = NO_PITCH;
978 setup->format = SNDRV_PCM_FORMAT_S16_LE;
979 setup->rate = 48000;
980 setup->channels = 2;
981 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
982 idx++) {
983 kctl = snd_ctl_new1(&loopback_controls[idx],
984 loopback);
985 if (!kctl)
986 return -ENOMEM;
987 kctl->id.device = dev;
988 kctl->id.subdevice = substr;
989 switch (idx) {
990 case ACTIVE_IDX:
991 setup->active_id = kctl->id;
992 break;
993 case FORMAT_IDX:
994 setup->format_id = kctl->id;
995 break;
996 case RATE_IDX:
997 setup->rate_id = kctl->id;
998 break;
999 case CHANNELS_IDX:
1000 setup->channels_id = kctl->id;
1001 break;
1002 default:
1003 break;
1004 }
1005 err = snd_ctl_add(card, kctl);
1006 if (err < 0)
1007 return err;
1008 }
1009 }
1010 }
1011 return 0;
1012}
1013
1014static int __devinit loopback_probe(struct platform_device *devptr)
1015{
1016 struct snd_card *card;
1017 struct loopback *loopback;
1018 int dev = devptr->id;
1019 int err;
1020
1021 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
1022 sizeof(struct loopback), &card);
1023 if (err < 0)
1024 return err;
1025 loopback = card->private_data;
1026
1027 if (pcm_substreams[dev] < 1)
1028 pcm_substreams[dev] = 1;
1029 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1030 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1031
1032 loopback->card = card;
1033 mutex_init(&loopback->cable_lock);
1034
1035 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1036 if (err < 0)
1037 goto __nodev;
1038 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1039 if (err < 0)
1040 goto __nodev;
1041 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1042 if (err < 0)
1043 goto __nodev;
1044 strcpy(card->driver, "Loopback");
1045 strcpy(card->shortname, "Loopback");
1046 sprintf(card->longname, "Loopback %i", dev + 1);
1047 err = snd_card_register(card);
1048 if (!err) {
1049 platform_set_drvdata(devptr, card);
1050 return 0;
1051 }
1052 __nodev:
1053 snd_card_free(card);
1054 return err;
1055}
1056
1057static int __devexit loopback_remove(struct platform_device *devptr)
1058{
1059 snd_card_free(platform_get_drvdata(devptr));
1060 platform_set_drvdata(devptr, NULL);
1061 return 0;
1062}
1063
1064#ifdef CONFIG_PM
1065static int loopback_suspend(struct platform_device *pdev,
1066 pm_message_t state)
1067{
1068 struct snd_card *card = platform_get_drvdata(pdev);
1069 struct loopback *loopback = card->private_data;
1070
1071 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1072
1073 snd_pcm_suspend_all(loopback->pcm[0]);
1074 snd_pcm_suspend_all(loopback->pcm[1]);
1075 return 0;
1076}
1077
1078static int loopback_resume(struct platform_device *pdev)
1079{
1080 struct snd_card *card = platform_get_drvdata(pdev);
1081
1082 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1083 return 0;
1084}
1085#endif
1086
1087#define SND_LOOPBACK_DRIVER "snd_aloop"
1088
1089static struct platform_driver loopback_driver = {
1090 .probe = loopback_probe,
1091 .remove = __devexit_p(loopback_remove),
1092#ifdef CONFIG_PM
1093 .suspend = loopback_suspend,
1094 .resume = loopback_resume,
1095#endif
1096 .driver = {
1097 .name = SND_LOOPBACK_DRIVER
1098 },
1099};
1100
1101static void loopback_unregister_all(void)
1102{
1103 int i;
1104
1105 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1106 platform_device_unregister(devices[i]);
1107 platform_driver_unregister(&loopback_driver);
1108}
1109
1110static int __init alsa_card_loopback_init(void)
1111{
1112 int i, err, cards;
1113
1114 err = platform_driver_register(&loopback_driver);
1115 if (err < 0)
1116 return err;
1117
1118
1119 cards = 0;
1120 for (i = 0; i < SNDRV_CARDS; i++) {
1121 struct platform_device *device;
1122 if (!enable[i])
1123 continue;
1124 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1125 i, NULL, 0);
1126 if (IS_ERR(device))
1127 continue;
1128 if (!platform_get_drvdata(device)) {
1129 platform_device_unregister(device);
1130 continue;
1131 }
1132 devices[i] = device;
1133 cards++;
1134 }
1135 if (!cards) {
1136#ifdef MODULE
1137 printk(KERN_ERR "aloop: No loopback enabled\n");
1138#endif
1139 loopback_unregister_all();
1140 return -ENODEV;
1141 }
1142 return 0;
1143}
1144
1145static void __exit alsa_card_loopback_exit(void)
1146{
1147 loopback_unregister_all();
1148}
1149
1150module_init(alsa_card_loopback_init)
1151module_exit(alsa_card_loopback_exit)