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