blob: 2adc88d6d507961cca3b645f7b7b9780b4eb9110 [file] [log] [blame]
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001/*
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>
Paul Gortmaker65a77212011-07-15 13:13:37 -040037#include <linux/module.h>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020038#include <linux/platform_device.h>
39#include <sound/core.h>
40#include <sound/control.h>
41#include <sound/pcm.h>
Jaroslav Kyselae74670b2010-10-18 09:43:10 +020042#include <sound/info.h>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020043#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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103054static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
Jaroslav Kysela597603d2010-08-09 14:21:11 +020055static 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;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +020080 unsigned int pause;
Jaroslav Kysela597603d2010-08-09 14:21:11 +020081};
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;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200120 unsigned int last_drift;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200121 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
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200168/* call in cable->lock */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200169static 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 }
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200178 if (dpcm->period_size_frac <= dpcm->irq_pos) {
179 dpcm->irq_pos %= dpcm->period_size_frac;
180 dpcm->period_update_pending = 1;
181 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200182 tick = dpcm->period_size_frac - dpcm->irq_pos;
183 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
Takashi Iwaidb974552015-01-19 11:27:31 +0100184 mod_timer(&dpcm->timer, jiffies + tick);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200185}
186
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200187/* call in cable->lock */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200188static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
189{
190 del_timer(&dpcm->timer);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +0200191 dpcm->timer.expires = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200192}
193
194#define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
195#define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
196#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
197
198static int loopback_check_format(struct loopback_cable *cable, int stream)
199{
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200200 struct snd_pcm_runtime *runtime, *cruntime;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200201 struct loopback_setup *setup;
202 struct snd_card *card;
203 int check;
204
205 if (cable->valid != CABLE_VALID_BOTH) {
206 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
207 goto __notify;
208 return 0;
209 }
210 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
211 substream->runtime;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200212 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
213 substream->runtime;
214 check = runtime->format != cruntime->format ||
215 runtime->rate != cruntime->rate ||
216 runtime->channels != cruntime->channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200217 if (!check)
218 return 0;
219 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
220 return -EIO;
221 } else {
222 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
223 substream, SNDRV_PCM_STATE_DRAINING);
224 __notify:
225 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
226 substream->runtime;
227 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
228 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
229 if (setup->format != runtime->format) {
230 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
231 &setup->format_id);
232 setup->format = runtime->format;
233 }
234 if (setup->rate != runtime->rate) {
235 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
236 &setup->rate_id);
237 setup->rate = runtime->rate;
238 }
239 if (setup->channels != runtime->channels) {
240 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
241 &setup->channels_id);
242 setup->channels = runtime->channels;
243 }
244 }
245 return 0;
246}
247
248static void loopback_active_notify(struct loopback_pcm *dpcm)
249{
250 snd_ctl_notify(dpcm->loopback->card,
251 SNDRV_CTL_EVENT_MASK_VALUE,
252 &get_setup(dpcm)->active_id);
253}
254
255static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
256{
257 struct snd_pcm_runtime *runtime = substream->runtime;
258 struct loopback_pcm *dpcm = runtime->private_data;
259 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200260 int err, stream = 1 << substream->stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200261
262 switch (cmd) {
263 case SNDRV_PCM_TRIGGER_START:
264 err = loopback_check_format(cable, substream->stream);
265 if (err < 0)
266 return err;
267 dpcm->last_jiffies = jiffies;
268 dpcm->pcm_rate_shift = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200269 dpcm->last_drift = 0;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200270 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200271 cable->running |= stream;
272 cable->pause &= ~stream;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200273 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200274 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
276 loopback_active_notify(dpcm);
277 break;
278 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200279 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200280 cable->running &= ~stream;
281 cable->pause &= ~stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200282 loopback_timer_stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200283 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200284 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
285 loopback_active_notify(dpcm);
286 break;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200287 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100288 case SNDRV_PCM_TRIGGER_SUSPEND:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200289 spin_lock(&cable->lock);
290 cable->pause |= stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200291 loopback_timer_stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200292 spin_unlock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200293 break;
294 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100295 case SNDRV_PCM_TRIGGER_RESUME:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200296 spin_lock(&cable->lock);
297 dpcm->last_jiffies = jiffies;
298 cable->pause &= ~stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200299 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200300 spin_unlock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200301 break;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200302 default:
303 return -EINVAL;
304 }
305 return 0;
306}
307
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200308static void params_change_substream(struct loopback_pcm *dpcm,
309 struct snd_pcm_runtime *runtime)
310{
311 struct snd_pcm_runtime *dst_runtime;
312
313 if (dpcm == NULL || dpcm->substream == NULL)
314 return;
315 dst_runtime = dpcm->substream->runtime;
316 if (dst_runtime == NULL)
317 return;
318 dst_runtime->hw = dpcm->cable->hw;
319}
320
321static void params_change(struct snd_pcm_substream *substream)
322{
323 struct snd_pcm_runtime *runtime = substream->runtime;
324 struct loopback_pcm *dpcm = runtime->private_data;
325 struct loopback_cable *cable = dpcm->cable;
326
Eldad Zack74c34ca2013-04-23 01:00:41 +0200327 cable->hw.formats = pcm_format_to_bits(runtime->format);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200328 cable->hw.rate_min = runtime->rate;
329 cable->hw.rate_max = runtime->rate;
330 cable->hw.channels_min = runtime->channels;
331 cable->hw.channels_max = runtime->channels;
332 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
333 runtime);
334 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
335 runtime);
336}
337
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200338static int loopback_prepare(struct snd_pcm_substream *substream)
339{
340 struct snd_pcm_runtime *runtime = substream->runtime;
341 struct loopback_pcm *dpcm = runtime->private_data;
342 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200343 int bps, salign;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200344
345 salign = (snd_pcm_format_width(runtime->format) *
346 runtime->channels) / 8;
347 bps = salign * runtime->rate;
348 if (bps <= 0 || salign <= 0)
349 return -EINVAL;
350
351 dpcm->buf_pos = 0;
352 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
353 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
354 /* clear capture buffer */
355 dpcm->silent_size = dpcm->pcm_buffer_size;
356 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
357 runtime->buffer_size * runtime->channels);
358 }
359
360 dpcm->irq_pos = 0;
361 dpcm->period_update_pending = 0;
362 dpcm->pcm_bps = bps;
363 dpcm->pcm_salign = salign;
364 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
365
366 mutex_lock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200367 if (!(cable->valid & ~(1 << substream->stream)) ||
368 (get_setup(dpcm)->notify &&
369 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
370 params_change(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200371 cable->valid |= 1 << substream->stream;
372 mutex_unlock(&dpcm->loopback->cable_lock);
373
374 return 0;
375}
376
377static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
378{
379 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
380 char *dst = runtime->dma_area;
381 unsigned int dst_off = dpcm->buf_pos;
382
383 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
384 return;
385 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
386 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
387
388 for (;;) {
389 unsigned int size = bytes;
390 if (dst_off + size > dpcm->pcm_buffer_size)
391 size = dpcm->pcm_buffer_size - dst_off;
392 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
393 bytes_to_frames(runtime, size) *
394 runtime->channels);
395 dpcm->silent_size += size;
396 bytes -= size;
397 if (!bytes)
398 break;
399 dst_off = 0;
400 }
401}
402
403static void copy_play_buf(struct loopback_pcm *play,
404 struct loopback_pcm *capt,
405 unsigned int bytes)
406{
407 struct snd_pcm_runtime *runtime = play->substream->runtime;
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200408 char *src = runtime->dma_area;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200409 char *dst = capt->substream->runtime->dma_area;
410 unsigned int src_off = play->buf_pos;
411 unsigned int dst_off = capt->buf_pos;
412 unsigned int clear_bytes = 0;
413
414 /* check if playback is draining, trim the capture copy size
415 * when our pointer is at the end of playback ring buffer */
416 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
417 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
418 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
419 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
420 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
421 appl_ptr1 += play->buf_pos / play->pcm_salign;
422 if (appl_ptr < appl_ptr1)
423 appl_ptr1 -= runtime->buffer_size;
424 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
425 if (diff < bytes) {
426 clear_bytes = bytes - diff;
427 bytes = diff;
428 }
429 }
430
431 for (;;) {
432 unsigned int size = bytes;
433 if (src_off + size > play->pcm_buffer_size)
434 size = play->pcm_buffer_size - src_off;
435 if (dst_off + size > capt->pcm_buffer_size)
436 size = capt->pcm_buffer_size - dst_off;
437 memcpy(dst + dst_off, src + src_off, size);
438 capt->silent_size = 0;
439 bytes -= size;
440 if (!bytes)
441 break;
442 src_off = (src_off + size) % play->pcm_buffer_size;
443 dst_off = (dst_off + size) % capt->pcm_buffer_size;
444 }
445
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200446 if (clear_bytes > 0) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200447 clear_capture_buf(capt, clear_bytes);
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200448 capt->silent_size = 0;
449 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200450}
451
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200452static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
453 unsigned int jiffies_delta)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200454{
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200455 unsigned long last_pos;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200456 unsigned int delta;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200457
458 last_pos = byte_pos(dpcm, dpcm->irq_pos);
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200459 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
460 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
461 if (delta >= dpcm->last_drift)
462 delta -= dpcm->last_drift;
463 dpcm->last_drift = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200464 if (dpcm->irq_pos >= dpcm->period_size_frac) {
465 dpcm->irq_pos %= dpcm->period_size_frac;
466 dpcm->period_update_pending = 1;
467 }
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200468 return delta;
469}
470
471static inline void bytepos_finish(struct loopback_pcm *dpcm,
472 unsigned int delta)
473{
474 dpcm->buf_pos += delta;
475 dpcm->buf_pos %= dpcm->pcm_buffer_size;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200476}
477
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200478/* call in cable->lock */
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200479static unsigned int loopback_pos_update(struct loopback_cable *cable)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200480{
481 struct loopback_pcm *dpcm_play =
482 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
483 struct loopback_pcm *dpcm_capt =
484 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
485 unsigned long delta_play = 0, delta_capt = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200486 unsigned int running, count1, count2;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200487
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200488 running = cable->running ^ cable->pause;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200489 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200490 delta_play = jiffies - dpcm_play->last_jiffies;
491 dpcm_play->last_jiffies += delta_play;
492 }
493
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200494 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200495 delta_capt = jiffies - dpcm_capt->last_jiffies;
496 dpcm_capt->last_jiffies += delta_capt;
497 }
498
Takashi Iwai98d21df2011-03-18 07:31:53 +0100499 if (delta_play == 0 && delta_capt == 0)
500 goto unlock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200501
502 if (delta_play > delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200503 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
504 bytepos_finish(dpcm_play, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200505 delta_play = delta_capt;
506 } else if (delta_play < delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200507 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
508 clear_capture_buf(dpcm_capt, count1);
509 bytepos_finish(dpcm_capt, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200510 delta_capt = delta_play;
511 }
512
Takashi Iwai98d21df2011-03-18 07:31:53 +0100513 if (delta_play == 0 && delta_capt == 0)
514 goto unlock;
515
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200516 /* note delta_capt == delta_play at this moment */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200517 count1 = bytepos_delta(dpcm_play, delta_play);
518 count2 = bytepos_delta(dpcm_capt, delta_capt);
519 if (count1 < count2) {
520 dpcm_capt->last_drift = count2 - count1;
521 count1 = count2;
522 } else if (count1 > count2) {
523 dpcm_play->last_drift = count1 - count2;
524 }
525 copy_play_buf(dpcm_play, dpcm_capt, count1);
526 bytepos_finish(dpcm_play, count1);
527 bytepos_finish(dpcm_capt, count1);
Takashi Iwai98d21df2011-03-18 07:31:53 +0100528 unlock:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200529 return running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200530}
531
532static void loopback_timer_function(unsigned long data)
533{
534 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200535 unsigned long flags;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200536
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200537 spin_lock_irqsave(&dpcm->cable->lock, flags);
538 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200539 loopback_timer_start(dpcm);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200540 if (dpcm->period_update_pending) {
541 dpcm->period_update_pending = 0;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200542 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
543 /* need to unlock before calling below */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200544 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200545 return;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200546 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200547 }
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200548 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200549}
550
551static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
552{
553 struct snd_pcm_runtime *runtime = substream->runtime;
554 struct loopback_pcm *dpcm = runtime->private_data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200555 snd_pcm_uframes_t pos;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200556
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200557 spin_lock(&dpcm->cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200558 loopback_pos_update(dpcm->cable);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200559 pos = dpcm->buf_pos;
560 spin_unlock(&dpcm->cable->lock);
561 return bytes_to_frames(runtime, pos);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200562}
563
564static struct snd_pcm_hardware loopback_pcm_hardware =
565{
566 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
Takashi Iwaiedac8942013-02-04 10:28:15 +0100567 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
568 SNDRV_PCM_INFO_RESUME),
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200569 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
570 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
571 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
572 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
573 .rate_min = 8000,
574 .rate_max = 192000,
575 .channels_min = 1,
576 .channels_max = 32,
577 .buffer_bytes_max = 2 * 1024 * 1024,
578 .period_bytes_min = 64,
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200579 /* note check overflow in frac_pos() using pcm_rate_shift before
580 changing period_bytes_max value */
581 .period_bytes_max = 1024 * 1024,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200582 .periods_min = 1,
583 .periods_max = 1024,
584 .fifo_size = 0,
585};
586
587static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
588{
589 struct loopback_pcm *dpcm = runtime->private_data;
590 kfree(dpcm);
591}
592
593static int loopback_hw_params(struct snd_pcm_substream *substream,
594 struct snd_pcm_hw_params *params)
595{
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200596 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
597 params_buffer_bytes(params));
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200598}
599
600static int loopback_hw_free(struct snd_pcm_substream *substream)
601{
602 struct snd_pcm_runtime *runtime = substream->runtime;
603 struct loopback_pcm *dpcm = runtime->private_data;
604 struct loopback_cable *cable = dpcm->cable;
605
606 mutex_lock(&dpcm->loopback->cable_lock);
607 cable->valid &= ~(1 << substream->stream);
608 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200609 return snd_pcm_lib_free_vmalloc_buffer(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200610}
611
612static unsigned int get_cable_index(struct snd_pcm_substream *substream)
613{
614 if (!substream->pcm->device)
615 return substream->stream;
616 else
617 return !substream->stream;
618}
619
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200620static int rule_format(struct snd_pcm_hw_params *params,
621 struct snd_pcm_hw_rule *rule)
622{
623
624 struct snd_pcm_hardware *hw = rule->private;
625 struct snd_mask *maskp = hw_param_mask(params, rule->var);
626
627 maskp->bits[0] &= (u_int32_t)hw->formats;
628 maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
629 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
630 if (! maskp->bits[0] && ! maskp->bits[1])
631 return -EINVAL;
632 return 0;
633}
634
635static int rule_rate(struct snd_pcm_hw_params *params,
636 struct snd_pcm_hw_rule *rule)
637{
638 struct snd_pcm_hardware *hw = rule->private;
639 struct snd_interval t;
640
641 t.min = hw->rate_min;
642 t.max = hw->rate_max;
643 t.openmin = t.openmax = 0;
644 t.integer = 0;
645 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
646}
647
648static int rule_channels(struct snd_pcm_hw_params *params,
649 struct snd_pcm_hw_rule *rule)
650{
651 struct snd_pcm_hardware *hw = rule->private;
652 struct snd_interval t;
653
654 t.min = hw->channels_min;
655 t.max = hw->channels_max;
656 t.openmin = t.openmax = 0;
657 t.integer = 0;
658 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
659}
660
Takashi Iwai01046dd2018-01-05 16:09:47 +0100661static void free_cable(struct snd_pcm_substream *substream)
662{
663 struct loopback *loopback = substream->private_data;
664 int dev = get_cable_index(substream);
665 struct loopback_cable *cable;
666
667 cable = loopback->cables[substream->number][dev];
668 if (!cable)
669 return;
670 if (cable->streams[!substream->stream]) {
671 /* other stream is still alive */
672 cable->streams[substream->stream] = NULL;
673 } else {
674 /* free the cable */
675 loopback->cables[substream->number][dev] = NULL;
676 kfree(cable);
677 }
678}
679
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200680static int loopback_open(struct snd_pcm_substream *substream)
681{
682 struct snd_pcm_runtime *runtime = substream->runtime;
683 struct loopback *loopback = substream->private_data;
684 struct loopback_pcm *dpcm;
Takashi Iwai01046dd2018-01-05 16:09:47 +0100685 struct loopback_cable *cable = NULL;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200686 int err = 0;
687 int dev = get_cable_index(substream);
688
689 mutex_lock(&loopback->cable_lock);
690 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
691 if (!dpcm) {
692 err = -ENOMEM;
693 goto unlock;
694 }
695 dpcm->loopback = loopback;
696 dpcm->substream = substream;
697 setup_timer(&dpcm->timer, loopback_timer_function,
698 (unsigned long)dpcm);
699
700 cable = loopback->cables[substream->number][dev];
701 if (!cable) {
702 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
703 if (!cable) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200704 err = -ENOMEM;
705 goto unlock;
706 }
707 spin_lock_init(&cable->lock);
708 cable->hw = loopback_pcm_hardware;
709 loopback->cables[substream->number][dev] = cable;
710 }
711 dpcm->cable = cable;
712 cable->streams[substream->stream] = dpcm;
713
714 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
715
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200716 /* use dynamic rules based on actual runtime->hw values */
717 /* note that the default rules created in the PCM midlevel code */
718 /* are cached -> they do not reflect the actual state */
719 err = snd_pcm_hw_rule_add(runtime, 0,
720 SNDRV_PCM_HW_PARAM_FORMAT,
721 rule_format, &runtime->hw,
722 SNDRV_PCM_HW_PARAM_FORMAT, -1);
723 if (err < 0)
724 goto unlock;
725 err = snd_pcm_hw_rule_add(runtime, 0,
726 SNDRV_PCM_HW_PARAM_RATE,
727 rule_rate, &runtime->hw,
728 SNDRV_PCM_HW_PARAM_RATE, -1);
729 if (err < 0)
730 goto unlock;
731 err = snd_pcm_hw_rule_add(runtime, 0,
732 SNDRV_PCM_HW_PARAM_CHANNELS,
733 rule_channels, &runtime->hw,
734 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
735 if (err < 0)
736 goto unlock;
737
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200738 runtime->private_data = dpcm;
739 runtime->private_free = loopback_runtime_free;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200740 if (get_notify(dpcm))
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200741 runtime->hw = loopback_pcm_hardware;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200742 else
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200743 runtime->hw = cable->hw;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200744 unlock:
Takashi Iwai01046dd2018-01-05 16:09:47 +0100745 if (err < 0) {
746 free_cable(substream);
747 kfree(dpcm);
748 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200749 mutex_unlock(&loopback->cable_lock);
750 return err;
751}
752
753static int loopback_close(struct snd_pcm_substream *substream)
754{
755 struct loopback *loopback = substream->private_data;
756 struct loopback_pcm *dpcm = substream->runtime->private_data;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200757
758 loopback_timer_stop(dpcm);
759 mutex_lock(&loopback->cable_lock);
Takashi Iwai01046dd2018-01-05 16:09:47 +0100760 free_cable(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200761 mutex_unlock(&loopback->cable_lock);
762 return 0;
763}
764
765static struct snd_pcm_ops loopback_playback_ops = {
766 .open = loopback_open,
767 .close = loopback_close,
768 .ioctl = snd_pcm_lib_ioctl,
769 .hw_params = loopback_hw_params,
770 .hw_free = loopback_hw_free,
771 .prepare = loopback_prepare,
772 .trigger = loopback_trigger,
773 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200774 .page = snd_pcm_lib_get_vmalloc_page,
775 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200776};
777
778static struct snd_pcm_ops loopback_capture_ops = {
779 .open = loopback_open,
780 .close = loopback_close,
781 .ioctl = snd_pcm_lib_ioctl,
782 .hw_params = loopback_hw_params,
783 .hw_free = loopback_hw_free,
784 .prepare = loopback_prepare,
785 .trigger = loopback_trigger,
786 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200787 .page = snd_pcm_lib_get_vmalloc_page,
788 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200789};
790
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500791static int loopback_pcm_new(struct loopback *loopback,
792 int device, int substreams)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200793{
794 struct snd_pcm *pcm;
795 int err;
796
797 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
798 substreams, substreams, &pcm);
799 if (err < 0)
800 return err;
801 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
802 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
803
804 pcm->private_data = loopback;
805 pcm->info_flags = 0;
806 strcpy(pcm->name, "Loopback PCM");
807
808 loopback->pcm[device] = pcm;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200809 return 0;
810}
811
812static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
813 struct snd_ctl_elem_info *uinfo)
814{
815 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
816 uinfo->count = 1;
817 uinfo->value.integer.min = 80000;
818 uinfo->value.integer.max = 120000;
819 uinfo->value.integer.step = 1;
820 return 0;
821}
822
823static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
824 struct snd_ctl_elem_value *ucontrol)
825{
826 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
827
828 ucontrol->value.integer.value[0] =
829 loopback->setup[kcontrol->id.subdevice]
830 [kcontrol->id.device].rate_shift;
831 return 0;
832}
833
834static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
835 struct snd_ctl_elem_value *ucontrol)
836{
837 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
838 unsigned int val;
839 int change = 0;
840
841 val = ucontrol->value.integer.value[0];
842 if (val < 80000)
843 val = 80000;
844 if (val > 120000)
845 val = 120000;
846 mutex_lock(&loopback->cable_lock);
847 if (val != loopback->setup[kcontrol->id.subdevice]
848 [kcontrol->id.device].rate_shift) {
849 loopback->setup[kcontrol->id.subdevice]
850 [kcontrol->id.device].rate_shift = val;
851 change = 1;
852 }
853 mutex_unlock(&loopback->cable_lock);
854 return change;
855}
856
857static int loopback_notify_get(struct snd_kcontrol *kcontrol,
858 struct snd_ctl_elem_value *ucontrol)
859{
860 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
861
862 ucontrol->value.integer.value[0] =
863 loopback->setup[kcontrol->id.subdevice]
864 [kcontrol->id.device].notify;
865 return 0;
866}
867
868static int loopback_notify_put(struct snd_kcontrol *kcontrol,
869 struct snd_ctl_elem_value *ucontrol)
870{
871 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
872 unsigned int val;
873 int change = 0;
874
875 val = ucontrol->value.integer.value[0] ? 1 : 0;
876 if (val != loopback->setup[kcontrol->id.subdevice]
877 [kcontrol->id.device].notify) {
878 loopback->setup[kcontrol->id.subdevice]
879 [kcontrol->id.device].notify = val;
880 change = 1;
881 }
882 return change;
883}
884
885static int loopback_active_get(struct snd_kcontrol *kcontrol,
886 struct snd_ctl_elem_value *ucontrol)
887{
888 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
889 struct loopback_cable *cable = loopback->cables
Jaroslav Kyselaac446fb2010-10-02 16:00:53 +0200890 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200891 unsigned int val = 0;
892
893 if (cable != NULL)
894 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
895 1 : 0;
896 ucontrol->value.integer.value[0] = val;
897 return 0;
898}
899
900static int loopback_format_info(struct snd_kcontrol *kcontrol,
901 struct snd_ctl_elem_info *uinfo)
902{
903 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
904 uinfo->count = 1;
905 uinfo->value.integer.min = 0;
906 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
907 uinfo->value.integer.step = 1;
908 return 0;
909}
910
911static int loopback_format_get(struct snd_kcontrol *kcontrol,
912 struct snd_ctl_elem_value *ucontrol)
913{
914 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
915
916 ucontrol->value.integer.value[0] =
917 loopback->setup[kcontrol->id.subdevice]
918 [kcontrol->id.device].format;
919 return 0;
920}
921
922static int loopback_rate_info(struct snd_kcontrol *kcontrol,
923 struct snd_ctl_elem_info *uinfo)
924{
925 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
926 uinfo->count = 1;
927 uinfo->value.integer.min = 0;
928 uinfo->value.integer.max = 192000;
929 uinfo->value.integer.step = 1;
930 return 0;
931}
932
933static int loopback_rate_get(struct snd_kcontrol *kcontrol,
934 struct snd_ctl_elem_value *ucontrol)
935{
936 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
937
938 ucontrol->value.integer.value[0] =
939 loopback->setup[kcontrol->id.subdevice]
940 [kcontrol->id.device].rate;
941 return 0;
942}
943
944static int loopback_channels_info(struct snd_kcontrol *kcontrol,
945 struct snd_ctl_elem_info *uinfo)
946{
947 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
948 uinfo->count = 1;
949 uinfo->value.integer.min = 1;
950 uinfo->value.integer.max = 1024;
951 uinfo->value.integer.step = 1;
952 return 0;
953}
954
955static int loopback_channels_get(struct snd_kcontrol *kcontrol,
956 struct snd_ctl_elem_value *ucontrol)
957{
958 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
959
960 ucontrol->value.integer.value[0] =
961 loopback->setup[kcontrol->id.subdevice]
Jaroslav Kysela1446c5f2010-09-15 08:01:57 +0200962 [kcontrol->id.device].channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200963 return 0;
964}
965
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500966static struct snd_kcontrol_new loopback_controls[] = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200967{
968 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
969 .name = "PCM Rate Shift 100000",
970 .info = loopback_rate_shift_info,
971 .get = loopback_rate_shift_get,
972 .put = loopback_rate_shift_put,
973},
974{
975 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
976 .name = "PCM Notify",
977 .info = snd_ctl_boolean_mono_info,
978 .get = loopback_notify_get,
979 .put = loopback_notify_put,
980},
981#define ACTIVE_IDX 2
982{
983 .access = SNDRV_CTL_ELEM_ACCESS_READ,
984 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
985 .name = "PCM Slave Active",
986 .info = snd_ctl_boolean_mono_info,
987 .get = loopback_active_get,
988},
989#define FORMAT_IDX 3
990{
991 .access = SNDRV_CTL_ELEM_ACCESS_READ,
992 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
993 .name = "PCM Slave Format",
994 .info = loopback_format_info,
995 .get = loopback_format_get
996},
997#define RATE_IDX 4
998{
999 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1000 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1001 .name = "PCM Slave Rate",
1002 .info = loopback_rate_info,
1003 .get = loopback_rate_get
1004},
1005#define CHANNELS_IDX 5
1006{
1007 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1008 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1009 .name = "PCM Slave Channels",
1010 .info = loopback_channels_info,
1011 .get = loopback_channels_get
1012}
1013};
1014
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001015static int loopback_mixer_new(struct loopback *loopback, int notify)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001016{
1017 struct snd_card *card = loopback->card;
1018 struct snd_pcm *pcm;
1019 struct snd_kcontrol *kctl;
1020 struct loopback_setup *setup;
1021 int err, dev, substr, substr_count, idx;
1022
1023 strcpy(card->mixername, "Loopback Mixer");
1024 for (dev = 0; dev < 2; dev++) {
1025 pcm = loopback->pcm[dev];
1026 substr_count =
1027 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1028 for (substr = 0; substr < substr_count; substr++) {
1029 setup = &loopback->setup[substr][dev];
1030 setup->notify = notify;
1031 setup->rate_shift = NO_PITCH;
1032 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1033 setup->rate = 48000;
1034 setup->channels = 2;
1035 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1036 idx++) {
1037 kctl = snd_ctl_new1(&loopback_controls[idx],
1038 loopback);
1039 if (!kctl)
1040 return -ENOMEM;
1041 kctl->id.device = dev;
1042 kctl->id.subdevice = substr;
1043 switch (idx) {
1044 case ACTIVE_IDX:
1045 setup->active_id = kctl->id;
1046 break;
1047 case FORMAT_IDX:
1048 setup->format_id = kctl->id;
1049 break;
1050 case RATE_IDX:
1051 setup->rate_id = kctl->id;
1052 break;
1053 case CHANNELS_IDX:
1054 setup->channels_id = kctl->id;
1055 break;
1056 default:
1057 break;
1058 }
1059 err = snd_ctl_add(card, kctl);
1060 if (err < 0)
1061 return err;
1062 }
1063 }
1064 }
1065 return 0;
1066}
1067
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001068static void print_dpcm_info(struct snd_info_buffer *buffer,
1069 struct loopback_pcm *dpcm,
1070 const char *id)
1071{
1072 snd_iprintf(buffer, " %s\n", id);
1073 if (dpcm == NULL) {
1074 snd_iprintf(buffer, " inactive\n");
1075 return;
1076 }
1077 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1078 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1079 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1080 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1081 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1082 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1083 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1084 snd_iprintf(buffer, " update_pending:\t%u\n",
1085 dpcm->period_update_pending);
1086 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1087 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1088 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1089 dpcm->last_jiffies, jiffies);
1090 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1091}
1092
1093static void print_substream_info(struct snd_info_buffer *buffer,
1094 struct loopback *loopback,
1095 int sub,
1096 int num)
1097{
1098 struct loopback_cable *cable = loopback->cables[sub][num];
1099
1100 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1101 if (cable == NULL) {
1102 snd_iprintf(buffer, " inactive\n");
1103 return;
1104 }
1105 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1106 snd_iprintf(buffer, " running: %u\n", cable->running);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +02001107 snd_iprintf(buffer, " pause: %u\n", cable->pause);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001108 print_dpcm_info(buffer, cable->streams[0], "Playback");
1109 print_dpcm_info(buffer, cable->streams[1], "Capture");
1110}
1111
1112static void print_cable_info(struct snd_info_entry *entry,
1113 struct snd_info_buffer *buffer)
1114{
1115 struct loopback *loopback = entry->private_data;
1116 int sub, num;
1117
1118 mutex_lock(&loopback->cable_lock);
1119 num = entry->name[strlen(entry->name)-1];
1120 num = num == '0' ? 0 : 1;
1121 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1122 print_substream_info(buffer, loopback, sub, num);
1123 mutex_unlock(&loopback->cable_lock);
1124}
1125
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001126static int loopback_proc_new(struct loopback *loopback, int cidx)
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001127{
1128 char name[32];
1129 struct snd_info_entry *entry;
1130 int err;
1131
1132 snprintf(name, sizeof(name), "cable#%d", cidx);
1133 err = snd_card_proc_new(loopback->card, name, &entry);
1134 if (err < 0)
1135 return err;
1136
1137 snd_info_set_text_ops(entry, loopback, print_cable_info);
1138 return 0;
1139}
1140
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001141static int loopback_probe(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001142{
1143 struct snd_card *card;
1144 struct loopback *loopback;
1145 int dev = devptr->id;
1146 int err;
1147
Takashi Iwai5872f3f2014-01-29 12:59:08 +01001148 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1149 sizeof(struct loopback), &card);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001150 if (err < 0)
1151 return err;
1152 loopback = card->private_data;
1153
1154 if (pcm_substreams[dev] < 1)
1155 pcm_substreams[dev] = 1;
1156 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1157 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1158
1159 loopback->card = card;
1160 mutex_init(&loopback->cable_lock);
1161
1162 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1163 if (err < 0)
1164 goto __nodev;
1165 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1166 if (err < 0)
1167 goto __nodev;
1168 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1169 if (err < 0)
1170 goto __nodev;
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001171 loopback_proc_new(loopback, 0);
1172 loopback_proc_new(loopback, 1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001173 strcpy(card->driver, "Loopback");
1174 strcpy(card->shortname, "Loopback");
1175 sprintf(card->longname, "Loopback %i", dev + 1);
1176 err = snd_card_register(card);
1177 if (!err) {
1178 platform_set_drvdata(devptr, card);
1179 return 0;
1180 }
1181 __nodev:
1182 snd_card_free(card);
1183 return err;
1184}
1185
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001186static int loopback_remove(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001187{
1188 snd_card_free(platform_get_drvdata(devptr));
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001189 return 0;
1190}
1191
Takashi Iwaid34e4e02012-08-09 15:47:15 +02001192#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001193static int loopback_suspend(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001194{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001195 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001196 struct loopback *loopback = card->private_data;
1197
1198 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1199
1200 snd_pcm_suspend_all(loopback->pcm[0]);
1201 snd_pcm_suspend_all(loopback->pcm[1]);
1202 return 0;
1203}
1204
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001205static int loopback_resume(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001206{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001207 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001208
1209 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1210 return 0;
1211}
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001212
1213static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1214#define LOOPBACK_PM_OPS &loopback_pm
1215#else
1216#define LOOPBACK_PM_OPS NULL
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001217#endif
1218
1219#define SND_LOOPBACK_DRIVER "snd_aloop"
1220
1221static struct platform_driver loopback_driver = {
1222 .probe = loopback_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001223 .remove = loopback_remove,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001224 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +02001225 .name = SND_LOOPBACK_DRIVER,
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001226 .pm = LOOPBACK_PM_OPS,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001227 },
1228};
1229
1230static void loopback_unregister_all(void)
1231{
1232 int i;
1233
1234 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1235 platform_device_unregister(devices[i]);
1236 platform_driver_unregister(&loopback_driver);
1237}
1238
1239static int __init alsa_card_loopback_init(void)
1240{
1241 int i, err, cards;
1242
1243 err = platform_driver_register(&loopback_driver);
1244 if (err < 0)
1245 return err;
1246
1247
1248 cards = 0;
1249 for (i = 0; i < SNDRV_CARDS; i++) {
1250 struct platform_device *device;
1251 if (!enable[i])
1252 continue;
1253 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1254 i, NULL, 0);
1255 if (IS_ERR(device))
1256 continue;
1257 if (!platform_get_drvdata(device)) {
1258 platform_device_unregister(device);
1259 continue;
1260 }
1261 devices[i] = device;
1262 cards++;
1263 }
1264 if (!cards) {
1265#ifdef MODULE
1266 printk(KERN_ERR "aloop: No loopback enabled\n");
1267#endif
1268 loopback_unregister_all();
1269 return -ENODEV;
1270 }
1271 return 0;
1272}
1273
1274static void __exit alsa_card_loopback_exit(void)
1275{
1276 loopback_unregister_all();
1277}
1278
1279module_init(alsa_card_loopback_init)
1280module_exit(alsa_card_loopback_exit)