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/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 bool 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
;
83 struct loopback_setup
{
84 unsigned int notify
: 1;
85 unsigned int rate_shift
;
88 unsigned int channels
;
89 struct snd_ctl_elem_id active_id
;
90 struct snd_ctl_elem_id format_id
;
91 struct snd_ctl_elem_id rate_id
;
92 struct snd_ctl_elem_id channels_id
;
96 struct snd_card
*card
;
97 struct mutex cable_lock
;
98 struct loopback_cable
*cables
[MAX_PCM_SUBSTREAMS
][2];
99 struct snd_pcm
*pcm
[2];
100 struct loopback_setup setup
[MAX_PCM_SUBSTREAMS
][2];
103 struct loopback_pcm
{
104 struct loopback
*loopback
;
105 struct snd_pcm_substream
*substream
;
106 struct loopback_cable
*cable
;
107 unsigned int pcm_buffer_size
;
108 unsigned int buf_pos
; /* position in buffer */
109 unsigned int silent_size
;
111 unsigned int pcm_period_size
;
112 unsigned int pcm_bps
; /* bytes per second */
113 unsigned int pcm_salign
; /* bytes per sample * channels */
114 unsigned int pcm_rate_shift
; /* rate shift value */
116 unsigned int period_update_pending
:1;
118 unsigned int irq_pos
; /* fractional IRQ position */
119 unsigned int period_size_frac
;
120 unsigned int last_drift
;
121 unsigned long last_jiffies
;
122 struct timer_list timer
;
125 static struct platform_device
*devices
[SNDRV_CARDS
];
127 static inline unsigned int byte_pos(struct loopback_pcm
*dpcm
, unsigned int x
)
129 if (dpcm
->pcm_rate_shift
== NO_PITCH
) {
132 x
= div_u64(NO_PITCH
* (unsigned long long)x
,
133 HZ
* (unsigned long long)dpcm
->pcm_rate_shift
);
135 return x
- (x
% dpcm
->pcm_salign
);
138 static inline unsigned int frac_pos(struct loopback_pcm
*dpcm
, unsigned int x
)
140 if (dpcm
->pcm_rate_shift
== NO_PITCH
) { /* no pitch */
143 x
= div_u64(dpcm
->pcm_rate_shift
* (unsigned long long)x
* HZ
,
149 static inline struct loopback_setup
*get_setup(struct loopback_pcm
*dpcm
)
151 int device
= dpcm
->substream
->pstr
->pcm
->device
;
153 if (dpcm
->substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
155 return &dpcm
->loopback
->setup
[dpcm
->substream
->number
][device
];
158 static inline unsigned int get_notify(struct loopback_pcm
*dpcm
)
160 return get_setup(dpcm
)->notify
;
163 static inline unsigned int get_rate_shift(struct loopback_pcm
*dpcm
)
165 return get_setup(dpcm
)->rate_shift
;
168 static void loopback_timer_start(struct loopback_pcm
*dpcm
)
171 unsigned int rate_shift
= get_rate_shift(dpcm
);
173 if (rate_shift
!= dpcm
->pcm_rate_shift
) {
174 dpcm
->pcm_rate_shift
= rate_shift
;
175 dpcm
->period_size_frac
= frac_pos(dpcm
, dpcm
->pcm_period_size
);
177 if (dpcm
->period_size_frac
<= dpcm
->irq_pos
) {
178 dpcm
->irq_pos
%= dpcm
->period_size_frac
;
179 dpcm
->period_update_pending
= 1;
181 tick
= dpcm
->period_size_frac
- dpcm
->irq_pos
;
182 tick
= (tick
+ dpcm
->pcm_bps
- 1) / dpcm
->pcm_bps
;
183 dpcm
->timer
.expires
= jiffies
+ tick
;
184 add_timer(&dpcm
->timer
);
187 static inline void loopback_timer_stop(struct loopback_pcm
*dpcm
)
189 del_timer(&dpcm
->timer
);
190 dpcm
->timer
.expires
= 0;
193 #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
194 #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
195 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
197 static int loopback_check_format(struct loopback_cable
*cable
, int stream
)
199 struct snd_pcm_runtime
*runtime
, *cruntime
;
200 struct loopback_setup
*setup
;
201 struct snd_card
*card
;
204 if (cable
->valid
!= CABLE_VALID_BOTH
) {
205 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
209 runtime
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->
211 cruntime
= cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
]->
213 check
= runtime
->format
!= cruntime
->format
||
214 runtime
->rate
!= cruntime
->rate
||
215 runtime
->channels
!= cruntime
->channels
;
218 if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
221 snd_pcm_stop(cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
]->
222 substream
, SNDRV_PCM_STATE_DRAINING
);
224 runtime
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->
226 setup
= get_setup(cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]);
227 card
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->loopback
->card
;
228 if (setup
->format
!= runtime
->format
) {
229 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
231 setup
->format
= runtime
->format
;
233 if (setup
->rate
!= runtime
->rate
) {
234 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
236 setup
->rate
= runtime
->rate
;
238 if (setup
->channels
!= runtime
->channels
) {
239 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
240 &setup
->channels_id
);
241 setup
->channels
= runtime
->channels
;
247 static void loopback_active_notify(struct loopback_pcm
*dpcm
)
249 snd_ctl_notify(dpcm
->loopback
->card
,
250 SNDRV_CTL_EVENT_MASK_VALUE
,
251 &get_setup(dpcm
)->active_id
);
254 static int loopback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
256 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
257 struct loopback_pcm
*dpcm
= runtime
->private_data
;
258 struct loopback_cable
*cable
= dpcm
->cable
;
259 int err
, stream
= 1 << substream
->stream
;
262 case SNDRV_PCM_TRIGGER_START
:
263 err
= loopback_check_format(cable
, substream
->stream
);
266 dpcm
->last_jiffies
= jiffies
;
267 dpcm
->pcm_rate_shift
= 0;
268 dpcm
->last_drift
= 0;
269 spin_lock(&cable
->lock
);
270 cable
->running
|= stream
;
271 cable
->pause
&= ~stream
;
272 spin_unlock(&cable
->lock
);
273 loopback_timer_start(dpcm
);
274 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
275 loopback_active_notify(dpcm
);
277 case SNDRV_PCM_TRIGGER_STOP
:
278 spin_lock(&cable
->lock
);
279 cable
->running
&= ~stream
;
280 cable
->pause
&= ~stream
;
281 spin_unlock(&cable
->lock
);
282 loopback_timer_stop(dpcm
);
283 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
284 loopback_active_notify(dpcm
);
286 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
287 spin_lock(&cable
->lock
);
288 cable
->pause
|= stream
;
289 spin_unlock(&cable
->lock
);
290 loopback_timer_stop(dpcm
);
292 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
293 spin_lock(&cable
->lock
);
294 dpcm
->last_jiffies
= jiffies
;
295 cable
->pause
&= ~stream
;
296 spin_unlock(&cable
->lock
);
297 loopback_timer_start(dpcm
);
305 static void params_change_substream(struct loopback_pcm
*dpcm
,
306 struct snd_pcm_runtime
*runtime
)
308 struct snd_pcm_runtime
*dst_runtime
;
310 if (dpcm
== NULL
|| dpcm
->substream
== NULL
)
312 dst_runtime
= dpcm
->substream
->runtime
;
313 if (dst_runtime
== NULL
)
315 dst_runtime
->hw
= dpcm
->cable
->hw
;
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
= (1ULL << 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
;
329 params_change_substream(cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
],
331 params_change_substream(cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
],
335 static int loopback_prepare(struct snd_pcm_substream
*substream
)
337 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
338 struct loopback_pcm
*dpcm
= runtime
->private_data
;
339 struct loopback_cable
*cable
= dpcm
->cable
;
342 salign
= (snd_pcm_format_width(runtime
->format
) *
343 runtime
->channels
) / 8;
344 bps
= salign
* runtime
->rate
;
345 if (bps
<= 0 || salign
<= 0)
349 dpcm
->pcm_buffer_size
= frames_to_bytes(runtime
, runtime
->buffer_size
);
350 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
351 /* clear capture buffer */
352 dpcm
->silent_size
= dpcm
->pcm_buffer_size
;
353 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
,
354 runtime
->buffer_size
* runtime
->channels
);
358 dpcm
->period_update_pending
= 0;
360 dpcm
->pcm_salign
= salign
;
361 dpcm
->pcm_period_size
= frames_to_bytes(runtime
, runtime
->period_size
);
363 mutex_lock(&dpcm
->loopback
->cable_lock
);
364 if (!(cable
->valid
& ~(1 << substream
->stream
)) ||
365 (get_setup(dpcm
)->notify
&&
366 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
))
367 params_change(substream
);
368 cable
->valid
|= 1 << substream
->stream
;
369 mutex_unlock(&dpcm
->loopback
->cable_lock
);
374 static void clear_capture_buf(struct loopback_pcm
*dpcm
, unsigned int bytes
)
376 struct snd_pcm_runtime
*runtime
= dpcm
->substream
->runtime
;
377 char *dst
= runtime
->dma_area
;
378 unsigned int dst_off
= dpcm
->buf_pos
;
380 if (dpcm
->silent_size
>= dpcm
->pcm_buffer_size
)
382 if (dpcm
->silent_size
+ bytes
> dpcm
->pcm_buffer_size
)
383 bytes
= dpcm
->pcm_buffer_size
- dpcm
->silent_size
;
386 unsigned int size
= bytes
;
387 if (dst_off
+ size
> dpcm
->pcm_buffer_size
)
388 size
= dpcm
->pcm_buffer_size
- dst_off
;
389 snd_pcm_format_set_silence(runtime
->format
, dst
+ dst_off
,
390 bytes_to_frames(runtime
, size
) *
392 dpcm
->silent_size
+= size
;
400 static void copy_play_buf(struct loopback_pcm
*play
,
401 struct loopback_pcm
*capt
,
404 struct snd_pcm_runtime
*runtime
= play
->substream
->runtime
;
405 char *src
= runtime
->dma_area
;
406 char *dst
= capt
->substream
->runtime
->dma_area
;
407 unsigned int src_off
= play
->buf_pos
;
408 unsigned int dst_off
= capt
->buf_pos
;
409 unsigned int clear_bytes
= 0;
411 /* check if playback is draining, trim the capture copy size
412 * when our pointer is at the end of playback ring buffer */
413 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
414 snd_pcm_playback_hw_avail(runtime
) < runtime
->buffer_size
) {
415 snd_pcm_uframes_t appl_ptr
, appl_ptr1
, diff
;
416 appl_ptr
= appl_ptr1
= runtime
->control
->appl_ptr
;
417 appl_ptr1
-= appl_ptr1
% runtime
->buffer_size
;
418 appl_ptr1
+= play
->buf_pos
/ play
->pcm_salign
;
419 if (appl_ptr
< appl_ptr1
)
420 appl_ptr1
-= runtime
->buffer_size
;
421 diff
= (appl_ptr
- appl_ptr1
) * play
->pcm_salign
;
423 clear_bytes
= bytes
- diff
;
429 unsigned int size
= bytes
;
430 if (src_off
+ size
> play
->pcm_buffer_size
)
431 size
= play
->pcm_buffer_size
- src_off
;
432 if (dst_off
+ size
> capt
->pcm_buffer_size
)
433 size
= capt
->pcm_buffer_size
- dst_off
;
434 memcpy(dst
+ dst_off
, src
+ src_off
, size
);
435 capt
->silent_size
= 0;
439 src_off
= (src_off
+ size
) % play
->pcm_buffer_size
;
440 dst_off
= (dst_off
+ size
) % capt
->pcm_buffer_size
;
443 if (clear_bytes
> 0) {
444 clear_capture_buf(capt
, clear_bytes
);
445 capt
->silent_size
= 0;
449 static inline unsigned int bytepos_delta(struct loopback_pcm
*dpcm
,
450 unsigned int jiffies_delta
)
452 unsigned long last_pos
;
455 last_pos
= byte_pos(dpcm
, dpcm
->irq_pos
);
456 dpcm
->irq_pos
+= jiffies_delta
* dpcm
->pcm_bps
;
457 delta
= byte_pos(dpcm
, dpcm
->irq_pos
) - last_pos
;
458 if (delta
>= dpcm
->last_drift
)
459 delta
-= dpcm
->last_drift
;
460 dpcm
->last_drift
= 0;
461 if (dpcm
->irq_pos
>= dpcm
->period_size_frac
) {
462 dpcm
->irq_pos
%= dpcm
->period_size_frac
;
463 dpcm
->period_update_pending
= 1;
468 static inline void bytepos_finish(struct loopback_pcm
*dpcm
,
471 dpcm
->buf_pos
+= delta
;
472 dpcm
->buf_pos
%= dpcm
->pcm_buffer_size
;
475 static unsigned int loopback_pos_update(struct loopback_cable
*cable
)
477 struct loopback_pcm
*dpcm_play
=
478 cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
479 struct loopback_pcm
*dpcm_capt
=
480 cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
481 unsigned long delta_play
= 0, delta_capt
= 0;
482 unsigned int running
, count1
, count2
;
485 spin_lock_irqsave(&cable
->lock
, flags
);
486 running
= cable
->running
^ cable
->pause
;
487 if (running
& (1 << SNDRV_PCM_STREAM_PLAYBACK
)) {
488 delta_play
= jiffies
- dpcm_play
->last_jiffies
;
489 dpcm_play
->last_jiffies
+= delta_play
;
492 if (running
& (1 << SNDRV_PCM_STREAM_CAPTURE
)) {
493 delta_capt
= jiffies
- dpcm_capt
->last_jiffies
;
494 dpcm_capt
->last_jiffies
+= delta_capt
;
497 if (delta_play
== 0 && delta_capt
== 0)
500 if (delta_play
> delta_capt
) {
501 count1
= bytepos_delta(dpcm_play
, delta_play
- delta_capt
);
502 bytepos_finish(dpcm_play
, count1
);
503 delta_play
= delta_capt
;
504 } else if (delta_play
< delta_capt
) {
505 count1
= bytepos_delta(dpcm_capt
, delta_capt
- delta_play
);
506 clear_capture_buf(dpcm_capt
, count1
);
507 bytepos_finish(dpcm_capt
, count1
);
508 delta_capt
= delta_play
;
511 if (delta_play
== 0 && delta_capt
== 0)
514 /* note delta_capt == delta_play at this moment */
515 count1
= bytepos_delta(dpcm_play
, delta_play
);
516 count2
= bytepos_delta(dpcm_capt
, delta_capt
);
517 if (count1
< count2
) {
518 dpcm_capt
->last_drift
= count2
- count1
;
520 } else if (count1
> count2
) {
521 dpcm_play
->last_drift
= count1
- count2
;
523 copy_play_buf(dpcm_play
, dpcm_capt
, count1
);
524 bytepos_finish(dpcm_play
, count1
);
525 bytepos_finish(dpcm_capt
, count1
);
527 spin_unlock_irqrestore(&cable
->lock
, flags
);
531 static void loopback_timer_function(unsigned long data
)
533 struct loopback_pcm
*dpcm
= (struct loopback_pcm
*)data
;
534 unsigned int running
;
536 running
= loopback_pos_update(dpcm
->cable
);
537 if (running
& (1 << dpcm
->substream
->stream
)) {
538 loopback_timer_start(dpcm
);
539 if (dpcm
->period_update_pending
) {
540 dpcm
->period_update_pending
= 0;
541 snd_pcm_period_elapsed(dpcm
->substream
);
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
;
551 loopback_pos_update(dpcm
->cable
);
552 return bytes_to_frames(runtime
, dpcm
->buf_pos
);
555 static struct snd_pcm_hardware loopback_pcm_hardware
=
557 .info
= (SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_MMAP
|
558 SNDRV_PCM_INFO_MMAP_VALID
| SNDRV_PCM_INFO_PAUSE
),
559 .formats
= (SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
|
560 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
|
561 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
),
562 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_192000
,
567 .buffer_bytes_max
= 2 * 1024 * 1024,
568 .period_bytes_min
= 64,
569 /* note check overflow in frac_pos() using pcm_rate_shift before
570 changing period_bytes_max value */
571 .period_bytes_max
= 1024 * 1024,
577 static void loopback_runtime_free(struct snd_pcm_runtime
*runtime
)
579 struct loopback_pcm
*dpcm
= runtime
->private_data
;
583 static int loopback_hw_params(struct snd_pcm_substream
*substream
,
584 struct snd_pcm_hw_params
*params
)
586 return snd_pcm_lib_alloc_vmalloc_buffer(substream
,
587 params_buffer_bytes(params
));
590 static int loopback_hw_free(struct snd_pcm_substream
*substream
)
592 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
593 struct loopback_pcm
*dpcm
= runtime
->private_data
;
594 struct loopback_cable
*cable
= dpcm
->cable
;
596 mutex_lock(&dpcm
->loopback
->cable_lock
);
597 cable
->valid
&= ~(1 << substream
->stream
);
598 mutex_unlock(&dpcm
->loopback
->cable_lock
);
599 return snd_pcm_lib_free_vmalloc_buffer(substream
);
602 static unsigned int get_cable_index(struct snd_pcm_substream
*substream
)
604 if (!substream
->pcm
->device
)
605 return substream
->stream
;
607 return !substream
->stream
;
610 static int rule_format(struct snd_pcm_hw_params
*params
,
611 struct snd_pcm_hw_rule
*rule
)
614 struct snd_pcm_hardware
*hw
= rule
->private;
615 struct snd_mask
*maskp
= hw_param_mask(params
, rule
->var
);
617 maskp
->bits
[0] &= (u_int32_t
)hw
->formats
;
618 maskp
->bits
[1] &= (u_int32_t
)(hw
->formats
>> 32);
619 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
620 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
625 static int rule_rate(struct snd_pcm_hw_params
*params
,
626 struct snd_pcm_hw_rule
*rule
)
628 struct snd_pcm_hardware
*hw
= rule
->private;
629 struct snd_interval t
;
631 t
.min
= hw
->rate_min
;
632 t
.max
= hw
->rate_max
;
633 t
.openmin
= t
.openmax
= 0;
635 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
638 static int rule_channels(struct snd_pcm_hw_params
*params
,
639 struct snd_pcm_hw_rule
*rule
)
641 struct snd_pcm_hardware
*hw
= rule
->private;
642 struct snd_interval t
;
644 t
.min
= hw
->channels_min
;
645 t
.max
= hw
->channels_max
;
646 t
.openmin
= t
.openmax
= 0;
648 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
651 static int loopback_open(struct snd_pcm_substream
*substream
)
653 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
654 struct loopback
*loopback
= substream
->private_data
;
655 struct loopback_pcm
*dpcm
;
656 struct loopback_cable
*cable
;
658 int dev
= get_cable_index(substream
);
660 mutex_lock(&loopback
->cable_lock
);
661 dpcm
= kzalloc(sizeof(*dpcm
), GFP_KERNEL
);
666 dpcm
->loopback
= loopback
;
667 dpcm
->substream
= substream
;
668 setup_timer(&dpcm
->timer
, loopback_timer_function
,
669 (unsigned long)dpcm
);
671 cable
= loopback
->cables
[substream
->number
][dev
];
673 cable
= kzalloc(sizeof(*cable
), GFP_KERNEL
);
679 spin_lock_init(&cable
->lock
);
680 cable
->hw
= loopback_pcm_hardware
;
681 loopback
->cables
[substream
->number
][dev
] = cable
;
684 cable
->streams
[substream
->stream
] = dpcm
;
686 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
688 /* use dynamic rules based on actual runtime->hw values */
689 /* note that the default rules created in the PCM midlevel code */
690 /* are cached -> they do not reflect the actual state */
691 err
= snd_pcm_hw_rule_add(runtime
, 0,
692 SNDRV_PCM_HW_PARAM_FORMAT
,
693 rule_format
, &runtime
->hw
,
694 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
697 err
= snd_pcm_hw_rule_add(runtime
, 0,
698 SNDRV_PCM_HW_PARAM_RATE
,
699 rule_rate
, &runtime
->hw
,
700 SNDRV_PCM_HW_PARAM_RATE
, -1);
703 err
= snd_pcm_hw_rule_add(runtime
, 0,
704 SNDRV_PCM_HW_PARAM_CHANNELS
,
705 rule_channels
, &runtime
->hw
,
706 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
710 runtime
->private_data
= dpcm
;
711 runtime
->private_free
= loopback_runtime_free
;
712 if (get_notify(dpcm
))
713 runtime
->hw
= loopback_pcm_hardware
;
715 runtime
->hw
= cable
->hw
;
717 mutex_unlock(&loopback
->cable_lock
);
721 static int loopback_close(struct snd_pcm_substream
*substream
)
723 struct loopback
*loopback
= substream
->private_data
;
724 struct loopback_pcm
*dpcm
= substream
->runtime
->private_data
;
725 struct loopback_cable
*cable
;
726 int dev
= get_cable_index(substream
);
728 loopback_timer_stop(dpcm
);
729 mutex_lock(&loopback
->cable_lock
);
730 cable
= loopback
->cables
[substream
->number
][dev
];
731 if (cable
->streams
[!substream
->stream
]) {
732 /* other stream is still alive */
733 cable
->streams
[substream
->stream
] = NULL
;
736 loopback
->cables
[substream
->number
][dev
] = NULL
;
739 mutex_unlock(&loopback
->cable_lock
);
743 static struct snd_pcm_ops loopback_playback_ops
= {
744 .open
= loopback_open
,
745 .close
= loopback_close
,
746 .ioctl
= snd_pcm_lib_ioctl
,
747 .hw_params
= loopback_hw_params
,
748 .hw_free
= loopback_hw_free
,
749 .prepare
= loopback_prepare
,
750 .trigger
= loopback_trigger
,
751 .pointer
= loopback_pointer
,
752 .page
= snd_pcm_lib_get_vmalloc_page
,
753 .mmap
= snd_pcm_lib_mmap_vmalloc
,
756 static struct snd_pcm_ops loopback_capture_ops
= {
757 .open
= loopback_open
,
758 .close
= loopback_close
,
759 .ioctl
= snd_pcm_lib_ioctl
,
760 .hw_params
= loopback_hw_params
,
761 .hw_free
= loopback_hw_free
,
762 .prepare
= loopback_prepare
,
763 .trigger
= loopback_trigger
,
764 .pointer
= loopback_pointer
,
765 .page
= snd_pcm_lib_get_vmalloc_page
,
766 .mmap
= snd_pcm_lib_mmap_vmalloc
,
769 static int __devinit
loopback_pcm_new(struct loopback
*loopback
,
770 int device
, int substreams
)
775 err
= snd_pcm_new(loopback
->card
, "Loopback PCM", device
,
776 substreams
, substreams
, &pcm
);
779 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &loopback_playback_ops
);
780 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &loopback_capture_ops
);
782 pcm
->private_data
= loopback
;
784 strcpy(pcm
->name
, "Loopback PCM");
786 loopback
->pcm
[device
] = pcm
;
790 static int loopback_rate_shift_info(struct snd_kcontrol
*kcontrol
,
791 struct snd_ctl_elem_info
*uinfo
)
793 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
795 uinfo
->value
.integer
.min
= 80000;
796 uinfo
->value
.integer
.max
= 120000;
797 uinfo
->value
.integer
.step
= 1;
801 static int loopback_rate_shift_get(struct snd_kcontrol
*kcontrol
,
802 struct snd_ctl_elem_value
*ucontrol
)
804 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
806 ucontrol
->value
.integer
.value
[0] =
807 loopback
->setup
[kcontrol
->id
.subdevice
]
808 [kcontrol
->id
.device
].rate_shift
;
812 static int loopback_rate_shift_put(struct snd_kcontrol
*kcontrol
,
813 struct snd_ctl_elem_value
*ucontrol
)
815 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
819 val
= ucontrol
->value
.integer
.value
[0];
824 mutex_lock(&loopback
->cable_lock
);
825 if (val
!= loopback
->setup
[kcontrol
->id
.subdevice
]
826 [kcontrol
->id
.device
].rate_shift
) {
827 loopback
->setup
[kcontrol
->id
.subdevice
]
828 [kcontrol
->id
.device
].rate_shift
= val
;
831 mutex_unlock(&loopback
->cable_lock
);
835 static int loopback_notify_get(struct snd_kcontrol
*kcontrol
,
836 struct snd_ctl_elem_value
*ucontrol
)
838 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
840 ucontrol
->value
.integer
.value
[0] =
841 loopback
->setup
[kcontrol
->id
.subdevice
]
842 [kcontrol
->id
.device
].notify
;
846 static int loopback_notify_put(struct snd_kcontrol
*kcontrol
,
847 struct snd_ctl_elem_value
*ucontrol
)
849 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
853 val
= ucontrol
->value
.integer
.value
[0] ? 1 : 0;
854 if (val
!= loopback
->setup
[kcontrol
->id
.subdevice
]
855 [kcontrol
->id
.device
].notify
) {
856 loopback
->setup
[kcontrol
->id
.subdevice
]
857 [kcontrol
->id
.device
].notify
= val
;
863 static int loopback_active_get(struct snd_kcontrol
*kcontrol
,
864 struct snd_ctl_elem_value
*ucontrol
)
866 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
867 struct loopback_cable
*cable
= loopback
->cables
868 [kcontrol
->id
.subdevice
][kcontrol
->id
.device
^ 1];
869 unsigned int val
= 0;
872 val
= (cable
->running
& (1 << SNDRV_PCM_STREAM_PLAYBACK
)) ?
874 ucontrol
->value
.integer
.value
[0] = val
;
878 static int loopback_format_info(struct snd_kcontrol
*kcontrol
,
879 struct snd_ctl_elem_info
*uinfo
)
881 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
883 uinfo
->value
.integer
.min
= 0;
884 uinfo
->value
.integer
.max
= SNDRV_PCM_FORMAT_LAST
;
885 uinfo
->value
.integer
.step
= 1;
889 static int loopback_format_get(struct snd_kcontrol
*kcontrol
,
890 struct snd_ctl_elem_value
*ucontrol
)
892 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
894 ucontrol
->value
.integer
.value
[0] =
895 loopback
->setup
[kcontrol
->id
.subdevice
]
896 [kcontrol
->id
.device
].format
;
900 static int loopback_rate_info(struct snd_kcontrol
*kcontrol
,
901 struct snd_ctl_elem_info
*uinfo
)
903 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
905 uinfo
->value
.integer
.min
= 0;
906 uinfo
->value
.integer
.max
= 192000;
907 uinfo
->value
.integer
.step
= 1;
911 static int loopback_rate_get(struct snd_kcontrol
*kcontrol
,
912 struct snd_ctl_elem_value
*ucontrol
)
914 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
916 ucontrol
->value
.integer
.value
[0] =
917 loopback
->setup
[kcontrol
->id
.subdevice
]
918 [kcontrol
->id
.device
].rate
;
922 static int loopback_channels_info(struct snd_kcontrol
*kcontrol
,
923 struct snd_ctl_elem_info
*uinfo
)
925 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
927 uinfo
->value
.integer
.min
= 1;
928 uinfo
->value
.integer
.max
= 1024;
929 uinfo
->value
.integer
.step
= 1;
933 static int loopback_channels_get(struct snd_kcontrol
*kcontrol
,
934 struct snd_ctl_elem_value
*ucontrol
)
936 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
938 ucontrol
->value
.integer
.value
[0] =
939 loopback
->setup
[kcontrol
->id
.subdevice
]
940 [kcontrol
->id
.device
].channels
;
944 static struct snd_kcontrol_new loopback_controls
[] __devinitdata
= {
946 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
947 .name
= "PCM Rate Shift 100000",
948 .info
= loopback_rate_shift_info
,
949 .get
= loopback_rate_shift_get
,
950 .put
= loopback_rate_shift_put
,
953 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
954 .name
= "PCM Notify",
955 .info
= snd_ctl_boolean_mono_info
,
956 .get
= loopback_notify_get
,
957 .put
= loopback_notify_put
,
961 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
962 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
963 .name
= "PCM Slave Active",
964 .info
= snd_ctl_boolean_mono_info
,
965 .get
= loopback_active_get
,
969 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
970 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
971 .name
= "PCM Slave Format",
972 .info
= loopback_format_info
,
973 .get
= loopback_format_get
977 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
978 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
979 .name
= "PCM Slave Rate",
980 .info
= loopback_rate_info
,
981 .get
= loopback_rate_get
983 #define CHANNELS_IDX 5
985 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
986 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
987 .name
= "PCM Slave Channels",
988 .info
= loopback_channels_info
,
989 .get
= loopback_channels_get
993 static int __devinit
loopback_mixer_new(struct loopback
*loopback
, int notify
)
995 struct snd_card
*card
= loopback
->card
;
997 struct snd_kcontrol
*kctl
;
998 struct loopback_setup
*setup
;
999 int err
, dev
, substr
, substr_count
, idx
;
1001 strcpy(card
->mixername
, "Loopback Mixer");
1002 for (dev
= 0; dev
< 2; dev
++) {
1003 pcm
= loopback
->pcm
[dev
];
1005 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
;
1006 for (substr
= 0; substr
< substr_count
; substr
++) {
1007 setup
= &loopback
->setup
[substr
][dev
];
1008 setup
->notify
= notify
;
1009 setup
->rate_shift
= NO_PITCH
;
1010 setup
->format
= SNDRV_PCM_FORMAT_S16_LE
;
1011 setup
->rate
= 48000;
1012 setup
->channels
= 2;
1013 for (idx
= 0; idx
< ARRAY_SIZE(loopback_controls
);
1015 kctl
= snd_ctl_new1(&loopback_controls
[idx
],
1019 kctl
->id
.device
= dev
;
1020 kctl
->id
.subdevice
= substr
;
1023 setup
->active_id
= kctl
->id
;
1026 setup
->format_id
= kctl
->id
;
1029 setup
->rate_id
= kctl
->id
;
1032 setup
->channels_id
= kctl
->id
;
1037 err
= snd_ctl_add(card
, kctl
);
1046 #ifdef CONFIG_PROC_FS
1048 static void print_dpcm_info(struct snd_info_buffer
*buffer
,
1049 struct loopback_pcm
*dpcm
,
1052 snd_iprintf(buffer
, " %s\n", id
);
1054 snd_iprintf(buffer
, " inactive\n");
1057 snd_iprintf(buffer
, " buffer_size:\t%u\n", dpcm
->pcm_buffer_size
);
1058 snd_iprintf(buffer
, " buffer_pos:\t\t%u\n", dpcm
->buf_pos
);
1059 snd_iprintf(buffer
, " silent_size:\t%u\n", dpcm
->silent_size
);
1060 snd_iprintf(buffer
, " period_size:\t%u\n", dpcm
->pcm_period_size
);
1061 snd_iprintf(buffer
, " bytes_per_sec:\t%u\n", dpcm
->pcm_bps
);
1062 snd_iprintf(buffer
, " sample_align:\t%u\n", dpcm
->pcm_salign
);
1063 snd_iprintf(buffer
, " rate_shift:\t\t%u\n", dpcm
->pcm_rate_shift
);
1064 snd_iprintf(buffer
, " update_pending:\t%u\n",
1065 dpcm
->period_update_pending
);
1066 snd_iprintf(buffer
, " irq_pos:\t\t%u\n", dpcm
->irq_pos
);
1067 snd_iprintf(buffer
, " period_frac:\t%u\n", dpcm
->period_size_frac
);
1068 snd_iprintf(buffer
, " last_jiffies:\t%lu (%lu)\n",
1069 dpcm
->last_jiffies
, jiffies
);
1070 snd_iprintf(buffer
, " timer_expires:\t%lu\n", dpcm
->timer
.expires
);
1073 static void print_substream_info(struct snd_info_buffer
*buffer
,
1074 struct loopback
*loopback
,
1078 struct loopback_cable
*cable
= loopback
->cables
[sub
][num
];
1080 snd_iprintf(buffer
, "Cable %i substream %i:\n", num
, sub
);
1081 if (cable
== NULL
) {
1082 snd_iprintf(buffer
, " inactive\n");
1085 snd_iprintf(buffer
, " valid: %u\n", cable
->valid
);
1086 snd_iprintf(buffer
, " running: %u\n", cable
->running
);
1087 snd_iprintf(buffer
, " pause: %u\n", cable
->pause
);
1088 print_dpcm_info(buffer
, cable
->streams
[0], "Playback");
1089 print_dpcm_info(buffer
, cable
->streams
[1], "Capture");
1092 static void print_cable_info(struct snd_info_entry
*entry
,
1093 struct snd_info_buffer
*buffer
)
1095 struct loopback
*loopback
= entry
->private_data
;
1098 mutex_lock(&loopback
->cable_lock
);
1099 num
= entry
->name
[strlen(entry
->name
)-1];
1100 num
= num
== '0' ? 0 : 1;
1101 for (sub
= 0; sub
< MAX_PCM_SUBSTREAMS
; sub
++)
1102 print_substream_info(buffer
, loopback
, sub
, num
);
1103 mutex_unlock(&loopback
->cable_lock
);
1106 static int __devinit
loopback_proc_new(struct loopback
*loopback
, int cidx
)
1109 struct snd_info_entry
*entry
;
1112 snprintf(name
, sizeof(name
), "cable#%d", cidx
);
1113 err
= snd_card_proc_new(loopback
->card
, name
, &entry
);
1117 snd_info_set_text_ops(entry
, loopback
, print_cable_info
);
1121 #else /* !CONFIG_PROC_FS */
1123 #define loopback_proc_new(loopback, cidx) do { } while (0)
1127 static int __devinit
loopback_probe(struct platform_device
*devptr
)
1129 struct snd_card
*card
;
1130 struct loopback
*loopback
;
1131 int dev
= devptr
->id
;
1134 err
= snd_card_create(index
[dev
], id
[dev
], THIS_MODULE
,
1135 sizeof(struct loopback
), &card
);
1138 loopback
= card
->private_data
;
1140 if (pcm_substreams
[dev
] < 1)
1141 pcm_substreams
[dev
] = 1;
1142 if (pcm_substreams
[dev
] > MAX_PCM_SUBSTREAMS
)
1143 pcm_substreams
[dev
] = MAX_PCM_SUBSTREAMS
;
1145 loopback
->card
= card
;
1146 mutex_init(&loopback
->cable_lock
);
1148 err
= loopback_pcm_new(loopback
, 0, pcm_substreams
[dev
]);
1151 err
= loopback_pcm_new(loopback
, 1, pcm_substreams
[dev
]);
1154 err
= loopback_mixer_new(loopback
, pcm_notify
[dev
] ? 1 : 0);
1157 loopback_proc_new(loopback
, 0);
1158 loopback_proc_new(loopback
, 1);
1159 strcpy(card
->driver
, "Loopback");
1160 strcpy(card
->shortname
, "Loopback");
1161 sprintf(card
->longname
, "Loopback %i", dev
+ 1);
1162 err
= snd_card_register(card
);
1164 platform_set_drvdata(devptr
, card
);
1168 snd_card_free(card
);
1172 static int __devexit
loopback_remove(struct platform_device
*devptr
)
1174 snd_card_free(platform_get_drvdata(devptr
));
1175 platform_set_drvdata(devptr
, NULL
);
1180 static int loopback_suspend(struct device
*pdev
)
1182 struct snd_card
*card
= dev_get_drvdata(pdev
);
1183 struct loopback
*loopback
= card
->private_data
;
1185 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
1187 snd_pcm_suspend_all(loopback
->pcm
[0]);
1188 snd_pcm_suspend_all(loopback
->pcm
[1]);
1192 static int loopback_resume(struct device
*pdev
)
1194 struct snd_card
*card
= dev_get_drvdata(pdev
);
1196 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
1200 static SIMPLE_DEV_PM_OPS(loopback_pm
, loopback_suspend
, loopback_resume
);
1201 #define LOOPBACK_PM_OPS &loopback_pm
1203 #define LOOPBACK_PM_OPS NULL
1206 #define SND_LOOPBACK_DRIVER "snd_aloop"
1208 static struct platform_driver loopback_driver
= {
1209 .probe
= loopback_probe
,
1210 .remove
= __devexit_p(loopback_remove
),
1212 .name
= SND_LOOPBACK_DRIVER
,
1213 .owner
= THIS_MODULE
,
1214 .pm
= LOOPBACK_PM_OPS
,
1218 static void loopback_unregister_all(void)
1222 for (i
= 0; i
< ARRAY_SIZE(devices
); ++i
)
1223 platform_device_unregister(devices
[i
]);
1224 platform_driver_unregister(&loopback_driver
);
1227 static int __init
alsa_card_loopback_init(void)
1231 err
= platform_driver_register(&loopback_driver
);
1237 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1238 struct platform_device
*device
;
1241 device
= platform_device_register_simple(SND_LOOPBACK_DRIVER
,
1245 if (!platform_get_drvdata(device
)) {
1246 platform_device_unregister(device
);
1249 devices
[i
] = device
;
1254 printk(KERN_ERR
"aloop: No loopback enabled\n");
1256 loopback_unregister_all();
1262 static void __exit
alsa_card_loopback_exit(void)
1264 loopback_unregister_all();
1267 module_init(alsa_card_loopback_init
)
1268 module_exit(alsa_card_loopback_exit
)