blob: 847f70348d4d5bb83012cc3cf75c20e27025cd96 [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);
Robert Rosengren68f4bc32018-03-26 07:24:49 +0200299 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
300 loopback_active_notify(dpcm);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200301 break;
302 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100303 case SNDRV_PCM_TRIGGER_RESUME:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200304 spin_lock(&cable->lock);
305 dpcm->last_jiffies = jiffies;
306 cable->pause &= ~stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200307 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200308 spin_unlock(&cable->lock);
Robert Rosengren68f4bc32018-03-26 07:24:49 +0200309 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
310 loopback_active_notify(dpcm);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200311 break;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200312 default:
313 return -EINVAL;
314 }
315 return 0;
316}
317
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200318static void params_change(struct snd_pcm_substream *substream)
319{
320 struct snd_pcm_runtime *runtime = substream->runtime;
321 struct loopback_pcm *dpcm = runtime->private_data;
322 struct loopback_cable *cable = dpcm->cable;
323
Eldad Zack74c34ca2013-04-23 01:00:41 +0200324 cable->hw.formats = pcm_format_to_bits(runtime->format);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200325 cable->hw.rate_min = runtime->rate;
326 cable->hw.rate_max = runtime->rate;
327 cable->hw.channels_min = runtime->channels;
328 cable->hw.channels_max = runtime->channels;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200329}
330
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200331static int loopback_prepare(struct snd_pcm_substream *substream)
332{
333 struct snd_pcm_runtime *runtime = substream->runtime;
334 struct loopback_pcm *dpcm = runtime->private_data;
335 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200336 int bps, salign;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200337
Takashi Iwaid44f3ad2018-03-22 08:56:06 +0100338 loopback_timer_stop_sync(dpcm);
339
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200340 salign = (snd_pcm_format_width(runtime->format) *
341 runtime->channels) / 8;
342 bps = salign * runtime->rate;
343 if (bps <= 0 || salign <= 0)
344 return -EINVAL;
345
346 dpcm->buf_pos = 0;
347 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
348 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
349 /* clear capture buffer */
350 dpcm->silent_size = dpcm->pcm_buffer_size;
351 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
352 runtime->buffer_size * runtime->channels);
353 }
354
355 dpcm->irq_pos = 0;
356 dpcm->period_update_pending = 0;
357 dpcm->pcm_bps = bps;
358 dpcm->pcm_salign = salign;
359 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
360
361 mutex_lock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200362 if (!(cable->valid & ~(1 << substream->stream)) ||
363 (get_setup(dpcm)->notify &&
364 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
365 params_change(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200366 cable->valid |= 1 << substream->stream;
367 mutex_unlock(&dpcm->loopback->cable_lock);
368
369 return 0;
370}
371
372static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
373{
374 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
375 char *dst = runtime->dma_area;
376 unsigned int dst_off = dpcm->buf_pos;
377
378 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
379 return;
380 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
381 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
382
383 for (;;) {
384 unsigned int size = bytes;
385 if (dst_off + size > dpcm->pcm_buffer_size)
386 size = dpcm->pcm_buffer_size - dst_off;
387 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
388 bytes_to_frames(runtime, size) *
389 runtime->channels);
390 dpcm->silent_size += size;
391 bytes -= size;
392 if (!bytes)
393 break;
394 dst_off = 0;
395 }
396}
397
398static void copy_play_buf(struct loopback_pcm *play,
399 struct loopback_pcm *capt,
400 unsigned int bytes)
401{
402 struct snd_pcm_runtime *runtime = play->substream->runtime;
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200403 char *src = runtime->dma_area;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200404 char *dst = capt->substream->runtime->dma_area;
405 unsigned int src_off = play->buf_pos;
406 unsigned int dst_off = capt->buf_pos;
407 unsigned int clear_bytes = 0;
408
409 /* check if playback is draining, trim the capture copy size
410 * when our pointer is at the end of playback ring buffer */
411 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
412 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
413 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
414 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
415 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
416 appl_ptr1 += play->buf_pos / play->pcm_salign;
417 if (appl_ptr < appl_ptr1)
418 appl_ptr1 -= runtime->buffer_size;
419 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
420 if (diff < bytes) {
421 clear_bytes = bytes - diff;
422 bytes = diff;
423 }
424 }
425
426 for (;;) {
427 unsigned int size = bytes;
428 if (src_off + size > play->pcm_buffer_size)
429 size = play->pcm_buffer_size - src_off;
430 if (dst_off + size > capt->pcm_buffer_size)
431 size = capt->pcm_buffer_size - dst_off;
432 memcpy(dst + dst_off, src + src_off, size);
433 capt->silent_size = 0;
434 bytes -= size;
435 if (!bytes)
436 break;
437 src_off = (src_off + size) % play->pcm_buffer_size;
438 dst_off = (dst_off + size) % capt->pcm_buffer_size;
439 }
440
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200441 if (clear_bytes > 0) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200442 clear_capture_buf(capt, clear_bytes);
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200443 capt->silent_size = 0;
444 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200445}
446
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200447static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
448 unsigned int jiffies_delta)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200449{
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200450 unsigned long last_pos;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200451 unsigned int delta;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200452
453 last_pos = byte_pos(dpcm, dpcm->irq_pos);
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200454 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
455 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
456 if (delta >= dpcm->last_drift)
457 delta -= dpcm->last_drift;
458 dpcm->last_drift = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200459 if (dpcm->irq_pos >= dpcm->period_size_frac) {
460 dpcm->irq_pos %= dpcm->period_size_frac;
461 dpcm->period_update_pending = 1;
462 }
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200463 return delta;
464}
465
466static inline void bytepos_finish(struct loopback_pcm *dpcm,
467 unsigned int delta)
468{
469 dpcm->buf_pos += delta;
470 dpcm->buf_pos %= dpcm->pcm_buffer_size;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200471}
472
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200473/* call in cable->lock */
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200474static unsigned int loopback_pos_update(struct loopback_cable *cable)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200475{
476 struct loopback_pcm *dpcm_play =
477 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
478 struct loopback_pcm *dpcm_capt =
479 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
480 unsigned long delta_play = 0, delta_capt = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200481 unsigned int running, count1, count2;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200482
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200483 running = cable->running ^ cable->pause;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200484 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200485 delta_play = jiffies - dpcm_play->last_jiffies;
486 dpcm_play->last_jiffies += delta_play;
487 }
488
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200489 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200490 delta_capt = jiffies - dpcm_capt->last_jiffies;
491 dpcm_capt->last_jiffies += delta_capt;
492 }
493
Takashi Iwai98d21df2011-03-18 07:31:53 +0100494 if (delta_play == 0 && delta_capt == 0)
495 goto unlock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200496
497 if (delta_play > delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200498 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
499 bytepos_finish(dpcm_play, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200500 delta_play = delta_capt;
501 } else if (delta_play < delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200502 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
503 clear_capture_buf(dpcm_capt, count1);
504 bytepos_finish(dpcm_capt, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200505 delta_capt = delta_play;
506 }
507
Takashi Iwai98d21df2011-03-18 07:31:53 +0100508 if (delta_play == 0 && delta_capt == 0)
509 goto unlock;
510
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200511 /* note delta_capt == delta_play at this moment */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200512 count1 = bytepos_delta(dpcm_play, delta_play);
513 count2 = bytepos_delta(dpcm_capt, delta_capt);
514 if (count1 < count2) {
515 dpcm_capt->last_drift = count2 - count1;
516 count1 = count2;
517 } else if (count1 > count2) {
518 dpcm_play->last_drift = count1 - count2;
519 }
520 copy_play_buf(dpcm_play, dpcm_capt, count1);
521 bytepos_finish(dpcm_play, count1);
522 bytepos_finish(dpcm_capt, count1);
Takashi Iwai98d21df2011-03-18 07:31:53 +0100523 unlock:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200524 return running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200525}
526
527static void loopback_timer_function(unsigned long data)
528{
529 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200530 unsigned long flags;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200531
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200532 spin_lock_irqsave(&dpcm->cable->lock, flags);
533 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200534 loopback_timer_start(dpcm);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200535 if (dpcm->period_update_pending) {
536 dpcm->period_update_pending = 0;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200537 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
538 /* need to unlock before calling below */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200539 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200540 return;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200541 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200542 }
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200543 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200544}
545
546static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
547{
548 struct snd_pcm_runtime *runtime = substream->runtime;
549 struct loopback_pcm *dpcm = runtime->private_data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200550 snd_pcm_uframes_t pos;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200551
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200552 spin_lock(&dpcm->cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200553 loopback_pos_update(dpcm->cable);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200554 pos = dpcm->buf_pos;
555 spin_unlock(&dpcm->cable->lock);
556 return bytes_to_frames(runtime, pos);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200557}
558
559static struct snd_pcm_hardware loopback_pcm_hardware =
560{
561 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
Takashi Iwaiedac8942013-02-04 10:28:15 +0100562 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
563 SNDRV_PCM_INFO_RESUME),
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200564 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
565 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
566 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
567 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
568 .rate_min = 8000,
569 .rate_max = 192000,
570 .channels_min = 1,
571 .channels_max = 32,
572 .buffer_bytes_max = 2 * 1024 * 1024,
573 .period_bytes_min = 64,
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200574 /* note check overflow in frac_pos() using pcm_rate_shift before
575 changing period_bytes_max value */
576 .period_bytes_max = 1024 * 1024,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200577 .periods_min = 1,
578 .periods_max = 1024,
579 .fifo_size = 0,
580};
581
582static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
583{
584 struct loopback_pcm *dpcm = runtime->private_data;
585 kfree(dpcm);
586}
587
588static int loopback_hw_params(struct snd_pcm_substream *substream,
589 struct snd_pcm_hw_params *params)
590{
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200591 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
592 params_buffer_bytes(params));
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200593}
594
595static int loopback_hw_free(struct snd_pcm_substream *substream)
596{
597 struct snd_pcm_runtime *runtime = substream->runtime;
598 struct loopback_pcm *dpcm = runtime->private_data;
599 struct loopback_cable *cable = dpcm->cable;
600
601 mutex_lock(&dpcm->loopback->cable_lock);
602 cable->valid &= ~(1 << substream->stream);
603 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200604 return snd_pcm_lib_free_vmalloc_buffer(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200605}
606
607static unsigned int get_cable_index(struct snd_pcm_substream *substream)
608{
609 if (!substream->pcm->device)
610 return substream->stream;
611 else
612 return !substream->stream;
613}
614
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200615static int rule_format(struct snd_pcm_hw_params *params,
616 struct snd_pcm_hw_rule *rule)
617{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100618 struct loopback_pcm *dpcm = rule->private;
619 struct loopback_cable *cable = dpcm->cable;
Takashi Iwai5af666d2018-01-05 16:15:33 +0100620 struct snd_mask m;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200621
Takashi Iwai5af666d2018-01-05 16:15:33 +0100622 snd_mask_none(&m);
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100623 mutex_lock(&dpcm->loopback->cable_lock);
624 m.bits[0] = (u_int32_t)cable->hw.formats;
625 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
626 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai5af666d2018-01-05 16:15:33 +0100627 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200628}
629
630static int rule_rate(struct snd_pcm_hw_params *params,
631 struct snd_pcm_hw_rule *rule)
632{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100633 struct loopback_pcm *dpcm = rule->private;
634 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200635 struct snd_interval t;
636
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100637 mutex_lock(&dpcm->loopback->cable_lock);
638 t.min = cable->hw.rate_min;
639 t.max = cable->hw.rate_max;
640 mutex_unlock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200641 t.openmin = t.openmax = 0;
642 t.integer = 0;
643 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
644}
645
646static int rule_channels(struct snd_pcm_hw_params *params,
647 struct snd_pcm_hw_rule *rule)
648{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100649 struct loopback_pcm *dpcm = rule->private;
650 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200651 struct snd_interval t;
652
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100653 mutex_lock(&dpcm->loopback->cable_lock);
654 t.min = cable->hw.channels_min;
655 t.max = cable->hw.channels_max;
656 mutex_unlock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200657 t.openmin = t.openmax = 0;
658 t.integer = 0;
659 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
660}
661
Takashi Iwai01046dd2018-01-05 16:09:47 +0100662static void free_cable(struct snd_pcm_substream *substream)
663{
664 struct loopback *loopback = substream->private_data;
665 int dev = get_cable_index(substream);
666 struct loopback_cable *cable;
667
668 cable = loopback->cables[substream->number][dev];
669 if (!cable)
670 return;
671 if (cable->streams[!substream->stream]) {
672 /* other stream is still alive */
Takashi Iwai78969702018-03-22 10:40:27 +0100673 spin_lock_irq(&cable->lock);
Takashi Iwai01046dd2018-01-05 16:09:47 +0100674 cable->streams[substream->stream] = NULL;
Takashi Iwai78969702018-03-22 10:40:27 +0100675 spin_unlock_irq(&cable->lock);
Takashi Iwai01046dd2018-01-05 16:09:47 +0100676 } else {
677 /* free the cable */
678 loopback->cables[substream->number][dev] = NULL;
679 kfree(cable);
680 }
681}
682
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200683static int loopback_open(struct snd_pcm_substream *substream)
684{
685 struct snd_pcm_runtime *runtime = substream->runtime;
686 struct loopback *loopback = substream->private_data;
687 struct loopback_pcm *dpcm;
Takashi Iwai01046dd2018-01-05 16:09:47 +0100688 struct loopback_cable *cable = NULL;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200689 int err = 0;
690 int dev = get_cable_index(substream);
691
692 mutex_lock(&loopback->cable_lock);
693 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
694 if (!dpcm) {
695 err = -ENOMEM;
696 goto unlock;
697 }
698 dpcm->loopback = loopback;
699 dpcm->substream = substream;
700 setup_timer(&dpcm->timer, loopback_timer_function,
701 (unsigned long)dpcm);
702
703 cable = loopback->cables[substream->number][dev];
704 if (!cable) {
705 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
706 if (!cable) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200707 err = -ENOMEM;
708 goto unlock;
709 }
710 spin_lock_init(&cable->lock);
711 cable->hw = loopback_pcm_hardware;
712 loopback->cables[substream->number][dev] = cable;
713 }
714 dpcm->cable = cable;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200715
716 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
717
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200718 /* use dynamic rules based on actual runtime->hw values */
719 /* note that the default rules created in the PCM midlevel code */
720 /* are cached -> they do not reflect the actual state */
721 err = snd_pcm_hw_rule_add(runtime, 0,
722 SNDRV_PCM_HW_PARAM_FORMAT,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100723 rule_format, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200724 SNDRV_PCM_HW_PARAM_FORMAT, -1);
725 if (err < 0)
726 goto unlock;
727 err = snd_pcm_hw_rule_add(runtime, 0,
728 SNDRV_PCM_HW_PARAM_RATE,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100729 rule_rate, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200730 SNDRV_PCM_HW_PARAM_RATE, -1);
731 if (err < 0)
732 goto unlock;
733 err = snd_pcm_hw_rule_add(runtime, 0,
734 SNDRV_PCM_HW_PARAM_CHANNELS,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100735 rule_channels, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200736 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
737 if (err < 0)
738 goto unlock;
739
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200740 runtime->private_data = dpcm;
741 runtime->private_free = loopback_runtime_free;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200742 if (get_notify(dpcm))
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200743 runtime->hw = loopback_pcm_hardware;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200744 else
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200745 runtime->hw = cable->hw;
Takashi Iwai78969702018-03-22 10:40:27 +0100746
747 spin_lock_irq(&cable->lock);
748 cable->streams[substream->stream] = dpcm;
749 spin_unlock_irq(&cable->lock);
750
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200751 unlock:
Takashi Iwai01046dd2018-01-05 16:09:47 +0100752 if (err < 0) {
753 free_cable(substream);
754 kfree(dpcm);
755 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200756 mutex_unlock(&loopback->cable_lock);
757 return err;
758}
759
760static int loopback_close(struct snd_pcm_substream *substream)
761{
762 struct loopback *loopback = substream->private_data;
763 struct loopback_pcm *dpcm = substream->runtime->private_data;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200764
Takashi Iwaid44f3ad2018-03-22 08:56:06 +0100765 loopback_timer_stop_sync(dpcm);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200766 mutex_lock(&loopback->cable_lock);
Takashi Iwai01046dd2018-01-05 16:09:47 +0100767 free_cable(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200768 mutex_unlock(&loopback->cable_lock);
769 return 0;
770}
771
772static struct snd_pcm_ops loopback_playback_ops = {
773 .open = loopback_open,
774 .close = loopback_close,
775 .ioctl = snd_pcm_lib_ioctl,
776 .hw_params = loopback_hw_params,
777 .hw_free = loopback_hw_free,
778 .prepare = loopback_prepare,
779 .trigger = loopback_trigger,
780 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200781 .page = snd_pcm_lib_get_vmalloc_page,
782 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200783};
784
785static struct snd_pcm_ops loopback_capture_ops = {
786 .open = loopback_open,
787 .close = loopback_close,
788 .ioctl = snd_pcm_lib_ioctl,
789 .hw_params = loopback_hw_params,
790 .hw_free = loopback_hw_free,
791 .prepare = loopback_prepare,
792 .trigger = loopback_trigger,
793 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200794 .page = snd_pcm_lib_get_vmalloc_page,
795 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200796};
797
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500798static int loopback_pcm_new(struct loopback *loopback,
799 int device, int substreams)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200800{
801 struct snd_pcm *pcm;
802 int err;
803
804 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
805 substreams, substreams, &pcm);
806 if (err < 0)
807 return err;
808 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
809 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
810
811 pcm->private_data = loopback;
812 pcm->info_flags = 0;
813 strcpy(pcm->name, "Loopback PCM");
814
815 loopback->pcm[device] = pcm;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200816 return 0;
817}
818
819static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
820 struct snd_ctl_elem_info *uinfo)
821{
822 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
823 uinfo->count = 1;
824 uinfo->value.integer.min = 80000;
825 uinfo->value.integer.max = 120000;
826 uinfo->value.integer.step = 1;
827 return 0;
828}
829
830static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
831 struct snd_ctl_elem_value *ucontrol)
832{
833 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
834
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200835 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200836 ucontrol->value.integer.value[0] =
837 loopback->setup[kcontrol->id.subdevice]
838 [kcontrol->id.device].rate_shift;
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200839 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200840 return 0;
841}
842
843static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
844 struct snd_ctl_elem_value *ucontrol)
845{
846 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
847 unsigned int val;
848 int change = 0;
849
850 val = ucontrol->value.integer.value[0];
851 if (val < 80000)
852 val = 80000;
853 if (val > 120000)
854 val = 120000;
855 mutex_lock(&loopback->cable_lock);
856 if (val != loopback->setup[kcontrol->id.subdevice]
857 [kcontrol->id.device].rate_shift) {
858 loopback->setup[kcontrol->id.subdevice]
859 [kcontrol->id.device].rate_shift = val;
860 change = 1;
861 }
862 mutex_unlock(&loopback->cable_lock);
863 return change;
864}
865
866static int loopback_notify_get(struct snd_kcontrol *kcontrol,
867 struct snd_ctl_elem_value *ucontrol)
868{
869 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
870
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200871 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200872 ucontrol->value.integer.value[0] =
873 loopback->setup[kcontrol->id.subdevice]
874 [kcontrol->id.device].notify;
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200875 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200876 return 0;
877}
878
879static int loopback_notify_put(struct snd_kcontrol *kcontrol,
880 struct snd_ctl_elem_value *ucontrol)
881{
882 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
883 unsigned int val;
884 int change = 0;
885
886 val = ucontrol->value.integer.value[0] ? 1 : 0;
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200887 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200888 if (val != loopback->setup[kcontrol->id.subdevice]
889 [kcontrol->id.device].notify) {
890 loopback->setup[kcontrol->id.subdevice]
891 [kcontrol->id.device].notify = val;
892 change = 1;
893 }
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200894 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200895 return change;
896}
897
898static int loopback_active_get(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_value *ucontrol)
900{
901 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200902 struct loopback_cable *cable;
903
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200904 unsigned int val = 0;
905
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200906 mutex_lock(&loopback->cable_lock);
907 cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
Robert Rosengren68f4bc32018-03-26 07:24:49 +0200908 if (cable != NULL) {
909 unsigned int running = cable->running ^ cable->pause;
910
911 val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
912 }
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200913 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200914 ucontrol->value.integer.value[0] = val;
915 return 0;
916}
917
918static int loopback_format_info(struct snd_kcontrol *kcontrol,
919 struct snd_ctl_elem_info *uinfo)
920{
921 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
922 uinfo->count = 1;
923 uinfo->value.integer.min = 0;
924 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
925 uinfo->value.integer.step = 1;
926 return 0;
927}
928
929static int loopback_format_get(struct snd_kcontrol *kcontrol,
930 struct snd_ctl_elem_value *ucontrol)
931{
932 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
933
934 ucontrol->value.integer.value[0] =
935 loopback->setup[kcontrol->id.subdevice]
936 [kcontrol->id.device].format;
937 return 0;
938}
939
940static int loopback_rate_info(struct snd_kcontrol *kcontrol,
941 struct snd_ctl_elem_info *uinfo)
942{
943 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
944 uinfo->count = 1;
945 uinfo->value.integer.min = 0;
946 uinfo->value.integer.max = 192000;
947 uinfo->value.integer.step = 1;
948 return 0;
949}
950
951static int loopback_rate_get(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol)
953{
954 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
955
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200956 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200957 ucontrol->value.integer.value[0] =
958 loopback->setup[kcontrol->id.subdevice]
959 [kcontrol->id.device].rate;
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200960 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200961 return 0;
962}
963
964static int loopback_channels_info(struct snd_kcontrol *kcontrol,
965 struct snd_ctl_elem_info *uinfo)
966{
967 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
968 uinfo->count = 1;
969 uinfo->value.integer.min = 1;
970 uinfo->value.integer.max = 1024;
971 uinfo->value.integer.step = 1;
972 return 0;
973}
974
975static int loopback_channels_get(struct snd_kcontrol *kcontrol,
976 struct snd_ctl_elem_value *ucontrol)
977{
978 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
979
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200980 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200981 ucontrol->value.integer.value[0] =
982 loopback->setup[kcontrol->id.subdevice]
Jaroslav Kysela1446c5f2010-09-15 08:01:57 +0200983 [kcontrol->id.device].channels;
Takashi Iwaib6a49cc2018-04-30 10:06:48 +0200984 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200985 return 0;
986}
987
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500988static struct snd_kcontrol_new loopback_controls[] = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200989{
990 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
991 .name = "PCM Rate Shift 100000",
992 .info = loopback_rate_shift_info,
993 .get = loopback_rate_shift_get,
994 .put = loopback_rate_shift_put,
995},
996{
997 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
998 .name = "PCM Notify",
999 .info = snd_ctl_boolean_mono_info,
1000 .get = loopback_notify_get,
1001 .put = loopback_notify_put,
1002},
1003#define ACTIVE_IDX 2
1004{
1005 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1006 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1007 .name = "PCM Slave Active",
1008 .info = snd_ctl_boolean_mono_info,
1009 .get = loopback_active_get,
1010},
1011#define FORMAT_IDX 3
1012{
1013 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1014 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1015 .name = "PCM Slave Format",
1016 .info = loopback_format_info,
1017 .get = loopback_format_get
1018},
1019#define RATE_IDX 4
1020{
1021 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1022 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1023 .name = "PCM Slave Rate",
1024 .info = loopback_rate_info,
1025 .get = loopback_rate_get
1026},
1027#define CHANNELS_IDX 5
1028{
1029 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1030 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1031 .name = "PCM Slave Channels",
1032 .info = loopback_channels_info,
1033 .get = loopback_channels_get
1034}
1035};
1036
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001037static int loopback_mixer_new(struct loopback *loopback, int notify)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001038{
1039 struct snd_card *card = loopback->card;
1040 struct snd_pcm *pcm;
1041 struct snd_kcontrol *kctl;
1042 struct loopback_setup *setup;
1043 int err, dev, substr, substr_count, idx;
1044
1045 strcpy(card->mixername, "Loopback Mixer");
1046 for (dev = 0; dev < 2; dev++) {
1047 pcm = loopback->pcm[dev];
1048 substr_count =
1049 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1050 for (substr = 0; substr < substr_count; substr++) {
1051 setup = &loopback->setup[substr][dev];
1052 setup->notify = notify;
1053 setup->rate_shift = NO_PITCH;
1054 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1055 setup->rate = 48000;
1056 setup->channels = 2;
1057 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1058 idx++) {
1059 kctl = snd_ctl_new1(&loopback_controls[idx],
1060 loopback);
1061 if (!kctl)
1062 return -ENOMEM;
1063 kctl->id.device = dev;
1064 kctl->id.subdevice = substr;
1065 switch (idx) {
1066 case ACTIVE_IDX:
1067 setup->active_id = kctl->id;
1068 break;
1069 case FORMAT_IDX:
1070 setup->format_id = kctl->id;
1071 break;
1072 case RATE_IDX:
1073 setup->rate_id = kctl->id;
1074 break;
1075 case CHANNELS_IDX:
1076 setup->channels_id = kctl->id;
1077 break;
1078 default:
1079 break;
1080 }
1081 err = snd_ctl_add(card, kctl);
1082 if (err < 0)
1083 return err;
1084 }
1085 }
1086 }
1087 return 0;
1088}
1089
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001090static void print_dpcm_info(struct snd_info_buffer *buffer,
1091 struct loopback_pcm *dpcm,
1092 const char *id)
1093{
1094 snd_iprintf(buffer, " %s\n", id);
1095 if (dpcm == NULL) {
1096 snd_iprintf(buffer, " inactive\n");
1097 return;
1098 }
1099 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1100 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1101 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1102 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1103 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1104 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1105 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1106 snd_iprintf(buffer, " update_pending:\t%u\n",
1107 dpcm->period_update_pending);
1108 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1109 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1110 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1111 dpcm->last_jiffies, jiffies);
1112 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1113}
1114
1115static void print_substream_info(struct snd_info_buffer *buffer,
1116 struct loopback *loopback,
1117 int sub,
1118 int num)
1119{
1120 struct loopback_cable *cable = loopback->cables[sub][num];
1121
1122 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1123 if (cable == NULL) {
1124 snd_iprintf(buffer, " inactive\n");
1125 return;
1126 }
1127 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1128 snd_iprintf(buffer, " running: %u\n", cable->running);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +02001129 snd_iprintf(buffer, " pause: %u\n", cable->pause);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001130 print_dpcm_info(buffer, cable->streams[0], "Playback");
1131 print_dpcm_info(buffer, cable->streams[1], "Capture");
1132}
1133
1134static void print_cable_info(struct snd_info_entry *entry,
1135 struct snd_info_buffer *buffer)
1136{
1137 struct loopback *loopback = entry->private_data;
1138 int sub, num;
1139
1140 mutex_lock(&loopback->cable_lock);
1141 num = entry->name[strlen(entry->name)-1];
1142 num = num == '0' ? 0 : 1;
1143 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1144 print_substream_info(buffer, loopback, sub, num);
1145 mutex_unlock(&loopback->cable_lock);
1146}
1147
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001148static int loopback_proc_new(struct loopback *loopback, int cidx)
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001149{
1150 char name[32];
1151 struct snd_info_entry *entry;
1152 int err;
1153
1154 snprintf(name, sizeof(name), "cable#%d", cidx);
1155 err = snd_card_proc_new(loopback->card, name, &entry);
1156 if (err < 0)
1157 return err;
1158
1159 snd_info_set_text_ops(entry, loopback, print_cable_info);
1160 return 0;
1161}
1162
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001163static int loopback_probe(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001164{
1165 struct snd_card *card;
1166 struct loopback *loopback;
1167 int dev = devptr->id;
1168 int err;
1169
Takashi Iwai5872f3f2014-01-29 12:59:08 +01001170 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1171 sizeof(struct loopback), &card);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001172 if (err < 0)
1173 return err;
1174 loopback = card->private_data;
1175
1176 if (pcm_substreams[dev] < 1)
1177 pcm_substreams[dev] = 1;
1178 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1179 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1180
1181 loopback->card = card;
1182 mutex_init(&loopback->cable_lock);
1183
1184 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1185 if (err < 0)
1186 goto __nodev;
1187 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1188 if (err < 0)
1189 goto __nodev;
1190 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1191 if (err < 0)
1192 goto __nodev;
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001193 loopback_proc_new(loopback, 0);
1194 loopback_proc_new(loopback, 1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001195 strcpy(card->driver, "Loopback");
1196 strcpy(card->shortname, "Loopback");
1197 sprintf(card->longname, "Loopback %i", dev + 1);
1198 err = snd_card_register(card);
1199 if (!err) {
1200 platform_set_drvdata(devptr, card);
1201 return 0;
1202 }
1203 __nodev:
1204 snd_card_free(card);
1205 return err;
1206}
1207
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001208static int loopback_remove(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001209{
1210 snd_card_free(platform_get_drvdata(devptr));
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001211 return 0;
1212}
1213
Takashi Iwaid34e4e02012-08-09 15:47:15 +02001214#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001215static int loopback_suspend(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001216{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001217 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001218 struct loopback *loopback = card->private_data;
1219
1220 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1221
1222 snd_pcm_suspend_all(loopback->pcm[0]);
1223 snd_pcm_suspend_all(loopback->pcm[1]);
1224 return 0;
1225}
1226
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001227static int loopback_resume(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001228{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001229 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001230
1231 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1232 return 0;
1233}
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001234
1235static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1236#define LOOPBACK_PM_OPS &loopback_pm
1237#else
1238#define LOOPBACK_PM_OPS NULL
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001239#endif
1240
1241#define SND_LOOPBACK_DRIVER "snd_aloop"
1242
1243static struct platform_driver loopback_driver = {
1244 .probe = loopback_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001245 .remove = loopback_remove,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001246 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +02001247 .name = SND_LOOPBACK_DRIVER,
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001248 .pm = LOOPBACK_PM_OPS,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001249 },
1250};
1251
1252static void loopback_unregister_all(void)
1253{
1254 int i;
1255
1256 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1257 platform_device_unregister(devices[i]);
1258 platform_driver_unregister(&loopback_driver);
1259}
1260
1261static int __init alsa_card_loopback_init(void)
1262{
1263 int i, err, cards;
1264
1265 err = platform_driver_register(&loopback_driver);
1266 if (err < 0)
1267 return err;
1268
1269
1270 cards = 0;
1271 for (i = 0; i < SNDRV_CARDS; i++) {
1272 struct platform_device *device;
1273 if (!enable[i])
1274 continue;
1275 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1276 i, NULL, 0);
1277 if (IS_ERR(device))
1278 continue;
1279 if (!platform_get_drvdata(device)) {
1280 platform_device_unregister(device);
1281 continue;
1282 }
1283 devices[i] = device;
1284 cards++;
1285 }
1286 if (!cards) {
1287#ifdef MODULE
1288 printk(KERN_ERR "aloop: No loopback enabled\n");
1289#endif
1290 loopback_unregister_all();
1291 return -ENODEV;
1292 }
1293 return 0;
1294}
1295
1296static void __exit alsa_card_loopback_exit(void)
1297{
1298 loopback_unregister_all();
1299}
1300
1301module_init(alsa_card_loopback_init)
1302module_exit(alsa_card_loopback_exit)