Linux Audio

Check our new training course

Loading...
   1/*
   2 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
   3 *                   Creative Labs, Inc.
   4 *  Routines for control of EMU10K1 chips / PCM routines
   5 *  Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
   6 *
   7 *  BUGS:
   8 *    --
   9 *
  10 *  TODO:
  11 *    --
  12 *
  13 *   This program is free software; you can redistribute it and/or modify
  14 *   it under the terms of the GNU General Public License as published by
  15 *   the Free Software Foundation; either version 2 of the License, or
  16 *   (at your option) any later version.
  17 *
  18 *   This program is distributed in the hope that it will be useful,
  19 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 *   GNU General Public License for more details.
  22 *
  23 *   You should have received a copy of the GNU General Public License
  24 *   along with this program; if not, write to the Free Software
  25 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  26 *
  27 */
  28
  29#include <linux/pci.h>
  30#include <linux/delay.h>
  31#include <linux/slab.h>
  32#include <linux/time.h>
  33#include <linux/init.h>
  34#include <sound/core.h>
  35#include <sound/emu10k1.h>
  36
  37static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
  38				      struct snd_emu10k1_voice *voice)
  39{
  40	struct snd_emu10k1_pcm *epcm;
  41
  42	if ((epcm = voice->epcm) == NULL)
  43		return;
  44	if (epcm->substream == NULL)
  45		return;
  46#if 0
  47	printk(KERN_DEBUG "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
  48			epcm->substream->runtime->hw->pointer(emu, epcm->substream),
  49			snd_pcm_lib_period_bytes(epcm->substream),
  50			snd_pcm_lib_buffer_bytes(epcm->substream));
  51#endif
  52	snd_pcm_period_elapsed(epcm->substream);
  53}
  54
  55static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
  56					      unsigned int status)
  57{
  58#if 0
  59	if (status & IPR_ADCBUFHALFFULL) {
  60		if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  61			return;
  62	}
  63#endif
  64	snd_pcm_period_elapsed(emu->pcm_capture_substream);
  65}
  66
  67static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
  68					      unsigned int status)
  69{
  70#if 0
  71	if (status & IPR_MICBUFHALFFULL) {
  72		if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  73			return;
  74	}
  75#endif
  76	snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
  77}
  78
  79static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
  80					  unsigned int status)
  81{
  82#if 0
  83	if (status & IPR_EFXBUFHALFFULL) {
  84		if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  85			return;
  86	}
  87#endif
  88	snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
  89}	 
  90
  91static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
  92{
  93	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  94	struct snd_pcm_runtime *runtime = substream->runtime;
  95	struct snd_emu10k1_pcm *epcm = runtime->private_data;
  96	unsigned int ptr;
  97
  98	if (!epcm->running)
  99		return 0;
 100	ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
 101	ptr += runtime->buffer_size;
 102	ptr -= epcm->ccca_start_addr;
 103	ptr %= runtime->buffer_size;
 104
 105	return ptr;
 106}
 107
 108static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
 109{
 110	int err, i;
 111
 112	if (epcm->voices[1] != NULL && voices < 2) {
 113		snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
 114		epcm->voices[1] = NULL;
 115	}
 116	for (i = 0; i < voices; i++) {
 117		if (epcm->voices[i] == NULL)
 118			break;
 119	}
 120	if (i == voices)
 121		return 0; /* already allocated */
 122
 123	for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
 124		if (epcm->voices[i]) {
 125			snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
 126			epcm->voices[i] = NULL;
 127		}
 128	}
 129	err = snd_emu10k1_voice_alloc(epcm->emu,
 130				      epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
 131				      voices,
 132				      &epcm->voices[0]);
 133	
 134	if (err < 0)
 135		return err;
 136	epcm->voices[0]->epcm = epcm;
 137	if (voices > 1) {
 138		for (i = 1; i < voices; i++) {
 139			epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
 140			epcm->voices[i]->epcm = epcm;
 141		}
 142	}
 143	if (epcm->extra == NULL) {
 144		err = snd_emu10k1_voice_alloc(epcm->emu,
 145					      epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
 146					      1,
 147					      &epcm->extra);
 148		if (err < 0) {
 149			/*
 150			printk(KERN_DEBUG "pcm_channel_alloc: "
 151			       "failed extra: voices=%d, frame=%d\n",
 152			       voices, frame);
 153			*/
 154			for (i = 0; i < voices; i++) {
 155				snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
 156				epcm->voices[i] = NULL;
 157			}
 158			return err;
 159		}
 160		epcm->extra->epcm = epcm;
 161		epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
 162	}
 163	return 0;
 164}
 165
 166static unsigned int capture_period_sizes[31] = {
 167	384,	448,	512,	640,
 168	384*2,	448*2,	512*2,	640*2,
 169	384*4,	448*4,	512*4,	640*4,
 170	384*8,	448*8,	512*8,	640*8,
 171	384*16,	448*16,	512*16,	640*16,
 172	384*32,	448*32,	512*32,	640*32,
 173	384*64,	448*64,	512*64,	640*64,
 174	384*128,448*128,512*128
 175};
 176
 177static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
 178	.count = 31,
 179	.list = capture_period_sizes,
 180	.mask = 0
 181};
 182
 183static unsigned int capture_rates[8] = {
 184	8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
 185};
 186
 187static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
 188	.count = 8,
 189	.list = capture_rates,
 190	.mask = 0
 191};
 192
 193static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
 194{
 195	switch (rate) {
 196	case 8000:	return ADCCR_SAMPLERATE_8;
 197	case 11025:	return ADCCR_SAMPLERATE_11;
 198	case 16000:	return ADCCR_SAMPLERATE_16;
 199	case 22050:	return ADCCR_SAMPLERATE_22;
 200	case 24000:	return ADCCR_SAMPLERATE_24;
 201	case 32000:	return ADCCR_SAMPLERATE_32;
 202	case 44100:	return ADCCR_SAMPLERATE_44;
 203	case 48000:	return ADCCR_SAMPLERATE_48;
 204	default:
 205			snd_BUG();
 206			return ADCCR_SAMPLERATE_8;
 207	}
 208}
 209
 210static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
 211{
 212	switch (rate) {
 213	case 8000:	return A_ADCCR_SAMPLERATE_8;
 214	case 11025:	return A_ADCCR_SAMPLERATE_11;
 215	case 12000:	return A_ADCCR_SAMPLERATE_12; /* really supported? */
 216	case 16000:	return ADCCR_SAMPLERATE_16;
 217	case 22050:	return ADCCR_SAMPLERATE_22;
 218	case 24000:	return ADCCR_SAMPLERATE_24;
 219	case 32000:	return ADCCR_SAMPLERATE_32;
 220	case 44100:	return ADCCR_SAMPLERATE_44;
 221	case 48000:	return ADCCR_SAMPLERATE_48;
 222	default:
 223			snd_BUG();
 224			return A_ADCCR_SAMPLERATE_8;
 225	}
 226}
 227
 228static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
 229{
 230	unsigned int pitch_target;
 231
 232	pitch_target = (rate << 8) / 375;
 233	pitch_target = (pitch_target >> 1) + (pitch_target & 1);
 234	return pitch_target;
 235}
 236
 237#define PITCH_48000 0x00004000
 238#define PITCH_96000 0x00008000
 239#define PITCH_85000 0x00007155
 240#define PITCH_80726 0x00006ba2
 241#define PITCH_67882 0x00005a82
 242#define PITCH_57081 0x00004c1c
 243
 244static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
 245{
 246	if (pitch_target == PITCH_48000)
 247		return CCCA_INTERPROM_0;
 248	else if (pitch_target < PITCH_48000)
 249		return CCCA_INTERPROM_1;
 250	else if (pitch_target >= PITCH_96000)
 251		return CCCA_INTERPROM_0;
 252	else if (pitch_target >= PITCH_85000)
 253		return CCCA_INTERPROM_6;
 254	else if (pitch_target >= PITCH_80726)
 255		return CCCA_INTERPROM_5;
 256	else if (pitch_target >= PITCH_67882)
 257		return CCCA_INTERPROM_4;
 258	else if (pitch_target >= PITCH_57081)
 259		return CCCA_INTERPROM_3;
 260	else  
 261		return CCCA_INTERPROM_2;
 262}
 263
 264/*
 265 * calculate cache invalidate size 
 266 *
 267 * stereo: channel is stereo
 268 * w_16: using 16bit samples
 269 *
 270 * returns: cache invalidate size in samples
 271 */
 272static inline int emu10k1_ccis(int stereo, int w_16)
 273{
 274	if (w_16) {
 275		return stereo ? 24 : 26;
 276	} else {
 277		return stereo ? 24*2 : 26*2;
 278	}
 279}
 280
 281static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
 282				       int master, int extra,
 283				       struct snd_emu10k1_voice *evoice,
 284				       unsigned int start_addr,
 285				       unsigned int end_addr,
 286				       struct snd_emu10k1_pcm_mixer *mix)
 287{
 288	struct snd_pcm_substream *substream = evoice->epcm->substream;
 289	struct snd_pcm_runtime *runtime = substream->runtime;
 290	unsigned int silent_page, tmp;
 291	int voice, stereo, w_16;
 292	unsigned char attn, send_amount[8];
 293	unsigned char send_routing[8];
 294	unsigned long flags;
 295	unsigned int pitch_target;
 296	unsigned int ccis;
 297
 298	voice = evoice->number;
 299	stereo = runtime->channels == 2;
 300	w_16 = snd_pcm_format_width(runtime->format) == 16;
 301
 302	if (!extra && stereo) {
 303		start_addr >>= 1;
 304		end_addr >>= 1;
 305	}
 306	if (w_16) {
 307		start_addr >>= 1;
 308		end_addr >>= 1;
 309	}
 310
 311	spin_lock_irqsave(&emu->reg_lock, flags);
 312
 313	/* volume parameters */
 314	if (extra) {
 315		attn = 0;
 316		memset(send_routing, 0, sizeof(send_routing));
 317		send_routing[0] = 0;
 318		send_routing[1] = 1;
 319		send_routing[2] = 2;
 320		send_routing[3] = 3;
 321		memset(send_amount, 0, sizeof(send_amount));
 322	} else {
 323		/* mono, left, right (master voice = left) */
 324		tmp = stereo ? (master ? 1 : 2) : 0;
 325		memcpy(send_routing, &mix->send_routing[tmp][0], 8);
 326		memcpy(send_amount, &mix->send_volume[tmp][0], 8);
 327	}
 328
 329	ccis = emu10k1_ccis(stereo, w_16);
 330	
 331	if (master) {
 332		evoice->epcm->ccca_start_addr = start_addr + ccis;
 333		if (extra) {
 334			start_addr += ccis;
 335			end_addr += ccis + emu->delay_pcm_irq;
 336		}
 337		if (stereo && !extra) {
 338			snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
 339			snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
 340		} else {
 341			snd_emu10k1_ptr_write(emu, CPF, voice, 0);
 342		}
 343	}
 344
 345	/* setup routing */
 346	if (emu->audigy) {
 347		snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
 348				      snd_emu10k1_compose_audigy_fxrt1(send_routing));
 349		snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
 350				      snd_emu10k1_compose_audigy_fxrt2(send_routing));
 351		snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
 352				      ((unsigned int)send_amount[4] << 24) |
 353				      ((unsigned int)send_amount[5] << 16) |
 354				      ((unsigned int)send_amount[6] << 8) |
 355				      (unsigned int)send_amount[7]);
 356	} else
 357		snd_emu10k1_ptr_write(emu, FXRT, voice,
 358				      snd_emu10k1_compose_send_routing(send_routing));
 359	/* Stop CA */
 360	/* Assumption that PT is already 0 so no harm overwriting */
 361	snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
 362	snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
 363	snd_emu10k1_ptr_write(emu, PSST, voice,
 364			(start_addr + (extra ? emu->delay_pcm_irq : 0)) |
 365			(send_amount[2] << 24));
 366	if (emu->card_capabilities->emu_model)
 367		pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
 368	else 
 369		pitch_target = emu10k1_calc_pitch_target(runtime->rate);
 370	if (extra)
 371		snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
 372			      emu10k1_select_interprom(pitch_target) |
 373			      (w_16 ? 0 : CCCA_8BITSELECT));
 374	else
 375		snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
 376			      emu10k1_select_interprom(pitch_target) |
 377			      (w_16 ? 0 : CCCA_8BITSELECT));
 378	/* Clear filter delay memory */
 379	snd_emu10k1_ptr_write(emu, Z1, voice, 0);
 380	snd_emu10k1_ptr_write(emu, Z2, voice, 0);
 381	/* invalidate maps */
 382	silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
 383	snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
 384	snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
 385	/* modulation envelope */
 386	snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
 387	snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
 388	snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
 389	snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
 390	snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
 391	snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
 392	snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
 393	snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
 394	snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
 395	snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
 396	/* volume envelope */
 397	snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
 398	snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
 399	/* filter envelope */
 400	snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
 401	/* pitch envelope */
 402	snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
 403
 404	spin_unlock_irqrestore(&emu->reg_lock, flags);
 405}
 406
 407static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
 408					  struct snd_pcm_hw_params *hw_params)
 409{
 410	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 411	struct snd_pcm_runtime *runtime = substream->runtime;
 412	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 413	int err;
 414
 415	if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
 416		return err;
 417	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
 418		return err;
 419	if (err > 0) {	/* change */
 420		int mapped;
 421		if (epcm->memblk != NULL)
 422			snd_emu10k1_free_pages(emu, epcm->memblk);
 423		epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
 424		epcm->start_addr = 0;
 425		if (! epcm->memblk)
 426			return -ENOMEM;
 427		mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
 428		if (mapped < 0)
 429			return -ENOMEM;
 430		epcm->start_addr = mapped << PAGE_SHIFT;
 431	}
 432	return 0;
 433}
 434
 435static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
 436{
 437	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 438	struct snd_pcm_runtime *runtime = substream->runtime;
 439	struct snd_emu10k1_pcm *epcm;
 440
 441	if (runtime->private_data == NULL)
 442		return 0;
 443	epcm = runtime->private_data;
 444	if (epcm->extra) {
 445		snd_emu10k1_voice_free(epcm->emu, epcm->extra);
 446		epcm->extra = NULL;
 447	}
 448	if (epcm->voices[1]) {
 449		snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
 450		epcm->voices[1] = NULL;
 451	}
 452	if (epcm->voices[0]) {
 453		snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
 454		epcm->voices[0] = NULL;
 455	}
 456	if (epcm->memblk) {
 457		snd_emu10k1_free_pages(emu, epcm->memblk);
 458		epcm->memblk = NULL;
 459		epcm->start_addr = 0;
 460	}
 461	snd_pcm_lib_free_pages(substream);
 462	return 0;
 463}
 464
 465static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
 466{
 467	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 468	struct snd_pcm_runtime *runtime = substream->runtime;
 469	struct snd_emu10k1_pcm *epcm;
 470	int i;
 471
 472	if (runtime->private_data == NULL)
 473		return 0;
 474	epcm = runtime->private_data;
 475	if (epcm->extra) {
 476		snd_emu10k1_voice_free(epcm->emu, epcm->extra);
 477		epcm->extra = NULL;
 478	}
 479	for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
 480		if (epcm->voices[i]) {
 481			snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
 482			epcm->voices[i] = NULL;
 483		}
 484	}
 485	if (epcm->memblk) {
 486		snd_emu10k1_free_pages(emu, epcm->memblk);
 487		epcm->memblk = NULL;
 488		epcm->start_addr = 0;
 489	}
 490	snd_pcm_lib_free_pages(substream);
 491	return 0;
 492}
 493
 494static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
 495{
 496	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 497	struct snd_pcm_runtime *runtime = substream->runtime;
 498	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 499	unsigned int start_addr, end_addr;
 500
 501	start_addr = epcm->start_addr;
 502	end_addr = snd_pcm_lib_period_bytes(substream);
 503	if (runtime->channels == 2) {
 504		start_addr >>= 1;
 505		end_addr >>= 1;
 506	}
 507	end_addr += start_addr;
 508	snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
 509				   start_addr, end_addr, NULL);
 510	start_addr = epcm->start_addr;
 511	end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
 512	snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
 513				   start_addr, end_addr,
 514				   &emu->pcm_mixer[substream->number]);
 515	if (epcm->voices[1])
 516		snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
 517					   start_addr, end_addr,
 518					   &emu->pcm_mixer[substream->number]);
 519	return 0;
 520}
 521
 522static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
 523{
 524	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 525	struct snd_pcm_runtime *runtime = substream->runtime;
 526	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 527	unsigned int start_addr, end_addr;
 528	unsigned int channel_size;
 529	int i;
 530
 531	start_addr = epcm->start_addr;
 532	end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
 533
 534	/*
 535	 * the kX driver leaves some space between voices
 536	 */
 537	channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
 538
 539	snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
 540				   start_addr, start_addr + (channel_size / 2), NULL);
 541
 542	/* only difference with the master voice is we use it for the pointer */
 543	snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
 544				   start_addr, start_addr + channel_size,
 545				   &emu->efx_pcm_mixer[0]);
 546
 547	start_addr += channel_size;
 548	for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
 549		snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
 550					   start_addr, start_addr + channel_size,
 551					   &emu->efx_pcm_mixer[i]);
 552		start_addr += channel_size;
 553	}
 554
 555	return 0;
 556}
 557
 558static struct snd_pcm_hardware snd_emu10k1_efx_playback =
 559{
 560	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
 561				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 562				 SNDRV_PCM_INFO_RESUME |
 563				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
 564	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
 565	.rates =		SNDRV_PCM_RATE_48000,
 566	.rate_min =		48000,
 567	.rate_max =		48000,
 568	.channels_min =		NUM_EFX_PLAYBACK,
 569	.channels_max =		NUM_EFX_PLAYBACK,
 570	.buffer_bytes_max =	(64*1024),
 571	.period_bytes_min =	64,
 572	.period_bytes_max =	(64*1024),
 573	.periods_min =		2,
 574	.periods_max =		2,
 575	.fifo_size =		0,
 576};
 577
 578static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
 579					 struct snd_pcm_hw_params *hw_params)
 580{
 581	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
 582}
 583
 584static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
 585{
 586	return snd_pcm_lib_free_pages(substream);
 587}
 588
 589static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
 590{
 591	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 592	struct snd_pcm_runtime *runtime = substream->runtime;
 593	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 594	int idx;
 595
 596	/* zeroing the buffer size will stop capture */
 597	snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
 598	switch (epcm->type) {
 599	case CAPTURE_AC97ADC:
 600		snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
 601		break;
 602	case CAPTURE_EFX:
 603		if (emu->audigy) {
 604			snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
 605			snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
 606		} else
 607			snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
 608		break;
 609	default:
 610		break;
 611	}	
 612	snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
 613	epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
 614	epcm->capture_bs_val = 0;
 615	for (idx = 0; idx < 31; idx++) {
 616		if (capture_period_sizes[idx] == epcm->capture_bufsize) {
 617			epcm->capture_bs_val = idx + 1;
 618			break;
 619		}
 620	}
 621	if (epcm->capture_bs_val == 0) {
 622		snd_BUG();
 623		epcm->capture_bs_val++;
 624	}
 625	if (epcm->type == CAPTURE_AC97ADC) {
 626		epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
 627		if (runtime->channels > 1)
 628			epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
 629		epcm->capture_cr_val |= emu->audigy ?
 630			snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
 631			snd_emu10k1_capture_rate_reg(runtime->rate);
 632	}
 633	return 0;
 634}
 635
 636static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
 637{
 638	struct snd_pcm_runtime *runtime;
 639	unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
 640
 641	if (evoice == NULL)
 642		return;
 643	runtime = evoice->epcm->substream->runtime;
 644	voice = evoice->number;
 645	stereo = (!extra && runtime->channels == 2);
 646	sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
 647	ccis = emu10k1_ccis(stereo, sample == 0);
 648	/* set cs to 2 * number of cache registers beside the invalidated */
 649	cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
 650	if (cs > 16) cs = 16;
 651	for (i = 0; i < cs; i++) {
 652		snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
 653		if (stereo) {
 654			snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
 655		}
 656	}
 657	/* reset cache */
 658	snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
 659	snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
 660	if (stereo) {
 661		snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
 662		snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
 663	}
 664	/* fill cache */
 665	snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
 666	if (stereo) {
 667		snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
 668	}
 669}
 670
 671static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
 672					       int master, int extra,
 673					       struct snd_emu10k1_pcm_mixer *mix)
 674{
 675	struct snd_pcm_substream *substream;
 676	struct snd_pcm_runtime *runtime;
 677	unsigned int attn, vattn;
 678	unsigned int voice, tmp;
 679
 680	if (evoice == NULL)	/* skip second voice for mono */
 681		return;
 682	substream = evoice->epcm->substream;
 683	runtime = substream->runtime;
 684	voice = evoice->number;
 685
 686	attn = extra ? 0 : 0x00ff;
 687	tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
 688	vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
 689	snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
 690	snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
 691	snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
 692	snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
 693	snd_emu10k1_voice_clear_loop_stop(emu, voice);
 694}	
 695
 696static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
 697{
 698	struct snd_pcm_substream *substream;
 699	struct snd_pcm_runtime *runtime;
 700	unsigned int voice, pitch, pitch_target;
 701
 702	if (evoice == NULL)	/* skip second voice for mono */
 703		return;
 704	substream = evoice->epcm->substream;
 705	runtime = substream->runtime;
 706	voice = evoice->number;
 707
 708	pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
 709	if (emu->card_capabilities->emu_model)
 710		pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
 711	else 
 712		pitch_target = emu10k1_calc_pitch_target(runtime->rate);
 713	snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
 714	if (master || evoice->epcm->type == PLAYBACK_EFX)
 715		snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
 716	snd_emu10k1_ptr_write(emu, IP, voice, pitch);
 717	if (extra)
 718		snd_emu10k1_voice_intr_enable(emu, voice);
 719}
 720
 721static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
 722{
 723	unsigned int voice;
 724
 725	if (evoice == NULL)
 726		return;
 727	voice = evoice->number;
 728	snd_emu10k1_voice_intr_disable(emu, voice);
 729	snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
 730	snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
 731	snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
 732	snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
 733	snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
 734	snd_emu10k1_ptr_write(emu, IP, voice, 0);
 735}
 736
 737static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
 738		struct snd_emu10k1_pcm *epcm,
 739		struct snd_pcm_substream *substream,
 740		struct snd_pcm_runtime *runtime)
 741{
 742	unsigned int ptr, period_pos;
 743
 744	/* try to sychronize the current position for the interrupt
 745	   source voice */
 746	period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
 747	period_pos %= runtime->period_size;
 748	ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
 749	ptr &= ~0x00ffffff;
 750	ptr |= epcm->ccca_start_addr + period_pos;
 751	snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
 752}
 753
 754static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
 755				        int cmd)
 756{
 757	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 758	struct snd_pcm_runtime *runtime = substream->runtime;
 759	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 760	struct snd_emu10k1_pcm_mixer *mix;
 761	int result = 0;
 762
 763	/*
 764	printk(KERN_DEBUG "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
 765	       (int)emu, cmd, substream->ops->pointer(substream))
 766	*/
 767	spin_lock(&emu->reg_lock);
 768	switch (cmd) {
 769	case SNDRV_PCM_TRIGGER_START:
 770		snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);	/* do we need this? */
 771		snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
 772		/* follow thru */
 773	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 774	case SNDRV_PCM_TRIGGER_RESUME:
 775		if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
 776			snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
 777		mix = &emu->pcm_mixer[substream->number];
 778		snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
 779		snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
 780		snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
 781		snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
 782		snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
 783		snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
 784		epcm->running = 1;
 785		break;
 786	case SNDRV_PCM_TRIGGER_STOP:
 787	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 788	case SNDRV_PCM_TRIGGER_SUSPEND:
 789		epcm->running = 0;
 790		snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
 791		snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
 792		snd_emu10k1_playback_stop_voice(emu, epcm->extra);
 793		break;
 794	default:
 795		result = -EINVAL;
 796		break;
 797	}
 798	spin_unlock(&emu->reg_lock);
 799	return result;
 800}
 801
 802static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
 803				       int cmd)
 804{
 805	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 806	struct snd_pcm_runtime *runtime = substream->runtime;
 807	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 808	int result = 0;
 809
 810	spin_lock(&emu->reg_lock);
 811	switch (cmd) {
 812	case SNDRV_PCM_TRIGGER_START:
 813	case SNDRV_PCM_TRIGGER_RESUME:
 814		/* hmm this should cause full and half full interrupt to be raised? */
 815		outl(epcm->capture_ipr, emu->port + IPR);
 816		snd_emu10k1_intr_enable(emu, epcm->capture_inte);
 817		/*
 818		printk(KERN_DEBUG "adccr = 0x%x, adcbs = 0x%x\n",
 819		       epcm->adccr, epcm->adcbs);
 820		*/
 821		switch (epcm->type) {
 822		case CAPTURE_AC97ADC:
 823			snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
 824			break;
 825		case CAPTURE_EFX:
 826			if (emu->audigy) {
 827				snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
 828				snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
 829				snd_printdd("cr_val=0x%x, cr_val2=0x%x\n", epcm->capture_cr_val, epcm->capture_cr_val2);
 830			} else
 831				snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
 832			break;
 833		default:	
 834			break;
 835		}
 836		snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
 837		epcm->running = 1;
 838		epcm->first_ptr = 1;
 839		break;
 840	case SNDRV_PCM_TRIGGER_STOP:
 841	case SNDRV_PCM_TRIGGER_SUSPEND:
 842		epcm->running = 0;
 843		snd_emu10k1_intr_disable(emu, epcm->capture_inte);
 844		outl(epcm->capture_ipr, emu->port + IPR);
 845		snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
 846		switch (epcm->type) {
 847		case CAPTURE_AC97ADC:
 848			snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
 849			break;
 850		case CAPTURE_EFX:
 851			if (emu->audigy) {
 852				snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
 853				snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
 854			} else
 855				snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
 856			break;
 857		default:
 858			break;
 859		}
 860		break;
 861	default:
 862		result = -EINVAL;
 863	}
 864	spin_unlock(&emu->reg_lock);
 865	return result;
 866}
 867
 868static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
 869{
 870	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 871	struct snd_pcm_runtime *runtime = substream->runtime;
 872	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 873	unsigned int ptr;
 874
 875	if (!epcm->running)
 876		return 0;
 877	ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
 878#if 0	/* Perex's code */
 879	ptr += runtime->buffer_size;
 880	ptr -= epcm->ccca_start_addr;
 881	ptr %= runtime->buffer_size;
 882#else	/* EMU10K1 Open Source code from Creative */
 883	if (ptr < epcm->ccca_start_addr)
 884		ptr += runtime->buffer_size - epcm->ccca_start_addr;
 885	else {
 886		ptr -= epcm->ccca_start_addr;
 887		if (ptr >= runtime->buffer_size)
 888			ptr -= runtime->buffer_size;
 889	}
 890#endif
 891	/*
 892	printk(KERN_DEBUG
 893	       "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
 894	       (long)ptr, (long)runtime->buffer_size,
 895	       (long)runtime->period_size);
 896	*/
 897	return ptr;
 898}
 899
 900
 901static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
 902				        int cmd)
 903{
 904	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 905	struct snd_pcm_runtime *runtime = substream->runtime;
 906	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 907	int i;
 908	int result = 0;
 909
 910	spin_lock(&emu->reg_lock);
 911	switch (cmd) {
 912	case SNDRV_PCM_TRIGGER_START:
 913		/* prepare voices */
 914		for (i = 0; i < NUM_EFX_PLAYBACK; i++) {	
 915			snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
 916		}
 917		snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
 918
 919		/* follow thru */
 920	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 921	case SNDRV_PCM_TRIGGER_RESUME:
 922		snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
 923		snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
 924						   &emu->efx_pcm_mixer[0]);
 925		for (i = 1; i < NUM_EFX_PLAYBACK; i++)
 926			snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
 927							   &emu->efx_pcm_mixer[i]);
 928		snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
 929		snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
 930		for (i = 1; i < NUM_EFX_PLAYBACK; i++)
 931			snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
 932		epcm->running = 1;
 933		break;
 934	case SNDRV_PCM_TRIGGER_SUSPEND:
 935	case SNDRV_PCM_TRIGGER_STOP:
 936	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 937		epcm->running = 0;
 938		for (i = 0; i < NUM_EFX_PLAYBACK; i++) {	
 939			snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
 940		}
 941		snd_emu10k1_playback_stop_voice(emu, epcm->extra);
 942		break;
 943	default:
 944		result = -EINVAL;
 945		break;
 946	}
 947	spin_unlock(&emu->reg_lock);
 948	return result;
 949}
 950
 951
 952static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
 953{
 954	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 955	struct snd_pcm_runtime *runtime = substream->runtime;
 956	struct snd_emu10k1_pcm *epcm = runtime->private_data;
 957	unsigned int ptr;
 958
 959	if (!epcm->running)
 960		return 0;
 961	if (epcm->first_ptr) {
 962		udelay(50);	/* hack, it takes awhile until capture is started */
 963		epcm->first_ptr = 0;
 964	}
 965	ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
 966	return bytes_to_frames(runtime, ptr);
 967}
 968
 969/*
 970 *  Playback support device description
 971 */
 972
 973static struct snd_pcm_hardware snd_emu10k1_playback =
 974{
 975	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
 976				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 977				 SNDRV_PCM_INFO_RESUME |
 978				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
 979	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
 980	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
 981	.rate_min =		4000,
 982	.rate_max =		96000,
 983	.channels_min =		1,
 984	.channels_max =		2,
 985	.buffer_bytes_max =	(128*1024),
 986	.period_bytes_min =	64,
 987	.period_bytes_max =	(128*1024),
 988	.periods_min =		1,
 989	.periods_max =		1024,
 990	.fifo_size =		0,
 991};
 992
 993/*
 994 *  Capture support device description
 995 */
 996
 997static struct snd_pcm_hardware snd_emu10k1_capture =
 998{
 999	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1000				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1001				 SNDRV_PCM_INFO_RESUME |
1002				 SNDRV_PCM_INFO_MMAP_VALID),
1003	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
1004	.rates =		SNDRV_PCM_RATE_8000_48000,
1005	.rate_min =		8000,
1006	.rate_max =		48000,
1007	.channels_min =		1,
1008	.channels_max =		2,
1009	.buffer_bytes_max =	(64*1024),
1010	.period_bytes_min =	384,
1011	.period_bytes_max =	(64*1024),
1012	.periods_min =		2,
1013	.periods_max =		2,
1014	.fifo_size =		0,
1015};
1016
1017static struct snd_pcm_hardware snd_emu10k1_capture_efx =
1018{
1019	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1020				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1021				 SNDRV_PCM_INFO_RESUME |
1022				 SNDRV_PCM_INFO_MMAP_VALID),
1023	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
1024	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 
1025				 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | 
1026				 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1027	.rate_min =		44100,
1028	.rate_max =		192000,
1029	.channels_min =		8,
1030	.channels_max =		8,
1031	.buffer_bytes_max =	(64*1024),
1032	.period_bytes_min =	384,
1033	.period_bytes_max =	(64*1024),
1034	.periods_min =		2,
1035	.periods_max =		2,
1036	.fifo_size =		0,
1037};
1038
1039/*
1040 *
1041 */
1042
1043static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1044{
1045	struct snd_ctl_elem_id id;
1046
1047	if (! kctl)
1048		return;
1049	if (activate)
1050		kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1051	else
1052		kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1053	snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1054		       SNDRV_CTL_EVENT_MASK_INFO,
1055		       snd_ctl_build_ioff(&id, kctl, idx));
1056}
1057
1058static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1059{
1060	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1061	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1062	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1063}
1064
1065static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1066{
1067	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1068	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1069	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1070}
1071
1072static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1073{
1074	kfree(runtime->private_data);
1075}
1076
1077static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1078{
1079	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1080	struct snd_emu10k1_pcm_mixer *mix;
1081	int i;
1082
1083	for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1084		mix = &emu->efx_pcm_mixer[i];
1085		mix->epcm = NULL;
1086		snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1087	}
1088	return 0;
1089}
1090
1091static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1092{
1093	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1094	struct snd_emu10k1_pcm *epcm;
1095	struct snd_emu10k1_pcm_mixer *mix;
1096	struct snd_pcm_runtime *runtime = substream->runtime;
1097	int i;
1098
1099	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1100	if (epcm == NULL)
1101		return -ENOMEM;
1102	epcm->emu = emu;
1103	epcm->type = PLAYBACK_EFX;
1104	epcm->substream = substream;
1105	
1106	emu->pcm_playback_efx_substream = substream;
1107
1108	runtime->private_data = epcm;
1109	runtime->private_free = snd_emu10k1_pcm_free_substream;
1110	runtime->hw = snd_emu10k1_efx_playback;
1111	
1112	for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1113		mix = &emu->efx_pcm_mixer[i];
1114		mix->send_routing[0][0] = i;
1115		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1116		mix->send_volume[0][0] = 255;
1117		mix->attn[0] = 0xffff;
1118		mix->epcm = epcm;
1119		snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1120	}
1121	return 0;
1122}
1123
1124static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1125{
1126	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1127	struct snd_emu10k1_pcm *epcm;
1128	struct snd_emu10k1_pcm_mixer *mix;
1129	struct snd_pcm_runtime *runtime = substream->runtime;
1130	int i, err;
1131
1132	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1133	if (epcm == NULL)
1134		return -ENOMEM;
1135	epcm->emu = emu;
1136	epcm->type = PLAYBACK_EMUVOICE;
1137	epcm->substream = substream;
1138	runtime->private_data = epcm;
1139	runtime->private_free = snd_emu10k1_pcm_free_substream;
1140	runtime->hw = snd_emu10k1_playback;
1141	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1142		kfree(epcm);
1143		return err;
1144	}
1145	if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1146		kfree(epcm);
1147		return err;
1148	}
1149	err = snd_pcm_hw_rule_noresample(runtime, 48000);
1150	if (err < 0) {
1151		kfree(epcm);
1152		return err;
1153	}
1154	mix = &emu->pcm_mixer[substream->number];
1155	for (i = 0; i < 4; i++)
1156		mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1157	memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1158	mix->send_volume[0][0] = mix->send_volume[0][1] =
1159	mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1160	mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1161	mix->epcm = epcm;
1162	snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1163	return 0;
1164}
1165
1166static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1167{
1168	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1169	struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1170
1171	mix->epcm = NULL;
1172	snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1173	return 0;
1174}
1175
1176static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1177{
1178	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1179	struct snd_pcm_runtime *runtime = substream->runtime;
1180	struct snd_emu10k1_pcm *epcm;
1181
1182	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1183	if (epcm == NULL)
1184		return -ENOMEM;
1185	epcm->emu = emu;
1186	epcm->type = CAPTURE_AC97ADC;
1187	epcm->substream = substream;
1188	epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1189	epcm->capture_inte = INTE_ADCBUFENABLE;
1190	epcm->capture_ba_reg = ADCBA;
1191	epcm->capture_bs_reg = ADCBS;
1192	epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1193	runtime->private_data = epcm;
1194	runtime->private_free = snd_emu10k1_pcm_free_substream;
1195	runtime->hw = snd_emu10k1_capture;
1196	emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1197	emu->pcm_capture_substream = substream;
1198	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1199	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1200	return 0;
1201}
1202
1203static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1204{
1205	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1206
1207	emu->capture_interrupt = NULL;
1208	emu->pcm_capture_substream = NULL;
1209	return 0;
1210}
1211
1212static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1213{
1214	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1215	struct snd_emu10k1_pcm *epcm;
1216	struct snd_pcm_runtime *runtime = substream->runtime;
1217
1218	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1219	if (epcm == NULL)
1220		return -ENOMEM;
1221	epcm->emu = emu;
1222	epcm->type = CAPTURE_AC97MIC;
1223	epcm->substream = substream;
1224	epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1225	epcm->capture_inte = INTE_MICBUFENABLE;
1226	epcm->capture_ba_reg = MICBA;
1227	epcm->capture_bs_reg = MICBS;
1228	epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1229	substream->runtime->private_data = epcm;
1230	substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1231	runtime->hw = snd_emu10k1_capture;
1232	runtime->hw.rates = SNDRV_PCM_RATE_8000;
1233	runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1234	runtime->hw.channels_min = 1;
1235	emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1236	emu->pcm_capture_mic_substream = substream;
1237	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1238	return 0;
1239}
1240
1241static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1242{
1243	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1244
1245	emu->capture_interrupt = NULL;
1246	emu->pcm_capture_mic_substream = NULL;
1247	return 0;
1248}
1249
1250static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1251{
1252	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1253	struct snd_emu10k1_pcm *epcm;
1254	struct snd_pcm_runtime *runtime = substream->runtime;
1255	int nefx = emu->audigy ? 64 : 32;
1256	int idx;
1257
1258	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1259	if (epcm == NULL)
1260		return -ENOMEM;
1261	epcm->emu = emu;
1262	epcm->type = CAPTURE_EFX;
1263	epcm->substream = substream;
1264	epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1265	epcm->capture_inte = INTE_EFXBUFENABLE;
1266	epcm->capture_ba_reg = FXBA;
1267	epcm->capture_bs_reg = FXBS;
1268	epcm->capture_idx_reg = FXIDX;
1269	substream->runtime->private_data = epcm;
1270	substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1271	runtime->hw = snd_emu10k1_capture_efx;
1272	runtime->hw.rates = SNDRV_PCM_RATE_48000;
1273	runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1274	spin_lock_irq(&emu->reg_lock);
1275	if (emu->card_capabilities->emu_model) {
1276		/*  Nb. of channels has been increased to 16 */
1277		/* TODO
1278		 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1279		 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1280		 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1281		 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1282		 * rate_min = 44100,
1283		 * rate_max = 192000,
1284		 * channels_min = 16,
1285		 * channels_max = 16,
1286		 * Need to add mixer control to fix sample rate
1287		 *                 
1288		 * There are 32 mono channels of 16bits each.
1289		 * 24bit Audio uses 2x channels over 16bit
1290		 * 96kHz uses 2x channels over 48kHz
1291		 * 192kHz uses 4x channels over 48kHz
1292		 * So, for 48kHz 24bit, one has 16 channels
1293		 * for 96kHz 24bit, one has 8 channels
1294		 * for 192kHz 24bit, one has 4 channels
1295		 *
1296		 */
1297#if 1
1298		switch (emu->emu1010.internal_clock) {
1299		case 0:
1300			/* For 44.1kHz */
1301			runtime->hw.rates = SNDRV_PCM_RATE_44100;
1302			runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1303			runtime->hw.channels_min =
1304				runtime->hw.channels_max = 16;
1305			break;
1306		case 1:
1307			/* For 48kHz */
1308			runtime->hw.rates = SNDRV_PCM_RATE_48000;
1309			runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1310			runtime->hw.channels_min =
1311				runtime->hw.channels_max = 16;
1312			break;
1313		};
1314#endif
1315#if 0
1316		/* For 96kHz */
1317		runtime->hw.rates = SNDRV_PCM_RATE_96000;
1318		runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1319		runtime->hw.channels_min = runtime->hw.channels_max = 4;
1320#endif
1321#if 0
1322		/* For 192kHz */
1323		runtime->hw.rates = SNDRV_PCM_RATE_192000;
1324		runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1325		runtime->hw.channels_min = runtime->hw.channels_max = 2;
1326#endif
1327		runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1328		/* efx_voices_mask[0] is expected to be zero
1329 		 * efx_voices_mask[1] is expected to have 32bits set
1330		 */
1331	} else {
1332		runtime->hw.channels_min = runtime->hw.channels_max = 0;
1333		for (idx = 0; idx < nefx; idx++) {
1334			if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1335				runtime->hw.channels_min++;
1336				runtime->hw.channels_max++;
1337			}
1338		}
1339	}
1340	epcm->capture_cr_val = emu->efx_voices_mask[0];
1341	epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1342	spin_unlock_irq(&emu->reg_lock);
1343	emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1344	emu->pcm_capture_efx_substream = substream;
1345	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1346	return 0;
1347}
1348
1349static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1350{
1351	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1352
1353	emu->capture_interrupt = NULL;
1354	emu->pcm_capture_efx_substream = NULL;
1355	return 0;
1356}
1357
1358static struct snd_pcm_ops snd_emu10k1_playback_ops = {
1359	.open =			snd_emu10k1_playback_open,
1360	.close =		snd_emu10k1_playback_close,
1361	.ioctl =		snd_pcm_lib_ioctl,
1362	.hw_params =		snd_emu10k1_playback_hw_params,
1363	.hw_free =		snd_emu10k1_playback_hw_free,
1364	.prepare =		snd_emu10k1_playback_prepare,
1365	.trigger =		snd_emu10k1_playback_trigger,
1366	.pointer =		snd_emu10k1_playback_pointer,
1367	.page =			snd_pcm_sgbuf_ops_page,
1368};
1369
1370static struct snd_pcm_ops snd_emu10k1_capture_ops = {
1371	.open =			snd_emu10k1_capture_open,
1372	.close =		snd_emu10k1_capture_close,
1373	.ioctl =		snd_pcm_lib_ioctl,
1374	.hw_params =		snd_emu10k1_capture_hw_params,
1375	.hw_free =		snd_emu10k1_capture_hw_free,
1376	.prepare =		snd_emu10k1_capture_prepare,
1377	.trigger =		snd_emu10k1_capture_trigger,
1378	.pointer =		snd_emu10k1_capture_pointer,
1379};
1380
1381/* EFX playback */
1382static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1383	.open =			snd_emu10k1_efx_playback_open,
1384	.close =		snd_emu10k1_efx_playback_close,
1385	.ioctl =		snd_pcm_lib_ioctl,
1386	.hw_params =		snd_emu10k1_playback_hw_params,
1387	.hw_free =		snd_emu10k1_efx_playback_hw_free,
1388	.prepare =		snd_emu10k1_efx_playback_prepare,
1389	.trigger =		snd_emu10k1_efx_playback_trigger,
1390	.pointer =		snd_emu10k1_efx_playback_pointer,
1391	.page =			snd_pcm_sgbuf_ops_page,
1392};
1393
1394int __devinit snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1395{
1396	struct snd_pcm *pcm;
1397	struct snd_pcm_substream *substream;
1398	int err;
1399
1400	if (rpcm)
1401		*rpcm = NULL;
1402
1403	if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1404		return err;
1405
1406	pcm->private_data = emu;
1407
1408	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1409	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1410
1411	pcm->info_flags = 0;
1412	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1413	strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1414	emu->pcm = pcm;
1415
1416	for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1417		if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1418			return err;
1419
1420	for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1421		snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1422
1423	if (rpcm)
1424		*rpcm = pcm;
1425
1426	return 0;
1427}
1428
1429int __devinit snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1430{
1431	struct snd_pcm *pcm;
1432	struct snd_pcm_substream *substream;
1433	int err;
1434
1435	if (rpcm)
1436		*rpcm = NULL;
1437
1438	if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1439		return err;
1440
1441	pcm->private_data = emu;
1442
1443	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1444
1445	pcm->info_flags = 0;
1446	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1447	strcpy(pcm->name, "Multichannel Playback");
1448	emu->pcm_multi = pcm;
1449
1450	for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1451		if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1452			return err;
1453
1454	if (rpcm)
1455		*rpcm = pcm;
1456
1457	return 0;
1458}
1459
1460
1461static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1462	.open =			snd_emu10k1_capture_mic_open,
1463	.close =		snd_emu10k1_capture_mic_close,
1464	.ioctl =		snd_pcm_lib_ioctl,
1465	.hw_params =		snd_emu10k1_capture_hw_params,
1466	.hw_free =		snd_emu10k1_capture_hw_free,
1467	.prepare =		snd_emu10k1_capture_prepare,
1468	.trigger =		snd_emu10k1_capture_trigger,
1469	.pointer =		snd_emu10k1_capture_pointer,
1470};
1471
1472int __devinit snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1473{
1474	struct snd_pcm *pcm;
1475	int err;
1476
1477	if (rpcm)
1478		*rpcm = NULL;
1479
1480	if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1481		return err;
1482
1483	pcm->private_data = emu;
1484
1485	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1486
1487	pcm->info_flags = 0;
1488	strcpy(pcm->name, "Mic Capture");
1489	emu->pcm_mic = pcm;
1490
1491	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1492
1493	if (rpcm)
1494		*rpcm = pcm;
1495	return 0;
1496}
1497
1498static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1499{
1500	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1501	int nefx = emu->audigy ? 64 : 32;
1502	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1503	uinfo->count = nefx;
1504	uinfo->value.integer.min = 0;
1505	uinfo->value.integer.max = 1;
1506	return 0;
1507}
1508
1509static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1510{
1511	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1512	int nefx = emu->audigy ? 64 : 32;
1513	int idx;
1514	
1515	spin_lock_irq(&emu->reg_lock);
1516	for (idx = 0; idx < nefx; idx++)
1517		ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1518	spin_unlock_irq(&emu->reg_lock);
1519	return 0;
1520}
1521
1522static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1523{
1524	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1525	unsigned int nval[2], bits;
1526	int nefx = emu->audigy ? 64 : 32;
1527	int nefxb = emu->audigy ? 7 : 6;
1528	int change, idx;
1529	
1530	nval[0] = nval[1] = 0;
1531	for (idx = 0, bits = 0; idx < nefx; idx++)
1532		if (ucontrol->value.integer.value[idx]) {
1533			nval[idx / 32] |= 1 << (idx % 32);
1534			bits++;
1535		}
1536		
1537	for (idx = 0; idx < nefxb; idx++)
1538		if (1 << idx == bits)
1539			break;
1540	
1541	if (idx >= nefxb)
1542		return -EINVAL;
1543
1544	spin_lock_irq(&emu->reg_lock);
1545	change = (nval[0] != emu->efx_voices_mask[0]) ||
1546		(nval[1] != emu->efx_voices_mask[1]);
1547	emu->efx_voices_mask[0] = nval[0];
1548	emu->efx_voices_mask[1] = nval[1];
1549	spin_unlock_irq(&emu->reg_lock);
1550	return change;
1551}
1552
1553static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1554	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1555	.name = "Captured FX8010 Outputs",
1556	.info = snd_emu10k1_pcm_efx_voices_mask_info,
1557	.get = snd_emu10k1_pcm_efx_voices_mask_get,
1558	.put = snd_emu10k1_pcm_efx_voices_mask_put
1559};
1560
1561static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1562	.open =			snd_emu10k1_capture_efx_open,
1563	.close =		snd_emu10k1_capture_efx_close,
1564	.ioctl =		snd_pcm_lib_ioctl,
1565	.hw_params =		snd_emu10k1_capture_hw_params,
1566	.hw_free =		snd_emu10k1_capture_hw_free,
1567	.prepare =		snd_emu10k1_capture_prepare,
1568	.trigger =		snd_emu10k1_capture_trigger,
1569	.pointer =		snd_emu10k1_capture_pointer,
1570};
1571
1572
1573/* EFX playback */
1574
1575#define INITIAL_TRAM_SHIFT     14
1576#define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1577
1578static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1579{
1580	struct snd_pcm_substream *substream = private_data;
1581	snd_pcm_period_elapsed(substream);
1582}
1583
1584static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1585						   unsigned short *dst_right,
1586						   unsigned short *src,
1587						   unsigned int count,
1588						   unsigned int tram_shift)
1589{
1590	/*
1591	printk(KERN_DEBUG "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1592	       "src = 0x%p, count = 0x%x\n",
1593	       dst_left, dst_right, src, count);
1594	*/
1595	if ((tram_shift & 1) == 0) {
1596		while (count--) {
1597			*dst_left-- = *src++;
1598			*dst_right-- = *src++;
1599		}
1600	} else {
1601		while (count--) {
1602			*dst_right-- = *src++;
1603			*dst_left-- = *src++;
1604		}
1605	}
1606}
1607
1608static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1609				 struct snd_pcm_indirect *rec, size_t bytes)
1610{
1611	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1612	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1613	unsigned int tram_size = pcm->buffer_size;
1614	unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1615	unsigned int frames = bytes >> 2, count;
1616	unsigned int tram_pos = pcm->tram_pos;
1617	unsigned int tram_shift = pcm->tram_shift;
1618
1619	while (frames > tram_pos) {
1620		count = tram_pos + 1;
1621		snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1622						       (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1623						       src, count, tram_shift);
1624		src += count * 2;
1625		frames -= count;
1626		tram_pos = (tram_size / 2) - 1;
1627		tram_shift++;
1628	}
1629	snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1630					       (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1631					       src, frames, tram_shift);
1632	tram_pos -= frames;
1633	pcm->tram_pos = tram_pos;
1634	pcm->tram_shift = tram_shift;
1635}
1636
1637static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1638{
1639	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1640	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1641
1642	snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1643	return 0;
1644}
1645
1646static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1647						 struct snd_pcm_hw_params *hw_params)
1648{
1649	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1650}
1651
1652static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1653{
1654	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1655	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1656	unsigned int i;
1657
1658	for (i = 0; i < pcm->channels; i++)
1659		snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1660	snd_pcm_lib_free_pages(substream);
1661	return 0;
1662}
1663
1664static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1665{
1666	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1667	struct snd_pcm_runtime *runtime = substream->runtime;
1668	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1669	unsigned int i;
1670	
1671	/*
1672	printk(KERN_DEBUG "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1673	       "buffer_size = 0x%x (0x%x)\n",
1674	       emu->fx8010.etram_pages, runtime->dma_area,
1675	       runtime->buffer_size, runtime->buffer_size << 2);
1676	*/
1677	memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1678	pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1679	pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1680	pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1681	pcm->tram_shift = 0;
1682	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0);	/* reset */
1683	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);	/* reset */
1684	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1685	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0);		/* reset ptr number */
1686	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1687	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1688	for (i = 0; i < pcm->channels; i++)
1689		snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1690	return 0;
1691}
1692
1693static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1694{
1695	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1696	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1697	int result = 0;
1698
1699	spin_lock(&emu->reg_lock);
1700	switch (cmd) {
1701	case SNDRV_PCM_TRIGGER_START:
1702		/* follow thru */
1703	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1704	case SNDRV_PCM_TRIGGER_RESUME:
1705#ifdef EMU10K1_SET_AC3_IEC958
1706	{
1707		int i;
1708		for (i = 0; i < 3; i++) {
1709			unsigned int bits;
1710			bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1711			       SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1712			       0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1713			snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1714		}
1715	}
1716#endif
1717		result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1718		if (result < 0)
1719			goto __err;
1720		snd_emu10k1_fx8010_playback_transfer(substream);	/* roll the ball */
1721		snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1722		break;
1723	case SNDRV_PCM_TRIGGER_STOP:
1724	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1725	case SNDRV_PCM_TRIGGER_SUSPEND:
1726		snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1727		snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1728		pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1729		pcm->tram_shift = 0;
1730		break;
1731	default:
1732		result = -EINVAL;
1733		break;
1734	}
1735      __err:
1736	spin_unlock(&emu->reg_lock);
1737	return result;
1738}
1739
1740static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1741{
1742	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1743	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1744	size_t ptr; /* byte pointer */
1745
1746	if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1747		return 0;
1748	ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1749	return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1750}
1751
1752static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1753{
1754	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1755				 SNDRV_PCM_INFO_RESUME |
1756				 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1757	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1758	.rates =		SNDRV_PCM_RATE_48000,
1759	.rate_min =		48000,
1760	.rate_max =		48000,
1761	.channels_min =		1,
1762	.channels_max =		1,
1763	.buffer_bytes_max =	(128*1024),
1764	.period_bytes_min =	1024,
1765	.period_bytes_max =	(128*1024),
1766	.periods_min =		2,
1767	.periods_max =		1024,
1768	.fifo_size =		0,
1769};
1770
1771static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1772{
1773	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1774	struct snd_pcm_runtime *runtime = substream->runtime;
1775	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1776
1777	runtime->hw = snd_emu10k1_fx8010_playback;
1778	runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1779	runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1780	spin_lock_irq(&emu->reg_lock);
1781	if (pcm->valid == 0) {
1782		spin_unlock_irq(&emu->reg_lock);
1783		return -ENODEV;
1784	}
1785	pcm->opened = 1;
1786	spin_unlock_irq(&emu->reg_lock);
1787	return 0;
1788}
1789
1790static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1791{
1792	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1793	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1794
1795	spin_lock_irq(&emu->reg_lock);
1796	pcm->opened = 0;
1797	spin_unlock_irq(&emu->reg_lock);
1798	return 0;
1799}
1800
1801static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1802	.open =			snd_emu10k1_fx8010_playback_open,
1803	.close =		snd_emu10k1_fx8010_playback_close,
1804	.ioctl =		snd_pcm_lib_ioctl,
1805	.hw_params =		snd_emu10k1_fx8010_playback_hw_params,
1806	.hw_free =		snd_emu10k1_fx8010_playback_hw_free,
1807	.prepare =		snd_emu10k1_fx8010_playback_prepare,
1808	.trigger =		snd_emu10k1_fx8010_playback_trigger,
1809	.pointer =		snd_emu10k1_fx8010_playback_pointer,
1810	.ack =			snd_emu10k1_fx8010_playback_transfer,
1811};
1812
1813int __devinit snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1814{
1815	struct snd_pcm *pcm;
1816	struct snd_kcontrol *kctl;
1817	int err;
1818
1819	if (rpcm)
1820		*rpcm = NULL;
1821
1822	if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1823		return err;
1824
1825	pcm->private_data = emu;
1826
1827	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1828	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1829
1830	pcm->info_flags = 0;
1831	strcpy(pcm->name, "Multichannel Capture/PT Playback");
1832	emu->pcm_efx = pcm;
1833	if (rpcm)
1834		*rpcm = pcm;
1835
1836	/* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs 
1837	 * to these
1838	 */	
1839	
1840	/* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1841	if (emu->audigy) {
1842		emu->efx_voices_mask[0] = 0;
1843		if (emu->card_capabilities->emu_model)
1844			/* Pavel Hofman - 32 voices will be used for
1845			 * capture (write mode) -
1846			 * each bit = corresponding voice
1847			 */
1848			emu->efx_voices_mask[1] = 0xffffffff;
1849		else
1850			emu->efx_voices_mask[1] = 0xffff;
1851	} else {
1852		emu->efx_voices_mask[0] = 0xffff0000;
1853		emu->efx_voices_mask[1] = 0;
1854	}
1855	/* For emu1010, the control has to set 32 upper bits (voices)
1856	 * out of the 64 bits (voices) to true for the 16-channels capture
1857	 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1858	 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1859	 * control can override this register's value.
1860	 */
1861	kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1862	if (!kctl)
1863		return -ENOMEM;
1864	kctl->id.device = device;
1865	snd_ctl_add(emu->card, kctl);
1866
1867	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1868
1869	return 0;
1870}