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