emupcm.c 57.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds's avatar
Linus Torvalds committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *                   Creative Labs, Inc.
 *  Routines for control of EMU10K1 chips / PCM routines
 *  Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
 *
 *  BUGS:
 *    --
 *
 *  TODO:
 *    --
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/init.h>
#include <sound/core.h>
#include <sound/emu10k1.h>

37
38
static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
				      struct snd_emu10k1_voice *voice)
Linus Torvalds's avatar
Linus Torvalds committed
39
{
40
	struct snd_emu10k1_pcm *epcm;
Linus Torvalds's avatar
Linus Torvalds committed
41
42
43
44
45
46

	if ((epcm = voice->epcm) == NULL)
		return;
	if (epcm->substream == NULL)
		return;
#if 0
47
48
	dev_dbg(emu->card->dev,
		"IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
Linus Torvalds's avatar
Linus Torvalds committed
49
50
51
52
53
54
55
			epcm->substream->runtime->hw->pointer(emu, epcm->substream),
			snd_pcm_lib_period_bytes(epcm->substream),
			snd_pcm_lib_buffer_bytes(epcm->substream));
#endif
	snd_pcm_period_elapsed(epcm->substream);
}

56
57
static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
					      unsigned int status)
Linus Torvalds's avatar
Linus Torvalds committed
58
59
60
61
62
63
64
65
66
67
{
#if 0
	if (status & IPR_ADCBUFHALFFULL) {
		if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
			return;
	}
#endif
	snd_pcm_period_elapsed(emu->pcm_capture_substream);
}

68
69
static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
					      unsigned int status)
Linus Torvalds's avatar
Linus Torvalds committed
70
71
72
73
74
75
76
77
78
79
{
#if 0
	if (status & IPR_MICBUFHALFFULL) {
		if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
			return;
	}
#endif
	snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
}

80
81
static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
					  unsigned int status)
Linus Torvalds's avatar
Linus Torvalds committed
82
83
84
85
86
87
88
89
90
91
{
#if 0
	if (status & IPR_EFXBUFHALFFULL) {
		if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
			return;
	}
#endif
	snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
}	 

92
static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
93
{
94
95
96
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
97
98
99
100
101
102
103
104
105
106
107
108
	unsigned int ptr;

	if (!epcm->running)
		return 0;
	ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
	ptr += runtime->buffer_size;
	ptr -= epcm->ccca_start_addr;
	ptr %= runtime->buffer_size;

	return ptr;
}

109
static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
Linus Torvalds's avatar
Linus Torvalds committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
	int err, i;

	if (epcm->voices[1] != NULL && voices < 2) {
		snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
		epcm->voices[1] = NULL;
	}
	for (i = 0; i < voices; i++) {
		if (epcm->voices[i] == NULL)
			break;
	}
	if (i == voices)
		return 0; /* already allocated */

	for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
		if (epcm->voices[i]) {
			snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
			epcm->voices[i] = NULL;
		}
	}
	err = snd_emu10k1_voice_alloc(epcm->emu,
				      epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
				      voices,
				      &epcm->voices[0]);
	
	if (err < 0)
		return err;
	epcm->voices[0]->epcm = epcm;
	if (voices > 1) {
		for (i = 1; i < voices; i++) {
			epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
			epcm->voices[i]->epcm = epcm;
		}
	}
	if (epcm->extra == NULL) {
		err = snd_emu10k1_voice_alloc(epcm->emu,
					      epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
					      1,
					      &epcm->extra);
		if (err < 0) {
150
			/*
151
			dev_dbg(emu->card->dev, "pcm_channel_alloc: "
152
153
154
			       "failed extra: voices=%d, frame=%d\n",
			       voices, frame);
			*/
Linus Torvalds's avatar
Linus Torvalds committed
155
156
157
158
159
160
161
162
163
164
165
166
			for (i = 0; i < voices; i++) {
				snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
				epcm->voices[i] = NULL;
			}
			return err;
		}
		epcm->extra->epcm = epcm;
		epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
	}
	return 0;
}

167
static const unsigned int capture_period_sizes[31] = {
Linus Torvalds's avatar
Linus Torvalds committed
168
169
170
171
172
173
174
175
176
177
	384,	448,	512,	640,
	384*2,	448*2,	512*2,	640*2,
	384*4,	448*4,	512*4,	640*4,
	384*8,	448*8,	512*8,	640*8,
	384*16,	448*16,	512*16,	640*16,
	384*32,	448*32,	512*32,	640*32,
	384*64,	448*64,	512*64,	640*64,
	384*128,448*128,512*128
};

178
static const struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
Linus Torvalds's avatar
Linus Torvalds committed
179
180
181
182
183
	.count = 31,
	.list = capture_period_sizes,
	.mask = 0
};

184
static const unsigned int capture_rates[8] = {
Linus Torvalds's avatar
Linus Torvalds committed
185
186
187
	8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
};

188
static const struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
Linus Torvalds's avatar
Linus Torvalds committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	.count = 8,
	.list = capture_rates,
	.mask = 0
};

static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
{
	switch (rate) {
	case 8000:	return ADCCR_SAMPLERATE_8;
	case 11025:	return ADCCR_SAMPLERATE_11;
	case 16000:	return ADCCR_SAMPLERATE_16;
	case 22050:	return ADCCR_SAMPLERATE_22;
	case 24000:	return ADCCR_SAMPLERATE_24;
	case 32000:	return ADCCR_SAMPLERATE_32;
	case 44100:	return ADCCR_SAMPLERATE_44;
	case 48000:	return ADCCR_SAMPLERATE_48;
	default:
			snd_BUG();
			return ADCCR_SAMPLERATE_8;
	}
}

