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 /* call in cable->lock */
169 static void loopback_timer_start(struct loopback_pcm
*dpcm
)
172 unsigned int rate_shift
= get_rate_shift(dpcm
);
174 if (rate_shift
!= dpcm
->pcm_rate_shift
) {
175 dpcm
->pcm_rate_shift
= rate_shift
;
176 dpcm
->period_size_frac
= frac_pos(dpcm
, dpcm
->pcm_period_size
);
178 if (dpcm
->period_size_frac
<= dpcm
->irq_pos
) {
179 dpcm
->irq_pos
%= dpcm
->period_size_frac
;
180 dpcm
->period_update_pending
= 1;
182 tick
= dpcm
->period_size_frac
- dpcm
->irq_pos
;
183 tick
= (tick
+ dpcm
->pcm_bps
- 1) / dpcm
->pcm_bps
;
184 dpcm
->timer
.expires
= jiffies
+ tick
;
185 add_timer(&dpcm
->timer
);
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 #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
196 #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
197 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
199 static int loopback_check_format(struct loopback_cable
*cable
, int stream
)
201 struct snd_pcm_runtime
*runtime
, *cruntime
;
202 struct loopback_setup
*setup
;
203 struct snd_card
*card
;
206 if (cable
->valid
!= CABLE_VALID_BOTH
) {
207 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
211 runtime
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->
213 cruntime
= cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
]->
215 check
= runtime
->format
!= cruntime
->format
||
216 runtime
->rate
!= cruntime
->rate
||
217 runtime
->channels
!= cruntime
->channels
;
220 if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
223 snd_pcm_stop(cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
]->
224 substream
, SNDRV_PCM_STATE_DRAINING
);
226 runtime
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->
228 setup
= get_setup(cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]);
229 card
= cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]->loopback
->card
;
230 if (setup
->format
!= runtime
->format
) {
231 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
233 setup
->format
= runtime
->format
;
235 if (setup
->rate
!= runtime
->rate
) {
236 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
238 setup
->rate
= runtime
->rate
;
240 if (setup
->channels
!= runtime
->channels
) {
241 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
,
242 &setup
->channels_id
);
243 setup
->channels
= runtime
->channels
;
249 static void loopback_active_notify(struct loopback_pcm
*dpcm
)
251 snd_ctl_notify(dpcm
->loopback
->card
,
252 SNDRV_CTL_EVENT_MASK_VALUE
,
253 &get_setup(dpcm
)->active_id
);
256 static int loopback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
258 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
259 struct loopback_pcm
*dpcm
= runtime
->private_data
;
260 struct loopback_cable
*cable
= dpcm
->cable
;
261 int err
, stream
= 1 << substream
->stream
;
264 case SNDRV_PCM_TRIGGER_START
:
265 err
= loopback_check_format(cable
, substream
->stream
);
268 dpcm
->last_jiffies
= jiffies
;
269 dpcm
->pcm_rate_shift
= 0;
270 dpcm
->last_drift
= 0;
271 spin_lock(&cable
->lock
);
272 cable
->running
|= stream
;
273 cable
->pause
&= ~stream
;
274 loopback_timer_start(dpcm
);
275 spin_unlock(&cable
->lock
);
276 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
277 loopback_active_notify(dpcm
);
279 case SNDRV_PCM_TRIGGER_STOP
:
280 spin_lock(&cable
->lock
);
281 cable
->running
&= ~stream
;
282 cable
->pause
&= ~stream
;
283 loopback_timer_stop(dpcm
);
284 spin_unlock(&cable
->lock
);
285 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
286 loopback_active_notify(dpcm
);
288 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
289 spin_lock(&cable
->lock
);
290 cable
->pause
|= stream
;
291 loopback_timer_stop(dpcm
);
292 spin_unlock(&cable
->lock
);
294 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
295 spin_lock(&cable
->lock
);
296 dpcm
->last_jiffies
= jiffies
;
297 cable
->pause
&= ~stream
;
298 loopback_timer_start(dpcm
);
299 spin_unlock(&cable
->lock
);
307 static void params_change_substream(struct loopback_pcm
*dpcm
,
308 struct snd_pcm_runtime
*runtime
)
310 struct snd_pcm_runtime
*dst_runtime
;
312 if (dpcm
== NULL
|| dpcm
->substream
== NULL
)
314 dst_runtime
= dpcm
->substream
->runtime
;
315 if (dst_runtime
== NULL
)
317 dst_runtime
->hw
= dpcm
->cable
->hw
;
320 static void params_change(struct snd_pcm_substream
*substream
)
322 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
323 struct loopback_pcm
*dpcm
= runtime
->private_data
;
324 struct loopback_cable
*cable
= dpcm
->cable
;
326 cable
->hw
.formats
= (1ULL << runtime
->format
);
327 cable
->hw
.rate_min
= runtime
->rate
;
328 cable
->hw
.rate_max
= runtime
->rate
;
329 cable
->hw
.channels_min
= runtime
->channels
;
330 cable
->hw
.channels_max
= runtime
->channels
;
331 params_change_substream(cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
],
333 params_change_substream(cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
],
337 static int loopback_prepare(struct snd_pcm_substream
*substream
)
339 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
340 struct loopback_pcm
*dpcm
= runtime
->private_data
;
341 struct loopback_cable
*cable
= dpcm
->cable
;
344 salign
= (snd_pcm_format_width(runtime
->format
) *
345 runtime
->channels
) / 8;
346 bps
= salign
* runtime
->rate
;
347 if (bps
<= 0 || salign
<= 0)
351 dpcm
->pcm_buffer_size
= frames_to_bytes(runtime
, runtime
->buffer_size
);
352 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
353 /* clear capture buffer */
354 dpcm
->silent_size
= dpcm
->pcm_buffer_size
;
355 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
,
356 runtime
->buffer_size
* runtime
->channels
);
360 dpcm
->period_update_pending
= 0;
362 dpcm
->pcm_salign
= salign
;
363 dpcm
->pcm_period_size
= frames_to_bytes(runtime
, runtime
->period_size
);
365 mutex_lock(&dpcm
->loopback
->cable_lock
);
366 if (!(cable
->valid
& ~(1 << substream
->stream
)) ||
367 (get_setup(dpcm
)->notify
&&
368 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
))
369 params_change(substream
);
370 cable
->valid
|= 1 << substream
->stream
;
371 mutex_unlock(&dpcm
->loopback
->cable_lock
);
376 static void clear_capture_buf(struct loopback_pcm
*dpcm
, unsigned int bytes
)
378 struct snd_pcm_runtime
*runtime
= dpcm
->substream
->runtime
;
379 char *dst
= runtime
->dma_area
;
380 unsigned int dst_off
= dpcm
->buf_pos
;
382 if (dpcm
->silent_size
>= dpcm
->pcm_buffer_size
)
384 if (dpcm
->silent_size
+ bytes
> dpcm
->pcm_buffer_size
)
385 bytes
= dpcm
->pcm_buffer_size
- dpcm
->silent_size
;
388 unsigned int size
= bytes
;
389 if (dst_off
+ size
> dpcm
->pcm_buffer_size
)
390 size
= dpcm
->pcm_buffer_size
- dst_off
;
391 snd_pcm_format_set_silence(runtime
->format
, dst
+ dst_off
,
392 bytes_to_frames(runtime
, size
) *
394 dpcm
->silent_size
+= size
;
402 static void copy_play_buf(struct loopback_pcm
*play
,
403 struct loopback_pcm
*capt
,
406 struct snd_pcm_runtime
*runtime
= play
->substream
->runtime
;
407 char *src
= runtime
->dma_area
;
408 char *dst
= capt
->substream
->runtime
->dma_area
;
409 unsigned int src_off
= play
->buf_pos
;
410 unsigned int dst_off
= capt
->buf_pos
;
411 unsigned int clear_bytes
= 0;
413 /* check if playback is draining, trim the capture copy size
414 * when our pointer is at the end of playback ring buffer */
415 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
416 snd_pcm_playback_hw_avail(runtime
) < runtime
->buffer_size
) {
417 snd_pcm_uframes_t appl_ptr
, appl_ptr1
, diff
;
418 appl_ptr
= appl_ptr1
= runtime
->control
->appl_ptr
;
419 appl_ptr1
-= appl_ptr1
% runtime
->buffer_size
;
420 appl_ptr1
+= play
->buf_pos
/ play
->pcm_salign
;
421 if (appl_ptr
< appl_ptr1
)
422 appl_ptr1
-= runtime
->buffer_size
;
423 diff
= (appl_ptr
- appl_ptr1
) * play
->pcm_salign
;
425 clear_bytes
= bytes
- diff
;
431 unsigned int size
= bytes
;
432 if (src_off
+ size
> play
->pcm_buffer_size
)
433 size
= play
->pcm_buffer_size
- src_off
;
434 if (dst_off
+ size
> capt
->pcm_buffer_size
)
435 size
= capt
->pcm_buffer_size
- dst_off
;
436 memcpy(dst
+ dst_off
, src
+ src_off
, size
);
437 capt
->silent_size
= 0;
441 src_off
= (src_off
+ size
) % play
->pcm_buffer_size
;
442 dst_off
= (dst_off
+ size
) % capt
->pcm_buffer_size
;
445 if (clear_bytes
> 0) {
446 clear_capture_buf(capt
, clear_bytes
);
447 capt
->silent_size
= 0;
451 static inline unsigned int bytepos_delta(struct loopback_pcm
*dpcm
,
452 unsigned int jiffies_delta
)
454 unsigned long last_pos
;
457 last_pos
= byte_pos(dpcm
, dpcm
->irq_pos
);
458 dpcm
->irq_pos
+= jiffies_delta
* dpcm
->pcm_bps
;
459 delta
= byte_pos(dpcm
, dpcm
->irq_pos
) - last_pos
;
460 if (delta
>= dpcm
->last_drift
)
461 delta
-= dpcm
->last_drift
;
462 dpcm
->last_drift
= 0;
463 if (dpcm
->irq_pos
>= dpcm
->period_size_frac
) {
464 dpcm
->irq_pos
%= dpcm
->period_size_frac
;
465 dpcm
->period_update_pending
= 1;
470 static inline void bytepos_finish(struct loopback_pcm
*dpcm
,
473 dpcm
->buf_pos
+= delta
;
474 dpcm
->buf_pos
%= dpcm
->pcm_buffer_size
;
477 /* call in cable->lock */
478 static unsigned int loopback_pos_update(struct loopback_cable
*cable
)
480 struct loopback_pcm
*dpcm_play
=
481 cable
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
482 struct loopback_pcm
*dpcm_capt
=
483 cable
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
484 unsigned long delta_play
= 0, delta_capt
= 0;
485 unsigned int running
, count1
, count2
;
487 running
= cable
->running
^ cable
->pause
;
488 if (running
& (1 << SNDRV_PCM_STREAM_PLAYBACK
)) {
489 delta_play
= jiffies
- dpcm_play
->last_jiffies
;
490 dpcm_play
->last_jiffies
+= delta_play
;
493 if (running
& (1 << SNDRV_PCM_STREAM_CAPTURE
)) {
494 delta_capt
= jiffies
- dpcm_capt
->last_jiffies
;
495 dpcm_capt
->last_jiffies
+= delta_capt
;
498 if (delta_play
== 0 && delta_capt
== 0)
501 if (delta_play
> delta_capt
) {
502 count1
= bytepos_delta(dpcm_play
, delta_play
- delta_capt
);
503 bytepos_finish(dpcm_play
, count1
);
504 delta_play
= delta_capt
;
505 } else if (delta_play
< delta_capt
) {
506 count1
= bytepos_delta(dpcm_capt
, delta_capt
- delta_play
);
507 clear_capture_buf(dpcm_capt
, count1
);
508 bytepos_finish(dpcm_capt
, count1
);
509 delta_capt
= delta_play
;
512 if (delta_play
== 0 && delta_capt
== 0)
515 /* note delta_capt == delta_play at this moment */
516 count1
= bytepos_delta(dpcm_play
, delta_play
);
517 count2
= bytepos_delta(dpcm_capt
, delta_capt
);
518 if (count1
< count2
) {
519 dpcm_capt
->last_drift
= count2
- count1
;
521 } else if (count1
> count2
) {
522 dpcm_play
->last_drift
= count1
- count2
;
524 copy_play_buf(dpcm_play
, dpcm_capt
, count1
);
525 bytepos_finish(dpcm_play
, count1
);
526 bytepos_finish(dpcm_capt
, count1
);
531 static void loopback_timer_function(unsigned long data
)
533 struct loopback_pcm
*dpcm
= (struct loopback_pcm
*)data
;
536 spin_lock_irqsave(&dpcm
->cable
->lock
, flags
);
537 if (loopback_pos_update(dpcm
->cable
) & (1 << dpcm
->substream
->stream
)) {
538 loopback_timer_start(dpcm
);
539 if (dpcm
->period_update_pending
) {
540 dpcm
->period_update_pending
= 0;
541 spin_unlock_irqrestore(&dpcm
->cable
->lock
, flags
);
542 /* need to unlock before calling below */
543 snd_pcm_period_elapsed(dpcm
->substream
);
547 spin_unlock_irqrestore(&dpcm
->cable
->lock
, flags
);
550 static snd_pcm_uframes_t
loopback_pointer(struct snd_pcm_substream
*substream
)
552 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
553 struct loopback_pcm
*dpcm
= runtime
->private_data
;
554 snd_pcm_uframes_t pos
;
556 spin_lock(&dpcm
->cable
->lock
);
557 loopback_pos_update(dpcm
->cable
);
559 spin_unlock(&dpcm
->cable
->lock
);
560 return bytes_to_frames(runtime
, pos
);
563 static struct snd_pcm_hardware loopback_pcm_hardware
=
565 .info
= (SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_MMAP
|
566 SNDRV_PCM_INFO_MMAP_VALID
| SNDRV_PCM_INFO_PAUSE
),
567 .formats
= (SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
|
568 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
|
569 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
),
570 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_192000
,
575 .buffer_bytes_max
= 2 * 1024 * 1024,
576 .period_bytes_min
= 64,
577 /* note check overflow in frac_pos() using pcm_rate_shift before
578 changing period_bytes_max value */
579 .period_bytes_max
= 1024 * 1024,
585 static void loopback_runtime_free(struct snd_pcm_runtime
*runtime
)
587 struct loopback_pcm
*dpcm
= runtime
->private_data
;
591 static int loopback_hw_params(struct snd_pcm_substream
*substream
,
592 struct snd_pcm_hw_params
*params
)
594 return snd_pcm_lib_alloc_vmalloc_buffer(substream
,
595 params_buffer_bytes(params
));
598 static int loopback_hw_free(struct snd_pcm_substream
*substream
)
600 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
601 struct loopback_pcm
*dpcm
= runtime
->private_data
;
602 struct loopback_cable
*cable
= dpcm
->cable
;
604 mutex_lock(&dpcm
->loopback
->cable_lock
);
605 cable
->valid
&= ~(1 << substream
->stream
);
606 mutex_unlock(&dpcm
->loopback
->cable_lock
);
607 return snd_pcm_lib_free_vmalloc_buffer(substream
);
610 static unsigned int get_cable_index(struct snd_pcm_substream
*substream
)
612 if (!substream
->pcm
->device
)
613 return substream
->stream
;
615 return !substream
->stream
;
618 static int rule_format(struct snd_pcm_hw_params
*params
,
619 struct snd_pcm_hw_rule
*rule
)
622 struct snd_pcm_hardware
*hw
= rule
->private;
623 struct snd_mask
*maskp
= hw_param_mask(params
, rule
->var
);
625 maskp
->bits
[0] &= (u_int32_t
)hw
->formats
;
626 maskp
->bits
[1] &= (u_int32_t
)(hw
->formats
>> 32);
627 memset(maskp
->bits
+ 2, 0, (SNDRV_MASK_MAX
-64) / 8); /* clear rest */
628 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
633 static int rule_rate(struct snd_pcm_hw_params
*params
,
634 struct snd_pcm_hw_rule
*rule
)
636 struct snd_pcm_hardware
*hw
= rule
->private;
637 struct snd_interval t
;
639 t
.min
= hw
->rate_min
;
640 t
.max
= hw
->rate_max
;
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 snd_pcm_hardware
*hw
= rule
->private;
650 struct snd_interval t
;
652 t
.min
= hw
->channels_min
;
653 t
.max
= hw
->channels_max
;
654 t
.openmin
= t
.openmax
= 0;
656 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
659 static int loopback_open(struct snd_pcm_substream
*substream
)
661 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
662 struct loopback
*loopback
= substream
->private_data
;
663 struct loopback_pcm
*dpcm
;
664 struct loopback_cable
*cable
;
666 int dev
= get_cable_index(substream
);
668 mutex_lock(&loopback
->cable_lock
);
669 dpcm
= kzalloc(sizeof(*dpcm
), GFP_KERNEL
);
674 dpcm
->loopback
= loopback
;
675 dpcm
->substream
= substream
;
676 setup_timer(&dpcm
->timer
, loopback_timer_function
,
677 (unsigned long)dpcm
);
679 cable
= loopback
->cables
[substream
->number
][dev
];
681 cable
= kzalloc(sizeof(*cable
), GFP_KERNEL
);
687 spin_lock_init(&cable
->lock
);
688 cable
->hw
= loopback_pcm_hardware
;
689 loopback
->cables
[substream
->number
][dev
] = cable
;
692 cable
->streams
[substream
->stream
] = dpcm
;
694 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
696 /* use dynamic rules based on actual runtime->hw values */
697 /* note that the default rules created in the PCM midlevel code */
698 /* are cached -> they do not reflect the actual state */
699 err
= snd_pcm_hw_rule_add(runtime
, 0,
700 SNDRV_PCM_HW_PARAM_FORMAT
,
701 rule_format
, &runtime
->hw
,
702 SNDRV_PCM_HW_PARAM_FORMAT
, -1);
705 err
= snd_pcm_hw_rule_add(runtime
, 0,
706 SNDRV_PCM_HW_PARAM_RATE
,
707 rule_rate
, &runtime
->hw
,
708 SNDRV_PCM_HW_PARAM_RATE
, -1);
711 err
= snd_pcm_hw_rule_add(runtime
, 0,
712 SNDRV_PCM_HW_PARAM_CHANNELS
,
713 rule_channels
, &runtime
->hw
,
714 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
718 runtime
->private_data
= dpcm
;
719 runtime
->private_free
= loopback_runtime_free
;
720 if (get_notify(dpcm
))
721 runtime
->hw
= loopback_pcm_hardware
;
723 runtime
->hw
= cable
->hw
;
725 mutex_unlock(&loopback
->cable_lock
);
729 static int loopback_close(struct snd_pcm_substream
*substream
)
731 struct loopback
*loopback
= substream
->private_data
;
732 struct loopback_pcm
*dpcm
= substream
->runtime
->private_data
;
733 struct loopback_cable
*cable
;
734 int dev
= get_cable_index(substream
);
736 loopback_timer_stop(dpcm
);
737 mutex_lock(&loopback
->cable_lock
);
738 cable
= loopback
->cables
[substream
->number
][dev
];
739 if (cable
->streams
[!substream
->stream
]) {
740 /* other stream is still alive */
741 cable
->streams
[substream
->stream
] = NULL
;
744 loopback
->cables
[substream
->number
][dev
] = NULL
;
747 mutex_unlock(&loopback
->cable_lock
);
751 static struct snd_pcm_ops loopback_playback_ops
= {
752 .open
= loopback_open
,
753 .close
= loopback_close
,
754 .ioctl
= snd_pcm_lib_ioctl
,
755 .hw_params
= loopback_hw_params
,
756 .hw_free
= loopback_hw_free
,
757 .prepare
= loopback_prepare
,
758 .trigger
= loopback_trigger
,
759 .pointer
= loopback_pointer
,
760 .page
= snd_pcm_lib_get_vmalloc_page
,
761 .mmap
= snd_pcm_lib_mmap_vmalloc
,
764 static struct snd_pcm_ops loopback_capture_ops
= {
765 .open
= loopback_open
,
766 .close
= loopback_close
,
767 .ioctl
= snd_pcm_lib_ioctl
,
768 .hw_params
= loopback_hw_params
,
769 .hw_free
= loopback_hw_free
,
770 .prepare
= loopback_prepare
,
771 .trigger
= loopback_trigger
,
772 .pointer
= loopback_pointer
,
773 .page
= snd_pcm_lib_get_vmalloc_page
,
774 .mmap
= snd_pcm_lib_mmap_vmalloc
,
777 static int loopback_pcm_new(struct loopback
*loopback
,
778 int device
, int substreams
)
783 err
= snd_pcm_new(loopback
->card
, "Loopback PCM", device
,
784 substreams
, substreams
, &pcm
);
787 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &loopback_playback_ops
);
788 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &loopback_capture_ops
);
790 pcm
->private_data
= loopback
;
792 strcpy(pcm
->name
, "Loopback PCM");
794 loopback
->pcm
[device
] = pcm
;
798 static int loopback_rate_shift_info(struct snd_kcontrol
*kcontrol
,
799 struct snd_ctl_elem_info
*uinfo
)
801 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
803 uinfo
->value
.integer
.min
= 80000;
804 uinfo
->value
.integer
.max
= 120000;
805 uinfo
->value
.integer
.step
= 1;
809 static int loopback_rate_shift_get(struct snd_kcontrol
*kcontrol
,
810 struct snd_ctl_elem_value
*ucontrol
)
812 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
814 ucontrol
->value
.integer
.value
[0] =
815 loopback
->setup
[kcontrol
->id
.subdevice
]
816 [kcontrol
->id
.device
].rate_shift
;
820 static int loopback_rate_shift_put(struct snd_kcontrol
*kcontrol
,
821 struct snd_ctl_elem_value
*ucontrol
)
823 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
827 val
= ucontrol
->value
.integer
.value
[0];
832 mutex_lock(&loopback
->cable_lock
);
833 if (val
!= loopback
->setup
[kcontrol
->id
.subdevice
]
834 [kcontrol
->id
.device
].rate_shift
) {
835 loopback
->setup
[kcontrol
->id
.subdevice
]
836 [kcontrol
->id
.device
].rate_shift
= val
;
839 mutex_unlock(&loopback
->cable_lock
);
843 static int loopback_notify_get(struct snd_kcontrol
*kcontrol
,
844 struct snd_ctl_elem_value
*ucontrol
)
846 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
848 ucontrol
->value
.integer
.value
[0] =
849 loopback
->setup
[kcontrol
->id
.subdevice
]
850 [kcontrol
->id
.device
].notify
;
854 static int loopback_notify_put(struct snd_kcontrol
*kcontrol
,
855 struct snd_ctl_elem_value
*ucontrol
)
857 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
861 val
= ucontrol
->value
.integer
.value
[0] ? 1 : 0;
862 if (val
!= loopback
->setup
[kcontrol
->id
.subdevice
]
863 [kcontrol
->id
.device
].notify
) {
864 loopback
->setup
[kcontrol
->id
.subdevice
]
865 [kcontrol
->id
.device
].notify
= val
;
871 static int loopback_active_get(struct snd_kcontrol
*kcontrol
,
872 struct snd_ctl_elem_value
*ucontrol
)
874 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
875 struct loopback_cable
*cable
= loopback
->cables
876 [kcontrol
->id
.subdevice
][kcontrol
->id
.device
^ 1];
877 unsigned int val
= 0;
880 val
= (cable
->running
& (1 << SNDRV_PCM_STREAM_PLAYBACK
)) ?
882 ucontrol
->value
.integer
.value
[0] = val
;
886 static int loopback_format_info(struct snd_kcontrol
*kcontrol
,
887 struct snd_ctl_elem_info
*uinfo
)
889 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
891 uinfo
->value
.integer
.min
= 0;
892 uinfo
->value
.integer
.max
= SNDRV_PCM_FORMAT_LAST
;
893 uinfo
->value
.integer
.step
= 1;
897 static int loopback_format_get(struct snd_kcontrol
*kcontrol
,
898 struct snd_ctl_elem_value
*ucontrol
)
900 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
902 ucontrol
->value
.integer
.value
[0] =
903 loopback
->setup
[kcontrol
->id
.subdevice
]
904 [kcontrol
->id
.device
].format
;
908 static int loopback_rate_info(struct snd_kcontrol
*kcontrol
,
909 struct snd_ctl_elem_info
*uinfo
)
911 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
913 uinfo
->value
.integer
.min
= 0;
914 uinfo
->value
.integer
.max
= 192000;
915 uinfo
->value
.integer
.step
= 1;
919 static int loopback_rate_get(struct snd_kcontrol
*kcontrol
,
920 struct snd_ctl_elem_value
*ucontrol
)
922 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
924 ucontrol
->value
.integer
.value
[0] =
925 loopback
->setup
[kcontrol
->id
.subdevice
]
926 [kcontrol
->id
.device
].rate
;
930 static int loopback_channels_info(struct snd_kcontrol
*kcontrol
,
931 struct snd_ctl_elem_info
*uinfo
)
933 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
935 uinfo
->value
.integer
.min
= 1;
936 uinfo
->value
.integer
.max
= 1024;
937 uinfo
->value
.integer
.step
= 1;
941 static int loopback_channels_get(struct snd_kcontrol
*kcontrol
,
942 struct snd_ctl_elem_value
*ucontrol
)
944 struct loopback
*loopback
= snd_kcontrol_chip(kcontrol
);
946 ucontrol
->value
.integer
.value
[0] =
947 loopback
->setup
[kcontrol
->id
.subdevice
]
948 [kcontrol
->id
.device
].channels
;
952 static struct snd_kcontrol_new loopback_controls
[] = {
954 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
955 .name
= "PCM Rate Shift 100000",
956 .info
= loopback_rate_shift_info
,
957 .get
= loopback_rate_shift_get
,
958 .put
= loopback_rate_shift_put
,
961 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
962 .name
= "PCM Notify",
963 .info
= snd_ctl_boolean_mono_info
,
964 .get
= loopback_notify_get
,
965 .put
= loopback_notify_put
,
969 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
970 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
971 .name
= "PCM Slave Active",
972 .info
= snd_ctl_boolean_mono_info
,
973 .get
= loopback_active_get
,
977 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
978 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
979 .name
= "PCM Slave Format",
980 .info
= loopback_format_info
,
981 .get
= loopback_format_get
985 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
986 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
987 .name
= "PCM Slave Rate",
988 .info
= loopback_rate_info
,
989 .get
= loopback_rate_get
991 #define CHANNELS_IDX 5
993 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
994 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
995 .name
= "PCM Slave Channels",
996 .info
= loopback_channels_info
,
997 .get
= loopback_channels_get
1001 static int loopback_mixer_new(struct loopback
*loopback
, int notify
)
1003 struct snd_card
*card
= loopback
->card
;
1004 struct snd_pcm
*pcm
;
1005 struct snd_kcontrol
*kctl
;
1006 struct loopback_setup
*setup
;
1007 int err
, dev
, substr
, substr_count
, idx
;
1009 strcpy(card
->mixername
, "Loopback Mixer");
1010 for (dev
= 0; dev
< 2; dev
++) {
1011 pcm
= loopback
->pcm
[dev
];
1013 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
;
1014 for (substr
= 0; substr
< substr_count
; substr
++) {
1015 setup
= &loopback
->setup
[substr
][dev
];
1016 setup
->notify
= notify
;
1017 setup
->rate_shift
= NO_PITCH
;
1018 setup
->format
= SNDRV_PCM_FORMAT_S16_LE
;
1019 setup
->rate
= 48000;
1020 setup
->channels
= 2;
1021 for (idx
= 0; idx
< ARRAY_SIZE(loopback_controls
);
1023 kctl
= snd_ctl_new1(&loopback_controls
[idx
],
1027 kctl
->id
.device
= dev
;
1028 kctl
->id
.subdevice
= substr
;
1031 setup
->active_id
= kctl
->id
;
1034 setup
->format_id
= kctl
->id
;
1037 setup
->rate_id
= kctl
->id
;
1040 setup
->channels_id
= kctl
->id
;
1045 err
= snd_ctl_add(card
, kctl
);
1054 #ifdef CONFIG_PROC_FS
1056 static void print_dpcm_info(struct snd_info_buffer
*buffer
,
1057 struct loopback_pcm
*dpcm
,
1060 snd_iprintf(buffer
, " %s\n", id
);
1062 snd_iprintf(buffer
, " inactive\n");
1065 snd_iprintf(buffer
, " buffer_size:\t%u\n", dpcm
->pcm_buffer_size
);
1066 snd_iprintf(buffer
, " buffer_pos:\t\t%u\n", dpcm
->buf_pos
);
1067 snd_iprintf(buffer
, " silent_size:\t%u\n", dpcm
->silent_size
);
1068 snd_iprintf(buffer
, " period_size:\t%u\n", dpcm
->pcm_period_size
);
1069 snd_iprintf(buffer
, " bytes_per_sec:\t%u\n", dpcm
->pcm_bps
);
1070 snd_iprintf(buffer
, " sample_align:\t%u\n", dpcm
->pcm_salign
);
1071 snd_iprintf(buffer
, " rate_shift:\t\t%u\n", dpcm
->pcm_rate_shift
);
1072 snd_iprintf(buffer
, " update_pending:\t%u\n",
1073 dpcm
->period_update_pending
);
1074 snd_iprintf(buffer
, " irq_pos:\t\t%u\n", dpcm
->irq_pos
);
1075 snd_iprintf(buffer
, " period_frac:\t%u\n", dpcm
->period_size_frac
);
1076 snd_iprintf(buffer
, " last_jiffies:\t%lu (%lu)\n",
1077 dpcm
->last_jiffies
, jiffies
);
1078 snd_iprintf(buffer
, " timer_expires:\t%lu\n", dpcm
->timer
.expires
);
1081 static void print_substream_info(struct snd_info_buffer
*buffer
,
1082 struct loopback
*loopback
,
1086 struct loopback_cable
*cable
= loopback
->cables
[sub
][num
];
1088 snd_iprintf(buffer
, "Cable %i substream %i:\n", num
, sub
);
1089 if (cable
== NULL
) {
1090 snd_iprintf(buffer
, " inactive\n");
1093 snd_iprintf(buffer
, " valid: %u\n", cable
->valid
);
1094 snd_iprintf(buffer
, " running: %u\n", cable
->running
);
1095 snd_iprintf(buffer
, " pause: %u\n", cable
->pause
);
1096 print_dpcm_info(buffer
, cable
->streams
[0], "Playback");
1097 print_dpcm_info(buffer
, cable
->streams
[1], "Capture");
1100 static void print_cable_info(struct snd_info_entry
*entry
,
1101 struct snd_info_buffer
*buffer
)
1103 struct loopback
*loopback
= entry
->private_data
;
1106 mutex_lock(&loopback
->cable_lock
);
1107 num
= entry
->name
[strlen(entry
->name
)-1];
1108 num
= num
== '0' ? 0 : 1;
1109 for (sub
= 0; sub
< MAX_PCM_SUBSTREAMS
; sub
++)
1110 print_substream_info(buffer
, loopback
, sub
, num
);
1111 mutex_unlock(&loopback
->cable_lock
);
1114 static int loopback_proc_new(struct loopback
*loopback
, int cidx
)
1117 struct snd_info_entry
*entry
;
1120 snprintf(name
, sizeof(name
), "cable#%d", cidx
);
1121 err
= snd_card_proc_new(loopback
->card
, name
, &entry
);
1125 snd_info_set_text_ops(entry
, loopback
, print_cable_info
);
1129 #else /* !CONFIG_PROC_FS */
1131 #define loopback_proc_new(loopback, cidx) do { } while (0)
1135 static int loopback_probe(struct platform_device
*devptr
)
1137 struct snd_card
*card
;
1138 struct loopback
*loopback
;
1139 int dev
= devptr
->id
;
1142 err
= snd_card_create(index
[dev
], id
[dev
], THIS_MODULE
,
1143 sizeof(struct loopback
), &card
);
1146 loopback
= card
->private_data
;
1148 if (pcm_substreams
[dev
] < 1)
1149 pcm_substreams
[dev
] = 1;
1150 if (pcm_substreams
[dev
] > MAX_PCM_SUBSTREAMS
)
1151 pcm_substreams
[dev
] = MAX_PCM_SUBSTREAMS
;
1153 loopback
->card
= card
;
1154 mutex_init(&loopback
->cable_lock
);
1156 err
= loopback_pcm_new(loopback
, 0, pcm_substreams
[dev
]);
1159 err
= loopback_pcm_new(loopback
, 1, pcm_substreams
[dev
]);
1162 err
= loopback_mixer_new(loopback
, pcm_notify
[dev
] ? 1 : 0);
1165 loopback_proc_new(loopback
, 0);
1166 loopback_proc_new(loopback
, 1);
1167 strcpy(card
->driver
, "Loopback");
1168 strcpy(card
->shortname
, "Loopback");
1169 sprintf(card
->longname
, "Loopback %i", dev
+ 1);
1170 err
= snd_card_register(card
);
1172 platform_set_drvdata(devptr
, card
);
1176 snd_card_free(card
);
1180 static int loopback_remove(struct platform_device
*devptr
)
1182 snd_card_free(platform_get_drvdata(devptr
));
1183 platform_set_drvdata(devptr
, NULL
);
1187 #ifdef CONFIG_PM_SLEEP
1188 static int loopback_suspend(struct device
*pdev
)
1190 struct snd_card
*card
= dev_get_drvdata(pdev
);
1191 struct loopback
*loopback
= card
->private_data
;
1193 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
1195 snd_pcm_suspend_all(loopback
->pcm
[0]);
1196 snd_pcm_suspend_all(loopback
->pcm
[1]);
1200 static int loopback_resume(struct device
*pdev
)
1202 struct snd_card
*card
= dev_get_drvdata(pdev
);
1204 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
1208 static SIMPLE_DEV_PM_OPS(loopback_pm
, loopback_suspend
, loopback_resume
);
1209 #define LOOPBACK_PM_OPS &loopback_pm
1211 #define LOOPBACK_PM_OPS NULL
1214 #define SND_LOOPBACK_DRIVER "snd_aloop"
1216 static struct platform_driver loopback_driver
= {
1217 .probe
= loopback_probe
,
1218 .remove
= loopback_remove
,
1220 .name
= SND_LOOPBACK_DRIVER
,
1221 .owner
= THIS_MODULE
,
1222 .pm
= LOOPBACK_PM_OPS
,
1226 static void loopback_unregister_all(void)
1230 for (i
= 0; i
< ARRAY_SIZE(devices
); ++i
)
1231 platform_device_unregister(devices
[i
]);
1232 platform_driver_unregister(&loopback_driver
);
1235 static int __init
alsa_card_loopback_init(void)
1239 err
= platform_driver_register(&loopback_driver
);
1245 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
1246 struct platform_device
*device
;
1249 device
= platform_device_register_simple(SND_LOOPBACK_DRIVER
,
1253 if (!platform_get_drvdata(device
)) {
1254 platform_device_unregister(device
);
1257 devices
[i
] = device
;
1262 printk(KERN_ERR
"aloop: No loopback enabled\n");
1264 loopback_unregister_all();
1270 static void __exit
alsa_card_loopback_exit(void)
1272 loopback_unregister_all();
1275 module_init(alsa_card_loopback_init
)
1276 module_exit(alsa_card_loopback_exit
)