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