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