1160383Snetchild/*-
2160383Snetchild * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3172150Sariff * Copyright (c) 2003-2007 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru>
4160383Snetchild * All rights reserved.
5160383Snetchild *
6160383Snetchild * Redistribution and use in source and binary forms, with or without
7160383Snetchild * modification, are permitted provided that the following conditions
8160383Snetchild * are met:
9160383Snetchild * 1. Redistributions of source code must retain the above copyright
10160383Snetchild *    notice, this list of conditions and the following disclaimer.
11160383Snetchild * 2. Redistributions in binary form must reproduce the above copyright
12160383Snetchild *    notice, this list of conditions and the following disclaimer in the
13160383Snetchild *    documentation and/or other materials provided with the distribution.
14160383Snetchild *
15160383Snetchild * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16160383Snetchild * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17160383Snetchild * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18160383Snetchild * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19160383Snetchild * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20160383Snetchild * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21160383Snetchild * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22160383Snetchild * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
23160383Snetchild * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24160383Snetchild * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25160383Snetchild * SUCH DAMAGE.
26160383Snetchild *
27160383Snetchild * $FreeBSD$
28160383Snetchild */
29160383Snetchild
30160383Snetchild#include <sys/param.h>
31160383Snetchild#include <sys/types.h>
32160383Snetchild#include <sys/bus.h>
33160383Snetchild#include <machine/bus.h>
34160383Snetchild#include <sys/rman.h>
35160383Snetchild#include <sys/systm.h>
36160383Snetchild#include <sys/sbuf.h>
37160383Snetchild#include <sys/queue.h>
38160383Snetchild#include <sys/systm.h>
39160383Snetchild#include <sys/lock.h>
40160383Snetchild#include <sys/mutex.h>
41160383Snetchild
42193640Sariff#ifdef HAVE_KERNEL_OPTION_HEADERS
43193640Sariff#include "opt_snd.h"
44193640Sariff#endif
45193640Sariff
46160383Snetchild#include <dev/sound/chip.h>
47160383Snetchild#include <dev/sound/pcm/sound.h>
48160383Snetchild#include <dev/sound/pcm/ac97.h>
49160383Snetchild
50160383Snetchild#include "mixer_if.h"
51160383Snetchild
52230964Spfg#include <dev/sound/pci/emuxkireg.h>
53160383Snetchild#include <dev/sound/pci/emu10kx.h>
54160383Snetchild
55160383Snetchildstruct emu_pcm_pchinfo {
56160383Snetchild	int		spd;
57160383Snetchild	int		fmt;
58172150Sariff	unsigned int	blksz;
59160383Snetchild	int		run;
60160383Snetchild	struct emu_voice *master;
61160383Snetchild	struct emu_voice *slave;
62160383Snetchild	struct snd_dbuf	*buffer;
63160383Snetchild	struct pcm_channel *channel;
64160383Snetchild	struct emu_pcm_info *pcm;
65160383Snetchild	int		timer;
66160383Snetchild};
67160383Snetchild
68160383Snetchildstruct emu_pcm_rchinfo {
69160383Snetchild	int		spd;
70160383Snetchild	int		fmt;
71172150Sariff	unsigned int	blksz;
72160383Snetchild	int		run;
73160383Snetchild	uint32_t 	idxreg;
74160383Snetchild	uint32_t	basereg;
75160383Snetchild	uint32_t	sizereg;
76160383Snetchild	uint32_t	setupreg;
77160383Snetchild	uint32_t	irqmask;
78160383Snetchild	uint32_t	iprmask;
79160383Snetchild	int 		ihandle;
80160383Snetchild	struct snd_dbuf	*buffer;
81160383Snetchild	struct pcm_channel *channel;
82160383Snetchild	struct emu_pcm_info *pcm;
83172150Sariff	int		timer;
84160383Snetchild};
85160383Snetchild
86165833Snetchild/* XXX Hardware playback channels */
87160383Snetchild#define	MAX_CHANNELS	4
88160383Snetchild
89160383Snetchild#if MAX_CHANNELS > 13
90160383Snetchild#error	Too many hardware channels defined. 13 is the maximum
91160383Snetchild#endif
92165833Snetchild
93160383Snetchildstruct emu_pcm_info {
94160383Snetchild	struct mtx		*lock;
95160383Snetchild	device_t		dev;		/* device information */
96160383Snetchild	struct emu_sc_info 	*card;
97160383Snetchild	struct emu_pcm_pchinfo	pch[MAX_CHANNELS];	/* hardware channels */
98160383Snetchild	int			pnum;		/* next free channel number */
99165833Snetchild	struct emu_pcm_rchinfo	rch_adc;
100165833Snetchild	struct emu_pcm_rchinfo	rch_efx;
101160383Snetchild	struct emu_route	rt;
102161056Snetchild	struct emu_route	rt_mono;
103160383Snetchild	int			route;
104160383Snetchild	int			ihandle;	/* interrupt handler */
105160383Snetchild	unsigned int		bufsz;
106160383Snetchild	int			is_emu10k1;
107160383Snetchild	struct ac97_info	*codec;
108160383Snetchild	uint32_t 		ac97_state[0x7F];
109172150Sariff	kobj_class_t		ac97_mixerclass;
110172150Sariff	uint32_t		ac97_recdevs;
111172150Sariff	uint32_t		ac97_playdevs;
112172150Sariff	struct snd_mixer	*sm;
113172150Sariff	int			mch_disabled;
114172150Sariff	unsigned int		emu10k1_volcache[2][2];
115160383Snetchild};
116160383Snetchild
117160383Snetchild
118165833Snetchildstatic uint32_t emu_rfmt_adc[] = {
119193640Sariff	SND_FORMAT(AFMT_S16_LE, 1, 0),
120193640Sariff	SND_FORMAT(AFMT_S16_LE, 2, 0),
121160383Snetchild	0
122160383Snetchild};
123165833Snetchildstatic struct pcmchan_caps emu_reccaps_adc = {
124165833Snetchild	8000, 48000, emu_rfmt_adc, 0
125160383Snetchild};
126160383Snetchild
127165833Snetchildstatic uint32_t emu_rfmt_efx[] = {
128193640Sariff	SND_FORMAT(AFMT_S16_LE, 1, 0),
129165833Snetchild	0
130165833Snetchild};
131165833Snetchild
132165833Snetchildstatic struct pcmchan_caps emu_reccaps_efx_live = {
133165833Snetchild	48000*32, 48000*32, emu_rfmt_efx, 0
134165833Snetchild};
135165833Snetchild
136165833Snetchildstatic struct pcmchan_caps emu_reccaps_efx_audigy = {
137165833Snetchild	48000*64, 48000*64, emu_rfmt_efx, 0
138165833Snetchild};
139165833Snetchild
140172150Sariffstatic int emu_rates_live[] = {
141172150Sariff	48000*32
142172150Sariff};
143172150Sariff
144172150Sariffstatic int emu_rates_audigy[] = {
145172150Sariff	48000*64
146172150Sariff};
147172150Sariff
148160383Snetchildstatic uint32_t emu_pfmt[] = {
149193640Sariff	SND_FORMAT(AFMT_U8, 1, 0),
150193640Sariff	SND_FORMAT(AFMT_U8, 2, 0),
151193640Sariff	SND_FORMAT(AFMT_S16_LE, 1, 0),
152193640Sariff	SND_FORMAT(AFMT_S16_LE, 2, 0),
153160383Snetchild	0
154160383Snetchild};
155160383Snetchildstatic uint32_t emu_pfmt_mono[] = {
156193640Sariff	SND_FORMAT(AFMT_U8, 1, 0),
157193640Sariff	SND_FORMAT(AFMT_S16_LE, 1, 0),
158160383Snetchild	0
159160383Snetchild};
160160383Snetchild
161160383Snetchildstatic struct pcmchan_caps emu_playcaps = {4000, 48000, emu_pfmt, 0};
162160383Snetchildstatic struct pcmchan_caps emu_playcaps_mono = {4000, 48000, emu_pfmt_mono, 0};
163160383Snetchild
164160383Snetchildstatic int emu10k1_adcspeed[8] = {48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000};
165160383Snetchild/* audigy supports 12kHz. */
166160383Snetchildstatic int emu10k2_adcspeed[9] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000};
167160383Snetchild
168160383Snetchildstatic uint32_t emu_pcm_intr(void *pcm, uint32_t stat);
169160383Snetchild
170172150Sariffstatic const struct emu_dspmix_props_k1 {
171172150Sariff	uint8_t	present;
172172150Sariff	uint8_t	recdev;
173172150Sariff	int8_t	input;
174172150Sariff} dspmix_k1 [SOUND_MIXER_NRDEVICES] = {
175172150Sariff	/* no mixer device for ac97 */		/* in0 AC97 */
176172150Sariff	[SOUND_MIXER_DIGITAL1] = {1, 1, 1},	/* in1 CD SPDIF */
177172150Sariff	/* not connected */			/* in2 (zoom) */
178172150Sariff	[SOUND_MIXER_DIGITAL2] = {1, 1, 3},	/* in3 toslink */
179172150Sariff	[SOUND_MIXER_LINE2] =    {1, 1, 4},	/* in4 Line-In2 */
180172150Sariff	[SOUND_MIXER_DIGITAL3] = {1, 1, 5},	/* in5 on-card  SPDIF */
181172150Sariff	[SOUND_MIXER_LINE3] =    {1, 1, 6},	/* in6 AUX2 */
182172150Sariff	/* not connected */			/* in7 */
183160383Snetchild};
184172150Sariffstatic const struct emu_dspmix_props_k2 {
185172150Sariff	uint8_t	present;
186172150Sariff	uint8_t	recdev;
187172150Sariff	int8_t	input;
188172150Sariff} dspmix_k2 [SOUND_MIXER_NRDEVICES] = {
189172150Sariff	[SOUND_MIXER_VOLUME] =	{1, 0, (-1)},
190172150Sariff	[SOUND_MIXER_PCM] =	{1, 0, (-1)},
191160383Snetchild
192172150Sariff	/* no mixer device */			/* in0 AC97 */
193172150Sariff	[SOUND_MIXER_DIGITAL1] = {1, 1, 1},	/* in1 CD SPDIF */
194172150Sariff	[SOUND_MIXER_DIGITAL2] = {1, 1, 2},	/* in2 COAX SPDIF */
195172150Sariff	/* not connected */			/* in3 */
196172150Sariff	[SOUND_MIXER_LINE2] =    {1, 1, 4},	/* in4 Line-In2 */
197172150Sariff	[SOUND_MIXER_DIGITAL3] = {1, 1, 5},	/* in5 on-card  SPDIF */
198172150Sariff	[SOUND_MIXER_LINE3] =    {1, 1, 6},	/* in6 AUX2 */
199172150Sariff	/* not connected */			/* in7 */
200172150Sariff};
201172150Sariff
202160383Snetchildstatic int
203160383Snetchildemu_dspmixer_init(struct snd_mixer *m)
204160383Snetchild{
205172150Sariff	struct emu_pcm_info	*sc;
206160383Snetchild	int i;
207172150Sariff	int p, r;
208160383Snetchild
209172150Sariff	p = 0;
210172150Sariff	r = 0;
211172150Sariff
212172150Sariff	sc = mix_getdevinfo(m);
213172150Sariff
214172150Sariff	if (sc->route == RT_FRONT) {
215172150Sariff		/* create submixer for AC97 codec */
216172150Sariff		if ((sc->ac97_mixerclass != NULL) && (sc->codec != NULL)) {
217172150Sariff			sc->sm = mixer_create(sc->dev, sc->ac97_mixerclass, sc->codec, "ac97");
218172150Sariff			if (sc->sm != NULL) {
219172150Sariff				p = mix_getdevs(sc->sm);
220172150Sariff				r = mix_getrecdevs(sc->sm);
221172150Sariff			}
222172150Sariff		}
223172150Sariff
224172150Sariff		sc->ac97_playdevs = p;
225172150Sariff		sc->ac97_recdevs = r;
226160383Snetchild	}
227160383Snetchild
228172150Sariff	/* This two are always here */
229172150Sariff	p |= (1 << SOUND_MIXER_PCM);
230172150Sariff	p |= (1 << SOUND_MIXER_VOLUME);
231172150Sariff
232172150Sariff	if (sc->route == RT_FRONT) {
233172150Sariff		if (sc->is_emu10k1) {
234172150Sariff			for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
235172150Sariff				if (dspmix_k1[i].present)
236172150Sariff					p |= (1 << i);
237172150Sariff				if (dspmix_k1[i].recdev)
238172150Sariff					r |= (1 << i);
239172150Sariff			}
240172150Sariff		} else {
241172150Sariff			for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
242172150Sariff				if (dspmix_k2[i].present)
243172150Sariff					p |= (1 << i);
244172150Sariff				if (dspmix_k2[i].recdev)
245172150Sariff					r |= (1 << i);
246172150Sariff			}
247172150Sariff		}
248172150Sariff	}
249172150Sariff
250172150Sariff	mix_setdevs(m, p);
251172150Sariff	mix_setrecdevs(m, r);
252172150Sariff
253160383Snetchild	return (0);
254160383Snetchild}
255160383Snetchild
256160383Snetchildstatic int
257172150Sariffemu_dspmixer_uninit(struct snd_mixer *m)
258172150Sariff{
259172150Sariff	struct emu_pcm_info	*sc;
260172150Sariff	int err = 0;
261172150Sariff
262172150Sariff	/* drop submixer for AC97 codec */
263172150Sariff	sc = mix_getdevinfo(m);
264172150Sariff	if (sc->sm != NULL)
265172150Sariff		err = mixer_delete(sc->sm);
266172150Sariff		if (err)
267172150Sariff			return (err);
268172150Sariff		sc->sm = NULL;
269172150Sariff	return (0);
270172150Sariff}
271172150Sariff
272172150Sariffstatic int
273160383Snetchildemu_dspmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
274160383Snetchild{
275160383Snetchild	struct emu_pcm_info *sc;
276160383Snetchild
277160383Snetchild	sc = mix_getdevinfo(m);
278160383Snetchild
279160383Snetchild	switch (dev) {
280160383Snetchild	case SOUND_MIXER_VOLUME:
281160383Snetchild		switch (sc->route) {
282172150Sariff		case RT_FRONT:
283172150Sariff			if (sc->sm != NULL)
284172150Sariff				mix_set(sc->sm, dev, left, right);
285172150Sariff			if (sc->mch_disabled) {
286172150Sariff				/* In emu10k1 case PCM volume does not affect
287172150Sariff				   sound routed to rear & center/sub (it is connected
288172150Sariff				   to AC97 codec). Calculate it manually. */
289172150Sariff				/* This really should belong to emu10kx.c */
290172150Sariff				if (sc->is_emu10k1) {
291172150Sariff					sc->emu10k1_volcache[0][0] = left;
292172150Sariff					left = left * sc->emu10k1_volcache[1][0] / 100;
293172150Sariff					sc->emu10k1_volcache[0][1] = right;
294172150Sariff					right = right * sc->emu10k1_volcache[1][1] / 100;
295172150Sariff				}
296172150Sariff
297172150Sariff				emumix_set_volume(sc->card, M_MASTER_REAR_L, left);
298172150Sariff				emumix_set_volume(sc->card, M_MASTER_REAR_R, right);
299172150Sariff				if (!sc->is_emu10k1) {
300172150Sariff					emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2);
301172150Sariff					emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2);
302172150Sariff					/* XXX side */
303172150Sariff				}
304172150Sariff			} /* mch disabled */
305172150Sariff			break;
306160383Snetchild		case RT_REAR:
307160383Snetchild			emumix_set_volume(sc->card, M_MASTER_REAR_L, left);
308160383Snetchild			emumix_set_volume(sc->card, M_MASTER_REAR_R, right);
309160383Snetchild			break;
310160383Snetchild		case RT_CENTER:
311160383Snetchild			emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2);
312160383Snetchild			break;
313160383Snetchild		case RT_SUB:
314160383Snetchild			emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2);
315160383Snetchild			break;
316160383Snetchild		}
317160383Snetchild		break;
318160383Snetchild	case SOUND_MIXER_PCM:
319160383Snetchild		switch (sc->route) {
320172150Sariff		case RT_FRONT:
321172150Sariff			if (sc->sm != NULL)
322172150Sariff				mix_set(sc->sm, dev, left, right);
323172150Sariff			if (sc->mch_disabled) {
324172150Sariff				/* See SOUND_MIXER_VOLUME case */
325172150Sariff				if (sc->is_emu10k1) {
326172150Sariff					sc->emu10k1_volcache[1][0] = left;
327172150Sariff					left = left * sc->emu10k1_volcache[0][0] / 100;
328172150Sariff					sc->emu10k1_volcache[1][1] = right;
329172150Sariff					right = right * sc->emu10k1_volcache[0][1] / 100;
330172150Sariff				}
331172150Sariff				emumix_set_volume(sc->card, M_MASTER_REAR_L, left);
332172150Sariff				emumix_set_volume(sc->card, M_MASTER_REAR_R, right);
333172150Sariff
334172150Sariff				if (!sc->is_emu10k1) {
335172150Sariff					emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2);
336172150Sariff					emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2);
337172150Sariff					/* XXX side */
338172150Sariff				}
339172150Sariff			} /* mch_disabled */
340172150Sariff			break;
341160383Snetchild		case RT_REAR:
342160383Snetchild			emumix_set_volume(sc->card, M_FX2_REAR_L, left);
343160383Snetchild			emumix_set_volume(sc->card, M_FX3_REAR_R, right);
344160383Snetchild			break;
345160383Snetchild		case RT_CENTER:
346160383Snetchild			emumix_set_volume(sc->card, M_FX4_CENTER, (left+right)/2);
347160383Snetchild			break;
348160383Snetchild		case RT_SUB:
349160383Snetchild			emumix_set_volume(sc->card, M_FX5_SUBWOOFER, (left+right)/2);
350160383Snetchild			break;
351160383Snetchild		}
352160383Snetchild		break;
353172150Sariff	case SOUND_MIXER_DIGITAL1:	/* CD SPDIF, in1 */
354172150Sariff			emumix_set_volume(sc->card, M_IN1_FRONT_L, left);
355172150Sariff			emumix_set_volume(sc->card, M_IN1_FRONT_R, right);
356172150Sariff		break;
357172150Sariff	case SOUND_MIXER_DIGITAL2:
358172150Sariff			if (sc->is_emu10k1) {
359172150Sariff				/* TOSLink, in3 */
360172150Sariff				emumix_set_volume(sc->card, M_IN3_FRONT_L, left);
361172150Sariff				emumix_set_volume(sc->card, M_IN3_FRONT_R, right);
362172150Sariff			} else {
363172150Sariff				/* COAX SPDIF, in2 */
364172150Sariff				emumix_set_volume(sc->card, M_IN2_FRONT_L, left);
365172150Sariff				emumix_set_volume(sc->card, M_IN2_FRONT_R, right);
366172150Sariff			}
367172150Sariff		break;
368172150Sariff	case SOUND_MIXER_LINE2:		/* Line-In2, in4 */
369172150Sariff			emumix_set_volume(sc->card, M_IN4_FRONT_L, left);
370172150Sariff			emumix_set_volume(sc->card, M_IN4_FRONT_R, right);
371172150Sariff		break;
372172150Sariff	case SOUND_MIXER_DIGITAL3:	/* on-card SPDIF, in5 */
373172150Sariff			emumix_set_volume(sc->card, M_IN5_FRONT_L, left);
374172150Sariff			emumix_set_volume(sc->card, M_IN5_FRONT_R, right);
375172150Sariff		break;
376172150Sariff	case SOUND_MIXER_LINE3:		/* AUX2, in6 */
377172150Sariff			emumix_set_volume(sc->card, M_IN6_FRONT_L, left);
378172150Sariff			emumix_set_volume(sc->card, M_IN6_FRONT_R, right);
379172150Sariff		break;
380160383Snetchild	default:
381172150Sariff		if (sc->sm != NULL) {
382172150Sariff			/* XXX emumix_set_volume is not required here */
383172150Sariff			emumix_set_volume(sc->card, M_IN0_FRONT_L, 100);
384172150Sariff			emumix_set_volume(sc->card, M_IN0_FRONT_R, 100);
385172150Sariff			mix_set(sc->sm, dev, left, right);
386172150Sariff		} else
387172150Sariff			device_printf(sc->dev, "mixer error: unknown device %d\n", dev);
388160383Snetchild	}
389160383Snetchild	return  (0);
390160383Snetchild}
391160383Snetchild
392193640Sariffstatic u_int32_t
393172150Sariffemu_dspmixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
394160383Snetchild{
395172150Sariff	struct emu_pcm_info *sc;
396172150Sariff	int i;
397172150Sariff	u_int32_t recmask;
398172150Sariff	int	input[8];
399172150Sariff
400172150Sariff	sc = mix_getdevinfo(m);
401172150Sariff	recmask = 0;
402172150Sariff	for (i=0; i < 8; i++)
403172150Sariff		input[i]=0;
404172150Sariff
405172150Sariff	if (sc->sm != NULL)
406172150Sariff		if ((src & sc->ac97_recdevs) !=0)
407172150Sariff			if (mix_setrecsrc(sc->sm, src & sc->ac97_recdevs) == 0) {
408172150Sariff				recmask |= (src & sc->ac97_recdevs);
409172150Sariff				/* Recording from AC97 codec.
410172150Sariff				   Enable AC97 route to rec on DSP */
411172150Sariff				input[0] = 1;
412172150Sariff			}
413172150Sariff	if (sc->is_emu10k1) {
414172150Sariff		for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
415172150Sariff			if (dspmix_k1[i].recdev)
416172150Sariff				if ((src & (1 << i)) == ((uint32_t)1 << i)) {
417172150Sariff				recmask |= (1 << i);
418172150Sariff				/* enable device i */
419172150Sariff				input[dspmix_k1[i].input] = 1;
420172150Sariff				}
421172150Sariff		}
422172150Sariff	} else {
423172150Sariff		for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
424172150Sariff			if (dspmix_k2[i].recdev)
425172150Sariff				if ((src & (1 << i)) == ((uint32_t)1 << i)) {
426172150Sariff				recmask |= (1 << i);
427172150Sariff				/* enable device i */
428172150Sariff				input[dspmix_k2[i].input] = 1;
429172150Sariff				}
430172150Sariff		}
431172150Sariff	}
432172150Sariff	emumix_set_volume(sc->card, M_IN0_REC_L, input[0] == 1 ? 100 : 0);
433172150Sariff	emumix_set_volume(sc->card, M_IN0_REC_R, input[0] == 1 ? 100 : 0);
434172150Sariff
435172150Sariff	emumix_set_volume(sc->card, M_IN1_REC_L, input[1] == 1 ? 100 : 0);
436172150Sariff	emumix_set_volume(sc->card, M_IN1_REC_R, input[1] == 1 ? 100 : 0);
437172150Sariff
438172150Sariff	if (!sc->is_emu10k1) {
439172150Sariff		emumix_set_volume(sc->card, M_IN2_REC_L, input[2] == 1 ? 100 : 0);
440172150Sariff		emumix_set_volume(sc->card, M_IN2_REC_R, input[2] == 1 ? 100 : 0);
441172150Sariff	}
442172150Sariff
443172150Sariff	if (sc->is_emu10k1) {
444172150Sariff		emumix_set_volume(sc->card, M_IN3_REC_L, input[3] == 1 ? 100 : 0);
445172150Sariff		emumix_set_volume(sc->card, M_IN3_REC_R, input[3] == 1 ? 100 : 0);
446172150Sariff	}
447172150Sariff
448172150Sariff	emumix_set_volume(sc->card, M_IN4_REC_L, input[4] == 1 ? 100 : 0);
449172150Sariff	emumix_set_volume(sc->card, M_IN4_REC_R, input[4] == 1 ? 100 : 0);
450172150Sariff
451172150Sariff	emumix_set_volume(sc->card, M_IN5_REC_L, input[5] == 1 ? 100 : 0);
452172150Sariff	emumix_set_volume(sc->card, M_IN5_REC_R, input[5] == 1 ? 100 : 0);
453172150Sariff
454172150Sariff	emumix_set_volume(sc->card, M_IN6_REC_L, input[6] == 1 ? 100 : 0);
455172150Sariff	emumix_set_volume(sc->card, M_IN6_REC_R, input[6] == 1 ? 100 : 0);
456172150Sariff
457172150Sariff	/* XXX check for K1/k2 differences? */
458172150Sariff	if ((src & (1 << SOUND_MIXER_PCM)) == (1 << SOUND_MIXER_PCM)) {
459172150Sariff		emumix_set_volume(sc->card, M_FX0_REC_L, emumix_get_volume(sc->card, M_FX0_FRONT_L));
460172150Sariff		emumix_set_volume(sc->card, M_FX1_REC_R, emumix_get_volume(sc->card, M_FX1_FRONT_R));
461172150Sariff	} else {
462172150Sariff		emumix_set_volume(sc->card, M_FX0_REC_L, 0);
463172150Sariff		emumix_set_volume(sc->card, M_FX1_REC_R, 0);
464172150Sariff	}
465172150Sariff
466172150Sariff	return (recmask);
467160383Snetchild}
468160383Snetchild
469160383Snetchildstatic kobj_method_t emudspmixer_methods[] = {
470160383Snetchild	KOBJMETHOD(mixer_init,		emu_dspmixer_init),
471172150Sariff	KOBJMETHOD(mixer_uninit,	emu_dspmixer_uninit),
472160383Snetchild	KOBJMETHOD(mixer_set,		emu_dspmixer_set),
473160383Snetchild	KOBJMETHOD(mixer_setrecsrc,	emu_dspmixer_setrecsrc),
474193640Sariff	KOBJMETHOD_END
475160383Snetchild};
476160383SnetchildMIXER_DECLARE(emudspmixer);
477160383Snetchild
478172150Sariffstatic int
479172150Sariffemu_efxmixer_init(struct snd_mixer *m)
480172150Sariff{
481172150Sariff	mix_setdevs(m, SOUND_MASK_VOLUME);
482172150Sariff	mix_setrecdevs(m, SOUND_MASK_MONITOR);
483172150Sariff	return (0);
484172150Sariff}
485172150Sariff
486172150Sariffstatic int
487172150Sariffemu_efxmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
488172150Sariff{
489172150Sariff	if (left + right == 200) return (0);
490172150Sariff	return  (0);
491172150Sariff}
492172150Sariff
493193640Sariffstatic u_int32_t
494172150Sariffemu_efxmixer_setrecsrc(struct snd_mixer *m __unused, u_int32_t src __unused)
495172150Sariff{
496172150Sariff	return (SOUND_MASK_MONITOR);
497172150Sariff}
498172150Sariff
499172150Sariffstatic kobj_method_t emuefxmixer_methods[] = {
500172150Sariff	KOBJMETHOD(mixer_init,		emu_efxmixer_init),
501172150Sariff	KOBJMETHOD(mixer_set,		emu_efxmixer_set),
502172150Sariff	KOBJMETHOD(mixer_setrecsrc,	emu_efxmixer_setrecsrc),
503193640Sariff	KOBJMETHOD_END
504172150Sariff};
505172150SariffMIXER_DECLARE(emuefxmixer);
506172150Sariff
507160383Snetchild/*
508160383Snetchild * AC97 emulation code for Audigy and later cards.
509160383Snetchild * Some parts of AC97 codec are not used by hardware, but can be used
510160383Snetchild * to change some DSP controls via AC97 mixer interface. This includes:
511160383Snetchild * - master volume controls MASTER_FRONT_[R|L]
512160383Snetchild * - pcm volume controls FX[0|1]_FRONT_[R|L]
513160383Snetchild * - rec volume controls MASTER_REC_[R|L]
514160383Snetchild * We do it because we need to put it under user control....
515160383Snetchild * We also keep some parts of AC97 disabled to get better sound quality
516160383Snetchild */
517160383Snetchild
518160383Snetchild#define	AC97LEFT(x)	((x & 0x7F00)>>8)
519160383Snetchild#define	AC97RIGHT(x)	(x & 0x007F)
520160383Snetchild#define	AC97MUTE(x)	((x & 0x8000)>>15)
521160383Snetchild#define	BIT4_TO100(x)	(100-(x)*100/(0x0f))
522160383Snetchild#define	BIT6_TO100(x)	(100-(x)*100/(0x3f))
523160383Snetchild#define	BIT4_TO255(x)	(255-(x)*255/(0x0f))
524160383Snetchild#define	BIT6_TO255(x)	(255-(x)*255/(0x3f))
525160383Snetchild#define	V100_TOBIT6(x)	(0x3f*(100-x)/100)
526160383Snetchild#define	V100_TOBIT4(x)	(0x0f*(100-x)/100)
527172150Sariff#define	AC97ENCODE(x_muted, x_left, x_right)	(((x_muted & 1)<<15) | ((x_left & 0x3f)<<8) | (x_right & 0x3f))
528160383Snetchild
529160383Snetchildstatic int
530160383Snetchildemu_ac97_read_emulation(struct emu_pcm_info *sc, int regno)
531160383Snetchild{
532160383Snetchild	int use_ac97;
533160383Snetchild	int emulated;
534160383Snetchild	int tmp;
535160383Snetchild
536160383Snetchild	use_ac97 = 1;
537160383Snetchild	emulated = 0;
538160383Snetchild
539160383Snetchild	switch (regno) {
540160383Snetchild	case AC97_MIX_MASTER:
541160383Snetchild		emulated = sc->ac97_state[AC97_MIX_MASTER];
542160383Snetchild		use_ac97 = 0;
543160383Snetchild		break;
544160383Snetchild	case AC97_MIX_PCM:
545160383Snetchild		emulated = sc->ac97_state[AC97_MIX_PCM];
546160383Snetchild		use_ac97 = 0;
547160383Snetchild		break;
548160383Snetchild	case AC97_REG_RECSEL:
549160383Snetchild		emulated = 0x0505;
550160383Snetchild		use_ac97 = 0;
551160383Snetchild		break;
552160383Snetchild	case AC97_MIX_RGAIN:
553160383Snetchild		emulated = sc->ac97_state[AC97_MIX_RGAIN];
554160383Snetchild		use_ac97 = 0;
555160383Snetchild		break;
556160383Snetchild	}
557160383Snetchild
558230964Spfg	emu_wr(sc->card, EMU_AC97ADDR, regno, 1);
559230964Spfg	tmp = emu_rd(sc->card, EMU_AC97DATA, 2);
560160383Snetchild
561160383Snetchild	if (use_ac97)
562160383Snetchild		emulated = tmp;
563160383Snetchild
564160383Snetchild	return (emulated);
565160383Snetchild}
566160383Snetchild
567160383Snetchildstatic void
568160383Snetchildemu_ac97_write_emulation(struct emu_pcm_info *sc, int regno, uint32_t data)
569160383Snetchild{
570160383Snetchild	int write_ac97;
571160383Snetchild	int left, right;
572160383Snetchild	uint32_t emu_left, emu_right;
573160383Snetchild	int is_mute;
574160383Snetchild
575160383Snetchild	write_ac97 = 1;
576160383Snetchild
577160383Snetchild	left = AC97LEFT(data);
578160383Snetchild	emu_left = BIT6_TO100(left);	/* We show us as 6-bit AC97 mixer */
579160383Snetchild	right = AC97RIGHT(data);
580160383Snetchild	emu_right = BIT6_TO100(right);
581160383Snetchild	is_mute = AC97MUTE(data);
582160383Snetchild	if (is_mute)
583160383Snetchild		emu_left = emu_right = 0;
584160383Snetchild
585160383Snetchild	switch (regno) {
586160383Snetchild		/* TODO: reset emulator on AC97_RESET */
587160383Snetchild	case AC97_MIX_MASTER:
588160383Snetchild		emumix_set_volume(sc->card, M_MASTER_FRONT_L, emu_left);
589160383Snetchild		emumix_set_volume(sc->card, M_MASTER_FRONT_R, emu_right);
590160383Snetchild		sc->ac97_state[AC97_MIX_MASTER] = data & (0x8000 | 0x3f3f);
591160383Snetchild		data = 0x8000;	/* Mute AC97 main out */
592160383Snetchild		break;
593160383Snetchild	case AC97_MIX_PCM:	/* PCM OUT VOL */
594160383Snetchild		emumix_set_volume(sc->card, M_FX0_FRONT_L, emu_left);
595160383Snetchild		emumix_set_volume(sc->card, M_FX1_FRONT_R, emu_right);
596160383Snetchild		sc->ac97_state[AC97_MIX_PCM] = data & (0x8000 | 0x3f3f);
597160383Snetchild		data = 0x8000;	/* Mute AC97 PCM out */
598160383Snetchild		break;
599160383Snetchild	case AC97_REG_RECSEL:
600160383Snetchild		/*
601160383Snetchild		 * PCM recording source is set to "stereo mix" (labeled "vol"
602172150Sariff		 * in mixer). There is no 'playback' from AC97 codec -
603172150Sariff		 * if you want to hear anything from AC97 you have to _record_
604172150Sariff		 * it. Keep things simple and record "stereo mix".
605160383Snetchild		 */
606160383Snetchild		data = 0x0505;
607160383Snetchild		break;
608160383Snetchild	case AC97_MIX_RGAIN:	/* RECORD GAIN */
609160383Snetchild		emu_left = BIT4_TO100(left);	/* rgain is 4-bit */
610160383Snetchild		emu_right = BIT4_TO100(right);
611160383Snetchild		emumix_set_volume(sc->card, M_MASTER_REC_L, 100-emu_left);
612160383Snetchild		emumix_set_volume(sc->card, M_MASTER_REC_R, 100-emu_right);
613160383Snetchild		/*
614160383Snetchild		 * Record gain on AC97 should stay zero to get AC97 sound on
615160383Snetchild		 * AC97_[RL] connectors on EMU10K2 chip. AC97 on Audigy is not
616160383Snetchild		 * directly connected to any output, only to EMU10K2 chip Use
617160383Snetchild		 * this control to set AC97 mix volume inside EMU10K2 chip
618160383Snetchild		 */
619160383Snetchild		sc->ac97_state[AC97_MIX_RGAIN] = data & (0x8000 | 0x0f0f);
620160383Snetchild		data = 0x0000;
621160383Snetchild		break;
622160383Snetchild	}
623160383Snetchild	if (write_ac97) {
624230964Spfg		emu_wr(sc->card, EMU_AC97ADDR, regno, 1);
625230964Spfg		emu_wr(sc->card, EMU_AC97DATA, data, 2);
626160383Snetchild	}
627160383Snetchild}
628160383Snetchild
629160383Snetchildstatic int
630160383Snetchildemu_erdcd(kobj_t obj __unused, void *devinfo, int regno)
631160383Snetchild{
632160383Snetchild	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
633160383Snetchild
634160383Snetchild	return (emu_ac97_read_emulation(sc, regno));
635160383Snetchild}
636160383Snetchild
637160383Snetchildstatic int
638160383Snetchildemu_ewrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data)
639160383Snetchild{
640160383Snetchild	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
641160383Snetchild
642160383Snetchild	emu_ac97_write_emulation(sc, regno, data);
643160383Snetchild	return (0);
644160383Snetchild}
645160383Snetchild
646160383Snetchildstatic kobj_method_t emu_eac97_methods[] = {
647160383Snetchild	KOBJMETHOD(ac97_read, emu_erdcd),
648160383Snetchild	KOBJMETHOD(ac97_write, emu_ewrcd),
649193640Sariff	KOBJMETHOD_END
650160383Snetchild};
651160383SnetchildAC97_DECLARE(emu_eac97);
652160383Snetchild
653160383Snetchild/* real ac97 codec */
654160383Snetchildstatic int
655160383Snetchildemu_rdcd(kobj_t obj __unused, void *devinfo, int regno)
656160383Snetchild{
657160383Snetchild	int rd;
658160383Snetchild	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
659160383Snetchild
660160383Snetchild	KASSERT(sc->card != NULL, ("emu_rdcd: no soundcard"));
661230964Spfg	emu_wr(sc->card, EMU_AC97ADDR, regno, 1);
662230964Spfg	rd = emu_rd(sc->card, EMU_AC97DATA, 2);
663160383Snetchild	return (rd);
664160383Snetchild}
665160383Snetchild
666160383Snetchildstatic int
667160383Snetchildemu_wrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data)
668160383Snetchild{
669160383Snetchild	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
670160383Snetchild
671160383Snetchild	KASSERT(sc->card != NULL, ("emu_wrcd: no soundcard"));
672230964Spfg	emu_wr(sc->card, EMU_AC97ADDR, regno, 1);
673230964Spfg	emu_wr(sc->card, EMU_AC97DATA, data, 2);
674160383Snetchild	return (0);
675160383Snetchild}
676160383Snetchild
677160383Snetchildstatic kobj_method_t emu_ac97_methods[] = {
678160383Snetchild	KOBJMETHOD(ac97_read, emu_rdcd),
679160383Snetchild	KOBJMETHOD(ac97_write, emu_wrcd),
680193640Sariff	KOBJMETHOD_END
681160383Snetchild};
682160383SnetchildAC97_DECLARE(emu_ac97);
683160383Snetchild
684160383Snetchild
685160383Snetchildstatic int
686160383Snetchildemu_k1_recval(int speed)
687160383Snetchild{
688160383Snetchild	int val;
689160383Snetchild
690160383Snetchild	val = 0;
691160383Snetchild	while ((val < 7) && (speed < emu10k1_adcspeed[val]))
692160383Snetchild		val++;
693160383Snetchild	return (val);
694160383Snetchild}
695160383Snetchild
696160383Snetchildstatic int
697160383Snetchildemu_k2_recval(int speed)
698160383Snetchild{
699160383Snetchild	int val;
700160383Snetchild
701160383Snetchild	val = 0;
702160383Snetchild	while ((val < 8) && (speed < emu10k2_adcspeed[val]))
703160383Snetchild		val++;
704160383Snetchild	return (val);
705160383Snetchild}
706160383Snetchild
707160383Snetchildstatic void *
708160383Snetchildemupchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
709160383Snetchild{
710160383Snetchild	struct emu_pcm_info *sc = devinfo;
711160383Snetchild	struct emu_pcm_pchinfo *ch;
712160383Snetchild	void *r;
713160383Snetchild
714160383Snetchild	KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction"));
715160383Snetchild	KASSERT(sc->card != NULL, ("empchan_init: no soundcard"));
716160383Snetchild
717160383Snetchild
718160383Snetchild	if (sc->pnum >= MAX_CHANNELS)
719160383Snetchild		return (NULL);
720160383Snetchild	ch = &(sc->pch[sc->pnum++]);
721160383Snetchild	ch->buffer = b;
722160383Snetchild	ch->pcm = sc;
723160383Snetchild	ch->channel = c;
724165833Snetchild	ch->blksz = sc->bufsz;
725193640Sariff	ch->fmt = SND_FORMAT(AFMT_U8, 1, 0);
726160383Snetchild	ch->spd = 8000;
727160383Snetchild	ch->master = emu_valloc(sc->card);
728160383Snetchild	/*
729160383Snetchild	 * XXX we have to allocate slave even for mono channel until we
730160383Snetchild	 * fix emu_vfree to handle this case.
731160383Snetchild	 */
732160383Snetchild	ch->slave = emu_valloc(sc->card);
733160383Snetchild	ch->timer = emu_timer_create(sc->card);
734161054Snetchild	r = (emu_vinit(sc->card, ch->master, ch->slave, EMU_PLAY_BUFSZ, ch->buffer)) ? NULL : ch;
735160383Snetchild	return (r);
736160383Snetchild}
737160383Snetchild
738160383Snetchildstatic int
739160383Snetchildemupchan_free(kobj_t obj __unused, void *c_devinfo)
740160383Snetchild{
741160383Snetchild	struct emu_pcm_pchinfo *ch = c_devinfo;
742160383Snetchild	struct emu_pcm_info *sc = ch->pcm;
743160383Snetchild
744160383Snetchild	emu_timer_clear(sc->card, ch->timer);
745160383Snetchild	if (ch->slave != NULL)
746160383Snetchild		emu_vfree(sc->card, ch->slave);
747160383Snetchild	emu_vfree(sc->card, ch->master);
748160383Snetchild	return (0);
749160383Snetchild}
750160383Snetchild
751160383Snetchildstatic int
752160383Snetchildemupchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format)
753160383Snetchild{
754160383Snetchild	struct emu_pcm_pchinfo *ch = c_devinfo;
755160383Snetchild
756160383Snetchild	ch->fmt = format;
757160383Snetchild	return (0);
758160383Snetchild}
759160383Snetchild
760193640Sariffstatic uint32_t
761160383Snetchildemupchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
762160383Snetchild{
763160383Snetchild	struct emu_pcm_pchinfo *ch = c_devinfo;
764160383Snetchild
765160383Snetchild	ch->spd = speed;
766160383Snetchild	return (ch->spd);
767160383Snetchild}
768160383Snetchild
769193640Sariffstatic uint32_t
770160383Snetchildemupchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
771160383Snetchild{
772160383Snetchild	struct emu_pcm_pchinfo *ch = c_devinfo;
773160383Snetchild	struct emu_pcm_info *sc = ch->pcm;
774160383Snetchild
775160383Snetchild	if (blocksize > ch->pcm->bufsz)
776160383Snetchild		blocksize = ch->pcm->bufsz;
777160383Snetchild	snd_mtxlock(sc->lock);
778160383Snetchild	ch->blksz = blocksize;
779193640Sariff	emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer));
780160383Snetchild	snd_mtxunlock(sc->lock);
781172150Sariff	return (ch->blksz);
782160383Snetchild}
783160383Snetchild
784160383Snetchildstatic int
785160383Snetchildemupchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
786160383Snetchild{
787160383Snetchild	struct emu_pcm_pchinfo *ch = c_devinfo;
788160383Snetchild	struct emu_pcm_info *sc = ch->pcm;
789160383Snetchild
790170521Sariff	if (!PCMTRIG_COMMON(go))
791160383Snetchild		return (0);
792170521Sariff
793160383Snetchild	snd_mtxlock(sc->lock); /* XXX can we trigger on parallel threads ? */
794160383Snetchild	if (go == PCMTRIG_START) {
795160383Snetchild		emu_vsetup(ch->master, ch->fmt, ch->spd);
796193640Sariff		if (AFMT_CHANNEL(ch->fmt) > 1)
797161056Snetchild			emu_vroute(sc->card, &(sc->rt), ch->master);
798161056Snetchild		else
799161056Snetchild			emu_vroute(sc->card, &(sc->rt_mono), ch->master);
800160383Snetchild		emu_vwrite(sc->card, ch->master);
801193640Sariff		emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer));
802160383Snetchild		emu_timer_enable(sc->card, ch->timer, 1);
803160383Snetchild	}
804160383Snetchild	/* PCM interrupt handler will handle PCMTRIG_STOP event */
805160383Snetchild	ch->run = (go == PCMTRIG_START) ? 1 : 0;
806160383Snetchild	emu_vtrigger(sc->card, ch->master, ch->run);
807160383Snetchild	snd_mtxunlock(sc->lock);
808160383Snetchild	return (0);
809160383Snetchild}
810160383Snetchild
811193640Sariffstatic uint32_t
812160383Snetchildemupchan_getptr(kobj_t obj __unused, void *c_devinfo)
813160383Snetchild{
814160383Snetchild	struct emu_pcm_pchinfo *ch = c_devinfo;
815160383Snetchild	struct emu_pcm_info *sc = ch->pcm;
816160383Snetchild	int r;
817160383Snetchild
818160383Snetchild	r = emu_vpos(sc->card, ch->master);
819160383Snetchild
820160383Snetchild	return (r);
821160383Snetchild}
822160383Snetchild
823160383Snetchildstatic struct pcmchan_caps *
824160383Snetchildemupchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused)
825160383Snetchild{
826160383Snetchild	struct emu_pcm_pchinfo *ch = c_devinfo;
827160383Snetchild	struct emu_pcm_info *sc = ch->pcm;
828160383Snetchild
829160383Snetchild	switch (sc->route) {
830160383Snetchild	case RT_FRONT:
831160383Snetchild		/* FALLTHROUGH */
832160383Snetchild	case RT_REAR:
833160383Snetchild		/* FALLTHROUGH */
834160383Snetchild	case RT_SIDE:
835160383Snetchild		return (&emu_playcaps);
836160383Snetchild		break;
837160383Snetchild	case RT_CENTER:
838160383Snetchild		/* FALLTHROUGH */
839160383Snetchild	case RT_SUB:
840160383Snetchild		return (&emu_playcaps_mono);
841160383Snetchild		break;
842160383Snetchild	}
843160383Snetchild	return (NULL);
844160383Snetchild}
845160383Snetchild
846160383Snetchildstatic kobj_method_t emupchan_methods[] = {
847160383Snetchild	KOBJMETHOD(channel_init, emupchan_init),
848160383Snetchild	KOBJMETHOD(channel_free, emupchan_free),
849160383Snetchild	KOBJMETHOD(channel_setformat, emupchan_setformat),
850160383Snetchild	KOBJMETHOD(channel_setspeed, emupchan_setspeed),
851160383Snetchild	KOBJMETHOD(channel_setblocksize, emupchan_setblocksize),
852160383Snetchild	KOBJMETHOD(channel_trigger, emupchan_trigger),
853160383Snetchild	KOBJMETHOD(channel_getptr, emupchan_getptr),
854160383Snetchild	KOBJMETHOD(channel_getcaps, emupchan_getcaps),
855193640Sariff	KOBJMETHOD_END
856160383Snetchild};
857160383SnetchildCHANNEL_DECLARE(emupchan);
858160383Snetchild
859160383Snetchildstatic void *
860160383Snetchildemurchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
861160383Snetchild{
862160383Snetchild	struct emu_pcm_info *sc = devinfo;
863160383Snetchild	struct emu_pcm_rchinfo *ch;
864160383Snetchild
865160383Snetchild	KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction"));
866165833Snetchild	ch = &sc->rch_adc;
867160383Snetchild	ch->buffer = b;
868160383Snetchild	ch->pcm = sc;
869160383Snetchild	ch->channel = c;
870165833Snetchild	ch->blksz = sc->bufsz / 2; /* We rise interrupt for half-full buffer */
871193640Sariff	ch->fmt = SND_FORMAT(AFMT_U8, 1, 0);
872165833Snetchild	ch->spd = 8000;
873230964Spfg	ch->idxreg = sc->is_emu10k1 ? EMU_ADCIDX : EMU_A_ADCIDX;
874230964Spfg	ch->basereg = EMU_ADCBA;
875230964Spfg	ch->sizereg = EMU_ADCBS;
876230964Spfg	ch->setupreg = EMU_ADCCR;
877230964Spfg	ch->irqmask = EMU_INTE_ADCBUFENABLE;
878230964Spfg	ch->iprmask = EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL;
879160383Snetchild
880168847Sariff	if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0)
881160383Snetchild		return (NULL);
882160383Snetchild	else {
883172150Sariff		ch->timer = emu_timer_create(sc->card);
884160383Snetchild		emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer));
885160383Snetchild		emu_wrptr(sc->card, 0, ch->sizereg, 0);	/* off */
886160383Snetchild		return (ch);
887160383Snetchild	}
888160383Snetchild}
889160383Snetchild
890160383Snetchildstatic int
891172150Sariffemurchan_free(kobj_t obj __unused, void *c_devinfo)
892172150Sariff{
893172150Sariff	struct emu_pcm_rchinfo *ch = c_devinfo;
894172150Sariff	struct emu_pcm_info *sc = ch->pcm;
895172150Sariff
896172150Sariff	emu_timer_clear(sc->card, ch->timer);
897172150Sariff	return (0);
898172150Sariff}
899172150Sariff
900172150Sariffstatic int
901160383Snetchildemurchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format)
902160383Snetchild{
903160383Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
904160383Snetchild
905160383Snetchild	ch->fmt = format;
906160383Snetchild	return (0);
907160383Snetchild}
908160383Snetchild
909193640Sariffstatic uint32_t
910160383Snetchildemurchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
911160383Snetchild{
912160383Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
913160383Snetchild
914160383Snetchild	if (ch->pcm->is_emu10k1) {
915160383Snetchild		speed = emu10k1_adcspeed[emu_k1_recval(speed)];
916160383Snetchild	} else {
917160383Snetchild		speed = emu10k2_adcspeed[emu_k2_recval(speed)];
918160383Snetchild	}
919160383Snetchild	ch->spd = speed;
920160383Snetchild	return (ch->spd);
921160383Snetchild}
922160383Snetchild
923193640Sariffstatic uint32_t
924160383Snetchildemurchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
925160383Snetchild{
926160383Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
927172150Sariff	struct emu_pcm_info *sc = ch->pcm;
928160383Snetchild
929160383Snetchild	ch->blksz = blocksize;
930172150Sariff	/*
931172150Sariff	 * If blocksize is less than half of buffer size we will not get
932172150Sariff	 * BUFHALFFULL interrupt in time and channel will need to generate
933172150Sariff	 * (and use) timer interrupts. Otherwise channel will be marked dead.
934172150Sariff	 */
935172150Sariff	if (ch->blksz < (ch->pcm->bufsz / 2)) {
936193640Sariff		emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer));
937172150Sariff		emu_timer_enable(sc->card, ch->timer, 1);
938172150Sariff	} else {
939172150Sariff		emu_timer_enable(sc->card, ch->timer, 0);
940172150Sariff	}
941165833Snetchild	return (ch->blksz);
942160383Snetchild}
943160383Snetchild
944160383Snetchildstatic int
945160383Snetchildemurchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
946160383Snetchild{
947160383Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
948160383Snetchild	struct emu_pcm_info *sc = ch->pcm;
949160383Snetchild	uint32_t val, sz;
950160383Snetchild
951170521Sariff	if (!PCMTRIG_COMMON(go))
952170521Sariff		return (0);
953170521Sariff
954160383Snetchild	switch (sc->bufsz) {
955160383Snetchild	case 4096:
956230964Spfg		sz = EMU_RECBS_BUFSIZE_4096;
957160383Snetchild		break;
958160383Snetchild	case 8192:
959230964Spfg		sz = EMU_RECBS_BUFSIZE_8192;
960160383Snetchild		break;
961160383Snetchild	case 16384:
962230964Spfg		sz = EMU_RECBS_BUFSIZE_16384;
963160383Snetchild		break;
964160383Snetchild	case 32768:
965230964Spfg		sz = EMU_RECBS_BUFSIZE_32768;
966160383Snetchild		break;
967160383Snetchild	case 65536:
968230964Spfg		sz = EMU_RECBS_BUFSIZE_65536;
969160383Snetchild		break;
970160383Snetchild	default:
971230964Spfg		sz = EMU_RECBS_BUFSIZE_4096;
972160383Snetchild	}
973160383Snetchild
974160383Snetchild	snd_mtxlock(sc->lock);
975160383Snetchild	switch (go) {
976160383Snetchild	case PCMTRIG_START:
977160383Snetchild		ch->run = 1;
978160383Snetchild		emu_wrptr(sc->card, 0, ch->sizereg, sz);
979230964Spfg		val = sc->is_emu10k1 ? EMU_ADCCR_LCHANENABLE : EMU_A_ADCCR_LCHANENABLE;
980193640Sariff		if (AFMT_CHANNEL(ch->fmt) > 1)
981230964Spfg			val |= sc->is_emu10k1 ? EMU_ADCCR_RCHANENABLE : EMU_A_ADCCR_RCHANENABLE;
982160383Snetchild		val |= sc->is_emu10k1 ? emu_k1_recval(ch->spd) : emu_k2_recval(ch->spd);
983160383Snetchild		emu_wrptr(sc->card, 0, ch->setupreg, 0);
984160383Snetchild		emu_wrptr(sc->card, 0, ch->setupreg, val);
985160383Snetchild		ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc);
986160383Snetchild		break;
987160383Snetchild	case PCMTRIG_STOP:
988160383Snetchild		/* FALLTHROUGH */
989160383Snetchild	case PCMTRIG_ABORT:
990160383Snetchild		ch->run = 0;
991160383Snetchild		emu_wrptr(sc->card, 0, ch->sizereg, 0);
992160383Snetchild		if (ch->setupreg)
993160383Snetchild			emu_wrptr(sc->card, 0, ch->setupreg, 0);
994160383Snetchild		(void)emu_intr_unregister(sc->card, ch->ihandle);
995160383Snetchild		break;
996160383Snetchild	case PCMTRIG_EMLDMAWR:
997160383Snetchild		/* FALLTHROUGH */
998160383Snetchild	case PCMTRIG_EMLDMARD:
999160383Snetchild		/* FALLTHROUGH */
1000160383Snetchild	default:
1001160383Snetchild		break;
1002160383Snetchild	}
1003160383Snetchild	snd_mtxunlock(sc->lock);
1004160383Snetchild
1005160383Snetchild	return (0);
1006160383Snetchild}
1007160383Snetchild
1008193640Sariffstatic uint32_t
1009160383Snetchildemurchan_getptr(kobj_t obj __unused, void *c_devinfo)
1010160383Snetchild{
1011160383Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
1012160383Snetchild	struct emu_pcm_info *sc = ch->pcm;
1013160383Snetchild	int r;
1014160383Snetchild
1015160383Snetchild	r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff;
1016160383Snetchild
1017160383Snetchild	return (r);
1018160383Snetchild}
1019160383Snetchild
1020160383Snetchildstatic struct pcmchan_caps *
1021160383Snetchildemurchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused)
1022160383Snetchild{
1023165833Snetchild	return (&emu_reccaps_adc);
1024160383Snetchild}
1025160383Snetchild
1026160383Snetchildstatic kobj_method_t emurchan_methods[] = {
1027160383Snetchild	KOBJMETHOD(channel_init, emurchan_init),
1028172150Sariff	KOBJMETHOD(channel_free, emurchan_free),
1029160383Snetchild	KOBJMETHOD(channel_setformat, emurchan_setformat),
1030160383Snetchild	KOBJMETHOD(channel_setspeed, emurchan_setspeed),
1031160383Snetchild	KOBJMETHOD(channel_setblocksize, emurchan_setblocksize),
1032160383Snetchild	KOBJMETHOD(channel_trigger, emurchan_trigger),
1033160383Snetchild	KOBJMETHOD(channel_getptr, emurchan_getptr),
1034160383Snetchild	KOBJMETHOD(channel_getcaps, emurchan_getcaps),
1035193640Sariff	KOBJMETHOD_END
1036160383Snetchild};
1037160383SnetchildCHANNEL_DECLARE(emurchan);
1038160383Snetchild
1039165833Snetchildstatic void *
1040165833Snetchildemufxrchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
1041165833Snetchild{
1042165833Snetchild	struct emu_pcm_info *sc = devinfo;
1043165833Snetchild	struct emu_pcm_rchinfo *ch;
1044160383Snetchild
1045165833Snetchild	KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction"));
1046165833Snetchild
1047165833Snetchild	if (sc == NULL) return (NULL);
1048165833Snetchild
1049165833Snetchild	ch = &(sc->rch_efx);
1050193640Sariff	ch->fmt = SND_FORMAT(AFMT_S16_LE, 1, 0);
1051165833Snetchild	ch->spd = sc->is_emu10k1 ? 48000*32 : 48000 * 64;
1052230964Spfg	ch->idxreg = EMU_FXIDX;
1053230964Spfg	ch->basereg = EMU_FXBA;
1054230964Spfg	ch->sizereg = EMU_FXBS;
1055230964Spfg	ch->irqmask = EMU_INTE_EFXBUFENABLE;
1056230964Spfg	ch->iprmask = EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL;
1057165833Snetchild	ch->buffer = b;
1058165833Snetchild	ch->pcm = sc;
1059165833Snetchild	ch->channel = c;
1060172150Sariff	ch->blksz = sc->bufsz / 2;
1061165833Snetchild
1062168847Sariff	if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0)
1063165833Snetchild		return (NULL);
1064165833Snetchild	else {
1065165833Snetchild		emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer));
1066165833Snetchild		emu_wrptr(sc->card, 0, ch->sizereg, 0);	/* off */
1067165833Snetchild		return (ch);
1068165833Snetchild	}
1069165833Snetchild}
1070165833Snetchild
1071165833Snetchildstatic int
1072165833Snetchildemufxrchan_setformat(kobj_t obj __unused, void *c_devinfo __unused, uint32_t format)
1073165833Snetchild{
1074193640Sariff	if (format == SND_FORMAT(AFMT_S16_LE, 1, 0)) return (0);
1075172150Sariff	return (EINVAL);
1076165833Snetchild}
1077165833Snetchild
1078193640Sariffstatic uint32_t
1079165833Snetchildemufxrchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
1080165833Snetchild{
1081165833Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
1082165833Snetchild
1083165833Snetchild	/* FIXED RATE CHANNEL */
1084165833Snetchild	return (ch->spd);
1085165833Snetchild}
1086165833Snetchild
1087193640Sariffstatic uint32_t
1088165833Snetchildemufxrchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
1089165833Snetchild{
1090165833Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
1091165833Snetchild
1092165833Snetchild	ch->blksz = blocksize;
1093172150Sariff	/*
1094172150Sariff	 * XXX If blocksize is less than half of buffer size we will not get
1095172150Sariff	 * interrupt in time and channel will die due to interrupt timeout.
1096172150Sariff	 * This	should not happen with FX rchan, because it will fill buffer
1097172150Sariff	 * very	fast (64K buffer is 0.021seconds on Audigy).
1098172150Sariff	 */
1099172150Sariff	if (ch->blksz < (ch->pcm->bufsz / 2))
1100165833Snetchild		ch->blksz = ch->pcm->bufsz / 2;
1101165833Snetchild	return (ch->blksz);
1102165833Snetchild}
1103165833Snetchild
1104165833Snetchildstatic int
1105165833Snetchildemufxrchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
1106165833Snetchild{
1107165833Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
1108165833Snetchild	struct emu_pcm_info *sc = ch->pcm;
1109165833Snetchild	uint32_t sz;
1110165833Snetchild
1111170521Sariff	if (!PCMTRIG_COMMON(go))
1112170521Sariff		return (0);
1113170521Sariff
1114165833Snetchild	switch (sc->bufsz) {
1115165833Snetchild	case 4096:
1116230964Spfg		sz = EMU_RECBS_BUFSIZE_4096;
1117165833Snetchild		break;
1118165833Snetchild	case 8192:
1119230964Spfg		sz = EMU_RECBS_BUFSIZE_8192;
1120165833Snetchild		break;
1121165833Snetchild	case 16384:
1122230964Spfg		sz = EMU_RECBS_BUFSIZE_16384;
1123165833Snetchild		break;
1124165833Snetchild	case 32768:
1125230964Spfg		sz = EMU_RECBS_BUFSIZE_32768;
1126165833Snetchild		break;
1127165833Snetchild	case 65536:
1128230964Spfg		sz = EMU_RECBS_BUFSIZE_65536;
1129165833Snetchild		break;
1130165833Snetchild	default:
1131230964Spfg		sz = EMU_RECBS_BUFSIZE_4096;
1132165833Snetchild	}
1133165833Snetchild
1134165833Snetchild	snd_mtxlock(sc->lock);
1135165833Snetchild	switch (go) {
1136165833Snetchild	case PCMTRIG_START:
1137165833Snetchild		ch->run = 1;
1138165833Snetchild		emu_wrptr(sc->card, 0, ch->sizereg, sz);
1139165833Snetchild		ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc);
1140172150Sariff		/*
1141172150Sariff		 * SB Live! is limited to 32 mono channels. Audigy
1142172150Sariff		 * has 64 mono channels. Channels are enabled
1143230964Spfg		 * by setting a bit in EMU_A_FXWC[1|2] registers.
1144165833Snetchild		 */
1145165833Snetchild		/* XXX there is no way to demultiplex this streams for now */
1146172150Sariff		if (sc->is_emu10k1) {
1147230964Spfg			emu_wrptr(sc->card, 0, EMU_FXWC, 0xffffffff);
1148165833Snetchild		} else {
1149230964Spfg			emu_wrptr(sc->card, 0, EMU_A_FXWC1, 0xffffffff);
1150230964Spfg			emu_wrptr(sc->card, 0, EMU_A_FXWC2, 0xffffffff);
1151165833Snetchild		}
1152165833Snetchild		break;
1153165833Snetchild	case PCMTRIG_STOP:
1154165833Snetchild		/* FALLTHROUGH */
1155165833Snetchild	case PCMTRIG_ABORT:
1156165833Snetchild		ch->run = 0;
1157172150Sariff		if (sc->is_emu10k1) {
1158230964Spfg			emu_wrptr(sc->card, 0, EMU_FXWC, 0x0);
1159165833Snetchild		} else {
1160230964Spfg			emu_wrptr(sc->card, 0, EMU_A_FXWC1, 0x0);
1161230964Spfg			emu_wrptr(sc->card, 0, EMU_A_FXWC2, 0x0);
1162165833Snetchild		}
1163165833Snetchild		emu_wrptr(sc->card, 0, ch->sizereg, 0);
1164165833Snetchild		(void)emu_intr_unregister(sc->card, ch->ihandle);
1165165833Snetchild		break;
1166165833Snetchild	case PCMTRIG_EMLDMAWR:
1167165833Snetchild		/* FALLTHROUGH */
1168165833Snetchild	case PCMTRIG_EMLDMARD:
1169165833Snetchild		/* FALLTHROUGH */
1170165833Snetchild	default:
1171165833Snetchild		break;
1172165833Snetchild	}
1173165833Snetchild	snd_mtxunlock(sc->lock);
1174165833Snetchild
1175165833Snetchild	return (0);
1176165833Snetchild}
1177165833Snetchild
1178193640Sariffstatic uint32_t
1179165833Snetchildemufxrchan_getptr(kobj_t obj __unused, void *c_devinfo)
1180165833Snetchild{
1181165833Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
1182165833Snetchild	struct emu_pcm_info *sc = ch->pcm;
1183165833Snetchild	int r;
1184165833Snetchild
1185165833Snetchild	r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff;
1186165833Snetchild
1187165833Snetchild	return (r);
1188165833Snetchild}
1189165833Snetchild
1190165833Snetchildstatic struct pcmchan_caps *
1191165833Snetchildemufxrchan_getcaps(kobj_t obj __unused, void *c_devinfo)
1192165833Snetchild{
1193165833Snetchild	struct emu_pcm_rchinfo *ch = c_devinfo;
1194165833Snetchild	struct emu_pcm_info *sc = ch->pcm;
1195165833Snetchild
1196172150Sariff	if (sc->is_emu10k1)
1197165833Snetchild		return (&emu_reccaps_efx_live);
1198165833Snetchild	return (&emu_reccaps_efx_audigy);
1199165833Snetchild
1200165833Snetchild}
1201165833Snetchild
1202172150Sariffstatic int
1203172150Sariffemufxrchan_getrates(kobj_t obj __unused, void *c_devinfo, int **rates)
1204172150Sariff{
1205172150Sariff	struct emu_pcm_rchinfo *ch = c_devinfo;
1206172150Sariff	struct emu_pcm_info *sc = ch->pcm;
1207172150Sariff
1208172150Sariff	if (sc->is_emu10k1)
1209172150Sariff		*rates = emu_rates_live;
1210172150Sariff	else
1211172150Sariff		*rates = emu_rates_audigy;
1212172150Sariff
1213172150Sariff	return 1;
1214172150Sariff}
1215172150Sariff
1216165833Snetchildstatic kobj_method_t emufxrchan_methods[] = {
1217165833Snetchild	KOBJMETHOD(channel_init, emufxrchan_init),
1218165833Snetchild	KOBJMETHOD(channel_setformat, emufxrchan_setformat),
1219165833Snetchild	KOBJMETHOD(channel_setspeed, emufxrchan_setspeed),
1220165833Snetchild	KOBJMETHOD(channel_setblocksize, emufxrchan_setblocksize),
1221165833Snetchild	KOBJMETHOD(channel_trigger, emufxrchan_trigger),
1222165833Snetchild	KOBJMETHOD(channel_getptr, emufxrchan_getptr),
1223165833Snetchild	KOBJMETHOD(channel_getcaps, emufxrchan_getcaps),
1224172150Sariff	KOBJMETHOD(channel_getrates, emufxrchan_getrates),
1225193640Sariff	KOBJMETHOD_END
1226165833Snetchild};
1227165833SnetchildCHANNEL_DECLARE(emufxrchan);
1228165833Snetchild
1229165833Snetchild
1230160383Snetchildstatic uint32_t
1231160383Snetchildemu_pcm_intr(void *pcm, uint32_t stat)
1232160383Snetchild{
1233160383Snetchild	struct emu_pcm_info *sc = (struct emu_pcm_info *)pcm;
1234160383Snetchild	uint32_t ack;
1235160383Snetchild	int i;
1236160383Snetchild
1237160383Snetchild	ack = 0;
1238160383Snetchild
1239172150Sariff	snd_mtxlock(sc->lock);
1240172150Sariff
1241230964Spfg	if (stat & EMU_IPR_INTERVALTIMER) {
1242230964Spfg		ack |= EMU_IPR_INTERVALTIMER;
1243160383Snetchild		for (i = 0; i < MAX_CHANNELS; i++)
1244160383Snetchild			if (sc->pch[i].channel) {
1245172150Sariff				if (sc->pch[i].run == 1) {
1246172150Sariff					snd_mtxunlock(sc->lock);
1247160383Snetchild					chn_intr(sc->pch[i].channel);
1248172150Sariff					snd_mtxlock(sc->lock);
1249172150Sariff				} else
1250160383Snetchild					emu_timer_enable(sc->card, sc->pch[i].timer, 0);
1251160383Snetchild			}
1252172150Sariff		/* ADC may install timer to get low-latency interrupts */
1253172150Sariff		if ((sc->rch_adc.channel) && (sc->rch_adc.run)) {
1254172150Sariff			snd_mtxunlock(sc->lock);
1255172150Sariff			chn_intr(sc->rch_adc.channel);
1256172150Sariff			snd_mtxlock(sc->lock);
1257172150Sariff		}
1258172150Sariff		/*
1259172150Sariff		 * EFX does not use timer, because it will fill
1260172150Sariff		 * buffer at least 32x times faster than ADC.
1261172150Sariff		 */
1262160383Snetchild	}
1263160383Snetchild
1264160383Snetchild
1265230964Spfg	if (stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL)) {
1266230964Spfg		ack |= stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL);
1267172150Sariff		if (sc->rch_adc.channel) {
1268172150Sariff			snd_mtxunlock(sc->lock);
1269165833Snetchild			chn_intr(sc->rch_adc.channel);
1270172150Sariff			snd_mtxlock(sc->lock);
1271172150Sariff		}
1272160383Snetchild	}
1273165833Snetchild
1274230964Spfg	if (stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL)) {
1275230964Spfg		ack |= stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL);
1276172150Sariff		if (sc->rch_efx.channel) {
1277172150Sariff			snd_mtxunlock(sc->lock);
1278165833Snetchild			chn_intr(sc->rch_efx.channel);
1279172150Sariff			snd_mtxlock(sc->lock);
1280172150Sariff		}
1281165833Snetchild	}
1282172150Sariff	snd_mtxunlock(sc->lock);
1283172150Sariff
1284160383Snetchild	return (ack);
1285160383Snetchild}
1286160383Snetchild
1287160383Snetchildstatic int
1288160383Snetchildemu_pcm_init(struct emu_pcm_info *sc)
1289160383Snetchild{
1290161054Snetchild	sc->bufsz = pcm_getbuffersize(sc->dev, EMUPAGESIZE, EMU_REC_BUFSZ, EMU_MAX_BUFSZ);
1291160383Snetchild	return (0);
1292160383Snetchild}
1293160383Snetchild
1294160383Snetchildstatic int
1295160383Snetchildemu_pcm_uninit(struct emu_pcm_info *sc __unused)
1296160383Snetchild{
1297160383Snetchild	return (0);
1298160383Snetchild}
1299160383Snetchild
1300160383Snetchildstatic int
1301160383Snetchildemu_pcm_probe(device_t dev)
1302160383Snetchild{
1303160383Snetchild	uintptr_t func, route, r;
1304160383Snetchild	const char *rt;
1305160383Snetchild	char buffer[255];
1306160383Snetchild
1307160383Snetchild	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_FUNC, &func);
1308160383Snetchild
1309160383Snetchild	if (func != SCF_PCM)
1310160383Snetchild		return (ENXIO);
1311160383Snetchild
1312160383Snetchild	rt = "UNKNOWN";
1313160383Snetchild	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route);
1314160383Snetchild	switch (route) {
1315160383Snetchild	case RT_FRONT:
1316165833Snetchild		rt = "front";
1317160383Snetchild		break;
1318160383Snetchild	case RT_REAR:
1319165833Snetchild		rt = "rear";
1320160383Snetchild		break;
1321160383Snetchild	case RT_CENTER:
1322165833Snetchild		rt = "center";
1323160383Snetchild		break;
1324160383Snetchild	case RT_SUB:
1325165833Snetchild		rt = "subwoofer";
1326160383Snetchild		break;
1327160383Snetchild	case RT_SIDE:
1328165833Snetchild		rt = "side";
1329160383Snetchild		break;
1330165833Snetchild	case RT_MCHRECORD:
1331165833Snetchild		rt = "multichannel recording";
1332165833Snetchild		break;
1333160383Snetchild	}
1334160383Snetchild
1335165833Snetchild	snprintf(buffer, 255, "EMU10Kx DSP %s PCM interface", rt);
1336160383Snetchild	device_set_desc_copy(dev, buffer);
1337160383Snetchild	return (0);
1338160383Snetchild}
1339160383Snetchild
1340160383Snetchildstatic int
1341160383Snetchildemu_pcm_attach(device_t dev)
1342160383Snetchild{
1343160383Snetchild	struct emu_pcm_info *sc;
1344160383Snetchild	unsigned int i;
1345160383Snetchild	char status[SND_STATUSLEN];
1346160383Snetchild	uint32_t inte, ipr;
1347172150Sariff	uintptr_t route, r, ivar;
1348160383Snetchild
1349170873Sariff	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
1350160383Snetchild	sc->card = (struct emu_sc_info *)(device_get_softc(device_get_parent(dev)));
1351160383Snetchild	if (sc->card == NULL) {
1352160383Snetchild		device_printf(dev, "cannot get bridge conf\n");
1353170873Sariff		free(sc, M_DEVBUF);
1354160383Snetchild		return (ENXIO);
1355160383Snetchild	}
1356160383Snetchild
1357172150Sariff	sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_emu10kx pcm softc");
1358160383Snetchild	sc->dev = dev;
1359160383Snetchild
1360172150Sariff	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &ivar);
1361172150Sariff	sc->is_emu10k1 = ivar ? 1 : 0;
1362160383Snetchild
1363172150Sariff	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_MCH_DISABLED, &ivar);
1364172150Sariff	sc->mch_disabled = ivar ? 1 : 0;
1365172150Sariff
1366160383Snetchild	sc->codec = NULL;
1367160383Snetchild
1368160383Snetchild	for (i = 0; i < 8; i++) {
1369160383Snetchild		sc->rt.routing_left[i] = i;
1370160383Snetchild		sc->rt.amounts_left[i] = 0x00;
1371160383Snetchild		sc->rt.routing_right[i] = i;
1372160383Snetchild		sc->rt.amounts_right[i] = 0x00;
1373160383Snetchild	}
1374160383Snetchild
1375161056Snetchild	for (i = 0; i < 8; i++) {
1376161056Snetchild		sc->rt_mono.routing_left[i] = i;
1377161056Snetchild		sc->rt_mono.amounts_left[i] = 0x00;
1378161056Snetchild		sc->rt_mono.routing_right[i] = i;
1379161056Snetchild		sc->rt_mono.amounts_right[i] = 0x00;
1380161056Snetchild	}
1381161056Snetchild
1382172150Sariff	sc->emu10k1_volcache[0][0] = 75;
1383172150Sariff	sc->emu10k1_volcache[1][0] = 75;
1384172150Sariff	sc->emu10k1_volcache[0][1] = 75;
1385172150Sariff	sc->emu10k1_volcache[1][1] = 75;
1386160383Snetchild	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route);
1387160383Snetchild	sc->route = route;
1388160383Snetchild	switch (route) {
1389160383Snetchild	case RT_FRONT:
1390160383Snetchild		sc->rt.amounts_left[0] = 0xff;
1391160383Snetchild		sc->rt.amounts_right[1] = 0xff;
1392161056Snetchild		sc->rt_mono.amounts_left[0] = 0xff;
1393161056Snetchild		sc->rt_mono.amounts_left[1] = 0xff;
1394160383Snetchild		if (sc->is_emu10k1)
1395160383Snetchild			sc->codec = AC97_CREATE(dev, sc, emu_ac97);
1396160383Snetchild		else
1397160383Snetchild			sc->codec = AC97_CREATE(dev, sc, emu_eac97);
1398172150Sariff		sc->ac97_mixerclass = NULL;
1399172150Sariff		if (sc->codec != NULL)
1400172150Sariff			sc->ac97_mixerclass = ac97_getmixerclass();
1401172150Sariff		if (mixer_init(dev, &emudspmixer_class, sc)) {
1402172150Sariff			device_printf(dev, "failed to initialize DSP mixer\n");
1403172150Sariff			goto bad;
1404172150Sariff		}
1405160383Snetchild		break;
1406160383Snetchild	case RT_REAR:
1407160383Snetchild		sc->rt.amounts_left[2] = 0xff;
1408160383Snetchild		sc->rt.amounts_right[3] = 0xff;
1409161056Snetchild		sc->rt_mono.amounts_left[2] = 0xff;
1410161056Snetchild		sc->rt_mono.amounts_left[3] = 0xff;
1411160383Snetchild		if (mixer_init(dev, &emudspmixer_class, sc)) {
1412160383Snetchild			device_printf(dev, "failed to initialize mixer\n");
1413160383Snetchild			goto bad;
1414160383Snetchild		}
1415160383Snetchild		break;
1416160383Snetchild	case RT_CENTER:
1417160383Snetchild		sc->rt.amounts_left[4] = 0xff;
1418161056Snetchild		sc->rt_mono.amounts_left[4] = 0xff;
1419160383Snetchild		if (mixer_init(dev, &emudspmixer_class, sc)) {
1420160383Snetchild			device_printf(dev, "failed to initialize mixer\n");
1421160383Snetchild			goto bad;
1422160383Snetchild		}
1423160383Snetchild		break;
1424160383Snetchild	case RT_SUB:
1425160383Snetchild		sc->rt.amounts_left[5] = 0xff;
1426161056Snetchild		sc->rt_mono.amounts_left[5] = 0xff;
1427160383Snetchild		if (mixer_init(dev, &emudspmixer_class, sc)) {
1428160383Snetchild			device_printf(dev, "failed to initialize mixer\n");
1429160383Snetchild			goto bad;
1430160383Snetchild		}
1431160383Snetchild		break;
1432160383Snetchild	case RT_SIDE:
1433160383Snetchild		sc->rt.amounts_left[6] = 0xff;
1434160383Snetchild		sc->rt.amounts_right[7] = 0xff;
1435161056Snetchild		sc->rt_mono.amounts_left[6] = 0xff;
1436161056Snetchild		sc->rt_mono.amounts_left[7] = 0xff;
1437160383Snetchild		if (mixer_init(dev, &emudspmixer_class, sc)) {
1438160383Snetchild			device_printf(dev, "failed to initialize mixer\n");
1439160383Snetchild			goto bad;
1440160383Snetchild		}
1441160383Snetchild		break;
1442165833Snetchild	case RT_MCHRECORD:
1443172150Sariff		if (mixer_init(dev, &emuefxmixer_class, sc)) {
1444172150Sariff			device_printf(dev, "failed to initialize EFX mixer\n");
1445172150Sariff			goto bad;
1446172150Sariff		}
1447165833Snetchild		break;
1448160383Snetchild	default:
1449160383Snetchild		device_printf(dev, "invalid default route\n");
1450160383Snetchild		goto bad;
1451160383Snetchild	}
1452160383Snetchild
1453230964Spfg	inte = EMU_INTE_INTERTIMERENB;
1454230964Spfg	ipr = EMU_IPR_INTERVALTIMER; /* Used by playback & ADC */
1455160383Snetchild	sc->ihandle = emu_intr_register(sc->card, inte, ipr, &emu_pcm_intr, sc);
1456160383Snetchild
1457160383Snetchild	if (emu_pcm_init(sc) == -1) {
1458160383Snetchild		device_printf(dev, "unable to initialize PCM part of the card\n");
1459160383Snetchild		goto bad;
1460160383Snetchild	}
1461160383Snetchild
1462172150Sariff	/*
1463172150Sariff	 * We don't register interrupt handler with snd_setup_intr
1464172150Sariff	 * in pcm device. Mark pcm device as MPSAFE manually.
1465172150Sariff	 */
1466172150Sariff	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
1467172150Sariff
1468160383Snetchild	/* XXX we should better get number of available channels from parent */
1469160383Snetchild	if (pcm_register(dev, sc, (route == RT_FRONT) ? MAX_CHANNELS : 1, (route == RT_FRONT) ? 1 : 0)) {
1470160383Snetchild		device_printf(dev, "can't register PCM channels!\n");
1471160383Snetchild		goto bad;
1472160383Snetchild	}
1473160383Snetchild	sc->pnum = 0;
1474165833Snetchild	if (route != RT_MCHRECORD)
1475165833Snetchild		pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc);
1476160383Snetchild	if (route == RT_FRONT) {
1477160383Snetchild		for (i = 1; i < MAX_CHANNELS; i++)
1478160383Snetchild			pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc);
1479160383Snetchild		pcm_addchan(dev, PCMDIR_REC, &emurchan_class, sc);
1480160383Snetchild	}
1481165833Snetchild	if (route == RT_MCHRECORD)
1482165833Snetchild		pcm_addchan(dev, PCMDIR_REC, &emufxrchan_class, sc);
1483165833Snetchild
1484160383Snetchild	snprintf(status, SND_STATUSLEN, "on %s", device_get_nameunit(device_get_parent(dev)));
1485160383Snetchild	pcm_setstatus(dev, status);
1486160383Snetchild
1487160383Snetchild	return (0);
1488160383Snetchild
1489160383Snetchildbad:
1490160383Snetchild	if (sc->codec)
1491160383Snetchild		ac97_destroy(sc->codec);
1492160383Snetchild	if (sc->lock)
1493160383Snetchild		snd_mtxfree(sc->lock);
1494160383Snetchild	free(sc, M_DEVBUF);
1495160383Snetchild	return (ENXIO);
1496160383Snetchild}
1497160383Snetchild
1498160383Snetchildstatic int
1499160383Snetchildemu_pcm_detach(device_t dev)
1500160383Snetchild{
1501160383Snetchild	int r;
1502160383Snetchild	struct emu_pcm_info *sc;
1503160383Snetchild
1504160383Snetchild	sc = pcm_getdevinfo(dev);
1505160383Snetchild
1506160383Snetchild	r = pcm_unregister(dev);
1507160383Snetchild
1508160383Snetchild	if (r) 	return (r);
1509160383Snetchild
1510160383Snetchild	emu_pcm_uninit(sc);
1511160383Snetchild
1512160383Snetchild	if (sc->lock)
1513160383Snetchild		snd_mtxfree(sc->lock);
1514160383Snetchild	free(sc, M_DEVBUF);
1515160383Snetchild
1516160383Snetchild	return (0);
1517160383Snetchild}
1518160383Snetchild
1519160383Snetchildstatic device_method_t emu_pcm_methods[] = {
1520160383Snetchild	DEVMETHOD(device_probe, emu_pcm_probe),
1521160383Snetchild	DEVMETHOD(device_attach, emu_pcm_attach),
1522160383Snetchild	DEVMETHOD(device_detach, emu_pcm_detach),
1523160383Snetchild	{0, 0}
1524160383Snetchild};
1525160383Snetchild
1526160383Snetchildstatic driver_t emu_pcm_driver = {
1527160383Snetchild	"pcm",
1528160383Snetchild	emu_pcm_methods,
1529160383Snetchild	PCM_SOFTC_SIZE,
1530160383Snetchild	NULL,
1531160383Snetchild	0,
1532160383Snetchild	NULL
1533160383Snetchild};
1534160383SnetchildDRIVER_MODULE(snd_emu10kx_pcm, emu10kx, emu_pcm_driver, pcm_devclass, 0, 0);
1535160383SnetchildMODULE_DEPEND(snd_emu10kx_pcm, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER);
1536160383SnetchildMODULE_DEPEND(snd_emu10kx_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1537160383SnetchildMODULE_VERSION(snd_emu10kx_pcm, SND_EMU10KX_PREFVER);
1538