xref: /freebsd/sys/dev/sound/pci/solo.c (revision 3d11b6c8f01e1fca5936a11d6996448467851a94)
1 /*-
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <dev/sound/pcm/sound.h>
27 
28 #include <dev/pci/pcireg.h>
29 #include <dev/pci/pcivar.h>
30 
31 #include  <dev/sound/isa/sb.h>
32 #include  <dev/sound/chip.h>
33 
34 #include "mixer_if.h"
35 
36 SND_DECLARE_FILE("$FreeBSD$");
37 
38 #define SOLO_DEFAULT_BUFSZ 16384
39 #define ABS(x) (((x) < 0)? -(x) : (x))
40 
41 /* if defined, playback always uses the 2nd channel and full duplex works */
42 #define ESS18XX_DUPLEX	1
43 
44 /* more accurate clocks and split audio1/audio2 rates */
45 #define ESS18XX_NEWSPEED
46 
47 /* 1 = INTR_MPSAFE, 0 = GIANT */
48 #define ESS18XX_MPSAFE	1
49 
50 static u_int32_t ess_playfmt[] = {
51 	AFMT_U8,
52 	AFMT_STEREO | AFMT_U8,
53 	AFMT_S8,
54 	AFMT_STEREO | AFMT_S8,
55 	AFMT_S16_LE,
56 	AFMT_STEREO | AFMT_S16_LE,
57 	AFMT_U16_LE,
58 	AFMT_STEREO | AFMT_U16_LE,
59 	0
60 };
61 static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_playfmt, 0};
62 
63 /*
64  * Recording output is byte-swapped
65  */
66 static u_int32_t ess_recfmt[] = {
67 	AFMT_U8,
68 	AFMT_STEREO | AFMT_U8,
69 	AFMT_S8,
70 	AFMT_STEREO | AFMT_S8,
71 	AFMT_S16_BE,
72 	AFMT_STEREO | AFMT_S16_BE,
73 	AFMT_U16_BE,
74 	AFMT_STEREO | AFMT_U16_BE,
75 	0
76 };
77 static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_recfmt, 0};
78 
79 struct ess_info;
80 
81 struct ess_chinfo {
82 	struct ess_info *parent;
83 	struct pcm_channel *channel;
84 	struct snd_dbuf *buffer;
85 	int dir, hwch, stopping;
86 	u_int32_t fmt, spd, blksz;
87 };
88 
89 struct ess_info {
90     	struct resource *io, *sb, *vc, *mpu, *gp;	/* I/O address for the board */
91     	struct resource *irq;
92 	void		*ih;
93     	bus_dma_tag_t parent_dmat;
94 
95     	int simplex_dir, type, duplex:1, newspeed:1, dmasz[2];
96 	unsigned int bufsz;
97 
98     	struct ess_chinfo pch, rch;
99 #if ESS18XX_MPSAFE == 1
100 	struct mtx *lock;
101 #endif
102 };
103 
104 #if ESS18XX_MPSAFE == 1
105 #define ess_lock(_ess) snd_mtxlock((_ess)->lock)
106 #define ess_unlock(_ess) snd_mtxunlock((_ess)->lock)
107 #define ess_lock_assert(_ess) snd_mtxassert((_ess)->lock)
108 #else
109 #define ess_lock(_ess)
110 #define ess_unlock(_ess)
111 #define ess_lock_assert(_ess)
112 #endif
113 
114 static int ess_rd(struct ess_info *sc, int reg);
115 static void ess_wr(struct ess_info *sc, int reg, u_int8_t val);
116 static int ess_dspready(struct ess_info *sc);
117 static int ess_cmd(struct ess_info *sc, u_char val);
118 static int ess_cmd1(struct ess_info *sc, u_char cmd, int val);
119 static int ess_get_byte(struct ess_info *sc);
120 static void ess_setmixer(struct ess_info *sc, u_int port, u_int value);
121 static int ess_getmixer(struct ess_info *sc, u_int port);
122 static int ess_reset_dsp(struct ess_info *sc);
123 
124 static int ess_write(struct ess_info *sc, u_char reg, int val);
125 static int ess_read(struct ess_info *sc, u_char reg);
126 
127 static void ess_intr(void *arg);
128 static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len);
129 static int ess_start(struct ess_chinfo *ch);
130 static int ess_stop(struct ess_chinfo *ch);
131 
132 static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir);
133 static int ess_dmapos(struct ess_info *sc, int ch);
134 static int ess_dmatrigger(struct ess_info *sc, int ch, int go);
135 
136 /*
137  * Common code for the midi and pcm functions
138  *
139  * ess_cmd write a single byte to the CMD port.
140  * ess_cmd1 write a CMD + 1 byte arg
141  * ess_cmd2 write a CMD + 2 byte arg
142  * ess_get_byte returns a single byte from the DSP data port
143  *
144  * ess_write is actually ess_cmd1
145  * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte
146  */
147 
148 static int
149 port_rd(struct resource *port, int regno, int size)
150 {
151 	bus_space_tag_t st = rman_get_bustag(port);
152 	bus_space_handle_t sh = rman_get_bushandle(port);
153 
154 	switch (size) {
155 	case 1:
156 		return bus_space_read_1(st, sh, regno);
157 	case 2:
158 		return bus_space_read_2(st, sh, regno);
159 	case 4:
160 		return bus_space_read_4(st, sh, regno);
161 	default:
162 		return 0xffffffff;
163 	}
164 }
165 
166 static void
167 port_wr(struct resource *port, int regno, u_int32_t data, int size)
168 {
169 	bus_space_tag_t st = rman_get_bustag(port);
170 	bus_space_handle_t sh = rman_get_bushandle(port);
171 
172 	switch (size) {
173 	case 1:
174 		bus_space_write_1(st, sh, regno, data);
175 		break;
176 	case 2:
177 		bus_space_write_2(st, sh, regno, data);
178 		break;
179 	case 4:
180 		bus_space_write_4(st, sh, regno, data);
181 		break;
182 	}
183 }
184 
185 static int
186 ess_rd(struct ess_info *sc, int reg)
187 {
188 	return port_rd(sc->sb, reg, 1);
189 }
190 
191 static void
192 ess_wr(struct ess_info *sc, int reg, u_int8_t val)
193 {
194 	port_wr(sc->sb, reg, val, 1);
195 }
196 
197 static int
198 ess_dspready(struct ess_info *sc)
199 {
200 	return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0);
201 }
202 
203 static int
204 ess_dspwr(struct ess_info *sc, u_char val)
205 {
206     	int  i;
207 
208     	for (i = 0; i < 1000; i++) {
209 		if (ess_dspready(sc)) {
210 	    		ess_wr(sc, SBDSP_CMD, val);
211 	    		return 1;
212 		}
213 		if (i > 10) DELAY((i > 100)? 1000 : 10);
214     	}
215     	printf("ess_dspwr(0x%02x) timed out.\n", val);
216     	return 0;
217 }
218 
219 static int
220 ess_cmd(struct ess_info *sc, u_char val)
221 {
222 	DEB(printf("ess_cmd: %x\n", val));
223     	return ess_dspwr(sc, val);
224 }
225 
226 static int
227 ess_cmd1(struct ess_info *sc, u_char cmd, int val)
228 {
229     	DEB(printf("ess_cmd1: %x, %x\n", cmd, val));
230     	if (ess_dspwr(sc, cmd)) {
231 		return ess_dspwr(sc, val & 0xff);
232     	} else return 0;
233 }
234 
235 static void
236 ess_setmixer(struct ess_info *sc, u_int port, u_int value)
237 {
238 	DEB(printf("ess_setmixer: reg=%x, val=%x\n", port, value);)
239     	ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
240     	DELAY(10);
241     	ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff));
242     	DELAY(10);
243 }
244 
245 static int
246 ess_getmixer(struct ess_info *sc, u_int port)
247 {
248     	int val;
249 
250     	ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
251     	DELAY(10);
252     	val = ess_rd(sc, SB_MIX_DATA);
253     	DELAY(10);
254 
255     	return val;
256 }
257 
258 static int
259 ess_get_byte(struct ess_info *sc)
260 {
261     	int i;
262 
263     	for (i = 1000; i > 0; i--) {
264 		if (ess_rd(sc, 0xc) & 0x40)
265 			return ess_rd(sc, DSP_READ);
266 		else
267 			DELAY(20);
268     	}
269     	return -1;
270 }
271 
272 static int
273 ess_write(struct ess_info *sc, u_char reg, int val)
274 {
275     	return ess_cmd1(sc, reg, val);
276 }
277 
278 static int
279 ess_read(struct ess_info *sc, u_char reg)
280 {
281     	return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1;
282 }
283 
284 static int
285 ess_reset_dsp(struct ess_info *sc)
286 {
287 	DEB(printf("ess_reset_dsp\n"));
288     	ess_wr(sc, SBDSP_RST, 3);
289     	DELAY(100);
290     	ess_wr(sc, SBDSP_RST, 0);
291     	if (ess_get_byte(sc) != 0xAA) {
292         	DEB(printf("ess_reset_dsp failed\n"));
293 /*
294 			   rman_get_start(d->io_base)));
295 */
296 		return ENXIO;	/* Sorry */
297     	}
298     	ess_cmd(sc, 0xc6);
299     	return 0;
300 }
301 
302 static void
303 ess_intr(void *arg)
304 {
305     	struct ess_info *sc = (struct ess_info *)arg;
306 	int src, pirq = 0, rirq = 0;
307 
308 	ess_lock(sc);
309 	src = 0;
310 	if (ess_getmixer(sc, 0x7a) & 0x80)
311 		src |= 2;
312 	if (ess_rd(sc, 0x0c) & 0x01)
313 		src |= 1;
314 
315 	if (src == 0)
316 		return;
317 
318 	if (sc->duplex) {
319 		pirq = (src & sc->pch.hwch)? 1 : 0;
320 		rirq = (src & sc->rch.hwch)? 1 : 0;
321 	} else {
322 		if (sc->simplex_dir == PCMDIR_PLAY)
323 			pirq = 1;
324 		if (sc->simplex_dir == PCMDIR_REC)
325 			rirq = 1;
326 		if (!pirq && !rirq)
327 			printf("solo: IRQ neither playback nor rec!\n");
328 	}
329 
330 	DEB(printf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq));
331 
332 	if (pirq) {
333 		if (sc->pch.stopping) {
334 			ess_dmatrigger(sc, sc->pch.hwch, 0);
335 			sc->pch.stopping = 0;
336 			if (sc->pch.hwch == 1)
337 				ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
338 			else
339 				ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03);
340 		}
341 		ess_unlock(sc);
342 		chn_intr(sc->pch.channel);
343 		ess_lock(sc);
344 	}
345 
346 	if (rirq) {
347 		if (sc->rch.stopping) {
348 			ess_dmatrigger(sc, sc->rch.hwch, 0);
349 			sc->rch.stopping = 0;
350 			/* XXX: will this stop audio2? */
351 			ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
352 		}
353 		ess_unlock(sc);
354 		chn_intr(sc->rch.channel);
355 		ess_lock(sc);
356 	}
357 
358 	if (src & 2)
359 		ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80);
360 	if (src & 1)
361     		ess_rd(sc, DSP_DATA_AVAIL);
362 
363 	ess_unlock(sc);
364 }
365 
366 /* utility functions for ESS */
367 static u_int8_t
368 ess_calcspeed8(int *spd)
369 {
370 	int speed = *spd;
371 	u_int32_t t;
372 
373 	if (speed > 22000) {
374 		t = (795500 + speed / 2) / speed;
375 		speed = (795500 + t / 2) / t;
376 		t = (256 - t) | 0x80;
377 	} else {
378 		t = (397700 + speed / 2) / speed;
379 		speed = (397700 + t / 2) / t;
380 		t = 128 - t;
381 	}
382 	*spd = speed;
383 	return t & 0x000000ff;
384 }
385 
386 static u_int8_t
387 ess_calcspeed9(int *spd)
388 {
389 	int speed, s0, s1, use0;
390 	u_int8_t t0, t1;
391 
392 	/* rate = source / (256 - divisor) */
393 	/* divisor = 256 - (source / rate) */
394 	speed = *spd;
395 	t0 = 128 - (793800 / speed);
396 	s0 = 793800 / (128 - t0);
397 
398 	t1 = 128 - (768000 / speed);
399 	s1 = 768000 / (128 - t1);
400 	t1 |= 0x80;
401 
402 	use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0;
403 
404 	*spd = use0? s0 : s1;
405 	return use0? t0 : t1;
406 }
407 
408 static u_int8_t
409 ess_calcfilter(int spd)
410 {
411 	int cutoff;
412 
413 	/* cutoff = 7160000 / (256 - divisor) */
414 	/* divisor = 256 - (7160000 / cutoff) */
415 	cutoff = (spd * 9 * 82) / 20;
416 	return (256 - (7160000 / cutoff));
417 }
418 
419 static int
420 ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
421 {
422 	int play = (dir == PCMDIR_PLAY)? 1 : 0;
423 	int b16 = (fmt & AFMT_16BIT)? 1 : 0;
424 	int stereo = (fmt & AFMT_STEREO)? 1 : 0;
425 	int unsign = (fmt == AFMT_U8 || fmt == AFMT_U16_LE || fmt == AFMT_U16_BE)? 1 : 0;
426 	u_int8_t spdval, fmtval;
427 
428 	DEB(printf("ess_setupch\n"));
429 	spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd);
430 
431 	sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ;
432 
433 	if (ch == 1) {
434 		KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad"));
435 		len = -len;
436 		/* transfer length low */
437 		ess_write(sc, 0xa4, len & 0x00ff);
438 		/* transfer length high */
439 		ess_write(sc, 0xa5, (len & 0xff00) >> 8);
440 		/* autoinit, dma dir */
441 		ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a));
442 		/* mono/stereo */
443 		ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02));
444 		/* demand mode, 4 bytes/xfer */
445 		ess_write(sc, 0xb9, 0x02);
446 		/* sample rate */
447         	ess_write(sc, 0xa1, spdval);
448 		/* filter cutoff */
449 		ess_write(sc, 0xa2, ess_calcfilter(spd));
450 		/* setup dac/adc */
451 		/*
452 		if (play)
453 			ess_write(sc, 0xb6, unsign? 0x80 : 0x00);
454 		*/
455 		/* mono, b16: signed, load signal */
456 		/*
457 		ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20));
458 		*/
459 		/* setup fifo */
460 		ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) |
461 					   (b16? 0x04 : 0x00) |
462 					   (stereo? 0x08 : 0x40));
463 		/* irq control */
464 		ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50);
465 		/* drq control */
466 		ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50);
467 	} else if (ch == 2) {
468 		KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad"));
469 		len >>= 1;
470 		len = -len;
471 		/* transfer length low */
472 		ess_setmixer(sc, 0x74, len & 0x00ff);
473 		/* transfer length high */
474 		ess_setmixer(sc, 0x76, (len & 0xff00) >> 8);
475 		/* autoinit, 4 bytes/req */
476 		ess_setmixer(sc, 0x78, 0x10);
477 		fmtval = b16 | (stereo << 1) | ((!unsign) << 2);
478 		/* enable irq, set format */
479 		ess_setmixer(sc, 0x7a, 0x40 | fmtval);
480 		if (sc->newspeed) {
481 			/* sample rate */
482 			ess_setmixer(sc, 0x70, spdval);
483 			/* filter cutoff */
484 			ess_setmixer(sc, 0x72, ess_calcfilter(spd));
485 		}
486 
487 	}
488 	return 0;
489 }
490 static int
491 ess_start(struct ess_chinfo *ch)
492 {
493 	struct ess_info *sc = ch->parent;
494 
495 	DEB(printf("ess_start\n"););
496 	ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz);
497 	ch->stopping = 0;
498 	if (ch->hwch == 1) {
499 		ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01);
500 		if (ch->dir == PCMDIR_PLAY) {
501 #if 0
502 			DELAY(100000); /* 100 ms */
503 #endif
504 			ess_cmd(sc, 0xd1);
505 		}
506 	} else
507 		ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03);
508 	return 0;
509 }
510 
511 static int
512 ess_stop(struct ess_chinfo *ch)
513 {
514 	struct ess_info *sc = ch->parent;
515 
516 	DEB(printf("ess_stop\n"));
517 	ch->stopping = 1;
518 	if (ch->hwch == 1)
519 		ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04);
520 	else
521 		ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10);
522 	DEB(printf("done with stop\n"));
523 	return 0;
524 }
525 
526 /* -------------------------------------------------------------------- */
527 /* channel interface for ESS18xx */
528 static void *
529 esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
530 {
531 	struct ess_info *sc = devinfo;
532 	struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
533 
534 	DEB(printf("esschan_init\n"));
535 	ch->parent = sc;
536 	ch->channel = c;
537 	ch->buffer = b;
538 	ch->dir = dir;
539 	if (sndbuf_alloc(ch->buffer, sc->parent_dmat, sc->bufsz) != 0)
540 		return NULL;
541 	ch->hwch = 1;
542 	if ((dir == PCMDIR_PLAY) && (sc->duplex))
543 		ch->hwch = 2;
544 	return ch;
545 }
546 
547 static int
548 esschan_setformat(kobj_t obj, void *data, u_int32_t format)
549 {
550 	struct ess_chinfo *ch = data;
551 
552 	ch->fmt = format;
553 	return 0;
554 }
555 
556 static int
557 esschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
558 {
559 	struct ess_chinfo *ch = data;
560 	struct ess_info *sc = ch->parent;
561 
562 	ch->spd = speed;
563 	if (sc->newspeed)
564 		ess_calcspeed9(&ch->spd);
565 	else
566 		ess_calcspeed8(&ch->spd);
567 	return ch->spd;
568 }
569 
570 static int
571 esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
572 {
573 	struct ess_chinfo *ch = data;
574 
575 	ch->blksz = blocksize;
576 	return ch->blksz;
577 }
578 
579 static int
580 esschan_trigger(kobj_t obj, void *data, int go)
581 {
582 	struct ess_chinfo *ch = data;
583 	struct ess_info *sc = ch->parent;
584 
585 	DEB(printf("esschan_trigger: %d\n",go));
586 	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
587 		return 0;
588 
589 	ess_lock(sc);
590 	switch (go) {
591 	case PCMTRIG_START:
592 		ess_dmasetup(sc, ch->hwch, sndbuf_getbufaddr(ch->buffer), sndbuf_getsize(ch->buffer), ch->dir);
593 		ess_dmatrigger(sc, ch->hwch, 1);
594 		ess_start(ch);
595 		break;
596 
597 	case PCMTRIG_STOP:
598 	case PCMTRIG_ABORT:
599 	default:
600 		ess_stop(ch);
601 		break;
602 	}
603 	ess_unlock(sc);
604 	return 0;
605 }
606 
607 static int
608 esschan_getptr(kobj_t obj, void *data)
609 {
610 	struct ess_chinfo *ch = data;
611 	struct ess_info *sc = ch->parent;
612 	int ret;
613 
614 	ess_lock(sc);
615 	ret = ess_dmapos(sc, ch->hwch);
616 	ess_unlock(sc);
617 	return ret;
618 }
619 
620 static struct pcmchan_caps *
621 esschan_getcaps(kobj_t obj, void *data)
622 {
623 	struct ess_chinfo *ch = data;
624 
625 	return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
626 }
627 
628 static kobj_method_t esschan_methods[] = {
629     	KOBJMETHOD(channel_init,		esschan_init),
630     	KOBJMETHOD(channel_setformat,		esschan_setformat),
631     	KOBJMETHOD(channel_setspeed,		esschan_setspeed),
632     	KOBJMETHOD(channel_setblocksize,	esschan_setblocksize),
633     	KOBJMETHOD(channel_trigger,		esschan_trigger),
634     	KOBJMETHOD(channel_getptr,		esschan_getptr),
635     	KOBJMETHOD(channel_getcaps,		esschan_getcaps),
636 	{ 0, 0 }
637 };
638 CHANNEL_DECLARE(esschan);
639 
640 /************************************************************/
641 
642 static int
643 essmix_init(struct snd_mixer *m)
644 {
645     	struct ess_info *sc = mix_getdevinfo(m);
646 
647 	mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE |
648 			  SOUND_MASK_IMIX);
649 
650 	mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
651 		       SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME |
652 		       SOUND_MASK_LINE1);
653 
654 	ess_setmixer(sc, 0, 0); /* reset */
655 
656 	return 0;
657 }
658 
659 static int
660 essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
661 {
662     	struct ess_info *sc = mix_getdevinfo(m);
663     	int preg = 0, rreg = 0, l, r;
664 
665 	l = (left * 15) / 100;
666 	r = (right * 15) / 100;
667 	switch (dev) {
668 	case SOUND_MIXER_SYNTH:
669 		preg = 0x36;
670 		rreg = 0x6b;
671 		break;
672 
673 	case SOUND_MIXER_PCM:
674 		preg = 0x14;
675 		rreg = 0x7c;
676 		break;
677 
678 	case SOUND_MIXER_LINE:
679 		preg = 0x3e;
680 		rreg = 0x6e;
681 		break;
682 
683 	case SOUND_MIXER_MIC:
684 		preg = 0x1a;
685 		rreg = 0x68;
686 		break;
687 
688 	case SOUND_MIXER_LINE1:
689 		preg = 0x3a;
690 		rreg = 0x6c;
691 		break;
692 
693 	case SOUND_MIXER_CD:
694 		preg = 0x38;
695 		rreg = 0x6a;
696 		break;
697 
698 	case SOUND_MIXER_VOLUME:
699 		l = left? (left * 63) / 100 : 64;
700 		r = right? (right * 63) / 100 : 64;
701 		ess_setmixer(sc, 0x60, l);
702 		ess_setmixer(sc, 0x62, r);
703 		left = (l == 64)? 0 : (l * 100) / 63;
704 		right = (r == 64)? 0 : (r * 100) / 63;
705     		return left | (right << 8);
706 	}
707 
708 	if (preg)
709 		ess_setmixer(sc, preg, (l << 4) | r);
710 	if (rreg)
711 		ess_setmixer(sc, rreg, (l << 4) | r);
712 
713 	left = (l * 100) / 15;
714 	right = (r * 100) / 15;
715 
716     	return left | (right << 8);
717 }
718 
719 static int
720 essmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
721 {
722     	struct ess_info *sc = mix_getdevinfo(m);
723     	u_char recdev;
724 
725     	switch (src) {
726 	case SOUND_MASK_CD:
727 		recdev = 0x02;
728 		break;
729 
730 	case SOUND_MASK_LINE:
731 		recdev = 0x06;
732 		break;
733 
734 	case SOUND_MASK_IMIX:
735 		recdev = 0x05;
736 		break;
737 
738 	case SOUND_MASK_MIC:
739 	default:
740 		recdev = 0x00;
741 		src = SOUND_MASK_MIC;
742 		break;
743 	}
744 
745 	ess_setmixer(sc, 0x1c, recdev);
746 
747 	return src;
748 }
749 
750 static kobj_method_t solomixer_methods[] = {
751     	KOBJMETHOD(mixer_init,		essmix_init),
752     	KOBJMETHOD(mixer_set,		essmix_set),
753     	KOBJMETHOD(mixer_setrecsrc,	essmix_setrecsrc),
754 	{ 0, 0 }
755 };
756 MIXER_DECLARE(solomixer);
757 
758 /************************************************************/
759 
760 static int
761 ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir)
762 {
763 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
764 	sc->dmasz[ch - 1] = cnt;
765 	if (ch == 1) {
766 		port_wr(sc->vc, 0x8, 0xc4, 1); /* command */
767 		port_wr(sc->vc, 0xd, 0xff, 1); /* reset */
768 		port_wr(sc->vc, 0xf, 0x01, 1); /* mask */
769 		port_wr(sc->vc, 0xb, dir == PCMDIR_PLAY? 0x58 : 0x54, 1); /* mode */
770 		port_wr(sc->vc, 0x0, base, 4);
771 		port_wr(sc->vc, 0x4, cnt - 1, 2);
772 
773 	} else if (ch == 2) {
774 		port_wr(sc->io, 0x6, 0x08, 1); /* autoinit */
775 		port_wr(sc->io, 0x0, base, 4);
776 		port_wr(sc->io, 0x4, cnt, 2);
777 	}
778 	return 0;
779 }
780 
781 static int
782 ess_dmapos(struct ess_info *sc, int ch)
783 {
784 	int p = 0, i = 0, j = 0;
785 
786 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
787 	if (ch == 1) {
788 
789 /*
790  * During recording, this register is known to give back
791  * garbage if it's not quiescent while being read. That's
792  * why we spl, stop the DMA, and try over and over until
793  * adjacent reads are "close", in the right order and not
794  * bigger than is otherwise possible.
795  */
796 		ess_dmatrigger(sc, ch, 0);
797 		DELAY(20);
798 		do {
799 			DELAY(10);
800 			if (j > 1)
801 				printf("DMA count reg bogus: %04x & %04x\n",
802 					i, p);
803 			i = port_rd(sc->vc, 0x4, 2) + 1;
804 			p = port_rd(sc->vc, 0x4, 2) + 1;
805 		} while ((p > sc->dmasz[ch - 1] || i < p || (p - i) > 0x8) && j++ < 1000);
806 		ess_dmatrigger(sc, ch, 1);
807 	}
808 	else if (ch == 2)
809 		p = port_rd(sc->io, 0x4, 2);
810 	return sc->dmasz[ch - 1] - p;
811 }
812 
813 static int
814 ess_dmatrigger(struct ess_info *sc, int ch, int go)
815 {
816 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
817 	if (ch == 1)
818 		port_wr(sc->vc, 0xf, go? 0x00 : 0x01, 1); /* mask */
819 	else if (ch == 2)
820 		port_wr(sc->io, 0x6, 0x08 | (go? 0x02 : 0x00), 1); /* autoinit */
821 	return 0;
822 }
823 
824 static void
825 ess_release_resources(struct ess_info *sc, device_t dev)
826 {
827     	if (sc->irq) {
828 		if (sc->ih)
829 			bus_teardown_intr(dev, sc->irq, sc->ih);
830 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
831 		sc->irq = 0;
832     	}
833     	if (sc->io) {
834 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->io);
835 		sc->io = 0;
836     	}
837 
838     	if (sc->sb) {
839 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(1), sc->sb);
840 		sc->sb = 0;
841     	}
842 
843     	if (sc->vc) {
844 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(2), sc->vc);
845 		sc->vc = 0;
846     	}
847 
848     	if (sc->mpu) {
849 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(3), sc->mpu);
850 		sc->mpu = 0;
851     	}
852 
853     	if (sc->gp) {
854 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(4), sc->gp);
855 		sc->gp = 0;
856     	}
857 
858 	if (sc->parent_dmat) {
859 		bus_dma_tag_destroy(sc->parent_dmat);
860 		sc->parent_dmat = 0;
861     	}
862 
863 #if ESS18XX_MPSAFE == 1
864 	if (sc->lock) {
865 		snd_mtxfree(sc->lock);
866 		sc->lock = NULL;
867 	}
868 #endif
869 
870     	free(sc, M_DEVBUF);
871 }
872 
873 static int
874 ess_alloc_resources(struct ess_info *sc, device_t dev)
875 {
876 	int rid;
877 
878 	rid = PCIR_BAR(0);
879     	sc->io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
880 
881 	rid = PCIR_BAR(1);
882     	sc->sb = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
883 
884 	rid = PCIR_BAR(2);
885     	sc->vc = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
886 
887 	rid = PCIR_BAR(3);
888     	sc->mpu = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
889 
890 	rid = PCIR_BAR(4);
891     	sc->gp = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
892 
893 	rid = 0;
894 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
895 		RF_ACTIVE | RF_SHAREABLE);
896 
897 #if ESS18XX_MPSAFE == 1
898 	sc->lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc");
899 
900 	return (sc->irq && sc->io && sc->sb && sc->vc &&
901 				sc->mpu && sc->gp && sc->lock)? 0 : ENXIO;
902 #else
903 	return (sc->irq && sc->io && sc->sb && sc->vc && sc->mpu && sc->gp)? 0 : ENXIO;
904 #endif
905 }
906 
907 static int
908 ess_probe(device_t dev)
909 {
910 	char *s = NULL;
911 	u_int32_t subdev;
912 
913 	subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev);
914 	switch (pci_get_devid(dev)) {
915 	case 0x1969125d:
916 		if (subdev == 0x8888125d)
917 			s = "ESS Solo-1E";
918 		else if (subdev == 0x1818125d)
919 			s = "ESS Solo-1";
920 		else
921 			s = "ESS Solo-1 (unknown vendor)";
922 		break;
923 	}
924 
925 	if (s)
926 		device_set_desc(dev, s);
927 	return s ? BUS_PROBE_DEFAULT : ENXIO;
928 }
929 
930 #define ESS_PCI_LEGACYCONTROL       0x40
931 #define ESS_PCI_CONFIG              0x50
932 #define ESS_PCI_DDMACONTROL      	0x60
933 
934 static int
935 ess_suspend(device_t dev)
936 {
937   return 0;
938 }
939 
940 static int
941 ess_resume(device_t dev)
942 {
943 	uint16_t ddma;
944 	uint32_t data;
945 	struct ess_info *sc = pcm_getdevinfo(dev);
946 
947 	ess_lock(sc);
948 	data = pci_read_config(dev, PCIR_COMMAND, 2);
949 	data |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN;
950 	pci_write_config(dev, PCIR_COMMAND, data, 2);
951 	data = pci_read_config(dev, PCIR_COMMAND, 2);
952 
953 	ddma = rman_get_start(sc->vc) | 1;
954 	pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2);
955 	pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2);
956 	pci_write_config(dev, ESS_PCI_CONFIG, 0, 2);
957 
958     	if (ess_reset_dsp(sc)) {
959 		ess_unlock(sc);
960 		goto no;
961 	}
962 	ess_unlock(sc);
963     	if (mixer_reinit(dev))
964 		goto no;
965 	ess_lock(sc);
966 	if (sc->newspeed)
967 		ess_setmixer(sc, 0x71, 0x2a);
968 
969 	port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */
970 	ess_unlock(sc);
971 
972 	return 0;
973  no:
974 	return EIO;
975 }
976 
977 static int
978 ess_attach(device_t dev)
979 {
980     	struct ess_info *sc;
981     	char status[SND_STATUSLEN];
982 	u_int16_t ddma;
983 	u_int32_t data;
984 
985 	sc = (struct ess_info *)malloc(sizeof *sc, M_DEVBUF, M_NOWAIT | M_ZERO);
986     	if (!sc)
987 		return ENXIO;
988 
989 	data = pci_read_config(dev, PCIR_COMMAND, 2);
990 	data |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN;
991 	pci_write_config(dev, PCIR_COMMAND, data, 2);
992 	data = pci_read_config(dev, PCIR_COMMAND, 2);
993 
994     	if (ess_alloc_resources(sc, dev))
995 		goto no;
996 
997 	sc->bufsz = pcm_getbuffersize(dev, 4096, SOLO_DEFAULT_BUFSZ, 65536);
998 
999 	ddma = rman_get_start(sc->vc) | 1;
1000 	pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2);
1001 	pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2);
1002 	pci_write_config(dev, ESS_PCI_CONFIG, 0, 2);
1003 
1004 	port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */
1005 #ifdef ESS18XX_DUPLEX
1006 	sc->duplex = 1;
1007 #else
1008 	sc->duplex = 0;
1009 #endif
1010 
1011 #ifdef ESS18XX_NEWSPEED
1012 	sc->newspeed = 1;
1013 #else
1014 	sc->newspeed = 0;
1015 #endif
1016 	if (snd_setup_intr(dev, sc->irq,
1017 #if ESS18XX_MPSAFE == 1
1018 			INTR_MPSAFE
1019 #else
1020 			0
1021 #endif
1022 			, ess_intr, sc, &sc->ih)) {
1023 		device_printf(dev, "unable to map interrupt\n");
1024 		goto no;
1025 	}
1026 
1027     	if (!sc->duplex)
1028 		pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
1029 
1030 #if 0
1031     	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/65536, /*boundary*/0,
1032 #endif
1033     	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
1034 			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
1035 			/*highaddr*/BUS_SPACE_MAXADDR,
1036 			/*filter*/NULL, /*filterarg*/NULL,
1037 			/*maxsize*/sc->bufsz, /*nsegments*/1,
1038 			/*maxsegz*/0x3ffff,
1039 			/*flags*/0,
1040 #if ESS18XX_MPSAFE == 1
1041 			/*lockfunc*/NULL, /*lockarg*/NULL,
1042 #else
1043 			/*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant,
1044 #endif
1045 			&sc->parent_dmat) != 0) {
1046 		device_printf(dev, "unable to create dma tag\n");
1047 		goto no;
1048     	}
1049 
1050     	if (ess_reset_dsp(sc))
1051 		goto no;
1052 
1053 	if (sc->newspeed)
1054 		ess_setmixer(sc, 0x71, 0x2a);
1055 
1056     	if (mixer_init(dev, &solomixer_class, sc))
1057 		goto no;
1058 
1059     	snprintf(status, SND_STATUSLEN, "at io 0x%lx,0x%lx,0x%lx irq %ld %s",
1060     	     	rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc),
1061 		rman_get_start(sc->irq),PCM_KLDSTRING(snd_solo));
1062 
1063     	if (pcm_register(dev, sc, 1, 1))
1064 		goto no;
1065       	pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc);
1066 	pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc);
1067 	pcm_setstatus(dev, status);
1068 
1069     	return 0;
1070 
1071 no:
1072     	ess_release_resources(sc, dev);
1073     	return ENXIO;
1074 }
1075 
1076 static int
1077 ess_detach(device_t dev)
1078 {
1079 	int r;
1080 	struct ess_info *sc;
1081 
1082 	r = pcm_unregister(dev);
1083 	if (r)
1084 		return r;
1085 
1086 	sc = pcm_getdevinfo(dev);
1087     	ess_release_resources(sc, dev);
1088 	return 0;
1089 }
1090 
1091 static device_method_t ess_methods[] = {
1092 	/* Device interface */
1093 	DEVMETHOD(device_probe,		ess_probe),
1094 	DEVMETHOD(device_attach,	ess_attach),
1095 	DEVMETHOD(device_detach,	ess_detach),
1096 	DEVMETHOD(device_resume,	ess_resume),
1097 	DEVMETHOD(device_suspend,	ess_suspend),
1098 
1099 	{ 0, 0 }
1100 };
1101 
1102 static driver_t ess_driver = {
1103 	"pcm",
1104 	ess_methods,
1105 	PCM_SOFTC_SIZE,
1106 };
1107 
1108 DRIVER_MODULE(snd_solo, pci, ess_driver, pcm_devclass, 0, 0);
1109 MODULE_DEPEND(snd_solo, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1110 MODULE_VERSION(snd_solo, 1);
1111 
1112 
1113 
1114