1 | // SPDX-License-Identifier: GPL-2.0-or-later |
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> |
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> |
23 | #include <linux/module.h> |
24 | #include <linux/platform_device.h> |
25 | #include <sound/core.h> |
26 | #include <sound/control.h> |
27 | #include <sound/pcm.h> |
28 | #include <sound/pcm_params.h> |
29 | #include <sound/info.h> |
30 | #include <sound/initval.h> |
31 | #include <sound/timer.h> |
32 | |
33 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>" ); |
34 | MODULE_DESCRIPTION("A loopback soundcard" ); |
35 | MODULE_LICENSE("GPL" ); |
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 */ |
41 | static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; |
42 | static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; |
43 | static int pcm_notify[SNDRV_CARDS]; |
44 | static char *timer_source[SNDRV_CARDS]; |
45 | |
46 | module_param_array(index, int, NULL, 0444); |
47 | MODULE_PARM_DESC(index, "Index value for loopback soundcard." ); |
48 | module_param_array(id, charp, NULL, 0444); |
49 | MODULE_PARM_DESC(id, "ID string for loopback soundcard." ); |
50 | module_param_array(enable, bool, NULL, 0444); |
51 | MODULE_PARM_DESC(enable, "Enable this loopback soundcard." ); |
52 | module_param_array(pcm_substreams, int, NULL, 0444); |
53 | MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver." ); |
54 | module_param_array(pcm_notify, int, NULL, 0444); |
55 | MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes." ); |
56 | module_param_array(timer_source, charp, NULL, 0444); |
57 | MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default]." ); |
58 | |
59 | #define NO_PITCH 100000 |
60 | |
61 | #define CABLE_VALID_PLAYBACK BIT(SNDRV_PCM_STREAM_PLAYBACK) |
62 | #define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE) |
63 | #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE) |
64 | |
65 | struct loopback_cable; |
66 | struct loopback_pcm; |
67 | |
68 | struct loopback_ops { |
69 | /* optional |
70 | * call in loopback->cable_lock |
71 | */ |
72 | int (*open)(struct loopback_pcm *dpcm); |
73 | /* required |
74 | * call in cable->lock |
75 | */ |
76 | int (*start)(struct loopback_pcm *dpcm); |
77 | /* required |
78 | * call in cable->lock |
79 | */ |
80 | int (*stop)(struct loopback_pcm *dpcm); |
81 | /* optional */ |
82 | int (*stop_sync)(struct loopback_pcm *dpcm); |
83 | /* optional */ |
84 | int (*close_substream)(struct loopback_pcm *dpcm); |
85 | /* optional |
86 | * call in loopback->cable_lock |
87 | */ |
88 | int (*close_cable)(struct loopback_pcm *dpcm); |
89 | /* optional |
90 | * call in cable->lock |
91 | */ |
92 | unsigned int (*pos_update)(struct loopback_cable *cable); |
93 | /* optional */ |
94 | void (*dpcm_info)(struct loopback_pcm *dpcm, |
95 | struct snd_info_buffer *buffer); |
96 | }; |
97 | |
98 | struct loopback_cable { |
99 | spinlock_t lock; |
100 | struct loopback_pcm *streams[2]; |
101 | struct snd_pcm_hardware hw; |
102 | /* flags */ |
103 | unsigned int valid; |
104 | unsigned int running; |
105 | unsigned int pause; |
106 | /* timer specific */ |
107 | const struct loopback_ops *ops; |
108 | /* If sound timer is used */ |
109 | struct { |
110 | int stream; |
111 | struct snd_timer_id id; |
112 | struct work_struct event_work; |
113 | struct snd_timer_instance *instance; |
114 | } snd_timer; |
115 | }; |
116 | |
117 | struct loopback_setup { |
118 | unsigned int notify: 1; |
119 | unsigned int rate_shift; |
120 | snd_pcm_format_t format; |
121 | unsigned int rate; |
122 | snd_pcm_access_t access; |
123 | unsigned int channels; |
124 | struct snd_ctl_elem_id active_id; |
125 | struct snd_ctl_elem_id format_id; |
126 | struct snd_ctl_elem_id rate_id; |
127 | struct snd_ctl_elem_id channels_id; |
128 | struct snd_ctl_elem_id access_id; |
129 | }; |
130 | |
131 | struct loopback { |
132 | struct snd_card *card; |
133 | struct mutex cable_lock; |
134 | struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2]; |
135 | struct snd_pcm *pcm[2]; |
136 | struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2]; |
137 | const char *timer_source; |
138 | }; |
139 | |
140 | struct loopback_pcm { |
141 | struct loopback *loopback; |
142 | struct snd_pcm_substream *substream; |
143 | struct loopback_cable *cable; |
144 | unsigned int pcm_buffer_size; |
145 | unsigned int buf_pos; /* position in buffer */ |
146 | unsigned int silent_size; |
147 | /* PCM parameters */ |
148 | unsigned int pcm_period_size; |
149 | unsigned int pcm_bps; /* bytes per second */ |
150 | unsigned int pcm_salign; /* bytes per sample * channels */ |
151 | unsigned int pcm_rate_shift; /* rate shift value */ |
152 | /* flags */ |
153 | unsigned int period_update_pending :1; |
154 | /* timer stuff */ |
155 | unsigned int irq_pos; /* fractional IRQ position in jiffies |
156 | * ticks |
157 | */ |
158 | unsigned int period_size_frac; /* period size in jiffies ticks */ |
159 | unsigned int last_drift; |
160 | unsigned long last_jiffies; |
161 | /* If jiffies timer is used */ |
162 | struct timer_list timer; |
163 | |
164 | /* size of per channel buffer in case of non-interleaved access */ |
165 | unsigned int channel_buf_n; |
166 | }; |
167 | |
168 | static struct platform_device *devices[SNDRV_CARDS]; |
169 | |
170 | static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x) |
171 | { |
172 | if (dpcm->pcm_rate_shift == NO_PITCH) { |
173 | x /= HZ; |
174 | } else { |
175 | x = div_u64(NO_PITCH * (unsigned long long)x, |
176 | HZ * (unsigned long long)dpcm->pcm_rate_shift); |
177 | } |
178 | return x - (x % dpcm->pcm_salign); |
179 | } |
180 | |
181 | static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x) |
182 | { |
183 | if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */ |
184 | return x * HZ; |
185 | } else { |
186 | x = div_u64(dividend: dpcm->pcm_rate_shift * (unsigned long long)x * HZ, |
187 | NO_PITCH); |
188 | } |
189 | return x; |
190 | } |
191 | |
192 | static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm) |
193 | { |
194 | int device = dpcm->substream->pstr->pcm->device; |
195 | |
196 | if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
197 | device ^= 1; |
198 | return &dpcm->loopback->setup[dpcm->substream->number][device]; |
199 | } |
200 | |
201 | static inline unsigned int get_notify(struct loopback_pcm *dpcm) |
202 | { |
203 | return get_setup(dpcm)->notify; |
204 | } |
205 | |
206 | static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm) |
207 | { |
208 | return get_setup(dpcm)->rate_shift; |
209 | } |
210 | |
211 | /* call in cable->lock */ |
212 | static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm) |
213 | { |
214 | unsigned long tick; |
215 | unsigned int rate_shift = get_rate_shift(dpcm); |
216 | |
217 | if (rate_shift != dpcm->pcm_rate_shift) { |
218 | dpcm->pcm_rate_shift = rate_shift; |
219 | dpcm->period_size_frac = frac_pos(dpcm, x: dpcm->pcm_period_size); |
220 | } |
221 | if (dpcm->period_size_frac <= dpcm->irq_pos) { |
222 | dpcm->irq_pos %= dpcm->period_size_frac; |
223 | dpcm->period_update_pending = 1; |
224 | } |
225 | tick = dpcm->period_size_frac - dpcm->irq_pos; |
226 | tick = DIV_ROUND_UP(tick, dpcm->pcm_bps); |
227 | mod_timer(timer: &dpcm->timer, expires: jiffies + tick); |
228 | |
229 | return 0; |
230 | } |
231 | |
232 | /* call in cable->lock */ |
233 | static int loopback_snd_timer_start(struct loopback_pcm *dpcm) |
234 | { |
235 | struct loopback_cable *cable = dpcm->cable; |
236 | int err; |
237 | |
238 | /* Loopback device has to use same period as timer card. Therefore |
239 | * wake up for each snd_pcm_period_elapsed() call of timer card. |
240 | */ |
241 | err = snd_timer_start(timeri: cable->snd_timer.instance, ticks: 1); |
242 | if (err < 0) { |
243 | /* do not report error if trying to start but already |
244 | * running. For example called by opposite substream |
245 | * of the same cable |
246 | */ |
247 | if (err == -EBUSY) |
248 | return 0; |
249 | |
250 | pcm_err(dpcm->substream->pcm, |
251 | "snd_timer_start(%d,%d,%d) failed with %d" , |
252 | cable->snd_timer.id.card, |
253 | cable->snd_timer.id.device, |
254 | cable->snd_timer.id.subdevice, |
255 | err); |
256 | } |
257 | |
258 | return err; |
259 | } |
260 | |
261 | /* call in cable->lock */ |
262 | static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm) |
263 | { |
264 | del_timer(timer: &dpcm->timer); |
265 | dpcm->timer.expires = 0; |
266 | |
267 | return 0; |
268 | } |
269 | |
270 | /* call in cable->lock */ |
271 | static int loopback_snd_timer_stop(struct loopback_pcm *dpcm) |
272 | { |
273 | struct loopback_cable *cable = dpcm->cable; |
274 | int err; |
275 | |
276 | /* only stop if both devices (playback and capture) are not running */ |
277 | if (cable->running ^ cable->pause) |
278 | return 0; |
279 | |
280 | err = snd_timer_stop(timeri: cable->snd_timer.instance); |
281 | if (err < 0) { |
282 | pcm_err(dpcm->substream->pcm, |
283 | "snd_timer_stop(%d,%d,%d) failed with %d" , |
284 | cable->snd_timer.id.card, |
285 | cable->snd_timer.id.device, |
286 | cable->snd_timer.id.subdevice, |
287 | err); |
288 | } |
289 | |
290 | return err; |
291 | } |
292 | |
293 | static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm) |
294 | { |
295 | del_timer_sync(timer: &dpcm->timer); |
296 | |
297 | return 0; |
298 | } |
299 | |
300 | /* call in loopback->cable_lock */ |
301 | static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm) |
302 | { |
303 | struct loopback_cable *cable = dpcm->cable; |
304 | |
305 | /* snd_timer was not opened */ |
306 | if (!cable->snd_timer.instance) |
307 | return 0; |
308 | |
309 | /* will only be called from free_cable() when other stream was |
310 | * already closed. Other stream cannot be reopened as long as |
311 | * loopback->cable_lock is locked. Therefore no need to lock |
312 | * cable->lock; |
313 | */ |
314 | snd_timer_close(timeri: cable->snd_timer.instance); |
315 | |
316 | /* wait till drain work has finished if requested */ |
317 | cancel_work_sync(work: &cable->snd_timer.event_work); |
318 | |
319 | snd_timer_instance_free(timeri: cable->snd_timer.instance); |
320 | memset(&cable->snd_timer, 0, sizeof(cable->snd_timer)); |
321 | |
322 | return 0; |
323 | } |
324 | |
325 | static int loopback_check_format(struct loopback_cable *cable, int stream) |
326 | { |
327 | struct snd_pcm_runtime *runtime, *cruntime; |
328 | struct loopback_setup *setup; |
329 | struct snd_card *card; |
330 | int check; |
331 | |
332 | if (cable->valid != CABLE_VALID_BOTH) { |
333 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
334 | goto __notify; |
335 | return 0; |
336 | } |
337 | runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]-> |
338 | substream->runtime; |
339 | cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]-> |
340 | substream->runtime; |
341 | check = runtime->format != cruntime->format || |
342 | runtime->rate != cruntime->rate || |
343 | runtime->channels != cruntime->channels || |
344 | runtime->access != cruntime->access; |
345 | if (!check) |
346 | return 0; |
347 | if (stream == SNDRV_PCM_STREAM_CAPTURE) { |
348 | return -EIO; |
349 | } else { |
350 | snd_pcm_stop(substream: cable->streams[SNDRV_PCM_STREAM_CAPTURE]-> |
351 | substream, SNDRV_PCM_STATE_DRAINING); |
352 | __notify: |
353 | runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]-> |
354 | substream->runtime; |
355 | setup = get_setup(dpcm: cable->streams[SNDRV_PCM_STREAM_PLAYBACK]); |
356 | card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card; |
357 | if (setup->format != runtime->format) { |
358 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, |
359 | id: &setup->format_id); |
360 | setup->format = runtime->format; |
361 | } |
362 | if (setup->rate != runtime->rate) { |
363 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, |
364 | id: &setup->rate_id); |
365 | setup->rate = runtime->rate; |
366 | } |
367 | if (setup->channels != runtime->channels) { |
368 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, |
369 | id: &setup->channels_id); |
370 | setup->channels = runtime->channels; |
371 | } |
372 | if (setup->access != runtime->access) { |
373 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, |
374 | id: &setup->access_id); |
375 | setup->access = runtime->access; |
376 | } |
377 | } |
378 | return 0; |
379 | } |
380 | |
381 | static void loopback_active_notify(struct loopback_pcm *dpcm) |
382 | { |
383 | snd_ctl_notify(card: dpcm->loopback->card, |
384 | SNDRV_CTL_EVENT_MASK_VALUE, |
385 | id: &get_setup(dpcm)->active_id); |
386 | } |
387 | |
388 | static int loopback_trigger(struct snd_pcm_substream *substream, int cmd) |
389 | { |
390 | struct snd_pcm_runtime *runtime = substream->runtime; |
391 | struct loopback_pcm *dpcm = runtime->private_data; |
392 | struct loopback_cable *cable = dpcm->cable; |
393 | int err = 0, stream = 1 << substream->stream; |
394 | |
395 | switch (cmd) { |
396 | case SNDRV_PCM_TRIGGER_START: |
397 | err = loopback_check_format(cable, stream: substream->stream); |
398 | if (err < 0) |
399 | return err; |
400 | dpcm->last_jiffies = jiffies; |
401 | dpcm->pcm_rate_shift = 0; |
402 | dpcm->last_drift = 0; |
403 | spin_lock(lock: &cable->lock); |
404 | cable->running |= stream; |
405 | cable->pause &= ~stream; |
406 | err = cable->ops->start(dpcm); |
407 | spin_unlock(lock: &cable->lock); |
408 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
409 | loopback_active_notify(dpcm); |
410 | break; |
411 | case SNDRV_PCM_TRIGGER_STOP: |
412 | spin_lock(lock: &cable->lock); |
413 | cable->running &= ~stream; |
414 | cable->pause &= ~stream; |
415 | err = cable->ops->stop(dpcm); |
416 | spin_unlock(lock: &cable->lock); |
417 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
418 | loopback_active_notify(dpcm); |
419 | break; |
420 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
421 | case SNDRV_PCM_TRIGGER_SUSPEND: |
422 | spin_lock(lock: &cable->lock); |
423 | cable->pause |= stream; |
424 | err = cable->ops->stop(dpcm); |
425 | spin_unlock(lock: &cable->lock); |
426 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
427 | loopback_active_notify(dpcm); |
428 | break; |
429 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
430 | case SNDRV_PCM_TRIGGER_RESUME: |
431 | spin_lock(lock: &cable->lock); |
432 | dpcm->last_jiffies = jiffies; |
433 | cable->pause &= ~stream; |
434 | err = cable->ops->start(dpcm); |
435 | spin_unlock(lock: &cable->lock); |
436 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
437 | loopback_active_notify(dpcm); |
438 | break; |
439 | default: |
440 | return -EINVAL; |
441 | } |
442 | return err; |
443 | } |
444 | |
445 | static void params_change(struct snd_pcm_substream *substream) |
446 | { |
447 | struct snd_pcm_runtime *runtime = substream->runtime; |
448 | struct loopback_pcm *dpcm = runtime->private_data; |
449 | struct loopback_cable *cable = dpcm->cable; |
450 | |
451 | cable->hw.formats = pcm_format_to_bits(pcm_format: runtime->format); |
452 | cable->hw.rate_min = runtime->rate; |
453 | cable->hw.rate_max = runtime->rate; |
454 | cable->hw.channels_min = runtime->channels; |
455 | cable->hw.channels_max = runtime->channels; |
456 | |
457 | if (cable->snd_timer.instance) { |
458 | cable->hw.period_bytes_min = |
459 | frames_to_bytes(runtime, size: runtime->period_size); |
460 | cable->hw.period_bytes_max = cable->hw.period_bytes_min; |
461 | } |
462 | |
463 | } |
464 | |
465 | static int loopback_prepare(struct snd_pcm_substream *substream) |
466 | { |
467 | struct snd_pcm_runtime *runtime = substream->runtime; |
468 | struct loopback_pcm *dpcm = runtime->private_data; |
469 | struct loopback_cable *cable = dpcm->cable; |
470 | int err, bps, salign; |
471 | |
472 | if (cable->ops->stop_sync) { |
473 | err = cable->ops->stop_sync(dpcm); |
474 | if (err < 0) |
475 | return err; |
476 | } |
477 | |
478 | salign = (snd_pcm_format_physical_width(format: runtime->format) * |
479 | runtime->channels) / 8; |
480 | bps = salign * runtime->rate; |
481 | if (bps <= 0 || salign <= 0) |
482 | return -EINVAL; |
483 | |
484 | dpcm->buf_pos = 0; |
485 | dpcm->pcm_buffer_size = frames_to_bytes(runtime, size: runtime->buffer_size); |
486 | dpcm->channel_buf_n = dpcm->pcm_buffer_size / runtime->channels; |
487 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
488 | /* clear capture buffer */ |
489 | dpcm->silent_size = dpcm->pcm_buffer_size; |
490 | snd_pcm_format_set_silence(format: runtime->format, buf: runtime->dma_area, |
491 | frames: runtime->buffer_size * runtime->channels); |
492 | } |
493 | |
494 | dpcm->irq_pos = 0; |
495 | dpcm->period_update_pending = 0; |
496 | dpcm->pcm_bps = bps; |
497 | dpcm->pcm_salign = salign; |
498 | dpcm->pcm_period_size = frames_to_bytes(runtime, size: runtime->period_size); |
499 | |
500 | mutex_lock(&dpcm->loopback->cable_lock); |
501 | if (!(cable->valid & ~(1 << substream->stream)) || |
502 | (get_setup(dpcm)->notify && |
503 | substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) |
504 | params_change(substream); |
505 | cable->valid |= 1 << substream->stream; |
506 | mutex_unlock(lock: &dpcm->loopback->cable_lock); |
507 | |
508 | return 0; |
509 | } |
510 | |
511 | static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes) |
512 | { |
513 | struct snd_pcm_runtime *runtime = dpcm->substream->runtime; |
514 | char *dst = runtime->dma_area; |
515 | unsigned int dst_off = dpcm->buf_pos; |
516 | |
517 | if (dpcm->silent_size >= dpcm->pcm_buffer_size) |
518 | return; |
519 | if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size) |
520 | bytes = dpcm->pcm_buffer_size - dpcm->silent_size; |
521 | |
522 | for (;;) { |
523 | unsigned int size = bytes; |
524 | if (dst_off + size > dpcm->pcm_buffer_size) |
525 | size = dpcm->pcm_buffer_size - dst_off; |
526 | snd_pcm_format_set_silence(format: runtime->format, buf: dst + dst_off, |
527 | frames: bytes_to_frames(runtime, size) * |
528 | runtime->channels); |
529 | dpcm->silent_size += size; |
530 | bytes -= size; |
531 | if (!bytes) |
532 | break; |
533 | dst_off = 0; |
534 | } |
535 | } |
536 | |
537 | static void copy_play_buf_part_n(struct loopback_pcm *play, struct loopback_pcm *capt, |
538 | unsigned int size, unsigned int src_off, unsigned int dst_off) |
539 | { |
540 | unsigned int channels = capt->substream->runtime->channels; |
541 | unsigned int size_p_ch = size / channels; |
542 | unsigned int src_off_ch = src_off / channels; |
543 | unsigned int dst_off_ch = dst_off / channels; |
544 | int i; |
545 | |
546 | for (i = 0; i < channels; i++) { |
547 | memcpy(capt->substream->runtime->dma_area + capt->channel_buf_n * i + dst_off_ch, |
548 | play->substream->runtime->dma_area + play->channel_buf_n * i + src_off_ch, |
549 | size_p_ch); |
550 | } |
551 | } |
552 | |
553 | static void copy_play_buf(struct loopback_pcm *play, |
554 | struct loopback_pcm *capt, |
555 | unsigned int bytes) |
556 | { |
557 | struct snd_pcm_runtime *runtime = play->substream->runtime; |
558 | char *src = runtime->dma_area; |
559 | char *dst = capt->substream->runtime->dma_area; |
560 | unsigned int src_off = play->buf_pos; |
561 | unsigned int dst_off = capt->buf_pos; |
562 | unsigned int clear_bytes = 0; |
563 | |
564 | /* check if playback is draining, trim the capture copy size |
565 | * when our pointer is at the end of playback ring buffer */ |
566 | if (runtime->state == SNDRV_PCM_STATE_DRAINING && |
567 | snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) { |
568 | snd_pcm_uframes_t appl_ptr, appl_ptr1, diff; |
569 | appl_ptr = appl_ptr1 = runtime->control->appl_ptr; |
570 | appl_ptr1 -= appl_ptr1 % runtime->buffer_size; |
571 | appl_ptr1 += play->buf_pos / play->pcm_salign; |
572 | if (appl_ptr < appl_ptr1) |
573 | appl_ptr1 -= runtime->buffer_size; |
574 | diff = (appl_ptr - appl_ptr1) * play->pcm_salign; |
575 | if (diff < bytes) { |
576 | clear_bytes = bytes - diff; |
577 | bytes = diff; |
578 | } |
579 | } |
580 | |
581 | for (;;) { |
582 | unsigned int size = bytes; |
583 | if (src_off + size > play->pcm_buffer_size) |
584 | size = play->pcm_buffer_size - src_off; |
585 | if (dst_off + size > capt->pcm_buffer_size) |
586 | size = capt->pcm_buffer_size - dst_off; |
587 | if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED || |
588 | runtime->access == SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED) |
589 | copy_play_buf_part_n(play, capt, size, src_off, dst_off); |
590 | else |
591 | memcpy(dst + dst_off, src + src_off, size); |
592 | capt->silent_size = 0; |
593 | bytes -= size; |
594 | if (!bytes) |
595 | break; |
596 | src_off = (src_off + size) % play->pcm_buffer_size; |
597 | dst_off = (dst_off + size) % capt->pcm_buffer_size; |
598 | } |
599 | |
600 | if (clear_bytes > 0) { |
601 | clear_capture_buf(dpcm: capt, bytes: clear_bytes); |
602 | capt->silent_size = 0; |
603 | } |
604 | } |
605 | |
606 | static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm, |
607 | unsigned int jiffies_delta) |
608 | { |
609 | unsigned long last_pos; |
610 | unsigned int delta; |
611 | |
612 | last_pos = byte_pos(dpcm, x: dpcm->irq_pos); |
613 | dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps; |
614 | delta = byte_pos(dpcm, x: dpcm->irq_pos) - last_pos; |
615 | if (delta >= dpcm->last_drift) |
616 | delta -= dpcm->last_drift; |
617 | dpcm->last_drift = 0; |
618 | if (dpcm->irq_pos >= dpcm->period_size_frac) { |
619 | dpcm->irq_pos %= dpcm->period_size_frac; |
620 | dpcm->period_update_pending = 1; |
621 | } |
622 | return delta; |
623 | } |
624 | |
625 | static inline void bytepos_finish(struct loopback_pcm *dpcm, |
626 | unsigned int delta) |
627 | { |
628 | dpcm->buf_pos += delta; |
629 | dpcm->buf_pos %= dpcm->pcm_buffer_size; |
630 | } |
631 | |
632 | /* call in cable->lock */ |
633 | static unsigned int loopback_jiffies_timer_pos_update |
634 | (struct loopback_cable *cable) |
635 | { |
636 | struct loopback_pcm *dpcm_play = |
637 | cable->streams[SNDRV_PCM_STREAM_PLAYBACK]; |
638 | struct loopback_pcm *dpcm_capt = |
639 | cable->streams[SNDRV_PCM_STREAM_CAPTURE]; |
640 | unsigned long delta_play = 0, delta_capt = 0, cur_jiffies; |
641 | unsigned int running, count1, count2; |
642 | |
643 | cur_jiffies = jiffies; |
644 | running = cable->running ^ cable->pause; |
645 | if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) { |
646 | delta_play = cur_jiffies - dpcm_play->last_jiffies; |
647 | dpcm_play->last_jiffies += delta_play; |
648 | } |
649 | |
650 | if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) { |
651 | delta_capt = cur_jiffies - dpcm_capt->last_jiffies; |
652 | dpcm_capt->last_jiffies += delta_capt; |
653 | } |
654 | |
655 | if (delta_play == 0 && delta_capt == 0) |
656 | goto unlock; |
657 | |
658 | if (delta_play > delta_capt) { |
659 | count1 = bytepos_delta(dpcm: dpcm_play, jiffies_delta: delta_play - delta_capt); |
660 | bytepos_finish(dpcm: dpcm_play, delta: count1); |
661 | delta_play = delta_capt; |
662 | } else if (delta_play < delta_capt) { |
663 | count1 = bytepos_delta(dpcm: dpcm_capt, jiffies_delta: delta_capt - delta_play); |
664 | clear_capture_buf(dpcm: dpcm_capt, bytes: count1); |
665 | bytepos_finish(dpcm: dpcm_capt, delta: count1); |
666 | delta_capt = delta_play; |
667 | } |
668 | |
669 | if (delta_play == 0 && delta_capt == 0) |
670 | goto unlock; |
671 | |
672 | /* note delta_capt == delta_play at this moment */ |
673 | count1 = bytepos_delta(dpcm: dpcm_play, jiffies_delta: delta_play); |
674 | count2 = bytepos_delta(dpcm: dpcm_capt, jiffies_delta: delta_capt); |
675 | if (count1 < count2) { |
676 | dpcm_capt->last_drift = count2 - count1; |
677 | count1 = count2; |
678 | } else if (count1 > count2) { |
679 | dpcm_play->last_drift = count1 - count2; |
680 | } |
681 | copy_play_buf(play: dpcm_play, capt: dpcm_capt, bytes: count1); |
682 | bytepos_finish(dpcm: dpcm_play, delta: count1); |
683 | bytepos_finish(dpcm: dpcm_capt, delta: count1); |
684 | unlock: |
685 | return running; |
686 | } |
687 | |
688 | static void loopback_jiffies_timer_function(struct timer_list *t) |
689 | { |
690 | struct loopback_pcm *dpcm = from_timer(dpcm, t, timer); |
691 | unsigned long flags; |
692 | |
693 | spin_lock_irqsave(&dpcm->cable->lock, flags); |
694 | if (loopback_jiffies_timer_pos_update(cable: dpcm->cable) & |
695 | (1 << dpcm->substream->stream)) { |
696 | loopback_jiffies_timer_start(dpcm); |
697 | if (dpcm->period_update_pending) { |
698 | dpcm->period_update_pending = 0; |
699 | spin_unlock_irqrestore(lock: &dpcm->cable->lock, flags); |
700 | /* need to unlock before calling below */ |
701 | snd_pcm_period_elapsed(substream: dpcm->substream); |
702 | return; |
703 | } |
704 | } |
705 | spin_unlock_irqrestore(lock: &dpcm->cable->lock, flags); |
706 | } |
707 | |
708 | /* call in cable->lock */ |
709 | static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime, |
710 | unsigned long resolution) |
711 | { |
712 | if (resolution != runtime->timer_resolution) { |
713 | struct loopback_pcm *dpcm = runtime->private_data; |
714 | struct loopback_cable *cable = dpcm->cable; |
715 | /* Worst case estimation of possible values for resolution |
716 | * resolution <= (512 * 1024) frames / 8kHz in nsec |
717 | * resolution <= 65.536.000.000 nsec |
718 | * |
719 | * period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz + |
720 | * 500.000 |
721 | * period_size <= 12.582.912.000.000 <64bit |
722 | * / 1.000.000 usec/sec |
723 | */ |
724 | snd_pcm_uframes_t period_size_usec = |
725 | resolution / 1000 * runtime->rate; |
726 | /* round to nearest sample rate */ |
727 | snd_pcm_uframes_t period_size = |
728 | (period_size_usec + 500 * 1000) / (1000 * 1000); |
729 | |
730 | pcm_err(dpcm->substream->pcm, |
731 | "Period size (%lu frames) of loopback device is not corresponding to timer resolution (%lu nsec = %lu frames) of card timer %d,%d,%d. Use period size of %lu frames for loopback device." , |
732 | runtime->period_size, resolution, period_size, |
733 | cable->snd_timer.id.card, |
734 | cable->snd_timer.id.device, |
735 | cable->snd_timer.id.subdevice, |
736 | period_size); |
737 | return -EINVAL; |
738 | } |
739 | return 0; |
740 | } |
741 | |
742 | static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable, |
743 | int event, |
744 | unsigned long resolution) |
745 | { |
746 | struct loopback_pcm *dpcm_play, *dpcm_capt; |
747 | struct snd_pcm_substream *substream_play, *substream_capt; |
748 | struct snd_pcm_runtime *valid_runtime; |
749 | unsigned int running, elapsed_bytes; |
750 | unsigned long flags; |
751 | |
752 | spin_lock_irqsave(&cable->lock, flags); |
753 | running = cable->running ^ cable->pause; |
754 | /* no need to do anything if no stream is running */ |
755 | if (!running) { |
756 | spin_unlock_irqrestore(lock: &cable->lock, flags); |
757 | return; |
758 | } |
759 | |
760 | dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]; |
761 | dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE]; |
762 | |
763 | if (event == SNDRV_TIMER_EVENT_MSTOP) { |
764 | if (!dpcm_play || |
765 | dpcm_play->substream->runtime->state != |
766 | SNDRV_PCM_STATE_DRAINING) { |
767 | spin_unlock_irqrestore(lock: &cable->lock, flags); |
768 | return; |
769 | } |
770 | } |
771 | |
772 | substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? |
773 | dpcm_play->substream : NULL; |
774 | substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ? |
775 | dpcm_capt->substream : NULL; |
776 | valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? |
777 | dpcm_play->substream->runtime : |
778 | dpcm_capt->substream->runtime; |
779 | |
780 | /* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */ |
781 | if (event == SNDRV_TIMER_EVENT_TICK) { |
782 | /* The hardware rules guarantee that playback and capture period |
783 | * are the same. Therefore only one device has to be checked |
784 | * here. |
785 | */ |
786 | if (loopback_snd_timer_check_resolution(runtime: valid_runtime, |
787 | resolution) < 0) { |
788 | spin_unlock_irqrestore(lock: &cable->lock, flags); |
789 | if (substream_play) |
790 | snd_pcm_stop_xrun(substream: substream_play); |
791 | if (substream_capt) |
792 | snd_pcm_stop_xrun(substream: substream_capt); |
793 | return; |
794 | } |
795 | } |
796 | |
797 | elapsed_bytes = frames_to_bytes(runtime: valid_runtime, |
798 | size: valid_runtime->period_size); |
799 | /* The same timer interrupt is used for playback and capture device */ |
800 | if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) && |
801 | (running & (1 << SNDRV_PCM_STREAM_CAPTURE))) { |
802 | copy_play_buf(play: dpcm_play, capt: dpcm_capt, bytes: elapsed_bytes); |
803 | bytepos_finish(dpcm: dpcm_play, delta: elapsed_bytes); |
804 | bytepos_finish(dpcm: dpcm_capt, delta: elapsed_bytes); |
805 | } else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) { |
806 | bytepos_finish(dpcm: dpcm_play, delta: elapsed_bytes); |
807 | } else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) { |
808 | clear_capture_buf(dpcm: dpcm_capt, bytes: elapsed_bytes); |
809 | bytepos_finish(dpcm: dpcm_capt, delta: elapsed_bytes); |
810 | } |
811 | spin_unlock_irqrestore(lock: &cable->lock, flags); |
812 | |
813 | if (substream_play) |
814 | snd_pcm_period_elapsed(substream: substream_play); |
815 | if (substream_capt) |
816 | snd_pcm_period_elapsed(substream: substream_capt); |
817 | } |
818 | |
819 | static void loopback_snd_timer_function(struct snd_timer_instance *timeri, |
820 | unsigned long resolution, |
821 | unsigned long ticks) |
822 | { |
823 | struct loopback_cable *cable = timeri->callback_data; |
824 | |
825 | loopback_snd_timer_period_elapsed(cable, event: SNDRV_TIMER_EVENT_TICK, |
826 | resolution); |
827 | } |
828 | |
829 | static void loopback_snd_timer_work(struct work_struct *work) |
830 | { |
831 | struct loopback_cable *cable; |
832 | |
833 | cable = container_of(work, struct loopback_cable, snd_timer.event_work); |
834 | loopback_snd_timer_period_elapsed(cable, event: SNDRV_TIMER_EVENT_MSTOP, resolution: 0); |
835 | } |
836 | |
837 | static void loopback_snd_timer_event(struct snd_timer_instance *timeri, |
838 | int event, |
839 | struct timespec64 *tstamp, |
840 | unsigned long resolution) |
841 | { |
842 | /* Do not lock cable->lock here because timer->lock is already hold. |
843 | * There are other functions which first lock cable->lock and than |
844 | * timer->lock e.g. |
845 | * loopback_trigger() |
846 | * spin_lock(&cable->lock) |
847 | * loopback_snd_timer_start() |
848 | * snd_timer_start() |
849 | * spin_lock(&timer->lock) |
850 | * Therefore when using the oposit order of locks here it could result |
851 | * in a deadlock. |
852 | */ |
853 | |
854 | if (event == SNDRV_TIMER_EVENT_MSTOP) { |
855 | struct loopback_cable *cable = timeri->callback_data; |
856 | |
857 | /* sound card of the timer was stopped. Therefore there will not |
858 | * be any further timer callbacks. Due to this forward audio |
859 | * data from here if in draining state. When still in running |
860 | * state the streaming will be aborted by the usual timeout. It |
861 | * should not be aborted here because may be the timer sound |
862 | * card does only a recovery and the timer is back soon. |
863 | * This work triggers loopback_snd_timer_work() |
864 | */ |
865 | schedule_work(work: &cable->snd_timer.event_work); |
866 | } |
867 | } |
868 | |
869 | static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm, |
870 | struct snd_info_buffer *buffer) |
871 | { |
872 | snd_iprintf(buffer, " update_pending:\t%u\n" , |
873 | dpcm->period_update_pending); |
874 | snd_iprintf(buffer, " irq_pos:\t\t%u\n" , dpcm->irq_pos); |
875 | snd_iprintf(buffer, " period_frac:\t%u\n" , dpcm->period_size_frac); |
876 | snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n" , |
877 | dpcm->last_jiffies, jiffies); |
878 | snd_iprintf(buffer, " timer_expires:\t%lu\n" , dpcm->timer.expires); |
879 | } |
880 | |
881 | static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm, |
882 | struct snd_info_buffer *buffer) |
883 | { |
884 | struct loopback_cable *cable = dpcm->cable; |
885 | |
886 | snd_iprintf(buffer, " sound timer:\thw:%d,%d,%d\n" , |
887 | cable->snd_timer.id.card, |
888 | cable->snd_timer.id.device, |
889 | cable->snd_timer.id.subdevice); |
890 | snd_iprintf(buffer, " timer open:\t\t%s\n" , |
891 | (cable->snd_timer.stream == SNDRV_PCM_STREAM_CAPTURE) ? |
892 | "capture" : "playback" ); |
893 | } |
894 | |
895 | static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream) |
896 | { |
897 | struct snd_pcm_runtime *runtime = substream->runtime; |
898 | struct loopback_pcm *dpcm = runtime->private_data; |
899 | snd_pcm_uframes_t pos; |
900 | |
901 | spin_lock(lock: &dpcm->cable->lock); |
902 | if (dpcm->cable->ops->pos_update) |
903 | dpcm->cable->ops->pos_update(dpcm->cable); |
904 | pos = dpcm->buf_pos; |
905 | spin_unlock(lock: &dpcm->cable->lock); |
906 | return bytes_to_frames(runtime, size: pos); |
907 | } |
908 | |
909 | static const struct snd_pcm_hardware loopback_pcm_hardware = |
910 | { |
911 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP | |
912 | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE | |
913 | SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_NONINTERLEAVED), |
914 | .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | |
915 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | |
916 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | |
917 | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE | |
918 | SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE), |
919 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000, |
920 | .rate_min = 8000, |
921 | .rate_max = 192000, |
922 | .channels_min = 1, |
923 | .channels_max = 32, |
924 | .buffer_bytes_max = 2 * 1024 * 1024, |
925 | .period_bytes_min = 64, |
926 | /* note check overflow in frac_pos() using pcm_rate_shift before |
927 | changing period_bytes_max value */ |
928 | .period_bytes_max = 1024 * 1024, |
929 | .periods_min = 1, |
930 | .periods_max = 1024, |
931 | .fifo_size = 0, |
932 | }; |
933 | |
934 | static void loopback_runtime_free(struct snd_pcm_runtime *runtime) |
935 | { |
936 | struct loopback_pcm *dpcm = runtime->private_data; |
937 | kfree(objp: dpcm); |
938 | } |
939 | |
940 | static int loopback_hw_free(struct snd_pcm_substream *substream) |
941 | { |
942 | struct snd_pcm_runtime *runtime = substream->runtime; |
943 | struct loopback_pcm *dpcm = runtime->private_data; |
944 | struct loopback_cable *cable = dpcm->cable; |
945 | |
946 | mutex_lock(&dpcm->loopback->cable_lock); |
947 | cable->valid &= ~(1 << substream->stream); |
948 | mutex_unlock(lock: &dpcm->loopback->cable_lock); |
949 | return 0; |
950 | } |
951 | |
952 | static unsigned int get_cable_index(struct snd_pcm_substream *substream) |
953 | { |
954 | if (!substream->pcm->device) |
955 | return substream->stream; |
956 | else |
957 | return !substream->stream; |
958 | } |
959 | |
960 | static int rule_format(struct snd_pcm_hw_params *params, |
961 | struct snd_pcm_hw_rule *rule) |
962 | { |
963 | struct loopback_pcm *dpcm = rule->private; |
964 | struct loopback_cable *cable = dpcm->cable; |
965 | struct snd_mask m; |
966 | |
967 | snd_mask_none(mask: &m); |
968 | mutex_lock(&dpcm->loopback->cable_lock); |
969 | m.bits[0] = (u_int32_t)cable->hw.formats; |
970 | m.bits[1] = (u_int32_t)(cable->hw.formats >> 32); |
971 | mutex_unlock(lock: &dpcm->loopback->cable_lock); |
972 | return snd_mask_refine(mask: hw_param_mask(params, var: rule->var), v: &m); |
973 | } |
974 | |
975 | static int rule_rate(struct snd_pcm_hw_params *params, |
976 | struct snd_pcm_hw_rule *rule) |
977 | { |
978 | struct loopback_pcm *dpcm = rule->private; |
979 | struct loopback_cable *cable = dpcm->cable; |
980 | struct snd_interval t; |
981 | |
982 | mutex_lock(&dpcm->loopback->cable_lock); |
983 | t.min = cable->hw.rate_min; |
984 | t.max = cable->hw.rate_max; |
985 | mutex_unlock(lock: &dpcm->loopback->cable_lock); |
986 | t.openmin = t.openmax = 0; |
987 | t.integer = 0; |
988 | return snd_interval_refine(i: hw_param_interval(params, var: rule->var), v: &t); |
989 | } |
990 | |
991 | static int rule_channels(struct snd_pcm_hw_params *params, |
992 | struct snd_pcm_hw_rule *rule) |
993 | { |
994 | struct loopback_pcm *dpcm = rule->private; |
995 | struct loopback_cable *cable = dpcm->cable; |
996 | struct snd_interval t; |
997 | |
998 | mutex_lock(&dpcm->loopback->cable_lock); |
999 | t.min = cable->hw.channels_min; |
1000 | t.max = cable->hw.channels_max; |
1001 | mutex_unlock(lock: &dpcm->loopback->cable_lock); |
1002 | t.openmin = t.openmax = 0; |
1003 | t.integer = 0; |
1004 | return snd_interval_refine(i: hw_param_interval(params, var: rule->var), v: &t); |
1005 | } |
1006 | |
1007 | static int rule_period_bytes(struct snd_pcm_hw_params *params, |
1008 | struct snd_pcm_hw_rule *rule) |
1009 | { |
1010 | struct loopback_pcm *dpcm = rule->private; |
1011 | struct loopback_cable *cable = dpcm->cable; |
1012 | struct snd_interval t; |
1013 | |
1014 | mutex_lock(&dpcm->loopback->cable_lock); |
1015 | t.min = cable->hw.period_bytes_min; |
1016 | t.max = cable->hw.period_bytes_max; |
1017 | mutex_unlock(lock: &dpcm->loopback->cable_lock); |
1018 | t.openmin = 0; |
1019 | t.openmax = 0; |
1020 | t.integer = 0; |
1021 | return snd_interval_refine(i: hw_param_interval(params, var: rule->var), v: &t); |
1022 | } |
1023 | |
1024 | static void free_cable(struct snd_pcm_substream *substream) |
1025 | { |
1026 | struct loopback *loopback = substream->private_data; |
1027 | int dev = get_cable_index(substream); |
1028 | struct loopback_cable *cable; |
1029 | |
1030 | cable = loopback->cables[substream->number][dev]; |
1031 | if (!cable) |
1032 | return; |
1033 | if (cable->streams[!substream->stream]) { |
1034 | /* other stream is still alive */ |
1035 | spin_lock_irq(lock: &cable->lock); |
1036 | cable->streams[substream->stream] = NULL; |
1037 | spin_unlock_irq(lock: &cable->lock); |
1038 | } else { |
1039 | struct loopback_pcm *dpcm = substream->runtime->private_data; |
1040 | |
1041 | if (cable->ops && cable->ops->close_cable && dpcm) |
1042 | cable->ops->close_cable(dpcm); |
1043 | /* free the cable */ |
1044 | loopback->cables[substream->number][dev] = NULL; |
1045 | kfree(objp: cable); |
1046 | } |
1047 | } |
1048 | |
1049 | static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm) |
1050 | { |
1051 | timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0); |
1052 | |
1053 | return 0; |
1054 | } |
1055 | |
1056 | static const struct loopback_ops loopback_jiffies_timer_ops = { |
1057 | .open = loopback_jiffies_timer_open, |
1058 | .start = loopback_jiffies_timer_start, |
1059 | .stop = loopback_jiffies_timer_stop, |
1060 | .stop_sync = loopback_jiffies_timer_stop_sync, |
1061 | .close_substream = loopback_jiffies_timer_stop_sync, |
1062 | .pos_update = loopback_jiffies_timer_pos_update, |
1063 | .dpcm_info = loopback_jiffies_timer_dpcm_info, |
1064 | }; |
1065 | |
1066 | static int loopback_parse_timer_id(const char *str, |
1067 | struct snd_timer_id *tid) |
1068 | { |
1069 | /* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */ |
1070 | const char * const sep_dev = ".," ; |
1071 | const char * const sep_pref = ":" ; |
1072 | const char *name = str; |
1073 | char *sep, save = '\0'; |
1074 | int card_idx = 0, dev = 0, subdev = 0; |
1075 | int err; |
1076 | |
1077 | sep = strpbrk(str, sep_pref); |
1078 | if (sep) |
1079 | name = sep + 1; |
1080 | sep = strpbrk(name, sep_dev); |
1081 | if (sep) { |
1082 | save = *sep; |
1083 | *sep = '\0'; |
1084 | } |
1085 | err = kstrtoint(s: name, base: 0, res: &card_idx); |
1086 | if (err == -EINVAL) { |
1087 | /* Must be the name, not number */ |
1088 | for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) { |
1089 | struct snd_card *card = snd_card_ref(card: card_idx); |
1090 | |
1091 | if (card) { |
1092 | if (!strcmp(card->id, name)) |
1093 | err = 0; |
1094 | snd_card_unref(card); |
1095 | } |
1096 | if (!err) |
1097 | break; |
1098 | } |
1099 | } |
1100 | if (sep) { |
1101 | *sep = save; |
1102 | if (!err) { |
1103 | char *sep2, save2 = '\0'; |
1104 | |
1105 | sep2 = strpbrk(sep + 1, sep_dev); |
1106 | if (sep2) { |
1107 | save2 = *sep2; |
1108 | *sep2 = '\0'; |
1109 | } |
1110 | err = kstrtoint(s: sep + 1, base: 0, res: &dev); |
1111 | if (sep2) { |
1112 | *sep2 = save2; |
1113 | if (!err) |
1114 | err = kstrtoint(s: sep2 + 1, base: 0, res: &subdev); |
1115 | } |
1116 | } |
1117 | } |
1118 | if (!err && tid) { |
1119 | tid->card = card_idx; |
1120 | tid->device = dev; |
1121 | tid->subdevice = subdev; |
1122 | } |
1123 | return err; |
1124 | } |
1125 | |
1126 | /* call in loopback->cable_lock */ |
1127 | static int loopback_snd_timer_open(struct loopback_pcm *dpcm) |
1128 | { |
1129 | int err = 0; |
1130 | struct snd_timer_id tid = { |
1131 | .dev_class = SNDRV_TIMER_CLASS_PCM, |
1132 | .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION, |
1133 | }; |
1134 | struct snd_timer_instance *timeri; |
1135 | struct loopback_cable *cable = dpcm->cable; |
1136 | |
1137 | /* check if timer was already opened. It is only opened once |
1138 | * per playback and capture subdevice (aka cable). |
1139 | */ |
1140 | if (cable->snd_timer.instance) |
1141 | goto exit; |
1142 | |
1143 | err = loopback_parse_timer_id(str: dpcm->loopback->timer_source, tid: &tid); |
1144 | if (err < 0) { |
1145 | pcm_err(dpcm->substream->pcm, |
1146 | "Parsing timer source \'%s\' failed with %d" , |
1147 | dpcm->loopback->timer_source, err); |
1148 | goto exit; |
1149 | } |
1150 | |
1151 | cable->snd_timer.stream = dpcm->substream->stream; |
1152 | cable->snd_timer.id = tid; |
1153 | |
1154 | timeri = snd_timer_instance_new(owner: dpcm->loopback->card->id); |
1155 | if (!timeri) { |
1156 | err = -ENOMEM; |
1157 | goto exit; |
1158 | } |
1159 | /* The callback has to be called from another work. If |
1160 | * SNDRV_TIMER_IFLG_FAST is specified it will be called from the |
1161 | * snd_pcm_period_elapsed() call of the selected sound card. |
1162 | * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave(). |
1163 | * Due to our callback loopback_snd_timer_function() also calls |
1164 | * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave(). |
1165 | * This would end up in a dead lock. |
1166 | */ |
1167 | timeri->flags |= SNDRV_TIMER_IFLG_AUTO; |
1168 | timeri->callback = loopback_snd_timer_function; |
1169 | timeri->callback_data = (void *)cable; |
1170 | timeri->ccallback = loopback_snd_timer_event; |
1171 | |
1172 | /* initialise a work used for draining */ |
1173 | INIT_WORK(&cable->snd_timer.event_work, loopback_snd_timer_work); |
1174 | |
1175 | /* The mutex loopback->cable_lock is kept locked. |
1176 | * Therefore snd_timer_open() cannot be called a second time |
1177 | * by the other device of the same cable. |
1178 | * Therefore the following issue cannot happen: |
1179 | * [proc1] Call loopback_timer_open() -> |
1180 | * Unlock cable->lock for snd_timer_close/open() call |
1181 | * [proc2] Call loopback_timer_open() -> snd_timer_open(), |
1182 | * snd_timer_start() |
1183 | * [proc1] Call snd_timer_open() and overwrite running timer |
1184 | * instance |
1185 | */ |
1186 | err = snd_timer_open(timeri, tid: &cable->snd_timer.id, current->pid); |
1187 | if (err < 0) { |
1188 | pcm_err(dpcm->substream->pcm, |
1189 | "snd_timer_open (%d,%d,%d) failed with %d" , |
1190 | cable->snd_timer.id.card, |
1191 | cable->snd_timer.id.device, |
1192 | cable->snd_timer.id.subdevice, |
1193 | err); |
1194 | snd_timer_instance_free(timeri); |
1195 | goto exit; |
1196 | } |
1197 | |
1198 | cable->snd_timer.instance = timeri; |
1199 | |
1200 | exit: |
1201 | return err; |
1202 | } |
1203 | |
1204 | /* stop_sync() is not required for sound timer because it does not need to be |
1205 | * restarted in loopback_prepare() on Xrun recovery |
1206 | */ |
1207 | static const struct loopback_ops loopback_snd_timer_ops = { |
1208 | .open = loopback_snd_timer_open, |
1209 | .start = loopback_snd_timer_start, |
1210 | .stop = loopback_snd_timer_stop, |
1211 | .close_cable = loopback_snd_timer_close_cable, |
1212 | .dpcm_info = loopback_snd_timer_dpcm_info, |
1213 | }; |
1214 | |
1215 | static int loopback_open(struct snd_pcm_substream *substream) |
1216 | { |
1217 | struct snd_pcm_runtime *runtime = substream->runtime; |
1218 | struct loopback *loopback = substream->private_data; |
1219 | struct loopback_pcm *dpcm; |
1220 | struct loopback_cable *cable = NULL; |
1221 | int err = 0; |
1222 | int dev = get_cable_index(substream); |
1223 | |
1224 | mutex_lock(&loopback->cable_lock); |
1225 | dpcm = kzalloc(size: sizeof(*dpcm), GFP_KERNEL); |
1226 | if (!dpcm) { |
1227 | err = -ENOMEM; |
1228 | goto unlock; |
1229 | } |
1230 | dpcm->loopback = loopback; |
1231 | dpcm->substream = substream; |
1232 | |
1233 | cable = loopback->cables[substream->number][dev]; |
1234 | if (!cable) { |
1235 | cable = kzalloc(size: sizeof(*cable), GFP_KERNEL); |
1236 | if (!cable) { |
1237 | err = -ENOMEM; |
1238 | goto unlock; |
1239 | } |
1240 | spin_lock_init(&cable->lock); |
1241 | cable->hw = loopback_pcm_hardware; |
1242 | if (loopback->timer_source) |
1243 | cable->ops = &loopback_snd_timer_ops; |
1244 | else |
1245 | cable->ops = &loopback_jiffies_timer_ops; |
1246 | loopback->cables[substream->number][dev] = cable; |
1247 | } |
1248 | dpcm->cable = cable; |
1249 | runtime->private_data = dpcm; |
1250 | |
1251 | if (cable->ops->open) { |
1252 | err = cable->ops->open(dpcm); |
1253 | if (err < 0) |
1254 | goto unlock; |
1255 | } |
1256 | |
1257 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
1258 | |
1259 | /* use dynamic rules based on actual runtime->hw values */ |
1260 | /* note that the default rules created in the PCM midlevel code */ |
1261 | /* are cached -> they do not reflect the actual state */ |
1262 | err = snd_pcm_hw_rule_add(runtime, cond: 0, |
1263 | SNDRV_PCM_HW_PARAM_FORMAT, |
1264 | func: rule_format, private: dpcm, |
1265 | SNDRV_PCM_HW_PARAM_FORMAT, -1); |
1266 | if (err < 0) |
1267 | goto unlock; |
1268 | err = snd_pcm_hw_rule_add(runtime, cond: 0, |
1269 | SNDRV_PCM_HW_PARAM_RATE, |
1270 | func: rule_rate, private: dpcm, |
1271 | SNDRV_PCM_HW_PARAM_RATE, -1); |
1272 | if (err < 0) |
1273 | goto unlock; |
1274 | err = snd_pcm_hw_rule_add(runtime, cond: 0, |
1275 | SNDRV_PCM_HW_PARAM_CHANNELS, |
1276 | func: rule_channels, private: dpcm, |
1277 | SNDRV_PCM_HW_PARAM_CHANNELS, -1); |
1278 | if (err < 0) |
1279 | goto unlock; |
1280 | |
1281 | /* In case of sound timer the period time of both devices of the same |
1282 | * loop has to be the same. |
1283 | * This rule only takes effect if a sound timer was chosen |
1284 | */ |
1285 | if (cable->snd_timer.instance) { |
1286 | err = snd_pcm_hw_rule_add(runtime, cond: 0, |
1287 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, |
1288 | func: rule_period_bytes, private: dpcm, |
1289 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1); |
1290 | if (err < 0) |
1291 | goto unlock; |
1292 | } |
1293 | |
1294 | /* loopback_runtime_free() has not to be called if kfree(dpcm) was |
1295 | * already called here. Otherwise it will end up with a double free. |
1296 | */ |
1297 | runtime->private_free = loopback_runtime_free; |
1298 | if (get_notify(dpcm)) |
1299 | runtime->hw = loopback_pcm_hardware; |
1300 | else |
1301 | runtime->hw = cable->hw; |
1302 | |
1303 | spin_lock_irq(lock: &cable->lock); |
1304 | cable->streams[substream->stream] = dpcm; |
1305 | spin_unlock_irq(lock: &cable->lock); |
1306 | |
1307 | unlock: |
1308 | if (err < 0) { |
1309 | free_cable(substream); |
1310 | kfree(objp: dpcm); |
1311 | } |
1312 | mutex_unlock(lock: &loopback->cable_lock); |
1313 | return err; |
1314 | } |
1315 | |
1316 | static int loopback_close(struct snd_pcm_substream *substream) |
1317 | { |
1318 | struct loopback *loopback = substream->private_data; |
1319 | struct loopback_pcm *dpcm = substream->runtime->private_data; |
1320 | int err = 0; |
1321 | |
1322 | if (dpcm->cable->ops->close_substream) |
1323 | err = dpcm->cable->ops->close_substream(dpcm); |
1324 | mutex_lock(&loopback->cable_lock); |
1325 | free_cable(substream); |
1326 | mutex_unlock(lock: &loopback->cable_lock); |
1327 | return err; |
1328 | } |
1329 | |
1330 | static const struct snd_pcm_ops loopback_pcm_ops = { |
1331 | .open = loopback_open, |
1332 | .close = loopback_close, |
1333 | .hw_free = loopback_hw_free, |
1334 | .prepare = loopback_prepare, |
1335 | .trigger = loopback_trigger, |
1336 | .pointer = loopback_pointer, |
1337 | }; |
1338 | |
1339 | static int loopback_pcm_new(struct loopback *loopback, |
1340 | int device, int substreams) |
1341 | { |
1342 | struct snd_pcm *pcm; |
1343 | int err; |
1344 | |
1345 | err = snd_pcm_new(card: loopback->card, id: "Loopback PCM" , device, |
1346 | playback_count: substreams, capture_count: substreams, rpcm: &pcm); |
1347 | if (err < 0) |
1348 | return err; |
1349 | snd_pcm_set_ops(pcm, direction: SNDRV_PCM_STREAM_PLAYBACK, ops: &loopback_pcm_ops); |
1350 | snd_pcm_set_ops(pcm, direction: SNDRV_PCM_STREAM_CAPTURE, ops: &loopback_pcm_ops); |
1351 | snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, size: 0, max: 0); |
1352 | |
1353 | pcm->private_data = loopback; |
1354 | pcm->info_flags = 0; |
1355 | strcpy(p: pcm->name, q: "Loopback PCM" ); |
1356 | |
1357 | loopback->pcm[device] = pcm; |
1358 | return 0; |
1359 | } |
1360 | |
1361 | static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol, |
1362 | struct snd_ctl_elem_info *uinfo) |
1363 | { |
1364 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
1365 | uinfo->count = 1; |
1366 | uinfo->value.integer.min = 80000; |
1367 | uinfo->value.integer.max = 120000; |
1368 | uinfo->value.integer.step = 1; |
1369 | return 0; |
1370 | } |
1371 | |
1372 | static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol, |
1373 | struct snd_ctl_elem_value *ucontrol) |
1374 | { |
1375 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1376 | |
1377 | mutex_lock(&loopback->cable_lock); |
1378 | ucontrol->value.integer.value[0] = |
1379 | loopback->setup[kcontrol->id.subdevice] |
1380 | [kcontrol->id.device].rate_shift; |
1381 | mutex_unlock(lock: &loopback->cable_lock); |
1382 | return 0; |
1383 | } |
1384 | |
1385 | static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol, |
1386 | struct snd_ctl_elem_value *ucontrol) |
1387 | { |
1388 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1389 | unsigned int val; |
1390 | int change = 0; |
1391 | |
1392 | val = ucontrol->value.integer.value[0]; |
1393 | if (val < 80000) |
1394 | val = 80000; |
1395 | if (val > 120000) |
1396 | val = 120000; |
1397 | mutex_lock(&loopback->cable_lock); |
1398 | if (val != loopback->setup[kcontrol->id.subdevice] |
1399 | [kcontrol->id.device].rate_shift) { |
1400 | loopback->setup[kcontrol->id.subdevice] |
1401 | [kcontrol->id.device].rate_shift = val; |
1402 | change = 1; |
1403 | } |
1404 | mutex_unlock(lock: &loopback->cable_lock); |
1405 | return change; |
1406 | } |
1407 | |
1408 | static int loopback_notify_get(struct snd_kcontrol *kcontrol, |
1409 | struct snd_ctl_elem_value *ucontrol) |
1410 | { |
1411 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1412 | |
1413 | mutex_lock(&loopback->cable_lock); |
1414 | ucontrol->value.integer.value[0] = |
1415 | loopback->setup[kcontrol->id.subdevice] |
1416 | [kcontrol->id.device].notify; |
1417 | mutex_unlock(lock: &loopback->cable_lock); |
1418 | return 0; |
1419 | } |
1420 | |
1421 | static int loopback_notify_put(struct snd_kcontrol *kcontrol, |
1422 | struct snd_ctl_elem_value *ucontrol) |
1423 | { |
1424 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1425 | unsigned int val; |
1426 | int change = 0; |
1427 | |
1428 | val = ucontrol->value.integer.value[0] ? 1 : 0; |
1429 | mutex_lock(&loopback->cable_lock); |
1430 | if (val != loopback->setup[kcontrol->id.subdevice] |
1431 | [kcontrol->id.device].notify) { |
1432 | loopback->setup[kcontrol->id.subdevice] |
1433 | [kcontrol->id.device].notify = val; |
1434 | change = 1; |
1435 | } |
1436 | mutex_unlock(lock: &loopback->cable_lock); |
1437 | return change; |
1438 | } |
1439 | |
1440 | static int loopback_active_get(struct snd_kcontrol *kcontrol, |
1441 | struct snd_ctl_elem_value *ucontrol) |
1442 | { |
1443 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1444 | struct loopback_cable *cable; |
1445 | |
1446 | unsigned int val = 0; |
1447 | |
1448 | mutex_lock(&loopback->cable_lock); |
1449 | cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1]; |
1450 | if (cable != NULL) { |
1451 | unsigned int running = cable->running ^ cable->pause; |
1452 | |
1453 | val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0; |
1454 | } |
1455 | mutex_unlock(lock: &loopback->cable_lock); |
1456 | ucontrol->value.integer.value[0] = val; |
1457 | return 0; |
1458 | } |
1459 | |
1460 | static int loopback_format_info(struct snd_kcontrol *kcontrol, |
1461 | struct snd_ctl_elem_info *uinfo) |
1462 | { |
1463 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
1464 | uinfo->count = 1; |
1465 | uinfo->value.integer.min = 0; |
1466 | uinfo->value.integer.max = (__force int)SNDRV_PCM_FORMAT_LAST; |
1467 | uinfo->value.integer.step = 1; |
1468 | return 0; |
1469 | } |
1470 | |
1471 | static int loopback_format_get(struct snd_kcontrol *kcontrol, |
1472 | struct snd_ctl_elem_value *ucontrol) |
1473 | { |
1474 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1475 | |
1476 | ucontrol->value.integer.value[0] = |
1477 | (__force int)loopback->setup[kcontrol->id.subdevice] |
1478 | [kcontrol->id.device].format; |
1479 | return 0; |
1480 | } |
1481 | |
1482 | static int loopback_rate_info(struct snd_kcontrol *kcontrol, |
1483 | struct snd_ctl_elem_info *uinfo) |
1484 | { |
1485 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
1486 | uinfo->count = 1; |
1487 | uinfo->value.integer.min = 0; |
1488 | uinfo->value.integer.max = 192000; |
1489 | uinfo->value.integer.step = 1; |
1490 | return 0; |
1491 | } |
1492 | |
1493 | static int loopback_rate_get(struct snd_kcontrol *kcontrol, |
1494 | struct snd_ctl_elem_value *ucontrol) |
1495 | { |
1496 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1497 | |
1498 | mutex_lock(&loopback->cable_lock); |
1499 | ucontrol->value.integer.value[0] = |
1500 | loopback->setup[kcontrol->id.subdevice] |
1501 | [kcontrol->id.device].rate; |
1502 | mutex_unlock(lock: &loopback->cable_lock); |
1503 | return 0; |
1504 | } |
1505 | |
1506 | static int loopback_channels_info(struct snd_kcontrol *kcontrol, |
1507 | struct snd_ctl_elem_info *uinfo) |
1508 | { |
1509 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
1510 | uinfo->count = 1; |
1511 | uinfo->value.integer.min = 1; |
1512 | uinfo->value.integer.max = 1024; |
1513 | uinfo->value.integer.step = 1; |
1514 | return 0; |
1515 | } |
1516 | |
1517 | static int loopback_channels_get(struct snd_kcontrol *kcontrol, |
1518 | struct snd_ctl_elem_value *ucontrol) |
1519 | { |
1520 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1521 | |
1522 | mutex_lock(&loopback->cable_lock); |
1523 | ucontrol->value.integer.value[0] = |
1524 | loopback->setup[kcontrol->id.subdevice] |
1525 | [kcontrol->id.device].channels; |
1526 | mutex_unlock(lock: &loopback->cable_lock); |
1527 | return 0; |
1528 | } |
1529 | |
1530 | static int loopback_access_info(struct snd_kcontrol *kcontrol, |
1531 | struct snd_ctl_elem_info *uinfo) |
1532 | { |
1533 | const char * const texts[] = {"Interleaved" , "Non-interleaved" }; |
1534 | |
1535 | return snd_ctl_enum_info(info: uinfo, channels: 1, ARRAY_SIZE(texts), names: texts); |
1536 | } |
1537 | |
1538 | static int loopback_access_get(struct snd_kcontrol *kcontrol, |
1539 | struct snd_ctl_elem_value *ucontrol) |
1540 | { |
1541 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
1542 | snd_pcm_access_t access; |
1543 | |
1544 | mutex_lock(&loopback->cable_lock); |
1545 | access = loopback->setup[kcontrol->id.subdevice][kcontrol->id.device].access; |
1546 | |
1547 | ucontrol->value.enumerated.item[0] = access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED || |
1548 | access == SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED; |
1549 | |
1550 | mutex_unlock(lock: &loopback->cable_lock); |
1551 | return 0; |
1552 | } |
1553 | |
1554 | static const struct snd_kcontrol_new loopback_controls[] = { |
1555 | { |
1556 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
1557 | .name = "PCM Rate Shift 100000" , |
1558 | .info = loopback_rate_shift_info, |
1559 | .get = loopback_rate_shift_get, |
1560 | .put = loopback_rate_shift_put, |
1561 | }, |
1562 | { |
1563 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
1564 | .name = "PCM Notify" , |
1565 | .info = snd_ctl_boolean_mono_info, |
1566 | .get = loopback_notify_get, |
1567 | .put = loopback_notify_put, |
1568 | }, |
1569 | #define ACTIVE_IDX 2 |
1570 | { |
1571 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
1572 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
1573 | .name = "PCM Slave Active" , |
1574 | .info = snd_ctl_boolean_mono_info, |
1575 | .get = loopback_active_get, |
1576 | }, |
1577 | #define FORMAT_IDX 3 |
1578 | { |
1579 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
1580 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
1581 | .name = "PCM Slave Format" , |
1582 | .info = loopback_format_info, |
1583 | .get = loopback_format_get |
1584 | }, |
1585 | #define RATE_IDX 4 |
1586 | { |
1587 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
1588 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
1589 | .name = "PCM Slave Rate" , |
1590 | .info = loopback_rate_info, |
1591 | .get = loopback_rate_get |
1592 | }, |
1593 | #define CHANNELS_IDX 5 |
1594 | { |
1595 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
1596 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
1597 | .name = "PCM Slave Channels" , |
1598 | .info = loopback_channels_info, |
1599 | .get = loopback_channels_get |
1600 | }, |
1601 | #define ACCESS_IDX 6 |
1602 | { |
1603 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
1604 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
1605 | .name = "PCM Slave Access Mode" , |
1606 | .info = loopback_access_info, |
1607 | .get = loopback_access_get, |
1608 | }, |
1609 | }; |
1610 | |
1611 | static int loopback_mixer_new(struct loopback *loopback, int notify) |
1612 | { |
1613 | struct snd_card *card = loopback->card; |
1614 | struct snd_pcm *pcm; |
1615 | struct snd_kcontrol *kctl; |
1616 | struct loopback_setup *setup; |
1617 | int err, dev, substr, substr_count, idx; |
1618 | |
1619 | strcpy(p: card->mixername, q: "Loopback Mixer" ); |
1620 | for (dev = 0; dev < 2; dev++) { |
1621 | pcm = loopback->pcm[dev]; |
1622 | substr_count = |
1623 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count; |
1624 | for (substr = 0; substr < substr_count; substr++) { |
1625 | setup = &loopback->setup[substr][dev]; |
1626 | setup->notify = notify; |
1627 | setup->rate_shift = NO_PITCH; |
1628 | setup->format = SNDRV_PCM_FORMAT_S16_LE; |
1629 | setup->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; |
1630 | setup->rate = 48000; |
1631 | setup->channels = 2; |
1632 | for (idx = 0; idx < ARRAY_SIZE(loopback_controls); |
1633 | idx++) { |
1634 | kctl = snd_ctl_new1(kcontrolnew: &loopback_controls[idx], |
1635 | private_data: loopback); |
1636 | if (!kctl) |
1637 | return -ENOMEM; |
1638 | kctl->id.device = dev; |
1639 | kctl->id.subdevice = substr; |
1640 | |
1641 | /* Add the control before copying the id so that |
1642 | * the numid field of the id is set in the copy. |
1643 | */ |
1644 | err = snd_ctl_add(card, kcontrol: kctl); |
1645 | if (err < 0) |
1646 | return err; |
1647 | |
1648 | switch (idx) { |
1649 | case ACTIVE_IDX: |
1650 | setup->active_id = kctl->id; |
1651 | break; |
1652 | case FORMAT_IDX: |
1653 | setup->format_id = kctl->id; |
1654 | break; |
1655 | case RATE_IDX: |
1656 | setup->rate_id = kctl->id; |
1657 | break; |
1658 | case CHANNELS_IDX: |
1659 | setup->channels_id = kctl->id; |
1660 | break; |
1661 | case ACCESS_IDX: |
1662 | setup->access_id = kctl->id; |
1663 | break; |
1664 | default: |
1665 | break; |
1666 | } |
1667 | } |
1668 | } |
1669 | } |
1670 | return 0; |
1671 | } |
1672 | |
1673 | static void print_dpcm_info(struct snd_info_buffer *buffer, |
1674 | struct loopback_pcm *dpcm, |
1675 | const char *id) |
1676 | { |
1677 | snd_iprintf(buffer, " %s\n" , id); |
1678 | if (dpcm == NULL) { |
1679 | snd_iprintf(buffer, " inactive\n" ); |
1680 | return; |
1681 | } |
1682 | snd_iprintf(buffer, " buffer_size:\t%u\n" , dpcm->pcm_buffer_size); |
1683 | snd_iprintf(buffer, " buffer_pos:\t\t%u\n" , dpcm->buf_pos); |
1684 | snd_iprintf(buffer, " silent_size:\t%u\n" , dpcm->silent_size); |
1685 | snd_iprintf(buffer, " period_size:\t%u\n" , dpcm->pcm_period_size); |
1686 | snd_iprintf(buffer, " bytes_per_sec:\t%u\n" , dpcm->pcm_bps); |
1687 | snd_iprintf(buffer, " sample_align:\t%u\n" , dpcm->pcm_salign); |
1688 | snd_iprintf(buffer, " rate_shift:\t\t%u\n" , dpcm->pcm_rate_shift); |
1689 | if (dpcm->cable->ops->dpcm_info) |
1690 | dpcm->cable->ops->dpcm_info(dpcm, buffer); |
1691 | } |
1692 | |
1693 | static void print_substream_info(struct snd_info_buffer *buffer, |
1694 | struct loopback *loopback, |
1695 | int sub, |
1696 | int num) |
1697 | { |
1698 | struct loopback_cable *cable = loopback->cables[sub][num]; |
1699 | |
1700 | snd_iprintf(buffer, "Cable %i substream %i:\n" , num, sub); |
1701 | if (cable == NULL) { |
1702 | snd_iprintf(buffer, " inactive\n" ); |
1703 | return; |
1704 | } |
1705 | snd_iprintf(buffer, " valid: %u\n" , cable->valid); |
1706 | snd_iprintf(buffer, " running: %u\n" , cable->running); |
1707 | snd_iprintf(buffer, " pause: %u\n" , cable->pause); |
1708 | print_dpcm_info(buffer, dpcm: cable->streams[0], id: "Playback" ); |
1709 | print_dpcm_info(buffer, dpcm: cable->streams[1], id: "Capture" ); |
1710 | } |
1711 | |
1712 | static void print_cable_info(struct snd_info_entry *entry, |
1713 | struct snd_info_buffer *buffer) |
1714 | { |
1715 | struct loopback *loopback = entry->private_data; |
1716 | int sub, num; |
1717 | |
1718 | mutex_lock(&loopback->cable_lock); |
1719 | num = entry->name[strlen(entry->name)-1]; |
1720 | num = num == '0' ? 0 : 1; |
1721 | for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++) |
1722 | print_substream_info(buffer, loopback, sub, num); |
1723 | mutex_unlock(lock: &loopback->cable_lock); |
1724 | } |
1725 | |
1726 | static int loopback_cable_proc_new(struct loopback *loopback, int cidx) |
1727 | { |
1728 | char name[32]; |
1729 | |
1730 | snprintf(buf: name, size: sizeof(name), fmt: "cable#%d" , cidx); |
1731 | return snd_card_ro_proc_new(card: loopback->card, name, private_data: loopback, |
1732 | read: print_cable_info); |
1733 | } |
1734 | |
1735 | static void loopback_set_timer_source(struct loopback *loopback, |
1736 | const char *value) |
1737 | { |
1738 | if (loopback->timer_source) { |
1739 | devm_kfree(dev: loopback->card->dev, p: loopback->timer_source); |
1740 | loopback->timer_source = NULL; |
1741 | } |
1742 | if (value && *value) |
1743 | loopback->timer_source = devm_kstrdup(dev: loopback->card->dev, |
1744 | s: value, GFP_KERNEL); |
1745 | } |
1746 | |
1747 | static void print_timer_source_info(struct snd_info_entry *entry, |
1748 | struct snd_info_buffer *buffer) |
1749 | { |
1750 | struct loopback *loopback = entry->private_data; |
1751 | |
1752 | mutex_lock(&loopback->cable_lock); |
1753 | snd_iprintf(buffer, "%s\n" , |
1754 | loopback->timer_source ? loopback->timer_source : "" ); |
1755 | mutex_unlock(lock: &loopback->cable_lock); |
1756 | } |
1757 | |
1758 | static void change_timer_source_info(struct snd_info_entry *entry, |
1759 | struct snd_info_buffer *buffer) |
1760 | { |
1761 | struct loopback *loopback = entry->private_data; |
1762 | char line[64]; |
1763 | |
1764 | mutex_lock(&loopback->cable_lock); |
1765 | if (!snd_info_get_line(buffer, line, len: sizeof(line))) |
1766 | loopback_set_timer_source(loopback, value: strim(line)); |
1767 | mutex_unlock(lock: &loopback->cable_lock); |
1768 | } |
1769 | |
1770 | static int loopback_timer_source_proc_new(struct loopback *loopback) |
1771 | { |
1772 | return snd_card_rw_proc_new(card: loopback->card, name: "timer_source" , private_data: loopback, |
1773 | read: print_timer_source_info, |
1774 | write: change_timer_source_info); |
1775 | } |
1776 | |
1777 | static int loopback_probe(struct platform_device *devptr) |
1778 | { |
1779 | struct snd_card *card; |
1780 | struct loopback *loopback; |
1781 | int dev = devptr->id; |
1782 | int err; |
1783 | |
1784 | err = snd_devm_card_new(parent: &devptr->dev, idx: index[dev], xid: id[dev], THIS_MODULE, |
1785 | extra_size: sizeof(struct loopback), card_ret: &card); |
1786 | if (err < 0) |
1787 | return err; |
1788 | loopback = card->private_data; |
1789 | |
1790 | if (pcm_substreams[dev] < 1) |
1791 | pcm_substreams[dev] = 1; |
1792 | if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) |
1793 | pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; |
1794 | |
1795 | loopback->card = card; |
1796 | loopback_set_timer_source(loopback, value: timer_source[dev]); |
1797 | |
1798 | mutex_init(&loopback->cable_lock); |
1799 | |
1800 | err = loopback_pcm_new(loopback, device: 0, substreams: pcm_substreams[dev]); |
1801 | if (err < 0) |
1802 | return err; |
1803 | err = loopback_pcm_new(loopback, device: 1, substreams: pcm_substreams[dev]); |
1804 | if (err < 0) |
1805 | return err; |
1806 | err = loopback_mixer_new(loopback, notify: pcm_notify[dev] ? 1 : 0); |
1807 | if (err < 0) |
1808 | return err; |
1809 | loopback_cable_proc_new(loopback, cidx: 0); |
1810 | loopback_cable_proc_new(loopback, cidx: 1); |
1811 | loopback_timer_source_proc_new(loopback); |
1812 | strcpy(p: card->driver, q: "Loopback" ); |
1813 | strcpy(p: card->shortname, q: "Loopback" ); |
1814 | sprintf(buf: card->longname, fmt: "Loopback %i" , dev + 1); |
1815 | err = snd_card_register(card); |
1816 | if (err < 0) |
1817 | return err; |
1818 | platform_set_drvdata(pdev: devptr, data: card); |
1819 | return 0; |
1820 | } |
1821 | |
1822 | #ifdef CONFIG_PM_SLEEP |
1823 | static int loopback_suspend(struct device *pdev) |
1824 | { |
1825 | struct snd_card *card = dev_get_drvdata(dev: pdev); |
1826 | |
1827 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
1828 | return 0; |
1829 | } |
1830 | |
1831 | static int loopback_resume(struct device *pdev) |
1832 | { |
1833 | struct snd_card *card = dev_get_drvdata(dev: pdev); |
1834 | |
1835 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
1836 | return 0; |
1837 | } |
1838 | |
1839 | static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume); |
1840 | #define LOOPBACK_PM_OPS &loopback_pm |
1841 | #else |
1842 | #define LOOPBACK_PM_OPS NULL |
1843 | #endif |
1844 | |
1845 | #define SND_LOOPBACK_DRIVER "snd_aloop" |
1846 | |
1847 | static struct platform_driver loopback_driver = { |
1848 | .probe = loopback_probe, |
1849 | .driver = { |
1850 | .name = SND_LOOPBACK_DRIVER, |
1851 | .pm = LOOPBACK_PM_OPS, |
1852 | }, |
1853 | }; |
1854 | |
1855 | static void loopback_unregister_all(void) |
1856 | { |
1857 | int i; |
1858 | |
1859 | for (i = 0; i < ARRAY_SIZE(devices); ++i) |
1860 | platform_device_unregister(devices[i]); |
1861 | platform_driver_unregister(&loopback_driver); |
1862 | } |
1863 | |
1864 | static int __init alsa_card_loopback_init(void) |
1865 | { |
1866 | int i, err, cards; |
1867 | |
1868 | err = platform_driver_register(&loopback_driver); |
1869 | if (err < 0) |
1870 | return err; |
1871 | |
1872 | |
1873 | cards = 0; |
1874 | for (i = 0; i < SNDRV_CARDS; i++) { |
1875 | struct platform_device *device; |
1876 | if (!enable[i]) |
1877 | continue; |
1878 | device = platform_device_register_simple(SND_LOOPBACK_DRIVER, |
1879 | id: i, NULL, num: 0); |
1880 | if (IS_ERR(ptr: device)) |
1881 | continue; |
1882 | if (!platform_get_drvdata(pdev: device)) { |
1883 | platform_device_unregister(device); |
1884 | continue; |
1885 | } |
1886 | devices[i] = device; |
1887 | cards++; |
1888 | } |
1889 | if (!cards) { |
1890 | #ifdef MODULE |
1891 | printk(KERN_ERR "aloop: No loopback enabled\n" ); |
1892 | #endif |
1893 | loopback_unregister_all(); |
1894 | return -ENODEV; |
1895 | } |
1896 | return 0; |
1897 | } |
1898 | |
1899 | static void __exit alsa_card_loopback_exit(void) |
1900 | { |
1901 | loopback_unregister_all(); |
1902 | } |
1903 | |
1904 | module_init(alsa_card_loopback_init) |
1905 | module_exit(alsa_card_loopback_exit) |
1906 | |