static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
{
	switch (rate) {
	case 8000:	return A_ADCCR_SAMPLERATE_8;
	case 11025:	return A_ADCCR_SAMPLERATE_11;
	case 12000:	return A_ADCCR_SAMPLERATE_12; /* really supported? */
	case 16000:	return ADCCR_SAMPLERATE_16;
	case 22050:	return ADCCR_SAMPLERATE_22;
	case 24000:	return ADCCR_SAMPLERATE_24;
	case 32000:	return ADCCR_SAMPLERATE_32;
	case 44100:	return ADCCR_SAMPLERATE_44;
	case 48000:	return ADCCR_SAMPLERATE_48;
	default:
			snd_BUG();
			return A_ADCCR_SAMPLERATE_8;
	}
}

static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
{
	unsigned int pitch_target;

	pitch_target = (rate << 8) / 375;
	pitch_target = (pitch_target >> 1) + (pitch_target & 1);
	return pitch_target;
}

#define PITCH_48000 0x00004000
#define PITCH_96000 0x00008000
#define PITCH_85000 0x00007155
#define PITCH_80726 0x00006ba2
#define PITCH_67882 0x00005a82
#define PITCH_57081 0x00004c1c

static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
{
	if (pitch_target == PITCH_48000)
		return CCCA_INTERPROM_0;
	else if (pitch_target < PITCH_48000)
		return CCCA_INTERPROM_1;
	else if (pitch_target >= PITCH_96000)
		return CCCA_INTERPROM_0;
	else if (pitch_target >= PITCH_85000)
		return CCCA_INTERPROM_6;
	else if (pitch_target >= PITCH_80726)
		return CCCA_INTERPROM_5;
	else if (pitch_target >= PITCH_67882)
		return CCCA_INTERPROM_4;
	else if (pitch_target >= PITCH_57081)
		return CCCA_INTERPROM_3;
	else  
		return CCCA_INTERPROM_2;
}

/*
 * calculate cache invalidate size 
 *
 * stereo: channel is stereo
 * w_16: using 16bit samples
 *
 * returns: cache invalidate size in samples
 */
273
static inline int emu10k1_ccis(int stereo, int w_16)
Linus Torvalds's avatar
Linus Torvalds committed
274
275
276
277
278
279
280
281
{
	if (w_16) {
		return stereo ? 24 : 26;
	} else {
		return stereo ? 24*2 : 26*2;
	}
}

282
static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
Linus Torvalds's avatar
Linus Torvalds committed
283
				       int master, int extra,
284
				       struct snd_emu10k1_voice *evoice,
Linus Torvalds's avatar
Linus Torvalds committed
285
286
				       unsigned int start_addr,
				       unsigned int end_addr,
287
				       struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds's avatar
Linus Torvalds committed
288
{
289
290
	struct snd_pcm_substream *substream = evoice->epcm->substream;
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
	unsigned int silent_page, tmp;
	int voice, stereo, w_16;
	unsigned char attn, send_amount[8];
	unsigned char send_routing[8];
	unsigned long flags;
	unsigned int pitch_target;
	unsigned int ccis;

	voice = evoice->number;
	stereo = runtime->channels == 2;
	w_16 = snd_pcm_format_width(runtime->format) == 16;

	if (!extra && stereo) {
		start_addr >>= 1;
		end_addr >>= 1;
	}
	if (w_16) {
		start_addr >>= 1;
		end_addr >>= 1;
	}

	spin_lock_irqsave(&emu->reg_lock, flags);

	/* volume parameters */
	if (extra) {
		attn = 0;
		memset(send_routing, 0, sizeof(send_routing));
		send_routing[0] = 0;
		send_routing[1] = 1;
		send_routing[2] = 2;
		send_routing[3] = 3;
		memset(send_amount, 0, sizeof(send_amount));
	} else {
		/* mono, left, right (master voice = left) */
		tmp = stereo ? (master ? 1 : 2) : 0;
		memcpy(send_routing, &mix->send_routing[tmp][0], 8);
		memcpy(send_amount, &mix->send_volume[tmp][0], 8);
	}

	ccis = emu10k1_ccis(stereo, w_16);
	
	if (master) {
		evoice->epcm->ccca_start_addr = start_addr + ccis;
		if (extra) {
			start_addr += ccis;
336
			end_addr += ccis + emu->delay_pcm_irq;
Linus Torvalds's avatar
Linus Torvalds committed
337
338
339
340
341
342
343
344
345
		}
		if (stereo && !extra) {
			snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
			snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
		} else {
			snd_emu10k1_ptr_write(emu, CPF, voice, 0);
		}
	}

346
	/* setup routing */
Linus Torvalds's avatar
Linus Torvalds committed
347
348
349
350
351
352
353
354
355
356
357
358
359
	if (emu->audigy) {
		snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
				      snd_emu10k1_compose_audigy_fxrt1(send_routing));
		snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
				      snd_emu10k1_compose_audigy_fxrt2(send_routing));
		snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
				      ((unsigned int)send_amount[4] << 24) |
				      ((unsigned int)send_amount[5] << 16) |
				      ((unsigned int)send_amount[6] << 8) |
				      (unsigned int)send_amount[7]);
	} else
		snd_emu10k1_ptr_write(emu, FXRT, voice,
				      snd_emu10k1_compose_send_routing(send_routing));
