blob: 610d36bce2c2f1aca4124775460e4b1089c4ee53 [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>
Takashi Iwai5af666d2018-01-05 16:15:33 +010042#include <sound/pcm_params.h>
Jaroslav Kyselae74670b2010-10-18 09:43:10 +020043#include <sound/info.h>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020044#include <sound/initval.h>
45
46MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
47MODULE_DESCRIPTION("A loopback soundcard");
48MODULE_LICENSE("GPL");
49MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
50
51#define MAX_PCM_SUBSTREAMS 8
52
53static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
54static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103055static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
Jaroslav Kysela597603d2010-08-09 14:21:11 +020056static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
57static int pcm_notify[SNDRV_CARDS];
58
59module_param_array(index, int, NULL, 0444);
60MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
61module_param_array(id, charp, NULL, 0444);
62MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
63module_param_array(enable, bool, NULL, 0444);
64MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
65module_param_array(pcm_substreams, int, NULL, 0444);
66MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
67module_param_array(pcm_notify, int, NULL, 0444);
68MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
69
70#define NO_PITCH 100000
71
72struct loopback_pcm;
73
74struct loopback_cable {
75 spinlock_t lock;
76 struct loopback_pcm *streams[2];
77 struct snd_pcm_hardware hw;
78 /* flags */
79 unsigned int valid;
80 unsigned int running;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +020081 unsigned int pause;
Jaroslav Kysela597603d2010-08-09 14:21:11 +020082};
83
84struct loopback_setup {
85 unsigned int notify: 1;
86 unsigned int rate_shift;
87 unsigned int format;
88 unsigned int rate;
89 unsigned int channels;
90 struct snd_ctl_elem_id active_id;
91 struct snd_ctl_elem_id format_id;
92 struct snd_ctl_elem_id rate_id;
93 struct snd_ctl_elem_id channels_id;
94};
95
96struct loopback {
97 struct snd_card *card;
98 struct mutex cable_lock;
99 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
100 struct snd_pcm *pcm[2];
101 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
102};
103
104struct loopback_pcm {
105 struct loopback *loopback;
106 struct snd_pcm_substream *substream;
107 struct loopback_cable *cable;
108 unsigned int pcm_buffer_size;
109 unsigned int buf_pos; /* position in buffer */
110 unsigned int silent_size;
111 /* PCM parameters */
112 unsigned int pcm_period_size;
113 unsigned int pcm_bps; /* bytes per second */
114 unsigned int pcm_salign; /* bytes per sample * channels */
115 unsigned int pcm_rate_shift; /* rate shift value */
116 /* flags */
117 unsigned int period_update_pending :1;
118 /* timer stuff */
119 unsigned int irq_pos; /* fractional IRQ position */
120 unsigned int period_size_frac;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200121 unsigned int last_drift;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200122 unsigned long last_jiffies;
123 struct timer_list timer;
124};
125
126static struct platform_device *devices[SNDRV_CARDS];
127
128static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
129{
130 if (dpcm->pcm_rate_shift == NO_PITCH) {
131 x /= HZ;
132 } else {
133 x = div_u64(NO_PITCH * (unsigned long long)x,
134 HZ * (unsigned long long)dpcm->pcm_rate_shift);
135 }
136 return x - (x % dpcm->pcm_salign);
137}
138
139static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
140{
141 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
142 return x * HZ;
143 } else {
144 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
145 NO_PITCH);
146 }
147 return x;
148}
149
150static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
151{
152 int device = dpcm->substream->pstr->pcm->device;
153
154 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
155 device ^= 1;
156 return &dpcm->loopback->setup[dpcm->substream->number][device];
157}
158
159static inline unsigned int get_notify(struct loopback_pcm *dpcm)
160{
161 return get_setup(dpcm)->notify;
162}
163
164static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
165{
166 return get_setup(dpcm)->rate_shift;
167}
168
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200169/* call in cable->lock */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200170static void loopback_timer_start(struct loopback_pcm *dpcm)
171{
172 unsigned long tick;
173 unsigned int rate_shift = get_rate_shift(dpcm);
174
175 if (rate_shift != dpcm->pcm_rate_shift) {
176 dpcm->pcm_rate_shift = rate_shift;
177 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
178 }
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200179 if (dpcm->period_size_frac <= dpcm->irq_pos) {
180 dpcm->irq_pos %= dpcm->period_size_frac;
181 dpcm->period_update_pending = 1;
182 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200183 tick = dpcm->period_size_frac - dpcm->irq_pos;
184 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
Takashi Iwaidb974552015-01-19 11:27:31 +0100185 mod_timer(&dpcm->timer, jiffies + tick);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200186}
187
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200188/* call in cable->lock */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200189static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
190{
191 del_timer(&dpcm->timer);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +0200192 dpcm->timer.expires = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200193}
194
Takashi Iwaid44f3ad2018-03-22 08:56:06 +0100195static inline void loopback_timer_stop_sync(struct loopback_pcm *dpcm)
196{
197 del_timer_sync(&dpcm->timer);
198}
199
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200200#define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
201#define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
202#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
203
204static int loopback_check_format(struct loopback_cable *cable, int stream)
205{
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200206 struct snd_pcm_runtime *runtime, *cruntime;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200207 struct loopback_setup *setup;
208 struct snd_card *card;
209 int check;
210
211 if (cable->valid != CABLE_VALID_BOTH) {
212 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
213 goto __notify;
214 return 0;
215 }
216 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
217 substream->runtime;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200218 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
219 substream->runtime;
220 check = runtime->format != cruntime->format ||
221 runtime->rate != cruntime->rate ||
222 runtime->channels != cruntime->channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200223 if (!check)
224 return 0;
225 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
226 return -EIO;
227 } else {
228 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
229 substream, SNDRV_PCM_STATE_DRAINING);
230 __notify:
231 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
232 substream->runtime;
233 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
234 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
235 if (setup->format != runtime->format) {
236 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
237 &setup->format_id);
238 setup->format = runtime->format;
239 }
240 if (setup->rate != runtime->rate) {
241 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
242 &setup->rate_id);
243 setup->rate = runtime->rate;
244 }
245 if (setup->channels != runtime->channels) {
246 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
247 &setup->channels_id);
248 setup->channels = runtime->channels;
249 }
250 }
251 return 0;
252}
253
254static void loopback_active_notify(struct loopback_pcm *dpcm)
255{
256 snd_ctl_notify(dpcm->loopback->card,
257 SNDRV_CTL_EVENT_MASK_VALUE,
258 &get_setup(dpcm)->active_id);
259}
260
261static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
262{
263 struct snd_pcm_runtime *runtime = substream->runtime;
264 struct loopback_pcm *dpcm = runtime->private_data;
265 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200266 int err, stream = 1 << substream->stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200267
268 switch (cmd) {
269 case SNDRV_PCM_TRIGGER_START:
270 err = loopback_check_format(cable, substream->stream);
271 if (err < 0)
272 return err;
273 dpcm->last_jiffies = jiffies;
274 dpcm->pcm_rate_shift = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200275 dpcm->last_drift = 0;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200276 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200277 cable->running |= stream;
278 cable->pause &= ~stream;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200279 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200280 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200281 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
282 loopback_active_notify(dpcm);
283 break;
284 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200285 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200286 cable->running &= ~stream;
287 cable->pause &= ~stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200288 loopback_timer_stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200289 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200290 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
291 loopback_active_notify(dpcm);
292 break;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200293 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100294 case SNDRV_PCM_TRIGGER_SUSPEND:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200295 spin_lock(&cable->lock);
296 cable->pause |= stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200297 loopback_timer_stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200298 spin_unlock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200299 break;
300 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100301 case SNDRV_PCM_TRIGGER_RESUME:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200302 spin_lock(&cable->lock);
303 dpcm->last_jiffies = jiffies;
304 cable->pause &= ~stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200305 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200306 spin_unlock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200307 break;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200308 default:
309 return -EINVAL;
310 }
311 return 0;
312}
313
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200314static void params_change(struct snd_pcm_substream *substream)
315{
316 struct snd_pcm_runtime *runtime = substream->runtime;
317 struct loopback_pcm *dpcm = runtime->private_data;
318 struct loopback_cable *cable = dpcm->cable;
319
Eldad Zack74c34ca2013-04-23 01:00:41 +0200320 cable->hw.formats = pcm_format_to_bits(runtime->format);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200321 cable->hw.rate_min = runtime->rate;
322 cable->hw.rate_max = runtime->rate;
323 cable->hw.channels_min = runtime->channels;
324 cable->hw.channels_max = runtime->channels;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200325}
326
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200327static int loopback_prepare(struct snd_pcm_substream *substream)
328{
329 struct snd_pcm_runtime *runtime = substream->runtime;
330 struct loopback_pcm *dpcm = runtime->private_data;
331 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200332 int bps, salign;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200333
Takashi Iwaid44f3ad2018-03-22 08:56:06 +0100334 loopback_timer_stop_sync(dpcm);
335
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200336 salign = (snd_pcm_format_width(runtime->format) *
337 runtime->channels) / 8;
338 bps = salign * runtime->rate;
339 if (bps <= 0 || salign <= 0)
340 return -EINVAL;
341
342 dpcm->buf_pos = 0;
343 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
344 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
345 /* clear capture buffer */
346 dpcm->silent_size = dpcm->pcm_buffer_size;
347 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
348 runtime->buffer_size * runtime->channels);
349 }
350
351 dpcm->irq_pos = 0;
352 dpcm->period_update_pending = 0;
353 dpcm->pcm_bps = bps;
354 dpcm->pcm_salign = salign;
355 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
356
357 mutex_lock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200358 if (!(cable->valid & ~(1 << substream->stream)) ||
359 (get_setup(dpcm)->notify &&
360 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
361 params_change(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200362 cable->valid |= 1 << substream->stream;
363 mutex_unlock(&dpcm->loopback->cable_lock);
364
365 return 0;
366}
367
368static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
369{
370 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
371 char *dst = runtime->dma_area;
372 unsigned int dst_off = dpcm->buf_pos;
373
374 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
375 return;
376 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
377 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
378
379 for (;;) {
380 unsigned int size = bytes;
381 if (dst_off + size > dpcm->pcm_buffer_size)
382 size = dpcm->pcm_buffer_size - dst_off;
383 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
384 bytes_to_frames(runtime, size) *
385 runtime->channels);
386 dpcm->silent_size += size;
387 bytes -= size;
388 if (!bytes)
389 break;
390 dst_off = 0;
391 }
392}
393
394static void copy_play_buf(struct loopback_pcm *play,
395 struct loopback_pcm *capt,
396 unsigned int bytes)
397{
398 struct snd_pcm_runtime *runtime = play->substream->runtime;
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200399 char *src = runtime->dma_area;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200400 char *dst = capt->substream->runtime->dma_area;
401 unsigned int src_off = play->buf_pos;
402 unsigned int dst_off = capt->buf_pos;
403 unsigned int clear_bytes = 0;
404
405 /* check if playback is draining, trim the capture copy size
406 * when our pointer is at the end of playback ring buffer */
407 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
408 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
409 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
410 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
411 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
412 appl_ptr1 += play->buf_pos / play->pcm_salign;
413 if (appl_ptr < appl_ptr1)
414 appl_ptr1 -= runtime->buffer_size;
415 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
416 if (diff < bytes) {
417 clear_bytes = bytes - diff;
418 bytes = diff;
419 }
420 }
421
422 for (;;) {
423 unsigned int size = bytes;
424 if (src_off + size > play->pcm_buffer_size)
425 size = play->pcm_buffer_size - src_off;
426 if (dst_off + size > capt->pcm_buffer_size)
427 size = capt->pcm_buffer_size - dst_off;
428 memcpy(dst + dst_off, src + src_off, size);
429 capt->silent_size = 0;
430 bytes -= size;
431 if (!bytes)
432 break;
433 src_off = (src_off + size) % play->pcm_buffer_size;
434 dst_off = (dst_off + size) % capt->pcm_buffer_size;
435 }
436
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200437 if (clear_bytes > 0) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200438 clear_capture_buf(capt, clear_bytes);
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200439 capt->silent_size = 0;
440 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200441}
442
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200443static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
444 unsigned int jiffies_delta)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200445{
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200446 unsigned long last_pos;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200447 unsigned int delta;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200448
449 last_pos = byte_pos(dpcm, dpcm->irq_pos);
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200450 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
451 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
452 if (delta >= dpcm->last_drift)
453 delta -= dpcm->last_drift;
454 dpcm->last_drift = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200455 if (dpcm->irq_pos >= dpcm->period_size_frac) {
456 dpcm->irq_pos %= dpcm->period_size_frac;
457 dpcm->period_update_pending = 1;
458 }
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200459 return delta;
460}
461
462static inline void bytepos_finish(struct loopback_pcm *dpcm,
463 unsigned int delta)
464{
465 dpcm->buf_pos += delta;
466 dpcm->buf_pos %= dpcm->pcm_buffer_size;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200467}
468
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200469/* call in cable->lock */
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200470static unsigned int loopback_pos_update(struct loopback_cable *cable)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200471{
472 struct loopback_pcm *dpcm_play =
473 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
474 struct loopback_pcm *dpcm_capt =
475 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
476 unsigned long delta_play = 0, delta_capt = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200477 unsigned int running, count1, count2;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200478
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200479 running = cable->running ^ cable->pause;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200480 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200481 delta_play = jiffies - dpcm_play->last_jiffies;
482 dpcm_play->last_jiffies += delta_play;
483 }
484
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200485 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200486 delta_capt = jiffies - dpcm_capt->last_jiffies;
487 dpcm_capt->last_jiffies += delta_capt;
488 }
489
Takashi Iwai98d21df2011-03-18 07:31:53 +0100490 if (delta_play == 0 && delta_capt == 0)
491 goto unlock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200492
493 if (delta_play > delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200494 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
495 bytepos_finish(dpcm_play, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200496 delta_play = delta_capt;
497 } else if (delta_play < delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200498 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
499 clear_capture_buf(dpcm_capt, count1);
500 bytepos_finish(dpcm_capt, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200501 delta_capt = delta_play;
502 }
503
Takashi Iwai98d21df2011-03-18 07:31:53 +0100504 if (delta_play == 0 && delta_capt == 0)
505 goto unlock;
506
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200507 /* note delta_capt == delta_play at this moment */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200508 count1 = bytepos_delta(dpcm_play, delta_play);
509 count2 = bytepos_delta(dpcm_capt, delta_capt);
510 if (count1 < count2) {
511 dpcm_capt->last_drift = count2 - count1;
512 count1 = count2;
513 } else if (count1 > count2) {
514 dpcm_play->last_drift = count1 - count2;
515 }
516 copy_play_buf(dpcm_play, dpcm_capt, count1);
517 bytepos_finish(dpcm_play, count1);
518 bytepos_finish(dpcm_capt, count1);
Takashi Iwai98d21df2011-03-18 07:31:53 +0100519 unlock:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200520 return running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200521}
522
523static void loopback_timer_function(unsigned long data)
524{
525 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200526 unsigned long flags;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200527
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200528 spin_lock_irqsave(&dpcm->cable->lock, flags);
529 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200530 loopback_timer_start(dpcm);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200531 if (dpcm->period_update_pending) {
532 dpcm->period_update_pending = 0;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200533 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
534 /* need to unlock before calling below */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200535 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200536 return;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200537 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200538 }
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200539 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200540}
541
542static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
543{
544 struct snd_pcm_runtime *runtime = substream->runtime;
545 struct loopback_pcm *dpcm = runtime->private_data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200546 snd_pcm_uframes_t pos;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200547
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200548 spin_lock(&dpcm->cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200549 loopback_pos_update(dpcm->cable);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200550 pos = dpcm->buf_pos;
551 spin_unlock(&dpcm->cable->lock);
552 return bytes_to_frames(runtime, pos);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200553}
554
555static struct snd_pcm_hardware loopback_pcm_hardware =
556{
557 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
Takashi Iwaiedac8942013-02-04 10:28:15 +0100558 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
559 SNDRV_PCM_INFO_RESUME),
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200560 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
561 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
562 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
563 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
564 .rate_min = 8000,
565 .rate_max = 192000,
566 .channels_min = 1,
567 .channels_max = 32,
568 .buffer_bytes_max = 2 * 1024 * 1024,
569 .period_bytes_min = 64,
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200570 /* note check overflow in frac_pos() using pcm_rate_shift before
571 changing period_bytes_max value */
572 .period_bytes_max = 1024 * 1024,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200573 .periods_min = 1,
574 .periods_max = 1024,
575 .fifo_size = 0,
576};
577
578static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
579{
580 struct loopback_pcm *dpcm = runtime->private_data;
581 kfree(dpcm);
582}
583
584static int loopback_hw_params(struct snd_pcm_substream *substream,
585 struct snd_pcm_hw_params *params)
586{
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200587 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
588 params_buffer_bytes(params));
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200589}
590
591static int loopback_hw_free(struct snd_pcm_substream *substream)
592{
593 struct snd_pcm_runtime *runtime = substream->runtime;
594 struct loopback_pcm *dpcm = runtime->private_data;
595 struct loopback_cable *cable = dpcm->cable;
596
597 mutex_lock(&dpcm->loopback->cable_lock);
598 cable->valid &= ~(1 << substream->stream);
599 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200600 return snd_pcm_lib_free_vmalloc_buffer(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200601}
602
603static unsigned int get_cable_index(struct snd_pcm_substream *substream)
604{
605 if (!substream->pcm->device)
606 return substream->stream;
607 else
608 return !substream->stream;
609}
610
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200611static int rule_format(struct snd_pcm_hw_params *params,
612 struct snd_pcm_hw_rule *rule)
613{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100614 struct loopback_pcm *dpcm = rule->private;
615 struct loopback_cable *cable = dpcm->cable;
Takashi Iwai5af666d2018-01-05 16:15:33 +0100616 struct snd_mask m;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200617
Takashi Iwai5af666d2018-01-05 16:15:33 +0100618 snd_mask_none(&m);
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100619 mutex_lock(&dpcm->loopback->cable_lock);
620 m.bits[0] = (u_int32_t)cable->hw.formats;
621 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
622 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai5af666d2018-01-05 16:15:33 +0100623 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200624}
625
626static int rule_rate(struct snd_pcm_hw_params *params,
627 struct snd_pcm_hw_rule *rule)
628{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100629 struct loopback_pcm *dpcm = rule->private;
630 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200631 struct snd_interval t;
632
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100633 mutex_lock(&dpcm->loopback->cable_lock);
634 t.min = cable->hw.rate_min;
635 t.max = cable->hw.rate_max;
636 mutex_unlock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200637 t.openmin = t.openmax = 0;
638 t.integer = 0;
639 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
640}
641
642static int rule_channels(struct snd_pcm_hw_params *params,
643 struct snd_pcm_hw_rule *rule)
644{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100645 struct loopback_pcm *dpcm = rule->private;
646 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200647 struct snd_interval t;
648
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100649 mutex_lock(&dpcm->loopback->cable_lock);
650 t.min = cable->hw.channels_min;
651 t.max = cable->hw.channels_max;
652 mutex_unlock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200653 t.openmin = t.openmax = 0;
654 t.integer = 0;
655 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
656}
657
Takashi Iwai01046dd2018-01-05 16:09:47 +0100658static void free_cable(struct snd_pcm_substream *substream)
659{
660 struct loopback *loopback = substream->private_data;
661 int dev = get_cable_index(substream);
662 struct loopback_cable *cable;
663
664 cable = loopback->cables[substream->number][dev];
665 if (!cable)
666 return;
667 if (cable->streams[!substream->stream]) {
668 /* other stream is still alive */
669 cable->streams[substream->stream] = NULL;
670 } else {
671 /* free the cable */
672 loopback->cables[substream->number][dev] = NULL;
673 kfree(cable);
674 }
675}
676
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200677static int loopback_open(struct snd_pcm_substream *substream)
678{
679 struct snd_pcm_runtime *runtime = substream->runtime;
680 struct loopback *loopback = substream->private_data;
681 struct loopback_pcm *dpcm;
Takashi Iwai01046dd2018-01-05 16:09:47 +0100682 struct loopback_cable *cable = NULL;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200683 int err = 0;
684 int dev = get_cable_index(substream);
685
686 mutex_lock(&loopback->cable_lock);
687 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
688 if (!dpcm) {
689 err = -ENOMEM;
690 goto unlock;
691 }
692 dpcm->loopback = loopback;
693 dpcm->substream = substream;
694 setup_timer(&dpcm->timer, loopback_timer_function,
695 (unsigned long)dpcm);
696
697 cable = loopback->cables[substream->number][dev];
698 if (!cable) {
699 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
700 if (!cable) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200701 err = -ENOMEM;
702 goto unlock;
703 }
704 spin_lock_init(&cable->lock);
705 cable->hw = loopback_pcm_hardware;
706 loopback->cables[substream->number][dev] = cable;
707 }
708 dpcm->cable = cable;
709 cable->streams[substream->stream] = dpcm;
710
711 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
712
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200713 /* use dynamic rules based on actual runtime->hw values */
714 /* note that the default rules created in the PCM midlevel code */
715 /* are cached -> they do not reflect the actual state */
716 err = snd_pcm_hw_rule_add(runtime, 0,
717 SNDRV_PCM_HW_PARAM_FORMAT,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100718 rule_format, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200719 SNDRV_PCM_HW_PARAM_FORMAT, -1);
720 if (err < 0)
721 goto unlock;
722 err = snd_pcm_hw_rule_add(runtime, 0,
723 SNDRV_PCM_HW_PARAM_RATE,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100724 rule_rate, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200725 SNDRV_PCM_HW_PARAM_RATE, -1);
726 if (err < 0)
727 goto unlock;
728 err = snd_pcm_hw_rule_add(runtime, 0,
729 SNDRV_PCM_HW_PARAM_CHANNELS,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100730 rule_channels, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200731 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
732 if (err < 0)
733 goto unlock;
734
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200735 runtime->private_data = dpcm;
736 runtime->private_free = loopback_runtime_free;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200737 if (get_notify(dpcm))
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200738 runtime->hw = loopback_pcm_hardware;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200739 else
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200740 runtime->hw = cable->hw;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200741 unlock:
Takashi Iwai01046dd2018-01-05 16:09:47 +0100742 if (err < 0) {
743 free_cable(substream);
744 kfree(dpcm);
745 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200746 mutex_unlock(&loopback->cable_lock);
747 return err;
748}
749
750static int loopback_close(struct snd_pcm_substream *substream)
751{
752 struct loopback *loopback = substream->private_data;
753 struct loopback_pcm *dpcm = substream->runtime->private_data;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200754
Takashi Iwaid44f3ad2018-03-22 08:56:06 +0100755 loopback_timer_stop_sync(dpcm);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200756 mutex_lock(&loopback->cable_lock);
Takashi Iwai01046dd2018-01-05 16:09:47 +0100757 free_cable(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200758 mutex_unlock(&loopback->cable_lock);
759 return 0;
760}
761
762static struct snd_pcm_ops loopback_playback_ops = {
763 .open = loopback_open,
764 .close = loopback_close,
765 .ioctl = snd_pcm_lib_ioctl,
766 .hw_params = loopback_hw_params,
767 .hw_free = loopback_hw_free,
768 .prepare = loopback_prepare,
769 .trigger = loopback_trigger,
770 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200771 .page = snd_pcm_lib_get_vmalloc_page,
772 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200773};
774
775static struct snd_pcm_ops loopback_capture_ops = {
776 .open = loopback_open,
777 .close = loopback_close,
778 .ioctl = snd_pcm_lib_ioctl,
779 .hw_params = loopback_hw_params,
780 .hw_free = loopback_hw_free,
781 .prepare = loopback_prepare,
782 .trigger = loopback_trigger,
783 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200784 .page = snd_pcm_lib_get_vmalloc_page,
785 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200786};
787
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500788static int loopback_pcm_new(struct loopback *loopback,
789 int device, int substreams)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200790{
791 struct snd_pcm *pcm;
792 int err;
793
794 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
795 substreams, substreams, &pcm);
796 if (err < 0)
797 return err;
798 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
799 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
800
801 pcm->private_data = loopback;
802 pcm->info_flags = 0;
803 strcpy(pcm->name, "Loopback PCM");
804
805 loopback->pcm[device] = pcm;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200806 return 0;
807}
808
809static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
810 struct snd_ctl_elem_info *uinfo)
811{
812 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
813 uinfo->count = 1;
814 uinfo->value.integer.min = 80000;
815 uinfo->value.integer.max = 120000;
816 uinfo->value.integer.step = 1;
817 return 0;
818}
819
820static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
821 struct snd_ctl_elem_value *ucontrol)
822{
823 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
824
825 ucontrol->value.integer.value[0] =
826 loopback->setup[kcontrol->id.subdevice]
827 [kcontrol->id.device].rate_shift;
828 return 0;
829}
830
831static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
832 struct snd_ctl_elem_value *ucontrol)
833{
834 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
835 unsigned int val;
836 int change = 0;
837
838 val = ucontrol->value.integer.value[0];
839 if (val < 80000)
840 val = 80000;
841 if (val > 120000)
842 val = 120000;
843 mutex_lock(&loopback->cable_lock);
844 if (val != loopback->setup[kcontrol->id.subdevice]
845 [kcontrol->id.device].rate_shift) {
846 loopback->setup[kcontrol->id.subdevice]
847 [kcontrol->id.device].rate_shift = val;
848 change = 1;
849 }
850 mutex_unlock(&loopback->cable_lock);
851 return change;
852}
853
854static int loopback_notify_get(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_value *ucontrol)
856{
857 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
858
859 ucontrol->value.integer.value[0] =
860 loopback->setup[kcontrol->id.subdevice]
861 [kcontrol->id.device].notify;
862 return 0;
863}
864
865static int loopback_notify_put(struct snd_kcontrol *kcontrol,
866 struct snd_ctl_elem_value *ucontrol)
867{
868 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
869 unsigned int val;
870 int change = 0;
871
872 val = ucontrol->value.integer.value[0] ? 1 : 0;
873 if (val != loopback->setup[kcontrol->id.subdevice]
874 [kcontrol->id.device].notify) {
875 loopback->setup[kcontrol->id.subdevice]
876 [kcontrol->id.device].notify = val;
877 change = 1;
878 }
879 return change;
880}
881
882static int loopback_active_get(struct snd_kcontrol *kcontrol,
883 struct snd_ctl_elem_value *ucontrol)
884{
885 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
886 struct loopback_cable *cable = loopback->cables
Jaroslav Kyselaac446fb2010-10-02 16:00:53 +0200887 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200888 unsigned int val = 0;
889
890 if (cable != NULL)
891 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
892 1 : 0;
893 ucontrol->value.integer.value[0] = val;
894 return 0;
895}
896
897static int loopback_format_info(struct snd_kcontrol *kcontrol,
898 struct snd_ctl_elem_info *uinfo)
899{
900 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
901 uinfo->count = 1;
902 uinfo->value.integer.min = 0;
903 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
904 uinfo->value.integer.step = 1;
905 return 0;
906}
907
908static int loopback_format_get(struct snd_kcontrol *kcontrol,
909 struct snd_ctl_elem_value *ucontrol)
910{
911 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
912
913 ucontrol->value.integer.value[0] =
914 loopback->setup[kcontrol->id.subdevice]
915 [kcontrol->id.device].format;
916 return 0;
917}
918
919static int loopback_rate_info(struct snd_kcontrol *kcontrol,
920 struct snd_ctl_elem_info *uinfo)
921{
922 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
923 uinfo->count = 1;
924 uinfo->value.integer.min = 0;
925 uinfo->value.integer.max = 192000;
926 uinfo->value.integer.step = 1;
927 return 0;
928}
929
930static int loopback_rate_get(struct snd_kcontrol *kcontrol,
931 struct snd_ctl_elem_value *ucontrol)
932{
933 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
934
935 ucontrol->value.integer.value[0] =
936 loopback->setup[kcontrol->id.subdevice]
937 [kcontrol->id.device].rate;
938 return 0;
939}
940
941static int loopback_channels_info(struct snd_kcontrol *kcontrol,
942 struct snd_ctl_elem_info *uinfo)
943{
944 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
945 uinfo->count = 1;
946 uinfo->value.integer.min = 1;
947 uinfo->value.integer.max = 1024;
948 uinfo->value.integer.step = 1;
949 return 0;
950}
951
952static int loopback_channels_get(struct snd_kcontrol *kcontrol,
953 struct snd_ctl_elem_value *ucontrol)
954{
955 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
956
957 ucontrol->value.integer.value[0] =
958 loopback->setup[kcontrol->id.subdevice]
Jaroslav Kysela1446c5f2010-09-15 08:01:57 +0200959 [kcontrol->id.device].channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200960 return 0;
961}
962
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500963static struct snd_kcontrol_new loopback_controls[] = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200964{
965 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
966 .name = "PCM Rate Shift 100000",
967 .info = loopback_rate_shift_info,
968 .get = loopback_rate_shift_get,
969 .put = loopback_rate_shift_put,
970},
971{
972 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
973 .name = "PCM Notify",
974 .info = snd_ctl_boolean_mono_info,
975 .get = loopback_notify_get,
976 .put = loopback_notify_put,
977},
978#define ACTIVE_IDX 2
979{
980 .access = SNDRV_CTL_ELEM_ACCESS_READ,
981 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
982 .name = "PCM Slave Active",
983 .info = snd_ctl_boolean_mono_info,
984 .get = loopback_active_get,
985},
986#define FORMAT_IDX 3
987{
988 .access = SNDRV_CTL_ELEM_ACCESS_READ,
989 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
990 .name = "PCM Slave Format",
991 .info = loopback_format_info,
992 .get = loopback_format_get
993},
994#define RATE_IDX 4
995{
996 .access = SNDRV_CTL_ELEM_ACCESS_READ,
997 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
998 .name = "PCM Slave Rate",
999 .info = loopback_rate_info,
1000 .get = loopback_rate_get
1001},
1002#define CHANNELS_IDX 5
1003{
1004 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1005 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1006 .name = "PCM Slave Channels",
1007 .info = loopback_channels_info,
1008 .get = loopback_channels_get
1009}
1010};
1011
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001012static int loopback_mixer_new(struct loopback *loopback, int notify)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001013{
1014 struct snd_card *card = loopback->card;
1015 struct snd_pcm *pcm;
1016 struct snd_kcontrol *kctl;
1017 struct loopback_setup *setup;
1018 int err, dev, substr, substr_count, idx;
1019
1020 strcpy(card->mixername, "Loopback Mixer");
1021 for (dev = 0; dev < 2; dev++) {
1022 pcm = loopback->pcm[dev];
1023 substr_count =
1024 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1025 for (substr = 0; substr < substr_count; substr++) {
1026 setup = &loopback->setup[substr][dev];
1027 setup->notify = notify;
1028 setup->rate_shift = NO_PITCH;
1029 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1030 setup->rate = 48000;
1031 setup->channels = 2;
1032 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1033 idx++) {
1034 kctl = snd_ctl_new1(&loopback_controls[idx],
1035 loopback);
1036 if (!kctl)
1037 return -ENOMEM;
1038 kctl->id.device = dev;
1039 kctl->id.subdevice = substr;
1040 switch (idx) {
1041 case ACTIVE_IDX:
1042 setup->active_id = kctl->id;
1043 break;
1044 case FORMAT_IDX:
1045 setup->format_id = kctl->id;
1046 break;
1047 case RATE_IDX:
1048 setup->rate_id = kctl->id;
1049 break;
1050 case CHANNELS_IDX:
1051 setup->channels_id = kctl->id;
1052 break;
1053 default:
1054 break;
1055 }
1056 err = snd_ctl_add(card, kctl);
1057 if (err < 0)
1058 return err;
1059 }
1060 }
1061 }
1062 return 0;
1063}
1064
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001065static void print_dpcm_info(struct snd_info_buffer *buffer,
1066 struct loopback_pcm *dpcm,
1067 const char *id)
1068{
1069 snd_iprintf(buffer, " %s\n", id);
1070 if (dpcm == NULL) {
1071 snd_iprintf(buffer, " inactive\n");
1072 return;
1073 }
1074 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1075 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1076 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1077 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1078 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1079 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1080 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1081 snd_iprintf(buffer, " update_pending:\t%u\n",
1082 dpcm->period_update_pending);
1083 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1084 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1085 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1086 dpcm->last_jiffies, jiffies);
1087 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1088}
1089
1090static void print_substream_info(struct snd_info_buffer *buffer,
1091 struct loopback *loopback,
1092 int sub,
1093 int num)
1094{
1095 struct loopback_cable *cable = loopback->cables[sub][num];
1096
1097 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1098 if (cable == NULL) {
1099 snd_iprintf(buffer, " inactive\n");
1100 return;
1101 }
1102 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1103 snd_iprintf(buffer, " running: %u\n", cable->running);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +02001104 snd_iprintf(buffer, " pause: %u\n", cable->pause);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001105 print_dpcm_info(buffer, cable->streams[0], "Playback");
1106 print_dpcm_info(buffer, cable->streams[1], "Capture");
1107}
1108
1109static void print_cable_info(struct snd_info_entry *entry,
1110 struct snd_info_buffer *buffer)
1111{
1112 struct loopback *loopback = entry->private_data;
1113 int sub, num;
1114
1115 mutex_lock(&loopback->cable_lock);
1116 num = entry->name[strlen(entry->name)-1];
1117 num = num == '0' ? 0 : 1;
1118 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1119 print_substream_info(buffer, loopback, sub, num);
1120 mutex_unlock(&loopback->cable_lock);
1121}
1122
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001123static int loopback_proc_new(struct loopback *loopback, int cidx)
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001124{
1125 char name[32];
1126 struct snd_info_entry *entry;
1127 int err;
1128
1129 snprintf(name, sizeof(name), "cable#%d", cidx);
1130 err = snd_card_proc_new(loopback->card, name, &entry);
1131 if (err < 0)
1132 return err;
1133
1134 snd_info_set_text_ops(entry, loopback, print_cable_info);
1135 return 0;
1136}
1137
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001138static int loopback_probe(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001139{
1140 struct snd_card *card;
1141 struct loopback *loopback;
1142 int dev = devptr->id;
1143 int err;
1144
Takashi Iwai5872f3f2014-01-29 12:59:08 +01001145 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1146 sizeof(struct loopback), &card);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001147 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;
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001168 loopback_proc_new(loopback, 0);
1169 loopback_proc_new(loopback, 1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001170 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
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001183static int loopback_remove(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001184{
1185 snd_card_free(platform_get_drvdata(devptr));
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001186 return 0;
1187}
1188
Takashi Iwaid34e4e02012-08-09 15:47:15 +02001189#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001190static int loopback_suspend(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001191{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001192 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001193 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
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001202static int loopback_resume(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001203{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001204 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001205
1206 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1207 return 0;
1208}
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001209
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
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001214#endif
1215
1216#define SND_LOOPBACK_DRIVER "snd_aloop"
1217
1218static struct platform_driver loopback_driver = {
1219 .probe = loopback_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001220 .remove = loopback_remove,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001221 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +02001222 .name = SND_LOOPBACK_DRIVER,
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001223 .pm = LOOPBACK_PM_OPS,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001224 },
1225};
1226
1227static void loopback_unregister_all(void)
1228{
1229 int i;
1230
1231 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1232 platform_device_unregister(devices[i]);
1233 platform_driver_unregister(&loopback_driver);
1234}
1235
1236static int __init alsa_card_loopback_init(void)
1237{
1238 int i, err, cards;
1239
1240 err = platform_driver_register(&loopback_driver);
1241 if (err < 0)
1242 return err;
1243
1244
1245 cards = 0;
1246 for (i = 0; i < SNDRV_CARDS; i++) {
1247 struct platform_device *device;
1248 if (!enable[i])
1249 continue;
1250 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1251 i, NULL, 0);
1252 if (IS_ERR(device))
1253 continue;
1254 if (!platform_get_drvdata(device)) {
1255 platform_device_unregister(device);
1256 continue;
1257 }
1258 devices[i] = device;
1259 cards++;
1260 }
1261 if (!cards) {
1262#ifdef MODULE
1263 printk(KERN_ERR "aloop: No loopback enabled\n");
1264#endif
1265 loopback_unregister_all();
1266 return -ENODEV;
1267 }
1268 return 0;
1269}
1270
1271static void __exit alsa_card_loopback_exit(void)
1272{
1273 loopback_unregister_all();
1274}
1275
1276module_init(alsa_card_loopback_init)
1277module_exit(alsa_card_loopback_exit)