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