blob: d78a27271d6d9046772e8f9322adc7110725d789 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jaroslav Kysela597603d2010-08-09 14:21:11 +02002/*
3 * Loopback soundcard
4 *
5 * Original code:
6 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 *
8 * More accurate positioning and full-duplex support:
9 * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
10 *
11 * Major (almost complete) rewrite:
12 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
13 *
14 * A next major update in 2010 (separate timers for playback and capture):
15 * Copyright (c) Jaroslav Kysela <perex@perex.cz>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020016 */
17
18#include <linux/init.h>
19#include <linux/jiffies.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/wait.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040023#include <linux/module.h>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020024#include <linux/platform_device.h>
25#include <sound/core.h>
26#include <sound/control.h>
27#include <sound/pcm.h>
Takashi Iwaib088b532018-01-05 16:15:33 +010028#include <sound/pcm_params.h>
Jaroslav Kyselae74670b2010-10-18 09:43:10 +020029#include <sound/info.h>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020030#include <sound/initval.h>
Timo Wischer26c53372019-11-20 11:49:54 -060031#include <sound/timer.h>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020032
33MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
34MODULE_DESCRIPTION("A loopback soundcard");
35MODULE_LICENSE("GPL");
36MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
37
38#define MAX_PCM_SUBSTREAMS 8
39
40static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
41static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103042static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
Jaroslav Kysela597603d2010-08-09 14:21:11 +020043static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
44static int pcm_notify[SNDRV_CARDS];
Timo Wischer26c53372019-11-20 11:49:54 -060045static char *timer_source[SNDRV_CARDS];
Jaroslav Kysela597603d2010-08-09 14:21:11 +020046
47module_param_array(index, int, NULL, 0444);
48MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
49module_param_array(id, charp, NULL, 0444);
50MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
51module_param_array(enable, bool, NULL, 0444);
52MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
53module_param_array(pcm_substreams, int, NULL, 0444);
54MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
55module_param_array(pcm_notify, int, NULL, 0444);
56MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
Timo Wischer26c53372019-11-20 11:49:54 -060057module_param_array(timer_source, charp, NULL, 0444);
58MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default].");
Jaroslav Kysela597603d2010-08-09 14:21:11 +020059
60#define NO_PITCH 100000
61
Timo Wischerfd1f7c72019-11-20 11:49:53 -060062#define CABLE_VALID_PLAYBACK BIT(SNDRV_PCM_STREAM_PLAYBACK)
63#define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE)
64#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE)
65
Timo Wischer133f3752019-11-20 11:49:51 -060066struct loopback_cable;
Jaroslav Kysela597603d2010-08-09 14:21:11 +020067struct loopback_pcm;
68
Timo Wischer133f3752019-11-20 11:49:51 -060069struct loopback_ops {
70 /* optional
71 * call in loopback->cable_lock
72 */
73 int (*open)(struct loopback_pcm *dpcm);
74 /* required
75 * call in cable->lock
76 */
77 int (*start)(struct loopback_pcm *dpcm);
78 /* required
79 * call in cable->lock
80 */
81 int (*stop)(struct loopback_pcm *dpcm);
82 /* optional */
83 int (*stop_sync)(struct loopback_pcm *dpcm);
84 /* optional */
85 int (*close_substream)(struct loopback_pcm *dpcm);
86 /* optional
87 * call in loopback->cable_lock
88 */
89 int (*close_cable)(struct loopback_pcm *dpcm);
90 /* optional
91 * call in cable->lock
92 */
93 unsigned int (*pos_update)(struct loopback_cable *cable);
94 /* optional */
95 void (*dpcm_info)(struct loopback_pcm *dpcm,
96 struct snd_info_buffer *buffer);
97};
98
Jaroslav Kysela597603d2010-08-09 14:21:11 +020099struct loopback_cable {
100 spinlock_t lock;
101 struct loopback_pcm *streams[2];
102 struct snd_pcm_hardware hw;
103 /* flags */
104 unsigned int valid;
105 unsigned int running;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200106 unsigned int pause;
Timo Wischer133f3752019-11-20 11:49:51 -0600107 /* timer specific */
108 struct loopback_ops *ops;
Timo Wischer26c53372019-11-20 11:49:54 -0600109 /* If sound timer is used */
110 struct {
111 int stream;
112 struct snd_timer_id id;
113 struct tasklet_struct event_tasklet;
114 struct snd_timer_instance *instance;
115 } snd_timer;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200116};
117
118struct loopback_setup {
119 unsigned int notify: 1;
120 unsigned int rate_shift;
121 unsigned int format;
122 unsigned int rate;
123 unsigned int channels;
124 struct snd_ctl_elem_id active_id;
125 struct snd_ctl_elem_id format_id;
126 struct snd_ctl_elem_id rate_id;
127 struct snd_ctl_elem_id channels_id;
128};
129
130struct loopback {
131 struct snd_card *card;
132 struct mutex cable_lock;
133 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
134 struct snd_pcm *pcm[2];
135 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
Timo Wischer26c53372019-11-20 11:49:54 -0600136 const char *timer_source;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200137};
138
139struct loopback_pcm {
140 struct loopback *loopback;
141 struct snd_pcm_substream *substream;
142 struct loopback_cable *cable;
143 unsigned int pcm_buffer_size;
144 unsigned int buf_pos; /* position in buffer */
145 unsigned int silent_size;
146 /* PCM parameters */
147 unsigned int pcm_period_size;
148 unsigned int pcm_bps; /* bytes per second */
149 unsigned int pcm_salign; /* bytes per sample * channels */
150 unsigned int pcm_rate_shift; /* rate shift value */
151 /* flags */
152 unsigned int period_update_pending :1;
153 /* timer stuff */
Timo Wischer97dda3d2019-11-20 11:49:49 -0600154 unsigned int irq_pos; /* fractional IRQ position in jiffies
155 * ticks
156 */
157 unsigned int period_size_frac; /* period size in jiffies ticks */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200158 unsigned int last_drift;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200159 unsigned long last_jiffies;
Timo Wischer26c53372019-11-20 11:49:54 -0600160 /* If jiffies timer is used */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200161 struct timer_list timer;
162};
163
164static struct platform_device *devices[SNDRV_CARDS];
165
166static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
167{
168 if (dpcm->pcm_rate_shift == NO_PITCH) {
169 x /= HZ;
170 } else {
171 x = div_u64(NO_PITCH * (unsigned long long)x,
172 HZ * (unsigned long long)dpcm->pcm_rate_shift);
173 }
174 return x - (x % dpcm->pcm_salign);
175}
176
177static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
178{
179 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
180 return x * HZ;
181 } else {
182 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
183 NO_PITCH);
184 }
185 return x;
186}
187
188static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
189{
190 int device = dpcm->substream->pstr->pcm->device;
191
192 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
193 device ^= 1;
194 return &dpcm->loopback->setup[dpcm->substream->number][device];
195}
196
197static inline unsigned int get_notify(struct loopback_pcm *dpcm)
198{
199 return get_setup(dpcm)->notify;
200}
201
202static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
203{
204 return get_setup(dpcm)->rate_shift;
205}
206
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200207/* call in cable->lock */
Timo Wischer8e3bf7c2019-11-20 11:49:52 -0600208static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200209{
210 unsigned long tick;
211 unsigned int rate_shift = get_rate_shift(dpcm);
212
213 if (rate_shift != dpcm->pcm_rate_shift) {
214 dpcm->pcm_rate_shift = rate_shift;
215 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
216 }
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200217 if (dpcm->period_size_frac <= dpcm->irq_pos) {
218 dpcm->irq_pos %= dpcm->period_size_frac;
219 dpcm->period_update_pending = 1;
220 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200221 tick = dpcm->period_size_frac - dpcm->irq_pos;
222 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
Takashi Iwaidb974552015-01-19 11:27:31 +0100223 mod_timer(&dpcm->timer, jiffies + tick);
Timo Wischer09419f12019-11-20 11:49:50 -0600224
225 return 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200226}
227
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200228/* call in cable->lock */
Timo Wischer26c53372019-11-20 11:49:54 -0600229static int loopback_snd_timer_start(struct loopback_pcm *dpcm)
230{
231 struct loopback_cable *cable = dpcm->cable;
232 int err;
233
234 /* Loopback device has to use same period as timer card. Therefore
235 * wake up for each snd_pcm_period_elapsed() call of timer card.
236 */
237 err = snd_timer_start(cable->snd_timer.instance, 1);
238 if (err < 0) {
239 /* do not report error if trying to start but already
240 * running. For example called by opposite substream
241 * of the same cable
242 */
243 if (err == -EBUSY)
244 return 0;
245
246 pcm_err(dpcm->substream->pcm,
247 "snd_timer_start(%d,%d,%d) failed with %d",
248 cable->snd_timer.id.card,
249 cable->snd_timer.id.device,
250 cable->snd_timer.id.subdevice,
251 err);
252 }
253
254 return err;
255}
256
257/* call in cable->lock */
Timo Wischer8e3bf7c2019-11-20 11:49:52 -0600258static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200259{
260 del_timer(&dpcm->timer);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +0200261 dpcm->timer.expires = 0;
Timo Wischer09419f12019-11-20 11:49:50 -0600262
263 return 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200264}
265
Timo Wischer26c53372019-11-20 11:49:54 -0600266/* call in cable->lock */
267static int loopback_snd_timer_stop(struct loopback_pcm *dpcm)
268{
269 struct loopback_cable *cable = dpcm->cable;
270 int err;
271
272 /* only stop if both devices (playback and capture) are not running */
273 if (cable->running ^ cable->pause)
274 return 0;
275
276 err = snd_timer_stop(cable->snd_timer.instance);
277 if (err < 0) {
278 pcm_err(dpcm->substream->pcm,
279 "snd_timer_stop(%d,%d,%d) failed with %d",
280 cable->snd_timer.id.card,
281 cable->snd_timer.id.device,
282 cable->snd_timer.id.subdevice,
283 err);
284 }
285
286 return err;
287}
288
Timo Wischer8e3bf7c2019-11-20 11:49:52 -0600289static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm)
Takashi Iwai67a01af2018-03-22 08:56:06 +0100290{
291 del_timer_sync(&dpcm->timer);
Timo Wischer09419f12019-11-20 11:49:50 -0600292
293 return 0;
Takashi Iwai67a01af2018-03-22 08:56:06 +0100294}
295
Timo Wischer26c53372019-11-20 11:49:54 -0600296/* call in loopback->cable_lock */
297static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)
298{
299 struct loopback_cable *cable = dpcm->cable;
300
301 /* snd_timer was not opened */
302 if (!cable->snd_timer.instance)
303 return 0;
304
Timo Wischer26c53372019-11-20 11:49:54 -0600305 /* will only be called from free_cable() when other stream was
306 * already closed. Other stream cannot be reopened as long as
307 * loopback->cable_lock is locked. Therefore no need to lock
308 * cable->lock;
309 */
310 snd_timer_close(cable->snd_timer.instance);
Andrew Gabbasov9314e442019-11-22 11:52:18 -0600311
312 /* wait till drain tasklet has finished if requested */
313 tasklet_kill(&cable->snd_timer.event_tasklet);
314
Timo Wischer26c53372019-11-20 11:49:54 -0600315 snd_timer_instance_free(cable->snd_timer.instance);
316 memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));
317
318 return 0;
319}
320
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200321static int loopback_check_format(struct loopback_cable *cable, int stream)
322{
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200323 struct snd_pcm_runtime *runtime, *cruntime;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200324 struct loopback_setup *setup;
325 struct snd_card *card;
326 int check;
327
328 if (cable->valid != CABLE_VALID_BOTH) {
329 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
330 goto __notify;
331 return 0;
332 }
333 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
334 substream->runtime;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200335 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
336 substream->runtime;
337 check = runtime->format != cruntime->format ||
338 runtime->rate != cruntime->rate ||
339 runtime->channels != cruntime->channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200340 if (!check)
341 return 0;
342 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
343 return -EIO;
344 } else {
345 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
346 substream, SNDRV_PCM_STATE_DRAINING);
347 __notify:
348 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
349 substream->runtime;
350 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
351 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
352 if (setup->format != runtime->format) {
353 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
354 &setup->format_id);
355 setup->format = runtime->format;
356 }
357 if (setup->rate != runtime->rate) {
358 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
359 &setup->rate_id);
360 setup->rate = runtime->rate;
361 }
362 if (setup->channels != runtime->channels) {
363 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
364 &setup->channels_id);
365 setup->channels = runtime->channels;
366 }
367 }
368 return 0;
369}
370
371static void loopback_active_notify(struct loopback_pcm *dpcm)
372{
373 snd_ctl_notify(dpcm->loopback->card,
374 SNDRV_CTL_EVENT_MASK_VALUE,
375 &get_setup(dpcm)->active_id);
376}
377
378static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
379{
380 struct snd_pcm_runtime *runtime = substream->runtime;
381 struct loopback_pcm *dpcm = runtime->private_data;
382 struct loopback_cable *cable = dpcm->cable;
Timo Wischer09419f12019-11-20 11:49:50 -0600383 int err = 0, stream = 1 << substream->stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200384
385 switch (cmd) {
386 case SNDRV_PCM_TRIGGER_START:
387 err = loopback_check_format(cable, substream->stream);
388 if (err < 0)
389 return err;
390 dpcm->last_jiffies = jiffies;
391 dpcm->pcm_rate_shift = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200392 dpcm->last_drift = 0;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200393 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200394 cable->running |= stream;
395 cable->pause &= ~stream;
Timo Wischer133f3752019-11-20 11:49:51 -0600396 err = cable->ops->start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200397 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200398 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
399 loopback_active_notify(dpcm);
400 break;
401 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200402 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200403 cable->running &= ~stream;
404 cable->pause &= ~stream;
Timo Wischer133f3752019-11-20 11:49:51 -0600405 err = cable->ops->stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200406 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
408 loopback_active_notify(dpcm);
409 break;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200410 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100411 case SNDRV_PCM_TRIGGER_SUSPEND:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200412 spin_lock(&cable->lock);
413 cable->pause |= stream;
Timo Wischer133f3752019-11-20 11:49:51 -0600414 err = cable->ops->stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200415 spin_unlock(&cable->lock);
Robert Rosengren306a4f32018-03-26 07:24:49 +0200416 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
417 loopback_active_notify(dpcm);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200418 break;
419 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaiedac8942013-02-04 10:28:15 +0100420 case SNDRV_PCM_TRIGGER_RESUME:
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200421 spin_lock(&cable->lock);
422 dpcm->last_jiffies = jiffies;
423 cable->pause &= ~stream;
Timo Wischer133f3752019-11-20 11:49:51 -0600424 err = cable->ops->start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200425 spin_unlock(&cable->lock);
Robert Rosengren306a4f32018-03-26 07:24:49 +0200426 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
427 loopback_active_notify(dpcm);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200428 break;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200429 default:
430 return -EINVAL;
431 }
Timo Wischer09419f12019-11-20 11:49:50 -0600432 return err;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200433}
434
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200435static void params_change(struct snd_pcm_substream *substream)
436{
437 struct snd_pcm_runtime *runtime = substream->runtime;
438 struct loopback_pcm *dpcm = runtime->private_data;
439 struct loopback_cable *cable = dpcm->cable;
440
Eldad Zack74c34ca2013-04-23 01:00:41 +0200441 cable->hw.formats = pcm_format_to_bits(runtime->format);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200442 cable->hw.rate_min = runtime->rate;
443 cable->hw.rate_max = runtime->rate;
444 cable->hw.channels_min = runtime->channels;
445 cable->hw.channels_max = runtime->channels;
Timo Wischer26c53372019-11-20 11:49:54 -0600446
447 if (cable->snd_timer.instance) {
448 cable->hw.period_bytes_min =
449 frames_to_bytes(runtime, runtime->period_size);
450 cable->hw.period_bytes_max = cable->hw.period_bytes_min;
451 }
452
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200453}
454
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200455static int loopback_prepare(struct snd_pcm_substream *substream)
456{
457 struct snd_pcm_runtime *runtime = substream->runtime;
458 struct loopback_pcm *dpcm = runtime->private_data;
459 struct loopback_cable *cable = dpcm->cable;
Timo Wischer09419f12019-11-20 11:49:50 -0600460 int err, bps, salign;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200461
Timo Wischer133f3752019-11-20 11:49:51 -0600462 if (cable->ops->stop_sync) {
463 err = cable->ops->stop_sync(dpcm);
464 if (err < 0)
465 return err;
466 }
Takashi Iwai67a01af2018-03-22 08:56:06 +0100467
Timo Wischer50e09082019-03-25 16:14:14 +0100468 salign = (snd_pcm_format_physical_width(runtime->format) *
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200469 runtime->channels) / 8;
470 bps = salign * runtime->rate;
471 if (bps <= 0 || salign <= 0)
472 return -EINVAL;
473
474 dpcm->buf_pos = 0;
475 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
476 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
477 /* clear capture buffer */
478 dpcm->silent_size = dpcm->pcm_buffer_size;
479 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
480 runtime->buffer_size * runtime->channels);
481 }
482
483 dpcm->irq_pos = 0;
484 dpcm->period_update_pending = 0;
485 dpcm->pcm_bps = bps;
486 dpcm->pcm_salign = salign;
487 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
488
489 mutex_lock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200490 if (!(cable->valid & ~(1 << substream->stream)) ||
491 (get_setup(dpcm)->notify &&
492 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
493 params_change(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200494 cable->valid |= 1 << substream->stream;
495 mutex_unlock(&dpcm->loopback->cable_lock);
496
497 return 0;
498}
499
500static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
501{
502 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
503 char *dst = runtime->dma_area;
504 unsigned int dst_off = dpcm->buf_pos;
505
506 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
507 return;
508 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
509 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
510
511 for (;;) {
512 unsigned int size = bytes;
513 if (dst_off + size > dpcm->pcm_buffer_size)
514 size = dpcm->pcm_buffer_size - dst_off;
515 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
516 bytes_to_frames(runtime, size) *
517 runtime->channels);
518 dpcm->silent_size += size;
519 bytes -= size;
520 if (!bytes)
521 break;
522 dst_off = 0;
523 }
524}
525
526static void copy_play_buf(struct loopback_pcm *play,
527 struct loopback_pcm *capt,
528 unsigned int bytes)
529{
530 struct snd_pcm_runtime *runtime = play->substream->runtime;
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200531 char *src = runtime->dma_area;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200532 char *dst = capt->substream->runtime->dma_area;
533 unsigned int src_off = play->buf_pos;
534 unsigned int dst_off = capt->buf_pos;
535 unsigned int clear_bytes = 0;
536
537 /* check if playback is draining, trim the capture copy size
538 * when our pointer is at the end of playback ring buffer */
539 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
540 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
541 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
542 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
543 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
544 appl_ptr1 += play->buf_pos / play->pcm_salign;
545 if (appl_ptr < appl_ptr1)
546 appl_ptr1 -= runtime->buffer_size;
547 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
548 if (diff < bytes) {
549 clear_bytes = bytes - diff;
550 bytes = diff;
551 }
552 }
553
554 for (;;) {
555 unsigned int size = bytes;
556 if (src_off + size > play->pcm_buffer_size)
557 size = play->pcm_buffer_size - src_off;
558 if (dst_off + size > capt->pcm_buffer_size)
559 size = capt->pcm_buffer_size - dst_off;
560 memcpy(dst + dst_off, src + src_off, size);
561 capt->silent_size = 0;
562 bytes -= size;
563 if (!bytes)
564 break;
565 src_off = (src_off + size) % play->pcm_buffer_size;
566 dst_off = (dst_off + size) % capt->pcm_buffer_size;
567 }
568
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200569 if (clear_bytes > 0) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200570 clear_capture_buf(capt, clear_bytes);
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200571 capt->silent_size = 0;
572 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200573}
574
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200575static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
576 unsigned int jiffies_delta)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200577{
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200578 unsigned long last_pos;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200579 unsigned int delta;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200580
581 last_pos = byte_pos(dpcm, dpcm->irq_pos);
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200582 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
583 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
584 if (delta >= dpcm->last_drift)
585 delta -= dpcm->last_drift;
586 dpcm->last_drift = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200587 if (dpcm->irq_pos >= dpcm->period_size_frac) {
588 dpcm->irq_pos %= dpcm->period_size_frac;
589 dpcm->period_update_pending = 1;
590 }
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200591 return delta;
592}
593
594static inline void bytepos_finish(struct loopback_pcm *dpcm,
595 unsigned int delta)
596{
597 dpcm->buf_pos += delta;
598 dpcm->buf_pos %= dpcm->pcm_buffer_size;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200599}
600
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200601/* call in cable->lock */
Timo Wischer8e3bf7c2019-11-20 11:49:52 -0600602static unsigned int loopback_jiffies_timer_pos_update
603 (struct loopback_cable *cable)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200604{
605 struct loopback_pcm *dpcm_play =
606 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
607 struct loopback_pcm *dpcm_capt =
608 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
609 unsigned long delta_play = 0, delta_capt = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200610 unsigned int running, count1, count2;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200611
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200612 running = cable->running ^ cable->pause;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200613 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200614 delta_play = jiffies - dpcm_play->last_jiffies;
615 dpcm_play->last_jiffies += delta_play;
616 }
617
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200618 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200619 delta_capt = jiffies - dpcm_capt->last_jiffies;
620 dpcm_capt->last_jiffies += delta_capt;
621 }
622
Takashi Iwai98d21df2011-03-18 07:31:53 +0100623 if (delta_play == 0 && delta_capt == 0)
624 goto unlock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200625
626 if (delta_play > delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200627 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
628 bytepos_finish(dpcm_play, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200629 delta_play = delta_capt;
630 } else if (delta_play < delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200631 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
632 clear_capture_buf(dpcm_capt, count1);
633 bytepos_finish(dpcm_capt, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200634 delta_capt = delta_play;
635 }
636
Takashi Iwai98d21df2011-03-18 07:31:53 +0100637 if (delta_play == 0 && delta_capt == 0)
638 goto unlock;
639
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200640 /* note delta_capt == delta_play at this moment */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200641 count1 = bytepos_delta(dpcm_play, delta_play);
642 count2 = bytepos_delta(dpcm_capt, delta_capt);
643 if (count1 < count2) {
644 dpcm_capt->last_drift = count2 - count1;
645 count1 = count2;
646 } else if (count1 > count2) {
647 dpcm_play->last_drift = count1 - count2;
648 }
649 copy_play_buf(dpcm_play, dpcm_capt, count1);
650 bytepos_finish(dpcm_play, count1);
651 bytepos_finish(dpcm_capt, count1);
Takashi Iwai98d21df2011-03-18 07:31:53 +0100652 unlock:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200653 return running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200654}
655
Timo Wischer8e3bf7c2019-11-20 11:49:52 -0600656static void loopback_jiffies_timer_function(struct timer_list *t)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200657{
Kees Cookbc47ba92017-10-24 08:34:29 -0700658 struct loopback_pcm *dpcm = from_timer(dpcm, t, timer);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200659 unsigned long flags;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200660
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200661 spin_lock_irqsave(&dpcm->cable->lock, flags);
Timo Wischer8e3bf7c2019-11-20 11:49:52 -0600662 if (loopback_jiffies_timer_pos_update(dpcm->cable) &
663 (1 << dpcm->substream->stream)) {
664 loopback_jiffies_timer_start(dpcm);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200665 if (dpcm->period_update_pending) {
666 dpcm->period_update_pending = 0;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200667 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
668 /* need to unlock before calling below */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200669 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200670 return;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200671 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200672 }
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200673 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200674}
675
Timo Wischer26c53372019-11-20 11:49:54 -0600676/* call in cable->lock */
677static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime,
678 unsigned long resolution)
679{
680 if (resolution != runtime->timer_resolution) {
681 struct loopback_pcm *dpcm = runtime->private_data;
682 struct loopback_cable *cable = dpcm->cable;
683 /* Worst case estimation of possible values for resolution
684 * resolution <= (512 * 1024) frames / 8kHz in nsec
685 * resolution <= 65.536.000.000 nsec
686 *
687 * period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz +
688 * 500.000
689 * period_size <= 12.582.912.000.000 <64bit
690 * / 1.000.000 usec/sec
691 */
692 snd_pcm_uframes_t period_size_usec =
693 resolution / 1000 * runtime->rate;
694 /* round to nearest sample rate */
695 snd_pcm_uframes_t period_size =
696 (period_size_usec + 500 * 1000) / (1000 * 1000);
697
698 pcm_err(dpcm->substream->pcm,
699 "Period size (%lu frames) of loopback device is not corresponding to timer resolution (%lu nsec = %lu frames) of card timer %d,%d,%d. Use period size of %lu frames for loopback device.",
700 runtime->period_size, resolution, period_size,
701 cable->snd_timer.id.card,
702 cable->snd_timer.id.device,
703 cable->snd_timer.id.subdevice,
704 period_size);
705 return -EINVAL;
706 }
707 return 0;
708}
709
710static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
711 int event,
712 unsigned long resolution)
713{
714 struct loopback_pcm *dpcm_play, *dpcm_capt;
715 struct snd_pcm_substream *substream_play, *substream_capt;
716 struct snd_pcm_runtime *valid_runtime;
717 unsigned int running, elapsed_bytes;
718 unsigned long flags;
719
720 spin_lock_irqsave(&cable->lock, flags);
721 running = cable->running ^ cable->pause;
722 /* no need to do anything if no stream is running */
723 if (!running) {
724 spin_unlock_irqrestore(&cable->lock, flags);
725 return;
726 }
727
728 dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
729 dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
Timo Wischer26c53372019-11-20 11:49:54 -0600730
731 if (event == SNDRV_TIMER_EVENT_MSTOP) {
732 if (!dpcm_play ||
733 dpcm_play->substream->runtime->status->state !=
734 SNDRV_PCM_STATE_DRAINING) {
735 spin_unlock_irqrestore(&cable->lock, flags);
736 return;
737 }
738 }
739
Andrew Gabbasov5061bb72019-11-27 05:06:22 -0600740 substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
741 dpcm_play->substream : NULL;
742 substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
743 dpcm_capt->substream : NULL;
Timo Wischer26c53372019-11-20 11:49:54 -0600744 valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
745 dpcm_play->substream->runtime :
746 dpcm_capt->substream->runtime;
747
748 /* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
749 if (event == SNDRV_TIMER_EVENT_TICK) {
750 /* The hardware rules guarantee that playback and capture period
751 * are the same. Therefore only one device has to be checked
752 * here.
753 */
754 if (loopback_snd_timer_check_resolution(valid_runtime,
755 resolution) < 0) {
756 spin_unlock_irqrestore(&cable->lock, flags);
757 if (substream_play)
758 snd_pcm_stop_xrun(substream_play);
759 if (substream_capt)
760 snd_pcm_stop_xrun(substream_capt);
761 return;
762 }
763 }
764
765 elapsed_bytes = frames_to_bytes(valid_runtime,
766 valid_runtime->period_size);
767 /* The same timer interrupt is used for playback and capture device */
768 if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
769 (running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
770 copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
771 bytepos_finish(dpcm_play, elapsed_bytes);
772 bytepos_finish(dpcm_capt, elapsed_bytes);
773 } else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
774 bytepos_finish(dpcm_play, elapsed_bytes);
775 } else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
776 clear_capture_buf(dpcm_capt, elapsed_bytes);
777 bytepos_finish(dpcm_capt, elapsed_bytes);
778 }
779 spin_unlock_irqrestore(&cable->lock, flags);
780
781 if (substream_play)
782 snd_pcm_period_elapsed(substream_play);
783 if (substream_capt)
784 snd_pcm_period_elapsed(substream_capt);
785}
786
787static void loopback_snd_timer_function(struct snd_timer_instance *timeri,
788 unsigned long resolution,
789 unsigned long ticks)
790{
791 struct loopback_cable *cable = timeri->callback_data;
792
793 loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK,
794 resolution);
795}
796
797static void loopback_snd_timer_tasklet(unsigned long arg)
798{
799 struct snd_timer_instance *timeri = (struct snd_timer_instance *)arg;
800 struct loopback_cable *cable = timeri->callback_data;
801
802 loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0);
803}
804
805static void loopback_snd_timer_event(struct snd_timer_instance *timeri,
806 int event,
Baolin Wangfcae40c2018-04-24 20:06:08 +0800807 struct timespec64 *tstamp,
Timo Wischer26c53372019-11-20 11:49:54 -0600808 unsigned long resolution)
809{
810 /* Do not lock cable->lock here because timer->lock is already hold.
811 * There are other functions which first lock cable->lock and than
812 * timer->lock e.g.
813 * loopback_trigger()
814 * spin_lock(&cable->lock)
815 * loopback_snd_timer_start()
816 * snd_timer_start()
817 * spin_lock(&timer->lock)
818 * Therefore when using the oposit order of locks here it could result
819 * in a deadlock.
820 */
821
822 if (event == SNDRV_TIMER_EVENT_MSTOP) {
823 struct loopback_cable *cable = timeri->callback_data;
824
825 /* sound card of the timer was stopped. Therefore there will not
826 * be any further timer callbacks. Due to this forward audio
827 * data from here if in draining state. When still in running
828 * state the streaming will be aborted by the usual timeout. It
829 * should not be aborted here because may be the timer sound
830 * card does only a recovery and the timer is back soon.
831 * This tasklet triggers loopback_snd_timer_tasklet()
832 */
833 tasklet_schedule(&cable->snd_timer.event_tasklet);
834 }
835}
836
Timo Wischer133f3752019-11-20 11:49:51 -0600837static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm,
838 struct snd_info_buffer *buffer)
839{
840 snd_iprintf(buffer, " update_pending:\t%u\n",
841 dpcm->period_update_pending);
842 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
843 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
844 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
845 dpcm->last_jiffies, jiffies);
846 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
847}
848
Timo Wischer26c53372019-11-20 11:49:54 -0600849static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm,
850 struct snd_info_buffer *buffer)
851{
852 struct loopback_cable *cable = dpcm->cable;
853
854 snd_iprintf(buffer, " sound timer:\thw:%d,%d,%d\n",
855 cable->snd_timer.id.card,
856 cable->snd_timer.id.device,
857 cable->snd_timer.id.subdevice);
858 snd_iprintf(buffer, " timer open:\t\t%s\n",
859 (cable->snd_timer.stream == SNDRV_PCM_STREAM_CAPTURE) ?
860 "capture" : "playback");
861}
862
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200863static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
864{
865 struct snd_pcm_runtime *runtime = substream->runtime;
866 struct loopback_pcm *dpcm = runtime->private_data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200867 snd_pcm_uframes_t pos;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200868
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200869 spin_lock(&dpcm->cable->lock);
Timo Wischer133f3752019-11-20 11:49:51 -0600870 if (dpcm->cable->ops->pos_update)
871 dpcm->cable->ops->pos_update(dpcm->cable);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200872 pos = dpcm->buf_pos;
873 spin_unlock(&dpcm->cable->lock);
874 return bytes_to_frames(runtime, pos);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200875}
876
Bhumika Goyalb6c0b712017-08-17 14:45:51 +0530877static const struct snd_pcm_hardware loopback_pcm_hardware =
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200878{
879 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
Takashi Iwaiedac8942013-02-04 10:28:15 +0100880 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
881 SNDRV_PCM_INFO_RESUME),
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200882 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
Timo Wischer50e09082019-03-25 16:14:14 +0100883 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
884 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200885 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
886 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
887 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
888 .rate_min = 8000,
889 .rate_max = 192000,
890 .channels_min = 1,
891 .channels_max = 32,
892 .buffer_bytes_max = 2 * 1024 * 1024,
893 .period_bytes_min = 64,
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200894 /* note check overflow in frac_pos() using pcm_rate_shift before
895 changing period_bytes_max value */
896 .period_bytes_max = 1024 * 1024,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200897 .periods_min = 1,
898 .periods_max = 1024,
899 .fifo_size = 0,
900};
901
902static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
903{
904 struct loopback_pcm *dpcm = runtime->private_data;
905 kfree(dpcm);
906}
907
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200908static int loopback_hw_free(struct snd_pcm_substream *substream)
909{
910 struct snd_pcm_runtime *runtime = substream->runtime;
911 struct loopback_pcm *dpcm = runtime->private_data;
912 struct loopback_cable *cable = dpcm->cable;
913
914 mutex_lock(&dpcm->loopback->cable_lock);
915 cable->valid &= ~(1 << substream->stream);
916 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai39729882019-12-09 10:48:36 +0100917 return 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200918}
919
920static unsigned int get_cable_index(struct snd_pcm_substream *substream)
921{
922 if (!substream->pcm->device)
923 return substream->stream;
924 else
925 return !substream->stream;
926}
927
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200928static int rule_format(struct snd_pcm_hw_params *params,
929 struct snd_pcm_hw_rule *rule)
930{
Takashi Iwai898dfe42018-01-04 17:38:54 +0100931 struct loopback_pcm *dpcm = rule->private;
932 struct loopback_cable *cable = dpcm->cable;
Takashi Iwaib088b532018-01-05 16:15:33 +0100933 struct snd_mask m;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200934
Takashi Iwaib088b532018-01-05 16:15:33 +0100935 snd_mask_none(&m);
Takashi Iwai898dfe42018-01-04 17:38:54 +0100936 mutex_lock(&dpcm->loopback->cable_lock);
937 m.bits[0] = (u_int32_t)cable->hw.formats;
938 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
939 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwaib088b532018-01-05 16:15:33 +0100940 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200941}
942
943static int rule_rate(struct snd_pcm_hw_params *params,
944 struct snd_pcm_hw_rule *rule)
945{
Takashi Iwai898dfe42018-01-04 17:38:54 +0100946 struct loopback_pcm *dpcm = rule->private;
947 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200948 struct snd_interval t;
949
Takashi Iwai898dfe42018-01-04 17:38:54 +0100950 mutex_lock(&dpcm->loopback->cable_lock);
951 t.min = cable->hw.rate_min;
952 t.max = cable->hw.rate_max;
953 mutex_unlock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200954 t.openmin = t.openmax = 0;
955 t.integer = 0;
956 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
957}
958
959static int rule_channels(struct snd_pcm_hw_params *params,
960 struct snd_pcm_hw_rule *rule)
961{
Takashi Iwai898dfe42018-01-04 17:38:54 +0100962 struct loopback_pcm *dpcm = rule->private;
963 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200964 struct snd_interval t;
965
Takashi Iwai898dfe42018-01-04 17:38:54 +0100966 mutex_lock(&dpcm->loopback->cable_lock);
967 t.min = cable->hw.channels_min;
968 t.max = cable->hw.channels_max;
969 mutex_unlock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200970 t.openmin = t.openmax = 0;
971 t.integer = 0;
972 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
973}
974
Timo Wischer26c53372019-11-20 11:49:54 -0600975static int rule_period_bytes(struct snd_pcm_hw_params *params,
976 struct snd_pcm_hw_rule *rule)
977{
978 struct loopback_pcm *dpcm = rule->private;
979 struct loopback_cable *cable = dpcm->cable;
980 struct snd_interval t;
981
982 mutex_lock(&dpcm->loopback->cable_lock);
983 t.min = cable->hw.period_bytes_min;
984 t.max = cable->hw.period_bytes_max;
985 mutex_unlock(&dpcm->loopback->cable_lock);
986 t.openmin = 0;
987 t.openmax = 0;
988 t.integer = 0;
989 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
990}
991
Takashi Iwai96853472018-01-05 16:09:47 +0100992static void free_cable(struct snd_pcm_substream *substream)
993{
994 struct loopback *loopback = substream->private_data;
995 int dev = get_cable_index(substream);
996 struct loopback_cable *cable;
997
998 cable = loopback->cables[substream->number][dev];
999 if (!cable)
1000 return;
1001 if (cable->streams[!substream->stream]) {
1002 /* other stream is still alive */
Takashi Iwai8e6b1a72018-03-22 10:40:27 +01001003 spin_lock_irq(&cable->lock);
Takashi Iwai96853472018-01-05 16:09:47 +01001004 cable->streams[substream->stream] = NULL;
Takashi Iwai8e6b1a72018-03-22 10:40:27 +01001005 spin_unlock_irq(&cable->lock);
Takashi Iwai96853472018-01-05 16:09:47 +01001006 } else {
Timo Wischer133f3752019-11-20 11:49:51 -06001007 struct loopback_pcm *dpcm = substream->runtime->private_data;
1008
1009 if (cable->ops && cable->ops->close_cable && dpcm)
1010 cable->ops->close_cable(dpcm);
Takashi Iwai96853472018-01-05 16:09:47 +01001011 /* free the cable */
1012 loopback->cables[substream->number][dev] = NULL;
1013 kfree(cable);
1014 }
1015}
1016
Timo Wischer133f3752019-11-20 11:49:51 -06001017static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm)
1018{
Timo Wischer8e3bf7c2019-11-20 11:49:52 -06001019 timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0);
Timo Wischer133f3752019-11-20 11:49:51 -06001020
1021 return 0;
1022}
1023
1024static struct loopback_ops loopback_jiffies_timer_ops = {
1025 .open = loopback_jiffies_timer_open,
Timo Wischer8e3bf7c2019-11-20 11:49:52 -06001026 .start = loopback_jiffies_timer_start,
1027 .stop = loopback_jiffies_timer_stop,
1028 .stop_sync = loopback_jiffies_timer_stop_sync,
1029 .close_substream = loopback_jiffies_timer_stop_sync,
1030 .pos_update = loopback_jiffies_timer_pos_update,
Timo Wischer133f3752019-11-20 11:49:51 -06001031 .dpcm_info = loopback_jiffies_timer_dpcm_info,
1032};
1033
Timo Wischer26c53372019-11-20 11:49:54 -06001034static int loopback_parse_timer_id(const char *str,
1035 struct snd_timer_id *tid)
1036{
1037 /* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */
1038 const char * const sep_dev = ".,";
1039 const char * const sep_pref = ":";
1040 const char *name = str;
1041 char *sep, save = '\0';
1042 int card_idx = 0, dev = 0, subdev = 0;
1043 int err;
1044
1045 sep = strpbrk(str, sep_pref);
1046 if (sep)
1047 name = sep + 1;
1048 sep = strpbrk(name, sep_dev);
1049 if (sep) {
1050 save = *sep;
1051 *sep = '\0';
1052 }
1053 err = kstrtoint(name, 0, &card_idx);
1054 if (err == -EINVAL) {
1055 /* Must be the name, not number */
1056 for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) {
1057 struct snd_card *card = snd_card_ref(card_idx);
1058
1059 if (card) {
1060 if (!strcmp(card->id, name))
1061 err = 0;
1062 snd_card_unref(card);
1063 }
1064 if (!err)
1065 break;
1066 }
1067 }
1068 if (sep) {
1069 *sep = save;
1070 if (!err) {
1071 char *sep2, save2 = '\0';
1072
1073 sep2 = strpbrk(sep + 1, sep_dev);
1074 if (sep2) {
1075 save2 = *sep2;
1076 *sep2 = '\0';
1077 }
1078 err = kstrtoint(sep + 1, 0, &dev);
1079 if (sep2) {
1080 *sep2 = save2;
1081 if (!err)
1082 err = kstrtoint(sep2 + 1, 0, &subdev);
1083 }
1084 }
1085 }
1086 if (!err && tid) {
1087 tid->card = card_idx;
1088 tid->device = dev;
1089 tid->subdevice = subdev;
1090 }
1091 return err;
1092}
1093
1094/* call in loopback->cable_lock */
1095static int loopback_snd_timer_open(struct loopback_pcm *dpcm)
1096{
1097 int err = 0;
1098 struct snd_timer_id tid = {
1099 .dev_class = SNDRV_TIMER_CLASS_PCM,
1100 .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION,
1101 };
1102 struct snd_timer_instance *timeri;
1103 struct loopback_cable *cable = dpcm->cable;
1104
Timo Wischer26c53372019-11-20 11:49:54 -06001105 /* check if timer was already opened. It is only opened once
1106 * per playback and capture subdevice (aka cable).
1107 */
1108 if (cable->snd_timer.instance)
Andrew Gabbasovc0372392019-11-22 11:52:17 -06001109 goto exit;
Timo Wischer26c53372019-11-20 11:49:54 -06001110
1111 err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid);
1112 if (err < 0) {
1113 pcm_err(dpcm->substream->pcm,
1114 "Parsing timer source \'%s\' failed with %d",
1115 dpcm->loopback->timer_source, err);
Andrew Gabbasovc0372392019-11-22 11:52:17 -06001116 goto exit;
Timo Wischer26c53372019-11-20 11:49:54 -06001117 }
1118
1119 cable->snd_timer.stream = dpcm->substream->stream;
1120 cable->snd_timer.id = tid;
1121
1122 timeri = snd_timer_instance_new(dpcm->loopback->card->id);
1123 if (!timeri) {
1124 err = -ENOMEM;
Andrew Gabbasovc0372392019-11-22 11:52:17 -06001125 goto exit;
Timo Wischer26c53372019-11-20 11:49:54 -06001126 }
1127 /* The callback has to be called from another tasklet. If
1128 * SNDRV_TIMER_IFLG_FAST is specified it will be called from the
1129 * snd_pcm_period_elapsed() call of the selected sound card.
1130 * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave().
1131 * Due to our callback loopback_snd_timer_function() also calls
1132 * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave().
1133 * This would end up in a dead lock.
1134 */
1135 timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1136 timeri->callback = loopback_snd_timer_function;
1137 timeri->callback_data = (void *)cable;
1138 timeri->ccallback = loopback_snd_timer_event;
1139
1140 /* initialise a tasklet used for draining */
1141 tasklet_init(&cable->snd_timer.event_tasklet,
1142 loopback_snd_timer_tasklet, (unsigned long)timeri);
1143
Andrew Gabbasovc0372392019-11-22 11:52:17 -06001144 /* The mutex loopback->cable_lock is kept locked.
1145 * Therefore snd_timer_open() cannot be called a second time
1146 * by the other device of the same cable.
Timo Wischer26c53372019-11-20 11:49:54 -06001147 * Therefore the following issue cannot happen:
1148 * [proc1] Call loopback_timer_open() ->
1149 * Unlock cable->lock for snd_timer_close/open() call
1150 * [proc2] Call loopback_timer_open() -> snd_timer_open(),
1151 * snd_timer_start()
1152 * [proc1] Call snd_timer_open() and overwrite running timer
1153 * instance
1154 */
Timo Wischer26c53372019-11-20 11:49:54 -06001155 err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid);
Timo Wischer26c53372019-11-20 11:49:54 -06001156 if (err < 0) {
1157 pcm_err(dpcm->substream->pcm,
1158 "snd_timer_open (%d,%d,%d) failed with %d",
1159 cable->snd_timer.id.card,
1160 cable->snd_timer.id.device,
1161 cable->snd_timer.id.subdevice,
1162 err);
1163 snd_timer_instance_free(timeri);
Andrew Gabbasovc0372392019-11-22 11:52:17 -06001164 goto exit;
Timo Wischer26c53372019-11-20 11:49:54 -06001165 }
1166
1167 cable->snd_timer.instance = timeri;
1168
Andrew Gabbasovc0372392019-11-22 11:52:17 -06001169exit:
Timo Wischer26c53372019-11-20 11:49:54 -06001170 return err;
1171}
1172
1173/* stop_sync() is not required for sound timer because it does not need to be
1174 * restarted in loopback_prepare() on Xrun recovery
1175 */
1176static struct loopback_ops loopback_snd_timer_ops = {
1177 .open = loopback_snd_timer_open,
1178 .start = loopback_snd_timer_start,
1179 .stop = loopback_snd_timer_stop,
1180 .close_cable = loopback_snd_timer_close_cable,
1181 .dpcm_info = loopback_snd_timer_dpcm_info,
1182};
1183
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001184static int loopback_open(struct snd_pcm_substream *substream)
1185{
1186 struct snd_pcm_runtime *runtime = substream->runtime;
1187 struct loopback *loopback = substream->private_data;
1188 struct loopback_pcm *dpcm;
Takashi Iwai96853472018-01-05 16:09:47 +01001189 struct loopback_cable *cable = NULL;
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001190 int err = 0;
1191 int dev = get_cable_index(substream);
1192
1193 mutex_lock(&loopback->cable_lock);
1194 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1195 if (!dpcm) {
1196 err = -ENOMEM;
1197 goto unlock;
1198 }
1199 dpcm->loopback = loopback;
1200 dpcm->substream = substream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001201
1202 cable = loopback->cables[substream->number][dev];
1203 if (!cable) {
1204 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1205 if (!cable) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001206 err = -ENOMEM;
1207 goto unlock;
1208 }
1209 spin_lock_init(&cable->lock);
1210 cable->hw = loopback_pcm_hardware;
Timo Wischer26c53372019-11-20 11:49:54 -06001211 if (loopback->timer_source)
1212 cable->ops = &loopback_snd_timer_ops;
1213 else
1214 cable->ops = &loopback_jiffies_timer_ops;
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001215 loopback->cables[substream->number][dev] = cable;
1216 }
1217 dpcm->cable = cable;
Timo Wischer133f3752019-11-20 11:49:51 -06001218 runtime->private_data = dpcm;
1219
1220 if (cable->ops->open) {
1221 err = cable->ops->open(dpcm);
1222 if (err < 0)
1223 goto unlock;
1224 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001225
1226 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1227
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +02001228 /* use dynamic rules based on actual runtime->hw values */
1229 /* note that the default rules created in the PCM midlevel code */
1230 /* are cached -> they do not reflect the actual state */
1231 err = snd_pcm_hw_rule_add(runtime, 0,
1232 SNDRV_PCM_HW_PARAM_FORMAT,
Takashi Iwai898dfe42018-01-04 17:38:54 +01001233 rule_format, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +02001234 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1235 if (err < 0)
1236 goto unlock;
1237 err = snd_pcm_hw_rule_add(runtime, 0,
1238 SNDRV_PCM_HW_PARAM_RATE,
Takashi Iwai898dfe42018-01-04 17:38:54 +01001239 rule_rate, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +02001240 SNDRV_PCM_HW_PARAM_RATE, -1);
1241 if (err < 0)
1242 goto unlock;
1243 err = snd_pcm_hw_rule_add(runtime, 0,
1244 SNDRV_PCM_HW_PARAM_CHANNELS,
Takashi Iwai898dfe42018-01-04 17:38:54 +01001245 rule_channels, dpcm,
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +02001246 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1247 if (err < 0)
1248 goto unlock;
1249
Timo Wischer26c53372019-11-20 11:49:54 -06001250 /* In case of sound timer the period time of both devices of the same
1251 * loop has to be the same.
1252 * This rule only takes effect if a sound timer was chosen
1253 */
1254 if (cable->snd_timer.instance) {
1255 err = snd_pcm_hw_rule_add(runtime, 0,
1256 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1257 rule_period_bytes, dpcm,
1258 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1);
1259 if (err < 0)
1260 goto unlock;
1261 }
1262
Timo Wischer133f3752019-11-20 11:49:51 -06001263 /* loopback_runtime_free() has not to be called if kfree(dpcm) was
1264 * already called here. Otherwise it will end up with a double free.
1265 */
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001266 runtime->private_free = loopback_runtime_free;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +02001267 if (get_notify(dpcm))
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001268 runtime->hw = loopback_pcm_hardware;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +02001269 else
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001270 runtime->hw = cable->hw;
Takashi Iwai8e6b1a72018-03-22 10:40:27 +01001271
1272 spin_lock_irq(&cable->lock);
1273 cable->streams[substream->stream] = dpcm;
1274 spin_unlock_irq(&cable->lock);
1275
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001276 unlock:
Takashi Iwai96853472018-01-05 16:09:47 +01001277 if (err < 0) {
1278 free_cable(substream);
1279 kfree(dpcm);
1280 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001281 mutex_unlock(&loopback->cable_lock);
1282 return err;
1283}
1284
1285static int loopback_close(struct snd_pcm_substream *substream)
1286{
1287 struct loopback *loopback = substream->private_data;
1288 struct loopback_pcm *dpcm = substream->runtime->private_data;
Timo Wischer133f3752019-11-20 11:49:51 -06001289 int err = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001290
Timo Wischer133f3752019-11-20 11:49:51 -06001291 if (dpcm->cable->ops->close_substream)
1292 err = dpcm->cable->ops->close_substream(dpcm);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001293 mutex_lock(&loopback->cable_lock);
Takashi Iwai96853472018-01-05 16:09:47 +01001294 free_cable(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001295 mutex_unlock(&loopback->cable_lock);
Timo Wischer133f3752019-11-20 11:49:51 -06001296 return err;
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001297}
1298
Takashi Iwai9f88058e2018-05-27 13:55:01 +02001299static const struct snd_pcm_ops loopback_pcm_ops = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001300 .open = loopback_open,
1301 .close = loopback_close,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001302 .hw_free = loopback_hw_free,
1303 .prepare = loopback_prepare,
1304 .trigger = loopback_trigger,
1305 .pointer = loopback_pointer,
1306};
1307
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001308static int loopback_pcm_new(struct loopback *loopback,
1309 int device, int substreams)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001310{
1311 struct snd_pcm *pcm;
1312 int err;
1313
1314 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
1315 substreams, substreams, &pcm);
1316 if (err < 0)
1317 return err;
Takashi Iwai9f88058e2018-05-27 13:55:01 +02001318 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops);
1319 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops);
Takashi Iwai39729882019-12-09 10:48:36 +01001320 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001321
1322 pcm->private_data = loopback;
1323 pcm->info_flags = 0;
1324 strcpy(pcm->name, "Loopback PCM");
1325
1326 loopback->pcm[device] = pcm;
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001327 return 0;
1328}
1329
1330static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
1331 struct snd_ctl_elem_info *uinfo)
1332{
1333 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1334 uinfo->count = 1;
1335 uinfo->value.integer.min = 80000;
1336 uinfo->value.integer.max = 120000;
1337 uinfo->value.integer.step = 1;
1338 return 0;
1339}
1340
1341static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
1342 struct snd_ctl_elem_value *ucontrol)
1343{
1344 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1345
Takashi Iwai76b34212018-04-30 10:06:48 +02001346 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001347 ucontrol->value.integer.value[0] =
1348 loopback->setup[kcontrol->id.subdevice]
1349 [kcontrol->id.device].rate_shift;
Takashi Iwai76b34212018-04-30 10:06:48 +02001350 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001351 return 0;
1352}
1353
1354static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
1355 struct snd_ctl_elem_value *ucontrol)
1356{
1357 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1358 unsigned int val;
1359 int change = 0;
1360
1361 val = ucontrol->value.integer.value[0];
1362 if (val < 80000)
1363 val = 80000;
1364 if (val > 120000)
1365 val = 120000;
1366 mutex_lock(&loopback->cable_lock);
1367 if (val != loopback->setup[kcontrol->id.subdevice]
1368 [kcontrol->id.device].rate_shift) {
1369 loopback->setup[kcontrol->id.subdevice]
1370 [kcontrol->id.device].rate_shift = val;
1371 change = 1;
1372 }
1373 mutex_unlock(&loopback->cable_lock);
1374 return change;
1375}
1376
1377static int loopback_notify_get(struct snd_kcontrol *kcontrol,
1378 struct snd_ctl_elem_value *ucontrol)
1379{
1380 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1381
Takashi Iwai76b34212018-04-30 10:06:48 +02001382 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001383 ucontrol->value.integer.value[0] =
1384 loopback->setup[kcontrol->id.subdevice]
1385 [kcontrol->id.device].notify;
Takashi Iwai76b34212018-04-30 10:06:48 +02001386 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001387 return 0;
1388}
1389
1390static int loopback_notify_put(struct snd_kcontrol *kcontrol,
1391 struct snd_ctl_elem_value *ucontrol)
1392{
1393 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1394 unsigned int val;
1395 int change = 0;
1396
1397 val = ucontrol->value.integer.value[0] ? 1 : 0;
Takashi Iwai76b34212018-04-30 10:06:48 +02001398 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001399 if (val != loopback->setup[kcontrol->id.subdevice]
1400 [kcontrol->id.device].notify) {
1401 loopback->setup[kcontrol->id.subdevice]
1402 [kcontrol->id.device].notify = val;
1403 change = 1;
1404 }
Takashi Iwai76b34212018-04-30 10:06:48 +02001405 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001406 return change;
1407}
1408
1409static int loopback_active_get(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_value *ucontrol)
1411{
1412 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
Takashi Iwai76b34212018-04-30 10:06:48 +02001413 struct loopback_cable *cable;
1414
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001415 unsigned int val = 0;
1416
Takashi Iwai76b34212018-04-30 10:06:48 +02001417 mutex_lock(&loopback->cable_lock);
1418 cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
Robert Rosengren306a4f32018-03-26 07:24:49 +02001419 if (cable != NULL) {
1420 unsigned int running = cable->running ^ cable->pause;
1421
1422 val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
1423 }
Takashi Iwai76b34212018-04-30 10:06:48 +02001424 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001425 ucontrol->value.integer.value[0] = val;
1426 return 0;
1427}
1428
1429static int loopback_format_info(struct snd_kcontrol *kcontrol,
1430 struct snd_ctl_elem_info *uinfo)
1431{
1432 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1433 uinfo->count = 1;
1434 uinfo->value.integer.min = 0;
1435 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
1436 uinfo->value.integer.step = 1;
1437 return 0;
1438}
1439
1440static int loopback_format_get(struct snd_kcontrol *kcontrol,
1441 struct snd_ctl_elem_value *ucontrol)
1442{
1443 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1444
1445 ucontrol->value.integer.value[0] =
1446 loopback->setup[kcontrol->id.subdevice]
1447 [kcontrol->id.device].format;
1448 return 0;
1449}
1450
1451static int loopback_rate_info(struct snd_kcontrol *kcontrol,
1452 struct snd_ctl_elem_info *uinfo)
1453{
1454 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1455 uinfo->count = 1;
1456 uinfo->value.integer.min = 0;
1457 uinfo->value.integer.max = 192000;
1458 uinfo->value.integer.step = 1;
1459 return 0;
1460}
1461
1462static int loopback_rate_get(struct snd_kcontrol *kcontrol,
1463 struct snd_ctl_elem_value *ucontrol)
1464{
1465 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1466
Takashi Iwai76b34212018-04-30 10:06:48 +02001467 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001468 ucontrol->value.integer.value[0] =
1469 loopback->setup[kcontrol->id.subdevice]
1470 [kcontrol->id.device].rate;
Takashi Iwai76b34212018-04-30 10:06:48 +02001471 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001472 return 0;
1473}
1474
1475static int loopback_channels_info(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_info *uinfo)
1477{
1478 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1479 uinfo->count = 1;
1480 uinfo->value.integer.min = 1;
1481 uinfo->value.integer.max = 1024;
1482 uinfo->value.integer.step = 1;
1483 return 0;
1484}
1485
1486static int loopback_channels_get(struct snd_kcontrol *kcontrol,
1487 struct snd_ctl_elem_value *ucontrol)
1488{
1489 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1490
Takashi Iwai76b34212018-04-30 10:06:48 +02001491 mutex_lock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001492 ucontrol->value.integer.value[0] =
1493 loopback->setup[kcontrol->id.subdevice]
Jaroslav Kysela1446c5f2010-09-15 08:01:57 +02001494 [kcontrol->id.device].channels;
Takashi Iwai76b34212018-04-30 10:06:48 +02001495 mutex_unlock(&loopback->cable_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001496 return 0;
1497}
1498
Takashi Iwai2eccd402020-01-03 09:16:49 +01001499static const struct snd_kcontrol_new loopback_controls[] = {
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001500{
1501 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1502 .name = "PCM Rate Shift 100000",
1503 .info = loopback_rate_shift_info,
1504 .get = loopback_rate_shift_get,
1505 .put = loopback_rate_shift_put,
1506},
1507{
1508 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1509 .name = "PCM Notify",
1510 .info = snd_ctl_boolean_mono_info,
1511 .get = loopback_notify_get,
1512 .put = loopback_notify_put,
1513},
1514#define ACTIVE_IDX 2
1515{
1516 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1517 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1518 .name = "PCM Slave Active",
1519 .info = snd_ctl_boolean_mono_info,
1520 .get = loopback_active_get,
1521},
1522#define FORMAT_IDX 3
1523{
1524 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1525 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1526 .name = "PCM Slave Format",
1527 .info = loopback_format_info,
1528 .get = loopback_format_get
1529},
1530#define RATE_IDX 4
1531{
1532 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1533 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1534 .name = "PCM Slave Rate",
1535 .info = loopback_rate_info,
1536 .get = loopback_rate_get
1537},
1538#define CHANNELS_IDX 5
1539{
1540 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1541 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1542 .name = "PCM Slave Channels",
1543 .info = loopback_channels_info,
1544 .get = loopback_channels_get
1545}
1546};
1547
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001548static int loopback_mixer_new(struct loopback *loopback, int notify)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001549{
1550 struct snd_card *card = loopback->card;
1551 struct snd_pcm *pcm;
1552 struct snd_kcontrol *kctl;
1553 struct loopback_setup *setup;
1554 int err, dev, substr, substr_count, idx;
1555
1556 strcpy(card->mixername, "Loopback Mixer");
1557 for (dev = 0; dev < 2; dev++) {
1558 pcm = loopback->pcm[dev];
1559 substr_count =
1560 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1561 for (substr = 0; substr < substr_count; substr++) {
1562 setup = &loopback->setup[substr][dev];
1563 setup->notify = notify;
1564 setup->rate_shift = NO_PITCH;
1565 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1566 setup->rate = 48000;
1567 setup->channels = 2;
1568 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1569 idx++) {
1570 kctl = snd_ctl_new1(&loopback_controls[idx],
1571 loopback);
1572 if (!kctl)
1573 return -ENOMEM;
1574 kctl->id.device = dev;
1575 kctl->id.subdevice = substr;
1576 switch (idx) {
1577 case ACTIVE_IDX:
1578 setup->active_id = kctl->id;
1579 break;
1580 case FORMAT_IDX:
1581 setup->format_id = kctl->id;
1582 break;
1583 case RATE_IDX:
1584 setup->rate_id = kctl->id;
1585 break;
1586 case CHANNELS_IDX:
1587 setup->channels_id = kctl->id;
1588 break;
1589 default:
1590 break;
1591 }
1592 err = snd_ctl_add(card, kctl);
1593 if (err < 0)
1594 return err;
1595 }
1596 }
1597 }
1598 return 0;
1599}
1600
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001601static void print_dpcm_info(struct snd_info_buffer *buffer,
1602 struct loopback_pcm *dpcm,
1603 const char *id)
1604{
1605 snd_iprintf(buffer, " %s\n", id);
1606 if (dpcm == NULL) {
1607 snd_iprintf(buffer, " inactive\n");
1608 return;
1609 }
1610 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1611 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1612 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1613 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1614 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1615 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1616 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
Timo Wischer133f3752019-11-20 11:49:51 -06001617 if (dpcm->cable->ops->dpcm_info)
1618 dpcm->cable->ops->dpcm_info(dpcm, buffer);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001619}
1620
1621static void print_substream_info(struct snd_info_buffer *buffer,
1622 struct loopback *loopback,
1623 int sub,
1624 int num)
1625{
1626 struct loopback_cable *cable = loopback->cables[sub][num];
1627
1628 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1629 if (cable == NULL) {
1630 snd_iprintf(buffer, " inactive\n");
1631 return;
1632 }
1633 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1634 snd_iprintf(buffer, " running: %u\n", cable->running);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +02001635 snd_iprintf(buffer, " pause: %u\n", cable->pause);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001636 print_dpcm_info(buffer, cable->streams[0], "Playback");
1637 print_dpcm_info(buffer, cable->streams[1], "Capture");
1638}
1639
1640static void print_cable_info(struct snd_info_entry *entry,
1641 struct snd_info_buffer *buffer)
1642{
1643 struct loopback *loopback = entry->private_data;
1644 int sub, num;
1645
1646 mutex_lock(&loopback->cable_lock);
1647 num = entry->name[strlen(entry->name)-1];
1648 num = num == '0' ? 0 : 1;
1649 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1650 print_substream_info(buffer, loopback, sub, num);
1651 mutex_unlock(&loopback->cable_lock);
1652}
1653
Andrew Gabbasovc6ae9962019-11-20 11:49:55 -06001654static int loopback_cable_proc_new(struct loopback *loopback, int cidx)
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001655{
1656 char name[32];
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001657
1658 snprintf(name, sizeof(name), "cable#%d", cidx);
Takashi Iwai815d8082019-02-04 15:58:33 +01001659 return snd_card_ro_proc_new(loopback->card, name, loopback,
1660 print_cable_info);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001661}
1662
Timo Wischer26c53372019-11-20 11:49:54 -06001663static void loopback_set_timer_source(struct loopback *loopback,
1664 const char *value)
1665{
1666 if (loopback->timer_source) {
1667 devm_kfree(loopback->card->dev, loopback->timer_source);
1668 loopback->timer_source = NULL;
1669 }
1670 if (value && *value)
1671 loopback->timer_source = devm_kstrdup(loopback->card->dev,
1672 value, GFP_KERNEL);
1673}
1674
Andrew Gabbasovc6ae9962019-11-20 11:49:55 -06001675static void print_timer_source_info(struct snd_info_entry *entry,
1676 struct snd_info_buffer *buffer)
1677{
1678 struct loopback *loopback = entry->private_data;
1679
1680 mutex_lock(&loopback->cable_lock);
1681 snd_iprintf(buffer, "%s\n",
1682 loopback->timer_source ? loopback->timer_source : "");
1683 mutex_unlock(&loopback->cable_lock);
1684}
1685
1686static void change_timer_source_info(struct snd_info_entry *entry,
1687 struct snd_info_buffer *buffer)
1688{
1689 struct loopback *loopback = entry->private_data;
1690 char line[64];
1691
1692 mutex_lock(&loopback->cable_lock);
1693 if (!snd_info_get_line(buffer, line, sizeof(line)))
1694 loopback_set_timer_source(loopback, strim(line));
1695 mutex_unlock(&loopback->cable_lock);
1696}
1697
1698static int loopback_timer_source_proc_new(struct loopback *loopback)
1699{
1700 return snd_card_rw_proc_new(loopback->card, "timer_source", loopback,
1701 print_timer_source_info,
1702 change_timer_source_info);
1703}
1704
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001705static int loopback_probe(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001706{
1707 struct snd_card *card;
1708 struct loopback *loopback;
1709 int dev = devptr->id;
1710 int err;
1711
Takashi Iwai5872f3f2014-01-29 12:59:08 +01001712 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1713 sizeof(struct loopback), &card);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001714 if (err < 0)
1715 return err;
1716 loopback = card->private_data;
1717
1718 if (pcm_substreams[dev] < 1)
1719 pcm_substreams[dev] = 1;
1720 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1721 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1722
1723 loopback->card = card;
Timo Wischer26c53372019-11-20 11:49:54 -06001724 loopback_set_timer_source(loopback, timer_source[dev]);
1725
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001726 mutex_init(&loopback->cable_lock);
1727
1728 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1729 if (err < 0)
1730 goto __nodev;
1731 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1732 if (err < 0)
1733 goto __nodev;
1734 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1735 if (err < 0)
1736 goto __nodev;
Andrew Gabbasovc6ae9962019-11-20 11:49:55 -06001737 loopback_cable_proc_new(loopback, 0);
1738 loopback_cable_proc_new(loopback, 1);
1739 loopback_timer_source_proc_new(loopback);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001740 strcpy(card->driver, "Loopback");
1741 strcpy(card->shortname, "Loopback");
1742 sprintf(card->longname, "Loopback %i", dev + 1);
1743 err = snd_card_register(card);
1744 if (!err) {
1745 platform_set_drvdata(devptr, card);
1746 return 0;
1747 }
1748 __nodev:
1749 snd_card_free(card);
1750 return err;
1751}
1752
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001753static int loopback_remove(struct platform_device *devptr)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001754{
1755 snd_card_free(platform_get_drvdata(devptr));
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001756 return 0;
1757}
1758
Takashi Iwaid34e4e02012-08-09 15:47:15 +02001759#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001760static int loopback_suspend(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001761{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001762 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001763
1764 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001765 return 0;
1766}
1767
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001768static int loopback_resume(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001769{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001770 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001771
1772 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1773 return 0;
1774}
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001775
1776static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1777#define LOOPBACK_PM_OPS &loopback_pm
1778#else
1779#define LOOPBACK_PM_OPS NULL
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001780#endif
1781
1782#define SND_LOOPBACK_DRIVER "snd_aloop"
1783
1784static struct platform_driver loopback_driver = {
1785 .probe = loopback_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -05001786 .remove = loopback_remove,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001787 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +02001788 .name = SND_LOOPBACK_DRIVER,
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001789 .pm = LOOPBACK_PM_OPS,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001790 },
1791};
1792
1793static void loopback_unregister_all(void)
1794{
1795 int i;
1796
1797 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1798 platform_device_unregister(devices[i]);
1799 platform_driver_unregister(&loopback_driver);
1800}
1801
1802static int __init alsa_card_loopback_init(void)
1803{
1804 int i, err, cards;
1805
1806 err = platform_driver_register(&loopback_driver);
1807 if (err < 0)
1808 return err;
1809
1810
1811 cards = 0;
1812 for (i = 0; i < SNDRV_CARDS; i++) {
1813 struct platform_device *device;
1814 if (!enable[i])
1815 continue;
1816 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1817 i, NULL, 0);
1818 if (IS_ERR(device))
1819 continue;
1820 if (!platform_get_drvdata(device)) {
1821 platform_device_unregister(device);
1822 continue;
1823 }
1824 devices[i] = device;
1825 cards++;
1826 }
1827 if (!cards) {
1828#ifdef MODULE
1829 printk(KERN_ERR "aloop: No loopback enabled\n");
1830#endif
1831 loopback_unregister_all();
1832 return -ENODEV;
1833 }
1834 return 0;
1835}
1836
1837static void __exit alsa_card_loopback_exit(void)
1838{
1839 loopback_unregister_all();
1840}
1841
1842module_init(alsa_card_loopback_init)
1843module_exit(alsa_card_loopback_exit)