xref: /freebsd/sys/dev/sound/pci/emu10kx-pcm.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
1 /*-
2  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3  * Copyright (c) 2003-2006 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/bus.h>
33 #include <machine/bus.h>
34 #include <sys/rman.h>
35 #include <sys/systm.h>
36 #include <sys/sbuf.h>
37 #include <sys/queue.h>
38 #include <sys/systm.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 
42 #include <dev/sound/chip.h>
43 #include <dev/sound/pcm/sound.h>
44 #include <dev/sound/pcm/ac97.h>
45 
46 #include "mixer_if.h"
47 
48 #include "opt_emu10kx.h"
49 #include <dev/sound/pci/emu10kx.h>
50 #include "emu10k1-alsa%diked.h"
51 
52 struct emu_pcm_pchinfo {
53 	int		spd;
54 	int		fmt;
55 	int		blksz;
56 	int		run;
57 	struct emu_voice *master;
58 	struct emu_voice *slave;
59 	struct snd_dbuf	*buffer;
60 	struct pcm_channel *channel;
61 	struct emu_pcm_info *pcm;
62 	int		timer;
63 };
64 
65 struct emu_pcm_rchinfo {
66 	int		spd;
67 	int		fmt;
68 	int		blksz;
69 	int		run;
70 	uint32_t 	idxreg;
71 	uint32_t	basereg;
72 	uint32_t	sizereg;
73 	uint32_t	setupreg;
74 	uint32_t	irqmask;
75 	uint32_t	iprmask;
76 	int 		ihandle;
77 	struct snd_dbuf	*buffer;
78 	struct pcm_channel *channel;
79 	struct emu_pcm_info *pcm;
80 };
81 
82 /* XXX Hardware playback channels */
83 #define	MAX_CHANNELS	4
84 
85 #if MAX_CHANNELS > 13
86 #error	Too many hardware channels defined. 13 is the maximum
87 #endif
88 
89 struct emu_pcm_info {
90 	struct mtx		*lock;
91 	device_t		dev;		/* device information */
92 	struct snddev_info 	*devinfo;	/* pcm device information */
93 	struct emu_sc_info 	*card;
94 	struct emu_pcm_pchinfo	pch[MAX_CHANNELS];	/* hardware channels */
95 	int			pnum;		/* next free channel number */
96 	struct emu_pcm_rchinfo	rch_adc;
97 	struct emu_pcm_rchinfo	rch_efx;
98 	struct emu_route	rt;
99 	struct emu_route	rt_mono;
100 	int			route;
101 	int			ihandle;	/* interrupt handler */
102 	unsigned int		bufsz;
103 	int			is_emu10k1;
104 	struct ac97_info	*codec;
105 	uint32_t 		ac97_state[0x7F];
106 };
107 
108 
109 static uint32_t emu_rfmt_adc[] = {
110 	AFMT_S16_LE,
111 	AFMT_STEREO | AFMT_S16_LE,
112 	0
113 };
114 static struct pcmchan_caps emu_reccaps_adc = {
115 	8000, 48000, emu_rfmt_adc, 0
116 };
117 
118 static uint32_t emu_rfmt_efx[] = {
119 	AFMT_S16_LE,
120 	0
121 };
122 
123 static struct pcmchan_caps emu_reccaps_efx_live = {
124 	48000*32, 48000*32, emu_rfmt_efx, 0
125 };
126 
127 static struct pcmchan_caps emu_reccaps_efx_audigy = {
128 	48000*64, 48000*64, emu_rfmt_efx, 0
129 };
130 
131 static uint32_t emu_pfmt[] = {
132 	AFMT_U8,
133 	AFMT_STEREO | AFMT_U8,
134 	AFMT_S16_LE,
135 	AFMT_STEREO | AFMT_S16_LE,
136 	0
137 };
138 static uint32_t emu_pfmt_mono[] = {
139 	AFMT_U8,
140 	AFMT_S16_LE,
141 	0
142 };
143 
144 static struct pcmchan_caps emu_playcaps = {4000, 48000, emu_pfmt, 0};
145 static struct pcmchan_caps emu_playcaps_mono = {4000, 48000, emu_pfmt_mono, 0};
146 
147 static int emu10k1_adcspeed[8] = {48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000};
148 /* audigy supports 12kHz. */
149 static int emu10k2_adcspeed[9] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000};
150 
151 static uint32_t emu_pcm_intr(void *pcm, uint32_t stat);
152 
153 static const struct emu_dspmix_props {
154 	u_int8_t	present;
155 } dspmix [SOUND_MIXER_NRDEVICES] = {
156 	[SOUND_MIXER_VOLUME] =	{1},
157 	[SOUND_MIXER_PCM] =	{1},
158 };
159 
160 static int
161 emu_dspmixer_init(struct snd_mixer *m)
162 {
163 	int i;
164 	int v;
165 
166 	v = 0;
167 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
168 		if (dspmix[i].present)
169 			v |= 1 << i;
170 	}
171 	mix_setdevs(m, v);
172 
173 	mix_setrecdevs(m, 0);
174 	return (0);
175 }
176 
177 static int
178 emu_dspmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
179 {
180 	struct emu_pcm_info *sc;
181 
182 	sc = mix_getdevinfo(m);
183 
184 	switch (dev) {
185 	case SOUND_MIXER_VOLUME:
186 		switch (sc->route) {
187 		case RT_REAR:
188 			emumix_set_volume(sc->card, M_MASTER_REAR_L, left);
189 			emumix_set_volume(sc->card, M_MASTER_REAR_R, right);
190 			break;
191 		case RT_CENTER:
192 			emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2);
193 			break;
194 		case RT_SUB:
195 			emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2);
196 			break;
197 		}
198 		break;
199 	case SOUND_MIXER_PCM:
200 		switch (sc->route) {
201 		case RT_REAR:
202 			emumix_set_volume(sc->card, M_FX2_REAR_L, left);
203 			emumix_set_volume(sc->card, M_FX3_REAR_R, right);
204 			break;
205 		case RT_CENTER:
206 			emumix_set_volume(sc->card, M_FX4_CENTER, (left+right)/2);
207 			break;
208 		case RT_SUB:
209 			emumix_set_volume(sc->card, M_FX5_SUBWOOFER, (left+right)/2);
210 			break;
211 		}
212 		break;
213 	default:
214 		device_printf(sc->dev, "mixer error: unknown device %d\n", dev);
215 	}
216 	return  (0);
217 }
218 
219 static int
220 emu_dspmixer_setrecsrc(struct snd_mixer *m __unused, u_int32_t src __unused)
221 {
222 	return (0);
223 }
224 
225 static kobj_method_t emudspmixer_methods[] = {
226 	KOBJMETHOD(mixer_init,		emu_dspmixer_init),
227 	KOBJMETHOD(mixer_set,		emu_dspmixer_set),
228 	KOBJMETHOD(mixer_setrecsrc,	emu_dspmixer_setrecsrc),
229 	{ 0, 0 }
230 };
231 MIXER_DECLARE(emudspmixer);
232 
233 /*
234  * AC97 emulation code for Audigy and later cards.
235  * Some parts of AC97 codec are not used by hardware, but can be used
236  * to change some DSP controls via AC97 mixer interface. This includes:
237  * - master volume controls MASTER_FRONT_[R|L]
238  * - pcm volume controls FX[0|1]_FRONT_[R|L]
239  * - rec volume controls MASTER_REC_[R|L]
240  * We do it because we need to put it under user control....
241  * We also keep some parts of AC97 disabled to get better sound quality
242  */
243 
244 #define	AC97LEFT(x)	((x & 0x7F00)>>8)
245 #define	AC97RIGHT(x)	(x & 0x007F)
246 #define	AC97MUTE(x)	((x & 0x8000)>>15)
247 #define	BIT4_TO100(x)	(100-(x)*100/(0x0f))
248 #define	BIT6_TO100(x)	(100-(x)*100/(0x3f))
249 #define	BIT4_TO255(x)	(255-(x)*255/(0x0f))
250 #define	BIT6_TO255(x)	(255-(x)*255/(0x3f))
251 #define	V100_TOBIT6(x)	(0x3f*(100-x)/100)
252 #define	V100_TOBIT4(x)	(0x0f*(100-x)/100)
253 #define	AC97ENCODE(x_muted,x_left,x_right)	(((x_muted&1)<<15) | ((x_left&0x3f)<<8) | (x_right&0x3f))
254 
255 static int
256 emu_ac97_read_emulation(struct emu_pcm_info *sc, int regno)
257 {
258 	int use_ac97;
259 	int emulated;
260 	int tmp;
261 
262 	use_ac97 = 1;
263 	emulated = 0;
264 
265 	switch (regno) {
266 	case AC97_MIX_MASTER:
267 		emulated = sc->ac97_state[AC97_MIX_MASTER];
268 		use_ac97 = 0;
269 		break;
270 	case AC97_MIX_PCM:
271 		emulated = sc->ac97_state[AC97_MIX_PCM];
272 		use_ac97 = 0;
273 		break;
274 	case AC97_REG_RECSEL:
275 		emulated = 0x0505;
276 		use_ac97 = 0;
277 		break;
278 	case AC97_MIX_RGAIN:
279 		emulated = sc->ac97_state[AC97_MIX_RGAIN];
280 		use_ac97 = 0;
281 		break;
282 	}
283 
284 	emu_wr(sc->card, AC97ADDRESS, regno, 1);
285 	tmp = emu_rd(sc->card, AC97DATA, 2);
286 
287 	if (use_ac97)
288 		emulated = tmp;
289 
290 	return (emulated);
291 }
292 
293 static void
294 emu_ac97_write_emulation(struct emu_pcm_info *sc, int regno, uint32_t data)
295 {
296 	int write_ac97;
297 	int left, right;
298 	uint32_t emu_left, emu_right;
299 	int is_mute;
300 
301 	write_ac97 = 1;
302 
303 	left = AC97LEFT(data);
304 	emu_left = BIT6_TO100(left);	/* We show us as 6-bit AC97 mixer */
305 	right = AC97RIGHT(data);
306 	emu_right = BIT6_TO100(right);
307 	is_mute = AC97MUTE(data);
308 	if (is_mute)
309 		emu_left = emu_right = 0;
310 
311 	switch (regno) {
312 		/* TODO: reset emulator on AC97_RESET */
313 	case AC97_MIX_MASTER:
314 		emumix_set_volume(sc->card, M_MASTER_FRONT_L, emu_left);
315 		emumix_set_volume(sc->card, M_MASTER_FRONT_R, emu_right);
316 		sc->ac97_state[AC97_MIX_MASTER] = data & (0x8000 | 0x3f3f);
317 		data = 0x8000;	/* Mute AC97 main out */
318 		break;
319 	case AC97_MIX_PCM:	/* PCM OUT VOL */
320 		emumix_set_volume(sc->card, M_FX0_FRONT_L, emu_left);
321 		emumix_set_volume(sc->card, M_FX1_FRONT_R, emu_right);
322 		sc->ac97_state[AC97_MIX_PCM] = data & (0x8000 | 0x3f3f);
323 		data = 0x8000;	/* Mute AC97 PCM out */
324 		break;
325 	case AC97_REG_RECSEL:
326 		/*
327 		 * PCM recording source is set to "stereo mix" (labeled "vol"
328 		 * in mixer) XXX !I can't remember why!
329 		 */
330 		data = 0x0505;
331 		break;
332 	case AC97_MIX_RGAIN:	/* RECORD GAIN */
333 		emu_left = BIT4_TO100(left);	/* rgain is 4-bit */
334 		emu_right = BIT4_TO100(right);
335 		emumix_set_volume(sc->card, M_MASTER_REC_L, 100-emu_left);
336 		emumix_set_volume(sc->card, M_MASTER_REC_R, 100-emu_right);
337 		/*
338 		 * Record gain on AC97 should stay zero to get AC97 sound on
339 		 * AC97_[RL] connectors on EMU10K2 chip. AC97 on Audigy is not
340 		 * directly connected to any output, only to EMU10K2 chip Use
341 		 * this control to set AC97 mix volume inside EMU10K2 chip
342 		 */
343 		sc->ac97_state[AC97_MIX_RGAIN] = data & (0x8000 | 0x0f0f);
344 		data = 0x0000;
345 		break;
346 	}
347 	if (write_ac97) {
348 		emu_wr(sc->card, AC97ADDRESS, regno, 1);
349 		emu_wr(sc->card, AC97DATA, data, 2);
350 	}
351 }
352 
353 static int
354 emu_erdcd(kobj_t obj __unused, void *devinfo, int regno)
355 {
356 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
357 
358 	return (emu_ac97_read_emulation(sc, regno));
359 }
360 
361 static int
362 emu_ewrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data)
363 {
364 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
365 
366 	emu_ac97_write_emulation(sc, regno, data);
367 	return (0);
368 }
369 
370 static kobj_method_t emu_eac97_methods[] = {
371 	KOBJMETHOD(ac97_read, emu_erdcd),
372 	KOBJMETHOD(ac97_write, emu_ewrcd),
373 	{0, 0}
374 };
375 AC97_DECLARE(emu_eac97);
376 
377 /* real ac97 codec */
378 static int
379 emu_rdcd(kobj_t obj __unused, void *devinfo, int regno)
380 {
381 	int rd;
382 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
383 
384 	KASSERT(sc->card != NULL, ("emu_rdcd: no soundcard"));
385 	emu_wr(sc->card, AC97ADDRESS, regno, 1);
386 	rd = emu_rd(sc->card, AC97DATA, 2);
387 	return (rd);
388 }
389 
390 static int
391 emu_wrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data)
392 {
393 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
394 
395 	KASSERT(sc->card != NULL, ("emu_wrcd: no soundcard"));
396 	emu_wr(sc->card, AC97ADDRESS, regno, 1);
397 	emu_wr(sc->card, AC97DATA, data, 2);
398 	return (0);
399 }
400 
401 static kobj_method_t emu_ac97_methods[] = {
402 	KOBJMETHOD(ac97_read, emu_rdcd),
403 	KOBJMETHOD(ac97_write, emu_wrcd),
404 	{0, 0}
405 };
406 AC97_DECLARE(emu_ac97);
407 
408 
409 static int
410 emu_k1_recval(int speed)
411 {
412 	int val;
413 
414 	val = 0;
415 	while ((val < 7) && (speed < emu10k1_adcspeed[val]))
416 		val++;
417 	if (val == 6) val=5; /* XXX 8kHz does not work */
418 	return (val);
419 }
420 
421 static int
422 emu_k2_recval(int speed)
423 {
424 	int val;
425 
426 	val = 0;
427 	while ((val < 8) && (speed < emu10k2_adcspeed[val]))
428 		val++;
429 	if (val == 7) val=6; /* XXX 8kHz does not work */
430 	return (val);
431 }
432 
433 static void *
434 emupchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
435 {
436 	struct emu_pcm_info *sc = devinfo;
437 	struct emu_pcm_pchinfo *ch;
438 	void *r;
439 
440 	KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction"));
441 	KASSERT(sc->card != NULL, ("empchan_init: no soundcard"));
442 
443 
444 	if (sc->pnum >= MAX_CHANNELS)
445 		return (NULL);
446 	ch = &(sc->pch[sc->pnum++]);
447 	ch->buffer = b;
448 	ch->pcm = sc;
449 	ch->channel = c;
450 	ch->blksz = sc->bufsz;
451 	ch->fmt = AFMT_U8;
452 	ch->spd = 8000;
453 	ch->master = emu_valloc(sc->card);
454 	/*
455 	 * XXX we have to allocate slave even for mono channel until we
456 	 * fix emu_vfree to handle this case.
457 	 */
458 	ch->slave = emu_valloc(sc->card);
459 	ch->timer = emu_timer_create(sc->card);
460 	r = (emu_vinit(sc->card, ch->master, ch->slave, EMU_PLAY_BUFSZ, ch->buffer)) ? NULL : ch;
461 	return (r);
462 }
463 
464 static int
465 emupchan_free(kobj_t obj __unused, void *c_devinfo)
466 {
467 	struct emu_pcm_pchinfo *ch = c_devinfo;
468 	struct emu_pcm_info *sc = ch->pcm;
469 
470 	emu_timer_clear(sc->card, ch->timer);
471 	if (ch->slave != NULL)
472 		emu_vfree(sc->card, ch->slave);
473 	emu_vfree(sc->card, ch->master);
474 	return (0);
475 }
476 
477 static int
478 emupchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format)
479 {
480 	struct emu_pcm_pchinfo *ch = c_devinfo;
481 
482 	ch->fmt = format;
483 	return (0);
484 }
485 
486 static int
487 emupchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
488 {
489 	struct emu_pcm_pchinfo *ch = c_devinfo;
490 
491 	ch->spd = speed;
492 	return (ch->spd);
493 }
494 
495 static int
496 emupchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
497 {
498 	struct emu_pcm_pchinfo *ch = c_devinfo;
499 	struct emu_pcm_info *sc = ch->pcm;
500 
501 	if (blocksize > ch->pcm->bufsz)
502 		blocksize = ch->pcm->bufsz;
503 	snd_mtxlock(sc->lock);
504 	ch->blksz = blocksize;
505 	emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getbps(ch->buffer));
506 	snd_mtxunlock(sc->lock);
507 	return (blocksize);
508 }
509 
510 static int
511 emupchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
512 {
513 	struct emu_pcm_pchinfo *ch = c_devinfo;
514 	struct emu_pcm_info *sc = ch->pcm;
515 
516 	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
517 		return (0);
518 	snd_mtxlock(sc->lock); /* XXX can we trigger on parallel threads ? */
519 	if (go == PCMTRIG_START) {
520 		emu_vsetup(ch->master, ch->fmt, ch->spd);
521 		if ((ch->fmt & AFMT_STEREO) == AFMT_STEREO)
522 			emu_vroute(sc->card, &(sc->rt), ch->master);
523 		else
524 			emu_vroute(sc->card, &(sc->rt_mono), ch->master);
525 		emu_vwrite(sc->card, ch->master);
526 		emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getbps(ch->buffer));
527 		emu_timer_enable(sc->card, ch->timer, 1);
528 	}
529 	/* PCM interrupt handler will handle PCMTRIG_STOP event */
530 	ch->run = (go == PCMTRIG_START) ? 1 : 0;
531 	emu_vtrigger(sc->card, ch->master, ch->run);
532 	snd_mtxunlock(sc->lock);
533 	return (0);
534 }
535 
536 static int
537 emupchan_getptr(kobj_t obj __unused, void *c_devinfo)
538 {
539 	struct emu_pcm_pchinfo *ch = c_devinfo;
540 	struct emu_pcm_info *sc = ch->pcm;
541 	int r;
542 
543 	r = emu_vpos(sc->card, ch->master);
544 
545 	return (r);
546 }
547 
548 static struct pcmchan_caps *
549 emupchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused)
550 {
551 	struct emu_pcm_pchinfo *ch = c_devinfo;
552 	struct emu_pcm_info *sc = ch->pcm;
553 
554 	switch (sc->route) {
555 	case RT_FRONT:
556 		/* FALLTHROUGH */
557 	case RT_REAR:
558 		/* FALLTHROUGH */
559 	case RT_SIDE:
560 		return (&emu_playcaps);
561 		break;
562 	case RT_CENTER:
563 		/* FALLTHROUGH */
564 	case RT_SUB:
565 		return (&emu_playcaps_mono);
566 		break;
567 	}
568 	return (NULL);
569 }
570 
571 static kobj_method_t emupchan_methods[] = {
572 	KOBJMETHOD(channel_init, emupchan_init),
573 	KOBJMETHOD(channel_free, emupchan_free),
574 	KOBJMETHOD(channel_setformat, emupchan_setformat),
575 	KOBJMETHOD(channel_setspeed, emupchan_setspeed),
576 	KOBJMETHOD(channel_setblocksize, emupchan_setblocksize),
577 	KOBJMETHOD(channel_trigger, emupchan_trigger),
578 	KOBJMETHOD(channel_getptr, emupchan_getptr),
579 	KOBJMETHOD(channel_getcaps, emupchan_getcaps),
580 	{0, 0}
581 };
582 CHANNEL_DECLARE(emupchan);
583 
584 static void *
585 emurchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
586 {
587 	struct emu_pcm_info *sc = devinfo;
588 	struct emu_pcm_rchinfo *ch;
589 
590 	KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction"));
591 	ch = &sc->rch_adc;
592 	ch->buffer = b;
593 	ch->pcm = sc;
594 	ch->channel = c;
595 	ch->blksz = sc->bufsz / 2; /* We rise interrupt for half-full buffer */
596 	ch->fmt = AFMT_U8;
597 	ch->spd = 8000;
598 	ch->idxreg = sc->is_emu10k1 ? ADCIDX : A_ADCIDX;
599 	ch->basereg = ADCBA;
600 	ch->sizereg = ADCBS;
601 	ch->setupreg = ADCCR;
602 	ch->irqmask = INTE_ADCBUFENABLE;
603 	ch->iprmask = IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL;
604 
605 	if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), sc->bufsz) != 0)
606 		return (NULL);
607 	else {
608 		emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer));
609 		emu_wrptr(sc->card, 0, ch->sizereg, 0);	/* off */
610 		return (ch);
611 	}
612 }
613 
614 static int
615 emurchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format)
616 {
617 	struct emu_pcm_rchinfo *ch = c_devinfo;
618 
619 	ch->fmt = format;
620 	return (0);
621 }
622 
623 static int
624 emurchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
625 {
626 	struct emu_pcm_rchinfo *ch = c_devinfo;
627 
628 	if (ch->pcm->is_emu10k1) {
629 		speed = emu10k1_adcspeed[emu_k1_recval(speed)];
630 	} else {
631 		speed = emu10k2_adcspeed[emu_k2_recval(speed)];
632 	}
633 	ch->spd = speed;
634 	return (ch->spd);
635 }
636 
637 static int
638 emurchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
639 {
640 	struct emu_pcm_rchinfo *ch = c_devinfo;
641 
642 	ch->blksz = blocksize;
643 	/* If blocksize is less than half of buffer size we will not get
644 	interrupt in time and channel will die due to interrupt timeout */
645 	if(ch->blksz < (ch->pcm->bufsz / 2))
646 		ch->blksz = ch->pcm->bufsz / 2;
647 	return (ch->blksz);
648 }
649 
650 static int
651 emurchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
652 {
653 	struct emu_pcm_rchinfo *ch = c_devinfo;
654 	struct emu_pcm_info *sc = ch->pcm;
655 	uint32_t val, sz;
656 
657 	switch (sc->bufsz) {
658 	case 4096:
659 		sz = ADCBS_BUFSIZE_4096;
660 		break;
661 	case 8192:
662 		sz = ADCBS_BUFSIZE_8192;
663 		break;
664 	case 16384:
665 		sz = ADCBS_BUFSIZE_16384;
666 		break;
667 	case 32768:
668 		sz = ADCBS_BUFSIZE_32768;
669 		break;
670 	case 65536:
671 		sz = ADCBS_BUFSIZE_65536;
672 		break;
673 	default:
674 		sz = ADCBS_BUFSIZE_4096;
675 	}
676 
677 	snd_mtxlock(sc->lock);
678 	switch (go) {
679 	case PCMTRIG_START:
680 		ch->run = 1;
681 		emu_wrptr(sc->card, 0, ch->sizereg, sz);
682 		val = sc->is_emu10k1 ? ADCCR_LCHANENABLE : A_ADCCR_LCHANENABLE;
683 		if (ch->fmt & AFMT_STEREO)
684 			val |= sc->is_emu10k1 ? ADCCR_RCHANENABLE : A_ADCCR_RCHANENABLE;
685 		val |= sc->is_emu10k1 ? emu_k1_recval(ch->spd) : emu_k2_recval(ch->spd);
686 		emu_wrptr(sc->card, 0, ch->setupreg, 0);
687 		emu_wrptr(sc->card, 0, ch->setupreg, val);
688 		ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc);
689 		break;
690 	case PCMTRIG_STOP:
691 		/* FALLTHROUGH */
692 	case PCMTRIG_ABORT:
693 		ch->run = 0;
694 		emu_wrptr(sc->card, 0, ch->sizereg, 0);
695 		if (ch->setupreg)
696 			emu_wrptr(sc->card, 0, ch->setupreg, 0);
697 		(void)emu_intr_unregister(sc->card, ch->ihandle);
698 		break;
699 	case PCMTRIG_EMLDMAWR:
700 		/* FALLTHROUGH */
701 	case PCMTRIG_EMLDMARD:
702 		/* FALLTHROUGH */
703 	default:
704 		break;
705 	}
706 	snd_mtxunlock(sc->lock);
707 
708 	return (0);
709 }
710 
711 static int
712 emurchan_getptr(kobj_t obj __unused, void *c_devinfo)
713 {
714 	struct emu_pcm_rchinfo *ch = c_devinfo;
715 	struct emu_pcm_info *sc = ch->pcm;
716 	int r;
717 
718 	r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff;
719 
720 	return (r);
721 }
722 
723 static struct pcmchan_caps *
724 emurchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused)
725 {
726 	return (&emu_reccaps_adc);
727 }
728 
729 static kobj_method_t emurchan_methods[] = {
730 	KOBJMETHOD(channel_init, emurchan_init),
731 	KOBJMETHOD(channel_setformat, emurchan_setformat),
732 	KOBJMETHOD(channel_setspeed, emurchan_setspeed),
733 	KOBJMETHOD(channel_setblocksize, emurchan_setblocksize),
734 	KOBJMETHOD(channel_trigger, emurchan_trigger),
735 	KOBJMETHOD(channel_getptr, emurchan_getptr),
736 	KOBJMETHOD(channel_getcaps, emurchan_getcaps),
737 	{0, 0}
738 };
739 CHANNEL_DECLARE(emurchan);
740 
741 static void *
742 emufxrchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused)
743 {
744 	struct emu_pcm_info *sc = devinfo;
745 	struct emu_pcm_rchinfo *ch;
746 
747 	KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction"));
748 
749 	if (sc == NULL) return (NULL);
750 
751 	ch = &(sc->rch_efx);
752 	ch->fmt = AFMT_S16_LE;
753 	ch->spd = sc->is_emu10k1 ? 48000*32 : 48000 * 64;
754 	ch->idxreg = FXIDX;
755 	ch->basereg = FXBA;
756 	ch->sizereg = FXBS;
757 	ch->irqmask = INTE_EFXBUFENABLE;
758 	ch->iprmask = IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL;
759 	ch->buffer = b;
760 	ch->pcm = sc;
761 	ch->channel = c;
762 	ch->blksz = sc->bufsz;
763 
764 	if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), sc->bufsz) != 0)
765 		return (NULL);
766 	else {
767 		emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer));
768 		emu_wrptr(sc->card, 0, ch->sizereg, 0);	/* off */
769 		return (ch);
770 	}
771 }
772 
773 static int
774 emufxrchan_setformat(kobj_t obj __unused, void *c_devinfo __unused, uint32_t format)
775 {
776 	if (format == AFMT_S16_LE) return (0);
777 	return (-1);
778 }
779 
780 static int
781 emufxrchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed)
782 {
783 	struct emu_pcm_rchinfo *ch = c_devinfo;
784 
785 	/* FIXED RATE CHANNEL */
786 	return (ch->spd);
787 }
788 
789 static int
790 emufxrchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize)
791 {
792 	struct emu_pcm_rchinfo *ch = c_devinfo;
793 
794 	ch->blksz = blocksize;
795 	/* If blocksize is less than half of buffer size we will not get
796 	interrupt in time and channel will die due to interrupt timeout */
797 	if(ch->blksz < (ch->pcm->bufsz / 2))
798 		ch->blksz = ch->pcm->bufsz / 2;
799 	return (ch->blksz);
800 }
801 
802 static int
803 emufxrchan_trigger(kobj_t obj __unused, void *c_devinfo, int go)
804 {
805 	struct emu_pcm_rchinfo *ch = c_devinfo;
806 	struct emu_pcm_info *sc = ch->pcm;
807 	uint32_t sz;
808 
809 	switch (sc->bufsz) {
810 	case 4096:
811 		sz = ADCBS_BUFSIZE_4096;
812 		break;
813 	case 8192:
814 		sz = ADCBS_BUFSIZE_8192;
815 		break;
816 	case 16384:
817 		sz = ADCBS_BUFSIZE_16384;
818 		break;
819 	case 32768:
820 		sz = ADCBS_BUFSIZE_32768;
821 		break;
822 	case 65536:
823 		sz = ADCBS_BUFSIZE_65536;
824 		break;
825 	default:
826 		sz = ADCBS_BUFSIZE_4096;
827 	}
828 
829 	snd_mtxlock(sc->lock);
830 	switch (go) {
831 	case PCMTRIG_START:
832 		ch->run = 1;
833 		emu_wrptr(sc->card, 0, ch->sizereg, sz);
834 		ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc);
835 		/*
836 		 SB Live! is limited to 32 mono channels. Audigy
837 		 has 64 mono channels, each of them is selected from
838 		 one of two A_FXWC[1|2] registers.
839 		 */
840 		/* XXX there is no way to demultiplex this streams for now */
841 		if(sc->is_emu10k1) {
842 			emu_wrptr(sc->card, 0, FXWC, 0xffffffff);
843 		} else {
844 			emu_wrptr(sc->card, 0, A_FXWC1, 0xffffffff);
845 			emu_wrptr(sc->card, 0, A_FXWC2, 0xffffffff);
846 		}
847 		break;
848 	case PCMTRIG_STOP:
849 		/* FALLTHROUGH */
850 	case PCMTRIG_ABORT:
851 		ch->run = 0;
852 		if(sc->is_emu10k1) {
853 			emu_wrptr(sc->card, 0, FXWC, 0x0);
854 		} else {
855 			emu_wrptr(sc->card, 0, A_FXWC1, 0x0);
856 			emu_wrptr(sc->card, 0, A_FXWC2, 0x0);
857 		}
858 		emu_wrptr(sc->card, 0, ch->sizereg, 0);
859 		(void)emu_intr_unregister(sc->card, ch->ihandle);
860 		break;
861 	case PCMTRIG_EMLDMAWR:
862 		/* FALLTHROUGH */
863 	case PCMTRIG_EMLDMARD:
864 		/* FALLTHROUGH */
865 	default:
866 		break;
867 	}
868 	snd_mtxunlock(sc->lock);
869 
870 	return (0);
871 }
872 
873 static int
874 emufxrchan_getptr(kobj_t obj __unused, void *c_devinfo)
875 {
876 	struct emu_pcm_rchinfo *ch = c_devinfo;
877 	struct emu_pcm_info *sc = ch->pcm;
878 	int r;
879 
880 	r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff;
881 
882 	return (r);
883 }
884 
885 static struct pcmchan_caps *
886 emufxrchan_getcaps(kobj_t obj __unused, void *c_devinfo)
887 {
888 	struct emu_pcm_rchinfo *ch = c_devinfo;
889 	struct emu_pcm_info *sc = ch->pcm;
890 
891 	if(sc->is_emu10k1)
892 		return (&emu_reccaps_efx_live);
893 	return (&emu_reccaps_efx_audigy);
894 
895 }
896 
897 static kobj_method_t emufxrchan_methods[] = {
898 	KOBJMETHOD(channel_init, emufxrchan_init),
899 	KOBJMETHOD(channel_setformat, emufxrchan_setformat),
900 	KOBJMETHOD(channel_setspeed, emufxrchan_setspeed),
901 	KOBJMETHOD(channel_setblocksize, emufxrchan_setblocksize),
902 	KOBJMETHOD(channel_trigger, emufxrchan_trigger),
903 	KOBJMETHOD(channel_getptr, emufxrchan_getptr),
904 	KOBJMETHOD(channel_getcaps, emufxrchan_getcaps),
905 	{0, 0}
906 };
907 CHANNEL_DECLARE(emufxrchan);
908 
909 
910 static uint32_t
911 emu_pcm_intr(void *pcm, uint32_t stat)
912 {
913 	struct emu_pcm_info *sc = (struct emu_pcm_info *)pcm;
914 	uint32_t ack;
915 	int i;
916 
917 	ack = 0;
918 
919 	if (stat & IPR_INTERVALTIMER) {
920 		ack |= IPR_INTERVALTIMER;
921 		for (i = 0; i < MAX_CHANNELS; i++)
922 			if (sc->pch[i].channel) {
923 				if (sc->pch[i].run == 1)
924 					chn_intr(sc->pch[i].channel);
925 				else
926 					emu_timer_enable(sc->card, sc->pch[i].timer, 0);
927 			}
928 	}
929 
930 
931 	if (stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL)) {
932 		ack |= stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL);
933 		if (sc->rch_adc.channel)
934 			chn_intr(sc->rch_adc.channel);
935 	}
936 
937 	if (stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL)) {
938 		ack |= stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL);
939 		if (sc->rch_efx.channel)
940 			chn_intr(sc->rch_efx.channel);
941 	}
942 	return (ack);
943 }
944 
945 static int
946 emu_pcm_init(struct emu_pcm_info *sc)
947 {
948 	sc->bufsz = pcm_getbuffersize(sc->dev, EMUPAGESIZE, EMU_REC_BUFSZ, EMU_MAX_BUFSZ);
949 	return (0);
950 }
951 
952 static int
953 emu_pcm_uninit(struct emu_pcm_info *sc __unused)
954 {
955 	return (0);
956 }
957 
958 static int
959 emu_pcm_probe(device_t dev)
960 {
961 	uintptr_t func, route, r;
962 	const char *rt;
963 	char buffer[255];
964 
965 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_FUNC, &func);
966 
967 	if (func != SCF_PCM)
968 		return (ENXIO);
969 
970 	rt = "UNKNOWN";
971 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route);
972 	switch (route) {
973 	case RT_FRONT:
974 		rt = "front";
975 		break;
976 	case RT_REAR:
977 		rt = "rear";
978 		break;
979 	case RT_CENTER:
980 		rt = "center";
981 		break;
982 	case RT_SUB:
983 		rt = "subwoofer";
984 		break;
985 	case RT_SIDE:
986 		rt = "side";
987 		break;
988 	case RT_MCHRECORD:
989 		rt = "multichannel recording";
990 		break;
991 	}
992 
993 	snprintf(buffer, 255, "EMU10Kx DSP %s PCM interface", rt);
994 	device_set_desc_copy(dev, buffer);
995 	return (0);
996 }
997 
998 static int
999 emu_pcm_attach(device_t dev)
1000 {
1001 	struct emu_pcm_info *sc;
1002 	unsigned int i;
1003 	char status[SND_STATUSLEN];
1004 	uint32_t inte, ipr;
1005 	uintptr_t route, r, is_emu10k1;
1006 
1007 	if ((sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO)) == NULL) {
1008 		device_printf(dev, "cannot allocate softc\n");
1009 		return (ENXIO);
1010 	}
1011 	bzero(sc, sizeof(*sc));
1012 
1013 	sc->card = (struct emu_sc_info *)(device_get_softc(device_get_parent(dev)));
1014 	if (sc->card == NULL) {
1015 		device_printf(dev, "cannot get bridge conf\n");
1016 		return (ENXIO);
1017 	}
1018 
1019 	sc->lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc");
1020 	sc->dev = dev;
1021 
1022 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &is_emu10k1);
1023 	sc->is_emu10k1 = is_emu10k1 ? 1 : 0;
1024 
1025 	sc->codec = NULL;
1026 
1027 	for (i = 0; i < 8; i++) {
1028 		sc->rt.routing_left[i] = i;
1029 		sc->rt.amounts_left[i] = 0x00;
1030 		sc->rt.routing_right[i] = i;
1031 		sc->rt.amounts_right[i] = 0x00;
1032 	}
1033 
1034 	for (i = 0; i < 8; i++) {
1035 		sc->rt_mono.routing_left[i] = i;
1036 		sc->rt_mono.amounts_left[i] = 0x00;
1037 		sc->rt_mono.routing_right[i] = i;
1038 		sc->rt_mono.amounts_right[i] = 0x00;
1039 	}
1040 
1041 	r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route);
1042 	sc->route = route;
1043 	switch (route) {
1044 	case RT_FRONT:
1045 		sc->rt.amounts_left[0] = 0xff;
1046 		sc->rt.amounts_right[1] = 0xff;
1047 		sc->rt_mono.amounts_left[0] = 0xff;
1048 		sc->rt_mono.amounts_left[1] = 0xff;
1049 		if (sc->is_emu10k1)
1050 			sc->codec = AC97_CREATE(dev, sc, emu_ac97);
1051 		else
1052 			sc->codec = AC97_CREATE(dev, sc, emu_eac97);
1053  		if (sc->codec == NULL) {
1054  			if (mixer_init(dev, &emudspmixer_class, sc)) {
1055  				device_printf(dev, "failed to initialize DSP mixer\n");
1056  				goto bad;
1057  			}
1058  		} else
1059 			if (mixer_init(dev, ac97_getmixerclass(), sc->codec) == -1) {
1060  				device_printf(dev, "can't initialize AC97 mixer!\n");
1061  				goto bad;
1062 			}
1063 		break;
1064 	case RT_REAR:
1065 		sc->rt.amounts_left[2] = 0xff;
1066 		sc->rt.amounts_right[3] = 0xff;
1067 		sc->rt_mono.amounts_left[2] = 0xff;
1068 		sc->rt_mono.amounts_left[3] = 0xff;
1069 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1070 			device_printf(dev, "failed to initialize mixer\n");
1071 			goto bad;
1072 		}
1073 		break;
1074 	case RT_CENTER:
1075 		sc->rt.amounts_left[4] = 0xff;
1076 		sc->rt_mono.amounts_left[4] = 0xff;
1077 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1078 			device_printf(dev, "failed to initialize mixer\n");
1079 			goto bad;
1080 		}
1081 		break;
1082 	case RT_SUB:
1083 		sc->rt.amounts_left[5] = 0xff;
1084 		sc->rt_mono.amounts_left[5] = 0xff;
1085 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1086 			device_printf(dev, "failed to initialize mixer\n");
1087 			goto bad;
1088 		}
1089 		break;
1090 	case RT_SIDE:
1091 		sc->rt.amounts_left[6] = 0xff;
1092 		sc->rt.amounts_right[7] = 0xff;
1093 		sc->rt_mono.amounts_left[6] = 0xff;
1094 		sc->rt_mono.amounts_left[7] = 0xff;
1095 		if (mixer_init(dev, &emudspmixer_class, sc)) {
1096 			device_printf(dev, "failed to initialize mixer\n");
1097 			goto bad;
1098 		}
1099 		break;
1100 	case RT_MCHRECORD:
1101 			/* XXX add mixer here */
1102 		break;
1103 	default:
1104 		device_printf(dev, "invalid default route\n");
1105 		goto bad;
1106 	}
1107 
1108 	inte = INTE_INTERVALTIMERENB;
1109 	ipr = IPR_INTERVALTIMER; /* Used by playback */
1110 	sc->ihandle = emu_intr_register(sc->card, inte, ipr, &emu_pcm_intr, sc);
1111 
1112 	if (emu_pcm_init(sc) == -1) {
1113 		device_printf(dev, "unable to initialize PCM part of the card\n");
1114 		goto bad;
1115 	}
1116 
1117 	/* XXX we should better get number of available channels from parent */
1118 	if (pcm_register(dev, sc, (route == RT_FRONT) ? MAX_CHANNELS : 1, (route == RT_FRONT) ? 1 : 0)) {
1119 		device_printf(dev, "can't register PCM channels!\n");
1120 		goto bad;
1121 	}
1122 	sc->pnum = 0;
1123 	if (route != RT_MCHRECORD)
1124 		pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc);
1125 	if (route == RT_FRONT) {
1126 		for (i = 1; i < MAX_CHANNELS; i++)
1127 			pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc);
1128 		pcm_addchan(dev, PCMDIR_REC, &emurchan_class, sc);
1129 	}
1130 	if (route == RT_MCHRECORD)
1131 		pcm_addchan(dev, PCMDIR_REC, &emufxrchan_class, sc);
1132 
1133 	snprintf(status, SND_STATUSLEN, "on %s", device_get_nameunit(device_get_parent(dev)));
1134 	pcm_setstatus(dev, status);
1135 
1136 	return (0);
1137 
1138 bad:
1139 	if (sc->codec)
1140 		ac97_destroy(sc->codec);
1141 	if (sc->lock)
1142 		snd_mtxfree(sc->lock);
1143 	free(sc, M_DEVBUF);
1144 	return (ENXIO);
1145 }
1146 
1147 static int
1148 emu_pcm_detach(device_t dev)
1149 {
1150 	int r;
1151 	struct emu_pcm_info *sc;
1152 
1153 	sc = pcm_getdevinfo(dev);
1154 
1155 	r = pcm_unregister(dev);
1156 
1157 	if (r) 	return (r);
1158 
1159 	emu_pcm_uninit(sc);
1160 
1161 	if (sc->lock)
1162 		snd_mtxfree(sc->lock);
1163 	free(sc, M_DEVBUF);
1164 
1165 	return (0);
1166 }
1167 
1168 static device_method_t emu_pcm_methods[] = {
1169 	DEVMETHOD(device_probe, emu_pcm_probe),
1170 	DEVMETHOD(device_attach, emu_pcm_attach),
1171 	DEVMETHOD(device_detach, emu_pcm_detach),
1172 
1173 	{0, 0}
1174 };
1175 
1176 static driver_t emu_pcm_driver = {
1177 	"pcm",
1178 	emu_pcm_methods,
1179 	PCM_SOFTC_SIZE,
1180 	NULL,
1181 	0,
1182 	NULL
1183 };
1184 DRIVER_MODULE(snd_emu10kx_pcm, emu10kx, emu_pcm_driver, pcm_devclass, 0, 0);
1185 MODULE_DEPEND(snd_emu10kx_pcm, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER);
1186 MODULE_DEPEND(snd_emu10kx_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1187 MODULE_VERSION(snd_emu10kx_pcm, SND_EMU10KX_PREFVER);
1188