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