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