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