xref: /openbmc/linux/sound/drivers/aloop.c (revision f7efa9b8)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2597603d6SJaroslav Kysela /*
3597603d6SJaroslav Kysela  *  Loopback soundcard
4597603d6SJaroslav Kysela  *
5597603d6SJaroslav Kysela  *  Original code:
6597603d6SJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7597603d6SJaroslav Kysela  *
8597603d6SJaroslav Kysela  *  More accurate positioning and full-duplex support:
9597603d6SJaroslav Kysela  *  Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
10597603d6SJaroslav Kysela  *
11597603d6SJaroslav Kysela  *  Major (almost complete) rewrite:
12597603d6SJaroslav Kysela  *  Copyright (c) by Takashi Iwai <tiwai@suse.de>
13597603d6SJaroslav Kysela  *
14597603d6SJaroslav Kysela  *  A next major update in 2010 (separate timers for playback and capture):
15597603d6SJaroslav Kysela  *  Copyright (c) Jaroslav Kysela <perex@perex.cz>
16597603d6SJaroslav Kysela  */
17597603d6SJaroslav Kysela 
18597603d6SJaroslav Kysela #include <linux/init.h>
19597603d6SJaroslav Kysela #include <linux/jiffies.h>
20597603d6SJaroslav Kysela #include <linux/slab.h>
21597603d6SJaroslav Kysela #include <linux/time.h>
22597603d6SJaroslav Kysela #include <linux/wait.h>
2365a77217SPaul Gortmaker #include <linux/module.h>
24597603d6SJaroslav Kysela #include <linux/platform_device.h>
25597603d6SJaroslav Kysela #include <sound/core.h>
26597603d6SJaroslav Kysela #include <sound/control.h>
27597603d6SJaroslav Kysela #include <sound/pcm.h>
28b088b53eSTakashi Iwai #include <sound/pcm_params.h>
29e74670b6SJaroslav Kysela #include <sound/info.h>
30597603d6SJaroslav Kysela #include <sound/initval.h>
3126c53379STimo Wischer #include <sound/timer.h>
32597603d6SJaroslav Kysela 
33597603d6SJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
34597603d6SJaroslav Kysela MODULE_DESCRIPTION("A loopback soundcard");
35597603d6SJaroslav Kysela MODULE_LICENSE("GPL");
36597603d6SJaroslav Kysela 
37597603d6SJaroslav Kysela #define MAX_PCM_SUBSTREAMS	8
38597603d6SJaroslav Kysela 
39597603d6SJaroslav Kysela static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
40597603d6SJaroslav Kysela static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
41a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
42597603d6SJaroslav Kysela static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
43597603d6SJaroslav Kysela static int pcm_notify[SNDRV_CARDS];
4426c53379STimo Wischer static char *timer_source[SNDRV_CARDS];
45597603d6SJaroslav Kysela 
46597603d6SJaroslav Kysela module_param_array(index, int, NULL, 0444);
47597603d6SJaroslav Kysela MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
48597603d6SJaroslav Kysela module_param_array(id, charp, NULL, 0444);
49597603d6SJaroslav Kysela MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
50597603d6SJaroslav Kysela module_param_array(enable, bool, NULL, 0444);
51597603d6SJaroslav Kysela MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
52597603d6SJaroslav Kysela module_param_array(pcm_substreams, int, NULL, 0444);
53597603d6SJaroslav Kysela MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
54597603d6SJaroslav Kysela module_param_array(pcm_notify, int, NULL, 0444);
55597603d6SJaroslav Kysela MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
5626c53379STimo Wischer module_param_array(timer_source, charp, NULL, 0444);
5726c53379STimo Wischer MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default].");
58597603d6SJaroslav Kysela 
59597603d6SJaroslav Kysela #define NO_PITCH 100000
60597603d6SJaroslav Kysela 
61fd1f7c74STimo Wischer #define CABLE_VALID_PLAYBACK	BIT(SNDRV_PCM_STREAM_PLAYBACK)
62fd1f7c74STimo Wischer #define CABLE_VALID_CAPTURE	BIT(SNDRV_PCM_STREAM_CAPTURE)
63fd1f7c74STimo Wischer #define CABLE_VALID_BOTH	(CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE)
64fd1f7c74STimo Wischer 
65133f3759STimo Wischer struct loopback_cable;
66597603d6SJaroslav Kysela struct loopback_pcm;
67597603d6SJaroslav Kysela 
68133f3759STimo Wischer struct loopback_ops {
69133f3759STimo Wischer 	/* optional
70133f3759STimo Wischer 	 * call in loopback->cable_lock
71133f3759STimo Wischer 	 */
72133f3759STimo Wischer 	int (*open)(struct loopback_pcm *dpcm);
73133f3759STimo Wischer 	/* required
74133f3759STimo Wischer 	 * call in cable->lock
75133f3759STimo Wischer 	 */
76133f3759STimo Wischer 	int (*start)(struct loopback_pcm *dpcm);
77133f3759STimo Wischer 	/* required
78133f3759STimo Wischer 	 * call in cable->lock
79133f3759STimo Wischer 	 */
80133f3759STimo Wischer 	int (*stop)(struct loopback_pcm *dpcm);
81133f3759STimo Wischer 	/* optional */
82133f3759STimo Wischer 	int (*stop_sync)(struct loopback_pcm *dpcm);
83133f3759STimo Wischer 	/* optional */
84133f3759STimo Wischer 	int (*close_substream)(struct loopback_pcm *dpcm);
85133f3759STimo Wischer 	/* optional
86133f3759STimo Wischer 	 * call in loopback->cable_lock
87133f3759STimo Wischer 	 */
88133f3759STimo Wischer 	int (*close_cable)(struct loopback_pcm *dpcm);
89133f3759STimo Wischer 	/* optional
90133f3759STimo Wischer 	 * call in cable->lock
91133f3759STimo Wischer 	 */
92133f3759STimo Wischer 	unsigned int (*pos_update)(struct loopback_cable *cable);
93133f3759STimo Wischer 	/* optional */
94133f3759STimo Wischer 	void (*dpcm_info)(struct loopback_pcm *dpcm,
95133f3759STimo Wischer 			  struct snd_info_buffer *buffer);
96133f3759STimo Wischer };
97133f3759STimo Wischer 
98597603d6SJaroslav Kysela struct loopback_cable {
99597603d6SJaroslav Kysela 	spinlock_t lock;
100597603d6SJaroslav Kysela 	struct loopback_pcm *streams[2];
101597603d6SJaroslav Kysela 	struct snd_pcm_hardware hw;
102597603d6SJaroslav Kysela 	/* flags */
103597603d6SJaroslav Kysela 	unsigned int valid;
104597603d6SJaroslav Kysela 	unsigned int running;
1055de9e45fSJaroslav Kysela 	unsigned int pause;
106133f3759STimo Wischer 	/* timer specific */
107e714fa93SRikard Falkeborn 	const struct loopback_ops *ops;
10826c53379STimo Wischer 	/* If sound timer is used */
10926c53379STimo Wischer 	struct {
11026c53379STimo Wischer 		int stream;
11126c53379STimo Wischer 		struct snd_timer_id id;
1126053a712STakashi Iwai 		struct work_struct event_work;
11326c53379STimo Wischer 		struct snd_timer_instance *instance;
11426c53379STimo Wischer 	} snd_timer;
115597603d6SJaroslav Kysela };
116597603d6SJaroslav Kysela 
117597603d6SJaroslav Kysela struct loopback_setup {
118597603d6SJaroslav Kysela 	unsigned int notify: 1;
119597603d6SJaroslav Kysela 	unsigned int rate_shift;
1208c356c52STakashi Iwai 	snd_pcm_format_t format;
121597603d6SJaroslav Kysela 	unsigned int rate;
122597603d6SJaroslav Kysela 	unsigned int channels;
123597603d6SJaroslav Kysela 	struct snd_ctl_elem_id active_id;
124597603d6SJaroslav Kysela 	struct snd_ctl_elem_id format_id;
125597603d6SJaroslav Kysela 	struct snd_ctl_elem_id rate_id;
126597603d6SJaroslav Kysela 	struct snd_ctl_elem_id channels_id;
127597603d6SJaroslav Kysela };
128597603d6SJaroslav Kysela 
129597603d6SJaroslav Kysela struct loopback {
130597603d6SJaroslav Kysela 	struct snd_card *card;
131597603d6SJaroslav Kysela 	struct mutex cable_lock;
132597603d6SJaroslav Kysela 	struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
133597603d6SJaroslav Kysela 	struct snd_pcm *pcm[2];
134597603d6SJaroslav Kysela 	struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
13526c53379STimo Wischer 	const char *timer_source;
136597603d6SJaroslav Kysela };
137597603d6SJaroslav Kysela 
138597603d6SJaroslav Kysela struct loopback_pcm {
139597603d6SJaroslav Kysela 	struct loopback *loopback;
140597603d6SJaroslav Kysela 	struct snd_pcm_substream *substream;
141597603d6SJaroslav Kysela 	struct loopback_cable *cable;
142597603d6SJaroslav Kysela 	unsigned int pcm_buffer_size;
143597603d6SJaroslav Kysela 	unsigned int buf_pos;	/* position in buffer */
144597603d6SJaroslav Kysela 	unsigned int silent_size;
145597603d6SJaroslav Kysela 	/* PCM parameters */
146597603d6SJaroslav Kysela 	unsigned int pcm_period_size;
147597603d6SJaroslav Kysela 	unsigned int pcm_bps;		/* bytes per second */
148597603d6SJaroslav Kysela 	unsigned int pcm_salign;	/* bytes per sample * channels */
149597603d6SJaroslav Kysela 	unsigned int pcm_rate_shift;	/* rate shift value */
150597603d6SJaroslav Kysela 	/* flags */
151597603d6SJaroslav Kysela 	unsigned int period_update_pending :1;
152597603d6SJaroslav Kysela 	/* timer stuff */
15397dda3daSTimo Wischer 	unsigned int irq_pos;		/* fractional IRQ position in jiffies
15497dda3daSTimo Wischer 					 * ticks
15597dda3daSTimo Wischer 					 */
15697dda3daSTimo Wischer 	unsigned int period_size_frac;	/* period size in jiffies ticks */
157b012513cSJaroslav Kysela 	unsigned int last_drift;
158597603d6SJaroslav Kysela 	unsigned long last_jiffies;
15926c53379STimo Wischer 	/* If jiffies timer is used */
160597603d6SJaroslav Kysela 	struct timer_list timer;
161597603d6SJaroslav Kysela };
162597603d6SJaroslav Kysela 
163597603d6SJaroslav Kysela static struct platform_device *devices[SNDRV_CARDS];
164597603d6SJaroslav Kysela 
byte_pos(struct loopback_pcm * dpcm,unsigned int x)165597603d6SJaroslav Kysela static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
166597603d6SJaroslav Kysela {
167597603d6SJaroslav Kysela 	if (dpcm->pcm_rate_shift == NO_PITCH) {
168597603d6SJaroslav Kysela 		x /= HZ;
169597603d6SJaroslav Kysela 	} else {
170597603d6SJaroslav Kysela 		x = div_u64(NO_PITCH * (unsigned long long)x,
171597603d6SJaroslav Kysela 			    HZ * (unsigned long long)dpcm->pcm_rate_shift);
172597603d6SJaroslav Kysela 	}
173597603d6SJaroslav Kysela 	return x - (x % dpcm->pcm_salign);
174597603d6SJaroslav Kysela }
175597603d6SJaroslav Kysela 
frac_pos(struct loopback_pcm * dpcm,unsigned int x)176597603d6SJaroslav Kysela static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
177597603d6SJaroslav Kysela {
178597603d6SJaroslav Kysela 	if (dpcm->pcm_rate_shift == NO_PITCH) {	/* no pitch */
179597603d6SJaroslav Kysela 		return x * HZ;
180597603d6SJaroslav Kysela 	} else {
181597603d6SJaroslav Kysela 		x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
182597603d6SJaroslav Kysela 			    NO_PITCH);
183597603d6SJaroslav Kysela 	}
184597603d6SJaroslav Kysela 	return x;
185597603d6SJaroslav Kysela }
186597603d6SJaroslav Kysela 
get_setup(struct loopback_pcm * dpcm)187597603d6SJaroslav Kysela static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
188597603d6SJaroslav Kysela {
189597603d6SJaroslav Kysela 	int device = dpcm->substream->pstr->pcm->device;
190597603d6SJaroslav Kysela 
191597603d6SJaroslav Kysela 	if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
192597603d6SJaroslav Kysela 		device ^= 1;
193597603d6SJaroslav Kysela 	return &dpcm->loopback->setup[dpcm->substream->number][device];
194597603d6SJaroslav Kysela }
195597603d6SJaroslav Kysela 
get_notify(struct loopback_pcm * dpcm)196597603d6SJaroslav Kysela static inline unsigned int get_notify(struct loopback_pcm *dpcm)
197597603d6SJaroslav Kysela {
198597603d6SJaroslav Kysela 	return get_setup(dpcm)->notify;
199597603d6SJaroslav Kysela }
200597603d6SJaroslav Kysela 
get_rate_shift(struct loopback_pcm * dpcm)201597603d6SJaroslav Kysela static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
202597603d6SJaroslav Kysela {
203597603d6SJaroslav Kysela 	return get_setup(dpcm)->rate_shift;
204597603d6SJaroslav Kysela }
205597603d6SJaroslav Kysela 
206999fc9f6STakashi Iwai /* call in cable->lock */
loopback_jiffies_timer_start(struct loopback_pcm * dpcm)2078e3bf7cdSTimo Wischer static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm)
208597603d6SJaroslav Kysela {
209597603d6SJaroslav Kysela 	unsigned long tick;
210597603d6SJaroslav Kysela 	unsigned int rate_shift = get_rate_shift(dpcm);
211597603d6SJaroslav Kysela 
212597603d6SJaroslav Kysela 	if (rate_shift != dpcm->pcm_rate_shift) {
213597603d6SJaroslav Kysela 		dpcm->pcm_rate_shift = rate_shift;
214597603d6SJaroslav Kysela 		dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
215597603d6SJaroslav Kysela 	}
2160db71023SJaroslav Kysela 	if (dpcm->period_size_frac <= dpcm->irq_pos) {
2170db71023SJaroslav Kysela 		dpcm->irq_pos %= dpcm->period_size_frac;
2180db71023SJaroslav Kysela 		dpcm->period_update_pending = 1;
2190db71023SJaroslav Kysela 	}
220597603d6SJaroslav Kysela 	tick = dpcm->period_size_frac - dpcm->irq_pos;
2217ef74bdcSLars-Peter Clausen 	tick = DIV_ROUND_UP(tick, dpcm->pcm_bps);
222db974553STakashi Iwai 	mod_timer(&dpcm->timer, jiffies + tick);
22309419f1aSTimo Wischer 
22409419f1aSTimo Wischer 	return 0;
225597603d6SJaroslav Kysela }
226597603d6SJaroslav Kysela 
227999fc9f6STakashi Iwai /* call in cable->lock */
loopback_snd_timer_start(struct loopback_pcm * dpcm)22826c53379STimo Wischer static int loopback_snd_timer_start(struct loopback_pcm *dpcm)
22926c53379STimo Wischer {
23026c53379STimo Wischer 	struct loopback_cable *cable = dpcm->cable;
23126c53379STimo Wischer 	int err;
23226c53379STimo Wischer 
23326c53379STimo Wischer 	/* Loopback device has to use same period as timer card. Therefore
23426c53379STimo Wischer 	 * wake up for each snd_pcm_period_elapsed() call of timer card.
23526c53379STimo Wischer 	 */
23626c53379STimo Wischer 	err = snd_timer_start(cable->snd_timer.instance, 1);
23726c53379STimo Wischer 	if (err < 0) {
23826c53379STimo Wischer 		/* do not report error if trying to start but already
23926c53379STimo Wischer 		 * running. For example called by opposite substream
24026c53379STimo Wischer 		 * of the same cable
24126c53379STimo Wischer 		 */
24226c53379STimo Wischer 		if (err == -EBUSY)
24326c53379STimo Wischer 			return 0;
24426c53379STimo Wischer 
24526c53379STimo Wischer 		pcm_err(dpcm->substream->pcm,
24626c53379STimo Wischer 			"snd_timer_start(%d,%d,%d) failed with %d",
24726c53379STimo Wischer 			cable->snd_timer.id.card,
24826c53379STimo Wischer 			cable->snd_timer.id.device,
24926c53379STimo Wischer 			cable->snd_timer.id.subdevice,
25026c53379STimo Wischer 			err);
25126c53379STimo Wischer 	}
25226c53379STimo Wischer 
25326c53379STimo Wischer 	return err;
25426c53379STimo Wischer }
25526c53379STimo Wischer 
25626c53379STimo Wischer /* call in cable->lock */
loopback_jiffies_timer_stop(struct loopback_pcm * dpcm)2578e3bf7cdSTimo Wischer static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm)
258597603d6SJaroslav Kysela {
259597603d6SJaroslav Kysela 	del_timer(&dpcm->timer);
260e74670b6SJaroslav Kysela 	dpcm->timer.expires = 0;
26109419f1aSTimo Wischer 
26209419f1aSTimo Wischer 	return 0;
263597603d6SJaroslav Kysela }
264597603d6SJaroslav Kysela 
26526c53379STimo Wischer /* call in cable->lock */
loopback_snd_timer_stop(struct loopback_pcm * dpcm)26626c53379STimo Wischer static int loopback_snd_timer_stop(struct loopback_pcm *dpcm)
26726c53379STimo Wischer {
26826c53379STimo Wischer 	struct loopback_cable *cable = dpcm->cable;
26926c53379STimo Wischer 	int err;
27026c53379STimo Wischer 
27126c53379STimo Wischer 	/* only stop if both devices (playback and capture) are not running */
27226c53379STimo Wischer 	if (cable->running ^ cable->pause)
27326c53379STimo Wischer 		return 0;
27426c53379STimo Wischer 
27526c53379STimo Wischer 	err = snd_timer_stop(cable->snd_timer.instance);
27626c53379STimo Wischer 	if (err < 0) {
27726c53379STimo Wischer 		pcm_err(dpcm->substream->pcm,
27826c53379STimo Wischer 			"snd_timer_stop(%d,%d,%d) failed with %d",
27926c53379STimo Wischer 			cable->snd_timer.id.card,
28026c53379STimo Wischer 			cable->snd_timer.id.device,
28126c53379STimo Wischer 			cable->snd_timer.id.subdevice,
28226c53379STimo Wischer 			err);
28326c53379STimo Wischer 	}
28426c53379STimo Wischer 
28526c53379STimo Wischer 	return err;
28626c53379STimo Wischer }
28726c53379STimo Wischer 
loopback_jiffies_timer_stop_sync(struct loopback_pcm * dpcm)2888e3bf7cdSTimo Wischer static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm)
28967a01afaSTakashi Iwai {
29067a01afaSTakashi Iwai 	del_timer_sync(&dpcm->timer);
29109419f1aSTimo Wischer 
29209419f1aSTimo Wischer 	return 0;
29367a01afaSTakashi Iwai }
29467a01afaSTakashi Iwai 
29526c53379STimo Wischer /* call in loopback->cable_lock */
loopback_snd_timer_close_cable(struct loopback_pcm * dpcm)29626c53379STimo Wischer static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)
29726c53379STimo Wischer {
29826c53379STimo Wischer 	struct loopback_cable *cable = dpcm->cable;
29926c53379STimo Wischer 
30026c53379STimo Wischer 	/* snd_timer was not opened */
30126c53379STimo Wischer 	if (!cable->snd_timer.instance)
30226c53379STimo Wischer 		return 0;
30326c53379STimo Wischer 
30426c53379STimo Wischer 	/* will only be called from free_cable() when other stream was
30526c53379STimo Wischer 	 * already closed. Other stream cannot be reopened as long as
30626c53379STimo Wischer 	 * loopback->cable_lock is locked. Therefore no need to lock
30726c53379STimo Wischer 	 * cable->lock;
30826c53379STimo Wischer 	 */
30926c53379STimo Wischer 	snd_timer_close(cable->snd_timer.instance);
3109314e44fSAndrew Gabbasov 
3116053a712STakashi Iwai 	/* wait till drain work has finished if requested */
3126053a712STakashi Iwai 	cancel_work_sync(&cable->snd_timer.event_work);
3139314e44fSAndrew Gabbasov 
31426c53379STimo Wischer 	snd_timer_instance_free(cable->snd_timer.instance);
31526c53379STimo Wischer 	memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));
31626c53379STimo Wischer 
31726c53379STimo Wischer 	return 0;
31826c53379STimo Wischer }
31926c53379STimo Wischer 
loopback_check_format(struct loopback_cable * cable,int stream)320597603d6SJaroslav Kysela static int loopback_check_format(struct loopback_cable *cable, int stream)
321597603d6SJaroslav Kysela {
322b1c73fc8SJaroslav Kysela 	struct snd_pcm_runtime *runtime, *cruntime;
323597603d6SJaroslav Kysela 	struct loopback_setup *setup;
324597603d6SJaroslav Kysela 	struct snd_card *card;
325597603d6SJaroslav Kysela 	int check;
326597603d6SJaroslav Kysela 
327597603d6SJaroslav Kysela 	if (cable->valid != CABLE_VALID_BOTH) {
328597603d6SJaroslav Kysela 		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
329597603d6SJaroslav Kysela 			goto __notify;
330597603d6SJaroslav Kysela 		return 0;
331597603d6SJaroslav Kysela 	}
332597603d6SJaroslav Kysela 	runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
333597603d6SJaroslav Kysela 							substream->runtime;
334b1c73fc8SJaroslav Kysela 	cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
335b1c73fc8SJaroslav Kysela 							substream->runtime;
336b1c73fc8SJaroslav Kysela 	check = runtime->format != cruntime->format ||
337b1c73fc8SJaroslav Kysela 		runtime->rate != cruntime->rate ||
338b1c73fc8SJaroslav Kysela 		runtime->channels != cruntime->channels;
339597603d6SJaroslav Kysela 	if (!check)
340597603d6SJaroslav Kysela 		return 0;
341597603d6SJaroslav Kysela 	if (stream == SNDRV_PCM_STREAM_CAPTURE) {
342597603d6SJaroslav Kysela 		return -EIO;
343597603d6SJaroslav Kysela 	} else {
344597603d6SJaroslav Kysela 		snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
345597603d6SJaroslav Kysela 					substream, SNDRV_PCM_STATE_DRAINING);
346597603d6SJaroslav Kysela 	      __notify:
347597603d6SJaroslav Kysela 		runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
348597603d6SJaroslav Kysela 							substream->runtime;
349597603d6SJaroslav Kysela 		setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
350597603d6SJaroslav Kysela 		card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
351597603d6SJaroslav Kysela 		if (setup->format != runtime->format) {
352597603d6SJaroslav Kysela 			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
353597603d6SJaroslav Kysela 							&setup->format_id);
354597603d6SJaroslav Kysela 			setup->format = runtime->format;
355597603d6SJaroslav Kysela 		}
356597603d6SJaroslav Kysela 		if (setup->rate != runtime->rate) {
357597603d6SJaroslav Kysela 			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
358597603d6SJaroslav Kysela 							&setup->rate_id);
359597603d6SJaroslav Kysela 			setup->rate = runtime->rate;
360597603d6SJaroslav Kysela 		}
361597603d6SJaroslav Kysela 		if (setup->channels != runtime->channels) {
362597603d6SJaroslav Kysela 			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
363597603d6SJaroslav Kysela 							&setup->channels_id);
364597603d6SJaroslav Kysela 			setup->channels = runtime->channels;
365597603d6SJaroslav Kysela 		}
366597603d6SJaroslav Kysela 	}
367597603d6SJaroslav Kysela 	return 0;
368597603d6SJaroslav Kysela }
369597603d6SJaroslav Kysela 
loopback_active_notify(struct loopback_pcm * dpcm)370597603d6SJaroslav Kysela static void loopback_active_notify(struct loopback_pcm *dpcm)
371597603d6SJaroslav Kysela {
372597603d6SJaroslav Kysela 	snd_ctl_notify(dpcm->loopback->card,
373597603d6SJaroslav Kysela 		       SNDRV_CTL_EVENT_MASK_VALUE,
374597603d6SJaroslav Kysela 		       &get_setup(dpcm)->active_id);
375597603d6SJaroslav Kysela }
376597603d6SJaroslav Kysela 
loopback_trigger(struct snd_pcm_substream * substream,int cmd)377597603d6SJaroslav Kysela static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
378597603d6SJaroslav Kysela {
379597603d6SJaroslav Kysela 	struct snd_pcm_runtime *runtime = substream->runtime;
380597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm = runtime->private_data;
381597603d6SJaroslav Kysela 	struct loopback_cable *cable = dpcm->cable;
38209419f1aSTimo Wischer 	int err = 0, stream = 1 << substream->stream;
383597603d6SJaroslav Kysela 
384597603d6SJaroslav Kysela 	switch (cmd) {
385597603d6SJaroslav Kysela 	case SNDRV_PCM_TRIGGER_START:
386597603d6SJaroslav Kysela 		err = loopback_check_format(cable, substream->stream);
387597603d6SJaroslav Kysela 		if (err < 0)
388597603d6SJaroslav Kysela 			return err;
389597603d6SJaroslav Kysela 		dpcm->last_jiffies = jiffies;
390597603d6SJaroslav Kysela 		dpcm->pcm_rate_shift = 0;
391b012513cSJaroslav Kysela 		dpcm->last_drift = 0;
392dd04bb12SJaroslav Kysela 		spin_lock(&cable->lock);
3935de9e45fSJaroslav Kysela 		cable->running |= stream;
3945de9e45fSJaroslav Kysela 		cable->pause &= ~stream;
395133f3759STimo Wischer 		err = cable->ops->start(dpcm);
396999fc9f6STakashi Iwai 		spin_unlock(&cable->lock);
397597603d6SJaroslav Kysela 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
398597603d6SJaroslav Kysela 			loopback_active_notify(dpcm);
399597603d6SJaroslav Kysela 		break;
400597603d6SJaroslav Kysela 	case SNDRV_PCM_TRIGGER_STOP:
401dd04bb12SJaroslav Kysela 		spin_lock(&cable->lock);
4025de9e45fSJaroslav Kysela 		cable->running &= ~stream;
4035de9e45fSJaroslav Kysela 		cable->pause &= ~stream;
404133f3759STimo Wischer 		err = cable->ops->stop(dpcm);
405999fc9f6STakashi Iwai 		spin_unlock(&cable->lock);
406597603d6SJaroslav Kysela 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
407597603d6SJaroslav Kysela 			loopback_active_notify(dpcm);
408597603d6SJaroslav Kysela 		break;
4095de9e45fSJaroslav Kysela 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
410edac8943STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4115de9e45fSJaroslav Kysela 		spin_lock(&cable->lock);
4125de9e45fSJaroslav Kysela 		cable->pause |= stream;
413133f3759STimo Wischer 		err = cable->ops->stop(dpcm);
414999fc9f6STakashi Iwai 		spin_unlock(&cable->lock);
415306a4f3cSRobert Rosengren 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
416306a4f3cSRobert Rosengren 			loopback_active_notify(dpcm);
4175de9e45fSJaroslav Kysela 		break;
4185de9e45fSJaroslav Kysela 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
419edac8943STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4205de9e45fSJaroslav Kysela 		spin_lock(&cable->lock);
4215de9e45fSJaroslav Kysela 		dpcm->last_jiffies = jiffies;
4225de9e45fSJaroslav Kysela 		cable->pause &= ~stream;
423133f3759STimo Wischer 		err = cable->ops->start(dpcm);
424999fc9f6STakashi Iwai 		spin_unlock(&cable->lock);
425306a4f3cSRobert Rosengren 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
426306a4f3cSRobert Rosengren 			loopback_active_notify(dpcm);
4275de9e45fSJaroslav Kysela 		break;
428597603d6SJaroslav Kysela 	default:
429597603d6SJaroslav Kysela 		return -EINVAL;
430597603d6SJaroslav Kysela 	}
43109419f1aSTimo Wischer 	return err;
432597603d6SJaroslav Kysela }
433597603d6SJaroslav Kysela 
params_change(struct snd_pcm_substream * substream)434b1c73fc8SJaroslav Kysela static void params_change(struct snd_pcm_substream *substream)
435b1c73fc8SJaroslav Kysela {
436b1c73fc8SJaroslav Kysela 	struct snd_pcm_runtime *runtime = substream->runtime;
437b1c73fc8SJaroslav Kysela 	struct loopback_pcm *dpcm = runtime->private_data;
438b1c73fc8SJaroslav Kysela 	struct loopback_cable *cable = dpcm->cable;
439b1c73fc8SJaroslav Kysela 
44074c34ca1SEldad Zack 	cable->hw.formats = pcm_format_to_bits(runtime->format);
441b1c73fc8SJaroslav Kysela 	cable->hw.rate_min = runtime->rate;
442b1c73fc8SJaroslav Kysela 	cable->hw.rate_max = runtime->rate;
443b1c73fc8SJaroslav Kysela 	cable->hw.channels_min = runtime->channels;
444b1c73fc8SJaroslav Kysela 	cable->hw.channels_max = runtime->channels;
44526c53379STimo Wischer 
44626c53379STimo Wischer 	if (cable->snd_timer.instance) {
44726c53379STimo Wischer 		cable->hw.period_bytes_min =
44826c53379STimo Wischer 				frames_to_bytes(runtime, runtime->period_size);
44926c53379STimo Wischer 		cable->hw.period_bytes_max = cable->hw.period_bytes_min;
45026c53379STimo Wischer 	}
45126c53379STimo Wischer 
452b1c73fc8SJaroslav Kysela }
453b1c73fc8SJaroslav Kysela 
loopback_prepare(struct snd_pcm_substream * substream)454597603d6SJaroslav Kysela static int loopback_prepare(struct snd_pcm_substream *substream)
455597603d6SJaroslav Kysela {
456597603d6SJaroslav Kysela 	struct snd_pcm_runtime *runtime = substream->runtime;
457597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm = runtime->private_data;
458597603d6SJaroslav Kysela 	struct loopback_cable *cable = dpcm->cable;
45909419f1aSTimo Wischer 	int err, bps, salign;
460597603d6SJaroslav Kysela 
461133f3759STimo Wischer 	if (cable->ops->stop_sync) {
462133f3759STimo Wischer 		err = cable->ops->stop_sync(dpcm);
46309419f1aSTimo Wischer 		if (err < 0)
46409419f1aSTimo Wischer 			return err;
465133f3759STimo Wischer 	}
46667a01afaSTakashi Iwai 
46750e09084STimo Wischer 	salign = (snd_pcm_format_physical_width(runtime->format) *
468597603d6SJaroslav Kysela 						runtime->channels) / 8;
469597603d6SJaroslav Kysela 	bps = salign * runtime->rate;
470597603d6SJaroslav Kysela 	if (bps <= 0 || salign <= 0)
471597603d6SJaroslav Kysela 		return -EINVAL;
472597603d6SJaroslav Kysela 
473597603d6SJaroslav Kysela 	dpcm->buf_pos = 0;
474597603d6SJaroslav Kysela 	dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
475597603d6SJaroslav Kysela 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
476597603d6SJaroslav Kysela 		/* clear capture buffer */
477597603d6SJaroslav Kysela 		dpcm->silent_size = dpcm->pcm_buffer_size;
478597603d6SJaroslav Kysela 		snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
479597603d6SJaroslav Kysela 					   runtime->buffer_size * runtime->channels);
480597603d6SJaroslav Kysela 	}
481597603d6SJaroslav Kysela 
482597603d6SJaroslav Kysela 	dpcm->irq_pos = 0;
483597603d6SJaroslav Kysela 	dpcm->period_update_pending = 0;
484597603d6SJaroslav Kysela 	dpcm->pcm_bps = bps;
485597603d6SJaroslav Kysela 	dpcm->pcm_salign = salign;
486597603d6SJaroslav Kysela 	dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
487597603d6SJaroslav Kysela 
488597603d6SJaroslav Kysela 	mutex_lock(&dpcm->loopback->cable_lock);
489b1c73fc8SJaroslav Kysela 	if (!(cable->valid & ~(1 << substream->stream)) ||
490b1c73fc8SJaroslav Kysela             (get_setup(dpcm)->notify &&
491b1c73fc8SJaroslav Kysela 	     substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
492b1c73fc8SJaroslav Kysela 		params_change(substream);
493597603d6SJaroslav Kysela 	cable->valid |= 1 << substream->stream;
494597603d6SJaroslav Kysela 	mutex_unlock(&dpcm->loopback->cable_lock);
495597603d6SJaroslav Kysela 
496597603d6SJaroslav Kysela 	return 0;
497597603d6SJaroslav Kysela }
498597603d6SJaroslav Kysela 
clear_capture_buf(struct loopback_pcm * dpcm,unsigned int bytes)499597603d6SJaroslav Kysela static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
500597603d6SJaroslav Kysela {
501597603d6SJaroslav Kysela 	struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
502597603d6SJaroslav Kysela 	char *dst = runtime->dma_area;
503597603d6SJaroslav Kysela 	unsigned int dst_off = dpcm->buf_pos;
504597603d6SJaroslav Kysela 
505597603d6SJaroslav Kysela 	if (dpcm->silent_size >= dpcm->pcm_buffer_size)
506597603d6SJaroslav Kysela 		return;
507597603d6SJaroslav Kysela 	if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
508597603d6SJaroslav Kysela 		bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
509597603d6SJaroslav Kysela 
510597603d6SJaroslav Kysela 	for (;;) {
511597603d6SJaroslav Kysela 		unsigned int size = bytes;
512597603d6SJaroslav Kysela 		if (dst_off + size > dpcm->pcm_buffer_size)
513597603d6SJaroslav Kysela 			size = dpcm->pcm_buffer_size - dst_off;
514597603d6SJaroslav Kysela 		snd_pcm_format_set_silence(runtime->format, dst + dst_off,
515597603d6SJaroslav Kysela 					   bytes_to_frames(runtime, size) *
516597603d6SJaroslav Kysela 					   	runtime->channels);
517597603d6SJaroslav Kysela 		dpcm->silent_size += size;
518597603d6SJaroslav Kysela 		bytes -= size;
519597603d6SJaroslav Kysela 		if (!bytes)
520597603d6SJaroslav Kysela 			break;
521597603d6SJaroslav Kysela 		dst_off = 0;
522597603d6SJaroslav Kysela 	}
523597603d6SJaroslav Kysela }
524597603d6SJaroslav Kysela 
copy_play_buf(struct loopback_pcm * play,struct loopback_pcm * capt,unsigned int bytes)525597603d6SJaroslav Kysela static void copy_play_buf(struct loopback_pcm *play,
526597603d6SJaroslav Kysela 			  struct loopback_pcm *capt,
527597603d6SJaroslav Kysela 			  unsigned int bytes)
528597603d6SJaroslav Kysela {
529597603d6SJaroslav Kysela 	struct snd_pcm_runtime *runtime = play->substream->runtime;
53020d9a26dSJaroslav Kysela 	char *src = runtime->dma_area;
531597603d6SJaroslav Kysela 	char *dst = capt->substream->runtime->dma_area;
532597603d6SJaroslav Kysela 	unsigned int src_off = play->buf_pos;
533597603d6SJaroslav Kysela 	unsigned int dst_off = capt->buf_pos;
534597603d6SJaroslav Kysela 	unsigned int clear_bytes = 0;
535597603d6SJaroslav Kysela 
536597603d6SJaroslav Kysela 	/* check if playback is draining, trim the capture copy size
537597603d6SJaroslav Kysela 	 * when our pointer is at the end of playback ring buffer */
538*f7efa9b8STakashi Iwai 	if (runtime->state == SNDRV_PCM_STATE_DRAINING &&
539597603d6SJaroslav Kysela 	    snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
540597603d6SJaroslav Kysela 	    	snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
541597603d6SJaroslav Kysela 		appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
542597603d6SJaroslav Kysela 		appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
543597603d6SJaroslav Kysela 		appl_ptr1 += play->buf_pos / play->pcm_salign;
544597603d6SJaroslav Kysela 		if (appl_ptr < appl_ptr1)
545597603d6SJaroslav Kysela 			appl_ptr1 -= runtime->buffer_size;
546597603d6SJaroslav Kysela 		diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
547597603d6SJaroslav Kysela 		if (diff < bytes) {
548597603d6SJaroslav Kysela 			clear_bytes = bytes - diff;
549597603d6SJaroslav Kysela 			bytes = diff;
550597603d6SJaroslav Kysela 		}
551597603d6SJaroslav Kysela 	}
552597603d6SJaroslav Kysela 
553597603d6SJaroslav Kysela 	for (;;) {
554597603d6SJaroslav Kysela 		unsigned int size = bytes;
555597603d6SJaroslav Kysela 		if (src_off + size > play->pcm_buffer_size)
556597603d6SJaroslav Kysela 			size = play->pcm_buffer_size - src_off;
557597603d6SJaroslav Kysela 		if (dst_off + size > capt->pcm_buffer_size)
558597603d6SJaroslav Kysela 			size = capt->pcm_buffer_size - dst_off;
559597603d6SJaroslav Kysela 		memcpy(dst + dst_off, src + src_off, size);
560597603d6SJaroslav Kysela 		capt->silent_size = 0;
561597603d6SJaroslav Kysela 		bytes -= size;
562597603d6SJaroslav Kysela 		if (!bytes)
563597603d6SJaroslav Kysela 			break;
564597603d6SJaroslav Kysela 		src_off = (src_off + size) % play->pcm_buffer_size;
565597603d6SJaroslav Kysela 		dst_off = (dst_off + size) % capt->pcm_buffer_size;
566597603d6SJaroslav Kysela 	}
567597603d6SJaroslav Kysela 
56820d9a26dSJaroslav Kysela 	if (clear_bytes > 0) {
569597603d6SJaroslav Kysela 		clear_capture_buf(capt, clear_bytes);
57020d9a26dSJaroslav Kysela 		capt->silent_size = 0;
57120d9a26dSJaroslav Kysela 	}
572597603d6SJaroslav Kysela }
573597603d6SJaroslav Kysela 
bytepos_delta(struct loopback_pcm * dpcm,unsigned int jiffies_delta)574b012513cSJaroslav Kysela static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
575b012513cSJaroslav Kysela 					 unsigned int jiffies_delta)
576597603d6SJaroslav Kysela {
577597603d6SJaroslav Kysela 	unsigned long last_pos;
578b012513cSJaroslav Kysela 	unsigned int delta;
579597603d6SJaroslav Kysela 
580597603d6SJaroslav Kysela 	last_pos = byte_pos(dpcm, dpcm->irq_pos);
581b012513cSJaroslav Kysela 	dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
582b012513cSJaroslav Kysela 	delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
583b012513cSJaroslav Kysela 	if (delta >= dpcm->last_drift)
584b012513cSJaroslav Kysela 		delta -= dpcm->last_drift;
585b012513cSJaroslav Kysela 	dpcm->last_drift = 0;
586597603d6SJaroslav Kysela 	if (dpcm->irq_pos >= dpcm->period_size_frac) {
587597603d6SJaroslav Kysela 		dpcm->irq_pos %= dpcm->period_size_frac;
588597603d6SJaroslav Kysela 		dpcm->period_update_pending = 1;
589597603d6SJaroslav Kysela 	}
590b012513cSJaroslav Kysela 	return delta;
591b012513cSJaroslav Kysela }
592b012513cSJaroslav Kysela 
bytepos_finish(struct loopback_pcm * dpcm,unsigned int delta)593b012513cSJaroslav Kysela static inline void bytepos_finish(struct loopback_pcm *dpcm,
594b012513cSJaroslav Kysela 				  unsigned int delta)
595b012513cSJaroslav Kysela {
596b012513cSJaroslav Kysela 	dpcm->buf_pos += delta;
597b012513cSJaroslav Kysela 	dpcm->buf_pos %= dpcm->pcm_buffer_size;
598597603d6SJaroslav Kysela }
599597603d6SJaroslav Kysela 
600999fc9f6STakashi Iwai /* call in cable->lock */
loopback_jiffies_timer_pos_update(struct loopback_cable * cable)6018e3bf7cdSTimo Wischer static unsigned int loopback_jiffies_timer_pos_update
6028e3bf7cdSTimo Wischer 		(struct loopback_cable *cable)
603597603d6SJaroslav Kysela {
604597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm_play =
605597603d6SJaroslav Kysela 			cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
606597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm_capt =
607597603d6SJaroslav Kysela 			cable->streams[SNDRV_PCM_STREAM_CAPTURE];
6083e48940aSPattara Teerapong 	unsigned long delta_play = 0, delta_capt = 0, cur_jiffies;
609b012513cSJaroslav Kysela 	unsigned int running, count1, count2;
610597603d6SJaroslav Kysela 
6113e48940aSPattara Teerapong 	cur_jiffies = jiffies;
6125de9e45fSJaroslav Kysela 	running = cable->running ^ cable->pause;
613dd04bb12SJaroslav Kysela 	if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
6143e48940aSPattara Teerapong 		delta_play = cur_jiffies - dpcm_play->last_jiffies;
615597603d6SJaroslav Kysela 		dpcm_play->last_jiffies += delta_play;
616597603d6SJaroslav Kysela 	}
617597603d6SJaroslav Kysela 
618dd04bb12SJaroslav Kysela 	if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
6193e48940aSPattara Teerapong 		delta_capt = cur_jiffies - dpcm_capt->last_jiffies;
620597603d6SJaroslav Kysela 		dpcm_capt->last_jiffies += delta_capt;
621597603d6SJaroslav Kysela 	}
622597603d6SJaroslav Kysela 
62398d21df4STakashi Iwai 	if (delta_play == 0 && delta_capt == 0)
62498d21df4STakashi Iwai 		goto unlock;
625597603d6SJaroslav Kysela 
626597603d6SJaroslav Kysela 	if (delta_play > delta_capt) {
627b012513cSJaroslav Kysela 		count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
628b012513cSJaroslav Kysela 		bytepos_finish(dpcm_play, count1);
629597603d6SJaroslav Kysela 		delta_play = delta_capt;
630597603d6SJaroslav Kysela 	} else if (delta_play < delta_capt) {
631b012513cSJaroslav Kysela 		count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
632b012513cSJaroslav Kysela 		clear_capture_buf(dpcm_capt, count1);
633b012513cSJaroslav Kysela 		bytepos_finish(dpcm_capt, count1);
634597603d6SJaroslav Kysela 		delta_capt = delta_play;
635597603d6SJaroslav Kysela 	}
636597603d6SJaroslav Kysela 
63798d21df4STakashi Iwai 	if (delta_play == 0 && delta_capt == 0)
63898d21df4STakashi Iwai 		goto unlock;
63998d21df4STakashi Iwai 
640597603d6SJaroslav Kysela 	/* note delta_capt == delta_play at this moment */
641b012513cSJaroslav Kysela 	count1 = bytepos_delta(dpcm_play, delta_play);
642b012513cSJaroslav Kysela 	count2 = bytepos_delta(dpcm_capt, delta_capt);
643b012513cSJaroslav Kysela 	if (count1 < count2) {
644b012513cSJaroslav Kysela 		dpcm_capt->last_drift = count2 - count1;
645b012513cSJaroslav Kysela 		count1 = count2;
646b012513cSJaroslav Kysela 	} else if (count1 > count2) {
647b012513cSJaroslav Kysela 		dpcm_play->last_drift = count1 - count2;
648b012513cSJaroslav Kysela 	}
649b012513cSJaroslav Kysela 	copy_play_buf(dpcm_play, dpcm_capt, count1);
650b012513cSJaroslav Kysela 	bytepos_finish(dpcm_play, count1);
651b012513cSJaroslav Kysela 	bytepos_finish(dpcm_capt, count1);
65298d21df4STakashi Iwai  unlock:
653dd04bb12SJaroslav Kysela 	return running;
654597603d6SJaroslav Kysela }
655597603d6SJaroslav Kysela 
loopback_jiffies_timer_function(struct timer_list * t)6568e3bf7cdSTimo Wischer static void loopback_jiffies_timer_function(struct timer_list *t)
657597603d6SJaroslav Kysela {
658bc47ba90SKees Cook 	struct loopback_pcm *dpcm = from_timer(dpcm, t, timer);
659999fc9f6STakashi Iwai 	unsigned long flags;
660597603d6SJaroslav Kysela 
661999fc9f6STakashi Iwai 	spin_lock_irqsave(&dpcm->cable->lock, flags);
6628e3bf7cdSTimo Wischer 	if (loopback_jiffies_timer_pos_update(dpcm->cable) &
6638e3bf7cdSTimo Wischer 			(1 << dpcm->substream->stream)) {
6648e3bf7cdSTimo Wischer 		loopback_jiffies_timer_start(dpcm);
665597603d6SJaroslav Kysela 		if (dpcm->period_update_pending) {
666597603d6SJaroslav Kysela 			dpcm->period_update_pending = 0;
667999fc9f6STakashi Iwai 			spin_unlock_irqrestore(&dpcm->cable->lock, flags);
668999fc9f6STakashi Iwai 			/* need to unlock before calling below */
669597603d6SJaroslav Kysela 			snd_pcm_period_elapsed(dpcm->substream);
670999fc9f6STakashi Iwai 			return;
671597603d6SJaroslav Kysela 		}
672597603d6SJaroslav Kysela 	}
673999fc9f6STakashi Iwai 	spin_unlock_irqrestore(&dpcm->cable->lock, flags);
674dd04bb12SJaroslav Kysela }
675597603d6SJaroslav Kysela 
67626c53379STimo Wischer /* call in cable->lock */
loopback_snd_timer_check_resolution(struct snd_pcm_runtime * runtime,unsigned long resolution)67726c53379STimo Wischer static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime,
67826c53379STimo Wischer 					       unsigned long resolution)
67926c53379STimo Wischer {
68026c53379STimo Wischer 	if (resolution != runtime->timer_resolution) {
68126c53379STimo Wischer 		struct loopback_pcm *dpcm = runtime->private_data;
68226c53379STimo Wischer 		struct loopback_cable *cable = dpcm->cable;
68326c53379STimo Wischer 		/* Worst case estimation of possible values for resolution
68426c53379STimo Wischer 		 * resolution <= (512 * 1024) frames / 8kHz in nsec
68526c53379STimo Wischer 		 * resolution <= 65.536.000.000 nsec
68626c53379STimo Wischer 		 *
68726c53379STimo Wischer 		 * period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz +
68826c53379STimo Wischer 		 *  500.000
68926c53379STimo Wischer 		 * period_size <= 12.582.912.000.000  <64bit
69026c53379STimo Wischer 		 *  / 1.000.000 usec/sec
69126c53379STimo Wischer 		 */
69226c53379STimo Wischer 		snd_pcm_uframes_t period_size_usec =
69326c53379STimo Wischer 				resolution / 1000 * runtime->rate;
69426c53379STimo Wischer 		/* round to nearest sample rate */
69526c53379STimo Wischer 		snd_pcm_uframes_t period_size =
69626c53379STimo Wischer 				(period_size_usec + 500 * 1000) / (1000 * 1000);
69726c53379STimo Wischer 
69826c53379STimo Wischer 		pcm_err(dpcm->substream->pcm,
69926c53379STimo Wischer 			"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.",
70026c53379STimo Wischer 			runtime->period_size, resolution, period_size,
70126c53379STimo Wischer 			cable->snd_timer.id.card,
70226c53379STimo Wischer 			cable->snd_timer.id.device,
70326c53379STimo Wischer 			cable->snd_timer.id.subdevice,
70426c53379STimo Wischer 			period_size);
70526c53379STimo Wischer 		return -EINVAL;
70626c53379STimo Wischer 	}
70726c53379STimo Wischer 	return 0;
70826c53379STimo Wischer }
70926c53379STimo Wischer 
loopback_snd_timer_period_elapsed(struct loopback_cable * cable,int event,unsigned long resolution)71026c53379STimo Wischer static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
71126c53379STimo Wischer 					      int event,
71226c53379STimo Wischer 					      unsigned long resolution)
71326c53379STimo Wischer {
71426c53379STimo Wischer 	struct loopback_pcm *dpcm_play, *dpcm_capt;
71526c53379STimo Wischer 	struct snd_pcm_substream *substream_play, *substream_capt;
71626c53379STimo Wischer 	struct snd_pcm_runtime *valid_runtime;
71726c53379STimo Wischer 	unsigned int running, elapsed_bytes;
71826c53379STimo Wischer 	unsigned long flags;
71926c53379STimo Wischer 
72026c53379STimo Wischer 	spin_lock_irqsave(&cable->lock, flags);
72126c53379STimo Wischer 	running = cable->running ^ cable->pause;
72226c53379STimo Wischer 	/* no need to do anything if no stream is running */
72326c53379STimo Wischer 	if (!running) {
72426c53379STimo Wischer 		spin_unlock_irqrestore(&cable->lock, flags);
72526c53379STimo Wischer 		return;
72626c53379STimo Wischer 	}
72726c53379STimo Wischer 
72826c53379STimo Wischer 	dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
72926c53379STimo Wischer 	dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
73026c53379STimo Wischer 
73126c53379STimo Wischer 	if (event == SNDRV_TIMER_EVENT_MSTOP) {
73226c53379STimo Wischer 		if (!dpcm_play ||
733*f7efa9b8STakashi Iwai 		    dpcm_play->substream->runtime->state !=
73426c53379STimo Wischer 				SNDRV_PCM_STATE_DRAINING) {
73526c53379STimo Wischer 			spin_unlock_irqrestore(&cable->lock, flags);
73626c53379STimo Wischer 			return;
73726c53379STimo Wischer 		}
73826c53379STimo Wischer 	}
73926c53379STimo Wischer 
7405061bb70SAndrew Gabbasov 	substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
7415061bb70SAndrew Gabbasov 			dpcm_play->substream : NULL;
7425061bb70SAndrew Gabbasov 	substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
7435061bb70SAndrew Gabbasov 			dpcm_capt->substream : NULL;
74426c53379STimo Wischer 	valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
74526c53379STimo Wischer 				dpcm_play->substream->runtime :
74626c53379STimo Wischer 				dpcm_capt->substream->runtime;
74726c53379STimo Wischer 
74826c53379STimo Wischer 	/* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
74926c53379STimo Wischer 	if (event == SNDRV_TIMER_EVENT_TICK) {
75026c53379STimo Wischer 		/* The hardware rules guarantee that playback and capture period
75126c53379STimo Wischer 		 * are the same. Therefore only one device has to be checked
75226c53379STimo Wischer 		 * here.
75326c53379STimo Wischer 		 */
75426c53379STimo Wischer 		if (loopback_snd_timer_check_resolution(valid_runtime,
75526c53379STimo Wischer 							resolution) < 0) {
75626c53379STimo Wischer 			spin_unlock_irqrestore(&cable->lock, flags);
75726c53379STimo Wischer 			if (substream_play)
75826c53379STimo Wischer 				snd_pcm_stop_xrun(substream_play);
75926c53379STimo Wischer 			if (substream_capt)
76026c53379STimo Wischer 				snd_pcm_stop_xrun(substream_capt);
76126c53379STimo Wischer 			return;
76226c53379STimo Wischer 		}
76326c53379STimo Wischer 	}
76426c53379STimo Wischer 
76526c53379STimo Wischer 	elapsed_bytes = frames_to_bytes(valid_runtime,
76626c53379STimo Wischer 					valid_runtime->period_size);
76726c53379STimo Wischer 	/* The same timer interrupt is used for playback and capture device */
76826c53379STimo Wischer 	if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
76926c53379STimo Wischer 	    (running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
77026c53379STimo Wischer 		copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
77126c53379STimo Wischer 		bytepos_finish(dpcm_play, elapsed_bytes);
77226c53379STimo Wischer 		bytepos_finish(dpcm_capt, elapsed_bytes);
77326c53379STimo Wischer 	} else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
77426c53379STimo Wischer 		bytepos_finish(dpcm_play, elapsed_bytes);
77526c53379STimo Wischer 	} else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
77626c53379STimo Wischer 		clear_capture_buf(dpcm_capt, elapsed_bytes);
77726c53379STimo Wischer 		bytepos_finish(dpcm_capt, elapsed_bytes);
77826c53379STimo Wischer 	}
77926c53379STimo Wischer 	spin_unlock_irqrestore(&cable->lock, flags);
78026c53379STimo Wischer 
78126c53379STimo Wischer 	if (substream_play)
78226c53379STimo Wischer 		snd_pcm_period_elapsed(substream_play);
78326c53379STimo Wischer 	if (substream_capt)
78426c53379STimo Wischer 		snd_pcm_period_elapsed(substream_capt);
78526c53379STimo Wischer }
78626c53379STimo Wischer 
loopback_snd_timer_function(struct snd_timer_instance * timeri,unsigned long resolution,unsigned long ticks)78726c53379STimo Wischer static void loopback_snd_timer_function(struct snd_timer_instance *timeri,
78826c53379STimo Wischer 					unsigned long resolution,
78926c53379STimo Wischer 					unsigned long ticks)
79026c53379STimo Wischer {
79126c53379STimo Wischer 	struct loopback_cable *cable = timeri->callback_data;
79226c53379STimo Wischer 
79326c53379STimo Wischer 	loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK,
79426c53379STimo Wischer 					  resolution);
79526c53379STimo Wischer }
79626c53379STimo Wischer 
loopback_snd_timer_work(struct work_struct * work)7976053a712STakashi Iwai static void loopback_snd_timer_work(struct work_struct *work)
79826c53379STimo Wischer {
7996053a712STakashi Iwai 	struct loopback_cable *cable;
80026c53379STimo Wischer 
8016053a712STakashi Iwai 	cable = container_of(work, struct loopback_cable, snd_timer.event_work);
80226c53379STimo Wischer 	loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0);
80326c53379STimo Wischer }
80426c53379STimo Wischer 
loopback_snd_timer_event(struct snd_timer_instance * timeri,int event,struct timespec64 * tstamp,unsigned long resolution)80526c53379STimo Wischer static void loopback_snd_timer_event(struct snd_timer_instance *timeri,
80626c53379STimo Wischer 				     int event,
807fcae40c9SBaolin Wang 				     struct timespec64 *tstamp,
80826c53379STimo Wischer 				     unsigned long resolution)
80926c53379STimo Wischer {
81026c53379STimo Wischer 	/* Do not lock cable->lock here because timer->lock is already hold.
81126c53379STimo Wischer 	 * There are other functions which first lock cable->lock and than
81226c53379STimo Wischer 	 * timer->lock e.g.
81326c53379STimo Wischer 	 * loopback_trigger()
81426c53379STimo Wischer 	 * spin_lock(&cable->lock)
81526c53379STimo Wischer 	 * loopback_snd_timer_start()
81626c53379STimo Wischer 	 * snd_timer_start()
81726c53379STimo Wischer 	 * spin_lock(&timer->lock)
81826c53379STimo Wischer 	 * Therefore when using the oposit order of locks here it could result
81926c53379STimo Wischer 	 * in a deadlock.
82026c53379STimo Wischer 	 */
82126c53379STimo Wischer 
82226c53379STimo Wischer 	if (event == SNDRV_TIMER_EVENT_MSTOP) {
82326c53379STimo Wischer 		struct loopback_cable *cable = timeri->callback_data;
82426c53379STimo Wischer 
82526c53379STimo Wischer 		/* sound card of the timer was stopped. Therefore there will not
82626c53379STimo Wischer 		 * be any further timer callbacks. Due to this forward audio
82726c53379STimo Wischer 		 * data from here if in draining state. When still in running
82826c53379STimo Wischer 		 * state the streaming will be aborted by the usual timeout. It
82926c53379STimo Wischer 		 * should not be aborted here because may be the timer sound
83026c53379STimo Wischer 		 * card does only a recovery and the timer is back soon.
8316053a712STakashi Iwai 		 * This work triggers loopback_snd_timer_work()
83226c53379STimo Wischer 		 */
8336053a712STakashi Iwai 		schedule_work(&cable->snd_timer.event_work);
83426c53379STimo Wischer 	}
83526c53379STimo Wischer }
83626c53379STimo Wischer 
loopback_jiffies_timer_dpcm_info(struct loopback_pcm * dpcm,struct snd_info_buffer * buffer)837133f3759STimo Wischer static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm,
838133f3759STimo Wischer 					     struct snd_info_buffer *buffer)
839133f3759STimo Wischer {
840133f3759STimo Wischer 	snd_iprintf(buffer, "    update_pending:\t%u\n",
841133f3759STimo Wischer 		    dpcm->period_update_pending);
842133f3759STimo Wischer 	snd_iprintf(buffer, "    irq_pos:\t\t%u\n", dpcm->irq_pos);
843133f3759STimo Wischer 	snd_iprintf(buffer, "    period_frac:\t%u\n", dpcm->period_size_frac);
844133f3759STimo Wischer 	snd_iprintf(buffer, "    last_jiffies:\t%lu (%lu)\n",
845133f3759STimo Wischer 		    dpcm->last_jiffies, jiffies);
846133f3759STimo Wischer 	snd_iprintf(buffer, "    timer_expires:\t%lu\n", dpcm->timer.expires);
847133f3759STimo Wischer }
848133f3759STimo Wischer 
loopback_snd_timer_dpcm_info(struct loopback_pcm * dpcm,struct snd_info_buffer * buffer)84926c53379STimo Wischer static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm,
85026c53379STimo Wischer 					 struct snd_info_buffer *buffer)
85126c53379STimo Wischer {
85226c53379STimo Wischer 	struct loopback_cable *cable = dpcm->cable;
85326c53379STimo Wischer 
85426c53379STimo Wischer 	snd_iprintf(buffer, "    sound timer:\thw:%d,%d,%d\n",
85526c53379STimo Wischer 		    cable->snd_timer.id.card,
85626c53379STimo Wischer 		    cable->snd_timer.id.device,
85726c53379STimo Wischer 		    cable->snd_timer.id.subdevice);
85826c53379STimo Wischer 	snd_iprintf(buffer, "    timer open:\t\t%s\n",
85926c53379STimo Wischer 		    (cable->snd_timer.stream == SNDRV_PCM_STREAM_CAPTURE) ?
86026c53379STimo Wischer 			    "capture" : "playback");
86126c53379STimo Wischer }
86226c53379STimo Wischer 
loopback_pointer(struct snd_pcm_substream * substream)863597603d6SJaroslav Kysela static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
864597603d6SJaroslav Kysela {
865597603d6SJaroslav Kysela 	struct snd_pcm_runtime *runtime = substream->runtime;
866597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm = runtime->private_data;
867999fc9f6STakashi Iwai 	snd_pcm_uframes_t pos;
868597603d6SJaroslav Kysela 
869999fc9f6STakashi Iwai 	spin_lock(&dpcm->cable->lock);
870133f3759STimo Wischer 	if (dpcm->cable->ops->pos_update)
871133f3759STimo Wischer 		dpcm->cable->ops->pos_update(dpcm->cable);
872999fc9f6STakashi Iwai 	pos = dpcm->buf_pos;
873999fc9f6STakashi Iwai 	spin_unlock(&dpcm->cable->lock);
874999fc9f6STakashi Iwai 	return bytes_to_frames(runtime, pos);
875597603d6SJaroslav Kysela }
876597603d6SJaroslav Kysela 
877b6c0b715SBhumika Goyal static const struct snd_pcm_hardware loopback_pcm_hardware =
878597603d6SJaroslav Kysela {
879597603d6SJaroslav Kysela 	.info =		(SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
880edac8943STakashi Iwai 			 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
881edac8943STakashi Iwai 			 SNDRV_PCM_INFO_RESUME),
882597603d6SJaroslav Kysela 	.formats =	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
88350e09084STimo Wischer 			 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
88450e09084STimo Wischer 			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
885597603d6SJaroslav Kysela 			 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
886597603d6SJaroslav Kysela 			 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
887597603d6SJaroslav Kysela 	.rates =	SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
888597603d6SJaroslav Kysela 	.rate_min =		8000,
889597603d6SJaroslav Kysela 	.rate_max =		192000,
890597603d6SJaroslav Kysela 	.channels_min =		1,
891597603d6SJaroslav Kysela 	.channels_max =		32,
892597603d6SJaroslav Kysela 	.buffer_bytes_max =	2 * 1024 * 1024,
893597603d6SJaroslav Kysela 	.period_bytes_min =	64,
8940db71023SJaroslav Kysela 	/* note check overflow in frac_pos() using pcm_rate_shift before
8950db71023SJaroslav Kysela 	   changing period_bytes_max value */
8960db71023SJaroslav Kysela 	.period_bytes_max =	1024 * 1024,
897597603d6SJaroslav Kysela 	.periods_min =		1,
898597603d6SJaroslav Kysela 	.periods_max =		1024,
899597603d6SJaroslav Kysela 	.fifo_size =		0,
900597603d6SJaroslav Kysela };
901597603d6SJaroslav Kysela 
loopback_runtime_free(struct snd_pcm_runtime * runtime)902597603d6SJaroslav Kysela static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
903597603d6SJaroslav Kysela {
904597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm = runtime->private_data;
905597603d6SJaroslav Kysela 	kfree(dpcm);
906597603d6SJaroslav Kysela }
907597603d6SJaroslav Kysela 
loopback_hw_free(struct snd_pcm_substream * substream)908597603d6SJaroslav Kysela static int loopback_hw_free(struct snd_pcm_substream *substream)
909597603d6SJaroslav Kysela {
910597603d6SJaroslav Kysela 	struct snd_pcm_runtime *runtime = substream->runtime;
911597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm = runtime->private_data;
912597603d6SJaroslav Kysela 	struct loopback_cable *cable = dpcm->cable;
913597603d6SJaroslav Kysela 
914597603d6SJaroslav Kysela 	mutex_lock(&dpcm->loopback->cable_lock);
915597603d6SJaroslav Kysela 	cable->valid &= ~(1 << substream->stream);
916597603d6SJaroslav Kysela 	mutex_unlock(&dpcm->loopback->cable_lock);
91739729889STakashi Iwai 	return 0;
918597603d6SJaroslav Kysela }
919597603d6SJaroslav Kysela 
get_cable_index(struct snd_pcm_substream * substream)920597603d6SJaroslav Kysela static unsigned int get_cable_index(struct snd_pcm_substream *substream)
921597603d6SJaroslav Kysela {
922597603d6SJaroslav Kysela 	if (!substream->pcm->device)
923597603d6SJaroslav Kysela 		return substream->stream;
924597603d6SJaroslav Kysela 	else
925597603d6SJaroslav Kysela 		return !substream->stream;
926597603d6SJaroslav Kysela }
927597603d6SJaroslav Kysela 
rule_format(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)928b1c73fc8SJaroslav Kysela static int rule_format(struct snd_pcm_hw_params *params,
929b1c73fc8SJaroslav Kysela 		       struct snd_pcm_hw_rule *rule)
930b1c73fc8SJaroslav Kysela {
931898dfe46STakashi Iwai 	struct loopback_pcm *dpcm = rule->private;
932898dfe46STakashi Iwai 	struct loopback_cable *cable = dpcm->cable;
933b088b53eSTakashi Iwai 	struct snd_mask m;
934b1c73fc8SJaroslav Kysela 
935b088b53eSTakashi Iwai 	snd_mask_none(&m);
936898dfe46STakashi Iwai 	mutex_lock(&dpcm->loopback->cable_lock);
937898dfe46STakashi Iwai 	m.bits[0] = (u_int32_t)cable->hw.formats;
938898dfe46STakashi Iwai 	m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
939898dfe46STakashi Iwai 	mutex_unlock(&dpcm->loopback->cable_lock);
940b088b53eSTakashi Iwai 	return snd_mask_refine(hw_param_mask(params, rule->var), &m);
941b1c73fc8SJaroslav Kysela }
942b1c73fc8SJaroslav Kysela 
rule_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)943b1c73fc8SJaroslav Kysela static int rule_rate(struct snd_pcm_hw_params *params,
944b1c73fc8SJaroslav Kysela 		     struct snd_pcm_hw_rule *rule)
945b1c73fc8SJaroslav Kysela {
946898dfe46STakashi Iwai 	struct loopback_pcm *dpcm = rule->private;
947898dfe46STakashi Iwai 	struct loopback_cable *cable = dpcm->cable;
948b1c73fc8SJaroslav Kysela 	struct snd_interval t;
949b1c73fc8SJaroslav Kysela 
950898dfe46STakashi Iwai 	mutex_lock(&dpcm->loopback->cable_lock);
951898dfe46STakashi Iwai 	t.min = cable->hw.rate_min;
952898dfe46STakashi Iwai 	t.max = cable->hw.rate_max;
953898dfe46STakashi Iwai 	mutex_unlock(&dpcm->loopback->cable_lock);
954b1c73fc8SJaroslav Kysela         t.openmin = t.openmax = 0;
955b1c73fc8SJaroslav Kysela         t.integer = 0;
956b1c73fc8SJaroslav Kysela 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
957b1c73fc8SJaroslav Kysela }
958b1c73fc8SJaroslav Kysela 
rule_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)959b1c73fc8SJaroslav Kysela static int rule_channels(struct snd_pcm_hw_params *params,
960b1c73fc8SJaroslav Kysela 			 struct snd_pcm_hw_rule *rule)
961b1c73fc8SJaroslav Kysela {
962898dfe46STakashi Iwai 	struct loopback_pcm *dpcm = rule->private;
963898dfe46STakashi Iwai 	struct loopback_cable *cable = dpcm->cable;
964b1c73fc8SJaroslav Kysela 	struct snd_interval t;
965b1c73fc8SJaroslav Kysela 
966898dfe46STakashi Iwai 	mutex_lock(&dpcm->loopback->cable_lock);
967898dfe46STakashi Iwai 	t.min = cable->hw.channels_min;
968898dfe46STakashi Iwai 	t.max = cable->hw.channels_max;
969898dfe46STakashi Iwai 	mutex_unlock(&dpcm->loopback->cable_lock);
970b1c73fc8SJaroslav Kysela         t.openmin = t.openmax = 0;
971b1c73fc8SJaroslav Kysela         t.integer = 0;
972b1c73fc8SJaroslav Kysela 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
973b1c73fc8SJaroslav Kysela }
974b1c73fc8SJaroslav Kysela 
rule_period_bytes(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)97526c53379STimo Wischer static int rule_period_bytes(struct snd_pcm_hw_params *params,
97626c53379STimo Wischer 			     struct snd_pcm_hw_rule *rule)
97726c53379STimo Wischer {
97826c53379STimo Wischer 	struct loopback_pcm *dpcm = rule->private;
97926c53379STimo Wischer 	struct loopback_cable *cable = dpcm->cable;
98026c53379STimo Wischer 	struct snd_interval t;
98126c53379STimo Wischer 
98226c53379STimo Wischer 	mutex_lock(&dpcm->loopback->cable_lock);
98326c53379STimo Wischer 	t.min = cable->hw.period_bytes_min;
98426c53379STimo Wischer 	t.max = cable->hw.period_bytes_max;
98526c53379STimo Wischer 	mutex_unlock(&dpcm->loopback->cable_lock);
98626c53379STimo Wischer 	t.openmin = 0;
98726c53379STimo Wischer 	t.openmax = 0;
98826c53379STimo Wischer 	t.integer = 0;
98926c53379STimo Wischer 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
99026c53379STimo Wischer }
99126c53379STimo Wischer 
free_cable(struct snd_pcm_substream * substream)9929685347aSTakashi Iwai static void free_cable(struct snd_pcm_substream *substream)
9939685347aSTakashi Iwai {
9949685347aSTakashi Iwai 	struct loopback *loopback = substream->private_data;
9959685347aSTakashi Iwai 	int dev = get_cable_index(substream);
9969685347aSTakashi Iwai 	struct loopback_cable *cable;
9979685347aSTakashi Iwai 
9989685347aSTakashi Iwai 	cable = loopback->cables[substream->number][dev];
9999685347aSTakashi Iwai 	if (!cable)
10009685347aSTakashi Iwai 		return;
10019685347aSTakashi Iwai 	if (cable->streams[!substream->stream]) {
10029685347aSTakashi Iwai 		/* other stream is still alive */
10038e6b1a72STakashi Iwai 		spin_lock_irq(&cable->lock);
10049685347aSTakashi Iwai 		cable->streams[substream->stream] = NULL;
10058e6b1a72STakashi Iwai 		spin_unlock_irq(&cable->lock);
10069685347aSTakashi Iwai 	} else {
1007133f3759STimo Wischer 		struct loopback_pcm *dpcm = substream->runtime->private_data;
1008133f3759STimo Wischer 
1009133f3759STimo Wischer 		if (cable->ops && cable->ops->close_cable && dpcm)
1010133f3759STimo Wischer 			cable->ops->close_cable(dpcm);
10119685347aSTakashi Iwai 		/* free the cable */
10129685347aSTakashi Iwai 		loopback->cables[substream->number][dev] = NULL;
10139685347aSTakashi Iwai 		kfree(cable);
10149685347aSTakashi Iwai 	}
10159685347aSTakashi Iwai }
10169685347aSTakashi Iwai 
loopback_jiffies_timer_open(struct loopback_pcm * dpcm)1017133f3759STimo Wischer static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm)
1018133f3759STimo Wischer {
10198e3bf7cdSTimo Wischer 	timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0);
1020133f3759STimo Wischer 
1021133f3759STimo Wischer 	return 0;
1022133f3759STimo Wischer }
1023133f3759STimo Wischer 
1024e714fa93SRikard Falkeborn static const struct loopback_ops loopback_jiffies_timer_ops = {
1025133f3759STimo Wischer 	.open = loopback_jiffies_timer_open,
10268e3bf7cdSTimo Wischer 	.start = loopback_jiffies_timer_start,
10278e3bf7cdSTimo Wischer 	.stop = loopback_jiffies_timer_stop,
10288e3bf7cdSTimo Wischer 	.stop_sync = loopback_jiffies_timer_stop_sync,
10298e3bf7cdSTimo Wischer 	.close_substream = loopback_jiffies_timer_stop_sync,
10308e3bf7cdSTimo Wischer 	.pos_update = loopback_jiffies_timer_pos_update,
1031133f3759STimo Wischer 	.dpcm_info = loopback_jiffies_timer_dpcm_info,
1032133f3759STimo Wischer };
1033133f3759STimo Wischer 
loopback_parse_timer_id(const char * str,struct snd_timer_id * tid)103426c53379STimo Wischer static int loopback_parse_timer_id(const char *str,
103526c53379STimo Wischer 				   struct snd_timer_id *tid)
103626c53379STimo Wischer {
103726c53379STimo Wischer 	/* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */
103826c53379STimo Wischer 	const char * const sep_dev = ".,";
103926c53379STimo Wischer 	const char * const sep_pref = ":";
104026c53379STimo Wischer 	const char *name = str;
104126c53379STimo Wischer 	char *sep, save = '\0';
104226c53379STimo Wischer 	int card_idx = 0, dev = 0, subdev = 0;
104326c53379STimo Wischer 	int err;
104426c53379STimo Wischer 
104526c53379STimo Wischer 	sep = strpbrk(str, sep_pref);
104626c53379STimo Wischer 	if (sep)
104726c53379STimo Wischer 		name = sep + 1;
104826c53379STimo Wischer 	sep = strpbrk(name, sep_dev);
104926c53379STimo Wischer 	if (sep) {
105026c53379STimo Wischer 		save = *sep;
105126c53379STimo Wischer 		*sep = '\0';
105226c53379STimo Wischer 	}
105326c53379STimo Wischer 	err = kstrtoint(name, 0, &card_idx);
105426c53379STimo Wischer 	if (err == -EINVAL) {
105526c53379STimo Wischer 		/* Must be the name, not number */
105626c53379STimo Wischer 		for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) {
105726c53379STimo Wischer 			struct snd_card *card = snd_card_ref(card_idx);
105826c53379STimo Wischer 
105926c53379STimo Wischer 			if (card) {
106026c53379STimo Wischer 				if (!strcmp(card->id, name))
106126c53379STimo Wischer 					err = 0;
106226c53379STimo Wischer 				snd_card_unref(card);
106326c53379STimo Wischer 			}
106426c53379STimo Wischer 			if (!err)
106526c53379STimo Wischer 				break;
106626c53379STimo Wischer 		}
106726c53379STimo Wischer 	}
106826c53379STimo Wischer 	if (sep) {
106926c53379STimo Wischer 		*sep = save;
107026c53379STimo Wischer 		if (!err) {
107126c53379STimo Wischer 			char *sep2, save2 = '\0';
107226c53379STimo Wischer 
107326c53379STimo Wischer 			sep2 = strpbrk(sep + 1, sep_dev);
107426c53379STimo Wischer 			if (sep2) {
107526c53379STimo Wischer 				save2 = *sep2;
107626c53379STimo Wischer 				*sep2 = '\0';
107726c53379STimo Wischer 			}
107826c53379STimo Wischer 			err = kstrtoint(sep + 1, 0, &dev);
107926c53379STimo Wischer 			if (sep2) {
108026c53379STimo Wischer 				*sep2 = save2;
108126c53379STimo Wischer 				if (!err)
108226c53379STimo Wischer 					err = kstrtoint(sep2 + 1, 0, &subdev);
108326c53379STimo Wischer 			}
108426c53379STimo Wischer 		}
108526c53379STimo Wischer 	}
108626c53379STimo Wischer 	if (!err && tid) {
108726c53379STimo Wischer 		tid->card = card_idx;
108826c53379STimo Wischer 		tid->device = dev;
108926c53379STimo Wischer 		tid->subdevice = subdev;
109026c53379STimo Wischer 	}
109126c53379STimo Wischer 	return err;
109226c53379STimo Wischer }
109326c53379STimo Wischer 
109426c53379STimo Wischer /* call in loopback->cable_lock */
loopback_snd_timer_open(struct loopback_pcm * dpcm)109526c53379STimo Wischer static int loopback_snd_timer_open(struct loopback_pcm *dpcm)
109626c53379STimo Wischer {
109726c53379STimo Wischer 	int err = 0;
109826c53379STimo Wischer 	struct snd_timer_id tid = {
109926c53379STimo Wischer 		.dev_class = SNDRV_TIMER_CLASS_PCM,
110026c53379STimo Wischer 		.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION,
110126c53379STimo Wischer 	};
110226c53379STimo Wischer 	struct snd_timer_instance *timeri;
110326c53379STimo Wischer 	struct loopback_cable *cable = dpcm->cable;
110426c53379STimo Wischer 
110526c53379STimo Wischer 	/* check if timer was already opened. It is only opened once
110626c53379STimo Wischer 	 * per playback and capture subdevice (aka cable).
110726c53379STimo Wischer 	 */
110826c53379STimo Wischer 	if (cable->snd_timer.instance)
1109c037239cSAndrew Gabbasov 		goto exit;
111026c53379STimo Wischer 
111126c53379STimo Wischer 	err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid);
111226c53379STimo Wischer 	if (err < 0) {
111326c53379STimo Wischer 		pcm_err(dpcm->substream->pcm,
111426c53379STimo Wischer 			"Parsing timer source \'%s\' failed with %d",
111526c53379STimo Wischer 			dpcm->loopback->timer_source, err);
1116c037239cSAndrew Gabbasov 		goto exit;
111726c53379STimo Wischer 	}
111826c53379STimo Wischer 
111926c53379STimo Wischer 	cable->snd_timer.stream = dpcm->substream->stream;
112026c53379STimo Wischer 	cable->snd_timer.id = tid;
112126c53379STimo Wischer 
112226c53379STimo Wischer 	timeri = snd_timer_instance_new(dpcm->loopback->card->id);
112326c53379STimo Wischer 	if (!timeri) {
112426c53379STimo Wischer 		err = -ENOMEM;
1125c037239cSAndrew Gabbasov 		goto exit;
112626c53379STimo Wischer 	}
11276053a712STakashi Iwai 	/* The callback has to be called from another work. If
112826c53379STimo Wischer 	 * SNDRV_TIMER_IFLG_FAST is specified it will be called from the
112926c53379STimo Wischer 	 * snd_pcm_period_elapsed() call of the selected sound card.
113026c53379STimo Wischer 	 * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave().
113126c53379STimo Wischer 	 * Due to our callback loopback_snd_timer_function() also calls
113226c53379STimo Wischer 	 * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave().
113326c53379STimo Wischer 	 * This would end up in a dead lock.
113426c53379STimo Wischer 	 */
113526c53379STimo Wischer 	timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
113626c53379STimo Wischer 	timeri->callback = loopback_snd_timer_function;
113726c53379STimo Wischer 	timeri->callback_data = (void *)cable;
113826c53379STimo Wischer 	timeri->ccallback = loopback_snd_timer_event;
113926c53379STimo Wischer 
11406053a712STakashi Iwai 	/* initialise a work used for draining */
11416053a712STakashi Iwai 	INIT_WORK(&cable->snd_timer.event_work, loopback_snd_timer_work);
114226c53379STimo Wischer 
1143c037239cSAndrew Gabbasov 	/* The mutex loopback->cable_lock is kept locked.
1144c037239cSAndrew Gabbasov 	 * Therefore snd_timer_open() cannot be called a second time
1145c037239cSAndrew Gabbasov 	 * by the other device of the same cable.
114626c53379STimo Wischer 	 * Therefore the following issue cannot happen:
114726c53379STimo Wischer 	 * [proc1] Call loopback_timer_open() ->
114826c53379STimo Wischer 	 *	   Unlock cable->lock for snd_timer_close/open() call
114926c53379STimo Wischer 	 * [proc2] Call loopback_timer_open() -> snd_timer_open(),
115026c53379STimo Wischer 	 *	   snd_timer_start()
115126c53379STimo Wischer 	 * [proc1] Call snd_timer_open() and overwrite running timer
115226c53379STimo Wischer 	 *	   instance
115326c53379STimo Wischer 	 */
115426c53379STimo Wischer 	err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid);
115526c53379STimo Wischer 	if (err < 0) {
115626c53379STimo Wischer 		pcm_err(dpcm->substream->pcm,
115726c53379STimo Wischer 			"snd_timer_open (%d,%d,%d) failed with %d",
115826c53379STimo Wischer 			cable->snd_timer.id.card,
115926c53379STimo Wischer 			cable->snd_timer.id.device,
116026c53379STimo Wischer 			cable->snd_timer.id.subdevice,
116126c53379STimo Wischer 			err);
116226c53379STimo Wischer 		snd_timer_instance_free(timeri);
1163c037239cSAndrew Gabbasov 		goto exit;
116426c53379STimo Wischer 	}
116526c53379STimo Wischer 
116626c53379STimo Wischer 	cable->snd_timer.instance = timeri;
116726c53379STimo Wischer 
1168c037239cSAndrew Gabbasov exit:
116926c53379STimo Wischer 	return err;
117026c53379STimo Wischer }
117126c53379STimo Wischer 
117226c53379STimo Wischer /* stop_sync() is not required for sound timer because it does not need to be
117326c53379STimo Wischer  * restarted in loopback_prepare() on Xrun recovery
117426c53379STimo Wischer  */
1175e714fa93SRikard Falkeborn static const struct loopback_ops loopback_snd_timer_ops = {
117626c53379STimo Wischer 	.open = loopback_snd_timer_open,
117726c53379STimo Wischer 	.start = loopback_snd_timer_start,
117826c53379STimo Wischer 	.stop = loopback_snd_timer_stop,
117926c53379STimo Wischer 	.close_cable = loopback_snd_timer_close_cable,
118026c53379STimo Wischer 	.dpcm_info = loopback_snd_timer_dpcm_info,
118126c53379STimo Wischer };
118226c53379STimo Wischer 
loopback_open(struct snd_pcm_substream * substream)1183597603d6SJaroslav Kysela static int loopback_open(struct snd_pcm_substream *substream)
1184597603d6SJaroslav Kysela {
1185597603d6SJaroslav Kysela 	struct snd_pcm_runtime *runtime = substream->runtime;
1186597603d6SJaroslav Kysela 	struct loopback *loopback = substream->private_data;
1187597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm;
11889685347aSTakashi Iwai 	struct loopback_cable *cable = NULL;
1189597603d6SJaroslav Kysela 	int err = 0;
1190597603d6SJaroslav Kysela 	int dev = get_cable_index(substream);
1191597603d6SJaroslav Kysela 
1192597603d6SJaroslav Kysela 	mutex_lock(&loopback->cable_lock);
1193597603d6SJaroslav Kysela 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1194597603d6SJaroslav Kysela 	if (!dpcm) {
1195597603d6SJaroslav Kysela 		err = -ENOMEM;
1196597603d6SJaroslav Kysela 		goto unlock;
1197597603d6SJaroslav Kysela 	}
1198597603d6SJaroslav Kysela 	dpcm->loopback = loopback;
1199597603d6SJaroslav Kysela 	dpcm->substream = substream;
1200597603d6SJaroslav Kysela 
1201597603d6SJaroslav Kysela 	cable = loopback->cables[substream->number][dev];
1202597603d6SJaroslav Kysela 	if (!cable) {
1203597603d6SJaroslav Kysela 		cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1204597603d6SJaroslav Kysela 		if (!cable) {
1205597603d6SJaroslav Kysela 			err = -ENOMEM;
1206597603d6SJaroslav Kysela 			goto unlock;
1207597603d6SJaroslav Kysela 		}
1208597603d6SJaroslav Kysela 		spin_lock_init(&cable->lock);
1209597603d6SJaroslav Kysela 		cable->hw = loopback_pcm_hardware;
121026c53379STimo Wischer 		if (loopback->timer_source)
121126c53379STimo Wischer 			cable->ops = &loopback_snd_timer_ops;
121226c53379STimo Wischer 		else
1213133f3759STimo Wischer 			cable->ops = &loopback_jiffies_timer_ops;
1214597603d6SJaroslav Kysela 		loopback->cables[substream->number][dev] = cable;
1215597603d6SJaroslav Kysela 	}
1216597603d6SJaroslav Kysela 	dpcm->cable = cable;
1217133f3759STimo Wischer 	runtime->private_data = dpcm;
1218133f3759STimo Wischer 
1219133f3759STimo Wischer 	if (cable->ops->open) {
1220133f3759STimo Wischer 		err = cable->ops->open(dpcm);
1221133f3759STimo Wischer 		if (err < 0)
1222133f3759STimo Wischer 			goto unlock;
1223133f3759STimo Wischer 	}
1224597603d6SJaroslav Kysela 
1225597603d6SJaroslav Kysela 	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1226597603d6SJaroslav Kysela 
1227b1c73fc8SJaroslav Kysela 	/* use dynamic rules based on actual runtime->hw values */
1228b1c73fc8SJaroslav Kysela 	/* note that the default rules created in the PCM midlevel code */
1229b1c73fc8SJaroslav Kysela 	/* are cached -> they do not reflect the actual state */
1230b1c73fc8SJaroslav Kysela 	err = snd_pcm_hw_rule_add(runtime, 0,
1231b1c73fc8SJaroslav Kysela 				  SNDRV_PCM_HW_PARAM_FORMAT,
1232898dfe46STakashi Iwai 				  rule_format, dpcm,
1233b1c73fc8SJaroslav Kysela 				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
1234b1c73fc8SJaroslav Kysela 	if (err < 0)
1235b1c73fc8SJaroslav Kysela 		goto unlock;
1236b1c73fc8SJaroslav Kysela 	err = snd_pcm_hw_rule_add(runtime, 0,
1237b1c73fc8SJaroslav Kysela 				  SNDRV_PCM_HW_PARAM_RATE,
1238898dfe46STakashi Iwai 				  rule_rate, dpcm,
1239b1c73fc8SJaroslav Kysela 				  SNDRV_PCM_HW_PARAM_RATE, -1);
1240b1c73fc8SJaroslav Kysela 	if (err < 0)
1241b1c73fc8SJaroslav Kysela 		goto unlock;
1242b1c73fc8SJaroslav Kysela 	err = snd_pcm_hw_rule_add(runtime, 0,
1243b1c73fc8SJaroslav Kysela 				  SNDRV_PCM_HW_PARAM_CHANNELS,
1244898dfe46STakashi Iwai 				  rule_channels, dpcm,
1245b1c73fc8SJaroslav Kysela 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1246b1c73fc8SJaroslav Kysela 	if (err < 0)
1247b1c73fc8SJaroslav Kysela 		goto unlock;
1248b1c73fc8SJaroslav Kysela 
124926c53379STimo Wischer 	/* In case of sound timer the period time of both devices of the same
125026c53379STimo Wischer 	 * loop has to be the same.
125126c53379STimo Wischer 	 * This rule only takes effect if a sound timer was chosen
125226c53379STimo Wischer 	 */
125326c53379STimo Wischer 	if (cable->snd_timer.instance) {
125426c53379STimo Wischer 		err = snd_pcm_hw_rule_add(runtime, 0,
125526c53379STimo Wischer 					  SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
125626c53379STimo Wischer 					  rule_period_bytes, dpcm,
125726c53379STimo Wischer 					  SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1);
125826c53379STimo Wischer 		if (err < 0)
125926c53379STimo Wischer 			goto unlock;
126026c53379STimo Wischer 	}
126126c53379STimo Wischer 
1262133f3759STimo Wischer 	/* loopback_runtime_free() has not to be called if kfree(dpcm) was
1263133f3759STimo Wischer 	 * already called here. Otherwise it will end up with a double free.
1264133f3759STimo Wischer 	 */
1265597603d6SJaroslav Kysela 	runtime->private_free = loopback_runtime_free;
1266b1c73fc8SJaroslav Kysela 	if (get_notify(dpcm))
1267597603d6SJaroslav Kysela 		runtime->hw = loopback_pcm_hardware;
1268b1c73fc8SJaroslav Kysela 	else
1269597603d6SJaroslav Kysela 		runtime->hw = cable->hw;
12708e6b1a72STakashi Iwai 
12718e6b1a72STakashi Iwai 	spin_lock_irq(&cable->lock);
12728e6b1a72STakashi Iwai 	cable->streams[substream->stream] = dpcm;
12738e6b1a72STakashi Iwai 	spin_unlock_irq(&cable->lock);
12748e6b1a72STakashi Iwai 
1275597603d6SJaroslav Kysela  unlock:
12769685347aSTakashi Iwai 	if (err < 0) {
12779685347aSTakashi Iwai 		free_cable(substream);
12789685347aSTakashi Iwai 		kfree(dpcm);
12799685347aSTakashi Iwai 	}
1280597603d6SJaroslav Kysela 	mutex_unlock(&loopback->cable_lock);
1281597603d6SJaroslav Kysela 	return err;
1282597603d6SJaroslav Kysela }
1283597603d6SJaroslav Kysela 
loopback_close(struct snd_pcm_substream * substream)1284597603d6SJaroslav Kysela static int loopback_close(struct snd_pcm_substream *substream)
1285597603d6SJaroslav Kysela {
1286597603d6SJaroslav Kysela 	struct loopback *loopback = substream->private_data;
1287597603d6SJaroslav Kysela 	struct loopback_pcm *dpcm = substream->runtime->private_data;
1288133f3759STimo Wischer 	int err = 0;
1289597603d6SJaroslav Kysela 
1290133f3759STimo Wischer 	if (dpcm->cable->ops->close_substream)
1291133f3759STimo Wischer 		err = dpcm->cable->ops->close_substream(dpcm);
1292597603d6SJaroslav Kysela 	mutex_lock(&loopback->cable_lock);
12939685347aSTakashi Iwai 	free_cable(substream);
1294597603d6SJaroslav Kysela 	mutex_unlock(&loopback->cable_lock);
1295133f3759STimo Wischer 	return err;
1296597603d6SJaroslav Kysela }
1297597603d6SJaroslav Kysela 
12989f88058eSTakashi Iwai static const struct snd_pcm_ops loopback_pcm_ops = {
1299597603d6SJaroslav Kysela 	.open =		loopback_open,
1300597603d6SJaroslav Kysela 	.close =	loopback_close,
1301597603d6SJaroslav Kysela 	.hw_free =	loopback_hw_free,
1302597603d6SJaroslav Kysela 	.prepare =	loopback_prepare,
1303597603d6SJaroslav Kysela 	.trigger =	loopback_trigger,
1304597603d6SJaroslav Kysela 	.pointer =	loopback_pointer,
1305597603d6SJaroslav Kysela };
1306597603d6SJaroslav Kysela 
loopback_pcm_new(struct loopback * loopback,int device,int substreams)1307fbbb01a1SBill Pemberton static int loopback_pcm_new(struct loopback *loopback,
1308597603d6SJaroslav Kysela 			    int device, int substreams)
1309597603d6SJaroslav Kysela {
1310597603d6SJaroslav Kysela 	struct snd_pcm *pcm;
1311597603d6SJaroslav Kysela 	int err;
1312597603d6SJaroslav Kysela 
1313597603d6SJaroslav Kysela 	err = snd_pcm_new(loopback->card, "Loopback PCM", device,
1314597603d6SJaroslav Kysela 			  substreams, substreams, &pcm);
1315597603d6SJaroslav Kysela 	if (err < 0)
1316597603d6SJaroslav Kysela 		return err;
13179f88058eSTakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops);
13189f88058eSTakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops);
131939729889STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
1320597603d6SJaroslav Kysela 
1321597603d6SJaroslav Kysela 	pcm->private_data = loopback;
1322597603d6SJaroslav Kysela 	pcm->info_flags = 0;
1323597603d6SJaroslav Kysela 	strcpy(pcm->name, "Loopback PCM");
1324597603d6SJaroslav Kysela 
1325597603d6SJaroslav Kysela 	loopback->pcm[device] = pcm;
1326597603d6SJaroslav Kysela 	return 0;
1327597603d6SJaroslav Kysela }
1328597603d6SJaroslav Kysela 
loopback_rate_shift_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1329597603d6SJaroslav Kysela static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
1330597603d6SJaroslav Kysela 				    struct snd_ctl_elem_info *uinfo)
1331597603d6SJaroslav Kysela {
1332597603d6SJaroslav Kysela 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1333597603d6SJaroslav Kysela 	uinfo->count = 1;
1334597603d6SJaroslav Kysela 	uinfo->value.integer.min = 80000;
1335597603d6SJaroslav Kysela 	uinfo->value.integer.max = 120000;
1336597603d6SJaroslav Kysela 	uinfo->value.integer.step = 1;
1337597603d6SJaroslav Kysela 	return 0;
1338597603d6SJaroslav Kysela }
1339597603d6SJaroslav Kysela 
loopback_rate_shift_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1340597603d6SJaroslav Kysela static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
1341597603d6SJaroslav Kysela 				   struct snd_ctl_elem_value *ucontrol)
1342597603d6SJaroslav Kysela {
1343597603d6SJaroslav Kysela 	struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1344597603d6SJaroslav Kysela 
134576b3421bSTakashi Iwai 	mutex_lock(&loopback->cable_lock);
1346597603d6SJaroslav Kysela 	ucontrol->value.integer.value[0] =
1347597603d6SJaroslav Kysela 		loopback->setup[kcontrol->id.subdevice]
1348597603d6SJaroslav Kysela 			       [kcontrol->id.device].rate_shift;
134976b3421bSTakashi Iwai 	mutex_unlock(&loopback->cable_lock);
1350597603d6SJaroslav Kysela 	return 0;
1351597603d6SJaroslav Kysela }
1352597603d6SJaroslav Kysela 
loopback_rate_shift_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1353597603d6SJaroslav Kysela static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
1354597603d6SJaroslav Kysela 				   struct snd_ctl_elem_value *ucontrol)
1355597603d6SJaroslav Kysela {
1356597603d6SJaroslav Kysela 	struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1357597603d6SJaroslav Kysela 	unsigned int val;
1358597603d6SJaroslav Kysela 	int change = 0;
1359597603d6SJaroslav Kysela 
1360597603d6SJaroslav Kysela 	val = ucontrol->value.integer.value[0];
1361597603d6SJaroslav Kysela 	if (val < 80000)
1362597603d6SJaroslav Kysela 		val = 80000;
1363597603d6SJaroslav Kysela 	if (val > 120000)
1364597603d6SJaroslav Kysela 		val = 120000;
1365597603d6SJaroslav Kysela 	mutex_lock(&loopback->cable_lock);
1366597603d6SJaroslav Kysela 	if (val != loopback->setup[kcontrol->id.subdevice]
1367597603d6SJaroslav Kysela 				  [kcontrol->id.device].rate_shift) {
1368597603d6SJaroslav Kysela 		loopback->setup[kcontrol->id.subdevice]
1369597603d6SJaroslav Kysela 			       [kcontrol->id.device].rate_shift = val;
1370597603d6SJaroslav Kysela 		change = 1;
1371597603d6SJaroslav Kysela 	}
1372597603d6SJaroslav Kysela 	mutex_unlock(&loopback->cable_lock);
1373597603d6SJaroslav Kysela 	return change;
1374597603d6SJaroslav Kysela }
1375597603d6SJaroslav Kysela 
loopback_notify_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1376597603d6SJaroslav Kysela static int loopback_notify_get(struct snd_kcontrol *kcontrol,
1377597603d6SJaroslav Kysela 			       struct snd_ctl_elem_value *ucontrol)
1378597603d6SJaroslav Kysela {
1379597603d6SJaroslav Kysela 	struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1380597603d6SJaroslav Kysela 
138176b3421bSTakashi Iwai 	mutex_lock(&loopback->cable_lock);
1382597603d6SJaroslav Kysela 	ucontrol->value.integer.value[0] =
1383597603d6SJaroslav Kysela 		loopback->setup[kcontrol->id.subdevice]
1384597603d6SJaroslav Kysela 			       [kcontrol->id.device].notify;
138576b3421bSTakashi Iwai 	mutex_unlock(&loopback->cable_lock);
1386597603d6SJaroslav Kysela 	return 0;
1387597603d6SJaroslav Kysela }
1388597603d6SJaroslav Kysela 
loopback_notify_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1389597603d6SJaroslav Kysela static int loopback_notify_put(struct snd_kcontrol *kcontrol,
1390597603d6SJaroslav Kysela 			       struct snd_ctl_elem_value *ucontrol)
1391597603d6SJaroslav Kysela {
1392597603d6SJaroslav Kysela 	struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1393597603d6SJaroslav Kysela 	unsigned int val;
1394597603d6SJaroslav Kysela 	int change = 0;
1395597603d6SJaroslav Kysela 
1396597603d6SJaroslav Kysela 	val = ucontrol->value.integer.value[0] ? 1 : 0;
139776b3421bSTakashi Iwai 	mutex_lock(&loopback->cable_lock);
1398597603d6SJaroslav Kysela 	if (val != loopback->setup[kcontrol->id.subdevice]
1399597603d6SJaroslav Kysela 				[kcontrol->id.device].notify) {
1400597603d6SJaroslav Kysela 		loopback->setup[kcontrol->id.subdevice]
1401597603d6SJaroslav Kysela 			[kcontrol->id.device].notify = val;
1402597603d6SJaroslav Kysela 		change = 1;
1403597603d6SJaroslav Kysela 	}
140476b3421bSTakashi Iwai 	mutex_unlock(&loopback->cable_lock);
1405597603d6SJaroslav Kysela 	return change;
1406597603d6SJaroslav Kysela }
1407597603d6SJaroslav Kysela 
loopback_active_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1408597603d6SJaroslav Kysela static int loopback_active_get(struct snd_kcontrol *kcontrol,
1409597603d6SJaroslav Kysela 			       struct snd_ctl_elem_value *ucontrol)
1410597603d6SJaroslav Kysela {
1411597603d6SJaroslav Kysela 	struct loopback *loopback = snd_kcontrol_chip(kcontrol);
141276b3421bSTakashi Iwai 	struct loopback_cable *cable;
141376b3421bSTakashi Iwai 
1414597603d6SJaroslav Kysela 	unsigned int val = 0;
1415597603d6SJaroslav Kysela 
141676b3421bSTakashi Iwai 	mutex_lock(&loopback->cable_lock);
141776b3421bSTakashi Iwai 	cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
1418306a4f3cSRobert Rosengren 	if (cable != NULL) {
1419306a4f3cSRobert Rosengren 		unsigned int running = cable->running ^ cable->pause;
1420306a4f3cSRobert Rosengren 
1421306a4f3cSRobert Rosengren 		val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
1422306a4f3cSRobert Rosengren 	}
142376b3421bSTakashi Iwai 	mutex_unlock(&loopback->cable_lock);
1424597603d6SJaroslav Kysela 	ucontrol->value.integer.value[0] = val;
1425597603d6SJaroslav Kysela 	return 0;
1426597603d6SJaroslav Kysela }
1427597603d6SJaroslav Kysela 
loopback_format_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1428597603d6SJaroslav Kysela static int loopback_format_info(struct snd_kcontrol *kcontrol,
1429597603d6SJaroslav Kysela 				struct snd_ctl_elem_info *uinfo)
1430597603d6SJaroslav Kysela {
1431597603d6SJaroslav Kysela 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1432597603d6SJaroslav Kysela 	uinfo->count = 1;
1433597603d6SJaroslav Kysela 	uinfo->value.integer.min = 0;
14348c356c52STakashi Iwai 	uinfo->value.integer.max = (__force int)SNDRV_PCM_FORMAT_LAST;
1435597603d6SJaroslav Kysela 	uinfo->value.integer.step = 1;
1436597603d6SJaroslav Kysela 	return 0;
1437597603d6SJaroslav Kysela }
1438597603d6SJaroslav Kysela 
loopback_format_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1439597603d6SJaroslav Kysela static int loopback_format_get(struct snd_kcontrol *kcontrol,
1440597603d6SJaroslav Kysela 			       struct snd_ctl_elem_value *ucontrol)
1441597603d6SJaroslav Kysela {
1442597603d6SJaroslav Kysela 	struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1443597603d6SJaroslav Kysela 
1444597603d6SJaroslav Kysela 	ucontrol->value.integer.value[0] =
14458c356c52STakashi Iwai 		(__force int)loopback->setup[kcontrol->id.subdevice]
1446597603d6SJaroslav Kysela 			       [kcontrol->id.device].format;
1447597603d6SJaroslav Kysela 	return 0;
1448597603d6SJaroslav Kysela }
1449597603d6SJaroslav Kysela 
loopback_rate_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1450597603d6SJaroslav Kysela static int loopback_rate_info(struct snd_kcontrol *kcontrol,
1451597603d6SJaroslav Kysela 			      struct snd_ctl_elem_info *uinfo)
1452597603d6SJaroslav Kysela {
1453597603d6SJaroslav Kysela 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1454597603d6SJaroslav Kysela 	uinfo->count = 1;
1455597603d6SJaroslav Kysela 	uinfo->value.integer.min = 0;
1456597603d6SJaroslav Kysela 	uinfo->value.integer.max = 192000;
1457597603d6SJaroslav Kysela 	uinfo->value.integer.step = 1;
1458597603d6SJaroslav Kysela 	return 0;
1459597603d6SJaroslav Kysela }
1460597603d6SJaroslav Kysela 
loopback_rate_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1461597603d6SJaroslav Kysela static int loopback_rate_get(struct snd_kcontrol *kcontrol,
1462597603d6SJaroslav Kysela 			     struct snd_ctl_elem_value *ucontrol)
1463597603d6SJaroslav Kysela {
1464597603d6SJaroslav Kysela 	struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1465597603d6SJaroslav Kysela 
146676b3421bSTakashi Iwai 	mutex_lock(&loopback->cable_lock);
1467597603d6SJaroslav Kysela 	ucontrol->value.integer.value[0] =
1468597603d6SJaroslav Kysela 		loopback->setup[kcontrol->id.subdevice]
1469597603d6SJaroslav Kysela 			       [kcontrol->id.device].rate;
147076b3421bSTakashi Iwai 	mutex_unlock(&loopback->cable_lock);
1471597603d6SJaroslav Kysela 	return 0;
1472597603d6SJaroslav Kysela }
1473597603d6SJaroslav Kysela 
loopback_channels_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1474597603d6SJaroslav Kysela static int loopback_channels_info(struct snd_kcontrol *kcontrol,
1475597603d6SJaroslav Kysela 				  struct snd_ctl_elem_info *uinfo)
1476597603d6SJaroslav Kysela {
1477597603d6SJaroslav Kysela 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1478597603d6SJaroslav Kysela 	uinfo->count = 1;
1479597603d6SJaroslav Kysela 	uinfo->value.integer.min = 1;
1480597603d6SJaroslav Kysela 	uinfo->value.integer.max = 1024;
1481597603d6SJaroslav Kysela 	uinfo->value.integer.step = 1;
1482597603d6SJaroslav Kysela 	return 0;
1483597603d6SJaroslav Kysela }
1484597603d6SJaroslav Kysela 
loopback_channels_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1485597603d6SJaroslav Kysela static int loopback_channels_get(struct snd_kcontrol *kcontrol,
1486597603d6SJaroslav Kysela 				 struct snd_ctl_elem_value *ucontrol)
1487597603d6SJaroslav Kysela {
1488597603d6SJaroslav Kysela 	struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1489597603d6SJaroslav Kysela 
149076b3421bSTakashi Iwai 	mutex_lock(&loopback->cable_lock);
1491597603d6SJaroslav Kysela 	ucontrol->value.integer.value[0] =
1492597603d6SJaroslav Kysela 		loopback->setup[kcontrol->id.subdevice]
14931446c5fbSJaroslav Kysela 			       [kcontrol->id.device].channels;
149476b3421bSTakashi Iwai 	mutex_unlock(&loopback->cable_lock);
1495597603d6SJaroslav Kysela 	return 0;
1496597603d6SJaroslav Kysela }
1497597603d6SJaroslav Kysela 
14982eccd408STakashi Iwai static const struct snd_kcontrol_new loopback_controls[]  = {
1499597603d6SJaroslav Kysela {
1500597603d6SJaroslav Kysela 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1501597603d6SJaroslav Kysela 	.name =         "PCM Rate Shift 100000",
1502597603d6SJaroslav Kysela 	.info =         loopback_rate_shift_info,
1503597603d6SJaroslav Kysela 	.get =          loopback_rate_shift_get,
1504597603d6SJaroslav Kysela 	.put =          loopback_rate_shift_put,
1505597603d6SJaroslav Kysela },
1506597603d6SJaroslav Kysela {
1507597603d6SJaroslav Kysela 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1508597603d6SJaroslav Kysela 	.name =         "PCM Notify",
1509597603d6SJaroslav Kysela 	.info =         snd_ctl_boolean_mono_info,
1510597603d6SJaroslav Kysela 	.get =          loopback_notify_get,
1511597603d6SJaroslav Kysela 	.put =          loopback_notify_put,
1512597603d6SJaroslav Kysela },
1513597603d6SJaroslav Kysela #define ACTIVE_IDX 2
1514597603d6SJaroslav Kysela {
1515597603d6SJaroslav Kysela 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1516597603d6SJaroslav Kysela 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1517597603d6SJaroslav Kysela 	.name =         "PCM Slave Active",
1518597603d6SJaroslav Kysela 	.info =         snd_ctl_boolean_mono_info,
1519597603d6SJaroslav Kysela 	.get =          loopback_active_get,
1520597603d6SJaroslav Kysela },
1521597603d6SJaroslav Kysela #define FORMAT_IDX 3
1522597603d6SJaroslav Kysela {
1523597603d6SJaroslav Kysela 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1524597603d6SJaroslav Kysela 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1525597603d6SJaroslav Kysela 	.name =         "PCM Slave Format",
1526597603d6SJaroslav Kysela 	.info =         loopback_format_info,
1527597603d6SJaroslav Kysela 	.get =          loopback_format_get
1528597603d6SJaroslav Kysela },
1529597603d6SJaroslav Kysela #define RATE_IDX 4
1530597603d6SJaroslav Kysela {
1531597603d6SJaroslav Kysela 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1532597603d6SJaroslav Kysela 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1533597603d6SJaroslav Kysela 	.name =         "PCM Slave Rate",
1534597603d6SJaroslav Kysela 	.info =         loopback_rate_info,
1535597603d6SJaroslav Kysela 	.get =          loopback_rate_get
1536597603d6SJaroslav Kysela },
1537597603d6SJaroslav Kysela #define CHANNELS_IDX 5
1538597603d6SJaroslav Kysela {
1539597603d6SJaroslav Kysela 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1540597603d6SJaroslav Kysela 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1541597603d6SJaroslav Kysela 	.name =         "PCM Slave Channels",
1542597603d6SJaroslav Kysela 	.info =         loopback_channels_info,
1543597603d6SJaroslav Kysela 	.get =          loopback_channels_get
1544597603d6SJaroslav Kysela }
1545597603d6SJaroslav Kysela };
1546597603d6SJaroslav Kysela 
loopback_mixer_new(struct loopback * loopback,int notify)1547fbbb01a1SBill Pemberton static int loopback_mixer_new(struct loopback *loopback, int notify)
1548597603d6SJaroslav Kysela {
1549597603d6SJaroslav Kysela 	struct snd_card *card = loopback->card;
1550597603d6SJaroslav Kysela 	struct snd_pcm *pcm;
1551597603d6SJaroslav Kysela 	struct snd_kcontrol *kctl;
1552597603d6SJaroslav Kysela 	struct loopback_setup *setup;
1553597603d6SJaroslav Kysela 	int err, dev, substr, substr_count, idx;
1554597603d6SJaroslav Kysela 
1555597603d6SJaroslav Kysela 	strcpy(card->mixername, "Loopback Mixer");
1556597603d6SJaroslav Kysela 	for (dev = 0; dev < 2; dev++) {
1557597603d6SJaroslav Kysela 		pcm = loopback->pcm[dev];
1558597603d6SJaroslav Kysela 		substr_count =
1559597603d6SJaroslav Kysela 		    pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1560597603d6SJaroslav Kysela 		for (substr = 0; substr < substr_count; substr++) {
1561597603d6SJaroslav Kysela 			setup = &loopback->setup[substr][dev];
1562597603d6SJaroslav Kysela 			setup->notify = notify;
1563597603d6SJaroslav Kysela 			setup->rate_shift = NO_PITCH;
1564597603d6SJaroslav Kysela 			setup->format = SNDRV_PCM_FORMAT_S16_LE;
1565597603d6SJaroslav Kysela 			setup->rate = 48000;
1566597603d6SJaroslav Kysela 			setup->channels = 2;
1567597603d6SJaroslav Kysela 			for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1568597603d6SJaroslav Kysela 									idx++) {
1569597603d6SJaroslav Kysela 				kctl = snd_ctl_new1(&loopback_controls[idx],
1570597603d6SJaroslav Kysela 						    loopback);
1571597603d6SJaroslav Kysela 				if (!kctl)
1572597603d6SJaroslav Kysela 					return -ENOMEM;
1573597603d6SJaroslav Kysela 				kctl->id.device = dev;
1574597603d6SJaroslav Kysela 				kctl->id.subdevice = substr;
1575168632a4SJonas Holmberg 
1576168632a4SJonas Holmberg 				/* Add the control before copying the id so that
1577168632a4SJonas Holmberg 				 * the numid field of the id is set in the copy.
1578168632a4SJonas Holmberg 				 */
1579168632a4SJonas Holmberg 				err = snd_ctl_add(card, kctl);
1580168632a4SJonas Holmberg 				if (err < 0)
1581168632a4SJonas Holmberg 					return err;
1582168632a4SJonas Holmberg 
1583597603d6SJaroslav Kysela 				switch (idx) {
1584597603d6SJaroslav Kysela 				case ACTIVE_IDX:
1585597603d6SJaroslav Kysela 					setup->active_id = kctl->id;
1586597603d6SJaroslav Kysela 					break;
1587597603d6SJaroslav Kysela 				case FORMAT_IDX:
1588597603d6SJaroslav Kysela 					setup->format_id = kctl->id;
1589597603d6SJaroslav Kysela 					break;
1590597603d6SJaroslav Kysela 				case RATE_IDX:
1591597603d6SJaroslav Kysela 					setup->rate_id = kctl->id;
1592597603d6SJaroslav Kysela 					break;
1593597603d6SJaroslav Kysela 				case CHANNELS_IDX:
1594597603d6SJaroslav Kysela 					setup->channels_id = kctl->id;
1595597603d6SJaroslav Kysela 					break;
1596597603d6SJaroslav Kysela 				default:
1597597603d6SJaroslav Kysela 					break;
1598597603d6SJaroslav Kysela 				}
1599597603d6SJaroslav Kysela 			}
1600597603d6SJaroslav Kysela 		}
1601597603d6SJaroslav Kysela 	}
1602597603d6SJaroslav Kysela 	return 0;
1603597603d6SJaroslav Kysela }
1604597603d6SJaroslav Kysela 
print_dpcm_info(struct snd_info_buffer * buffer,struct loopback_pcm * dpcm,const char * id)1605e74670b6SJaroslav Kysela static void print_dpcm_info(struct snd_info_buffer *buffer,
1606e74670b6SJaroslav Kysela 			    struct loopback_pcm *dpcm,
1607e74670b6SJaroslav Kysela 			    const char *id)
1608e74670b6SJaroslav Kysela {
1609e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "  %s\n", id);
1610e74670b6SJaroslav Kysela 	if (dpcm == NULL) {
1611e74670b6SJaroslav Kysela 		snd_iprintf(buffer, "    inactive\n");
1612e74670b6SJaroslav Kysela 		return;
1613e74670b6SJaroslav Kysela 	}
1614e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "    buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1615e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "    buffer_pos:\t\t%u\n", dpcm->buf_pos);
1616e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "    silent_size:\t%u\n", dpcm->silent_size);
1617e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "    period_size:\t%u\n", dpcm->pcm_period_size);
1618e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "    bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1619e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "    sample_align:\t%u\n", dpcm->pcm_salign);
1620e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "    rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1621133f3759STimo Wischer 	if (dpcm->cable->ops->dpcm_info)
1622133f3759STimo Wischer 		dpcm->cable->ops->dpcm_info(dpcm, buffer);
1623e74670b6SJaroslav Kysela }
1624e74670b6SJaroslav Kysela 
print_substream_info(struct snd_info_buffer * buffer,struct loopback * loopback,int sub,int num)1625e74670b6SJaroslav Kysela static void print_substream_info(struct snd_info_buffer *buffer,
1626e74670b6SJaroslav Kysela 				 struct loopback *loopback,
1627e74670b6SJaroslav Kysela 				 int sub,
1628e74670b6SJaroslav Kysela 				 int num)
1629e74670b6SJaroslav Kysela {
1630e74670b6SJaroslav Kysela 	struct loopback_cable *cable = loopback->cables[sub][num];
1631e74670b6SJaroslav Kysela 
1632e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1633e74670b6SJaroslav Kysela 	if (cable == NULL) {
1634e74670b6SJaroslav Kysela 		snd_iprintf(buffer, "  inactive\n");
1635e74670b6SJaroslav Kysela 		return;
1636e74670b6SJaroslav Kysela 	}
1637e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "  valid: %u\n", cable->valid);
1638e74670b6SJaroslav Kysela 	snd_iprintf(buffer, "  running: %u\n", cable->running);
16395de9e45fSJaroslav Kysela 	snd_iprintf(buffer, "  pause: %u\n", cable->pause);
1640e74670b6SJaroslav Kysela 	print_dpcm_info(buffer, cable->streams[0], "Playback");
1641e74670b6SJaroslav Kysela 	print_dpcm_info(buffer, cable->streams[1], "Capture");
1642e74670b6SJaroslav Kysela }
1643e74670b6SJaroslav Kysela 
print_cable_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1644e74670b6SJaroslav Kysela static void print_cable_info(struct snd_info_entry *entry,
1645e74670b6SJaroslav Kysela 			     struct snd_info_buffer *buffer)
1646e74670b6SJaroslav Kysela {
1647e74670b6SJaroslav Kysela 	struct loopback *loopback = entry->private_data;
1648e74670b6SJaroslav Kysela 	int sub, num;
1649e74670b6SJaroslav Kysela 
1650e74670b6SJaroslav Kysela 	mutex_lock(&loopback->cable_lock);
1651e74670b6SJaroslav Kysela 	num = entry->name[strlen(entry->name)-1];
1652e74670b6SJaroslav Kysela 	num = num == '0' ? 0 : 1;
1653e74670b6SJaroslav Kysela 	for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1654e74670b6SJaroslav Kysela 		print_substream_info(buffer, loopback, sub, num);
1655e74670b6SJaroslav Kysela 	mutex_unlock(&loopback->cable_lock);
1656e74670b6SJaroslav Kysela }
1657e74670b6SJaroslav Kysela 
loopback_cable_proc_new(struct loopback * loopback,int cidx)1658c6ae9960SAndrew Gabbasov static int loopback_cable_proc_new(struct loopback *loopback, int cidx)
1659e74670b6SJaroslav Kysela {
1660e74670b6SJaroslav Kysela 	char name[32];
1661e74670b6SJaroslav Kysela 
1662e74670b6SJaroslav Kysela 	snprintf(name, sizeof(name), "cable#%d", cidx);
1663815d808cSTakashi Iwai 	return snd_card_ro_proc_new(loopback->card, name, loopback,
1664815d808cSTakashi Iwai 				    print_cable_info);
1665e74670b6SJaroslav Kysela }
1666e74670b6SJaroslav Kysela 
loopback_set_timer_source(struct loopback * loopback,const char * value)166726c53379STimo Wischer static void loopback_set_timer_source(struct loopback *loopback,
166826c53379STimo Wischer 				      const char *value)
166926c53379STimo Wischer {
167026c53379STimo Wischer 	if (loopback->timer_source) {
167126c53379STimo Wischer 		devm_kfree(loopback->card->dev, loopback->timer_source);
167226c53379STimo Wischer 		loopback->timer_source = NULL;
167326c53379STimo Wischer 	}
167426c53379STimo Wischer 	if (value && *value)
167526c53379STimo Wischer 		loopback->timer_source = devm_kstrdup(loopback->card->dev,
167626c53379STimo Wischer 						      value, GFP_KERNEL);
167726c53379STimo Wischer }
167826c53379STimo Wischer 
print_timer_source_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1679c6ae9960SAndrew Gabbasov static void print_timer_source_info(struct snd_info_entry *entry,
1680c6ae9960SAndrew Gabbasov 				    struct snd_info_buffer *buffer)
1681c6ae9960SAndrew Gabbasov {
1682c6ae9960SAndrew Gabbasov 	struct loopback *loopback = entry->private_data;
1683c6ae9960SAndrew Gabbasov 
1684c6ae9960SAndrew Gabbasov 	mutex_lock(&loopback->cable_lock);
1685c6ae9960SAndrew Gabbasov 	snd_iprintf(buffer, "%s\n",
1686c6ae9960SAndrew Gabbasov 		    loopback->timer_source ? loopback->timer_source : "");
1687c6ae9960SAndrew Gabbasov 	mutex_unlock(&loopback->cable_lock);
1688c6ae9960SAndrew Gabbasov }
1689c6ae9960SAndrew Gabbasov 
change_timer_source_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1690c6ae9960SAndrew Gabbasov static void change_timer_source_info(struct snd_info_entry *entry,
1691c6ae9960SAndrew Gabbasov 				     struct snd_info_buffer *buffer)
1692c6ae9960SAndrew Gabbasov {
1693c6ae9960SAndrew Gabbasov 	struct loopback *loopback = entry->private_data;
1694c6ae9960SAndrew Gabbasov 	char line[64];
1695c6ae9960SAndrew Gabbasov 
1696c6ae9960SAndrew Gabbasov 	mutex_lock(&loopback->cable_lock);
1697c6ae9960SAndrew Gabbasov 	if (!snd_info_get_line(buffer, line, sizeof(line)))
1698c6ae9960SAndrew Gabbasov 		loopback_set_timer_source(loopback, strim(line));
1699c6ae9960SAndrew Gabbasov 	mutex_unlock(&loopback->cable_lock);
1700c6ae9960SAndrew Gabbasov }
1701c6ae9960SAndrew Gabbasov 
loopback_timer_source_proc_new(struct loopback * loopback)1702c6ae9960SAndrew Gabbasov static int loopback_timer_source_proc_new(struct loopback *loopback)
1703c6ae9960SAndrew Gabbasov {
1704c6ae9960SAndrew Gabbasov 	return snd_card_rw_proc_new(loopback->card, "timer_source", loopback,
1705c6ae9960SAndrew Gabbasov 				    print_timer_source_info,
1706c6ae9960SAndrew Gabbasov 				    change_timer_source_info);
1707c6ae9960SAndrew Gabbasov }
1708c6ae9960SAndrew Gabbasov 
loopback_probe(struct platform_device * devptr)1709fbbb01a1SBill Pemberton static int loopback_probe(struct platform_device *devptr)
1710597603d6SJaroslav Kysela {
1711597603d6SJaroslav Kysela 	struct snd_card *card;
1712597603d6SJaroslav Kysela 	struct loopback *loopback;
1713597603d6SJaroslav Kysela 	int dev = devptr->id;
1714597603d6SJaroslav Kysela 	int err;
1715597603d6SJaroslav Kysela 
1716b072e65aSTakashi Iwai 	err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1717597603d6SJaroslav Kysela 				sizeof(struct loopback), &card);
1718597603d6SJaroslav Kysela 	if (err < 0)
1719597603d6SJaroslav Kysela 		return err;
1720597603d6SJaroslav Kysela 	loopback = card->private_data;
1721597603d6SJaroslav Kysela 
1722597603d6SJaroslav Kysela 	if (pcm_substreams[dev] < 1)
1723597603d6SJaroslav Kysela 		pcm_substreams[dev] = 1;
1724597603d6SJaroslav Kysela 	if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1725597603d6SJaroslav Kysela 		pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1726597603d6SJaroslav Kysela 
1727597603d6SJaroslav Kysela 	loopback->card = card;
172826c53379STimo Wischer 	loopback_set_timer_source(loopback, timer_source[dev]);
172926c53379STimo Wischer 
1730597603d6SJaroslav Kysela 	mutex_init(&loopback->cable_lock);
1731597603d6SJaroslav Kysela 
1732597603d6SJaroslav Kysela 	err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1733597603d6SJaroslav Kysela 	if (err < 0)
1734b072e65aSTakashi Iwai 		return err;
1735597603d6SJaroslav Kysela 	err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1736597603d6SJaroslav Kysela 	if (err < 0)
1737b072e65aSTakashi Iwai 		return err;
1738597603d6SJaroslav Kysela 	err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1739597603d6SJaroslav Kysela 	if (err < 0)
1740b072e65aSTakashi Iwai 		return err;
1741c6ae9960SAndrew Gabbasov 	loopback_cable_proc_new(loopback, 0);
1742c6ae9960SAndrew Gabbasov 	loopback_cable_proc_new(loopback, 1);
1743c6ae9960SAndrew Gabbasov 	loopback_timer_source_proc_new(loopback);
1744597603d6SJaroslav Kysela 	strcpy(card->driver, "Loopback");
1745597603d6SJaroslav Kysela 	strcpy(card->shortname, "Loopback");
1746597603d6SJaroslav Kysela 	sprintf(card->longname, "Loopback %i", dev + 1);
1747597603d6SJaroslav Kysela 	err = snd_card_register(card);
1748b072e65aSTakashi Iwai 	if (err < 0)
1749597603d6SJaroslav Kysela 		return err;
1750b072e65aSTakashi Iwai 	platform_set_drvdata(devptr, card);
1751597603d6SJaroslav Kysela 	return 0;
1752597603d6SJaroslav Kysela }
1753597603d6SJaroslav Kysela 
1754d34e4e00STakashi Iwai #ifdef CONFIG_PM_SLEEP
loopback_suspend(struct device * pdev)1755284e7ca7STakashi Iwai static int loopback_suspend(struct device *pdev)
1756597603d6SJaroslav Kysela {
1757284e7ca7STakashi Iwai 	struct snd_card *card = dev_get_drvdata(pdev);
1758597603d6SJaroslav Kysela 
1759597603d6SJaroslav Kysela 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1760597603d6SJaroslav Kysela 	return 0;
1761597603d6SJaroslav Kysela }
1762597603d6SJaroslav Kysela 
loopback_resume(struct device * pdev)1763284e7ca7STakashi Iwai static int loopback_resume(struct device *pdev)
1764597603d6SJaroslav Kysela {
1765284e7ca7STakashi Iwai 	struct snd_card *card = dev_get_drvdata(pdev);
1766597603d6SJaroslav Kysela 
1767597603d6SJaroslav Kysela 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1768597603d6SJaroslav Kysela 	return 0;
1769597603d6SJaroslav Kysela }
1770284e7ca7STakashi Iwai 
1771284e7ca7STakashi Iwai static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1772284e7ca7STakashi Iwai #define LOOPBACK_PM_OPS	&loopback_pm
1773284e7ca7STakashi Iwai #else
1774284e7ca7STakashi Iwai #define LOOPBACK_PM_OPS	NULL
1775597603d6SJaroslav Kysela #endif
1776597603d6SJaroslav Kysela 
1777597603d6SJaroslav Kysela #define SND_LOOPBACK_DRIVER	"snd_aloop"
1778597603d6SJaroslav Kysela 
1779597603d6SJaroslav Kysela static struct platform_driver loopback_driver = {
1780597603d6SJaroslav Kysela 	.probe		= loopback_probe,
1781597603d6SJaroslav Kysela 	.driver		= {
17828bf01d8aSTakashi Iwai 		.name	= SND_LOOPBACK_DRIVER,
1783284e7ca7STakashi Iwai 		.pm	= LOOPBACK_PM_OPS,
1784597603d6SJaroslav Kysela 	},
1785597603d6SJaroslav Kysela };
1786597603d6SJaroslav Kysela 
loopback_unregister_all(void)1787597603d6SJaroslav Kysela static void loopback_unregister_all(void)
1788597603d6SJaroslav Kysela {
1789597603d6SJaroslav Kysela 	int i;
1790597603d6SJaroslav Kysela 
1791597603d6SJaroslav Kysela 	for (i = 0; i < ARRAY_SIZE(devices); ++i)
1792597603d6SJaroslav Kysela 		platform_device_unregister(devices[i]);
1793597603d6SJaroslav Kysela 	platform_driver_unregister(&loopback_driver);
1794597603d6SJaroslav Kysela }
1795597603d6SJaroslav Kysela 
alsa_card_loopback_init(void)1796597603d6SJaroslav Kysela static int __init alsa_card_loopback_init(void)
1797597603d6SJaroslav Kysela {
1798597603d6SJaroslav Kysela 	int i, err, cards;
1799597603d6SJaroslav Kysela 
1800597603d6SJaroslav Kysela 	err = platform_driver_register(&loopback_driver);
1801597603d6SJaroslav Kysela 	if (err < 0)
1802597603d6SJaroslav Kysela 		return err;
1803597603d6SJaroslav Kysela 
1804597603d6SJaroslav Kysela 
1805597603d6SJaroslav Kysela 	cards = 0;
1806597603d6SJaroslav Kysela 	for (i = 0; i < SNDRV_CARDS; i++) {
1807597603d6SJaroslav Kysela 		struct platform_device *device;
1808597603d6SJaroslav Kysela 		if (!enable[i])
1809597603d6SJaroslav Kysela 			continue;
1810597603d6SJaroslav Kysela 		device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1811597603d6SJaroslav Kysela 							 i, NULL, 0);
1812597603d6SJaroslav Kysela 		if (IS_ERR(device))
1813597603d6SJaroslav Kysela 			continue;
1814597603d6SJaroslav Kysela 		if (!platform_get_drvdata(device)) {
1815597603d6SJaroslav Kysela 			platform_device_unregister(device);
1816597603d6SJaroslav Kysela 			continue;
1817597603d6SJaroslav Kysela 		}
1818597603d6SJaroslav Kysela 		devices[i] = device;
1819597603d6SJaroslav Kysela 		cards++;
1820597603d6SJaroslav Kysela 	}
1821597603d6SJaroslav Kysela 	if (!cards) {
1822597603d6SJaroslav Kysela #ifdef MODULE
1823597603d6SJaroslav Kysela 		printk(KERN_ERR "aloop: No loopback enabled\n");
1824597603d6SJaroslav Kysela #endif
1825597603d6SJaroslav Kysela 		loopback_unregister_all();
1826597603d6SJaroslav Kysela 		return -ENODEV;
1827597603d6SJaroslav Kysela 	}
1828597603d6SJaroslav Kysela 	return 0;
1829597603d6SJaroslav Kysela }
1830597603d6SJaroslav Kysela 
alsa_card_loopback_exit(void)1831597603d6SJaroslav Kysela static void __exit alsa_card_loopback_exit(void)
1832597603d6SJaroslav Kysela {
1833597603d6SJaroslav Kysela 	loopback_unregister_all();
1834597603d6SJaroslav Kysela }
1835597603d6SJaroslav Kysela 
1836597603d6SJaroslav Kysela module_init(alsa_card_loopback_init)
1837597603d6SJaroslav Kysela module_exit(alsa_card_loopback_exit)
1838