360
361
	/* Stop CA */
	/* Assumption that PT is already 0 so no harm overwriting */
Linus Torvalds's avatar
Linus Torvalds committed
362
363
	snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
	snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
364
365
366
	snd_emu10k1_ptr_write(emu, PSST, voice,
			(start_addr + (extra ? emu->delay_pcm_irq : 0)) |
			(send_amount[2] << 24));
367
	if (emu->card_capabilities->emu_model)
368
369
370
		pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
	else 
		pitch_target = emu10k1_calc_pitch_target(runtime->rate);
Linus Torvalds's avatar
Linus Torvalds committed
371
372
373
374
375
376
377
378
	if (extra)
		snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
			      emu10k1_select_interprom(pitch_target) |
			      (w_16 ? 0 : CCCA_8BITSELECT));
	else
		snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
			      emu10k1_select_interprom(pitch_target) |
			      (w_16 ? 0 : CCCA_8BITSELECT));
379
	/* Clear filter delay memory */
Linus Torvalds's avatar
Linus Torvalds committed
380
381
	snd_emu10k1_ptr_write(emu, Z1, voice, 0);
	snd_emu10k1_ptr_write(emu, Z2, voice, 0);
382
	/* invalidate maps */
383
	silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
Linus Torvalds's avatar
Linus Torvalds committed
384
385
	snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
	snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
386
	/* modulation envelope */
Linus Torvalds's avatar
Linus Torvalds committed
387
388
389
390
391
392
393
394
395
396
	snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
	snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
	snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
	snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
	snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
	snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
	snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
	snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
	snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
	snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
397
	/* volume envelope */
Linus Torvalds's avatar
Linus Torvalds committed
398
399
	snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
	snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
400
	/* filter envelope */
Linus Torvalds's avatar
Linus Torvalds committed
401
	snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
402
	/* pitch envelope */
Linus Torvalds's avatar
Linus Torvalds committed
403
404
405
406
407
	snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);

	spin_unlock_irqrestore(&emu->reg_lock, flags);
}

408
409
static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
					  struct snd_pcm_hw_params *hw_params)
Linus Torvalds's avatar
Linus Torvalds committed
410
{
411
412
413
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
414
	size_t alloc_size;
Linus Torvalds's avatar
Linus Torvalds committed
415
416
417
418
	int err;

	if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
		return err;
419
420
421
422
423
424

	alloc_size = params_buffer_bytes(hw_params);
	if (emu->iommu_workaround)
		alloc_size += EMUPAGESIZE;
	err = snd_pcm_lib_malloc_pages(substream, alloc_size);
	if (err < 0)
Linus Torvalds's avatar
Linus Torvalds committed
425
		return err;
426
427
	if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
		runtime->dma_bytes -= EMUPAGESIZE;
Linus Torvalds's avatar
Linus Torvalds committed
428
	if (err > 0) {	/* change */
429
		int mapped;
Linus Torvalds's avatar
Linus Torvalds committed
430
431
		if (epcm->memblk != NULL)
			snd_emu10k1_free_pages(emu, epcm->memblk);
432
433
434
		epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
		epcm->start_addr = 0;
		if (! epcm->memblk)
Linus Torvalds's avatar
Linus Torvalds committed
435
			return -ENOMEM;
436
		mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
437
438
439
		if (mapped < 0)
			return -ENOMEM;
		epcm->start_addr = mapped << PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
440
441
442
443
	}
	return 0;
}

444
static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
445
{
446
447
448
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm;
Linus Torvalds's avatar
Linus Torvalds committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

	if (runtime->private_data == NULL)
		return 0;
	epcm = runtime->private_data;
	if (epcm->extra) {
		snd_emu10k1_voice_free(epcm->emu, epcm->extra);
		epcm->extra = NULL;
	}
	if (epcm->voices[1]) {
		snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
		epcm->voices[1] = NULL;
	}
	if (epcm->voices[0]) {
		snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
		epcm->voices[0] = NULL;
	}
	if (epcm->memblk) {
		snd_emu10k1_free_pages(emu, epcm->memblk);
		epcm->memblk = NULL;
		epcm->start_addr = 0;
	}
	snd_pcm_lib_free_pages(substream);
	return 0;
}

474
static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
475
{
476
477
478
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm;
Linus Torvalds's avatar
Linus Torvalds committed
479
480
481
482
483
484
485
486
487
	int i;

	if (runtime->private_data == NULL)
		return 0;
	epcm = runtime->private_data;
	if (epcm->extra) {
		snd_emu10k1_voice_free(epcm->emu, epcm->extra);
		epcm->extra = NULL;
	}
488
	for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
		if (epcm->voices[i]) {
			snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
			epcm->voices[i] = NULL;
		}
	}
	if (epcm->memblk) {
		snd_emu10k1_free_pages(emu, epcm->memblk);
		epcm->memblk = NULL;
		epcm->start_addr = 0;
	}
	snd_pcm_lib_free_pages(substream);
	return 0;
}

503
static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
504
{
505
506
507
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
	unsigned int start_addr, end_addr;

	start_addr = epcm->start_addr;
	end_addr = snd_pcm_lib_period_bytes(substream);
	if (runtime->channels == 2) {
		start_addr >>= 1;
		end_addr >>= 1;
	}
	end_addr += start_addr;
	snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
				   start_addr, end_addr, NULL);
	start_addr = epcm->start_addr;
	end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
	snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
				   start_addr, end_addr,
				   &emu->pcm_mixer[substream->number]);
	if (epcm->voices[1])
		snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
					   start_addr, end_addr,
					   &emu->pcm_mixer[substream->number]);
	return 0;
}

