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