xref: /freebsd/sys/dev/sound/pci/solo.c (revision 516a9c0212b003e1da0c6f4476dbe4f3f431606c)
1098ca2bdSWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
43f225978SCameron Grant  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5fd1aaeccSCameron Grant  *
6fd1aaeccSCameron Grant  * Redistribution and use in source and binary forms, with or without
7fd1aaeccSCameron Grant  * modification, are permitted provided that the following conditions
8fd1aaeccSCameron Grant  * are met:
9fd1aaeccSCameron Grant  * 1. Redistributions of source code must retain the above copyright
10fd1aaeccSCameron Grant  *    notice, this list of conditions and the following disclaimer.
11fd1aaeccSCameron Grant  * 2. Redistributions in binary form must reproduce the above copyright
12fd1aaeccSCameron Grant  *    notice, this list of conditions and the following disclaimer in the
13fd1aaeccSCameron Grant  *    documentation and/or other materials provided with the distribution.
14fd1aaeccSCameron Grant  *
15fd1aaeccSCameron Grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16fd1aaeccSCameron Grant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17fd1aaeccSCameron Grant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18fd1aaeccSCameron Grant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19fd1aaeccSCameron Grant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20fd1aaeccSCameron Grant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21fd1aaeccSCameron Grant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22fd1aaeccSCameron Grant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23fd1aaeccSCameron Grant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24fd1aaeccSCameron Grant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25fd1aaeccSCameron Grant  * SUCH DAMAGE.
26fd1aaeccSCameron Grant  */
27fd1aaeccSCameron Grant 
2890da2b28SAriff Abdullah #ifdef HAVE_KERNEL_OPTION_HEADERS
2990da2b28SAriff Abdullah #include "opt_snd.h"
3090da2b28SAriff Abdullah #endif
3190da2b28SAriff Abdullah 
32fd1aaeccSCameron Grant #include <dev/sound/pcm/sound.h>
33fd1aaeccSCameron Grant 
3490cf0136SWarner Losh #include <dev/pci/pcireg.h>
3590cf0136SWarner Losh #include <dev/pci/pcivar.h>
36fd1aaeccSCameron Grant 
37fd1aaeccSCameron Grant #include  <dev/sound/isa/sb.h>
38fd1aaeccSCameron Grant 
390f55ac6cSCameron Grant #include "mixer_if.h"
400f55ac6cSCameron Grant 
4139dbd126SCameron Grant #define SOLO_DEFAULT_BUFSZ 16384
42fd1aaeccSCameron Grant #define ABS(x) (((x) < 0)? -(x) : (x))
43fd1aaeccSCameron Grant 
443ac1ca33SNick Sayer /* if defined, playback always uses the 2nd channel and full duplex works */
4586b391b2SAriff Abdullah #define ESS18XX_DUPLEX	1
46fd1aaeccSCameron Grant 
47fd1aaeccSCameron Grant /* more accurate clocks and split audio1/audio2 rates */
48fd1aaeccSCameron Grant #define ESS18XX_NEWSPEED
49fd1aaeccSCameron Grant 
50513693beSCameron Grant static u_int32_t ess_playfmt[] = {
5190da2b28SAriff Abdullah 	SND_FORMAT(AFMT_U8, 1, 0),
5290da2b28SAriff Abdullah 	SND_FORMAT(AFMT_U8, 2, 0),
5390da2b28SAriff Abdullah 	SND_FORMAT(AFMT_S8, 1, 0),
5490da2b28SAriff Abdullah 	SND_FORMAT(AFMT_S8, 2, 0),
5590da2b28SAriff Abdullah 	SND_FORMAT(AFMT_S16_LE, 1, 0),
5690da2b28SAriff Abdullah 	SND_FORMAT(AFMT_S16_LE, 2, 0),
5790da2b28SAriff Abdullah 	SND_FORMAT(AFMT_U16_LE, 1, 0),
5890da2b28SAriff Abdullah 	SND_FORMAT(AFMT_U16_LE, 2, 0),
59513693beSCameron Grant 	0
60fd1aaeccSCameron Grant };
611333b4a4SAriff Abdullah static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_playfmt, 0};
62fd1aaeccSCameron Grant 
633ac1ca33SNick Sayer /*
643ac1ca33SNick Sayer  * Recording output is byte-swapped
653ac1ca33SNick Sayer  */
66513693beSCameron Grant static u_int32_t ess_recfmt[] = {
6790da2b28SAriff Abdullah 	SND_FORMAT(AFMT_U8, 1, 0),
6890da2b28SAriff Abdullah 	SND_FORMAT(AFMT_U8, 2, 0),
6990da2b28SAriff Abdullah 	SND_FORMAT(AFMT_S8, 1, 0),
7090da2b28SAriff Abdullah 	SND_FORMAT(AFMT_S8, 2, 0),
7190da2b28SAriff Abdullah 	SND_FORMAT(AFMT_S16_BE, 1, 0),
7290da2b28SAriff Abdullah 	SND_FORMAT(AFMT_S16_BE, 2, 0),
7390da2b28SAriff Abdullah 	SND_FORMAT(AFMT_U16_BE, 1, 0),
7490da2b28SAriff Abdullah 	SND_FORMAT(AFMT_U16_BE, 2, 0),
75513693beSCameron Grant 	0
76fd1aaeccSCameron Grant };
771333b4a4SAriff Abdullah static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_recfmt, 0};
78fd1aaeccSCameron Grant 
79fd1aaeccSCameron Grant struct ess_info;
80fd1aaeccSCameron Grant 
81fd1aaeccSCameron Grant struct ess_chinfo {
82fd1aaeccSCameron Grant 	struct ess_info *parent;
8366ef8af5SCameron Grant 	struct pcm_channel *channel;
8466ef8af5SCameron Grant 	struct snd_dbuf *buffer;
85fd1aaeccSCameron Grant 	int dir, hwch, stopping;
86350a5fafSCameron Grant 	u_int32_t fmt, spd, blksz;
87fd1aaeccSCameron Grant };
88fd1aaeccSCameron Grant 
89fd1aaeccSCameron Grant struct ess_info {
90fd1aaeccSCameron Grant     	struct resource *io, *sb, *vc, *mpu, *gp;	/* I/O address for the board */
91fd1aaeccSCameron Grant     	struct resource *irq;
92306f91b6SCameron Grant 	void		*ih;
93fd1aaeccSCameron Grant     	bus_dma_tag_t parent_dmat;
94fd1aaeccSCameron Grant 
9579462204SAriff Abdullah     	int simplex_dir, type, dmasz[2];
9679462204SAriff Abdullah 	unsigned int duplex:1, newspeed:1;
9739dbd126SCameron Grant 	unsigned int bufsz;
9839dbd126SCameron Grant 
99fd1aaeccSCameron Grant     	struct ess_chinfo pch, rch;
1008b6d3fe1SAriff Abdullah 	struct mtx *lock;
101fd1aaeccSCameron Grant };
102fd1aaeccSCameron Grant 
1038b6d3fe1SAriff Abdullah #define ess_lock(_ess) snd_mtxlock((_ess)->lock)
1048b6d3fe1SAriff Abdullah #define ess_unlock(_ess) snd_mtxunlock((_ess)->lock)
1058b6d3fe1SAriff Abdullah #define ess_lock_assert(_ess) snd_mtxassert((_ess)->lock)
1068b6d3fe1SAriff Abdullah 
107fd1aaeccSCameron Grant static int ess_rd(struct ess_info *sc, int reg);
108fd1aaeccSCameron Grant static void ess_wr(struct ess_info *sc, int reg, u_int8_t val);
109fd1aaeccSCameron Grant static int ess_dspready(struct ess_info *sc);
110fd1aaeccSCameron Grant static int ess_cmd(struct ess_info *sc, u_char val);
111fd1aaeccSCameron Grant static int ess_cmd1(struct ess_info *sc, u_char cmd, int val);
112fd1aaeccSCameron Grant static int ess_get_byte(struct ess_info *sc);
113fd1aaeccSCameron Grant static void ess_setmixer(struct ess_info *sc, u_int port, u_int value);
114fd1aaeccSCameron Grant static int ess_getmixer(struct ess_info *sc, u_int port);
115fd1aaeccSCameron Grant static int ess_reset_dsp(struct ess_info *sc);
116fd1aaeccSCameron Grant 
117fd1aaeccSCameron Grant static int ess_write(struct ess_info *sc, u_char reg, int val);
118fd1aaeccSCameron Grant static int ess_read(struct ess_info *sc, u_char reg);
119fd1aaeccSCameron Grant 
120fd1aaeccSCameron Grant static void ess_intr(void *arg);
121fd1aaeccSCameron Grant static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len);
122fd1aaeccSCameron Grant static int ess_start(struct ess_chinfo *ch);
123fd1aaeccSCameron Grant static int ess_stop(struct ess_chinfo *ch);
124fd1aaeccSCameron Grant 
125fd1aaeccSCameron Grant static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir);
126fd1aaeccSCameron Grant static int ess_dmapos(struct ess_info *sc, int ch);
127fd1aaeccSCameron Grant static int ess_dmatrigger(struct ess_info *sc, int ch, int go);
128fd1aaeccSCameron Grant 
129fd1aaeccSCameron Grant /*
130fd1aaeccSCameron Grant  * Common code for the midi and pcm functions
131fd1aaeccSCameron Grant  *
132fd1aaeccSCameron Grant  * ess_cmd write a single byte to the CMD port.
133fd1aaeccSCameron Grant  * ess_cmd1 write a CMD + 1 byte arg
134fd1aaeccSCameron Grant  * ess_cmd2 write a CMD + 2 byte arg
135fd1aaeccSCameron Grant  * ess_get_byte returns a single byte from the DSP data port
136fd1aaeccSCameron Grant  *
137fd1aaeccSCameron Grant  * ess_write is actually ess_cmd1
138fd1aaeccSCameron Grant  * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte
139fd1aaeccSCameron Grant  */
140fd1aaeccSCameron Grant 
141fd1aaeccSCameron Grant static int
port_rd(struct resource * port,int regno,int size)142fd1aaeccSCameron Grant port_rd(struct resource *port, int regno, int size)
143fd1aaeccSCameron Grant {
144fd1aaeccSCameron Grant 	bus_space_tag_t st = rman_get_bustag(port);
145fd1aaeccSCameron Grant 	bus_space_handle_t sh = rman_get_bushandle(port);
146fd1aaeccSCameron Grant 
147fd1aaeccSCameron Grant 	switch (size) {
148fd1aaeccSCameron Grant 	case 1:
149fd1aaeccSCameron Grant 		return bus_space_read_1(st, sh, regno);
150fd1aaeccSCameron Grant 	case 2:
151fd1aaeccSCameron Grant 		return bus_space_read_2(st, sh, regno);
152fd1aaeccSCameron Grant 	case 4:
153fd1aaeccSCameron Grant 		return bus_space_read_4(st, sh, regno);
154fd1aaeccSCameron Grant 	default:
155fd1aaeccSCameron Grant 		return 0xffffffff;
156fd1aaeccSCameron Grant 	}
157fd1aaeccSCameron Grant }
158fd1aaeccSCameron Grant 
159fd1aaeccSCameron Grant static void
port_wr(struct resource * port,int regno,u_int32_t data,int size)160fd1aaeccSCameron Grant port_wr(struct resource *port, int regno, u_int32_t data, int size)
161fd1aaeccSCameron Grant {
162fd1aaeccSCameron Grant 	bus_space_tag_t st = rman_get_bustag(port);
163fd1aaeccSCameron Grant 	bus_space_handle_t sh = rman_get_bushandle(port);
164fd1aaeccSCameron Grant 
165fd1aaeccSCameron Grant 	switch (size) {
166fd1aaeccSCameron Grant 	case 1:
167fd1aaeccSCameron Grant 		bus_space_write_1(st, sh, regno, data);
168fd1aaeccSCameron Grant 		break;
169fd1aaeccSCameron Grant 	case 2:
170fd1aaeccSCameron Grant 		bus_space_write_2(st, sh, regno, data);
171fd1aaeccSCameron Grant 		break;
172fd1aaeccSCameron Grant 	case 4:
173fd1aaeccSCameron Grant 		bus_space_write_4(st, sh, regno, data);
174fd1aaeccSCameron Grant 		break;
175fd1aaeccSCameron Grant 	}
176fd1aaeccSCameron Grant }
177fd1aaeccSCameron Grant 
178fd1aaeccSCameron Grant static int
ess_rd(struct ess_info * sc,int reg)179fd1aaeccSCameron Grant ess_rd(struct ess_info *sc, int reg)
180fd1aaeccSCameron Grant {
181fd1aaeccSCameron Grant 	return port_rd(sc->sb, reg, 1);
182fd1aaeccSCameron Grant }
183fd1aaeccSCameron Grant 
184fd1aaeccSCameron Grant static void
ess_wr(struct ess_info * sc,int reg,u_int8_t val)185fd1aaeccSCameron Grant ess_wr(struct ess_info *sc, int reg, u_int8_t val)
186fd1aaeccSCameron Grant {
187fd1aaeccSCameron Grant 	port_wr(sc->sb, reg, val, 1);
188fd1aaeccSCameron Grant }
189fd1aaeccSCameron Grant 
190fd1aaeccSCameron Grant static int
ess_dspready(struct ess_info * sc)191fd1aaeccSCameron Grant ess_dspready(struct ess_info *sc)
192fd1aaeccSCameron Grant {
193fd1aaeccSCameron Grant 	return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0);
194fd1aaeccSCameron Grant }
195fd1aaeccSCameron Grant 
196fd1aaeccSCameron Grant static int
ess_dspwr(struct ess_info * sc,u_char val)197fd1aaeccSCameron Grant ess_dspwr(struct ess_info *sc, u_char val)
198fd1aaeccSCameron Grant {
199fd1aaeccSCameron Grant     	int  i;
200fd1aaeccSCameron Grant 
201fd1aaeccSCameron Grant     	for (i = 0; i < 1000; i++) {
202fd1aaeccSCameron Grant 		if (ess_dspready(sc)) {
203fd1aaeccSCameron Grant 	    		ess_wr(sc, SBDSP_CMD, val);
204fd1aaeccSCameron Grant 	    		return 1;
205fd1aaeccSCameron Grant 		}
206fd1aaeccSCameron Grant 		if (i > 10) DELAY((i > 100)? 1000 : 10);
207fd1aaeccSCameron Grant     	}
208fd1aaeccSCameron Grant     	printf("ess_dspwr(0x%02x) timed out.\n", val);
209fd1aaeccSCameron Grant     	return 0;
210fd1aaeccSCameron Grant }
211fd1aaeccSCameron Grant 
212fd1aaeccSCameron Grant static int
ess_cmd(struct ess_info * sc,u_char val)213fd1aaeccSCameron Grant ess_cmd(struct ess_info *sc, u_char val)
214fd1aaeccSCameron Grant {
215a7e11506SNick Sayer 	DEB(printf("ess_cmd: %x\n", val));
216fd1aaeccSCameron Grant     	return ess_dspwr(sc, val);
217fd1aaeccSCameron Grant }
218fd1aaeccSCameron Grant 
219fd1aaeccSCameron Grant static int
ess_cmd1(struct ess_info * sc,u_char cmd,int val)220fd1aaeccSCameron Grant ess_cmd1(struct ess_info *sc, u_char cmd, int val)
221fd1aaeccSCameron Grant {
222a7e11506SNick Sayer     	DEB(printf("ess_cmd1: %x, %x\n", cmd, val));
223fd1aaeccSCameron Grant     	if (ess_dspwr(sc, cmd)) {
224fd1aaeccSCameron Grant 		return ess_dspwr(sc, val & 0xff);
225fd1aaeccSCameron Grant     	} else return 0;
226fd1aaeccSCameron Grant }
227fd1aaeccSCameron Grant 
228fd1aaeccSCameron Grant static void
ess_setmixer(struct ess_info * sc,u_int port,u_int value)229fd1aaeccSCameron Grant ess_setmixer(struct ess_info *sc, u_int port, u_int value)
230fd1aaeccSCameron Grant {
231fd1aaeccSCameron Grant 	DEB(printf("ess_setmixer: reg=%x, val=%x\n", port, value);)
232fd1aaeccSCameron Grant     	ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
233fd1aaeccSCameron Grant     	DELAY(10);
234fd1aaeccSCameron Grant     	ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff));
235fd1aaeccSCameron Grant     	DELAY(10);
236fd1aaeccSCameron Grant }
237fd1aaeccSCameron Grant 
238fd1aaeccSCameron Grant static int
ess_getmixer(struct ess_info * sc,u_int port)239fd1aaeccSCameron Grant ess_getmixer(struct ess_info *sc, u_int port)
240fd1aaeccSCameron Grant {
241fd1aaeccSCameron Grant     	int val;
242fd1aaeccSCameron Grant 
243fd1aaeccSCameron Grant     	ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
244fd1aaeccSCameron Grant     	DELAY(10);
245fd1aaeccSCameron Grant     	val = ess_rd(sc, SB_MIX_DATA);
246fd1aaeccSCameron Grant     	DELAY(10);
247fd1aaeccSCameron Grant 
248fd1aaeccSCameron Grant     	return val;
249fd1aaeccSCameron Grant }
250fd1aaeccSCameron Grant 
251fd1aaeccSCameron Grant static int
ess_get_byte(struct ess_info * sc)252fd1aaeccSCameron Grant ess_get_byte(struct ess_info *sc)
253fd1aaeccSCameron Grant {
254fd1aaeccSCameron Grant     	int i;
255fd1aaeccSCameron Grant 
256fd1aaeccSCameron Grant     	for (i = 1000; i > 0; i--) {
2570edeb3dcSNick Sayer 		if (ess_rd(sc, 0xc) & 0x40)
258fd1aaeccSCameron Grant 			return ess_rd(sc, DSP_READ);
259fd1aaeccSCameron Grant 		else
260fd1aaeccSCameron Grant 			DELAY(20);
261fd1aaeccSCameron Grant     	}
262fd1aaeccSCameron Grant     	return -1;
263fd1aaeccSCameron Grant }
264fd1aaeccSCameron Grant 
265fd1aaeccSCameron Grant static int
ess_write(struct ess_info * sc,u_char reg,int val)266fd1aaeccSCameron Grant ess_write(struct ess_info *sc, u_char reg, int val)
267fd1aaeccSCameron Grant {
268fd1aaeccSCameron Grant     	return ess_cmd1(sc, reg, val);
269fd1aaeccSCameron Grant }
270fd1aaeccSCameron Grant 
271fd1aaeccSCameron Grant static int
ess_read(struct ess_info * sc,u_char reg)272fd1aaeccSCameron Grant ess_read(struct ess_info *sc, u_char reg)
273fd1aaeccSCameron Grant {
274fd1aaeccSCameron Grant     	return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1;
275fd1aaeccSCameron Grant }
276fd1aaeccSCameron Grant 
277fd1aaeccSCameron Grant static int
ess_reset_dsp(struct ess_info * sc)278fd1aaeccSCameron Grant ess_reset_dsp(struct ess_info *sc)
279fd1aaeccSCameron Grant {
280a7e11506SNick Sayer 	DEB(printf("ess_reset_dsp\n"));
281fd1aaeccSCameron Grant     	ess_wr(sc, SBDSP_RST, 3);
282fd1aaeccSCameron Grant     	DELAY(100);
283fd1aaeccSCameron Grant     	ess_wr(sc, SBDSP_RST, 0);
284fd1aaeccSCameron Grant     	if (ess_get_byte(sc) != 0xAA) {
285a7e11506SNick Sayer         	DEB(printf("ess_reset_dsp failed\n"));
286a7e11506SNick Sayer /*
287fd1aaeccSCameron Grant 			   rman_get_start(d->io_base)));
288a7e11506SNick Sayer */
289fd1aaeccSCameron Grant 		return ENXIO;	/* Sorry */
290fd1aaeccSCameron Grant     	}
291fd1aaeccSCameron Grant     	ess_cmd(sc, 0xc6);
292fd1aaeccSCameron Grant     	return 0;
293fd1aaeccSCameron Grant }
294fd1aaeccSCameron Grant 
295fd1aaeccSCameron Grant static void
ess_intr(void * arg)296fd1aaeccSCameron Grant ess_intr(void *arg)
297fd1aaeccSCameron Grant {
298fd1aaeccSCameron Grant     	struct ess_info *sc = (struct ess_info *)arg;
2993ac1ca33SNick Sayer 	int src, pirq = 0, rirq = 0;
300fd1aaeccSCameron Grant 
3018b6d3fe1SAriff Abdullah 	ess_lock(sc);
302fd1aaeccSCameron Grant 	src = 0;
303fd1aaeccSCameron Grant 	if (ess_getmixer(sc, 0x7a) & 0x80)
304fd1aaeccSCameron Grant 		src |= 2;
305fd1aaeccSCameron Grant 	if (ess_rd(sc, 0x0c) & 0x01)
306fd1aaeccSCameron Grant 		src |= 1;
307fd1aaeccSCameron Grant 
3088d934d50SPyun YongHyeon 	if (src == 0) {
3098d934d50SPyun YongHyeon 		ess_unlock(sc);
3104e77c048SCameron Grant 		return;
3118d934d50SPyun YongHyeon 	}
3124e77c048SCameron Grant 
3133ac1ca33SNick Sayer 	if (sc->duplex) {
314fd1aaeccSCameron Grant 		pirq = (src & sc->pch.hwch)? 1 : 0;
315fd1aaeccSCameron Grant 		rirq = (src & sc->rch.hwch)? 1 : 0;
3163ac1ca33SNick Sayer 	} else {
3173ac1ca33SNick Sayer 		if (sc->simplex_dir == PCMDIR_PLAY)
3183ac1ca33SNick Sayer 			pirq = 1;
3193ac1ca33SNick Sayer 		if (sc->simplex_dir == PCMDIR_REC)
3203ac1ca33SNick Sayer 			rirq = 1;
3213ac1ca33SNick Sayer 		if (!pirq && !rirq)
3223ac1ca33SNick Sayer 			printf("solo: IRQ neither playback nor rec!\n");
3233ac1ca33SNick Sayer 	}
324fd1aaeccSCameron Grant 
325a7e11506SNick Sayer 	DEB(printf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq));
326a7e11506SNick Sayer 
327fd1aaeccSCameron Grant 	if (pirq) {
328fd1aaeccSCameron Grant 		if (sc->pch.stopping) {
329fd1aaeccSCameron Grant 			ess_dmatrigger(sc, sc->pch.hwch, 0);
330fd1aaeccSCameron Grant 			sc->pch.stopping = 0;
331fd1aaeccSCameron Grant 			if (sc->pch.hwch == 1)
332fd1aaeccSCameron Grant 				ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
333fd1aaeccSCameron Grant 			else
334fd1aaeccSCameron Grant 				ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03);
335fd1aaeccSCameron Grant 		}
3368b6d3fe1SAriff Abdullah 		ess_unlock(sc);
337fd1aaeccSCameron Grant 		chn_intr(sc->pch.channel);
3388b6d3fe1SAriff Abdullah 		ess_lock(sc);
339fd1aaeccSCameron Grant 	}
340fd1aaeccSCameron Grant 
341fd1aaeccSCameron Grant 	if (rirq) {
342fd1aaeccSCameron Grant 		if (sc->rch.stopping) {
343fd1aaeccSCameron Grant 			ess_dmatrigger(sc, sc->rch.hwch, 0);
344fd1aaeccSCameron Grant 			sc->rch.stopping = 0;
345fd1aaeccSCameron Grant 			/* XXX: will this stop audio2? */
346fd1aaeccSCameron Grant 			ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
347fd1aaeccSCameron Grant 		}
3488b6d3fe1SAriff Abdullah 		ess_unlock(sc);
349fd1aaeccSCameron Grant 		chn_intr(sc->rch.channel);
3508b6d3fe1SAriff Abdullah 		ess_lock(sc);
351fd1aaeccSCameron Grant 	}
352fd1aaeccSCameron Grant 
353fd1aaeccSCameron Grant 	if (src & 2)
354fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80);
355fd1aaeccSCameron Grant 	if (src & 1)
356fd1aaeccSCameron Grant     		ess_rd(sc, DSP_DATA_AVAIL);
3578b6d3fe1SAriff Abdullah 
3588b6d3fe1SAriff Abdullah 	ess_unlock(sc);
359fd1aaeccSCameron Grant }
360fd1aaeccSCameron Grant 
361fd1aaeccSCameron Grant /* utility functions for ESS */
362fd1aaeccSCameron Grant static u_int8_t
ess_calcspeed8(int * spd)363fd1aaeccSCameron Grant ess_calcspeed8(int *spd)
364fd1aaeccSCameron Grant {
365fd1aaeccSCameron Grant 	int speed = *spd;
366fd1aaeccSCameron Grant 	u_int32_t t;
367fd1aaeccSCameron Grant 
368fd1aaeccSCameron Grant 	if (speed > 22000) {
369fd1aaeccSCameron Grant 		t = (795500 + speed / 2) / speed;
370fd1aaeccSCameron Grant 		speed = (795500 + t / 2) / t;
371fd1aaeccSCameron Grant 		t = (256 - t) | 0x80;
372fd1aaeccSCameron Grant 	} else {
373fd1aaeccSCameron Grant 		t = (397700 + speed / 2) / speed;
374fd1aaeccSCameron Grant 		speed = (397700 + t / 2) / t;
375fd1aaeccSCameron Grant 		t = 128 - t;
376fd1aaeccSCameron Grant 	}
377fd1aaeccSCameron Grant 	*spd = speed;
378fd1aaeccSCameron Grant 	return t & 0x000000ff;
379fd1aaeccSCameron Grant }
380fd1aaeccSCameron Grant 
381fd1aaeccSCameron Grant static u_int8_t
ess_calcspeed9(int * spd)382fd1aaeccSCameron Grant ess_calcspeed9(int *spd)
383fd1aaeccSCameron Grant {
384fd1aaeccSCameron Grant 	int speed, s0, s1, use0;
385fd1aaeccSCameron Grant 	u_int8_t t0, t1;
386fd1aaeccSCameron Grant 
387fd1aaeccSCameron Grant 	/* rate = source / (256 - divisor) */
388fd1aaeccSCameron Grant 	/* divisor = 256 - (source / rate) */
389fd1aaeccSCameron Grant 	speed = *spd;
390fd1aaeccSCameron Grant 	t0 = 128 - (793800 / speed);
391fd1aaeccSCameron Grant 	s0 = 793800 / (128 - t0);
392fd1aaeccSCameron Grant 
393fd1aaeccSCameron Grant 	t1 = 128 - (768000 / speed);
394fd1aaeccSCameron Grant 	s1 = 768000 / (128 - t1);
395fd1aaeccSCameron Grant 	t1 |= 0x80;
396fd1aaeccSCameron Grant 
397fd1aaeccSCameron Grant 	use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0;
398fd1aaeccSCameron Grant 
399fd1aaeccSCameron Grant 	*spd = use0? s0 : s1;
400fd1aaeccSCameron Grant 	return use0? t0 : t1;
401fd1aaeccSCameron Grant }
402fd1aaeccSCameron Grant 
403fd1aaeccSCameron Grant static u_int8_t
ess_calcfilter(int spd)404fd1aaeccSCameron Grant ess_calcfilter(int spd)
405fd1aaeccSCameron Grant {
406fd1aaeccSCameron Grant 	int cutoff;
407fd1aaeccSCameron Grant 
408fd1aaeccSCameron Grant 	/* cutoff = 7160000 / (256 - divisor) */
409fd1aaeccSCameron Grant 	/* divisor = 256 - (7160000 / cutoff) */
410fd1aaeccSCameron Grant 	cutoff = (spd * 9 * 82) / 20;
411fd1aaeccSCameron Grant 	return (256 - (7160000 / cutoff));
412fd1aaeccSCameron Grant }
413fd1aaeccSCameron Grant 
414fd1aaeccSCameron Grant static int
ess_setupch(struct ess_info * sc,int ch,int dir,int spd,u_int32_t fmt,int len)415fd1aaeccSCameron Grant ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
416fd1aaeccSCameron Grant {
417fd1aaeccSCameron Grant 	int play = (dir == PCMDIR_PLAY)? 1 : 0;
418fd1aaeccSCameron Grant 	int b16 = (fmt & AFMT_16BIT)? 1 : 0;
41990da2b28SAriff Abdullah 	int stereo = (AFMT_CHANNEL(fmt) > 1)? 1 : 0;
42090da2b28SAriff Abdullah 	int unsign = (!(fmt & AFMT_SIGNED))? 1 : 0;
421fd1aaeccSCameron Grant 	u_int8_t spdval, fmtval;
422fd1aaeccSCameron Grant 
423a7e11506SNick Sayer 	DEB(printf("ess_setupch\n"));
424fd1aaeccSCameron Grant 	spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd);
425fd1aaeccSCameron Grant 
4263ac1ca33SNick Sayer 	sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ;
4273ac1ca33SNick Sayer 
428fd1aaeccSCameron Grant 	if (ch == 1) {
429fd1aaeccSCameron Grant 		KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad"));
430fd1aaeccSCameron Grant 		len = -len;
431fd1aaeccSCameron Grant 		/* transfer length low */
432fd1aaeccSCameron Grant 		ess_write(sc, 0xa4, len & 0x00ff);
433fd1aaeccSCameron Grant 		/* transfer length high */
434fd1aaeccSCameron Grant 		ess_write(sc, 0xa5, (len & 0xff00) >> 8);
435fd1aaeccSCameron Grant 		/* autoinit, dma dir */
436a7e11506SNick Sayer 		ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a));
437fd1aaeccSCameron Grant 		/* mono/stereo */
438fd1aaeccSCameron Grant 		ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02));
439fd1aaeccSCameron Grant 		/* demand mode, 4 bytes/xfer */
440fd1aaeccSCameron Grant 		ess_write(sc, 0xb9, 0x02);
441fd1aaeccSCameron Grant 		/* sample rate */
442fd1aaeccSCameron Grant         	ess_write(sc, 0xa1, spdval);
443fd1aaeccSCameron Grant 		/* filter cutoff */
444fd1aaeccSCameron Grant 		ess_write(sc, 0xa2, ess_calcfilter(spd));
445fd1aaeccSCameron Grant 		/* setup dac/adc */
446fd1aaeccSCameron Grant 		/*
447fd1aaeccSCameron Grant 		if (play)
448fd1aaeccSCameron Grant 			ess_write(sc, 0xb6, unsign? 0x80 : 0x00);
449fd1aaeccSCameron Grant 		*/
450fd1aaeccSCameron Grant 		/* mono, b16: signed, load signal */
451fd1aaeccSCameron Grant 		/*
452fd1aaeccSCameron Grant 		ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20));
453fd1aaeccSCameron Grant 		*/
454fd1aaeccSCameron Grant 		/* setup fifo */
455a7e11506SNick Sayer 		ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) |
456fd1aaeccSCameron Grant 					   (b16? 0x04 : 0x00) |
457fd1aaeccSCameron Grant 					   (stereo? 0x08 : 0x40));
458fd1aaeccSCameron Grant 		/* irq control */
459fd1aaeccSCameron Grant 		ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50);
460fd1aaeccSCameron Grant 		/* drq control */
461fd1aaeccSCameron Grant 		ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50);
462fd1aaeccSCameron Grant 	} else if (ch == 2) {
463fd1aaeccSCameron Grant 		KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad"));
464fd1aaeccSCameron Grant 		len >>= 1;
465fd1aaeccSCameron Grant 		len = -len;
466fd1aaeccSCameron Grant 		/* transfer length low */
467fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x74, len & 0x00ff);
468fd1aaeccSCameron Grant 		/* transfer length high */
469fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x76, (len & 0xff00) >> 8);
470fd1aaeccSCameron Grant 		/* autoinit, 4 bytes/req */
471fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x78, 0x10);
47280a8e065SNick Sayer 		fmtval = b16 | (stereo << 1) | ((!unsign) << 2);
473fd1aaeccSCameron Grant 		/* enable irq, set format */
474fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x7a, 0x40 | fmtval);
475fd1aaeccSCameron Grant 		if (sc->newspeed) {
476fd1aaeccSCameron Grant 			/* sample rate */
477fd1aaeccSCameron Grant 			ess_setmixer(sc, 0x70, spdval);
478fd1aaeccSCameron Grant 			/* filter cutoff */
479fd1aaeccSCameron Grant 			ess_setmixer(sc, 0x72, ess_calcfilter(spd));
480fd1aaeccSCameron Grant 		}
481fd1aaeccSCameron Grant 	}
482fd1aaeccSCameron Grant 	return 0;
483fd1aaeccSCameron Grant }
484fd1aaeccSCameron Grant static int
ess_start(struct ess_chinfo * ch)485fd1aaeccSCameron Grant ess_start(struct ess_chinfo *ch)
486fd1aaeccSCameron Grant {
487fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
488fd1aaeccSCameron Grant 
489a7e11506SNick Sayer 	DEB(printf("ess_start\n"););
490350a5fafSCameron Grant 	ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz);
491fd1aaeccSCameron Grant 	ch->stopping = 0;
49219a0702eSNick Sayer 	if (ch->hwch == 1) {
493fd1aaeccSCameron Grant 		ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01);
49419a0702eSNick Sayer 		if (ch->dir == PCMDIR_PLAY) {
4950edeb3dcSNick Sayer #if 0
49619a0702eSNick Sayer 			DELAY(100000); /* 100 ms */
4970edeb3dcSNick Sayer #endif
49819a0702eSNick Sayer 			ess_cmd(sc, 0xd1);
49919a0702eSNick Sayer 		}
50019a0702eSNick Sayer 	} else
501fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03);
502fd1aaeccSCameron Grant 	return 0;
503fd1aaeccSCameron Grant }
504fd1aaeccSCameron Grant 
505fd1aaeccSCameron Grant static int
ess_stop(struct ess_chinfo * ch)506fd1aaeccSCameron Grant ess_stop(struct ess_chinfo *ch)
507fd1aaeccSCameron Grant {
508fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
509fd1aaeccSCameron Grant 
510a7e11506SNick Sayer 	DEB(printf("ess_stop\n"));
511fd1aaeccSCameron Grant 	ch->stopping = 1;
512fd1aaeccSCameron Grant 	if (ch->hwch == 1)
513fd1aaeccSCameron Grant 		ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04);
514fd1aaeccSCameron Grant 	else
515fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10);
516a7e11506SNick Sayer 	DEB(printf("done with stop\n"));
517fd1aaeccSCameron Grant 	return 0;
518fd1aaeccSCameron Grant }
519fd1aaeccSCameron Grant 
5200f55ac6cSCameron Grant /* -------------------------------------------------------------------- */
521fd1aaeccSCameron Grant /* channel interface for ESS18xx */
522fd1aaeccSCameron Grant static void *
esschan_init(kobj_t obj,void * devinfo,struct snd_dbuf * b,struct pcm_channel * c,int dir)52366ef8af5SCameron Grant esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
524fd1aaeccSCameron Grant {
525fd1aaeccSCameron Grant 	struct ess_info *sc = devinfo;
526fd1aaeccSCameron Grant 	struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
527fd1aaeccSCameron Grant 
528a7e11506SNick Sayer 	DEB(printf("esschan_init\n"));
529fd1aaeccSCameron Grant 	ch->parent = sc;
530fd1aaeccSCameron Grant 	ch->channel = c;
531fd1aaeccSCameron Grant 	ch->buffer = b;
532306f91b6SCameron Grant 	ch->dir = dir;
5332e334adfSAriff Abdullah 	if (sndbuf_alloc(ch->buffer, sc->parent_dmat, 0, sc->bufsz) != 0)
534fd1aaeccSCameron Grant 		return NULL;
535fd1aaeccSCameron Grant 	ch->hwch = 1;
536fd1aaeccSCameron Grant 	if ((dir == PCMDIR_PLAY) && (sc->duplex))
537fd1aaeccSCameron Grant 		ch->hwch = 2;
538fd1aaeccSCameron Grant 	return ch;
539fd1aaeccSCameron Grant }
540fd1aaeccSCameron Grant 
541fd1aaeccSCameron Grant static int
esschan_setformat(kobj_t obj,void * data,u_int32_t format)5420f55ac6cSCameron Grant esschan_setformat(kobj_t obj, void *data, u_int32_t format)
543fd1aaeccSCameron Grant {
544fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
545fd1aaeccSCameron Grant 
546fd1aaeccSCameron Grant 	ch->fmt = format;
547fd1aaeccSCameron Grant 	return 0;
548fd1aaeccSCameron Grant }
549fd1aaeccSCameron Grant 
55090da2b28SAriff Abdullah static u_int32_t
esschan_setspeed(kobj_t obj,void * data,u_int32_t speed)5510f55ac6cSCameron Grant esschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
552fd1aaeccSCameron Grant {
553fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
554fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
555fd1aaeccSCameron Grant 
556fd1aaeccSCameron Grant 	ch->spd = speed;
557fd1aaeccSCameron Grant 	if (sc->newspeed)
558fd1aaeccSCameron Grant 		ess_calcspeed9(&ch->spd);
559fd1aaeccSCameron Grant 	else
560fd1aaeccSCameron Grant 		ess_calcspeed8(&ch->spd);
561fd1aaeccSCameron Grant 	return ch->spd;
562fd1aaeccSCameron Grant }
563fd1aaeccSCameron Grant 
56490da2b28SAriff Abdullah static u_int32_t
esschan_setblocksize(kobj_t obj,void * data,u_int32_t blocksize)5650f55ac6cSCameron Grant esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
566fd1aaeccSCameron Grant {
567350a5fafSCameron Grant 	struct ess_chinfo *ch = data;
568350a5fafSCameron Grant 
569350a5fafSCameron Grant 	ch->blksz = blocksize;
570350a5fafSCameron Grant 	return ch->blksz;
571fd1aaeccSCameron Grant }
572fd1aaeccSCameron Grant 
573fd1aaeccSCameron Grant static int
esschan_trigger(kobj_t obj,void * data,int go)5740f55ac6cSCameron Grant esschan_trigger(kobj_t obj, void *data, int go)
575fd1aaeccSCameron Grant {
576fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
577fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
578fd1aaeccSCameron Grant 
579bdfbdcecSAriff Abdullah 	if (!PCMTRIG_COMMON(go))
580fd1aaeccSCameron Grant 		return 0;
581fd1aaeccSCameron Grant 
582bdfbdcecSAriff Abdullah 	DEB(printf("esschan_trigger: %d\n",go));
583bdfbdcecSAriff Abdullah 
5848b6d3fe1SAriff Abdullah 	ess_lock(sc);
585fd1aaeccSCameron Grant 	switch (go) {
586fd1aaeccSCameron Grant 	case PCMTRIG_START:
58738cc9942SOlivier Houchard 		ess_dmasetup(sc, ch->hwch, sndbuf_getbufaddr(ch->buffer), sndbuf_getsize(ch->buffer), ch->dir);
588fd1aaeccSCameron Grant 		ess_dmatrigger(sc, ch->hwch, 1);
589fd1aaeccSCameron Grant 		ess_start(ch);
590fd1aaeccSCameron Grant 		break;
591fd1aaeccSCameron Grant 
592fd1aaeccSCameron Grant 	case PCMTRIG_STOP:
593fd1aaeccSCameron Grant 	case PCMTRIG_ABORT:
594fd1aaeccSCameron Grant 	default:
595fd1aaeccSCameron Grant 		ess_stop(ch);
596fd1aaeccSCameron Grant 		break;
597fd1aaeccSCameron Grant 	}
5988b6d3fe1SAriff Abdullah 	ess_unlock(sc);
599fd1aaeccSCameron Grant 	return 0;
600fd1aaeccSCameron Grant }
601fd1aaeccSCameron Grant 
60290da2b28SAriff Abdullah static u_int32_t
esschan_getptr(kobj_t obj,void * data)6030f55ac6cSCameron Grant esschan_getptr(kobj_t obj, void *data)
604fd1aaeccSCameron Grant {
605fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
606fd1aaeccSCameron Grant 	struct ess_info *sc = ch->parent;
60790da2b28SAriff Abdullah 	u_int32_t ret;
608fd1aaeccSCameron Grant 
6098b6d3fe1SAriff Abdullah 	ess_lock(sc);
6108b6d3fe1SAriff Abdullah 	ret = ess_dmapos(sc, ch->hwch);
6118b6d3fe1SAriff Abdullah 	ess_unlock(sc);
6128b6d3fe1SAriff Abdullah 	return ret;
613fd1aaeccSCameron Grant }
614fd1aaeccSCameron Grant 
61566ef8af5SCameron Grant static struct pcmchan_caps *
esschan_getcaps(kobj_t obj,void * data)6160f55ac6cSCameron Grant esschan_getcaps(kobj_t obj, void *data)
617fd1aaeccSCameron Grant {
618fd1aaeccSCameron Grant 	struct ess_chinfo *ch = data;
619fd1aaeccSCameron Grant 
620fd1aaeccSCameron Grant 	return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
621fd1aaeccSCameron Grant }
622fd1aaeccSCameron Grant 
6230f55ac6cSCameron Grant static kobj_method_t esschan_methods[] = {
6240f55ac6cSCameron Grant     	KOBJMETHOD(channel_init,		esschan_init),
6250f55ac6cSCameron Grant     	KOBJMETHOD(channel_setformat,		esschan_setformat),
6260f55ac6cSCameron Grant     	KOBJMETHOD(channel_setspeed,		esschan_setspeed),
6270f55ac6cSCameron Grant     	KOBJMETHOD(channel_setblocksize,	esschan_setblocksize),
6280f55ac6cSCameron Grant     	KOBJMETHOD(channel_trigger,		esschan_trigger),
6290f55ac6cSCameron Grant     	KOBJMETHOD(channel_getptr,		esschan_getptr),
6300f55ac6cSCameron Grant     	KOBJMETHOD(channel_getcaps,		esschan_getcaps),
63190da2b28SAriff Abdullah 	KOBJMETHOD_END
6320f55ac6cSCameron Grant };
6330f55ac6cSCameron Grant CHANNEL_DECLARE(esschan);
6340f55ac6cSCameron Grant 
635fd1aaeccSCameron Grant /************************************************************/
636fd1aaeccSCameron Grant 
637fd1aaeccSCameron Grant static int
essmix_init(struct snd_mixer * m)63866ef8af5SCameron Grant essmix_init(struct snd_mixer *m)
639fd1aaeccSCameron Grant {
640fd1aaeccSCameron Grant     	struct ess_info *sc = mix_getdevinfo(m);
641fd1aaeccSCameron Grant 
642fd1aaeccSCameron Grant 	mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE |
643fd1aaeccSCameron Grant 			  SOUND_MASK_IMIX);
644fd1aaeccSCameron Grant 
645fd1aaeccSCameron Grant 	mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
646fd1aaeccSCameron Grant 		       SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME |
647fd1aaeccSCameron Grant 		       SOUND_MASK_LINE1);
648fd1aaeccSCameron Grant 
649fd1aaeccSCameron Grant 	ess_setmixer(sc, 0, 0); /* reset */
650fd1aaeccSCameron Grant 
651fd1aaeccSCameron Grant 	return 0;
652fd1aaeccSCameron Grant }
653fd1aaeccSCameron Grant 
654fd1aaeccSCameron Grant static int
essmix_set(struct snd_mixer * m,unsigned dev,unsigned left,unsigned right)65566ef8af5SCameron Grant essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
656fd1aaeccSCameron Grant {
657fd1aaeccSCameron Grant     	struct ess_info *sc = mix_getdevinfo(m);
658fd1aaeccSCameron Grant     	int preg = 0, rreg = 0, l, r;
659fd1aaeccSCameron Grant 
660fd1aaeccSCameron Grant 	l = (left * 15) / 100;
661fd1aaeccSCameron Grant 	r = (right * 15) / 100;
662fd1aaeccSCameron Grant 	switch (dev) {
663fd1aaeccSCameron Grant 	case SOUND_MIXER_SYNTH:
664fd1aaeccSCameron Grant 		preg = 0x36;
665fd1aaeccSCameron Grant 		rreg = 0x6b;
666fd1aaeccSCameron Grant 		break;
667fd1aaeccSCameron Grant 
668fd1aaeccSCameron Grant 	case SOUND_MIXER_PCM:
669fd1aaeccSCameron Grant 		preg = 0x14;
670fd1aaeccSCameron Grant 		rreg = 0x7c;
671fd1aaeccSCameron Grant 		break;
672fd1aaeccSCameron Grant 
673fd1aaeccSCameron Grant 	case SOUND_MIXER_LINE:
674fd1aaeccSCameron Grant 		preg = 0x3e;
675fd1aaeccSCameron Grant 		rreg = 0x6e;
676fd1aaeccSCameron Grant 		break;
677fd1aaeccSCameron Grant 
678fd1aaeccSCameron Grant 	case SOUND_MIXER_MIC:
679fd1aaeccSCameron Grant 		preg = 0x1a;
680fd1aaeccSCameron Grant 		rreg = 0x68;
681fd1aaeccSCameron Grant 		break;
682fd1aaeccSCameron Grant 
683fd1aaeccSCameron Grant 	case SOUND_MIXER_LINE1:
684fd1aaeccSCameron Grant 		preg = 0x3a;
685fd1aaeccSCameron Grant 		rreg = 0x6c;
686fd1aaeccSCameron Grant 		break;
687fd1aaeccSCameron Grant 
688fd1aaeccSCameron Grant 	case SOUND_MIXER_CD:
689fd1aaeccSCameron Grant 		preg = 0x38;
690fd1aaeccSCameron Grant 		rreg = 0x6a;
691fd1aaeccSCameron Grant 		break;
692fd1aaeccSCameron Grant 
693fd1aaeccSCameron Grant 	case SOUND_MIXER_VOLUME:
694fd1aaeccSCameron Grant 		l = left? (left * 63) / 100 : 64;
695fd1aaeccSCameron Grant 		r = right? (right * 63) / 100 : 64;
696fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x60, l);
697fd1aaeccSCameron Grant 		ess_setmixer(sc, 0x62, r);
698fd1aaeccSCameron Grant 		left = (l == 64)? 0 : (l * 100) / 63;
699fd1aaeccSCameron Grant 		right = (r == 64)? 0 : (r * 100) / 63;
700fd1aaeccSCameron Grant     		return left | (right << 8);
701fd1aaeccSCameron Grant 	}
702fd1aaeccSCameron Grant 
703fd1aaeccSCameron Grant 	if (preg)
704fd1aaeccSCameron Grant 		ess_setmixer(sc, preg, (l << 4) | r);
705fd1aaeccSCameron Grant 	if (rreg)
706fd1aaeccSCameron Grant 		ess_setmixer(sc, rreg, (l << 4) | r);
707fd1aaeccSCameron Grant 
708fd1aaeccSCameron Grant 	left = (l * 100) / 15;
709fd1aaeccSCameron Grant 	right = (r * 100) / 15;
710fd1aaeccSCameron Grant 
711fd1aaeccSCameron Grant     	return left | (right << 8);
712fd1aaeccSCameron Grant }
713fd1aaeccSCameron Grant 
71490da2b28SAriff Abdullah static u_int32_t
essmix_setrecsrc(struct snd_mixer * m,u_int32_t src)71566ef8af5SCameron Grant essmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
716fd1aaeccSCameron Grant {
717fd1aaeccSCameron Grant     	struct ess_info *sc = mix_getdevinfo(m);
718fd1aaeccSCameron Grant     	u_char recdev;
719fd1aaeccSCameron Grant 
720fd1aaeccSCameron Grant     	switch (src) {
721fd1aaeccSCameron Grant 	case SOUND_MASK_CD:
722fd1aaeccSCameron Grant 		recdev = 0x02;
723fd1aaeccSCameron Grant 		break;
724fd1aaeccSCameron Grant 
725fd1aaeccSCameron Grant 	case SOUND_MASK_LINE:
726fd1aaeccSCameron Grant 		recdev = 0x06;
727fd1aaeccSCameron Grant 		break;
728fd1aaeccSCameron Grant 
729fd1aaeccSCameron Grant 	case SOUND_MASK_IMIX:
730fd1aaeccSCameron Grant 		recdev = 0x05;
731fd1aaeccSCameron Grant 		break;
732fd1aaeccSCameron Grant 
733fd1aaeccSCameron Grant 	case SOUND_MASK_MIC:
734fd1aaeccSCameron Grant 	default:
735fd1aaeccSCameron Grant 		recdev = 0x00;
736fd1aaeccSCameron Grant 		src = SOUND_MASK_MIC;
737fd1aaeccSCameron Grant 		break;
738fd1aaeccSCameron Grant 	}
739fd1aaeccSCameron Grant 
740fd1aaeccSCameron Grant 	ess_setmixer(sc, 0x1c, recdev);
741fd1aaeccSCameron Grant 
742fd1aaeccSCameron Grant 	return src;
743fd1aaeccSCameron Grant }
744fd1aaeccSCameron Grant 
7450f55ac6cSCameron Grant static kobj_method_t solomixer_methods[] = {
7460f55ac6cSCameron Grant     	KOBJMETHOD(mixer_init,		essmix_init),
7470f55ac6cSCameron Grant     	KOBJMETHOD(mixer_set,		essmix_set),
7480f55ac6cSCameron Grant     	KOBJMETHOD(mixer_setrecsrc,	essmix_setrecsrc),
74990da2b28SAriff Abdullah 	KOBJMETHOD_END
7500f55ac6cSCameron Grant };
7510f55ac6cSCameron Grant MIXER_DECLARE(solomixer);
7520f55ac6cSCameron Grant 
753fd1aaeccSCameron Grant /************************************************************/
754fd1aaeccSCameron Grant 
755fd1aaeccSCameron Grant static int
ess_dmasetup(struct ess_info * sc,int ch,u_int32_t base,u_int16_t cnt,int dir)756fd1aaeccSCameron Grant ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir)
757fd1aaeccSCameron Grant {
758fd1aaeccSCameron Grant 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
759fd1aaeccSCameron Grant 	sc->dmasz[ch - 1] = cnt;
760fd1aaeccSCameron Grant 	if (ch == 1) {
76119a0702eSNick Sayer 		port_wr(sc->vc, 0x8, 0xc4, 1); /* command */
762fd1aaeccSCameron Grant 		port_wr(sc->vc, 0xd, 0xff, 1); /* reset */
763fd1aaeccSCameron Grant 		port_wr(sc->vc, 0xf, 0x01, 1); /* mask */
76419a0702eSNick Sayer 		port_wr(sc->vc, 0xb, dir == PCMDIR_PLAY? 0x58 : 0x54, 1); /* mode */
765fd1aaeccSCameron Grant 		port_wr(sc->vc, 0x0, base, 4);
766bb7f26c3SNick Sayer 		port_wr(sc->vc, 0x4, cnt - 1, 2);
767fd1aaeccSCameron Grant 
768fd1aaeccSCameron Grant 	} else if (ch == 2) {
769fd1aaeccSCameron Grant 		port_wr(sc->io, 0x6, 0x08, 1); /* autoinit */
770fd1aaeccSCameron Grant 		port_wr(sc->io, 0x0, base, 4);
771fd1aaeccSCameron Grant 		port_wr(sc->io, 0x4, cnt, 2);
772fd1aaeccSCameron Grant 	}
773fd1aaeccSCameron Grant 	return 0;
774fd1aaeccSCameron Grant }
775fd1aaeccSCameron Grant 
776fd1aaeccSCameron Grant static int
ess_dmapos(struct ess_info * sc,int ch)777fd1aaeccSCameron Grant ess_dmapos(struct ess_info *sc, int ch)
778fd1aaeccSCameron Grant {
7796ba60b3cSNick Sayer 	int p = 0, i = 0, j = 0;
780fd1aaeccSCameron Grant 
781fd1aaeccSCameron Grant 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
7820edeb3dcSNick Sayer 	if (ch == 1) {
7830edeb3dcSNick Sayer /*
7840edeb3dcSNick Sayer  * During recording, this register is known to give back
7850edeb3dcSNick Sayer  * garbage if it's not quiescent while being read. That's
7866ba60b3cSNick Sayer  * why we spl, stop the DMA, and try over and over until
7876ba60b3cSNick Sayer  * adjacent reads are "close", in the right order and not
7886ba60b3cSNick Sayer  * bigger than is otherwise possible.
7890edeb3dcSNick Sayer  */
7900edeb3dcSNick Sayer 		ess_dmatrigger(sc, ch, 0);
7910edeb3dcSNick Sayer 		DELAY(20);
7926ba60b3cSNick Sayer 		do {
7936ba60b3cSNick Sayer 			DELAY(10);
7946ba60b3cSNick Sayer 			if (j > 1)
7956ba60b3cSNick Sayer 				printf("DMA count reg bogus: %04x & %04x\n",
7966ba60b3cSNick Sayer 					i, p);
7976ba60b3cSNick Sayer 			i = port_rd(sc->vc, 0x4, 2) + 1;
798fd1aaeccSCameron Grant 			p = port_rd(sc->vc, 0x4, 2) + 1;
7996ba60b3cSNick Sayer 		} while ((p > sc->dmasz[ch - 1] || i < p || (p - i) > 0x8) && j++ < 1000);
8000edeb3dcSNick Sayer 		ess_dmatrigger(sc, ch, 1);
8010edeb3dcSNick Sayer 	}
802fd1aaeccSCameron Grant 	else if (ch == 2)
803fd1aaeccSCameron Grant 		p = port_rd(sc->io, 0x4, 2);
804fd1aaeccSCameron Grant 	return sc->dmasz[ch - 1] - p;
805fd1aaeccSCameron Grant }
806fd1aaeccSCameron Grant 
807fd1aaeccSCameron Grant static int
ess_dmatrigger(struct ess_info * sc,int ch,int go)808fd1aaeccSCameron Grant ess_dmatrigger(struct ess_info *sc, int ch, int go)
809fd1aaeccSCameron Grant {
810fd1aaeccSCameron Grant 	KASSERT(ch == 1 || ch == 2, ("bad ch"));
811fd1aaeccSCameron Grant 	if (ch == 1)
8128eb3acc9SNick Sayer 		port_wr(sc->vc, 0xf, go? 0x00 : 0x01, 1); /* mask */
813fd1aaeccSCameron Grant 	else if (ch == 2)
814fd1aaeccSCameron Grant 		port_wr(sc->io, 0x6, 0x08 | (go? 0x02 : 0x00), 1); /* autoinit */
815fd1aaeccSCameron Grant 	return 0;
816fd1aaeccSCameron Grant }
817fd1aaeccSCameron Grant 
818fd1aaeccSCameron Grant static void
ess_release_resources(struct ess_info * sc,device_t dev)819fd1aaeccSCameron Grant ess_release_resources(struct ess_info *sc, device_t dev)
820fd1aaeccSCameron Grant {
821fd1aaeccSCameron Grant     	if (sc->irq) {
822306f91b6SCameron Grant 		if (sc->ih)
823306f91b6SCameron Grant 			bus_teardown_intr(dev, sc->irq, sc->ih);
824fd1aaeccSCameron Grant 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
82587d8fcc8SPedro F. Giffuni 		sc->irq = NULL;
826fd1aaeccSCameron Grant     	}
827fd1aaeccSCameron Grant     	if (sc->io) {
828e27951b2SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->io);
82987d8fcc8SPedro F. Giffuni 		sc->io = NULL;
830fd1aaeccSCameron Grant     	}
831fd1aaeccSCameron Grant 
832fd1aaeccSCameron Grant     	if (sc->sb) {
833e27951b2SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(1), sc->sb);
83487d8fcc8SPedro F. Giffuni 		sc->sb = NULL;
835fd1aaeccSCameron Grant     	}
836fd1aaeccSCameron Grant 
837fd1aaeccSCameron Grant     	if (sc->vc) {
838e27951b2SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(2), sc->vc);
83987d8fcc8SPedro F. Giffuni 		sc->vc = NULL;
840fd1aaeccSCameron Grant     	}
841fd1aaeccSCameron Grant 
842fd1aaeccSCameron Grant     	if (sc->mpu) {
843e27951b2SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(3), sc->mpu);
84487d8fcc8SPedro F. Giffuni 		sc->mpu = NULL;
845fd1aaeccSCameron Grant     	}
846fd1aaeccSCameron Grant 
847fd1aaeccSCameron Grant     	if (sc->gp) {
848e27951b2SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(4), sc->gp);
84987d8fcc8SPedro F. Giffuni 		sc->gp = NULL;
850fd1aaeccSCameron Grant     	}
851fd1aaeccSCameron Grant 
852306f91b6SCameron Grant 	if (sc->parent_dmat) {
853306f91b6SCameron Grant 		bus_dma_tag_destroy(sc->parent_dmat);
854306f91b6SCameron Grant 		sc->parent_dmat = 0;
855306f91b6SCameron Grant     	}
856306f91b6SCameron Grant 
8578b6d3fe1SAriff Abdullah 	if (sc->lock) {
8588b6d3fe1SAriff Abdullah 		snd_mtxfree(sc->lock);
8598b6d3fe1SAriff Abdullah 		sc->lock = NULL;
8608b6d3fe1SAriff Abdullah 	}
8618b6d3fe1SAriff Abdullah 
862fd1aaeccSCameron Grant     	free(sc, M_DEVBUF);
863fd1aaeccSCameron Grant }
864fd1aaeccSCameron Grant 
865fd1aaeccSCameron Grant static int
ess_alloc_resources(struct ess_info * sc,device_t dev)866fd1aaeccSCameron Grant ess_alloc_resources(struct ess_info *sc, device_t dev)
867fd1aaeccSCameron Grant {
868fd1aaeccSCameron Grant 	int rid;
869fd1aaeccSCameron Grant 
870e27951b2SJohn Baldwin 	rid = PCIR_BAR(0);
8715f96beb9SNate Lawson     	sc->io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
872fd1aaeccSCameron Grant 
873e27951b2SJohn Baldwin 	rid = PCIR_BAR(1);
8745f96beb9SNate Lawson     	sc->sb = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
875fd1aaeccSCameron Grant 
876e27951b2SJohn Baldwin 	rid = PCIR_BAR(2);
8775f96beb9SNate Lawson     	sc->vc = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
878fd1aaeccSCameron Grant 
879e27951b2SJohn Baldwin 	rid = PCIR_BAR(3);
8805f96beb9SNate Lawson     	sc->mpu = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
881fd1aaeccSCameron Grant 
882e27951b2SJohn Baldwin 	rid = PCIR_BAR(4);
8835f96beb9SNate Lawson     	sc->gp = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
884fd1aaeccSCameron Grant 
885fd1aaeccSCameron Grant 	rid = 0;
8865f96beb9SNate Lawson 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
8875f96beb9SNate Lawson 		RF_ACTIVE | RF_SHAREABLE);
888fd1aaeccSCameron Grant 
8894582b3a1SAriff Abdullah 	sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_solo softc");
8908b6d3fe1SAriff Abdullah 
8918b6d3fe1SAriff Abdullah 	return (sc->irq && sc->io && sc->sb && sc->vc &&
8928b6d3fe1SAriff Abdullah 				sc->mpu && sc->gp && sc->lock)? 0 : ENXIO;
893fd1aaeccSCameron Grant }
894fd1aaeccSCameron Grant 
895fd1aaeccSCameron Grant static int
ess_probe(device_t dev)896fd1aaeccSCameron Grant ess_probe(device_t dev)
897fd1aaeccSCameron Grant {
898fd1aaeccSCameron Grant 	char *s = NULL;
899fd1aaeccSCameron Grant 	u_int32_t subdev;
900fd1aaeccSCameron Grant 
901fd1aaeccSCameron Grant 	subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev);
902fd1aaeccSCameron Grant 	switch (pci_get_devid(dev)) {
903fd1aaeccSCameron Grant 	case 0x1969125d:
904fd1aaeccSCameron Grant 		if (subdev == 0x8888125d)
905fd1aaeccSCameron Grant 			s = "ESS Solo-1E";
906fd1aaeccSCameron Grant 		else if (subdev == 0x1818125d)
907fd1aaeccSCameron Grant 			s = "ESS Solo-1";
908fd1aaeccSCameron Grant 		else
909fd1aaeccSCameron Grant 			s = "ESS Solo-1 (unknown vendor)";
910fd1aaeccSCameron Grant 		break;
911fd1aaeccSCameron Grant 	}
912fd1aaeccSCameron Grant 
913fd1aaeccSCameron Grant 	if (s)
914fd1aaeccSCameron Grant 		device_set_desc(dev, s);
915d2b677bbSWarner Losh 	return s ? BUS_PROBE_DEFAULT : ENXIO;
916fd1aaeccSCameron Grant }
917fd1aaeccSCameron Grant 
918933ce6faSOrion Hodson #define ESS_PCI_LEGACYCONTROL       0x40
919933ce6faSOrion Hodson #define ESS_PCI_CONFIG              0x50
920933ce6faSOrion Hodson #define ESS_PCI_DDMACONTROL      	0x60
921933ce6faSOrion Hodson 
922933ce6faSOrion Hodson static int
ess_suspend(device_t dev)923933ce6faSOrion Hodson ess_suspend(device_t dev)
924933ce6faSOrion Hodson {
925933ce6faSOrion Hodson   return 0;
926933ce6faSOrion Hodson }
927933ce6faSOrion Hodson 
928933ce6faSOrion Hodson static int
ess_resume(device_t dev)929933ce6faSOrion Hodson ess_resume(device_t dev)
930933ce6faSOrion Hodson {
931933ce6faSOrion Hodson 	uint16_t ddma;
932933ce6faSOrion Hodson 	struct ess_info *sc = pcm_getdevinfo(dev);
933933ce6faSOrion Hodson 
9348b6d3fe1SAriff Abdullah 	ess_lock(sc);
935933ce6faSOrion Hodson 	ddma = rman_get_start(sc->vc) | 1;
936933ce6faSOrion Hodson 	pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2);
937933ce6faSOrion Hodson 	pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2);
938933ce6faSOrion Hodson 	pci_write_config(dev, ESS_PCI_CONFIG, 0, 2);
939933ce6faSOrion Hodson 
9408b6d3fe1SAriff Abdullah     	if (ess_reset_dsp(sc)) {
9418b6d3fe1SAriff Abdullah 		ess_unlock(sc);
942933ce6faSOrion Hodson 		goto no;
9438b6d3fe1SAriff Abdullah 	}
9448b6d3fe1SAriff Abdullah 	ess_unlock(sc);
945933ce6faSOrion Hodson     	if (mixer_reinit(dev))
946933ce6faSOrion Hodson 		goto no;
9478b6d3fe1SAriff Abdullah 	ess_lock(sc);
948933ce6faSOrion Hodson 	if (sc->newspeed)
949933ce6faSOrion Hodson 		ess_setmixer(sc, 0x71, 0x2a);
950933ce6faSOrion Hodson 
951933ce6faSOrion Hodson 	port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */
9528b6d3fe1SAriff Abdullah 	ess_unlock(sc);
953933ce6faSOrion Hodson 
954933ce6faSOrion Hodson 	return 0;
955933ce6faSOrion Hodson  no:
956933ce6faSOrion Hodson 	return EIO;
957933ce6faSOrion Hodson }
958fd1aaeccSCameron Grant 
959fd1aaeccSCameron Grant static int
ess_attach(device_t dev)960fd1aaeccSCameron Grant ess_attach(device_t dev)
961fd1aaeccSCameron Grant {
962fd1aaeccSCameron Grant     	struct ess_info *sc;
963fd1aaeccSCameron Grant     	char status[SND_STATUSLEN];
964fd1aaeccSCameron Grant 	u_int16_t ddma;
965fd1aaeccSCameron Grant 
966082f6383SAriff Abdullah 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
967c68534f1SScott Long 	pci_enable_busmaster(dev);
968fd1aaeccSCameron Grant 
969fd1aaeccSCameron Grant     	if (ess_alloc_resources(sc, dev))
970fd1aaeccSCameron Grant 		goto no;
971fd1aaeccSCameron Grant 
97239dbd126SCameron Grant 	sc->bufsz = pcm_getbuffersize(dev, 4096, SOLO_DEFAULT_BUFSZ, 65536);
97339dbd126SCameron Grant 
974fd1aaeccSCameron Grant 	ddma = rman_get_start(sc->vc) | 1;
975933ce6faSOrion Hodson 	pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2);
976933ce6faSOrion Hodson 	pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2);
977933ce6faSOrion Hodson 	pci_write_config(dev, ESS_PCI_CONFIG, 0, 2);
978fd1aaeccSCameron Grant 
979fd1aaeccSCameron Grant 	port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */
9803ac1ca33SNick Sayer #ifdef ESS18XX_DUPLEX
981fd1aaeccSCameron Grant 	sc->duplex = 1;
9823ac1ca33SNick Sayer #else
9833ac1ca33SNick Sayer 	sc->duplex = 0;
9843ac1ca33SNick Sayer #endif
985fd1aaeccSCameron Grant 
9863ac1ca33SNick Sayer #ifdef ESS18XX_NEWSPEED
9873ac1ca33SNick Sayer 	sc->newspeed = 1;
9883ac1ca33SNick Sayer #else
9893ac1ca33SNick Sayer 	sc->newspeed = 0;
9903ac1ca33SNick Sayer #endif
9911f7a6325SAlexander Motin 	if (snd_setup_intr(dev, sc->irq, INTR_MPSAFE, ess_intr, sc, &sc->ih)) {
9928b6d3fe1SAriff Abdullah 		device_printf(dev, "unable to map interrupt\n");
9938b6d3fe1SAriff Abdullah 		goto no;
9948b6d3fe1SAriff Abdullah 	}
995fd1aaeccSCameron Grant 
996fd1aaeccSCameron Grant     	if (!sc->duplex)
997fd1aaeccSCameron Grant 		pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
998fd1aaeccSCameron Grant 
9998b6d3fe1SAriff Abdullah #if 0
10000b989078SAlexander Leidinger     	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/65536, /*boundary*/0,
10018b6d3fe1SAriff Abdullah #endif
10020b989078SAlexander Leidinger     	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2, /*boundary*/0,
1003fd1aaeccSCameron Grant 			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
1004fd1aaeccSCameron Grant 			/*highaddr*/BUS_SPACE_MAXADDR,
1005fd1aaeccSCameron Grant 			/*filter*/NULL, /*filterarg*/NULL,
100639dbd126SCameron Grant 			/*maxsize*/sc->bufsz, /*nsegments*/1,
1007fd1aaeccSCameron Grant 			/*maxsegz*/0x3ffff,
10088b6d3fe1SAriff Abdullah 			/*flags*/0,
10098b6d3fe1SAriff Abdullah 			/*lockfunc*/NULL, /*lockarg*/NULL,
10108b6d3fe1SAriff Abdullah 			&sc->parent_dmat) != 0) {
1011fd1aaeccSCameron Grant 		device_printf(dev, "unable to create dma tag\n");
1012fd1aaeccSCameron Grant 		goto no;
1013fd1aaeccSCameron Grant     	}
1014fd1aaeccSCameron Grant 
10158b6d3fe1SAriff Abdullah     	if (ess_reset_dsp(sc))
10168b6d3fe1SAriff Abdullah 		goto no;
10178b6d3fe1SAriff Abdullah 
10188b6d3fe1SAriff Abdullah 	if (sc->newspeed)
10198b6d3fe1SAriff Abdullah 		ess_setmixer(sc, 0x71, 0x2a);
10208b6d3fe1SAriff Abdullah 
10218b6d3fe1SAriff Abdullah     	if (mixer_init(dev, &solomixer_class, sc))
10228b6d3fe1SAriff Abdullah 		goto no;
10238b6d3fe1SAriff Abdullah 
1024837cd192SChristos Margiolis 	snprintf(status, SND_STATUSLEN, "port 0x%jx,0x%jx,0x%jx irq %jd on %s",
1025fd1aaeccSCameron Grant     	     	rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc),
1026837cd192SChristos Margiolis 		rman_get_start(sc->irq),
1027837cd192SChristos Margiolis 		device_get_nameunit(device_get_parent(dev)));
1028fd1aaeccSCameron Grant 
1029*516a9c02SChristos Margiolis 	pcm_init(dev, sc);
10300f55ac6cSCameron Grant       	pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc);
10310f55ac6cSCameron Grant 	pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc);
1032*516a9c02SChristos Margiolis 	if (pcm_register(dev, status))
1033*516a9c02SChristos Margiolis 		goto no;
1034fd1aaeccSCameron Grant 
1035fd1aaeccSCameron Grant     	return 0;
1036fd1aaeccSCameron Grant 
1037fd1aaeccSCameron Grant no:
1038fd1aaeccSCameron Grant     	ess_release_resources(sc, dev);
1039fd1aaeccSCameron Grant     	return ENXIO;
1040fd1aaeccSCameron Grant }
1041fd1aaeccSCameron Grant 
1042306f91b6SCameron Grant static int
ess_detach(device_t dev)1043306f91b6SCameron Grant ess_detach(device_t dev)
1044306f91b6SCameron Grant {
1045306f91b6SCameron Grant 	int r;
10467dfc9325SCameron Grant 	struct ess_info *sc;
1047306f91b6SCameron Grant 
1048306f91b6SCameron Grant 	r = pcm_unregister(dev);
1049306f91b6SCameron Grant 	if (r)
1050306f91b6SCameron Grant 		return r;
1051306f91b6SCameron Grant 
1052306f91b6SCameron Grant 	sc = pcm_getdevinfo(dev);
1053306f91b6SCameron Grant     	ess_release_resources(sc, dev);
1054306f91b6SCameron Grant 	return 0;
1055306f91b6SCameron Grant }
1056306f91b6SCameron Grant 
1057fd1aaeccSCameron Grant static device_method_t ess_methods[] = {
1058fd1aaeccSCameron Grant 	/* Device interface */
1059fd1aaeccSCameron Grant 	DEVMETHOD(device_probe,		ess_probe),
1060fd1aaeccSCameron Grant 	DEVMETHOD(device_attach,	ess_attach),
1061306f91b6SCameron Grant 	DEVMETHOD(device_detach,	ess_detach),
1062933ce6faSOrion Hodson 	DEVMETHOD(device_resume,	ess_resume),
1063933ce6faSOrion Hodson 	DEVMETHOD(device_suspend,	ess_suspend),
1064fd1aaeccSCameron Grant 	{ 0, 0 }
1065fd1aaeccSCameron Grant };
1066fd1aaeccSCameron Grant 
1067fd1aaeccSCameron Grant static driver_t ess_driver = {
1068fd1aaeccSCameron Grant 	"pcm",
1069fd1aaeccSCameron Grant 	ess_methods,
107067b1dce3SCameron Grant 	PCM_SOFTC_SIZE,
1071fd1aaeccSCameron Grant };
1072fd1aaeccSCameron Grant 
10732287364eSJohn Baldwin DRIVER_MODULE(snd_solo, pci, ess_driver, 0, 0);
10740739ea1dSSeigo Tanimura MODULE_DEPEND(snd_solo, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1075fd1aaeccSCameron Grant MODULE_VERSION(snd_solo, 1);
1076