sb8.c revision 54823
1284345Ssjg/*
2284345Ssjg * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3284345Ssjg * Copyright 1997,1998 Luigi Rizzo.
4284345Ssjg *
5290816Ssjg * Derived from files in the Voxware 3.5 distribution,
6284345Ssjg * Copyright by Hannu Savolainen 1994, under the same copyright
7284349Ssjg * conditions.
8284349Ssjg * All rights reserved.
9284349Ssjg *
10287898Sbdrewery * Redistribution and use in source and binary forms, with or without
11287869Sbdrewery * modification, are permitted provided that the following conditions
12284345Ssjg * are met:
13287898Sbdrewery * 1. Redistributions of source code must retain the above copyright
14284650Ssjg *    notice, this list of conditions and the following disclaimer.
15287898Sbdrewery * 2. Redistributions in binary form must reproduce the above copyright
16287636Ssjg *    notice, this list of conditions and the following disclaimer in the
17287636Ssjg *    documentation and/or other materials provided with the distribution.
18287636Ssjg *
19287867Sbdrewery * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20284650Ssjg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21287867Sbdrewery * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22284650Ssjg * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23287636Ssjg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24287887Sbdrewery * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25287887Sbdrewery * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26284650Ssjg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27284345Ssjg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28284345Ssjg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29284345Ssjg * SUCH DAMAGE.
30284345Ssjg *
31284345Ssjg * $FreeBSD: head/sys/dev/sound/isa/sb8.c 54823 1999-12-19 14:21:38Z cg $
32287966Sbdrewery */
33284345Ssjg
34287871Sbdrewery#include <dev/sound/pcm/sound.h>
35284345Ssjg
36284345Ssjg#include "sbc.h"
37284345Ssjg
38284345Ssjg#define __SB_MIXER_C__	/* XXX warning... */
39284345Ssjg#include  <dev/sound/isa/sb.h>
40284345Ssjg#include  <dev/sound/chip.h>
41284345Ssjg
42284345Ssjg/* channel interface */
43284345Ssjgstatic void *sbchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir);
44284345Ssjgstatic int sbchan_setdir(void *data, int dir);
45284345Ssjgstatic int sbchan_setformat(void *data, u_int32_t format);
46287885Sbdrewerystatic int sbchan_setspeed(void *data, u_int32_t speed);
47287885Sbdrewerystatic int sbchan_setblocksize(void *data, u_int32_t blocksize);
48287885Sbdrewerystatic int sbchan_trigger(void *data, int go);
49284345Ssjgstatic int sbchan_getptr(void *data);
50284345Ssjgstatic pcmchan_caps *sbchan_getcaps(void *data);
51284345Ssjg
52284345Ssjg/* channel interface for ESS */
53284345Ssjg#ifdef notyet
54284345Ssjgstatic void *esschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir);
55284345Ssjg#endif
56284345Ssjgstatic int esschan_setdir(void *data, int dir);
57291086Sbdrewerystatic int esschan_setformat(void *data, u_int32_t format);
58284345Ssjgstatic int esschan_setspeed(void *data, u_int32_t speed);
59284345Ssjgstatic int esschan_setblocksize(void *data, u_int32_t blocksize);
60284345Ssjgstatic int esschan_trigger(void *data, int go);
61284345Ssjgstatic int esschan_getptr(void *data);
62284345Ssjgstatic pcmchan_caps *esschan_getcaps(void *data);
63284345Ssjg
64284345Ssjgstatic pcmchan_caps sb_playcaps = {
65284345Ssjg	4000, 22050,
66284345Ssjg	AFMT_U8,
67284345Ssjg	AFMT_U8
68284345Ssjg};
69284345Ssjg
70284345Ssjgstatic pcmchan_caps sb_reccaps = {
71284345Ssjg	4000, 13000,
72284345Ssjg	AFMT_U8,
73284345Ssjg	AFMT_U8
74284345Ssjg};
75284345Ssjg
76284345Ssjgstatic pcmchan_caps sbpro_playcaps = {
77284345Ssjg	4000, 45000,
78284345Ssjg	AFMT_STEREO | AFMT_U8,
79284345Ssjg	AFMT_STEREO | AFMT_U8
80284345Ssjg};
81284345Ssjg
82284345Ssjgstatic pcmchan_caps sbpro_reccaps = {
83284345Ssjg	4000, 15000,
84284345Ssjg	AFMT_STEREO | AFMT_U8,
85284345Ssjg	AFMT_STEREO | AFMT_U8
86284345Ssjg};
87284345Ssjg
88284345Ssjgstatic pcmchan_caps sb16_hcaps = {
89284345Ssjg	5000, 45000,
90284345Ssjg	AFMT_STEREO | AFMT_S16_LE,
91284345Ssjg	AFMT_STEREO | AFMT_S16_LE
92284345Ssjg};
93284345Ssjg
94284345Ssjgstatic pcmchan_caps sb16_lcaps = {
95284345Ssjg	5000, 45000,
96284345Ssjg	AFMT_STEREO | AFMT_U8,
97284345Ssjg	AFMT_STEREO | AFMT_U8
98284345Ssjg};
99284345Ssjg
100284345Ssjgstatic pcmchan_caps sb16x_caps = {
101284345Ssjg	5000, 49000,
102284345Ssjg	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
103284345Ssjg	AFMT_STEREO | AFMT_S16_LE
104284345Ssjg};
105284345Ssjg
106284345Ssjgstatic pcmchan_caps ess_playcaps = {
107284345Ssjg	5000, 49000,
108284345Ssjg	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
109287869Sbdrewery	AFMT_STEREO | AFMT_S16_LE
110287869Sbdrewery};
111287869Sbdrewery
112287869Sbdrewerystatic pcmchan_caps ess_reccaps = {
113287869Sbdrewery	5000, 49000,
114287869Sbdrewery	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
115284345Ssjg	AFMT_STEREO | AFMT_S16_LE
116284345Ssjg};
117284345Ssjg
118284345Ssjgstatic pcm_channel sb_chantemplate = {
119284345Ssjg	sbchan_init,
120284345Ssjg	sbchan_setdir,
121284345Ssjg	sbchan_setformat,
122284345Ssjg	sbchan_setspeed,
123284345Ssjg	sbchan_setblocksize,
124284345Ssjg	sbchan_trigger,
125284345Ssjg	sbchan_getptr,
126284345Ssjg	sbchan_getcaps,
127284345Ssjg};
128284345Ssjg
129284345Ssjgstatic pcm_channel ess_chantemplate = {
130284345Ssjg	sbchan_init,
131284345Ssjg	esschan_setdir,
132284345Ssjg	esschan_setformat,
133284345Ssjg	esschan_setspeed,
134284345Ssjg	esschan_setblocksize,
135284345Ssjg	esschan_trigger,
136284345Ssjg	esschan_getptr,
137284345Ssjg	esschan_getcaps,
138284345Ssjg};
139284345Ssjg#define PLAIN_SB16(x) ((((x)->bd_flags) & (BD_F_SB16|BD_F_SB16X)) == BD_F_SB16)
140284345Ssjg
141284345Ssjgstruct sb_info;
142284345Ssjg
143284345Ssjgstruct sb_chinfo {
144284345Ssjg	struct sb_info *parent;
145284345Ssjg	pcm_channel *channel;
146284345Ssjg	snd_dbuf *buffer;
147284345Ssjg	int dir;
148284345Ssjg	u_int32_t fmt;
149284345Ssjg	int ess_dma_started;
150284345Ssjg};
151284345Ssjg
152284345Ssjgstruct sb_info {
153284345Ssjg    	struct resource *io_base;	/* I/O address for the board */
154284345Ssjg    	struct resource *irq;
155284345Ssjg   	struct resource *drq1;
156284345Ssjg    	struct resource *drq2;
157284345Ssjg    	bus_dma_tag_t    parent_dmat;
158284345Ssjg
159284345Ssjg    	int bd_id;
160284345Ssjg    	u_long bd_flags;       /* board-specific flags */
161284345Ssjg    	struct sb_chinfo pch, rch;
162288156Sbdrewery};
163284345Ssjg
164284345Ssjgstatic int sb_rd(struct sb_info *sb, int reg);
165284345Ssjgstatic void sb_wr(struct sb_info *sb, int reg, u_int8_t val);
166284345Ssjgstatic int sb_dspready(struct sb_info *sb);
167284345Ssjgstatic int sb_cmd(struct sb_info *sb, u_char val);
168284345Ssjgstatic int sb_cmd1(struct sb_info *sb, u_char cmd, int val);
169284345Ssjgstatic int sb_cmd2(struct sb_info *sb, u_char cmd, int val);
170284345Ssjgstatic u_int sb_get_byte(struct sb_info *sb);
171284345Ssjgstatic void sb_setmixer(struct sb_info *sb, u_int port, u_int value);
172284345Ssjgstatic int sb_getmixer(struct sb_info *sb, u_int port);
173284345Ssjgstatic int sb_reset_dsp(struct sb_info *sb);
174284345Ssjg
175284345Ssjgstatic void sb_intr(void *arg);
176284345Ssjgstatic int sb_format(struct sb_chinfo *ch, u_int32_t format);
177284345Ssjgstatic int sb_speed(struct sb_chinfo *ch, int speed);
178284345Ssjgstatic int sb_start(struct sb_chinfo *ch);
179284345Ssjgstatic int sb_stop(struct sb_chinfo *ch);
180284345Ssjg
181284345Ssjgstatic int ess_write(struct sb_info *sb, u_char reg, int val);
182284345Ssjgstatic int ess_read(struct sb_info *sb, u_char reg);
183284345Ssjgstatic void ess_intr(void *arg);
184284345Ssjgstatic int ess_format(struct sb_chinfo *ch, u_int32_t format);
185284345Ssjgstatic int ess_speed(struct sb_chinfo *ch, int speed);
186284345Ssjgstatic int ess_start(struct sb_chinfo *ch);
187284345Ssjgstatic int ess_stop(struct sb_chinfo *ch);
188284345Ssjgstatic int ess_abort(struct sb_chinfo *ch);
189284345Ssjg
190284345Ssjgstatic int sbmix_init(snd_mixer *m);
191284345Ssjgstatic int sbmix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right);
192284345Ssjgstatic int sbmix_setrecsrc(snd_mixer *m, u_int32_t src);
193284345Ssjg
194284345Ssjgstatic snd_mixer sb_mixer = {
195284345Ssjg    "SoundBlaster mixer",
196284345Ssjg    sbmix_init,
197284345Ssjg    sbmix_set,
198284345Ssjg    sbmix_setrecsrc,
199284345Ssjg};
200284345Ssjg
201284345Ssjgstatic devclass_t pcm_devclass;
202284345Ssjg
203284345Ssjg/*
204284345Ssjg * Common code for the midi and pcm functions
205288157Sbdrewery *
206284345Ssjg * sb_cmd write a single byte to the CMD port.
207284345Ssjg * sb_cmd1 write a CMD + 1 byte arg
208284345Ssjg * sb_cmd2 write a CMD + 2 byte arg
209284345Ssjg * sb_get_byte returns a single byte from the DSP data port
210288432Sbdrewery *
211288432Sbdrewery * ess_write is actually sb_cmd1
212288432Sbdrewery * ess_read access ext. regs via sb_cmd(0xc0, reg) followed by sb_get_byte
213288432Sbdrewery */
214288432Sbdrewery
215288432Sbdrewerystatic int
216288432Sbdreweryport_rd(struct resource *port, int off)
217284345Ssjg{
218284345Ssjg	return bus_space_read_1(rman_get_bustag(port),
219288157Sbdrewery				rman_get_bushandle(port),
220284345Ssjg				off);
221290691Sbdrewery}
222290691Sbdrewery
223290691Sbdrewerystatic void
224290691Sbdreweryport_wr(struct resource *port, int off, u_int8_t data)
225290691Sbdrewery{
226290691Sbdrewery	return bus_space_write_1(rman_get_bustag(port),
227290691Sbdrewery				 rman_get_bushandle(port),
228290691Sbdrewery				 off, data);
229290691Sbdrewery}
230284345Ssjg
231284345Ssjgstatic int
232284345Ssjgsb_rd(struct sb_info *sb, int reg)
233284345Ssjg{
234284345Ssjg	return port_rd(sb->io_base, reg);
235284345Ssjg}
236284345Ssjg
237284345Ssjgstatic void
238284345Ssjgsb_wr(struct sb_info *sb, int reg, u_int8_t val)
239284345Ssjg{
240284345Ssjg	port_wr(sb->io_base, reg, val);
241}
242
243static int
244sb_dspready(struct sb_info *sb)
245{
246	return ((sb_rd(sb, SBDSP_STATUS) & 0x80) == 0);
247}
248
249static int
250sb_dspwr(struct sb_info *sb, u_char val)
251{
252    	int  i;
253
254    	for (i = 0; i < 1000; i++) {
255		if (sb_dspready(sb)) {
256	    		sb_wr(sb, SBDSP_CMD, val);
257	    		return 1;
258		}
259		if (i > 10) DELAY((i > 100)? 1000 : 10);
260    	}
261    	printf("sb_dspwr(0x%02x) timed out.\n", val);
262    	return 0;
263}
264
265static int
266sb_cmd(struct sb_info *sb, u_char val)
267{
268#if 0
269	printf("sb_cmd: %x\n", val);
270#endif
271    	return sb_dspwr(sb, val);
272}
273
274static int
275sb_cmd1(struct sb_info *sb, u_char cmd, int val)
276{
277#if 0
278    	printf("sb_cmd1: %x, %x\n", cmd, val);
279#endif
280    	if (sb_dspwr(sb, cmd)) {
281		return sb_dspwr(sb, val & 0xff);
282    	} else return 0;
283}
284
285static int
286sb_cmd2(struct sb_info *sb, u_char cmd, int val)
287{
288#if 0
289    	printf("sb_cmd2: %x, %x\n", cmd, val);
290#endif
291    	if (sb_dspwr(sb, cmd)) {
292		return sb_dspwr(sb, val & 0xff) &&
293		       sb_dspwr(sb, (val >> 8) & 0xff);
294    	} else return 0;
295}
296
297/*
298 * in the SB, there is a set of indirect "mixer" registers with
299 * address at offset 4, data at offset 5
300 */
301static void
302sb_setmixer(struct sb_info *sb, u_int port, u_int value)
303{
304    	u_long   flags;
305
306    	flags = spltty();
307    	sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
308    	DELAY(10);
309    	sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff));
310    	DELAY(10);
311    	splx(flags);
312}
313
314static int
315sb_getmixer(struct sb_info *sb, u_int port)
316{
317    	int val;
318    	u_long flags;
319
320    	flags = spltty();
321    	sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
322    	DELAY(10);
323    	val = sb_rd(sb, SB_MIX_DATA);
324    	DELAY(10);
325    	splx(flags);
326
327    	return val;
328}
329
330static u_int
331sb_get_byte(struct sb_info *sb)
332{
333    	int i;
334
335    	for (i = 1000; i > 0; i--) {
336		if (sb_rd(sb, DSP_DATA_AVAIL) & 0x80)
337			return sb_rd(sb, DSP_READ);
338		else
339			DELAY(20);
340    	}
341    	return 0xffff;
342}
343
344static int
345ess_write(struct sb_info *sb, u_char reg, int val)
346{
347    	return sb_cmd1(sb, reg, val);
348}
349
350static int
351ess_read(struct sb_info *sb, u_char reg)
352{
353    	return (sb_cmd(sb, 0xc0) && sb_cmd(sb, reg))? sb_get_byte(sb) : 0xffff;
354}
355
356static int
357sb_reset_dsp(struct sb_info *sb)
358{
359    	sb_wr(sb, SBDSP_RST, 3);
360    	DELAY(100);
361    	sb_wr(sb, SBDSP_RST, 0);
362    	if (sb_get_byte(sb) != 0xAA) {
363        	DEB(printf("sb_reset_dsp 0x%lx failed\n",
364			   rman_get_start(d->io_base)));
365		return ENXIO;	/* Sorry */
366    	}
367    	if (sb->bd_flags & BD_F_ESS) sb_cmd(sb, 0xc6);
368    	return 0;
369}
370
371static void
372sb_release_resources(struct sb_info *sb, device_t dev)
373{
374    	/* should we bus_teardown_intr here? */
375    	if (sb->irq) {
376		bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq);
377		sb->irq = 0;
378    	}
379    	if (sb->drq1) {
380		bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq1);
381		sb->drq1 = 0;
382    	}
383    	if (sb->drq2) {
384		bus_release_resource(dev, SYS_RES_DRQ, 1, sb->drq2);
385		sb->drq2 = 0;
386    	}
387    	if (sb->io_base) {
388		bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base);
389		sb->io_base = 0;
390    	}
391    	free(sb, M_DEVBUF);
392}
393
394static int
395sb_alloc_resources(struct sb_info *sb, device_t dev)
396{
397	int rid;
398
399	rid = 0;
400	if (!sb->io_base)
401    		sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT,
402						 &rid, 0, ~0, 1,
403						 RF_ACTIVE);
404	rid = 0;
405	if (!sb->irq)
406    		sb->irq = bus_alloc_resource(dev, SYS_RES_IRQ,
407					     &rid, 0, ~0, 1,
408					     RF_ACTIVE);
409	rid = 0;
410	if (!sb->drq1)
411    		sb->drq1 = bus_alloc_resource(dev, SYS_RES_DRQ,
412					      &rid, 0, ~0, 1,
413					      RF_ACTIVE);
414	rid = 1;
415	if (!sb->drq2)
416        	sb->drq2 = bus_alloc_resource(dev, SYS_RES_DRQ,
417					      &rid, 0, ~0, 1,
418					      RF_ACTIVE);
419
420    	if (sb->io_base && sb->drq1 && sb->irq) {
421		isa_dma_acquire(rman_get_start(sb->drq1));
422		isa_dmainit(rman_get_start(sb->drq1), DSP_BUFFSIZE);
423
424		if (sb->drq2) {
425			isa_dma_acquire(rman_get_start(sb->drq2));
426			isa_dmainit(rman_get_start(sb->drq2), DSP_BUFFSIZE);
427		}
428
429		return 0;
430	} else return ENXIO;
431}
432
433static void
434sb16_swap(void *v, int dir)
435{
436	struct sb_info *sb = v;
437	int pb = sb->pch.buffer->dl;
438	int rb = sb->rch.buffer->dl;
439	int pc = sb->pch.buffer->chan;
440	int rc = sb->rch.buffer->chan;
441	int swp = 0;
442
443	if (!pb && !rb) {
444		if (dir == PCMDIR_PLAY && pc < 4) swp = 1;
445		else if (dir == PCMDIR_REC && rc < 4) swp = 1;
446		if (sb->bd_flags & BD_F_SB16X) swp = !swp;
447		if (swp) {
448			int t;
449
450			t = sb->pch.buffer->chan;
451			sb->pch.buffer->chan = sb->rch.buffer->chan;
452			sb->rch.buffer->chan = t;
453			sb->pch.buffer->dir = B_WRITE;
454			sb->rch.buffer->dir = B_READ;
455		}
456	}
457}
458
459static int
460sb_doattach(device_t dev, struct sb_info *sb)
461{
462    	snddev_info *d = device_get_softc(dev);
463    	void *ih;
464    	char status[SND_STATUSLEN];
465
466    	if (sb_alloc_resources(sb, dev)) goto no;
467    	if (sb_reset_dsp(sb)) goto no;
468    	mixer_init(d, &sb_mixer, sb);
469
470	if (sb->bd_flags & BD_F_ESS)
471		bus_setup_intr(dev, sb->irq, INTR_TYPE_TTY, ess_intr, sb, &ih);
472	else
473		bus_setup_intr(dev, sb->irq, INTR_TYPE_TTY, sb_intr, sb, &ih);
474    	if ((sb->bd_flags & BD_F_SB16) && !(sb->bd_flags & BD_F_SB16X))
475		pcm_setswap(dev, sb16_swap);
476    	if (!sb->drq2)
477		pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
478
479    	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
480			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
481			/*highaddr*/BUS_SPACE_MAXADDR,
482			/*filter*/NULL, /*filterarg*/NULL,
483			/*maxsize*/DSP_BUFFSIZE, /*nsegments*/1,
484			/*maxsegz*/0x3ffff,
485			/*flags*/0, &sb->parent_dmat) != 0) {
486		device_printf(dev, "unable to create dma tag\n");
487		goto no;
488    	}
489
490    	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld",
491    	     	rman_get_start(sb->io_base), rman_get_start(sb->irq),
492		rman_get_start(sb->drq1));
493    	if (sb->drq2) snprintf(status + strlen(status),	SND_STATUSLEN - strlen(status),
494		":%ld", rman_get_start(sb->drq2));
495
496    	if (pcm_register(dev, sb, 1, 1)) goto no;
497	if (sb->bd_flags & BD_F_ESS) {
498		pcm_addchan(dev, PCMDIR_REC, &ess_chantemplate, sb);
499		pcm_addchan(dev, PCMDIR_PLAY, &ess_chantemplate, sb);
500	} else {
501		pcm_addchan(dev, PCMDIR_REC, &sb_chantemplate, sb);
502		pcm_addchan(dev, PCMDIR_PLAY, &sb_chantemplate, sb);
503	}
504    	pcm_setstatus(dev, status);
505
506    	return 0;
507
508no:
509    	sb_release_resources(sb, dev);
510    	return ENXIO;
511}
512
513static void
514sb_intr(void *arg)
515{
516    	struct sb_info *sb = (struct sb_info *)arg;
517    	int reason = 3, c;
518
519    	/*
520     	* SB < 4.0 is half duplex and has only 1 bit for int source,
521     	* so we fake it. SB 4.x (SB16) has the int source in a separate
522     	* register.
523     	* The Vibra16X has separate flags for 8 and 16 bit transfers, but
524     	* I have no idea how to tell capture from playback interrupts...
525     	*/
526    	if (sb->bd_flags & BD_F_SB16) {
527    		c = sb_getmixer(sb, IRQ_STAT);
528    		/* this tells us if the source is 8-bit or 16-bit dma. We
529     		* have to check the io channel to map it to read or write...
530     		*/
531    		reason = 0;
532    		if (c & 1) { /* 8-bit dma */
533			if (sb->pch.fmt & AFMT_U8) reason |= 1;
534			if (sb->rch.fmt & AFMT_U8) reason |= 2;
535    		}
536    		if (c & 2) { /* 16-bit dma */
537			if (sb->pch.fmt & AFMT_S16_LE) reason |= 1;
538			if (sb->rch.fmt & AFMT_S16_LE) reason |= 2;
539    		}
540    	} else c = 1;
541#if 0
542    	printf("sb_intr: reason=%d c=0x%x\n", reason, c);
543#endif
544    	if ((reason & 1) && (sb->pch.buffer->dl > 0))
545		chn_intr(sb->pch.channel);
546    	if ((reason & 2) && (sb->rch.buffer->dl > 0))
547		chn_intr(sb->rch.channel);
548    	if (c & 1) sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */
549    	if (c & 2) sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */
550}
551
552static void
553ess_intr(void *arg)
554{
555    struct sb_info *sb = (struct sb_info *)arg;
556    sb_rd(sb, DSP_DATA_AVAIL); /* int ack */
557#ifdef notyet
558    /*
559     * XXX
560     * for full-duplex mode:
561     * should read port 0x6 to identify where interrupt came from.
562     */
563#endif
564    /*
565     * We are transferring data in DSP normal mode,
566     * so clear the dl to indicate the DMA is stopped.
567     */
568    if (sb->pch.buffer->dl > 0) {
569	sb->pch.buffer->dl = -1;
570	chn_intr(sb->pch.channel);
571    }
572    if (sb->rch.buffer->dl > 0) {
573	sb->rch.buffer->dl = -1;
574	chn_intr(sb->rch.channel);
575    }
576}
577
578static int
579sb_format(struct sb_chinfo *ch, u_int32_t format)
580{
581	ch->fmt = format;
582	return 0;
583}
584
585static int
586sb_speed(struct sb_chinfo *ch, int speed)
587{
588    	struct sb_info *sb = ch->parent;
589    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
590    	int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0;
591
592    	if (sb->bd_flags & BD_F_SB16) {
593		RANGE(speed, 5000, 45000);
594		sb_cmd(sb, 0x42 - play);
595    		sb_cmd(sb, speed >> 8);
596		sb_cmd(sb, speed & 0xff);
597    	} else {
598		u_char tconst;
599		int max_speed = 45000, tmp;
600        	u_long flags;
601
602    		/* here enforce speed limitations - max 22050 on sb 1.x*/
603    		if (sb->bd_id <= 0x200) max_speed = 22050;
604
605    		/*
606     	 	* SB models earlier than SB Pro have low limit for the
607     	 	* input rate. Note that this is only for input, but since
608     	 	* we do not support separate values for rec & play....
609     	 	*/
610		if (!play) {
611    			if (sb->bd_id <= 0x200) max_speed = 13000;
612    			else if (sb->bd_id < 0x300) max_speed = 15000;
613		}
614    		RANGE(speed, 4000, max_speed);
615    		if (stereo) speed <<= 1;
616
617    		/*
618     	 	* Now the speed should be valid. Compute the value to be
619     	 	* programmed into the board.
620     	 	*/
621    		if (speed > 22050) { /* High speed mode on 2.01/3.xx */
622			tconst = (u_char)
623				((65536 - ((256000000 + speed / 2) / speed))
624				>> 8);
625			sb->bd_flags |= BD_F_HISPEED;
626			tmp = 65536 - (tconst << 8);
627			speed = (256000000 + tmp / 2) / tmp;
628    		} else {
629			sb->bd_flags &= ~BD_F_HISPEED;
630			tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
631			tmp = 256 - tconst;
632			speed = (1000000 + tmp / 2) / tmp;
633    		}
634		flags = spltty();
635		sb_cmd1(sb, 0x40, tconst); /* set time constant */
636		splx(flags);
637    		if (stereo) speed >>= 1;
638    	}
639    	return speed;
640}
641
642static int
643sb_start(struct sb_chinfo *ch)
644{
645	struct sb_info *sb = ch->parent;
646    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
647    	int b16 = (ch->fmt & AFMT_S16_LE)? 1 : 0;
648    	int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0;
649	int l = ch->buffer->dl;
650	int dh = ch->buffer->chan > 3;
651	u_char i1, i2;
652
653	if (b16 || dh) l >>= 1;
654	l--;
655	if (play) sb_cmd(sb, DSP_CMD_SPKON);
656	if (sb->bd_flags & BD_F_SB16) {
657	    i1 = DSP_F16_AUTO | DSP_F16_FIFO_ON |
658	         (play? DSP_F16_DAC : DSP_F16_ADC);
659	    i1 |= (b16 || dh)? DSP_DMA16 : DSP_DMA8;
660	    i2 = (stereo? DSP_F16_STEREO : 0) | (b16? DSP_F16_SIGNED : 0);
661	    sb_cmd(sb, i1);
662	    sb_cmd2(sb, i2, l);
663	} else {
664	    if (sb->bd_flags & BD_F_HISPEED) i1 = play? 0x90 : 0x98;
665	    else i1 = play? 0x1c : 0x2c;
666	    sb_setmixer(sb, 0x0e, stereo? 2 : 0);
667	    /* an ESS extension -- they can do 16 bits */
668	    if (b16) i1 |= 1;
669	    sb_cmd2(sb, 0x48, l);
670	    sb_cmd(sb, i1);
671	}
672	sb->bd_flags |= BD_F_DMARUN << b16;
673	return 0;
674}
675
676static int
677sb_stop(struct sb_chinfo *ch)
678{
679	struct sb_info *sb = ch->parent;
680    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
681    	int b16 = (ch->fmt & AFMT_S16_LE)? 1 : 0;
682
683    	if (sb->bd_flags & BD_F_HISPEED) sb_reset_dsp(sb);
684	else {
685		sb_cmd(sb, b16? DSP_CMD_DMAPAUSE_16 : DSP_CMD_DMAPAUSE_8);
686	       /*
687		* The above seems to have the undocumented side effect of
688		* blocking the other side as well. If the other
689		* channel was active (SB16) I have to re-enable it :(
690		*/
691		if (sb->bd_flags & (BD_F_DMARUN << (1 - b16)))
692			sb_cmd(sb, b16? 0xd4 : 0xd6 );
693	}
694	if (play) sb_cmd(sb, DSP_CMD_SPKOFF); /* speaker off */
695	sb->bd_flags &= ~(BD_F_DMARUN << b16);
696	return 0;
697}
698
699/* utility functions for ESS */
700static int
701ess_format(struct sb_chinfo *ch, u_int32_t format)
702{
703	struct sb_info *sb = ch->parent;
704	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
705	int b16 = (format & AFMT_S16_LE)? 1 : 0;
706	int stereo = (format & AFMT_STEREO)? 1 : 0;
707	u_char c;
708	ch->fmt = format;
709	sb_reset_dsp(sb);
710	/* normal DMA mode */
711	ess_write(sb, 0xb8, play ? 0x00 : 0x0a);
712	/* mono/stereo */
713	c = (ess_read(sb, 0xa8) & ~0x03) | 1;
714	if (!stereo) c++;
715	ess_write(sb, 0xa8, c);
716	/* demand mode, 4 bytes/xfer */
717	ess_write(sb, 0xb9, 2);
718	/* setup dac/adc */
719	if (play) ess_write(sb, 0xb6, b16? 0x00 : 0x80);
720	ess_write(sb, 0xb7, 0x51 | (b16? 0x20 : 0x00));
721	ess_write(sb, 0xb7, 0x98 + (b16? 0x24 : 0x00) + (stereo? 0x00 : 0x38));
722	/* irq/drq control */
723	ess_write(sb, 0xb1, (ess_read(sb, 0xb1) & 0x0f) | 0x50);
724	ess_write(sb, 0xb2, (ess_read(sb, 0xb2) & 0x0f) | 0x50);
725	return 0;
726}
727
728static int
729ess_speed(struct sb_chinfo *ch, int speed)
730{
731	struct sb_info *sb = ch->parent;
732	int t;
733	RANGE (speed, 5000, 49000);
734	if (speed > 22000) {
735		t = (795500 + speed / 2) / speed;
736		speed = (795500 + t / 2) / t;
737		t = (256 - t ) | 0x80;
738	} else {
739		t = (397700 + speed / 2) / speed;
740		speed = (397700 + t / 2) / t;
741		t = 128 - t;
742	}
743	ess_write(sb, 0xa1, t); /* set time constant */
744#if 0
745	d->play_speed = d->rec_speed = speed;
746	speed = (speed * 9 ) / 20;
747#endif
748	t = 256 - 7160000 / ((speed * 9 / 20) * 82);
749	ess_write(sb, 0xa2, t);
750	return speed;
751}
752
753static int
754ess_start(struct sb_chinfo *ch)
755{
756	struct sb_info *sb = ch->parent;
757    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
758	short c = - ch->buffer->dl;
759	u_char c1;
760	/*
761	 * clear bit 0 of register B8h
762	 */
763#if 1
764	c1 = play ? 0x00 : 0x0a;
765	ess_write(sb, 0xb8, c1++);
766#else
767	c1 = ess_read(sb, 0xb8) & 0xfe;
768	ess_write(sb, 0xb8, c1++);
769#endif
770	/*
771	 * update ESS Transfer Count Register
772	 */
773	ess_write(sb, 0xa4, (u_char)((u_short)c & 0xff));
774	ess_write(sb, 0xa5, (u_char)(((u_short)c >> 8) & 0xff));
775	/*
776	 * set bit 0 of register B8h
777	 */
778	ess_write(sb, 0xb8, c1);
779	if (play)
780		sb_cmd(sb, DSP_CMD_SPKON);
781	return 0;
782}
783
784static int
785ess_stop(struct sb_chinfo *ch)
786{
787	struct sb_info *sb = ch->parent;
788	/*
789	 * no need to send a stop command if the DMA has already stopped.
790	 */
791	if (ch->buffer->dl > 0) {
792		sb_cmd(sb, DSP_CMD_DMAPAUSE_8); /* pause dma. */
793	}
794	return 0;
795}
796
797static int
798ess_abort(struct sb_chinfo *ch)
799{
800	struct sb_info *sb = ch->parent;
801    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
802	if (play) sb_cmd(sb, DSP_CMD_SPKOFF); /* speaker off */
803	sb_reset_dsp(sb);
804	ess_format(ch, ch->fmt);
805	ess_speed(ch, ch->channel->speed);
806	return 0;
807}
808
809/* channel interface */
810static void *
811sbchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
812{
813	struct sb_info *sb = devinfo;
814	struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
815	int dch, dl, dh;
816
817	ch->parent = sb;
818	ch->channel = c;
819	ch->buffer = b;
820	ch->buffer->bufsize = DSP_BUFFSIZE;
821	if (chn_allocbuf(ch->buffer, sb->parent_dmat) == -1) return NULL;
822	dch = (dir == PCMDIR_PLAY)? 1 : 0;
823	if (sb->bd_flags & BD_F_SB16X) dch = !dch;
824	dl = rman_get_start(sb->drq1);
825	if (sb->drq2) dh = rman_get_start(sb->drq2); else dh = dl;
826	ch->buffer->chan = dch? dh : dl;
827	return ch;
828}
829
830static int
831sbchan_setdir(void *data, int dir)
832{
833	struct sb_chinfo *ch = data;
834	ch->dir = dir;
835	return 0;
836}
837
838static int
839sbchan_setformat(void *data, u_int32_t format)
840{
841	struct sb_chinfo *ch = data;
842	sb_format(ch, format);
843	return 0;
844}
845
846static int
847sbchan_setspeed(void *data, u_int32_t speed)
848{
849	struct sb_chinfo *ch = data;
850	return sb_speed(ch, speed);
851}
852
853static int
854sbchan_setblocksize(void *data, u_int32_t blocksize)
855{
856	return blocksize;
857}
858
859static int
860sbchan_trigger(void *data, int go)
861{
862	struct sb_chinfo *ch = data;
863	buf_isadma(ch->buffer, go);
864	if (go == PCMTRIG_START) sb_start(ch); else sb_stop(ch);
865	return 0;
866}
867
868static int
869sbchan_getptr(void *data)
870{
871	struct sb_chinfo *ch = data;
872	return buf_isadmaptr(ch->buffer);
873}
874
875static pcmchan_caps *
876sbchan_getcaps(void *data)
877{
878	struct sb_chinfo *ch = data;
879	int p = (ch->dir == PCMDIR_PLAY)? 1 : 0;
880	if (ch->parent->bd_id < 0x300)
881		return p? &sb_playcaps : &sb_reccaps;
882	else if (ch->parent->bd_id < 0x400)
883		return p? &sbpro_playcaps : &sbpro_reccaps;
884	else if (ch->parent->bd_flags & BD_F_SB16X)
885		return &sb16x_caps;
886	else
887		return (ch->buffer->chan >= 4)? &sb16_hcaps : &sb16_lcaps;
888}
889/* channel interface for ESS18xx */
890#ifdef notyet
891static void *
892esschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
893{
894	/* the same as sbchan_init()? */
895}
896#endif
897
898static int
899esschan_setdir(void *data, int dir)
900{
901	struct sb_chinfo *ch = data;
902	ch->dir = dir;
903	return 0;
904}
905
906static int
907esschan_setformat(void *data, u_int32_t format)
908{
909	struct sb_chinfo *ch = data;
910	ess_format(ch, format);
911	return 0;
912}
913
914static int
915esschan_setspeed(void *data, u_int32_t speed)
916{
917	struct sb_chinfo *ch = data;
918	return ess_speed(ch, speed);
919}
920
921static int
922esschan_setblocksize(void *data, u_int32_t blocksize)
923{
924	return blocksize;
925}
926
927static int
928esschan_trigger(void *data, int go)
929{
930	struct sb_chinfo *ch = data;
931	switch (go) {
932	case PCMTRIG_START:
933		if (!ch->ess_dma_started)
934			buf_isadma(ch->buffer, go);
935		ch->ess_dma_started = 1;
936		ess_start(ch);
937		break;
938	case PCMTRIG_STOP:
939		if (ch->buffer->dl >= 0) {
940			buf_isadma(ch->buffer, go);
941			ch->ess_dma_started = 0;
942			ess_stop(ch);
943		}
944		break;
945	case PCMTRIG_ABORT:
946	default:
947		ch->ess_dma_started = 0;
948		ess_abort(ch);
949		buf_isadma(ch->buffer, go);
950		break;
951	}
952	return 0;
953}
954
955static int
956esschan_getptr(void *data)
957{
958	struct sb_chinfo *ch = data;
959	return buf_isadmaptr(ch->buffer);
960}
961
962static pcmchan_caps *
963esschan_getcaps(void *data)
964{
965	struct sb_chinfo *ch = data;
966	return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
967}
968
969/************************************************************/
970
971static int
972sbmix_init(snd_mixer *m)
973{
974    	struct sb_info *sb = mix_getdevinfo(m);
975
976    	switch (sb->bd_flags & BD_F_MIX_MASK) {
977    	case BD_F_MIX_CT1345: /* SB 3.0 has 1345 mixer */
978		mix_setdevs(m, SBPRO_MIXER_DEVICES);
979		mix_setrecdevs(m, SBPRO_RECORDING_DEVICES);
980		sb_setmixer(sb, 0, 1); /* reset mixer */
981		sb_setmixer(sb, MIC_VOL, 0x6); /* mic volume max */
982		sb_setmixer(sb, RECORD_SRC, 0x0); /* mic source */
983		sb_setmixer(sb, FM_VOL, 0x0); /* no midi */
984		break;
985
986    	case BD_F_MIX_CT1745: /* SB16 mixer ... */
987		mix_setdevs(m, SB16_MIXER_DEVICES);
988		mix_setrecdevs(m, SB16_RECORDING_DEVICES);
989		sb_setmixer(sb, 0x3c, 0x1f); /* make all output active */
990		sb_setmixer(sb, 0x3d, 0); /* make all inputs-l off */
991		sb_setmixer(sb, 0x3e, 0); /* make all inputs-r off */
992    	}
993    	return 0;
994}
995
996static int
997sbmix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right)
998{
999    	struct sb_info *sb = mix_getdevinfo(m);
1000    	int regoffs;
1001    	u_char   val;
1002    	mixer_tab *iomap;
1003
1004    	switch (sb->bd_flags & BD_F_MIX_MASK) {
1005    	case BD_F_MIX_CT1345:
1006		if (sb->bd_flags & BD_F_ESS)
1007			iomap = &ess_mix;
1008		else
1009			iomap = &sbpro_mix;
1010		break;
1011
1012    	case BD_F_MIX_CT1745:
1013		iomap = &sb16_mix;
1014		break;
1015
1016    	default:
1017        	return -1;
1018    	/* XXX how about the SG NX Pro, iomap = sgnxpro_mix */
1019    	}
1020
1021	/* Change left channel */
1022    	regoffs = (*iomap)[dev][LEFT_CHN].regno;
1023    	if (regoffs != 0) {
1024		val = sb_getmixer(sb, regoffs);
1025		change_bits(iomap, &val, dev, LEFT_CHN, left);
1026		sb_setmixer(sb, regoffs, val);
1027	}
1028
1029	/* Change right channel */
1030	regoffs = (*iomap)[dev][RIGHT_CHN].regno;
1031	if (regoffs != 0) {
1032		val = sb_getmixer(sb, regoffs); /* Read the new one */
1033		change_bits(iomap, &val, dev, RIGHT_CHN, right);
1034		sb_setmixer(sb, regoffs, val);
1035	} else
1036		right = left;
1037
1038    	return left | (right << 8);
1039}
1040
1041static int
1042sbmix_setrecsrc(snd_mixer *m, u_int32_t src)
1043{
1044    	struct sb_info *sb = mix_getdevinfo(m);
1045    	u_char recdev;
1046
1047    	switch (sb->bd_flags & BD_F_MIX_MASK) {
1048    	case BD_F_MIX_CT1345:
1049		if      (src == SOUND_MASK_LINE) 	recdev = 0x06;
1050		else if (src == SOUND_MASK_CD) 		recdev = 0x02;
1051		else { /* default: mic */
1052	    		src = SOUND_MASK_MIC;
1053	    		recdev = 0;
1054		}
1055		sb_setmixer(sb, RECORD_SRC, recdev |
1056			    (sb_getmixer(sb, RECORD_SRC) & ~0x07));
1057		break;
1058
1059    	case BD_F_MIX_CT1745: /* sb16 */
1060		recdev = 0;
1061		if (src & SOUND_MASK_MIC)   recdev |= 0x01; /* mono mic */
1062		if (src & SOUND_MASK_CD)    recdev |= 0x06; /* l+r cd */
1063		if (src & SOUND_MASK_LINE)  recdev |= 0x18; /* l+r line */
1064		if (src & SOUND_MASK_SYNTH) recdev |= 0x60; /* l+r midi */
1065		sb_setmixer(sb, SB16_IMASK_L, recdev);
1066		sb_setmixer(sb, SB16_IMASK_R, recdev);
1067		/*
1068	 	* since the same volume controls apply to the input and
1069	 	* output sections, the best approach to have a consistent
1070	 	* behaviour among cards would be to disable the output path
1071	 	* on devices which are used to record.
1072	 	* However, since users like to have feedback, we only disable
1073	 	* the mic -- permanently.
1074	 	*/
1075        	sb_setmixer(sb, SB16_OMASK, 0x1f & ~1);
1076		break;
1077       	}
1078    	return src;
1079}
1080
1081static int
1082sbsbc_probe(device_t dev)
1083{
1084    	char buf[64];
1085	u_int32_t func, ver, r, f;
1086
1087	/* The parent device has already been probed. */
1088	r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
1089	if (func != SCF_PCM)
1090		return (ENXIO);
1091
1092	r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
1093	f = (ver & 0xffff0000) >> 16;
1094	ver &= 0x0000ffff;
1095	snprintf(buf, sizeof buf, "SB DSP %d.%02d%s%s", ver >> 8, ver & 0xff,
1096		(f & BD_F_ESS)? " (ESS mode)" : "",
1097		(f & BD_F_SB16X)? " (ViBRA16X)" : "");
1098    	device_set_desc_copy(dev, buf);
1099
1100	return 0;
1101}
1102
1103static int
1104sbsbc_attach(device_t dev)
1105{
1106    	struct sb_info *sb;
1107	u_int32_t ver;
1108
1109    	sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT);
1110    	if (!sb) return ENXIO;
1111    	bzero(sb, sizeof *sb);
1112
1113	BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
1114	sb->bd_id = ver & 0x0000ffff;
1115	sb->bd_flags = (ver & 0xffff0000) >> 16;
1116
1117    	return sb_doattach(dev, sb);
1118}
1119
1120static device_method_t sbsbc_methods[] = {
1121	/* Device interface */
1122	DEVMETHOD(device_probe,		sbsbc_probe),
1123	DEVMETHOD(device_attach,	sbsbc_attach),
1124
1125	{ 0, 0 }
1126};
1127
1128static driver_t sbsbc_driver = {
1129	"pcm",
1130	sbsbc_methods,
1131	sizeof(snddev_info),
1132};
1133
1134DRIVER_MODULE(sbsbc, sbc, sbsbc_driver, pcm_devclass, 0, 0);
1135
1136
1137
1138