blob: 006521db487d7408bd076a74ae2e2ce9d487c1ac [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 Iwaib088b532018-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
195#define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
196#define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
197#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
198
199static int loopback_check_format(struct loopback_cable *cable, int stream)
200{
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200201 struct snd_pcm_runtime *runtime, *cruntime;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200202 struct loopback_setup *setup;
203 struct snd_card *card;
204 int check;
205
206 if (cable->valid != CABLE_VALID_BOTH) {
207 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
208 goto __notify;
209 return 0;
210 }
211 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
212 substream->runtime;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200213 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
214 substream->runtime;
215 check = runtime->format != cruntime->format ||
216 runtime->rate != cruntime->rate ||
217 runtime->channels != cruntime->channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200218 if (!check)
219 return 0;
220 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
221 return -EIO;
222 } else {
223 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
224 substream, SNDRV_PCM_STATE_DRAINING);
225 __notify:
226 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
227 substream->runtime;
228 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
229 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
230 if (setup->format != runtime->format) {
231 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
232 &setup->format_id);
233 setup->format = runtime->format;
234 }
235 if (setup->rate != runtime->rate) {
236 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
237 &setup->rate_id);
238 setup->rate = runtime->rate;
239 }
240 if (setup->channels != runtime->channels) {
241 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
242 &setup->channels_id);
243 setup->channels = runtime->channels;
244 }
245 }
246 return 0;
247}
248
249static void loopback_active_notify(struct loopback_pcm *dpcm)
250{
251 snd_ctl_notify(dpcm->loopback->card,
252 SNDRV_CTL_EVENT_MASK_VALUE,
253 &get_setup(dpcm)->active_id);
254}
255
256static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
257{
258 struct snd_pcm_runtime *runtime = substream->runtime;
259 struct loopback_pcm *dpcm = runtime->private_data;
260 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200261 int err, stream = 1 << substream->stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200262
263 switch (cmd) {
264 case SNDRV_PCM_TRIGGER_START:
265 err = loopback_check_format(cable, substream->stream);
266 if (err < 0)
267 return err;
268 dpcm->last_jiffies = jiffies;
269 dpcm->pcm_rate_shift = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200270 dpcm->last_drift = 0;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200271 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200272 cable->running |= stream;
273 cable->pause &= ~stream;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200274 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200275 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200276 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
277 loopback_active_notify(dpcm);
278 break;
279 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200280 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200281 cable->running &= ~stream;
282 cable->pause &= ~stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200283 loopback_timer_stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200284 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
286 loopback_active_notify(dpcm);
287 break;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200288 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100289 case SNDRV_PCM_TRIGGER_SUSPEND:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200290 spin_lock(&cable->lock);
291 cable->pause |= stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200292 loopback_timer_stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200293 spin_unlock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200294 break;
295 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100296 case SNDRV_PCM_TRIGGER_RESUME:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200297 spin_lock(&cable->lock);
298 dpcm->last_jiffies = jiffies;
299 cable->pause &= ~stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200300 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200301 spin_unlock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200302 break;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200303 default:
304 return -EINVAL;
305 }
306 return 0;
307}
308
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200309static void params_change_substream(struct loopback_pcm *dpcm,
310 struct snd_pcm_runtime *runtime)
311{
312 struct snd_pcm_runtime *dst_runtime;
313
314 if (dpcm == NULL || dpcm->substream == NULL)
315 return;
316 dst_runtime = dpcm->substream->runtime;
317 if (dst_runtime == NULL)
318 return;
319 dst_runtime->hw = dpcm->cable->hw;
320}
321
322static void params_change(struct snd_pcm_substream *substream)
323{
324 struct snd_pcm_runtime *runtime = substream->runtime;
325 struct loopback_pcm *dpcm = runtime->private_data;
326 struct loopback_cable *cable = dpcm->cable;
327
Eldad Zack74c34ca2013-04-23 01:00:41 +0200328 cable->hw.formats = pcm_format_to_bits(runtime->format);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200329 cable->hw.rate_min = runtime->rate;
330 cable->hw.rate_max = runtime->rate;
331 cable->hw.channels_min = runtime->channels;
332 cable->hw.channels_max = runtime->channels;
333 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
334 runtime);
335 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
336 runtime);
337}
338
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200339static int loopback_prepare(struct snd_pcm_substream *substream)
340{
341 struct snd_pcm_runtime *runtime = substream->runtime;
342 struct loopback_pcm *dpcm = runtime->private_data;
343 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200344 int bps, salign;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200345
346 salign = (snd_pcm_format_width(runtime->format) *
347 runtime->channels) / 8;
348 bps = salign * runtime->rate;
349 if (bps <= 0 || salign <= 0)
350 return -EINVAL;
351
352 dpcm->buf_pos = 0;
353 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
354 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
355 /* clear capture buffer */
356 dpcm->silent_size = dpcm->pcm_buffer_size;
357 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
358 runtime->buffer_size * runtime->channels);
359 }
360
361 dpcm->irq_pos = 0;
362 dpcm->period_update_pending = 0;
363 dpcm->pcm_bps = bps;
364 dpcm->pcm_salign = salign;
365 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
366
367 mutex_lock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200368 if (!(cable->valid & ~(1 << substream->stream)) ||
369 (get_setup(dpcm)->notify &&
370 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
371 params_change(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200372 cable->valid |= 1 << substream->stream;
373 mutex_unlock(&dpcm->loopback->cable_lock);
374
375 return 0;
376}
377
378static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
379{
380 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
381 char *dst = runtime->dma_area;
382 unsigned int dst_off = dpcm->buf_pos;
383
384 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
385 return;
386 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
387 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
388
389 for (;;) {
390 unsigned int size = bytes;
391 if (dst_off + size > dpcm->pcm_buffer_size)
392 size = dpcm->pcm_buffer_size - dst_off;
393 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
394 bytes_to_frames(runtime, size) *
395 runtime->channels);
396 dpcm->silent_size += size;
397 bytes -= size;
398 if (!bytes)
399 break;
400 dst_off = 0;
401 }
402}
403
404static void copy_play_buf(struct loopback_pcm *play,
405 struct loopback_pcm *capt,
406 unsigned int bytes)
407{
408 struct snd_pcm_runtime *runtime = play->substream->runtime;
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200409 char *src = runtime->dma_area;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200410 char *dst = capt->substream->runtime->dma_area;
411 unsigned int src_off = play->buf_pos;
412 unsigned int dst_off = capt->buf_pos;
413 unsigned int clear_bytes = 0;
414
415 /* check if playback is draining, trim the capture copy size
416 * when our pointer is at the end of playback ring buffer */
417 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
418 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
419 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
420 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
421 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
422 appl_ptr1 += play->buf_pos / play->pcm_salign;
423 if (appl_ptr < appl_ptr1)
424 appl_ptr1 -= runtime->buffer_size;
425 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
426 if (diff < bytes) {
427 clear_bytes = bytes - diff;
428 bytes = diff;
429 }
430 }
431
432 for (;;) {
433 unsigned int size = bytes;
434 if (src_off + size > play->pcm_buffer_size)
435 size = play->pcm_buffer_size - src_off;
436 if (dst_off + size > capt->pcm_buffer_size)
437 size = capt->pcm_buffer_size - dst_off;
438 memcpy(dst + dst_off, src + src_off, size);
439 capt->silent_size = 0;
440 bytes -= size;
441 if (!bytes)
442 break;
443 src_off = (src_off + size) % play->pcm_buffer_size;
444 dst_off = (dst_off + size) % capt->pcm_buffer_size;
445 }
446
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200447 if (clear_bytes > 0) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200448 clear_capture_buf(capt, clear_bytes);
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200449 capt->silent_size = 0;
450 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200451}
452
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200453static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
454 unsigned int jiffies_delta)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200455{
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200456 unsigned long last_pos;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200457 unsigned int delta;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200458
459 last_pos = byte_pos(dpcm, dpcm->irq_pos);
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200460 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
461 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
462 if (delta >= dpcm->last_drift)
463 delta -= dpcm->last_drift;
464 dpcm->last_drift = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200465 if (dpcm->irq_pos >= dpcm->period_size_frac) {
466 dpcm->irq_pos %= dpcm->period_size_frac;
467 dpcm->period_update_pending = 1;
468 }
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200469 return delta;
470}
471
472static inline void bytepos_finish(struct loopback_pcm *dpcm,
473 unsigned int delta)
474{
475 dpcm->buf_pos += delta;
476 dpcm->buf_pos %= dpcm->pcm_buffer_size;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200477}
478
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200479/* call in cable->lock */
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200480static unsigned int loopback_pos_update(struct loopback_cable *cable)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200481{
482 struct loopback_pcm *dpcm_play =
483 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
484 struct loopback_pcm *dpcm_capt =
485 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
486 unsigned long delta_play = 0, delta_capt = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200487 unsigned int running, count1, count2;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200488
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200489 running = cable->running ^ cable->pause;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200490 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200491 delta_play = jiffies - dpcm_play->last_jiffies;
492 dpcm_play->last_jiffies += delta_play;
493 }
494
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200495 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200496 delta_capt = jiffies - dpcm_capt->last_jiffies;
497 dpcm_capt->last_jiffies += delta_capt;
498 }
499
Takashi Iwai98d21df2011-03-18 07:31:53 +0100500 if (delta_play == 0 && delta_capt == 0)
501 goto unlock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200502
503 if (delta_play > delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200504 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
505 bytepos_finish(dpcm_play, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200506 delta_play = delta_capt;
507 } else if (delta_play < delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200508 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
509 clear_capture_buf(dpcm_capt, count1);
510 bytepos_finish(dpcm_capt, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200511 delta_capt = delta_play;
512 }
513
Takashi Iwai98d21df2011-03-18 07:31:53 +0100514 if (delta_play == 0 && delta_capt == 0)
515 goto unlock;
516
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200517 /* note delta_capt == delta_play at this moment */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200518 count1 = bytepos_delta(dpcm_play, delta_play);
519 count2 = bytepos_delta(dpcm_capt, delta_capt);
520 if (count1 < count2) {
521 dpcm_capt->last_drift = count2 - count1;
522 count1 = count2;
523 } else if (count1 > count2) {
524 dpcm_play->last_drift = count1 - count2;
525 }
526 copy_play_buf(dpcm_play, dpcm_capt, count1);
527 bytepos_finish(dpcm_play, count1);
528 bytepos_finish(dpcm_capt, count1);
Takashi Iwai98d21df2011-03-18 07:31:53 +0100529 unlock:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200530 return running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200531}
532
Kees Cookbc47ba92017-10-24 08:34:29 -0700533static void loopback_timer_function(struct timer_list *t)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200534{
Kees Cookbc47ba92017-10-24 08:34:29 -0700535 struct loopback_pcm *dpcm = from_timer(dpcm, t, timer);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200536 unsigned long flags;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200537
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200538 spin_lock_irqsave(&dpcm->cable->lock, flags);
539 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200540 loopback_timer_start(dpcm);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200541 if (dpcm->period_update_pending) {
542 dpcm->period_update_pending = 0;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200543 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
544 /* need to unlock before calling below */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200545 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200546 return;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200547 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200548 }
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200549 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200550}
551
552static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
553{
554 struct snd_pcm_runtime *runtime = substream->runtime;
555 struct loopback_pcm *dpcm = runtime->private_data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200556 snd_pcm_uframes_t pos;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200557
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200558 spin_lock(&dpcm->cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200559 loopback_pos_update(dpcm->cable);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200560 pos = dpcm->buf_pos;
561 spin_unlock(&dpcm->cable->lock);
562 return bytes_to_frames(runtime, pos);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200563}
564
Bhumika Goyalb6c0b712017-08-17 14:45:51 +0530565static const struct snd_pcm_hardware loopback_pcm_hardware =
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200566{
567 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
Takashi Iwaiedac8942013-02-04 10:28:15 +0100568 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
569 SNDRV_PCM_INFO_RESUME),
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200570 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
571 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
572 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
573 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
574 .rate_min = 8000,
575 .rate_max = 192000,
576 .channels_min = 1,
577 .channels_max = 32,
578 .buffer_bytes_max = 2 * 1024 * 1024,
579 .period_bytes_min = 64,
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200580 /* note check overflow in frac_pos() using pcm_rate_shift before
581 changing period_bytes_max value */
582 .period_bytes_max = 1024 * 1024,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200583 .periods_min = 1,
584 .periods_max = 1024,
585 .fifo_size = 0,
586};
587
588static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
589{
590 struct loopback_pcm *dpcm = runtime->private_data;
591 kfree(dpcm);
592}
593
594static int loopback_hw_params(struct snd_pcm_substream *substream,
595 struct snd_pcm_hw_params *params)
596{
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200597 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
598 params_buffer_bytes(params));
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200599}
600
601static int loopback_hw_free(struct snd_pcm_substream *substream)
602{
603 struct snd_pcm_runtime *runtime = substream->runtime;
604 struct loopback_pcm *dpcm = runtime->private_data;
605 struct loopback_cable *cable = dpcm->cable;
606
607 mutex_lock(&dpcm->loopback->cable_lock);
608 cable->valid &= ~(1 << substream->stream);
609 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200610 return snd_pcm_lib_free_vmalloc_buffer(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200611}
612
613static unsigned int get_cable_index(struct snd_pcm_substream *substream)
614{
615 if (!substream->pcm->device)
616 return substream->stream;
617 else
618 return !substream->stream;
619}
620
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200621static int rule_format(struct snd_pcm_hw_params *params,
622 struct snd_pcm_hw_rule *rule)
623{
624
625 struct snd_pcm_hardware *hw = rule->private;
Takashi Iwaib088b532018-01-05 16:15:33 +0100626 struct snd_mask m;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200627
Takashi Iwaib088b532018-01-05 16:15:33 +0100628 snd_mask_none(&m);
629 m.bits[0] = (u_int32_t)hw->formats;
630 m.bits[1] = (u_int32_t)(hw->formats >> 32);
631 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200632}
633
634static int rule_rate(struct snd_pcm_hw_params *params,
635 struct snd_pcm_hw_rule *rule)
636{
637 struct snd_pcm_hardware *hw = rule->private;
638 struct snd_interval t;
639
640 t.min = hw->rate_min;
641 t.max = hw->rate_max;
642 t.openmin = t.openmax = 0;
643 t.integer = 0;
644 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
645}
646
647static int rule_channels(struct snd_pcm_hw_params *params,
648 struct snd_pcm_hw_rule *rule)
649{
650 struct snd_pcm_hardware *hw = rule->private;
651 struct snd_interval t;
652
653 t.min = hw->channels_min;
654 t.max = hw->channels_max;
655 t.openmin = t.openmax = 0;
656 t.integer = 0;
657 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
658}
659
Takashi Iwai96853472018-01-05 16:09:47 +0100660static void free_cable(struct snd_pcm_substream *substream)
661{
662 struct loopback *loopback = substream->private_data;
663 int dev = get_cable_index(substream);
664 struct loopback_cable *cable;
665
666 cable = loopback->cables[substream->number][dev];
667 if (!cable)
668 return;
669 if (cable->streams[!substream->stream]) {
670 /* other stream is still alive */
671 cable->streams[substream->stream] = NULL;
672 } else {
673 /* free the cable */
674 loopback->cables[substream->number][dev] = NULL;
675 kfree(cable);
676 }
677}
678
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200679static int loopback_open(struct snd_pcm_substream *substream)
680{
681 struct snd_pcm_runtime *runtime = substream->runtime;
682 struct loopback *loopback = substream->private_data;
683 struct loopback_pcm *dpcm;
Takashi Iwai96853472018-01-05 16:09:47 +0100684 struct loopback_cable *cable = NULL;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200685 int err = 0;
686 int dev = get_cable_index(substream);
687
688 mutex_lock(&loopback->cable_lock);
689 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
690 if (!dpcm) {
691 err = -ENOMEM;
692 goto unlock;
693 }
694 dpcm->loopback = loopback;
695 dpcm->substream = substream;
Kees Cookbc47ba92017-10-24 08:34:29 -0700696 timer_setup(&dpcm->timer, loopback_timer_function, 0);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200697
698 cable = loopback->cables[substream->number][dev];
699 if (!cable) {
700 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
701 if (!cable) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200702 err = -ENOMEM;
703 goto unlock;
704 }
705 spin_lock_init(&cable->lock);
706 cable->hw = loopback_pcm_hardware;
707 loopback->cables[substream->number][dev] = cable;
708 }
709 dpcm->cable = cable;
710 cable->streams[substream->stream] = dpcm;
711
712 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
713
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200714 /* use dynamic rules based on actual runtime->hw values */
715 /* note that the default rules created in the PCM midlevel code */
716 /* are cached -> they do not reflect the actual state */
717 err = snd_pcm_hw_rule_add(runtime, 0,
718 SNDRV_PCM_HW_PARAM_FORMAT,
719 rule_format, &runtime->hw,
720 SNDRV_PCM_HW_PARAM_FORMAT, -1);
721 if (err < 0)
722 goto unlock;
723 err = snd_pcm_hw_rule_add(runtime, 0,
724 SNDRV_PCM_HW_PARAM_RATE,
725 rule_rate, &runtime->hw,
726 SNDRV_PCM_HW_PARAM_RATE, -1);
727 if (err < 0)
728 goto unlock;
729 err = snd_pcm_hw_rule_add(runtime, 0,
730 SNDRV_PCM_HW_PARAM_CHANNELS,
731 rule_channels, &runtime->hw,
732 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
733 if (err < 0)
734 goto unlock;
735
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200736 runtime->private_data = dpcm;
737 runtime->private_free = loopback_runtime_free;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200738 if (get_notify(dpcm))
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200739 runtime->hw = loopback_pcm_hardware;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200740 else
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200741 runtime->hw = cable->hw;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200742 unlock:
Takashi Iwai96853472018-01-05 16:09:47 +0100743 if (err < 0) {
744 free_cable(substream);
745 kfree(dpcm);
746 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200747 mutex_unlock(&loopback->cable_lock);
748 return err;
749}
750
751static int loopback_close(struct snd_pcm_substream *substream)
752{
753 struct loopback *loopback = substream->private_data;
754 struct loopback_pcm *dpcm = substream->runtime->private_data;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200755
756 loopback_timer_stop(dpcm);
757 mutex_lock(&loopback->cable_lock);
Takashi Iwai96853472018-01-05 16:09:47 +0100758 free_cable(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200759 mutex_unlock(&loopback->cable_lock);
760 return 0;
761}
762
Arvind Yadavf9592582017-08-18 13:15:12 +0530763static const struct snd_pcm_ops loopback_playback_ops = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200764 .open = loopback_open,
765 .close = loopback_close,
766 .ioctl = snd_pcm_lib_ioctl,
767 .hw_params = loopback_hw_params,
768 .hw_free = loopback_hw_free,
769 .prepare = loopback_prepare,
770 .trigger = loopback_trigger,
771 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200772 .page = snd_pcm_lib_get_vmalloc_page,
773 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200774};
775
Arvind Yadavf9592582017-08-18 13:15:12 +0530776static const struct snd_pcm_ops loopback_capture_ops = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200777 .open = loopback_open,
778 .close = loopback_close,
779 .ioctl = snd_pcm_lib_ioctl,
780 .hw_params = loopback_hw_params,
781 .hw_free = loopback_hw_free,
782 .prepare = loopback_prepare,
783 .trigger = loopback_trigger,
784 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200785 .page = snd_pcm_lib_get_vmalloc_page,
786 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200787};
788
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500789static int loopback_pcm_new(struct loopback *loopback,
790 int device, int substreams)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200791{
792 struct snd_pcm *pcm;
793 int err;
794
795 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
796 substreams, substreams, &pcm);
797 if (err < 0)
798 return err;
799 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
800 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
801
802 pcm->private_data = loopback;
803 pcm->info_flags = 0;
804 strcpy(pcm->name, "Loopback PCM");
805
806 loopback->pcm[device] = pcm;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200807 return 0;
808}
809
810static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
811 struct snd_ctl_elem_info *uinfo)
812{
813 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
814 uinfo->count = 1;
815 uinfo->value.integer.min = 80000;
816 uinfo->value.integer.max = 120000;
817 uinfo->value.integer.step = 1;
818 return 0;
819}
820
821static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
822 struct snd_ctl_elem_value *ucontrol)
823{
824 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
825
826 ucontrol->value.integer.value[0] =
827 loopback->setup[kcontrol->id.subdevice]
828 [kcontrol->id.device].rate_shift;
829 return 0;
830}
831
832static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
833 struct snd_ctl_elem_value *ucontrol)
834{
835 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
836 unsigned int val;
837 int change = 0;
838
839 val = ucontrol->value.integer.value[0];
840 if (val < 80000)
841 val = 80000;
842 if (val > 120000)
843 val = 120000;
844 mutex_lock(&loopback->cable_lock);
845 if (val != loopback->setup[kcontrol->id.subdevice]
846 [kcontrol->id.device].rate_shift) {
847 loopback->setup[kcontrol->id.subdevice]
848 [kcontrol->id.device].rate_shift = val;
849 change = 1;
850 }
851 mutex_unlock(&loopback->cable_lock);
852 return change;
853}
854
855static int loopback_notify_get(struct snd_kcontrol *kcontrol,
856 struct snd_ctl_elem_value *ucontrol)
857{
858 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
859
860 ucontrol->value.integer.value[0] =
861 loopback->setup[kcontrol->id.subdevice]
862 [kcontrol->id.device].notify;
863 return 0;
864}
865
866static int loopback_notify_put(struct snd_kcontrol *kcontrol,
867 struct snd_ctl_elem_value *ucontrol)
868{
869 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
870 unsigned int val;
871 int change = 0;
872
873 val = ucontrol->value.integer.value[0] ? 1 : 0;
874 if (val != loopback->setup[kcontrol->id.subdevice]
875 [kcontrol->id.device].notify) {
876 loopback->setup[kcontrol->id.subdevice]
877 [kcontrol->id.device].notify = val;
878 change = 1;
879 }
880 return change;
881}
882
883static int loopback_active_get(struct snd_kcontrol *kcontrol,
884 struct snd_ctl_elem_value *ucontrol)
885{
886 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
887 struct loopback_cable *cable = loopback->cables
Jaroslav Kyselaac446fb2010-10-02 16:00:53 +0200888 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200889 unsigned int val = 0;
890
891 if (cable != NULL)
892 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
893 1 : 0;
894 ucontrol->value.integer.value[0] = val;
895 return 0;
896}
897
898static int loopback_format_info(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_info *uinfo)
900{
901 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
902 uinfo->count = 1;
903 uinfo->value.integer.min = 0;
904 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
905 uinfo->value.integer.step = 1;
906 return 0;
907}
908
909static int loopback_format_get(struct snd_kcontrol *kcontrol,
910 struct snd_ctl_elem_value *ucontrol)
911{
912 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
913
914 ucontrol->value.integer.value[0] =
915 loopback->setup[kcontrol->id.subdevice]
916 [kcontrol->id.device].format;
917 return 0;
918}
919
920static int loopback_rate_info(struct snd_kcontrol *kcontrol,
921 struct snd_ctl_elem_info *uinfo)
922{
923 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
924 uinfo->count = 1;
925 uinfo->value.integer.min = 0;
926 uinfo->value.integer.max = 192000;
927 uinfo->value.integer.step = 1;
928 return 0;
929}
930
931static int loopback_rate_get(struct snd_kcontrol *kcontrol,
932 struct snd_ctl_elem_value *ucontrol)
933{
934 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
935
936 ucontrol->value.integer.value[0] =
937 loopback->setup[kcontrol->id.subdevice]
938 [kcontrol->id.device].rate;
939 return 0;
940}
941
942static int loopback_channels_info(struct snd_kcontrol *kcontrol,
943 struct snd_ctl_elem_info *uinfo)
944{
945 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
946 uinfo->count = 1;
947 uinfo->value.integer.min = 1;
948 uinfo->value.integer.max = 1024;
949 uinfo->value.integer.step = 1;
950 return 0;
951}
952
953static int loopback_channels_get(struct snd_kcontrol *kcontrol,
954 struct snd_ctl_elem_value *ucontrol)
955{
956 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
957
958 ucontrol->value.integer.value[0] =
959 loopback->setup[kcontrol->id.subdevice]
Jaroslav Kysela1446c5f2010-09-15 08:01:57 +0200960 [kcontrol->id.device].channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200961 return 0;
962}
963
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500964static struct snd_kcontrol_new loopback_controls[] = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200965{
966 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
967 .name = "PCM Rate Shift 100000",
968 .info = loopback_rate_shift_info,
969 .get = loopback_rate_shift_get,
970 .put = loopback_rate_shift_put,
971},
972{
973 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
974 .name = "PCM Notify",
975 .info = snd_ctl_boolean_mono_info,
976 .get = loopback_notify_get,
977 .put = loopback_notify_put,
978},
979#define ACTIVE_IDX 2
980{
981 .access = SNDRV_CTL_ELEM_ACCESS_READ,
982 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
983 .name = "PCM Slave Active",
984 .info = snd_ctl_boolean_mono_info,
985 .get = loopback_active_get,
986},
987#define FORMAT_IDX 3
988{
989 .access = SNDRV_CTL_ELEM_ACCESS_READ,
990 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
991 .name = "PCM Slave Format",
992 .info = loopback_format_info,
993 .get = loopback_format_get
994},
995#define RATE_IDX 4
996{
997 .access = SNDRV_CTL_ELEM_ACCESS_READ,
998 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
999 .name = "PCM Slave Rate",
1000 .info = loopback_rate_info,
1001 .get = loopback_rate_get
1002},
1003#define CHANNELS_IDX 5
1004{
1005 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1006 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1007 .name = "PCM Slave Channels",
1008 .info = loopback_channels_info,
1009 .get = loopback_channels_get
1010}
1011};
1012
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001013static int loopback_mixer_new(struct loopback *loopback, int notify)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001014{
1015 struct snd_card *card = loopback->card;
1016 struct snd_pcm *pcm;
1017 struct snd_kcontrol *kctl;
1018 struct loopback_setup *setup;
1019 int err, dev, substr, substr_count, idx;
1020
1021 strcpy(card->mixername, "Loopback Mixer");
1022 for (dev = 0; dev < 2; dev++) {
1023 pcm = loopback->pcm[dev];
1024 substr_count =
1025 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1026 for (substr = 0; substr < substr_count; substr++) {
1027 setup = &loopback->setup[substr][dev];
1028 setup->notify = notify;
1029 setup->rate_shift = NO_PITCH;
1030 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1031 setup->rate = 48000;
1032 setup->channels = 2;
1033 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1034 idx++) {
1035 kctl = snd_ctl_new1(&loopback_controls[idx],
1036 loopback);
1037 if (!kctl)
1038 return -ENOMEM;
1039 kctl->id.device = dev;
1040 kctl->id.subdevice = substr;
1041 switch (idx) {
1042 case ACTIVE_IDX:
1043 setup->active_id = kctl->id;
1044 break;
1045 case FORMAT_IDX:
1046 setup->format_id = kctl->id;
1047 break;
1048 case RATE_IDX:
1049 setup->rate_id = kctl->id;
1050 break;
1051 case CHANNELS_IDX:
1052 setup->channels_id = kctl->id;
1053 break;
1054 default:
1055 break;
1056 }
1057 err = snd_ctl_add(card, kctl);
1058 if (err < 0)
1059 return err;
1060 }
1061 }
1062 }
1063 return 0;
1064}
1065
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001066static void print_dpcm_info(struct snd_info_buffer *buffer,
1067 struct loopback_pcm *dpcm,
1068 const char *id)
1069{
1070 snd_iprintf(buffer, " %s\n", id);
1071 if (dpcm == NULL) {
1072 snd_iprintf(buffer, " inactive\n");
1073 return;
1074 }
1075 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1076 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1077 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1078 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1079 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1080 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1081 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1082 snd_iprintf(buffer, " update_pending:\t%u\n",
1083 dpcm->period_update_pending);
1084 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1085 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1086 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1087 dpcm->last_jiffies, jiffies);
1088 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1089}
1090
1091static void print_substream_info(struct snd_info_buffer *buffer,
1092 struct loopback *loopback,
1093 int sub,
1094 int num)
1095{
1096 struct loopback_cable *cable = loopback->cables[sub][num];
1097
1098 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1099 if (cable == NULL) {
1100 snd_iprintf(buffer, " inactive\n");
1101 return;
1102 }
1103 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1104 snd_iprintf(buffer, " running: %u\n", cable->running);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +02001105 snd_iprintf(buffer, " pause: %u\n", cable->pause);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001106 print_dpcm_info(buffer, cable->streams[0], "Playback");
1107 print_dpcm_info(buffer, cable->streams[1], "Capture");
1108}
1109
1110static void print_cable_info(struct snd_info_entry *entry,
1111 struct snd_info_buffer *buffer)
1112{
1113 struct loopback *loopback = entry->private_data;
1114 int sub, num;
1115
1116 mutex_lock(&loopback->cable_lock);
1117 num = entry->name[strlen(entry->name)-1];
1118 num = num == '0' ? 0 : 1;
1119 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1120 print_substream_info(buffer, loopback, sub, num);
1121 mutex_unlock(&loopback->cable_lock);
1122}
1123
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001124static int loopback_proc_new(struct loopback *loopback, int cidx)
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001125{
1126 char name[32];
1127 struct snd_info_entry *entry;
1128 int err;
1129
1130 snprintf(name, sizeof(name), "cable#%d", cidx);
1131 err = snd_card_proc_new(loopback->card, name, &entry);
1132 if (err < 0)
1133 return err;
1134
1135 snd_info_set_text_ops(entry, loopback, print_cable_info);
1136 return 0;
1137}
1138
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001139static int loopback_probe(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001140{
1141 struct snd_card *card;
1142 struct loopback *loopback;
1143 int dev = devptr->id;
1144 int err;
1145
Takashi Iwai5872f3f2014-01-29 12:59:08 +01001146 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1147 sizeof(struct loopback), &card);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001148 if (err < 0)
1149 return err;
1150 loopback = card->private_data;
1151
1152 if (pcm_substreams[dev] < 1)
1153 pcm_substreams[dev] = 1;
1154 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1155 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1156
1157 loopback->card = card;
1158 mutex_init(&loopback->cable_lock);
1159
1160 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1161 if (err < 0)
1162 goto __nodev;
1163 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1164 if (err < 0)
1165 goto __nodev;
1166 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1167 if (err < 0)
1168 goto __nodev;
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001169 loopback_proc_new(loopback, 0);
1170 loopback_proc_new(loopback, 1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001171 strcpy(card->driver, "Loopback");
1172 strcpy(card->shortname, "Loopback");
1173 sprintf(card->longname, "Loopback %i", dev + 1);
1174 err = snd_card_register(card);
1175 if (!err) {
1176 platform_set_drvdata(devptr, card);
1177 return 0;
1178 }
1179 __nodev:
1180 snd_card_free(card);
1181 return err;
1182}
1183
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001184static int loopback_remove(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001185{
1186 snd_card_free(platform_get_drvdata(devptr));
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001187 return 0;
1188}
1189
Takashi Iwaid34e4e02012-08-09 15:47:15 +02001190#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001191static int loopback_suspend(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001192{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001193 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001194 struct loopback *loopback = card->private_data;
1195
1196 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1197
1198 snd_pcm_suspend_all(loopback->pcm[0]);
1199 snd_pcm_suspend_all(loopback->pcm[1]);
1200 return 0;
1201}
1202
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001203static int loopback_resume(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001204{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001205 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001206
1207 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1208 return 0;
1209}
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001210
1211static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1212#define LOOPBACK_PM_OPS &loopback_pm
1213#else
1214#define LOOPBACK_PM_OPS NULL
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001215#endif
1216
1217#define SND_LOOPBACK_DRIVER "snd_aloop"
1218
1219static struct platform_driver loopback_driver = {
1220 .probe = loopback_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001221 .remove = loopback_remove,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001222 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +02001223 .name = SND_LOOPBACK_DRIVER,
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001224 .pm = LOOPBACK_PM_OPS,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001225 },
1226};
1227
1228static void loopback_unregister_all(void)
1229{
1230 int i;
1231
1232 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1233 platform_device_unregister(devices[i]);
1234 platform_driver_unregister(&loopback_driver);
1235}
1236
1237static int __init alsa_card_loopback_init(void)
1238{
1239 int i, err, cards;
1240
1241 err = platform_driver_register(&loopback_driver);
1242 if (err < 0)
1243 return err;
1244
1245
1246 cards = 0;
1247 for (i = 0; i < SNDRV_CARDS; i++) {
1248 struct platform_device *device;
1249 if (!enable[i])
1250 continue;
1251 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1252 i, NULL, 0);
1253 if (IS_ERR(device))
1254 continue;
1255 if (!platform_get_drvdata(device)) {
1256 platform_device_unregister(device);
1257 continue;
1258 }
1259 devices[i] = device;
1260 cards++;
1261 }
1262 if (!cards) {
1263#ifdef MODULE
1264 printk(KERN_ERR "aloop: No loopback enabled\n");
1265#endif
1266 loopback_unregister_all();
1267 return -ENODEV;
1268 }
1269 return 0;
1270}
1271
1272static void __exit alsa_card_loopback_exit(void)
1273{
1274 loopback_unregister_all();
1275}
1276
1277module_init(alsa_card_loopback_init)
1278module_exit(alsa_card_loopback_exit)