531
static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
532
{
533
534
535
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
	unsigned int start_addr, end_addr;
	unsigned int channel_size;
	int i;

	start_addr = epcm->start_addr;
	end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);

	/*
	 * the kX driver leaves some space between voices
	 */
	channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;

	snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
				   start_addr, start_addr + (channel_size / 2), NULL);

	/* only difference with the master voice is we use it for the pointer */
	snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
				   start_addr, start_addr + channel_size,
				   &emu->efx_pcm_mixer[0]);

	start_addr += channel_size;
	for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
		snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
					   start_addr, start_addr + channel_size,
					   &emu->efx_pcm_mixer[i]);
		start_addr += channel_size;
	}

	return 0;
}

567
static const struct snd_pcm_hardware snd_emu10k1_efx_playback =
Linus Torvalds's avatar
Linus Torvalds committed
568
569
570
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai's avatar
Takashi Iwai committed
571
				 SNDRV_PCM_INFO_RESUME |
Linus Torvalds's avatar
Linus Torvalds committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_48000,
	.rate_min =		48000,
	.rate_max =		48000,
	.channels_min =		NUM_EFX_PLAYBACK,
	.channels_max =		NUM_EFX_PLAYBACK,
	.buffer_bytes_max =	(64*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(64*1024),
	.periods_min =		2,
	.periods_max =		2,
	.fifo_size =		0,
};

587
588
static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
					 struct snd_pcm_hw_params *hw_params)
Linus Torvalds's avatar
Linus Torvalds committed
589
590
591
592
{
	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
}

593
static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
594
595
596
597
{
	return snd_pcm_lib_free_pages(substream);
}

598
static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
599
{
600
601
602
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
	int idx;

	/* zeroing the buffer size will stop capture */
	snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
	switch (epcm->type) {
	case CAPTURE_AC97ADC:
		snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
		break;
	case CAPTURE_EFX:
		if (emu->audigy) {
			snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
			snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
		} else
			snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
		break;
	default:
		break;
	}	
	snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
	epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
	epcm->capture_bs_val = 0;
	for (idx = 0; idx < 31; idx++) {
		if (capture_period_sizes[idx] == epcm->capture_bufsize) {
			epcm->capture_bs_val = idx + 1;
			break;
		}
	}
	if (epcm->capture_bs_val == 0) {
		snd_BUG();
		epcm->capture_bs_val++;
	}
	if (epcm->type == CAPTURE_AC97ADC) {
		epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
		if (runtime->channels > 1)
			epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
		epcm->capture_cr_val |= emu->audigy ?
			snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
			snd_emu10k1_capture_rate_reg(runtime->rate);
	}
	return 0;
}

645
static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
Linus Torvalds's avatar
Linus Torvalds committed
646
{
647
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
648
649
650
651
652
653
654
655
656
	unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;

	if (evoice == NULL)
		return;
	runtime = evoice->epcm->substream->runtime;
	voice = evoice->number;
	stereo = (!extra && runtime->channels == 2);
	sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
	ccis = emu10k1_ccis(stereo, sample == 0);
657
	/* set cs to 2 * number of cache registers beside the invalidated */
Linus Torvalds's avatar
Linus Torvalds committed
658
659
660
661
662
663
664
665
	cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
	if (cs > 16) cs = 16;
	for (i = 0; i < cs; i++) {
		snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
		if (stereo) {
			snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
		}
	}
666
	/* reset cache */
Linus Torvalds's avatar
Linus Torvalds committed
667
668
669
670
671
672
	snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
	snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
	if (stereo) {
		snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
		snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
	}
673
	/* fill cache */
Linus Torvalds's avatar
Linus Torvalds committed
674
675
676
677
678
679
	snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
	if (stereo) {
		snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
	}
}

680
static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
Linus Torvalds's avatar
Linus Torvalds committed
681
					       int master, int extra,
682
					       struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds's avatar
Linus Torvalds committed
683
{
684
685
	struct snd_pcm_substream *substream;
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
	unsigned int attn, vattn;
	unsigned int voice, tmp;

	if (evoice == NULL)	/* skip second voice for mono */
		return;
	substream = evoice->epcm->substream;
	runtime = substream->runtime;
	voice = evoice->number;

	attn = extra ? 0 : 0x00ff;
	tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
	vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
	snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
	snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
	snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
	snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
	snd_emu10k1_voice_clear_loop_stop(emu, voice);
}	

705
static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
Linus Torvalds's avatar
Linus Torvalds committed
706
{
707
708
	struct snd_pcm_substream *substream;
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
709
710
711
712
713
714
715
716
717
	unsigned int voice, pitch, pitch_target;

	if (evoice == NULL)	/* skip second voice for mono */
		return;
	substream = evoice->epcm->substream;
	runtime = substream->runtime;
	voice = evoice->number;

	pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
718
	if (emu->card_capabilities->emu_model)
719
720
721
		pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
	else 
		pitch_target = emu10k1_calc_pitch_target(runtime->rate);
Linus Torvalds's avatar
Linus Torvalds committed
722
723
724
725
726
727
728
729
	snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
	if (master || evoice->epcm->type == PLAYBACK_EFX)
		snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
	snd_emu10k1_ptr_write(emu, IP, voice, pitch);
	if (extra)
		snd_emu10k1_voice_intr_enable(emu, voice);
}

