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/moduleparam.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/info.h>
43 #include <sound/initval.h>
45 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
46 MODULE_DESCRIPTION("A loopback soundcard");
47 MODULE_LICENSE("GPL");
48 MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
50 #define MAX_PCM_SUBSTREAMS 8
52 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-MAX */
53 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* ID for this card */
54 static int enable
[SNDRV_CARDS
] = {1, [1 ... (SNDRV_CARDS
- 1)] = 0};
55 static int pcm_substreams
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
- 1)] = 8};
56 static int pcm_notify
[SNDRV_CARDS
];
58 module_param_array(index
, int, NULL
, 0444);
59 MODULE_PARM_DESC(index
, "Index value for loopback soundcard.");
60 module_param_array(id
, charp
, NULL
, 0444);
61 MODULE_PARM_DESC(id
, "ID string for loopback soundcard.");
62 module_param_array(enable
, bool, NULL
, 0444);
63 MODULE_PARM_DESC(enable
, "Enable this loopback soundcard.");
64 module_param_array(pcm_substreams
, int, NULL
, 0444);
65 MODULE_PARM_DESC(pcm_substreams
, "PCM substreams # (1-8) for loopback driver.");
66 module_param_array(pcm_notify
, int, NULL
, 0444);
67 MODULE_PARM_DESC(pcm_notify
, "Break capture when PCM format/rate/channels changes.");
69 #define NO_PITCH 100000
73 struct loopback_cable
{
75 struct loopback_pcm
*streams
[2];
76 struct snd_pcm_hardware hw
;
82 struct loopback_setup
{
83 unsigned int notify
: 1;
84 unsigned int rate_shift
;
87 unsigned int channels
;
88 struct snd_ctl_elem_id active_id
;
89 struct snd_ctl_elem_id format_id
;
90 struct snd_ctl_elem_id rate_id
;
91 struct snd_ctl_elem_id channels_id
;
95 struct snd_card
*card
;
96 struct mutex cable_lock
;
97 struct loopback_cable
*cables
[MAX_PCM_SUBSTREAMS
][2];
98 struct snd_pcm
*pcm
[2];
99 struct loopback_setup setup
[MAX_PCM_SUBSTREAMS
][2];
102 struct loopback_pcm
{
103 struct loopback
*loopback
;
104 struct snd_pcm_substream
*substream
;
105 struct loopback_cable
*cable
;
106 unsigned int pcm_buffer_size
;
107 unsigned int buf_pos
; /* position in buffer */
108 unsigned int silent_size
;
110 unsigned int pcm_period_size
;
111 unsigned int pcm_bps
; /* bytes per second */
112 unsigned int pcm_salign
; /* bytes per sample * channels */
113 unsigned int pcm_rate_shift
; /* rate shift value */
115 unsigned int period_update_pending
:1;
117 unsigned int irq_pos
; /* fractional IRQ position */
118 unsigned int period_size_frac
;
119 unsigned long last_jiffies
;
120 struct timer_list timer
;
123 static struct platform_device
*devices
[SNDRV_CARDS
];
125 static inline unsigned int byte_pos(struct loopback_pcm
*dpcm
, unsigned int x
)
127 if (dpcm
->pcm_rate_shift
== NO_PITCH
) {
130 x
= div_u64(NO_PITCH
* (unsigned long long)x
,
131 HZ
* (unsigned long long)dpcm
->pcm_rate_shift
);
133 return x
- (x
% dpcm
->pcm_salign
);
136 static inline unsigned int frac_pos(struct loopback_pcm
*dpcm
, unsigned int x
)
138 if (dpcm
->pcm_rate_shift
== NO_PITCH
) { /* no pitch */
141 x
= div_u64(dpcm
->pcm_rate_shift
* (unsigned long long)x
* HZ
,
147 static inline struct loopback_setup
*get_setup(struct loopback_pcm
*dpcm
)
149 int device
= dpcm
->substream
->pstr
->pcm
->device
;
151 if (dpcm
->substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
153 return &dpcm
->loopback
->setup
[dpcm
->substream
->number
][device
];
156 static inline unsigned int get_notify(struct loopback_pcm
*dpcm
)
158 return get_setup(dpcm
)->notify
;
161 static inline unsigned int get_rate_shift(struct loopback_pcm
*dpcm
)
163 return get_setup(dpcm
)->rate_shift
;
166 static void loopback_timer_start(struct loopback_pcm
*dpcm
)
169 unsigned int rate_shift
= get_rate_shift(dpcm
);
171 if (rate_shift
!= dpcm
->pcm_rate_shift
) {
172 dpcm
->pcm_rate_shift
= rate_shift
;
173 dpcm
->period_size_frac
= frac_pos(dpcm
, dpcm
->pcm_period_size
);
175 if (dpcm
->period_size_frac
<= dpcm
->irq_pos
) {
176 dpcm
->irq_pos
%= dpcm
->period_size_frac
;
177 dpcm
->period_update_pending
= 1;
179 tick
= dpcm
->period_size_frac
- dpcm
->irq_pos
;
180 tick
= (tick
+ dpcm
->pcm_bps
- 1) / dpcm
->pcm_bps
;
181 dpcm
->timer
.expires
= jiffies
+ tick
;
182 add_timer(&dpcm
->timer
);
185 static inline void loopback_timer_stop(struct loopback_pcm
*dpcm
)
187 del_timer(&dpcm
->timer
);
188 dpcm
->timer
.expires
= 0;
191 #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
192 #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
193 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
195 static int loopback_check_format(struct loopback_cable
*cable
, int stream
)
197 struct snd_pcm_runtime
*runtime
, *cruntime
;
198 struct loopback_setup
*setup
;
199 struct snd_card
*card
;
202 if (cable
->valid
!= CABLE_VALID_BOTH
) {
203 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
207 runtime
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->
209 cruntime
= cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
]->
211 check
= runtime
->format
!= cruntime
->format
||
212 runtime
->rate
!= cruntime
->rate
||
213 runtime
->channels
!= cruntime
->channels
;
216 if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
219 snd_pcm_stop(cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
]->
220 substream
, SNDRV_PCM_STATE_DRAINING
);
222 runtime
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->
224 setup
= get_setup(cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]);
225 card
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->loopback
->card
;
226 if (setup
->format
!= runtime
->format
) {
227 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
229 setup
->format
= runtime
->format
;
231 if (setup
->rate
!= runtime
->rate
) {
232 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
234 setup
->rate
= runtime
->rate
;
236 if (setup
->channels
!= runtime
->channels
) {
237 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
238 &setup
->channels_id
);
239 setup
->channels
= runtime
->channels
;
245 static void loopback_active_notify(struct loopback_pcm
*dpcm
)
247 snd_ctl_notify(dpcm
->loopback
->card
,
248 SNDRV_CTL_EVENT_MASK_VALUE
,
249 &get_setup(dpcm
)->active_id
);
252 static int loopback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
254 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
255 struct loopback_pcm
*dpcm
= runtime
->private_data
;
256 struct loopback_cable
*cable
= dpcm
->cable
;
260 case SNDRV_PCM_TRIGGER_START
:
261 err
= loopback_check_format(cable
, substream
->stream
);
264 dpcm
->last_jiffies
= jiffies
;
265 dpcm
->pcm_rate_shift
= 0;
266 spin_lock(&cable
->lock
);
267 cable
->running
|= (1 << substream
->stream
);
268 spin_unlock(&cable
->lock
);
269 loopback_timer_start(dpcm
);
270 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
271 loopback_active_notify(dpcm
);
273 case SNDRV_PCM_TRIGGER_STOP
:
274 spin_lock(&cable
->lock
);
275 cable
->running
&= ~(1 << substream
->stream
);
276 spin_unlock(&cable
->lock
);
277 loopback_timer_stop(dpcm
);
278 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
279 loopback_active_notify(dpcm
);
287 static void params_change_substream(struct loopback_pcm
*dpcm
,
288 struct snd_pcm_runtime
*runtime
)
290 struct snd_pcm_runtime
*dst_runtime
;
292 if (dpcm
== NULL
|| dpcm
->substream
== NULL
)
294 dst_runtime
= dpcm
->substream
->runtime
;
295 if (dst_runtime
== NULL
)
297 dst_runtime
->hw
= dpcm
->cable
->hw
;
300 static void params_change(struct snd_pcm_substream
*substream
)
302 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
303 struct loopback_pcm
*dpcm
= runtime
->private_data
;
304 struct loopback_cable
*cable
= dpcm
->cable
;
306 cable
->hw
.formats
= (1ULL << runtime
->format
);
307 cable
->hw
.rate_min
= runtime
->rate
;
308 cable
->hw
.rate_max
= runtime
->rate
;
309 cable
->hw
.channels_min
= runtime
->channels
;
310 cable
->hw
.channels_max
= runtime
->channels
;
311 params_change_substream(cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
],
313 params_change_substream(cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
],
317 static int loopback_prepare(struct snd_pcm_substream
*substream
)
319 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
320 struct loopback_pcm
*dpcm
= runtime
->private_data
;
321 struct loopback_cable
*cable
= dpcm
->cable
;
324 salign
= (snd_pcm_format_width(runtime
->format
) *
325 runtime
->channels
) / 8;
326 bps
= salign
* runtime
->rate
;
327 if (bps
<= 0 || salign
<= 0)
331 dpcm
->pcm_buffer_size
= frames_to_bytes(runtime
, runtime
->buffer_size
);
332 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
333 /* clear capture buffer */
334 dpcm
->silent_size
= dpcm
->pcm_buffer_size
;
335 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
,
336 runtime
->buffer_size
* runtime
->channels
);
340 dpcm
->period_update_pending
= 0;
342 dpcm
->pcm_salign
= salign
;
343 dpcm
->pcm_period_size
= frames_to_bytes(runtime
, runtime
->period_size
);
345 mutex_lock(&dpcm
->loopback
->cable_lock
);
346 if (!(cable
->valid
& ~(1 << substream
->stream
)) ||
347 (get_setup(dpcm
)->notify
&&
348 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
))
349 params_change(substream
);
350 cable
->valid
|= 1 << substream
->stream
;
351 mutex_unlock(&dpcm
->loopback
->cable_lock
);
356 static void clear_capture_buf(struct loopback_pcm
*dpcm
, unsigned int bytes
)
358 struct snd_pcm_runtime
*runtime
= dpcm
->substream
->runtime
;
359 char *dst
= runtime
->dma_area
;
360 unsigned int dst_off
= dpcm
->buf_pos
;
362 if (dpcm
->silent_size
>= dpcm
->pcm_buffer_size
)
364 if (dpcm
->silent_size
+ bytes
> dpcm
->pcm_buffer_size
)
365 bytes
= dpcm
->pcm_buffer_size
- dpcm
->silent_size
;
368 unsigned int size
= bytes
;
369 if (dst_off
+ size
> dpcm
->pcm_buffer_size
)
370 size
= dpcm
->pcm_buffer_size
- dst_off
;
371 snd_pcm_format_set_silence(runtime
->format
, dst
+ dst_off
,
372 bytes_to_frames(runtime
, size
) *
374 dpcm
->silent_size
+= size
;
382 static void copy_play_buf(struct loopback_pcm
*play
,
383 struct loopback_pcm
*capt
,
386 struct snd_pcm_runtime
*runtime
= play
->substream
->runtime
;
387 char *src
= runtime
->dma_area
;
388 char *dst
= capt
->substream
->runtime
->dma_area
;
389 unsigned int src_off
= play
->buf_pos
;
390 unsigned int dst_off
= capt
->buf_pos
;
391 unsigned int clear_bytes
= 0;
393 /* check if playback is draining, trim the capture copy size
394 * when our pointer is at the end of playback ring buffer */
395 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
396 snd_pcm_playback_hw_avail(runtime
) < runtime
->buffer_size
) {
397 snd_pcm_uframes_t appl_ptr
, appl_ptr1
, diff
;
398 appl_ptr
= appl_ptr1
= runtime
->control
->appl_ptr
;
399 appl_ptr1
-= appl_ptr1
% runtime
->buffer_size
;
400 appl_ptr1
+= play
->buf_pos
/ play
->pcm_salign
;
401 if (appl_ptr
< appl_ptr1
)
402 appl_ptr1
-= runtime
->buffer_size
;
403 diff
= (appl_ptr
- appl_ptr1
) * play
->pcm_salign
;
405 clear_bytes
= bytes
- diff
;
411 unsigned int size
= bytes
;
412 if (src_off
+ size
> play
->pcm_buffer_size
)
413 size
= play
->pcm_buffer_size
- src_off
;
414 if (dst_off
+ size
> capt
->pcm_buffer_size
)
415 size
= capt
->pcm_buffer_size
- dst_off
;
416 memcpy(dst
+ dst_off
, src
+ src_off
, size
);
417 capt
->silent_size
= 0;
421 src_off
= (src_off
+ size
) % play
->pcm_buffer_size
;
422 dst_off
= (dst_off
+ size
) % capt
->pcm_buffer_size
;
425 if (clear_bytes
> 0) {
426 clear_capture_buf(capt
, clear_bytes
);
427 capt
->silent_size
= 0;
431 #define BYTEPOS_UPDATE_POSONLY 0
432 #define BYTEPOS_UPDATE_CLEAR 1
433 #define BYTEPOS_UPDATE_COPY 2
435 static void loopback_bytepos_update(struct loopback_pcm
*dpcm
,
440 unsigned long last_pos
;
442 last_pos
= byte_pos(dpcm
, dpcm
->irq_pos
);
443 dpcm
->irq_pos
+= delta
* dpcm
->pcm_bps
;
444 count
= byte_pos(dpcm
, dpcm
->irq_pos
) - last_pos
;
447 if (cmd
== BYTEPOS_UPDATE_CLEAR
)
448 clear_capture_buf(dpcm
, count
);
449 else if (cmd
== BYTEPOS_UPDATE_COPY
)
450 copy_play_buf(dpcm
->cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
],
451 dpcm
->cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
],
453 dpcm
->buf_pos
+= count
;
454 dpcm
->buf_pos
%= dpcm
->pcm_buffer_size
;
455 if (dpcm
->irq_pos
>= dpcm
->period_size_frac
) {
456 dpcm
->irq_pos
%= dpcm
->period_size_frac
;
457 dpcm
->period_update_pending
= 1;
461 static unsigned int loopback_pos_update(struct loopback_cable
*cable
)
463 struct loopback_pcm
*dpcm_play
=
464 cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
465 struct loopback_pcm
*dpcm_capt
=
466 cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
467 unsigned long delta_play
= 0, delta_capt
= 0;
468 unsigned int running
;
470 spin_lock(&cable
->lock
);
471 running
= cable
->running
;
472 if (running
& (1 << SNDRV_PCM_STREAM_PLAYBACK
)) {
473 delta_play
= jiffies
- dpcm_play
->last_jiffies
;
474 dpcm_play
->last_jiffies
+= delta_play
;
477 if (running
& (1 << SNDRV_PCM_STREAM_CAPTURE
)) {
478 delta_capt
= jiffies
- dpcm_capt
->last_jiffies
;
479 dpcm_capt
->last_jiffies
+= delta_capt
;
482 if (delta_play
== 0 && delta_capt
== 0) {
483 spin_unlock(&cable
->lock
);
487 if (delta_play
> delta_capt
) {
488 loopback_bytepos_update(dpcm_play
, delta_play
- delta_capt
,
489 BYTEPOS_UPDATE_POSONLY
);
490 delta_play
= delta_capt
;
491 } else if (delta_play
< delta_capt
) {
492 loopback_bytepos_update(dpcm_capt
, delta_capt
- delta_play
,
493 BYTEPOS_UPDATE_CLEAR
);
494 delta_capt
= delta_play
;
497 if (delta_play
== 0 && delta_capt
== 0) {
498 spin_unlock(&cable
->lock
);
501 /* note delta_capt == delta_play at this moment */
502 loopback_bytepos_update(dpcm_capt
, delta_capt
, BYTEPOS_UPDATE_COPY
);
503 loopback_bytepos_update(dpcm_play
, delta_play
, BYTEPOS_UPDATE_POSONLY
);
504 spin_unlock(&cable
->lock
);
508 static void loopback_timer_function(unsigned long data
)
510 struct loopback_pcm
*dpcm
= (struct loopback_pcm
*)data
;
511 unsigned int running
;
513 running
= loopback_pos_update(dpcm
->cable
);
514 if (running
& (1 << dpcm
->substream
->stream
)) {
515 loopback_timer_start(dpcm
);
516 if (dpcm
->period_update_pending
) {
517 dpcm
->period_update_pending
= 0;
518 snd_pcm_period_elapsed(dpcm
->substream
);
523 static snd_pcm_uframes_t
loopback_pointer(struct snd_pcm_substream
*substream
)
525 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
526 struct loopback_pcm
*dpcm
= runtime
->private_data
;
528 loopback_pos_update(dpcm
->cable
);
529 return bytes_to_frames(runtime
, dpcm
->buf_pos
);
532 static struct snd_pcm_hardware loopback_pcm_hardware
=
534 .info
= (SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_MMAP
|
535 SNDRV_PCM_INFO_MMAP_VALID
),
536 .formats
= (SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
|
537 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
|
538 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
),
539 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_192000
,
544 .buffer_bytes_max
= 2 * 1024 * 1024,
545 .period_bytes_min
= 64,
546 /* note check overflow in frac_pos() using pcm_rate_shift before
547 changing period_bytes_max value */
548 .period_bytes_max
= 1024 * 1024,
554 static void loopback_runtime_free(struct snd_pcm_runtime
*runtime
)
556 struct loopback_pcm
*dpcm
= runtime
->private_data
;
560 static int loopback_hw_params(struct snd_pcm_substream
*substream
,
561 struct snd_pcm_hw_params
*params
)
563 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
566 static int loopback_hw_free(struct snd_pcm_substream
*substream
)
568 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
569 struct loopback_pcm
*dpcm
= runtime
->private_data
;
570 struct loopback_cable
*cable
= dpcm
->cable
;
572 mutex_lock(&dpcm
->loopback
->cable_lock
);
573 cable
->valid
&= ~(1 << substream
->stream
);
574 mutex_unlock(&dpcm
->loopback
->cable_lock
);
575 return snd_pcm_lib_free_pages(substream
);
578 static unsigned int get_cable_index(struct snd_pcm_substream
*substream
)
580 if (!substream
->pcm
->device
)
581 return substream
->stream
;
583 return !substream
->stream
;
586 static int rule_format(struct snd_pcm_hw_params
*params
,
587 struct snd_pcm_hw_rule
*rule
)
590 struct snd_pcm_hardware
*hw
= rule
->private;
591 struct snd_mask
*maskp
= hw_param_mask(params
, rule
->var
);
593 maskp
->bits
[0] &= (u_int32_t
)hw
->formats
;
594 maskp
->bits
[1] &= (u_int32_t
)(hw
->formats
>> 32);
595 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
596 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
601 static int rule_rate(struct snd_pcm_hw_params
*params
,
602 struct snd_pcm_hw_rule
*rule
)
604 struct snd_pcm_hardware
*hw
= rule
->private;
605 struct snd_interval t
;
607 t
.min
= hw
->rate_min
;
608 t
.max
= hw
->rate_max
;
609 t
.openmin
= t
.openmax
= 0;
611 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
614 static int rule_channels(struct snd_pcm_hw_params
*params
,
615 struct snd_pcm_hw_rule
*rule
)
617 struct snd_pcm_hardware
*hw
= rule
->private;
618 struct snd_interval t
;
620 t
.min
= hw
->channels_min
;
621 t
.max
= hw
->channels_max
;
622 t
.openmin
= t
.openmax
= 0;
624 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
627 static int loopback_open(struct snd_pcm_substream
*substream
)
629 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
630 struct loopback
*loopback
= substream
->private_data
;
631 struct loopback_pcm
*dpcm
;
632 struct loopback_cable
*cable
;
634 int dev
= get_cable_index(substream
);
636 mutex_lock(&loopback
->cable_lock
);
637 dpcm
= kzalloc(sizeof(*dpcm
), GFP_KERNEL
);
642 dpcm
->loopback
= loopback
;
643 dpcm
->substream
= substream
;
644 setup_timer(&dpcm
->timer
, loopback_timer_function
,
645 (unsigned long)dpcm
);
647 cable
= loopback
->cables
[substream
->number
][dev
];
649 cable
= kzalloc(sizeof(*cable
), GFP_KERNEL
);
655 spin_lock_init(&cable
->lock
);
656 cable
->hw
= loopback_pcm_hardware
;
657 loopback
->cables
[substream
->number
][dev
] = cable
;
660 cable
->streams
[substream
->stream
] = dpcm
;
662 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
664 /* use dynamic rules based on actual runtime->hw values */
665 /* note that the default rules created in the PCM midlevel code */
666 /* are cached -> they do not reflect the actual state */
667 err
= snd_pcm_hw_rule_add(runtime
, 0,
668 SNDRV_PCM_HW_PARAM_FORMAT
,
669 rule_format
, &runtime
->hw
,
670 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
673 err
= snd_pcm_hw_rule_add(runtime
, 0,
674 SNDRV_PCM_HW_PARAM_RATE
,
675 rule_rate
, &runtime
->hw
,
676 SNDRV_PCM_HW_PARAM_RATE
, -1);
679 err
= snd_pcm_hw_rule_add(runtime
, 0,
680 SNDRV_PCM_HW_PARAM_CHANNELS
,
681 rule_channels
, &runtime
->hw
,
682 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
686 runtime
->private_data
= dpcm
;
687 runtime
->private_free
= loopback_runtime_free
;
688 if (get_notify(dpcm
))
689 runtime
->hw
= loopback_pcm_hardware
;
691 runtime
->hw
= cable
->hw
;
693 mutex_unlock(&loopback
->cable_lock
);
697 static int loopback_close(struct snd_pcm_substream
*substream
)
699 struct loopback
*loopback
= substream
->private_data
;
700 struct loopback_pcm
*dpcm
= substream
->runtime
->private_data
;
701 struct loopback_cable
*cable
;
702 int dev
= get_cable_index(substream
);
704 loopback_timer_stop(dpcm
);
705 mutex_lock(&loopback
->cable_lock
);
706 cable
= loopback
->cables
[substream
->number
][dev
];
707 if (cable
->streams
[!substream
->stream
]) {
708 /* other stream is still alive */
709 cable
->streams
[substream
->stream
] = NULL
;
712 loopback
->cables
[substream
->number
][dev
] = NULL
;
715 mutex_unlock(&loopback
->cable_lock
);
719 static struct snd_pcm_ops loopback_playback_ops
= {
720 .open
= loopback_open
,
721 .close
= loopback_close
,
722 .ioctl
= snd_pcm_lib_ioctl
,
723 .hw_params
= loopback_hw_params
,
724 .hw_free
= loopback_hw_free
,
725 .prepare
= loopback_prepare
,
726 .trigger
= loopback_trigger
,
727 .pointer
= loopback_pointer
,
730 static struct snd_pcm_ops loopback_capture_ops
= {
731 .open
= loopback_open
,
732 .close
= loopback_close
,
733 .ioctl
= snd_pcm_lib_ioctl
,
734 .hw_params
= loopback_hw_params
,
735 .hw_free
= loopback_hw_free
,
736 .prepare
= loopback_prepare
,
737 .trigger
= loopback_trigger
,
738 .pointer
= loopback_pointer
,
741 static int __devinit
loopback_pcm_new(struct loopback
*loopback
,
742 int device
, int substreams
)
747 err
= snd_pcm_new(loopback
->card
, "Loopback PCM", device
,
748 substreams
, substreams
, &pcm
);
751 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &loopback_playback_ops
);
752 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &loopback_capture_ops
);
754 pcm
->private_data
= loopback
;
756 strcpy(pcm
->name
, "Loopback PCM");
758 loopback
->pcm
[device
] = pcm
;
760 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
761 snd_dma_continuous_data(GFP_KERNEL
),
766 static int loopback_rate_shift_info(struct snd_kcontrol
*kcontrol
,
767 struct snd_ctl_elem_info
*uinfo
)
769 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
771 uinfo
->value
.integer
.min
= 80000;
772 uinfo
->value
.integer
.max
= 120000;
773 uinfo
->value
.integer
.step
= 1;
777 static int loopback_rate_shift_get(struct snd_kcontrol
*kcontrol
,
778 struct snd_ctl_elem_value
*ucontrol
)
780 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
782 ucontrol
->value
.integer
.value
[0] =
783 loopback
->setup
[kcontrol
->id
.subdevice
]
784 [kcontrol
->id
.device
].rate_shift
;
788 static int loopback_rate_shift_put(struct snd_kcontrol
*kcontrol
,
789 struct snd_ctl_elem_value
*ucontrol
)
791 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
795 val
= ucontrol
->value
.integer
.value
[0];
800 mutex_lock(&loopback
->cable_lock
);
801 if (val
!= loopback
->setup
[kcontrol
->id
.subdevice
]
802 [kcontrol
->id
.device
].rate_shift
) {
803 loopback
->setup
[kcontrol
->id
.subdevice
]
804 [kcontrol
->id
.device
].rate_shift
= val
;
807 mutex_unlock(&loopback
->cable_lock
);
811 static int loopback_notify_get(struct snd_kcontrol
*kcontrol
,
812 struct snd_ctl_elem_value
*ucontrol
)
814 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
816 ucontrol
->value
.integer
.value
[0] =
817 loopback
->setup
[kcontrol
->id
.subdevice
]
818 [kcontrol
->id
.device
].notify
;
822 static int loopback_notify_put(struct snd_kcontrol
*kcontrol
,
823 struct snd_ctl_elem_value
*ucontrol
)
825 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
829 val
= ucontrol
->value
.integer
.value
[0] ? 1 : 0;
830 if (val
!= loopback
->setup
[kcontrol
->id
.subdevice
]
831 [kcontrol
->id
.device
].notify
) {
832 loopback
->setup
[kcontrol
->id
.subdevice
]
833 [kcontrol
->id
.device
].notify
= val
;
839 static int loopback_active_get(struct snd_kcontrol
*kcontrol
,
840 struct snd_ctl_elem_value
*ucontrol
)
842 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
843 struct loopback_cable
*cable
= loopback
->cables
844 [kcontrol
->id
.subdevice
][kcontrol
->id
.device
^ 1];
845 unsigned int val
= 0;
848 val
= (cable
->running
& (1 << SNDRV_PCM_STREAM_PLAYBACK
)) ?
850 ucontrol
->value
.integer
.value
[0] = val
;
854 static int loopback_format_info(struct snd_kcontrol
*kcontrol
,
855 struct snd_ctl_elem_info
*uinfo
)
857 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
859 uinfo
->value
.integer
.min
= 0;
860 uinfo
->value
.integer
.max
= SNDRV_PCM_FORMAT_LAST
;
861 uinfo
->value
.integer
.step
= 1;
865 static int loopback_format_get(struct snd_kcontrol
*kcontrol
,
866 struct snd_ctl_elem_value
*ucontrol
)
868 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
870 ucontrol
->value
.integer
.value
[0] =
871 loopback
->setup
[kcontrol
->id
.subdevice
]
872 [kcontrol
->id
.device
].format
;
876 static int loopback_rate_info(struct snd_kcontrol
*kcontrol
,
877 struct snd_ctl_elem_info
*uinfo
)
879 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
881 uinfo
->value
.integer
.min
= 0;
882 uinfo
->value
.integer
.max
= 192000;
883 uinfo
->value
.integer
.step
= 1;
887 static int loopback_rate_get(struct snd_kcontrol
*kcontrol
,
888 struct snd_ctl_elem_value
*ucontrol
)
890 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
892 ucontrol
->value
.integer
.value
[0] =
893 loopback
->setup
[kcontrol
->id
.subdevice
]
894 [kcontrol
->id
.device
].rate
;
898 static int loopback_channels_info(struct snd_kcontrol
*kcontrol
,
899 struct snd_ctl_elem_info
*uinfo
)
901 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
903 uinfo
->value
.integer
.min
= 1;
904 uinfo
->value
.integer
.max
= 1024;
905 uinfo
->value
.integer
.step
= 1;
909 static int loopback_channels_get(struct snd_kcontrol
*kcontrol
,
910 struct snd_ctl_elem_value
*ucontrol
)
912 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
914 ucontrol
->value
.integer
.value
[0] =
915 loopback
->setup
[kcontrol
->id
.subdevice
]
916 [kcontrol
->id
.device
].channels
;
920 static struct snd_kcontrol_new loopback_controls
[] __devinitdata
= {
922 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
923 .name
= "PCM Rate Shift 100000",
924 .info
= loopback_rate_shift_info
,
925 .get
= loopback_rate_shift_get
,
926 .put
= loopback_rate_shift_put
,
929 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
930 .name
= "PCM Notify",
931 .info
= snd_ctl_boolean_mono_info
,
932 .get
= loopback_notify_get
,
933 .put
= loopback_notify_put
,
937 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
938 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
939 .name
= "PCM Slave Active",
940 .info
= snd_ctl_boolean_mono_info
,
941 .get
= loopback_active_get
,
945 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
946 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
947 .name
= "PCM Slave Format",
948 .info
= loopback_format_info
,
949 .get
= loopback_format_get
953 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
954 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
955 .name
= "PCM Slave Rate",
956 .info
= loopback_rate_info
,
957 .get
= loopback_rate_get
959 #define CHANNELS_IDX 5
961 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
962 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
963 .name
= "PCM Slave Channels",
964 .info
= loopback_channels_info
,
965 .get
= loopback_channels_get
969 static int __devinit
loopback_mixer_new(struct loopback
*loopback
, int notify
)
971 struct snd_card
*card
= loopback
->card
;
973 struct snd_kcontrol
*kctl
;
974 struct loopback_setup
*setup
;
975 int err
, dev
, substr
, substr_count
, idx
;
977 strcpy(card
->mixername
, "Loopback Mixer");
978 for (dev
= 0; dev
< 2; dev
++) {
979 pcm
= loopback
->pcm
[dev
];
981 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
;
982 for (substr
= 0; substr
< substr_count
; substr
++) {
983 setup
= &loopback
->setup
[substr
][dev
];
984 setup
->notify
= notify
;
985 setup
->rate_shift
= NO_PITCH
;
986 setup
->format
= SNDRV_PCM_FORMAT_S16_LE
;
989 for (idx
= 0; idx
< ARRAY_SIZE(loopback_controls
);
991 kctl
= snd_ctl_new1(&loopback_controls
[idx
],
995 kctl
->id
.device
= dev
;
996 kctl
->id
.subdevice
= substr
;
999 setup
->active_id
= kctl
->id
;
1002 setup
->format_id
= kctl
->id
;
1005 setup
->rate_id
= kctl
->id
;
1008 setup
->channels_id
= kctl
->id
;
1013 err
= snd_ctl_add(card
, kctl
);
1022 #ifdef CONFIG_PROC_FS
1024 static void print_dpcm_info(struct snd_info_buffer
*buffer
,
1025 struct loopback_pcm
*dpcm
,
1028 snd_iprintf(buffer
, " %s\n", id
);
1030 snd_iprintf(buffer
, " inactive\n");
1033 snd_iprintf(buffer
, " buffer_size:\t%u\n", dpcm
->pcm_buffer_size
);
1034 snd_iprintf(buffer
, " buffer_pos:\t\t%u\n", dpcm
->buf_pos
);
1035 snd_iprintf(buffer
, " silent_size:\t%u\n", dpcm
->silent_size
);
1036 snd_iprintf(buffer
, " period_size:\t%u\n", dpcm
->pcm_period_size
);
1037 snd_iprintf(buffer
, " bytes_per_sec:\t%u\n", dpcm
->pcm_bps
);
1038 snd_iprintf(buffer
, " sample_align:\t%u\n", dpcm
->pcm_salign
);
1039 snd_iprintf(buffer
, " rate_shift:\t\t%u\n", dpcm
->pcm_rate_shift
);
1040 snd_iprintf(buffer
, " update_pending:\t%u\n",
1041 dpcm
->period_update_pending
);
1042 snd_iprintf(buffer
, " irq_pos:\t\t%u\n", dpcm
->irq_pos
);
1043 snd_iprintf(buffer
, " period_frac:\t%u\n", dpcm
->period_size_frac
);
1044 snd_iprintf(buffer
, " last_jiffies:\t%lu (%lu)\n",
1045 dpcm
->last_jiffies
, jiffies
);
1046 snd_iprintf(buffer
, " timer_expires:\t%lu\n", dpcm
->timer
.expires
);
1049 static void print_substream_info(struct snd_info_buffer
*buffer
,
1050 struct loopback
*loopback
,
1054 struct loopback_cable
*cable
= loopback
->cables
[sub
][num
];
1056 snd_iprintf(buffer
, "Cable %i substream %i:\n", num
, sub
);
1057 if (cable
== NULL
) {
1058 snd_iprintf(buffer
, " inactive\n");
1061 snd_iprintf(buffer
, " valid: %u\n", cable
->valid
);
1062 snd_iprintf(buffer
, " running: %u\n", cable
->running
);
1063 print_dpcm_info(buffer
, cable
->streams
[0], "Playback");
1064 print_dpcm_info(buffer
, cable
->streams
[1], "Capture");
1067 static void print_cable_info(struct snd_info_entry
*entry
,
1068 struct snd_info_buffer
*buffer
)
1070 struct loopback
*loopback
= entry
->private_data
;
1073 mutex_lock(&loopback
->cable_lock
);
1074 num
= entry
->name
[strlen(entry
->name
)-1];
1075 num
= num
== '0' ? 0 : 1;
1076 for (sub
= 0; sub
< MAX_PCM_SUBSTREAMS
; sub
++)
1077 print_substream_info(buffer
, loopback
, sub
, num
);
1078 mutex_unlock(&loopback
->cable_lock
);
1081 static int __devinit
loopback_proc_new(struct loopback
*loopback
, int cidx
)
1084 struct snd_info_entry
*entry
;
1087 snprintf(name
, sizeof(name
), "cable#%d", cidx
);
1088 err
= snd_card_proc_new(loopback
->card
, name
, &entry
);
1092 snd_info_set_text_ops(entry
, loopback
, print_cable_info
);
1096 #else /* !CONFIG_PROC_FS */
1098 #define loopback_proc_new(loopback, cidx) do { } while (0)
1102 static int __devinit
loopback_probe(struct platform_device
*devptr
)
1104 struct snd_card
*card
;
1105 struct loopback
*loopback
;
1106 int dev
= devptr
->id
;
1109 err
= snd_card_create(index
[dev
], id
[dev
], THIS_MODULE
,
1110 sizeof(struct loopback
), &card
);
1113 loopback
= card
->private_data
;
1115 if (pcm_substreams
[dev
] < 1)
1116 pcm_substreams
[dev
] = 1;
1117 if (pcm_substreams
[dev
] > MAX_PCM_SUBSTREAMS
)
1118 pcm_substreams
[dev
] = MAX_PCM_SUBSTREAMS
;
1120 loopback
->card
= card
;
1121 mutex_init(&loopback
->cable_lock
);
1123 err
= loopback_pcm_new(loopback
, 0, pcm_substreams
[dev
]);
1126 err
= loopback_pcm_new(loopback
, 1, pcm_substreams
[dev
]);
1129 err
= loopback_mixer_new(loopback
, pcm_notify
[dev
] ? 1 : 0);
1132 loopback_proc_new(loopback
, 0);
1133 loopback_proc_new(loopback
, 1);
1134 strcpy(card
->driver
, "Loopback");
1135 strcpy(card
->shortname
, "Loopback");
1136 sprintf(card
->longname
, "Loopback %i", dev
+ 1);
1137 err
= snd_card_register(card
);
1139 platform_set_drvdata(devptr
, card
);
1143 snd_card_free(card
);
1147 static int __devexit
loopback_remove(struct platform_device
*devptr
)
1149 snd_card_free(platform_get_drvdata(devptr
));
1150 platform_set_drvdata(devptr
, NULL
);
1155 static int loopback_suspend(struct platform_device
*pdev
,
1158 struct snd_card
*card
= platform_get_drvdata(pdev
);
1159 struct loopback
*loopback
= card
->private_data
;
1161 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
1163 snd_pcm_suspend_all(loopback
->pcm
[0]);
1164 snd_pcm_suspend_all(loopback
->pcm
[1]);
1168 static int loopback_resume(struct platform_device
*pdev
)
1170 struct snd_card
*card
= platform_get_drvdata(pdev
);
1172 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
1177 #define SND_LOOPBACK_DRIVER "snd_aloop"
1179 static struct platform_driver loopback_driver
= {
1180 .probe
= loopback_probe
,
1181 .remove
= __devexit_p(loopback_remove
),
1183 .suspend
= loopback_suspend
,
1184 .resume
= loopback_resume
,
1187 .name
= SND_LOOPBACK_DRIVER
1191 static void loopback_unregister_all(void)
1195 for (i
= 0; i
< ARRAY_SIZE(devices
); ++i
)
1196 platform_device_unregister(devices
[i
]);
1197 platform_driver_unregister(&loopback_driver
);
1200 static int __init
alsa_card_loopback_init(void)
1204 err
= platform_driver_register(&loopback_driver
);
1210 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1211 struct platform_device
*device
;
1214 device
= platform_device_register_simple(SND_LOOPBACK_DRIVER
,
1218 if (!platform_get_drvdata(device
)) {
1219 platform_device_unregister(device
);
1222 devices
[i
] = device
;
1227 printk(KERN_ERR
"aloop: No loopback enabled\n");
1229 loopback_unregister_all();
1235 static void __exit
alsa_card_loopback_exit(void)
1237 loopback_unregister_all();
1240 module_init(alsa_card_loopback_init
)
1241 module_exit(alsa_card_loopback_exit
)