xref: /openbmc/linux/sound/pci/emu10k1/emupcm.c (revision e72e8bf1)
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 		/* fall through */
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 
906 		/* fall through */
907 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
908 	case SNDRV_PCM_TRIGGER_RESUME:
909 		snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
910 		snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
911 						   &emu->efx_pcm_mixer[0]);
912 		for (i = 1; i < NUM_EFX_PLAYBACK; i++)
913 			snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
914 							   &emu->efx_pcm_mixer[i]);
915 		snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
916 		snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
917 		for (i = 1; i < NUM_EFX_PLAYBACK; i++)
918 			snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
919 		epcm->running = 1;
920 		break;
921 	case SNDRV_PCM_TRIGGER_SUSPEND:
922 	case SNDRV_PCM_TRIGGER_STOP:
923 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
924 		epcm->running = 0;
925 		for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
926 			snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
927 		}
928 		snd_emu10k1_playback_stop_voice(emu, epcm->extra);
929 		break;
930 	default:
931 		result = -EINVAL;
932 		break;
933 	}
934 	spin_unlock(&emu->reg_lock);
935 	return result;
936 }
937 
938 
939 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
940 {
941 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
942 	struct snd_pcm_runtime *runtime = substream->runtime;
943 	struct snd_emu10k1_pcm *epcm = runtime->private_data;
944 	unsigned int ptr;
945 
946 	if (!epcm->running)
947 		return 0;
948 	if (epcm->first_ptr) {
949 		udelay(50);	/* hack, it takes awhile until capture is started */
950 		epcm->first_ptr = 0;
951 	}
952 	ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
953 	return bytes_to_frames(runtime, ptr);
954 }
955 
956 /*
957  *  Playback support device description
958  */
959 
960 static const struct snd_pcm_hardware snd_emu10k1_playback =
961 {
962 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
963 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
964 				 SNDRV_PCM_INFO_RESUME |
965 				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
966 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
967 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
968 	.rate_min =		4000,
969 	.rate_max =		96000,
970 	.channels_min =		1,
971 	.channels_max =		2,
972 	.buffer_bytes_max =	(128*1024),
973 	.period_bytes_min =	64,
974 	.period_bytes_max =	(128*1024),
975 	.periods_min =		1,
976 	.periods_max =		1024,
977 	.fifo_size =		0,
978 };
979 
980 /*
981  *  Capture support device description
982  */
983 
984 static const struct snd_pcm_hardware snd_emu10k1_capture =
985 {
986 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
987 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
988 				 SNDRV_PCM_INFO_RESUME |
989 				 SNDRV_PCM_INFO_MMAP_VALID),
990 	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
991 	.rates =		SNDRV_PCM_RATE_8000_48000,
992 	.rate_min =		8000,
993 	.rate_max =		48000,
994 	.channels_min =		1,
995 	.channels_max =		2,
996 	.buffer_bytes_max =	(64*1024),
997 	.period_bytes_min =	384,
998 	.period_bytes_max =	(64*1024),
999 	.periods_min =		2,
1000 	.periods_max =		2,
1001 	.fifo_size =		0,
1002 };
1003 
1004 static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1005 {
1006 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1007 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1008 				 SNDRV_PCM_INFO_RESUME |
1009 				 SNDRV_PCM_INFO_MMAP_VALID),
1010 	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
1011 	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1012 				 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1013 				 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1014 	.rate_min =		44100,
1015 	.rate_max =		192000,
1016 	.channels_min =		8,
1017 	.channels_max =		8,
1018 	.buffer_bytes_max =	(64*1024),
1019 	.period_bytes_min =	384,
1020 	.period_bytes_max =	(64*1024),
1021 	.periods_min =		2,
1022 	.periods_max =		2,
1023 	.fifo_size =		0,
1024 };
1025 
1026 /*
1027  *
1028  */
1029 
1030 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1031 {
1032 	struct snd_ctl_elem_id id;
1033 
1034 	if (! kctl)
1035 		return;
1036 	if (activate)
1037 		kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1038 	else
1039 		kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1040 	snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1041 		       SNDRV_CTL_EVENT_MASK_INFO,
1042 		       snd_ctl_build_ioff(&id, kctl, idx));
1043 }
1044 
1045 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1046 {
1047 	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1048 	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1049 	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1050 }
1051 
1052 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1053 {
1054 	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1055 	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1056 	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1057 }
1058 
1059 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1060 {
1061 	kfree(runtime->private_data);
1062 }
1063 
1064 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1065 {
1066 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1067 	struct snd_emu10k1_pcm_mixer *mix;
1068 	int i;
1069 
1070 	for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1071 		mix = &emu->efx_pcm_mixer[i];
1072 		mix->epcm = NULL;
1073 		snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1074 	}
1075 	return 0;
1076 }
1077 
1078 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1079 {
1080 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1081 	struct snd_emu10k1_pcm *epcm;
1082 	struct snd_emu10k1_pcm_mixer *mix;
1083 	struct snd_pcm_runtime *runtime = substream->runtime;
1084 	int i;
1085 
1086 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1087 	if (epcm == NULL)
1088 		return -ENOMEM;
1089 	epcm->emu = emu;
1090 	epcm->type = PLAYBACK_EFX;
1091 	epcm->substream = substream;
1092 
1093 	emu->pcm_playback_efx_substream = substream;
1094 
1095 	runtime->private_data = epcm;
1096 	runtime->private_free = snd_emu10k1_pcm_free_substream;
1097 	runtime->hw = snd_emu10k1_efx_playback;
1098 
1099 	for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1100 		mix = &emu->efx_pcm_mixer[i];
1101 		mix->send_routing[0][0] = i;
1102 		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1103 		mix->send_volume[0][0] = 255;
1104 		mix->attn[0] = 0xffff;
1105 		mix->epcm = epcm;
1106 		snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1107 	}
1108 	return 0;
1109 }
1110 
1111 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1112 {
1113 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1114 	struct snd_emu10k1_pcm *epcm;
1115 	struct snd_emu10k1_pcm_mixer *mix;
1116 	struct snd_pcm_runtime *runtime = substream->runtime;
1117 	int i, err, sample_rate;
1118 
1119 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1120 	if (epcm == NULL)
1121 		return -ENOMEM;
1122 	epcm->emu = emu;
1123 	epcm->type = PLAYBACK_EMUVOICE;
1124 	epcm->substream = substream;
1125 	runtime->private_data = epcm;
1126 	runtime->private_free = snd_emu10k1_pcm_free_substream;
1127 	runtime->hw = snd_emu10k1_playback;
1128 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1129 		kfree(epcm);
1130 		return err;
1131 	}
1132 	if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1133 		kfree(epcm);
1134 		return err;
1135 	}
1136 	if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
1137 		sample_rate = 44100;
1138 	else
1139 		sample_rate = 48000;
1140 	err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1141 	if (err < 0) {
1142 		kfree(epcm);
1143 		return err;
1144 	}
1145 	mix = &emu->pcm_mixer[substream->number];
1146 	for (i = 0; i < 4; i++)
1147 		mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1148 	memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1149 	mix->send_volume[0][0] = mix->send_volume[0][1] =
1150 	mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1151 	mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1152 	mix->epcm = epcm;
1153 	snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1154 	return 0;
1155 }
1156 
1157 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1158 {
1159 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1160 	struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1161 
1162 	mix->epcm = NULL;
1163 	snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1164 	return 0;
1165 }
1166 
1167 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1168 {
1169 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1170 	struct snd_pcm_runtime *runtime = substream->runtime;
1171 	struct snd_emu10k1_pcm *epcm;
1172 
1173 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1174 	if (epcm == NULL)
1175 		return -ENOMEM;
1176 	epcm->emu = emu;
1177 	epcm->type = CAPTURE_AC97ADC;
1178 	epcm->substream = substream;
1179 	epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1180 	epcm->capture_inte = INTE_ADCBUFENABLE;
1181 	epcm->capture_ba_reg = ADCBA;
1182 	epcm->capture_bs_reg = ADCBS;
1183 	epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1184 	runtime->private_data = epcm;
1185 	runtime->private_free = snd_emu10k1_pcm_free_substream;
1186 	runtime->hw = snd_emu10k1_capture;
1187 	emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1188 	emu->pcm_capture_substream = substream;
1189 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1190 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1191 	return 0;
1192 }
1193 
1194 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1195 {
1196 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1197 
1198 	emu->capture_interrupt = NULL;
1199 	emu->pcm_capture_substream = NULL;
1200 	return 0;
1201 }
1202 
1203 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1204 {
1205 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1206 	struct snd_emu10k1_pcm *epcm;
1207 	struct snd_pcm_runtime *runtime = substream->runtime;
1208 
1209 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1210 	if (epcm == NULL)
1211 		return -ENOMEM;
1212 	epcm->emu = emu;
1213 	epcm->type = CAPTURE_AC97MIC;
1214 	epcm->substream = substream;
1215 	epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1216 	epcm->capture_inte = INTE_MICBUFENABLE;
1217 	epcm->capture_ba_reg = MICBA;
1218 	epcm->capture_bs_reg = MICBS;
1219 	epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1220 	substream->runtime->private_data = epcm;
1221 	substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1222 	runtime->hw = snd_emu10k1_capture;
1223 	runtime->hw.rates = SNDRV_PCM_RATE_8000;
1224 	runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1225 	runtime->hw.channels_min = 1;
1226 	emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1227 	emu->pcm_capture_mic_substream = substream;
1228 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1229 	return 0;
1230 }
1231 
1232 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1233 {
1234 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1235 
1236 	emu->capture_interrupt = NULL;
1237 	emu->pcm_capture_mic_substream = NULL;
1238 	return 0;
1239 }
1240 
1241 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1242 {
1243 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1244 	struct snd_emu10k1_pcm *epcm;
1245 	struct snd_pcm_runtime *runtime = substream->runtime;
1246 	int nefx = emu->audigy ? 64 : 32;
1247 	int idx;
1248 
1249 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1250 	if (epcm == NULL)
1251 		return -ENOMEM;
1252 	epcm->emu = emu;
1253 	epcm->type = CAPTURE_EFX;
1254 	epcm->substream = substream;
1255 	epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1256 	epcm->capture_inte = INTE_EFXBUFENABLE;
1257 	epcm->capture_ba_reg = FXBA;
1258 	epcm->capture_bs_reg = FXBS;
1259 	epcm->capture_idx_reg = FXIDX;
1260 	substream->runtime->private_data = epcm;
1261 	substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1262 	runtime->hw = snd_emu10k1_capture_efx;
1263 	runtime->hw.rates = SNDRV_PCM_RATE_48000;
1264 	runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1265 	spin_lock_irq(&emu->reg_lock);
1266 	if (emu->card_capabilities->emu_model) {
1267 		/*  Nb. of channels has been increased to 16 */
1268 		/* TODO
1269 		 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1270 		 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1271 		 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1272 		 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1273 		 * rate_min = 44100,
1274 		 * rate_max = 192000,
1275 		 * channels_min = 16,
1276 		 * channels_max = 16,
1277 		 * Need to add mixer control to fix sample rate
1278 		 *
1279 		 * There are 32 mono channels of 16bits each.
1280 		 * 24bit Audio uses 2x channels over 16bit
1281 		 * 96kHz uses 2x channels over 48kHz
1282 		 * 192kHz uses 4x channels over 48kHz
1283 		 * So, for 48kHz 24bit, one has 16 channels
1284 		 * for 96kHz 24bit, one has 8 channels
1285 		 * for 192kHz 24bit, one has 4 channels
1286 		 *
1287 		 */
1288 #if 1
1289 		switch (emu->emu1010.internal_clock) {
1290 		case 0:
1291 			/* For 44.1kHz */
1292 			runtime->hw.rates = SNDRV_PCM_RATE_44100;
1293 			runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1294 			runtime->hw.channels_min =
1295 				runtime->hw.channels_max = 16;
1296 			break;
1297 		case 1:
1298 			/* For 48kHz */
1299 			runtime->hw.rates = SNDRV_PCM_RATE_48000;
1300 			runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1301 			runtime->hw.channels_min =
1302 				runtime->hw.channels_max = 16;
1303 			break;
1304 		}
1305 #endif
1306 #if 0
1307 		/* For 96kHz */
1308 		runtime->hw.rates = SNDRV_PCM_RATE_96000;
1309 		runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1310 		runtime->hw.channels_min = runtime->hw.channels_max = 4;
1311 #endif
1312 #if 0
1313 		/* For 192kHz */
1314 		runtime->hw.rates = SNDRV_PCM_RATE_192000;
1315 		runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1316 		runtime->hw.channels_min = runtime->hw.channels_max = 2;
1317 #endif
1318 		runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1319 		/* efx_voices_mask[0] is expected to be zero
1320  		 * efx_voices_mask[1] is expected to have 32bits set
1321 		 */
1322 	} else {
1323 		runtime->hw.channels_min = runtime->hw.channels_max = 0;
1324 		for (idx = 0; idx < nefx; idx++) {
1325 			if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1326 				runtime->hw.channels_min++;
1327 				runtime->hw.channels_max++;
1328 			}
1329 		}
1330 	}
1331 	epcm->capture_cr_val = emu->efx_voices_mask[0];
1332 	epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1333 	spin_unlock_irq(&emu->reg_lock);
1334 	emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1335 	emu->pcm_capture_efx_substream = substream;
1336 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1337 	return 0;
1338 }
1339 
1340 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1341 {
1342 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1343 
1344 	emu->capture_interrupt = NULL;
1345 	emu->pcm_capture_efx_substream = NULL;
1346 	return 0;
1347 }
1348 
1349 static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
1350 	.open =			snd_emu10k1_playback_open,
1351 	.close =		snd_emu10k1_playback_close,
1352 	.hw_params =		snd_emu10k1_playback_hw_params,
1353 	.hw_free =		snd_emu10k1_playback_hw_free,
1354 	.prepare =		snd_emu10k1_playback_prepare,
1355 	.trigger =		snd_emu10k1_playback_trigger,
1356 	.pointer =		snd_emu10k1_playback_pointer,
1357 };
1358 
1359 static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
1360 	.open =			snd_emu10k1_capture_open,
1361 	.close =		snd_emu10k1_capture_close,
1362 	.prepare =		snd_emu10k1_capture_prepare,
1363 	.trigger =		snd_emu10k1_capture_trigger,
1364 	.pointer =		snd_emu10k1_capture_pointer,
1365 };
1366 
1367 /* EFX playback */
1368 static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1369 	.open =			snd_emu10k1_efx_playback_open,
1370 	.close =		snd_emu10k1_efx_playback_close,
1371 	.hw_params =		snd_emu10k1_playback_hw_params,
1372 	.hw_free =		snd_emu10k1_efx_playback_hw_free,
1373 	.prepare =		snd_emu10k1_efx_playback_prepare,
1374 	.trigger =		snd_emu10k1_efx_playback_trigger,
1375 	.pointer =		snd_emu10k1_efx_playback_pointer,
1376 };
1377 
1378 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
1379 {
1380 	struct snd_pcm *pcm;
1381 	struct snd_pcm_substream *substream;
1382 	int err;
1383 
1384 	if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1385 		return err;
1386 
1387 	pcm->private_data = emu;
1388 
1389 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1390 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1391 
1392 	pcm->info_flags = 0;
1393 	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1394 	strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1395 	emu->pcm = pcm;
1396 
1397 	/* playback substream can't use managed buffers due to alignment */
1398 	for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1399 		snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1400 					      &emu->pci->dev,
1401 					      64*1024, 64*1024);
1402 
1403 	for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1404 		snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
1405 					   &emu->pci->dev, 64*1024, 64*1024);
1406 
1407 	return 0;
1408 }
1409 
1410 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
1411 {
1412 	struct snd_pcm *pcm;
1413 	struct snd_pcm_substream *substream;
1414 	int err;
1415 
1416 	if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1417 		return err;
1418 
1419 	pcm->private_data = emu;
1420 
1421 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1422 
1423 	pcm->info_flags = 0;
1424 	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1425 	strcpy(pcm->name, "Multichannel Playback");
1426 	emu->pcm_multi = pcm;
1427 
1428 	for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1429 		snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1430 					      &emu->pci->dev,
1431 					      64*1024, 64*1024);
1432 
1433 	return 0;
1434 }
1435 
1436 
1437 static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1438 	.open =			snd_emu10k1_capture_mic_open,
1439 	.close =		snd_emu10k1_capture_mic_close,
1440 	.prepare =		snd_emu10k1_capture_prepare,
1441 	.trigger =		snd_emu10k1_capture_trigger,
1442 	.pointer =		snd_emu10k1_capture_pointer,
1443 };
1444 
1445 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
1446 {
1447 	struct snd_pcm *pcm;
1448 	int err;
1449 
1450 	if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1451 		return err;
1452 
1453 	pcm->private_data = emu;
1454 
1455 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1456 
1457 	pcm->info_flags = 0;
1458 	strcpy(pcm->name, "Mic Capture");
1459 	emu->pcm_mic = pcm;
1460 
1461 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1462 				       64*1024, 64*1024);
1463 
1464 	return 0;
1465 }
1466 
1467 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1468 {
1469 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1470 	int nefx = emu->audigy ? 64 : 32;
1471 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1472 	uinfo->count = nefx;
1473 	uinfo->value.integer.min = 0;
1474 	uinfo->value.integer.max = 1;
1475 	return 0;
1476 }
1477 
1478 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1479 {
1480 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1481 	int nefx = emu->audigy ? 64 : 32;
1482 	int idx;
1483 
1484 	spin_lock_irq(&emu->reg_lock);
1485 	for (idx = 0; idx < nefx; idx++)
1486 		ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1487 	spin_unlock_irq(&emu->reg_lock);
1488 	return 0;
1489 }
1490 
1491 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1492 {
1493 	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1494 	unsigned int nval[2], bits;
1495 	int nefx = emu->audigy ? 64 : 32;
1496 	int nefxb = emu->audigy ? 7 : 6;
1497 	int change, idx;
1498 
1499 	nval[0] = nval[1] = 0;
1500 	for (idx = 0, bits = 0; idx < nefx; idx++)
1501 		if (ucontrol->value.integer.value[idx]) {
1502 			nval[idx / 32] |= 1 << (idx % 32);
1503 			bits++;
1504 		}
1505 
1506 	for (idx = 0; idx < nefxb; idx++)
1507 		if (1 << idx == bits)
1508 			break;
1509 
1510 	if (idx >= nefxb)
1511 		return -EINVAL;
1512 
1513 	spin_lock_irq(&emu->reg_lock);
1514 	change = (nval[0] != emu->efx_voices_mask[0]) ||
1515 		(nval[1] != emu->efx_voices_mask[1]);
1516 	emu->efx_voices_mask[0] = nval[0];
1517 	emu->efx_voices_mask[1] = nval[1];
1518 	spin_unlock_irq(&emu->reg_lock);
1519 	return change;
1520 }
1521 
1522 static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1523 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1524 	.name = "Captured FX8010 Outputs",
1525 	.info = snd_emu10k1_pcm_efx_voices_mask_info,
1526 	.get = snd_emu10k1_pcm_efx_voices_mask_get,
1527 	.put = snd_emu10k1_pcm_efx_voices_mask_put
1528 };
1529 
1530 static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1531 	.open =			snd_emu10k1_capture_efx_open,
1532 	.close =		snd_emu10k1_capture_efx_close,
1533 	.prepare =		snd_emu10k1_capture_prepare,
1534 	.trigger =		snd_emu10k1_capture_trigger,
1535 	.pointer =		snd_emu10k1_capture_pointer,
1536 };
1537 
1538 
1539 /* EFX playback */
1540 
1541 #define INITIAL_TRAM_SHIFT     14
1542 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1543 
1544 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1545 {
1546 	struct snd_pcm_substream *substream = private_data;
1547 	snd_pcm_period_elapsed(substream);
1548 }
1549 
1550 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1551 						   unsigned short *dst_right,
1552 						   unsigned short *src,
1553 						   unsigned int count,
1554 						   unsigned int tram_shift)
1555 {
1556 	/*
1557 	dev_dbg(emu->card->dev,
1558 		"tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1559 	       "src = 0x%p, count = 0x%x\n",
1560 	       dst_left, dst_right, src, count);
1561 	*/
1562 	if ((tram_shift & 1) == 0) {
1563 		while (count--) {
1564 			*dst_left-- = *src++;
1565 			*dst_right-- = *src++;
1566 		}
1567 	} else {
1568 		while (count--) {
1569 			*dst_right-- = *src++;
1570 			*dst_left-- = *src++;
1571 		}
1572 	}
1573 }
1574 
1575 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1576 				 struct snd_pcm_indirect *rec, size_t bytes)
1577 {
1578 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1579 	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1580 	unsigned int tram_size = pcm->buffer_size;
1581 	unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1582 	unsigned int frames = bytes >> 2, count;
1583 	unsigned int tram_pos = pcm->tram_pos;
1584 	unsigned int tram_shift = pcm->tram_shift;
1585 
1586 	while (frames > tram_pos) {
1587 		count = tram_pos + 1;
1588 		snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1589 						       (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1590 						       src, count, tram_shift);
1591 		src += count * 2;
1592 		frames -= count;
1593 		tram_pos = (tram_size / 2) - 1;
1594 		tram_shift++;
1595 	}
1596 	snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1597 					       (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1598 					       src, frames, tram_shift);
1599 	tram_pos -= frames;
1600 	pcm->tram_pos = tram_pos;
1601 	pcm->tram_shift = tram_shift;
1602 }
1603 
1604 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1605 {
1606 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1607 	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1608 
1609 	return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec,
1610 						  fx8010_pb_trans_copy);
1611 }
1612 
1613 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1614 {
1615 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1616 	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1617 	unsigned int i;
1618 
1619 	for (i = 0; i < pcm->channels; i++)
1620 		snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1621 	return 0;
1622 }
1623 
1624 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1625 {
1626 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1627 	struct snd_pcm_runtime *runtime = substream->runtime;
1628 	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1629 	unsigned int i;
1630 
1631 	/*
1632 	dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1633 	       "buffer_size = 0x%x (0x%x)\n",
1634 	       emu->fx8010.etram_pages, runtime->dma_area,
1635 	       runtime->buffer_size, runtime->buffer_size << 2);
1636 	*/
1637 	memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1638 	pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1639 	pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1640 	pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1641 	pcm->tram_shift = 0;
1642 	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0);	/* reset */
1643 	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);	/* reset */
1644 	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1645 	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0);		/* reset ptr number */
1646 	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1647 	snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1648 	for (i = 0; i < pcm->channels; i++)
1649 		snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1650 	return 0;
1651 }
1652 
1653 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1654 {
1655 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1656 	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1657 	int result = 0;
1658 
1659 	spin_lock(&emu->reg_lock);
1660 	switch (cmd) {
1661 	case SNDRV_PCM_TRIGGER_START:
1662 		/* follow thru */
1663 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1664 	case SNDRV_PCM_TRIGGER_RESUME:
1665 #ifdef EMU10K1_SET_AC3_IEC958
1666 	{
1667 		int i;
1668 		for (i = 0; i < 3; i++) {
1669 			unsigned int bits;
1670 			bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1671 			       SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1672 			       0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1673 			snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1674 		}
1675 	}
1676 #endif
1677 		result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1678 		if (result < 0)
1679 			goto __err;
1680 		snd_emu10k1_fx8010_playback_transfer(substream);	/* roll the ball */
1681 		snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1682 		break;
1683 	case SNDRV_PCM_TRIGGER_STOP:
1684 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1685 	case SNDRV_PCM_TRIGGER_SUSPEND:
1686 		snd_emu10k1_fx8010_unregister_irq_handler(emu, &pcm->irq);
1687 		snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1688 		pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1689 		pcm->tram_shift = 0;
1690 		break;
1691 	default:
1692 		result = -EINVAL;
1693 		break;
1694 	}
1695       __err:
1696 	spin_unlock(&emu->reg_lock);
1697 	return result;
1698 }
1699 
1700 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1701 {
1702 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1703 	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1704 	size_t ptr; /* byte pointer */
1705 
1706 	if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1707 		return 0;
1708 	ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1709 	return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1710 }
1711 
1712 static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1713 {
1714 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1715 				 SNDRV_PCM_INFO_RESUME |
1716 				 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE |
1717 				 SNDRV_PCM_INFO_SYNC_APPLPTR),
1718 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1719 	.rates =		SNDRV_PCM_RATE_48000,
1720 	.rate_min =		48000,
1721 	.rate_max =		48000,
1722 	.channels_min =		1,
1723 	.channels_max =		1,
1724 	.buffer_bytes_max =	(128*1024),
1725 	.period_bytes_min =	1024,
1726 	.period_bytes_max =	(128*1024),
1727 	.periods_min =		2,
1728 	.periods_max =		1024,
1729 	.fifo_size =		0,
1730 };
1731 
1732 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1733 {
1734 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1735 	struct snd_pcm_runtime *runtime = substream->runtime;
1736 	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1737 
1738 	runtime->hw = snd_emu10k1_fx8010_playback;
1739 	runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1740 	runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1741 	spin_lock_irq(&emu->reg_lock);
1742 	if (pcm->valid == 0) {
1743 		spin_unlock_irq(&emu->reg_lock);
1744 		return -ENODEV;
1745 	}
1746 	pcm->opened = 1;
1747 	spin_unlock_irq(&emu->reg_lock);
1748 	return 0;
1749 }
1750 
1751 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1752 {
1753 	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1754 	struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1755 
1756 	spin_lock_irq(&emu->reg_lock);
1757 	pcm->opened = 0;
1758 	spin_unlock_irq(&emu->reg_lock);
1759 	return 0;
1760 }
1761 
1762 static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1763 	.open =			snd_emu10k1_fx8010_playback_open,
1764 	.close =		snd_emu10k1_fx8010_playback_close,
1765 	.hw_free =		snd_emu10k1_fx8010_playback_hw_free,
1766 	.prepare =		snd_emu10k1_fx8010_playback_prepare,
1767 	.trigger =		snd_emu10k1_fx8010_playback_trigger,
1768 	.pointer =		snd_emu10k1_fx8010_playback_pointer,
1769 	.ack =			snd_emu10k1_fx8010_playback_transfer,
1770 };
1771 
1772 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
1773 {
1774 	struct snd_pcm *pcm;
1775 	struct snd_kcontrol *kctl;
1776 	int err;
1777 
1778 	if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1779 		return err;
1780 
1781 	pcm->private_data = emu;
1782 
1783 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1784 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1785 
1786 	pcm->info_flags = 0;
1787 	strcpy(pcm->name, "Multichannel Capture/PT Playback");
1788 	emu->pcm_efx = pcm;
1789 
1790 	/* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1791 	 * to these
1792 	 */
1793 
1794 	/* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1795 	if (emu->audigy) {
1796 		emu->efx_voices_mask[0] = 0;
1797 		if (emu->card_capabilities->emu_model)
1798 			/* Pavel Hofman - 32 voices will be used for
1799 			 * capture (write mode) -
1800 			 * each bit = corresponding voice
1801 			 */
1802 			emu->efx_voices_mask[1] = 0xffffffff;
1803 		else
1804 			emu->efx_voices_mask[1] = 0xffff;
1805 	} else {
1806 		emu->efx_voices_mask[0] = 0xffff0000;
1807 		emu->efx_voices_mask[1] = 0;
1808 	}
1809 	/* For emu1010, the control has to set 32 upper bits (voices)
1810 	 * out of the 64 bits (voices) to true for the 16-channels capture
1811 	 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1812 	 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1813 	 * control can override this register's value.
1814 	 */
1815 	kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1816 	if (!kctl)
1817 		return -ENOMEM;
1818 	kctl->id.device = device;
1819 	err = snd_ctl_add(emu->card, kctl);
1820 	if (err < 0)
1821 		return err;
1822 
1823 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1824 				       64*1024, 64*1024);
1825 
1826 	return 0;
1827 }
1828