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