xref: /freebsd/sys/dev/sound/pci/emu10kx-pcm.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*-
2  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3  * Copyright (c) 2003-2007 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/bus.h>
33 #include <machine/bus.h>
34 #include <sys/rman.h>
35 #include <sys/systm.h>
36 #include <sys/sbuf.h>
37 #include <sys/queue.h>
38 #include <sys/systm.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 
42 #include <dev/sound/chip.h>
43 #include <dev/sound/pcm/sound.h>
44 #include <dev/sound/pcm/ac97.h>
45 
46 #include "mixer_if.h"
47 
48 #include <dev/sound/pci/emu10kx.h>
49 #include "emu10k1-alsa%diked.h"
50 
51 struct emu_pcm_pchinfo {
52 	int		spd;
53 	int		fmt;
54 	unsigned int	blksz;
55 	int		run;
56 	struct emu_voice *master;
57 	struct emu_voice *slave;
58 	struct snd_dbuf	*buffer;
59 	struct pcm_channel *channel;
60 	struct emu_pcm_info *pcm;
61 	int		timer;
62 };
63 
64 struct emu_pcm_rchinfo {
65 	int		spd;
66 	int		fmt;
67 	unsigned int	blksz;
68 	int		run;
69 	uint32_t 	idxreg;
70 	uint32_t	basereg;
71 	uint32_t	sizereg;
72 	uint32_t	setupreg;
73 	uint32_t	irqmask;
74 	uint32_t	iprmask;
75 	int 		ihandle;
76 	struct snd_dbuf	*buffer;
77 	struct pcm_channel *channel;
78 	struct emu_pcm_info *pcm;
79 	int		timer;
80 };
81 
82 /* XXX Hardware playback channels */
83 #define	MAX_CHANNELS	4
84 
85 #if MAX_CHANNELS > 13
86 #error	Too many hardware channels defined. 13 is the maximum
87 #endif
88 
89 struct emu_pcm_info {
90 	struct mtx		*lock;
91 	device_t		dev;		/* device information */
92 	struct emu_sc_info 	*card;
93 	struct emu_pcm_pchinfo	pch[MAX_CHANNELS];	/* hardware channels */
94 	int			pnum;		/* next free channel number */
95 	struct emu_pcm_rchinfo	rch_adc;
96 	struct emu_pcm_rchinfo	rch_efx;
97 	struct emu_route	rt;
98 	struct emu_route	rt_mono;
99 	int			route;
100 	int			ihandle;	/* interrupt handler */
101 	unsigned int		bufsz;
102 	int			is_emu10k1;
103 	struct ac97_info	*codec;
104 	uint32_t 		ac97_state[0x7F];
105 	kobj_class_t		ac97_mixerclass;
106 	uint32_t		ac97_recdevs;
107 	uint32_t		ac97_playdevs;
108 	struct snd_mixer	*sm;
109 	int			mch_disabled;
110 	unsigned int		emu10k1_volcache[2][2];
111 };
112 
113 
114 static uint32_t emu_rfmt_adc[] = {
115 	AFMT_S16_LE,
116 	AFMT_STEREO | AFMT_S16_LE,
117 	0
118 };
119 static struct pcmchan_caps emu_reccaps_adc = {
120 	8000, 48000, emu_rfmt_adc, 0
121 };
122 
123 static uint32_t emu_rfmt_efx[] = {
124 	AFMT_S16_LE,
125 	0
126 };
127 
128 static struct pcmchan_caps emu_reccaps_efx_live = {
129 	48000*32, 48000*32, emu_rfmt_efx, 0
130 };
131 
132 static struct pcmchan_caps emu_reccaps_efx_audigy = {
133 	48000*64, 48000*64, emu_rfmt_efx, 0
134 };
135 
136 static int emu_rates_live[] = {
137 	48000*32
138 };
139 
140 static int emu_rates_audigy[] = {
141 	48000*64
142 };
143 
144 static uint32_t emu_pfmt[] = {
145 	AFMT_U8,
146 	AFMT_STEREO | AFMT_U8,
147 	AFMT_S16_LE,
148 	AFMT_STEREO | AFMT_S16_LE,
149 	0
150 };
151 static uint32_t emu_pfmt_mono[] = {
152 	AFMT_U8,
153 	AFMT_S16_LE,
154 	0
155 };
156 
157 static struct pcmchan_caps emu_playcaps = {4000, 48000, emu_pfmt, 0};
158 static struct pcmchan_caps emu_playcaps_mono = {4000, 48000, emu_pfmt_mono, 0};
159 
160 static int emu10k1_adcspeed[8] = {48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000};
161 /* audigy supports 12kHz. */
162 static int emu10k2_adcspeed[9] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000};
163 
164 static uint32_t emu_pcm_intr(void *pcm, uint32_t stat);
165 
166 static const struct emu_dspmix_props_k1 {
167 	uint8_t	present;
168 	uint8_t	recdev;
169 	int8_t	input;
170 } dspmix_k1 [SOUND_MIXER_NRDEVICES] = {
171 	/* no mixer device for ac97 */		/* in0 AC97 */
172 	[SOUND_MIXER_DIGITAL1] = {1, 1, 1},	/* in1 CD SPDIF */
173 	/* not connected */			/* in2 (zoom) */
174 	[SOUND_MIXER_DIGITAL2] = {1, 1, 3},	/* in3 toslink */
175 	[SOUND_MIXER_LINE2] =    {1, 1, 4},	/* in4 Line-In2 */
176 	[SOUND_MIXER_DIGITAL3] = {1, 1, 5},	/* in5 on-card  SPDIF */
177 	[SOUND_MIXER_LINE3] =    {1, 1, 6},	/* in6 AUX2 */
178 	/* not connected */			/* in7 */
179 };
180 static const struct emu_dspmix_props_k2 {
181 	uint8_t	present;
182 	uint8_t	recdev;
183 	int8_t	input;
184 } dspmix_k2 [SOUND_MIXER_NRDEVICES] = {
185 	[SOUND_MIXER_VOLUME] =	{1, 0, (-1)},
186 	[SOUND_MIXER_PCM] =	{1, 0, (-1)},
187 
188 	/* no mixer device */			/* in0 AC97 */
189 	[SOUND_MIXER_DIGITAL1] = {1, 1, 1},	/* in1 CD SPDIF */
190 	[SOUND_MIXER_DIGITAL2] = {1, 1, 2},	/* in2 COAX SPDIF */
191 	/* not connected */			/* in3 */
192 	[SOUND_MIXER_LINE2] =    {1, 1, 4},	/* in4 Line-In2 */
193 	[SOUND_MIXER_DIGITAL3] = {1, 1, 5},	/* in5 on-card  SPDIF */
194 	[SOUND_MIXER_LINE3] =    {1, 1, 6},	/* in6 AUX2 */
195 	/* not connected */			/* in7 */
196 };
197 
198 static int
199 emu_dspmixer_init(struct snd_mixer *m)
200 {
201 	struct emu_pcm_info	*sc;
202 	int i;
203 	int p, r;
204 
205 	p = 0;
206 	r = 0;
207 
208 	sc = mix_getdevinfo(m);
209 
210 	if (sc->route == RT_FRONT) {
211 		/* create submixer for AC97 codec */
212 		if ((sc->ac97_mixerclass != NULL) && (sc->codec != NULL)) {
213 			sc->sm = mixer_create(sc->dev, sc->ac97_mixerclass, sc->codec, "ac97");
214 			if (sc->sm != NULL) {
215 				p = mix_getdevs(sc->sm);
216 				r = mix_getrecdevs(sc->sm);
217 			}
218 		}
219 
220 		sc->ac97_playdevs = p;
221 		sc->ac97_recdevs = r;
222 	}
223 
224 	/* This two are always here */
225 	p |= (1 << SOUND_MIXER_PCM);
226 	p |= (1 << SOUND_MIXER_VOLUME);
227 
228 	if (sc->route == RT_FRONT) {
229 		if (sc->is_emu10k1) {
230 			for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
231 				if (dspmix_k1[i].present)
232 					p |= (1 << i);
233 				if (dspmix_k1[i].recdev)
234 					r |= (1 << i);
235 			}
236 		} else {
237 			for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
238 				if (dspmix_k2[i].present)
239 					p |= (1 << i);
240 				if (dspmix_k2[i].recdev)
241 					r |= (1 << i);
242 			}
243 		}
244 	}
245 
246 	mix_setdevs(m, p);
247 	mix_setrecdevs(m, r);
248 
249 	return (0);
250 }
251 
252 static int
253 emu_dspmixer_uninit(struct snd_mixer *m)
254 {
255 	struct emu_pcm_info	*sc;
256 	int err = 0;
257 
258 	/* drop submixer for AC97 codec */
259 	sc = mix_getdevinfo(m);
260 	if (sc->sm != NULL)
261 		err = mixer_delete(sc->sm);
262 		if (err)
263 			return (err);
264 		sc->sm = NULL;
265 	return (0);
266 }
267 
268 static int
269 emu_dspmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
270 {
271 	struct emu_pcm_info *sc;
272 
273 	sc = mix_getdevinfo(m);
274 
275 	switch (dev) {
276 	case SOUND_MIXER_VOLUME:
277 		switch (sc->route) {
278 		case RT_FRONT:
279 			if (sc->sm != NULL)
280 				mix_set(sc->sm, dev, left, right);
281 			if (sc->mch_disabled) {
282 				/* In emu10k1 case PCM volume does not affect
283 				   sound routed to rear & center/sub (it is connected
284 				   to AC97 codec). Calculate it manually. */
285 				/* This really should belong to emu10kx.c */
286 				if (sc->is_emu10k1) {
287 					sc->emu10k1_volcache[0][0] = left;
288 					left = left * sc->emu10k1_volcache[1][0] / 100;
289 					sc->emu10k1_volcache[0][1] = right;
290 					right = right * sc->emu10k1_volcache[1][1] / 100;
291 				}
292 
293 				emumix_set_volume(sc->card, M_MASTER_REAR_L, left);
294 				emumix_set_volume(sc->card, M_MASTER_REAR_R, right);
295 				if (!sc->is_emu10k1) {
296 					emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2);
297 					emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2);
298 					/* XXX side */
299 				}
300 			} /* mch disabled */
301 			break;
302 		case RT_REAR:
303 			emumix_set_volume(sc->card, M_MASTER_REAR_L, left);
304 			emumix_set_volume(sc->card, M_MASTER_REAR_R, right);
305 			break;
306 		case RT_CENTER:
307 			emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2);
308 			break;
309 		case RT_SUB:
310 			emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2);
311 			break;
312 		}
313 		break;
314 	case SOUND_MIXER_PCM:
315 		switch (sc->route) {
316 		case RT_FRONT:
317 			if (sc->sm != NULL)
318 				mix_set(sc->sm, dev, left, right);
319 			if (sc->mch_disabled) {
320 				/* See SOUND_MIXER_VOLUME case */
321 				if (sc->is_emu10k1) {
322 					sc->emu10k1_volcache[1][0] = left;
323 					left = left * sc->emu10k1_volcache[0][0] / 100;
324 					sc->emu10k1_volcache[1][1] = right;
325 					right = right * sc->emu10k1_volcache[0][1] / 100;
326 				}
327 				emumix_set_volume(sc->card, M_MASTER_REAR_L, left);
328 				emumix_set_volume(sc->card, M_MASTER_REAR_R, right);
329 
330 				if (!sc->is_emu10k1) {
331 					emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2);
332 					emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2);
333 					/* XXX side */
334 				}
335 			} /* mch_disabled */
336 			break;
337 		case RT_REAR:
338 			emumix_set_volume(sc->card, M_FX2_REAR_L, left);
339 			emumix_set_volume(sc->card, M_FX3_REAR_R, right);
340 			break;
341 		case RT_CENTER:
342 			emumix_set_volume(sc->card, M_FX4_CENTER, (left+right)/2);
343 			break;
344 		case RT_SUB:
345 			emumix_set_volume(sc->card, M_FX5_SUBWOOFER, (left+right)/2);
346 			break;
347 		}
348 		break;
349 	case SOUND_MIXER_DIGITAL1:	/* CD SPDIF, in1 */
350 			emumix_set_volume(sc->card, M_IN1_FRONT_L, left);
351 			emumix_set_volume(sc->card, M_IN1_FRONT_R, right);
352 		break;
353 	case SOUND_MIXER_DIGITAL2:
354 			if (sc->is_emu10k1) {
355 				/* TOSLink, in3 */
356 				emumix_set_volume(sc->card, M_IN3_FRONT_L, left);
357 				emumix_set_volume(sc->card, M_IN3_FRONT_R, right);
358 			} else {
359 				/* COAX SPDIF, in2 */
360 				emumix_set_volume(sc->card, M_IN2_FRONT_L, left);
361 				emumix_set_volume(sc->card, M_IN2_FRONT_R, right);
362 			}
363 		break;
364 	case SOUND_MIXER_LINE2:		/* Line-In2, in4 */
365 			emumix_set_volume(sc->card, M_IN4_FRONT_L, left);
366 			emumix_set_volume(sc->card, M_IN4_FRONT_R, right);
367 		break;
368 	case SOUND_MIXER_DIGITAL3:	/* on-card SPDIF, in5 */
369 			emumix_set_volume(sc->card, M_IN5_FRONT_L, left);
370 			emumix_set_volume(sc->card, M_IN5_FRONT_R, right);
371 		break;
372 	case SOUND_MIXER_LINE3:		/* AUX2, in6 */
373 			emumix_set_volume(sc->card, M_IN6_FRONT_L, left);
374 			emumix_set_volume(sc->card, M_IN6_FRONT_R, right);
375 		break;
376 	default:
377 		if (sc->sm != NULL) {
378 			/* XXX emumix_set_volume is not required here */
379 			emumix_set_volume(sc->card, M_IN0_FRONT_L, 100);
380 			emumix_set_volume(sc->card, M_IN0_FRONT_R, 100);
381 			mix_set(sc->sm, dev, left, right);
382 		} else
383 			device_printf(sc->dev, "mixer error: unknown device %d\n", dev);
384 	}
385 	return  (0);
386 }
387 
388 static int
389 emu_dspmixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
390 {
391 	struct emu_pcm_info *sc;
392 	int i;
393 	u_int32_t recmask;
394 	int	input[8];
395 
396 	sc = mix_getdevinfo(m);
397 	recmask = 0;
398 	for (i=0; i < 8; i++)
399 		input[i]=0;
400 
401 	if (sc->sm != NULL)
402 		if ((src & sc->ac97_recdevs) !=0)
403 			if (mix_setrecsrc(sc->sm, src & sc->ac97_recdevs) == 0) {
404 				recmask |= (src & sc->ac97_recdevs);
405 				/* Recording from AC97 codec.
406 				   Enable AC97 route to rec on DSP */
407 				input[0] = 1;
408 			}
409 	if (sc->is_emu10k1) {
410 		for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
411 			if (dspmix_k1[i].recdev)
412 				if ((src & (1 << i)) == ((uint32_t)1 << i)) {
413 				recmask |= (1 << i);
414 				/* enable device i */
415 				input[dspmix_k1[i].input] = 1;
416 				}
417 		}
418 	} else {
419 		for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
420 			if (dspmix_k2[i].recdev)
421 				if ((src & (1 << i)) == ((uint32_t)1 << i)) {
422 				recmask |= (1 << i);
423 				/* enable device i */
424 				input[dspmix_k2[i].input] = 1;
425 				}
426 		}
427 	}
428 	emumix_set_volume(sc->card, M_IN0_REC_L, input[0] == 1 ? 100 : 0);
429 	emumix_set_volume(sc->card, M_IN0_REC_R, input[0] == 1 ? 100 : 0);
430 
431 	emumix_set_volume(sc->card, M_IN1_REC_L, input[1] == 1 ? 100 : 0);
432 	emumix_set_volume(sc->card, M_IN1_REC_R, input[1] == 1 ? 100 : 0);
433 
434 	if (!sc->is_emu10k1) {
435 		emumix_set_volume(sc->card, M_IN2_REC_L, input[2] == 1 ? 100 : 0);
436 		emumix_set_volume(sc->card, M_IN2_REC_R, input[2] == 1 ? 100 : 0);
437 	}
438 
439 	if (sc->is_emu10k1) {
440 		emumix_set_volume(sc->card, M_IN3_REC_L, input[3] == 1 ? 100 : 0);
441 		emumix_set_volume(sc->card, M_IN3_REC_R, input[3] == 1 ? 100 : 0);
442 	}
443 
444 	emumix_set_volume(sc->card, M_IN4_REC_L, input[4] == 1 ? 100 : 0);
445 	emumix_set_volume(sc->card, M_IN4_REC_R, input[4] == 1 ? 100 : 0);
446 
447 	emumix_set_volume(sc->card, M_IN5_REC_L, input[5] == 1 ? 100 : 0);
448 	emumix_set_volume(sc->card, M_IN5_REC_R, input[5] == 1 ? 100 : 0);
449 
450 	emumix_set_volume(sc->card, M_IN6_REC_L, input[6] == 1 ? 100 : 0);
451 	emumix_set_volume(sc->card, M_IN6_REC_R, input[6] == 1 ? 100 : 0);
452 
453 	/* XXX check for K1/k2 differences? */
454 	if ((src & (1 << SOUND_MIXER_PCM)) == (1 << SOUND_MIXER_PCM)) {
455 		emumix_set_volume(sc->card, M_FX0_REC_L, emumix_get_volume(sc->card, M_FX0_FRONT_L));
456 		emumix_set_volume(sc->card, M_FX1_REC_R, emumix_get_volume(sc->card, M_FX1_FRONT_R));
457 	} else {
458 		emumix_set_volume(sc->card, M_FX0_REC_L, 0);
459 		emumix_set_volume(sc->card, M_FX1_REC_R, 0);
460 	}
461 
462 	return (recmask);
463 }
464 
465 static kobj_method_t emudspmixer_methods[] = {
466 	KOBJMETHOD(mixer_init,		emu_dspmixer_init),
467 	KOBJMETHOD(mixer_uninit,	emu_dspmixer_uninit),
468 	KOBJMETHOD(mixer_set,		emu_dspmixer_set),
469 	KOBJMETHOD(mixer_setrecsrc,	emu_dspmixer_setrecsrc),
470 	{ 0, 0 }
471 };
472 MIXER_DECLARE(emudspmixer);
473 
474 static int
475 emu_efxmixer_init(struct snd_mixer *m)
476 {
477 	mix_setdevs(m, SOUND_MASK_VOLUME);
478 	mix_setrecdevs(m, SOUND_MASK_MONITOR);
479 	return (0);
480 }
481 
482 static int
483 emu_efxmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
484 {
485 	if (left + right == 200) return (0);
486 	return  (0);
487 }
488 
489 static int
490 emu_efxmixer_setrecsrc(struct snd_mixer *m __unused, u_int32_t src __unused)
491 {
492 	return (SOUND_MASK_MONITOR);
493 }
494 
495 static kobj_method_t emuefxmixer_methods[] = {
496 	KOBJMETHOD(mixer_init,		emu_efxmixer_init),
497 	KOBJMETHOD(mixer_set,		emu_efxmixer_set),
498 	KOBJMETHOD(mixer_setrecsrc,	emu_efxmixer_setrecsrc),
499 	{ 0, 0 }
500 };
501 MIXER_DECLARE(emuefxmixer);
502 
503 /*
504  * AC97 emulation code for Audigy and later cards.
505  * Some parts of AC97 codec are not used by hardware, but can be used
506  * to change some DSP controls via AC97 mixer interface. This includes:
507  * - master volume controls MASTER_FRONT_[R|L]
508  * - pcm volume controls FX[0|1]_FRONT_[R|L]
509  * - rec volume controls MASTER_REC_[R|L]
510  * We do it because we need to put it under user control....
511  * We also keep some parts of AC97 disabled to get better sound quality
512  */
513 
514 #define	AC97LEFT(x)	((x & 0x7F00)>>8)
515 #define	AC97RIGHT(x)	(x & 0x007F)
516 #define	AC97MUTE(x)	((x & 0x8000)>>15)
517 #define	BIT4_TO100(x)	(100-(x)*100/(0x0f))
518 #define	BIT6_TO100(x)	(100-(x)*100/(0x3f))
519 #define	BIT4_TO255(x)	(255-(x)*255/(0x0f))
520 #define	BIT6_TO255(x)	(255-(x)*255/(0x3f))
521 #define	V100_TOBIT6(x)	(0x3f*(100-x)/100)
522 #define	V100_TOBIT4(x)	(0x0f*(100-x)/100)
523 #define	AC97ENCODE(x_muted, x_left, x_right)	(((x_muted & 1)<<15) | ((x_left & 0x3f)<<8) | (x_right & 0x3f))
524 
525 static int
526 emu_ac97_read_emulation(struct emu_pcm_info *sc, int regno)
527 {
528 	int use_ac97;
529 	int emulated;
530 	int tmp;
531 
532 	use_ac97 = 1;
533 	emulated = 0;
534 
535 	switch (regno) {
536 	case AC97_MIX_MASTER:
537 		emulated = sc->ac97_state[AC97_MIX_MASTER];
538 		use_ac97 = 0;
539 		break;
540 	case AC97_MIX_PCM:
541 		emulated = sc->ac97_state[AC97_MIX_PCM];
542 		use_ac97 = 0;
543 		break;
544 	case AC97_REG_RECSEL:
545 		emulated = 0x0505;
546 		use_ac97 = 0;
547 		break;
548 	case AC97_MIX_RGAIN:
549 		emulated = sc->ac97_state[AC97_MIX_RGAIN];
550 		use_ac97 = 0;
551 		break;
552 	}
553 
554 	emu_wr(sc->card, AC97ADDRESS, regno, 1);
555 	tmp = emu_rd(sc->card, AC97DATA, 2);
556 
557 	if (use_ac97)
558 		emulated = tmp;
559 
560 	return (emulated);
561 }
562 
563 static void
564 emu_ac97_write_emulation(struct emu_pcm_info *sc, int regno, uint32_t data)
565 {
566 	int write_ac97;
567 	int left, right;
568 	uint32_t emu_left, emu_right;
569 	int is_mute;
570 
571 	write_ac97 = 1;
572 
573 	left = AC97LEFT(data);
574 	emu_left = BIT6_TO100(left);	/* We show us as 6-bit AC97 mixer */
575 	right = AC97RIGHT(data);
576 	emu_right = BIT6_TO100(right);
577 	is_mute = AC97MUTE(data);
578 	if (is_mute)
579 		emu_left = emu_right = 0;
580 
581 	switch (regno) {
582 		/* TODO: reset emulator on AC97_RESET */
583 	case AC97_MIX_MASTER:
584 		emumix_set_volume(sc->card, M_MASTER_FRONT_L, emu_left);
585 		emumix_set_volume(sc->card, M_MASTER_FRONT_R, emu_right);
586 		sc->ac97_state[AC97_MIX_MASTER] = data & (0x8000 | 0x3f3f);
587 		data = 0x8000;	/* Mute AC97 main out */
588 		break;
589 	case AC97_MIX_PCM:	/* PCM OUT VOL */
590 		emumix_set_volume(sc->card, M_FX0_FRONT_L, emu_left);
591 		emumix_set_volume(sc->card, M_FX1_FRONT_R, emu_right);
592 		sc->ac97_state[AC97_MIX_PCM] = data & (0x8000 | 0x3f3f);
593 		data = 0x8000;	/* Mute AC97 PCM out */
594 		break;
595 	case AC97_REG_RECSEL:
596 		/*
597 		 * PCM recording source is set to "stereo mix" (labeled "vol"
598 		 * in mixer). There is no 'playback' from AC97 codec -
599 		 * if you want to hear anything from AC97 you have to _record_
600 		 * it. Keep things simple and record "stereo mix".
601 		 */
602 		data = 0x0505;
603 		break;
604 	case AC97_MIX_RGAIN:	/* RECORD GAIN */
605 		emu_left = BIT4_TO100(left);	/* rgain is 4-bit */
606 		emu_right = BIT4_TO100(right);
607 		emumix_set_volume(sc->card, M_MASTER_REC_L, 100-emu_left);
608 		emumix_set_volume(sc->card, M_MASTER_REC_R, 100-emu_right);
609 		/*
610 		 * Record gain on AC97 should stay zero to get AC97 sound on
611 		 * AC97_[RL] connectors on EMU10K2 chip. AC97 on Audigy is not
612 		 * directly connected to any output, only to EMU10K2 chip Use
613 		 * this control to set AC97 mix volume inside EMU10K2 chip
614 		 */
615 		sc->ac97_state[AC97_MIX_RGAIN] = data & (0x8000 | 0x0f0f);
616 		data = 0x0000;
617 		break;
618 	}
619 	if (write_ac97) {
620 		emu_wr(sc->card, AC97ADDRESS, regno, 1);
621 		emu_wr(sc->card, AC97DATA, data, 2);
622 	}
623 }
624 
625 static int
626 emu_erdcd(kobj_t obj __unused, void *devinfo, int regno)
627 {
628 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
629 
630 	return (emu_ac97_read_emulation(sc, regno));
631 }
632 
633 static int
634 emu_ewrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data)
635 {
636 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
637 
638 	emu_ac97_write_emulation(sc, regno, data);
639 	return (0);
640 }
641 
642 static kobj_method_t emu_eac97_methods[] = {
643 	KOBJMETHOD(ac97_read, emu_erdcd),
644 	KOBJMETHOD(ac97_write, emu_ewrcd),
645 	{0, 0}
646 };
647 AC97_DECLARE(emu_eac97);
648 
649 /* real ac97 codec */
650 static int
651 emu_rdcd(kobj_t obj __unused, void *devinfo, int regno)
652 {
653 	int rd;
654 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
655 
656 	KASSERT(sc->card != NULL, ("emu_rdcd: no soundcard"));
657 	emu_wr(sc->card, AC97ADDRESS, regno, 1);
658 	rd = emu_rd(sc->card, AC97DATA, 2);
659 	return (rd);
660 }
661 
662 static int
663 emu_wrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data)
664 {
665 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
666 
667 	KASSERT(sc->card != NULL, ("emu_wrcd: no soundcard"));
668 	emu_wr(sc->card, AC97ADDRESS, regno, 1);
669 	emu_wr(sc->card, AC97DATA, data, 2);
670 	return (0);
671 }
672 
673 static kobj_method_t emu_ac97_methods[] = {
674 	KOBJMETHOD(ac97_read, emu_rdcd),
675 	KOBJMETHOD(ac97_write, emu_wrcd),
676 	{0, 0}
677 };
678 AC97_DECLARE(emu_ac97);
679 
680 
681 static int
682 emu_k1_recval(int speed)
683 {
684 	int val;
685 
686 	val = 0;
687 	while ((val < 7) && (speed < emu10k1_adcspeed[val]))
688 		val++;
689 	return (val);
690 }
691 
692 static int
693 emu_k2_recval(int speed)
694 {
695 	int val;
696 
697 	val = 0;
698 	while ((val < 8) && (speed < emu10k2_adcspeed[val]))
699 		val++;
700 	return (val);
701 }
702 
703 static void *
704 emupchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
705 {
706 	struct emu_pcm_info *sc = devinfo;
707 	struct emu_pcm_pchinfo *ch;
708 	void *r;
709 
710 	KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction"));
711 	KASSERT(sc->card != NULL, ("empchan_init: no soundcard"));
712 
713 
714 	if (sc->pnum >= MAX_CHANNELS)
715 		return (NULL);
716 	ch = &(sc->pch[sc->pnum++]);
717 	ch->buffer = b;
718 	ch->pcm = sc;
719 	ch->channel = c;
720 	ch->blksz = sc->bufsz;
721 	ch->fmt = AFMT_U8;
722 	ch->spd = 8000;
723 	ch->master = emu_valloc(sc->card);
724 	/*
725 	 * XXX we have to allocate slave even for mono channel until we
726 	 * fix emu_vfree to handle this case.
727 	 */
728 	ch->slave = emu_valloc(sc->card);
729 	ch->timer = emu_timer_create(sc->card);
730 	r = (emu_vinit(sc->card, ch->master, ch->slave, EMU_PLAY_BUFSZ, ch->buffer)) ? NULL : ch;
731 	return (r);
732 }
733 
734 static int
735 emupchan_free(kobj_t obj __unused, void *c_devinfo)
736 {
737 	struct emu_pcm_pchinfo *ch = c_devinfo;
738 	struct emu_pcm_info *sc = ch->pcm;
739 
740 	emu_timer_clear(sc->card, ch->timer);
741 	if (ch->slave != NULL)
742 		emu_vfree(sc->card, ch->slave);
743 	emu_vfree(sc->card, ch->master);
744 	return (0);
745 }
746 
747 static int
748 emupchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format)
749 {
750 	struct emu_pcm_pchinfo *ch = c_devinfo;
751 
752 	ch->fmt = format;
753 	return (0);
754 }
755 
756 static int
757 emupchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
758 {
759 	struct emu_pcm_pchinfo *ch = c_devinfo;
760 
761 	ch->spd = speed;
762 	return (ch->spd);
763 }
764 
765 static int
766 emupchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
767 {
768 	struct emu_pcm_pchinfo *ch = c_devinfo;
769 	struct emu_pcm_info *sc = ch->pcm;
770 
771 	if (blocksize > ch->pcm->bufsz)
772 		blocksize = ch->pcm->bufsz;
773 	snd_mtxlock(sc->lock);
774 	ch->blksz = blocksize;
775 	emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getbps(ch->buffer));
776 	snd_mtxunlock(sc->lock);
777 	return (ch->blksz);
778 }
779 
780 static int
781 emupchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
782 {
783 	struct emu_pcm_pchinfo *ch = c_devinfo;
784 	struct emu_pcm_info *sc = ch->pcm;
785 
786 	if (!PCMTRIG_COMMON(go))
787 		return (0);
788 
789 	snd_mtxlock(sc->lock); /* XXX can we trigger on parallel threads ? */
790 	if (go == PCMTRIG_START) {
791 		emu_vsetup(ch->master, ch->fmt, ch->spd);
792 		if ((ch->fmt & AFMT_STEREO) == AFMT_STEREO)
793 			emu_vroute(sc->card, &(sc->rt), ch->master);
794 		else
795 			emu_vroute(sc->card, &(sc->rt_mono), ch->master);
796 		emu_vwrite(sc->card, ch->master);
797 		emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getbps(ch->buffer));
798 		emu_timer_enable(sc->card, ch->timer, 1);
799 	}
800 	/* PCM interrupt handler will handle PCMTRIG_STOP event */
801 	ch->run = (go == PCMTRIG_START) ? 1 : 0;
802 	emu_vtrigger(sc->card, ch->master, ch->run);
803 	snd_mtxunlock(sc->lock);
804 	return (0);
805 }
806 
807 static int
808 emupchan_getptr(kobj_t obj __unused, void *c_devinfo)
809 {
810 	struct emu_pcm_pchinfo *ch = c_devinfo;
811 	struct emu_pcm_info *sc = ch->pcm;
812 	int r;
813 
814 	r = emu_vpos(sc->card, ch->master);
815 
816 	return (r);
817 }
818 
819 static struct pcmchan_caps *
820 emupchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused)
821 {
822 	struct emu_pcm_pchinfo *ch = c_devinfo;
823 	struct emu_pcm_info *sc = ch->pcm;
824 
825 	switch (sc->route) {
826 	case RT_FRONT:
827 		/* FALLTHROUGH */
828 	case RT_REAR:
829 		/* FALLTHROUGH */
830 	case RT_SIDE:
831 		return (&emu_playcaps);
832 		break;
833 	case RT_CENTER:
834 		/* FALLTHROUGH */
835 	case RT_SUB:
836 		return (&emu_playcaps_mono);
837 		break;
838 	}
839 	return (NULL);
840 }
841 
842 static kobj_method_t emupchan_methods[] = {
843 	KOBJMETHOD(channel_init, emupchan_init),
844 	KOBJMETHOD(channel_free, emupchan_free),
845 	KOBJMETHOD(channel_setformat, emupchan_setformat),
846 	KOBJMETHOD(channel_setspeed, emupchan_setspeed),
847 	KOBJMETHOD(channel_setblocksize, emupchan_setblocksize),
848 	KOBJMETHOD(channel_trigger, emupchan_trigger),
849 	KOBJMETHOD(channel_getptr, emupchan_getptr),
850 	KOBJMETHOD(channel_getcaps, emupchan_getcaps),
851 	{0, 0}
852 };
853 CHANNEL_DECLARE(emupchan);
854 
855 static void *
856 emurchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
857 {
858 	struct emu_pcm_info *sc = devinfo;
859 	struct emu_pcm_rchinfo *ch;
860 
861 	KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction"));
862 	ch = &sc->rch_adc;
863 	ch->buffer = b;
864 	ch->pcm = sc;
865 	ch->channel = c;
866 	ch->blksz = sc->bufsz / 2; /* We rise interrupt for half-full buffer */
867 	ch->fmt = AFMT_U8;
868 	ch->spd = 8000;
869 	ch->idxreg = sc->is_emu10k1 ? ADCIDX : A_ADCIDX;
870 	ch->basereg = ADCBA;
871 	ch->sizereg = ADCBS;
872 	ch->setupreg = ADCCR;
873 	ch->irqmask = INTE_ADCBUFENABLE;
874 	ch->iprmask = IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL;
875 
876 	if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0)
877 		return (NULL);
878 	else {
879 		ch->timer = emu_timer_create(sc->card);
880 		emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer));
881 		emu_wrptr(sc->card, 0, ch->sizereg, 0);	/* off */
882 		return (ch);
883 	}
884 }
885 
886 static int
887 emurchan_free(kobj_t obj __unused, void *c_devinfo)
888 {
889 	struct emu_pcm_rchinfo *ch = c_devinfo;
890 	struct emu_pcm_info *sc = ch->pcm;
891 
892 	emu_timer_clear(sc->card, ch->timer);
893 	return (0);
894 }
895 
896 static int
897 emurchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format)
898 {
899 	struct emu_pcm_rchinfo *ch = c_devinfo;
900 
901 	ch->fmt = format;
902 	return (0);
903 }
904 
905 static int
906 emurchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
907 {
908 	struct emu_pcm_rchinfo *ch = c_devinfo;
909 
910 	if (ch->pcm->is_emu10k1) {
911 		speed = emu10k1_adcspeed[emu_k1_recval(speed)];
912 	} else {
913 		speed = emu10k2_adcspeed[emu_k2_recval(speed)];
914 	}
915 	ch->spd = speed;
916 	return (ch->spd);
917 }
918 
919 static int
920 emurchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
921 {
922 	struct emu_pcm_rchinfo *ch = c_devinfo;
923 	struct emu_pcm_info *sc = ch->pcm;
924 
925 	ch->blksz = blocksize;
926 	/*
927 	 * If blocksize is less than half of buffer size we will not get
928 	 * BUFHALFFULL interrupt in time and channel will need to generate
929 	 * (and use) timer interrupts. Otherwise channel will be marked dead.
930 	 */
931 	if (ch->blksz < (ch->pcm->bufsz / 2)) {
932 		emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getbps(ch->buffer));
933 		emu_timer_enable(sc->card, ch->timer, 1);
934 	} else {
935 		emu_timer_enable(sc->card, ch->timer, 0);
936 	}
937 	return (ch->blksz);
938 }
939 
940 static int
941 emurchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
942 {
943 	struct emu_pcm_rchinfo *ch = c_devinfo;
944 	struct emu_pcm_info *sc = ch->pcm;
945 	uint32_t val, sz;
946 
947 	if (!PCMTRIG_COMMON(go))
948 		return (0);
949 
950 	switch (sc->bufsz) {
951 	case 4096:
952 		sz = ADCBS_BUFSIZE_4096;
953 		break;
954 	case 8192:
955 		sz = ADCBS_BUFSIZE_8192;
956 		break;
957 	case 16384:
958 		sz = ADCBS_BUFSIZE_16384;
959 		break;
960 	case 32768:
961 		sz = ADCBS_BUFSIZE_32768;
962 		break;
963 	case 65536:
964 		sz = ADCBS_BUFSIZE_65536;
965 		break;
966 	default:
967 		sz = ADCBS_BUFSIZE_4096;
968 	}
969 
970 	snd_mtxlock(sc->lock);
971 	switch (go) {
972 	case PCMTRIG_START:
973 		ch->run = 1;
974 		emu_wrptr(sc->card, 0, ch->sizereg, sz);
975 		val = sc->is_emu10k1 ? ADCCR_LCHANENABLE : A_ADCCR_LCHANENABLE;
976 		if (ch->fmt & AFMT_STEREO)
977 			val |= sc->is_emu10k1 ? ADCCR_RCHANENABLE : A_ADCCR_RCHANENABLE;
978 		val |= sc->is_emu10k1 ? emu_k1_recval(ch->spd) : emu_k2_recval(ch->spd);
979 		emu_wrptr(sc->card, 0, ch->setupreg, 0);
980 		emu_wrptr(sc->card, 0, ch->setupreg, val);
981 		ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc);
982 		break;
983 	case PCMTRIG_STOP:
984 		/* FALLTHROUGH */
985 	case PCMTRIG_ABORT:
986 		ch->run = 0;
987 		emu_wrptr(sc->card, 0, ch->sizereg, 0);
988 		if (ch->setupreg)
989 			emu_wrptr(sc->card, 0, ch->setupreg, 0);
990 		(void)emu_intr_unregister(sc->card, ch->ihandle);
991 		break;
992 	case PCMTRIG_EMLDMAWR:
993 		/* FALLTHROUGH */
994 	case PCMTRIG_EMLDMARD:
995 		/* FALLTHROUGH */
996 	default:
997 		break;
998 	}
999 	snd_mtxunlock(sc->lock);
1000 
1001 	return (0);
1002 }
1003 
1004 static int
1005 emurchan_getptr(kobj_t obj __unused, void *c_devinfo)
1006 {
1007 	struct emu_pcm_rchinfo *ch = c_devinfo;
1008 	struct emu_pcm_info *sc = ch->pcm;
1009 	int r;
1010 
1011 	r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff;
1012 
1013 	return (r);
1014 }
1015 
1016 static struct pcmchan_caps *
1017 emurchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused)
1018 {
1019 	return (&emu_reccaps_adc);
1020 }
1021 
1022 static kobj_method_t emurchan_methods[] = {
1023 	KOBJMETHOD(channel_init, emurchan_init),
1024 	KOBJMETHOD(channel_free, emurchan_free),
1025 	KOBJMETHOD(channel_setformat, emurchan_setformat),
1026 	KOBJMETHOD(channel_setspeed, emurchan_setspeed),
1027 	KOBJMETHOD(channel_setblocksize, emurchan_setblocksize),
1028 	KOBJMETHOD(channel_trigger, emurchan_trigger),
1029 	KOBJMETHOD(channel_getptr, emurchan_getptr),
1030 	KOBJMETHOD(channel_getcaps, emurchan_getcaps),
1031 	{0, 0}
1032 };
1033 CHANNEL_DECLARE(emurchan);
1034 
1035 static void *
1036 emufxrchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
1037 {
1038 	struct emu_pcm_info *sc = devinfo;
1039 	struct emu_pcm_rchinfo *ch;
1040 
1041 	KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction"));
1042 
1043 	if (sc == NULL) return (NULL);
1044 
1045 	ch = &(sc->rch_efx);
1046 	ch->fmt = AFMT_S16_LE;
1047 	ch->spd = sc->is_emu10k1 ? 48000*32 : 48000 * 64;
1048 	ch->idxreg = FXIDX;
1049 	ch->basereg = FXBA;
1050 	ch->sizereg = FXBS;
1051 	ch->irqmask = INTE_EFXBUFENABLE;
1052 	ch->iprmask = IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL;
1053 	ch->buffer = b;
1054 	ch->pcm = sc;
1055 	ch->channel = c;
1056 	ch->blksz = sc->bufsz / 2;
1057 
1058 	if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0)
1059 		return (NULL);
1060 	else {
1061 		emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer));
1062 		emu_wrptr(sc->card, 0, ch->sizereg, 0);	/* off */
1063 		return (ch);
1064 	}
1065 }
1066 
1067 static int
1068 emufxrchan_setformat(kobj_t obj __unused, void *c_devinfo __unused, uint32_t format)
1069 {
1070 	if (format == AFMT_S16_LE) return (0);
1071 	return (EINVAL);
1072 }
1073 
1074 static int
1075 emufxrchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
1076 {
1077 	struct emu_pcm_rchinfo *ch = c_devinfo;
1078 
1079 	/* FIXED RATE CHANNEL */
1080 	return (ch->spd);
1081 }
1082 
1083 static int
1084 emufxrchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
1085 {
1086 	struct emu_pcm_rchinfo *ch = c_devinfo;
1087 
1088 	ch->blksz = blocksize;
1089 	/*
1090 	 * XXX If blocksize is less than half of buffer size we will not get
1091 	 * interrupt in time and channel will die due to interrupt timeout.
1092 	 * This	should not happen with FX rchan, because it will fill buffer
1093 	 * very	fast (64K buffer is 0.021seconds on Audigy).
1094 	 */
1095 	if (ch->blksz < (ch->pcm->bufsz / 2))
1096 		ch->blksz = ch->pcm->bufsz / 2;
1097 	return (ch->blksz);
1098 }
1099 
1100 static int
1101 emufxrchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
1102 {
1103 	struct emu_pcm_rchinfo *ch = c_devinfo;
1104 	struct emu_pcm_info *sc = ch->pcm;
1105 	uint32_t sz;
1106 
1107 	if (!PCMTRIG_COMMON(go))
1108 		return (0);
1109 
1110 	switch (sc->bufsz) {
1111 	case 4096:
1112 		sz = ADCBS_BUFSIZE_4096;
1113 		break;
1114 	case 8192:
1115 		sz = ADCBS_BUFSIZE_8192;
1116 		break;
1117 	case 16384:
1118 		sz = ADCBS_BUFSIZE_16384;
1119 		break;
1120 	case 32768:
1121 		sz = ADCBS_BUFSIZE_32768;
1122 		break;
1123 	case 65536:
1124 		sz = ADCBS_BUFSIZE_65536;
1125 		break;
1126 	default:
1127 		sz = ADCBS_BUFSIZE_4096;
1128 	}
1129 
1130 	snd_mtxlock(sc->lock);
1131 	switch (go) {
1132 	case PCMTRIG_START:
1133 		ch->run = 1;
1134 		emu_wrptr(sc->card, 0, ch->sizereg, sz);
1135 		ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc);
1136 		/*
1137 		 * SB Live! is limited to 32 mono channels. Audigy
1138 		 * has 64 mono channels. Channels are enabled
1139 		 * by setting a bit in A_FXWC[1|2] registers.
1140 		 */
1141 		/* XXX there is no way to demultiplex this streams for now */
1142 		if (sc->is_emu10k1) {
1143 			emu_wrptr(sc->card, 0, FXWC, 0xffffffff);
1144 		} else {
1145 			emu_wrptr(sc->card, 0, A_FXWC1, 0xffffffff);
1146 			emu_wrptr(sc->card, 0, A_FXWC2, 0xffffffff);
1147 		}
1148 		break;
1149 	case PCMTRIG_STOP:
1150 		/* FALLTHROUGH */
1151 	case PCMTRIG_ABORT:
1152 		ch->run = 0;
1153 		if (sc->is_emu10k1) {
1154 			emu_wrptr(sc->card, 0, FXWC, 0x0);
1155 		} else {
1156 			emu_wrptr(sc->card, 0, A_FXWC1, 0x0);
1157 			emu_wrptr(sc->card, 0, A_FXWC2, 0x0);
1158 		}
1159 		emu_wrptr(sc->card, 0, ch->sizereg, 0);
1160 		(void)emu_intr_unregister(sc->card, ch->ihandle);
1161 		break;
1162 	case PCMTRIG_EMLDMAWR:
1163 		/* FALLTHROUGH */
1164 	case PCMTRIG_EMLDMARD:
1165 		/* FALLTHROUGH */
1166 	default:
1167 		break;
1168 	}
1169 	snd_mtxunlock(sc->lock);
1170 
1171 	return (0);
1172 }
1173 
1174 static int
1175 emufxrchan_getptr(kobj_t obj __unused, void *c_devinfo)
1176 {
1177 	struct emu_pcm_rchinfo *ch = c_devinfo;
1178 	struct emu_pcm_info *sc = ch->pcm;
1179 	int r;
1180 
1181 	r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff;
1182 
1183 	return (r);
1184 }
1185 
1186 static struct pcmchan_caps *
1187 emufxrchan_getcaps(kobj_t obj __unused, void *c_devinfo)
1188 {
1189 	struct emu_pcm_rchinfo *ch = c_devinfo;
1190 	struct emu_pcm_info *sc = ch->pcm;
1191 
1192 	if (sc->is_emu10k1)
1193 		return (&emu_reccaps_efx_live);
1194 	return (&emu_reccaps_efx_audigy);
1195 
1196 }
1197 
1198 static int
1199 emufxrchan_getrates(kobj_t obj __unused, void *c_devinfo, int **rates)
1200 {
1201 	struct emu_pcm_rchinfo *ch = c_devinfo;
1202 	struct emu_pcm_info *sc = ch->pcm;
1203 
1204 	if (sc->is_emu10k1)
1205 		*rates = emu_rates_live;
1206 	else
1207 		*rates = emu_rates_audigy;
1208 
1209 	return 1;
1210 }
1211 
1212 static kobj_method_t emufxrchan_methods[] = {
1213 	KOBJMETHOD(channel_init, emufxrchan_init),
1214 	KOBJMETHOD(channel_setformat, emufxrchan_setformat),
1215 	KOBJMETHOD(channel_setspeed, emufxrchan_setspeed),
1216 	KOBJMETHOD(channel_setblocksize, emufxrchan_setblocksize),
1217 	KOBJMETHOD(channel_trigger, emufxrchan_trigger),
1218 	KOBJMETHOD(channel_getptr, emufxrchan_getptr),
1219 	KOBJMETHOD(channel_getcaps, emufxrchan_getcaps),
1220 	KOBJMETHOD(channel_getrates, emufxrchan_getrates),
1221 	{0, 0}
1222 };
1223 CHANNEL_DECLARE(emufxrchan);
1224 
1225 
1226 static uint32_t
1227 emu_pcm_intr(void *pcm, uint32_t stat)
1228 {
1229 	struct emu_pcm_info *sc = (struct emu_pcm_info *)pcm;
1230 	uint32_t ack;
1231 	int i;
1232 
1233 	ack = 0;
1234 
1235 	snd_mtxlock(sc->lock);
1236 
1237 	if (stat & IPR_INTERVALTIMER) {
1238 		ack |= IPR_INTERVALTIMER;
1239 		for (i = 0; i < MAX_CHANNELS; i++)
1240 			if (sc->pch[i].channel) {
1241 				if (sc->pch[i].run == 1) {
1242 					snd_mtxunlock(sc->lock);
1243 					chn_intr(sc->pch[i].channel);
1244 					snd_mtxlock(sc->lock);
1245 				} else
1246 					emu_timer_enable(sc->card, sc->pch[i].timer, 0);
1247 			}
1248 		/* ADC may install timer to get low-latency interrupts */
1249 		if ((sc->rch_adc.channel) && (sc->rch_adc.run)) {
1250 			snd_mtxunlock(sc->lock);
1251 			chn_intr(sc->rch_adc.channel);
1252 			snd_mtxlock(sc->lock);
1253 		}
1254 		/*
1255 		 * EFX does not use timer, because it will fill
1256 		 * buffer at least 32x times faster than ADC.
1257 		 */
1258 	}
1259 
1260 
1261 	if (stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL)) {
1262 		ack |= stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL);
1263 		if (sc->rch_adc.channel) {
1264 			snd_mtxunlock(sc->lock);
1265 			chn_intr(sc->rch_adc.channel);
1266 			snd_mtxlock(sc->lock);
1267 		}
1268 	}
1269 
1270 	if (stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL)) {
1271 		ack |= stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL);
1272 		if (sc->rch_efx.channel) {
1273 			snd_mtxunlock(sc->lock);
1274 			chn_intr(sc->rch_efx.channel);
1275 			snd_mtxlock(sc->lock);
1276 		}
1277 	}
1278 	snd_mtxunlock(sc->lock);
1279 
1280 	return (ack);
1281 }
1282 
1283 static int
1284 emu_pcm_init(struct emu_pcm_info *sc)
1285 {
1286 	sc->bufsz = pcm_getbuffersize(sc->dev, EMUPAGESIZE, EMU_REC_BUFSZ, EMU_MAX_BUFSZ);
1287 	return (0);
1288 }
1289 
1290 static int
1291 emu_pcm_uninit(struct emu_pcm_info *sc __unused)
1292 {
1293 	return (0);
1294 }
1295 
1296 static int
1297 emu_pcm_probe(device_t dev)
1298 {
1299 	uintptr_t func, route, r;
1300 	const char *rt;
1301 	char buffer[255];
1302 
1303 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_FUNC, &func);
1304 
1305 	if (func != SCF_PCM)
1306 		return (ENXIO);
1307 
1308 	rt = "UNKNOWN";
1309 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route);
1310 	switch (route) {
1311 	case RT_FRONT:
1312 		rt = "front";
1313 		break;
1314 	case RT_REAR:
1315 		rt = "rear";
1316 		break;
1317 	case RT_CENTER:
1318 		rt = "center";
1319 		break;
1320 	case RT_SUB:
1321 		rt = "subwoofer";
1322 		break;
1323 	case RT_SIDE:
1324 		rt = "side";
1325 		break;
1326 	case RT_MCHRECORD:
1327 		rt = "multichannel recording";
1328 		break;
1329 	}
1330 
1331 	snprintf(buffer, 255, "EMU10Kx DSP %s PCM interface", rt);
1332 	device_set_desc_copy(dev, buffer);
1333 	return (0);
1334 }
1335 
1336 static int
1337 emu_pcm_attach(device_t dev)
1338 {
1339 	struct emu_pcm_info *sc;
1340 	unsigned int i;
1341 	char status[SND_STATUSLEN];
1342 	uint32_t inte, ipr;
1343 	uintptr_t route, r, ivar;
1344 
1345 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
1346 	sc->card = (struct emu_sc_info *)(device_get_softc(device_get_parent(dev)));
1347 	if (sc->card == NULL) {
1348 		device_printf(dev, "cannot get bridge conf\n");
1349 		free(sc, M_DEVBUF);
1350 		return (ENXIO);
1351 	}
1352 
1353 	sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_emu10kx pcm softc");
1354 	sc->dev = dev;
1355 
1356 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &ivar);
1357 	sc->is_emu10k1 = ivar ? 1 : 0;
1358 
1359 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_MCH_DISABLED, &ivar);
1360 	sc->mch_disabled = ivar ? 1 : 0;
1361 
1362 	sc->codec = NULL;
1363 
1364 	for (i = 0; i < 8; i++) {
1365 		sc->rt.routing_left[i] = i;
1366 		sc->rt.amounts_left[i] = 0x00;
1367 		sc->rt.routing_right[i] = i;
1368 		sc->rt.amounts_right[i] = 0x00;
1369 	}
1370 
1371 	for (i = 0; i < 8; i++) {
1372 		sc->rt_mono.routing_left[i] = i;
1373 		sc->rt_mono.amounts_left[i] = 0x00;
1374 		sc->rt_mono.routing_right[i] = i;
1375 		sc->rt_mono.amounts_right[i] = 0x00;
1376 	}
1377 
1378 	sc->emu10k1_volcache[0][0] = 75;
1379 	sc->emu10k1_volcache[1][0] = 75;
1380 	sc->emu10k1_volcache[0][1] = 75;
1381 	sc->emu10k1_volcache[1][1] = 75;
1382 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route);
1383 	sc->route = route;
1384 	switch (route) {
1385 	case RT_FRONT:
1386 		sc->rt.amounts_left[0] = 0xff;
1387 		sc->rt.amounts_right[1] = 0xff;
1388 		sc->rt_mono.amounts_left[0] = 0xff;
1389 		sc->rt_mono.amounts_left[1] = 0xff;
1390 		if (sc->is_emu10k1)
1391 			sc->codec = AC97_CREATE(dev, sc, emu_ac97);
1392 		else
1393 			sc->codec = AC97_CREATE(dev, sc, emu_eac97);
1394 		sc->ac97_mixerclass = NULL;
1395 		if (sc->codec != NULL)
1396 			sc->ac97_mixerclass = ac97_getmixerclass();
1397 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1398 			device_printf(dev, "failed to initialize DSP mixer\n");
1399 			goto bad;
1400 		}
1401 		break;
1402 	case RT_REAR:
1403 		sc->rt.amounts_left[2] = 0xff;
1404 		sc->rt.amounts_right[3] = 0xff;
1405 		sc->rt_mono.amounts_left[2] = 0xff;
1406 		sc->rt_mono.amounts_left[3] = 0xff;
1407 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1408 			device_printf(dev, "failed to initialize mixer\n");
1409 			goto bad;
1410 		}
1411 		break;
1412 	case RT_CENTER:
1413 		sc->rt.amounts_left[4] = 0xff;
1414 		sc->rt_mono.amounts_left[4] = 0xff;
1415 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1416 			device_printf(dev, "failed to initialize mixer\n");
1417 			goto bad;
1418 		}
1419 		break;
1420 	case RT_SUB:
1421 		sc->rt.amounts_left[5] = 0xff;
1422 		sc->rt_mono.amounts_left[5] = 0xff;
1423 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1424 			device_printf(dev, "failed to initialize mixer\n");
1425 			goto bad;
1426 		}
1427 		break;
1428 	case RT_SIDE:
1429 		sc->rt.amounts_left[6] = 0xff;
1430 		sc->rt.amounts_right[7] = 0xff;
1431 		sc->rt_mono.amounts_left[6] = 0xff;
1432 		sc->rt_mono.amounts_left[7] = 0xff;
1433 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1434 			device_printf(dev, "failed to initialize mixer\n");
1435 			goto bad;
1436 		}
1437 		break;
1438 	case RT_MCHRECORD:
1439 		if (mixer_init(dev, &emuefxmixer_class, sc)) {
1440 			device_printf(dev, "failed to initialize EFX mixer\n");
1441 			goto bad;
1442 		}
1443 		break;
1444 	default:
1445 		device_printf(dev, "invalid default route\n");
1446 		goto bad;
1447 	}
1448 
1449 	inte = INTE_INTERVALTIMERENB;
1450 	ipr = IPR_INTERVALTIMER; /* Used by playback & ADC */
1451 	sc->ihandle = emu_intr_register(sc->card, inte, ipr, &emu_pcm_intr, sc);
1452 
1453 	if (emu_pcm_init(sc) == -1) {
1454 		device_printf(dev, "unable to initialize PCM part of the card\n");
1455 		goto bad;
1456 	}
1457 
1458 	/*
1459 	 * We don't register interrupt handler with snd_setup_intr
1460 	 * in pcm device. Mark pcm device as MPSAFE manually.
1461 	 */
1462 	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
1463 
1464 	/* XXX we should better get number of available channels from parent */
1465 	if (pcm_register(dev, sc, (route == RT_FRONT) ? MAX_CHANNELS : 1, (route == RT_FRONT) ? 1 : 0)) {
1466 		device_printf(dev, "can't register PCM channels!\n");
1467 		goto bad;
1468 	}
1469 	sc->pnum = 0;
1470 	if (route != RT_MCHRECORD)
1471 		pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc);
1472 	if (route == RT_FRONT) {
1473 		for (i = 1; i < MAX_CHANNELS; i++)
1474 			pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc);
1475 		pcm_addchan(dev, PCMDIR_REC, &emurchan_class, sc);
1476 	}
1477 	if (route == RT_MCHRECORD)
1478 		pcm_addchan(dev, PCMDIR_REC, &emufxrchan_class, sc);
1479 
1480 	snprintf(status, SND_STATUSLEN, "on %s", device_get_nameunit(device_get_parent(dev)));
1481 	pcm_setstatus(dev, status);
1482 
1483 	return (0);
1484 
1485 bad:
1486 	if (sc->codec)
1487 		ac97_destroy(sc->codec);
1488 	if (sc->lock)
1489 		snd_mtxfree(sc->lock);
1490 	free(sc, M_DEVBUF);
1491 	return (ENXIO);
1492 }
1493 
1494 static int
1495 emu_pcm_detach(device_t dev)
1496 {
1497 	int r;
1498 	struct emu_pcm_info *sc;
1499 
1500 	sc = pcm_getdevinfo(dev);
1501 
1502 	r = pcm_unregister(dev);
1503 
1504 	if (r) 	return (r);
1505 
1506 	emu_pcm_uninit(sc);
1507 
1508 	if (sc->lock)
1509 		snd_mtxfree(sc->lock);
1510 	free(sc, M_DEVBUF);
1511 
1512 	return (0);
1513 }
1514 
1515 static device_method_t emu_pcm_methods[] = {
1516 	DEVMETHOD(device_probe, emu_pcm_probe),
1517 	DEVMETHOD(device_attach, emu_pcm_attach),
1518 	DEVMETHOD(device_detach, emu_pcm_detach),
1519 	{0, 0}
1520 };
1521 
1522 static driver_t emu_pcm_driver = {
1523 	"pcm",
1524 	emu_pcm_methods,
1525 	PCM_SOFTC_SIZE,
1526 	NULL,
1527 	0,
1528 	NULL
1529 };
1530 DRIVER_MODULE(snd_emu10kx_pcm, emu10kx, emu_pcm_driver, pcm_devclass, 0, 0);
1531 MODULE_DEPEND(snd_emu10kx_pcm, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER);
1532 MODULE_DEPEND(snd_emu10kx_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1533 MODULE_VERSION(snd_emu10kx_pcm, SND_EMU10KX_PREFVER);
1534