Commit | Line | Data |
---|---|---|
597603d6 JK |
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> | |
65a77217 | 37 | #include <linux/module.h> |
597603d6 JK |
38 | #include <linux/platform_device.h> |
39 | #include <sound/core.h> | |
40 | #include <sound/control.h> | |
41 | #include <sound/pcm.h> | |
e74670b6 | 42 | #include <sound/info.h> |
597603d6 JK |
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 */ | |
a67ff6a5 | 54 | static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; |
597603d6 JK |
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; | |
5de9e45f | 80 | unsigned int pause; |
597603d6 JK |
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; | |
b012513c | 120 | unsigned int last_drift; |
597603d6 JK |
121 | unsigned long last_jiffies; |
122 | struct timer_list timer; | |
123 | }; | |
124 | ||
125 | static struct platform_device *devices[SNDRV_CARDS]; | |
126 | ||
127 | static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x) | |
128 | { | |
129 | if (dpcm->pcm_rate_shift == NO_PITCH) { | |
130 | x /= HZ; | |
131 | } else { | |
132 | x = div_u64(NO_PITCH * (unsigned long long)x, | |
133 | HZ * (unsigned long long)dpcm->pcm_rate_shift); | |
134 | } | |
135 | return x - (x % dpcm->pcm_salign); | |
136 | } | |
137 | ||
138 | static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x) | |
139 | { | |
140 | if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */ | |
141 | return x * HZ; | |
142 | } else { | |
143 | x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ, | |
144 | NO_PITCH); | |
145 | } | |
146 | return x; | |
147 | } | |
148 | ||
149 | static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm) | |
150 | { | |
151 | int device = dpcm->substream->pstr->pcm->device; | |
152 | ||
153 | if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
154 | device ^= 1; | |
155 | return &dpcm->loopback->setup[dpcm->substream->number][device]; | |
156 | } | |
157 | ||
158 | static inline unsigned int get_notify(struct loopback_pcm *dpcm) | |
159 | { | |
160 | return get_setup(dpcm)->notify; | |
161 | } | |
162 | ||
163 | static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm) | |
164 | { | |
165 | return get_setup(dpcm)->rate_shift; | |
166 | } | |
167 | ||
999fc9f6 | 168 | /* call in cable->lock */ |
597603d6 JK |
169 | static void loopback_timer_start(struct loopback_pcm *dpcm) |
170 | { | |
171 | unsigned long tick; | |
172 | unsigned int rate_shift = get_rate_shift(dpcm); | |
173 | ||
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); | |
177 | } | |
0db71023 JK |
178 | if (dpcm->period_size_frac <= dpcm->irq_pos) { |
179 | dpcm->irq_pos %= dpcm->period_size_frac; | |
180 | dpcm->period_update_pending = 1; | |
181 | } | |
597603d6 JK |
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); | |
186 | } | |
187 | ||
999fc9f6 | 188 | /* call in cable->lock */ |
597603d6 JK |
189 | static inline void loopback_timer_stop(struct loopback_pcm *dpcm) |
190 | { | |
191 | del_timer(&dpcm->timer); | |
e74670b6 | 192 | dpcm->timer.expires = 0; |
597603d6 JK |
193 | } |
194 | ||
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) | |
198 | ||
199 | static int loopback_check_format(struct loopback_cable *cable, int stream) | |
200 | { | |
b1c73fc8 | 201 | struct snd_pcm_runtime *runtime, *cruntime; |
597603d6 JK |
202 | struct loopback_setup *setup; |
203 | struct snd_card *card; | |
204 | int check; | |
205 | ||
206 | if (cable->valid != CABLE_VALID_BOTH) { | |
207 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
208 | goto __notify; | |
209 | return 0; | |
210 | } | |
211 | runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]-> | |
212 | substream->runtime; | |
b1c73fc8 JK |
213 | cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]-> |
214 | substream->runtime; | |
215 | check = runtime->format != cruntime->format || | |
216 | runtime->rate != cruntime->rate || | |
217 | runtime->channels != cruntime->channels; | |
597603d6 JK |
218 | if (!check) |
219 | return 0; | |
220 | if (stream == SNDRV_PCM_STREAM_CAPTURE) { | |
221 | return -EIO; | |
222 | } else { | |
223 | snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]-> | |
224 | substream, SNDRV_PCM_STATE_DRAINING); | |
225 | __notify: | |
226 | runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]-> | |
227 | substream->runtime; | |
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, | |
232 | &setup->format_id); | |
233 | setup->format = runtime->format; | |
234 | } | |
235 | if (setup->rate != runtime->rate) { | |
236 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, | |
237 | &setup->rate_id); | |
238 | setup->rate = runtime->rate; | |
239 | } | |
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; | |
244 | } | |
245 | } | |
246 | return 0; | |
247 | } | |
248 | ||
249 | static void loopback_active_notify(struct loopback_pcm *dpcm) | |
250 | { | |
251 | snd_ctl_notify(dpcm->loopback->card, | |
252 | SNDRV_CTL_EVENT_MASK_VALUE, | |
253 | &get_setup(dpcm)->active_id); | |
254 | } | |
255 | ||
256 | static int loopback_trigger(struct snd_pcm_substream *substream, int cmd) | |
257 | { | |
258 | struct snd_pcm_runtime *runtime = substream->runtime; | |
259 | struct loopback_pcm *dpcm = runtime->private_data; | |
260 | struct loopback_cable *cable = dpcm->cable; | |
5de9e45f | 261 | int err, stream = 1 << substream->stream; |
597603d6 JK |
262 | |
263 | switch (cmd) { | |
264 | case SNDRV_PCM_TRIGGER_START: | |
265 | err = loopback_check_format(cable, substream->stream); | |
266 | if (err < 0) | |
267 | return err; | |
268 | dpcm->last_jiffies = jiffies; | |
269 | dpcm->pcm_rate_shift = 0; | |
b012513c | 270 | dpcm->last_drift = 0; |
dd04bb12 | 271 | spin_lock(&cable->lock); |
5de9e45f JK |
272 | cable->running |= stream; |
273 | cable->pause &= ~stream; | |
dd04bb12 | 274 | loopback_timer_start(dpcm); |
999fc9f6 | 275 | spin_unlock(&cable->lock); |
597603d6 JK |
276 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
277 | loopback_active_notify(dpcm); | |
278 | break; | |
279 | case SNDRV_PCM_TRIGGER_STOP: | |
dd04bb12 | 280 | spin_lock(&cable->lock); |
5de9e45f JK |
281 | cable->running &= ~stream; |
282 | cable->pause &= ~stream; | |
597603d6 | 283 | loopback_timer_stop(dpcm); |
999fc9f6 | 284 | spin_unlock(&cable->lock); |
597603d6 JK |
285 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
286 | loopback_active_notify(dpcm); | |
287 | break; | |
5de9e45f JK |
288 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
289 | spin_lock(&cable->lock); | |
290 | cable->pause |= stream; | |
5de9e45f | 291 | loopback_timer_stop(dpcm); |
999fc9f6 | 292 | spin_unlock(&cable->lock); |
5de9e45f JK |
293 | break; |
294 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
295 | spin_lock(&cable->lock); | |
296 | dpcm->last_jiffies = jiffies; | |
297 | cable->pause &= ~stream; | |
5de9e45f | 298 | loopback_timer_start(dpcm); |
999fc9f6 | 299 | spin_unlock(&cable->lock); |
5de9e45f | 300 | break; |
597603d6 JK |
301 | default: |
302 | return -EINVAL; | |
303 | } | |
304 | return 0; | |
305 | } | |
306 | ||
b1c73fc8 JK |
307 | static void params_change_substream(struct loopback_pcm *dpcm, |
308 | struct snd_pcm_runtime *runtime) | |
309 | { | |
310 | struct snd_pcm_runtime *dst_runtime; | |
311 | ||
312 | if (dpcm == NULL || dpcm->substream == NULL) | |
313 | return; | |
314 | dst_runtime = dpcm->substream->runtime; | |
315 | if (dst_runtime == NULL) | |
316 | return; | |
317 | dst_runtime->hw = dpcm->cable->hw; | |
318 | } | |
319 | ||
320 | static void params_change(struct snd_pcm_substream *substream) | |
321 | { | |
322 | struct snd_pcm_runtime *runtime = substream->runtime; | |
323 | struct loopback_pcm *dpcm = runtime->private_data; | |
324 | struct loopback_cable *cable = dpcm->cable; | |
325 | ||
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], | |
332 | runtime); | |
333 | params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE], | |
334 | runtime); | |
335 | } | |
336 | ||
597603d6 JK |
337 | static int loopback_prepare(struct snd_pcm_substream *substream) |
338 | { | |
339 | struct snd_pcm_runtime *runtime = substream->runtime; | |
340 | struct loopback_pcm *dpcm = runtime->private_data; | |
341 | struct loopback_cable *cable = dpcm->cable; | |
b1c73fc8 | 342 | int bps, salign; |
597603d6 JK |
343 | |
344 | salign = (snd_pcm_format_width(runtime->format) * | |
345 | runtime->channels) / 8; | |
346 | bps = salign * runtime->rate; | |
347 | if (bps <= 0 || salign <= 0) | |
348 | return -EINVAL; | |
349 | ||
350 | dpcm->buf_pos = 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); | |
357 | } | |
358 | ||
359 | dpcm->irq_pos = 0; | |
360 | dpcm->period_update_pending = 0; | |
361 | dpcm->pcm_bps = bps; | |
362 | dpcm->pcm_salign = salign; | |
363 | dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size); | |
364 | ||
365 | mutex_lock(&dpcm->loopback->cable_lock); | |
b1c73fc8 JK |
366 | if (!(cable->valid & ~(1 << substream->stream)) || |
367 | (get_setup(dpcm)->notify && | |
368 | substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) | |
369 | params_change(substream); | |
597603d6 JK |
370 | cable->valid |= 1 << substream->stream; |
371 | mutex_unlock(&dpcm->loopback->cable_lock); | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes) | |
377 | { | |
378 | struct snd_pcm_runtime *runtime = dpcm->substream->runtime; | |
379 | char *dst = runtime->dma_area; | |
380 | unsigned int dst_off = dpcm->buf_pos; | |
381 | ||
382 | if (dpcm->silent_size >= dpcm->pcm_buffer_size) | |
383 | return; | |
384 | if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size) | |
385 | bytes = dpcm->pcm_buffer_size - dpcm->silent_size; | |
386 | ||
387 | for (;;) { | |
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) * | |
393 | runtime->channels); | |
394 | dpcm->silent_size += size; | |
395 | bytes -= size; | |
396 | if (!bytes) | |
397 | break; | |
398 | dst_off = 0; | |
399 | } | |
400 | } | |
401 | ||
402 | static void copy_play_buf(struct loopback_pcm *play, | |
403 | struct loopback_pcm *capt, | |
404 | unsigned int bytes) | |
405 | { | |
406 | struct snd_pcm_runtime *runtime = play->substream->runtime; | |
20d9a26d | 407 | char *src = runtime->dma_area; |
597603d6 JK |
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; | |
412 | ||
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; | |
424 | if (diff < bytes) { | |
425 | clear_bytes = bytes - diff; | |
426 | bytes = diff; | |
427 | } | |
428 | } | |
429 | ||
430 | for (;;) { | |
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; | |
438 | bytes -= size; | |
439 | if (!bytes) | |
440 | break; | |
441 | src_off = (src_off + size) % play->pcm_buffer_size; | |
442 | dst_off = (dst_off + size) % capt->pcm_buffer_size; | |
443 | } | |
444 | ||
20d9a26d | 445 | if (clear_bytes > 0) { |
597603d6 | 446 | clear_capture_buf(capt, clear_bytes); |
20d9a26d JK |
447 | capt->silent_size = 0; |
448 | } | |
597603d6 JK |
449 | } |
450 | ||
b012513c JK |
451 | static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm, |
452 | unsigned int jiffies_delta) | |
597603d6 | 453 | { |
597603d6 | 454 | unsigned long last_pos; |
b012513c | 455 | unsigned int delta; |
597603d6 JK |
456 | |
457 | last_pos = byte_pos(dpcm, dpcm->irq_pos); | |
b012513c JK |
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; | |
597603d6 JK |
463 | if (dpcm->irq_pos >= dpcm->period_size_frac) { |
464 | dpcm->irq_pos %= dpcm->period_size_frac; | |
465 | dpcm->period_update_pending = 1; | |
466 | } | |
b012513c JK |
467 | return delta; |
468 | } | |
469 | ||
470 | static inline void bytepos_finish(struct loopback_pcm *dpcm, | |
471 | unsigned int delta) | |
472 | { | |
473 | dpcm->buf_pos += delta; | |
474 | dpcm->buf_pos %= dpcm->pcm_buffer_size; | |
597603d6 JK |
475 | } |
476 | ||
999fc9f6 | 477 | /* call in cable->lock */ |
dd04bb12 | 478 | static unsigned int loopback_pos_update(struct loopback_cable *cable) |
597603d6 JK |
479 | { |
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; | |
b012513c | 485 | unsigned int running, count1, count2; |
597603d6 | 486 | |
5de9e45f | 487 | running = cable->running ^ cable->pause; |
dd04bb12 | 488 | if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) { |
597603d6 JK |
489 | delta_play = jiffies - dpcm_play->last_jiffies; |
490 | dpcm_play->last_jiffies += delta_play; | |
491 | } | |
492 | ||
dd04bb12 | 493 | if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) { |
597603d6 JK |
494 | delta_capt = jiffies - dpcm_capt->last_jiffies; |
495 | dpcm_capt->last_jiffies += delta_capt; | |
496 | } | |
497 | ||
98d21df4 TI |
498 | if (delta_play == 0 && delta_capt == 0) |
499 | goto unlock; | |
597603d6 JK |
500 | |
501 | if (delta_play > delta_capt) { | |
b012513c JK |
502 | count1 = bytepos_delta(dpcm_play, delta_play - delta_capt); |
503 | bytepos_finish(dpcm_play, count1); | |
597603d6 JK |
504 | delta_play = delta_capt; |
505 | } else if (delta_play < delta_capt) { | |
b012513c JK |
506 | count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play); |
507 | clear_capture_buf(dpcm_capt, count1); | |
508 | bytepos_finish(dpcm_capt, count1); | |
597603d6 JK |
509 | delta_capt = delta_play; |
510 | } | |
511 | ||
98d21df4 TI |
512 | if (delta_play == 0 && delta_capt == 0) |
513 | goto unlock; | |
514 | ||
597603d6 | 515 | /* note delta_capt == delta_play at this moment */ |
b012513c JK |
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; | |
520 | count1 = count2; | |
521 | } else if (count1 > count2) { | |
522 | dpcm_play->last_drift = count1 - count2; | |
523 | } | |
524 | copy_play_buf(dpcm_play, dpcm_capt, count1); | |
525 | bytepos_finish(dpcm_play, count1); | |
526 | bytepos_finish(dpcm_capt, count1); | |
98d21df4 | 527 | unlock: |
dd04bb12 | 528 | return running; |
597603d6 JK |
529 | } |
530 | ||
531 | static void loopback_timer_function(unsigned long data) | |
532 | { | |
533 | struct loopback_pcm *dpcm = (struct loopback_pcm *)data; | |
999fc9f6 | 534 | unsigned long flags; |
597603d6 | 535 | |
999fc9f6 TI |
536 | spin_lock_irqsave(&dpcm->cable->lock, flags); |
537 | if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) { | |
597603d6 | 538 | loopback_timer_start(dpcm); |
dd04bb12 JK |
539 | if (dpcm->period_update_pending) { |
540 | dpcm->period_update_pending = 0; | |
999fc9f6 TI |
541 | spin_unlock_irqrestore(&dpcm->cable->lock, flags); |
542 | /* need to unlock before calling below */ | |
597603d6 | 543 | snd_pcm_period_elapsed(dpcm->substream); |
999fc9f6 | 544 | return; |
dd04bb12 | 545 | } |
597603d6 | 546 | } |
999fc9f6 | 547 | spin_unlock_irqrestore(&dpcm->cable->lock, flags); |
597603d6 JK |
548 | } |
549 | ||
550 | static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream) | |
551 | { | |
552 | struct snd_pcm_runtime *runtime = substream->runtime; | |
553 | struct loopback_pcm *dpcm = runtime->private_data; | |
999fc9f6 | 554 | snd_pcm_uframes_t pos; |
597603d6 | 555 | |
999fc9f6 | 556 | spin_lock(&dpcm->cable->lock); |
597603d6 | 557 | loopback_pos_update(dpcm->cable); |
999fc9f6 TI |
558 | pos = dpcm->buf_pos; |
559 | spin_unlock(&dpcm->cable->lock); | |
560 | return bytes_to_frames(runtime, pos); | |
597603d6 JK |
561 | } |
562 | ||
563 | static struct snd_pcm_hardware loopback_pcm_hardware = | |
564 | { | |
565 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP | | |
5de9e45f | 566 | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE), |
597603d6 JK |
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, | |
571 | .rate_min = 8000, | |
572 | .rate_max = 192000, | |
573 | .channels_min = 1, | |
574 | .channels_max = 32, | |
575 | .buffer_bytes_max = 2 * 1024 * 1024, | |
576 | .period_bytes_min = 64, | |
0db71023 JK |
577 | /* note check overflow in frac_pos() using pcm_rate_shift before |
578 | changing period_bytes_max value */ | |
579 | .period_bytes_max = 1024 * 1024, | |
597603d6 JK |
580 | .periods_min = 1, |
581 | .periods_max = 1024, | |
582 | .fifo_size = 0, | |
583 | }; | |
584 | ||
585 | static void loopback_runtime_free(struct snd_pcm_runtime *runtime) | |
586 | { | |
587 | struct loopback_pcm *dpcm = runtime->private_data; | |
588 | kfree(dpcm); | |
589 | } | |
590 | ||
591 | static int loopback_hw_params(struct snd_pcm_substream *substream, | |
592 | struct snd_pcm_hw_params *params) | |
593 | { | |
6b69a0e5 TI |
594 | return snd_pcm_lib_alloc_vmalloc_buffer(substream, |
595 | params_buffer_bytes(params)); | |
597603d6 JK |
596 | } |
597 | ||
598 | static int loopback_hw_free(struct snd_pcm_substream *substream) | |
599 | { | |
600 | struct snd_pcm_runtime *runtime = substream->runtime; | |
601 | struct loopback_pcm *dpcm = runtime->private_data; | |
602 | struct loopback_cable *cable = dpcm->cable; | |
603 | ||
604 | mutex_lock(&dpcm->loopback->cable_lock); | |
605 | cable->valid &= ~(1 << substream->stream); | |
606 | mutex_unlock(&dpcm->loopback->cable_lock); | |
6b69a0e5 | 607 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
597603d6 JK |
608 | } |
609 | ||
610 | static unsigned int get_cable_index(struct snd_pcm_substream *substream) | |
611 | { | |
612 | if (!substream->pcm->device) | |
613 | return substream->stream; | |
614 | else | |
615 | return !substream->stream; | |
616 | } | |
617 | ||
b1c73fc8 JK |
618 | static int rule_format(struct snd_pcm_hw_params *params, |
619 | struct snd_pcm_hw_rule *rule) | |
620 | { | |
621 | ||
622 | struct snd_pcm_hardware *hw = rule->private; | |
623 | struct snd_mask *maskp = hw_param_mask(params, rule->var); | |
624 | ||
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]) | |
629 | return -EINVAL; | |
630 | return 0; | |
631 | } | |
632 | ||
633 | static int rule_rate(struct snd_pcm_hw_params *params, | |
634 | struct snd_pcm_hw_rule *rule) | |
635 | { | |
636 | struct snd_pcm_hardware *hw = rule->private; | |
637 | struct snd_interval t; | |
638 | ||
639 | t.min = hw->rate_min; | |
640 | t.max = hw->rate_max; | |
641 | t.openmin = t.openmax = 0; | |
642 | t.integer = 0; | |
643 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
644 | } | |
645 | ||
646 | static int rule_channels(struct snd_pcm_hw_params *params, | |
647 | struct snd_pcm_hw_rule *rule) | |
648 | { | |
649 | struct snd_pcm_hardware *hw = rule->private; | |
650 | struct snd_interval t; | |
651 | ||
652 | t.min = hw->channels_min; | |
653 | t.max = hw->channels_max; | |
654 | t.openmin = t.openmax = 0; | |
655 | t.integer = 0; | |
656 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
657 | } | |
658 | ||
597603d6 JK |
659 | static int loopback_open(struct snd_pcm_substream *substream) |
660 | { | |
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; | |
665 | int err = 0; | |
666 | int dev = get_cable_index(substream); | |
667 | ||
668 | mutex_lock(&loopback->cable_lock); | |
669 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); | |
670 | if (!dpcm) { | |
671 | err = -ENOMEM; | |
672 | goto unlock; | |
673 | } | |
674 | dpcm->loopback = loopback; | |
675 | dpcm->substream = substream; | |
676 | setup_timer(&dpcm->timer, loopback_timer_function, | |
677 | (unsigned long)dpcm); | |
678 | ||
679 | cable = loopback->cables[substream->number][dev]; | |
680 | if (!cable) { | |
681 | cable = kzalloc(sizeof(*cable), GFP_KERNEL); | |
682 | if (!cable) { | |
683 | kfree(dpcm); | |
684 | err = -ENOMEM; | |
685 | goto unlock; | |
686 | } | |
687 | spin_lock_init(&cable->lock); | |
688 | cable->hw = loopback_pcm_hardware; | |
689 | loopback->cables[substream->number][dev] = cable; | |
690 | } | |
691 | dpcm->cable = cable; | |
692 | cable->streams[substream->stream] = dpcm; | |
693 | ||
694 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); | |
695 | ||
b1c73fc8 JK |
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); | |
703 | if (err < 0) | |
704 | goto unlock; | |
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); | |
709 | if (err < 0) | |
710 | goto unlock; | |
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); | |
715 | if (err < 0) | |
716 | goto unlock; | |
717 | ||
597603d6 JK |
718 | runtime->private_data = dpcm; |
719 | runtime->private_free = loopback_runtime_free; | |
b1c73fc8 | 720 | if (get_notify(dpcm)) |
597603d6 | 721 | runtime->hw = loopback_pcm_hardware; |
b1c73fc8 | 722 | else |
597603d6 | 723 | runtime->hw = cable->hw; |
597603d6 JK |
724 | unlock: |
725 | mutex_unlock(&loopback->cable_lock); | |
726 | return err; | |
727 | } | |
728 | ||
729 | static int loopback_close(struct snd_pcm_substream *substream) | |
730 | { | |
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); | |
735 | ||
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; | |
742 | } else { | |
743 | /* free the cable */ | |
744 | loopback->cables[substream->number][dev] = NULL; | |
745 | kfree(cable); | |
746 | } | |
747 | mutex_unlock(&loopback->cable_lock); | |
748 | return 0; | |
749 | } | |
750 | ||
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, | |
6b69a0e5 TI |
760 | .page = snd_pcm_lib_get_vmalloc_page, |
761 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
597603d6 JK |
762 | }; |
763 | ||
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, | |
6b69a0e5 TI |
773 | .page = snd_pcm_lib_get_vmalloc_page, |
774 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
597603d6 JK |
775 | }; |
776 | ||
777 | static int __devinit loopback_pcm_new(struct loopback *loopback, | |
778 | int device, int substreams) | |
779 | { | |
780 | struct snd_pcm *pcm; | |
781 | int err; | |
782 | ||
783 | err = snd_pcm_new(loopback->card, "Loopback PCM", device, | |
784 | substreams, substreams, &pcm); | |
785 | if (err < 0) | |
786 | return err; | |
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); | |
789 | ||
790 | pcm->private_data = loopback; | |
791 | pcm->info_flags = 0; | |
792 | strcpy(pcm->name, "Loopback PCM"); | |
793 | ||
794 | loopback->pcm[device] = pcm; | |
597603d6 JK |
795 | return 0; |
796 | } | |
797 | ||
798 | static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol, | |
799 | struct snd_ctl_elem_info *uinfo) | |
800 | { | |
801 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
802 | uinfo->count = 1; | |
803 | uinfo->value.integer.min = 80000; | |
804 | uinfo->value.integer.max = 120000; | |
805 | uinfo->value.integer.step = 1; | |
806 | return 0; | |
807 | } | |
808 | ||
809 | static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol, | |
810 | struct snd_ctl_elem_value *ucontrol) | |
811 | { | |
812 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); | |
813 | ||
814 | ucontrol->value.integer.value[0] = | |
815 | loopback->setup[kcontrol->id.subdevice] | |
816 | [kcontrol->id.device].rate_shift; | |
817 | return 0; | |
818 | } | |
819 | ||
820 | static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol, | |
821 | struct snd_ctl_elem_value *ucontrol) | |
822 | { | |
823 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); | |
824 | unsigned int val; | |
825 | int change = 0; | |
826 | ||
827 | val = ucontrol->value.integer.value[0]; | |
828 | if (val < 80000) | |
829 | val = 80000; | |
830 | if (val > 120000) | |
831 | val = 120000; | |
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; | |
837 | change = 1; | |
838 | } | |
839 | mutex_unlock(&loopback->cable_lock); | |
840 | return change; | |
841 | } | |
842 | ||
843 | static int loopback_notify_get(struct snd_kcontrol *kcontrol, | |
844 | struct snd_ctl_elem_value *ucontrol) | |
845 | { | |
846 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); | |
847 | ||
848 | ucontrol->value.integer.value[0] = | |
849 | loopback->setup[kcontrol->id.subdevice] | |
850 | [kcontrol->id.device].notify; | |
851 | return 0; | |
852 | } | |
853 | ||
854 | static int loopback_notify_put(struct snd_kcontrol *kcontrol, | |
855 | struct snd_ctl_elem_value *ucontrol) | |
856 | { | |
857 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); | |
858 | unsigned int val; | |
859 | int change = 0; | |
860 | ||
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; | |
866 | change = 1; | |
867 | } | |
868 | return change; | |
869 | } | |
870 | ||
871 | static int loopback_active_get(struct snd_kcontrol *kcontrol, | |
872 | struct snd_ctl_elem_value *ucontrol) | |
873 | { | |
874 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); | |
875 | struct loopback_cable *cable = loopback->cables | |
ac446fb7 | 876 | [kcontrol->id.subdevice][kcontrol->id.device ^ 1]; |
597603d6 JK |
877 | unsigned int val = 0; |
878 | ||
879 | if (cable != NULL) | |
880 | val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? | |
881 | 1 : 0; | |
882 | ucontrol->value.integer.value[0] = val; | |
883 | return 0; | |
884 | } | |
885 | ||
886 | static int loopback_format_info(struct snd_kcontrol *kcontrol, | |
887 | struct snd_ctl_elem_info *uinfo) | |
888 | { | |
889 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
890 | uinfo->count = 1; | |
891 | uinfo->value.integer.min = 0; | |
892 | uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST; | |
893 | uinfo->value.integer.step = 1; | |
894 | return 0; | |
895 | } | |
896 | ||
897 | static int loopback_format_get(struct snd_kcontrol *kcontrol, | |
898 | struct snd_ctl_elem_value *ucontrol) | |
899 | { | |
900 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); | |
901 | ||
902 | ucontrol->value.integer.value[0] = | |
903 | loopback->setup[kcontrol->id.subdevice] | |
904 | [kcontrol->id.device].format; | |
905 | return 0; | |
906 | } | |
907 | ||
908 | static int loopback_rate_info(struct snd_kcontrol *kcontrol, | |
909 | struct snd_ctl_elem_info *uinfo) | |
910 | { | |
911 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
912 | uinfo->count = 1; | |
913 | uinfo->value.integer.min = 0; | |
914 | uinfo->value.integer.max = 192000; | |
915 | uinfo->value.integer.step = 1; | |
916 | return 0; | |
917 | } | |
918 | ||
919 | static int loopback_rate_get(struct snd_kcontrol *kcontrol, | |
920 | struct snd_ctl_elem_value *ucontrol) | |
921 | { | |
922 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); | |
923 | ||
924 | ucontrol->value.integer.value[0] = | |
925 | loopback->setup[kcontrol->id.subdevice] | |
926 | [kcontrol->id.device].rate; | |
927 | return 0; | |
928 | } | |
929 | ||
930 | static int loopback_channels_info(struct snd_kcontrol *kcontrol, | |
931 | struct snd_ctl_elem_info *uinfo) | |
932 | { | |
933 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
934 | uinfo->count = 1; | |
935 | uinfo->value.integer.min = 1; | |
936 | uinfo->value.integer.max = 1024; | |
937 | uinfo->value.integer.step = 1; | |
938 | return 0; | |
939 | } | |
940 | ||
941 | static int loopback_channels_get(struct snd_kcontrol *kcontrol, | |
942 | struct snd_ctl_elem_value *ucontrol) | |
943 | { | |
944 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); | |
945 | ||
946 | ucontrol->value.integer.value[0] = | |
947 | loopback->setup[kcontrol->id.subdevice] | |
1446c5fb | 948 | [kcontrol->id.device].channels; |
597603d6 JK |
949 | return 0; |
950 | } | |
951 | ||
952 | static struct snd_kcontrol_new loopback_controls[] __devinitdata = { | |
953 | { | |
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, | |
959 | }, | |
960 | { | |
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, | |
966 | }, | |
967 | #define ACTIVE_IDX 2 | |
968 | { | |
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, | |
974 | }, | |
975 | #define FORMAT_IDX 3 | |
976 | { | |
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 | |
982 | }, | |
983 | #define RATE_IDX 4 | |
984 | { | |
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 | |
990 | }, | |
991 | #define CHANNELS_IDX 5 | |
992 | { | |
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 | |
998 | } | |
999 | }; | |
1000 | ||
1001 | static int __devinit loopback_mixer_new(struct loopback *loopback, int notify) | |
1002 | { | |
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; | |
1008 | ||
1009 | strcpy(card->mixername, "Loopback Mixer"); | |
1010 | for (dev = 0; dev < 2; dev++) { | |
1011 | pcm = loopback->pcm[dev]; | |
1012 | substr_count = | |
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); | |
1022 | idx++) { | |
1023 | kctl = snd_ctl_new1(&loopback_controls[idx], | |
1024 | loopback); | |
1025 | if (!kctl) | |
1026 | return -ENOMEM; | |
1027 | kctl->id.device = dev; | |
1028 | kctl->id.subdevice = substr; | |
1029 | switch (idx) { | |
1030 | case ACTIVE_IDX: | |
1031 | setup->active_id = kctl->id; | |
1032 | break; | |
1033 | case FORMAT_IDX: | |
1034 | setup->format_id = kctl->id; | |
1035 | break; | |
1036 | case RATE_IDX: | |
1037 | setup->rate_id = kctl->id; | |
1038 | break; | |
1039 | case CHANNELS_IDX: | |
1040 | setup->channels_id = kctl->id; | |
1041 | break; | |
1042 | default: | |
1043 | break; | |
1044 | } | |
1045 | err = snd_ctl_add(card, kctl); | |
1046 | if (err < 0) | |
1047 | return err; | |
1048 | } | |
1049 | } | |
1050 | } | |
1051 | return 0; | |
1052 | } | |
1053 | ||
e74670b6 JK |
1054 | #ifdef CONFIG_PROC_FS |
1055 | ||
1056 | static void print_dpcm_info(struct snd_info_buffer *buffer, | |
1057 | struct loopback_pcm *dpcm, | |
1058 | const char *id) | |
1059 | { | |
1060 | snd_iprintf(buffer, " %s\n", id); | |
1061 | if (dpcm == NULL) { | |
1062 | snd_iprintf(buffer, " inactive\n"); | |
1063 | return; | |
1064 | } | |
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); | |
1079 | } | |
1080 | ||
1081 | static void print_substream_info(struct snd_info_buffer *buffer, | |
1082 | struct loopback *loopback, | |
1083 | int sub, | |
1084 | int num) | |
1085 | { | |
1086 | struct loopback_cable *cable = loopback->cables[sub][num]; | |
1087 | ||
1088 | snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub); | |
1089 | if (cable == NULL) { | |
1090 | snd_iprintf(buffer, " inactive\n"); | |
1091 | return; | |
1092 | } | |
1093 | snd_iprintf(buffer, " valid: %u\n", cable->valid); | |
1094 | snd_iprintf(buffer, " running: %u\n", cable->running); | |
5de9e45f | 1095 | snd_iprintf(buffer, " pause: %u\n", cable->pause); |
e74670b6 JK |
1096 | print_dpcm_info(buffer, cable->streams[0], "Playback"); |
1097 | print_dpcm_info(buffer, cable->streams[1], "Capture"); | |
1098 | } | |
1099 | ||
1100 | static void print_cable_info(struct snd_info_entry *entry, | |
1101 | struct snd_info_buffer *buffer) | |
1102 | { | |
1103 | struct loopback *loopback = entry->private_data; | |
1104 | int sub, num; | |
1105 | ||
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); | |
1112 | } | |
1113 | ||
1114 | static int __devinit loopback_proc_new(struct loopback *loopback, int cidx) | |
1115 | { | |
1116 | char name[32]; | |
1117 | struct snd_info_entry *entry; | |
1118 | int err; | |
1119 | ||
1120 | snprintf(name, sizeof(name), "cable#%d", cidx); | |
1121 | err = snd_card_proc_new(loopback->card, name, &entry); | |
1122 | if (err < 0) | |
1123 | return err; | |
1124 | ||
1125 | snd_info_set_text_ops(entry, loopback, print_cable_info); | |
1126 | return 0; | |
1127 | } | |
1128 | ||
1129 | #else /* !CONFIG_PROC_FS */ | |
1130 | ||
1131 | #define loopback_proc_new(loopback, cidx) do { } while (0) | |
1132 | ||
1133 | #endif | |
1134 | ||
597603d6 JK |
1135 | static int __devinit loopback_probe(struct platform_device *devptr) |
1136 | { | |
1137 | struct snd_card *card; | |
1138 | struct loopback *loopback; | |
1139 | int dev = devptr->id; | |
1140 | int err; | |
1141 | ||
1142 | err = snd_card_create(index[dev], id[dev], THIS_MODULE, | |
1143 | sizeof(struct loopback), &card); | |
1144 | if (err < 0) | |
1145 | return err; | |
1146 | loopback = card->private_data; | |
1147 | ||
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; | |
1152 | ||
1153 | loopback->card = card; | |
1154 | mutex_init(&loopback->cable_lock); | |
1155 | ||
1156 | err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]); | |
1157 | if (err < 0) | |
1158 | goto __nodev; | |
1159 | err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]); | |
1160 | if (err < 0) | |
1161 | goto __nodev; | |
1162 | err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0); | |
1163 | if (err < 0) | |
1164 | goto __nodev; | |
e74670b6 JK |
1165 | loopback_proc_new(loopback, 0); |
1166 | loopback_proc_new(loopback, 1); | |
597603d6 JK |
1167 | strcpy(card->driver, "Loopback"); |
1168 | strcpy(card->shortname, "Loopback"); | |
1169 | sprintf(card->longname, "Loopback %i", dev + 1); | |
1170 | err = snd_card_register(card); | |
1171 | if (!err) { | |
1172 | platform_set_drvdata(devptr, card); | |
1173 | return 0; | |
1174 | } | |
1175 | __nodev: | |
1176 | snd_card_free(card); | |
1177 | return err; | |
1178 | } | |
1179 | ||
1180 | static int __devexit loopback_remove(struct platform_device *devptr) | |
1181 | { | |
1182 | snd_card_free(platform_get_drvdata(devptr)); | |
1183 | platform_set_drvdata(devptr, NULL); | |
1184 | return 0; | |
1185 | } | |
1186 | ||
d34e4e00 | 1187 | #ifdef CONFIG_PM_SLEEP |
284e7ca7 | 1188 | static int loopback_suspend(struct device *pdev) |
597603d6 | 1189 | { |
284e7ca7 | 1190 | struct snd_card *card = dev_get_drvdata(pdev); |
597603d6 JK |
1191 | struct loopback *loopback = card->private_data; |
1192 | ||
1193 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); | |
1194 | ||
1195 | snd_pcm_suspend_all(loopback->pcm[0]); | |
1196 | snd_pcm_suspend_all(loopback->pcm[1]); | |
1197 | return 0; | |
1198 | } | |
1199 | ||
284e7ca7 | 1200 | static int loopback_resume(struct device *pdev) |
597603d6 | 1201 | { |
284e7ca7 | 1202 | struct snd_card *card = dev_get_drvdata(pdev); |
597603d6 JK |
1203 | |
1204 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); | |
1205 | return 0; | |
1206 | } | |
284e7ca7 TI |
1207 | |
1208 | static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume); | |
1209 | #define LOOPBACK_PM_OPS &loopback_pm | |
1210 | #else | |
1211 | #define LOOPBACK_PM_OPS NULL | |
597603d6 JK |
1212 | #endif |
1213 | ||
1214 | #define SND_LOOPBACK_DRIVER "snd_aloop" | |
1215 | ||
1216 | static struct platform_driver loopback_driver = { | |
1217 | .probe = loopback_probe, | |
1218 | .remove = __devexit_p(loopback_remove), | |
597603d6 | 1219 | .driver = { |
8bf01d8a TI |
1220 | .name = SND_LOOPBACK_DRIVER, |
1221 | .owner = THIS_MODULE, | |
284e7ca7 | 1222 | .pm = LOOPBACK_PM_OPS, |
597603d6 JK |
1223 | }, |
1224 | }; | |
1225 | ||
1226 | static void loopback_unregister_all(void) | |
1227 | { | |
1228 | int i; | |
1229 | ||
1230 | for (i = 0; i < ARRAY_SIZE(devices); ++i) | |
1231 | platform_device_unregister(devices[i]); | |
1232 | platform_driver_unregister(&loopback_driver); | |
1233 | } | |
1234 | ||
1235 | static int __init alsa_card_loopback_init(void) | |
1236 | { | |
1237 | int i, err, cards; | |
1238 | ||
1239 | err = platform_driver_register(&loopback_driver); | |
1240 | if (err < 0) | |
1241 | return err; | |
1242 | ||
1243 | ||
1244 | cards = 0; | |
1245 | for (i = 0; i < SNDRV_CARDS; i++) { | |
1246 | struct platform_device *device; | |
1247 | if (!enable[i]) | |
1248 | continue; | |
1249 | device = platform_device_register_simple(SND_LOOPBACK_DRIVER, | |
1250 | i, NULL, 0); | |
1251 | if (IS_ERR(device)) | |
1252 | continue; | |
1253 | if (!platform_get_drvdata(device)) { | |
1254 | platform_device_unregister(device); | |
1255 | continue; | |
1256 | } | |
1257 | devices[i] = device; | |
1258 | cards++; | |
1259 | } | |
1260 | if (!cards) { | |
1261 | #ifdef MODULE | |
1262 | printk(KERN_ERR "aloop: No loopback enabled\n"); | |
1263 | #endif | |
1264 | loopback_unregister_all(); | |
1265 | return -ENODEV; | |
1266 | } | |
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | static void __exit alsa_card_loopback_exit(void) | |
1271 | { | |
1272 | loopback_unregister_all(); | |
1273 | } | |
1274 | ||
1275 | module_init(alsa_card_loopback_init) | |
1276 | module_exit(alsa_card_loopback_exit) |