730
static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
Linus Torvalds's avatar
Linus Torvalds committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
{
	unsigned int voice;

	if (evoice == NULL)
		return;
	voice = evoice->number;
	snd_emu10k1_voice_intr_disable(emu, voice);
	snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
	snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
	snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
	snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
	snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
	snd_emu10k1_ptr_write(emu, IP, voice, 0);
}

746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
		struct snd_emu10k1_pcm *epcm,
		struct snd_pcm_substream *substream,
		struct snd_pcm_runtime *runtime)
{
	unsigned int ptr, period_pos;

	/* try to sychronize the current position for the interrupt
	   source voice */
	period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
	period_pos %= runtime->period_size;
	ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
	ptr &= ~0x00ffffff;
	ptr |= epcm->ccca_start_addr + period_pos;
	snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
}

763
static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
764
765
				        int cmd)
{
766
767
768
769
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
	struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds's avatar
Linus Torvalds committed
770
771
	int result = 0;

772
	/*
773
774
	dev_dbg(emu->card->dev,
		"trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
775
776
	       (int)emu, cmd, substream->ops->pointer(substream))
	*/
Linus Torvalds's avatar
Linus Torvalds committed
777
778
779
780
781
782
783
	spin_lock(&emu->reg_lock);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);	/* do we need this? */
		snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
		/* follow thru */
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai's avatar
Takashi Iwai committed
784
	case SNDRV_PCM_TRIGGER_RESUME:
785
786
		if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
			snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
Linus Torvalds's avatar
Linus Torvalds committed
787
788
789
790
791
792
793
794
795
796
797
		mix = &emu->pcm_mixer[substream->number];
		snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
		snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
		snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
		snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
		snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
		snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
		epcm->running = 1;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai's avatar
Takashi Iwai committed
798
	case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds's avatar
Linus Torvalds committed
799
800
801
802
803
804
805
806
807
808
809
810
811
		epcm->running = 0;
		snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
		snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
		snd_emu10k1_playback_stop_voice(emu, epcm->extra);
		break;
	default:
		result = -EINVAL;
		break;
	}
	spin_unlock(&emu->reg_lock);
	return result;
}

812
static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
813
814
				       int cmd)
{
815
816
817
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
818
819
820
821
822
	int result = 0;

	spin_lock(&emu->reg_lock);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
Takashi Iwai's avatar
Takashi Iwai committed
823
	case SNDRV_PCM_TRIGGER_RESUME:
824
		/* hmm this should cause full and half full interrupt to be raised? */
Linus Torvalds's avatar
Linus Torvalds committed
825
826
		outl(epcm->capture_ipr, emu->port + IPR);
		snd_emu10k1_intr_enable(emu, epcm->capture_inte);
827
		/*
828
		dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
829
830
		       epcm->adccr, epcm->adcbs);
		*/
Linus Torvalds's avatar
Linus Torvalds committed
831
832
833
834
835
836
837
838
		switch (epcm->type) {
		case CAPTURE_AC97ADC:
			snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
			break;
		case CAPTURE_EFX:
			if (emu->audigy) {
				snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
				snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
839
840
841
842
				dev_dbg(emu->card->dev,
					"cr_val=0x%x, cr_val2=0x%x\n",
					epcm->capture_cr_val,
					epcm->capture_cr_val2);
Linus Torvalds's avatar
Linus Torvalds committed
843
844
845
846
847
848
849
850
851
852
853
			} else
				snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
			break;
		default:	
			break;
		}
		snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
		epcm->running = 1;
		epcm->first_ptr = 1;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai's avatar
Takashi Iwai committed
854
	case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds's avatar
Linus Torvalds committed
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
		epcm->running = 0;
		snd_emu10k1_intr_disable(emu, epcm->capture_inte);
		outl(epcm->capture_ipr, emu->port + IPR);
		snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
		switch (epcm->type) {
		case CAPTURE_AC97ADC:
			snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
			break;
		case CAPTURE_EFX:
			if (emu->audigy) {
				snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
				snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
			} else
				snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
			break;
		default:
			break;
		}
		break;
	default:
		result = -EINVAL;
	}
	spin_unlock(&emu->reg_lock);
	return result;
}

881
static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
882
{
883
884
885
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
	unsigned int ptr;

	if (!epcm->running)
		return 0;
	ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
#if 0	/* Perex's code */
	ptr += runtime->buffer_size;
	ptr -= epcm->ccca_start_addr;
	ptr %= runtime->buffer_size;
#else	/* EMU10K1 Open Source code from Creative */
	if (ptr < epcm->ccca_start_addr)
		ptr += runtime->buffer_size - epcm->ccca_start_addr;
	else {
		ptr -= epcm->ccca_start_addr;
		if (ptr >= runtime->buffer_size)
			ptr -= runtime->buffer_size;
	}
#endif
904
	/*
905
	dev_dbg(emu->card->dev,
906
907
908
	       "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
	       (long)ptr, (long)runtime->buffer_size,
	       (long)runtime->period_size);
909
	*/
Linus Torvalds's avatar
Linus Torvalds committed
910
911
912
913
	return ptr;
}


