xref: /freebsd/sys/dev/sound/pci/emu10k1.c (revision 396c556d77189a5c474d35cec6f44a762e310b7d)
1 /*-
2  * Copyright (c) 2004 David O'Brien <obrien@FreeBSD.org>
3  * Copyright (c) 2003 Orlando Bassotto <orlando.bassotto@ieo-research.it>
4  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifdef HAVE_KERNEL_OPTION_HEADERS
30 #include "opt_snd.h"
31 #endif
32 
33 #include <dev/sound/pcm/sound.h>
34 #include <dev/sound/pcm/ac97.h>
35 #include <dev/sound/pci/emuxkireg.h>
36 
37 #include <dev/pci/pcireg.h>
38 #include <dev/pci/pcivar.h>
39 #include <sys/queue.h>
40 
41 #include <dev/sound/midi/mpu401.h>
42 #include "mpufoi_if.h"
43 
44 SND_DECLARE_FILE("$FreeBSD$");
45 
46 /* -------------------------------------------------------------------- */
47 
48 #define	NUM_G		64	/* use all channels */
49 #define	WAVEOUT_MAXBUFSIZE 32768
50 #define	EMUPAGESIZE	4096	/* don't change */
51 #define	EMUMAXPAGES	(WAVEOUT_MAXBUFSIZE * NUM_G / EMUPAGESIZE)
52 #define	EMU10K1_PCI_ID	0x00021102	/* 1102 => Creative Labs Vendor ID */
53 #define	EMU10K2_PCI_ID	0x00041102
54 #define	EMU10K3_PCI_ID	0x00081102
55 #define	EMU_DEFAULT_BUFSZ	4096
56 #define EMU_MAX_CHANS	8
57 #define	EMU_CHANS	4
58 
59 #define	MAXREQVOICES	8
60 #define	RESERVED	0
61 #define	NUM_MIDI	16
62 #define	NUM_FXSENDS	4
63 
64 #define	TMEMSIZE	256*1024
65 #define	TMEMSIZEREG	4
66 
67 #define	ENABLE		0xffffffff
68 #define	DISABLE		0x00000000
69 #define	ENV_ON		EMU_CHAN_DCYSUSV_CHANNELENABLE_MASK
70 #define	ENV_OFF		0x00	/* XXX: should this be 1? */
71 
72 #define	EMU_A_IOCFG_GPOUT_A	0x40
73 #define	EMU_A_IOCFG_GPOUT_D	0x04
74 #define	EMU_A_IOCFG_GPOUT_AD (EMU_A_IOCFG_GPOUT_A|EMU_A_IOCFG_GPOUT_D)  /* EMU_A_IOCFG_GPOUT0 */
75 
76 #define	EMU_HCFG_GPOUT1		0x00000800
77 
78 /* instruction set */
79 #define iACC3	 0x06
80 #define iMACINT0 0x04
81 #define iINTERP  0x0e
82 
83 #define C_00000000	0x40
84 #define C_00000001	0x41
85 #define C_00000004	0x44
86 #define C_40000000	0x4d
87 /* Audigy constants */
88 #define A_C_00000000	0xc0
89 #define A_C_40000000	0xcd
90 
91 /* GPRs */
92 #define FXBUS(x)	(0x00 + (x))
93 #define EXTIN(x)	(0x10 + (x))
94 #define EXTOUT(x)	(0x20 + (x))
95 
96 #define GPR(x)		(EMU_FXGPREGBASE + (x))
97 #define A_EXTIN(x)	(0x40 + (x))
98 #define A_FXBUS(x)	(0x00 + (x))
99 #define A_EXTOUT(x)	(0x60 + (x))
100 #define A_GPR(x)	(EMU_A_FXGPREGBASE + (x))
101 
102 /* FX buses */
103 #define FXBUS_PCM_LEFT		0x00
104 #define FXBUS_PCM_RIGHT		0x01
105 #define FXBUS_MIDI_LEFT		0x04
106 #define FXBUS_MIDI_RIGHT	0x05
107 #define FXBUS_MIDI_REVERB	0x0c
108 #define FXBUS_MIDI_CHORUS	0x0d
109 
110 /* Inputs */
111 #define EXTIN_AC97_L		0x00
112 #define EXTIN_AC97_R		0x01
113 #define EXTIN_SPDIF_CD_L	0x02
114 #define EXTIN_SPDIF_CD_R	0x03
115 #define EXTIN_TOSLINK_L		0x06
116 #define EXTIN_TOSLINK_R		0x07
117 #define EXTIN_COAX_SPDIF_L	0x0a
118 #define EXTIN_COAX_SPDIF_R	0x0b
119 /* Audigy Inputs */
120 #define A_EXTIN_AC97_L		0x00
121 #define A_EXTIN_AC97_R		0x01
122 
123 /* Outputs */
124 #define EXTOUT_AC97_L	   0x00
125 #define EXTOUT_AC97_R	   0x01
126 #define EXTOUT_TOSLINK_L   0x02
127 #define EXTOUT_TOSLINK_R   0x03
128 #define EXTOUT_AC97_CENTER 0x04
129 #define EXTOUT_AC97_LFE	   0x05
130 #define EXTOUT_HEADPHONE_L 0x06
131 #define EXTOUT_HEADPHONE_R 0x07
132 #define EXTOUT_REAR_L	   0x08
133 #define EXTOUT_REAR_R	   0x09
134 #define EXTOUT_ADC_CAP_L   0x0a
135 #define EXTOUT_ADC_CAP_R   0x0b
136 #define EXTOUT_ACENTER	   0x11
137 #define EXTOUT_ALFE	   0x12
138 /* Audigy Outputs */
139 #define A_EXTOUT_FRONT_L	0x00
140 #define A_EXTOUT_FRONT_R	0x01
141 #define A_EXTOUT_CENTER		0x02
142 #define A_EXTOUT_LFE		0x03
143 #define A_EXTOUT_HEADPHONE_L	0x04
144 #define A_EXTOUT_HEADPHONE_R	0x05
145 #define A_EXTOUT_REAR_L		0x06
146 #define A_EXTOUT_REAR_R		0x07
147 #define A_EXTOUT_AFRONT_L	0x08
148 #define A_EXTOUT_AFRONT_R	0x09
149 #define A_EXTOUT_ACENTER	0x0a
150 #define A_EXTOUT_ALFE		0x0b
151 #define A_EXTOUT_AREAR_L	0x0e
152 #define A_EXTOUT_AREAR_R	0x0f
153 #define A_EXTOUT_AC97_L		0x10
154 #define A_EXTOUT_AC97_R		0x11
155 #define A_EXTOUT_ADC_CAP_L	0x16
156 #define A_EXTOUT_ADC_CAP_R	0x17
157 
158 struct emu_memblk {
159 	SLIST_ENTRY(emu_memblk) link;
160 	void *buf;
161 	bus_addr_t buf_addr;
162 	u_int32_t pte_start, pte_size;
163 	bus_dmamap_t buf_map;
164 };
165 
166 struct emu_mem {
167 	u_int8_t bmap[EMUMAXPAGES / 8];
168 	u_int32_t *ptb_pages;
169 	void *silent_page;
170 	bus_addr_t silent_page_addr;
171 	bus_addr_t ptb_pages_addr;
172 	bus_dmamap_t ptb_map;
173 	bus_dmamap_t silent_map;
174 	SLIST_HEAD(, emu_memblk) blocks;
175 };
176 
177 struct emu_voice {
178 	int vnum;
179 	unsigned int b16:1, stereo:1, busy:1, running:1, ismaster:1;
180 	int speed;
181 	int start, end, vol;
182 	int fxrt1;	/* FX routing */
183 	int fxrt2;	/* FX routing (only for audigy) */
184 	u_int32_t buf;
185 	struct emu_voice *slave;
186 	struct pcm_channel *channel;
187 };
188 
189 struct sc_info;
190 
191 /* channel registers */
192 struct sc_pchinfo {
193 	int spd, fmt, blksz, run;
194 	struct emu_voice *master, *slave;
195 	struct snd_dbuf *buffer;
196 	struct pcm_channel *channel;
197 	struct sc_info *parent;
198 };
199 
200 struct sc_rchinfo {
201 	int spd, fmt, run, blksz, num;
202 	u_int32_t idxreg, basereg, sizereg, setupreg, irqmask;
203 	struct snd_dbuf *buffer;
204 	struct pcm_channel *channel;
205 	struct sc_info *parent;
206 };
207 
208 /* device private data */
209 struct sc_info {
210 	device_t	dev;
211 	u_int32_t	type, rev;
212 	u_int32_t	tos_link:1, APS:1, audigy:1, audigy2:1;
213 	u_int32_t	addrmask;	/* wider if audigy */
214 
215 	bus_space_tag_t st;
216 	bus_space_handle_t sh;
217 	bus_dma_tag_t parent_dmat;
218 
219 	struct resource *reg, *irq;
220 	void		*ih;
221 	struct mtx	*lock;
222 
223 	unsigned int bufsz;
224 	int timer, timerinterval;
225 	int pnum, rnum;
226 	int nchans;
227 	struct emu_mem mem;
228 	struct emu_voice voice[64];
229 	struct sc_pchinfo pch[EMU_MAX_CHANS];
230 	struct sc_rchinfo rch[3];
231 	struct mpu401   *mpu;
232 	mpu401_intr_t           *mpu_intr;
233 	int mputx;
234 };
235 
236 /* -------------------------------------------------------------------- */
237 
238 /*
239  * prototypes
240  */
241 
242 /* stuff */
243 static int emu_init(struct sc_info *);
244 static void emu_intr(void *);
245 static void *emu_malloc(struct sc_info *sc, u_int32_t sz, bus_addr_t *addr, bus_dmamap_t *map);
246 static void *emu_memalloc(struct sc_info *sc, u_int32_t sz, bus_addr_t *addr);
247 static int emu_memfree(struct sc_info *sc, void *buf);
248 static int emu_memstart(struct sc_info *sc, void *buf);
249 #ifdef EMUDEBUG
250 static void emu_vdump(struct sc_info *sc, struct emu_voice *v);
251 #endif
252 
253 /* talk to the card */
254 static u_int32_t emu_rd(struct sc_info *, int, int);
255 static void emu_wr(struct sc_info *, int, u_int32_t, int);
256 
257 /* -------------------------------------------------------------------- */
258 
259 static u_int32_t emu_rfmt_ac97[] = {
260 	SND_FORMAT(AFMT_S16_LE, 1, 0),
261 	SND_FORMAT(AFMT_S16_LE, 2, 0),
262 	0
263 };
264 
265 static u_int32_t emu_rfmt_mic[] = {
266 	SND_FORMAT(AFMT_U8, 1, 0),
267 	0
268 };
269 
270 static u_int32_t emu_rfmt_efx[] = {
271 	SND_FORMAT(AFMT_S16_LE, 2, 0),
272 	0
273 };
274 
275 static struct pcmchan_caps emu_reccaps[3] = {
276 	{8000, 48000, emu_rfmt_ac97, 0},
277 	{8000, 8000, emu_rfmt_mic, 0},
278 	{48000, 48000, emu_rfmt_efx, 0},
279 };
280 
281 static u_int32_t emu_pfmt[] = {
282 	SND_FORMAT(AFMT_U8, 1, 0),
283 	SND_FORMAT(AFMT_U8, 2, 0),
284 	SND_FORMAT(AFMT_S16_LE, 1, 0),
285 	SND_FORMAT(AFMT_S16_LE, 2, 0),
286 	0
287 };
288 
289 static struct pcmchan_caps emu_playcaps = {4000, 48000, emu_pfmt, 0};
290 
291 static int adcspeed[8] = {48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000};
292 /* audigy supports 12kHz. */
293 static int audigy_adcspeed[9] = {
294 	48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000
295 };
296 
297 /* -------------------------------------------------------------------- */
298 /* Hardware */
299 static u_int32_t
300 emu_rd(struct sc_info *sc, int regno, int size)
301 {
302 	switch (size) {
303 	case 1:
304 		return bus_space_read_1(sc->st, sc->sh, regno);
305 	case 2:
306 		return bus_space_read_2(sc->st, sc->sh, regno);
307 	case 4:
308 		return bus_space_read_4(sc->st, sc->sh, regno);
309 	default:
310 		return 0xffffffff;
311 	}
312 }
313 
314 static void
315 emu_wr(struct sc_info *sc, int regno, u_int32_t data, int size)
316 {
317 	switch (size) {
318 	case 1:
319 		bus_space_write_1(sc->st, sc->sh, regno, data);
320 		break;
321 	case 2:
322 		bus_space_write_2(sc->st, sc->sh, regno, data);
323 		break;
324 	case 4:
325 		bus_space_write_4(sc->st, sc->sh, regno, data);
326 		break;
327 	}
328 }
329 
330 static u_int32_t
331 emu_rdptr(struct sc_info *sc, int chn, int reg)
332 {
333 	u_int32_t ptr, val, mask, size, offset;
334 
335 	ptr = ((reg << 16) & sc->addrmask) | (chn & EMU_PTR_CHNO_MASK);
336 	emu_wr(sc, EMU_PTR, ptr, 4);
337 	val = emu_rd(sc, EMU_DATA, 4);
338 	if (reg & 0xff000000) {
339 		size = (reg >> 24) & 0x3f;
340 		offset = (reg >> 16) & 0x1f;
341 		mask = ((1 << size) - 1) << offset;
342 		val &= mask;
343 		val >>= offset;
344 	}
345 	return val;
346 }
347 
348 static void
349 emu_wrptr(struct sc_info *sc, int chn, int reg, u_int32_t data)
350 {
351 	u_int32_t ptr, mask, size, offset;
352 
353 	ptr = ((reg << 16) & sc->addrmask) | (chn & EMU_PTR_CHNO_MASK);
354 	emu_wr(sc, EMU_PTR, ptr, 4);
355 	if (reg & 0xff000000) {
356 		size = (reg >> 24) & 0x3f;
357 		offset = (reg >> 16) & 0x1f;
358 		mask = ((1 << size) - 1) << offset;
359 		data <<= offset;
360 		data &= mask;
361 		data |= emu_rd(sc, EMU_DATA, 4) & ~mask;
362 	}
363 	emu_wr(sc, EMU_DATA, data, 4);
364 }
365 
366 static void
367 emu_wrefx(struct sc_info *sc, unsigned int pc, unsigned int data)
368 {
369 	pc += sc->audigy ? EMU_A_MICROCODEBASE : EMU_MICROCODEBASE;
370 	emu_wrptr(sc, 0, pc, data);
371 }
372 
373 /* -------------------------------------------------------------------- */
374 /* ac97 codec */
375 /* no locking needed */
376 
377 static int
378 emu_rdcd(kobj_t obj, void *devinfo, int regno)
379 {
380 	struct sc_info *sc = (struct sc_info *)devinfo;
381 
382 	emu_wr(sc, EMU_AC97ADDR, regno, 1);
383 	return emu_rd(sc, EMU_AC97DATA, 2);
384 }
385 
386 static int
387 emu_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
388 {
389 	struct sc_info *sc = (struct sc_info *)devinfo;
390 
391 	emu_wr(sc, EMU_AC97ADDR, regno, 1);
392 	emu_wr(sc, EMU_AC97DATA, data, 2);
393 	return 0;
394 }
395 
396 static kobj_method_t emu_ac97_methods[] = {
397 	KOBJMETHOD(ac97_read,		emu_rdcd),
398 	KOBJMETHOD(ac97_write,		emu_wrcd),
399 	KOBJMETHOD_END
400 };
401 AC97_DECLARE(emu_ac97);
402 
403 /* -------------------------------------------------------------------- */
404 /* stuff */
405 static int
406 emu_settimer(struct sc_info *sc)
407 {
408 	struct sc_pchinfo *pch;
409 	struct sc_rchinfo *rch;
410 	int i, tmp, rate;
411 
412 	rate = 0;
413 	for (i = 0; i < sc->nchans; i++) {
414 		pch = &sc->pch[i];
415 		if (pch->buffer) {
416 			tmp = (pch->spd * sndbuf_getalign(pch->buffer))
417 			    / pch->blksz;
418 			if (tmp > rate)
419 				rate = tmp;
420 		}
421 	}
422 
423 	for (i = 0; i < 3; i++) {
424 		rch = &sc->rch[i];
425 		if (rch->buffer) {
426 			tmp = (rch->spd * sndbuf_getalign(rch->buffer))
427 			    / rch->blksz;
428 			if (tmp > rate)
429 				rate = tmp;
430 		}
431 	}
432 	RANGE(rate, 48, 9600);
433 	sc->timerinterval = 48000 / rate;
434 	emu_wr(sc, EMU_TIMER, sc->timerinterval & 0x03ff, 2);
435 
436 	return sc->timerinterval;
437 }
438 
439 static int
440 emu_enatimer(struct sc_info *sc, int go)
441 {
442 	u_int32_t x;
443 	if (go) {
444 		if (sc->timer++ == 0) {
445 			x = emu_rd(sc, EMU_INTE, 4);
446 			x |= EMU_INTE_INTERTIMERENB;
447 			emu_wr(sc, EMU_INTE, x, 4);
448 		}
449 	} else {
450 		sc->timer = 0;
451 		x = emu_rd(sc, EMU_INTE, 4);
452 		x &= ~EMU_INTE_INTERTIMERENB;
453 		emu_wr(sc, EMU_INTE, x, 4);
454 	}
455 	return 0;
456 }
457 
458 static void
459 emu_enastop(struct sc_info *sc, char channel, int enable)
460 {
461 	int reg = (channel & 0x20) ? EMU_SOLEH : EMU_SOLEL;
462 	channel &= 0x1f;
463 	reg |= 1 << 24;
464 	reg |= channel << 16;
465 	emu_wrptr(sc, 0, reg, enable);
466 }
467 
468 static int
469 emu_recval(int speed) {
470 	int val;
471 
472 	val = 0;
473 	while (val < 7 && speed < adcspeed[val])
474 		val++;
475 	return val;
476 }
477 
478 static int
479 audigy_recval(int speed) {
480 	int val;
481 
482 	val = 0;
483 	while (val < 8 && speed < audigy_adcspeed[val])
484 		val++;
485 	return val;
486 }
487 
488 static u_int32_t
489 emu_rate_to_pitch(u_int32_t rate)
490 {
491 	static u_int32_t logMagTable[128] = {
492 		0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 0x13aa2,
493 		0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 0x2655d, 0x28ed5,
494 		0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 0x381b6, 0x3a93d, 0x3d081,
495 		0x3f782, 0x41e42, 0x444c1, 0x46b01, 0x49101, 0x4b6c4, 0x4dc49, 0x50191,
496 		0x5269e, 0x54b6f, 0x57006, 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7,
497 		0x646ee, 0x66a00, 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829,
498 		0x759d4, 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
499 		0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 0x93d26,
500 		0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 0xa11d8, 0xa2f9d,
501 		0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 0xadf26, 0xafbe7, 0xb1885,
502 		0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899,
503 		0xc1404, 0xc2f50, 0xc4a7b, 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c,
504 		0xceaec, 0xd053f, 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3,
505 		0xdba4a, 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
506 		0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 0xf2c83,
507 		0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 0xfd1a7, 0xfe8df
508 	};
509 	static char logSlopeTable[128] = {
510 		0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
511 		0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
512 		0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
513 		0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
514 		0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
515 		0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
516 		0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
517 		0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
518 		0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
519 		0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
520 		0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
521 		0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
522 		0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
523 		0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
524 		0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
525 		0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
526 	};
527 	int i;
528 
529 	if (rate == 0)
530 		return 0;	/* Bail out if no leading "1" */
531 	rate *= 11185;	/* Scale 48000 to 0x20002380 */
532 	for (i = 31; i > 0; i--) {
533 		if (rate & 0x80000000) {	/* Detect leading "1" */
534 			return (((u_int32_t) (i - 15) << 20) +
535 			    logMagTable[0x7f & (rate >> 24)] +
536 			    (0x7f & (rate >> 17)) *
537 			    logSlopeTable[0x7f & (rate >> 24)]);
538 		}
539 		rate <<= 1;
540 	}
541 
542 	return 0;		/* Should never reach this point */
543 }
544 
545 static u_int32_t
546 emu_rate_to_linearpitch(u_int32_t rate)
547 {
548 	rate = (rate << 8) / 375;
549 	return (rate >> 1) + (rate & 1);
550 }
551 
552 static struct emu_voice *
553 emu_valloc(struct sc_info *sc)
554 {
555 	struct emu_voice *v;
556 	int i;
557 
558 	v = NULL;
559 	for (i = 0; i < 64 && sc->voice[i].busy; i++);
560 	if (i < 64) {
561 		v = &sc->voice[i];
562 		v->busy = 1;
563 	}
564 	return v;
565 }
566 
567 static int
568 emu_vinit(struct sc_info *sc, struct emu_voice *m, struct emu_voice *s,
569 	  u_int32_t sz, struct snd_dbuf *b)
570 {
571 	void *buf;
572 	bus_addr_t tmp_addr;
573 
574 	buf = emu_memalloc(sc, sz, &tmp_addr);
575 	if (buf == NULL)
576 		return -1;
577 	if (b != NULL)
578 		sndbuf_setup(b, buf, sz);
579 	m->start = emu_memstart(sc, buf) * EMUPAGESIZE;
580 	m->end = m->start + sz;
581 	m->channel = NULL;
582 	m->speed = 0;
583 	m->b16 = 0;
584 	m->stereo = 0;
585 	m->running = 0;
586 	m->ismaster = 1;
587 	m->vol = 0xff;
588 	m->buf = tmp_addr;
589 	m->slave = s;
590 	if (sc->audigy) {
591 		m->fxrt1 = FXBUS_MIDI_CHORUS | FXBUS_PCM_RIGHT << 8 |
592 		    FXBUS_PCM_LEFT << 16 | FXBUS_MIDI_REVERB << 24;
593 		m->fxrt2 = 0x3f3f3f3f;	/* No effects on second route */
594 	} else {
595 		m->fxrt1 = FXBUS_MIDI_CHORUS | FXBUS_PCM_RIGHT << 4 |
596 		    FXBUS_PCM_LEFT << 8 | FXBUS_MIDI_REVERB << 12;
597 		m->fxrt2 = 0;
598 	}
599 
600 	if (s != NULL) {
601 		s->start = m->start;
602 		s->end = m->end;
603 		s->channel = NULL;
604 		s->speed = 0;
605 		s->b16 = 0;
606 		s->stereo = 0;
607 		s->running = 0;
608 		s->ismaster = 0;
609 		s->vol = m->vol;
610 		s->buf = m->buf;
611 		s->fxrt1 = m->fxrt1;
612 		s->fxrt2 = m->fxrt2;
613 		s->slave = NULL;
614 	}
615 	return 0;
616 }
617 
618 static void
619 emu_vsetup(struct sc_pchinfo *ch)
620 {
621 	struct emu_voice *v = ch->master;
622 
623 	if (ch->fmt) {
624 		v->b16 = (ch->fmt & AFMT_16BIT) ? 1 : 0;
625 		v->stereo = (AFMT_CHANNEL(ch->fmt) > 1) ? 1 : 0;
626 		if (v->slave != NULL) {
627 			v->slave->b16 = v->b16;
628 			v->slave->stereo = v->stereo;
629 		}
630 	}
631 	if (ch->spd) {
632 		v->speed = ch->spd;
633 		if (v->slave != NULL)
634 			v->slave->speed = v->speed;
635 	}
636 }
637 
638 static void
639 emu_vwrite(struct sc_info *sc, struct emu_voice *v)
640 {
641 	int s;
642 	int l, r, x, y;
643 	u_int32_t sa, ea, start, val, silent_page;
644 
645 	s = (v->stereo ? 1 : 0) + (v->b16 ? 1 : 0);
646 
647 	sa = v->start >> s;
648 	ea = v->end >> s;
649 
650 	l = r = x = y = v->vol;
651 	if (v->stereo) {
652 		l = v->ismaster ? l : 0;
653 		r = v->ismaster ? 0 : r;
654 	}
655 
656 	emu_wrptr(sc, v->vnum, EMU_CHAN_CPF, v->stereo ? EMU_CHAN_CPF_STEREO_MASK : 0);
657 	val = v->stereo ? 28 : 30;
658 	val *= v->b16 ? 1 : 2;
659 	start = sa + val;
660 
661 	if (sc->audigy) {
662 		emu_wrptr(sc, v->vnum, EMU_A_CHAN_FXRT1, v->fxrt1);
663 		emu_wrptr(sc, v->vnum, EMU_A_CHAN_FXRT2, v->fxrt2);
664 		emu_wrptr(sc, v->vnum, EMU_A_CHAN_SENDAMOUNTS, 0);
665 	}
666 	else
667 		emu_wrptr(sc, v->vnum, EMU_CHAN_FXRT, v->fxrt1 << 16);
668 
669 	emu_wrptr(sc, v->vnum, EMU_CHAN_PTRX, (x << 8) | r);
670 	emu_wrptr(sc, v->vnum, EMU_CHAN_DSL, ea | (y << 24));
671 	emu_wrptr(sc, v->vnum, EMU_CHAN_PSST, sa | (l << 24));
672 	emu_wrptr(sc, v->vnum, EMU_CHAN_CCCA, start | (v->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT));
673 
674 	emu_wrptr(sc, v->vnum, EMU_CHAN_Z1, 0);
675 	emu_wrptr(sc, v->vnum, EMU_CHAN_Z2, 0);
676 
677 	silent_page = ((u_int32_t)(sc->mem.silent_page_addr) << 1)
678 	    | EMU_CHAN_MAP_PTI_MASK;
679 	emu_wrptr(sc, v->vnum, EMU_CHAN_MAPA, silent_page);
680 	emu_wrptr(sc, v->vnum, EMU_CHAN_MAPB, silent_page);
681 
682 	emu_wrptr(sc, v->vnum, EMU_CHAN_CVCF, EMU_CHAN_CVCF_CURRFILTER_MASK);
683 	emu_wrptr(sc, v->vnum, EMU_CHAN_VTFT, EMU_CHAN_VTFT_FILTERTARGET_MASK);
684 	emu_wrptr(sc, v->vnum, EMU_CHAN_ATKHLDM, 0);
685 	emu_wrptr(sc, v->vnum, EMU_CHAN_DCYSUSM, EMU_CHAN_DCYSUSM_DECAYTIME_MASK);
686 	emu_wrptr(sc, v->vnum, EMU_CHAN_LFOVAL1, 0x8000);
687 	emu_wrptr(sc, v->vnum, EMU_CHAN_LFOVAL2, 0x8000);
688 	emu_wrptr(sc, v->vnum, EMU_CHAN_FMMOD, 0);
689 	emu_wrptr(sc, v->vnum, EMU_CHAN_TREMFRQ, 0);
690 	emu_wrptr(sc, v->vnum, EMU_CHAN_FM2FRQ2, 0);
691 	emu_wrptr(sc, v->vnum, EMU_CHAN_ENVVAL, 0x8000);
692 
693 	emu_wrptr(sc, v->vnum, EMU_CHAN_ATKHLDV,
694 	    EMU_CHAN_ATKHLDV_HOLDTIME_MASK | EMU_CHAN_ATKHLDV_ATTACKTIME_MASK);
695 	emu_wrptr(sc, v->vnum, EMU_CHAN_ENVVOL, 0x8000);
696 
697 	emu_wrptr(sc, v->vnum, EMU_CHAN_PEFE_FILTERAMOUNT, 0x7f);
698 	emu_wrptr(sc, v->vnum, EMU_CHAN_PEFE_PITCHAMOUNT, 0);
699 
700 	if (v->slave != NULL)
701 		emu_vwrite(sc, v->slave);
702 }
703 
704 static void
705 emu_vtrigger(struct sc_info *sc, struct emu_voice *v, int go)
706 {
707 	u_int32_t pitch_target, initial_pitch;
708 	u_int32_t cra, cs, ccis;
709 	u_int32_t sample, i;
710 
711 	if (go) {
712 		cra = 64;
713 		cs = v->stereo ? 4 : 2;
714 		ccis = v->stereo ? 28 : 30;
715 		ccis *= v->b16 ? 1 : 2;
716 		sample = v->b16 ? 0x00000000 : 0x80808080;
717 
718 		for (i = 0; i < cs; i++)
719 			emu_wrptr(sc, v->vnum, EMU_CHAN_CD0 + i, sample);
720 		emu_wrptr(sc, v->vnum, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0);
721 		emu_wrptr(sc, v->vnum, EMU_CHAN_CCR_READADDRESS, cra);
722 		emu_wrptr(sc, v->vnum, EMU_CHAN_CCR_CACHEINVALIDSIZE, ccis);
723 
724 		emu_wrptr(sc, v->vnum, EMU_CHAN_IFATN, 0xff00);
725 		emu_wrptr(sc, v->vnum, EMU_CHAN_VTFT, 0xffffffff);
726 		emu_wrptr(sc, v->vnum, EMU_CHAN_CVCF, 0xffffffff);
727 		emu_wrptr(sc, v->vnum, EMU_CHAN_DCYSUSV, 0x00007f7f);
728 		emu_enastop(sc, v->vnum, 0);
729 
730 		pitch_target = emu_rate_to_linearpitch(v->speed);
731 		initial_pitch = emu_rate_to_pitch(v->speed) >> 8;
732 		emu_wrptr(sc, v->vnum, EMU_CHAN_PTRX_PITCHTARGET, pitch_target);
733 		emu_wrptr(sc, v->vnum, EMU_CHAN_CPF_PITCH, pitch_target);
734 		emu_wrptr(sc, v->vnum, EMU_CHAN_IP, initial_pitch);
735 	} else {
736 		emu_wrptr(sc, v->vnum, EMU_CHAN_PTRX_PITCHTARGET, 0);
737 		emu_wrptr(sc, v->vnum, EMU_CHAN_CPF_PITCH, 0);
738 		emu_wrptr(sc, v->vnum, EMU_CHAN_IFATN, 0xffff);
739 		emu_wrptr(sc, v->vnum, EMU_CHAN_VTFT, 0x0000ffff);
740 		emu_wrptr(sc, v->vnum, EMU_CHAN_CVCF, 0x0000ffff);
741 		emu_wrptr(sc, v->vnum, EMU_CHAN_IP, 0);
742 		emu_enastop(sc, v->vnum, 1);
743 	}
744 	if (v->slave != NULL)
745 		emu_vtrigger(sc, v->slave, go);
746 }
747 
748 static int
749 emu_vpos(struct sc_info *sc, struct emu_voice *v)
750 {
751 	int s, ptr;
752 
753 	s = (v->b16 ? 1 : 0) + (v->stereo ? 1 : 0);
754 	ptr = (emu_rdptr(sc, v->vnum, EMU_CHAN_CCCA_CURRADDR) - (v->start >> s)) << s;
755 	return ptr & ~0x0000001f;
756 }
757 
758 #ifdef EMUDEBUG
759 static void
760 emu_vdump(struct sc_info *sc, struct emu_voice *v)
761 {
762 	char *regname[] = {
763 		"cpf", "ptrx", "cvcf", "vtft", "z2", "z1", "psst", "dsl",
764 		"ccca", "ccr", "clp", "fxrt", "mapa", "mapb", NULL, NULL,
765 		"envvol", "atkhldv", "dcysusv", "lfoval1",
766 		"envval", "atkhldm", "dcysusm", "lfoval2",
767 		"ip", "ifatn", "pefe", "fmmod", "tremfrq", "fmfrq2",
768 		"tempenv"
769 	};
770 	char *regname2[] = {
771 		"mudata1", "mustat1", "mudata2", "mustat2",
772 		"fxwc1", "fxwc2", "spdrate", NULL, NULL,
773 		NULL, NULL, NULL, "fxrt2", "sndamnt", "fxrt1",
774 		NULL, NULL
775 	};
776 	int i, x;
777 
778 	printf("voice number %d\n", v->vnum);
779 	for (i = 0, x = 0; i <= 0x1e; i++) {
780 		if (regname[i] == NULL)
781 			continue;
782 		printf("%s\t[%08x]", regname[i], emu_rdptr(sc, v->vnum, i));
783 		printf("%s", (x == 2) ? "\n" : "\t");
784 		x++;
785 		if (x > 2)
786 			x = 0;
787 	}
788 
789 	/* Print out audigy extra registers */
790 	if (sc->audigy) {
791 		for (i = 0; i <= 0xe; i++) {
792 			if (regname2[i] == NULL)
793 				continue;
794 			printf("%s\t[%08x]", regname2[i],
795 			    emu_rdptr(sc, v->vnum, i + 0x70));
796 			printf("%s", (x == 2)? "\n" : "\t");
797 			x++;
798 			if (x > 2)
799 				x = 0;
800 		}
801 	}
802 	printf("\n\n");
803 }
804 #endif
805 
806 /* channel interface */
807 static void *
808 emupchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
809     struct pcm_channel *c, int dir)
810 {
811 	struct sc_info *sc = devinfo;
812 	struct sc_pchinfo *ch;
813 	void *r;
814 
815 	KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction"));
816 	ch = &sc->pch[sc->pnum++];
817 	ch->buffer = b;
818 	ch->parent = sc;
819 	ch->channel = c;
820 	ch->blksz = sc->bufsz / 2;
821 	ch->fmt = SND_FORMAT(AFMT_U8, 1, 0);
822 	ch->spd = 8000;
823 	snd_mtxlock(sc->lock);
824 	ch->master = emu_valloc(sc);
825 	ch->slave = emu_valloc(sc);
826 	snd_mtxunlock(sc->lock);
827 	r = (emu_vinit(sc, ch->master, ch->slave, sc->bufsz, ch->buffer))
828 	    ? NULL : ch;
829 
830 	return r;
831 }
832 
833 static int
834 emupchan_free(kobj_t obj, void *data)
835 {
836 	struct sc_pchinfo *ch = data;
837 	struct sc_info *sc = ch->parent;
838 	int r;
839 
840 	snd_mtxlock(sc->lock);
841 	r = emu_memfree(sc, sndbuf_getbuf(ch->buffer));
842 	snd_mtxunlock(sc->lock);
843 
844 	return r;
845 }
846 
847 static int
848 emupchan_setformat(kobj_t obj, void *data, u_int32_t format)
849 {
850 	struct sc_pchinfo *ch = data;
851 
852 	ch->fmt = format;
853 	return 0;
854 }
855 
856 static u_int32_t
857 emupchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
858 {
859 	struct sc_pchinfo *ch = data;
860 
861 	ch->spd = speed;
862 	return ch->spd;
863 }
864 
865 static u_int32_t
866 emupchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
867 {
868 	struct sc_pchinfo *ch = data;
869 	struct sc_info *sc = ch->parent;
870 	int irqrate, blksz;
871 
872 	ch->blksz = blocksize;
873 	snd_mtxlock(sc->lock);
874 	emu_settimer(sc);
875 	irqrate = 48000 / sc->timerinterval;
876 	snd_mtxunlock(sc->lock);
877 	blksz = (ch->spd * sndbuf_getalign(ch->buffer)) / irqrate;
878 	return blocksize;
879 }
880 
881 static int
882 emupchan_trigger(kobj_t obj, void *data, int go)
883 {
884 	struct sc_pchinfo *ch = data;
885 	struct sc_info *sc = ch->parent;
886 
887 	if (!PCMTRIG_COMMON(go))
888 		return 0;
889 
890 	snd_mtxlock(sc->lock);
891 	if (go == PCMTRIG_START) {
892 		emu_vsetup(ch);
893 		emu_vwrite(sc, ch->master);
894 		emu_settimer(sc);
895 		emu_enatimer(sc, 1);
896 #ifdef EMUDEBUG
897 		printf("start [%d bit, %s, %d hz]\n",
898 			ch->master->b16 ? 16 : 8,
899 			ch->master->stereo ? "stereo" : "mono",
900 			ch->master->speed);
901 		emu_vdump(sc, ch->master);
902 		emu_vdump(sc, ch->slave);
903 #endif
904 	}
905 	ch->run = (go == PCMTRIG_START) ? 1 : 0;
906 	emu_vtrigger(sc, ch->master, ch->run);
907 	snd_mtxunlock(sc->lock);
908 	return 0;
909 }
910 
911 static u_int32_t
912 emupchan_getptr(kobj_t obj, void *data)
913 {
914 	struct sc_pchinfo *ch = data;
915 	struct sc_info *sc = ch->parent;
916 	int r;
917 
918 	snd_mtxlock(sc->lock);
919 	r = emu_vpos(sc, ch->master);
920 	snd_mtxunlock(sc->lock);
921 
922 	return r;
923 }
924 
925 static struct pcmchan_caps *
926 emupchan_getcaps(kobj_t obj, void *data)
927 {
928 	return &emu_playcaps;
929 }
930 
931 static kobj_method_t emupchan_methods[] = {
932 	KOBJMETHOD(channel_init,		emupchan_init),
933 	KOBJMETHOD(channel_free,		emupchan_free),
934 	KOBJMETHOD(channel_setformat,		emupchan_setformat),
935 	KOBJMETHOD(channel_setspeed,		emupchan_setspeed),
936 	KOBJMETHOD(channel_setblocksize,	emupchan_setblocksize),
937 	KOBJMETHOD(channel_trigger,		emupchan_trigger),
938 	KOBJMETHOD(channel_getptr,		emupchan_getptr),
939 	KOBJMETHOD(channel_getcaps,		emupchan_getcaps),
940 	KOBJMETHOD_END
941 };
942 CHANNEL_DECLARE(emupchan);
943 
944 /* channel interface */
945 static void *
946 emurchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
947     struct pcm_channel *c, int dir)
948 {
949 	struct sc_info *sc = devinfo;
950 	struct sc_rchinfo *ch;
951 
952 	KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction"));
953 	ch = &sc->rch[sc->rnum];
954 	ch->buffer = b;
955 	ch->parent = sc;
956 	ch->channel = c;
957 	ch->blksz = sc->bufsz / 2;
958 	ch->fmt = SND_FORMAT(AFMT_U8, 1, 0);
959 	ch->spd = 8000;
960 	ch->num = sc->rnum;
961 	switch(sc->rnum) {
962 	case 0:
963 		ch->idxreg = sc->audigy ? EMU_A_ADCIDX : EMU_ADCIDX;
964 		ch->basereg = EMU_ADCBA;
965 		ch->sizereg = EMU_ADCBS;
966 		ch->setupreg = EMU_ADCCR;
967 		ch->irqmask = EMU_INTE_ADCBUFENABLE;
968 		break;
969 
970 	case 1:
971 		ch->idxreg = EMU_FXIDX;
972 		ch->basereg = EMU_FXBA;
973 		ch->sizereg = EMU_FXBS;
974 		ch->setupreg = EMU_FXWC;
975 		ch->irqmask = EMU_INTE_EFXBUFENABLE;
976 		break;
977 
978 	case 2:
979 		ch->idxreg = EMU_MICIDX;
980 		ch->basereg = EMU_MICBA;
981 		ch->sizereg = EMU_MICBS;
982 		ch->setupreg = 0;
983 		ch->irqmask = EMU_INTE_MICBUFENABLE;
984 		break;
985 	}
986 	sc->rnum++;
987 	if (sndbuf_alloc(ch->buffer, sc->parent_dmat, 0, sc->bufsz) != 0)
988 		return NULL;
989 	else {
990 		snd_mtxlock(sc->lock);
991 		emu_wrptr(sc, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer));
992 		emu_wrptr(sc, 0, ch->sizereg, 0); /* off */
993 		snd_mtxunlock(sc->lock);
994 		return ch;
995 	}
996 }
997 
998 static int
999 emurchan_setformat(kobj_t obj, void *data, u_int32_t format)
1000 {
1001 	struct sc_rchinfo *ch = data;
1002 
1003 	ch->fmt = format;
1004 	return 0;
1005 }
1006 
1007 static u_int32_t
1008 emurchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
1009 {
1010 	struct sc_rchinfo *ch = data;
1011 
1012 	if (ch->num == 0) {
1013 		if (ch->parent->audigy)
1014 			speed = audigy_adcspeed[audigy_recval(speed)];
1015 		else
1016 			speed = adcspeed[emu_recval(speed)];
1017 	}
1018 	if (ch->num == 1)
1019 		speed = 48000;
1020 	if (ch->num == 2)
1021 		speed = 8000;
1022 	ch->spd = speed;
1023 	return ch->spd;
1024 }
1025 
1026 static u_int32_t
1027 emurchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
1028 {
1029 	struct sc_rchinfo *ch = data;
1030 	struct sc_info *sc = ch->parent;
1031 	int irqrate, blksz;
1032 
1033 	ch->blksz = blocksize;
1034 	snd_mtxlock(sc->lock);
1035 	emu_settimer(sc);
1036 	irqrate = 48000 / sc->timerinterval;
1037 	snd_mtxunlock(sc->lock);
1038 	blksz = (ch->spd * sndbuf_getalign(ch->buffer)) / irqrate;
1039 	return blocksize;
1040 }
1041 
1042 /* semantic note: must start at beginning of buffer */
1043 static int
1044 emurchan_trigger(kobj_t obj, void *data, int go)
1045 {
1046 	struct sc_rchinfo *ch = data;
1047 	struct sc_info *sc = ch->parent;
1048 	u_int32_t val, sz;
1049 
1050 	if (!PCMTRIG_COMMON(go))
1051 		return 0;
1052 
1053 	switch(sc->bufsz) {
1054 	case 4096:
1055 		sz = EMU_RECBS_BUFSIZE_4096;
1056 		break;
1057 
1058 	case 8192:
1059 		sz = EMU_RECBS_BUFSIZE_8192;
1060 		break;
1061 
1062 	case 16384:
1063 		sz = EMU_RECBS_BUFSIZE_16384;
1064 		break;
1065 
1066 	case 32768:
1067 		sz = EMU_RECBS_BUFSIZE_32768;
1068 		break;
1069 
1070 	case 65536:
1071 		sz = EMU_RECBS_BUFSIZE_65536;
1072 		break;
1073 
1074 	default:
1075 		sz = EMU_RECBS_BUFSIZE_4096;
1076 	}
1077 
1078 	snd_mtxlock(sc->lock);
1079 	switch(go) {
1080 	case PCMTRIG_START:
1081 		ch->run = 1;
1082 		emu_wrptr(sc, 0, ch->sizereg, sz);
1083 		if (ch->num == 0) {
1084 			if (sc->audigy) {
1085 				val = EMU_A_ADCCR_LCHANENABLE;
1086 				if (AFMT_CHANNEL(ch->fmt) > 1)
1087 					val |= EMU_A_ADCCR_RCHANENABLE;
1088 				val |= audigy_recval(ch->spd);
1089 			} else {
1090 				val = EMU_ADCCR_LCHANENABLE;
1091 				if (AFMT_CHANNEL(ch->fmt) > 1)
1092 					val |= EMU_ADCCR_RCHANENABLE;
1093 				val |= emu_recval(ch->spd);
1094 			}
1095 
1096 			emu_wrptr(sc, 0, ch->setupreg, 0);
1097 			emu_wrptr(sc, 0, ch->setupreg, val);
1098 		}
1099 		val = emu_rd(sc, EMU_INTE, 4);
1100 		val |= ch->irqmask;
1101 		emu_wr(sc, EMU_INTE, val, 4);
1102 		break;
1103 
1104 	case PCMTRIG_STOP:
1105 	case PCMTRIG_ABORT:
1106 		ch->run = 0;
1107 		emu_wrptr(sc, 0, ch->sizereg, 0);
1108 		if (ch->setupreg)
1109 			emu_wrptr(sc, 0, ch->setupreg, 0);
1110 		val = emu_rd(sc, EMU_INTE, 4);
1111 		val &= ~ch->irqmask;
1112 		emu_wr(sc, EMU_INTE, val, 4);
1113 		break;
1114 
1115 	case PCMTRIG_EMLDMAWR:
1116 	case PCMTRIG_EMLDMARD:
1117 	default:
1118 		break;
1119 	}
1120 	snd_mtxunlock(sc->lock);
1121 
1122 	return 0;
1123 }
1124 
1125 static u_int32_t
1126 emurchan_getptr(kobj_t obj, void *data)
1127 {
1128 	struct sc_rchinfo *ch = data;
1129 	struct sc_info *sc = ch->parent;
1130 	int r;
1131 
1132 	snd_mtxlock(sc->lock);
1133 	r = emu_rdptr(sc, 0, ch->idxreg) & 0x0000ffff;
1134 	snd_mtxunlock(sc->lock);
1135 
1136 	return r;
1137 }
1138 
1139 static struct pcmchan_caps *
1140 emurchan_getcaps(kobj_t obj, void *data)
1141 {
1142 	struct sc_rchinfo *ch = data;
1143 
1144 	return &emu_reccaps[ch->num];
1145 }
1146 
1147 static kobj_method_t emurchan_methods[] = {
1148 	KOBJMETHOD(channel_init,		emurchan_init),
1149 	KOBJMETHOD(channel_setformat,		emurchan_setformat),
1150 	KOBJMETHOD(channel_setspeed,		emurchan_setspeed),
1151 	KOBJMETHOD(channel_setblocksize,	emurchan_setblocksize),
1152 	KOBJMETHOD(channel_trigger,		emurchan_trigger),
1153 	KOBJMETHOD(channel_getptr,		emurchan_getptr),
1154 	KOBJMETHOD(channel_getcaps,		emurchan_getcaps),
1155 	KOBJMETHOD_END
1156 };
1157 CHANNEL_DECLARE(emurchan);
1158 
1159 static unsigned char
1160 emu_mread(struct mpu401 *arg, void *sc, int reg)
1161 {
1162 	unsigned int d;
1163 
1164 	d = emu_rd((struct sc_info *)sc, 0x18 + reg, 1);
1165 	return d;
1166 }
1167 
1168 static void
1169 emu_mwrite(struct mpu401 *arg, void *sc, int reg, unsigned char b)
1170 {
1171 
1172 	emu_wr((struct sc_info *)sc, 0x18 + reg, b, 1);
1173 }
1174 
1175 static int
1176 emu_muninit(struct mpu401 *arg, void *cookie)
1177 {
1178 	struct sc_info *sc = cookie;
1179 
1180 	snd_mtxlock(sc->lock);
1181 	sc->mpu_intr = NULL;
1182 	snd_mtxunlock(sc->lock);
1183 
1184 	return 0;
1185 }
1186 
1187 static kobj_method_t emu_mpu_methods[] = {
1188     	KOBJMETHOD(mpufoi_read,		emu_mread),
1189     	KOBJMETHOD(mpufoi_write,	emu_mwrite),
1190     	KOBJMETHOD(mpufoi_uninit,	emu_muninit),
1191 	KOBJMETHOD_END
1192 };
1193 
1194 static DEFINE_CLASS(emu_mpu, emu_mpu_methods, 0);
1195 
1196 static void
1197 emu_intr2(void *p)
1198 {
1199 	struct sc_info *sc = (struct sc_info *)p;
1200 
1201 	if (sc->mpu_intr)
1202 	    (sc->mpu_intr)(sc->mpu);
1203 }
1204 
1205 static void
1206 emu_midiattach(struct sc_info *sc)
1207 {
1208 	int i;
1209 
1210 	i = emu_rd(sc, EMU_INTE, 4);
1211 	i |= EMU_INTE_MIDIRXENABLE;
1212 	emu_wr(sc, EMU_INTE, i, 4);
1213 
1214 	sc->mpu = mpu401_init(&emu_mpu_class, sc, emu_intr2, &sc->mpu_intr);
1215 }
1216 /* -------------------------------------------------------------------- */
1217 /* The interrupt handler */
1218 
1219 static void
1220 emu_intr(void *data)
1221 {
1222 	struct sc_info *sc = data;
1223 	u_int32_t stat, ack, i, x;
1224 
1225 	snd_mtxlock(sc->lock);
1226 	while (1) {
1227 		stat = emu_rd(sc, EMU_IPR, 4);
1228 		if (stat == 0)
1229 			break;
1230 		ack = 0;
1231 
1232 		/* process irq */
1233 		if (stat & EMU_IPR_INTERVALTIMER)
1234 			ack |= EMU_IPR_INTERVALTIMER;
1235 
1236 		if (stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL))
1237 			ack |= stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL);
1238 
1239 		if (stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL))
1240 			ack |= stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL);
1241 
1242 		if (stat & (EMU_IPR_MICBUFFULL | EMU_IPR_MICBUFHALFFULL))
1243 			ack |= stat & (EMU_IPR_MICBUFFULL | EMU_IPR_MICBUFHALFFULL);
1244 
1245 		if (stat & EMU_PCIERROR) {
1246 			ack |= EMU_PCIERROR;
1247 			device_printf(sc->dev, "pci error\n");
1248 			/* we still get an nmi with ecc ram even if we ack this */
1249 		}
1250 		if (stat & EMU_IPR_RATETRCHANGE) {
1251 			ack |= EMU_IPR_RATETRCHANGE;
1252 #ifdef EMUDEBUG
1253 			device_printf(sc->dev,
1254 			    "sample rate tracker lock status change\n");
1255 #endif
1256 		}
1257 
1258 	    if (stat & EMU_IPR_MIDIRECVBUFE)
1259 		if (sc->mpu_intr) {
1260 		    (sc->mpu_intr)(sc->mpu);
1261 		    ack |= EMU_IPR_MIDIRECVBUFE | EMU_IPR_MIDITRANSBUFE;
1262  		}
1263 		if (stat & ~ack)
1264 			device_printf(sc->dev, "dodgy irq: %x (harmless)\n",
1265 			    stat & ~ack);
1266 
1267 		emu_wr(sc, EMU_IPR, stat, 4);
1268 
1269 		if (ack) {
1270 			snd_mtxunlock(sc->lock);
1271 
1272 			if (ack & EMU_IPR_INTERVALTIMER) {
1273 				x = 0;
1274 				for (i = 0; i < sc->nchans; i++) {
1275 					if (sc->pch[i].run) {
1276 						x = 1;
1277 						chn_intr(sc->pch[i].channel);
1278 					}
1279 				}
1280 				if (x == 0)
1281 					emu_enatimer(sc, 0);
1282 			}
1283 
1284 
1285 			if (ack & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL)) {
1286 				if (sc->rch[0].channel)
1287 					chn_intr(sc->rch[0].channel);
1288 			}
1289 			if (ack & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL)) {
1290 				if (sc->rch[1].channel)
1291 					chn_intr(sc->rch[1].channel);
1292 			}
1293 			if (ack & (EMU_IPR_MICBUFFULL | EMU_IPR_MICBUFHALFFULL)) {
1294 				if (sc->rch[2].channel)
1295 					chn_intr(sc->rch[2].channel);
1296 			}
1297 
1298 			snd_mtxlock(sc->lock);
1299 		}
1300 	}
1301 	snd_mtxunlock(sc->lock);
1302 }
1303 
1304 /* -------------------------------------------------------------------- */
1305 
1306 static void
1307 emu_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1308 {
1309 	bus_addr_t *phys = arg;
1310 
1311 	*phys = error ? 0 : (bus_addr_t)segs->ds_addr;
1312 
1313 	if (bootverbose) {
1314 		printf("emu: setmap (%lx, %lx), nseg=%d, error=%d\n",
1315 		    (unsigned long)segs->ds_addr, (unsigned long)segs->ds_len,
1316 		    nseg, error);
1317 	}
1318 }
1319 
1320 static void *
1321 emu_malloc(struct sc_info *sc, u_int32_t sz, bus_addr_t *addr,
1322     bus_dmamap_t *map)
1323 {
1324 	void *buf;
1325 
1326 	*addr = 0;
1327 	if (bus_dmamem_alloc(sc->parent_dmat, &buf, BUS_DMA_NOWAIT, map))
1328 		return NULL;
1329 	if (bus_dmamap_load(sc->parent_dmat, *map, buf, sz, emu_setmap, addr, 0)
1330 	    || !*addr) {
1331 		bus_dmamem_free(sc->parent_dmat, buf, *map);
1332 		return NULL;
1333 	}
1334 	return buf;
1335 }
1336 
1337 static void
1338 emu_free(struct sc_info *sc, void *buf, bus_dmamap_t map)
1339 {
1340 	bus_dmamap_unload(sc->parent_dmat, map);
1341 	bus_dmamem_free(sc->parent_dmat, buf, map);
1342 }
1343 
1344 static void *
1345 emu_memalloc(struct sc_info *sc, u_int32_t sz, bus_addr_t *addr)
1346 {
1347 	u_int32_t blksz, start, idx, ofs, tmp, found;
1348 	struct emu_mem *mem = &sc->mem;
1349 	struct emu_memblk *blk;
1350 	void *buf;
1351 
1352 	blksz = sz / EMUPAGESIZE;
1353 	if (sz > (blksz * EMUPAGESIZE))
1354 		blksz++;
1355 	/* find a free block in the bitmap */
1356 	found = 0;
1357 	start = 1;
1358 	while (!found && start + blksz < EMUMAXPAGES) {
1359 		found = 1;
1360 		for (idx = start; idx < start + blksz; idx++)
1361 			if (mem->bmap[idx >> 3] & (1 << (idx & 7)))
1362 				found = 0;
1363 		if (!found)
1364 			start++;
1365 	}
1366 	if (!found)
1367 		return NULL;
1368 	blk = malloc(sizeof(*blk), M_DEVBUF, M_NOWAIT);
1369 	if (blk == NULL)
1370 		return NULL;
1371 	buf = emu_malloc(sc, sz, &blk->buf_addr, &blk->buf_map);
1372 	*addr = blk->buf_addr;
1373 	if (buf == NULL) {
1374 		free(blk, M_DEVBUF);
1375 		return NULL;
1376 	}
1377 	blk->buf = buf;
1378 	blk->pte_start = start;
1379 	blk->pte_size = blksz;
1380 #ifdef EMUDEBUG
1381 	printf("buf %p, pte_start %d, pte_size %d\n", blk->buf,
1382 	    blk->pte_start, blk->pte_size);
1383 #endif
1384 	ofs = 0;
1385 	for (idx = start; idx < start + blksz; idx++) {
1386 		mem->bmap[idx >> 3] |= 1 << (idx & 7);
1387 		tmp = (uint32_t)(blk->buf_addr + ofs);
1388 #ifdef EMUDEBUG
1389 		printf("pte[%d] -> %x phys, %x virt\n", idx, tmp,
1390 		    ((u_int32_t)buf) + ofs);
1391 #endif
1392 		mem->ptb_pages[idx] = (tmp << 1) | idx;
1393 		ofs += EMUPAGESIZE;
1394 	}
1395 	SLIST_INSERT_HEAD(&mem->blocks, blk, link);
1396 	return buf;
1397 }
1398 
1399 static int
1400 emu_memfree(struct sc_info *sc, void *buf)
1401 {
1402 	u_int32_t idx, tmp;
1403 	struct emu_mem *mem = &sc->mem;
1404 	struct emu_memblk *blk, *i;
1405 
1406 	blk = NULL;
1407 	SLIST_FOREACH(i, &mem->blocks, link) {
1408 		if (i->buf == buf)
1409 			blk = i;
1410 	}
1411 	if (blk == NULL)
1412 		return EINVAL;
1413 	SLIST_REMOVE(&mem->blocks, blk, emu_memblk, link);
1414 	emu_free(sc, buf, blk->buf_map);
1415 	tmp = (u_int32_t)(sc->mem.silent_page_addr) << 1;
1416 	for (idx = blk->pte_start; idx < blk->pte_start + blk->pte_size; idx++) {
1417 		mem->bmap[idx >> 3] &= ~(1 << (idx & 7));
1418 		mem->ptb_pages[idx] = tmp | idx;
1419 	}
1420 	free(blk, M_DEVBUF);
1421 	return 0;
1422 }
1423 
1424 static int
1425 emu_memstart(struct sc_info *sc, void *buf)
1426 {
1427 	struct emu_mem *mem = &sc->mem;
1428 	struct emu_memblk *blk, *i;
1429 
1430 	blk = NULL;
1431 	SLIST_FOREACH(i, &mem->blocks, link) {
1432 		if (i->buf == buf)
1433 			blk = i;
1434 	}
1435 	if (blk == NULL)
1436 		return -EINVAL;
1437 	return blk->pte_start;
1438 }
1439 
1440 static void
1441 emu_addefxop(struct sc_info *sc, int op, int z, int w, int x, int y,
1442     u_int32_t *pc)
1443 {
1444 	emu_wrefx(sc, (*pc) * 2, (x << 10) | y);
1445 	emu_wrefx(sc, (*pc) * 2 + 1, (op << 20) | (z << 10) | w);
1446 	(*pc)++;
1447 }
1448 
1449 static void
1450 audigy_addefxop(struct sc_info *sc, int op, int z, int w, int x, int y,
1451     u_int32_t *pc)
1452 {
1453 	emu_wrefx(sc, (*pc) * 2, (x << 12) | y);
1454 	emu_wrefx(sc, (*pc) * 2 + 1, (op << 24) | (z << 12) | w);
1455 	(*pc)++;
1456 }
1457 
1458 static void
1459 audigy_initefx(struct sc_info *sc)
1460 {
1461 	int i;
1462 	u_int32_t pc = 0;
1463 
1464 	/* skip 0, 0, -1, 0 - NOPs */
1465 	for (i = 0; i < 512; i++)
1466 		audigy_addefxop(sc, 0x0f, 0x0c0, 0x0c0, 0x0cf, 0x0c0, &pc);
1467 
1468 	for (i = 0; i < 512; i++)
1469 		emu_wrptr(sc, 0, EMU_A_FXGPREGBASE + i, 0x0);
1470 
1471 	pc = 16;
1472 
1473 	/* stop fx processor */
1474 	emu_wrptr(sc, 0, EMU_A_DBG, EMU_A_DBG_SINGLE_STEP);
1475 
1476 	/* Audigy 2 (EMU10K2) DSP Registers:
1477 	   FX Bus
1478 		0x000-0x00f : 16 registers (?)
1479 	   Input
1480 		0x040/0x041 : AC97 Codec (l/r)
1481 		0x042/0x043 : ADC, S/PDIF (l/r)
1482 		0x044/0x045 : Optical S/PDIF in (l/r)
1483 		0x046/0x047 : ?
1484 		0x048/0x049 : Line/Mic 2 (l/r)
1485 		0x04a/0x04b : RCA S/PDIF (l/r)
1486 		0x04c/0x04d : Aux 2 (l/r)
1487 	   Output
1488 		0x060/0x061 : Digital Front (l/r)
1489 		0x062/0x063 : Digital Center/LFE
1490 		0x064/0x065 : AudigyDrive Heaphone (l/r)
1491 		0x066/0x067 : Digital Rear (l/r)
1492 		0x068/0x069 : Analog Front (l/r)
1493 		0x06a/0x06b : Analog Center/LFE
1494 		0x06c/0x06d : ?
1495 		0x06e/0x06f : Analog Rear (l/r)
1496 		0x070/0x071 : AC97 Output (l/r)
1497 		0x072/0x073 : ?
1498 		0x074/0x075 : ?
1499 		0x076/0x077 : ADC Recording Buffer (l/r)
1500 	   Constants
1501 		0x0c0 - 0x0c4 = 0 - 4
1502 		0x0c5 = 0x8, 0x0c6 = 0x10, 0x0c7 = 0x20
1503 		0x0c8 = 0x100, 0x0c9 = 0x10000, 0x0ca = 0x80000
1504 		0x0cb = 0x10000000, 0x0cc = 0x20000000, 0x0cd = 0x40000000
1505 		0x0ce = 0x80000000, 0x0cf = 0x7fffffff, 0x0d0 = 0xffffffff
1506 		0x0d1 = 0xfffffffe, 0x0d2 = 0xc0000000, 0x0d3 = 0x41fbbcdc
1507 		0x0d4 = 0x5a7ef9db, 0x0d5 = 0x00100000, 0x0dc = 0x00000001 (?)
1508 	   Temporary Values
1509 		0x0d6 : Accumulator (?)
1510 		0x0d7 : Condition Register
1511 		0x0d8 : Noise source
1512 		0x0d9 : Noise source
1513 	   Tank Memory Data Registers
1514 		0x200 - 0x2ff
1515 	   Tank Memory Address Registers
1516 		0x300 - 0x3ff
1517 	   General Purpose Registers
1518 		0x400 - 0x5ff
1519 	 */
1520 
1521 	/* AC97Output[l/r] = FXBus PCM[l/r] */
1522 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AC97_L), A_C_00000000,
1523 			A_C_00000000, A_FXBUS(FXBUS_PCM_LEFT), &pc);
1524 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AC97_R), A_C_00000000,
1525 			A_C_00000000, A_FXBUS(FXBUS_PCM_RIGHT), &pc);
1526 
1527 	/* GPR[0/1] = RCA S/PDIF[l/r] -- Master volume */
1528 	audigy_addefxop(sc, iACC3, A_GPR(0), A_C_00000000,
1529 			A_C_00000000, A_EXTIN(EXTIN_COAX_SPDIF_L), &pc);
1530 	audigy_addefxop(sc, iACC3, A_GPR(1), A_C_00000000,
1531 			A_C_00000000, A_EXTIN(EXTIN_COAX_SPDIF_R), &pc);
1532 
1533 	/* GPR[2] = GPR[0] (Left) / 2 + GPR[1] (Right) / 2 -- Central volume */
1534 	audigy_addefxop(sc, iINTERP, A_GPR(2), A_GPR(1),
1535 			A_C_40000000, A_GPR(0), &pc);
1536 
1537 	/* Headphones[l/r] = GPR[0/1] */
1538 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_HEADPHONE_L),
1539 			A_C_00000000, A_C_00000000, A_GPR(0), &pc);
1540 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_HEADPHONE_R),
1541 			A_C_00000000, A_C_00000000, A_GPR(1), &pc);
1542 
1543 	/* Analog Front[l/r] = GPR[0/1] */
1544 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AFRONT_L), A_C_00000000,
1545 			A_C_00000000, A_GPR(0), &pc);
1546 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AFRONT_R), A_C_00000000,
1547 			A_C_00000000, A_GPR(1), &pc);
1548 
1549 	/* Digital Front[l/r] = GPR[0/1] */
1550 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_FRONT_L), A_C_00000000,
1551 			A_C_00000000, A_GPR(0), &pc);
1552 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_FRONT_R), A_C_00000000,
1553 			A_C_00000000, A_GPR(1), &pc);
1554 
1555 	/* Center and Subwoofer configuration */
1556 	/* Analog Center = GPR[0] + GPR[2] */
1557 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_ACENTER), A_C_00000000,
1558 			A_GPR(0), A_GPR(2), &pc);
1559 	/* Analog Sub = GPR[1] + GPR[2] */
1560 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_ALFE), A_C_00000000,
1561 			A_GPR(1), A_GPR(2), &pc);
1562 
1563 	/* Digital Center = GPR[0] + GPR[2] */
1564 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_CENTER), A_C_00000000,
1565 			A_GPR(0), A_GPR(2), &pc);
1566 	/* Digital Sub = GPR[1] + GPR[2] */
1567 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_LFE), A_C_00000000,
1568 			A_GPR(1), A_GPR(2), &pc);
1569 
1570 #if 0
1571 	/* Analog Rear[l/r] = (GPR[0/1] * RearVolume[l/r]) >> 31 */
1572 	/*   RearVolume = GPR[0x10/0x11] (Will this ever be implemented?) */
1573 	audigy_addefxop(sc, iMAC0, A_EXTOUT(A_EXTOUT_AREAR_L), A_C_00000000,
1574 			A_GPR(16), A_GPR(0), &pc);
1575 	audigy_addefxop(sc, iMAC0, A_EXTOUT(A_EXTOUT_AREAR_R), A_C_00000000,
1576 			A_GPR(17), A_GPR(1), &pc);
1577 
1578 	/* Digital Rear[l/r] = (GPR[0/1] * RearVolume[l/r]) >> 31 */
1579 	/*   RearVolume = GPR[0x10/0x11] (Will this ever be implemented?) */
1580 	audigy_addefxop(sc, iMAC0, A_EXTOUT(A_EXTOUT_REAR_L), A_C_00000000,
1581 			A_GPR(16), A_GPR(0), &pc);
1582 	audigy_addefxop(sc, iMAC0, A_EXTOUT(A_EXTOUT_REAR_R), A_C_00000000,
1583 			A_GPR(17), A_GPR(1), &pc);
1584 #else
1585 	/* XXX This is just a copy to the channel, since we do not have
1586 	 *     a patch manager, it is useful for have another output enabled.
1587 	 */
1588 
1589 	/* Analog Rear[l/r] = GPR[0/1] */
1590 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AREAR_L), A_C_00000000,
1591 			A_C_00000000, A_GPR(0), &pc);
1592 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AREAR_R), A_C_00000000,
1593 			A_C_00000000, A_GPR(1), &pc);
1594 
1595 	/* Digital Rear[l/r] = GPR[0/1] */
1596 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_REAR_L), A_C_00000000,
1597 			A_C_00000000, A_GPR(0), &pc);
1598 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_REAR_R), A_C_00000000,
1599 			A_C_00000000, A_GPR(1), &pc);
1600 #endif
1601 
1602 	/* ADC Recording buffer[l/r] = AC97Input[l/r] */
1603 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_ADC_CAP_L), A_C_00000000,
1604 			A_C_00000000, A_EXTIN(A_EXTIN_AC97_L), &pc);
1605 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_ADC_CAP_R), A_C_00000000,
1606 			A_C_00000000, A_EXTIN(A_EXTIN_AC97_R), &pc);
1607 
1608 	/* resume normal operations */
1609 	emu_wrptr(sc, 0, EMU_A_DBG, 0);
1610 }
1611 
1612 static void
1613 emu_initefx(struct sc_info *sc)
1614 {
1615 	int i;
1616 	u_int32_t pc = 16;
1617 
1618 	/* acc3 0,0,0,0 - NOPs */
1619 	for (i = 0; i < 512; i++) {
1620 		emu_wrefx(sc, i * 2, 0x10040);
1621 		emu_wrefx(sc, i * 2 + 1, 0x610040);
1622 	}
1623 
1624 	for (i = 0; i < 256; i++)
1625 		emu_wrptr(sc, 0, EMU_FXGPREGBASE + i, 0);
1626 
1627 	/* FX-8010 DSP Registers:
1628 	   FX Bus
1629 	     0x000-0x00f : 16 registers
1630 	   Input
1631 	     0x010/0x011 : AC97 Codec (l/r)
1632 	     0x012/0x013 : ADC, S/PDIF (l/r)
1633 	     0x014/0x015 : Mic(left), Zoom (l/r)
1634 	     0x016/0x017 : TOS link in (l/r)
1635 	     0x018/0x019 : Line/Mic 1 (l/r)
1636 	     0x01a/0x01b : COAX S/PDIF (l/r)
1637 	     0x01c/0x01d : Line/Mic 2 (l/r)
1638 	   Output
1639 	     0x020/0x021 : AC97 Output (l/r)
1640 	     0x022/0x023 : TOS link out (l/r)
1641 	     0x024/0x025 : Center/LFE
1642 	     0x026/0x027 : LiveDrive Headphone (l/r)
1643 	     0x028/0x029 : Rear Channel (l/r)
1644 	     0x02a/0x02b : ADC Recording Buffer (l/r)
1645 	     0x02c       : Mic Recording Buffer
1646 	     0x031/0x032 : Analog Center/LFE
1647 	   Constants
1648 	     0x040 - 0x044 = 0 - 4
1649 	     0x045 = 0x8, 0x046 = 0x10, 0x047 = 0x20
1650 	     0x048 = 0x100, 0x049 = 0x10000, 0x04a = 0x80000
1651 	     0x04b = 0x10000000, 0x04c = 0x20000000, 0x04d = 0x40000000
1652 	     0x04e = 0x80000000, 0x04f = 0x7fffffff, 0x050 = 0xffffffff
1653 	     0x051 = 0xfffffffe, 0x052 = 0xc0000000, 0x053 = 0x41fbbcdc
1654 	     0x054 = 0x5a7ef9db, 0x055 = 0x00100000
1655 	   Temporary Values
1656 	     0x056 : Accumulator
1657 	     0x057 : Condition Register
1658 	     0x058 : Noise source
1659 	     0x059 : Noise source
1660 	     0x05a : IRQ Register
1661 	     0x05b : TRAM Delay Base Address Count
1662 	   General Purpose Registers
1663 	     0x100 - 0x1ff
1664 	   Tank Memory Data Registers
1665 	     0x200 - 0x2ff
1666 	   Tank Memory Address Registers
1667 	     0x300 - 0x3ff
1668 	     */
1669 
1670 	/* Routing - this will be configurable in later version */
1671 
1672 	/* GPR[0/1] = FX * 4 + SPDIF-in */
1673 	emu_addefxop(sc, iMACINT0, GPR(0), EXTIN(EXTIN_SPDIF_CD_L),
1674 			FXBUS(FXBUS_PCM_LEFT), C_00000004, &pc);
1675 	emu_addefxop(sc, iMACINT0, GPR(1), EXTIN(EXTIN_SPDIF_CD_R),
1676 			FXBUS(FXBUS_PCM_RIGHT), C_00000004, &pc);
1677 
1678 	/* GPR[0/1] += APS-input */
1679 	emu_addefxop(sc, iACC3, GPR(0), GPR(0), C_00000000,
1680 			sc->APS ? EXTIN(EXTIN_TOSLINK_L) : C_00000000, &pc);
1681 	emu_addefxop(sc, iACC3, GPR(1), GPR(1), C_00000000,
1682 			sc->APS ? EXTIN(EXTIN_TOSLINK_R) : C_00000000, &pc);
1683 
1684 	/* FrontOut (AC97) = GPR[0/1] */
1685 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_AC97_L), C_00000000,
1686 			C_00000000, GPR(0), &pc);
1687 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_AC97_R), C_00000000,
1688 			C_00000001, GPR(1), &pc);
1689 
1690 	/* GPR[2] = GPR[0] (Left) / 2 + GPR[1] (Right) / 2 -- Central volume */
1691 	emu_addefxop(sc, iINTERP, GPR(2), GPR(1), C_40000000, GPR(0), &pc);
1692 
1693 #if 0
1694 	/* RearOut = (GPR[0/1] * RearVolume) >> 31 */
1695 	/*   RearVolume = GPR[0x10/0x11] */
1696 	emu_addefxop(sc, iMAC0, EXTOUT(EXTOUT_REAR_L), C_00000000,
1697 			GPR(16), GPR(0), &pc);
1698 	emu_addefxop(sc, iMAC0, EXTOUT(EXTOUT_REAR_R), C_00000000,
1699 			GPR(17), GPR(1), &pc);
1700 #else
1701 	/* XXX This is just a copy to the channel, since we do not have
1702 	 *     a patch manager, it is useful for have another output enabled.
1703 	 */
1704 
1705 	/* Rear[l/r] = GPR[0/1] */
1706 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_REAR_L), C_00000000,
1707 			C_00000000, GPR(0), &pc);
1708 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_REAR_R), C_00000000,
1709 			C_00000000, GPR(1), &pc);
1710 #endif
1711 
1712 	/* TOS out[l/r] = GPR[0/1] */
1713 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_TOSLINK_L), C_00000000,
1714 			C_00000000, GPR(0), &pc);
1715 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_TOSLINK_R), C_00000000,
1716 			C_00000000, GPR(1), &pc);
1717 
1718 	/* Center and Subwoofer configuration */
1719 	/* Analog Center = GPR[0] + GPR[2] */
1720 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_ACENTER), C_00000000,
1721 			GPR(0), GPR(2), &pc);
1722 	/* Analog Sub = GPR[1] + GPR[2] */
1723 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_ALFE), C_00000000,
1724 			GPR(1), GPR(2), &pc);
1725 	/* Digital Center = GPR[0] + GPR[2] */
1726 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_AC97_CENTER), C_00000000,
1727 			GPR(0), GPR(2), &pc);
1728 	/* Digital Sub = GPR[1] + GPR[2] */
1729 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_AC97_LFE), C_00000000,
1730 			GPR(1), GPR(2), &pc);
1731 
1732 	/* Headphones[l/r] = GPR[0/1] */
1733 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_HEADPHONE_L), C_00000000,
1734 			C_00000000, GPR(0), &pc);
1735 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_HEADPHONE_R), C_00000000,
1736 			C_00000000, GPR(1), &pc);
1737 
1738 	/* ADC Recording buffer[l/r] = AC97Input[l/r] */
1739 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_ADC_CAP_L), C_00000000,
1740 			C_00000000, EXTIN(EXTIN_AC97_L), &pc);
1741 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_ADC_CAP_R), C_00000000,
1742 			C_00000000, EXTIN(EXTIN_AC97_R), &pc);
1743 
1744 	/* resume normal operations */
1745 	emu_wrptr(sc, 0, EMU_DBG, 0);
1746 }
1747 
1748 /* Probe and attach the card */
1749 static int
1750 emu_init(struct sc_info *sc)
1751 {
1752 	u_int32_t spcs, ch, tmp, i;
1753 
1754 	if (sc->audigy) {
1755 		/* enable additional AC97 slots */
1756 		emu_wrptr(sc, 0, EMU_AC97SLOT, EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
1757 	}
1758 
1759 	/* disable audio and lock cache */
1760 	emu_wr(sc, EMU_HCFG,
1761 	    EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_MUTEBUTTONENABLE,
1762 	    4);
1763 
1764 	/* reset recording buffers */
1765 	emu_wrptr(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
1766 	emu_wrptr(sc, 0, EMU_MICBA, 0);
1767 	emu_wrptr(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
1768 	emu_wrptr(sc, 0, EMU_FXBA, 0);
1769 	emu_wrptr(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
1770 	emu_wrptr(sc, 0, EMU_ADCBA, 0);
1771 
1772 	/* disable channel interrupt */
1773 	emu_wr(sc, EMU_INTE,
1774 	    EMU_INTE_INTERTIMERENB | EMU_INTE_SAMPLERATER | EMU_INTE_PCIERRENABLE,
1775 	    4);
1776 	emu_wrptr(sc, 0, EMU_CLIEL, 0);
1777 	emu_wrptr(sc, 0, EMU_CLIEH, 0);
1778 	emu_wrptr(sc, 0, EMU_SOLEL, 0);
1779 	emu_wrptr(sc, 0, EMU_SOLEH, 0);
1780 
1781 	/* wonder what these do... */
1782 	if (sc->audigy) {
1783 		emu_wrptr(sc, 0, EMU_SPBYPASS, 0xf00);
1784 		emu_wrptr(sc, 0, EMU_AC97SLOT, 0x3);
1785 	}
1786 
1787 	/* init envelope engine */
1788 	for (ch = 0; ch < NUM_G; ch++) {
1789 		emu_wrptr(sc, ch, EMU_CHAN_DCYSUSV, ENV_OFF);
1790 		emu_wrptr(sc, ch, EMU_CHAN_IP, 0);
1791 		emu_wrptr(sc, ch, EMU_CHAN_VTFT, 0xffff);
1792 		emu_wrptr(sc, ch, EMU_CHAN_CVCF, 0xffff);
1793 		emu_wrptr(sc, ch, EMU_CHAN_PTRX, 0);
1794 		emu_wrptr(sc, ch, EMU_CHAN_CPF, 0);
1795 		emu_wrptr(sc, ch, EMU_CHAN_CCR, 0);
1796 
1797 		emu_wrptr(sc, ch, EMU_CHAN_PSST, 0);
1798 		emu_wrptr(sc, ch, EMU_CHAN_DSL, 0x10);
1799 		emu_wrptr(sc, ch, EMU_CHAN_CCCA, 0);
1800 		emu_wrptr(sc, ch, EMU_CHAN_Z1, 0);
1801 		emu_wrptr(sc, ch, EMU_CHAN_Z2, 0);
1802 		emu_wrptr(sc, ch, EMU_CHAN_FXRT, 0xd01c0000);
1803 
1804 		emu_wrptr(sc, ch, EMU_CHAN_ATKHLDM, 0);
1805 		emu_wrptr(sc, ch, EMU_CHAN_DCYSUSM, 0);
1806 		emu_wrptr(sc, ch, EMU_CHAN_IFATN, 0xffff);
1807 		emu_wrptr(sc, ch, EMU_CHAN_PEFE, 0);
1808 		emu_wrptr(sc, ch, EMU_CHAN_FMMOD, 0);
1809 		emu_wrptr(sc, ch, EMU_CHAN_TREMFRQ, 24);	/* 1 Hz */
1810 		emu_wrptr(sc, ch, EMU_CHAN_FM2FRQ2, 24);	/* 1 Hz */
1811 		emu_wrptr(sc, ch, EMU_CHAN_TEMPENV, 0);
1812 
1813 		/*** these are last so OFF prevents writing ***/
1814 		emu_wrptr(sc, ch, EMU_CHAN_LFOVAL2, 0);
1815 		emu_wrptr(sc, ch, EMU_CHAN_LFOVAL1, 0);
1816 		emu_wrptr(sc, ch, EMU_CHAN_ATKHLDV, 0);
1817 		emu_wrptr(sc, ch, EMU_CHAN_ENVVOL, 0);
1818 		emu_wrptr(sc, ch, EMU_CHAN_ENVVAL, 0);
1819 
1820 		if (sc->audigy) {
1821 			/* audigy cards need this to initialize correctly */
1822 			emu_wrptr(sc, ch, 0x4c, 0);
1823 			emu_wrptr(sc, ch, 0x4d, 0);
1824 			emu_wrptr(sc, ch, 0x4e, 0);
1825 			emu_wrptr(sc, ch, 0x4f, 0);
1826 			/* set default routing */
1827 			emu_wrptr(sc, ch, EMU_A_CHAN_FXRT1, 0x03020100);
1828 			emu_wrptr(sc, ch, EMU_A_CHAN_FXRT2, 0x3f3f3f3f);
1829 			emu_wrptr(sc, ch, EMU_A_CHAN_SENDAMOUNTS, 0);
1830 		}
1831 
1832 		sc->voice[ch].vnum = ch;
1833 		sc->voice[ch].slave = NULL;
1834 		sc->voice[ch].busy = 0;
1835 		sc->voice[ch].ismaster = 0;
1836 		sc->voice[ch].running = 0;
1837 		sc->voice[ch].b16 = 0;
1838 		sc->voice[ch].stereo = 0;
1839 		sc->voice[ch].speed = 0;
1840 		sc->voice[ch].start = 0;
1841 		sc->voice[ch].end = 0;
1842 		sc->voice[ch].channel = NULL;
1843 	}
1844 	sc->pnum = sc->rnum = 0;
1845 
1846 	/*
1847 	 *  Init to 0x02109204 :
1848 	 *  Clock accuracy    = 0     (1000ppm)
1849 	 *  Sample Rate       = 2     (48kHz)
1850 	 *  Audio Channel     = 1     (Left of 2)
1851 	 *  Source Number     = 0     (Unspecified)
1852 	 *  Generation Status = 1     (Original for Cat Code 12)
1853 	 *  Cat Code          = 12    (Digital Signal Mixer)
1854 	 *  Mode              = 0     (Mode 0)
1855 	 *  Emphasis          = 0     (None)
1856 	 *  CP                = 1     (Copyright unasserted)
1857 	 *  AN                = 0     (Audio data)
1858 	 *  P                 = 0     (Consumer)
1859 	 */
1860 	spcs = EMU_SPCS_CLKACCY_1000PPM | EMU_SPCS_SAMPLERATE_48 |
1861 	    EMU_SPCS_CHANNELNUM_LEFT | EMU_SPCS_SOURCENUM_UNSPEC |
1862 	    EMU_SPCS_GENERATIONSTATUS | 0x00001200 | 0x00000000 |
1863 	    EMU_SPCS_EMPHASIS_NONE | EMU_SPCS_COPYRIGHT;
1864 	emu_wrptr(sc, 0, EMU_SPCS0, spcs);
1865 	emu_wrptr(sc, 0, EMU_SPCS1, spcs);
1866 	emu_wrptr(sc, 0, EMU_SPCS2, spcs);
1867 
1868 	if (!sc->audigy)
1869 		emu_initefx(sc);
1870 	else if (sc->audigy2) {	/* Audigy 2 */
1871 		/* from ALSA initialization code: */
1872 
1873 		/* Hack for Alice3 to work independent of haP16V driver */
1874 		u_int32_t tmp;
1875 
1876 		/* Setup SRCMulti_I2S SamplingRate */
1877 		tmp = emu_rdptr(sc, 0, EMU_A_SPDIF_SAMPLERATE) & 0xfffff1ff;
1878 		emu_wrptr(sc, 0, EMU_A_SPDIF_SAMPLERATE, tmp | 0x400);
1879 
1880 		/* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */
1881 		emu_wr(sc, 0x20, 0x00600000, 4);
1882 		emu_wr(sc, 0x24, 0x00000014, 4);
1883 
1884 		/* Setup SRCMulti Input Audio Enable */
1885 		emu_wr(sc, 0x20, 0x006e0000, 4);
1886 		emu_wr(sc, 0x24, 0xff00ff00, 4);
1887 	}
1888 
1889 	SLIST_INIT(&sc->mem.blocks);
1890 	sc->mem.ptb_pages = emu_malloc(sc, EMUMAXPAGES * sizeof(u_int32_t),
1891 	    &sc->mem.ptb_pages_addr, &sc->mem.ptb_map);
1892 	if (sc->mem.ptb_pages == NULL)
1893 		return -1;
1894 
1895 	sc->mem.silent_page = emu_malloc(sc, EMUPAGESIZE,
1896 	    &sc->mem.silent_page_addr, &sc->mem.silent_map);
1897 	if (sc->mem.silent_page == NULL) {
1898 		emu_free(sc, sc->mem.ptb_pages, sc->mem.ptb_map);
1899 		return -1;
1900 	}
1901 	/* Clear page with silence & setup all pointers to this page */
1902 	bzero(sc->mem.silent_page, EMUPAGESIZE);
1903 	tmp = (u_int32_t)(sc->mem.silent_page_addr) << 1;
1904 	for (i = 0; i < EMUMAXPAGES; i++)
1905 		sc->mem.ptb_pages[i] = tmp | i;
1906 
1907 	emu_wrptr(sc, 0, EMU_PTB, (sc->mem.ptb_pages_addr));
1908 	emu_wrptr(sc, 0, EMU_TCB, 0);	/* taken from original driver */
1909 	emu_wrptr(sc, 0, EMU_TCBS, 0);	/* taken from original driver */
1910 
1911 	for (ch = 0; ch < NUM_G; ch++) {
1912 		emu_wrptr(sc, ch, EMU_CHAN_MAPA, tmp | EMU_CHAN_MAP_PTI_MASK);
1913 		emu_wrptr(sc, ch, EMU_CHAN_MAPB, tmp | EMU_CHAN_MAP_PTI_MASK);
1914 	}
1915 
1916 	/* emu_memalloc(sc, EMUPAGESIZE); */
1917 	/*
1918 	 *  Hokay, now enable the AUD bit
1919 	 *
1920 	 *  Audigy
1921 	 *   Enable Audio = 0 (enabled after fx processor initialization)
1922 	 *   Mute Disable Audio = 0
1923 	 *   Joystick = 1
1924 	 *
1925 	 *  Audigy 2
1926 	 *   Enable Audio = 1
1927 	 *   Mute Disable Audio = 0
1928 	 *   Joystick = 1
1929 	 *   GP S/PDIF AC3 Enable = 1
1930 	 *   CD S/PDIF AC3 Enable = 1
1931 	 *
1932 	 *  EMU10K1
1933 	 *   Enable Audio = 1
1934 	 *   Mute Disable Audio = 0
1935 	 *   Lock Tank Memory = 1
1936 	 *   Lock Sound Memory = 0
1937 	 *   Auto Mute = 1
1938 	 */
1939 
1940 	if (sc->audigy) {
1941 		tmp = EMU_HCFG_AUTOMUTE | EMU_HCFG_JOYENABLE;
1942 		if (sc->audigy2)	/* Audigy 2 */
1943 			tmp = EMU_HCFG_AUDIOENABLE | EMU_HCFG_AC3ENABLE_CDSPDIF |
1944 			    EMU_HCFG_AC3ENABLE_GPSPDIF;
1945 		emu_wr(sc, EMU_HCFG, tmp, 4);
1946 
1947 		audigy_initefx(sc);
1948 
1949 		/* from ALSA initialization code: */
1950 
1951 		/* enable audio and disable both audio/digital outputs */
1952 		emu_wr(sc, EMU_HCFG, emu_rd(sc, EMU_HCFG, 4) | EMU_HCFG_AUDIOENABLE, 4);
1953 		emu_wr(sc, EMU_A_IOCFG, emu_rd(sc, EMU_A_IOCFG, 4) & ~EMU_A_IOCFG_GPOUT_AD,
1954 		    4);
1955 		if (sc->audigy2) {	/* Audigy 2 */
1956 			/* Unmute Analog.
1957 			 * Set GPO6 to 1 for Apollo. This has to be done after
1958 			 * init Alice3 I2SOut beyond 48kHz.
1959 			 * So, sequence is important.
1960 			 */
1961 			emu_wr(sc, EMU_A_IOCFG,
1962 			    emu_rd(sc, EMU_A_IOCFG, 4) | EMU_A_IOCFG_GPOUT_A, 4);
1963 		}
1964 	} else {
1965 		/* EMU10K1 initialization code */
1966 		tmp = EMU_HCFG_AUDIOENABLE | EMU_HCFG_LOCKTANKCACHE_MASK
1967 		    | EMU_HCFG_AUTOMUTE;
1968 		if (sc->rev >= 6)
1969 			tmp |= EMU_HCFG_JOYENABLE;
1970 
1971 		emu_wr(sc, EMU_HCFG, tmp, 4);
1972 
1973 		/* TOSLink detection */
1974 		sc->tos_link = 0;
1975 		tmp = emu_rd(sc, EMU_HCFG, 4);
1976 		if (tmp & (EMU_HCFG_GPINPUT0 | EMU_HCFG_GPINPUT1)) {
1977 			emu_wr(sc, EMU_HCFG, tmp | EMU_HCFG_GPOUT1, 4);
1978 			DELAY(50);
1979 			if (tmp != (emu_rd(sc, EMU_HCFG, 4) & ~EMU_HCFG_GPOUT1)) {
1980 				sc->tos_link = 1;
1981 				emu_wr(sc, EMU_HCFG, tmp, 4);
1982 			}
1983 		}
1984 	}
1985 
1986 	return 0;
1987 }
1988 
1989 static int
1990 emu_uninit(struct sc_info *sc)
1991 {
1992 	u_int32_t ch;
1993 
1994 	emu_wr(sc, EMU_INTE, 0, 4);
1995 	for (ch = 0; ch < NUM_G; ch++)
1996 		emu_wrptr(sc, ch, EMU_CHAN_DCYSUSV, ENV_OFF);
1997 	for (ch = 0; ch < NUM_G; ch++) {
1998 		emu_wrptr(sc, ch, EMU_CHAN_VTFT, 0);
1999 		emu_wrptr(sc, ch, EMU_CHAN_CVCF, 0);
2000 		emu_wrptr(sc, ch, EMU_CHAN_PTRX, 0);
2001 		emu_wrptr(sc, ch, EMU_CHAN_CPF, 0);
2002 	}
2003 
2004 	if (sc->audigy) {	/* stop fx processor */
2005 		emu_wrptr(sc, 0, EMU_A_DBG, EMU_A_DBG_SINGLE_STEP);
2006 	}
2007 
2008 	/* disable audio and lock cache */
2009 	emu_wr(sc, EMU_HCFG,
2010 	    EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_MUTEBUTTONENABLE,
2011 	    4);
2012 
2013 	emu_wrptr(sc, 0, EMU_PTB, 0);
2014 	/* reset recording buffers */
2015 	emu_wrptr(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
2016 	emu_wrptr(sc, 0, EMU_MICBA, 0);
2017 	emu_wrptr(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
2018 	emu_wrptr(sc, 0, EMU_FXBA, 0);
2019 	emu_wrptr(sc, 0, EMU_FXWC, 0);
2020 	emu_wrptr(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
2021 	emu_wrptr(sc, 0, EMU_ADCBA, 0);
2022 	emu_wrptr(sc, 0, EMU_TCB, 0);
2023 	emu_wrptr(sc, 0, EMU_TCBS, 0);
2024 
2025 	/* disable channel interrupt */
2026 	emu_wrptr(sc, 0, EMU_CLIEL, 0);
2027 	emu_wrptr(sc, 0, EMU_CLIEH, 0);
2028 	emu_wrptr(sc, 0, EMU_SOLEL, 0);
2029 	emu_wrptr(sc, 0, EMU_SOLEH, 0);
2030 
2031 	/* init envelope engine */
2032 	if (!SLIST_EMPTY(&sc->mem.blocks))
2033 		device_printf(sc->dev, "warning: memblock list not empty\n");
2034 	emu_free(sc, sc->mem.ptb_pages, sc->mem.ptb_map);
2035 	emu_free(sc, sc->mem.silent_page, sc->mem.silent_map);
2036 
2037 	if(sc->mpu)
2038 	    mpu401_uninit(sc->mpu);
2039 	return 0;
2040 }
2041 
2042 static int
2043 emu_pci_probe(device_t dev)
2044 {
2045 	char *s = NULL;
2046 
2047 	switch (pci_get_devid(dev)) {
2048 	case EMU10K1_PCI_ID:
2049 		s = "Creative EMU10K1";
2050 		break;
2051 
2052 	case EMU10K2_PCI_ID:
2053 		if (pci_get_revid(dev) == 0x04)
2054 			s = "Creative Audigy 2 (EMU10K2)";
2055 		else
2056 			s = "Creative Audigy (EMU10K2)";
2057 		break;
2058 
2059 	case EMU10K3_PCI_ID:
2060 		s = "Creative Audigy 2 (EMU10K3)";
2061 		break;
2062 
2063 	default:
2064 		return ENXIO;
2065 	}
2066 
2067 	device_set_desc(dev, s);
2068 	return BUS_PROBE_LOW_PRIORITY;
2069 }
2070 
2071 static int
2072 emu_pci_attach(device_t dev)
2073 {
2074 	struct ac97_info *codec = NULL;
2075 	struct sc_info *sc;
2076 	int i, gotmic;
2077 	char status[SND_STATUSLEN];
2078 
2079 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
2080 	sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_emu10k1 softc");
2081 	sc->dev = dev;
2082 	sc->type = pci_get_devid(dev);
2083 	sc->rev = pci_get_revid(dev);
2084 	sc->audigy = sc->type == EMU10K2_PCI_ID || sc->type == EMU10K3_PCI_ID;
2085 	sc->audigy2 = (sc->audigy && sc->rev == 0x04);
2086 	sc->nchans = sc->audigy ? 8 : 4;
2087 	sc->addrmask = sc->audigy ? EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK;
2088 
2089 	pci_enable_busmaster(dev);
2090 
2091 	i = PCIR_BAR(0);
2092 	sc->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &i, RF_ACTIVE);
2093 	if (sc->reg == NULL) {
2094 		device_printf(dev, "unable to map register space\n");
2095 		goto bad;
2096 	}
2097 	sc->st = rman_get_bustag(sc->reg);
2098 	sc->sh = rman_get_bushandle(sc->reg);
2099 
2100 	sc->bufsz = pcm_getbuffersize(dev, 4096, EMU_DEFAULT_BUFSZ, 65536);
2101 
2102 	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
2103 		/*boundary*/0,
2104 		/*lowaddr*/(1U << 31) - 1, /* can only access 0-2gb */
2105 		/*highaddr*/BUS_SPACE_MAXADDR,
2106 		/*filter*/NULL, /*filterarg*/NULL,
2107 		/*maxsize*/sc->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
2108 		/*flags*/0, /*lockfunc*/busdma_lock_mutex,
2109 		/*lockarg*/&Giant, &sc->parent_dmat) != 0) {
2110 		device_printf(dev, "unable to create dma tag\n");
2111 		goto bad;
2112 	}
2113 
2114 	if (emu_init(sc) == -1) {
2115 		device_printf(dev, "unable to initialize the card\n");
2116 		goto bad;
2117 	}
2118 
2119 	codec = AC97_CREATE(dev, sc, emu_ac97);
2120 	if (codec == NULL) goto bad;
2121 	gotmic = (ac97_getcaps(codec) & AC97_CAP_MICCHANNEL) ? 1 : 0;
2122 	if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto bad;
2123 
2124 	emu_midiattach(sc);
2125 
2126 	i = 0;
2127 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
2128 	    RF_ACTIVE | RF_SHAREABLE);
2129 	if (!sc->irq ||
2130 	    snd_setup_intr(dev, sc->irq, INTR_MPSAFE, emu_intr, sc, &sc->ih)) {
2131 		device_printf(dev, "unable to map interrupt\n");
2132 		goto bad;
2133 	}
2134 
2135 	snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd %s",
2136 	    rman_get_start(sc->reg), rman_get_start(sc->irq),
2137 	    PCM_KLDSTRING(snd_emu10k1));
2138 
2139 	if (pcm_register(dev, sc, sc->nchans, gotmic ? 3 : 2)) goto bad;
2140 	for (i = 0; i < sc->nchans; i++)
2141 		pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc);
2142 	for (i = 0; i < (gotmic ? 3 : 2); i++)
2143 		pcm_addchan(dev, PCMDIR_REC, &emurchan_class, sc);
2144 
2145 	pcm_setstatus(dev, status);
2146 
2147 	return 0;
2148 
2149 bad:
2150 	if (codec) ac97_destroy(codec);
2151 	if (sc->reg) bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
2152 	if (sc->ih) bus_teardown_intr(dev, sc->irq, sc->ih);
2153 	if (sc->irq) bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
2154 	if (sc->parent_dmat) bus_dma_tag_destroy(sc->parent_dmat);
2155 	if (sc->lock) snd_mtxfree(sc->lock);
2156 	free(sc, M_DEVBUF);
2157 	return ENXIO;
2158 }
2159 
2160 static int
2161 emu_pci_detach(device_t dev)
2162 {
2163 	int r;
2164 	struct sc_info *sc;
2165 
2166 	r = pcm_unregister(dev);
2167 	if (r)
2168 		return r;
2169 
2170 	sc = pcm_getdevinfo(dev);
2171 	/* shutdown chip */
2172 	emu_uninit(sc);
2173 
2174 	bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
2175 	bus_teardown_intr(dev, sc->irq, sc->ih);
2176 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
2177 	bus_dma_tag_destroy(sc->parent_dmat);
2178 	snd_mtxfree(sc->lock);
2179 	free(sc, M_DEVBUF);
2180 
2181 	return 0;
2182 }
2183 
2184 /* add suspend, resume */
2185 static device_method_t emu_methods[] = {
2186 	/* Device interface */
2187 	DEVMETHOD(device_probe,		emu_pci_probe),
2188 	DEVMETHOD(device_attach,	emu_pci_attach),
2189 	DEVMETHOD(device_detach,	emu_pci_detach),
2190 
2191 	DEVMETHOD_END
2192 };
2193 
2194 static driver_t emu_driver = {
2195 	"pcm",
2196 	emu_methods,
2197 	PCM_SOFTC_SIZE,
2198 };
2199 
2200 DRIVER_MODULE(snd_emu10k1, pci, emu_driver, pcm_devclass, NULL, NULL);
2201 MODULE_DEPEND(snd_emu10k1, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
2202 MODULE_VERSION(snd_emu10k1, 1);
2203 MODULE_DEPEND(snd_emu10k1, midi, 1, 1, 1);
2204 
2205 /* dummy driver to silence the joystick device */
2206 static int
2207 emujoy_pci_probe(device_t dev)
2208 {
2209 	char *s = NULL;
2210 
2211 	switch (pci_get_devid(dev)) {
2212 	case 0x70021102:
2213 		s = "Creative EMU10K1 Joystick";
2214 		device_quiet(dev);
2215 		break;
2216 	case 0x70031102:
2217 		s = "Creative EMU10K2 Joystick";
2218 		device_quiet(dev);
2219 		break;
2220 	}
2221 
2222 	if (s) device_set_desc(dev, s);
2223 	return s ? -1000 : ENXIO;
2224 }
2225 
2226 static int
2227 emujoy_pci_attach(device_t dev)
2228 {
2229 
2230 	return 0;
2231 }
2232 
2233 static int
2234 emujoy_pci_detach(device_t dev)
2235 {
2236 
2237 	return 0;
2238 }
2239 
2240 static device_method_t emujoy_methods[] = {
2241 	DEVMETHOD(device_probe,		emujoy_pci_probe),
2242 	DEVMETHOD(device_attach,	emujoy_pci_attach),
2243 	DEVMETHOD(device_detach,	emujoy_pci_detach),
2244 
2245 	DEVMETHOD_END
2246 };
2247 
2248 static driver_t emujoy_driver = {
2249 	"emujoy",
2250 	emujoy_methods,
2251 	1	/* no softc */
2252 };
2253 
2254 static devclass_t emujoy_devclass;
2255 
2256 DRIVER_MODULE(emujoy, pci, emujoy_driver, emujoy_devclass, NULL, NULL);
2257