Loading...
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/module.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/info.h>
43#include <sound/initval.h>
44
45MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
46MODULE_DESCRIPTION("A loopback soundcard");
47MODULE_LICENSE("GPL");
48MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
49
50#define MAX_PCM_SUBSTREAMS 8
51
52static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
53static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
54static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
55static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
56static int pcm_notify[SNDRV_CARDS];
57
58module_param_array(index, int, NULL, 0444);
59MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
60module_param_array(id, charp, NULL, 0444);
61MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
62module_param_array(enable, bool, NULL, 0444);
63MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
64module_param_array(pcm_substreams, int, NULL, 0444);
65MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
66module_param_array(pcm_notify, int, NULL, 0444);
67MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
68
69#define NO_PITCH 100000
70
71struct loopback_pcm;
72
73struct loopback_cable {
74 spinlock_t lock;
75 struct loopback_pcm *streams[2];
76 struct snd_pcm_hardware hw;
77 /* flags */
78 unsigned int valid;
79 unsigned int running;
80 unsigned int pause;
81};
82
83struct loopback_setup {
84 unsigned int notify: 1;
85 unsigned int rate_shift;
86 unsigned int format;
87 unsigned int rate;
88 unsigned int channels;
89 struct snd_ctl_elem_id active_id;
90 struct snd_ctl_elem_id format_id;
91 struct snd_ctl_elem_id rate_id;
92 struct snd_ctl_elem_id channels_id;
93};
94
95struct loopback {
96 struct snd_card *card;
97 struct mutex cable_lock;
98 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
99 struct snd_pcm *pcm[2];
100 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
101};
102
103struct loopback_pcm {
104 struct loopback *loopback;
105 struct snd_pcm_substream *substream;
106 struct loopback_cable *cable;
107 unsigned int pcm_buffer_size;
108 unsigned int buf_pos; /* position in buffer */
109 unsigned int silent_size;
110 /* PCM parameters */
111 unsigned int pcm_period_size;
112 unsigned int pcm_bps; /* bytes per second */
113 unsigned int pcm_salign; /* bytes per sample * channels */
114 unsigned int pcm_rate_shift; /* rate shift value */
115 /* flags */
116 unsigned int period_update_pending :1;
117 /* timer stuff */
118 unsigned int irq_pos; /* fractional IRQ position */
119 unsigned int period_size_frac;
120 unsigned int last_drift;
121 unsigned long last_jiffies;
122 struct timer_list timer;
123};
124
125static struct platform_device *devices[SNDRV_CARDS];
126
127static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
128{
129 if (dpcm->pcm_rate_shift == NO_PITCH) {
130 x /= HZ;
131 } else {
132 x = div_u64(NO_PITCH * (unsigned long long)x,
133 HZ * (unsigned long long)dpcm->pcm_rate_shift);
134 }
135 return x - (x % dpcm->pcm_salign);
136}
137
138static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
139{
140 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
141 return x * HZ;
142 } else {
143 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
144 NO_PITCH);
145 }
146 return x;
147}
148
149static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
150{
151 int device = dpcm->substream->pstr->pcm->device;
152
153 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
154 device ^= 1;
155 return &dpcm->loopback->setup[dpcm->substream->number][device];
156}
157
158static inline unsigned int get_notify(struct loopback_pcm *dpcm)
159{
160 return get_setup(dpcm)->notify;
161}
162
163static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
164{
165 return get_setup(dpcm)->rate_shift;
166}
167
168/* call in cable->lock */
169static void loopback_timer_start(struct loopback_pcm *dpcm)
170{
171 unsigned long tick;
172 unsigned int rate_shift = get_rate_shift(dpcm);
173
174 if (rate_shift != dpcm->pcm_rate_shift) {
175 dpcm->pcm_rate_shift = rate_shift;
176 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
177 }
178 if (dpcm->period_size_frac <= dpcm->irq_pos) {
179 dpcm->irq_pos %= dpcm->period_size_frac;
180 dpcm->period_update_pending = 1;
181 }
182 tick = dpcm->period_size_frac - dpcm->irq_pos;
183 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
184 mod_timer(&dpcm->timer, jiffies + tick);
185}
186
187/* call in cable->lock */
188static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
189{
190 del_timer(&dpcm->timer);
191 dpcm->timer.expires = 0;
192}
193
194#define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
195#define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
196#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
197
198static int loopback_check_format(struct loopback_cable *cable, int stream)
199{
200 struct snd_pcm_runtime *runtime, *cruntime;
201 struct loopback_setup *setup;
202 struct snd_card *card;
203 int check;
204
205 if (cable->valid != CABLE_VALID_BOTH) {
206 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
207 goto __notify;
208 return 0;
209 }
210 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
211 substream->runtime;
212 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
213 substream->runtime;
214 check = runtime->format != cruntime->format ||
215 runtime->rate != cruntime->rate ||
216 runtime->channels != cruntime->channels;
217 if (!check)
218 return 0;
219 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
220 return -EIO;
221 } else {
222 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
223 substream, SNDRV_PCM_STATE_DRAINING);
224 __notify:
225 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
226 substream->runtime;
227 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
228 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
229 if (setup->format != runtime->format) {
230 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
231 &setup->format_id);
232 setup->format = runtime->format;
233 }
234 if (setup->rate != runtime->rate) {
235 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
236 &setup->rate_id);
237 setup->rate = runtime->rate;
238 }
239 if (setup->channels != runtime->channels) {
240 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
241 &setup->channels_id);
242 setup->channels = runtime->channels;
243 }
244 }
245 return 0;
246}
247
248static void loopback_active_notify(struct loopback_pcm *dpcm)
249{
250 snd_ctl_notify(dpcm->loopback->card,
251 SNDRV_CTL_EVENT_MASK_VALUE,
252 &get_setup(dpcm)->active_id);
253}
254
255static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
256{
257 struct snd_pcm_runtime *runtime = substream->runtime;
258 struct loopback_pcm *dpcm = runtime->private_data;
259 struct loopback_cable *cable = dpcm->cable;
260 int err, stream = 1 << substream->stream;
261
262 switch (cmd) {
263 case SNDRV_PCM_TRIGGER_START:
264 err = loopback_check_format(cable, substream->stream);
265 if (err < 0)
266 return err;
267 dpcm->last_jiffies = jiffies;
268 dpcm->pcm_rate_shift = 0;
269 dpcm->last_drift = 0;
270 spin_lock(&cable->lock);
271 cable->running |= stream;
272 cable->pause &= ~stream;
273 loopback_timer_start(dpcm);
274 spin_unlock(&cable->lock);
275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
276 loopback_active_notify(dpcm);
277 break;
278 case SNDRV_PCM_TRIGGER_STOP:
279 spin_lock(&cable->lock);
280 cable->running &= ~stream;
281 cable->pause &= ~stream;
282 loopback_timer_stop(dpcm);
283 spin_unlock(&cable->lock);
284 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
285 loopback_active_notify(dpcm);
286 break;
287 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
288 case SNDRV_PCM_TRIGGER_SUSPEND:
289 spin_lock(&cable->lock);
290 cable->pause |= stream;
291 loopback_timer_stop(dpcm);
292 spin_unlock(&cable->lock);
293 break;
294 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
295 case SNDRV_PCM_TRIGGER_RESUME:
296 spin_lock(&cable->lock);
297 dpcm->last_jiffies = jiffies;
298 cable->pause &= ~stream;
299 loopback_timer_start(dpcm);
300 spin_unlock(&cable->lock);
301 break;
302 default:
303 return -EINVAL;
304 }
305 return 0;
306}
307
308static void params_change_substream(struct loopback_pcm *dpcm,
309 struct snd_pcm_runtime *runtime)
310{
311 struct snd_pcm_runtime *dst_runtime;
312
313 if (dpcm == NULL || dpcm->substream == NULL)
314 return;
315 dst_runtime = dpcm->substream->runtime;
316 if (dst_runtime == NULL)
317 return;
318 dst_runtime->hw = dpcm->cable->hw;
319}
320
321static void params_change(struct snd_pcm_substream *substream)
322{
323 struct snd_pcm_runtime *runtime = substream->runtime;
324 struct loopback_pcm *dpcm = runtime->private_data;
325 struct loopback_cable *cable = dpcm->cable;
326
327 cable->hw.formats = pcm_format_to_bits(runtime->format);
328 cable->hw.rate_min = runtime->rate;
329 cable->hw.rate_max = runtime->rate;
330 cable->hw.channels_min = runtime->channels;
331 cable->hw.channels_max = runtime->channels;
332 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
333 runtime);
334 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
335 runtime);
336}
337
338static int loopback_prepare(struct snd_pcm_substream *substream)
339{
340 struct snd_pcm_runtime *runtime = substream->runtime;
341 struct loopback_pcm *dpcm = runtime->private_data;
342 struct loopback_cable *cable = dpcm->cable;
343 int bps, salign;
344
345 salign = (snd_pcm_format_width(runtime->format) *
346 runtime->channels) / 8;
347 bps = salign * runtime->rate;
348 if (bps <= 0 || salign <= 0)
349 return -EINVAL;
350
351 dpcm->buf_pos = 0;
352 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
353 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
354 /* clear capture buffer */
355 dpcm->silent_size = dpcm->pcm_buffer_size;
356 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
357 runtime->buffer_size * runtime->channels);
358 }
359
360 dpcm->irq_pos = 0;
361 dpcm->period_update_pending = 0;
362 dpcm->pcm_bps = bps;
363 dpcm->pcm_salign = salign;
364 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
365
366 mutex_lock(&dpcm->loopback->cable_lock);
367 if (!(cable->valid & ~(1 << substream->stream)) ||
368 (get_setup(dpcm)->notify &&
369 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
370 params_change(substream);
371 cable->valid |= 1 << substream->stream;
372 mutex_unlock(&dpcm->loopback->cable_lock);
373
374 return 0;
375}
376
377static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
378{
379 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
380 char *dst = runtime->dma_area;
381 unsigned int dst_off = dpcm->buf_pos;
382
383 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
384 return;
385 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
386 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
387
388 for (;;) {
389 unsigned int size = bytes;
390 if (dst_off + size > dpcm->pcm_buffer_size)
391 size = dpcm->pcm_buffer_size - dst_off;
392 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
393 bytes_to_frames(runtime, size) *
394 runtime->channels);
395 dpcm->silent_size += size;
396 bytes -= size;
397 if (!bytes)
398 break;
399 dst_off = 0;
400 }
401}
402
403static void copy_play_buf(struct loopback_pcm *play,
404 struct loopback_pcm *capt,
405 unsigned int bytes)
406{
407 struct snd_pcm_runtime *runtime = play->substream->runtime;
408 char *src = runtime->dma_area;
409 char *dst = capt->substream->runtime->dma_area;
410 unsigned int src_off = play->buf_pos;
411 unsigned int dst_off = capt->buf_pos;
412 unsigned int clear_bytes = 0;
413
414 /* check if playback is draining, trim the capture copy size
415 * when our pointer is at the end of playback ring buffer */
416 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
417 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
418 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
419 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
420 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
421 appl_ptr1 += play->buf_pos / play->pcm_salign;
422 if (appl_ptr < appl_ptr1)
423 appl_ptr1 -= runtime->buffer_size;
424 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
425 if (diff < bytes) {
426 clear_bytes = bytes - diff;
427 bytes = diff;
428 }
429 }
430
431 for (;;) {
432 unsigned int size = bytes;
433 if (src_off + size > play->pcm_buffer_size)
434 size = play->pcm_buffer_size - src_off;
435 if (dst_off + size > capt->pcm_buffer_size)
436 size = capt->pcm_buffer_size - dst_off;
437 memcpy(dst + dst_off, src + src_off, size);
438 capt->silent_size = 0;
439 bytes -= size;
440 if (!bytes)
441 break;
442 src_off = (src_off + size) % play->pcm_buffer_size;
443 dst_off = (dst_off + size) % capt->pcm_buffer_size;
444 }
445
446 if (clear_bytes > 0) {
447 clear_capture_buf(capt, clear_bytes);
448 capt->silent_size = 0;
449 }
450}
451
452static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
453 unsigned int jiffies_delta)
454{
455 unsigned long last_pos;
456 unsigned int delta;
457
458 last_pos = byte_pos(dpcm, dpcm->irq_pos);
459 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
460 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
461 if (delta >= dpcm->last_drift)
462 delta -= dpcm->last_drift;
463 dpcm->last_drift = 0;
464 if (dpcm->irq_pos >= dpcm->period_size_frac) {
465 dpcm->irq_pos %= dpcm->period_size_frac;
466 dpcm->period_update_pending = 1;
467 }
468 return delta;
469}
470
471static inline void bytepos_finish(struct loopback_pcm *dpcm,
472 unsigned int delta)
473{
474 dpcm->buf_pos += delta;
475 dpcm->buf_pos %= dpcm->pcm_buffer_size;
476}
477
478/* call in cable->lock */
479static unsigned int loopback_pos_update(struct loopback_cable *cable)
480{
481 struct loopback_pcm *dpcm_play =
482 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
483 struct loopback_pcm *dpcm_capt =
484 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
485 unsigned long delta_play = 0, delta_capt = 0;
486 unsigned int running, count1, count2;
487
488 running = cable->running ^ cable->pause;
489 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
490 delta_play = jiffies - dpcm_play->last_jiffies;
491 dpcm_play->last_jiffies += delta_play;
492 }
493
494 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
495 delta_capt = jiffies - dpcm_capt->last_jiffies;
496 dpcm_capt->last_jiffies += delta_capt;
497 }
498
499 if (delta_play == 0 && delta_capt == 0)
500 goto unlock;
501
502 if (delta_play > delta_capt) {
503 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
504 bytepos_finish(dpcm_play, count1);
505 delta_play = delta_capt;
506 } else if (delta_play < delta_capt) {
507 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
508 clear_capture_buf(dpcm_capt, count1);
509 bytepos_finish(dpcm_capt, count1);
510 delta_capt = delta_play;
511 }
512
513 if (delta_play == 0 && delta_capt == 0)
514 goto unlock;
515
516 /* note delta_capt == delta_play at this moment */
517 count1 = bytepos_delta(dpcm_play, delta_play);
518 count2 = bytepos_delta(dpcm_capt, delta_capt);
519 if (count1 < count2) {
520 dpcm_capt->last_drift = count2 - count1;
521 count1 = count2;
522 } else if (count1 > count2) {
523 dpcm_play->last_drift = count1 - count2;
524 }
525 copy_play_buf(dpcm_play, dpcm_capt, count1);
526 bytepos_finish(dpcm_play, count1);
527 bytepos_finish(dpcm_capt, count1);
528 unlock:
529 return running;
530}
531
532static void loopback_timer_function(unsigned long data)
533{
534 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
535 unsigned long flags;
536
537 spin_lock_irqsave(&dpcm->cable->lock, flags);
538 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
539 loopback_timer_start(dpcm);
540 if (dpcm->period_update_pending) {
541 dpcm->period_update_pending = 0;
542 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
543 /* need to unlock before calling below */
544 snd_pcm_period_elapsed(dpcm->substream);
545 return;
546 }
547 }
548 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
549}
550
551static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
552{
553 struct snd_pcm_runtime *runtime = substream->runtime;
554 struct loopback_pcm *dpcm = runtime->private_data;
555 snd_pcm_uframes_t pos;
556
557 spin_lock(&dpcm->cable->lock);
558 loopback_pos_update(dpcm->cable);
559 pos = dpcm->buf_pos;
560 spin_unlock(&dpcm->cable->lock);
561 return bytes_to_frames(runtime, pos);
562}
563
564static struct snd_pcm_hardware loopback_pcm_hardware =
565{
566 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
567 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
568 SNDRV_PCM_INFO_RESUME),
569 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
570 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
571 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
572 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
573 .rate_min = 8000,
574 .rate_max = 192000,
575 .channels_min = 1,
576 .channels_max = 32,
577 .buffer_bytes_max = 2 * 1024 * 1024,
578 .period_bytes_min = 64,
579 /* note check overflow in frac_pos() using pcm_rate_shift before
580 changing period_bytes_max value */
581 .period_bytes_max = 1024 * 1024,
582 .periods_min = 1,
583 .periods_max = 1024,
584 .fifo_size = 0,
585};
586
587static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
588{
589 struct loopback_pcm *dpcm = runtime->private_data;
590 kfree(dpcm);
591}
592
593static int loopback_hw_params(struct snd_pcm_substream *substream,
594 struct snd_pcm_hw_params *params)
595{
596 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
597 params_buffer_bytes(params));
598}
599
600static int loopback_hw_free(struct snd_pcm_substream *substream)
601{
602 struct snd_pcm_runtime *runtime = substream->runtime;
603 struct loopback_pcm *dpcm = runtime->private_data;
604 struct loopback_cable *cable = dpcm->cable;
605
606 mutex_lock(&dpcm->loopback->cable_lock);
607 cable->valid &= ~(1 << substream->stream);
608 mutex_unlock(&dpcm->loopback->cable_lock);
609 return snd_pcm_lib_free_vmalloc_buffer(substream);
610}
611
612static unsigned int get_cable_index(struct snd_pcm_substream *substream)
613{
614 if (!substream->pcm->device)
615 return substream->stream;
616 else
617 return !substream->stream;
618}
619
620static int rule_format(struct snd_pcm_hw_params *params,
621 struct snd_pcm_hw_rule *rule)
622{
623
624 struct snd_pcm_hardware *hw = rule->private;
625 struct snd_mask *maskp = hw_param_mask(params, rule->var);
626
627 maskp->bits[0] &= (u_int32_t)hw->formats;
628 maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
629 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
630 if (! maskp->bits[0] && ! maskp->bits[1])
631 return -EINVAL;
632 return 0;
633}
634
635static int rule_rate(struct snd_pcm_hw_params *params,
636 struct snd_pcm_hw_rule *rule)
637{
638 struct snd_pcm_hardware *hw = rule->private;
639 struct snd_interval t;
640
641 t.min = hw->rate_min;
642 t.max = hw->rate_max;
643 t.openmin = t.openmax = 0;
644 t.integer = 0;
645 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
646}
647
648static int rule_channels(struct snd_pcm_hw_params *params,
649 struct snd_pcm_hw_rule *rule)
650{
651 struct snd_pcm_hardware *hw = rule->private;
652 struct snd_interval t;
653
654 t.min = hw->channels_min;
655 t.max = hw->channels_max;
656 t.openmin = t.openmax = 0;
657 t.integer = 0;
658 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
659}
660
661static int loopback_open(struct snd_pcm_substream *substream)
662{
663 struct snd_pcm_runtime *runtime = substream->runtime;
664 struct loopback *loopback = substream->private_data;
665 struct loopback_pcm *dpcm;
666 struct loopback_cable *cable;
667 int err = 0;
668 int dev = get_cable_index(substream);
669
670 mutex_lock(&loopback->cable_lock);
671 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
672 if (!dpcm) {
673 err = -ENOMEM;
674 goto unlock;
675 }
676 dpcm->loopback = loopback;
677 dpcm->substream = substream;
678 setup_timer(&dpcm->timer, loopback_timer_function,
679 (unsigned long)dpcm);
680
681 cable = loopback->cables[substream->number][dev];
682 if (!cable) {
683 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
684 if (!cable) {
685 kfree(dpcm);
686 err = -ENOMEM;
687 goto unlock;
688 }
689 spin_lock_init(&cable->lock);
690 cable->hw = loopback_pcm_hardware;
691 loopback->cables[substream->number][dev] = cable;
692 }
693 dpcm->cable = cable;
694 cable->streams[substream->stream] = dpcm;
695
696 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
697
698 /* use dynamic rules based on actual runtime->hw values */
699 /* note that the default rules created in the PCM midlevel code */
700 /* are cached -> they do not reflect the actual state */
701 err = snd_pcm_hw_rule_add(runtime, 0,
702 SNDRV_PCM_HW_PARAM_FORMAT,
703 rule_format, &runtime->hw,
704 SNDRV_PCM_HW_PARAM_FORMAT, -1);
705 if (err < 0)
706 goto unlock;
707 err = snd_pcm_hw_rule_add(runtime, 0,
708 SNDRV_PCM_HW_PARAM_RATE,
709 rule_rate, &runtime->hw,
710 SNDRV_PCM_HW_PARAM_RATE, -1);
711 if (err < 0)
712 goto unlock;
713 err = snd_pcm_hw_rule_add(runtime, 0,
714 SNDRV_PCM_HW_PARAM_CHANNELS,
715 rule_channels, &runtime->hw,
716 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
717 if (err < 0)
718 goto unlock;
719
720 runtime->private_data = dpcm;
721 runtime->private_free = loopback_runtime_free;
722 if (get_notify(dpcm))
723 runtime->hw = loopback_pcm_hardware;
724 else
725 runtime->hw = cable->hw;
726 unlock:
727 mutex_unlock(&loopback->cable_lock);
728 return err;
729}
730
731static int loopback_close(struct snd_pcm_substream *substream)
732{
733 struct loopback *loopback = substream->private_data;
734 struct loopback_pcm *dpcm = substream->runtime->private_data;
735 struct loopback_cable *cable;
736 int dev = get_cable_index(substream);
737
738 loopback_timer_stop(dpcm);
739 mutex_lock(&loopback->cable_lock);
740 cable = loopback->cables[substream->number][dev];
741 if (cable->streams[!substream->stream]) {
742 /* other stream is still alive */
743 cable->streams[substream->stream] = NULL;
744 } else {
745 /* free the cable */
746 loopback->cables[substream->number][dev] = NULL;
747 kfree(cable);
748 }
749 mutex_unlock(&loopback->cable_lock);
750 return 0;
751}
752
753static struct snd_pcm_ops loopback_playback_ops = {
754 .open = loopback_open,
755 .close = loopback_close,
756 .ioctl = snd_pcm_lib_ioctl,
757 .hw_params = loopback_hw_params,
758 .hw_free = loopback_hw_free,
759 .prepare = loopback_prepare,
760 .trigger = loopback_trigger,
761 .pointer = loopback_pointer,
762 .page = snd_pcm_lib_get_vmalloc_page,
763 .mmap = snd_pcm_lib_mmap_vmalloc,
764};
765
766static struct snd_pcm_ops loopback_capture_ops = {
767 .open = loopback_open,
768 .close = loopback_close,
769 .ioctl = snd_pcm_lib_ioctl,
770 .hw_params = loopback_hw_params,
771 .hw_free = loopback_hw_free,
772 .prepare = loopback_prepare,
773 .trigger = loopback_trigger,
774 .pointer = loopback_pointer,
775 .page = snd_pcm_lib_get_vmalloc_page,
776 .mmap = snd_pcm_lib_mmap_vmalloc,
777};
778
779static int loopback_pcm_new(struct loopback *loopback,
780 int device, int substreams)
781{
782 struct snd_pcm *pcm;
783 int err;
784
785 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
786 substreams, substreams, &pcm);
787 if (err < 0)
788 return err;
789 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
790 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
791
792 pcm->private_data = loopback;
793 pcm->info_flags = 0;
794 strcpy(pcm->name, "Loopback PCM");
795
796 loopback->pcm[device] = pcm;
797 return 0;
798}
799
800static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
801 struct snd_ctl_elem_info *uinfo)
802{
803 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
804 uinfo->count = 1;
805 uinfo->value.integer.min = 80000;
806 uinfo->value.integer.max = 120000;
807 uinfo->value.integer.step = 1;
808 return 0;
809}
810
811static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
812 struct snd_ctl_elem_value *ucontrol)
813{
814 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
815
816 ucontrol->value.integer.value[0] =
817 loopback->setup[kcontrol->id.subdevice]
818 [kcontrol->id.device].rate_shift;
819 return 0;
820}
821
822static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol)
824{
825 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
826 unsigned int val;
827 int change = 0;
828
829 val = ucontrol->value.integer.value[0];
830 if (val < 80000)
831 val = 80000;
832 if (val > 120000)
833 val = 120000;
834 mutex_lock(&loopback->cable_lock);
835 if (val != loopback->setup[kcontrol->id.subdevice]
836 [kcontrol->id.device].rate_shift) {
837 loopback->setup[kcontrol->id.subdevice]
838 [kcontrol->id.device].rate_shift = val;
839 change = 1;
840 }
841 mutex_unlock(&loopback->cable_lock);
842 return change;
843}
844
845static int loopback_notify_get(struct snd_kcontrol *kcontrol,
846 struct snd_ctl_elem_value *ucontrol)
847{
848 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
849
850 ucontrol->value.integer.value[0] =
851 loopback->setup[kcontrol->id.subdevice]
852 [kcontrol->id.device].notify;
853 return 0;
854}
855
856static int loopback_notify_put(struct snd_kcontrol *kcontrol,
857 struct snd_ctl_elem_value *ucontrol)
858{
859 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
860 unsigned int val;
861 int change = 0;
862
863 val = ucontrol->value.integer.value[0] ? 1 : 0;
864 if (val != loopback->setup[kcontrol->id.subdevice]
865 [kcontrol->id.device].notify) {
866 loopback->setup[kcontrol->id.subdevice]
867 [kcontrol->id.device].notify = val;
868 change = 1;
869 }
870 return change;
871}
872
873static int loopback_active_get(struct snd_kcontrol *kcontrol,
874 struct snd_ctl_elem_value *ucontrol)
875{
876 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
877 struct loopback_cable *cable = loopback->cables
878 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
879 unsigned int val = 0;
880
881 if (cable != NULL)
882 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
883 1 : 0;
884 ucontrol->value.integer.value[0] = val;
885 return 0;
886}
887
888static int loopback_format_info(struct snd_kcontrol *kcontrol,
889 struct snd_ctl_elem_info *uinfo)
890{
891 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
892 uinfo->count = 1;
893 uinfo->value.integer.min = 0;
894 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
895 uinfo->value.integer.step = 1;
896 return 0;
897}
898
899static int loopback_format_get(struct snd_kcontrol *kcontrol,
900 struct snd_ctl_elem_value *ucontrol)
901{
902 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
903
904 ucontrol->value.integer.value[0] =
905 loopback->setup[kcontrol->id.subdevice]
906 [kcontrol->id.device].format;
907 return 0;
908}
909
910static int loopback_rate_info(struct snd_kcontrol *kcontrol,
911 struct snd_ctl_elem_info *uinfo)
912{
913 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
914 uinfo->count = 1;
915 uinfo->value.integer.min = 0;
916 uinfo->value.integer.max = 192000;
917 uinfo->value.integer.step = 1;
918 return 0;
919}
920
921static int loopback_rate_get(struct snd_kcontrol *kcontrol,
922 struct snd_ctl_elem_value *ucontrol)
923{
924 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
925
926 ucontrol->value.integer.value[0] =
927 loopback->setup[kcontrol->id.subdevice]
928 [kcontrol->id.device].rate;
929 return 0;
930}
931
932static int loopback_channels_info(struct snd_kcontrol *kcontrol,
933 struct snd_ctl_elem_info *uinfo)
934{
935 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
936 uinfo->count = 1;
937 uinfo->value.integer.min = 1;
938 uinfo->value.integer.max = 1024;
939 uinfo->value.integer.step = 1;
940 return 0;
941}
942
943static int loopback_channels_get(struct snd_kcontrol *kcontrol,
944 struct snd_ctl_elem_value *ucontrol)
945{
946 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
947
948 ucontrol->value.integer.value[0] =
949 loopback->setup[kcontrol->id.subdevice]
950 [kcontrol->id.device].channels;
951 return 0;
952}
953
954static struct snd_kcontrol_new loopback_controls[] = {
955{
956 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
957 .name = "PCM Rate Shift 100000",
958 .info = loopback_rate_shift_info,
959 .get = loopback_rate_shift_get,
960 .put = loopback_rate_shift_put,
961},
962{
963 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
964 .name = "PCM Notify",
965 .info = snd_ctl_boolean_mono_info,
966 .get = loopback_notify_get,
967 .put = loopback_notify_put,
968},
969#define ACTIVE_IDX 2
970{
971 .access = SNDRV_CTL_ELEM_ACCESS_READ,
972 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
973 .name = "PCM Slave Active",
974 .info = snd_ctl_boolean_mono_info,
975 .get = loopback_active_get,
976},
977#define FORMAT_IDX 3
978{
979 .access = SNDRV_CTL_ELEM_ACCESS_READ,
980 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
981 .name = "PCM Slave Format",
982 .info = loopback_format_info,
983 .get = loopback_format_get
984},
985#define RATE_IDX 4
986{
987 .access = SNDRV_CTL_ELEM_ACCESS_READ,
988 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
989 .name = "PCM Slave Rate",
990 .info = loopback_rate_info,
991 .get = loopback_rate_get
992},
993#define CHANNELS_IDX 5
994{
995 .access = SNDRV_CTL_ELEM_ACCESS_READ,
996 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
997 .name = "PCM Slave Channels",
998 .info = loopback_channels_info,
999 .get = loopback_channels_get
1000}
1001};
1002
1003static int loopback_mixer_new(struct loopback *loopback, int notify)
1004{
1005 struct snd_card *card = loopback->card;
1006 struct snd_pcm *pcm;
1007 struct snd_kcontrol *kctl;
1008 struct loopback_setup *setup;
1009 int err, dev, substr, substr_count, idx;
1010
1011 strcpy(card->mixername, "Loopback Mixer");
1012 for (dev = 0; dev < 2; dev++) {
1013 pcm = loopback->pcm[dev];
1014 substr_count =
1015 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1016 for (substr = 0; substr < substr_count; substr++) {
1017 setup = &loopback->setup[substr][dev];
1018 setup->notify = notify;
1019 setup->rate_shift = NO_PITCH;
1020 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1021 setup->rate = 48000;
1022 setup->channels = 2;
1023 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1024 idx++) {
1025 kctl = snd_ctl_new1(&loopback_controls[idx],
1026 loopback);
1027 if (!kctl)
1028 return -ENOMEM;
1029 kctl->id.device = dev;
1030 kctl->id.subdevice = substr;
1031 switch (idx) {
1032 case ACTIVE_IDX:
1033 setup->active_id = kctl->id;
1034 break;
1035 case FORMAT_IDX:
1036 setup->format_id = kctl->id;
1037 break;
1038 case RATE_IDX:
1039 setup->rate_id = kctl->id;
1040 break;
1041 case CHANNELS_IDX:
1042 setup->channels_id = kctl->id;
1043 break;
1044 default:
1045 break;
1046 }
1047 err = snd_ctl_add(card, kctl);
1048 if (err < 0)
1049 return err;
1050 }
1051 }
1052 }
1053 return 0;
1054}
1055
1056static void print_dpcm_info(struct snd_info_buffer *buffer,
1057 struct loopback_pcm *dpcm,
1058 const char *id)
1059{
1060 snd_iprintf(buffer, " %s\n", id);
1061 if (dpcm == NULL) {
1062 snd_iprintf(buffer, " inactive\n");
1063 return;
1064 }
1065 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1066 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1067 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1068 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1069 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1070 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1071 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1072 snd_iprintf(buffer, " update_pending:\t%u\n",
1073 dpcm->period_update_pending);
1074 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1075 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1076 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1077 dpcm->last_jiffies, jiffies);
1078 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1079}
1080
1081static void print_substream_info(struct snd_info_buffer *buffer,
1082 struct loopback *loopback,
1083 int sub,
1084 int num)
1085{
1086 struct loopback_cable *cable = loopback->cables[sub][num];
1087
1088 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1089 if (cable == NULL) {
1090 snd_iprintf(buffer, " inactive\n");
1091 return;
1092 }
1093 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1094 snd_iprintf(buffer, " running: %u\n", cable->running);
1095 snd_iprintf(buffer, " pause: %u\n", cable->pause);
1096 print_dpcm_info(buffer, cable->streams[0], "Playback");
1097 print_dpcm_info(buffer, cable->streams[1], "Capture");
1098}
1099
1100static void print_cable_info(struct snd_info_entry *entry,
1101 struct snd_info_buffer *buffer)
1102{
1103 struct loopback *loopback = entry->private_data;
1104 int sub, num;
1105
1106 mutex_lock(&loopback->cable_lock);
1107 num = entry->name[strlen(entry->name)-1];
1108 num = num == '0' ? 0 : 1;
1109 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1110 print_substream_info(buffer, loopback, sub, num);
1111 mutex_unlock(&loopback->cable_lock);
1112}
1113
1114static int loopback_proc_new(struct loopback *loopback, int cidx)
1115{
1116 char name[32];
1117 struct snd_info_entry *entry;
1118 int err;
1119
1120 snprintf(name, sizeof(name), "cable#%d", cidx);
1121 err = snd_card_proc_new(loopback->card, name, &entry);
1122 if (err < 0)
1123 return err;
1124
1125 snd_info_set_text_ops(entry, loopback, print_cable_info);
1126 return 0;
1127}
1128
1129static int loopback_probe(struct platform_device *devptr)
1130{
1131 struct snd_card *card;
1132 struct loopback *loopback;
1133 int dev = devptr->id;
1134 int err;
1135
1136 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1137 sizeof(struct loopback), &card);
1138 if (err < 0)
1139 return err;
1140 loopback = card->private_data;
1141
1142 if (pcm_substreams[dev] < 1)
1143 pcm_substreams[dev] = 1;
1144 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1145 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1146
1147 loopback->card = card;
1148 mutex_init(&loopback->cable_lock);
1149
1150 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1151 if (err < 0)
1152 goto __nodev;
1153 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1154 if (err < 0)
1155 goto __nodev;
1156 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1157 if (err < 0)
1158 goto __nodev;
1159 loopback_proc_new(loopback, 0);
1160 loopback_proc_new(loopback, 1);
1161 strcpy(card->driver, "Loopback");
1162 strcpy(card->shortname, "Loopback");
1163 sprintf(card->longname, "Loopback %i", dev + 1);
1164 err = snd_card_register(card);
1165 if (!err) {
1166 platform_set_drvdata(devptr, card);
1167 return 0;
1168 }
1169 __nodev:
1170 snd_card_free(card);
1171 return err;
1172}
1173
1174static int loopback_remove(struct platform_device *devptr)
1175{
1176 snd_card_free(platform_get_drvdata(devptr));
1177 return 0;
1178}
1179
1180#ifdef CONFIG_PM_SLEEP
1181static int loopback_suspend(struct device *pdev)
1182{
1183 struct snd_card *card = dev_get_drvdata(pdev);
1184 struct loopback *loopback = card->private_data;
1185
1186 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1187
1188 snd_pcm_suspend_all(loopback->pcm[0]);
1189 snd_pcm_suspend_all(loopback->pcm[1]);
1190 return 0;
1191}
1192
1193static int loopback_resume(struct device *pdev)
1194{
1195 struct snd_card *card = dev_get_drvdata(pdev);
1196
1197 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1198 return 0;
1199}
1200
1201static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1202#define LOOPBACK_PM_OPS &loopback_pm
1203#else
1204#define LOOPBACK_PM_OPS NULL
1205#endif
1206
1207#define SND_LOOPBACK_DRIVER "snd_aloop"
1208
1209static struct platform_driver loopback_driver = {
1210 .probe = loopback_probe,
1211 .remove = loopback_remove,
1212 .driver = {
1213 .name = SND_LOOPBACK_DRIVER,
1214 .pm = LOOPBACK_PM_OPS,
1215 },
1216};
1217
1218static void loopback_unregister_all(void)
1219{
1220 int i;
1221
1222 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1223 platform_device_unregister(devices[i]);
1224 platform_driver_unregister(&loopback_driver);
1225}
1226
1227static int __init alsa_card_loopback_init(void)
1228{
1229 int i, err, cards;
1230
1231 err = platform_driver_register(&loopback_driver);
1232 if (err < 0)
1233 return err;
1234
1235
1236 cards = 0;
1237 for (i = 0; i < SNDRV_CARDS; i++) {
1238 struct platform_device *device;
1239 if (!enable[i])
1240 continue;
1241 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1242 i, NULL, 0);
1243 if (IS_ERR(device))
1244 continue;
1245 if (!platform_get_drvdata(device)) {
1246 platform_device_unregister(device);
1247 continue;
1248 }
1249 devices[i] = device;
1250 cards++;
1251 }
1252 if (!cards) {
1253#ifdef MODULE
1254 printk(KERN_ERR "aloop: No loopback enabled\n");
1255#endif
1256 loopback_unregister_all();
1257 return -ENODEV;
1258 }
1259 return 0;
1260}
1261
1262static void __exit alsa_card_loopback_exit(void)
1263{
1264 loopback_unregister_all();
1265}
1266
1267module_init(alsa_card_loopback_init)
1268module_exit(alsa_card_loopback_exit)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Loopback soundcard
4 *
5 * Original code:
6 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 *
8 * More accurate positioning and full-duplex support:
9 * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
10 *
11 * Major (almost complete) rewrite:
12 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
13 *
14 * A next major update in 2010 (separate timers for playback and capture):
15 * Copyright (c) Jaroslav Kysela <perex@perex.cz>
16 */
17
18#include <linux/init.h>
19#include <linux/jiffies.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/wait.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <sound/core.h>
26#include <sound/control.h>
27#include <sound/pcm.h>
28#include <sound/pcm_params.h>
29#include <sound/info.h>
30#include <sound/initval.h>
31#include <sound/timer.h>
32
33MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
34MODULE_DESCRIPTION("A loopback soundcard");
35MODULE_LICENSE("GPL");
36
37#define MAX_PCM_SUBSTREAMS 8
38
39static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
40static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
41static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
42static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
43static int pcm_notify[SNDRV_CARDS];
44static char *timer_source[SNDRV_CARDS];
45
46module_param_array(index, int, NULL, 0444);
47MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
48module_param_array(id, charp, NULL, 0444);
49MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
50module_param_array(enable, bool, NULL, 0444);
51MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
52module_param_array(pcm_substreams, int, NULL, 0444);
53MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
54module_param_array(pcm_notify, int, NULL, 0444);
55MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
56module_param_array(timer_source, charp, NULL, 0444);
57MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default].");
58
59#define NO_PITCH 100000
60
61#define CABLE_VALID_PLAYBACK BIT(SNDRV_PCM_STREAM_PLAYBACK)
62#define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE)
63#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE)
64
65struct loopback_cable;
66struct loopback_pcm;
67
68struct loopback_ops {
69 /* optional
70 * call in loopback->cable_lock
71 */
72 int (*open)(struct loopback_pcm *dpcm);
73 /* required
74 * call in cable->lock
75 */
76 int (*start)(struct loopback_pcm *dpcm);
77 /* required
78 * call in cable->lock
79 */
80 int (*stop)(struct loopback_pcm *dpcm);
81 /* optional */
82 int (*stop_sync)(struct loopback_pcm *dpcm);
83 /* optional */
84 int (*close_substream)(struct loopback_pcm *dpcm);
85 /* optional
86 * call in loopback->cable_lock
87 */
88 int (*close_cable)(struct loopback_pcm *dpcm);
89 /* optional
90 * call in cable->lock
91 */
92 unsigned int (*pos_update)(struct loopback_cable *cable);
93 /* optional */
94 void (*dpcm_info)(struct loopback_pcm *dpcm,
95 struct snd_info_buffer *buffer);
96};
97
98struct loopback_cable {
99 spinlock_t lock;
100 struct loopback_pcm *streams[2];
101 struct snd_pcm_hardware hw;
102 /* flags */
103 unsigned int valid;
104 unsigned int running;
105 unsigned int pause;
106 /* timer specific */
107 const struct loopback_ops *ops;
108 /* If sound timer is used */
109 struct {
110 int stream;
111 struct snd_timer_id id;
112 struct work_struct event_work;
113 struct snd_timer_instance *instance;
114 } snd_timer;
115};
116
117struct loopback_setup {
118 unsigned int notify: 1;
119 unsigned int rate_shift;
120 snd_pcm_format_t format;
121 unsigned int rate;
122 snd_pcm_access_t access;
123 unsigned int channels;
124 struct snd_ctl_elem_id active_id;
125 struct snd_ctl_elem_id format_id;
126 struct snd_ctl_elem_id rate_id;
127 struct snd_ctl_elem_id channels_id;
128 struct snd_ctl_elem_id access_id;
129};
130
131struct loopback {
132 struct snd_card *card;
133 struct mutex cable_lock;
134 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
135 struct snd_pcm *pcm[2];
136 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
137 const char *timer_source;
138};
139
140struct loopback_pcm {
141 struct loopback *loopback;
142 struct snd_pcm_substream *substream;
143 struct loopback_cable *cable;
144 unsigned int pcm_buffer_size;
145 unsigned int buf_pos; /* position in buffer */
146 unsigned int silent_size;
147 /* PCM parameters */
148 unsigned int pcm_period_size;
149 unsigned int pcm_bps; /* bytes per second */
150 unsigned int pcm_salign; /* bytes per sample * channels */
151 unsigned int pcm_rate_shift; /* rate shift value */
152 /* flags */
153 unsigned int period_update_pending :1;
154 /* timer stuff */
155 unsigned int irq_pos; /* fractional IRQ position in jiffies
156 * ticks
157 */
158 unsigned int period_size_frac; /* period size in jiffies ticks */
159 unsigned int last_drift;
160 unsigned long last_jiffies;
161 /* If jiffies timer is used */
162 struct timer_list timer;
163
164 /* size of per channel buffer in case of non-interleaved access */
165 unsigned int channel_buf_n;
166};
167
168static struct platform_device *devices[SNDRV_CARDS];
169
170static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
171{
172 if (dpcm->pcm_rate_shift == NO_PITCH) {
173 x /= HZ;
174 } else {
175 x = div_u64(NO_PITCH * (unsigned long long)x,
176 HZ * (unsigned long long)dpcm->pcm_rate_shift);
177 }
178 return x - (x % dpcm->pcm_salign);
179}
180
181static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
182{
183 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
184 return x * HZ;
185 } else {
186 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
187 NO_PITCH);
188 }
189 return x;
190}
191
192static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
193{
194 int device = dpcm->substream->pstr->pcm->device;
195
196 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
197 device ^= 1;
198 return &dpcm->loopback->setup[dpcm->substream->number][device];
199}
200
201static inline unsigned int get_notify(struct loopback_pcm *dpcm)
202{
203 return get_setup(dpcm)->notify;
204}
205
206static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
207{
208 return get_setup(dpcm)->rate_shift;
209}
210
211/* call in cable->lock */
212static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm)
213{
214 unsigned long tick;
215 unsigned int rate_shift = get_rate_shift(dpcm);
216
217 if (rate_shift != dpcm->pcm_rate_shift) {
218 dpcm->pcm_rate_shift = rate_shift;
219 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
220 }
221 if (dpcm->period_size_frac <= dpcm->irq_pos) {
222 dpcm->irq_pos %= dpcm->period_size_frac;
223 dpcm->period_update_pending = 1;
224 }
225 tick = dpcm->period_size_frac - dpcm->irq_pos;
226 tick = DIV_ROUND_UP(tick, dpcm->pcm_bps);
227 mod_timer(&dpcm->timer, jiffies + tick);
228
229 return 0;
230}
231
232/* call in cable->lock */
233static int loopback_snd_timer_start(struct loopback_pcm *dpcm)
234{
235 struct loopback_cable *cable = dpcm->cable;
236 int err;
237
238 /* Loopback device has to use same period as timer card. Therefore
239 * wake up for each snd_pcm_period_elapsed() call of timer card.
240 */
241 err = snd_timer_start(cable->snd_timer.instance, 1);
242 if (err < 0) {
243 /* do not report error if trying to start but already
244 * running. For example called by opposite substream
245 * of the same cable
246 */
247 if (err == -EBUSY)
248 return 0;
249
250 pcm_err(dpcm->substream->pcm,
251 "snd_timer_start(%d,%d,%d) failed with %d",
252 cable->snd_timer.id.card,
253 cable->snd_timer.id.device,
254 cable->snd_timer.id.subdevice,
255 err);
256 }
257
258 return err;
259}
260
261/* call in cable->lock */
262static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm)
263{
264 del_timer(&dpcm->timer);
265 dpcm->timer.expires = 0;
266
267 return 0;
268}
269
270/* call in cable->lock */
271static int loopback_snd_timer_stop(struct loopback_pcm *dpcm)
272{
273 struct loopback_cable *cable = dpcm->cable;
274 int err;
275
276 /* only stop if both devices (playback and capture) are not running */
277 if (cable->running ^ cable->pause)
278 return 0;
279
280 err = snd_timer_stop(cable->snd_timer.instance);
281 if (err < 0) {
282 pcm_err(dpcm->substream->pcm,
283 "snd_timer_stop(%d,%d,%d) failed with %d",
284 cable->snd_timer.id.card,
285 cable->snd_timer.id.device,
286 cable->snd_timer.id.subdevice,
287 err);
288 }
289
290 return err;
291}
292
293static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm)
294{
295 del_timer_sync(&dpcm->timer);
296
297 return 0;
298}
299
300/* call in loopback->cable_lock */
301static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)
302{
303 struct loopback_cable *cable = dpcm->cable;
304
305 /* snd_timer was not opened */
306 if (!cable->snd_timer.instance)
307 return 0;
308
309 /* will only be called from free_cable() when other stream was
310 * already closed. Other stream cannot be reopened as long as
311 * loopback->cable_lock is locked. Therefore no need to lock
312 * cable->lock;
313 */
314 snd_timer_close(cable->snd_timer.instance);
315
316 /* wait till drain work has finished if requested */
317 cancel_work_sync(&cable->snd_timer.event_work);
318
319 snd_timer_instance_free(cable->snd_timer.instance);
320 memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));
321
322 return 0;
323}
324
325static bool is_access_interleaved(snd_pcm_access_t access)
326{
327 switch (access) {
328 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
329 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
330 return true;
331 default:
332 return false;
333 }
334};
335
336static int loopback_check_format(struct loopback_cable *cable, int stream)
337{
338 struct snd_pcm_runtime *runtime, *cruntime;
339 struct loopback_setup *setup;
340 struct snd_card *card;
341 int check;
342
343 if (cable->valid != CABLE_VALID_BOTH) {
344 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
345 goto __notify;
346 return 0;
347 }
348 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
349 substream->runtime;
350 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
351 substream->runtime;
352 check = runtime->format != cruntime->format ||
353 runtime->rate != cruntime->rate ||
354 runtime->channels != cruntime->channels ||
355 is_access_interleaved(runtime->access) !=
356 is_access_interleaved(cruntime->access);
357 if (!check)
358 return 0;
359 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
360 return -EIO;
361 } else {
362 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
363 substream, SNDRV_PCM_STATE_DRAINING);
364 __notify:
365 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
366 substream->runtime;
367 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
368 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
369 if (setup->format != runtime->format) {
370 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
371 &setup->format_id);
372 setup->format = runtime->format;
373 }
374 if (setup->rate != runtime->rate) {
375 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
376 &setup->rate_id);
377 setup->rate = runtime->rate;
378 }
379 if (setup->channels != runtime->channels) {
380 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
381 &setup->channels_id);
382 setup->channels = runtime->channels;
383 }
384 if (is_access_interleaved(setup->access) !=
385 is_access_interleaved(runtime->access)) {
386 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
387 &setup->access_id);
388 setup->access = runtime->access;
389 }
390 }
391 return 0;
392}
393
394static void loopback_active_notify(struct loopback_pcm *dpcm)
395{
396 snd_ctl_notify(dpcm->loopback->card,
397 SNDRV_CTL_EVENT_MASK_VALUE,
398 &get_setup(dpcm)->active_id);
399}
400
401static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
402{
403 struct snd_pcm_runtime *runtime = substream->runtime;
404 struct loopback_pcm *dpcm = runtime->private_data;
405 struct loopback_cable *cable = dpcm->cable;
406 int err = 0, stream = 1 << substream->stream;
407
408 switch (cmd) {
409 case SNDRV_PCM_TRIGGER_START:
410 err = loopback_check_format(cable, substream->stream);
411 if (err < 0)
412 return err;
413 dpcm->last_jiffies = jiffies;
414 dpcm->pcm_rate_shift = 0;
415 dpcm->last_drift = 0;
416 spin_lock(&cable->lock);
417 cable->running |= stream;
418 cable->pause &= ~stream;
419 err = cable->ops->start(dpcm);
420 spin_unlock(&cable->lock);
421 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
422 loopback_active_notify(dpcm);
423 break;
424 case SNDRV_PCM_TRIGGER_STOP:
425 spin_lock(&cable->lock);
426 cable->running &= ~stream;
427 cable->pause &= ~stream;
428 err = cable->ops->stop(dpcm);
429 spin_unlock(&cable->lock);
430 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
431 loopback_active_notify(dpcm);
432 break;
433 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
434 case SNDRV_PCM_TRIGGER_SUSPEND:
435 spin_lock(&cable->lock);
436 cable->pause |= stream;
437 err = cable->ops->stop(dpcm);
438 spin_unlock(&cable->lock);
439 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
440 loopback_active_notify(dpcm);
441 break;
442 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
443 case SNDRV_PCM_TRIGGER_RESUME:
444 spin_lock(&cable->lock);
445 dpcm->last_jiffies = jiffies;
446 cable->pause &= ~stream;
447 err = cable->ops->start(dpcm);
448 spin_unlock(&cable->lock);
449 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
450 loopback_active_notify(dpcm);
451 break;
452 default:
453 return -EINVAL;
454 }
455 return err;
456}
457
458static void params_change(struct snd_pcm_substream *substream)
459{
460 struct snd_pcm_runtime *runtime = substream->runtime;
461 struct loopback_pcm *dpcm = runtime->private_data;
462 struct loopback_cable *cable = dpcm->cable;
463
464 cable->hw.formats = pcm_format_to_bits(runtime->format);
465 cable->hw.rate_min = runtime->rate;
466 cable->hw.rate_max = runtime->rate;
467 cable->hw.channels_min = runtime->channels;
468 cable->hw.channels_max = runtime->channels;
469
470 if (cable->snd_timer.instance) {
471 cable->hw.period_bytes_min =
472 frames_to_bytes(runtime, runtime->period_size);
473 cable->hw.period_bytes_max = cable->hw.period_bytes_min;
474 }
475
476}
477
478static int loopback_prepare(struct snd_pcm_substream *substream)
479{
480 struct snd_pcm_runtime *runtime = substream->runtime;
481 struct loopback_pcm *dpcm = runtime->private_data;
482 struct loopback_cable *cable = dpcm->cable;
483 int err, bps, salign;
484
485 if (cable->ops->stop_sync) {
486 err = cable->ops->stop_sync(dpcm);
487 if (err < 0)
488 return err;
489 }
490
491 salign = (snd_pcm_format_physical_width(runtime->format) *
492 runtime->channels) / 8;
493 bps = salign * runtime->rate;
494 if (bps <= 0 || salign <= 0)
495 return -EINVAL;
496
497 dpcm->buf_pos = 0;
498 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
499 dpcm->channel_buf_n = dpcm->pcm_buffer_size / runtime->channels;
500 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
501 /* clear capture buffer */
502 dpcm->silent_size = dpcm->pcm_buffer_size;
503 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
504 runtime->buffer_size * runtime->channels);
505 }
506
507 dpcm->irq_pos = 0;
508 dpcm->period_update_pending = 0;
509 dpcm->pcm_bps = bps;
510 dpcm->pcm_salign = salign;
511 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
512
513 mutex_lock(&dpcm->loopback->cable_lock);
514 if (!(cable->valid & ~(1 << substream->stream)) ||
515 (get_setup(dpcm)->notify &&
516 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
517 params_change(substream);
518 cable->valid |= 1 << substream->stream;
519 mutex_unlock(&dpcm->loopback->cable_lock);
520
521 return 0;
522}
523
524static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
525{
526 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
527 char *dst = runtime->dma_area;
528 unsigned int dst_off = dpcm->buf_pos;
529
530 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
531 return;
532 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
533 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
534
535 for (;;) {
536 unsigned int size = bytes;
537 if (dst_off + size > dpcm->pcm_buffer_size)
538 size = dpcm->pcm_buffer_size - dst_off;
539 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
540 bytes_to_frames(runtime, size) *
541 runtime->channels);
542 dpcm->silent_size += size;
543 bytes -= size;
544 if (!bytes)
545 break;
546 dst_off = 0;
547 }
548}
549
550static void copy_play_buf_part_n(struct loopback_pcm *play, struct loopback_pcm *capt,
551 unsigned int size, unsigned int src_off, unsigned int dst_off)
552{
553 unsigned int channels = capt->substream->runtime->channels;
554 unsigned int size_p_ch = size / channels;
555 unsigned int src_off_ch = src_off / channels;
556 unsigned int dst_off_ch = dst_off / channels;
557 int i;
558
559 for (i = 0; i < channels; i++) {
560 memcpy(capt->substream->runtime->dma_area + capt->channel_buf_n * i + dst_off_ch,
561 play->substream->runtime->dma_area + play->channel_buf_n * i + src_off_ch,
562 size_p_ch);
563 }
564}
565
566static void copy_play_buf(struct loopback_pcm *play,
567 struct loopback_pcm *capt,
568 unsigned int bytes)
569{
570 struct snd_pcm_runtime *runtime = play->substream->runtime;
571 char *src = runtime->dma_area;
572 char *dst = capt->substream->runtime->dma_area;
573 unsigned int src_off = play->buf_pos;
574 unsigned int dst_off = capt->buf_pos;
575 unsigned int clear_bytes = 0;
576
577 /* check if playback is draining, trim the capture copy size
578 * when our pointer is at the end of playback ring buffer */
579 if (runtime->state == SNDRV_PCM_STATE_DRAINING &&
580 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
581 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
582 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
583 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
584 appl_ptr1 += play->buf_pos / play->pcm_salign;
585 if (appl_ptr < appl_ptr1)
586 appl_ptr1 -= runtime->buffer_size;
587 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
588 if (diff < bytes) {
589 clear_bytes = bytes - diff;
590 bytes = diff;
591 }
592 }
593
594 for (;;) {
595 unsigned int size = bytes;
596 if (src_off + size > play->pcm_buffer_size)
597 size = play->pcm_buffer_size - src_off;
598 if (dst_off + size > capt->pcm_buffer_size)
599 size = capt->pcm_buffer_size - dst_off;
600 if (!is_access_interleaved(runtime->access))
601 copy_play_buf_part_n(play, capt, size, src_off, dst_off);
602 else
603 memcpy(dst + dst_off, src + src_off, size);
604 capt->silent_size = 0;
605 bytes -= size;
606 if (!bytes)
607 break;
608 src_off = (src_off + size) % play->pcm_buffer_size;
609 dst_off = (dst_off + size) % capt->pcm_buffer_size;
610 }
611
612 if (clear_bytes > 0) {
613 clear_capture_buf(capt, clear_bytes);
614 capt->silent_size = 0;
615 }
616}
617
618static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
619 unsigned int jiffies_delta)
620{
621 unsigned long last_pos;
622 unsigned int delta;
623
624 last_pos = byte_pos(dpcm, dpcm->irq_pos);
625 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
626 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
627 if (delta >= dpcm->last_drift)
628 delta -= dpcm->last_drift;
629 dpcm->last_drift = 0;
630 if (dpcm->irq_pos >= dpcm->period_size_frac) {
631 dpcm->irq_pos %= dpcm->period_size_frac;
632 dpcm->period_update_pending = 1;
633 }
634 return delta;
635}
636
637static inline void bytepos_finish(struct loopback_pcm *dpcm,
638 unsigned int delta)
639{
640 dpcm->buf_pos += delta;
641 dpcm->buf_pos %= dpcm->pcm_buffer_size;
642}
643
644/* call in cable->lock */
645static unsigned int loopback_jiffies_timer_pos_update
646 (struct loopback_cable *cable)
647{
648 struct loopback_pcm *dpcm_play =
649 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
650 struct loopback_pcm *dpcm_capt =
651 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
652 unsigned long delta_play = 0, delta_capt = 0, cur_jiffies;
653 unsigned int running, count1, count2;
654
655 cur_jiffies = jiffies;
656 running = cable->running ^ cable->pause;
657 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
658 delta_play = cur_jiffies - dpcm_play->last_jiffies;
659 dpcm_play->last_jiffies += delta_play;
660 }
661
662 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
663 delta_capt = cur_jiffies - dpcm_capt->last_jiffies;
664 dpcm_capt->last_jiffies += delta_capt;
665 }
666
667 if (delta_play == 0 && delta_capt == 0)
668 goto unlock;
669
670 if (delta_play > delta_capt) {
671 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
672 bytepos_finish(dpcm_play, count1);
673 delta_play = delta_capt;
674 } else if (delta_play < delta_capt) {
675 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
676 clear_capture_buf(dpcm_capt, count1);
677 bytepos_finish(dpcm_capt, count1);
678 delta_capt = delta_play;
679 }
680
681 if (delta_play == 0 && delta_capt == 0)
682 goto unlock;
683
684 /* note delta_capt == delta_play at this moment */
685 count1 = bytepos_delta(dpcm_play, delta_play);
686 count2 = bytepos_delta(dpcm_capt, delta_capt);
687 if (count1 < count2) {
688 dpcm_capt->last_drift = count2 - count1;
689 count1 = count2;
690 } else if (count1 > count2) {
691 dpcm_play->last_drift = count1 - count2;
692 }
693 copy_play_buf(dpcm_play, dpcm_capt, count1);
694 bytepos_finish(dpcm_play, count1);
695 bytepos_finish(dpcm_capt, count1);
696 unlock:
697 return running;
698}
699
700static void loopback_jiffies_timer_function(struct timer_list *t)
701{
702 struct loopback_pcm *dpcm = from_timer(dpcm, t, timer);
703 unsigned long flags;
704
705 spin_lock_irqsave(&dpcm->cable->lock, flags);
706 if (loopback_jiffies_timer_pos_update(dpcm->cable) &
707 (1 << dpcm->substream->stream)) {
708 loopback_jiffies_timer_start(dpcm);
709 if (dpcm->period_update_pending) {
710 dpcm->period_update_pending = 0;
711 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
712 /* need to unlock before calling below */
713 snd_pcm_period_elapsed(dpcm->substream);
714 return;
715 }
716 }
717 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
718}
719
720/* call in cable->lock */
721static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime,
722 unsigned long resolution)
723{
724 if (resolution != runtime->timer_resolution) {
725 struct loopback_pcm *dpcm = runtime->private_data;
726 struct loopback_cable *cable = dpcm->cable;
727 /* Worst case estimation of possible values for resolution
728 * resolution <= (512 * 1024) frames / 8kHz in nsec
729 * resolution <= 65.536.000.000 nsec
730 *
731 * period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz +
732 * 500.000
733 * period_size <= 12.582.912.000.000 <64bit
734 * / 1.000.000 usec/sec
735 */
736 snd_pcm_uframes_t period_size_usec =
737 resolution / 1000 * runtime->rate;
738 /* round to nearest sample rate */
739 snd_pcm_uframes_t period_size =
740 (period_size_usec + 500 * 1000) / (1000 * 1000);
741
742 pcm_err(dpcm->substream->pcm,
743 "Period size (%lu frames) of loopback device is not corresponding to timer resolution (%lu nsec = %lu frames) of card timer %d,%d,%d. Use period size of %lu frames for loopback device.",
744 runtime->period_size, resolution, period_size,
745 cable->snd_timer.id.card,
746 cable->snd_timer.id.device,
747 cable->snd_timer.id.subdevice,
748 period_size);
749 return -EINVAL;
750 }
751 return 0;
752}
753
754static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
755 int event,
756 unsigned long resolution)
757{
758 struct loopback_pcm *dpcm_play, *dpcm_capt;
759 struct snd_pcm_substream *substream_play, *substream_capt;
760 struct snd_pcm_runtime *valid_runtime;
761 unsigned int running, elapsed_bytes;
762 unsigned long flags;
763
764 spin_lock_irqsave(&cable->lock, flags);
765 running = cable->running ^ cable->pause;
766 /* no need to do anything if no stream is running */
767 if (!running) {
768 spin_unlock_irqrestore(&cable->lock, flags);
769 return;
770 }
771
772 dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
773 dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
774
775 if (event == SNDRV_TIMER_EVENT_MSTOP) {
776 if (!dpcm_play ||
777 dpcm_play->substream->runtime->state !=
778 SNDRV_PCM_STATE_DRAINING) {
779 spin_unlock_irqrestore(&cable->lock, flags);
780 return;
781 }
782 }
783
784 substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
785 dpcm_play->substream : NULL;
786 substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
787 dpcm_capt->substream : NULL;
788 valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
789 dpcm_play->substream->runtime :
790 dpcm_capt->substream->runtime;
791
792 /* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
793 if (event == SNDRV_TIMER_EVENT_TICK) {
794 /* The hardware rules guarantee that playback and capture period
795 * are the same. Therefore only one device has to be checked
796 * here.
797 */
798 if (loopback_snd_timer_check_resolution(valid_runtime,
799 resolution) < 0) {
800 spin_unlock_irqrestore(&cable->lock, flags);
801 if (substream_play)
802 snd_pcm_stop_xrun(substream_play);
803 if (substream_capt)
804 snd_pcm_stop_xrun(substream_capt);
805 return;
806 }
807 }
808
809 elapsed_bytes = frames_to_bytes(valid_runtime,
810 valid_runtime->period_size);
811 /* The same timer interrupt is used for playback and capture device */
812 if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
813 (running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
814 copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
815 bytepos_finish(dpcm_play, elapsed_bytes);
816 bytepos_finish(dpcm_capt, elapsed_bytes);
817 } else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
818 bytepos_finish(dpcm_play, elapsed_bytes);
819 } else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
820 clear_capture_buf(dpcm_capt, elapsed_bytes);
821 bytepos_finish(dpcm_capt, elapsed_bytes);
822 }
823 spin_unlock_irqrestore(&cable->lock, flags);
824
825 if (substream_play)
826 snd_pcm_period_elapsed(substream_play);
827 if (substream_capt)
828 snd_pcm_period_elapsed(substream_capt);
829}
830
831static void loopback_snd_timer_function(struct snd_timer_instance *timeri,
832 unsigned long resolution,
833 unsigned long ticks)
834{
835 struct loopback_cable *cable = timeri->callback_data;
836
837 loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK,
838 resolution);
839}
840
841static void loopback_snd_timer_work(struct work_struct *work)
842{
843 struct loopback_cable *cable;
844
845 cable = container_of(work, struct loopback_cable, snd_timer.event_work);
846 loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0);
847}
848
849static void loopback_snd_timer_event(struct snd_timer_instance *timeri,
850 int event,
851 struct timespec64 *tstamp,
852 unsigned long resolution)
853{
854 /* Do not lock cable->lock here because timer->lock is already hold.
855 * There are other functions which first lock cable->lock and than
856 * timer->lock e.g.
857 * loopback_trigger()
858 * spin_lock(&cable->lock)
859 * loopback_snd_timer_start()
860 * snd_timer_start()
861 * spin_lock(&timer->lock)
862 * Therefore when using the oposit order of locks here it could result
863 * in a deadlock.
864 */
865
866 if (event == SNDRV_TIMER_EVENT_MSTOP) {
867 struct loopback_cable *cable = timeri->callback_data;
868
869 /* sound card of the timer was stopped. Therefore there will not
870 * be any further timer callbacks. Due to this forward audio
871 * data from here if in draining state. When still in running
872 * state the streaming will be aborted by the usual timeout. It
873 * should not be aborted here because may be the timer sound
874 * card does only a recovery and the timer is back soon.
875 * This work triggers loopback_snd_timer_work()
876 */
877 schedule_work(&cable->snd_timer.event_work);
878 }
879}
880
881static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm,
882 struct snd_info_buffer *buffer)
883{
884 snd_iprintf(buffer, " update_pending:\t%u\n",
885 dpcm->period_update_pending);
886 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
887 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
888 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
889 dpcm->last_jiffies, jiffies);
890 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
891}
892
893static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm,
894 struct snd_info_buffer *buffer)
895{
896 struct loopback_cable *cable = dpcm->cable;
897
898 snd_iprintf(buffer, " sound timer:\thw:%d,%d,%d\n",
899 cable->snd_timer.id.card,
900 cable->snd_timer.id.device,
901 cable->snd_timer.id.subdevice);
902 snd_iprintf(buffer, " timer open:\t\t%s\n",
903 (cable->snd_timer.stream == SNDRV_PCM_STREAM_CAPTURE) ?
904 "capture" : "playback");
905}
906
907static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
908{
909 struct snd_pcm_runtime *runtime = substream->runtime;
910 struct loopback_pcm *dpcm = runtime->private_data;
911 snd_pcm_uframes_t pos;
912
913 spin_lock(&dpcm->cable->lock);
914 if (dpcm->cable->ops->pos_update)
915 dpcm->cable->ops->pos_update(dpcm->cable);
916 pos = dpcm->buf_pos;
917 spin_unlock(&dpcm->cable->lock);
918 return bytes_to_frames(runtime, pos);
919}
920
921static const struct snd_pcm_hardware loopback_pcm_hardware =
922{
923 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
924 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
925 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_NONINTERLEAVED),
926 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
927 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
928 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
929 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
930 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
931 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
932 .rate_min = 8000,
933 .rate_max = 192000,
934 .channels_min = 1,
935 .channels_max = 32,
936 .buffer_bytes_max = 2 * 1024 * 1024,
937 .period_bytes_min = 64,
938 /* note check overflow in frac_pos() using pcm_rate_shift before
939 changing period_bytes_max value */
940 .period_bytes_max = 1024 * 1024,
941 .periods_min = 1,
942 .periods_max = 1024,
943 .fifo_size = 0,
944};
945
946static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
947{
948 struct loopback_pcm *dpcm = runtime->private_data;
949 kfree(dpcm);
950}
951
952static int loopback_hw_free(struct snd_pcm_substream *substream)
953{
954 struct snd_pcm_runtime *runtime = substream->runtime;
955 struct loopback_pcm *dpcm = runtime->private_data;
956 struct loopback_cable *cable = dpcm->cable;
957
958 mutex_lock(&dpcm->loopback->cable_lock);
959 cable->valid &= ~(1 << substream->stream);
960 mutex_unlock(&dpcm->loopback->cable_lock);
961 return 0;
962}
963
964static unsigned int get_cable_index(struct snd_pcm_substream *substream)
965{
966 if (!substream->pcm->device)
967 return substream->stream;
968 else
969 return !substream->stream;
970}
971
972static int rule_format(struct snd_pcm_hw_params *params,
973 struct snd_pcm_hw_rule *rule)
974{
975 struct loopback_pcm *dpcm = rule->private;
976 struct loopback_cable *cable = dpcm->cable;
977 struct snd_mask m;
978
979 snd_mask_none(&m);
980 mutex_lock(&dpcm->loopback->cable_lock);
981 m.bits[0] = (u_int32_t)cable->hw.formats;
982 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
983 mutex_unlock(&dpcm->loopback->cable_lock);
984 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
985}
986
987static int rule_rate(struct snd_pcm_hw_params *params,
988 struct snd_pcm_hw_rule *rule)
989{
990 struct loopback_pcm *dpcm = rule->private;
991 struct loopback_cable *cable = dpcm->cable;
992 struct snd_interval t;
993
994 mutex_lock(&dpcm->loopback->cable_lock);
995 t.min = cable->hw.rate_min;
996 t.max = cable->hw.rate_max;
997 mutex_unlock(&dpcm->loopback->cable_lock);
998 t.openmin = t.openmax = 0;
999 t.integer = 0;
1000 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1001}
1002
1003static int rule_channels(struct snd_pcm_hw_params *params,
1004 struct snd_pcm_hw_rule *rule)
1005{
1006 struct loopback_pcm *dpcm = rule->private;
1007 struct loopback_cable *cable = dpcm->cable;
1008 struct snd_interval t;
1009
1010 mutex_lock(&dpcm->loopback->cable_lock);
1011 t.min = cable->hw.channels_min;
1012 t.max = cable->hw.channels_max;
1013 mutex_unlock(&dpcm->loopback->cable_lock);
1014 t.openmin = t.openmax = 0;
1015 t.integer = 0;
1016 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1017}
1018
1019static int rule_period_bytes(struct snd_pcm_hw_params *params,
1020 struct snd_pcm_hw_rule *rule)
1021{
1022 struct loopback_pcm *dpcm = rule->private;
1023 struct loopback_cable *cable = dpcm->cable;
1024 struct snd_interval t;
1025
1026 mutex_lock(&dpcm->loopback->cable_lock);
1027 t.min = cable->hw.period_bytes_min;
1028 t.max = cable->hw.period_bytes_max;
1029 mutex_unlock(&dpcm->loopback->cable_lock);
1030 t.openmin = 0;
1031 t.openmax = 0;
1032 t.integer = 0;
1033 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1034}
1035
1036static void free_cable(struct snd_pcm_substream *substream)
1037{
1038 struct loopback *loopback = substream->private_data;
1039 int dev = get_cable_index(substream);
1040 struct loopback_cable *cable;
1041
1042 cable = loopback->cables[substream->number][dev];
1043 if (!cable)
1044 return;
1045 if (cable->streams[!substream->stream]) {
1046 /* other stream is still alive */
1047 spin_lock_irq(&cable->lock);
1048 cable->streams[substream->stream] = NULL;
1049 spin_unlock_irq(&cable->lock);
1050 } else {
1051 struct loopback_pcm *dpcm = substream->runtime->private_data;
1052
1053 if (cable->ops && cable->ops->close_cable && dpcm)
1054 cable->ops->close_cable(dpcm);
1055 /* free the cable */
1056 loopback->cables[substream->number][dev] = NULL;
1057 kfree(cable);
1058 }
1059}
1060
1061static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm)
1062{
1063 timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0);
1064
1065 return 0;
1066}
1067
1068static const struct loopback_ops loopback_jiffies_timer_ops = {
1069 .open = loopback_jiffies_timer_open,
1070 .start = loopback_jiffies_timer_start,
1071 .stop = loopback_jiffies_timer_stop,
1072 .stop_sync = loopback_jiffies_timer_stop_sync,
1073 .close_substream = loopback_jiffies_timer_stop_sync,
1074 .pos_update = loopback_jiffies_timer_pos_update,
1075 .dpcm_info = loopback_jiffies_timer_dpcm_info,
1076};
1077
1078static int loopback_parse_timer_id(const char *str,
1079 struct snd_timer_id *tid)
1080{
1081 /* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */
1082 const char * const sep_dev = ".,";
1083 const char * const sep_pref = ":";
1084 const char *name = str;
1085 char *sep, save = '\0';
1086 int card_idx = 0, dev = 0, subdev = 0;
1087 int err;
1088
1089 sep = strpbrk(str, sep_pref);
1090 if (sep)
1091 name = sep + 1;
1092 sep = strpbrk(name, sep_dev);
1093 if (sep) {
1094 save = *sep;
1095 *sep = '\0';
1096 }
1097 err = kstrtoint(name, 0, &card_idx);
1098 if (err == -EINVAL) {
1099 /* Must be the name, not number */
1100 for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) {
1101 struct snd_card *card = snd_card_ref(card_idx);
1102
1103 if (card) {
1104 if (!strcmp(card->id, name))
1105 err = 0;
1106 snd_card_unref(card);
1107 }
1108 if (!err)
1109 break;
1110 }
1111 }
1112 if (sep) {
1113 *sep = save;
1114 if (!err) {
1115 char *sep2, save2 = '\0';
1116
1117 sep2 = strpbrk(sep + 1, sep_dev);
1118 if (sep2) {
1119 save2 = *sep2;
1120 *sep2 = '\0';
1121 }
1122 err = kstrtoint(sep + 1, 0, &dev);
1123 if (sep2) {
1124 *sep2 = save2;
1125 if (!err)
1126 err = kstrtoint(sep2 + 1, 0, &subdev);
1127 }
1128 }
1129 }
1130 if (!err && tid) {
1131 tid->card = card_idx;
1132 tid->device = dev;
1133 tid->subdevice = subdev;
1134 }
1135 return err;
1136}
1137
1138/* call in loopback->cable_lock */
1139static int loopback_snd_timer_open(struct loopback_pcm *dpcm)
1140{
1141 int err = 0;
1142 struct snd_timer_id tid = {
1143 .dev_class = SNDRV_TIMER_CLASS_PCM,
1144 .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION,
1145 };
1146 struct snd_timer_instance *timeri;
1147 struct loopback_cable *cable = dpcm->cable;
1148
1149 /* check if timer was already opened. It is only opened once
1150 * per playback and capture subdevice (aka cable).
1151 */
1152 if (cable->snd_timer.instance)
1153 goto exit;
1154
1155 err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid);
1156 if (err < 0) {
1157 pcm_err(dpcm->substream->pcm,
1158 "Parsing timer source \'%s\' failed with %d",
1159 dpcm->loopback->timer_source, err);
1160 goto exit;
1161 }
1162
1163 cable->snd_timer.stream = dpcm->substream->stream;
1164 cable->snd_timer.id = tid;
1165
1166 timeri = snd_timer_instance_new(dpcm->loopback->card->id);
1167 if (!timeri) {
1168 err = -ENOMEM;
1169 goto exit;
1170 }
1171 /* The callback has to be called from another work. If
1172 * SNDRV_TIMER_IFLG_FAST is specified it will be called from the
1173 * snd_pcm_period_elapsed() call of the selected sound card.
1174 * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave().
1175 * Due to our callback loopback_snd_timer_function() also calls
1176 * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave().
1177 * This would end up in a dead lock.
1178 */
1179 timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1180 timeri->callback = loopback_snd_timer_function;
1181 timeri->callback_data = (void *)cable;
1182 timeri->ccallback = loopback_snd_timer_event;
1183
1184 /* initialise a work used for draining */
1185 INIT_WORK(&cable->snd_timer.event_work, loopback_snd_timer_work);
1186
1187 /* The mutex loopback->cable_lock is kept locked.
1188 * Therefore snd_timer_open() cannot be called a second time
1189 * by the other device of the same cable.
1190 * Therefore the following issue cannot happen:
1191 * [proc1] Call loopback_timer_open() ->
1192 * Unlock cable->lock for snd_timer_close/open() call
1193 * [proc2] Call loopback_timer_open() -> snd_timer_open(),
1194 * snd_timer_start()
1195 * [proc1] Call snd_timer_open() and overwrite running timer
1196 * instance
1197 */
1198 err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid);
1199 if (err < 0) {
1200 pcm_err(dpcm->substream->pcm,
1201 "snd_timer_open (%d,%d,%d) failed with %d",
1202 cable->snd_timer.id.card,
1203 cable->snd_timer.id.device,
1204 cable->snd_timer.id.subdevice,
1205 err);
1206 snd_timer_instance_free(timeri);
1207 goto exit;
1208 }
1209
1210 cable->snd_timer.instance = timeri;
1211
1212exit:
1213 return err;
1214}
1215
1216/* stop_sync() is not required for sound timer because it does not need to be
1217 * restarted in loopback_prepare() on Xrun recovery
1218 */
1219static const struct loopback_ops loopback_snd_timer_ops = {
1220 .open = loopback_snd_timer_open,
1221 .start = loopback_snd_timer_start,
1222 .stop = loopback_snd_timer_stop,
1223 .close_cable = loopback_snd_timer_close_cable,
1224 .dpcm_info = loopback_snd_timer_dpcm_info,
1225};
1226
1227static int loopback_open(struct snd_pcm_substream *substream)
1228{
1229 struct snd_pcm_runtime *runtime = substream->runtime;
1230 struct loopback *loopback = substream->private_data;
1231 struct loopback_pcm *dpcm;
1232 struct loopback_cable *cable = NULL;
1233 int err = 0;
1234 int dev = get_cable_index(substream);
1235
1236 mutex_lock(&loopback->cable_lock);
1237 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1238 if (!dpcm) {
1239 err = -ENOMEM;
1240 goto unlock;
1241 }
1242 dpcm->loopback = loopback;
1243 dpcm->substream = substream;
1244
1245 cable = loopback->cables[substream->number][dev];
1246 if (!cable) {
1247 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1248 if (!cable) {
1249 err = -ENOMEM;
1250 goto unlock;
1251 }
1252 spin_lock_init(&cable->lock);
1253 cable->hw = loopback_pcm_hardware;
1254 if (loopback->timer_source)
1255 cable->ops = &loopback_snd_timer_ops;
1256 else
1257 cable->ops = &loopback_jiffies_timer_ops;
1258 loopback->cables[substream->number][dev] = cable;
1259 }
1260 dpcm->cable = cable;
1261 runtime->private_data = dpcm;
1262
1263 if (cable->ops->open) {
1264 err = cable->ops->open(dpcm);
1265 if (err < 0)
1266 goto unlock;
1267 }
1268
1269 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1270
1271 /* use dynamic rules based on actual runtime->hw values */
1272 /* note that the default rules created in the PCM midlevel code */
1273 /* are cached -> they do not reflect the actual state */
1274 err = snd_pcm_hw_rule_add(runtime, 0,
1275 SNDRV_PCM_HW_PARAM_FORMAT,
1276 rule_format, dpcm,
1277 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1278 if (err < 0)
1279 goto unlock;
1280 err = snd_pcm_hw_rule_add(runtime, 0,
1281 SNDRV_PCM_HW_PARAM_RATE,
1282 rule_rate, dpcm,
1283 SNDRV_PCM_HW_PARAM_RATE, -1);
1284 if (err < 0)
1285 goto unlock;
1286 err = snd_pcm_hw_rule_add(runtime, 0,
1287 SNDRV_PCM_HW_PARAM_CHANNELS,
1288 rule_channels, dpcm,
1289 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1290 if (err < 0)
1291 goto unlock;
1292
1293 /* In case of sound timer the period time of both devices of the same
1294 * loop has to be the same.
1295 * This rule only takes effect if a sound timer was chosen
1296 */
1297 if (cable->snd_timer.instance) {
1298 err = snd_pcm_hw_rule_add(runtime, 0,
1299 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1300 rule_period_bytes, dpcm,
1301 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1);
1302 if (err < 0)
1303 goto unlock;
1304 }
1305
1306 /* loopback_runtime_free() has not to be called if kfree(dpcm) was
1307 * already called here. Otherwise it will end up with a double free.
1308 */
1309 runtime->private_free = loopback_runtime_free;
1310 if (get_notify(dpcm))
1311 runtime->hw = loopback_pcm_hardware;
1312 else
1313 runtime->hw = cable->hw;
1314
1315 spin_lock_irq(&cable->lock);
1316 cable->streams[substream->stream] = dpcm;
1317 spin_unlock_irq(&cable->lock);
1318
1319 unlock:
1320 if (err < 0) {
1321 free_cable(substream);
1322 kfree(dpcm);
1323 }
1324 mutex_unlock(&loopback->cable_lock);
1325 return err;
1326}
1327
1328static int loopback_close(struct snd_pcm_substream *substream)
1329{
1330 struct loopback *loopback = substream->private_data;
1331 struct loopback_pcm *dpcm = substream->runtime->private_data;
1332 int err = 0;
1333
1334 if (dpcm->cable->ops->close_substream)
1335 err = dpcm->cable->ops->close_substream(dpcm);
1336 mutex_lock(&loopback->cable_lock);
1337 free_cable(substream);
1338 mutex_unlock(&loopback->cable_lock);
1339 return err;
1340}
1341
1342static const struct snd_pcm_ops loopback_pcm_ops = {
1343 .open = loopback_open,
1344 .close = loopback_close,
1345 .hw_free = loopback_hw_free,
1346 .prepare = loopback_prepare,
1347 .trigger = loopback_trigger,
1348 .pointer = loopback_pointer,
1349};
1350
1351static int loopback_pcm_new(struct loopback *loopback,
1352 int device, int substreams)
1353{
1354 struct snd_pcm *pcm;
1355 int err;
1356
1357 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
1358 substreams, substreams, &pcm);
1359 if (err < 0)
1360 return err;
1361 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops);
1362 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops);
1363 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
1364
1365 pcm->private_data = loopback;
1366 pcm->info_flags = 0;
1367 strcpy(pcm->name, "Loopback PCM");
1368
1369 loopback->pcm[device] = pcm;
1370 return 0;
1371}
1372
1373static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
1374 struct snd_ctl_elem_info *uinfo)
1375{
1376 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1377 uinfo->count = 1;
1378 uinfo->value.integer.min = 80000;
1379 uinfo->value.integer.max = 120000;
1380 uinfo->value.integer.step = 1;
1381 return 0;
1382}
1383
1384static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
1385 struct snd_ctl_elem_value *ucontrol)
1386{
1387 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1388
1389 mutex_lock(&loopback->cable_lock);
1390 ucontrol->value.integer.value[0] =
1391 loopback->setup[kcontrol->id.subdevice]
1392 [kcontrol->id.device].rate_shift;
1393 mutex_unlock(&loopback->cable_lock);
1394 return 0;
1395}
1396
1397static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
1398 struct snd_ctl_elem_value *ucontrol)
1399{
1400 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1401 unsigned int val;
1402 int change = 0;
1403
1404 val = ucontrol->value.integer.value[0];
1405 if (val < 80000)
1406 val = 80000;
1407 if (val > 120000)
1408 val = 120000;
1409 mutex_lock(&loopback->cable_lock);
1410 if (val != loopback->setup[kcontrol->id.subdevice]
1411 [kcontrol->id.device].rate_shift) {
1412 loopback->setup[kcontrol->id.subdevice]
1413 [kcontrol->id.device].rate_shift = val;
1414 change = 1;
1415 }
1416 mutex_unlock(&loopback->cable_lock);
1417 return change;
1418}
1419
1420static int loopback_notify_get(struct snd_kcontrol *kcontrol,
1421 struct snd_ctl_elem_value *ucontrol)
1422{
1423 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1424
1425 mutex_lock(&loopback->cable_lock);
1426 ucontrol->value.integer.value[0] =
1427 loopback->setup[kcontrol->id.subdevice]
1428 [kcontrol->id.device].notify;
1429 mutex_unlock(&loopback->cable_lock);
1430 return 0;
1431}
1432
1433static int loopback_notify_put(struct snd_kcontrol *kcontrol,
1434 struct snd_ctl_elem_value *ucontrol)
1435{
1436 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1437 unsigned int val;
1438 int change = 0;
1439
1440 val = ucontrol->value.integer.value[0] ? 1 : 0;
1441 mutex_lock(&loopback->cable_lock);
1442 if (val != loopback->setup[kcontrol->id.subdevice]
1443 [kcontrol->id.device].notify) {
1444 loopback->setup[kcontrol->id.subdevice]
1445 [kcontrol->id.device].notify = val;
1446 change = 1;
1447 }
1448 mutex_unlock(&loopback->cable_lock);
1449 return change;
1450}
1451
1452static int loopback_active_get(struct snd_kcontrol *kcontrol,
1453 struct snd_ctl_elem_value *ucontrol)
1454{
1455 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1456 struct loopback_cable *cable;
1457
1458 unsigned int val = 0;
1459
1460 mutex_lock(&loopback->cable_lock);
1461 cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
1462 if (cable != NULL) {
1463 unsigned int running = cable->running ^ cable->pause;
1464
1465 val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
1466 }
1467 mutex_unlock(&loopback->cable_lock);
1468 ucontrol->value.integer.value[0] = val;
1469 return 0;
1470}
1471
1472static int loopback_format_info(struct snd_kcontrol *kcontrol,
1473 struct snd_ctl_elem_info *uinfo)
1474{
1475 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1476 uinfo->count = 1;
1477 uinfo->value.integer.min = 0;
1478 uinfo->value.integer.max = (__force int)SNDRV_PCM_FORMAT_LAST;
1479 uinfo->value.integer.step = 1;
1480 return 0;
1481}
1482
1483static int loopback_format_get(struct snd_kcontrol *kcontrol,
1484 struct snd_ctl_elem_value *ucontrol)
1485{
1486 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1487
1488 ucontrol->value.integer.value[0] =
1489 (__force int)loopback->setup[kcontrol->id.subdevice]
1490 [kcontrol->id.device].format;
1491 return 0;
1492}
1493
1494static int loopback_rate_info(struct snd_kcontrol *kcontrol,
1495 struct snd_ctl_elem_info *uinfo)
1496{
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1498 uinfo->count = 1;
1499 uinfo->value.integer.min = 0;
1500 uinfo->value.integer.max = 192000;
1501 uinfo->value.integer.step = 1;
1502 return 0;
1503}
1504
1505static int loopback_rate_get(struct snd_kcontrol *kcontrol,
1506 struct snd_ctl_elem_value *ucontrol)
1507{
1508 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1509
1510 mutex_lock(&loopback->cable_lock);
1511 ucontrol->value.integer.value[0] =
1512 loopback->setup[kcontrol->id.subdevice]
1513 [kcontrol->id.device].rate;
1514 mutex_unlock(&loopback->cable_lock);
1515 return 0;
1516}
1517
1518static int loopback_channels_info(struct snd_kcontrol *kcontrol,
1519 struct snd_ctl_elem_info *uinfo)
1520{
1521 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1522 uinfo->count = 1;
1523 uinfo->value.integer.min = 1;
1524 uinfo->value.integer.max = 1024;
1525 uinfo->value.integer.step = 1;
1526 return 0;
1527}
1528
1529static int loopback_channels_get(struct snd_kcontrol *kcontrol,
1530 struct snd_ctl_elem_value *ucontrol)
1531{
1532 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1533
1534 mutex_lock(&loopback->cable_lock);
1535 ucontrol->value.integer.value[0] =
1536 loopback->setup[kcontrol->id.subdevice]
1537 [kcontrol->id.device].channels;
1538 mutex_unlock(&loopback->cable_lock);
1539 return 0;
1540}
1541
1542static int loopback_access_info(struct snd_kcontrol *kcontrol,
1543 struct snd_ctl_elem_info *uinfo)
1544{
1545 const char * const texts[] = {"Interleaved", "Non-interleaved"};
1546
1547 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1548}
1549
1550static int loopback_access_get(struct snd_kcontrol *kcontrol,
1551 struct snd_ctl_elem_value *ucontrol)
1552{
1553 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1554 snd_pcm_access_t access;
1555
1556 mutex_lock(&loopback->cable_lock);
1557 access = loopback->setup[kcontrol->id.subdevice][kcontrol->id.device].access;
1558
1559 ucontrol->value.enumerated.item[0] = !is_access_interleaved(access);
1560
1561 mutex_unlock(&loopback->cable_lock);
1562 return 0;
1563}
1564
1565static const struct snd_kcontrol_new loopback_controls[] = {
1566{
1567 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1568 .name = "PCM Rate Shift 100000",
1569 .info = loopback_rate_shift_info,
1570 .get = loopback_rate_shift_get,
1571 .put = loopback_rate_shift_put,
1572},
1573{
1574 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1575 .name = "PCM Notify",
1576 .info = snd_ctl_boolean_mono_info,
1577 .get = loopback_notify_get,
1578 .put = loopback_notify_put,
1579},
1580#define ACTIVE_IDX 2
1581{
1582 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1583 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1584 .name = "PCM Slave Active",
1585 .info = snd_ctl_boolean_mono_info,
1586 .get = loopback_active_get,
1587},
1588#define FORMAT_IDX 3
1589{
1590 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1591 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1592 .name = "PCM Slave Format",
1593 .info = loopback_format_info,
1594 .get = loopback_format_get
1595},
1596#define RATE_IDX 4
1597{
1598 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1599 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1600 .name = "PCM Slave Rate",
1601 .info = loopback_rate_info,
1602 .get = loopback_rate_get
1603},
1604#define CHANNELS_IDX 5
1605{
1606 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1607 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1608 .name = "PCM Slave Channels",
1609 .info = loopback_channels_info,
1610 .get = loopback_channels_get
1611},
1612#define ACCESS_IDX 6
1613{
1614 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1615 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1616 .name = "PCM Slave Access Mode",
1617 .info = loopback_access_info,
1618 .get = loopback_access_get,
1619},
1620};
1621
1622static int loopback_mixer_new(struct loopback *loopback, int notify)
1623{
1624 struct snd_card *card = loopback->card;
1625 struct snd_pcm *pcm;
1626 struct snd_kcontrol *kctl;
1627 struct loopback_setup *setup;
1628 int err, dev, substr, substr_count, idx;
1629
1630 strcpy(card->mixername, "Loopback Mixer");
1631 for (dev = 0; dev < 2; dev++) {
1632 pcm = loopback->pcm[dev];
1633 substr_count =
1634 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1635 for (substr = 0; substr < substr_count; substr++) {
1636 setup = &loopback->setup[substr][dev];
1637 setup->notify = notify;
1638 setup->rate_shift = NO_PITCH;
1639 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1640 setup->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1641 setup->rate = 48000;
1642 setup->channels = 2;
1643 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1644 idx++) {
1645 kctl = snd_ctl_new1(&loopback_controls[idx],
1646 loopback);
1647 if (!kctl)
1648 return -ENOMEM;
1649 kctl->id.device = dev;
1650 kctl->id.subdevice = substr;
1651
1652 /* Add the control before copying the id so that
1653 * the numid field of the id is set in the copy.
1654 */
1655 err = snd_ctl_add(card, kctl);
1656 if (err < 0)
1657 return err;
1658
1659 switch (idx) {
1660 case ACTIVE_IDX:
1661 setup->active_id = kctl->id;
1662 break;
1663 case FORMAT_IDX:
1664 setup->format_id = kctl->id;
1665 break;
1666 case RATE_IDX:
1667 setup->rate_id = kctl->id;
1668 break;
1669 case CHANNELS_IDX:
1670 setup->channels_id = kctl->id;
1671 break;
1672 case ACCESS_IDX:
1673 setup->access_id = kctl->id;
1674 break;
1675 default:
1676 break;
1677 }
1678 }
1679 }
1680 }
1681 return 0;
1682}
1683
1684static void print_dpcm_info(struct snd_info_buffer *buffer,
1685 struct loopback_pcm *dpcm,
1686 const char *id)
1687{
1688 snd_iprintf(buffer, " %s\n", id);
1689 if (dpcm == NULL) {
1690 snd_iprintf(buffer, " inactive\n");
1691 return;
1692 }
1693 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1694 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1695 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1696 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1697 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1698 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1699 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1700 if (dpcm->cable->ops->dpcm_info)
1701 dpcm->cable->ops->dpcm_info(dpcm, buffer);
1702}
1703
1704static void print_substream_info(struct snd_info_buffer *buffer,
1705 struct loopback *loopback,
1706 int sub,
1707 int num)
1708{
1709 struct loopback_cable *cable = loopback->cables[sub][num];
1710
1711 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1712 if (cable == NULL) {
1713 snd_iprintf(buffer, " inactive\n");
1714 return;
1715 }
1716 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1717 snd_iprintf(buffer, " running: %u\n", cable->running);
1718 snd_iprintf(buffer, " pause: %u\n", cable->pause);
1719 print_dpcm_info(buffer, cable->streams[0], "Playback");
1720 print_dpcm_info(buffer, cable->streams[1], "Capture");
1721}
1722
1723static void print_cable_info(struct snd_info_entry *entry,
1724 struct snd_info_buffer *buffer)
1725{
1726 struct loopback *loopback = entry->private_data;
1727 int sub, num;
1728
1729 mutex_lock(&loopback->cable_lock);
1730 num = entry->name[strlen(entry->name)-1];
1731 num = num == '0' ? 0 : 1;
1732 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1733 print_substream_info(buffer, loopback, sub, num);
1734 mutex_unlock(&loopback->cable_lock);
1735}
1736
1737static int loopback_cable_proc_new(struct loopback *loopback, int cidx)
1738{
1739 char name[32];
1740
1741 snprintf(name, sizeof(name), "cable#%d", cidx);
1742 return snd_card_ro_proc_new(loopback->card, name, loopback,
1743 print_cable_info);
1744}
1745
1746static void loopback_set_timer_source(struct loopback *loopback,
1747 const char *value)
1748{
1749 if (loopback->timer_source) {
1750 devm_kfree(loopback->card->dev, loopback->timer_source);
1751 loopback->timer_source = NULL;
1752 }
1753 if (value && *value)
1754 loopback->timer_source = devm_kstrdup(loopback->card->dev,
1755 value, GFP_KERNEL);
1756}
1757
1758static void print_timer_source_info(struct snd_info_entry *entry,
1759 struct snd_info_buffer *buffer)
1760{
1761 struct loopback *loopback = entry->private_data;
1762
1763 mutex_lock(&loopback->cable_lock);
1764 snd_iprintf(buffer, "%s\n",
1765 loopback->timer_source ? loopback->timer_source : "");
1766 mutex_unlock(&loopback->cable_lock);
1767}
1768
1769static void change_timer_source_info(struct snd_info_entry *entry,
1770 struct snd_info_buffer *buffer)
1771{
1772 struct loopback *loopback = entry->private_data;
1773 char line[64];
1774
1775 mutex_lock(&loopback->cable_lock);
1776 if (!snd_info_get_line(buffer, line, sizeof(line)))
1777 loopback_set_timer_source(loopback, strim(line));
1778 mutex_unlock(&loopback->cable_lock);
1779}
1780
1781static int loopback_timer_source_proc_new(struct loopback *loopback)
1782{
1783 return snd_card_rw_proc_new(loopback->card, "timer_source", loopback,
1784 print_timer_source_info,
1785 change_timer_source_info);
1786}
1787
1788static int loopback_probe(struct platform_device *devptr)
1789{
1790 struct snd_card *card;
1791 struct loopback *loopback;
1792 int dev = devptr->id;
1793 int err;
1794
1795 err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1796 sizeof(struct loopback), &card);
1797 if (err < 0)
1798 return err;
1799 loopback = card->private_data;
1800
1801 if (pcm_substreams[dev] < 1)
1802 pcm_substreams[dev] = 1;
1803 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1804 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1805
1806 loopback->card = card;
1807 loopback_set_timer_source(loopback, timer_source[dev]);
1808
1809 mutex_init(&loopback->cable_lock);
1810
1811 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1812 if (err < 0)
1813 return err;
1814 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1815 if (err < 0)
1816 return err;
1817 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1818 if (err < 0)
1819 return err;
1820 loopback_cable_proc_new(loopback, 0);
1821 loopback_cable_proc_new(loopback, 1);
1822 loopback_timer_source_proc_new(loopback);
1823 strcpy(card->driver, "Loopback");
1824 strcpy(card->shortname, "Loopback");
1825 sprintf(card->longname, "Loopback %i", dev + 1);
1826 err = snd_card_register(card);
1827 if (err < 0)
1828 return err;
1829 platform_set_drvdata(devptr, card);
1830 return 0;
1831}
1832
1833#ifdef CONFIG_PM_SLEEP
1834static int loopback_suspend(struct device *pdev)
1835{
1836 struct snd_card *card = dev_get_drvdata(pdev);
1837
1838 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1839 return 0;
1840}
1841
1842static int loopback_resume(struct device *pdev)
1843{
1844 struct snd_card *card = dev_get_drvdata(pdev);
1845
1846 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1847 return 0;
1848}
1849
1850static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1851#define LOOPBACK_PM_OPS &loopback_pm
1852#else
1853#define LOOPBACK_PM_OPS NULL
1854#endif
1855
1856#define SND_LOOPBACK_DRIVER "snd_aloop"
1857
1858static struct platform_driver loopback_driver = {
1859 .probe = loopback_probe,
1860 .driver = {
1861 .name = SND_LOOPBACK_DRIVER,
1862 .pm = LOOPBACK_PM_OPS,
1863 },
1864};
1865
1866static void loopback_unregister_all(void)
1867{
1868 int i;
1869
1870 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1871 platform_device_unregister(devices[i]);
1872 platform_driver_unregister(&loopback_driver);
1873}
1874
1875static int __init alsa_card_loopback_init(void)
1876{
1877 int i, err, cards;
1878
1879 err = platform_driver_register(&loopback_driver);
1880 if (err < 0)
1881 return err;
1882
1883
1884 cards = 0;
1885 for (i = 0; i < SNDRV_CARDS; i++) {
1886 struct platform_device *device;
1887 if (!enable[i])
1888 continue;
1889 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1890 i, NULL, 0);
1891 if (IS_ERR(device))
1892 continue;
1893 if (!platform_get_drvdata(device)) {
1894 platform_device_unregister(device);
1895 continue;
1896 }
1897 devices[i] = device;
1898 cards++;
1899 }
1900 if (!cards) {
1901#ifdef MODULE
1902 printk(KERN_ERR "aloop: No loopback enabled\n");
1903#endif
1904 loopback_unregister_all();
1905 return -ENODEV;
1906 }
1907 return 0;
1908}
1909
1910static void __exit alsa_card_loopback_exit(void)
1911{
1912 loopback_unregister_all();
1913}
1914
1915module_init(alsa_card_loopback_init)
1916module_exit(alsa_card_loopback_exit)