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