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