blob: cbd20cb8ca1127704b9448952f5d907e4a17432d [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
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(struct snd_pcm_substream *substream)
310{
311 struct snd_pcm_runtime *runtime = substream->runtime;
312 struct loopback_pcm *dpcm = runtime->private_data;
313 struct loopback_cable *cable = dpcm->cable;
314
Eldad Zack74c34ca2013-04-23 01:00:41 +0200315 cable->hw.formats = pcm_format_to_bits(runtime->format);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200316 cable->hw.rate_min = runtime->rate;
317 cable->hw.rate_max = runtime->rate;
318 cable->hw.channels_min = runtime->channels;
319 cable->hw.channels_max = runtime->channels;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200320}
321
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200322static int loopback_prepare(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;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200327 int bps, salign;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200328
329 salign = (snd_pcm_format_width(runtime->format) *
330 runtime->channels) / 8;
331 bps = salign * runtime->rate;
332 if (bps <= 0 || salign <= 0)
333 return -EINVAL;
334
335 dpcm->buf_pos = 0;
336 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
337 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
338 /* clear capture buffer */
339 dpcm->silent_size = dpcm->pcm_buffer_size;
340 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
341 runtime->buffer_size * runtime->channels);
342 }
343
344 dpcm->irq_pos = 0;
345 dpcm->period_update_pending = 0;
346 dpcm->pcm_bps = bps;
347 dpcm->pcm_salign = salign;
348 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
349
350 mutex_lock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200351 if (!(cable->valid & ~(1 << substream->stream)) ||
352 (get_setup(dpcm)->notify &&
353 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
354 params_change(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200355 cable->valid |= 1 << substream->stream;
356 mutex_unlock(&dpcm->loopback->cable_lock);
357
358 return 0;
359}
360
361static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
362{
363 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
364 char *dst = runtime->dma_area;
365 unsigned int dst_off = dpcm->buf_pos;
366
367 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
368 return;
369 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
370 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
371
372 for (;;) {
373 unsigned int size = bytes;
374 if (dst_off + size > dpcm->pcm_buffer_size)
375 size = dpcm->pcm_buffer_size - dst_off;
376 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
377 bytes_to_frames(runtime, size) *
378 runtime->channels);
379 dpcm->silent_size += size;
380 bytes -= size;
381 if (!bytes)
382 break;
383 dst_off = 0;
384 }
385}
386
387static void copy_play_buf(struct loopback_pcm *play,
388 struct loopback_pcm *capt,
389 unsigned int bytes)
390{
391 struct snd_pcm_runtime *runtime = play->substream->runtime;
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200392 char *src = runtime->dma_area;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200393 char *dst = capt->substream->runtime->dma_area;
394 unsigned int src_off = play->buf_pos;
395 unsigned int dst_off = capt->buf_pos;
396 unsigned int clear_bytes = 0;
397
398 /* check if playback is draining, trim the capture copy size
399 * when our pointer is at the end of playback ring buffer */
400 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
401 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
402 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
403 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
404 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
405 appl_ptr1 += play->buf_pos / play->pcm_salign;
406 if (appl_ptr < appl_ptr1)
407 appl_ptr1 -= runtime->buffer_size;
408 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
409 if (diff < bytes) {
410 clear_bytes = bytes - diff;
411 bytes = diff;
412 }
413 }
414
415 for (;;) {
416 unsigned int size = bytes;
417 if (src_off + size > play->pcm_buffer_size)
418 size = play->pcm_buffer_size - src_off;
419 if (dst_off + size > capt->pcm_buffer_size)
420 size = capt->pcm_buffer_size - dst_off;
421 memcpy(dst + dst_off, src + src_off, size);
422 capt->silent_size = 0;
423 bytes -= size;
424 if (!bytes)
425 break;
426 src_off = (src_off + size) % play->pcm_buffer_size;
427 dst_off = (dst_off + size) % capt->pcm_buffer_size;
428 }
429
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200430 if (clear_bytes > 0) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200431 clear_capture_buf(capt, clear_bytes);
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200432 capt->silent_size = 0;
433 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200434}
435
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200436static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
437 unsigned int jiffies_delta)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200438{
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200439 unsigned long last_pos;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200440 unsigned int delta;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200441
442 last_pos = byte_pos(dpcm, dpcm->irq_pos);
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200443 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
444 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
445 if (delta >= dpcm->last_drift)
446 delta -= dpcm->last_drift;
447 dpcm->last_drift = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200448 if (dpcm->irq_pos >= dpcm->period_size_frac) {
449 dpcm->irq_pos %= dpcm->period_size_frac;
450 dpcm->period_update_pending = 1;
451 }
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200452 return delta;
453}
454
455static inline void bytepos_finish(struct loopback_pcm *dpcm,
456 unsigned int delta)
457{
458 dpcm->buf_pos += delta;
459 dpcm->buf_pos %= dpcm->pcm_buffer_size;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200460}
461
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200462/* call in cable->lock */
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200463static unsigned int loopback_pos_update(struct loopback_cable *cable)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200464{
465 struct loopback_pcm *dpcm_play =
466 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
467 struct loopback_pcm *dpcm_capt =
468 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
469 unsigned long delta_play = 0, delta_capt = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200470 unsigned int running, count1, count2;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200471
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200472 running = cable->running ^ cable->pause;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200473 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200474 delta_play = jiffies - dpcm_play->last_jiffies;
475 dpcm_play->last_jiffies += delta_play;
476 }
477
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200478 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200479 delta_capt = jiffies - dpcm_capt->last_jiffies;
480 dpcm_capt->last_jiffies += delta_capt;
481 }
482
Takashi Iwai98d21df2011-03-18 07:31:53 +0100483 if (delta_play == 0 && delta_capt == 0)
484 goto unlock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200485
486 if (delta_play > delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200487 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
488 bytepos_finish(dpcm_play, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200489 delta_play = delta_capt;
490 } else if (delta_play < delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200491 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
492 clear_capture_buf(dpcm_capt, count1);
493 bytepos_finish(dpcm_capt, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200494 delta_capt = delta_play;
495 }
496
Takashi Iwai98d21df2011-03-18 07:31:53 +0100497 if (delta_play == 0 && delta_capt == 0)
498 goto unlock;
499
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200500 /* note delta_capt == delta_play at this moment */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200501 count1 = bytepos_delta(dpcm_play, delta_play);
502 count2 = bytepos_delta(dpcm_capt, delta_capt);
503 if (count1 < count2) {
504 dpcm_capt->last_drift = count2 - count1;
505 count1 = count2;
506 } else if (count1 > count2) {
507 dpcm_play->last_drift = count1 - count2;
508 }
509 copy_play_buf(dpcm_play, dpcm_capt, count1);
510 bytepos_finish(dpcm_play, count1);
511 bytepos_finish(dpcm_capt, count1);
Takashi Iwai98d21df2011-03-18 07:31:53 +0100512 unlock:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200513 return running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200514}
515
516static void loopback_timer_function(unsigned long data)
517{
518 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200519 unsigned long flags;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200520
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200521 spin_lock_irqsave(&dpcm->cable->lock, flags);
522 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200523 loopback_timer_start(dpcm);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200524 if (dpcm->period_update_pending) {
525 dpcm->period_update_pending = 0;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200526 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
527 /* need to unlock before calling below */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200528 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200529 return;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200530 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200531 }
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200532 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200533}
534
535static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
536{
537 struct snd_pcm_runtime *runtime = substream->runtime;
538 struct loopback_pcm *dpcm = runtime->private_data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200539 snd_pcm_uframes_t pos;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200540
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200541 spin_lock(&dpcm->cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200542 loopback_pos_update(dpcm->cable);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200543 pos = dpcm->buf_pos;
544 spin_unlock(&dpcm->cable->lock);
545 return bytes_to_frames(runtime, pos);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200546}
547
548static struct snd_pcm_hardware loopback_pcm_hardware =
549{
550 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
Takashi Iwaiedac8942013-02-04 10:28:15 +0100551 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
552 SNDRV_PCM_INFO_RESUME),
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200553 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
554 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
555 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
556 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
557 .rate_min = 8000,
558 .rate_max = 192000,
559 .channels_min = 1,
560 .channels_max = 32,
561 .buffer_bytes_max = 2 * 1024 * 1024,
562 .period_bytes_min = 64,
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200563 /* note check overflow in frac_pos() using pcm_rate_shift before
564 changing period_bytes_max value */
565 .period_bytes_max = 1024 * 1024,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200566 .periods_min = 1,
567 .periods_max = 1024,
568 .fifo_size = 0,
569};
570
571static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
572{
573 struct loopback_pcm *dpcm = runtime->private_data;
574 kfree(dpcm);
575}
576
577static int loopback_hw_params(struct snd_pcm_substream *substream,
578 struct snd_pcm_hw_params *params)
579{
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200580 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
581 params_buffer_bytes(params));
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200582}
583
584static int loopback_hw_free(struct snd_pcm_substream *substream)
585{
586 struct snd_pcm_runtime *runtime = substream->runtime;
587 struct loopback_pcm *dpcm = runtime->private_data;
588 struct loopback_cable *cable = dpcm->cable;
589
590 mutex_lock(&dpcm->loopback->cable_lock);
591 cable->valid &= ~(1 << substream->stream);
592 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200593 return snd_pcm_lib_free_vmalloc_buffer(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200594}
595
596static unsigned int get_cable_index(struct snd_pcm_substream *substream)
597{
598 if (!substream->pcm->device)
599 return substream->stream;
600 else
601 return !substream->stream;
602}
603
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200604static int rule_format(struct snd_pcm_hw_params *params,
605 struct snd_pcm_hw_rule *rule)
606{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100607 struct loopback_pcm *dpcm = rule->private;
608 struct loopback_cable *cable = dpcm->cable;
Takashi Iwai5af666d2018-01-05 16:15:33 +0100609 struct snd_mask m;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200610
Takashi Iwai5af666d2018-01-05 16:15:33 +0100611 snd_mask_none(&m);
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100612 mutex_lock(&dpcm->loopback->cable_lock);
613 m.bits[0] = (u_int32_t)cable->hw.formats;
614 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
615 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai5af666d2018-01-05 16:15:33 +0100616 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200617}
618
619static int rule_rate(struct snd_pcm_hw_params *params,
620 struct snd_pcm_hw_rule *rule)
621{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100622 struct loopback_pcm *dpcm = rule->private;
623 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200624 struct snd_interval t;
625
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100626 mutex_lock(&dpcm->loopback->cable_lock);
627 t.min = cable->hw.rate_min;
628 t.max = cable->hw.rate_max;
629 mutex_unlock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200630 t.openmin = t.openmax = 0;
631 t.integer = 0;
632 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
633}
634
635static int rule_channels(struct snd_pcm_hw_params *params,
636 struct snd_pcm_hw_rule *rule)
637{
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100638 struct loopback_pcm *dpcm = rule->private;
639 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200640 struct snd_interval t;
641
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100642 mutex_lock(&dpcm->loopback->cable_lock);
643 t.min = cable->hw.channels_min;
644 t.max = cable->hw.channels_max;
645 mutex_unlock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200646 t.openmin = t.openmax = 0;
647 t.integer = 0;
648 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
649}
650
Takashi Iwai01046dd2018-01-05 16:09:47 +0100651static void free_cable(struct snd_pcm_substream *substream)
652{
653 struct loopback *loopback = substream->private_data;
654 int dev = get_cable_index(substream);
655 struct loopback_cable *cable;
656
657 cable = loopback->cables[substream->number][dev];
658 if (!cable)
659 return;
660 if (cable->streams[!substream->stream]) {
661 /* other stream is still alive */
662 cable->streams[substream->stream] = NULL;
663 } else {
664 /* free the cable */
665 loopback->cables[substream->number][dev] = NULL;
666 kfree(cable);
667 }
668}
669
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200670static int loopback_open(struct snd_pcm_substream *substream)
671{
672 struct snd_pcm_runtime *runtime = substream->runtime;
673 struct loopback *loopback = substream->private_data;
674 struct loopback_pcm *dpcm;
Takashi Iwai01046dd2018-01-05 16:09:47 +0100675 struct loopback_cable *cable = NULL;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200676 int err = 0;
677 int dev = get_cable_index(substream);
678
679 mutex_lock(&loopback->cable_lock);
680 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
681 if (!dpcm) {
682 err = -ENOMEM;
683 goto unlock;
684 }
685 dpcm->loopback = loopback;
686 dpcm->substream = substream;
687 setup_timer(&dpcm->timer, loopback_timer_function,
688 (unsigned long)dpcm);
689
690 cable = loopback->cables[substream->number][dev];
691 if (!cable) {
692 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
693 if (!cable) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200694 err = -ENOMEM;
695 goto unlock;
696 }
697 spin_lock_init(&cable->lock);
698 cable->hw = loopback_pcm_hardware;
699 loopback->cables[substream->number][dev] = cable;
700 }
701 dpcm->cable = cable;
702 cable->streams[substream->stream] = dpcm;
703
704 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
705
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200706 /* use dynamic rules based on actual runtime->hw values */
707 /* note that the default rules created in the PCM midlevel code */
708 /* are cached -> they do not reflect the actual state */
709 err = snd_pcm_hw_rule_add(runtime, 0,
710 SNDRV_PCM_HW_PARAM_FORMAT,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100711 rule_format, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200712 SNDRV_PCM_HW_PARAM_FORMAT, -1);
713 if (err < 0)
714 goto unlock;
715 err = snd_pcm_hw_rule_add(runtime, 0,
716 SNDRV_PCM_HW_PARAM_RATE,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100717 rule_rate, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200718 SNDRV_PCM_HW_PARAM_RATE, -1);
719 if (err < 0)
720 goto unlock;
721 err = snd_pcm_hw_rule_add(runtime, 0,
722 SNDRV_PCM_HW_PARAM_CHANNELS,
Takashi Iwai43ff00f2018-01-04 17:38:54 +0100723 rule_channels, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200724 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
725 if (err < 0)
726 goto unlock;
727
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200728 runtime->private_data = dpcm;
729 runtime->private_free = loopback_runtime_free;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200730 if (get_notify(dpcm))
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200731 runtime->hw = loopback_pcm_hardware;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200732 else
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200733 runtime->hw = cable->hw;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200734 unlock:
Takashi Iwai01046dd2018-01-05 16:09:47 +0100735 if (err < 0) {
736 free_cable(substream);
737 kfree(dpcm);
738 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200739 mutex_unlock(&loopback->cable_lock);
740 return err;
741}
742
743static int loopback_close(struct snd_pcm_substream *substream)
744{
745 struct loopback *loopback = substream->private_data;
746 struct loopback_pcm *dpcm = substream->runtime->private_data;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200747
748 loopback_timer_stop(dpcm);
749 mutex_lock(&loopback->cable_lock);
Takashi Iwai01046dd2018-01-05 16:09:47 +0100750 free_cable(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200751 mutex_unlock(&loopback->cable_lock);
752 return 0;
753}
754
755static struct snd_pcm_ops loopback_playback_ops = {
756 .open = loopback_open,
757 .close = loopback_close,
758 .ioctl = snd_pcm_lib_ioctl,
759 .hw_params = loopback_hw_params,
760 .hw_free = loopback_hw_free,
761 .prepare = loopback_prepare,
762 .trigger = loopback_trigger,
763 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200764 .page = snd_pcm_lib_get_vmalloc_page,
765 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200766};
767
768static struct snd_pcm_ops loopback_capture_ops = {
769 .open = loopback_open,
770 .close = loopback_close,
771 .ioctl = snd_pcm_lib_ioctl,
772 .hw_params = loopback_hw_params,
773 .hw_free = loopback_hw_free,
774 .prepare = loopback_prepare,
775 .trigger = loopback_trigger,
776 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200777 .page = snd_pcm_lib_get_vmalloc_page,
778 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200779};
780
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500781static int loopback_pcm_new(struct loopback *loopback,
782 int device, int substreams)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200783{
784 struct snd_pcm *pcm;
785 int err;
786
787 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
788 substreams, substreams, &pcm);
789 if (err < 0)
790 return err;
791 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
792 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
793
794 pcm->private_data = loopback;
795 pcm->info_flags = 0;
796 strcpy(pcm->name, "Loopback PCM");
797
798 loopback->pcm[device] = pcm;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200799 return 0;
800}
801
802static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
803 struct snd_ctl_elem_info *uinfo)
804{
805 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
806 uinfo->count = 1;
807 uinfo->value.integer.min = 80000;
808 uinfo->value.integer.max = 120000;
809 uinfo->value.integer.step = 1;
810 return 0;
811}
812
813static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
814 struct snd_ctl_elem_value *ucontrol)
815{
816 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
817
818 ucontrol->value.integer.value[0] =
819 loopback->setup[kcontrol->id.subdevice]
820 [kcontrol->id.device].rate_shift;
821 return 0;
822}
823
824static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
825 struct snd_ctl_elem_value *ucontrol)
826{
827 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
828 unsigned int val;
829 int change = 0;
830
831 val = ucontrol->value.integer.value[0];
832 if (val < 80000)
833 val = 80000;
834 if (val > 120000)
835 val = 120000;
836 mutex_lock(&loopback->cable_lock);
837 if (val != loopback->setup[kcontrol->id.subdevice]
838 [kcontrol->id.device].rate_shift) {
839 loopback->setup[kcontrol->id.subdevice]
840 [kcontrol->id.device].rate_shift = val;
841 change = 1;
842 }
843 mutex_unlock(&loopback->cable_lock);
844 return change;
845}
846
847static int loopback_notify_get(struct snd_kcontrol *kcontrol,
848 struct snd_ctl_elem_value *ucontrol)
849{
850 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
851
852 ucontrol->value.integer.value[0] =
853 loopback->setup[kcontrol->id.subdevice]
854 [kcontrol->id.device].notify;
855 return 0;
856}
857
858static int loopback_notify_put(struct snd_kcontrol *kcontrol,
859 struct snd_ctl_elem_value *ucontrol)
860{
861 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
862 unsigned int val;
863 int change = 0;
864
865 val = ucontrol->value.integer.value[0] ? 1 : 0;
866 if (val != loopback->setup[kcontrol->id.subdevice]
867 [kcontrol->id.device].notify) {
868 loopback->setup[kcontrol->id.subdevice]
869 [kcontrol->id.device].notify = val;
870 change = 1;
871 }
872 return change;
873}
874
875static int loopback_active_get(struct snd_kcontrol *kcontrol,
876 struct snd_ctl_elem_value *ucontrol)
877{
878 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
879 struct loopback_cable *cable = loopback->cables
Jaroslav Kyselaac446fb2010-10-02 16:00:53 +0200880 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200881 unsigned int val = 0;
882
883 if (cable != NULL)
884 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
885 1 : 0;
886 ucontrol->value.integer.value[0] = val;
887 return 0;
888}
889
890static int loopback_format_info(struct snd_kcontrol *kcontrol,
891 struct snd_ctl_elem_info *uinfo)
892{
893 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
894 uinfo->count = 1;
895 uinfo->value.integer.min = 0;
896 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
897 uinfo->value.integer.step = 1;
898 return 0;
899}
900
901static int loopback_format_get(struct snd_kcontrol *kcontrol,
902 struct snd_ctl_elem_value *ucontrol)
903{
904 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
905
906 ucontrol->value.integer.value[0] =
907 loopback->setup[kcontrol->id.subdevice]
908 [kcontrol->id.device].format;
909 return 0;
910}
911
912static int loopback_rate_info(struct snd_kcontrol *kcontrol,
913 struct snd_ctl_elem_info *uinfo)
914{
915 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
916 uinfo->count = 1;
917 uinfo->value.integer.min = 0;
918 uinfo->value.integer.max = 192000;
919 uinfo->value.integer.step = 1;
920 return 0;
921}
922
923static int loopback_rate_get(struct snd_kcontrol *kcontrol,
924 struct snd_ctl_elem_value *ucontrol)
925{
926 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
927
928 ucontrol->value.integer.value[0] =
929 loopback->setup[kcontrol->id.subdevice]
930 [kcontrol->id.device].rate;
931 return 0;
932}
933
934static int loopback_channels_info(struct snd_kcontrol *kcontrol,
935 struct snd_ctl_elem_info *uinfo)
936{
937 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
938 uinfo->count = 1;
939 uinfo->value.integer.min = 1;
940 uinfo->value.integer.max = 1024;
941 uinfo->value.integer.step = 1;
942 return 0;
943}
944
945static int loopback_channels_get(struct snd_kcontrol *kcontrol,
946 struct snd_ctl_elem_value *ucontrol)
947{
948 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
949
950 ucontrol->value.integer.value[0] =
951 loopback->setup[kcontrol->id.subdevice]
Jaroslav Kysela1446c5f2010-09-15 08:01:57 +0200952 [kcontrol->id.device].channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200953 return 0;
954}
955
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500956static struct snd_kcontrol_new loopback_controls[] = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200957{
958 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
959 .name = "PCM Rate Shift 100000",
960 .info = loopback_rate_shift_info,
961 .get = loopback_rate_shift_get,
962 .put = loopback_rate_shift_put,
963},
964{
965 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
966 .name = "PCM Notify",
967 .info = snd_ctl_boolean_mono_info,
968 .get = loopback_notify_get,
969 .put = loopback_notify_put,
970},
971#define ACTIVE_IDX 2
972{
973 .access = SNDRV_CTL_ELEM_ACCESS_READ,
974 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
975 .name = "PCM Slave Active",
976 .info = snd_ctl_boolean_mono_info,
977 .get = loopback_active_get,
978},
979#define FORMAT_IDX 3
980{
981 .access = SNDRV_CTL_ELEM_ACCESS_READ,
982 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
983 .name = "PCM Slave Format",
984 .info = loopback_format_info,
985 .get = loopback_format_get
986},
987#define RATE_IDX 4
988{
989 .access = SNDRV_CTL_ELEM_ACCESS_READ,
990 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
991 .name = "PCM Slave Rate",
992 .info = loopback_rate_info,
993 .get = loopback_rate_get
994},
995#define CHANNELS_IDX 5
996{
997 .access = SNDRV_CTL_ELEM_ACCESS_READ,
998 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
999 .name = "PCM Slave Channels",
1000 .info = loopback_channels_info,
1001 .get = loopback_channels_get
1002}
1003};
1004
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001005static int loopback_mixer_new(struct loopback *loopback, int notify)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001006{
1007 struct snd_card *card = loopback->card;
1008 struct snd_pcm *pcm;
1009 struct snd_kcontrol *kctl;
1010 struct loopback_setup *setup;
1011 int err, dev, substr, substr_count, idx;
1012
1013 strcpy(card->mixername, "Loopback Mixer");
1014 for (dev = 0; dev < 2; dev++) {
1015 pcm = loopback->pcm[dev];
1016 substr_count =
1017 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1018 for (substr = 0; substr < substr_count; substr++) {
1019 setup = &loopback->setup[substr][dev];
1020 setup->notify = notify;
1021 setup->rate_shift = NO_PITCH;
1022 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1023 setup->rate = 48000;
1024 setup->channels = 2;
1025 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1026 idx++) {
1027 kctl = snd_ctl_new1(&loopback_controls[idx],
1028 loopback);
1029 if (!kctl)
1030 return -ENOMEM;
1031 kctl->id.device = dev;
1032 kctl->id.subdevice = substr;
1033 switch (idx) {
1034 case ACTIVE_IDX:
1035 setup->active_id = kctl->id;
1036 break;
1037 case FORMAT_IDX:
1038 setup->format_id = kctl->id;
1039 break;
1040 case RATE_IDX:
1041 setup->rate_id = kctl->id;
1042 break;
1043 case CHANNELS_IDX:
1044 setup->channels_id = kctl->id;
1045 break;
1046 default:
1047 break;
1048 }
1049 err = snd_ctl_add(card, kctl);
1050 if (err < 0)
1051 return err;
1052 }
1053 }
1054 }
1055 return 0;
1056}
1057
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001058static void print_dpcm_info(struct snd_info_buffer *buffer,
1059 struct loopback_pcm *dpcm,
1060 const char *id)
1061{
1062 snd_iprintf(buffer, " %s\n", id);
1063 if (dpcm == NULL) {
1064 snd_iprintf(buffer, " inactive\n");
1065 return;
1066 }
1067 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1068 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1069 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1070 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1071 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1072 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1073 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1074 snd_iprintf(buffer, " update_pending:\t%u\n",
1075 dpcm->period_update_pending);
1076 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1077 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1078 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1079 dpcm->last_jiffies, jiffies);
1080 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1081}
1082
1083static void print_substream_info(struct snd_info_buffer *buffer,
1084 struct loopback *loopback,
1085 int sub,
1086 int num)
1087{
1088 struct loopback_cable *cable = loopback->cables[sub][num];
1089
1090 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1091 if (cable == NULL) {
1092 snd_iprintf(buffer, " inactive\n");
1093 return;
1094 }
1095 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1096 snd_iprintf(buffer, " running: %u\n", cable->running);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +02001097 snd_iprintf(buffer, " pause: %u\n", cable->pause);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001098 print_dpcm_info(buffer, cable->streams[0], "Playback");
1099 print_dpcm_info(buffer, cable->streams[1], "Capture");
1100}
1101
1102static void print_cable_info(struct snd_info_entry *entry,
1103 struct snd_info_buffer *buffer)
1104{
1105 struct loopback *loopback = entry->private_data;
1106 int sub, num;
1107
1108 mutex_lock(&loopback->cable_lock);
1109 num = entry->name[strlen(entry->name)-1];
1110 num = num == '0' ? 0 : 1;
1111 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1112 print_substream_info(buffer, loopback, sub, num);
1113 mutex_unlock(&loopback->cable_lock);
1114}
1115
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001116static int loopback_proc_new(struct loopback *loopback, int cidx)
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001117{
1118 char name[32];
1119 struct snd_info_entry *entry;
1120 int err;
1121
1122 snprintf(name, sizeof(name), "cable#%d", cidx);
1123 err = snd_card_proc_new(loopback->card, name, &entry);
1124 if (err < 0)
1125 return err;
1126
1127 snd_info_set_text_ops(entry, loopback, print_cable_info);
1128 return 0;
1129}
1130
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001131static int loopback_probe(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001132{
1133 struct snd_card *card;
1134 struct loopback *loopback;
1135 int dev = devptr->id;
1136 int err;
1137
Takashi Iwai5872f3f2014-01-29 12:59:08 +01001138 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1139 sizeof(struct loopback), &card);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001140 if (err < 0)
1141 return err;
1142 loopback = card->private_data;
1143
1144 if (pcm_substreams[dev] < 1)
1145 pcm_substreams[dev] = 1;
1146 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1147 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1148
1149 loopback->card = card;
1150 mutex_init(&loopback->cable_lock);
1151
1152 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1153 if (err < 0)
1154 goto __nodev;
1155 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1156 if (err < 0)
1157 goto __nodev;
1158 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1159 if (err < 0)
1160 goto __nodev;
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001161 loopback_proc_new(loopback, 0);
1162 loopback_proc_new(loopback, 1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001163 strcpy(card->driver, "Loopback");
1164 strcpy(card->shortname, "Loopback");
1165 sprintf(card->longname, "Loopback %i", dev + 1);
1166 err = snd_card_register(card);
1167 if (!err) {
1168 platform_set_drvdata(devptr, card);
1169 return 0;
1170 }
1171 __nodev:
1172 snd_card_free(card);
1173 return err;
1174}
1175
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001176static int loopback_remove(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001177{
1178 snd_card_free(platform_get_drvdata(devptr));
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001179 return 0;
1180}
1181
Takashi Iwaid34e4e02012-08-09 15:47:15 +02001182#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001183static int loopback_suspend(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001184{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001185 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001186 struct loopback *loopback = card->private_data;
1187
1188 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1189
1190 snd_pcm_suspend_all(loopback->pcm[0]);
1191 snd_pcm_suspend_all(loopback->pcm[1]);
1192 return 0;
1193}
1194
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001195static int loopback_resume(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001196{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001197 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001198
1199 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1200 return 0;
1201}
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001202
1203static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1204#define LOOPBACK_PM_OPS &loopback_pm
1205#else
1206#define LOOPBACK_PM_OPS NULL
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001207#endif
1208
1209#define SND_LOOPBACK_DRIVER "snd_aloop"
1210
1211static struct platform_driver loopback_driver = {
1212 .probe = loopback_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001213 .remove = loopback_remove,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001214 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +02001215 .name = SND_LOOPBACK_DRIVER,
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001216 .pm = LOOPBACK_PM_OPS,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001217 },
1218};
1219
1220static void loopback_unregister_all(void)
1221{
1222 int i;
1223
1224 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1225 platform_device_unregister(devices[i]);
1226 platform_driver_unregister(&loopback_driver);
1227}
1228
1229static int __init alsa_card_loopback_init(void)
1230{
1231 int i, err, cards;
1232
1233 err = platform_driver_register(&loopback_driver);
1234 if (err < 0)
1235 return err;
1236
1237
1238 cards = 0;
1239 for (i = 0; i < SNDRV_CARDS; i++) {
1240 struct platform_device *device;
1241 if (!enable[i])
1242 continue;
1243 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1244 i, NULL, 0);
1245 if (IS_ERR(device))
1246 continue;
1247 if (!platform_get_drvdata(device)) {
1248 platform_device_unregister(device);
1249 continue;
1250 }
1251 devices[i] = device;
1252 cards++;
1253 }
1254 if (!cards) {
1255#ifdef MODULE
1256 printk(KERN_ERR "aloop: No loopback enabled\n");
1257#endif
1258 loopback_unregister_all();
1259 return -ENODEV;
1260 }
1261 return 0;
1262}
1263
1264static void __exit alsa_card_loopback_exit(void)
1265{
1266 loopback_unregister_all();
1267}
1268
1269module_init(alsa_card_loopback_init)
1270module_exit(alsa_card_loopback_exit)