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

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

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

~ [ 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