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