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