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