5 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 * More accurate positioning and full-duplex support:
8 * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
10 * Major (almost complete) rewrite:
11 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
13 * A next major update in 2010 (separate timers for playback and capture):
14 * Copyright (c) Jaroslav Kysela <perex@perex.cz>
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.
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.
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
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>
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}}");
51 #define MAX_PCM_SUBSTREAMS 8
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
];
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.");
70 #define NO_PITCH 100000
74 struct loopback_cable
{
76 struct loopback_pcm
*streams
[2];
77 struct snd_pcm_hardware hw
;
84 struct loopback_setup
{
85 unsigned int notify
: 1;
86 unsigned int rate_shift
;
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
;
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];
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
;
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 */
117 unsigned int period_update_pending
:1;
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
;
126 static struct platform_device
*devices
[SNDRV_CARDS
];
128 static inline unsigned int byte_pos(struct loopback_pcm
*dpcm
, unsigned int x
)
130 if (dpcm
->pcm_rate_shift
== NO_PITCH
) {
133 x
= div_u64(NO_PITCH
* (unsigned long long)x
,
134 HZ
* (unsigned long long)dpcm
->pcm_rate_shift
);
136 return x
- (x
% dpcm
->pcm_salign
);
139 static inline unsigned int frac_pos(struct loopback_pcm
*dpcm
, unsigned int x
)
141 if (dpcm
->pcm_rate_shift
== NO_PITCH
) { /* no pitch */
144 x
= div_u64(dpcm
->pcm_rate_shift
* (unsigned long long)x
* HZ
,
150 static inline struct loopback_setup
*get_setup(struct loopback_pcm
*dpcm
)
152 int device
= dpcm
->substream
->pstr
->pcm
->device
;
154 if (dpcm
->substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
156 return &dpcm
->loopback
->setup
[dpcm
->substream
->number
][device
];
159 static inline unsigned int get_notify(struct loopback_pcm
*dpcm
)
161 return get_setup(dpcm
)->notify
;
164 static inline unsigned int get_rate_shift(struct loopback_pcm
*dpcm
)
166 return get_setup(dpcm
)->rate_shift
;
169 /* call in cable->lock */
170 static void loopback_timer_start(struct loopback_pcm
*dpcm
)
173 unsigned int rate_shift
= get_rate_shift(dpcm
);
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
);
179 if (dpcm
->period_size_frac
<= dpcm
->irq_pos
) {
180 dpcm
->irq_pos
%= dpcm
->period_size_frac
;
181 dpcm
->period_update_pending
= 1;
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
);
188 /* call in cable->lock */
189 static inline void loopback_timer_stop(struct loopback_pcm
*dpcm
)
191 del_timer(&dpcm
->timer
);
192 dpcm
->timer
.expires
= 0;
195 static inline void loopback_timer_stop_sync(struct loopback_pcm
*dpcm
)
197 del_timer_sync(&dpcm
->timer
);
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)
204 static int loopback_check_format(struct loopback_cable
*cable
, int stream
)
206 struct snd_pcm_runtime
*runtime
, *cruntime
;
207 struct loopback_setup
*setup
;
208 struct snd_card
*card
;
211 if (cable
->valid
!= CABLE_VALID_BOTH
) {
212 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
216 runtime
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->
218 cruntime
= cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
]->
220 check
= runtime
->format
!= cruntime
->format
||
221 runtime
->rate
!= cruntime
->rate
||
222 runtime
->channels
!= cruntime
->channels
;
225 if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
228 snd_pcm_stop(cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
]->
229 substream
, SNDRV_PCM_STATE_DRAINING
);
231 runtime
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->
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
,
238 setup
->format
= runtime
->format
;
240 if (setup
->rate
!= runtime
->rate
) {
241 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
243 setup
->rate
= runtime
->rate
;
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
;
254 static void loopback_active_notify(struct loopback_pcm
*dpcm
)
256 snd_ctl_notify(dpcm
->loopback
->card
,
257 SNDRV_CTL_EVENT_MASK_VALUE
,
258 &get_setup(dpcm
)->active_id
);
261 static int loopback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
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
;
269 case SNDRV_PCM_TRIGGER_START
:
270 err
= loopback_check_format(cable
, substream
->stream
);
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
);
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
);
293 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
294 case SNDRV_PCM_TRIGGER_SUSPEND
:
295 spin_lock(&cable
->lock
);
296 cable
->pause
|= stream
;
297 loopback_timer_stop(dpcm
);
298 spin_unlock(&cable
->lock
);
299 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
300 loopback_active_notify(dpcm
);
302 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
303 case SNDRV_PCM_TRIGGER_RESUME
:
304 spin_lock(&cable
->lock
);
305 dpcm
->last_jiffies
= jiffies
;
306 cable
->pause
&= ~stream
;
307 loopback_timer_start(dpcm
);
308 spin_unlock(&cable
->lock
);
309 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
310 loopback_active_notify(dpcm
);
318 static void params_change(struct snd_pcm_substream
*substream
)
320 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
321 struct loopback_pcm
*dpcm
= runtime
->private_data
;
322 struct loopback_cable
*cable
= dpcm
->cable
;
324 cable
->hw
.formats
= pcm_format_to_bits(runtime
->format
);
325 cable
->hw
.rate_min
= runtime
->rate
;
326 cable
->hw
.rate_max
= runtime
->rate
;
327 cable
->hw
.channels_min
= runtime
->channels
;
328 cable
->hw
.channels_max
= runtime
->channels
;
331 static int loopback_prepare(struct snd_pcm_substream
*substream
)
333 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
334 struct loopback_pcm
*dpcm
= runtime
->private_data
;
335 struct loopback_cable
*cable
= dpcm
->cable
;
338 loopback_timer_stop_sync(dpcm
);
340 salign
= (snd_pcm_format_width(runtime
->format
) *
341 runtime
->channels
) / 8;
342 bps
= salign
* runtime
->rate
;
343 if (bps
<= 0 || salign
<= 0)
347 dpcm
->pcm_buffer_size
= frames_to_bytes(runtime
, runtime
->buffer_size
);
348 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
349 /* clear capture buffer */
350 dpcm
->silent_size
= dpcm
->pcm_buffer_size
;
351 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
,
352 runtime
->buffer_size
* runtime
->channels
);
356 dpcm
->period_update_pending
= 0;
358 dpcm
->pcm_salign
= salign
;
359 dpcm
->pcm_period_size
= frames_to_bytes(runtime
, runtime
->period_size
);
361 mutex_lock(&dpcm
->loopback
->cable_lock
);
362 if (!(cable
->valid
& ~(1 << substream
->stream
)) ||
363 (get_setup(dpcm
)->notify
&&
364 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
))
365 params_change(substream
);
366 cable
->valid
|= 1 << substream
->stream
;
367 mutex_unlock(&dpcm
->loopback
->cable_lock
);
372 static void clear_capture_buf(struct loopback_pcm
*dpcm
, unsigned int bytes
)
374 struct snd_pcm_runtime
*runtime
= dpcm
->substream
->runtime
;
375 char *dst
= runtime
->dma_area
;
376 unsigned int dst_off
= dpcm
->buf_pos
;
378 if (dpcm
->silent_size
>= dpcm
->pcm_buffer_size
)
380 if (dpcm
->silent_size
+ bytes
> dpcm
->pcm_buffer_size
)
381 bytes
= dpcm
->pcm_buffer_size
- dpcm
->silent_size
;
384 unsigned int size
= bytes
;
385 if (dst_off
+ size
> dpcm
->pcm_buffer_size
)
386 size
= dpcm
->pcm_buffer_size
- dst_off
;
387 snd_pcm_format_set_silence(runtime
->format
, dst
+ dst_off
,
388 bytes_to_frames(runtime
, size
) *
390 dpcm
->silent_size
+= size
;
398 static void copy_play_buf(struct loopback_pcm
*play
,
399 struct loopback_pcm
*capt
,
402 struct snd_pcm_runtime
*runtime
= play
->substream
->runtime
;
403 char *src
= runtime
->dma_area
;
404 char *dst
= capt
->substream
->runtime
->dma_area
;
405 unsigned int src_off
= play
->buf_pos
;
406 unsigned int dst_off
= capt
->buf_pos
;
407 unsigned int clear_bytes
= 0;
409 /* check if playback is draining, trim the capture copy size
410 * when our pointer is at the end of playback ring buffer */
411 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
412 snd_pcm_playback_hw_avail(runtime
) < runtime
->buffer_size
) {
413 snd_pcm_uframes_t appl_ptr
, appl_ptr1
, diff
;
414 appl_ptr
= appl_ptr1
= runtime
->control
->appl_ptr
;
415 appl_ptr1
-= appl_ptr1
% runtime
->buffer_size
;
416 appl_ptr1
+= play
->buf_pos
/ play
->pcm_salign
;
417 if (appl_ptr
< appl_ptr1
)
418 appl_ptr1
-= runtime
->buffer_size
;
419 diff
= (appl_ptr
- appl_ptr1
) * play
->pcm_salign
;
421 clear_bytes
= bytes
- diff
;
427 unsigned int size
= bytes
;
428 if (src_off
+ size
> play
->pcm_buffer_size
)
429 size
= play
->pcm_buffer_size
- src_off
;
430 if (dst_off
+ size
> capt
->pcm_buffer_size
)
431 size
= capt
->pcm_buffer_size
- dst_off
;
432 memcpy(dst
+ dst_off
, src
+ src_off
, size
);
433 capt
->silent_size
= 0;
437 src_off
= (src_off
+ size
) % play
->pcm_buffer_size
;
438 dst_off
= (dst_off
+ size
) % capt
->pcm_buffer_size
;
441 if (clear_bytes
> 0) {
442 clear_capture_buf(capt
, clear_bytes
);
443 capt
->silent_size
= 0;
447 static inline unsigned int bytepos_delta(struct loopback_pcm
*dpcm
,
448 unsigned int jiffies_delta
)
450 unsigned long last_pos
;
453 last_pos
= byte_pos(dpcm
, dpcm
->irq_pos
);
454 dpcm
->irq_pos
+= jiffies_delta
* dpcm
->pcm_bps
;
455 delta
= byte_pos(dpcm
, dpcm
->irq_pos
) - last_pos
;
456 if (delta
>= dpcm
->last_drift
)
457 delta
-= dpcm
->last_drift
;
458 dpcm
->last_drift
= 0;
459 if (dpcm
->irq_pos
>= dpcm
->period_size_frac
) {
460 dpcm
->irq_pos
%= dpcm
->period_size_frac
;
461 dpcm
->period_update_pending
= 1;
466 static inline void bytepos_finish(struct loopback_pcm
*dpcm
,
469 dpcm
->buf_pos
+= delta
;
470 dpcm
->buf_pos
%= dpcm
->pcm_buffer_size
;
473 /* call in cable->lock */
474 static unsigned int loopback_pos_update(struct loopback_cable
*cable
)
476 struct loopback_pcm
*dpcm_play
=
477 cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
478 struct loopback_pcm
*dpcm_capt
=
479 cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
480 unsigned long delta_play
= 0, delta_capt
= 0;
481 unsigned int running
, count1
, count2
;
483 running
= cable
->running
^ cable
->pause
;
484 if (running
& (1 << SNDRV_PCM_STREAM_PLAYBACK
)) {
485 delta_play
= jiffies
- dpcm_play
->last_jiffies
;
486 dpcm_play
->last_jiffies
+= delta_play
;
489 if (running
& (1 << SNDRV_PCM_STREAM_CAPTURE
)) {
490 delta_capt
= jiffies
- dpcm_capt
->last_jiffies
;
491 dpcm_capt
->last_jiffies
+= delta_capt
;
494 if (delta_play
== 0 && delta_capt
== 0)
497 if (delta_play
> delta_capt
) {
498 count1
= bytepos_delta(dpcm_play
, delta_play
- delta_capt
);
499 bytepos_finish(dpcm_play
, count1
);
500 delta_play
= delta_capt
;
501 } else if (delta_play
< delta_capt
) {
502 count1
= bytepos_delta(dpcm_capt
, delta_capt
- delta_play
);
503 clear_capture_buf(dpcm_capt
, count1
);
504 bytepos_finish(dpcm_capt
, count1
);
505 delta_capt
= delta_play
;
508 if (delta_play
== 0 && delta_capt
== 0)
511 /* note delta_capt == delta_play at this moment */
512 count1
= bytepos_delta(dpcm_play
, delta_play
);
513 count2
= bytepos_delta(dpcm_capt
, delta_capt
);
514 if (count1
< count2
) {
515 dpcm_capt
->last_drift
= count2
- count1
;
517 } else if (count1
> count2
) {
518 dpcm_play
->last_drift
= count1
- count2
;
520 copy_play_buf(dpcm_play
, dpcm_capt
, count1
);
521 bytepos_finish(dpcm_play
, count1
);
522 bytepos_finish(dpcm_capt
, count1
);
527 static void loopback_timer_function(struct timer_list
*t
)
529 struct loopback_pcm
*dpcm
= from_timer(dpcm
, t
, timer
);
532 spin_lock_irqsave(&dpcm
->cable
->lock
, flags
);
533 if (loopback_pos_update(dpcm
->cable
) & (1 << dpcm
->substream
->stream
)) {
534 loopback_timer_start(dpcm
);
535 if (dpcm
->period_update_pending
) {
536 dpcm
->period_update_pending
= 0;
537 spin_unlock_irqrestore(&dpcm
->cable
->lock
, flags
);
538 /* need to unlock before calling below */
539 snd_pcm_period_elapsed(dpcm
->substream
);
543 spin_unlock_irqrestore(&dpcm
->cable
->lock
, flags
);
546 static snd_pcm_uframes_t
loopback_pointer(struct snd_pcm_substream
*substream
)
548 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
549 struct loopback_pcm
*dpcm
= runtime
->private_data
;
550 snd_pcm_uframes_t pos
;
552 spin_lock(&dpcm
->cable
->lock
);
553 loopback_pos_update(dpcm
->cable
);
555 spin_unlock(&dpcm
->cable
->lock
);
556 return bytes_to_frames(runtime
, pos
);
559 static const struct snd_pcm_hardware loopback_pcm_hardware
=
561 .info
= (SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_MMAP
|
562 SNDRV_PCM_INFO_MMAP_VALID
| SNDRV_PCM_INFO_PAUSE
|
563 SNDRV_PCM_INFO_RESUME
),
564 .formats
= (SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
|
565 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
|
566 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
),
567 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_192000
,
572 .buffer_bytes_max
= 2 * 1024 * 1024,
573 .period_bytes_min
= 64,
574 /* note check overflow in frac_pos() using pcm_rate_shift before
575 changing period_bytes_max value */
576 .period_bytes_max
= 1024 * 1024,
582 static void loopback_runtime_free(struct snd_pcm_runtime
*runtime
)
584 struct loopback_pcm
*dpcm
= runtime
->private_data
;
588 static int loopback_hw_params(struct snd_pcm_substream
*substream
,
589 struct snd_pcm_hw_params
*params
)
591 return snd_pcm_lib_alloc_vmalloc_buffer(substream
,
592 params_buffer_bytes(params
));
595 static int loopback_hw_free(struct snd_pcm_substream
*substream
)
597 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
598 struct loopback_pcm
*dpcm
= runtime
->private_data
;
599 struct loopback_cable
*cable
= dpcm
->cable
;
601 mutex_lock(&dpcm
->loopback
->cable_lock
);
602 cable
->valid
&= ~(1 << substream
->stream
);
603 mutex_unlock(&dpcm
->loopback
->cable_lock
);
604 return snd_pcm_lib_free_vmalloc_buffer(substream
);
607 static unsigned int get_cable_index(struct snd_pcm_substream
*substream
)
609 if (!substream
->pcm
->device
)
610 return substream
->stream
;
612 return !substream
->stream
;
615 static int rule_format(struct snd_pcm_hw_params
*params
,
616 struct snd_pcm_hw_rule
*rule
)
618 struct loopback_pcm
*dpcm
= rule
->private;
619 struct loopback_cable
*cable
= dpcm
->cable
;
623 mutex_lock(&dpcm
->loopback
->cable_lock
);
624 m
.bits
[0] = (u_int32_t
)cable
->hw
.formats
;
625 m
.bits
[1] = (u_int32_t
)(cable
->hw
.formats
>> 32);
626 mutex_unlock(&dpcm
->loopback
->cable_lock
);
627 return snd_mask_refine(hw_param_mask(params
, rule
->var
), &m
);
630 static int rule_rate(struct snd_pcm_hw_params
*params
,
631 struct snd_pcm_hw_rule
*rule
)
633 struct loopback_pcm
*dpcm
= rule
->private;
634 struct loopback_cable
*cable
= dpcm
->cable
;
635 struct snd_interval t
;
637 mutex_lock(&dpcm
->loopback
->cable_lock
);
638 t
.min
= cable
->hw
.rate_min
;
639 t
.max
= cable
->hw
.rate_max
;
640 mutex_unlock(&dpcm
->loopback
->cable_lock
);
641 t
.openmin
= t
.openmax
= 0;
643 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
646 static int rule_channels(struct snd_pcm_hw_params
*params
,
647 struct snd_pcm_hw_rule
*rule
)
649 struct loopback_pcm
*dpcm
= rule
->private;
650 struct loopback_cable
*cable
= dpcm
->cable
;
651 struct snd_interval t
;
653 mutex_lock(&dpcm
->loopback
->cable_lock
);
654 t
.min
= cable
->hw
.channels_min
;
655 t
.max
= cable
->hw
.channels_max
;
656 mutex_unlock(&dpcm
->loopback
->cable_lock
);
657 t
.openmin
= t
.openmax
= 0;
659 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
662 static void free_cable(struct snd_pcm_substream
*substream
)
664 struct loopback
*loopback
= substream
->private_data
;
665 int dev
= get_cable_index(substream
);
666 struct loopback_cable
*cable
;
668 cable
= loopback
->cables
[substream
->number
][dev
];
671 if (cable
->streams
[!substream
->stream
]) {
672 /* other stream is still alive */
673 spin_lock_irq(&cable
->lock
);
674 cable
->streams
[substream
->stream
] = NULL
;
675 spin_unlock_irq(&cable
->lock
);
678 loopback
->cables
[substream
->number
][dev
] = NULL
;
683 static int loopback_open(struct snd_pcm_substream
*substream
)
685 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
686 struct loopback
*loopback
= substream
->private_data
;
687 struct loopback_pcm
*dpcm
;
688 struct loopback_cable
*cable
= NULL
;
690 int dev
= get_cable_index(substream
);
692 mutex_lock(&loopback
->cable_lock
);
693 dpcm
= kzalloc(sizeof(*dpcm
), GFP_KERNEL
);
698 dpcm
->loopback
= loopback
;
699 dpcm
->substream
= substream
;
700 timer_setup(&dpcm
->timer
, loopback_timer_function
, 0);
702 cable
= loopback
->cables
[substream
->number
][dev
];
704 cable
= kzalloc(sizeof(*cable
), GFP_KERNEL
);
709 spin_lock_init(&cable
->lock
);
710 cable
->hw
= loopback_pcm_hardware
;
711 loopback
->cables
[substream
->number
][dev
] = cable
;
715 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
717 /* use dynamic rules based on actual runtime->hw values */
718 /* note that the default rules created in the PCM midlevel code */
719 /* are cached -> they do not reflect the actual state */
720 err
= snd_pcm_hw_rule_add(runtime
, 0,
721 SNDRV_PCM_HW_PARAM_FORMAT
,
723 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
726 err
= snd_pcm_hw_rule_add(runtime
, 0,
727 SNDRV_PCM_HW_PARAM_RATE
,
729 SNDRV_PCM_HW_PARAM_RATE
, -1);
732 err
= snd_pcm_hw_rule_add(runtime
, 0,
733 SNDRV_PCM_HW_PARAM_CHANNELS
,
735 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
739 runtime
->private_data
= dpcm
;
740 runtime
->private_free
= loopback_runtime_free
;
741 if (get_notify(dpcm
))
742 runtime
->hw
= loopback_pcm_hardware
;
744 runtime
->hw
= cable
->hw
;
746 spin_lock_irq(&cable
->lock
);
747 cable
->streams
[substream
->stream
] = dpcm
;
748 spin_unlock_irq(&cable
->lock
);
752 free_cable(substream
);
755 mutex_unlock(&loopback
->cable_lock
);
759 static int loopback_close(struct snd_pcm_substream
*substream
)
761 struct loopback
*loopback
= substream
->private_data
;
762 struct loopback_pcm
*dpcm
= substream
->runtime
->private_data
;
764 loopback_timer_stop_sync(dpcm
);
765 mutex_lock(&loopback
->cable_lock
);
766 free_cable(substream
);
767 mutex_unlock(&loopback
->cable_lock
);
771 static const struct snd_pcm_ops loopback_playback_ops
= {
772 .open
= loopback_open
,
773 .close
= loopback_close
,
774 .ioctl
= snd_pcm_lib_ioctl
,
775 .hw_params
= loopback_hw_params
,
776 .hw_free
= loopback_hw_free
,
777 .prepare
= loopback_prepare
,
778 .trigger
= loopback_trigger
,
779 .pointer
= loopback_pointer
,
780 .page
= snd_pcm_lib_get_vmalloc_page
,
781 .mmap
= snd_pcm_lib_mmap_vmalloc
,
784 static const struct snd_pcm_ops loopback_capture_ops
= {
785 .open
= loopback_open
,
786 .close
= loopback_close
,
787 .ioctl
= snd_pcm_lib_ioctl
,
788 .hw_params
= loopback_hw_params
,
789 .hw_free
= loopback_hw_free
,
790 .prepare
= loopback_prepare
,
791 .trigger
= loopback_trigger
,
792 .pointer
= loopback_pointer
,
793 .page
= snd_pcm_lib_get_vmalloc_page
,
794 .mmap
= snd_pcm_lib_mmap_vmalloc
,
797 static int loopback_pcm_new(struct loopback
*loopback
,
798 int device
, int substreams
)
803 err
= snd_pcm_new(loopback
->card
, "Loopback PCM", device
,
804 substreams
, substreams
, &pcm
);
807 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &loopback_playback_ops
);
808 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &loopback_capture_ops
);
810 pcm
->private_data
= loopback
;
812 strcpy(pcm
->name
, "Loopback PCM");
814 loopback
->pcm
[device
] = pcm
;
818 static int loopback_rate_shift_info(struct snd_kcontrol
*kcontrol
,
819 struct snd_ctl_elem_info
*uinfo
)
821 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
823 uinfo
->value
.integer
.min
= 80000;
824 uinfo
->value
.integer
.max
= 120000;
825 uinfo
->value
.integer
.step
= 1;
829 static int loopback_rate_shift_get(struct snd_kcontrol
*kcontrol
,
830 struct snd_ctl_elem_value
*ucontrol
)
832 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
834 mutex_lock(&loopback
->cable_lock
);
835 ucontrol
->value
.integer
.value
[0] =
836 loopback
->setup
[kcontrol
->id
.subdevice
]
837 [kcontrol
->id
.device
].rate_shift
;
838 mutex_unlock(&loopback
->cable_lock
);
842 static int loopback_rate_shift_put(struct snd_kcontrol
*kcontrol
,
843 struct snd_ctl_elem_value
*ucontrol
)
845 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
849 val
= ucontrol
->value
.integer
.value
[0];
854 mutex_lock(&loopback
->cable_lock
);
855 if (val
!= loopback
->setup
[kcontrol
->id
.subdevice
]
856 [kcontrol
->id
.device
].rate_shift
) {
857 loopback
->setup
[kcontrol
->id
.subdevice
]
858 [kcontrol
->id
.device
].rate_shift
= val
;
861 mutex_unlock(&loopback
->cable_lock
);
865 static int loopback_notify_get(struct snd_kcontrol
*kcontrol
,
866 struct snd_ctl_elem_value
*ucontrol
)
868 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
870 mutex_lock(&loopback
->cable_lock
);
871 ucontrol
->value
.integer
.value
[0] =
872 loopback
->setup
[kcontrol
->id
.subdevice
]
873 [kcontrol
->id
.device
].notify
;
874 mutex_unlock(&loopback
->cable_lock
);
878 static int loopback_notify_put(struct snd_kcontrol
*kcontrol
,
879 struct snd_ctl_elem_value
*ucontrol
)
881 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
885 val
= ucontrol
->value
.integer
.value
[0] ? 1 : 0;
886 mutex_lock(&loopback
->cable_lock
);
887 if (val
!= loopback
->setup
[kcontrol
->id
.subdevice
]
888 [kcontrol
->id
.device
].notify
) {
889 loopback
->setup
[kcontrol
->id
.subdevice
]
890 [kcontrol
->id
.device
].notify
= val
;
893 mutex_unlock(&loopback
->cable_lock
);
897 static int loopback_active_get(struct snd_kcontrol
*kcontrol
,
898 struct snd_ctl_elem_value
*ucontrol
)
900 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
901 struct loopback_cable
*cable
;
903 unsigned int val
= 0;
905 mutex_lock(&loopback
->cable_lock
);
906 cable
= loopback
->cables
[kcontrol
->id
.subdevice
][kcontrol
->id
.device
^ 1];
908 unsigned int running
= cable
->running
^ cable
->pause
;
910 val
= (running
& (1 << SNDRV_PCM_STREAM_PLAYBACK
)) ? 1 : 0;
912 mutex_unlock(&loopback
->cable_lock
);
913 ucontrol
->value
.integer
.value
[0] = val
;
917 static int loopback_format_info(struct snd_kcontrol
*kcontrol
,
918 struct snd_ctl_elem_info
*uinfo
)
920 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
922 uinfo
->value
.integer
.min
= 0;
923 uinfo
->value
.integer
.max
= SNDRV_PCM_FORMAT_LAST
;
924 uinfo
->value
.integer
.step
= 1;
928 static int loopback_format_get(struct snd_kcontrol
*kcontrol
,
929 struct snd_ctl_elem_value
*ucontrol
)
931 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
933 ucontrol
->value
.integer
.value
[0] =
934 loopback
->setup
[kcontrol
->id
.subdevice
]
935 [kcontrol
->id
.device
].format
;
939 static int loopback_rate_info(struct snd_kcontrol
*kcontrol
,
940 struct snd_ctl_elem_info
*uinfo
)
942 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
944 uinfo
->value
.integer
.min
= 0;
945 uinfo
->value
.integer
.max
= 192000;
946 uinfo
->value
.integer
.step
= 1;
950 static int loopback_rate_get(struct snd_kcontrol
*kcontrol
,
951 struct snd_ctl_elem_value
*ucontrol
)
953 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
955 mutex_lock(&loopback
->cable_lock
);
956 ucontrol
->value
.integer
.value
[0] =
957 loopback
->setup
[kcontrol
->id
.subdevice
]
958 [kcontrol
->id
.device
].rate
;
959 mutex_unlock(&loopback
->cable_lock
);
963 static int loopback_channels_info(struct snd_kcontrol
*kcontrol
,
964 struct snd_ctl_elem_info
*uinfo
)
966 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
968 uinfo
->value
.integer
.min
= 1;
969 uinfo
->value
.integer
.max
= 1024;
970 uinfo
->value
.integer
.step
= 1;
974 static int loopback_channels_get(struct snd_kcontrol
*kcontrol
,
975 struct snd_ctl_elem_value
*ucontrol
)
977 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
979 mutex_lock(&loopback
->cable_lock
);
980 ucontrol
->value
.integer
.value
[0] =
981 loopback
->setup
[kcontrol
->id
.subdevice
]
982 [kcontrol
->id
.device
].channels
;
983 mutex_unlock(&loopback
->cable_lock
);
987 static struct snd_kcontrol_new loopback_controls
[] = {
989 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
990 .name
= "PCM Rate Shift 100000",
991 .info
= loopback_rate_shift_info
,
992 .get
= loopback_rate_shift_get
,
993 .put
= loopback_rate_shift_put
,
996 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
997 .name
= "PCM Notify",
998 .info
= snd_ctl_boolean_mono_info
,
999 .get
= loopback_notify_get
,
1000 .put
= loopback_notify_put
,
1002 #define ACTIVE_IDX 2
1004 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1005 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1006 .name
= "PCM Slave Active",
1007 .info
= snd_ctl_boolean_mono_info
,
1008 .get
= loopback_active_get
,
1010 #define FORMAT_IDX 3
1012 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1013 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1014 .name
= "PCM Slave Format",
1015 .info
= loopback_format_info
,
1016 .get
= loopback_format_get
1020 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1021 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1022 .name
= "PCM Slave Rate",
1023 .info
= loopback_rate_info
,
1024 .get
= loopback_rate_get
1026 #define CHANNELS_IDX 5
1028 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1029 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1030 .name
= "PCM Slave Channels",
1031 .info
= loopback_channels_info
,
1032 .get
= loopback_channels_get
1036 static int loopback_mixer_new(struct loopback
*loopback
, int notify
)
1038 struct snd_card
*card
= loopback
->card
;
1039 struct snd_pcm
*pcm
;
1040 struct snd_kcontrol
*kctl
;
1041 struct loopback_setup
*setup
;
1042 int err
, dev
, substr
, substr_count
, idx
;
1044 strcpy(card
->mixername
, "Loopback Mixer");
1045 for (dev
= 0; dev
< 2; dev
++) {
1046 pcm
= loopback
->pcm
[dev
];
1048 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
;
1049 for (substr
= 0; substr
< substr_count
; substr
++) {
1050 setup
= &loopback
->setup
[substr
][dev
];
1051 setup
->notify
= notify
;
1052 setup
->rate_shift
= NO_PITCH
;
1053 setup
->format
= SNDRV_PCM_FORMAT_S16_LE
;
1054 setup
->rate
= 48000;
1055 setup
->channels
= 2;
1056 for (idx
= 0; idx
< ARRAY_SIZE(loopback_controls
);
1058 kctl
= snd_ctl_new1(&loopback_controls
[idx
],
1062 kctl
->id
.device
= dev
;
1063 kctl
->id
.subdevice
= substr
;
1066 setup
->active_id
= kctl
->id
;
1069 setup
->format_id
= kctl
->id
;
1072 setup
->rate_id
= kctl
->id
;
1075 setup
->channels_id
= kctl
->id
;
1080 err
= snd_ctl_add(card
, kctl
);
1089 static void print_dpcm_info(struct snd_info_buffer
*buffer
,
1090 struct loopback_pcm
*dpcm
,
1093 snd_iprintf(buffer
, " %s\n", id
);
1095 snd_iprintf(buffer
, " inactive\n");
1098 snd_iprintf(buffer
, " buffer_size:\t%u\n", dpcm
->pcm_buffer_size
);
1099 snd_iprintf(buffer
, " buffer_pos:\t\t%u\n", dpcm
->buf_pos
);
1100 snd_iprintf(buffer
, " silent_size:\t%u\n", dpcm
->silent_size
);
1101 snd_iprintf(buffer
, " period_size:\t%u\n", dpcm
->pcm_period_size
);
1102 snd_iprintf(buffer
, " bytes_per_sec:\t%u\n", dpcm
->pcm_bps
);
1103 snd_iprintf(buffer
, " sample_align:\t%u\n", dpcm
->pcm_salign
);
1104 snd_iprintf(buffer
, " rate_shift:\t\t%u\n", dpcm
->pcm_rate_shift
);
1105 snd_iprintf(buffer
, " update_pending:\t%u\n",
1106 dpcm
->period_update_pending
);
1107 snd_iprintf(buffer
, " irq_pos:\t\t%u\n", dpcm
->irq_pos
);
1108 snd_iprintf(buffer
, " period_frac:\t%u\n", dpcm
->period_size_frac
);
1109 snd_iprintf(buffer
, " last_jiffies:\t%lu (%lu)\n",
1110 dpcm
->last_jiffies
, jiffies
);
1111 snd_iprintf(buffer
, " timer_expires:\t%lu\n", dpcm
->timer
.expires
);
1114 static void print_substream_info(struct snd_info_buffer
*buffer
,
1115 struct loopback
*loopback
,
1119 struct loopback_cable
*cable
= loopback
->cables
[sub
][num
];
1121 snd_iprintf(buffer
, "Cable %i substream %i:\n", num
, sub
);
1122 if (cable
== NULL
) {
1123 snd_iprintf(buffer
, " inactive\n");
1126 snd_iprintf(buffer
, " valid: %u\n", cable
->valid
);
1127 snd_iprintf(buffer
, " running: %u\n", cable
->running
);
1128 snd_iprintf(buffer
, " pause: %u\n", cable
->pause
);
1129 print_dpcm_info(buffer
, cable
->streams
[0], "Playback");
1130 print_dpcm_info(buffer
, cable
->streams
[1], "Capture");
1133 static void print_cable_info(struct snd_info_entry
*entry
,
1134 struct snd_info_buffer
*buffer
)
1136 struct loopback
*loopback
= entry
->private_data
;
1139 mutex_lock(&loopback
->cable_lock
);
1140 num
= entry
->name
[strlen(entry
->name
)-1];
1141 num
= num
== '0' ? 0 : 1;
1142 for (sub
= 0; sub
< MAX_PCM_SUBSTREAMS
; sub
++)
1143 print_substream_info(buffer
, loopback
, sub
, num
);
1144 mutex_unlock(&loopback
->cable_lock
);
1147 static int loopback_proc_new(struct loopback
*loopback
, int cidx
)
1150 struct snd_info_entry
*entry
;
1153 snprintf(name
, sizeof(name
), "cable#%d", cidx
);
1154 err
= snd_card_proc_new(loopback
->card
, name
, &entry
);
1158 snd_info_set_text_ops(entry
, loopback
, print_cable_info
);
1162 static int loopback_probe(struct platform_device
*devptr
)
1164 struct snd_card
*card
;
1165 struct loopback
*loopback
;
1166 int dev
= devptr
->id
;
1169 err
= snd_card_new(&devptr
->dev
, index
[dev
], id
[dev
], THIS_MODULE
,
1170 sizeof(struct loopback
), &card
);
1173 loopback
= card
->private_data
;
1175 if (pcm_substreams
[dev
] < 1)
1176 pcm_substreams
[dev
] = 1;
1177 if (pcm_substreams
[dev
] > MAX_PCM_SUBSTREAMS
)
1178 pcm_substreams
[dev
] = MAX_PCM_SUBSTREAMS
;
1180 loopback
->card
= card
;
1181 mutex_init(&loopback
->cable_lock
);
1183 err
= loopback_pcm_new(loopback
, 0, pcm_substreams
[dev
]);
1186 err
= loopback_pcm_new(loopback
, 1, pcm_substreams
[dev
]);
1189 err
= loopback_mixer_new(loopback
, pcm_notify
[dev
] ? 1 : 0);
1192 loopback_proc_new(loopback
, 0);
1193 loopback_proc_new(loopback
, 1);
1194 strcpy(card
->driver
, "Loopback");
1195 strcpy(card
->shortname
, "Loopback");
1196 sprintf(card
->longname
, "Loopback %i", dev
+ 1);
1197 err
= snd_card_register(card
);
1199 platform_set_drvdata(devptr
, card
);
1203 snd_card_free(card
);
1207 static int loopback_remove(struct platform_device
*devptr
)
1209 snd_card_free(platform_get_drvdata(devptr
));
1213 #ifdef CONFIG_PM_SLEEP
1214 static int loopback_suspend(struct device
*pdev
)
1216 struct snd_card
*card
= dev_get_drvdata(pdev
);
1217 struct loopback
*loopback
= card
->private_data
;
1219 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
1221 snd_pcm_suspend_all(loopback
->pcm
[0]);
1222 snd_pcm_suspend_all(loopback
->pcm
[1]);
1226 static int loopback_resume(struct device
*pdev
)
1228 struct snd_card
*card
= dev_get_drvdata(pdev
);
1230 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
1234 static SIMPLE_DEV_PM_OPS(loopback_pm
, loopback_suspend
, loopback_resume
);
1235 #define LOOPBACK_PM_OPS &loopback_pm
1237 #define LOOPBACK_PM_OPS NULL
1240 #define SND_LOOPBACK_DRIVER "snd_aloop"
1242 static struct platform_driver loopback_driver
= {
1243 .probe
= loopback_probe
,
1244 .remove
= loopback_remove
,
1246 .name
= SND_LOOPBACK_DRIVER
,
1247 .pm
= LOOPBACK_PM_OPS
,
1251 static void loopback_unregister_all(void)
1255 for (i
= 0; i
< ARRAY_SIZE(devices
); ++i
)
1256 platform_device_unregister(devices
[i
]);
1257 platform_driver_unregister(&loopback_driver
);
1260 static int __init
alsa_card_loopback_init(void)
1264 err
= platform_driver_register(&loopback_driver
);
1270 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1271 struct platform_device
*device
;
1274 device
= platform_device_register_simple(SND_LOOPBACK_DRIVER
,
1278 if (!platform_get_drvdata(device
)) {
1279 platform_device_unregister(device
);
1282 devices
[i
] = device
;
1287 printk(KERN_ERR
"aloop: No loopback enabled\n");
1289 loopback_unregister_all();
1295 static void __exit
alsa_card_loopback_exit(void)
1297 loopback_unregister_all();
1300 module_init(alsa_card_loopback_init
)
1301 module_exit(alsa_card_loopback_exit
)