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