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