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