914
static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
915
916
				        int cmd)
{
917
918
919
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
920
921
922
923
924
925
	int i;
	int result = 0;

	spin_lock(&emu->reg_lock);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
926
		/* prepare voices */
Linus Torvalds's avatar
Linus Torvalds committed
927
928
929
930
931
932
933
		for (i = 0; i < NUM_EFX_PLAYBACK; i++) {	
			snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
		}
		snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);

		/* follow thru */
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai's avatar
Takashi Iwai committed
934
	case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds's avatar
Linus Torvalds committed
935
936
937
938
939
940
941
942
943
944
945
946
		snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
		snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
						   &emu->efx_pcm_mixer[0]);
		for (i = 1; i < NUM_EFX_PLAYBACK; i++)
			snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
							   &emu->efx_pcm_mixer[i]);
		snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
		snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
		for (i = 1; i < NUM_EFX_PLAYBACK; i++)
			snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
		epcm->running = 1;
		break;
Takashi Iwai's avatar
Takashi Iwai committed
947
	case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds's avatar
Linus Torvalds committed
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		epcm->running = 0;
		for (i = 0; i < NUM_EFX_PLAYBACK; i++) {	
			snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
		}
		snd_emu10k1_playback_stop_voice(emu, epcm->extra);
		break;
	default:
		result = -EINVAL;
		break;
	}
	spin_unlock(&emu->reg_lock);
	return result;
}


965
static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
966
{
967
968
969
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
970
971
972
973
974
	unsigned int ptr;

	if (!epcm->running)
		return 0;
	if (epcm->first_ptr) {
975
		udelay(50);	/* hack, it takes awhile until capture is started */
Linus Torvalds's avatar
Linus Torvalds committed
976
977
978
979
980
981
982
983
984
985
		epcm->first_ptr = 0;
	}
	ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
	return bytes_to_frames(runtime, ptr);
}

/*
 *  Playback support device description
 */

986
static const struct snd_pcm_hardware snd_emu10k1_playback =
Linus Torvalds's avatar
Linus Torvalds committed
987
988
989
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai's avatar
Takashi Iwai committed
990
				 SNDRV_PCM_INFO_RESUME |
Linus Torvalds's avatar
Linus Torvalds committed
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
	.rate_min =		4000,
	.rate_max =		96000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(128*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(128*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

/*
 *  Capture support device description
 */

1010
static const struct snd_pcm_hardware snd_emu10k1_capture =
Linus Torvalds's avatar
Linus Torvalds committed
1011
1012
1013
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai's avatar
Takashi Iwai committed
1014
				 SNDRV_PCM_INFO_RESUME |
Linus Torvalds's avatar
Linus Torvalds committed
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
				 SNDRV_PCM_INFO_MMAP_VALID),
	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_8000_48000,
	.rate_min =		8000,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(64*1024),
	.period_bytes_min =	384,
	.period_bytes_max =	(64*1024),
	.periods_min =		2,
	.periods_max =		2,
	.fifo_size =		0,
};

1030
static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
				 SNDRV_PCM_INFO_RESUME |
				 SNDRV_PCM_INFO_MMAP_VALID),
	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 
				 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | 
				 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
	.rate_min =		44100,
	.rate_max =		192000,
	.channels_min =		8,
	.channels_max =		8,
	.buffer_bytes_max =	(64*1024),
	.period_bytes_min =	384,
	.period_bytes_max =	(64*1024),
	.periods_min =		2,
	.periods_max =		2,
	.fifo_size =		0,
};

Linus Torvalds's avatar
Linus Torvalds committed
1052
1053
1054
1055
/*
 *
 */

1056
static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
Linus Torvalds's avatar
Linus Torvalds committed
1057
{
1058
	struct snd_ctl_elem_id id;
Linus Torvalds's avatar
Linus Torvalds committed
1059

1060
1061
	if (! kctl)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
	if (activate)
		kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
	else
		kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
	snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
		       SNDRV_CTL_EVENT_MASK_INFO,
		       snd_ctl_build_ioff(&id, kctl, idx));
}

1071
static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds's avatar
Linus Torvalds committed
1072
1073
1074
1075
1076
1077
{
	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
}

1078
static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds's avatar
Linus Torvalds committed
1079
1080
1081
1082
1083
1084
{
	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
	snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
}

1085
static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds's avatar
Linus Torvalds committed
1086
{
1087
	kfree(runtime->private_data);
Linus Torvalds's avatar
Linus Torvalds committed
1088
1089
}

1090
static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1091
{
1092
1093
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds's avatar
Linus Torvalds committed
1094
1095
	int i;

1096
	for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1097
1098
1099
1100
1101
1102
1103
		mix = &emu->efx_pcm_mixer[i];
		mix->epcm = NULL;
		snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
	}
	return 0;
}

1104
static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1105
{
1106
1107
1108
1109
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_emu10k1_pcm *epcm;
	struct snd_emu10k1_pcm_mixer *mix;
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1110
1111
	int i;

1112
	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
	if (epcm == NULL)
		return -ENOMEM;
	epcm->emu = emu;
	epcm->type = PLAYBACK_EFX;
	epcm->substream = substream;
	
	emu->pcm_playback_efx_substream = substream;

	runtime->private_data = epcm;
	runtime->private_free = snd_emu10k1_pcm_free_substream;
	runtime->hw = snd_emu10k1_efx_playback;
	
1125
	for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
		mix = &emu->efx_pcm_mixer[i];
		mix->send_routing[0][0] = i;
		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
		mix->send_volume[0][0] = 255;
		mix->attn[0] = 0xffff;
		mix->epcm = epcm;
		snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
	}
	return 0;
}

