1 | /*
|
---|
2 | * Loopback soundcard
|
---|
3 | *
|
---|
4 | * Original code:
|
---|
5 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
|
---|
6 | *
|
---|
7 | * More accurate positioning and full-duplex support:
|
---|
8 | * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
|
---|
9 | *
|
---|
10 | * Major (almost complete) rewrite:
|
---|
11 | * Copyright (c) by Takashi Iwai <tiwai@suse.de>
|
---|
12 | *
|
---|
13 | * A next major update in 2010 (separate timers for playback and capture):
|
---|
14 | * Copyright (c) Jaroslav Kysela <perex@perex.cz>
|
---|
15 | *
|
---|
16 | * This program is free software; you can redistribute it and/or modify
|
---|
17 | * it under the terms of the GNU General Public License as published by
|
---|
18 | * the Free Software Foundation; either version 2 of the License, or
|
---|
19 | * (at your option) any later version.
|
---|
20 | *
|
---|
21 | * This program is distributed in the hope that it will be useful,
|
---|
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
24 | * GNU General Public License for more details.
|
---|
25 | *
|
---|
26 | * You should have received a copy of the GNU General Public License
|
---|
27 | * along with this program; if not, write to the Free Software
|
---|
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
29 | *
|
---|
30 | */
|
---|
31 |
|
---|
32 | #include <linux/init.h>
|
---|
33 | #include <linux/jiffies.h>
|
---|
34 | #include <linux/slab.h>
|
---|
35 | #include <linux/time.h>
|
---|
36 | #include <linux/wait.h>
|
---|
37 | #include <linux/module.h>
|
---|
38 | #include <linux/platform_device.h>
|
---|
39 | #include <sound/core.h>
|
---|
40 | #include <sound/control.h>
|
---|
41 | #include <sound/pcm.h>
|
---|
42 | #include <sound/info.h>
|
---|
43 | #include <sound/initval.h>
|
---|
44 |
|
---|
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}}");
|
---|
49 |
|
---|
50 | #define MAX_PCM_SUBSTREAMS 8
|
---|
51 |
|
---|
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];
|
---|
57 |
|
---|
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.");
|
---|
68 |
|
---|
69 | #define NO_PITCH 100000
|
---|
70 |
|
---|
71 | struct loopback_pcm;
|
---|
72 |
|
---|
73 | struct loopback_cable {
|
---|
74 | spinlock_t lock;
|
---|
75 | struct loopback_pcm *streams[2];
|
---|
76 | struct snd_pcm_hardware hw;
|
---|
77 | /* flags */
|
---|
78 | unsigned int valid;
|
---|
79 | unsigned int running;
|
---|
80 | unsigned int pause;
|
---|
81 | };
|
---|
82 |
|
---|
83 | struct loopback_setup {
|
---|
84 | unsigned int notify: 1;
|
---|
85 | unsigned int rate_shift;
|
---|
86 | unsigned int format;
|
---|
87 | unsigned int rate;
|
---|
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;
|
---|
93 | };
|
---|
94 |
|
---|
95 | struct loopback {
|
---|
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];
|
---|
101 | };
|
---|
102 |
|
---|
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;
|
---|
110 | /* PCM parameters */
|
---|
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 */
|
---|
115 | /* flags */
|
---|
116 | unsigned int period_update_pending :1;
|
---|
117 | /* timer stuff */
|
---|
118 | unsigned int irq_pos; /* fractional IRQ position */
|
---|
119 | unsigned int period_size_frac;
|
---|
120 | unsigned long last_jiffies;
|
---|
121 | struct timer_list timer;
|
---|
122 | };
|
---|
123 |
|
---|
124 | static struct platform_device *devices[SNDRV_CARDS];
|
---|
125 |
|
---|
126 | static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
|
---|
127 | {
|
---|
128 | if (dpcm->pcm_rate_shift == NO_PITCH) {
|
---|
129 | x /= HZ;
|
---|
130 | } else {
|
---|
131 | x = div_u64(NO_PITCH * (unsigned long long)x,
|
---|
132 | HZ * (unsigned long long)dpcm->pcm_rate_shift);
|
---|
133 | }
|
---|
134 | return x - (x % dpcm->pcm_salign);
|
---|
135 | }
|
---|
136 |
|
---|
137 | static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
|
---|
138 | {
|
---|
139 | if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
|
---|
140 | return x * HZ;
|
---|
141 | } else {
|
---|
142 | x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
|
---|
143 | NO_PITCH);
|
---|
144 | }
|
---|
145 | return x;
|
---|
146 | }
|
---|
147 |
|
---|
148 | static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
|
---|
149 | {
|
---|
150 | int device = dpcm->substream->pstr->pcm->device;
|
---|
151 |
|
---|
152 | if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
|
---|
153 | device ^= 1;
|
---|
154 | return &dpcm->loopback->setup[dpcm->substream->number][device];
|
---|
155 | }
|
---|
156 |
|
---|
157 | static inline unsigned int get_notify(struct loopback_pcm *dpcm)
|
---|
158 | {
|
---|
159 | return get_setup(dpcm)->notify;
|
---|
160 | }
|
---|
161 |
|
---|
162 | static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
|
---|
163 | {
|
---|
164 | return get_setup(dpcm)->rate_shift;
|
---|
165 | }
|
---|
166 |
|
---|
167 | static void loopback_timer_start(struct loopback_pcm *dpcm)
|
---|
168 | {
|
---|
169 | unsigned long tick;
|
---|
170 | unsigned int rate_shift = get_rate_shift(dpcm);
|
---|
171 |
|
---|
172 | if (rate_shift != dpcm->pcm_rate_shift) {
|
---|
173 | dpcm->pcm_rate_shift = rate_shift;
|
---|
174 | dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
|
---|
175 | }
|
---|
176 | if (dpcm->period_size_frac <= dpcm->irq_pos) {
|
---|
177 | dpcm->irq_pos %= dpcm->period_size_frac;
|
---|
178 | dpcm->period_update_pending = 1;
|
---|
179 | }
|
---|
180 | tick = dpcm->period_size_frac - dpcm->irq_pos;
|
---|
181 | tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
|
---|
182 | dpcm->timer.expires = jiffies + tick;
|
---|
183 | add_timer(&dpcm->timer);
|
---|
184 | }
|
---|
185 |
|
---|
186 | static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
|
---|
187 | {
|
---|
188 | del_timer(&dpcm->timer);
|
---|
189 | dpcm->timer.expires = 0;
|
---|
190 | }
|
---|
191 |
|
---|
192 | #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
|
---|
193 | #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
|
---|
194 | #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
|
---|
195 |
|
---|
196 | static int loopback_check_format(struct loopback_cable *cable, int stream)
|
---|
197 | {
|
---|
198 | struct snd_pcm_runtime *runtime, *cruntime;
|
---|
199 | struct loopback_setup *setup;
|
---|
200 | struct snd_card *card;
|
---|
201 | int check;
|
---|
202 |
|
---|
203 | if (cable->valid != CABLE_VALID_BOTH) {
|
---|
204 | if (stream == SNDRV_PCM_STREAM_PLAYBACK)
|
---|
205 | goto __notify;
|
---|
206 | return 0;
|
---|
207 | }
|
---|
208 | runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
|
---|
209 | substream->runtime;
|
---|
210 | cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
|
---|
211 | substream->runtime;
|
---|
212 | check = runtime->format != cruntime->format ||
|
---|
213 | runtime->rate != cruntime->rate ||
|
---|
214 | runtime->channels != cruntime->channels;
|
---|
215 | if (!check)
|
---|
216 | return 0;
|
---|
217 | if (stream == SNDRV_PCM_STREAM_CAPTURE) {
|
---|
218 | return -EIO;
|
---|
219 | } else {
|
---|
220 | snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
|
---|
221 | substream, SNDRV_PCM_STATE_DRAINING);
|
---|
222 | __notify:
|
---|
223 | runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
|
---|
224 | substream->runtime;
|
---|
225 | setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
|
---|
226 | card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
|
---|
227 | if (setup->format != runtime->format) {
|
---|
228 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
|
---|
229 | &setup->format_id);
|
---|
230 | setup->format = runtime->format;
|
---|
231 | }
|
---|
232 | if (setup->rate != runtime->rate) {
|
---|
233 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
|
---|
234 | &setup->rate_id);
|
---|
235 | setup->rate = runtime->rate;
|
---|
236 | }
|
---|
237 | if (setup->channels != runtime->channels) {
|
---|
238 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
|
---|
239 | &setup->channels_id);
|
---|
240 | setup->channels = runtime->channels;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | static void loopback_active_notify(struct loopback_pcm *dpcm)
|
---|
247 | {
|
---|
248 | snd_ctl_notify(dpcm->loopback->card,
|
---|
249 | SNDRV_CTL_EVENT_MASK_VALUE,
|
---|
250 | &get_setup(dpcm)->active_id);
|
---|
251 | }
|
---|
252 |
|
---|
253 | static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
|
---|
254 | {
|
---|
255 | struct snd_pcm_runtime *runtime = substream->runtime;
|
---|
256 | struct loopback_pcm *dpcm = runtime->private_data;
|
---|
257 | struct loopback_cable *cable = dpcm->cable;
|
---|
258 | int err, stream = 1 << substream->stream;
|
---|
259 |
|
---|
260 | switch (cmd) {
|
---|
261 | case SNDRV_PCM_TRIGGER_START:
|
---|
262 | err = loopback_check_format(cable, substream->stream);
|
---|
263 | if (err < 0)
|
---|
264 | return err;
|
---|
265 | dpcm->last_jiffies = jiffies;
|
---|
266 | dpcm->pcm_rate_shift = 0;
|
---|
267 | spin_lock(&cable->lock);
|
---|
268 | cable->running |= stream;
|
---|
269 | cable->pause &= ~stream;
|
---|
270 | spin_unlock(&cable->lock);
|
---|
271 | loopback_timer_start(dpcm);
|
---|
272 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
|
---|
273 | loopback_active_notify(dpcm);
|
---|
274 | break;
|
---|
275 | case SNDRV_PCM_TRIGGER_STOP:
|
---|
276 | spin_lock(&cable->lock);
|
---|
277 | cable->running &= ~stream;
|
---|
278 | cable->pause &= ~stream;
|
---|
279 | spin_unlock(&cable->lock);
|
---|
280 | loopback_timer_stop(dpcm);
|
---|
281 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
|
---|
282 | loopback_active_notify(dpcm);
|
---|
283 | break;
|
---|
284 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
|
---|
285 | spin_lock(&cable->lock);
|
---|
286 | cable->pause |= stream;
|
---|
287 | spin_unlock(&cable->lock);
|
---|
288 | loopback_timer_stop(dpcm);
|
---|
289 | break;
|
---|
290 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
|
---|
291 | spin_lock(&cable->lock);
|
---|
292 | dpcm->last_jiffies = jiffies;
|
---|
293 | cable->pause &= ~stream;
|
---|
294 | spin_unlock(&cable->lock);
|
---|
295 | loopback_timer_start(dpcm);
|
---|
296 | break;
|
---|
297 | default:
|
---|
298 | return -EINVAL;
|
---|
299 | }
|
---|
300 | return 0;
|
---|
301 | }
|
---|
302 |
|
---|
303 | static void params_change_substream(struct loopback_pcm *dpcm,
|
---|
304 | struct snd_pcm_runtime *runtime)
|
---|
305 | {
|
---|
306 | struct snd_pcm_runtime *dst_runtime;
|
---|
307 |
|
---|
308 | if (dpcm == NULL || dpcm->substream == NULL)
|
---|
309 | return;
|
---|
310 | dst_runtime = dpcm->substream->runtime;
|
---|
311 | if (dst_runtime == NULL)
|
---|
312 | return;
|
---|
313 | dst_runtime->hw = dpcm->cable->hw;
|
---|
314 | }
|
---|
315 |
|
---|
316 | static void params_change(struct snd_pcm_substream *substream)
|
---|
317 | {
|
---|
318 | struct snd_pcm_runtime *runtime = substream->runtime;
|
---|
319 | struct loopback_pcm *dpcm = runtime->private_data;
|
---|
320 | struct loopback_cable *cable = dpcm->cable;
|
---|
321 |
|
---|
322 | cable->hw.formats = (1ULL << runtime->format);
|
---|
323 | cable->hw.rate_min = runtime->rate;
|
---|
324 | cable->hw.rate_max = runtime->rate;
|
---|
325 | cable->hw.channels_min = runtime->channels;
|
---|
326 | cable->hw.channels_max = runtime->channels;
|
---|
327 | params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
|
---|
328 | runtime);
|
---|
329 | params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
|
---|
330 | runtime);
|
---|
331 | }
|
---|
332 |
|
---|
333 | static int loopback_prepare(struct snd_pcm_substream *substream)
|
---|
334 | {
|
---|
335 | struct snd_pcm_runtime *runtime = substream->runtime;
|
---|
336 | struct loopback_pcm *dpcm = runtime->private_data;
|
---|
337 | struct loopback_cable *cable = dpcm->cable;
|
---|
338 | int bps, salign;
|
---|
339 |
|
---|
340 | salign = (snd_pcm_format_width(runtime->format) *
|
---|
341 | runtime->channels) / 8;
|
---|
342 | bps = salign * runtime->rate;
|
---|
343 | if (bps <= 0 || salign <= 0)
|
---|
344 | return -EINVAL;
|
---|
345 |
|
---|
346 | dpcm->buf_pos = 0;
|
---|
347 | dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
|
---|
348 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
|
---|
349 | /* clear capture buffer */
|
---|
350 | dpcm->silent_size = dpcm->pcm_buffer_size;
|
---|
351 | snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
|
---|
352 | runtime->buffer_size * runtime->channels);
|
---|
353 | }
|
---|
354 |
|
---|
355 | dpcm->irq_pos = 0;
|
---|
356 | dpcm->period_update_pending = 0;
|
---|
357 | dpcm->pcm_bps = bps;
|
---|
358 | dpcm->pcm_salign = salign;
|
---|
359 | dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
|
---|
360 |
|
---|
361 | mutex_lock(&dpcm->loopback->cable_lock);
|
---|
362 | if (!(cable->valid & ~(1 << substream->stream)) ||
|
---|
363 | (get_setup(dpcm)->notify &&
|
---|
364 | substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
|
---|
365 | params_change(substream);
|
---|
366 | cable->valid |= 1 << substream->stream;
|
---|
367 | mutex_unlock(&dpcm->loopback->cable_lock);
|
---|
368 |
|
---|
369 | return 0;
|
---|
370 | }
|
---|
371 |
|
---|
372 | static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
|
---|
373 | {
|
---|
374 | struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
|
---|
375 | char *dst = runtime->dma_area;
|
---|
376 | unsigned int dst_off = dpcm->buf_pos;
|
---|
377 |
|
---|
378 | if (dpcm->silent_size >= dpcm->pcm_buffer_size)
|
---|
379 | return;
|
---|
380 | if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
|
---|
381 | bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
|
---|
382 |
|
---|
383 | for (;;) {
|
---|
384 | unsigned int size = bytes;
|
---|
385 | if (dst_off + size > dpcm->pcm_buffer_size)
|
---|
386 | size = dpcm->pcm_buffer_size - dst_off;
|
---|
387 | snd_pcm_format_set_silence(runtime->format, dst + dst_off,
|
---|
388 | bytes_to_frames(runtime, size) *
|
---|
389 | runtime->channels);
|
---|
390 | dpcm->silent_size += size;
|
---|
391 | bytes -= size;
|
---|
392 | if (!bytes)
|
---|
393 | break;
|
---|
394 | dst_off = 0;
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | static void copy_play_buf(struct loopback_pcm *play,
|
---|
399 | struct loopback_pcm *capt,
|
---|
400 | unsigned int bytes)
|
---|
401 | {
|
---|
402 | struct snd_pcm_runtime *runtime = play->substream->runtime;
|
---|
403 | char *src = runtime->dma_area;
|
---|
404 | char *dst = capt->substream->runtime->dma_area;
|
---|
405 | unsigned int src_off = play->buf_pos;
|
---|
406 | unsigned int dst_off = capt->buf_pos;
|
---|
407 | unsigned int clear_bytes = 0;
|
---|
408 |
|
---|
409 | /* check if playback is draining, trim the capture copy size
|
---|
410 | * when our pointer is at the end of playback ring buffer */
|
---|
411 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
|
---|
412 | snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
|
---|
413 | snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
|
---|
414 | appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
|
---|
415 | appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
|
---|
416 | appl_ptr1 += play->buf_pos / play->pcm_salign;
|
---|
417 | if (appl_ptr < appl_ptr1)
|
---|
418 | appl_ptr1 -= runtime->buffer_size;
|
---|
419 | diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
|
---|
420 | if (diff < bytes) {
|
---|
421 | clear_bytes = bytes - diff;
|
---|
422 | bytes = diff;
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | for (;;) {
|
---|
427 | unsigned int size = bytes;
|
---|
428 | if (src_off + size > play->pcm_buffer_size)
|
---|
429 | size = play->pcm_buffer_size - src_off;
|
---|
430 | if (dst_off + size > capt->pcm_buffer_size)
|
---|
431 | size = capt->pcm_buffer_size - dst_off;
|
---|
432 | memcpy(dst + dst_off, src + src_off, size);
|
---|
433 | capt->silent_size = 0;
|
---|
434 | bytes -= size;
|
---|
435 | if (!bytes)
|
---|
436 | break;
|
---|
437 | src_off = (src_off + size) % play->pcm_buffer_size;
|
---|
438 | dst_off = (dst_off + size) % capt->pcm_buffer_size;
|
---|
439 | }
|
---|
440 |
|
---|
441 | if (clear_bytes > 0) {
|
---|
442 | clear_capture_buf(capt, clear_bytes);
|
---|
443 | capt->silent_size = 0;
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | #define BYTEPOS_UPDATE_POSONLY 0
|
---|
448 | #define BYTEPOS_UPDATE_CLEAR 1
|
---|
449 | #define BYTEPOS_UPDATE_COPY 2
|
---|
450 |
|
---|
451 | static void loopback_bytepos_update(struct loopback_pcm *dpcm,
|
---|
452 | unsigned int delta,
|
---|
453 | unsigned int cmd)
|
---|
454 | {
|
---|
455 | unsigned int count;
|
---|
456 | unsigned long last_pos;
|
---|
457 |
|
---|
458 | last_pos = byte_pos(dpcm, dpcm->irq_pos);
|
---|
459 | dpcm->irq_pos += delta * dpcm->pcm_bps;
|
---|
460 | count = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
|
---|
461 | if (!count)
|
---|
462 | return;
|
---|
463 | if (cmd == BYTEPOS_UPDATE_CLEAR)
|
---|
464 | clear_capture_buf(dpcm, count);
|
---|
465 | else if (cmd == BYTEPOS_UPDATE_COPY)
|
---|
466 | copy_play_buf(dpcm->cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
|
---|
467 | dpcm->cable->streams[SNDRV_PCM_STREAM_CAPTURE],
|
---|
468 | count);
|
---|
469 | dpcm->buf_pos += count;
|
---|
470 | dpcm->buf_pos %= dpcm->pcm_buffer_size;
|
---|
471 | if (dpcm->irq_pos >= dpcm->period_size_frac) {
|
---|
472 | dpcm->irq_pos %= dpcm->period_size_frac;
|
---|
473 | dpcm->period_update_pending = 1;
|
---|
474 | }
|
---|
475 | }
|
---|
476 |
|
---|
477 | static unsigned int loopback_pos_update(struct loopback_cable *cable)
|
---|
478 | {
|
---|
479 | struct loopback_pcm *dpcm_play =
|
---|
480 | cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
|
---|
481 | struct loopback_pcm *dpcm_capt =
|
---|
482 | cable->streams[SNDRV_PCM_STREAM_CAPTURE];
|
---|
483 | unsigned long delta_play = 0, delta_capt = 0;
|
---|
484 | unsigned int running;
|
---|
485 | unsigned long flags;
|
---|
486 |
|
---|
487 | spin_lock_irqsave(&cable->lock, flags);
|
---|
488 | running = cable->running ^ cable->pause;
|
---|
489 | if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
|
---|
490 | delta_play = jiffies - dpcm_play->last_jiffies;
|
---|
491 | dpcm_play->last_jiffies += delta_play;
|
---|
492 | }
|
---|
493 |
|
---|
494 | if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
|
---|
495 | delta_capt = jiffies - dpcm_capt->last_jiffies;
|
---|
496 | dpcm_capt->last_jiffies += delta_capt;
|
---|
497 | }
|
---|
498 |
|
---|
499 | if (delta_play == 0 && delta_capt == 0)
|
---|
500 | goto unlock;
|
---|
501 |
|
---|
502 | if (delta_play > delta_capt) {
|
---|
503 | loopback_bytepos_update(dpcm_play, delta_play - delta_capt,
|
---|
504 | BYTEPOS_UPDATE_POSONLY);
|
---|
505 | delta_play = delta_capt;
|
---|
506 | } else if (delta_play < delta_capt) {
|
---|
507 | loopback_bytepos_update(dpcm_capt, delta_capt - delta_play,
|
---|
508 | BYTEPOS_UPDATE_CLEAR);
|
---|
509 | delta_capt = delta_play;
|
---|
510 | }
|
---|
511 |
|
---|
512 | if (delta_play == 0 && delta_capt == 0)
|
---|
513 | goto unlock;
|
---|
514 |
|
---|
515 | /* note delta_capt == delta_play at this moment */
|
---|
516 | loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
|
---|
517 | loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
|
---|
518 | unlock:
|
---|
519 | spin_unlock_irqrestore(&cable->lock, flags);
|
---|
520 | return running;
|
---|
521 | }
|
---|
522 |
|
---|
523 | static void loopback_timer_function(unsigned long data)
|
---|
524 | {
|
---|
525 | struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
|
---|
526 | unsigned int running;
|
---|
527 |
|
---|
528 | running = loopback_pos_update(dpcm->cable);
|
---|
529 | if (running & (1 << dpcm->substream->stream)) {
|
---|
530 | loopback_timer_start(dpcm);
|
---|
531 | if (dpcm->period_update_pending) {
|
---|
532 | dpcm->period_update_pending = 0;
|
---|
533 | snd_pcm_period_elapsed(dpcm->substream);
|
---|
534 | }
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
|
---|
539 | {
|
---|
540 | struct snd_pcm_runtime *runtime = substream->runtime;
|
---|
541 | struct loopback_pcm *dpcm = runtime->private_data;
|
---|
542 |
|
---|
543 | loopback_pos_update(dpcm->cable);
|
---|
544 | return bytes_to_frames(runtime, dpcm->buf_pos);
|
---|
545 | }
|
---|
546 |
|
---|
547 | static struct snd_pcm_hardware loopback_pcm_hardware =
|
---|
548 | {
|
---|
549 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
|
---|
550 | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
|
---|
551 | .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
|
---|
552 | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
|
---|
553 | SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
|
---|
554 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
|
---|
555 | .rate_min = 8000,
|
---|
556 | .rate_max = 192000,
|
---|
557 | .channels_min = 1,
|
---|
558 | .channels_max = 32,
|
---|
559 | .buffer_bytes_max = 2 * 1024 * 1024,
|
---|
560 | .period_bytes_min = 64,
|
---|
561 | /* note check overflow in frac_pos() using pcm_rate_shift before
|
---|
562 | changing period_bytes_max value */
|
---|
563 | .period_bytes_max = 1024 * 1024,
|
---|
564 | .periods_min = 1,
|
---|
565 | .periods_max = 1024,
|
---|
566 | .fifo_size = 0,
|
---|
567 | };
|
---|
568 |
|
---|
569 | static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
|
---|
570 | {
|
---|
571 | struct loopback_pcm *dpcm = runtime->private_data;
|
---|
572 | kfree(dpcm);
|
---|
573 | }
|
---|
574 |
|
---|
575 | static int loopback_hw_params(struct snd_pcm_substream *substream,
|
---|
576 | struct snd_pcm_hw_params *params)
|
---|
577 | {
|
---|
578 | return snd_pcm_lib_alloc_vmalloc_buffer(substream,
|
---|
579 | params_buffer_bytes(params));
|
---|
580 | }
|
---|
581 |
|
---|
582 | static int loopback_hw_free(struct snd_pcm_substream *substream)
|
---|
583 | {
|
---|
584 | struct snd_pcm_runtime *runtime = substream->runtime;
|
---|
585 | struct loopback_pcm *dpcm = runtime->private_data;
|
---|
586 | struct loopback_cable *cable = dpcm->cable;
|
---|
587 |
|
---|
588 | mutex_lock(&dpcm->loopback->cable_lock);
|
---|
589 | cable->valid &= ~(1 << substream->stream);
|
---|
590 | mutex_unlock(&dpcm->loopback->cable_lock);
|
---|
591 | return snd_pcm_lib_free_vmalloc_buffer(substream);
|
---|
592 | }
|
---|
593 |
|
---|
594 | static unsigned int get_cable_index(struct snd_pcm_substream *substream)
|
---|
595 | {
|
---|
596 | if (!substream->pcm->device)
|
---|
597 | return substream->stream;
|
---|
598 | else
|
---|
599 | return !substream->stream;
|
---|
600 | }
|
---|
601 |
|
---|
602 | static int rule_format(struct snd_pcm_hw_params *params,
|
---|
603 | struct snd_pcm_hw_rule *rule)
|
---|
604 | {
|
---|
605 |
|
---|
606 | struct snd_pcm_hardware *hw = rule->private;
|
---|
607 | struct snd_mask *maskp = hw_param_mask(params, rule->var);
|
---|
608 |
|
---|
609 | maskp->bits[0] &= (u_int32_t)hw->formats;
|
---|
610 | maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
|
---|
611 | memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
|
---|
612 | if (! maskp->bits[0] && ! maskp->bits[1])
|
---|
613 | return -EINVAL;
|
---|
614 | return 0;
|
---|
615 | }
|
---|
616 |
|
---|
617 | static int rule_rate(struct snd_pcm_hw_params *params,
|
---|
618 | struct snd_pcm_hw_rule *rule)
|
---|
619 | {
|
---|
620 | struct snd_pcm_hardware *hw = rule->private;
|
---|
621 | struct snd_interval t;
|
---|
622 |
|
---|
623 | t.min = hw->rate_min;
|
---|
624 | t.max = hw->rate_max;
|
---|
625 | t.openmin = t.openmax = 0;
|
---|
626 | t.integer = 0;
|
---|
627 | return snd_interval_refine(hw_param_interval(params, rule->var), &t);
|
---|
628 | }
|
---|
629 |
|
---|
630 | static int rule_channels(struct snd_pcm_hw_params *params,
|
---|
631 | struct snd_pcm_hw_rule *rule)
|
---|
632 | {
|
---|
633 | struct snd_pcm_hardware *hw = rule->private;
|
---|
634 | struct snd_interval t;
|
---|
635 |
|
---|
636 | t.min = hw->channels_min;
|
---|
637 | t.max = hw->channels_max;
|
---|
638 | t.openmin = t.openmax = 0;
|
---|
639 | t.integer = 0;
|
---|
640 | return snd_interval_refine(hw_param_interval(params, rule->var), &t);
|
---|
641 | }
|
---|
642 |
|
---|
643 | static int loopback_open(struct snd_pcm_substream *substream)
|
---|
644 | {
|
---|
645 | struct snd_pcm_runtime *runtime = substream->runtime;
|
---|
646 | struct loopback *loopback = substream->private_data;
|
---|
647 | struct loopback_pcm *dpcm;
|
---|
648 | struct loopback_cable *cable;
|
---|
649 | int err = 0;
|
---|
650 | int dev = get_cable_index(substream);
|
---|
651 |
|
---|
652 | mutex_lock(&loopback->cable_lock);
|
---|
653 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
|
---|
654 | if (!dpcm) {
|
---|
655 | err = -ENOMEM;
|
---|
656 | goto unlock;
|
---|
657 | }
|
---|
658 | dpcm->loopback = loopback;
|
---|
659 | dpcm->substream = substream;
|
---|
660 | setup_timer(&dpcm->timer, loopback_timer_function,
|
---|
661 | (unsigned long)dpcm);
|
---|
662 |
|
---|
663 | cable = loopback->cables[substream->number][dev];
|
---|
664 | if (!cable) {
|
---|
665 | cable = kzalloc(sizeof(*cable), GFP_KERNEL);
|
---|
666 | if (!cable) {
|
---|
667 | kfree(dpcm);
|
---|
668 | err = -ENOMEM;
|
---|
669 | goto unlock;
|
---|
670 | }
|
---|
671 | spin_lock_init(&cable->lock);
|
---|
672 | cable->hw = loopback_pcm_hardware;
|
---|
673 | loopback->cables[substream->number][dev] = cable;
|
---|
674 | }
|
---|
675 | dpcm->cable = cable;
|
---|
676 | cable->streams[substream->stream] = dpcm;
|
---|
677 |
|
---|
678 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
|
---|
679 |
|
---|
680 | /* use dynamic rules based on actual runtime->hw values */
|
---|
681 | /* note that the default rules created in the PCM midlevel code */
|
---|
682 | /* are cached -> they do not reflect the actual state */
|
---|
683 | err = snd_pcm_hw_rule_add(runtime, 0,
|
---|
684 | SNDRV_PCM_HW_PARAM_FORMAT,
|
---|
685 | rule_format, &runtime->hw,
|
---|
686 | SNDRV_PCM_HW_PARAM_FORMAT, -1);
|
---|
687 | if (err < 0)
|
---|
688 | goto unlock;
|
---|
689 | err = snd_pcm_hw_rule_add(runtime, 0,
|
---|
690 | SNDRV_PCM_HW_PARAM_RATE,
|
---|
691 | rule_rate, &runtime->hw,
|
---|
692 | SNDRV_PCM_HW_PARAM_RATE, -1);
|
---|
693 | if (err < 0)
|
---|
694 | goto unlock;
|
---|
695 | err = snd_pcm_hw_rule_add(runtime, 0,
|
---|
696 | SNDRV_PCM_HW_PARAM_CHANNELS,
|
---|
697 | rule_channels, &runtime->hw,
|
---|
698 | SNDRV_PCM_HW_PARAM_CHANNELS, -1);
|
---|
699 | if (err < 0)
|
---|
700 | goto unlock;
|
---|
701 |
|
---|
702 | runtime->private_data = dpcm;
|
---|
703 | runtime->private_free = loopback_runtime_free;
|
---|
704 | if (get_notify(dpcm))
|
---|
705 | runtime->hw = loopback_pcm_hardware;
|
---|
706 | else
|
---|
707 | runtime->hw = cable->hw;
|
---|
708 | unlock:
|
---|
709 | mutex_unlock(&loopback->cable_lock);
|
---|
710 | return err;
|
---|
711 | }
|
---|
712 |
|
---|
713 | static int loopback_close(struct snd_pcm_substream *substream)
|
---|
714 | {
|
---|
715 | struct loopback *loopback = substream->private_data;
|
---|
716 | struct loopback_pcm *dpcm = substream->runtime->private_data;
|
---|
717 | struct loopback_cable *cable;
|
---|
718 | int dev = get_cable_index(substream);
|
---|
719 |
|
---|
720 | loopback_timer_stop(dpcm);
|
---|
721 | mutex_lock(&loopback->cable_lock);
|
---|
722 | cable = loopback->cables[substream->number][dev];
|
---|
723 | if (cable->streams[!substream->stream]) {
|
---|
724 | /* other stream is still alive */
|
---|
725 | cable->streams[substream->stream] = NULL;
|
---|
726 | } else {
|
---|
727 | /* free the cable */
|
---|
728 | loopback->cables[substream->number][dev] = NULL;
|
---|
729 | kfree(cable);
|
---|
730 | }
|
---|
731 | mutex_unlock(&loopback->cable_lock);
|
---|
732 | return 0;
|
---|
733 | }
|
---|
734 |
|
---|
735 | static struct snd_pcm_ops loopback_playback_ops = {
|
---|
736 | .open = loopback_open,
|
---|
737 | .close = loopback_close,
|
---|
738 | .ioctl = snd_pcm_lib_ioctl,
|
---|
739 | .hw_params = loopback_hw_params,
|
---|
740 | .hw_free = loopback_hw_free,
|
---|
741 | .prepare = loopback_prepare,
|
---|
742 | .trigger = loopback_trigger,
|
---|
743 | .pointer = loopback_pointer,
|
---|
744 | .page = snd_pcm_lib_get_vmalloc_page,
|
---|
745 | .mmap = snd_pcm_lib_mmap_vmalloc,
|
---|
746 | };
|
---|
747 |
|
---|
748 | static struct snd_pcm_ops loopback_capture_ops = {
|
---|
749 | .open = loopback_open,
|
---|
750 | .close = loopback_close,
|
---|
751 | .ioctl = snd_pcm_lib_ioctl,
|
---|
752 | .hw_params = loopback_hw_params,
|
---|
753 | .hw_free = loopback_hw_free,
|
---|
754 | .prepare = loopback_prepare,
|
---|
755 | .trigger = loopback_trigger,
|
---|
756 | .pointer = loopback_pointer,
|
---|
757 | .page = snd_pcm_lib_get_vmalloc_page,
|
---|
758 | .mmap = snd_pcm_lib_mmap_vmalloc,
|
---|
759 | };
|
---|
760 |
|
---|
761 | static int __devinit loopback_pcm_new(struct loopback *loopback,
|
---|
762 | int device, int substreams)
|
---|
763 | {
|
---|
764 | struct snd_pcm *pcm;
|
---|
765 | int err;
|
---|
766 |
|
---|
767 | err = snd_pcm_new(loopback->card, "Loopback PCM", device,
|
---|
768 | substreams, substreams, &pcm);
|
---|
769 | if (err < 0)
|
---|
770 | return err;
|
---|
771 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
|
---|
772 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
|
---|
773 |
|
---|
774 | pcm->private_data = loopback;
|
---|
775 | pcm->info_flags = 0;
|
---|
776 | strcpy(pcm->name, "Loopback PCM");
|
---|
777 |
|
---|
778 | loopback->pcm[device] = pcm;
|
---|
779 | return 0;
|
---|
780 | }
|
---|
781 |
|
---|
782 | static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
|
---|
783 | struct snd_ctl_elem_info *uinfo)
|
---|
784 | {
|
---|
785 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
|
---|
786 | uinfo->count = 1;
|
---|
787 | uinfo->value.integer.min = 80000;
|
---|
788 | uinfo->value.integer.max = 120000;
|
---|
789 | uinfo->value.integer.step = 1;
|
---|
790 | return 0;
|
---|
791 | }
|
---|
792 |
|
---|
793 | static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
|
---|
794 | struct snd_ctl_elem_value *ucontrol)
|
---|
795 | {
|
---|
796 | struct loopback *loopback = snd_kcontrol_chip(kcontrol);
|
---|
797 |
|
---|
798 | ucontrol->value.integer.value[0] =
|
---|
799 | loopback->setup[kcontrol->id.subdevice]
|
---|
800 | [kcontrol->id.device].rate_shift;
|
---|
801 | return 0;
|
---|
802 | }
|
---|
803 |
|
---|
804 | static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
|
---|
805 | struct snd_ctl_elem_value *ucontrol)
|
---|
806 | {
|
---|
807 | struct loopback *loopback = snd_kcontrol_chip(kcontrol);
|
---|
808 | unsigned int val;
|
---|
809 | int change = 0;
|
---|
810 |
|
---|
811 | val = ucontrol->value.integer.value[0];
|
---|
812 | if (val < 80000)
|
---|
813 | val = 80000;
|
---|
814 | if (val > 120000)
|
---|
815 | val = 120000;
|
---|
816 | mutex_lock(&loopback->cable_lock);
|
---|
817 | if (val != loopback->setup[kcontrol->id.subdevice]
|
---|
818 | [kcontrol->id.device].rate_shift) {
|
---|
819 | loopback->setup[kcontrol->id.subdevice]
|
---|
820 | [kcontrol->id.device].rate_shift = val;
|
---|
821 | change = 1;
|
---|
822 | }
|
---|
823 | mutex_unlock(&loopback->cable_lock);
|
---|
824 | return change;
|
---|
825 | }
|
---|
826 |
|
---|
827 | static int loopback_notify_get(struct snd_kcontrol *kcontrol,
|
---|
828 | struct snd_ctl_elem_value *ucontrol)
|
---|
829 | {
|
---|
830 | struct loopback *loopback = snd_kcontrol_chip(kcontrol);
|
---|
831 |
|
---|
832 | ucontrol->value.integer.value[0] =
|
---|
833 | loopback->setup[kcontrol->id.subdevice]
|
---|
834 | [kcontrol->id.device].notify;
|
---|
835 | return 0;
|
---|
836 | }
|
---|
837 |
|
---|
838 | static int loopback_notify_put(struct snd_kcontrol *kcontrol,
|
---|
839 | struct snd_ctl_elem_value *ucontrol)
|
---|
840 | {
|
---|
841 | struct loopback *loopback = snd_kcontrol_chip(kcontrol);
|
---|
842 | unsigned int val;
|
---|
843 | int change = 0;
|
---|
844 |
|
---|
845 | val = ucontrol->value.integer.value[0] ? 1 : 0;
|
---|
846 | if (val != loopback->setup[kcontrol->id.subdevice]
|
---|
847 | [kcontrol->id.device].notify) {
|
---|
848 | loopback->setup[kcontrol->id.subdevice]
|
---|
849 | [kcontrol->id.device].notify = val;
|
---|
850 | change = 1;
|
---|
851 | }
|
---|
852 | return change;
|
---|
853 | }
|
---|
854 |
|
---|
855 | static int loopback_active_get(struct snd_kcontrol *kcontrol,
|
---|
856 | struct snd_ctl_elem_value *ucontrol)
|
---|
857 | {
|
---|
858 | struct loopback *loopback = snd_kcontrol_chip(kcontrol);
|
---|
859 | struct loopback_cable *cable = loopback->cables
|
---|
860 | [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
|
---|
861 | unsigned int val = 0;
|
---|
862 |
|
---|
863 | if (cable != NULL)
|
---|
864 | val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
|
---|
865 | 1 : 0;
|
---|
866 | ucontrol->value.integer.value[0] = val;
|
---|
867 | return 0;
|
---|
868 | }
|
---|
869 |
|
---|
870 | static int loopback_format_info(struct snd_kcontrol *kcontrol,
|
---|
871 | struct snd_ctl_elem_info *uinfo)
|
---|
872 | {
|
---|
873 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
|
---|
874 | uinfo->count = 1;
|
---|
875 | uinfo->value.integer.min = 0;
|
---|
876 | uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
|
---|
877 | uinfo->value.integer.step = 1;
|
---|
878 | return 0;
|
---|
879 | }
|
---|
880 |
|
---|
881 | static int loopback_format_get(struct snd_kcontrol *kcontrol,
|
---|
882 | struct snd_ctl_elem_value *ucontrol)
|
---|
883 | {
|
---|
884 | struct loopback *loopback = snd_kcontrol_chip(kcontrol);
|
---|
885 |
|
---|
886 | ucontrol->value.integer.value[0] =
|
---|
887 | loopback->setup[kcontrol->id.subdevice]
|
---|
888 | [kcontrol->id.device].format;
|
---|
889 | return 0;
|
---|
890 | }
|
---|
891 |
|
---|
892 | static int loopback_rate_info(struct snd_kcontrol *kcontrol,
|
---|
893 | struct snd_ctl_elem_info *uinfo)
|
---|
894 | {
|
---|
895 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
|
---|
896 | uinfo->count = 1;
|
---|
897 | uinfo->value.integer.min = 0;
|
---|
898 | uinfo->value.integer.max = 192000;
|
---|
899 | uinfo->value.integer.step = 1;
|
---|
900 | return 0;
|
---|
901 | }
|
---|
902 |
|
---|
903 | static int loopback_rate_get(struct snd_kcontrol *kcontrol,
|
---|
904 | struct snd_ctl_elem_value *ucontrol)
|
---|
905 | {
|
---|
906 | struct loopback *loopback = snd_kcontrol_chip(kcontrol);
|
---|
907 |
|
---|
908 | ucontrol->value.integer.value[0] =
|
---|
909 | loopback->setup[kcontrol->id.subdevice]
|
---|
910 | [kcontrol->id.device].rate;
|
---|
911 | return 0;
|
---|
912 | }
|
---|
913 |
|
---|
914 | static int loopback_channels_info(struct snd_kcontrol *kcontrol,
|
---|
915 | struct snd_ctl_elem_info *uinfo)
|
---|
916 | {
|
---|
917 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
|
---|
918 | uinfo->count = 1;
|
---|
919 | uinfo->value.integer.min = 1;
|
---|
920 | uinfo->value.integer.max = 1024;
|
---|
921 | uinfo->value.integer.step = 1;
|
---|
922 | return 0;
|
---|
923 | }
|
---|
924 |
|
---|
925 | static int loopback_channels_get(struct snd_kcontrol *kcontrol,
|
---|
926 | struct snd_ctl_elem_value *ucontrol)
|
---|
927 | {
|
---|
928 | struct loopback *loopback = snd_kcontrol_chip(kcontrol);
|
---|
929 |
|
---|
930 | ucontrol->value.integer.value[0] =
|
---|
931 | loopback->setup[kcontrol->id.subdevice]
|
---|
932 | [kcontrol->id.device].channels;
|
---|
933 | return 0;
|
---|
934 | }
|
---|
935 |
|
---|
936 | static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
|
---|
937 | {
|
---|
938 | .iface = SNDRV_CTL_ELEM_IFACE_PCM,
|
---|
939 | .name = "PCM Rate Shift 100000",
|
---|
940 | .info = loopback_rate_shift_info,
|
---|
941 | .get = loopback_rate_shift_get,
|
---|
942 | .put = loopback_rate_shift_put,
|
---|
943 | },
|
---|
944 | {
|
---|
945 | .iface = SNDRV_CTL_ELEM_IFACE_PCM,
|
---|
946 | .name = "PCM Notify",
|
---|
947 | .info = snd_ctl_boolean_mono_info,
|
---|
948 | .get = loopback_notify_get,
|
---|
949 | .put = loopback_notify_put,
|
---|
950 | },
|
---|
951 | #define ACTIVE_IDX 2
|
---|
952 | {
|
---|
953 | .access = SNDRV_CTL_ELEM_ACCESS_READ,
|
---|
954 | .iface = SNDRV_CTL_ELEM_IFACE_PCM,
|
---|
955 | .name = "PCM Slave Active",
|
---|
956 | .info = snd_ctl_boolean_mono_info,
|
---|
957 | .get = loopback_active_get,
|
---|
958 | },
|
---|
959 | #define FORMAT_IDX 3
|
---|
960 | {
|
---|
961 | .access = SNDRV_CTL_ELEM_ACCESS_READ,
|
---|
962 | .iface = SNDRV_CTL_ELEM_IFACE_PCM,
|
---|
963 | .name = "PCM Slave Format",
|
---|
964 | .info = loopback_format_info,
|
---|
965 | .get = loopback_format_get
|
---|
966 | },
|
---|
967 | #define RATE_IDX 4
|
---|
968 | {
|
---|
969 | .access = SNDRV_CTL_ELEM_ACCESS_READ,
|
---|
970 | .iface = SNDRV_CTL_ELEM_IFACE_PCM,
|
---|
971 | .name = "PCM Slave Rate",
|
---|
972 | .info = loopback_rate_info,
|
---|
973 | .get = loopback_rate_get
|
---|
974 | },
|
---|
975 | #define CHANNELS_IDX 5
|
---|
976 | {
|
---|
977 | .access = SNDRV_CTL_ELEM_ACCESS_READ,
|
---|
978 | .iface = SNDRV_CTL_ELEM_IFACE_PCM,
|
---|
979 | .name = "PCM Slave Channels",
|
---|
980 | .info = loopback_channels_info,
|
---|
981 | .get = loopback_channels_get
|
---|
982 | }
|
---|
983 | };
|
---|
984 |
|
---|
985 | static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
|
---|
986 | {
|
---|
987 | struct snd_card *card = loopback->card;
|
---|
988 | struct snd_pcm *pcm;
|
---|
989 | struct snd_kcontrol *kctl;
|
---|
990 | struct loopback_setup *setup;
|
---|
991 | int err, dev, substr, substr_count, idx;
|
---|
992 |
|
---|
993 | strcpy(card->mixername, "Loopback Mixer");
|
---|
994 | for (dev = 0; dev < 2; dev++) {
|
---|
995 | pcm = loopback->pcm[dev];
|
---|
996 | substr_count =
|
---|
997 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
|
---|
998 | for (substr = 0; substr < substr_count; substr++) {
|
---|
999 | setup = &loopback->setup[substr][dev];
|
---|
1000 | setup->notify = notify;
|
---|
1001 | setup->rate_shift = NO_PITCH;
|
---|
1002 | setup->format = SNDRV_PCM_FORMAT_S16_LE;
|
---|
1003 | setup->rate = 48000;
|
---|
1004 | setup->channels = 2;
|
---|
1005 | for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
|
---|
1006 | idx++) {
|
---|
1007 | kctl = snd_ctl_new1(&loopback_controls[idx],
|
---|
1008 | loopback);
|
---|
1009 | if (!kctl)
|
---|
1010 | return -ENOMEM;
|
---|
1011 | kctl->id.device = dev;
|
---|
1012 | kctl->id.subdevice = substr;
|
---|
1013 | switch (idx) {
|
---|
1014 | case ACTIVE_IDX:
|
---|
1015 | setup->active_id = kctl->id;
|
---|
1016 | break;
|
---|
1017 | case FORMAT_IDX:
|
---|
1018 | setup->format_id = kctl->id;
|
---|
1019 | break;
|
---|
1020 | case RATE_IDX:
|
---|
1021 | setup->rate_id = kctl->id;
|
---|
1022 | break;
|
---|
1023 | case CHANNELS_IDX:
|
---|
1024 | setup->channels_id = kctl->id;
|
---|
1025 | break;
|
---|
1026 | default:
|
---|
1027 | break;
|
---|
1028 | }
|
---|
1029 | err = snd_ctl_add(card, kctl);
|
---|
1030 | if (err < 0)
|
---|
1031 | return err;
|
---|
1032 | }
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 | return 0;
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | #ifdef CONFIG_PROC_FS
|
---|
1039 |
|
---|
1040 | static void print_dpcm_info(struct snd_info_buffer *buffer,
|
---|
1041 | struct loopback_pcm *dpcm,
|
---|
1042 | const char *id)
|
---|
1043 | {
|
---|
1044 | snd_iprintf(buffer, " %s\n", id);
|
---|
1045 | if (dpcm == NULL) {
|
---|
1046 | snd_iprintf(buffer, " inactive\n");
|
---|
1047 | return;
|
---|
1048 | }
|
---|
1049 | snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
|
---|
1050 | snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
|
---|
1051 | snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
|
---|
1052 | snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
|
---|
1053 | snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
|
---|
1054 | snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
|
---|
1055 | snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
|
---|
1056 | snd_iprintf(buffer, " update_pending:\t%u\n",
|
---|
1057 | dpcm->period_update_pending);
|
---|
1058 | snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
|
---|
1059 | snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
|
---|
1060 | snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
|
---|
1061 | dpcm->last_jiffies, jiffies);
|
---|
1062 | snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | static void print_substream_info(struct snd_info_buffer *buffer,
|
---|
1066 | struct loopback *loopback,
|
---|
1067 | int sub,
|
---|
1068 | int num)
|
---|
1069 | {
|
---|
1070 | struct loopback_cable *cable = loopback->cables[sub][num];
|
---|
1071 |
|
---|
1072 | snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
|
---|
1073 | if (cable == NULL) {
|
---|
1074 | snd_iprintf(buffer, " inactive\n");
|
---|
1075 | return;
|
---|
1076 | }
|
---|
1077 | snd_iprintf(buffer, " valid: %u\n", cable->valid);
|
---|
1078 | snd_iprintf(buffer, " running: %u\n", cable->running);
|
---|
1079 | snd_iprintf(buffer, " pause: %u\n", cable->pause);
|
---|
1080 | print_dpcm_info(buffer, cable->streams[0], "Playback");
|
---|
1081 | print_dpcm_info(buffer, cable->streams[1], "Capture");
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | static void print_cable_info(struct snd_info_entry *entry,
|
---|
1085 | struct snd_info_buffer *buffer)
|
---|
1086 | {
|
---|
1087 | struct loopback *loopback = entry->private_data;
|
---|
1088 | int sub, num;
|
---|
1089 |
|
---|
1090 | mutex_lock(&loopback->cable_lock);
|
---|
1091 | num = entry->name[strlen(entry->name)-1];
|
---|
1092 | num = num == '0' ? 0 : 1;
|
---|
1093 | for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
|
---|
1094 | print_substream_info(buffer, loopback, sub, num);
|
---|
1095 | mutex_unlock(&loopback->cable_lock);
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | static int __devinit loopback_proc_new(struct loopback *loopback, int cidx)
|
---|
1099 | {
|
---|
1100 | char name[32];
|
---|
1101 | struct snd_info_entry *entry;
|
---|
1102 | int err;
|
---|
1103 |
|
---|
1104 | snprintf(name, sizeof(name), "cable#%d", cidx);
|
---|
1105 | err = snd_card_proc_new(loopback->card, name, &entry);
|
---|
1106 | if (err < 0)
|
---|
1107 | return err;
|
---|
1108 |
|
---|
1109 | snd_info_set_text_ops(entry, loopback, print_cable_info);
|
---|
1110 | return 0;
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | #else /* !CONFIG_PROC_FS */
|
---|
1114 |
|
---|
1115 | #define loopback_proc_new(loopback, cidx) do { } while (0)
|
---|
1116 |
|
---|
1117 | #endif
|
---|
1118 |
|
---|
1119 | static int __devinit loopback_probe(struct platform_device *devptr)
|
---|
1120 | {
|
---|
1121 | struct snd_card *card;
|
---|
1122 | struct loopback *loopback;
|
---|
1123 | int dev = devptr->id;
|
---|
1124 | int err;
|
---|
1125 |
|
---|
1126 | err = snd_card_create(index[dev], id[dev], THIS_MODULE,
|
---|
1127 | sizeof(struct loopback), &card);
|
---|
1128 | if (err < 0)
|
---|
1129 | return err;
|
---|
1130 | loopback = card->private_data;
|
---|
1131 |
|
---|
1132 | if (pcm_substreams[dev] < 1)
|
---|
1133 | pcm_substreams[dev] = 1;
|
---|
1134 | if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
|
---|
1135 | pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
|
---|
1136 |
|
---|
1137 | loopback->card = card;
|
---|
1138 | mutex_init(&loopback->cable_lock);
|
---|
1139 |
|
---|
1140 | err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
|
---|
1141 | if (err < 0)
|
---|
1142 | goto __nodev;
|
---|
1143 | err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
|
---|
1144 | if (err < 0)
|
---|
1145 | goto __nodev;
|
---|
1146 | err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
|
---|
1147 | if (err < 0)
|
---|
1148 | goto __nodev;
|
---|
1149 | loopback_proc_new(loopback, 0);
|
---|
1150 | loopback_proc_new(loopback, 1);
|
---|
1151 | strcpy(card->driver, "Loopback");
|
---|
1152 | strcpy(card->shortname, "Loopback");
|
---|
1153 | sprintf(card->longname, "Loopback %i", dev + 1);
|
---|
1154 | err = snd_card_register(card);
|
---|
1155 | if (!err) {
|
---|
1156 | platform_set_drvdata(devptr, card);
|
---|
1157 | return 0;
|
---|
1158 | }
|
---|
1159 | __nodev:
|
---|
1160 | snd_card_free(card);
|
---|
1161 | return err;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | static int __devexit loopback_remove(struct platform_device *devptr)
|
---|
1165 | {
|
---|
1166 | snd_card_free(platform_get_drvdata(devptr));
|
---|
1167 | platform_set_drvdata(devptr, NULL);
|
---|
1168 | return 0;
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | #ifdef CONFIG_PM
|
---|
1172 | static int loopback_suspend(struct platform_device *pdev,
|
---|
1173 | pm_message_t state)
|
---|
1174 | {
|
---|
1175 | struct snd_card *card = platform_get_drvdata(pdev);
|
---|
1176 | struct loopback *loopback = card->private_data;
|
---|
1177 |
|
---|
1178 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
|
---|
1179 |
|
---|
1180 | snd_pcm_suspend_all(loopback->pcm[0]);
|
---|
1181 | snd_pcm_suspend_all(loopback->pcm[1]);
|
---|
1182 | return 0;
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | static int loopback_resume(struct platform_device *pdev)
|
---|
1186 | {
|
---|
1187 | struct snd_card *card = platform_get_drvdata(pdev);
|
---|
1188 |
|
---|
1189 | snd_power_change_state(card, SNDRV_CTL_POWER_D0);
|
---|
1190 | return 0;
|
---|
1191 | }
|
---|
1192 | #endif
|
---|
1193 |
|
---|
1194 | #define SND_LOOPBACK_DRIVER "snd_aloop"
|
---|
1195 |
|
---|
1196 | static struct platform_driver loopback_driver = {
|
---|
1197 | .probe = loopback_probe,
|
---|
1198 | .remove = __devexit_p(loopback_remove),
|
---|
1199 | #ifdef CONFIG_PM
|
---|
1200 | .suspend = loopback_suspend,
|
---|
1201 | .resume = loopback_resume,
|
---|
1202 | #endif
|
---|
1203 | .driver = {
|
---|
1204 | .name = SND_LOOPBACK_DRIVER
|
---|
1205 | },
|
---|
1206 | };
|
---|
1207 |
|
---|
1208 | static void loopback_unregister_all(void)
|
---|
1209 | {
|
---|
1210 | int i;
|
---|
1211 |
|
---|
1212 | for (i = 0; i < ARRAY_SIZE(devices); ++i)
|
---|
1213 | platform_device_unregister(devices[i]);
|
---|
1214 | platform_driver_unregister(&loopback_driver);
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | static int __init alsa_card_loopback_init(void)
|
---|
1218 | {
|
---|
1219 | int i, err, cards;
|
---|
1220 |
|
---|
1221 | err = platform_driver_register(&loopback_driver);
|
---|
1222 | if (err < 0)
|
---|
1223 | return err;
|
---|
1224 |
|
---|
1225 |
|
---|
1226 | cards = 0;
|
---|
1227 | for (i = 0; i < SNDRV_CARDS; i++) {
|
---|
1228 | struct platform_device *device;
|
---|
1229 | if (!enable[i])
|
---|
1230 | continue;
|
---|
1231 | device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
|
---|
1232 | i, NULL, 0);
|
---|
1233 | if (IS_ERR(device))
|
---|
1234 | continue;
|
---|
1235 | if (!platform_get_drvdata(device)) {
|
---|
1236 | platform_device_unregister(device);
|
---|
1237 | continue;
|
---|
1238 | }
|
---|
1239 | devices[i] = device;
|
---|
1240 | cards++;
|
---|
1241 | }
|
---|
1242 | if (!cards) {
|
---|
1243 | #ifdef MODULE
|
---|
1244 | printk(KERN_ERR "aloop: No loopback enabled\n");
|
---|
1245 | #endif
|
---|
1246 | loopback_unregister_all();
|
---|
1247 | return -ENODEV;
|
---|
1248 | }
|
---|
1249 | return 0;
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | static void __exit alsa_card_loopback_exit(void)
|
---|
1253 | {
|
---|
1254 | loopback_unregister_all();
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | module_init(alsa_card_loopback_init)
|
---|
1258 | module_exit(alsa_card_loopback_exit)
|
---|