xref: /freebsd/sys/dev/sound/pci/es137x.c (revision 0efd6615cd5f39b67cec82a7034e655f3b5801e3)
1 /*-
2  * Support the ENSONIQ AudioPCI board and Creative Labs SoundBlaster PCI
3  * boards based on the ES1370, ES1371 and ES1373 chips.
4  *
5  * Copyright (c) 1999 Russell Cattelan <cattelan@thebarn.com>
6  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
7  * Copyright (c) 1998 by Joachim Kuebart. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgement:
23  *	This product includes software developed by Joachim Kuebart.
24  *
25  * 4. The name of the author may not be used to endorse or promote
26  *    products derived from this software without specific prior
27  *    written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  * DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*
43  * Part of this code was heavily inspired by the linux driver from
44  * Thomas Sailer (sailer@ife.ee.ethz.ch)
45  * Just about everything has been touched and reworked in some way but
46  * the all the underlying sequences/timing/register values are from
47  * Thomas' code.
48  *
49 */
50 
51 #include <dev/sound/pcm/sound.h>
52 #include <dev/sound/pcm/ac97.h>
53 #include <dev/sound/pci/es137x.h>
54 
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57 
58 #include <sys/sysctl.h>
59 
60 #include "mixer_if.h"
61 
62 SND_DECLARE_FILE("$FreeBSD$");
63 
64 #define MEM_MAP_REG 0x14
65 
66 /* PCI IDs of supported chips */
67 #define ES1370_PCI_ID 0x50001274
68 #define ES1371_PCI_ID 0x13711274
69 #define ES1371_PCI_ID2 0x13713274
70 #define CT5880_PCI_ID 0x58801274
71 #define CT4730_PCI_ID 0x89381102
72 
73 #define ES1371REV_ES1371_A  0x02
74 #define ES1371REV_ES1371_B  0x09
75 
76 #define ES1371REV_ES1373_8  0x08
77 #define ES1371REV_ES1373_A  0x04
78 #define ES1371REV_ES1373_B  0x06
79 
80 #define ES1371REV_CT5880_A  0x07
81 
82 #define CT5880REV_CT5880_C  0x02
83 #define CT5880REV_CT5880_D  0x03
84 #define CT5880REV_CT5880_E  0x04
85 
86 #define CT4730REV_CT4730_A  0x00
87 
88 #define ES_DEFAULT_BUFSZ 4096
89 
90 /* 2 DAC for playback, 1 ADC for record */
91 #define ES_DAC1		0
92 #define ES_DAC2		1
93 #define ES_ADC		2
94 #define ES_NCHANS	3
95 
96 #define ES1370_DAC1_MINSPEED	5512
97 #define ES1370_DAC1_MAXSPEED	44100
98 
99 /* device private data */
100 struct es_info;
101 
102 struct es_chinfo {
103 	struct es_info *parent;
104 	struct pcm_channel *channel;
105 	struct snd_dbuf *buffer;
106 	struct pcmchan_caps caps;
107 	int dir, num, index;
108 	uint32_t fmt, blksz, blkcnt, bufsz;
109 	uint32_t ptr, prevptr;
110 	int active;
111 };
112 
113 /*
114  *     32bit Ensoniq Configuration (es->escfg).
115  *     ----------------------------------------
116  *
117  *     +-------+--------+------+------+---------+--------+---------+---------+
118  * len |  16   |    1   |  1   |  1   |    2    |   2    |    1    |    8    |
119  *     +-------+--------+------+------+---------+--------+---------+---------+
120  *     | fixed | single |      |      |         |        |   is    | general |
121  *     | rate  |   pcm  | DACx | DACy | numplay | numrec | es1370? | purpose |
122  *     |       |  mixer |      |      |         |        |         |         |
123  *     +-------+--------+------+------+---------+--------+---------+---------+
124  */
125 #define ES_FIXED_RATE(cfgv)	\
126 		(((cfgv) & 0xffff0000) >> 16)
127 #define ES_SET_FIXED_RATE(cfgv, nv)	\
128 		(((cfgv) & ~0xffff0000) | (((nv) & 0xffff) << 16))
129 #define ES_SINGLE_PCM_MIX(cfgv)	\
130 		(((cfgv) & 0x8000) >> 15)
131 #define ES_SET_SINGLE_PCM_MIX(cfgv, nv)	\
132 		(((cfgv) & ~0x8000) | (((nv) ? 1 : 0) << 15))
133 #define ES_DAC_FIRST(cfgv)	\
134 		(((cfgv) & 0x4000) >> 14)
135 #define ES_SET_DAC_FIRST(cfgv, nv)	\
136 		(((cfgv) & ~0x4000) | (((nv) & 0x1) << 14))
137 #define ES_DAC_SECOND(cfgv)	\
138 		(((cfgv) & 0x2000) >> 13)
139 #define ES_SET_DAC_SECOND(cfgv, nv)	\
140 		(((cfgv) & ~0x2000) | (((nv) & 0x1) << 13))
141 #define ES_NUMPLAY(cfgv)	\
142 		(((cfgv) & 0x1800) >> 11)
143 #define ES_SET_NUMPLAY(cfgv, nv)	\
144 		(((cfgv) & ~0x1800) | (((nv) & 0x3) << 11))
145 #define ES_NUMREC(cfgv)	\
146 		(((cfgv) & 0x600) >> 9)
147 #define ES_SET_NUMREC(cfgv, nv)	\
148 		(((cfgv) & ~0x600) | (((nv) & 0x3) << 9))
149 #define ES_IS_ES1370(cfgv)	\
150 		(((cfgv) & 0x100) >> 8)
151 #define ES_SET_IS_ES1370(cfgv, nv)	\
152 		(((cfgv) & ~0x100) | (((nv) ? 1 : 0) << 8))
153 #define ES_GP(cfgv)	\
154 		((cfgv) & 0xff)
155 #define ES_SET_GP(cfgv, nv)	\
156 		(((cfgv) & ~0xff) | ((nv) & 0xff))
157 
158 #define ES_DAC1_ENABLED(cfgv)	\
159 		(ES_NUMPLAY(cfgv) > 1 || \
160 		(ES_NUMPLAY(cfgv) == 1 && ES_DAC_FIRST(cfgv) == ES_DAC1))
161 #define ES_DAC2_ENABLED(cfgv)	\
162 		(ES_NUMPLAY(cfgv) > 1 || \
163 		(ES_NUMPLAY(cfgv) == 1 && ES_DAC_FIRST(cfgv) == ES_DAC2))
164 
165 /*
166  * DAC 1/2 configuration through kernel hint - hint.pcm.<unit>.dac="val"
167  *
168  * 0 = Enable both DACs - Default
169  * 1 = Enable single DAC (DAC1)
170  * 2 = Enable single DAC (DAC2)
171  * 3 = Enable both DACs, swap position (DAC2 comes first instead of DAC1)
172  */
173 #define ES_DEFAULT_DAC_CFG	0
174 
175 struct es_info {
176 	bus_space_tag_t st;
177 	bus_space_handle_t sh;
178 	bus_dma_tag_t	parent_dmat;
179 
180 	struct resource *reg, *irq;
181 	int regtype, regid, irqid;
182 	void *ih;
183 
184 	device_t dev;
185 	int num;
186 	unsigned int bufsz, blkcnt;
187 
188 	/* Contents of board's registers */
189 	uint32_t	ctrl;
190 	uint32_t	sctrl;
191 	uint32_t	escfg;
192 	struct es_chinfo ch[ES_NCHANS];
193 	struct mtx	*lock;
194 	struct callout	poll_timer;
195 	int poll_ticks, polling;
196 };
197 
198 #define ES_LOCK(sc)		snd_mtxlock((sc)->lock)
199 #define ES_UNLOCK(sc)		snd_mtxunlock((sc)->lock)
200 #define ES_LOCK_ASSERT(sc)	snd_mtxassert((sc)->lock)
201 
202 /* prototypes */
203 static void     es_intr(void *);
204 static uint32_t	es1371_wait_src_ready(struct es_info *);
205 static void	es1371_src_write(struct es_info *,
206 					unsigned short, unsigned short);
207 static unsigned int	es1371_adc_rate(struct es_info *, unsigned int, int);
208 static unsigned int	es1371_dac_rate(struct es_info *, unsigned int, int);
209 static int	es1371_init(struct es_info *);
210 static int      es1370_init(struct es_info *);
211 static int      es1370_wrcodec(struct es_info *, unsigned char, unsigned char);
212 
213 static uint32_t es_fmt[] = {
214 	AFMT_U8,
215 	AFMT_STEREO | AFMT_U8,
216 	AFMT_S16_LE,
217 	AFMT_STEREO | AFMT_S16_LE,
218 	0
219 };
220 static struct pcmchan_caps es_caps = {4000, 48000, es_fmt, 0};
221 
222 static const struct {
223 	unsigned        volidx:4;
224 	unsigned        left:4;
225 	unsigned        right:4;
226 	unsigned        stereo:1;
227 	unsigned        recmask:13;
228 	unsigned        avail:1;
229 }       mixtable[SOUND_MIXER_NRDEVICES] = {
230 	[SOUND_MIXER_VOLUME]	= { 0, 0x0, 0x1, 1, 0x1f7f, 1 },
231 	[SOUND_MIXER_PCM] 	= { 1, 0x2, 0x3, 1, 0x0400, 1 },
232 	[SOUND_MIXER_SYNTH]	= { 2, 0x4, 0x5, 1, 0x0060, 1 },
233 	[SOUND_MIXER_CD]	= { 3, 0x6, 0x7, 1, 0x0006, 1 },
234 	[SOUND_MIXER_LINE]	= { 4, 0x8, 0x9, 1, 0x0018, 1 },
235 	[SOUND_MIXER_LINE1]	= { 5, 0xa, 0xb, 1, 0x1800, 1 },
236 	[SOUND_MIXER_LINE2]	= { 6, 0xc, 0x0, 0, 0x0100, 1 },
237 	[SOUND_MIXER_LINE3]	= { 7, 0xd, 0x0, 0, 0x0200, 1 },
238 	[SOUND_MIXER_MIC]	= { 8, 0xe, 0x0, 0, 0x0001, 1 },
239 	[SOUND_MIXER_OGAIN]	= { 9, 0xf, 0x0, 0, 0x0000, 1 }
240 };
241 
242 static __inline uint32_t
243 es_rd(struct es_info *es, int regno, int size)
244 {
245 	switch (size) {
246 	case 1:
247 		return (bus_space_read_1(es->st, es->sh, regno));
248 	case 2:
249 		return (bus_space_read_2(es->st, es->sh, regno));
250 	case 4:
251 		return (bus_space_read_4(es->st, es->sh, regno));
252 	default:
253 		return (0xFFFFFFFF);
254 	}
255 }
256 
257 static __inline void
258 es_wr(struct es_info *es, int regno, uint32_t data, int size)
259 {
260 
261 	switch (size) {
262 	case 1:
263 		bus_space_write_1(es->st, es->sh, regno, data);
264 		break;
265 	case 2:
266 		bus_space_write_2(es->st, es->sh, regno, data);
267 		break;
268 	case 4:
269 		bus_space_write_4(es->st, es->sh, regno, data);
270 		break;
271 	}
272 }
273 
274 /* -------------------------------------------------------------------- */
275 /* The es1370 mixer interface */
276 
277 static int
278 es1370_mixinit(struct snd_mixer *m)
279 {
280 	struct es_info *es;
281 	int i;
282 	uint32_t v;
283 
284 	es = mix_getdevinfo(m);
285 	v = 0;
286 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
287 		if (mixtable[i].avail)
288 			v |= (1 << i);
289 	}
290 	/*
291 	 * Each DAC1/2 for ES1370 can be controlled independently
292 	 *   DAC1 = controlled by synth
293 	 *   DAC2 = controlled by pcm
294 	 * This is indeed can confuse user if DAC1 become primary playback
295 	 * channel. Try to be smart and combine both if necessary.
296 	 */
297 	if (ES_SINGLE_PCM_MIX(es->escfg))
298 		v &= ~(1 << SOUND_MIXER_SYNTH);
299 	mix_setdevs(m, v);
300 	v = 0;
301 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
302 		if (mixtable[i].recmask)
303 			v |= (1 << i);
304 	}
305 	if (ES_SINGLE_PCM_MIX(es->escfg)) /* ditto */
306 		v &= ~(1 << SOUND_MIXER_SYNTH);
307 	mix_setrecdevs(m, v);
308 	return (0);
309 }
310 
311 static int
312 es1370_mixset(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
313 {
314 	struct es_info *es;
315 	int l, r, rl, rr, set_dac1;
316 
317 	if (!mixtable[dev].avail)
318 		return (-1);
319 	l = left;
320 	r = (mixtable[dev].stereo) ? right : l;
321 	if (mixtable[dev].left == 0xf)
322 		rl = (l < 2) ? 0x80 : 7 - (l - 2) / 14;
323 	else
324 		rl = (l < 10) ? 0x80 : 15 - (l - 10) / 6;
325 	es = mix_getdevinfo(m);
326 	ES_LOCK(es);
327 	if (dev == SOUND_MIXER_PCM && (ES_SINGLE_PCM_MIX(es->escfg)) &&
328 	    ES_DAC1_ENABLED(es->escfg))
329 		set_dac1 = 1;
330 	else
331 		set_dac1 = 0;
332 	if (mixtable[dev].stereo) {
333 		rr = (r < 10) ? 0x80 : 15 - (r - 10) / 6;
334 		es1370_wrcodec(es, mixtable[dev].right, rr);
335 		if (set_dac1 && mixtable[SOUND_MIXER_SYNTH].stereo)
336 			es1370_wrcodec(es,
337 			    mixtable[SOUND_MIXER_SYNTH].right, rr);
338 	}
339 	es1370_wrcodec(es, mixtable[dev].left, rl);
340 	if (set_dac1)
341 		es1370_wrcodec(es, mixtable[SOUND_MIXER_SYNTH].left, rl);
342 	ES_UNLOCK(es);
343 
344 	return (l | (r << 8));
345 }
346 
347 static int
348 es1370_mixsetrecsrc(struct snd_mixer *m, uint32_t src)
349 {
350 	struct es_info *es;
351 	int i, j = 0;
352 
353 	es = mix_getdevinfo(m);
354 	if (src == 0) src = 1 << SOUND_MIXER_MIC;
355 	src &= mix_getrecdevs(m);
356 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
357 		if ((src & (1 << i)) != 0) j |= mixtable[i].recmask;
358 
359 	ES_LOCK(es);
360 	if ((src & (1 << SOUND_MIXER_PCM)) && ES_SINGLE_PCM_MIX(es->escfg) &&
361 	    ES_DAC1_ENABLED(es->escfg))
362 		j |= mixtable[SOUND_MIXER_SYNTH].recmask;
363 	es1370_wrcodec(es, CODEC_LIMIX1, j & 0x55);
364 	es1370_wrcodec(es, CODEC_RIMIX1, j & 0xaa);
365 	es1370_wrcodec(es, CODEC_LIMIX2, (j >> 8) & 0x17);
366 	es1370_wrcodec(es, CODEC_RIMIX2, (j >> 8) & 0x0f);
367 	es1370_wrcodec(es, CODEC_OMIX1, 0x7f);
368 	es1370_wrcodec(es, CODEC_OMIX2, 0x3f);
369 	ES_UNLOCK(es);
370 
371 	return (src);
372 }
373 
374 static kobj_method_t es1370_mixer_methods[] = {
375 	KOBJMETHOD(mixer_init,		es1370_mixinit),
376 	KOBJMETHOD(mixer_set,		es1370_mixset),
377 	KOBJMETHOD(mixer_setrecsrc,	es1370_mixsetrecsrc),
378 	{ 0, 0 }
379 };
380 MIXER_DECLARE(es1370_mixer);
381 
382 /* -------------------------------------------------------------------- */
383 
384 static int
385 es1370_wrcodec(struct es_info *es, unsigned char i, unsigned char data)
386 {
387 	unsigned int t;
388 
389 	ES_LOCK_ASSERT(es);
390 
391 	for (t = 0; t < 0x1000; t++) {
392 		if ((es_rd(es, ES1370_REG_STATUS, 4) &
393 		    STAT_CSTAT) == 0) {
394 			es_wr(es, ES1370_REG_CODEC,
395 			    ((unsigned short)i << CODEC_INDEX_SHIFT) | data, 2);
396 			return (0);
397 		}
398 		DELAY(1);
399 	}
400 	device_printf(es->dev, "%s: timed out\n", __func__);
401 	return (-1);
402 }
403 
404 /* -------------------------------------------------------------------- */
405 
406 /* channel interface */
407 static void *
408 eschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
409 				struct pcm_channel *c, int dir)
410 {
411 	struct es_info *es = devinfo;
412 	struct es_chinfo *ch;
413 	uint32_t index;
414 
415 	ES_LOCK(es);
416 
417 	if (dir == PCMDIR_PLAY) {
418 		index = ES_GP(es->escfg);
419 		es->escfg = ES_SET_GP(es->escfg, index + 1);
420 		if (index == 0)
421 			index = ES_DAC_FIRST(es->escfg);
422 		else if (index == 1)
423 			index = ES_DAC_SECOND(es->escfg);
424 		else {
425 			device_printf(es->dev,
426 			    "Invalid ES_GP index: %d\n", index);
427 			ES_UNLOCK(es);
428 			return (NULL);
429 		}
430 		if (!(index == ES_DAC1 || index == ES_DAC2)) {
431 			device_printf(es->dev, "Unknown DAC: %d\n", index + 1);
432 			ES_UNLOCK(es);
433 			return (NULL);
434 		}
435 		if (es->ch[index].channel != NULL) {
436 			device_printf(es->dev, "DAC%d already initialized!\n",
437 			    index + 1);
438 			ES_UNLOCK(es);
439 			return (NULL);
440 		}
441 	} else
442 		index = ES_ADC;
443 
444 	ch = &es->ch[index];
445 	ch->index = index;
446 	ch->num = es->num++;
447 	ch->caps = es_caps;
448 	if (ES_IS_ES1370(es->escfg)) {
449 		if (ch->index == ES_DAC1) {
450 			ch->caps.maxspeed = ES1370_DAC1_MAXSPEED;
451 			ch->caps.minspeed = ES1370_DAC1_MINSPEED;
452 		} else {
453 			uint32_t fixed_rate = ES_FIXED_RATE(es->escfg);
454 			if (!(fixed_rate < es_caps.minspeed ||
455 			    fixed_rate > es_caps.maxspeed)) {
456 				ch->caps.maxspeed = fixed_rate;
457 				ch->caps.minspeed = fixed_rate;
458 			}
459 		}
460 	}
461 	ch->parent = es;
462 	ch->channel = c;
463 	ch->buffer = b;
464 	ch->bufsz = es->bufsz;
465 	ch->blkcnt = es->blkcnt;
466 	ch->blksz = ch->bufsz / ch->blkcnt;
467 	ch->dir = dir;
468 	ES_UNLOCK(es);
469 	if (sndbuf_alloc(ch->buffer, es->parent_dmat, ch->bufsz) != 0)
470 		return (NULL);
471 	ES_LOCK(es);
472 	if (dir == PCMDIR_PLAY) {
473 		if (ch->index == ES_DAC1) {
474 			es_wr(es, ES1370_REG_MEMPAGE,
475 			    ES1370_REG_DAC1_FRAMEADR >> 8, 1);
476 			es_wr(es, ES1370_REG_DAC1_FRAMEADR & 0xff,
477 			    sndbuf_getbufaddr(ch->buffer), 4);
478 			es_wr(es, ES1370_REG_DAC1_FRAMECNT & 0xff,
479 			    (ch->bufsz >> 2) - 1, 4);
480 		} else {
481 			es_wr(es, ES1370_REG_MEMPAGE,
482 			    ES1370_REG_DAC2_FRAMEADR >> 8, 1);
483 			es_wr(es, ES1370_REG_DAC2_FRAMEADR & 0xff,
484 			    sndbuf_getbufaddr(ch->buffer), 4);
485 			es_wr(es, ES1370_REG_DAC2_FRAMECNT & 0xff,
486 			    (ch->bufsz >> 2) - 1, 4);
487 		}
488 	} else {
489 		es_wr(es, ES1370_REG_MEMPAGE, ES1370_REG_ADC_FRAMEADR >> 8, 1);
490 		es_wr(es, ES1370_REG_ADC_FRAMEADR & 0xff,
491 		    sndbuf_getbufaddr(ch->buffer), 4);
492 		es_wr(es, ES1370_REG_ADC_FRAMECNT & 0xff,
493 		    (ch->bufsz >> 2) - 1, 4);
494 	}
495 	ES_UNLOCK(es);
496 	return (ch);
497 }
498 
499 static int
500 eschan_setformat(kobj_t obj, void *data, uint32_t format)
501 {
502 	struct es_chinfo *ch = data;
503 	struct es_info *es = ch->parent;
504 
505 	ES_LOCK(es);
506 	if (ch->dir == PCMDIR_PLAY) {
507 		if (ch->index == ES_DAC1) {
508 			es->sctrl &= ~SCTRL_P1FMT;
509 			if (format & AFMT_S16_LE)
510 				es->sctrl |= SCTRL_P1SEB;
511 			if (format & AFMT_STEREO)
512 				es->sctrl |= SCTRL_P1SMB;
513 		} else {
514 			es->sctrl &= ~SCTRL_P2FMT;
515 			if (format & AFMT_S16_LE)
516 				es->sctrl |= SCTRL_P2SEB;
517 			if (format & AFMT_STEREO)
518 				es->sctrl |= SCTRL_P2SMB;
519 		}
520 	} else {
521 		es->sctrl &= ~SCTRL_R1FMT;
522 		if (format & AFMT_S16_LE)
523 			es->sctrl |= SCTRL_R1SEB;
524 		if (format & AFMT_STEREO)
525 			es->sctrl |= SCTRL_R1SMB;
526 	}
527 	es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
528 	ES_UNLOCK(es);
529 	ch->fmt = format;
530 	return (0);
531 }
532 
533 static int
534 eschan1370_setspeed(kobj_t obj, void *data, uint32_t speed)
535 {
536 	struct es_chinfo *ch = data;
537 	struct es_info *es = ch->parent;
538 
539 	ES_LOCK(es);
540 	/* Fixed rate , do nothing. */
541 	if (ch->caps.minspeed == ch->caps.maxspeed)
542 		return (ch->caps.maxspeed);
543 	if (speed < ch->caps.minspeed)
544 		speed = ch->caps.minspeed;
545 	if (speed > ch->caps.maxspeed)
546 		speed = ch->caps.maxspeed;
547 	if (ch->index == ES_DAC1) {
548 		/*
549 		 * DAC1 does not support continuous rate settings.
550 		 * Pick the nearest and use it since FEEDER_RATE will
551 		 * do the the proper conversion for us.
552 		 */
553 		es->ctrl &= ~CTRL_WTSRSEL;
554 		if (speed < 8268) {
555 			speed = 5512;
556 			es->ctrl |= 0 << CTRL_SH_WTSRSEL;
557 		} else if (speed < 16537) {
558 			speed = 11025;
559 			es->ctrl |= 1 << CTRL_SH_WTSRSEL;
560 		} else if (speed < 33075) {
561 			speed = 22050;
562 			es->ctrl |= 2 << CTRL_SH_WTSRSEL;
563 		} else {
564 			speed = 44100;
565 			es->ctrl |= 3 << CTRL_SH_WTSRSEL;
566 		}
567 	} else {
568 		es->ctrl &= ~CTRL_PCLKDIV;
569 		es->ctrl |= DAC2_SRTODIV(speed) << CTRL_SH_PCLKDIV;
570 	}
571 	es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
572 	ES_UNLOCK(es);
573 	return (speed);
574 }
575 
576 static int
577 eschan1371_setspeed(kobj_t obj, void *data, uint32_t speed)
578 {
579   	struct es_chinfo *ch = data;
580   	struct es_info *es = ch->parent;
581 	uint32_t i;
582 	int delta;
583 
584 	ES_LOCK(es);
585 	if (ch->dir == PCMDIR_PLAY)
586   		i = es1371_dac_rate(es, speed, ch->index); /* play */
587 	else
588   		i = es1371_adc_rate(es, speed, ch->index); /* record */
589 	ES_UNLOCK(es);
590 	delta = (speed > i) ? (speed - i) : (i - speed);
591 	if (delta < 2)
592 		return (speed);
593 	return (i);
594 }
595 
596 static int
597 eschan_setblocksize(kobj_t obj, void *data, uint32_t blksz)
598 {
599   	struct es_chinfo *ch = data;
600   	struct es_info *es = ch->parent;
601 
602 	blksz &= ~0x3f;
603 	if (blksz < 0x40)
604 		blksz = 0x40;
605 
606 	if ((blksz * ch->blkcnt) > sndbuf_getmaxsize(ch->buffer))
607 		blksz = sndbuf_getmaxsize(ch->buffer) / ch->blkcnt;
608 
609 	if ((sndbuf_getblksz(ch->buffer) != blksz ||
610 	    sndbuf_getblkcnt(ch->buffer) != ch->blkcnt) &&
611 	    sndbuf_resize(ch->buffer, ch->blkcnt, blksz) != 0)
612 		device_printf(es->dev, "%s: failed blksz=%u blkcnt=%u\n",
613 		    __func__, blksz, ch->blkcnt);
614 
615 	ch->bufsz = sndbuf_getsize(ch->buffer);
616 	ch->blksz = sndbuf_getblksz(ch->buffer);
617 
618 	return (ch->blksz);
619 }
620 
621 #define es_chan_active(es)	((es)->ch[ES_DAC1].active + \
622 				(es)->ch[ES_DAC2].active + \
623 				(es)->ch[ES_ADC].active)
624 
625 static __inline int
626 es_poll_channel(struct es_chinfo *ch)
627 {
628 	struct es_info *es;
629 	uint32_t sz, delta;
630 	uint32_t reg, ptr;
631 
632 	if (ch == NULL || ch->channel == NULL || ch->active == 0)
633 		return (0);
634 
635 	es = ch->parent;
636 	if (ch->dir == PCMDIR_PLAY) {
637 		if (ch->index == ES_DAC1)
638 			reg = ES1370_REG_DAC1_FRAMECNT;
639 		else
640 			reg = ES1370_REG_DAC2_FRAMECNT;
641 	} else
642 		reg = ES1370_REG_ADC_FRAMECNT;
643 	sz = ch->blksz * ch->blkcnt;
644 	es_wr(es, ES1370_REG_MEMPAGE, reg >> 8, 4);
645 	ptr = es_rd(es, reg & 0x000000ff, 4) >> 16;
646 	ptr <<= 2;
647 	ch->ptr = ptr;
648 	ptr %= sz;
649 	ptr &= ~(ch->blksz - 1);
650 	delta = (sz + ptr - ch->prevptr) % sz;
651 
652 	if (delta < ch->blksz)
653 		return (0);
654 
655 	ch->prevptr = ptr;
656 
657 	return (1);
658 }
659 
660 static void
661 es_poll_callback(void *arg)
662 {
663 	struct es_info *es = arg;
664 	uint32_t trigger = 0;
665 	int i;
666 
667 	if (es == NULL)
668 		return;
669 
670 	ES_LOCK(es);
671 	if (es->polling == 0 || es_chan_active(es) == 0) {
672 		ES_UNLOCK(es);
673 		return;
674 	}
675 
676 	for (i = 0; i < ES_NCHANS; i++) {
677 		if (es_poll_channel(&es->ch[i]) != 0)
678 			trigger |= 1 << i;
679 	}
680 
681 	/* XXX */
682 	callout_reset(&es->poll_timer, 1/*es->poll_ticks*/,
683 	    es_poll_callback, es);
684 
685 	ES_UNLOCK(es);
686 
687 	for (i = 0; i < ES_NCHANS; i++) {
688 		if (trigger & (1 << i))
689 			chn_intr(es->ch[i].channel);
690 	}
691 }
692 
693 static int
694 eschan_trigger(kobj_t obj, void *data, int go)
695 {
696 	struct es_chinfo *ch = data;
697 	struct es_info *es = ch->parent;
698 	uint32_t cnt, b = 0;
699 
700 	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
701 		return (0);
702 
703 	ES_LOCK(es);
704 	cnt = (ch->blksz / sndbuf_getbps(ch->buffer)) - 1;
705 	if (ch->fmt & AFMT_16BIT)
706 		b |= 0x02;
707 	if (ch->fmt & AFMT_STEREO)
708 		b |= 0x01;
709 	if (ch->dir == PCMDIR_PLAY) {
710 		if (go == PCMTRIG_START) {
711 			if (ch->index == ES_DAC1) {
712 				es->ctrl |= CTRL_DAC1_EN;
713 				es->sctrl &= ~(SCTRL_P1LOOPSEL |
714 				    SCTRL_P1PAUSE | SCTRL_P1SCTRLD);
715 				if (es->polling == 0)
716 					es->sctrl |= SCTRL_P1INTEN;
717 				else
718 					es->sctrl &= ~SCTRL_P1INTEN;
719 				es->sctrl |= b;
720 				es_wr(es, ES1370_REG_DAC1_SCOUNT, cnt, 4);
721 				/* start at beginning of buffer */
722 				es_wr(es, ES1370_REG_MEMPAGE,
723 				    ES1370_REG_DAC1_FRAMECNT >> 8, 4);
724 				es_wr(es, ES1370_REG_DAC1_FRAMECNT & 0xff,
725 				    (ch->bufsz >> 2) - 1, 4);
726 			} else {
727 				es->ctrl |= CTRL_DAC2_EN;
728 				es->sctrl &= ~(SCTRL_P2ENDINC | SCTRL_P2STINC |
729 				    SCTRL_P2LOOPSEL | SCTRL_P2PAUSE |
730 				    SCTRL_P2DACSEN);
731 				if (es->polling == 0)
732 					es->sctrl |= SCTRL_P2INTEN;
733 				else
734 					es->sctrl &= ~SCTRL_P2INTEN;
735 				es->sctrl |= (b << 2) |
736 				    ((((b >> 1) & 1) + 1) << SCTRL_SH_P2ENDINC);
737 				es_wr(es, ES1370_REG_DAC2_SCOUNT, cnt, 4);
738 				/* start at beginning of buffer */
739 				es_wr(es, ES1370_REG_MEMPAGE,
740 				    ES1370_REG_DAC2_FRAMECNT >> 8, 4);
741 				es_wr(es, ES1370_REG_DAC2_FRAMECNT & 0xff,
742 				    (ch->bufsz >> 2) - 1, 4);
743 			}
744 		} else
745 			es->ctrl &= ~((ch->index == ES_DAC1) ?
746 			    CTRL_DAC1_EN : CTRL_DAC2_EN);
747 	} else {
748 		if (go == PCMTRIG_START) {
749 			es->ctrl |= CTRL_ADC_EN;
750 			es->sctrl &= ~SCTRL_R1LOOPSEL;
751 			if (es->polling == 0)
752 				es->sctrl |= SCTRL_R1INTEN;
753 			else
754 				es->sctrl &= ~SCTRL_R1INTEN;
755 			es->sctrl |= b << 4;
756 			es_wr(es, ES1370_REG_ADC_SCOUNT, cnt, 4);
757 			/* start at beginning of buffer */
758 			es_wr(es, ES1370_REG_MEMPAGE,
759 			    ES1370_REG_ADC_FRAMECNT >> 8, 4);
760 			es_wr(es, ES1370_REG_ADC_FRAMECNT & 0xff,
761 			    (ch->bufsz >> 2) - 1, 4);
762 		} else
763 			es->ctrl &= ~CTRL_ADC_EN;
764 	}
765 	es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
766 	es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
767 	if (go == PCMTRIG_START) {
768 		if (es->polling != 0) {
769 			ch->ptr = 0;
770 			ch->prevptr = 0;
771 			if (es_chan_active(es) == 0) {
772 				es->poll_ticks = 1;
773 				callout_reset(&es->poll_timer, 1,
774 				    es_poll_callback, es);
775 			}
776 		}
777 		ch->active = 1;
778 	} else {
779 		ch->active = 0;
780 		if (es->polling != 0) {
781 			if (es_chan_active(es) == 0) {
782 				callout_stop(&es->poll_timer);
783 				es->poll_ticks = 1;
784 			}
785 		}
786 	}
787 	ES_UNLOCK(es);
788 	return (0);
789 }
790 
791 static int
792 eschan_getptr(kobj_t obj, void *data)
793 {
794 	struct es_chinfo *ch = data;
795 	struct es_info *es = ch->parent;
796 	uint32_t reg, cnt;
797 
798 	ES_LOCK(es);
799 	if (es->polling != 0)
800 		cnt = ch->ptr;
801 	else {
802 		if (ch->dir == PCMDIR_PLAY) {
803 			if (ch->index == ES_DAC1)
804 				reg = ES1370_REG_DAC1_FRAMECNT;
805 			else
806 				reg = ES1370_REG_DAC2_FRAMECNT;
807 		} else
808 			reg = ES1370_REG_ADC_FRAMECNT;
809 		es_wr(es, ES1370_REG_MEMPAGE, reg >> 8, 4);
810 		cnt = es_rd(es, reg & 0x000000ff, 4) >> 16;
811 		/* cnt is longwords */
812 		cnt <<= 2;
813 	}
814 	ES_UNLOCK(es);
815 
816 	cnt &= ~0x3f;
817 
818 	return (cnt);
819 }
820 
821 static struct pcmchan_caps *
822 eschan_getcaps(kobj_t obj, void *data)
823 {
824 	struct es_chinfo *ch = data;
825 
826 	return (&ch->caps);
827 }
828 
829 static kobj_method_t eschan1370_methods[] = {
830 	KOBJMETHOD(channel_init,		eschan_init),
831 	KOBJMETHOD(channel_setformat,		eschan_setformat),
832 	KOBJMETHOD(channel_setspeed,		eschan1370_setspeed),
833 	KOBJMETHOD(channel_setblocksize,	eschan_setblocksize),
834 	KOBJMETHOD(channel_trigger,		eschan_trigger),
835 	KOBJMETHOD(channel_getptr,		eschan_getptr),
836 	KOBJMETHOD(channel_getcaps,		eschan_getcaps),
837 	{ 0, 0 }
838 };
839 CHANNEL_DECLARE(eschan1370);
840 
841 static kobj_method_t eschan1371_methods[] = {
842 	KOBJMETHOD(channel_init,		eschan_init),
843 	KOBJMETHOD(channel_setformat,		eschan_setformat),
844 	KOBJMETHOD(channel_setspeed,		eschan1371_setspeed),
845 	KOBJMETHOD(channel_setblocksize,	eschan_setblocksize),
846 	KOBJMETHOD(channel_trigger,		eschan_trigger),
847 	KOBJMETHOD(channel_getptr,		eschan_getptr),
848 	KOBJMETHOD(channel_getcaps,		eschan_getcaps),
849 	{ 0, 0 }
850 };
851 CHANNEL_DECLARE(eschan1371);
852 
853 /* -------------------------------------------------------------------- */
854 /* The interrupt handler */
855 static void
856 es_intr(void *p)
857 {
858 	struct es_info *es = p;
859 	uint32_t intsrc, sctrl;
860 
861 	ES_LOCK(es);
862 	if (es->polling != 0) {
863 		ES_UNLOCK(es);
864 		return;
865 	}
866 	intsrc = es_rd(es, ES1370_REG_STATUS, 4);
867 	if ((intsrc & STAT_INTR) == 0) {
868 		ES_UNLOCK(es);
869 		return;
870 	}
871 
872 	sctrl = es->sctrl;
873 	if (intsrc & STAT_ADC)
874 		sctrl &= ~SCTRL_R1INTEN;
875 	if (intsrc & STAT_DAC1)
876 		sctrl &= ~SCTRL_P1INTEN;
877 	if (intsrc & STAT_DAC2)
878 		sctrl &= ~SCTRL_P2INTEN;
879 
880 	es_wr(es, ES1370_REG_SERIAL_CONTROL, sctrl, 4);
881 	es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
882 	ES_UNLOCK(es);
883 
884 	if (intsrc & STAT_ADC)
885 		chn_intr(es->ch[ES_ADC].channel);
886 	if (intsrc & STAT_DAC1)
887 		chn_intr(es->ch[ES_DAC1].channel);
888 	if (intsrc & STAT_DAC2)
889 		chn_intr(es->ch[ES_DAC2].channel);
890 }
891 
892 /* ES1370 specific */
893 static int
894 es1370_init(struct es_info *es)
895 {
896 	uint32_t fixed_rate;
897 	int r, single_pcm;
898 
899 	/* ES1370 default to fixed rate operation */
900 	if (resource_int_value(device_get_name(es->dev),
901 	    device_get_unit(es->dev), "fixed_rate", &r) == 0) {
902 		fixed_rate = r;
903 		if (fixed_rate) {
904 			if (fixed_rate < es_caps.minspeed)
905 				fixed_rate = es_caps.minspeed;
906 			if (fixed_rate > es_caps.maxspeed)
907 				fixed_rate = es_caps.maxspeed;
908 		}
909 	} else
910 		fixed_rate = es_caps.maxspeed;
911 
912 	if (resource_int_value(device_get_name(es->dev),
913 	    device_get_unit(es->dev), "single_pcm_mixer", &r) == 0)
914 		single_pcm = (r != 0) ? 1 : 0;
915 	else
916 		single_pcm = 1;
917 
918 	ES_LOCK(es);
919 	if (ES_NUMPLAY(es->escfg) == 1)
920 		single_pcm = 1;
921 	/* This is ES1370 */
922 	es->escfg = ES_SET_IS_ES1370(es->escfg, 1);
923 	if (fixed_rate)
924 		es->escfg = ES_SET_FIXED_RATE(es->escfg, fixed_rate);
925 	else {
926 		es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
927 		fixed_rate = DSP_DEFAULT_SPEED;
928 	}
929 	if (single_pcm)
930 		es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 1);
931 	else
932 		es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 0);
933 	es->ctrl = CTRL_CDC_EN | CTRL_JYSTK_EN | CTRL_SERR_DIS |
934 	    (DAC2_SRTODIV(fixed_rate) << CTRL_SH_PCLKDIV);
935 	es->ctrl |= 3 << CTRL_SH_WTSRSEL;
936 	es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
937 
938 	es->sctrl = 0;
939 	es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
940 
941 	/* No RST, PD */
942 	es1370_wrcodec(es, CODEC_RES_PD, 3);
943 	/*
944 	 * CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off the LRCLK2 PLL;
945 	 * program DAC_SYNC=0!
946 	 */
947 	es1370_wrcodec(es, CODEC_CSEL, 0);
948 	/* Recording source is mixer */
949 	es1370_wrcodec(es, CODEC_ADSEL, 0);
950 	/* MIC amp is 0db */
951 	es1370_wrcodec(es, CODEC_MGAIN, 0);
952 	ES_UNLOCK(es);
953 
954 	return (0);
955 }
956 
957 /* ES1371 specific */
958 int
959 es1371_init(struct es_info *es)
960 {
961 	uint32_t cssr, devid, revid, subdev;
962 	int idx;
963 
964 	ES_LOCK(es);
965 	/* This is NOT ES1370 */
966 	es->escfg = ES_SET_IS_ES1370(es->escfg, 0);
967 	es->num = 0;
968 	es->sctrl = 0;
969 	cssr = 0;
970 	devid = pci_get_devid(es->dev);
971 	revid = pci_get_revid(es->dev);
972 	subdev = (pci_get_subdevice(es->dev) << 16) |
973 	    pci_get_subvendor(es->dev);
974 	/*
975 	 * Joyport blacklist. Either we're facing with broken hardware
976 	 * or because this hardware need special (unknown) initialization
977 	 * procedures.
978 	 */
979 	switch (subdev) {
980 	case 0x20001274:	/* old Ensoniq */
981 		es->ctrl = 0;
982 		break;
983 	default:
984 		es->ctrl = CTRL_JYSTK_EN;
985 		break;
986 	}
987 	if (devid == CT4730_PCI_ID) {
988 		/* XXX amplifier hack? */
989 		es->ctrl |= (1 << 16);
990 	}
991 	/* initialize the chips */
992 	es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
993 	es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
994 	es_wr(es, ES1371_REG_LEGACY, 0, 4);
995 	if ((devid == ES1371_PCI_ID && revid == ES1371REV_ES1373_8) ||
996 	    (devid == ES1371_PCI_ID && revid == ES1371REV_CT5880_A) ||
997 	    (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_C) ||
998 	    (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_D) ||
999 	    (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_E)) {
1000 		cssr = 1 << 29;
1001 		es_wr(es, ES1370_REG_STATUS, cssr, 4);
1002 		DELAY(20000);
1003 	}
1004 	/* AC'97 warm reset to start the bitclk */
1005 	es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1006 	es_wr(es, ES1371_REG_LEGACY, ES1371_SYNC_RES, 4);
1007 	DELAY(2000);
1008 	es_wr(es, ES1370_REG_CONTROL, es->sctrl, 4);
1009 	es1371_wait_src_ready(es);
1010 	/* Init the sample rate converter */
1011 	es_wr(es, ES1371_REG_SMPRATE, ES1371_DIS_SRC, 4);
1012 	for (idx = 0; idx < 0x80; idx++)
1013 		es1371_src_write(es, idx, 0);
1014 	es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
1015 	es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
1016 	es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
1017 	es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
1018 	es1371_src_write(es, ES_SMPREG_VOL_ADC, 1 << 12);
1019 	es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, 1 << 12);
1020 	es1371_src_write(es, ES_SMPREG_VOL_DAC1, 1 << 12);
1021 	es1371_src_write(es, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
1022 	es1371_src_write(es, ES_SMPREG_VOL_DAC2, 1 << 12);
1023 	es1371_src_write(es, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
1024 	es1371_adc_rate(es, 22050, ES_ADC);
1025 	es1371_dac_rate(es, 22050, ES_DAC1);
1026 	es1371_dac_rate(es, 22050, ES_DAC2);
1027 	/*
1028 	 * WARNING:
1029 	 * enabling the sample rate converter without properly programming
1030 	 * its parameters causes the chip to lock up (the SRC busy bit will
1031 	 * be stuck high, and I've found no way to rectify this other than
1032 	 * power cycle)
1033 	 */
1034 	es1371_wait_src_ready(es);
1035 	es_wr(es, ES1371_REG_SMPRATE, 0, 4);
1036 	/* try to reset codec directly */
1037 	es_wr(es, ES1371_REG_CODEC, 0, 4);
1038 	es_wr(es, ES1370_REG_STATUS, cssr, 4);
1039 	ES_UNLOCK(es);
1040 
1041 	return (0);
1042 }
1043 
1044 /* -------------------------------------------------------------------- */
1045 
1046 static int
1047 es1371_wrcd(kobj_t obj, void *s, int addr, uint32_t data)
1048 {
1049 	uint32_t t, x, orig;
1050 	struct es_info *es = (struct es_info*)s;
1051 
1052 	for (t = 0; t < 0x1000; t++)
1053 	  	if (!es_rd(es, ES1371_REG_CODEC & CODEC_WIP, 4))
1054 			break;
1055 	/* save the current state for later */
1056  	x = orig = es_rd(es, ES1371_REG_SMPRATE, 4);
1057 	/* enable SRC state data in SRC mux */
1058 	es_wr(es, ES1371_REG_SMPRATE, (x & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1059 	    ES1371_DIS_P2 | ES1371_DIS_R1)) | 0x00010000, 4);
1060 	/* busy wait */
1061 	for (t = 0; t < 0x1000; t++) {
1062 	  	if ((es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1063 		    0x00000000)
1064 			break;
1065 	}
1066 	/* wait for a SAFE time to write addr/data and then do it, dammit */
1067 	for (t = 0; t < 0x1000; t++) {
1068 	  	if ((es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1069 		    0x00010000)
1070 			break;
1071 	}
1072 
1073 	es_wr(es, ES1371_REG_CODEC, ((addr << CODEC_POADD_SHIFT) &
1074 	    CODEC_POADD_MASK) | ((data << CODEC_PODAT_SHIFT) &
1075 	    CODEC_PODAT_MASK), 4);
1076 	/* restore SRC reg */
1077 	es1371_wait_src_ready(s);
1078 	es_wr(es, ES1371_REG_SMPRATE, orig, 4);
1079 
1080 	return (0);
1081 }
1082 
1083 static int
1084 es1371_rdcd(kobj_t obj, void *s, int addr)
1085 {
1086   	uint32_t t, x, orig;
1087   	struct es_info *es = (struct es_info *)s;
1088 
1089   	for (t = 0; t < 0x1000; t++) {
1090 		if (!(x = es_rd(es, ES1371_REG_CODEC, 4) & CODEC_WIP))
1091 	  		break;
1092 	}
1093 
1094   	/* save the current state for later */
1095   	x = orig = es_rd(es, ES1371_REG_SMPRATE, 4);
1096   	/* enable SRC state data in SRC mux */
1097   	es_wr(es, ES1371_REG_SMPRATE, (x & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1098 	    ES1371_DIS_P2 | ES1371_DIS_R1)) | 0x00010000, 4);
1099 	/* busy wait */
1100   	for (t = 0; t < 0x1000; t++) {
1101 		if ((x = es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1102 		    0x00000000)
1103 	  		break;
1104 	}
1105   	/* wait for a SAFE time to write addr/data and then do it, dammit */
1106   	for (t = 0; t < 0x1000; t++) {
1107 		if ((x = es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1108 		    0x00010000)
1109 	  		break;
1110 	}
1111 
1112   	es_wr(es, ES1371_REG_CODEC, ((addr << CODEC_POADD_SHIFT) &
1113 	    CODEC_POADD_MASK) | CODEC_PORD, 4);
1114 
1115   	/* restore SRC reg */
1116   	es1371_wait_src_ready(s);
1117   	es_wr(es, ES1371_REG_SMPRATE, orig, 4);
1118 
1119   	/* now wait for the stinkin' data (RDY) */
1120   	for (t = 0; t < 0x1000; t++) {
1121 		if ((x = es_rd(es, ES1371_REG_CODEC, 4)) & CODEC_RDY)
1122 	  		break;
1123 	}
1124 
1125   	return ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT);
1126 }
1127 
1128 static kobj_method_t es1371_ac97_methods[] = {
1129 	KOBJMETHOD(ac97_read,		es1371_rdcd),
1130 	KOBJMETHOD(ac97_write,		es1371_wrcd),
1131 	{ 0, 0 }
1132 };
1133 AC97_DECLARE(es1371_ac97);
1134 
1135 /* -------------------------------------------------------------------- */
1136 
1137 static unsigned int
1138 es1371_src_read(struct es_info *es, unsigned short reg)
1139 {
1140   	uint32_t r;
1141 
1142   	r = es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1143 	    ES1371_DIS_P2 | ES1371_DIS_R1);
1144   	r |= ES1371_SRC_RAM_ADDRO(reg);
1145   	es_wr(es, ES1371_REG_SMPRATE, r, 4);
1146   	return (ES1371_SRC_RAM_DATAI(es1371_wait_src_ready(es)));
1147 }
1148 
1149 static void
1150 es1371_src_write(struct es_info *es, unsigned short reg, unsigned short data)
1151 {
1152 	uint32_t r;
1153 
1154 	r = es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1155 	    ES1371_DIS_P2 | ES1371_DIS_R1);
1156 	r |= ES1371_SRC_RAM_ADDRO(reg) |  ES1371_SRC_RAM_DATAO(data);
1157 	es_wr(es, ES1371_REG_SMPRATE, r | ES1371_SRC_RAM_WE, 4);
1158 }
1159 
1160 static unsigned int
1161 es1371_adc_rate(struct es_info *es, unsigned int rate, int set)
1162 {
1163   	unsigned int n, truncm, freq, result;
1164 
1165 	ES_LOCK_ASSERT(es);
1166 
1167   	if (rate > 48000)
1168 		rate = 48000;
1169   	if (rate < 4000)
1170 		rate = 4000;
1171   	n = rate / 3000;
1172   	if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
1173 		n--;
1174   	truncm = (21 * n - 1) | 1;
1175   	freq = ((48000UL << 15) / rate) * n;
1176   	result = (48000UL << 15) / (freq / n);
1177   	if (set) {
1178 		if (rate >= 24000) {
1179 	  		if (truncm > 239)
1180 				truncm = 239;
1181 	  		es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
1182 			    (((239 - truncm) >> 1) << 9) | (n << 4));
1183 		} else {
1184 	  		if (truncm > 119)
1185 				truncm = 119;
1186 	  		es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
1187 			    0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
1188 		}
1189 		es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
1190 		    (es1371_src_read(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS) &
1191 		    0x00ff) | ((freq >> 5) & 0xfc00));
1192 		es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC,
1193 		    freq & 0x7fff);
1194 		es1371_src_write(es, ES_SMPREG_VOL_ADC, n << 8);
1195 		es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, n << 8);
1196 	}
1197 	return (result);
1198 }
1199 
1200 static unsigned int
1201 es1371_dac_rate(struct es_info *es, unsigned int rate, int set)
1202 {
1203   	unsigned int freq, r, result, dac, dis;
1204 
1205 	ES_LOCK_ASSERT(es);
1206 
1207   	if (rate > 48000)
1208 		rate = 48000;
1209   	if (rate < 4000)
1210 		rate = 4000;
1211   	freq = ((rate << 15) + 1500) / 3000;
1212   	result = (freq * 3000) >> 15;
1213 
1214 	dac = (set == ES_DAC1) ? ES_SMPREG_DAC1 : ES_SMPREG_DAC2;
1215 	dis = (set == ES_DAC1) ? ES1371_DIS_P2 : ES1371_DIS_P1;
1216 	r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1217 	    ES1371_DIS_P2 | ES1371_DIS_R1));
1218 	es_wr(es, ES1371_REG_SMPRATE, r, 4);
1219 	es1371_src_write(es, dac + ES_SMPREG_INT_REGS,
1220 	    (es1371_src_read(es, dac + ES_SMPREG_INT_REGS) & 0x00ff) |
1221 	    ((freq >> 5) & 0xfc00));
1222 	es1371_src_write(es, dac + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
1223 	r = (es1371_wait_src_ready(es) &
1224 	    (ES1371_DIS_SRC | dis | ES1371_DIS_R1));
1225 	es_wr(es, ES1371_REG_SMPRATE, r, 4);
1226   	return (result);
1227 }
1228 
1229 static uint32_t
1230 es1371_wait_src_ready(struct es_info *es)
1231 {
1232   	uint32_t t, r;
1233 
1234   	for (t = 0; t < 0x1000; t++) {
1235 		if (!((r = es_rd(es, ES1371_REG_SMPRATE, 4)) &
1236 		    ES1371_SRC_RAM_BUSY))
1237 	  		return (r);
1238 		DELAY(1);
1239   	}
1240 	device_printf(es->dev, "%s: timed out 0x%x [0x%x]\n", __func__,
1241 		ES1371_REG_SMPRATE, r);
1242   	return (0);
1243 }
1244 
1245 /* -------------------------------------------------------------------- */
1246 
1247 /*
1248  * Probe and attach the card
1249  */
1250 
1251 static int
1252 es_pci_probe(device_t dev)
1253 {
1254 	switch(pci_get_devid(dev)) {
1255 	case ES1370_PCI_ID:
1256 		device_set_desc(dev, "AudioPCI ES1370");
1257 		return (BUS_PROBE_DEFAULT);
1258 	case ES1371_PCI_ID:
1259 		switch(pci_get_revid(dev)) {
1260 		case ES1371REV_ES1371_A:
1261 			device_set_desc(dev, "AudioPCI ES1371-A");
1262 			return (BUS_PROBE_DEFAULT);
1263 		case ES1371REV_ES1371_B:
1264 			device_set_desc(dev, "AudioPCI ES1371-B");
1265 			return (BUS_PROBE_DEFAULT);
1266 		case ES1371REV_ES1373_A:
1267 			device_set_desc(dev, "AudioPCI ES1373-A");
1268 			return (BUS_PROBE_DEFAULT);
1269 		case ES1371REV_ES1373_B:
1270 			device_set_desc(dev, "AudioPCI ES1373-B");
1271 			return (BUS_PROBE_DEFAULT);
1272 		case ES1371REV_ES1373_8:
1273 			device_set_desc(dev, "AudioPCI ES1373-8");
1274 			return (BUS_PROBE_DEFAULT);
1275 		case ES1371REV_CT5880_A:
1276 			device_set_desc(dev, "Creative CT5880-A");
1277 			return (BUS_PROBE_DEFAULT);
1278 		default:
1279 			device_set_desc(dev, "AudioPCI ES1371-?");
1280 			device_printf(dev,
1281 			    "unknown revision %d -- please report to "
1282 			    "freebsd-multimedia@freebsd.org\n",
1283 			    pci_get_revid(dev));
1284 			return (BUS_PROBE_DEFAULT);
1285 		}
1286 	case ES1371_PCI_ID2:
1287 		device_set_desc(dev, "Strange AudioPCI ES1371-? (vid=3274)");
1288 		device_printf(dev,
1289 		    "unknown revision %d -- please report to "
1290 		    "freebsd-multimedia@freebsd.org\n", pci_get_revid(dev));
1291 		return (BUS_PROBE_DEFAULT);
1292 	case CT4730_PCI_ID:
1293 		switch(pci_get_revid(dev)) {
1294 		case CT4730REV_CT4730_A:
1295 			device_set_desc(dev,
1296 			    "Creative SB AudioPCI CT4730/EV1938");
1297 			return (BUS_PROBE_DEFAULT);
1298 		default:
1299 			device_set_desc(dev, "Creative SB AudioPCI CT4730-?");
1300 			device_printf(dev,
1301 			    "unknown revision %d -- please report to "
1302 			    "freebsd-multimedia@freebsd.org\n",
1303 			    pci_get_revid(dev));
1304 			return (BUS_PROBE_DEFAULT);
1305 		}
1306 	case CT5880_PCI_ID:
1307 		switch(pci_get_revid(dev)) {
1308 		case CT5880REV_CT5880_C:
1309 			device_set_desc(dev, "Creative CT5880-C");
1310 			return (BUS_PROBE_DEFAULT);
1311 		case CT5880REV_CT5880_D:
1312 			device_set_desc(dev, "Creative CT5880-D");
1313 			return (BUS_PROBE_DEFAULT);
1314 		case CT5880REV_CT5880_E:
1315 			device_set_desc(dev, "Creative CT5880-E");
1316 			return (BUS_PROBE_DEFAULT);
1317 		default:
1318 			device_set_desc(dev, "Creative CT5880-?");
1319 			device_printf(dev,
1320 			    "unknown revision %d -- please report to "
1321 			    "freebsd-multimedia@freebsd.org\n",
1322 			    pci_get_revid(dev));
1323 			return (BUS_PROBE_DEFAULT);
1324 		}
1325 	default:
1326 		return (ENXIO);
1327 	}
1328 }
1329 
1330 #ifdef SND_DYNSYSCTL
1331 static int
1332 sysctl_es137x_spdif_enable(SYSCTL_HANDLER_ARGS)
1333 {
1334 	struct es_info *es;
1335 	device_t dev;
1336 	uint32_t r;
1337 	int err, new_en;
1338 
1339 	dev = oidp->oid_arg1;
1340 	es = pcm_getdevinfo(dev);
1341 	ES_LOCK(es);
1342 	r = es_rd(es, ES1370_REG_STATUS, 4);
1343 	ES_UNLOCK(es);
1344 	new_en = (r & ENABLE_SPDIF) ? 1 : 0;
1345 	err = sysctl_handle_int(oidp, &new_en, sizeof(new_en), req);
1346 
1347 	if (err || req->newptr == NULL)
1348 		return (err);
1349 	if (new_en < 0 || new_en > 1)
1350 		return (EINVAL);
1351 
1352 	ES_LOCK(es);
1353 	if (new_en) {
1354 		r |= ENABLE_SPDIF;
1355 		es->ctrl |= SPDIFEN_B;
1356 		es->ctrl |= RECEN_B;
1357 	} else {
1358 		r &= ~ENABLE_SPDIF;
1359 		es->ctrl &= ~SPDIFEN_B;
1360 		es->ctrl &= ~RECEN_B;
1361 	}
1362 	es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1363 	es_wr(es, ES1370_REG_STATUS, r, 4);
1364 	ES_UNLOCK(es);
1365 
1366 	return (0);
1367 }
1368 
1369 static int
1370 sysctl_es137x_latency_timer(SYSCTL_HANDLER_ARGS)
1371 {
1372 	struct es_info *es;
1373 	device_t dev;
1374 	uint32_t val;
1375 	int err;
1376 
1377 	dev = oidp->oid_arg1;
1378 	es = pcm_getdevinfo(dev);
1379 	ES_LOCK(es);
1380 	val = pci_read_config(dev, PCIR_LATTIMER, 1);
1381 	ES_UNLOCK(es);
1382 	err = sysctl_handle_int(oidp, &val, sizeof(val), req);
1383 
1384 	if (err || req->newptr == NULL)
1385 		return (err);
1386 	if (val > 255)
1387 		return (EINVAL);
1388 
1389 	ES_LOCK(es);
1390 	pci_write_config(dev, PCIR_LATTIMER, val, 1);
1391 	ES_UNLOCK(es);
1392 
1393 	return (0);
1394 }
1395 
1396 static int
1397 sysctl_es137x_fixed_rate(SYSCTL_HANDLER_ARGS)
1398 {
1399 	struct es_info *es;
1400 	device_t dev;
1401 	uint32_t val;
1402 	int err;
1403 
1404 	dev = oidp->oid_arg1;
1405 	es = pcm_getdevinfo(dev);
1406 	ES_LOCK(es);
1407 	val = ES_FIXED_RATE(es->escfg);
1408 	if (val < es_caps.minspeed)
1409 		val = 0;
1410 	ES_UNLOCK(es);
1411 	err = sysctl_handle_int(oidp, &val, sizeof(val), req);
1412 
1413 	if (err || req->newptr == NULL)
1414 		return (err);
1415 	if (val != 0 && (val < es_caps.minspeed || val > es_caps.maxspeed))
1416 		return (EINVAL);
1417 
1418 	ES_LOCK(es);
1419 	if (es->ctrl & (CTRL_DAC2_EN|CTRL_ADC_EN)) {
1420 		ES_UNLOCK(es);
1421 		return (EBUSY);
1422 	}
1423 	if (val) {
1424 		if (val != ES_FIXED_RATE(es->escfg)) {
1425 			es->escfg = ES_SET_FIXED_RATE(es->escfg, val);
1426 			es->ch[ES_DAC2].caps.maxspeed = val;
1427 			es->ch[ES_DAC2].caps.minspeed = val;
1428 			es->ch[ES_ADC].caps.maxspeed = val;
1429 			es->ch[ES_ADC].caps.minspeed = val;
1430 			es->ctrl &= ~CTRL_PCLKDIV;
1431 			es->ctrl |= DAC2_SRTODIV(val) << CTRL_SH_PCLKDIV;
1432 			es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1433 		}
1434 	} else {
1435 		es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
1436 		es->ch[ES_DAC2].caps = es_caps;
1437 		es->ch[ES_ADC].caps = es_caps;
1438 	}
1439 	ES_UNLOCK(es);
1440 
1441 	return (0);
1442 }
1443 
1444 static int
1445 sysctl_es137x_single_pcm_mixer(SYSCTL_HANDLER_ARGS)
1446 {
1447 	struct es_info *es;
1448 	struct snddev_info *d;
1449 	struct snd_mixer *m;
1450 	struct cdev *i_dev;
1451 	device_t dev;
1452 	uint32_t val, set;
1453 	int recsrc, level, err;
1454 
1455 	dev = oidp->oid_arg1;
1456 	d = device_get_softc(dev);
1457 	if (d == NULL || d->mixer_dev == NULL || d->mixer_dev->si_drv1 == NULL)
1458 		return (EINVAL);
1459 	es = d->devinfo;
1460 	if (es == NULL)
1461 		return (EINVAL);
1462 	ES_LOCK(es);
1463 	set = ES_SINGLE_PCM_MIX(es->escfg);
1464 	val = set;
1465 	ES_UNLOCK(es);
1466 	err = sysctl_handle_int(oidp, &val, sizeof(val), req);
1467 
1468 	if (err || req->newptr == NULL)
1469 		return (err);
1470 	if (!(val == 0 || val == 1))
1471 		return (EINVAL);
1472 	if (val == set)
1473 		return (0);
1474 	i_dev = d->mixer_dev;
1475 	if (mixer_ioctl(i_dev, 0, (caddr_t)&recsrc, 0, NULL) != EBADF)
1476 		return (EBUSY);
1477 	err = mixer_ioctl(i_dev, MIXER_READ(SOUND_MIXER_PCM), (caddr_t)&level,
1478 	    -1, NULL);
1479 	if (!err)
1480 		err = mixer_ioctl(i_dev, MIXER_READ(SOUND_MIXER_RECSRC),
1481 		    (caddr_t)&recsrc, -1, NULL);
1482 	if (err)
1483 		return (err);
1484 	if (level < 0)
1485 		return (EINVAL);
1486 
1487 	ES_LOCK(es);
1488 	if (es->ctrl & (CTRL_ADC_EN | CTRL_DAC1_EN | CTRL_DAC2_EN)) {
1489 		ES_UNLOCK(es);
1490 		return (EBUSY);
1491 	}
1492 	if (val)
1493 		es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 1);
1494 	else
1495 		es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 0);
1496 	ES_UNLOCK(es);
1497 	m = i_dev->si_drv1;
1498 	if (!val) {
1499 		mix_setdevs(m, mix_getdevs(d->mixer_dev->si_drv1) |
1500 		    (1 << SOUND_MIXER_SYNTH));
1501 		mix_setrecdevs(m, mix_getrecdevs(d->mixer_dev->si_drv1) |
1502 		    (1 << SOUND_MIXER_SYNTH));
1503 		err = mixer_ioctl(i_dev, MIXER_WRITE(SOUND_MIXER_SYNTH),
1504 		    (caddr_t)&level, -1, NULL);
1505 	} else {
1506 		err = mixer_ioctl(i_dev, MIXER_WRITE(SOUND_MIXER_SYNTH),
1507 		    (caddr_t)&level, -1, NULL);
1508 		mix_setdevs(m, mix_getdevs(d->mixer_dev->si_drv1) &
1509 		    ~(1 << SOUND_MIXER_SYNTH));
1510 		mix_setrecdevs(m, mix_getrecdevs(d->mixer_dev->si_drv1) &
1511 		    ~(1 << SOUND_MIXER_SYNTH));
1512 	}
1513 	if (!err) {
1514 		level = recsrc;
1515 		if (recsrc & (1 << SOUND_MIXER_PCM))
1516 			recsrc |= 1 << SOUND_MIXER_SYNTH;
1517 		else if (recsrc & (1 << SOUND_MIXER_SYNTH))
1518 			recsrc |= 1 << SOUND_MIXER_PCM;
1519 		if (level != recsrc)
1520 			err = mixer_ioctl(i_dev,
1521 			    MIXER_WRITE(SOUND_MIXER_RECSRC),
1522 			    (caddr_t)&recsrc, -1, NULL);
1523 	}
1524 	return (err);
1525 }
1526 
1527 static int
1528 sysctl_es_polling(SYSCTL_HANDLER_ARGS)
1529 {
1530 	struct es_info *es;
1531 	device_t dev;
1532 	int err, val;
1533 
1534 	dev = oidp->oid_arg1;
1535 	es = pcm_getdevinfo(dev);
1536 	if (es == NULL)
1537 		return (EINVAL);
1538 	ES_LOCK(es);
1539 	val = es->polling;
1540 	ES_UNLOCK(es);
1541 	err = sysctl_handle_int(oidp, &val, sizeof(val), req);
1542 
1543 	if (err || req->newptr == NULL)
1544 		return (err);
1545 	if (val < 0 || val > 1)
1546 		return (EINVAL);
1547 
1548 	ES_LOCK(es);
1549 	if (val != es->polling) {
1550 		if (es_chan_active(es) != 0)
1551 			err = EBUSY;
1552 		else if (val == 0)
1553 			es->polling = 0;
1554 		else
1555 			es->polling = 1;
1556 	}
1557 	ES_UNLOCK(es);
1558 
1559 	return (err);
1560 }
1561 #endif /* SND_DYNSYSCTL */
1562 
1563 static void
1564 es_init_sysctls(device_t dev)
1565 {
1566 #ifdef SND_DYNSYSCTL
1567 	struct es_info *es;
1568 	int r, devid, revid;
1569 
1570 	devid = pci_get_devid(dev);
1571 	revid = pci_get_revid(dev);
1572 	es = pcm_getdevinfo(dev);
1573 	if ((devid == ES1371_PCI_ID && revid == ES1371REV_ES1373_8) ||
1574 		    (devid == ES1371_PCI_ID && revid == ES1371REV_CT5880_A) ||
1575 		    (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_C) ||
1576 		    (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_D) ||
1577 		    (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_E)) {
1578 		/* XXX: an user should be able to set this with a control tool,
1579 		   if not done before 7.0-RELEASE, this needs to be converted
1580 		   to a device specific sysctl "dev.pcm.X.yyy" via
1581 		   device_get_sysctl_*() as discussed on multimedia@ in msg-id
1582 		   <861wujij2q.fsf@xps.des.no> */
1583 		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1584 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1585 		    "spdif_enabled", CTLTYPE_INT | CTLFLAG_RW, dev, sizeof(dev),
1586 		    sysctl_es137x_spdif_enable, "I",
1587 		    "Enable S/PDIF output on primary playback channel");
1588 	} else if (devid == ES1370_PCI_ID) {
1589 		/*
1590 		 * Enable fixed rate sysctl if both DAC2 / ADC enabled.
1591 		 */
1592 		if (es->ch[ES_DAC2].channel != NULL &&
1593 		    es->ch[ES_ADC].channel != NULL) {
1594 		/* XXX: an user should be able to set this with a control tool,
1595 		   if not done before 7.0-RELEASE, this needs to be converted
1596 		   to a device specific sysctl "dev.pcm.X.yyy" via
1597 		   device_get_sysctl_*() as discussed on multimedia@ in msg-id
1598 		   <861wujij2q.fsf@xps.des.no> */
1599 			SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1600 			    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1601 			    OID_AUTO, "fixed_rate", CTLTYPE_INT | CTLFLAG_RW,
1602 			    dev, sizeof(dev), sysctl_es137x_fixed_rate, "I",
1603 			    "Enable fixed rate playback/recording");
1604 		}
1605 		/*
1606 		 * Enable single pcm mixer sysctl if both DAC1/2 enabled.
1607 		 */
1608 		if (es->ch[ES_DAC1].channel != NULL &&
1609 		    es->ch[ES_DAC2].channel != NULL) {
1610 		/* XXX: an user should be able to set this with a control tool,
1611 		   if not done before 7.0-RELEASE, this needs to be converted
1612 		   to a device specific sysctl "dev.pcm.X.yyy" via
1613 		   device_get_sysctl_*() as discussed on multimedia@ in msg-id
1614 		   <861wujij2q.fsf@xps.des.no> */
1615 			SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1616 			    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1617 			    OID_AUTO, "single_pcm_mixer",
1618 			    CTLTYPE_INT | CTLFLAG_RW, dev, sizeof(dev),
1619 			    sysctl_es137x_single_pcm_mixer, "I",
1620 			    "Single PCM mixer controller for both DAC1/DAC2");
1621 		}
1622 	}
1623 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1624 	    "latency_timer", &r) == 0 && !(r < 0 || r > 255))
1625 		pci_write_config(dev, PCIR_LATTIMER, r, 1);
1626 	/* XXX: this needs to be converted to a device specific sysctl
1627 	   "dev.pcm.X.yyy" via device_get_sysctl_*() as discussed on
1628 	   multimedia@ in msg-id <861wujij2q.fsf@xps.des.no> */
1629 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1630 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1631 	    "latency_timer", CTLTYPE_INT | CTLFLAG_RW, dev, sizeof(dev),
1632 	    sysctl_es137x_latency_timer, "I",
1633 	    "PCI Latency Timer configuration");
1634 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1635 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1636 	    "polling", CTLTYPE_INT | CTLFLAG_RW, dev, sizeof(dev),
1637 	    sysctl_es_polling, "I",
1638 	    "Enable polling mode");
1639 #endif /* SND_DYNSYSCTL */
1640 }
1641 
1642 static int
1643 es_pci_attach(device_t dev)
1644 {
1645 	uint32_t	data;
1646 	struct es_info *es = NULL;
1647 	int		mapped, i, numplay, dac_cfg;
1648 	char		status[SND_STATUSLEN];
1649 	struct ac97_info *codec = NULL;
1650 	kobj_class_t    ct = NULL;
1651 	uint32_t devid;
1652 
1653 	if ((es = malloc(sizeof *es, M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
1654 		device_printf(dev, "cannot allocate softc\n");
1655 		return (ENXIO);
1656 	}
1657 	es->lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc");
1658 	es->dev = dev;
1659 	es->escfg = 0;
1660 	mapped = 0;
1661 
1662 	pci_enable_busmaster(dev);
1663 	data = pci_read_config(dev, PCIR_COMMAND, 2);
1664 	data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
1665 	pci_write_config(dev, PCIR_COMMAND, data, 2);
1666 	data = pci_read_config(dev, PCIR_COMMAND, 2);
1667 	if (mapped == 0 && (data & PCIM_CMD_MEMEN)) {
1668 		es->regid = MEM_MAP_REG;
1669 		es->regtype = SYS_RES_MEMORY;
1670 		es->reg = bus_alloc_resource_any(dev, es->regtype, &es->regid,
1671 		    RF_ACTIVE);
1672 		if (es->reg)
1673 			mapped++;
1674 	}
1675 	if (mapped == 0 && (data & PCIM_CMD_PORTEN)) {
1676 		es->regid = PCIR_BAR(0);
1677 		es->regtype = SYS_RES_IOPORT;
1678 		es->reg = bus_alloc_resource_any(dev, es->regtype, &es->regid,
1679 		    RF_ACTIVE);
1680 		if (es->reg)
1681 			mapped++;
1682 	}
1683 	if (mapped == 0) {
1684 		device_printf(dev, "unable to map register space\n");
1685 		goto bad;
1686 	}
1687 
1688 	es->st = rman_get_bustag(es->reg);
1689 	es->sh = rman_get_bushandle(es->reg);
1690 	callout_init(&es->poll_timer, CALLOUT_MPSAFE);
1691 	es->poll_ticks = 1;
1692 
1693 	if (resource_int_value(device_get_name(dev),
1694 	    device_get_unit(dev), "polling", &i) == 0 && i != 0)
1695 		es->polling = 1;
1696 	else
1697 		es->polling = 0;
1698 
1699 	es->bufsz = pcm_getbuffersize(dev, 4096, ES_DEFAULT_BUFSZ, 65536);
1700 	if (resource_int_value(device_get_name(dev),
1701 	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
1702 		i &= ~0x3f;
1703 		if (i < 0x40)
1704 			i = 0x40;
1705 		es->blkcnt = es->bufsz / i;
1706 		i = 0;
1707 		while (es->blkcnt >> i)
1708 			i++;
1709 		es->blkcnt = 1 << (i - 1);
1710 		if (es->blkcnt < 2)
1711 			es->blkcnt = 2;
1712 		else if (es->blkcnt > 256)
1713 			es->blkcnt = 256;
1714 
1715 	} else
1716 		es->blkcnt = 2;
1717 
1718 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1719 	    "dac", &dac_cfg) == 0) {
1720 		if (dac_cfg < 0 || dac_cfg > 3)
1721 			dac_cfg = ES_DEFAULT_DAC_CFG;
1722 	} else
1723 		dac_cfg = ES_DEFAULT_DAC_CFG;
1724 
1725 	switch (dac_cfg) {
1726 	case 0:	/* Enable all DAC: DAC1, DAC2 */
1727 		numplay = 2;
1728 		es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC1);
1729 		es->escfg = ES_SET_DAC_SECOND(es->escfg, ES_DAC2);
1730 		break;
1731 	case 1: /* Only DAC1 */
1732 		numplay = 1;
1733 		es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC1);
1734 		break;
1735 	case 3: /* Enable all DAC / swap position: DAC2, DAC1 */
1736 		numplay = 2;
1737 		es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC2);
1738 		es->escfg = ES_SET_DAC_SECOND(es->escfg, ES_DAC1);
1739 		break;
1740 	case 2: /* Only DAC2 */
1741 	default:
1742 		numplay = 1;
1743 		es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC2);
1744 		break;
1745 	}
1746 	es->escfg = ES_SET_NUMPLAY(es->escfg, numplay);
1747 	es->escfg = ES_SET_NUMREC(es->escfg, 1);
1748 
1749 	devid = pci_get_devid(dev);
1750 	switch (devid) {
1751 	case ES1371_PCI_ID:
1752 	case ES1371_PCI_ID2:
1753 	case CT5880_PCI_ID:
1754 	case CT4730_PCI_ID:
1755 		es1371_init(es);
1756 		codec = AC97_CREATE(dev, es, es1371_ac97);
1757 	  	if (codec == NULL)
1758 			goto bad;
1759 	  	/* our init routine does everything for us */
1760 	  	/* set to NULL; flag mixer_init not to run the ac97_init */
1761 	  	/*	  ac97_mixer.init = NULL;  */
1762 		if (mixer_init(dev, ac97_getmixerclass(), codec))
1763 			goto bad;
1764 		ct = &eschan1371_class;
1765 		break;
1766 	case ES1370_PCI_ID:
1767 	  	es1370_init(es);
1768 		/*
1769 		 * Disable fixed rate operation if DAC2 disabled.
1770 		 * This is a special case for es1370 only, where the
1771 		 * speed of both ADC and DAC2 locked together.
1772 		 */
1773 		if (!ES_DAC2_ENABLED(es->escfg))
1774 			es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
1775 	  	if (mixer_init(dev, &es1370_mixer_class, es))
1776 			goto bad;
1777 		ct = &eschan1370_class;
1778 		break;
1779 	default:
1780 		goto bad;
1781 		/* NOTREACHED */
1782 	}
1783 
1784 	es->irqid = 0;
1785 	es->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &es->irqid,
1786 	    RF_ACTIVE | RF_SHAREABLE);
1787 	if (!es->irq || snd_setup_intr(dev, es->irq, INTR_MPSAFE, es_intr,
1788 	    es, &es->ih)) {
1789 		device_printf(dev, "unable to map interrupt\n");
1790 		goto bad;
1791 	}
1792 
1793 	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
1794 		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1795 		/*highaddr*/BUS_SPACE_MAXADDR,
1796 		/*filter*/NULL, /*filterarg*/NULL,
1797 		/*maxsize*/es->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
1798 		/*flags*/0, /*lockfunc*/NULL,
1799 		/*lockarg*/NULL, &es->parent_dmat) != 0) {
1800 		device_printf(dev, "unable to create dma tag\n");
1801 		goto bad;
1802 	}
1803 
1804 	snprintf(status, SND_STATUSLEN, "at %s 0x%lx irq %ld %s",
1805 	    (es->regtype == SYS_RES_IOPORT)? "io" : "memory",
1806 	    rman_get_start(es->reg), rman_get_start(es->irq),
1807 	    PCM_KLDSTRING(snd_es137x));
1808 
1809 	if (pcm_register(dev, es, numplay, 1))
1810 		goto bad;
1811 	for (i = 0; i < numplay; i++)
1812 		pcm_addchan(dev, PCMDIR_PLAY, ct, es);
1813 	pcm_addchan(dev, PCMDIR_REC, ct, es);
1814 	es_init_sysctls(dev);
1815 	pcm_setstatus(dev, status);
1816 	es->escfg = ES_SET_GP(es->escfg, 0);
1817 	if (numplay == 1)
1818 		device_printf(dev, "<Playback: DAC%d / Record: ADC>\n",
1819 		    ES_DAC_FIRST(es->escfg) + 1);
1820 	else if (numplay == 2)
1821 		device_printf(dev, "<Playback: DAC%d,DAC%d / Record: ADC>\n",
1822 		    ES_DAC_FIRST(es->escfg) + 1, ES_DAC_SECOND(es->escfg) + 1);
1823 	return (0);
1824 
1825 bad:
1826 	if (es->parent_dmat)
1827 		bus_dma_tag_destroy(es->parent_dmat);
1828 	if (es->ih)
1829 		bus_teardown_intr(dev, es->irq, es->ih);
1830 	if (es->irq)
1831 		bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq);
1832 	if (codec)
1833 		ac97_destroy(codec);
1834 	if (es->reg)
1835 		bus_release_resource(dev, es->regtype, es->regid, es->reg);
1836 	if (es->lock)
1837 		snd_mtxfree(es->lock);
1838 	if (es)
1839 		free(es, M_DEVBUF);
1840 	return (ENXIO);
1841 }
1842 
1843 static int
1844 es_pci_detach(device_t dev)
1845 {
1846 	int r;
1847 	struct es_info *es;
1848 
1849 	r = pcm_unregister(dev);
1850 	if (r)
1851 		return (r);
1852 
1853 	es = pcm_getdevinfo(dev);
1854 	bus_teardown_intr(dev, es->irq, es->ih);
1855 	bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq);
1856 	bus_release_resource(dev, es->regtype, es->regid, es->reg);
1857 	bus_dma_tag_destroy(es->parent_dmat);
1858 	snd_mtxfree(es->lock);
1859 	free(es, M_DEVBUF);
1860 
1861 	return (0);
1862 }
1863 
1864 static device_method_t es_methods[] = {
1865 	/* Device interface */
1866 	DEVMETHOD(device_probe,		es_pci_probe),
1867 	DEVMETHOD(device_attach,	es_pci_attach),
1868 	DEVMETHOD(device_detach,	es_pci_detach),
1869 
1870 	{ 0, 0 }
1871 };
1872 
1873 static driver_t es_driver = {
1874 	"pcm",
1875 	es_methods,
1876 	PCM_SOFTC_SIZE,
1877 };
1878 
1879 DRIVER_MODULE(snd_es137x, pci, es_driver, pcm_devclass, 0, 0);
1880 MODULE_DEPEND(snd_es137x, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1881 MODULE_VERSION(snd_es137x, 1);
1882