~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/drivers/aloop.c

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp