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