xref: /dragonfly/sys/dev/sound/pci/solo.c (revision 25a2db75)
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  * $FreeBSD: src/sys/dev/sound/pci/solo.c,v 1.35.2.4 2006/07/13 01:53:54 yongari Exp $
26  */
27 
28 #include <dev/sound/pcm/sound.h>
29 
30 #include <bus/pci/pcireg.h>
31 #include <bus/pci/pcivar.h>
32 
33 #include  <dev/sound/pci/sb.h>
34 #include  <dev/sound/chip.h>
35 
36 #include "mixer_if.h"
37 
38 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/solo.c,v 1.10 2007/06/16 20:07:19 dillon Exp $");
39 
40 #define SOLO_DEFAULT_BUFSZ 16384
41 #define ABS(x) (((x) < 0)? -(x) : (x))
42 
43 /* if defined, playback always uses the 2nd channel and full duplex works */
44 #define ESS18XX_DUPLEX	1
45 
46 /* more accurate clocks and split audio1/audio2 rates */
47 #define ESS18XX_NEWSPEED
48 
49 /* 1 = INTR_MPSAFE, 0 = GIANT */
50 #define ESS18XX_MPSAFE	1
51 
52 static u_int32_t ess_playfmt[] = {
53 	AFMT_U8,
54 	AFMT_STEREO | AFMT_U8,
55 	AFMT_S8,
56 	AFMT_STEREO | AFMT_S8,
57 	AFMT_S16_LE,
58 	AFMT_STEREO | AFMT_S16_LE,
59 	AFMT_U16_LE,
60 	AFMT_STEREO | AFMT_U16_LE,
61 	0
62 };
63 static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_playfmt, 0};
64 
65 /*
66  * Recording output is byte-swapped
67  */
68 static u_int32_t ess_recfmt[] = {
69 	AFMT_U8,
70 	AFMT_STEREO | AFMT_U8,
71 	AFMT_S8,
72 	AFMT_STEREO | AFMT_S8,
73 	AFMT_S16_BE,
74 	AFMT_STEREO | AFMT_S16_BE,
75 	AFMT_U16_BE,
76 	AFMT_STEREO | AFMT_U16_BE,
77 	0
78 };
79 static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_recfmt, 0};
80 
81 struct ess_info;
82 
83 struct ess_chinfo {
84 	struct ess_info *parent;
85 	struct pcm_channel *channel;
86 	struct snd_dbuf *buffer;
87 	int dir, hwch, stopping;
88 	u_int32_t fmt, spd, blksz;
89 };
90 
91 struct ess_info {
92     	struct resource *io, *sb, *vc, *mpu, *gp;	/* I/O address for the board */
93     	struct resource *irq;
94 	void		*ih;
95     	bus_dma_tag_t parent_dmat;
96 
97     	int simplex_dir, type, duplex:1, newspeed:1, dmasz[2];
98 	unsigned int bufsz;
99 
100     	struct ess_chinfo pch, rch;
101 #if ESS18XX_MPSAFE == 1
102 	sndlock_t	lock;
103 #endif
104 };
105 
106 #if ESS18XX_MPSAFE == 1
107 #define ess_lock(_ess) snd_mtxlock((_ess)->lock)
108 #define ess_unlock(_ess) snd_mtxunlock((_ess)->lock)
109 #define ess_lock_assert(_ess) snd_mtxassert((_ess)->lock)
110 #else
111 #define ess_lock(_ess)
112 #define ess_unlock(_ess)
113 #define ess_lock_assert(_ess)
114 #endif
115 
116 static int ess_rd(struct ess_info *sc, int reg);
117 static void ess_wr(struct ess_info *sc, int reg, u_int8_t val);
118 static int ess_dspready(struct ess_info *sc);
119 static int ess_cmd(struct ess_info *sc, u_char val);
120 static int ess_cmd1(struct ess_info *sc, u_char cmd, int val);
121 static int ess_get_byte(struct ess_info *sc);
122 static void ess_setmixer(struct ess_info *sc, u_int port, u_int value);
123 static int ess_getmixer(struct ess_info *sc, u_int port);
124 static int ess_reset_dsp(struct ess_info *sc);
125 
126 static int ess_write(struct ess_info *sc, u_char reg, int val);
127 static int ess_read(struct ess_info *sc, u_char reg);
128 
129 static void ess_intr(void *arg);
130 static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len);
131 static int ess_start(struct ess_chinfo *ch);
132 static int ess_stop(struct ess_chinfo *ch);
133 
134 static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir);
135 static int ess_dmapos(struct ess_info *sc, int ch);
136 static int ess_dmatrigger(struct ess_info *sc, int ch, int go);
137 
138 /*
139  * Common code for the midi and pcm functions
140  *
141  * ess_cmd write a single byte to the CMD port.
142  * ess_cmd1 write a CMD + 1 byte arg
143  * ess_cmd2 write a CMD + 2 byte arg
144  * ess_get_byte returns a single byte from the DSP data port
145  *
146  * ess_write is actually ess_cmd1
147  * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte
148  */
149 
150 static int
151 port_rd(struct resource *port, int regno, int size)
152 {
153 	bus_space_tag_t st = rman_get_bustag(port);
154 	bus_space_handle_t sh = rman_get_bushandle(port);
155 
156 	switch (size) {
157 	case 1:
158 		return bus_space_read_1(st, sh, regno);
159 	case 2:
160 		return bus_space_read_2(st, sh, regno);
161 	case 4:
162 		return bus_space_read_4(st, sh, regno);
163 	default:
164 		return 0xffffffff;
165 	}
166 }
167 
168 static void
169 port_wr(struct resource *port, int regno, u_int32_t data, int size)
170 {
171 	bus_space_tag_t st = rman_get_bustag(port);
172 	bus_space_handle_t sh = rman_get_bushandle(port);
173 
174 	switch (size) {
175 	case 1:
176 		bus_space_write_1(st, sh, regno, data);
177 		break;
178 	case 2:
179 		bus_space_write_2(st, sh, regno, data);
180 		break;
181 	case 4:
182 		bus_space_write_4(st, sh, regno, data);
183 		break;
184 	}
185 }
186 
187 static int
188 ess_rd(struct ess_info *sc, int reg)
189 {
190 	return port_rd(sc->sb, reg, 1);
191 }
192 
193 static void
194 ess_wr(struct ess_info *sc, int reg, u_int8_t val)
195 {
196 	port_wr(sc->sb, reg, val, 1);
197 }
198 
199 static int
200 ess_dspready(struct ess_info *sc)
201 {
202 	return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0);
203 }
204 
205 static int
206 ess_dspwr(struct ess_info *sc, u_char val)
207 {
208     	int  i;
209 
210     	for (i = 0; i < 1000; i++) {
211 		if (ess_dspready(sc)) {
212 	    		ess_wr(sc, SBDSP_CMD, val);
213 	    		return 1;
214 		}
215 		if (i > 10) DELAY((i > 100)? 1000 : 10);
216     	}
217     	kprintf("ess_dspwr(0x%02x) timed out.\n", val);
218     	return 0;
219 }
220 
221 static int
222 ess_cmd(struct ess_info *sc, u_char val)
223 {
224 	DEB(kprintf("ess_cmd: %x\n", val));
225     	return ess_dspwr(sc, val);
226 }
227 
228 static int
229 ess_cmd1(struct ess_info *sc, u_char cmd, int val)
230 {
231     	DEB(kprintf("ess_cmd1: %x, %x\n", cmd, val));
232     	if (ess_dspwr(sc, cmd)) {
233 		return ess_dspwr(sc, val & 0xff);
234     	} else return 0;
235 }
236 
237 static void
238 ess_setmixer(struct ess_info *sc, u_int port, u_int value)
239 {
240 	DEB(kprintf("ess_setmixer: reg=%x, val=%x\n", port, value);)
241     	ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
242     	DELAY(10);
243     	ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff));
244     	DELAY(10);
245 }
246 
247 static int
248 ess_getmixer(struct ess_info *sc, u_int port)
249 {
250     	int val;
251 
252     	ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
253     	DELAY(10);
254     	val = ess_rd(sc, SB_MIX_DATA);
255     	DELAY(10);
256 
257     	return val;
258 }
259 
260 static int
261 ess_get_byte(struct ess_info *sc)
262 {
263     	int i;
264 
265     	for (i = 1000; i > 0; i--) {
266 		if (ess_rd(sc, 0xc) & 0x40)
267 			return ess_rd(sc, DSP_READ);
268 		else
269 			DELAY(20);
270     	}
271     	return -1;
272 }
273 
274 static int
275 ess_write(struct ess_info *sc, u_char reg, int val)
276 {
277     	return ess_cmd1(sc, reg, val);
278 }
279 
280 static int
281 ess_read(struct ess_info *sc, u_char reg)
282 {
283     	return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1;
284 }
285 
286 static int
287 ess_reset_dsp(struct ess_info *sc)
288 {
289 	DEB(kprintf("ess_reset_dsp\n"));
290     	ess_wr(sc, SBDSP_RST, 3);
291     	DELAY(100);
292     	ess_wr(sc, SBDSP_RST, 0);
293     	if (ess_get_byte(sc) != 0xAA) {
294         	DEB(kprintf("ess_reset_dsp failed\n"));
295 /*
296 			   rman_get_start(d->io_base)));
297 */
298 		return ENXIO;	/* Sorry */
299     	}
300     	ess_cmd(sc, 0xc6);
301     	return 0;
302 }
303 
304 static void
305 ess_intr(void *arg)
306 {
307     	struct ess_info *sc = (struct ess_info *)arg;
308 	int src, pirq = 0, rirq = 0;
309 
310 	ess_lock(sc);
311 	src = 0;
312 	if (ess_getmixer(sc, 0x7a) & 0x80)
313 		src |= 2;
314 	if (ess_rd(sc, 0x0c) & 0x01)
315 		src |= 1;
316 
317 	if (src == 0) {
318 		ess_unlock(sc);
319 		return;
320 	}
321 
322 	if (sc->duplex) {
323 		pirq = (src & sc->pch.hwch)? 1 : 0;
324 		rirq = (src & sc->rch.hwch)? 1 : 0;
325 	} else {
326 		if (sc->simplex_dir == PCMDIR_PLAY)
327 			pirq = 1;
328 		if (sc->simplex_dir == PCMDIR_REC)
329 			rirq = 1;
330 		if (!pirq && !rirq)
331 			kprintf("solo: IRQ neither playback nor rec!\n");
332 	}
333 
334 	DEB(kprintf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq));
335 
336 	if (pirq) {
337 		if (sc->pch.stopping) {
338 			ess_dmatrigger(sc, sc->pch.hwch, 0);
339 			sc->pch.stopping = 0;
340 			if (sc->pch.hwch == 1)
341 				ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
342 			else
343 				ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03);
344 		}
345 		ess_unlock(sc);
346 		chn_intr(sc->pch.channel);
347 		ess_lock(sc);
348 	}
349 
350 	if (rirq) {
351 		if (sc->rch.stopping) {
352 			ess_dmatrigger(sc, sc->rch.hwch, 0);
353 			sc->rch.stopping = 0;
354 			/* XXX: will this stop audio2? */
355 			ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
356 		}
357 		ess_unlock(sc);
358 		chn_intr(sc->rch.channel);
359 		ess_lock(sc);
360 	}
361 
362 	if (src & 2)
363 		ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80);
364 	if (src & 1)
365     		ess_rd(sc, DSP_DATA_AVAIL);
366 
367 	ess_unlock(sc);
368 }
369 
370 /* utility functions for ESS */
371 static u_int8_t
372 ess_calcspeed8(int *spd)
373 {
374 	int speed = *spd;
375 	u_int32_t t;
376 
377 	if (speed > 22000) {
378 		t = (795500 + speed / 2) / speed;
379 		speed = (795500 + t / 2) / t;
380 		t = (256 - t) | 0x80;
381 	} else {
382 		t = (397700 + speed / 2) / speed;
383 		speed = (397700 + t / 2) / t;
384 		t = 128 - t;
385 	}
386 	*spd = speed;
387 	return t & 0x000000ff;
388 }
389 
390 static u_int8_t
391 ess_calcspeed9(int *spd)
392 {
393 	int speed, s0, s1, use0;
394 	u_int8_t t0, t1;
395 
396 	/* rate = source / (256 - divisor) */
397 	/* divisor = 256 - (source / rate) */
398 	speed = *spd;
399 	t0 = 128 - (793800 / speed);
400 	s0 = 793800 / (128 - t0);
401 
402 	t1 = 128 - (768000 / speed);
403 	s1 = 768000 / (128 - t1);
404 	t1 |= 0x80;
405 
406 	use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0;
407 
408 	*spd = use0? s0 : s1;
409 	return use0? t0 : t1;
410 }
411 
412 static u_int8_t
413 ess_calcfilter(int spd)
414 {
415 	int cutoff;
416 
417 	/* cutoff = 7160000 / (256 - divisor) */
418 	/* divisor = 256 - (7160000 / cutoff) */
419 	cutoff = (spd * 9 * 82) / 20;
420 	return (256 - (7160000 / cutoff));
421 }
422 
423 static int
424 ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
425 {
426 	int play = (dir == PCMDIR_PLAY)? 1 : 0;
427 	int b16 = (fmt & AFMT_16BIT)? 1 : 0;
428 	int stereo = (fmt & AFMT_STEREO)? 1 : 0;
429 	int unsign = (fmt == AFMT_U8 || fmt == AFMT_U16_LE || fmt == AFMT_U16_BE)? 1 : 0;
430 	u_int8_t spdval, fmtval;
431 
432 	DEB(kprintf("ess_setupch\n"));
433 	spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd);
434 
435 	sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ;
436 
437 	if (ch == 1) {
438 		KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad"));
439 		len = -len;
440 		/* transfer length low */
441 		ess_write(sc, 0xa4, len & 0x00ff);
442 		/* transfer length high */
443 		ess_write(sc, 0xa5, (len & 0xff00) >> 8);
444 		/* autoinit, dma dir */
445 		ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a));
446 		/* mono/stereo */
447 		ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02));
448 		/* demand mode, 4 bytes/xfer */
449 		ess_write(sc, 0xb9, 0x02);
450 		/* sample rate */
451         	ess_write(sc, 0xa1, spdval);
452 		/* filter cutoff */
453 		ess_write(sc, 0xa2, ess_calcfilter(spd));
454 		/* setup dac/adc */
455 		/*
456 		if (play)
457 			ess_write(sc, 0xb6, unsign? 0x80 : 0x00);
458 		*/
459 		/* mono, b16: signed, load signal */
460 		/*
461 		ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20));
462 		*/
463 		/* setup fifo */
464 		ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) |
465 					   (b16? 0x04 : 0x00) |
466 					   (stereo? 0x08 : 0x40));
467 		/* irq control */
468 		ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50);
469 		/* drq control */
470 		ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50);
471 	} else if (ch == 2) {
472 		KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad"));
473 		len >>= 1;
474 		len = -len;
475 		/* transfer length low */
476 		ess_setmixer(sc, 0x74, len & 0x00ff);
477 		/* transfer length high */
478 		ess_setmixer(sc, 0x76, (len & 0xff00) >> 8);
479 		/* autoinit, 4 bytes/req */
480 		ess_setmixer(sc, 0x78, 0x10);
481 		fmtval = b16 | (stereo << 1) | ((!unsign) << 2);
482 		/* enable irq, set format */
483 		ess_setmixer(sc, 0x7a, 0x40 | fmtval);
484 		if (sc->newspeed) {
485 			/* sample rate */
486 			ess_setmixer(sc, 0x70, spdval);
487 			/* filter cutoff */
488 			ess_setmixer(sc, 0x72, ess_calcfilter(spd));
489 		}
490 
491 	}
492 	return 0;
493 }
494 static int
495 ess_start(struct ess_chinfo *ch)
496 {
497 	struct ess_info *sc = ch->parent;
498 
499 	DEB(kprintf("ess_start\n"););
500 	ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz);
501 	ch->stopping = 0;
502 	if (ch->hwch == 1) {
503 		ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01);
504 		if (ch->dir == PCMDIR_PLAY) {
505 #if 0
506 			DELAY(100000); /* 100 ms */
507 #endif
508 			ess_cmd(sc, 0xd1);
509 		}
510 	} else
511 		ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03);
512 	return 0;
513 }
514 
515 static int
516 ess_stop(struct ess_chinfo *ch)
517 {
518 	struct ess_info *sc = ch->parent;
519 
520 	DEB(kprintf("ess_stop\n"));
521 	ch->stopping = 1;
522 	if (ch->hwch == 1)
523 		ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04);
524 	else
525 		ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10);
526 	DEB(kprintf("done with stop\n"));
527 	return 0;
528 }
529 
530 /* -------------------------------------------------------------------- */
531 /* channel interface for ESS18xx */
532 static void *
533 esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
534 {
535 	struct ess_info *sc = devinfo;
536 	struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
537 
538 	DEB(kprintf("esschan_init\n"));
539 	ch->parent = sc;
540 	ch->channel = c;
541 	ch->buffer = b;
542 	ch->dir = dir;
543 	if (sndbuf_alloc(ch->buffer, sc->parent_dmat, sc->bufsz) != 0)
544 		return NULL;
545 	ch->hwch = 1;
546 	if ((dir == PCMDIR_PLAY) && (sc->duplex))
547 		ch->hwch = 2;
548 	return ch;
549 }
550 
551 static int
552 esschan_setformat(kobj_t obj, void *data, u_int32_t format)
553 {
554 	struct ess_chinfo *ch = data;
555 
556 	ch->fmt = format;
557 	return 0;
558 }
559 
560 static int
561 esschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
562 {
563 	struct ess_chinfo *ch = data;
564 	struct ess_info *sc = ch->parent;
565 
566 	ch->spd = speed;
567 	if (sc->newspeed)
568 		ess_calcspeed9(&ch->spd);
569 	else
570 		ess_calcspeed8(&ch->spd);
571 	return ch->spd;
572 }
573 
574 static int
575 esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
576 {
577 	struct ess_chinfo *ch = data;
578 
579 	ch->blksz = blocksize;
580 	return ch->blksz;
581 }
582 
583 static int
584 esschan_trigger(kobj_t obj, void *data, int go)
585 {
586 	struct ess_chinfo *ch = data;
587 	struct ess_info *sc = ch->parent;
588 
589 	DEB(kprintf("esschan_trigger: %d\n",go));
590 	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
591 		return 0;
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 	KOBJMETHOD_END
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 	KOBJMETHOD_END
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 				kprintf("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 = NULL;
836     	}
837     	if (sc->io) {
838 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->io);
839 		sc->io = NULL;
840     	}
841 
842     	if (sc->sb) {
843 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(1), sc->sb);
844 		sc->sb = NULL;
845     	}
846 
847     	if (sc->vc) {
848 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(2), sc->vc);
849 		sc->vc = NULL;
850     	}
851 
852     	if (sc->mpu) {
853 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(3), sc->mpu);
854 		sc->mpu = NULL;
855     	}
856 
857     	if (sc->gp) {
858 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(4), sc->gp);
859 		sc->gp = NULL;
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     	kfree(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), "sound 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 = kmalloc(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*/NULL, /*alignment*/65536, /*boundary*/0,
1033 #endif
1034     	if (bus_dma_tag_create(/*parent*/NULL, /*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 			&sc->parent_dmat) != 0) {
1042 		device_printf(dev, "unable to create dma tag\n");
1043 		goto no;
1044     	}
1045 
1046     	if (ess_reset_dsp(sc))
1047 		goto no;
1048 
1049 	if (sc->newspeed)
1050 		ess_setmixer(sc, 0x71, 0x2a);
1051 
1052     	if (mixer_init(dev, &solomixer_class, sc))
1053 		goto no;
1054 
1055     	ksnprintf(status, SND_STATUSLEN, "at io 0x%lx,0x%lx,0x%lx irq %ld %s",
1056     	     	rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc),
1057 		rman_get_start(sc->irq),PCM_KLDSTRING(snd_solo));
1058 
1059     	if (pcm_register(dev, sc, 1, 1))
1060 		goto no;
1061       	pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc);
1062 	pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc);
1063 	pcm_setstatus(dev, status);
1064 
1065     	return 0;
1066 
1067 no:
1068     	ess_release_resources(sc, dev);
1069     	return ENXIO;
1070 }
1071 
1072 static int
1073 ess_detach(device_t dev)
1074 {
1075 	int r;
1076 	struct ess_info *sc;
1077 
1078 	r = pcm_unregister(dev);
1079 	if (r)
1080 		return r;
1081 
1082 	sc = pcm_getdevinfo(dev);
1083     	ess_release_resources(sc, dev);
1084 	return 0;
1085 }
1086 
1087 static device_method_t ess_methods[] = {
1088 	/* Device interface */
1089 	DEVMETHOD(device_probe,		ess_probe),
1090 	DEVMETHOD(device_attach,	ess_attach),
1091 	DEVMETHOD(device_detach,	ess_detach),
1092 	DEVMETHOD(device_resume,	ess_resume),
1093 	DEVMETHOD(device_suspend,	ess_suspend),
1094 
1095 	DEVMETHOD_END
1096 };
1097 
1098 static driver_t ess_driver = {
1099 	"pcm",
1100 	ess_methods,
1101 	PCM_SOFTC_SIZE,
1102 };
1103 
1104 DRIVER_MODULE(snd_solo, pci, ess_driver, pcm_devclass, NULL, NULL);
1105 MODULE_DEPEND(snd_solo, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1106 MODULE_VERSION(snd_solo, 1);
1107 
1108 
1109 
1110