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