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