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