1137
static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1138
{
1139
1140
1141
1142
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_emu10k1_pcm *epcm;
	struct snd_emu10k1_pcm_mixer *mix;
	struct snd_pcm_runtime *runtime = substream->runtime;
1143
	int i, err, sample_rate;
Linus Torvalds's avatar
Linus Torvalds committed
1144

1145
	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
	if (epcm == NULL)
		return -ENOMEM;
	epcm->emu = emu;
	epcm->type = PLAYBACK_EMUVOICE;
	epcm->substream = substream;
	runtime->private_data = epcm;
	runtime->private_free = snd_emu10k1_pcm_free_substream;
	runtime->hw = snd_emu10k1_playback;
	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
		kfree(epcm);
		return err;
	}
	if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
		kfree(epcm);
		return err;
	}
1162
1163
1164
1165
1166
	if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
		sample_rate = 44100;
	else
		sample_rate = 48000;
	err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1167
1168
1169
1170
	if (err < 0) {
		kfree(epcm);
		return err;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
	mix = &emu->pcm_mixer[substream->number];
	for (i = 0; i < 4; i++)
		mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
	memset(&mix->send_volume, 0, sizeof(mix->send_volume));
	mix->send_volume[0][0] = mix->send_volume[0][1] =
	mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
	mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
	mix->epcm = epcm;
	snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
	return 0;
}

1183
static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1184
{
1185
1186
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
Linus Torvalds's avatar
Linus Torvalds committed
1187
1188
1189
1190
1191
1192

	mix->epcm = NULL;
	snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
	return 0;
}

1193
static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1194
{
1195
1196
1197
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_emu10k1_pcm *epcm;
Linus Torvalds's avatar
Linus Torvalds committed
1198

1199
	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
	if (epcm == NULL)
		return -ENOMEM;
	epcm->emu = emu;
	epcm->type = CAPTURE_AC97ADC;
	epcm->substream = substream;
	epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
	epcm->capture_inte = INTE_ADCBUFENABLE;
	epcm->capture_ba_reg = ADCBA;
	epcm->capture_bs_reg = ADCBS;
	epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
	runtime->private_data = epcm;
	runtime->private_free = snd_emu10k1_pcm_free_substream;
	runtime->hw = snd_emu10k1_capture;
	emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
	emu->pcm_capture_substream = substream;
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
	return 0;
}

1220
static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1221
{
1222
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
1223
1224
1225
1226
1227
1228

	emu->capture_interrupt = NULL;
	emu->pcm_capture_substream = NULL;
	return 0;
}

1229
static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1230
{
1231
1232
1233
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_emu10k1_pcm *epcm;
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1234

1235
	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
	if (epcm == NULL)
		return -ENOMEM;
	epcm->emu = emu;
	epcm->type = CAPTURE_AC97MIC;
	epcm->substream = substream;
	epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
	epcm->capture_inte = INTE_MICBUFENABLE;
	epcm->capture_ba_reg = MICBA;
	epcm->capture_bs_reg = MICBS;
	epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
	substream->runtime->private_data = epcm;
	substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
	runtime->hw = snd_emu10k1_capture;
	runtime->hw.rates = SNDRV_PCM_RATE_8000;
	runtime->hw.rate_min = runtime->hw.rate_max = 8000;
	runtime->hw.channels_min = 1;
	emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
	emu->pcm_capture_mic_substream = substream;
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
	return 0;
}

1258
static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1259
{
1260
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
1261
1262
1263
1264
1265
1266

	emu->capture_interrupt = NULL;
	emu->pcm_capture_mic_substream = NULL;
	return 0;
}

