ALSA: snd-aloop - fix "PCM Slave Active" element read value
[cascardo/linux.git] / sound / drivers / aloop.c
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>
37 #include <linux/moduleparam.h>
38 #include <linux/platform_device.h>
39 #include <sound/core.h>
40 #include <sound/control.h>
41 #include <sound/pcm.h>
42 #include <sound/initval.h>
43
44 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
45 MODULE_DESCRIPTION("A loopback soundcard");
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
48
49 #define MAX_PCM_SUBSTREAMS      8
50
51 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
52 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
53 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
54 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
55 static int pcm_notify[SNDRV_CARDS];
56
57 module_param_array(index, int, NULL, 0444);
58 MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
59 module_param_array(id, charp, NULL, 0444);
60 MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
61 module_param_array(enable, bool, NULL, 0444);
62 MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
63 module_param_array(pcm_substreams, int, NULL, 0444);
64 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
65 module_param_array(pcm_notify, int, NULL, 0444);
66 MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
67
68 #define NO_PITCH 100000
69
70 struct loopback_pcm;
71
72 struct loopback_cable {
73         spinlock_t lock;
74         struct loopback_pcm *streams[2];
75         struct snd_pcm_hardware hw;
76         /* flags */
77         unsigned int valid;
78         unsigned int running;
79 };
80
81 struct loopback_setup {
82         unsigned int notify: 1;
83         unsigned int rate_shift;
84         unsigned int format;
85         unsigned int rate;
86         unsigned int channels;
87         struct snd_ctl_elem_id active_id;
88         struct snd_ctl_elem_id format_id;
89         struct snd_ctl_elem_id rate_id;
90         struct snd_ctl_elem_id channels_id;
91 };
92
93 struct loopback {
94         struct snd_card *card;
95         struct mutex cable_lock;
96         struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
97         struct snd_pcm *pcm[2];
98         struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
99 };
100
101 struct loopback_pcm {
102         struct loopback *loopback;
103         struct snd_pcm_substream *substream;
104         struct loopback_cable *cable;
105         unsigned int pcm_buffer_size;
106         unsigned int buf_pos;   /* position in buffer */
107         unsigned int silent_size;
108         /* PCM parameters */
109         unsigned int pcm_period_size;
110         unsigned int pcm_bps;           /* bytes per second */
111         unsigned int pcm_salign;        /* bytes per sample * channels */
112         unsigned int pcm_rate_shift;    /* rate shift value */
113         /* flags */
114         unsigned int period_update_pending :1;
115         /* timer stuff */
116         unsigned int irq_pos;           /* fractional IRQ position */
117         unsigned int period_size_frac;
118         unsigned long last_jiffies;
119         struct timer_list timer;
120 };
121
122 static struct platform_device *devices[SNDRV_CARDS];
123
124 static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
125 {
126         if (dpcm->pcm_rate_shift == NO_PITCH) {
127                 x /= HZ;
128         } else {
129                 x = div_u64(NO_PITCH * (unsigned long long)x,
130                             HZ * (unsigned long long)dpcm->pcm_rate_shift);
131         }
132         return x - (x % dpcm->pcm_salign);
133 }
134
135 static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
136 {
137         if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
138                 return x * HZ;
139         } else {
140                 x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
141                             NO_PITCH);
142         }
143         return x;
144 }
145
146 static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
147 {
148         int device = dpcm->substream->pstr->pcm->device;
149         
150         if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
151                 device ^= 1;
152         return &dpcm->loopback->setup[dpcm->substream->number][device];
153 }
154
155 static inline unsigned int get_notify(struct loopback_pcm *dpcm)
156 {
157         return get_setup(dpcm)->notify;
158 }
159
160 static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
161 {
162         return get_setup(dpcm)->rate_shift;
163 }
164
165 static void loopback_timer_start(struct loopback_pcm *dpcm)
166 {
167         unsigned long tick;
168         unsigned int rate_shift = get_rate_shift(dpcm);
169
170         if (rate_shift != dpcm->pcm_rate_shift) {
171                 dpcm->pcm_rate_shift = rate_shift;
172                 dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
173         }
174         tick = dpcm->period_size_frac - dpcm->irq_pos;
175         tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
176         dpcm->timer.expires = jiffies + tick;
177         add_timer(&dpcm->timer);
178 }
179
180 static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
181 {
182         del_timer(&dpcm->timer);
183 }
184
185 #define CABLE_VALID_PLAYBACK    (1 << SNDRV_PCM_STREAM_PLAYBACK)
186 #define CABLE_VALID_CAPTURE     (1 << SNDRV_PCM_STREAM_CAPTURE)
187 #define CABLE_VALID_BOTH        (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
188
189 static int loopback_check_format(struct loopback_cable *cable, int stream)
190 {
191         struct snd_pcm_runtime *runtime;
192         struct loopback_setup *setup;
193         struct snd_card *card;
194         int check;
195
196         if (cable->valid != CABLE_VALID_BOTH) {
197                 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
198                         goto __notify;
199                 return 0;
200         }
201         runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
202                                                         substream->runtime;
203         check = cable->hw.formats != (1ULL << runtime->format) ||
204                 cable->hw.rate_min != runtime->rate ||
205                 cable->hw.rate_max != runtime->rate ||
206                 cable->hw.channels_min != runtime->channels ||
207                 cable->hw.channels_max != runtime->channels;
208         if (!check)
209                 return 0;
210         if (stream == SNDRV_PCM_STREAM_CAPTURE) {
211                 return -EIO;
212         } else {
213                 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
214                                         substream, SNDRV_PCM_STATE_DRAINING);
215               __notify:
216                 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
217                                                         substream->runtime;
218                 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
219                 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
220                 if (setup->format != runtime->format) {
221                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
222                                                         &setup->format_id);
223                         setup->format = runtime->format;
224                 }
225                 if (setup->rate != runtime->rate) {
226                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
227                                                         &setup->rate_id);
228                         setup->rate = runtime->rate;
229                 }
230                 if (setup->channels != runtime->channels) {
231                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
232                                                         &setup->channels_id);
233                         setup->channels = runtime->channels;
234                 }
235         }
236         return 0;
237 }
238
239 static void loopback_active_notify(struct loopback_pcm *dpcm)
240 {
241         snd_ctl_notify(dpcm->loopback->card,
242                        SNDRV_CTL_EVENT_MASK_VALUE,
243                        &get_setup(dpcm)->active_id);
244 }
245
246 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
247 {
248         struct snd_pcm_runtime *runtime = substream->runtime;
249         struct loopback_pcm *dpcm = runtime->private_data;
250         struct loopback_cable *cable = dpcm->cable;
251         int err;
252
253         switch (cmd) {
254         case SNDRV_PCM_TRIGGER_START:
255                 err = loopback_check_format(cable, substream->stream);
256                 if (err < 0)
257                         return err;
258                 dpcm->last_jiffies = jiffies;
259                 dpcm->pcm_rate_shift = 0;
260                 loopback_timer_start(dpcm);
261                 cable->running |= (1 << substream->stream);
262                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
263                         loopback_active_notify(dpcm);
264                 break;
265         case SNDRV_PCM_TRIGGER_STOP:
266                 cable->running &= ~(1 << substream->stream);
267                 loopback_timer_stop(dpcm);
268                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
269                         loopback_active_notify(dpcm);
270                 break;
271         default:
272                 return -EINVAL;
273         }
274         return 0;
275 }
276
277 static int loopback_prepare(struct snd_pcm_substream *substream)
278 {
279         struct snd_pcm_runtime *runtime = substream->runtime;
280         struct loopback_pcm *dpcm = runtime->private_data;
281         struct loopback_cable *cable = dpcm->cable;
282         unsigned int bps, salign;
283
284         salign = (snd_pcm_format_width(runtime->format) *
285                                                 runtime->channels) / 8;
286         bps = salign * runtime->rate;
287         if (bps <= 0 || salign <= 0)
288                 return -EINVAL;
289
290         dpcm->buf_pos = 0;
291         dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
292         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
293                 /* clear capture buffer */
294                 dpcm->silent_size = dpcm->pcm_buffer_size;
295                 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
296                                            runtime->buffer_size * runtime->channels);
297         }
298
299         dpcm->irq_pos = 0;
300         dpcm->period_update_pending = 0;
301         dpcm->pcm_bps = bps;
302         dpcm->pcm_salign = salign;
303         dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
304
305         mutex_lock(&dpcm->loopback->cable_lock);
306         if (!(cable->valid & ~(1 << substream->stream))) {
307                 cable->hw.formats = (1ULL << runtime->format);
308                 cable->hw.rate_min = runtime->rate;
309                 cable->hw.rate_max = runtime->rate;
310                 cable->hw.channels_min = runtime->channels;
311                 cable->hw.channels_max = runtime->channels;
312         }
313         cable->valid |= 1 << substream->stream;
314         mutex_unlock(&dpcm->loopback->cable_lock);
315
316         return 0;
317 }
318
319 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
320 {
321         struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
322         char *dst = runtime->dma_area;
323         unsigned int dst_off = dpcm->buf_pos;
324
325         if (dpcm->silent_size >= dpcm->pcm_buffer_size)
326                 return;
327         if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
328                 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
329
330         for (;;) {
331                 unsigned int size = bytes;
332                 if (dst_off + size > dpcm->pcm_buffer_size)
333                         size = dpcm->pcm_buffer_size - dst_off;
334                 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
335                                            bytes_to_frames(runtime, size) *
336                                                 runtime->channels);
337                 dpcm->silent_size += size;
338                 bytes -= size;
339                 if (!bytes)
340                         break;
341                 dst_off = 0;
342         }
343 }
344
345 static void copy_play_buf(struct loopback_pcm *play,
346                           struct loopback_pcm *capt,
347                           unsigned int bytes)
348 {
349         struct snd_pcm_runtime *runtime = play->substream->runtime;
350         char *src = runtime->dma_area;
351         char *dst = capt->substream->runtime->dma_area;
352         unsigned int src_off = play->buf_pos;
353         unsigned int dst_off = capt->buf_pos;
354         unsigned int clear_bytes = 0;
355
356         /* check if playback is draining, trim the capture copy size
357          * when our pointer is at the end of playback ring buffer */
358         if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
359             snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) { 
360                 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
361                 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
362                 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
363                 appl_ptr1 += play->buf_pos / play->pcm_salign;
364                 if (appl_ptr < appl_ptr1)
365                         appl_ptr1 -= runtime->buffer_size;
366                 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
367                 if (diff < bytes) {
368                         clear_bytes = bytes - diff;
369                         bytes = diff;
370                 }
371         }
372
373         for (;;) {
374                 unsigned int size = bytes;
375                 if (src_off + size > play->pcm_buffer_size)
376                         size = play->pcm_buffer_size - src_off;
377                 if (dst_off + size > capt->pcm_buffer_size)
378                         size = capt->pcm_buffer_size - dst_off;
379                 memcpy(dst + dst_off, src + src_off, size);
380                 capt->silent_size = 0;
381                 bytes -= size;
382                 if (!bytes)
383                         break;
384                 src_off = (src_off + size) % play->pcm_buffer_size;
385                 dst_off = (dst_off + size) % capt->pcm_buffer_size;
386         }
387
388         if (clear_bytes > 0) {
389                 clear_capture_buf(capt, clear_bytes);
390                 capt->silent_size = 0;
391         }
392 }
393
394 #define BYTEPOS_UPDATE_POSONLY  0
395 #define BYTEPOS_UPDATE_CLEAR    1
396 #define BYTEPOS_UPDATE_COPY     2
397
398 static void loopback_bytepos_update(struct loopback_pcm *dpcm,
399                                     unsigned int delta,
400                                     unsigned int cmd)
401 {
402         unsigned int count;
403         unsigned long last_pos;
404
405         last_pos = byte_pos(dpcm, dpcm->irq_pos);
406         dpcm->irq_pos += delta * dpcm->pcm_bps;
407         count = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
408         if (!count)
409                 return;
410         if (cmd == BYTEPOS_UPDATE_CLEAR)
411                 clear_capture_buf(dpcm, count);
412         else if (cmd == BYTEPOS_UPDATE_COPY)
413                 copy_play_buf(dpcm->cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
414                               dpcm->cable->streams[SNDRV_PCM_STREAM_CAPTURE],
415                               count);
416         dpcm->buf_pos += count;
417         dpcm->buf_pos %= dpcm->pcm_buffer_size;
418         if (dpcm->irq_pos >= dpcm->period_size_frac) {
419                 dpcm->irq_pos %= dpcm->period_size_frac;
420                 dpcm->period_update_pending = 1;
421         }
422 }
423
424 static void loopback_pos_update(struct loopback_cable *cable)
425 {
426         struct loopback_pcm *dpcm_play =
427                         cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
428         struct loopback_pcm *dpcm_capt =
429                         cable->streams[SNDRV_PCM_STREAM_CAPTURE];
430         unsigned long delta_play = 0, delta_capt = 0;
431
432         spin_lock(&cable->lock);        
433         if (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
434                 delta_play = jiffies - dpcm_play->last_jiffies;
435                 dpcm_play->last_jiffies += delta_play;
436         }
437
438         if (cable->running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
439                 delta_capt = jiffies - dpcm_capt->last_jiffies;
440                 dpcm_capt->last_jiffies += delta_capt;
441         }
442
443         if (delta_play == 0 && delta_capt == 0) {
444                 spin_unlock(&cable->lock);
445                 return;
446         }
447                 
448         if (delta_play > delta_capt) {
449                 loopback_bytepos_update(dpcm_play, delta_play - delta_capt,
450                                         BYTEPOS_UPDATE_POSONLY);
451                 delta_play = delta_capt;
452         } else if (delta_play < delta_capt) {
453                 loopback_bytepos_update(dpcm_capt, delta_capt - delta_play,
454                                         BYTEPOS_UPDATE_CLEAR);
455                 delta_capt = delta_play;
456         }
457
458         if (delta_play == 0 && delta_capt == 0) {
459                 spin_unlock(&cable->lock);
460                 return;
461         }
462         /* note delta_capt == delta_play at this moment */
463         loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
464         loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
465         spin_unlock(&cable->lock);
466 }
467
468 static void loopback_timer_function(unsigned long data)
469 {
470         struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
471         int stream;
472
473         loopback_pos_update(dpcm->cable);
474         stream = dpcm->substream->stream;
475         if (dpcm->cable->running & (1 << stream))
476                 loopback_timer_start(dpcm);
477         if (dpcm->period_update_pending) {
478                 dpcm->period_update_pending = 0;
479                 if (dpcm->cable->running & (1 << stream))
480                         snd_pcm_period_elapsed(dpcm->substream);
481         }
482 }
483
484 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
485 {
486         struct snd_pcm_runtime *runtime = substream->runtime;
487         struct loopback_pcm *dpcm = runtime->private_data;
488
489         loopback_pos_update(dpcm->cable);
490         return bytes_to_frames(runtime, dpcm->buf_pos);
491 }
492
493 static struct snd_pcm_hardware loopback_pcm_hardware =
494 {
495         .info =         (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
496                          SNDRV_PCM_INFO_MMAP_VALID),
497         .formats =      (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
498                          SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
499                          SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
500         .rates =        SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
501         .rate_min =             8000,
502         .rate_max =             192000,
503         .channels_min =         1,
504         .channels_max =         32,
505         .buffer_bytes_max =     2 * 1024 * 1024,
506         .period_bytes_min =     64,
507         .period_bytes_max =     2 * 1024 * 1024,
508         .periods_min =          1,
509         .periods_max =          1024,
510         .fifo_size =            0,
511 };
512
513 static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
514 {
515         struct loopback_pcm *dpcm = runtime->private_data;
516         kfree(dpcm);
517 }
518
519 static int loopback_hw_params(struct snd_pcm_substream *substream,
520                               struct snd_pcm_hw_params *params)
521 {
522         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
523 }
524
525 static int loopback_hw_free(struct snd_pcm_substream *substream)
526 {
527         struct snd_pcm_runtime *runtime = substream->runtime;
528         struct loopback_pcm *dpcm = runtime->private_data;
529         struct loopback_cable *cable = dpcm->cable;
530
531         mutex_lock(&dpcm->loopback->cable_lock);
532         cable->valid &= ~(1 << substream->stream);
533         mutex_unlock(&dpcm->loopback->cable_lock);
534         return snd_pcm_lib_free_pages(substream);
535 }
536
537 static unsigned int get_cable_index(struct snd_pcm_substream *substream)
538 {
539         if (!substream->pcm->device)
540                 return substream->stream;
541         else
542                 return !substream->stream;
543 }
544
545 static int loopback_open(struct snd_pcm_substream *substream)
546 {
547         struct snd_pcm_runtime *runtime = substream->runtime;
548         struct loopback *loopback = substream->private_data;
549         struct loopback_pcm *dpcm;
550         struct loopback_cable *cable;
551         int err = 0;
552         int dev = get_cable_index(substream);
553
554         mutex_lock(&loopback->cable_lock);
555         dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
556         if (!dpcm) {
557                 err = -ENOMEM;
558                 goto unlock;
559         }
560         dpcm->loopback = loopback;
561         dpcm->substream = substream;
562         setup_timer(&dpcm->timer, loopback_timer_function,
563                     (unsigned long)dpcm);
564
565         cable = loopback->cables[substream->number][dev];
566         if (!cable) {
567                 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
568                 if (!cable) {
569                         kfree(dpcm);
570                         err = -ENOMEM;
571                         goto unlock;
572                 }
573                 spin_lock_init(&cable->lock);
574                 cable->hw = loopback_pcm_hardware;
575                 loopback->cables[substream->number][dev] = cable;
576         }
577         dpcm->cable = cable;
578         cable->streams[substream->stream] = dpcm;
579
580         snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
581
582         runtime->private_data = dpcm;
583         runtime->private_free = loopback_runtime_free;
584         if (get_notify(dpcm) &&
585             substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
586                 runtime->hw = loopback_pcm_hardware;
587         } else {
588                 runtime->hw = cable->hw;
589         }
590  unlock:
591         mutex_unlock(&loopback->cable_lock);
592         return err;
593 }
594
595 static int loopback_close(struct snd_pcm_substream *substream)
596 {
597         struct loopback *loopback = substream->private_data;
598         struct loopback_pcm *dpcm = substream->runtime->private_data;
599         struct loopback_cable *cable;
600         int dev = get_cable_index(substream);
601
602         loopback_timer_stop(dpcm);
603         mutex_lock(&loopback->cable_lock);
604         cable = loopback->cables[substream->number][dev];
605         if (cable->streams[!substream->stream]) {
606                 /* other stream is still alive */
607                 cable->streams[substream->stream] = NULL;
608         } else {
609                 /* free the cable */
610                 loopback->cables[substream->number][dev] = NULL;
611                 kfree(cable);
612         }
613         mutex_unlock(&loopback->cable_lock);
614         return 0;
615 }
616
617 static struct snd_pcm_ops loopback_playback_ops = {
618         .open =         loopback_open,
619         .close =        loopback_close,
620         .ioctl =        snd_pcm_lib_ioctl,
621         .hw_params =    loopback_hw_params,
622         .hw_free =      loopback_hw_free,
623         .prepare =      loopback_prepare,
624         .trigger =      loopback_trigger,
625         .pointer =      loopback_pointer,
626 };
627
628 static struct snd_pcm_ops loopback_capture_ops = {
629         .open =         loopback_open,
630         .close =        loopback_close,
631         .ioctl =        snd_pcm_lib_ioctl,
632         .hw_params =    loopback_hw_params,
633         .hw_free =      loopback_hw_free,
634         .prepare =      loopback_prepare,
635         .trigger =      loopback_trigger,
636         .pointer =      loopback_pointer,
637 };
638
639 static int __devinit loopback_pcm_new(struct loopback *loopback,
640                                       int device, int substreams)
641 {
642         struct snd_pcm *pcm;
643         int err;
644
645         err = snd_pcm_new(loopback->card, "Loopback PCM", device,
646                           substreams, substreams, &pcm);
647         if (err < 0)
648                 return err;
649         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
650         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
651
652         pcm->private_data = loopback;
653         pcm->info_flags = 0;
654         strcpy(pcm->name, "Loopback PCM");
655
656         loopback->pcm[device] = pcm;
657
658         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
659                         snd_dma_continuous_data(GFP_KERNEL),
660                         0, 2 * 1024 * 1024);
661         return 0;
662 }
663
664 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,   
665                                     struct snd_ctl_elem_info *uinfo) 
666 {
667         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
668         uinfo->count = 1;
669         uinfo->value.integer.min = 80000;
670         uinfo->value.integer.max = 120000;
671         uinfo->value.integer.step = 1;
672         return 0;
673 }                                  
674
675 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
676                                    struct snd_ctl_elem_value *ucontrol)
677 {
678         struct loopback *loopback = snd_kcontrol_chip(kcontrol);
679         
680         ucontrol->value.integer.value[0] =
681                 loopback->setup[kcontrol->id.subdevice]
682                                [kcontrol->id.device].rate_shift;
683         return 0;
684 }
685
686 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
687                                    struct snd_ctl_elem_value *ucontrol)
688 {
689         struct loopback *loopback = snd_kcontrol_chip(kcontrol);
690         unsigned int val;
691         int change = 0;
692
693         val = ucontrol->value.integer.value[0];
694         if (val < 80000)
695                 val = 80000;
696         if (val > 120000)
697                 val = 120000;   
698         mutex_lock(&loopback->cable_lock);
699         if (val != loopback->setup[kcontrol->id.subdevice]
700                                   [kcontrol->id.device].rate_shift) {
701                 loopback->setup[kcontrol->id.subdevice]
702                                [kcontrol->id.device].rate_shift = val;
703                 change = 1;
704         }
705         mutex_unlock(&loopback->cable_lock);
706         return change;
707 }
708
709 static int loopback_notify_get(struct snd_kcontrol *kcontrol,
710                                struct snd_ctl_elem_value *ucontrol)
711 {
712         struct loopback *loopback = snd_kcontrol_chip(kcontrol);
713         
714         ucontrol->value.integer.value[0] =
715                 loopback->setup[kcontrol->id.subdevice]
716                                [kcontrol->id.device].notify;
717         return 0;
718 }
719
720 static int loopback_notify_put(struct snd_kcontrol *kcontrol,
721                                struct snd_ctl_elem_value *ucontrol)
722 {
723         struct loopback *loopback = snd_kcontrol_chip(kcontrol);
724         unsigned int val;
725         int change = 0;
726
727         val = ucontrol->value.integer.value[0] ? 1 : 0;
728         if (val != loopback->setup[kcontrol->id.subdevice]
729                                 [kcontrol->id.device].notify) {
730                 loopback->setup[kcontrol->id.subdevice]
731                         [kcontrol->id.device].notify = val;
732                 change = 1;
733         }
734         return change;
735 }
736
737 static int loopback_active_get(struct snd_kcontrol *kcontrol,
738                                struct snd_ctl_elem_value *ucontrol)
739 {
740         struct loopback *loopback = snd_kcontrol_chip(kcontrol);
741         struct loopback_cable *cable = loopback->cables
742                         [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
743         unsigned int val = 0;
744
745         if (cable != NULL)
746                 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
747                                                                         1 : 0;
748         ucontrol->value.integer.value[0] = val;
749         return 0;
750 }
751
752 static int loopback_format_info(struct snd_kcontrol *kcontrol,   
753                                 struct snd_ctl_elem_info *uinfo) 
754 {
755         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
756         uinfo->count = 1;
757         uinfo->value.integer.min = 0;
758         uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
759         uinfo->value.integer.step = 1;
760         return 0;
761 }                                  
762
763 static int loopback_format_get(struct snd_kcontrol *kcontrol,
764                                struct snd_ctl_elem_value *ucontrol)
765 {
766         struct loopback *loopback = snd_kcontrol_chip(kcontrol);
767         
768         ucontrol->value.integer.value[0] =
769                 loopback->setup[kcontrol->id.subdevice]
770                                [kcontrol->id.device].format;
771         return 0;
772 }
773
774 static int loopback_rate_info(struct snd_kcontrol *kcontrol,   
775                               struct snd_ctl_elem_info *uinfo) 
776 {
777         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
778         uinfo->count = 1;
779         uinfo->value.integer.min = 0;
780         uinfo->value.integer.max = 192000;
781         uinfo->value.integer.step = 1;
782         return 0;
783 }                                  
784
785 static int loopback_rate_get(struct snd_kcontrol *kcontrol,
786                              struct snd_ctl_elem_value *ucontrol)
787 {
788         struct loopback *loopback = snd_kcontrol_chip(kcontrol);
789         
790         ucontrol->value.integer.value[0] =
791                 loopback->setup[kcontrol->id.subdevice]
792                                [kcontrol->id.device].rate;
793         return 0;
794 }
795
796 static int loopback_channels_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 = 1;
802         uinfo->value.integer.max = 1024;
803         uinfo->value.integer.step = 1;
804         return 0;
805 }                                  
806
807 static int loopback_channels_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].channels;
815         return 0;
816 }
817
818 static struct snd_kcontrol_new loopback_controls[]  __devinitdata = {
819 {
820         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
821         .name =         "PCM Rate Shift 100000",
822         .info =         loopback_rate_shift_info,
823         .get =          loopback_rate_shift_get,
824         .put =          loopback_rate_shift_put,
825 },
826 {
827         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
828         .name =         "PCM Notify",
829         .info =         snd_ctl_boolean_mono_info,
830         .get =          loopback_notify_get,
831         .put =          loopback_notify_put,
832 },
833 #define ACTIVE_IDX 2
834 {
835         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
836         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
837         .name =         "PCM Slave Active",
838         .info =         snd_ctl_boolean_mono_info,
839         .get =          loopback_active_get,
840 },
841 #define FORMAT_IDX 3
842 {
843         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
844         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
845         .name =         "PCM Slave Format",
846         .info =         loopback_format_info,
847         .get =          loopback_format_get
848 },
849 #define RATE_IDX 4
850 {
851         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
852         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
853         .name =         "PCM Slave Rate",
854         .info =         loopback_rate_info,
855         .get =          loopback_rate_get
856 },
857 #define CHANNELS_IDX 5
858 {
859         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
860         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
861         .name =         "PCM Slave Channels",
862         .info =         loopback_channels_info,
863         .get =          loopback_channels_get
864 }
865 };
866
867 static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
868 {
869         struct snd_card *card = loopback->card;
870         struct snd_pcm *pcm;
871         struct snd_kcontrol *kctl;
872         struct loopback_setup *setup;
873         int err, dev, substr, substr_count, idx;
874
875         strcpy(card->mixername, "Loopback Mixer");
876         for (dev = 0; dev < 2; dev++) {
877                 pcm = loopback->pcm[dev];
878                 substr_count =
879                     pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
880                 for (substr = 0; substr < substr_count; substr++) {
881                         setup = &loopback->setup[substr][dev];
882                         setup->notify = notify;
883                         setup->rate_shift = NO_PITCH;
884                         setup->format = SNDRV_PCM_FORMAT_S16_LE;
885                         setup->rate = 48000;
886                         setup->channels = 2;
887                         for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
888                                                                         idx++) {
889                                 kctl = snd_ctl_new1(&loopback_controls[idx],
890                                                     loopback);
891                                 if (!kctl)
892                                         return -ENOMEM;
893                                 kctl->id.device = dev;
894                                 kctl->id.subdevice = substr;
895                                 switch (idx) {
896                                 case ACTIVE_IDX:
897                                         setup->active_id = kctl->id;
898                                         break;
899                                 case FORMAT_IDX:
900                                         setup->format_id = kctl->id;
901                                         break;
902                                 case RATE_IDX:
903                                         setup->rate_id = kctl->id;
904                                         break;
905                                 case CHANNELS_IDX:
906                                         setup->channels_id = kctl->id;
907                                         break;
908                                 default:
909                                         break;
910                                 }
911                                 err = snd_ctl_add(card, kctl);
912                                 if (err < 0)
913                                         return err;
914                         }
915                 }
916         }
917         return 0;
918 }
919
920 static int __devinit loopback_probe(struct platform_device *devptr)
921 {
922         struct snd_card *card;
923         struct loopback *loopback;
924         int dev = devptr->id;
925         int err;
926
927         err = snd_card_create(index[dev], id[dev], THIS_MODULE,
928                               sizeof(struct loopback), &card);
929         if (err < 0)
930                 return err;
931         loopback = card->private_data;
932
933         if (pcm_substreams[dev] < 1)
934                 pcm_substreams[dev] = 1;
935         if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
936                 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
937         
938         loopback->card = card;
939         mutex_init(&loopback->cable_lock);
940
941         err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
942         if (err < 0)
943                 goto __nodev;
944         err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
945         if (err < 0)
946                 goto __nodev;
947         err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
948         if (err < 0)
949                 goto __nodev;
950         strcpy(card->driver, "Loopback");
951         strcpy(card->shortname, "Loopback");
952         sprintf(card->longname, "Loopback %i", dev + 1);
953         err = snd_card_register(card);
954         if (!err) {
955                 platform_set_drvdata(devptr, card);
956                 return 0;
957         }
958       __nodev:
959         snd_card_free(card);
960         return err;
961 }
962
963 static int __devexit loopback_remove(struct platform_device *devptr)
964 {
965         snd_card_free(platform_get_drvdata(devptr));
966         platform_set_drvdata(devptr, NULL);
967         return 0;
968 }
969
970 #ifdef CONFIG_PM
971 static int loopback_suspend(struct platform_device *pdev,
972                                 pm_message_t state)
973 {
974         struct snd_card *card = platform_get_drvdata(pdev);
975         struct loopback *loopback = card->private_data;
976
977         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
978
979         snd_pcm_suspend_all(loopback->pcm[0]);
980         snd_pcm_suspend_all(loopback->pcm[1]);
981         return 0;
982 }
983         
984 static int loopback_resume(struct platform_device *pdev)
985 {
986         struct snd_card *card = platform_get_drvdata(pdev);
987
988         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
989         return 0;
990 }
991 #endif
992
993 #define SND_LOOPBACK_DRIVER     "snd_aloop"
994
995 static struct platform_driver loopback_driver = {
996         .probe          = loopback_probe,
997         .remove         = __devexit_p(loopback_remove),
998 #ifdef CONFIG_PM
999         .suspend        = loopback_suspend,
1000         .resume         = loopback_resume,
1001 #endif
1002         .driver         = {
1003                 .name   = SND_LOOPBACK_DRIVER
1004         },
1005 };
1006
1007 static void loopback_unregister_all(void)
1008 {
1009         int i;
1010
1011         for (i = 0; i < ARRAY_SIZE(devices); ++i)
1012                 platform_device_unregister(devices[i]);
1013         platform_driver_unregister(&loopback_driver);
1014 }
1015
1016 static int __init alsa_card_loopback_init(void)
1017 {
1018         int i, err, cards;
1019
1020         err = platform_driver_register(&loopback_driver);
1021         if (err < 0)
1022                 return err;
1023
1024
1025         cards = 0;
1026         for (i = 0; i < SNDRV_CARDS; i++) {
1027                 struct platform_device *device;
1028                 if (!enable[i])
1029                         continue;
1030                 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1031                                                          i, NULL, 0);
1032                 if (IS_ERR(device))
1033                         continue;
1034                 if (!platform_get_drvdata(device)) {
1035                         platform_device_unregister(device);
1036                         continue;
1037                 }
1038                 devices[i] = device;
1039                 cards++;
1040         }
1041         if (!cards) {
1042 #ifdef MODULE
1043                 printk(KERN_ERR "aloop: No loopback enabled\n");
1044 #endif
1045                 loopback_unregister_all();
1046                 return -ENODEV;
1047         }
1048         return 0;
1049 }
1050
1051 static void __exit alsa_card_loopback_exit(void)
1052 {
1053         loopback_unregister_all();
1054 }
1055
1056 module_init(alsa_card_loopback_init)
1057 module_exit(alsa_card_loopback_exit)