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