blob: 1904046686e2f915f939d333e56ba44e99a2eb3c [file] [log] [blame]
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001/*
2 * Loopback soundcard
3 *
4 * Original code:
5 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 *
7 * More accurate positioning and full-duplex support:
8 * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
9 *
10 * Major (almost complete) rewrite:
11 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
12 *
13 * A next major update in 2010 (separate timers for playback and capture):
14 * Copyright (c) Jaroslav Kysela <perex@perex.cz>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 */
31
32#include <linux/init.h>
33#include <linux/jiffies.h>
34#include <linux/slab.h>
35#include <linux/time.h>
36#include <linux/wait.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040037#include <linux/module.h>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020038#include <linux/platform_device.h>
39#include <sound/core.h>
40#include <sound/control.h>
41#include <sound/pcm.h>
Jaroslav Kyselae74670b2010-10-18 09:43:10 +020042#include <sound/info.h>
Jaroslav Kysela597603d2010-08-09 14:21:11 +020043#include <sound/initval.h>
44
45MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
46MODULE_DESCRIPTION("A loopback soundcard");
47MODULE_LICENSE("GPL");
48MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
49
50#define MAX_PCM_SUBSTREAMS 8
51
52static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
53static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103054static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
Jaroslav Kysela597603d2010-08-09 14:21:11 +020055static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
56static int pcm_notify[SNDRV_CARDS];
57
58module_param_array(index, int, NULL, 0444);
59MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
60module_param_array(id, charp, NULL, 0444);
61MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
62module_param_array(enable, bool, NULL, 0444);
63MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
64module_param_array(pcm_substreams, int, NULL, 0444);
65MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
66module_param_array(pcm_notify, int, NULL, 0444);
67MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
68
69#define NO_PITCH 100000
70
71struct loopback_pcm;
72
73struct loopback_cable {
74 spinlock_t lock;
75 struct loopback_pcm *streams[2];
76 struct snd_pcm_hardware hw;
77 /* flags */
78 unsigned int valid;
79 unsigned int running;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +020080 unsigned int pause;
Jaroslav Kysela597603d2010-08-09 14:21:11 +020081};
82
83struct loopback_setup {
84 unsigned int notify: 1;
85 unsigned int rate_shift;
86 unsigned int format;
87 unsigned int rate;
88 unsigned int channels;
89 struct snd_ctl_elem_id active_id;
90 struct snd_ctl_elem_id format_id;
91 struct snd_ctl_elem_id rate_id;
92 struct snd_ctl_elem_id channels_id;
93};
94
95struct loopback {
96 struct snd_card *card;
97 struct mutex cable_lock;
98 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
99 struct snd_pcm *pcm[2];
100 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
101};
102
103struct loopback_pcm {
104 struct loopback *loopback;
105 struct snd_pcm_substream *substream;
106 struct loopback_cable *cable;
107 unsigned int pcm_buffer_size;
108 unsigned int buf_pos; /* position in buffer */
109 unsigned int silent_size;
110 /* PCM parameters */
111 unsigned int pcm_period_size;
112 unsigned int pcm_bps; /* bytes per second */
113 unsigned int pcm_salign; /* bytes per sample * channels */
114 unsigned int pcm_rate_shift; /* rate shift value */
115 /* flags */
116 unsigned int period_update_pending :1;
117 /* timer stuff */
118 unsigned int irq_pos; /* fractional IRQ position */
119 unsigned int period_size_frac;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200120 unsigned int last_drift;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200121 unsigned long last_jiffies;
122 struct timer_list timer;
123};
124
125static struct platform_device *devices[SNDRV_CARDS];
126
127static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
128{
129 if (dpcm->pcm_rate_shift == NO_PITCH) {
130 x /= HZ;
131 } else {
132 x = div_u64(NO_PITCH * (unsigned long long)x,
133 HZ * (unsigned long long)dpcm->pcm_rate_shift);
134 }
135 return x - (x % dpcm->pcm_salign);
136}
137
138static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
139{
140 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
141 return x * HZ;
142 } else {
143 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
144 NO_PITCH);
145 }
146 return x;
147}
148
149static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
150{
151 int device = dpcm->substream->pstr->pcm->device;
152
153 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
154 device ^= 1;
155 return &dpcm->loopback->setup[dpcm->substream->number][device];
156}
157
158static inline unsigned int get_notify(struct loopback_pcm *dpcm)
159{
160 return get_setup(dpcm)->notify;
161}
162
163static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
164{
165 return get_setup(dpcm)->rate_shift;
166}
167
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200168/* call in cable->lock */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200169static void loopback_timer_start(struct loopback_pcm *dpcm)
170{
171 unsigned long tick;
172 unsigned int rate_shift = get_rate_shift(dpcm);
173
174 if (rate_shift != dpcm->pcm_rate_shift) {
175 dpcm->pcm_rate_shift = rate_shift;
176 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
177 }
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200178 if (dpcm->period_size_frac <= dpcm->irq_pos) {
179 dpcm->irq_pos %= dpcm->period_size_frac;
180 dpcm->period_update_pending = 1;
181 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200182 tick = dpcm->period_size_frac - dpcm->irq_pos;
183 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
184 dpcm->timer.expires = jiffies + tick;
185 add_timer(&dpcm->timer);
186}
187
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200188/* call in cable->lock */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200189static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
190{
191 del_timer(&dpcm->timer);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +0200192 dpcm->timer.expires = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200193}
194
195#define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
196#define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
197#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
198
199static int loopback_check_format(struct loopback_cable *cable, int stream)
200{
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200201 struct snd_pcm_runtime *runtime, *cruntime;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200202 struct loopback_setup *setup;
203 struct snd_card *card;
204 int check;
205
206 if (cable->valid != CABLE_VALID_BOTH) {
207 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
208 goto __notify;
209 return 0;
210 }
211 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
212 substream->runtime;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200213 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
214 substream->runtime;
215 check = runtime->format != cruntime->format ||
216 runtime->rate != cruntime->rate ||
217 runtime->channels != cruntime->channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200218 if (!check)
219 return 0;
220 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
221 return -EIO;
222 } else {
223 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
224 substream, SNDRV_PCM_STATE_DRAINING);
225 __notify:
226 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
227 substream->runtime;
228 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
229 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
230 if (setup->format != runtime->format) {
231 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
232 &setup->format_id);
233 setup->format = runtime->format;
234 }
235 if (setup->rate != runtime->rate) {
236 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
237 &setup->rate_id);
238 setup->rate = runtime->rate;
239 }
240 if (setup->channels != runtime->channels) {
241 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
242 &setup->channels_id);
243 setup->channels = runtime->channels;
244 }
245 }
246 return 0;
247}
248
249static void loopback_active_notify(struct loopback_pcm *dpcm)
250{
251 snd_ctl_notify(dpcm->loopback->card,
252 SNDRV_CTL_EVENT_MASK_VALUE,
253 &get_setup(dpcm)->active_id);
254}
255
256static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
257{
258 struct snd_pcm_runtime *runtime = substream->runtime;
259 struct loopback_pcm *dpcm = runtime->private_data;
260 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200261 int err, stream = 1 << substream->stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200262
263 switch (cmd) {
264 case SNDRV_PCM_TRIGGER_START:
265 err = loopback_check_format(cable, substream->stream);
266 if (err < 0)
267 return err;
268 dpcm->last_jiffies = jiffies;
269 dpcm->pcm_rate_shift = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200270 dpcm->last_drift = 0;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200271 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200272 cable->running |= stream;
273 cable->pause &= ~stream;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200274 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200275 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200276 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
277 loopback_active_notify(dpcm);
278 break;
279 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200280 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200281 cable->running &= ~stream;
282 cable->pause &= ~stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200283 loopback_timer_stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200284 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
286 loopback_active_notify(dpcm);
287 break;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200288 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
289 spin_lock(&cable->lock);
290 cable->pause |= stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200291 loopback_timer_stop(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200292 spin_unlock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200293 break;
294 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
295 spin_lock(&cable->lock);
296 dpcm->last_jiffies = jiffies;
297 cable->pause &= ~stream;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200298 loopback_timer_start(dpcm);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200299 spin_unlock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200300 break;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200301 default:
302 return -EINVAL;
303 }
304 return 0;
305}
306
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200307static void params_change_substream(struct loopback_pcm *dpcm,
308 struct snd_pcm_runtime *runtime)
309{
310 struct snd_pcm_runtime *dst_runtime;
311
312 if (dpcm == NULL || dpcm->substream == NULL)
313 return;
314 dst_runtime = dpcm->substream->runtime;
315 if (dst_runtime == NULL)
316 return;
317 dst_runtime->hw = dpcm->cable->hw;
318}
319
320static void params_change(struct snd_pcm_substream *substream)
321{
322 struct snd_pcm_runtime *runtime = substream->runtime;
323 struct loopback_pcm *dpcm = runtime->private_data;
324 struct loopback_cable *cable = dpcm->cable;
325
326 cable->hw.formats = (1ULL << runtime->format);
327 cable->hw.rate_min = runtime->rate;
328 cable->hw.rate_max = runtime->rate;
329 cable->hw.channels_min = runtime->channels;
330 cable->hw.channels_max = runtime->channels;
331 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
332 runtime);
333 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
334 runtime);
335}
336
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200337static int loopback_prepare(struct snd_pcm_substream *substream)
338{
339 struct snd_pcm_runtime *runtime = substream->runtime;
340 struct loopback_pcm *dpcm = runtime->private_data;
341 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200342 int bps, salign;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200343
344 salign = (snd_pcm_format_width(runtime->format) *
345 runtime->channels) / 8;
346 bps = salign * runtime->rate;
347 if (bps <= 0 || salign <= 0)
348 return -EINVAL;
349
350 dpcm->buf_pos = 0;
351 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
352 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
353 /* clear capture buffer */
354 dpcm->silent_size = dpcm->pcm_buffer_size;
355 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
356 runtime->buffer_size * runtime->channels);
357 }
358
359 dpcm->irq_pos = 0;
360 dpcm->period_update_pending = 0;
361 dpcm->pcm_bps = bps;
362 dpcm->pcm_salign = salign;
363 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
364
365 mutex_lock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200366 if (!(cable->valid & ~(1 << substream->stream)) ||
367 (get_setup(dpcm)->notify &&
368 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
369 params_change(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200370 cable->valid |= 1 << substream->stream;
371 mutex_unlock(&dpcm->loopback->cable_lock);
372
373 return 0;
374}
375
376static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
377{
378 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
379 char *dst = runtime->dma_area;
380 unsigned int dst_off = dpcm->buf_pos;
381
382 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
383 return;
384 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
385 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
386
387 for (;;) {
388 unsigned int size = bytes;
389 if (dst_off + size > dpcm->pcm_buffer_size)
390 size = dpcm->pcm_buffer_size - dst_off;
391 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
392 bytes_to_frames(runtime, size) *
393 runtime->channels);
394 dpcm->silent_size += size;
395 bytes -= size;
396 if (!bytes)
397 break;
398 dst_off = 0;
399 }
400}
401
402static void copy_play_buf(struct loopback_pcm *play,
403 struct loopback_pcm *capt,
404 unsigned int bytes)
405{
406 struct snd_pcm_runtime *runtime = play->substream->runtime;
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200407 char *src = runtime->dma_area;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200408 char *dst = capt->substream->runtime->dma_area;
409 unsigned int src_off = play->buf_pos;
410 unsigned int dst_off = capt->buf_pos;
411 unsigned int clear_bytes = 0;
412
413 /* check if playback is draining, trim the capture copy size
414 * when our pointer is at the end of playback ring buffer */
415 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
416 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
417 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
418 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
419 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
420 appl_ptr1 += play->buf_pos / play->pcm_salign;
421 if (appl_ptr < appl_ptr1)
422 appl_ptr1 -= runtime->buffer_size;
423 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
424 if (diff < bytes) {
425 clear_bytes = bytes - diff;
426 bytes = diff;
427 }
428 }
429
430 for (;;) {
431 unsigned int size = bytes;
432 if (src_off + size > play->pcm_buffer_size)
433 size = play->pcm_buffer_size - src_off;
434 if (dst_off + size > capt->pcm_buffer_size)
435 size = capt->pcm_buffer_size - dst_off;
436 memcpy(dst + dst_off, src + src_off, size);
437 capt->silent_size = 0;
438 bytes -= size;
439 if (!bytes)
440 break;
441 src_off = (src_off + size) % play->pcm_buffer_size;
442 dst_off = (dst_off + size) % capt->pcm_buffer_size;
443 }
444
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200445 if (clear_bytes > 0) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200446 clear_capture_buf(capt, clear_bytes);
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200447 capt->silent_size = 0;
448 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200449}
450
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200451static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
452 unsigned int jiffies_delta)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200453{
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200454 unsigned long last_pos;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200455 unsigned int delta;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200456
457 last_pos = byte_pos(dpcm, dpcm->irq_pos);
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200458 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
459 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
460 if (delta >= dpcm->last_drift)
461 delta -= dpcm->last_drift;
462 dpcm->last_drift = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200463 if (dpcm->irq_pos >= dpcm->period_size_frac) {
464 dpcm->irq_pos %= dpcm->period_size_frac;
465 dpcm->period_update_pending = 1;
466 }
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200467 return delta;
468}
469
470static inline void bytepos_finish(struct loopback_pcm *dpcm,
471 unsigned int delta)
472{
473 dpcm->buf_pos += delta;
474 dpcm->buf_pos %= dpcm->pcm_buffer_size;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200475}
476
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200477/* call in cable->lock */
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200478static unsigned int loopback_pos_update(struct loopback_cable *cable)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200479{
480 struct loopback_pcm *dpcm_play =
481 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
482 struct loopback_pcm *dpcm_capt =
483 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
484 unsigned long delta_play = 0, delta_capt = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200485 unsigned int running, count1, count2;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200486
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200487 running = cable->running ^ cable->pause;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200488 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200489 delta_play = jiffies - dpcm_play->last_jiffies;
490 dpcm_play->last_jiffies += delta_play;
491 }
492
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200493 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200494 delta_capt = jiffies - dpcm_capt->last_jiffies;
495 dpcm_capt->last_jiffies += delta_capt;
496 }
497
Takashi Iwai98d21df2011-03-18 07:31:53 +0100498 if (delta_play == 0 && delta_capt == 0)
499 goto unlock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200500
501 if (delta_play > delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200502 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
503 bytepos_finish(dpcm_play, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200504 delta_play = delta_capt;
505 } else if (delta_play < delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200506 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
507 clear_capture_buf(dpcm_capt, count1);
508 bytepos_finish(dpcm_capt, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200509 delta_capt = delta_play;
510 }
511
Takashi Iwai98d21df2011-03-18 07:31:53 +0100512 if (delta_play == 0 && delta_capt == 0)
513 goto unlock;
514
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200515 /* note delta_capt == delta_play at this moment */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200516 count1 = bytepos_delta(dpcm_play, delta_play);
517 count2 = bytepos_delta(dpcm_capt, delta_capt);
518 if (count1 < count2) {
519 dpcm_capt->last_drift = count2 - count1;
520 count1 = count2;
521 } else if (count1 > count2) {
522 dpcm_play->last_drift = count1 - count2;
523 }
524 copy_play_buf(dpcm_play, dpcm_capt, count1);
525 bytepos_finish(dpcm_play, count1);
526 bytepos_finish(dpcm_capt, count1);
Takashi Iwai98d21df2011-03-18 07:31:53 +0100527 unlock:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200528 return running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200529}
530
531static void loopback_timer_function(unsigned long data)
532{
533 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200534 unsigned long flags;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200535
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200536 spin_lock_irqsave(&dpcm->cable->lock, flags);
537 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200538 loopback_timer_start(dpcm);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200539 if (dpcm->period_update_pending) {
540 dpcm->period_update_pending = 0;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200541 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
542 /* need to unlock before calling below */
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200543 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200544 return;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200545 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200546 }
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200547 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200548}
549
550static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
551{
552 struct snd_pcm_runtime *runtime = substream->runtime;
553 struct loopback_pcm *dpcm = runtime->private_data;
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200554 snd_pcm_uframes_t pos;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200555
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200556 spin_lock(&dpcm->cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200557 loopback_pos_update(dpcm->cable);
Takashi Iwai999fc9f2012-10-21 10:53:14 +0200558 pos = dpcm->buf_pos;
559 spin_unlock(&dpcm->cable->lock);
560 return bytes_to_frames(runtime, pos);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200561}
562
563static struct snd_pcm_hardware loopback_pcm_hardware =
564{
565 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200566 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200567 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
568 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
569 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
570 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
571 .rate_min = 8000,
572 .rate_max = 192000,
573 .channels_min = 1,
574 .channels_max = 32,
575 .buffer_bytes_max = 2 * 1024 * 1024,
576 .period_bytes_min = 64,
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200577 /* note check overflow in frac_pos() using pcm_rate_shift before
578 changing period_bytes_max value */
579 .period_bytes_max = 1024 * 1024,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200580 .periods_min = 1,
581 .periods_max = 1024,
582 .fifo_size = 0,
583};
584
585static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
586{
587 struct loopback_pcm *dpcm = runtime->private_data;
588 kfree(dpcm);
589}
590
591static int loopback_hw_params(struct snd_pcm_substream *substream,
592 struct snd_pcm_hw_params *params)
593{
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200594 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
595 params_buffer_bytes(params));
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200596}
597
598static int loopback_hw_free(struct snd_pcm_substream *substream)
599{
600 struct snd_pcm_runtime *runtime = substream->runtime;
601 struct loopback_pcm *dpcm = runtime->private_data;
602 struct loopback_cable *cable = dpcm->cable;
603
604 mutex_lock(&dpcm->loopback->cable_lock);
605 cable->valid &= ~(1 << substream->stream);
606 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200607 return snd_pcm_lib_free_vmalloc_buffer(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200608}
609
610static unsigned int get_cable_index(struct snd_pcm_substream *substream)
611{
612 if (!substream->pcm->device)
613 return substream->stream;
614 else
615 return !substream->stream;
616}
617
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200618static int rule_format(struct snd_pcm_hw_params *params,
619 struct snd_pcm_hw_rule *rule)
620{
621
622 struct snd_pcm_hardware *hw = rule->private;
623 struct snd_mask *maskp = hw_param_mask(params, rule->var);
624
625 maskp->bits[0] &= (u_int32_t)hw->formats;
626 maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
627 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
628 if (! maskp->bits[0] && ! maskp->bits[1])
629 return -EINVAL;
630 return 0;
631}
632
633static int rule_rate(struct snd_pcm_hw_params *params,
634 struct snd_pcm_hw_rule *rule)
635{
636 struct snd_pcm_hardware *hw = rule->private;
637 struct snd_interval t;
638
639 t.min = hw->rate_min;
640 t.max = hw->rate_max;
641 t.openmin = t.openmax = 0;
642 t.integer = 0;
643 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
644}
645
646static int rule_channels(struct snd_pcm_hw_params *params,
647 struct snd_pcm_hw_rule *rule)
648{
649 struct snd_pcm_hardware *hw = rule->private;
650 struct snd_interval t;
651
652 t.min = hw->channels_min;
653 t.max = hw->channels_max;
654 t.openmin = t.openmax = 0;
655 t.integer = 0;
656 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
657}
658
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200659static int loopback_open(struct snd_pcm_substream *substream)
660{
661 struct snd_pcm_runtime *runtime = substream->runtime;
662 struct loopback *loopback = substream->private_data;
663 struct loopback_pcm *dpcm;
664 struct loopback_cable *cable;
665 int err = 0;
666 int dev = get_cable_index(substream);
667
668 mutex_lock(&loopback->cable_lock);
669 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
670 if (!dpcm) {
671 err = -ENOMEM;
672 goto unlock;
673 }
674 dpcm->loopback = loopback;
675 dpcm->substream = substream;
676 setup_timer(&dpcm->timer, loopback_timer_function,
677 (unsigned long)dpcm);
678
679 cable = loopback->cables[substream->number][dev];
680 if (!cable) {
681 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
682 if (!cable) {
683 kfree(dpcm);
684 err = -ENOMEM;
685 goto unlock;
686 }
687 spin_lock_init(&cable->lock);
688 cable->hw = loopback_pcm_hardware;
689 loopback->cables[substream->number][dev] = cable;
690 }
691 dpcm->cable = cable;
692 cable->streams[substream->stream] = dpcm;
693
694 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
695
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200696 /* use dynamic rules based on actual runtime->hw values */
697 /* note that the default rules created in the PCM midlevel code */
698 /* are cached -> they do not reflect the actual state */
699 err = snd_pcm_hw_rule_add(runtime, 0,
700 SNDRV_PCM_HW_PARAM_FORMAT,
701 rule_format, &runtime->hw,
702 SNDRV_PCM_HW_PARAM_FORMAT, -1);
703 if (err < 0)
704 goto unlock;
705 err = snd_pcm_hw_rule_add(runtime, 0,
706 SNDRV_PCM_HW_PARAM_RATE,
707 rule_rate, &runtime->hw,
708 SNDRV_PCM_HW_PARAM_RATE, -1);
709 if (err < 0)
710 goto unlock;
711 err = snd_pcm_hw_rule_add(runtime, 0,
712 SNDRV_PCM_HW_PARAM_CHANNELS,
713 rule_channels, &runtime->hw,
714 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
715 if (err < 0)
716 goto unlock;
717
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200718 runtime->private_data = dpcm;
719 runtime->private_free = loopback_runtime_free;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200720 if (get_notify(dpcm))
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200721 runtime->hw = loopback_pcm_hardware;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200722 else
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200723 runtime->hw = cable->hw;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200724 unlock:
725 mutex_unlock(&loopback->cable_lock);
726 return err;
727}
728
729static int loopback_close(struct snd_pcm_substream *substream)
730{
731 struct loopback *loopback = substream->private_data;
732 struct loopback_pcm *dpcm = substream->runtime->private_data;
733 struct loopback_cable *cable;
734 int dev = get_cable_index(substream);
735
736 loopback_timer_stop(dpcm);
737 mutex_lock(&loopback->cable_lock);
738 cable = loopback->cables[substream->number][dev];
739 if (cable->streams[!substream->stream]) {
740 /* other stream is still alive */
741 cable->streams[substream->stream] = NULL;
742 } else {
743 /* free the cable */
744 loopback->cables[substream->number][dev] = NULL;
745 kfree(cable);
746 }
747 mutex_unlock(&loopback->cable_lock);
748 return 0;
749}
750
751static struct snd_pcm_ops loopback_playback_ops = {
752 .open = loopback_open,
753 .close = loopback_close,
754 .ioctl = snd_pcm_lib_ioctl,
755 .hw_params = loopback_hw_params,
756 .hw_free = loopback_hw_free,
757 .prepare = loopback_prepare,
758 .trigger = loopback_trigger,
759 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200760 .page = snd_pcm_lib_get_vmalloc_page,
761 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200762};
763
764static struct snd_pcm_ops loopback_capture_ops = {
765 .open = loopback_open,
766 .close = loopback_close,
767 .ioctl = snd_pcm_lib_ioctl,
768 .hw_params = loopback_hw_params,
769 .hw_free = loopback_hw_free,
770 .prepare = loopback_prepare,
771 .trigger = loopback_trigger,
772 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200773 .page = snd_pcm_lib_get_vmalloc_page,
774 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200775};
776
777static int __devinit loopback_pcm_new(struct loopback *loopback,
778 int device, int substreams)
779{
780 struct snd_pcm *pcm;
781 int err;
782
783 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
784 substreams, substreams, &pcm);
785 if (err < 0)
786 return err;
787 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
788 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
789
790 pcm->private_data = loopback;
791 pcm->info_flags = 0;
792 strcpy(pcm->name, "Loopback PCM");
793
794 loopback->pcm[device] = pcm;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200795 return 0;
796}
797
798static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
799 struct snd_ctl_elem_info *uinfo)
800{
801 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
802 uinfo->count = 1;
803 uinfo->value.integer.min = 80000;
804 uinfo->value.integer.max = 120000;
805 uinfo->value.integer.step = 1;
806 return 0;
807}
808
809static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
810 struct snd_ctl_elem_value *ucontrol)
811{
812 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
813
814 ucontrol->value.integer.value[0] =
815 loopback->setup[kcontrol->id.subdevice]
816 [kcontrol->id.device].rate_shift;
817 return 0;
818}
819
820static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
821 struct snd_ctl_elem_value *ucontrol)
822{
823 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
824 unsigned int val;
825 int change = 0;
826
827 val = ucontrol->value.integer.value[0];
828 if (val < 80000)
829 val = 80000;
830 if (val > 120000)
831 val = 120000;
832 mutex_lock(&loopback->cable_lock);
833 if (val != loopback->setup[kcontrol->id.subdevice]
834 [kcontrol->id.device].rate_shift) {
835 loopback->setup[kcontrol->id.subdevice]
836 [kcontrol->id.device].rate_shift = val;
837 change = 1;
838 }
839 mutex_unlock(&loopback->cable_lock);
840 return change;
841}
842
843static int loopback_notify_get(struct snd_kcontrol *kcontrol,
844 struct snd_ctl_elem_value *ucontrol)
845{
846 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
847
848 ucontrol->value.integer.value[0] =
849 loopback->setup[kcontrol->id.subdevice]
850 [kcontrol->id.device].notify;
851 return 0;
852}
853
854static int loopback_notify_put(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_value *ucontrol)
856{
857 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
858 unsigned int val;
859 int change = 0;
860
861 val = ucontrol->value.integer.value[0] ? 1 : 0;
862 if (val != loopback->setup[kcontrol->id.subdevice]
863 [kcontrol->id.device].notify) {
864 loopback->setup[kcontrol->id.subdevice]
865 [kcontrol->id.device].notify = val;
866 change = 1;
867 }
868 return change;
869}
870
871static int loopback_active_get(struct snd_kcontrol *kcontrol,
872 struct snd_ctl_elem_value *ucontrol)
873{
874 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
875 struct loopback_cable *cable = loopback->cables
Jaroslav Kyselaac446fb2010-10-02 16:00:53 +0200876 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200877 unsigned int val = 0;
878
879 if (cable != NULL)
880 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
881 1 : 0;
882 ucontrol->value.integer.value[0] = val;
883 return 0;
884}
885
886static int loopback_format_info(struct snd_kcontrol *kcontrol,
887 struct snd_ctl_elem_info *uinfo)
888{
889 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
890 uinfo->count = 1;
891 uinfo->value.integer.min = 0;
892 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
893 uinfo->value.integer.step = 1;
894 return 0;
895}
896
897static int loopback_format_get(struct snd_kcontrol *kcontrol,
898 struct snd_ctl_elem_value *ucontrol)
899{
900 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
901
902 ucontrol->value.integer.value[0] =
903 loopback->setup[kcontrol->id.subdevice]
904 [kcontrol->id.device].format;
905 return 0;
906}
907
908static int loopback_rate_info(struct snd_kcontrol *kcontrol,
909 struct snd_ctl_elem_info *uinfo)
910{
911 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
912 uinfo->count = 1;
913 uinfo->value.integer.min = 0;
914 uinfo->value.integer.max = 192000;
915 uinfo->value.integer.step = 1;
916 return 0;
917}
918
919static int loopback_rate_get(struct snd_kcontrol *kcontrol,
920 struct snd_ctl_elem_value *ucontrol)
921{
922 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
923
924 ucontrol->value.integer.value[0] =
925 loopback->setup[kcontrol->id.subdevice]
926 [kcontrol->id.device].rate;
927 return 0;
928}
929
930static int loopback_channels_info(struct snd_kcontrol *kcontrol,
931 struct snd_ctl_elem_info *uinfo)
932{
933 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
934 uinfo->count = 1;
935 uinfo->value.integer.min = 1;
936 uinfo->value.integer.max = 1024;
937 uinfo->value.integer.step = 1;
938 return 0;
939}
940
941static int loopback_channels_get(struct snd_kcontrol *kcontrol,
942 struct snd_ctl_elem_value *ucontrol)
943{
944 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
945
946 ucontrol->value.integer.value[0] =
947 loopback->setup[kcontrol->id.subdevice]
Jaroslav Kysela1446c5f2010-09-15 08:01:57 +0200948 [kcontrol->id.device].channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200949 return 0;
950}
951
952static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
953{
954 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
955 .name = "PCM Rate Shift 100000",
956 .info = loopback_rate_shift_info,
957 .get = loopback_rate_shift_get,
958 .put = loopback_rate_shift_put,
959},
960{
961 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
962 .name = "PCM Notify",
963 .info = snd_ctl_boolean_mono_info,
964 .get = loopback_notify_get,
965 .put = loopback_notify_put,
966},
967#define ACTIVE_IDX 2
968{
969 .access = SNDRV_CTL_ELEM_ACCESS_READ,
970 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
971 .name = "PCM Slave Active",
972 .info = snd_ctl_boolean_mono_info,
973 .get = loopback_active_get,
974},
975#define FORMAT_IDX 3
976{
977 .access = SNDRV_CTL_ELEM_ACCESS_READ,
978 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
979 .name = "PCM Slave Format",
980 .info = loopback_format_info,
981 .get = loopback_format_get
982},
983#define RATE_IDX 4
984{
985 .access = SNDRV_CTL_ELEM_ACCESS_READ,
986 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
987 .name = "PCM Slave Rate",
988 .info = loopback_rate_info,
989 .get = loopback_rate_get
990},
991#define CHANNELS_IDX 5
992{
993 .access = SNDRV_CTL_ELEM_ACCESS_READ,
994 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
995 .name = "PCM Slave Channels",
996 .info = loopback_channels_info,
997 .get = loopback_channels_get
998}
999};
1000
1001static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
1002{
1003 struct snd_card *card = loopback->card;
1004 struct snd_pcm *pcm;
1005 struct snd_kcontrol *kctl;
1006 struct loopback_setup *setup;
1007 int err, dev, substr, substr_count, idx;
1008
1009 strcpy(card->mixername, "Loopback Mixer");
1010 for (dev = 0; dev < 2; dev++) {
1011 pcm = loopback->pcm[dev];
1012 substr_count =
1013 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1014 for (substr = 0; substr < substr_count; substr++) {
1015 setup = &loopback->setup[substr][dev];
1016 setup->notify = notify;
1017 setup->rate_shift = NO_PITCH;
1018 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1019 setup->rate = 48000;
1020 setup->channels = 2;
1021 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1022 idx++) {
1023 kctl = snd_ctl_new1(&loopback_controls[idx],
1024 loopback);
1025 if (!kctl)
1026 return -ENOMEM;
1027 kctl->id.device = dev;
1028 kctl->id.subdevice = substr;
1029 switch (idx) {
1030 case ACTIVE_IDX:
1031 setup->active_id = kctl->id;
1032 break;
1033 case FORMAT_IDX:
1034 setup->format_id = kctl->id;
1035 break;
1036 case RATE_IDX:
1037 setup->rate_id = kctl->id;
1038 break;
1039 case CHANNELS_IDX:
1040 setup->channels_id = kctl->id;
1041 break;
1042 default:
1043 break;
1044 }
1045 err = snd_ctl_add(card, kctl);
1046 if (err < 0)
1047 return err;
1048 }
1049 }
1050 }
1051 return 0;
1052}
1053
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001054#ifdef CONFIG_PROC_FS
1055
1056static void print_dpcm_info(struct snd_info_buffer *buffer,
1057 struct loopback_pcm *dpcm,
1058 const char *id)
1059{
1060 snd_iprintf(buffer, " %s\n", id);
1061 if (dpcm == NULL) {
1062 snd_iprintf(buffer, " inactive\n");
1063 return;
1064 }
1065 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1066 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1067 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1068 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1069 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1070 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1071 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1072 snd_iprintf(buffer, " update_pending:\t%u\n",
1073 dpcm->period_update_pending);
1074 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1075 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1076 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1077 dpcm->last_jiffies, jiffies);
1078 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1079}
1080
1081static void print_substream_info(struct snd_info_buffer *buffer,
1082 struct loopback *loopback,
1083 int sub,
1084 int num)
1085{
1086 struct loopback_cable *cable = loopback->cables[sub][num];
1087
1088 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1089 if (cable == NULL) {
1090 snd_iprintf(buffer, " inactive\n");
1091 return;
1092 }
1093 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1094 snd_iprintf(buffer, " running: %u\n", cable->running);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +02001095 snd_iprintf(buffer, " pause: %u\n", cable->pause);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001096 print_dpcm_info(buffer, cable->streams[0], "Playback");
1097 print_dpcm_info(buffer, cable->streams[1], "Capture");
1098}
1099
1100static void print_cable_info(struct snd_info_entry *entry,
1101 struct snd_info_buffer *buffer)
1102{
1103 struct loopback *loopback = entry->private_data;
1104 int sub, num;
1105
1106 mutex_lock(&loopback->cable_lock);
1107 num = entry->name[strlen(entry->name)-1];
1108 num = num == '0' ? 0 : 1;
1109 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1110 print_substream_info(buffer, loopback, sub, num);
1111 mutex_unlock(&loopback->cable_lock);
1112}
1113
1114static int __devinit loopback_proc_new(struct loopback *loopback, int cidx)
1115{
1116 char name[32];
1117 struct snd_info_entry *entry;
1118 int err;
1119
1120 snprintf(name, sizeof(name), "cable#%d", cidx);
1121 err = snd_card_proc_new(loopback->card, name, &entry);
1122 if (err < 0)
1123 return err;
1124
1125 snd_info_set_text_ops(entry, loopback, print_cable_info);
1126 return 0;
1127}
1128
1129#else /* !CONFIG_PROC_FS */
1130
1131#define loopback_proc_new(loopback, cidx) do { } while (0)
1132
1133#endif
1134
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001135static int __devinit loopback_probe(struct platform_device *devptr)
1136{
1137 struct snd_card *card;
1138 struct loopback *loopback;
1139 int dev = devptr->id;
1140 int err;
1141
1142 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
1143 sizeof(struct loopback), &card);
1144 if (err < 0)
1145 return err;
1146 loopback = card->private_data;
1147
1148 if (pcm_substreams[dev] < 1)
1149 pcm_substreams[dev] = 1;
1150 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1151 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1152
1153 loopback->card = card;
1154 mutex_init(&loopback->cable_lock);
1155
1156 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1157 if (err < 0)
1158 goto __nodev;
1159 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1160 if (err < 0)
1161 goto __nodev;
1162 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1163 if (err < 0)
1164 goto __nodev;
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001165 loopback_proc_new(loopback, 0);
1166 loopback_proc_new(loopback, 1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001167 strcpy(card->driver, "Loopback");
1168 strcpy(card->shortname, "Loopback");
1169 sprintf(card->longname, "Loopback %i", dev + 1);
1170 err = snd_card_register(card);
1171 if (!err) {
1172 platform_set_drvdata(devptr, card);
1173 return 0;
1174 }
1175 __nodev:
1176 snd_card_free(card);
1177 return err;
1178}
1179
1180static int __devexit loopback_remove(struct platform_device *devptr)
1181{
1182 snd_card_free(platform_get_drvdata(devptr));
1183 platform_set_drvdata(devptr, NULL);
1184 return 0;
1185}
1186
Takashi Iwaid34e4e02012-08-09 15:47:15 +02001187#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001188static int loopback_suspend(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001189{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001190 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001191 struct loopback *loopback = card->private_data;
1192
1193 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1194
1195 snd_pcm_suspend_all(loopback->pcm[0]);
1196 snd_pcm_suspend_all(loopback->pcm[1]);
1197 return 0;
1198}
1199
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001200static int loopback_resume(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001201{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001202 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001203
1204 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1205 return 0;
1206}
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001207
1208static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1209#define LOOPBACK_PM_OPS &loopback_pm
1210#else
1211#define LOOPBACK_PM_OPS NULL
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001212#endif
1213
1214#define SND_LOOPBACK_DRIVER "snd_aloop"
1215
1216static struct platform_driver loopback_driver = {
1217 .probe = loopback_probe,
1218 .remove = __devexit_p(loopback_remove),
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001219 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +02001220 .name = SND_LOOPBACK_DRIVER,
1221 .owner = THIS_MODULE,
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001222 .pm = LOOPBACK_PM_OPS,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001223 },
1224};
1225
1226static void loopback_unregister_all(void)
1227{
1228 int i;
1229
1230 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1231 platform_device_unregister(devices[i]);
1232 platform_driver_unregister(&loopback_driver);
1233}
1234
1235static int __init alsa_card_loopback_init(void)
1236{
1237 int i, err, cards;
1238
1239 err = platform_driver_register(&loopback_driver);
1240 if (err < 0)
1241 return err;
1242
1243
1244 cards = 0;
1245 for (i = 0; i < SNDRV_CARDS; i++) {
1246 struct platform_device *device;
1247 if (!enable[i])
1248 continue;
1249 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1250 i, NULL, 0);
1251 if (IS_ERR(device))
1252 continue;
1253 if (!platform_get_drvdata(device)) {
1254 platform_device_unregister(device);
1255 continue;
1256 }
1257 devices[i] = device;
1258 cards++;
1259 }
1260 if (!cards) {
1261#ifdef MODULE
1262 printk(KERN_ERR "aloop: No loopback enabled\n");
1263#endif
1264 loopback_unregister_all();
1265 return -ENODEV;
1266 }
1267 return 0;
1268}
1269
1270static void __exit alsa_card_loopback_exit(void)
1271{
1272 loopback_unregister_all();
1273}
1274
1275module_init(alsa_card_loopback_init)
1276module_exit(alsa_card_loopback_exit)