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