]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/drivers/aloop.c
Merge branch 'for-4.15-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[mirror_ubuntu-bionic-kernel.git] / sound / drivers / aloop.c
CommitLineData
597603d6
JK
1/*
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>
65a77217 37#include <linux/module.h>
597603d6
JK
38#include <linux/platform_device.h>
39#include <sound/core.h>
40#include <sound/control.h>
41#include <sound/pcm.h>
b088b53e 42#include <sound/pcm_params.h>
e74670b6 43#include <sound/info.h>
597603d6
JK
44#include <sound/initval.h>
45
46MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
47MODULE_DESCRIPTION("A loopback soundcard");
48MODULE_LICENSE("GPL");
49MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
50
51#define MAX_PCM_SUBSTREAMS 8
52
53static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
54static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
a67ff6a5 55static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
597603d6
JK
56static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
57static int pcm_notify[SNDRV_CARDS];
58
59module_param_array(index, int, NULL, 0444);
60MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
61module_param_array(id, charp, NULL, 0444);
62MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
63module_param_array(enable, bool, NULL, 0444);
64MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
65module_param_array(pcm_substreams, int, NULL, 0444);
66MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
67module_param_array(pcm_notify, int, NULL, 0444);
68MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
69
70#define NO_PITCH 100000
71
72struct loopback_pcm;
73
74struct loopback_cable {
75 spinlock_t lock;
76 struct loopback_pcm *streams[2];
77 struct snd_pcm_hardware hw;
78 /* flags */
79 unsigned int valid;
80 unsigned int running;
5de9e45f 81 unsigned int pause;
597603d6
JK
82};
83
84struct loopback_setup {
85 unsigned int notify: 1;
86 unsigned int rate_shift;
87 unsigned int format;
88 unsigned int rate;
89 unsigned int channels;
90 struct snd_ctl_elem_id active_id;
91 struct snd_ctl_elem_id format_id;
92 struct snd_ctl_elem_id rate_id;
93 struct snd_ctl_elem_id channels_id;
94};
95
96struct loopback {
97 struct snd_card *card;
98 struct mutex cable_lock;
99 struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
100 struct snd_pcm *pcm[2];
101 struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
102};
103
104struct loopback_pcm {
105 struct loopback *loopback;
106 struct snd_pcm_substream *substream;
107 struct loopback_cable *cable;
108 unsigned int pcm_buffer_size;
109 unsigned int buf_pos; /* position in buffer */
110 unsigned int silent_size;
111 /* PCM parameters */
112 unsigned int pcm_period_size;
113 unsigned int pcm_bps; /* bytes per second */
114 unsigned int pcm_salign; /* bytes per sample * channels */
115 unsigned int pcm_rate_shift; /* rate shift value */
116 /* flags */
117 unsigned int period_update_pending :1;
118 /* timer stuff */
119 unsigned int irq_pos; /* fractional IRQ position */
120 unsigned int period_size_frac;
b012513c 121 unsigned int last_drift;
597603d6
JK
122 unsigned long last_jiffies;
123 struct timer_list timer;
124};
125
126static struct platform_device *devices[SNDRV_CARDS];
127
128static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
129{
130 if (dpcm->pcm_rate_shift == NO_PITCH) {
131 x /= HZ;
132 } else {
133 x = div_u64(NO_PITCH * (unsigned long long)x,
134 HZ * (unsigned long long)dpcm->pcm_rate_shift);
135 }
136 return x - (x % dpcm->pcm_salign);
137}
138
139static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
140{
141 if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
142 return x * HZ;
143 } else {
144 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
145 NO_PITCH);
146 }
147 return x;
148}
149
150static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
151{
152 int device = dpcm->substream->pstr->pcm->device;
153
154 if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
155 device ^= 1;
156 return &dpcm->loopback->setup[dpcm->substream->number][device];
157}
158
159static inline unsigned int get_notify(struct loopback_pcm *dpcm)
160{
161 return get_setup(dpcm)->notify;
162}
163
164static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
165{
166 return get_setup(dpcm)->rate_shift;
167}
168
999fc9f6 169/* call in cable->lock */
597603d6
JK
170static void loopback_timer_start(struct loopback_pcm *dpcm)
171{
172 unsigned long tick;
173 unsigned int rate_shift = get_rate_shift(dpcm);
174
175 if (rate_shift != dpcm->pcm_rate_shift) {
176 dpcm->pcm_rate_shift = rate_shift;
177 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
178 }
0db71023
JK
179 if (dpcm->period_size_frac <= dpcm->irq_pos) {
180 dpcm->irq_pos %= dpcm->period_size_frac;
181 dpcm->period_update_pending = 1;
182 }
597603d6
JK
183 tick = dpcm->period_size_frac - dpcm->irq_pos;
184 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
db974553 185 mod_timer(&dpcm->timer, jiffies + tick);
597603d6
JK
186}
187
999fc9f6 188/* call in cable->lock */
597603d6
JK
189static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
190{
191 del_timer(&dpcm->timer);
e74670b6 192 dpcm->timer.expires = 0;
597603d6
JK
193}
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{
b1c73fc8 201 struct snd_pcm_runtime *runtime, *cruntime;
597603d6
JK
202 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;
b1c73fc8
JK
213 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;
597603d6
JK
218 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;
5de9e45f 261 int err, stream = 1 << substream->stream;
597603d6
JK
262
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;
b012513c 270 dpcm->last_drift = 0;
dd04bb12 271 spin_lock(&cable->lock);
5de9e45f
JK
272 cable->running |= stream;
273 cable->pause &= ~stream;
dd04bb12 274 loopback_timer_start(dpcm);
999fc9f6 275 spin_unlock(&cable->lock);
597603d6
JK
276 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
277 loopback_active_notify(dpcm);
278 break;
279 case SNDRV_PCM_TRIGGER_STOP:
dd04bb12 280 spin_lock(&cable->lock);
5de9e45f
JK
281 cable->running &= ~stream;
282 cable->pause &= ~stream;
597603d6 283 loopback_timer_stop(dpcm);
999fc9f6 284 spin_unlock(&cable->lock);
597603d6
JK
285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
286 loopback_active_notify(dpcm);
287 break;
5de9e45f 288 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
edac8943 289 case SNDRV_PCM_TRIGGER_SUSPEND:
5de9e45f
JK
290 spin_lock(&cable->lock);
291 cable->pause |= stream;
5de9e45f 292 loopback_timer_stop(dpcm);
999fc9f6 293 spin_unlock(&cable->lock);
5de9e45f
JK
294 break;
295 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
edac8943 296 case SNDRV_PCM_TRIGGER_RESUME:
5de9e45f
JK
297 spin_lock(&cable->lock);
298 dpcm->last_jiffies = jiffies;
299 cable->pause &= ~stream;
5de9e45f 300 loopback_timer_start(dpcm);
999fc9f6 301 spin_unlock(&cable->lock);
5de9e45f 302 break;
597603d6
JK
303 default:
304 return -EINVAL;
305 }
306 return 0;
307}
308
b1c73fc8
JK
309static void params_change(struct snd_pcm_substream *substream)
310{
311 struct snd_pcm_runtime *runtime = substream->runtime;
312 struct loopback_pcm *dpcm = runtime->private_data;
313 struct loopback_cable *cable = dpcm->cable;
314
74c34ca1 315 cable->hw.formats = pcm_format_to_bits(runtime->format);
b1c73fc8
JK
316 cable->hw.rate_min = runtime->rate;
317 cable->hw.rate_max = runtime->rate;
318 cable->hw.channels_min = runtime->channels;
319 cable->hw.channels_max = runtime->channels;
b1c73fc8
JK
320}
321
597603d6
JK
322static int loopback_prepare(struct snd_pcm_substream *substream)
323{
324 struct snd_pcm_runtime *runtime = substream->runtime;
325 struct loopback_pcm *dpcm = runtime->private_data;
326 struct loopback_cable *cable = dpcm->cable;
b1c73fc8 327 int bps, salign;
597603d6
JK
328
329 salign = (snd_pcm_format_width(runtime->format) *
330 runtime->channels) / 8;
331 bps = salign * runtime->rate;
332 if (bps <= 0 || salign <= 0)
333 return -EINVAL;
334
335 dpcm->buf_pos = 0;
336 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
337 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
338 /* clear capture buffer */
339 dpcm->silent_size = dpcm->pcm_buffer_size;
340 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
341 runtime->buffer_size * runtime->channels);
342 }
343
344 dpcm->irq_pos = 0;
345 dpcm->period_update_pending = 0;
346 dpcm->pcm_bps = bps;
347 dpcm->pcm_salign = salign;
348 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
349
350 mutex_lock(&dpcm->loopback->cable_lock);
b1c73fc8
JK
351 if (!(cable->valid & ~(1 << substream->stream)) ||
352 (get_setup(dpcm)->notify &&
353 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
354 params_change(substream);
597603d6
JK
355 cable->valid |= 1 << substream->stream;
356 mutex_unlock(&dpcm->loopback->cable_lock);
357
358 return 0;
359}
360
361static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
362{
363 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
364 char *dst = runtime->dma_area;
365 unsigned int dst_off = dpcm->buf_pos;
366
367 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
368 return;
369 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
370 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
371
372 for (;;) {
373 unsigned int size = bytes;
374 if (dst_off + size > dpcm->pcm_buffer_size)
375 size = dpcm->pcm_buffer_size - dst_off;
376 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
377 bytes_to_frames(runtime, size) *
378 runtime->channels);
379 dpcm->silent_size += size;
380 bytes -= size;
381 if (!bytes)
382 break;
383 dst_off = 0;
384 }
385}
386
387static void copy_play_buf(struct loopback_pcm *play,
388 struct loopback_pcm *capt,
389 unsigned int bytes)
390{
391 struct snd_pcm_runtime *runtime = play->substream->runtime;
20d9a26d 392 char *src = runtime->dma_area;
597603d6
JK
393 char *dst = capt->substream->runtime->dma_area;
394 unsigned int src_off = play->buf_pos;
395 unsigned int dst_off = capt->buf_pos;
396 unsigned int clear_bytes = 0;
397
398 /* check if playback is draining, trim the capture copy size
399 * when our pointer is at the end of playback ring buffer */
400 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
401 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
402 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
403 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
404 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
405 appl_ptr1 += play->buf_pos / play->pcm_salign;
406 if (appl_ptr < appl_ptr1)
407 appl_ptr1 -= runtime->buffer_size;
408 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
409 if (diff < bytes) {
410 clear_bytes = bytes - diff;
411 bytes = diff;
412 }
413 }
414
415 for (;;) {
416 unsigned int size = bytes;
417 if (src_off + size > play->pcm_buffer_size)
418 size = play->pcm_buffer_size - src_off;
419 if (dst_off + size > capt->pcm_buffer_size)
420 size = capt->pcm_buffer_size - dst_off;
421 memcpy(dst + dst_off, src + src_off, size);
422 capt->silent_size = 0;
423 bytes -= size;
424 if (!bytes)
425 break;
426 src_off = (src_off + size) % play->pcm_buffer_size;
427 dst_off = (dst_off + size) % capt->pcm_buffer_size;
428 }
429
20d9a26d 430 if (clear_bytes > 0) {
597603d6 431 clear_capture_buf(capt, clear_bytes);
20d9a26d
JK
432 capt->silent_size = 0;
433 }
597603d6
JK
434}
435
b012513c
JK
436static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
437 unsigned int jiffies_delta)
597603d6 438{
597603d6 439 unsigned long last_pos;
b012513c 440 unsigned int delta;
597603d6
JK
441
442 last_pos = byte_pos(dpcm, dpcm->irq_pos);
b012513c
JK
443 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
444 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
445 if (delta >= dpcm->last_drift)
446 delta -= dpcm->last_drift;
447 dpcm->last_drift = 0;
597603d6
JK
448 if (dpcm->irq_pos >= dpcm->period_size_frac) {
449 dpcm->irq_pos %= dpcm->period_size_frac;
450 dpcm->period_update_pending = 1;
451 }
b012513c
JK
452 return delta;
453}
454
455static inline void bytepos_finish(struct loopback_pcm *dpcm,
456 unsigned int delta)
457{
458 dpcm->buf_pos += delta;
459 dpcm->buf_pos %= dpcm->pcm_buffer_size;
597603d6
JK
460}
461
999fc9f6 462/* call in cable->lock */
dd04bb12 463static unsigned int loopback_pos_update(struct loopback_cable *cable)
597603d6
JK
464{
465 struct loopback_pcm *dpcm_play =
466 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
467 struct loopback_pcm *dpcm_capt =
468 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
469 unsigned long delta_play = 0, delta_capt = 0;
b012513c 470 unsigned int running, count1, count2;
597603d6 471
5de9e45f 472 running = cable->running ^ cable->pause;
dd04bb12 473 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
597603d6
JK
474 delta_play = jiffies - dpcm_play->last_jiffies;
475 dpcm_play->last_jiffies += delta_play;
476 }
477
dd04bb12 478 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
597603d6
JK
479 delta_capt = jiffies - dpcm_capt->last_jiffies;
480 dpcm_capt->last_jiffies += delta_capt;
481 }
482
98d21df4
TI
483 if (delta_play == 0 && delta_capt == 0)
484 goto unlock;
597603d6
JK
485
486 if (delta_play > delta_capt) {
b012513c
JK
487 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
488 bytepos_finish(dpcm_play, count1);
597603d6
JK
489 delta_play = delta_capt;
490 } else if (delta_play < delta_capt) {
b012513c
JK
491 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
492 clear_capture_buf(dpcm_capt, count1);
493 bytepos_finish(dpcm_capt, count1);
597603d6
JK
494 delta_capt = delta_play;
495 }
496
98d21df4
TI
497 if (delta_play == 0 && delta_capt == 0)
498 goto unlock;
499
597603d6 500 /* note delta_capt == delta_play at this moment */
b012513c
JK
501 count1 = bytepos_delta(dpcm_play, delta_play);
502 count2 = bytepos_delta(dpcm_capt, delta_capt);
503 if (count1 < count2) {
504 dpcm_capt->last_drift = count2 - count1;
505 count1 = count2;
506 } else if (count1 > count2) {
507 dpcm_play->last_drift = count1 - count2;
508 }
509 copy_play_buf(dpcm_play, dpcm_capt, count1);
510 bytepos_finish(dpcm_play, count1);
511 bytepos_finish(dpcm_capt, count1);
98d21df4 512 unlock:
dd04bb12 513 return running;
597603d6
JK
514}
515
bc47ba90 516static void loopback_timer_function(struct timer_list *t)
597603d6 517{
bc47ba90 518 struct loopback_pcm *dpcm = from_timer(dpcm, t, timer);
999fc9f6 519 unsigned long flags;
597603d6 520
999fc9f6
TI
521 spin_lock_irqsave(&dpcm->cable->lock, flags);
522 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
597603d6 523 loopback_timer_start(dpcm);
dd04bb12
JK
524 if (dpcm->period_update_pending) {
525 dpcm->period_update_pending = 0;
999fc9f6
TI
526 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
527 /* need to unlock before calling below */
597603d6 528 snd_pcm_period_elapsed(dpcm->substream);
999fc9f6 529 return;
dd04bb12 530 }
597603d6 531 }
999fc9f6 532 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
597603d6
JK
533}
534
535static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
536{
537 struct snd_pcm_runtime *runtime = substream->runtime;
538 struct loopback_pcm *dpcm = runtime->private_data;
999fc9f6 539 snd_pcm_uframes_t pos;
597603d6 540
999fc9f6 541 spin_lock(&dpcm->cable->lock);
597603d6 542 loopback_pos_update(dpcm->cable);
999fc9f6
TI
543 pos = dpcm->buf_pos;
544 spin_unlock(&dpcm->cable->lock);
545 return bytes_to_frames(runtime, pos);
597603d6
JK
546}
547
b6c0b715 548static const struct snd_pcm_hardware loopback_pcm_hardware =
597603d6
JK
549{
550 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
edac8943
TI
551 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
552 SNDRV_PCM_INFO_RESUME),
597603d6
JK
553 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
554 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
555 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
556 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
557 .rate_min = 8000,
558 .rate_max = 192000,
559 .channels_min = 1,
560 .channels_max = 32,
561 .buffer_bytes_max = 2 * 1024 * 1024,
562 .period_bytes_min = 64,
0db71023
JK
563 /* note check overflow in frac_pos() using pcm_rate_shift before
564 changing period_bytes_max value */
565 .period_bytes_max = 1024 * 1024,
597603d6
JK
566 .periods_min = 1,
567 .periods_max = 1024,
568 .fifo_size = 0,
569};
570
571static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
572{
573 struct loopback_pcm *dpcm = runtime->private_data;
574 kfree(dpcm);
575}
576
577static int loopback_hw_params(struct snd_pcm_substream *substream,
578 struct snd_pcm_hw_params *params)
579{
6b69a0e5
TI
580 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
581 params_buffer_bytes(params));
597603d6
JK
582}
583
584static int loopback_hw_free(struct snd_pcm_substream *substream)
585{
586 struct snd_pcm_runtime *runtime = substream->runtime;
587 struct loopback_pcm *dpcm = runtime->private_data;
588 struct loopback_cable *cable = dpcm->cable;
589
590 mutex_lock(&dpcm->loopback->cable_lock);
591 cable->valid &= ~(1 << substream->stream);
592 mutex_unlock(&dpcm->loopback->cable_lock);
6b69a0e5 593 return snd_pcm_lib_free_vmalloc_buffer(substream);
597603d6
JK
594}
595
596static unsigned int get_cable_index(struct snd_pcm_substream *substream)
597{
598 if (!substream->pcm->device)
599 return substream->stream;
600 else
601 return !substream->stream;
602}
603
b1c73fc8
JK
604static int rule_format(struct snd_pcm_hw_params *params,
605 struct snd_pcm_hw_rule *rule)
606{
898dfe46
TI
607 struct loopback_pcm *dpcm = rule->private;
608 struct loopback_cable *cable = dpcm->cable;
b088b53e 609 struct snd_mask m;
b1c73fc8 610
b088b53e 611 snd_mask_none(&m);
898dfe46
TI
612 mutex_lock(&dpcm->loopback->cable_lock);
613 m.bits[0] = (u_int32_t)cable->hw.formats;
614 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
615 mutex_unlock(&dpcm->loopback->cable_lock);
b088b53e 616 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
b1c73fc8
JK
617}
618
619static int rule_rate(struct snd_pcm_hw_params *params,
620 struct snd_pcm_hw_rule *rule)
621{
898dfe46
TI
622 struct loopback_pcm *dpcm = rule->private;
623 struct loopback_cable *cable = dpcm->cable;
b1c73fc8
JK
624 struct snd_interval t;
625
898dfe46
TI
626 mutex_lock(&dpcm->loopback->cable_lock);
627 t.min = cable->hw.rate_min;
628 t.max = cable->hw.rate_max;
629 mutex_unlock(&dpcm->loopback->cable_lock);
b1c73fc8
JK
630 t.openmin = t.openmax = 0;
631 t.integer = 0;
632 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
633}
634
635static int rule_channels(struct snd_pcm_hw_params *params,
636 struct snd_pcm_hw_rule *rule)
637{
898dfe46
TI
638 struct loopback_pcm *dpcm = rule->private;
639 struct loopback_cable *cable = dpcm->cable;
b1c73fc8
JK
640 struct snd_interval t;
641
898dfe46
TI
642 mutex_lock(&dpcm->loopback->cable_lock);
643 t.min = cable->hw.channels_min;
644 t.max = cable->hw.channels_max;
645 mutex_unlock(&dpcm->loopback->cable_lock);
b1c73fc8
JK
646 t.openmin = t.openmax = 0;
647 t.integer = 0;
648 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
649}
650
9685347a
TI
651static void free_cable(struct snd_pcm_substream *substream)
652{
653 struct loopback *loopback = substream->private_data;
654 int dev = get_cable_index(substream);
655 struct loopback_cable *cable;
656
657 cable = loopback->cables[substream->number][dev];
658 if (!cable)
659 return;
660 if (cable->streams[!substream->stream]) {
661 /* other stream is still alive */
662 cable->streams[substream->stream] = NULL;
663 } else {
664 /* free the cable */
665 loopback->cables[substream->number][dev] = NULL;
666 kfree(cable);
667 }
668}
669
597603d6
JK
670static int loopback_open(struct snd_pcm_substream *substream)
671{
672 struct snd_pcm_runtime *runtime = substream->runtime;
673 struct loopback *loopback = substream->private_data;
674 struct loopback_pcm *dpcm;
9685347a 675 struct loopback_cable *cable = NULL;
597603d6
JK
676 int err = 0;
677 int dev = get_cable_index(substream);
678
679 mutex_lock(&loopback->cable_lock);
680 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
681 if (!dpcm) {
682 err = -ENOMEM;
683 goto unlock;
684 }
685 dpcm->loopback = loopback;
686 dpcm->substream = substream;
bc47ba90 687 timer_setup(&dpcm->timer, loopback_timer_function, 0);
597603d6
JK
688
689 cable = loopback->cables[substream->number][dev];
690 if (!cable) {
691 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
692 if (!cable) {
597603d6
JK
693 err = -ENOMEM;
694 goto unlock;
695 }
696 spin_lock_init(&cable->lock);
697 cable->hw = loopback_pcm_hardware;
698 loopback->cables[substream->number][dev] = cable;
699 }
700 dpcm->cable = cable;
701 cable->streams[substream->stream] = dpcm;
702
703 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
704
b1c73fc8
JK
705 /* use dynamic rules based on actual runtime->hw values */
706 /* note that the default rules created in the PCM midlevel code */
707 /* are cached -> they do not reflect the actual state */
708 err = snd_pcm_hw_rule_add(runtime, 0,
709 SNDRV_PCM_HW_PARAM_FORMAT,
898dfe46 710 rule_format, dpcm,
b1c73fc8
JK
711 SNDRV_PCM_HW_PARAM_FORMAT, -1);
712 if (err < 0)
713 goto unlock;
714 err = snd_pcm_hw_rule_add(runtime, 0,
715 SNDRV_PCM_HW_PARAM_RATE,
898dfe46 716 rule_rate, dpcm,
b1c73fc8
JK
717 SNDRV_PCM_HW_PARAM_RATE, -1);
718 if (err < 0)
719 goto unlock;
720 err = snd_pcm_hw_rule_add(runtime, 0,
721 SNDRV_PCM_HW_PARAM_CHANNELS,
898dfe46 722 rule_channels, dpcm,
b1c73fc8
JK
723 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
724 if (err < 0)
725 goto unlock;
726
597603d6
JK
727 runtime->private_data = dpcm;
728 runtime->private_free = loopback_runtime_free;
b1c73fc8 729 if (get_notify(dpcm))
597603d6 730 runtime->hw = loopback_pcm_hardware;
b1c73fc8 731 else
597603d6 732 runtime->hw = cable->hw;
597603d6 733 unlock:
9685347a
TI
734 if (err < 0) {
735 free_cable(substream);
736 kfree(dpcm);
737 }
597603d6
JK
738 mutex_unlock(&loopback->cable_lock);
739 return err;
740}
741
742static int loopback_close(struct snd_pcm_substream *substream)
743{
744 struct loopback *loopback = substream->private_data;
745 struct loopback_pcm *dpcm = substream->runtime->private_data;
597603d6
JK
746
747 loopback_timer_stop(dpcm);
748 mutex_lock(&loopback->cable_lock);
9685347a 749 free_cable(substream);
597603d6
JK
750 mutex_unlock(&loopback->cable_lock);
751 return 0;
752}
753
f9592582 754static const struct snd_pcm_ops loopback_playback_ops = {
597603d6
JK
755 .open = loopback_open,
756 .close = loopback_close,
757 .ioctl = snd_pcm_lib_ioctl,
758 .hw_params = loopback_hw_params,
759 .hw_free = loopback_hw_free,
760 .prepare = loopback_prepare,
761 .trigger = loopback_trigger,
762 .pointer = loopback_pointer,
6b69a0e5
TI
763 .page = snd_pcm_lib_get_vmalloc_page,
764 .mmap = snd_pcm_lib_mmap_vmalloc,
597603d6
JK
765};
766
f9592582 767static const struct snd_pcm_ops loopback_capture_ops = {
597603d6
JK
768 .open = loopback_open,
769 .close = loopback_close,
770 .ioctl = snd_pcm_lib_ioctl,
771 .hw_params = loopback_hw_params,
772 .hw_free = loopback_hw_free,
773 .prepare = loopback_prepare,
774 .trigger = loopback_trigger,
775 .pointer = loopback_pointer,
6b69a0e5
TI
776 .page = snd_pcm_lib_get_vmalloc_page,
777 .mmap = snd_pcm_lib_mmap_vmalloc,
597603d6
JK
778};
779
fbbb01a1
BP
780static int loopback_pcm_new(struct loopback *loopback,
781 int device, int substreams)
597603d6
JK
782{
783 struct snd_pcm *pcm;
784 int err;
785
786 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
787 substreams, substreams, &pcm);
788 if (err < 0)
789 return err;
790 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
791 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
792
793 pcm->private_data = loopback;
794 pcm->info_flags = 0;
795 strcpy(pcm->name, "Loopback PCM");
796
797 loopback->pcm[device] = pcm;
597603d6
JK
798 return 0;
799}
800
801static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
802 struct snd_ctl_elem_info *uinfo)
803{
804 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
805 uinfo->count = 1;
806 uinfo->value.integer.min = 80000;
807 uinfo->value.integer.max = 120000;
808 uinfo->value.integer.step = 1;
809 return 0;
810}
811
812static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
813 struct snd_ctl_elem_value *ucontrol)
814{
815 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
816
817 ucontrol->value.integer.value[0] =
818 loopback->setup[kcontrol->id.subdevice]
819 [kcontrol->id.device].rate_shift;
820 return 0;
821}
822
823static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
824 struct snd_ctl_elem_value *ucontrol)
825{
826 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
827 unsigned int val;
828 int change = 0;
829
830 val = ucontrol->value.integer.value[0];
831 if (val < 80000)
832 val = 80000;
833 if (val > 120000)
834 val = 120000;
835 mutex_lock(&loopback->cable_lock);
836 if (val != loopback->setup[kcontrol->id.subdevice]
837 [kcontrol->id.device].rate_shift) {
838 loopback->setup[kcontrol->id.subdevice]
839 [kcontrol->id.device].rate_shift = val;
840 change = 1;
841 }
842 mutex_unlock(&loopback->cable_lock);
843 return change;
844}
845
846static int loopback_notify_get(struct snd_kcontrol *kcontrol,
847 struct snd_ctl_elem_value *ucontrol)
848{
849 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
850
851 ucontrol->value.integer.value[0] =
852 loopback->setup[kcontrol->id.subdevice]
853 [kcontrol->id.device].notify;
854 return 0;
855}
856
857static int loopback_notify_put(struct snd_kcontrol *kcontrol,
858 struct snd_ctl_elem_value *ucontrol)
859{
860 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
861 unsigned int val;
862 int change = 0;
863
864 val = ucontrol->value.integer.value[0] ? 1 : 0;
865 if (val != loopback->setup[kcontrol->id.subdevice]
866 [kcontrol->id.device].notify) {
867 loopback->setup[kcontrol->id.subdevice]
868 [kcontrol->id.device].notify = val;
869 change = 1;
870 }
871 return change;
872}
873
874static int loopback_active_get(struct snd_kcontrol *kcontrol,
875 struct snd_ctl_elem_value *ucontrol)
876{
877 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
878 struct loopback_cable *cable = loopback->cables
ac446fb7 879 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
597603d6
JK
880 unsigned int val = 0;
881
882 if (cable != NULL)
883 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
884 1 : 0;
885 ucontrol->value.integer.value[0] = val;
886 return 0;
887}
888
889static int loopback_format_info(struct snd_kcontrol *kcontrol,
890 struct snd_ctl_elem_info *uinfo)
891{
892 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
893 uinfo->count = 1;
894 uinfo->value.integer.min = 0;
895 uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
896 uinfo->value.integer.step = 1;
897 return 0;
898}
899
900static int loopback_format_get(struct snd_kcontrol *kcontrol,
901 struct snd_ctl_elem_value *ucontrol)
902{
903 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
904
905 ucontrol->value.integer.value[0] =
906 loopback->setup[kcontrol->id.subdevice]
907 [kcontrol->id.device].format;
908 return 0;
909}
910
911static int loopback_rate_info(struct snd_kcontrol *kcontrol,
912 struct snd_ctl_elem_info *uinfo)
913{
914 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
915 uinfo->count = 1;
916 uinfo->value.integer.min = 0;
917 uinfo->value.integer.max = 192000;
918 uinfo->value.integer.step = 1;
919 return 0;
920}
921
922static int loopback_rate_get(struct snd_kcontrol *kcontrol,
923 struct snd_ctl_elem_value *ucontrol)
924{
925 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
926
927 ucontrol->value.integer.value[0] =
928 loopback->setup[kcontrol->id.subdevice]
929 [kcontrol->id.device].rate;
930 return 0;
931}
932
933static int loopback_channels_info(struct snd_kcontrol *kcontrol,
934 struct snd_ctl_elem_info *uinfo)
935{
936 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
937 uinfo->count = 1;
938 uinfo->value.integer.min = 1;
939 uinfo->value.integer.max = 1024;
940 uinfo->value.integer.step = 1;
941 return 0;
942}
943
944static int loopback_channels_get(struct snd_kcontrol *kcontrol,
945 struct snd_ctl_elem_value *ucontrol)
946{
947 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
948
949 ucontrol->value.integer.value[0] =
950 loopback->setup[kcontrol->id.subdevice]
1446c5fb 951 [kcontrol->id.device].channels;
597603d6
JK
952 return 0;
953}
954
fbbb01a1 955static struct snd_kcontrol_new loopback_controls[] = {
597603d6
JK
956{
957 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
958 .name = "PCM Rate Shift 100000",
959 .info = loopback_rate_shift_info,
960 .get = loopback_rate_shift_get,
961 .put = loopback_rate_shift_put,
962},
963{
964 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
965 .name = "PCM Notify",
966 .info = snd_ctl_boolean_mono_info,
967 .get = loopback_notify_get,
968 .put = loopback_notify_put,
969},
970#define ACTIVE_IDX 2
971{
972 .access = SNDRV_CTL_ELEM_ACCESS_READ,
973 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
974 .name = "PCM Slave Active",
975 .info = snd_ctl_boolean_mono_info,
976 .get = loopback_active_get,
977},
978#define FORMAT_IDX 3
979{
980 .access = SNDRV_CTL_ELEM_ACCESS_READ,
981 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
982 .name = "PCM Slave Format",
983 .info = loopback_format_info,
984 .get = loopback_format_get
985},
986#define RATE_IDX 4
987{
988 .access = SNDRV_CTL_ELEM_ACCESS_READ,
989 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
990 .name = "PCM Slave Rate",
991 .info = loopback_rate_info,
992 .get = loopback_rate_get
993},
994#define CHANNELS_IDX 5
995{
996 .access = SNDRV_CTL_ELEM_ACCESS_READ,
997 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
998 .name = "PCM Slave Channels",
999 .info = loopback_channels_info,
1000 .get = loopback_channels_get
1001}
1002};
1003
fbbb01a1 1004static int loopback_mixer_new(struct loopback *loopback, int notify)
597603d6
JK
1005{
1006 struct snd_card *card = loopback->card;
1007 struct snd_pcm *pcm;
1008 struct snd_kcontrol *kctl;
1009 struct loopback_setup *setup;
1010 int err, dev, substr, substr_count, idx;
1011
1012 strcpy(card->mixername, "Loopback Mixer");
1013 for (dev = 0; dev < 2; dev++) {
1014 pcm = loopback->pcm[dev];
1015 substr_count =
1016 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1017 for (substr = 0; substr < substr_count; substr++) {
1018 setup = &loopback->setup[substr][dev];
1019 setup->notify = notify;
1020 setup->rate_shift = NO_PITCH;
1021 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1022 setup->rate = 48000;
1023 setup->channels = 2;
1024 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1025 idx++) {
1026 kctl = snd_ctl_new1(&loopback_controls[idx],
1027 loopback);
1028 if (!kctl)
1029 return -ENOMEM;
1030 kctl->id.device = dev;
1031 kctl->id.subdevice = substr;
1032 switch (idx) {
1033 case ACTIVE_IDX:
1034 setup->active_id = kctl->id;
1035 break;
1036 case FORMAT_IDX:
1037 setup->format_id = kctl->id;
1038 break;
1039 case RATE_IDX:
1040 setup->rate_id = kctl->id;
1041 break;
1042 case CHANNELS_IDX:
1043 setup->channels_id = kctl->id;
1044 break;
1045 default:
1046 break;
1047 }
1048 err = snd_ctl_add(card, kctl);
1049 if (err < 0)
1050 return err;
1051 }
1052 }
1053 }
1054 return 0;
1055}
1056
e74670b6
JK
1057static void print_dpcm_info(struct snd_info_buffer *buffer,
1058 struct loopback_pcm *dpcm,
1059 const char *id)
1060{
1061 snd_iprintf(buffer, " %s\n", id);
1062 if (dpcm == NULL) {
1063 snd_iprintf(buffer, " inactive\n");
1064 return;
1065 }
1066 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1067 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1068 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1069 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1070 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1071 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1072 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1073 snd_iprintf(buffer, " update_pending:\t%u\n",
1074 dpcm->period_update_pending);
1075 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1076 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1077 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1078 dpcm->last_jiffies, jiffies);
1079 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1080}
1081
1082static void print_substream_info(struct snd_info_buffer *buffer,
1083 struct loopback *loopback,
1084 int sub,
1085 int num)
1086{
1087 struct loopback_cable *cable = loopback->cables[sub][num];
1088
1089 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1090 if (cable == NULL) {
1091 snd_iprintf(buffer, " inactive\n");
1092 return;
1093 }
1094 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1095 snd_iprintf(buffer, " running: %u\n", cable->running);
5de9e45f 1096 snd_iprintf(buffer, " pause: %u\n", cable->pause);
e74670b6
JK
1097 print_dpcm_info(buffer, cable->streams[0], "Playback");
1098 print_dpcm_info(buffer, cable->streams[1], "Capture");
1099}
1100
1101static void print_cable_info(struct snd_info_entry *entry,
1102 struct snd_info_buffer *buffer)
1103{
1104 struct loopback *loopback = entry->private_data;
1105 int sub, num;
1106
1107 mutex_lock(&loopback->cable_lock);
1108 num = entry->name[strlen(entry->name)-1];
1109 num = num == '0' ? 0 : 1;
1110 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1111 print_substream_info(buffer, loopback, sub, num);
1112 mutex_unlock(&loopback->cable_lock);
1113}
1114
fbbb01a1 1115static int loopback_proc_new(struct loopback *loopback, int cidx)
e74670b6
JK
1116{
1117 char name[32];
1118 struct snd_info_entry *entry;
1119 int err;
1120
1121 snprintf(name, sizeof(name), "cable#%d", cidx);
1122 err = snd_card_proc_new(loopback->card, name, &entry);
1123 if (err < 0)
1124 return err;
1125
1126 snd_info_set_text_ops(entry, loopback, print_cable_info);
1127 return 0;
1128}
1129
fbbb01a1 1130static int loopback_probe(struct platform_device *devptr)
597603d6
JK
1131{
1132 struct snd_card *card;
1133 struct loopback *loopback;
1134 int dev = devptr->id;
1135 int err;
1136
5872f3f6
TI
1137 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1138 sizeof(struct loopback), &card);
597603d6
JK
1139 if (err < 0)
1140 return err;
1141 loopback = card->private_data;
1142
1143 if (pcm_substreams[dev] < 1)
1144 pcm_substreams[dev] = 1;
1145 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1146 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1147
1148 loopback->card = card;
1149 mutex_init(&loopback->cable_lock);
1150
1151 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1152 if (err < 0)
1153 goto __nodev;
1154 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1155 if (err < 0)
1156 goto __nodev;
1157 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1158 if (err < 0)
1159 goto __nodev;
e74670b6
JK
1160 loopback_proc_new(loopback, 0);
1161 loopback_proc_new(loopback, 1);
597603d6
JK
1162 strcpy(card->driver, "Loopback");
1163 strcpy(card->shortname, "Loopback");
1164 sprintf(card->longname, "Loopback %i", dev + 1);
1165 err = snd_card_register(card);
1166 if (!err) {
1167 platform_set_drvdata(devptr, card);
1168 return 0;
1169 }
1170 __nodev:
1171 snd_card_free(card);
1172 return err;
1173}
1174
fbbb01a1 1175static int loopback_remove(struct platform_device *devptr)
597603d6
JK
1176{
1177 snd_card_free(platform_get_drvdata(devptr));
597603d6
JK
1178 return 0;
1179}
1180
d34e4e00 1181#ifdef CONFIG_PM_SLEEP
284e7ca7 1182static int loopback_suspend(struct device *pdev)
597603d6 1183{
284e7ca7 1184 struct snd_card *card = dev_get_drvdata(pdev);
597603d6
JK
1185 struct loopback *loopback = card->private_data;
1186
1187 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1188
1189 snd_pcm_suspend_all(loopback->pcm[0]);
1190 snd_pcm_suspend_all(loopback->pcm[1]);
1191 return 0;
1192}
1193
284e7ca7 1194static int loopback_resume(struct device *pdev)
597603d6 1195{
284e7ca7 1196 struct snd_card *card = dev_get_drvdata(pdev);
597603d6
JK
1197
1198 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1199 return 0;
1200}
284e7ca7
TI
1201
1202static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1203#define LOOPBACK_PM_OPS &loopback_pm
1204#else
1205#define LOOPBACK_PM_OPS NULL
597603d6
JK
1206#endif
1207
1208#define SND_LOOPBACK_DRIVER "snd_aloop"
1209
1210static struct platform_driver loopback_driver = {
1211 .probe = loopback_probe,
fbbb01a1 1212 .remove = loopback_remove,
597603d6 1213 .driver = {
8bf01d8a 1214 .name = SND_LOOPBACK_DRIVER,
284e7ca7 1215 .pm = LOOPBACK_PM_OPS,
597603d6
JK
1216 },
1217};
1218
1219static void loopback_unregister_all(void)
1220{
1221 int i;
1222
1223 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1224 platform_device_unregister(devices[i]);
1225 platform_driver_unregister(&loopback_driver);
1226}
1227
1228static int __init alsa_card_loopback_init(void)
1229{
1230 int i, err, cards;
1231
1232 err = platform_driver_register(&loopback_driver);
1233 if (err < 0)
1234 return err;
1235
1236
1237 cards = 0;
1238 for (i = 0; i < SNDRV_CARDS; i++) {
1239 struct platform_device *device;
1240 if (!enable[i])
1241 continue;
1242 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1243 i, NULL, 0);
1244 if (IS_ERR(device))
1245 continue;
1246 if (!platform_get_drvdata(device)) {
1247 platform_device_unregister(device);
1248 continue;
1249 }
1250 devices[i] = device;
1251 cards++;
1252 }
1253 if (!cards) {
1254#ifdef MODULE
1255 printk(KERN_ERR "aloop: No loopback enabled\n");
1256#endif
1257 loopback_unregister_all();
1258 return -ENODEV;
1259 }
1260 return 0;
1261}
1262
1263static void __exit alsa_card_loopback_exit(void)
1264{
1265 loopback_unregister_all();
1266}
1267
1268module_init(alsa_card_loopback_init)
1269module_exit(alsa_card_loopback_exit)