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