1267
static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1268
{
1269
1270
1271
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
	struct snd_emu10k1_pcm *epcm;
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1272
1273
1274
	int nefx = emu->audigy ? 64 : 32;
	int idx;

1275
	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
	if (epcm == NULL)
		return -ENOMEM;
	epcm->emu = emu;
	epcm->type = CAPTURE_EFX;
	epcm->substream = substream;
	epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
	epcm->capture_inte = INTE_EFXBUFENABLE;
	epcm->capture_ba_reg = FXBA;
	epcm->capture_bs_reg = FXBS;
	epcm->capture_idx_reg = FXIDX;
	substream->runtime->private_data = epcm;
	substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1288
	runtime->hw = snd_emu10k1_capture_efx;
Linus Torvalds's avatar
Linus Torvalds committed
1289
1290
1291
	runtime->hw.rates = SNDRV_PCM_RATE_48000;
	runtime->hw.rate_min = runtime->hw.rate_max = 48000;
	spin_lock_irq(&emu->reg_lock);
1292
	if (emu->card_capabilities->emu_model) {
1293
1294
		/*  Nb. of channels has been increased to 16 */
		/* TODO
1295
1296
1297
1298
1299
1300
		 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
		 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
		 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
		 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
		 * rate_min = 44100,
		 * rate_max = 192000,
1301
1302
		 * channels_min = 16,
		 * channels_max = 16,
1303
1304
		 * Need to add mixer control to fix sample rate
		 *                 
1305
		 * There are 32 mono channels of 16bits each.
1306
1307
1308
		 * 24bit Audio uses 2x channels over 16bit
		 * 96kHz uses 2x channels over 48kHz
		 * 192kHz uses 4x channels over 48kHz
1309
1310
1311
1312
		 * So, for 48kHz 24bit, one has 16 channels
		 * for 96kHz 24bit, one has 8 channels
		 * for 192kHz 24bit, one has 4 channels
		 *
1313
1314
		 */
#if 1
1315
1316
1317
1318
1319
		switch (emu->emu1010.internal_clock) {
		case 0:
			/* For 44.1kHz */
			runtime->hw.rates = SNDRV_PCM_RATE_44100;
			runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1320
1321
			runtime->hw.channels_min =
				runtime->hw.channels_max = 16;
1322
1323
1324
1325
1326
			break;
		case 1:
			/* For 48kHz */
			runtime->hw.rates = SNDRV_PCM_RATE_48000;
			runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1327
1328
			runtime->hw.channels_min =
				runtime->hw.channels_max = 16;
1329
			break;
1330
		}
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
#endif
#if 0
		/* For 96kHz */
		runtime->hw.rates = SNDRV_PCM_RATE_96000;
		runtime->hw.rate_min = runtime->hw.rate_max = 96000;
		runtime->hw.channels_min = runtime->hw.channels_max = 4;
#endif
#if 0
		/* For 192kHz */
		runtime->hw.rates = SNDRV_PCM_RATE_192000;
		runtime->hw.rate_min = runtime->hw.rate_max = 192000;
		runtime->hw.channels_min = runtime->hw.channels_max = 2;
#endif
		runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
		/* efx_voices_mask[0] is expected to be zero
1346
 		 * efx_voices_mask[1] is expected to have 32bits set
1347
1348
1349
1350
1351
1352
1353
1354
		 */
	} else {
		runtime->hw.channels_min = runtime->hw.channels_max = 0;
		for (idx = 0; idx < nefx; idx++) {
			if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
				runtime->hw.channels_min++;
				runtime->hw.channels_max++;
			}
Linus Torvalds's avatar
Linus Torvalds committed
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
		}
	}
	epcm->capture_cr_val = emu->efx_voices_mask[0];
	epcm->capture_cr_val2 = emu->efx_voices_mask[1];
	spin_unlock_irq(&emu->reg_lock);
	emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
	emu->pcm_capture_efx_substream = substream;
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
	return 0;
}

1366
static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1367
{
1368
	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
1369
1370
1371
1372
1373
1374

	emu->capture_interrupt = NULL;
	emu->pcm_capture_efx_substream = NULL;
	return 0;
}

1375
static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
	.open =			snd_emu10k1_playback_open,
	.close =		snd_emu10k1_playback_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_emu10k1_playback_hw_params,
	.hw_free =		snd_emu10k1_playback_hw_free,
	.prepare =		snd_emu10k1_playback_prepare,
	.trigger =		snd_emu10k1_playback_trigger,
	.pointer =		snd_emu10k1_playback_pointer,
	.page =			snd_pcm_sgbuf_ops_page,
};

1387
static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
	.open =			snd_emu10k1_capture_open,
	.close =		snd_emu10k1_capture_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_emu10k1_capture_hw_params,
	.hw_free =		snd_emu10k1_capture_hw_free,
	.prepare =		snd_emu10k1_capture_prepare,
	.trigger =		snd_emu10k1_capture_trigger,
	.pointer =		snd_emu10k1_capture_pointer,
};

/* EFX playback */
1399
static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
	.open =			snd_emu10k1_efx_playback_open,
	.close =		snd_emu10k1_efx_playback_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_emu10k1_playback_hw_params,
	.hw_free =		snd_emu10k1_efx_playback_hw_free,
	.prepare =		snd_emu10k1_efx_playback_prepare,
	.trigger =		snd_emu10k1_efx_playback_trigger,
	.pointer =		snd_emu10k1_efx_playback_pointer,
	.page =			snd_pcm_sgbuf_ops_page,
};

1411
int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
Linus Torvalds's avatar
Linus Torvalds committed
1412
{
1413
1414
	struct snd_pcm *pcm;
	struct snd_pcm_substream *substream;
Linus Torvalds's avatar
Linus Torvalds committed
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
	int err;

	if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
		return err;

	pcm->private_data = emu;

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);

	pcm->info_flags = 0;
	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
	strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
	emu->pcm = pcm;

	for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
		if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
			return err;

	for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
		snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);

	return 0;
}

1440
int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
Linus Torvalds's avatar
Linus Torvalds committed
1441
{
1442
1443
	struct snd_pcm *pcm;
	struct snd_pcm_substream *substream;
Linus Torvalds's avatar
Linus Torvalds committed
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
	int err;

	if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
		return err;

	pcm->private_data = emu;

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);

	pcm->info_flags = 0;
	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
	strcpy(pcm->name, "Multichannel Playback");
Takashi Iwai's avatar
Takashi Iwai committed
1456
	emu->pcm_multi = pcm;
Linus Torvalds's avatar
Linus Torvalds committed
1457
1458
1459
1460
1461
1462
1463
1464
1465

	for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
		if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
			return err;

	return 0;
}


1466
static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
	.open =			snd_emu10k1_capture_mic_open,
	.close =		snd_emu10k1_capture_mic_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_emu10k1_capture_hw_params,
	.hw_free =		snd_emu10k1_capture_hw_free,
	.prepare =		snd_emu10k1_capture_prepare,
	.trigger =		snd_emu10k1_capture_trigger,
	.pointer =		snd_emu10k1_capture_pointer,
};

1477
int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
Linus Torvalds's avatar
Linus Torvalds committed
1478
{
1479
	struct snd_pcm *pcm;