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