blob: 0fe6d64ff840f02cdb17df4c27721f8e9a953cb5 [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;
Omair Mohammed Abdullahd4f1e482012-09-29 12:24:05 +0530123 spinlock_t timer_lock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200124};
125
126static struct platform_device *devices[SNDRV_CARDS];
127
128static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
129{
130 if (dpcm->pcm_rate_shift == NO_PITCH) {
131 x /= HZ;
132 } else {
133 x = div_u64(NO_PITCH * (unsigned long long)x,
134 HZ * (unsigned long long)dpcm->pcm_rate_shift);
135 }
136 return x - (x % dpcm->pcm_salign);
137}
138
139static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
140{
141 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
142 return x * HZ;
143 } else {
144 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
145 NO_PITCH);
146 }
147 return x;
148}
149
150static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
151{
152 int device = dpcm->substream->pstr->pcm->device;
153
154 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
155 device ^= 1;
156 return &dpcm->loopback->setup[dpcm->substream->number][device];
157}
158
159static inline unsigned int get_notify(struct loopback_pcm *dpcm)
160{
161 return get_setup(dpcm)->notify;
162}
163
164static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
165{
166 return get_setup(dpcm)->rate_shift;
167}
168
169static void loopback_timer_start(struct loopback_pcm *dpcm)
170{
171 unsigned long tick;
172 unsigned int rate_shift = get_rate_shift(dpcm);
173
Omair Mohammed Abdullahd4f1e482012-09-29 12:24:05 +0530174 spin_lock(&dpcm->timer_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200175 if (rate_shift != dpcm->pcm_rate_shift) {
176 dpcm->pcm_rate_shift = rate_shift;
177 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
178 }
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200179 if (dpcm->period_size_frac <= dpcm->irq_pos) {
180 dpcm->irq_pos %= dpcm->period_size_frac;
181 dpcm->period_update_pending = 1;
182 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200183 tick = dpcm->period_size_frac - dpcm->irq_pos;
184 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
185 dpcm->timer.expires = jiffies + tick;
186 add_timer(&dpcm->timer);
Omair Mohammed Abdullahd4f1e482012-09-29 12:24:05 +0530187 spin_unlock(&dpcm->timer_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200188}
189
190static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
191{
Omair Mohammed Abdullahd4f1e482012-09-29 12:24:05 +0530192 spin_lock(&dpcm->timer_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200193 del_timer(&dpcm->timer);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +0200194 dpcm->timer.expires = 0;
Omair Mohammed Abdullahd4f1e482012-09-29 12:24:05 +0530195 spin_unlock(&dpcm->timer_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200196}
197
198#define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
199#define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
200#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
201
202static int loopback_check_format(struct loopback_cable *cable, int stream)
203{
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200204 struct snd_pcm_runtime *runtime, *cruntime;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200205 struct loopback_setup *setup;
206 struct snd_card *card;
207 int check;
208
209 if (cable->valid != CABLE_VALID_BOTH) {
210 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
211 goto __notify;
212 return 0;
213 }
214 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
215 substream->runtime;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200216 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
217 substream->runtime;
218 check = runtime->format != cruntime->format ||
219 runtime->rate != cruntime->rate ||
220 runtime->channels != cruntime->channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200221 if (!check)
222 return 0;
223 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
224 return -EIO;
225 } else {
226 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
227 substream, SNDRV_PCM_STATE_DRAINING);
228 __notify:
229 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
230 substream->runtime;
231 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
232 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
233 if (setup->format != runtime->format) {
234 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
235 &setup->format_id);
236 setup->format = runtime->format;
237 }
238 if (setup->rate != runtime->rate) {
239 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
240 &setup->rate_id);
241 setup->rate = runtime->rate;
242 }
243 if (setup->channels != runtime->channels) {
244 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
245 &setup->channels_id);
246 setup->channels = runtime->channels;
247 }
248 }
249 return 0;
250}
251
252static void loopback_active_notify(struct loopback_pcm *dpcm)
253{
254 snd_ctl_notify(dpcm->loopback->card,
255 SNDRV_CTL_EVENT_MASK_VALUE,
256 &get_setup(dpcm)->active_id);
257}
258
259static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
260{
261 struct snd_pcm_runtime *runtime = substream->runtime;
262 struct loopback_pcm *dpcm = runtime->private_data;
263 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200264 int err, stream = 1 << substream->stream;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200265
266 switch (cmd) {
267 case SNDRV_PCM_TRIGGER_START:
268 err = loopback_check_format(cable, substream->stream);
269 if (err < 0)
270 return err;
271 dpcm->last_jiffies = jiffies;
272 dpcm->pcm_rate_shift = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200273 dpcm->last_drift = 0;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200274 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200275 cable->running |= stream;
276 cable->pause &= ~stream;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200277 spin_unlock(&cable->lock);
278 loopback_timer_start(dpcm);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200279 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
280 loopback_active_notify(dpcm);
281 break;
282 case SNDRV_PCM_TRIGGER_STOP:
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200283 spin_lock(&cable->lock);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200284 cable->running &= ~stream;
285 cable->pause &= ~stream;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200286 spin_unlock(&cable->lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200287 loopback_timer_stop(dpcm);
288 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
289 loopback_active_notify(dpcm);
290 break;
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200291 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
292 spin_lock(&cable->lock);
293 cable->pause |= stream;
294 spin_unlock(&cable->lock);
295 loopback_timer_stop(dpcm);
296 break;
297 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
298 spin_lock(&cable->lock);
299 dpcm->last_jiffies = jiffies;
300 cable->pause &= ~stream;
301 spin_unlock(&cable->lock);
302 loopback_timer_start(dpcm);
303 break;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200304 default:
305 return -EINVAL;
306 }
307 return 0;
308}
309
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200310static void params_change_substream(struct loopback_pcm *dpcm,
311 struct snd_pcm_runtime *runtime)
312{
313 struct snd_pcm_runtime *dst_runtime;
314
315 if (dpcm == NULL || dpcm->substream == NULL)
316 return;
317 dst_runtime = dpcm->substream->runtime;
318 if (dst_runtime == NULL)
319 return;
320 dst_runtime->hw = dpcm->cable->hw;
321}
322
323static void params_change(struct snd_pcm_substream *substream)
324{
325 struct snd_pcm_runtime *runtime = substream->runtime;
326 struct loopback_pcm *dpcm = runtime->private_data;
327 struct loopback_cable *cable = dpcm->cable;
328
329 cable->hw.formats = (1ULL << runtime->format);
330 cable->hw.rate_min = runtime->rate;
331 cable->hw.rate_max = runtime->rate;
332 cable->hw.channels_min = runtime->channels;
333 cable->hw.channels_max = runtime->channels;
334 params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
335 runtime);
336 params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
337 runtime);
338}
339
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200340static int loopback_prepare(struct snd_pcm_substream *substream)
341{
342 struct snd_pcm_runtime *runtime = substream->runtime;
343 struct loopback_pcm *dpcm = runtime->private_data;
344 struct loopback_cable *cable = dpcm->cable;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200345 int bps, salign;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200346
347 salign = (snd_pcm_format_width(runtime->format) *
348 runtime->channels) / 8;
349 bps = salign * runtime->rate;
350 if (bps <= 0 || salign <= 0)
351 return -EINVAL;
352
353 dpcm->buf_pos = 0;
354 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
355 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
356 /* clear capture buffer */
357 dpcm->silent_size = dpcm->pcm_buffer_size;
358 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
359 runtime->buffer_size * runtime->channels);
360 }
361
362 dpcm->irq_pos = 0;
363 dpcm->period_update_pending = 0;
364 dpcm->pcm_bps = bps;
365 dpcm->pcm_salign = salign;
366 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
367
368 mutex_lock(&dpcm->loopback->cable_lock);
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200369 if (!(cable->valid & ~(1 << substream->stream)) ||
370 (get_setup(dpcm)->notify &&
371 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
372 params_change(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200373 cable->valid |= 1 << substream->stream;
374 mutex_unlock(&dpcm->loopback->cable_lock);
375
376 return 0;
377}
378
379static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
380{
381 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
382 char *dst = runtime->dma_area;
383 unsigned int dst_off = dpcm->buf_pos;
384
385 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
386 return;
387 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
388 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
389
390 for (;;) {
391 unsigned int size = bytes;
392 if (dst_off + size > dpcm->pcm_buffer_size)
393 size = dpcm->pcm_buffer_size - dst_off;
394 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
395 bytes_to_frames(runtime, size) *
396 runtime->channels);
397 dpcm->silent_size += size;
398 bytes -= size;
399 if (!bytes)
400 break;
401 dst_off = 0;
402 }
403}
404
405static void copy_play_buf(struct loopback_pcm *play,
406 struct loopback_pcm *capt,
407 unsigned int bytes)
408{
409 struct snd_pcm_runtime *runtime = play->substream->runtime;
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200410 char *src = runtime->dma_area;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200411 char *dst = capt->substream->runtime->dma_area;
412 unsigned int src_off = play->buf_pos;
413 unsigned int dst_off = capt->buf_pos;
414 unsigned int clear_bytes = 0;
415
416 /* check if playback is draining, trim the capture copy size
417 * when our pointer is at the end of playback ring buffer */
418 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
419 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
420 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
421 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
422 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
423 appl_ptr1 += play->buf_pos / play->pcm_salign;
424 if (appl_ptr < appl_ptr1)
425 appl_ptr1 -= runtime->buffer_size;
426 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
427 if (diff < bytes) {
428 clear_bytes = bytes - diff;
429 bytes = diff;
430 }
431 }
432
433 for (;;) {
434 unsigned int size = bytes;
435 if (src_off + size > play->pcm_buffer_size)
436 size = play->pcm_buffer_size - src_off;
437 if (dst_off + size > capt->pcm_buffer_size)
438 size = capt->pcm_buffer_size - dst_off;
439 memcpy(dst + dst_off, src + src_off, size);
440 capt->silent_size = 0;
441 bytes -= size;
442 if (!bytes)
443 break;
444 src_off = (src_off + size) % play->pcm_buffer_size;
445 dst_off = (dst_off + size) % capt->pcm_buffer_size;
446 }
447
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200448 if (clear_bytes > 0) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200449 clear_capture_buf(capt, clear_bytes);
Jaroslav Kysela20d9a262010-09-30 00:16:50 +0200450 capt->silent_size = 0;
451 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200452}
453
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200454static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
455 unsigned int jiffies_delta)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200456{
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200457 unsigned long last_pos;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200458 unsigned int delta;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200459
460 last_pos = byte_pos(dpcm, dpcm->irq_pos);
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200461 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
462 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
463 if (delta >= dpcm->last_drift)
464 delta -= dpcm->last_drift;
465 dpcm->last_drift = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200466 if (dpcm->irq_pos >= dpcm->period_size_frac) {
467 dpcm->irq_pos %= dpcm->period_size_frac;
468 dpcm->period_update_pending = 1;
469 }
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200470 return delta;
471}
472
473static inline void bytepos_finish(struct loopback_pcm *dpcm,
474 unsigned int delta)
475{
476 dpcm->buf_pos += delta;
477 dpcm->buf_pos %= dpcm->pcm_buffer_size;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200478}
479
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200480static unsigned int loopback_pos_update(struct loopback_cable *cable)
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200481{
482 struct loopback_pcm *dpcm_play =
483 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
484 struct loopback_pcm *dpcm_capt =
485 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
486 unsigned long delta_play = 0, delta_capt = 0;
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200487 unsigned int running, count1, count2;
Takashi Iwai98d21df2011-03-18 07:31:53 +0100488 unsigned long flags;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200489
Takashi Iwai98d21df2011-03-18 07:31:53 +0100490 spin_lock_irqsave(&cable->lock, flags);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200491 running = cable->running ^ cable->pause;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200492 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200493 delta_play = jiffies - dpcm_play->last_jiffies;
494 dpcm_play->last_jiffies += delta_play;
495 }
496
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200497 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200498 delta_capt = jiffies - dpcm_capt->last_jiffies;
499 dpcm_capt->last_jiffies += delta_capt;
500 }
501
Takashi Iwai98d21df2011-03-18 07:31:53 +0100502 if (delta_play == 0 && delta_capt == 0)
503 goto unlock;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200504
505 if (delta_play > delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200506 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
507 bytepos_finish(dpcm_play, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200508 delta_play = delta_capt;
509 } else if (delta_play < delta_capt) {
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200510 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
511 clear_capture_buf(dpcm_capt, count1);
512 bytepos_finish(dpcm_capt, count1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200513 delta_capt = delta_play;
514 }
515
Takashi Iwai98d21df2011-03-18 07:31:53 +0100516 if (delta_play == 0 && delta_capt == 0)
517 goto unlock;
518
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200519 /* note delta_capt == delta_play at this moment */
Jaroslav Kyselab0125132012-05-13 13:39:45 +0200520 count1 = bytepos_delta(dpcm_play, delta_play);
521 count2 = bytepos_delta(dpcm_capt, delta_capt);
522 if (count1 < count2) {
523 dpcm_capt->last_drift = count2 - count1;
524 count1 = count2;
525 } else if (count1 > count2) {
526 dpcm_play->last_drift = count1 - count2;
527 }
528 copy_play_buf(dpcm_play, dpcm_capt, count1);
529 bytepos_finish(dpcm_play, count1);
530 bytepos_finish(dpcm_capt, count1);
Takashi Iwai98d21df2011-03-18 07:31:53 +0100531 unlock:
532 spin_unlock_irqrestore(&cable->lock, flags);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200533 return running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200534}
535
536static void loopback_timer_function(unsigned long data)
537{
538 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200539 unsigned int running;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200540
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200541 running = loopback_pos_update(dpcm->cable);
542 if (running & (1 << dpcm->substream->stream)) {
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200543 loopback_timer_start(dpcm);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200544 if (dpcm->period_update_pending) {
545 dpcm->period_update_pending = 0;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200546 snd_pcm_period_elapsed(dpcm->substream);
Jaroslav Kyseladd04bb12010-10-20 08:27:02 +0200547 }
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200548 }
549}
550
551static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
552{
553 struct snd_pcm_runtime *runtime = substream->runtime;
554 struct loopback_pcm *dpcm = runtime->private_data;
555
556 loopback_pos_update(dpcm->cable);
557 return bytes_to_frames(runtime, dpcm->buf_pos);
558}
559
560static struct snd_pcm_hardware loopback_pcm_hardware =
561{
562 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
Jaroslav Kysela5de9e452010-10-20 09:33:03 +0200563 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200564 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
565 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
566 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
567 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
568 .rate_min = 8000,
569 .rate_max = 192000,
570 .channels_min = 1,
571 .channels_max = 32,
572 .buffer_bytes_max = 2 * 1024 * 1024,
573 .period_bytes_min = 64,
Jaroslav Kysela0db71022010-10-14 21:46:12 +0200574 /* note check overflow in frac_pos() using pcm_rate_shift before
575 changing period_bytes_max value */
576 .period_bytes_max = 1024 * 1024,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200577 .periods_min = 1,
578 .periods_max = 1024,
579 .fifo_size = 0,
580};
581
582static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
583{
584 struct loopback_pcm *dpcm = runtime->private_data;
585 kfree(dpcm);
586}
587
588static int loopback_hw_params(struct snd_pcm_substream *substream,
589 struct snd_pcm_hw_params *params)
590{
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200591 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
592 params_buffer_bytes(params));
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200593}
594
595static int loopback_hw_free(struct snd_pcm_substream *substream)
596{
597 struct snd_pcm_runtime *runtime = substream->runtime;
598 struct loopback_pcm *dpcm = runtime->private_data;
599 struct loopback_cable *cable = dpcm->cable;
600
601 mutex_lock(&dpcm->loopback->cable_lock);
602 cable->valid &= ~(1 << substream->stream);
603 mutex_unlock(&dpcm->loopback->cable_lock);
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200604 return snd_pcm_lib_free_vmalloc_buffer(substream);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200605}
606
607static unsigned int get_cable_index(struct snd_pcm_substream *substream)
608{
609 if (!substream->pcm->device)
610 return substream->stream;
611 else
612 return !substream->stream;
613}
614
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200615static int rule_format(struct snd_pcm_hw_params *params,
616 struct snd_pcm_hw_rule *rule)
617{
618
619 struct snd_pcm_hardware *hw = rule->private;
620 struct snd_mask *maskp = hw_param_mask(params, rule->var);
621
622 maskp->bits[0] &= (u_int32_t)hw->formats;
623 maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
624 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
625 if (! maskp->bits[0] && ! maskp->bits[1])
626 return -EINVAL;
627 return 0;
628}
629
630static int rule_rate(struct snd_pcm_hw_params *params,
631 struct snd_pcm_hw_rule *rule)
632{
633 struct snd_pcm_hardware *hw = rule->private;
634 struct snd_interval t;
635
636 t.min = hw->rate_min;
637 t.max = hw->rate_max;
638 t.openmin = t.openmax = 0;
639 t.integer = 0;
640 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
641}
642
643static int rule_channels(struct snd_pcm_hw_params *params,
644 struct snd_pcm_hw_rule *rule)
645{
646 struct snd_pcm_hardware *hw = rule->private;
647 struct snd_interval t;
648
649 t.min = hw->channels_min;
650 t.max = hw->channels_max;
651 t.openmin = t.openmax = 0;
652 t.integer = 0;
653 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
654}
655
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200656static int loopback_open(struct snd_pcm_substream *substream)
657{
658 struct snd_pcm_runtime *runtime = substream->runtime;
659 struct loopback *loopback = substream->private_data;
660 struct loopback_pcm *dpcm;
661 struct loopback_cable *cable;
662 int err = 0;
663 int dev = get_cable_index(substream);
664
665 mutex_lock(&loopback->cable_lock);
666 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
667 if (!dpcm) {
668 err = -ENOMEM;
669 goto unlock;
670 }
671 dpcm->loopback = loopback;
672 dpcm->substream = substream;
673 setup_timer(&dpcm->timer, loopback_timer_function,
674 (unsigned long)dpcm);
Omair Mohammed Abdullahd4f1e482012-09-29 12:24:05 +0530675 spin_lock_init(&dpcm->timer_lock);
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200676
677 cable = loopback->cables[substream->number][dev];
678 if (!cable) {
679 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
680 if (!cable) {
681 kfree(dpcm);
682 err = -ENOMEM;
683 goto unlock;
684 }
685 spin_lock_init(&cable->lock);
686 cable->hw = loopback_pcm_hardware;
687 loopback->cables[substream->number][dev] = cable;
688 }
689 dpcm->cable = cable;
690 cable->streams[substream->stream] = dpcm;
691
692 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
693
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200694 /* use dynamic rules based on actual runtime->hw values */
695 /* note that the default rules created in the PCM midlevel code */
696 /* are cached -> they do not reflect the actual state */
697 err = snd_pcm_hw_rule_add(runtime, 0,
698 SNDRV_PCM_HW_PARAM_FORMAT,
699 rule_format, &runtime->hw,
700 SNDRV_PCM_HW_PARAM_FORMAT, -1);
701 if (err < 0)
702 goto unlock;
703 err = snd_pcm_hw_rule_add(runtime, 0,
704 SNDRV_PCM_HW_PARAM_RATE,
705 rule_rate, &runtime->hw,
706 SNDRV_PCM_HW_PARAM_RATE, -1);
707 if (err < 0)
708 goto unlock;
709 err = snd_pcm_hw_rule_add(runtime, 0,
710 SNDRV_PCM_HW_PARAM_CHANNELS,
711 rule_channels, &runtime->hw,
712 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
713 if (err < 0)
714 goto unlock;
715
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200716 runtime->private_data = dpcm;
717 runtime->private_free = loopback_runtime_free;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200718 if (get_notify(dpcm))
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200719 runtime->hw = loopback_pcm_hardware;
Jaroslav Kyselab1c73fc2010-10-11 10:45:00 +0200720 else
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200721 runtime->hw = cable->hw;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200722 unlock:
723 mutex_unlock(&loopback->cable_lock);
724 return err;
725}
726
727static int loopback_close(struct snd_pcm_substream *substream)
728{
729 struct loopback *loopback = substream->private_data;
730 struct loopback_pcm *dpcm = substream->runtime->private_data;
731 struct loopback_cable *cable;
732 int dev = get_cable_index(substream);
733
734 loopback_timer_stop(dpcm);
735 mutex_lock(&loopback->cable_lock);
736 cable = loopback->cables[substream->number][dev];
737 if (cable->streams[!substream->stream]) {
738 /* other stream is still alive */
739 cable->streams[substream->stream] = NULL;
740 } else {
741 /* free the cable */
742 loopback->cables[substream->number][dev] = NULL;
743 kfree(cable);
744 }
745 mutex_unlock(&loopback->cable_lock);
746 return 0;
747}
748
749static struct snd_pcm_ops loopback_playback_ops = {
750 .open = loopback_open,
751 .close = loopback_close,
752 .ioctl = snd_pcm_lib_ioctl,
753 .hw_params = loopback_hw_params,
754 .hw_free = loopback_hw_free,
755 .prepare = loopback_prepare,
756 .trigger = loopback_trigger,
757 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200758 .page = snd_pcm_lib_get_vmalloc_page,
759 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200760};
761
762static struct snd_pcm_ops loopback_capture_ops = {
763 .open = loopback_open,
764 .close = loopback_close,
765 .ioctl = snd_pcm_lib_ioctl,
766 .hw_params = loopback_hw_params,
767 .hw_free = loopback_hw_free,
768 .prepare = loopback_prepare,
769 .trigger = loopback_trigger,
770 .pointer = loopback_pointer,
Takashi Iwai6b69a0e2011-09-24 12:16:29 +0200771 .page = snd_pcm_lib_get_vmalloc_page,
772 .mmap = snd_pcm_lib_mmap_vmalloc,
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200773};
774
775static int __devinit loopback_pcm_new(struct loopback *loopback,
776 int device, int substreams)
777{
778 struct snd_pcm *pcm;
779 int err;
780
781 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
782 substreams, substreams, &pcm);
783 if (err < 0)
784 return err;
785 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
786 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
787
788 pcm->private_data = loopback;
789 pcm->info_flags = 0;
790 strcpy(pcm->name, "Loopback PCM");
791
792 loopback->pcm[device] = pcm;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200793 return 0;
794}
795
796static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
797 struct snd_ctl_elem_info *uinfo)
798{
799 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
800 uinfo->count = 1;
801 uinfo->value.integer.min = 80000;
802 uinfo->value.integer.max = 120000;
803 uinfo->value.integer.step = 1;
804 return 0;
805}
806
807static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
808 struct snd_ctl_elem_value *ucontrol)
809{
810 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
811
812 ucontrol->value.integer.value[0] =
813 loopback->setup[kcontrol->id.subdevice]
814 [kcontrol->id.device].rate_shift;
815 return 0;
816}
817
818static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
819 struct snd_ctl_elem_value *ucontrol)
820{
821 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
822 unsigned int val;
823 int change = 0;
824
825 val = ucontrol->value.integer.value[0];
826 if (val < 80000)
827 val = 80000;
828 if (val > 120000)
829 val = 120000;
830 mutex_lock(&loopback->cable_lock);
831 if (val != loopback->setup[kcontrol->id.subdevice]
832 [kcontrol->id.device].rate_shift) {
833 loopback->setup[kcontrol->id.subdevice]
834 [kcontrol->id.device].rate_shift = val;
835 change = 1;
836 }
837 mutex_unlock(&loopback->cable_lock);
838 return change;
839}
840
841static int loopback_notify_get(struct snd_kcontrol *kcontrol,
842 struct snd_ctl_elem_value *ucontrol)
843{
844 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
845
846 ucontrol->value.integer.value[0] =
847 loopback->setup[kcontrol->id.subdevice]
848 [kcontrol->id.device].notify;
849 return 0;
850}
851
852static int loopback_notify_put(struct snd_kcontrol *kcontrol,
853 struct snd_ctl_elem_value *ucontrol)
854{
855 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
856 unsigned int val;
857 int change = 0;
858
859 val = ucontrol->value.integer.value[0] ? 1 : 0;
860 if (val != loopback->setup[kcontrol->id.subdevice]
861 [kcontrol->id.device].notify) {
862 loopback->setup[kcontrol->id.subdevice]
863 [kcontrol->id.device].notify = val;
864 change = 1;
865 }
866 return change;
867}
868
869static int loopback_active_get(struct snd_kcontrol *kcontrol,
870 struct snd_ctl_elem_value *ucontrol)
871{
872 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
873 struct loopback_cable *cable = loopback->cables
Jaroslav Kyselaac446fb2010-10-02 16:00:53 +0200874 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200875 unsigned int val = 0;
876
877 if (cable != NULL)
878 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
879 1 : 0;
880 ucontrol->value.integer.value[0] = val;
881 return 0;
882}
883
884static int loopback_format_info(struct snd_kcontrol *kcontrol,
885 struct snd_ctl_elem_info *uinfo)
886{
887 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
888 uinfo->count = 1;
889 uinfo->value.integer.min = 0;
890 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
891 uinfo->value.integer.step = 1;
892 return 0;
893}
894
895static int loopback_format_get(struct snd_kcontrol *kcontrol,
896 struct snd_ctl_elem_value *ucontrol)
897{
898 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
899
900 ucontrol->value.integer.value[0] =
901 loopback->setup[kcontrol->id.subdevice]
902 [kcontrol->id.device].format;
903 return 0;
904}
905
906static int loopback_rate_info(struct snd_kcontrol *kcontrol,
907 struct snd_ctl_elem_info *uinfo)
908{
909 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
910 uinfo->count = 1;
911 uinfo->value.integer.min = 0;
912 uinfo->value.integer.max = 192000;
913 uinfo->value.integer.step = 1;
914 return 0;
915}
916
917static int loopback_rate_get(struct snd_kcontrol *kcontrol,
918 struct snd_ctl_elem_value *ucontrol)
919{
920 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
921
922 ucontrol->value.integer.value[0] =
923 loopback->setup[kcontrol->id.subdevice]
924 [kcontrol->id.device].rate;
925 return 0;
926}
927
928static int loopback_channels_info(struct snd_kcontrol *kcontrol,
929 struct snd_ctl_elem_info *uinfo)
930{
931 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
932 uinfo->count = 1;
933 uinfo->value.integer.min = 1;
934 uinfo->value.integer.max = 1024;
935 uinfo->value.integer.step = 1;
936 return 0;
937}
938
939static int loopback_channels_get(struct snd_kcontrol *kcontrol,
940 struct snd_ctl_elem_value *ucontrol)
941{
942 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
943
944 ucontrol->value.integer.value[0] =
945 loopback->setup[kcontrol->id.subdevice]
Jaroslav Kysela1446c5f2010-09-15 08:01:57 +0200946 [kcontrol->id.device].channels;
Jaroslav Kysela597603d2010-08-09 14:21:11 +0200947 return 0;
948}
949
950static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
951{
952 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
953 .name = "PCM Rate Shift 100000",
954 .info = loopback_rate_shift_info,
955 .get = loopback_rate_shift_get,
956 .put = loopback_rate_shift_put,
957},
958{
959 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
960 .name = "PCM Notify",
961 .info = snd_ctl_boolean_mono_info,
962 .get = loopback_notify_get,
963 .put = loopback_notify_put,
964},
965#define ACTIVE_IDX 2
966{
967 .access = SNDRV_CTL_ELEM_ACCESS_READ,
968 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
969 .name = "PCM Slave Active",
970 .info = snd_ctl_boolean_mono_info,
971 .get = loopback_active_get,
972},
973#define FORMAT_IDX 3
974{
975 .access = SNDRV_CTL_ELEM_ACCESS_READ,
976 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
977 .name = "PCM Slave Format",
978 .info = loopback_format_info,
979 .get = loopback_format_get
980},
981#define RATE_IDX 4
982{
983 .access = SNDRV_CTL_ELEM_ACCESS_READ,
984 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
985 .name = "PCM Slave Rate",
986 .info = loopback_rate_info,
987 .get = loopback_rate_get
988},
989#define CHANNELS_IDX 5
990{
991 .access = SNDRV_CTL_ELEM_ACCESS_READ,
992 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
993 .name = "PCM Slave Channels",
994 .info = loopback_channels_info,
995 .get = loopback_channels_get
996}
997};
998
999static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
1000{
1001 struct snd_card *card = loopback->card;
1002 struct snd_pcm *pcm;
1003 struct snd_kcontrol *kctl;
1004 struct loopback_setup *setup;
1005 int err, dev, substr, substr_count, idx;
1006
1007 strcpy(card->mixername, "Loopback Mixer");
1008 for (dev = 0; dev < 2; dev++) {
1009 pcm = loopback->pcm[dev];
1010 substr_count =
1011 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1012 for (substr = 0; substr < substr_count; substr++) {
1013 setup = &loopback->setup[substr][dev];
1014 setup->notify = notify;
1015 setup->rate_shift = NO_PITCH;
1016 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1017 setup->rate = 48000;
1018 setup->channels = 2;
1019 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1020 idx++) {
1021 kctl = snd_ctl_new1(&loopback_controls[idx],
1022 loopback);
1023 if (!kctl)
1024 return -ENOMEM;
1025 kctl->id.device = dev;
1026 kctl->id.subdevice = substr;
1027 switch (idx) {
1028 case ACTIVE_IDX:
1029 setup->active_id = kctl->id;
1030 break;
1031 case FORMAT_IDX:
1032 setup->format_id = kctl->id;
1033 break;
1034 case RATE_IDX:
1035 setup->rate_id = kctl->id;
1036 break;
1037 case CHANNELS_IDX:
1038 setup->channels_id = kctl->id;
1039 break;
1040 default:
1041 break;
1042 }
1043 err = snd_ctl_add(card, kctl);
1044 if (err < 0)
1045 return err;
1046 }
1047 }
1048 }
1049 return 0;
1050}
1051
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001052#ifdef CONFIG_PROC_FS
1053
1054static void print_dpcm_info(struct snd_info_buffer *buffer,
1055 struct loopback_pcm *dpcm,
1056 const char *id)
1057{
1058 snd_iprintf(buffer, " %s\n", id);
1059 if (dpcm == NULL) {
1060 snd_iprintf(buffer, " inactive\n");
1061 return;
1062 }
1063 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1064 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1065 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1066 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1067 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1068 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1069 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1070 snd_iprintf(buffer, " update_pending:\t%u\n",
1071 dpcm->period_update_pending);
1072 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1073 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1074 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1075 dpcm->last_jiffies, jiffies);
1076 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1077}
1078
1079static void print_substream_info(struct snd_info_buffer *buffer,
1080 struct loopback *loopback,
1081 int sub,
1082 int num)
1083{
1084 struct loopback_cable *cable = loopback->cables[sub][num];
1085
1086 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1087 if (cable == NULL) {
1088 snd_iprintf(buffer, " inactive\n");
1089 return;
1090 }
1091 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1092 snd_iprintf(buffer, " running: %u\n", cable->running);
Jaroslav Kysela5de9e452010-10-20 09:33:03 +02001093 snd_iprintf(buffer, " pause: %u\n", cable->pause);
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001094 print_dpcm_info(buffer, cable->streams[0], "Playback");
1095 print_dpcm_info(buffer, cable->streams[1], "Capture");
1096}
1097
1098static void print_cable_info(struct snd_info_entry *entry,
1099 struct snd_info_buffer *buffer)
1100{
1101 struct loopback *loopback = entry->private_data;
1102 int sub, num;
1103
1104 mutex_lock(&loopback->cable_lock);
1105 num = entry->name[strlen(entry->name)-1];
1106 num = num == '0' ? 0 : 1;
1107 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1108 print_substream_info(buffer, loopback, sub, num);
1109 mutex_unlock(&loopback->cable_lock);
1110}
1111
1112static int __devinit loopback_proc_new(struct loopback *loopback, int cidx)
1113{
1114 char name[32];
1115 struct snd_info_entry *entry;
1116 int err;
1117
1118 snprintf(name, sizeof(name), "cable#%d", cidx);
1119 err = snd_card_proc_new(loopback->card, name, &entry);
1120 if (err < 0)
1121 return err;
1122
1123 snd_info_set_text_ops(entry, loopback, print_cable_info);
1124 return 0;
1125}
1126
1127#else /* !CONFIG_PROC_FS */
1128
1129#define loopback_proc_new(loopback, cidx) do { } while (0)
1130
1131#endif
1132
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001133static int __devinit loopback_probe(struct platform_device *devptr)
1134{
1135 struct snd_card *card;
1136 struct loopback *loopback;
1137 int dev = devptr->id;
1138 int err;
1139
1140 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
1141 sizeof(struct loopback), &card);
1142 if (err < 0)
1143 return err;
1144 loopback = card->private_data;
1145
1146 if (pcm_substreams[dev] < 1)
1147 pcm_substreams[dev] = 1;
1148 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1149 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1150
1151 loopback->card = card;
1152 mutex_init(&loopback->cable_lock);
1153
1154 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1155 if (err < 0)
1156 goto __nodev;
1157 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1158 if (err < 0)
1159 goto __nodev;
1160 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1161 if (err < 0)
1162 goto __nodev;
Jaroslav Kyselae74670b2010-10-18 09:43:10 +02001163 loopback_proc_new(loopback, 0);
1164 loopback_proc_new(loopback, 1);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001165 strcpy(card->driver, "Loopback");
1166 strcpy(card->shortname, "Loopback");
1167 sprintf(card->longname, "Loopback %i", dev + 1);
1168 err = snd_card_register(card);
1169 if (!err) {
1170 platform_set_drvdata(devptr, card);
1171 return 0;
1172 }
1173 __nodev:
1174 snd_card_free(card);
1175 return err;
1176}
1177
1178static int __devexit loopback_remove(struct platform_device *devptr)
1179{
1180 snd_card_free(platform_get_drvdata(devptr));
1181 platform_set_drvdata(devptr, NULL);
1182 return 0;
1183}
1184
Takashi Iwaid34e4e02012-08-09 15:47:15 +02001185#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001186static int loopback_suspend(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001187{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001188 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001189 struct loopback *loopback = card->private_data;
1190
1191 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1192
1193 snd_pcm_suspend_all(loopback->pcm[0]);
1194 snd_pcm_suspend_all(loopback->pcm[1]);
1195 return 0;
1196}
1197
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001198static int loopback_resume(struct device *pdev)
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001199{
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001200 struct snd_card *card = dev_get_drvdata(pdev);
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001201
1202 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1203 return 0;
1204}
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001205
1206static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1207#define LOOPBACK_PM_OPS &loopback_pm
1208#else
1209#define LOOPBACK_PM_OPS NULL
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001210#endif
1211
1212#define SND_LOOPBACK_DRIVER "snd_aloop"
1213
1214static struct platform_driver loopback_driver = {
1215 .probe = loopback_probe,
1216 .remove = __devexit_p(loopback_remove),
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001217 .driver = {
Takashi Iwai8bf01d82012-07-02 10:50:24 +02001218 .name = SND_LOOPBACK_DRIVER,
1219 .owner = THIS_MODULE,
Takashi Iwai284e7ca2012-07-02 11:22:40 +02001220 .pm = LOOPBACK_PM_OPS,
Jaroslav Kysela597603d2010-08-09 14:21:11 +02001221 },
1222};
1223
1224static void loopback_unregister_all(void)
1225{
1226 int i;
1227
1228 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1229 platform_device_unregister(devices[i]);
1230 platform_driver_unregister(&loopback_driver);
1231}
1232
1233static int __init alsa_card_loopback_init(void)
1234{
1235 int i, err, cards;
1236
1237 err = platform_driver_register(&loopback_driver);
1238 if (err < 0)
1239 return err;
1240
1241
1242 cards = 0;
1243 for (i = 0; i < SNDRV_CARDS; i++) {
1244 struct platform_device *device;
1245 if (!enable[i])
1246 continue;
1247 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1248 i, NULL, 0);
1249 if (IS_ERR(device))
1250 continue;
1251 if (!platform_get_drvdata(device)) {
1252 platform_device_unregister(device);
1253 continue;
1254 }
1255 devices[i] = device;
1256 cards++;
1257 }
1258 if (!cards) {
1259#ifdef MODULE
1260 printk(KERN_ERR "aloop: No loopback enabled\n");
1261#endif
1262 loopback_unregister_all();
1263 return -ENODEV;
1264 }
1265 return 0;
1266}
1267
1268static void __exit alsa_card_loopback_exit(void)
1269{
1270 loopback_unregister_all();
1271}
1272
1273module_init(alsa_card_loopback_init)
1274module_exit(alsa_card_loopback_exit)