Linux Audio

Check our new training course

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