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