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