sb16.c revision 55424
1/*
2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3 * Copyright 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 * $FreeBSD: head/sys/dev/sound/isa/sb16.c 55424 2000-01-05 00:39:20Z cg $
32 */
33
34#include <dev/sound/pcm/sound.h>
35
36#include "sbc.h"
37
38#define __SB_MIXER_C__	/* XXX warning... */
39#include  <dev/sound/isa/sb.h>
40#include  <dev/sound/chip.h>
41
42#define ESS_BUFFSIZE (65536 - 256)
43
44/* channel interface */
45static void *sbchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir);
46static int sbchan_setdir(void *data, int dir);
47static int sbchan_setformat(void *data, u_int32_t format);
48static int sbchan_setspeed(void *data, u_int32_t speed);
49static int sbchan_setblocksize(void *data, u_int32_t blocksize);
50static int sbchan_trigger(void *data, int go);
51static int sbchan_getptr(void *data);
52static pcmchan_caps *sbchan_getcaps(void *data);
53
54/* channel interface for ESS */
55static void *esschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir);
56static int esschan_setdir(void *data, int dir);
57static int esschan_setformat(void *data, u_int32_t format);
58static int esschan_setspeed(void *data, u_int32_t speed);
59static int esschan_setblocksize(void *data, u_int32_t blocksize);
60static int esschan_trigger(void *data, int go);
61static int esschan_getptr(void *data);
62static pcmchan_caps *esschan_getcaps(void *data);
63
64static pcmchan_caps sb_playcaps = {
65	4000, 22050,
66	AFMT_U8,
67	AFMT_U8
68};
69
70static pcmchan_caps sb_reccaps = {
71	4000, 13000,
72	AFMT_U8,
73	AFMT_U8
74};
75
76static pcmchan_caps sbpro_playcaps = {
77	4000, 45000,
78	AFMT_STEREO | AFMT_U8,
79	AFMT_STEREO | AFMT_U8
80};
81
82static pcmchan_caps sbpro_reccaps = {
83	4000, 15000,
84	AFMT_STEREO | AFMT_U8,
85	AFMT_STEREO | AFMT_U8
86};
87
88static pcmchan_caps sb16_hcaps = {
89	5000, 45000,
90	AFMT_STEREO | AFMT_S16_LE,
91	AFMT_STEREO | AFMT_S16_LE
92};
93
94static pcmchan_caps sb16_lcaps = {
95	5000, 45000,
96	AFMT_STEREO | AFMT_U8,
97	AFMT_STEREO | AFMT_U8
98};
99
100static pcmchan_caps sb16x_caps = {
101	5000, 49000,
102	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
103	AFMT_STEREO | AFMT_S16_LE
104};
105
106static pcmchan_caps ess_playcaps = {
107	5000, 49000,
108	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
109	AFMT_STEREO | AFMT_S16_LE
110};
111
112static pcmchan_caps ess_reccaps = {
113	5000, 49000,
114	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
115	AFMT_STEREO | AFMT_S16_LE
116};
117
118static pcm_channel sb_chantemplate = {
119	sbchan_init,
120	sbchan_setdir,
121	sbchan_setformat,
122	sbchan_setspeed,
123	sbchan_setblocksize,
124	sbchan_trigger,
125	sbchan_getptr,
126	sbchan_getcaps,
127};
128
129static pcm_channel ess_chantemplate = {
130	esschan_init,
131	esschan_setdir,
132	esschan_setformat,
133	esschan_setspeed,
134	esschan_setblocksize,
135	esschan_trigger,
136	esschan_getptr,
137	esschan_getcaps,
138};
139#define PLAIN_SB16(x) ((((x)->bd_flags) & (BD_F_SB16|BD_F_SB16X)) == BD_F_SB16)
140
141struct sb_info;
142
143struct sb_chinfo {
144	struct sb_info *parent;
145	pcm_channel *channel;
146	snd_dbuf *buffer;
147	int dir;
148	u_int32_t fmt;
149	int ess_dma_started;
150};
151
152struct sb_info {
153    	struct resource *io_base;	/* I/O address for the board */
154    	struct resource *irq;
155   	struct resource *drq1;
156    	struct resource *drq2;
157    	bus_dma_tag_t    parent_dmat;
158
159    	int bd_id;
160    	u_long bd_flags;       /* board-specific flags */
161    	struct sb_chinfo pch, rch;
162};
163
164static int sb_rd(struct sb_info *sb, int reg);
165static void sb_wr(struct sb_info *sb, int reg, u_int8_t val);
166static int sb_dspready(struct sb_info *sb);
167static int sb_cmd(struct sb_info *sb, u_char val);
168static int sb_cmd1(struct sb_info *sb, u_char cmd, int val);
169static int sb_cmd2(struct sb_info *sb, u_char cmd, int val);
170static u_int sb_get_byte(struct sb_info *sb);
171static void sb_setmixer(struct sb_info *sb, u_int port, u_int value);
172static int sb_getmixer(struct sb_info *sb, u_int port);
173static int sb_reset_dsp(struct sb_info *sb);
174
175static void sb_intr(void *arg);
176static int sb_format(struct sb_chinfo *ch, u_int32_t format);
177static int sb_speed(struct sb_chinfo *ch, int speed);
178static int sb_start(struct sb_chinfo *ch);
179static int sb_stop(struct sb_chinfo *ch);
180
181static int ess_write(struct sb_info *sb, u_char reg, int val);
182static int ess_read(struct sb_info *sb, u_char reg);
183static void ess_intr(void *arg);
184static int ess_format(struct sb_chinfo *ch, u_int32_t format);
185static int ess_speed(struct sb_chinfo *ch, int speed);
186static int ess_start(struct sb_chinfo *ch);
187static int ess_stop(struct sb_chinfo *ch);
188static int ess_abort(struct sb_chinfo *ch);
189
190static int sbmix_init(snd_mixer *m);
191static int sbmix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right);
192static int sbmix_setrecsrc(snd_mixer *m, u_int32_t src);
193
194static snd_mixer sb_mixer = {
195    "SoundBlaster mixer",
196    sbmix_init,
197    sbmix_set,
198    sbmix_setrecsrc,
199};
200
201static devclass_t pcm_devclass;
202
203/*
204 * Common code for the midi and pcm functions
205 *
206 * sb_cmd write a single byte to the CMD port.
207 * sb_cmd1 write a CMD + 1 byte arg
208 * sb_cmd2 write a CMD + 2 byte arg
209 * sb_get_byte returns a single byte from the DSP data port
210 *
211 * ess_write is actually sb_cmd1
212 * ess_read access ext. regs via sb_cmd(0xc0, reg) followed by sb_get_byte
213 */
214
215static int
216port_rd(struct resource *port, int off)
217{
218	return bus_space_read_1(rman_get_bustag(port),
219				rman_get_bushandle(port),
220				off);
221}
222
223static void
224port_wr(struct resource *port, int off, u_int8_t data)
225{
226	return bus_space_write_1(rman_get_bustag(port),
227				 rman_get_bushandle(port),
228				 off, data);
229}
230
231static int
232sb_rd(struct sb_info *sb, int reg)
233{
234	return port_rd(sb->io_base, reg);
235}
236
237static void
238sb_wr(struct sb_info *sb, int reg, u_int8_t val)
239{
240	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 && !(sb->bd_flags & BD_F_ESS))
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		int bs = (sb->bd_flags & BD_F_ESS)? ESS_BUFFSIZE : DSP_BUFFSIZE;
422
423		isa_dma_acquire(rman_get_start(sb->drq1));
424		isa_dmainit(rman_get_start(sb->drq1), bs);
425
426		if (sb->drq2) {
427			isa_dma_acquire(rman_get_start(sb->drq2));
428			isa_dmainit(rman_get_start(sb->drq2), bs);
429		}
430
431		return 0;
432	} else return ENXIO;
433}
434
435static void
436sb16_swap(void *v, int dir)
437{
438	struct sb_info *sb = v;
439	int pb = sb->pch.buffer->dl;
440	int rb = sb->rch.buffer->dl;
441	int pc = sb->pch.buffer->chan;
442	int rc = sb->rch.buffer->chan;
443	int swp = 0;
444
445	if (!pb && !rb) {
446		if (dir == PCMDIR_PLAY && pc < 4) swp = 1;
447		else if (dir == PCMDIR_REC && rc < 4) swp = 1;
448		if (sb->bd_flags & BD_F_SB16X) swp = !swp;
449		if (swp) {
450			int t;
451
452			t = sb->pch.buffer->chan;
453			sb->pch.buffer->chan = sb->rch.buffer->chan;
454			sb->rch.buffer->chan = t;
455			sb->pch.buffer->dir = B_WRITE;
456			sb->rch.buffer->dir = B_READ;
457		}
458	}
459}
460
461static int
462sb_doattach(device_t dev, struct sb_info *sb)
463{
464    	snddev_info *d = device_get_softc(dev);
465    	void *ih;
466    	char status[SND_STATUSLEN];
467
468    	if (sb_alloc_resources(sb, dev)) goto no;
469    	if (sb_reset_dsp(sb)) goto no;
470    	mixer_init(d, &sb_mixer, sb);
471
472	if (sb->bd_flags & BD_F_ESS)
473		bus_setup_intr(dev, sb->irq, INTR_TYPE_TTY, ess_intr, sb, &ih);
474	else
475		bus_setup_intr(dev, sb->irq, INTR_TYPE_TTY, sb_intr, sb, &ih);
476    	if ((sb->bd_flags & BD_F_SB16) && !(sb->bd_flags & BD_F_SB16X))
477		pcm_setswap(dev, sb16_swap);
478    	if (!sb->drq2)
479		pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
480
481    	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
482			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
483			/*highaddr*/BUS_SPACE_MAXADDR,
484			/*filter*/NULL, /*filterarg*/NULL,
485			/*maxsize*/DSP_BUFFSIZE, /*nsegments*/1,
486			/*maxsegz*/0x3ffff,
487			/*flags*/0, &sb->parent_dmat) != 0) {
488		device_printf(dev, "unable to create dma tag\n");
489		goto no;
490    	}
491
492    	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld",
493    	     	rman_get_start(sb->io_base), rman_get_start(sb->irq),
494		rman_get_start(sb->drq1));
495    	if (sb->drq2) snprintf(status + strlen(status),	SND_STATUSLEN - strlen(status),
496		":%ld", rman_get_start(sb->drq2));
497
498    	if (pcm_register(dev, sb, 1, 1)) goto no;
499	if (sb->bd_flags & BD_F_ESS) {
500		pcm_addchan(dev, PCMDIR_REC, &ess_chantemplate, sb);
501		pcm_addchan(dev, PCMDIR_PLAY, &ess_chantemplate, sb);
502	} else {
503		pcm_addchan(dev, PCMDIR_REC, &sb_chantemplate, sb);
504		pcm_addchan(dev, PCMDIR_PLAY, &sb_chantemplate, sb);
505	}
506    	pcm_setstatus(dev, status);
507
508    	return 0;
509
510no:
511    	sb_release_resources(sb, dev);
512    	return ENXIO;
513}
514
515static void
516sb_intr(void *arg)
517{
518    	struct sb_info *sb = (struct sb_info *)arg;
519    	int reason = 3, c;
520
521    	/*
522     	* SB < 4.0 is half duplex and has only 1 bit for int source,
523     	* so we fake it. SB 4.x (SB16) has the int source in a separate
524     	* register.
525     	* The Vibra16X has separate flags for 8 and 16 bit transfers, but
526     	* I have no idea how to tell capture from playback interrupts...
527     	*/
528    	if (sb->bd_flags & BD_F_SB16) {
529    		c = sb_getmixer(sb, IRQ_STAT);
530    		/* this tells us if the source is 8-bit or 16-bit dma. We
531     		* have to check the io channel to map it to read or write...
532     		*/
533    		reason = 0;
534    		if (c & 1) { /* 8-bit dma */
535			if (sb->pch.fmt & AFMT_U8) reason |= 1;
536			if (sb->rch.fmt & AFMT_U8) reason |= 2;
537    		}
538    		if (c & 2) { /* 16-bit dma */
539			if (sb->pch.fmt & AFMT_S16_LE) reason |= 1;
540			if (sb->rch.fmt & AFMT_S16_LE) reason |= 2;
541    		}
542    	} else c = 1;
543#if 0
544    	printf("sb_intr: reason=%d c=0x%x\n", reason, c);
545#endif
546    	if ((reason & 1) && (sb->pch.buffer->dl > 0))
547		chn_intr(sb->pch.channel);
548    	if ((reason & 2) && (sb->rch.buffer->dl > 0))
549		chn_intr(sb->rch.channel);
550    	if (c & 1) sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */
551    	if (c & 2) sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */
552}
553
554static void
555ess_intr(void *arg)
556{
557    struct sb_info *sb = (struct sb_info *)arg;
558    sb_rd(sb, DSP_DATA_AVAIL); /* int ack */
559#ifdef notyet
560    /*
561     * XXX
562     * for full-duplex mode:
563     * should read port 0x6 to identify where interrupt came from.
564     */
565#endif
566    /*
567     * We are transferring data in DSP normal mode,
568     * so clear the dl to indicate the DMA is stopped.
569     */
570    if (sb->pch.buffer->dl > 0) {
571	sb->pch.buffer->dl = -1;
572	chn_intr(sb->pch.channel);
573    }
574    if (sb->rch.buffer->dl > 0) {
575	sb->rch.buffer->dl = -1;
576	chn_intr(sb->rch.channel);
577    }
578}
579
580static int
581sb_format(struct sb_chinfo *ch, u_int32_t format)
582{
583	ch->fmt = format;
584	return 0;
585}
586
587static int
588sb_speed(struct sb_chinfo *ch, int speed)
589{
590    	struct sb_info *sb = ch->parent;
591    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
592    	int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0;
593
594    	if (sb->bd_flags & BD_F_SB16) {
595		RANGE(speed, 5000, 45000);
596		sb_cmd(sb, 0x42 - play);
597    		sb_cmd(sb, speed >> 8);
598		sb_cmd(sb, speed & 0xff);
599    	} else {
600		u_char tconst;
601		int max_speed = 45000, tmp;
602        	u_long flags;
603
604    		/* here enforce speed limitations - max 22050 on sb 1.x*/
605    		if (sb->bd_id <= 0x200) max_speed = 22050;
606
607    		/*
608     	 	* SB models earlier than SB Pro have low limit for the
609     	 	* input rate. Note that this is only for input, but since
610     	 	* we do not support separate values for rec & play....
611     	 	*/
612		if (!play) {
613    			if (sb->bd_id <= 0x200) max_speed = 13000;
614    			else if (sb->bd_id < 0x300) max_speed = 15000;
615		}
616    		RANGE(speed, 4000, max_speed);
617    		if (stereo) speed <<= 1;
618
619    		/*
620     	 	* Now the speed should be valid. Compute the value to be
621     	 	* programmed into the board.
622     	 	*/
623    		if (speed > 22050) { /* High speed mode on 2.01/3.xx */
624			tconst = (u_char)
625				((65536 - ((256000000 + speed / 2) / speed))
626				>> 8);
627			sb->bd_flags |= BD_F_HISPEED;
628			tmp = 65536 - (tconst << 8);
629			speed = (256000000 + tmp / 2) / tmp;
630    		} else {
631			sb->bd_flags &= ~BD_F_HISPEED;
632			tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
633			tmp = 256 - tconst;
634			speed = (1000000 + tmp / 2) / tmp;
635    		}
636		flags = spltty();
637		sb_cmd1(sb, 0x40, tconst); /* set time constant */
638		splx(flags);
639    		if (stereo) speed >>= 1;
640    	}
641    	return speed;
642}
643
644static int
645sb_start(struct sb_chinfo *ch)
646{
647	struct sb_info *sb = ch->parent;
648    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
649    	int b16 = (ch->fmt & AFMT_S16_LE)? 1 : 0;
650    	int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0;
651	int l = ch->buffer->dl;
652	int dh = ch->buffer->chan > 3;
653	u_char i1, i2;
654
655	if (b16 || dh) l >>= 1;
656	l--;
657	if (play) sb_cmd(sb, DSP_CMD_SPKON);
658	if (sb->bd_flags & BD_F_SB16) {
659	    i1 = DSP_F16_AUTO | DSP_F16_FIFO_ON |
660	         (play? DSP_F16_DAC : DSP_F16_ADC);
661	    i1 |= (b16 || dh)? DSP_DMA16 : DSP_DMA8;
662	    i2 = (stereo? DSP_F16_STEREO : 0) | (b16? DSP_F16_SIGNED : 0);
663	    sb_cmd(sb, i1);
664	    sb_cmd2(sb, i2, l);
665	} else {
666	    if (sb->bd_flags & BD_F_HISPEED) i1 = play? 0x90 : 0x98;
667	    else i1 = play? 0x1c : 0x2c;
668	    sb_setmixer(sb, 0x0e, stereo? 2 : 0);
669	    /* an ESS extension -- they can do 16 bits */
670	    if (b16) i1 |= 1;
671	    sb_cmd2(sb, 0x48, l);
672	    sb_cmd(sb, i1);
673	}
674	sb->bd_flags |= BD_F_DMARUN << b16;
675	return 0;
676}
677
678static int
679sb_stop(struct sb_chinfo *ch)
680{
681	struct sb_info *sb = ch->parent;
682    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
683    	int b16 = (ch->fmt & AFMT_S16_LE)? 1 : 0;
684
685    	if (sb->bd_flags & BD_F_HISPEED) sb_reset_dsp(sb);
686	else {
687		sb_cmd(sb, b16? DSP_CMD_DMAPAUSE_16 : DSP_CMD_DMAPAUSE_8);
688	       /*
689		* The above seems to have the undocumented side effect of
690		* blocking the other side as well. If the other
691		* channel was active (SB16) I have to re-enable it :(
692		*/
693		if (sb->bd_flags & (BD_F_DMARUN << (1 - b16)))
694			sb_cmd(sb, b16? 0xd4 : 0xd6 );
695	}
696	if (play) sb_cmd(sb, DSP_CMD_SPKOFF); /* speaker off */
697	sb->bd_flags &= ~(BD_F_DMARUN << b16);
698	return 0;
699}
700
701/* utility functions for ESS */
702static int
703ess_format(struct sb_chinfo *ch, u_int32_t format)
704{
705	struct sb_info *sb = ch->parent;
706	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
707	int b16 = (format & AFMT_S16_LE)? 1 : 0;
708	int stereo = (format & AFMT_STEREO)? 1 : 0;
709	u_char c;
710	ch->fmt = format;
711	sb_reset_dsp(sb);
712	/* auto-init DMA mode */
713	ess_write(sb, 0xb8, play ? 0x04 : 0x0e);
714	/* mono/stereo */
715	c = (ess_read(sb, 0xa8) & ~0x03) | 1;
716	if (!stereo) c++;
717	ess_write(sb, 0xa8, c);
718	/* demand mode, 4 bytes/xfer */
719	ess_write(sb, 0xb9, 2);
720	/* setup dac/adc */
721	if (play) ess_write(sb, 0xb6, b16? 0x00 : 0x80);
722	ess_write(sb, 0xb7, 0x51 | (b16? 0x20 : 0x00));
723	ess_write(sb, 0xb7, 0x98 + (b16? 0x24 : 0x00) + (stereo? 0x00 : 0x38));
724	/* irq/drq control */
725	ess_write(sb, 0xb1, (ess_read(sb, 0xb1) & 0x0f) | 0x50);
726	ess_write(sb, 0xb2, (ess_read(sb, 0xb2) & 0x0f) | 0x50);
727	return 0;
728}
729
730static int
731ess_speed(struct sb_chinfo *ch, int speed)
732{
733	struct sb_info *sb = ch->parent;
734	int t;
735	RANGE (speed, 5000, 49000);
736	if (speed > 22000) {
737		t = (795500 + speed / 2) / speed;
738		speed = (795500 + t / 2) / t;
739		t = (256 - t ) | 0x80;
740	} else {
741		t = (397700 + speed / 2) / speed;
742		speed = (397700 + t / 2) / t;
743		t = 128 - t;
744	}
745	ess_write(sb, 0xa1, t); /* set time constant */
746#if 0
747	d->play_speed = d->rec_speed = speed;
748	speed = (speed * 9 ) / 20;
749#endif
750	t = 256 - 7160000 / ((speed * 9 / 20) * 82);
751	ess_write(sb, 0xa2, t);
752	return speed;
753}
754
755static int
756ess_start(struct sb_chinfo *ch)
757{
758	struct sb_info *sb = ch->parent;
759    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
760	short c = - ch->buffer->dl;
761	u_char c1;
762	/*
763	 * clear bit 0 of register B8h
764	 */
765#if 1
766	c1 = play ? 0x04 : 0x0e;
767	ess_write(sb, 0xb8, c1++);
768#else
769	c1 = ess_read(sb, 0xb8) & 0xfe;
770	ess_write(sb, 0xb8, c1++);
771#endif
772	/*
773	 * update ESS Transfer Count Register
774	 */
775	ess_write(sb, 0xa4, (u_char)((u_short)c & 0xff));
776	ess_write(sb, 0xa5, (u_char)(((u_short)c >> 8) & 0xff));
777	/*
778	 * set bit 0 of register B8h
779	 */
780	ess_write(sb, 0xb8, c1);
781	if (play)
782		sb_cmd(sb, DSP_CMD_SPKON);
783	return 0;
784}
785
786static int
787ess_stop(struct sb_chinfo *ch)
788{
789	struct sb_info *sb = ch->parent;
790	/*
791	 * no need to send a stop command if the DMA has already stopped.
792	 */
793	if (ch->buffer->dl > 0) {
794		sb_cmd(sb, DSP_CMD_DMAPAUSE_8); /* pause dma. */
795	}
796	return 0;
797}
798
799static int
800ess_abort(struct sb_chinfo *ch)
801{
802	struct sb_info *sb = ch->parent;
803    	int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
804	if (play) sb_cmd(sb, DSP_CMD_SPKOFF); /* speaker off */
805	sb_reset_dsp(sb);
806	ess_format(ch, ch->fmt);
807	ess_speed(ch, ch->channel->speed);
808	return 0;
809}
810
811/* channel interface */
812static void *
813sbchan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
814{
815	struct sb_info *sb = devinfo;
816	struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
817	int dch, dl, dh;
818
819	ch->parent = sb;
820	ch->channel = c;
821	ch->buffer = b;
822	ch->buffer->bufsize = DSP_BUFFSIZE;
823	if (chn_allocbuf(ch->buffer, sb->parent_dmat) == -1) return NULL;
824	dch = (dir == PCMDIR_PLAY)? 1 : 0;
825	if (sb->bd_flags & BD_F_SB16X) dch = !dch;
826	dl = rman_get_start(sb->drq1);
827	if (sb->drq2) dh = rman_get_start(sb->drq2); else dh = dl;
828	ch->buffer->chan = dch? dh : dl;
829	return ch;
830}
831
832static int
833sbchan_setdir(void *data, int dir)
834{
835	struct sb_chinfo *ch = data;
836	ch->dir = dir;
837	return 0;
838}
839
840static int
841sbchan_setformat(void *data, u_int32_t format)
842{
843	struct sb_chinfo *ch = data;
844	sb_format(ch, format);
845	return 0;
846}
847
848static int
849sbchan_setspeed(void *data, u_int32_t speed)
850{
851	struct sb_chinfo *ch = data;
852	return sb_speed(ch, speed);
853}
854
855static int
856sbchan_setblocksize(void *data, u_int32_t blocksize)
857{
858	return blocksize;
859}
860
861static int
862sbchan_trigger(void *data, int go)
863{
864	struct sb_chinfo *ch = data;
865	if (go == PCMTRIG_EMLDMAWR) return 0;
866	buf_isadma(ch->buffer, go);
867	if (go == PCMTRIG_START) sb_start(ch); else sb_stop(ch);
868	return 0;
869}
870
871static int
872sbchan_getptr(void *data)
873{
874	struct sb_chinfo *ch = data;
875	return buf_isadmaptr(ch->buffer);
876}
877
878static pcmchan_caps *
879sbchan_getcaps(void *data)
880{
881	struct sb_chinfo *ch = data;
882	int p = (ch->dir == PCMDIR_PLAY)? 1 : 0;
883	if (ch->parent->bd_id < 0x300)
884		return p? &sb_playcaps : &sb_reccaps;
885	else if (ch->parent->bd_id < 0x400)
886		return p? &sbpro_playcaps : &sbpro_reccaps;
887	else if (ch->parent->bd_flags & BD_F_SB16X)
888		return &sb16x_caps;
889	else
890		return (ch->buffer->chan >= 4)? &sb16_hcaps : &sb16_lcaps;
891}
892/* channel interface for ESS18xx */
893static void *
894esschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
895{
896	struct sb_info *sb = devinfo;
897	struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
898
899	ch->parent = sb;
900	ch->channel = c;
901	ch->buffer = b;
902	ch->buffer->bufsize = ESS_BUFFSIZE;
903	if (chn_allocbuf(ch->buffer, sb->parent_dmat) == -1) return NULL;
904	ch->buffer->chan = rman_get_start(sb->drq1);
905	return ch;
906}
907
908static int
909esschan_setdir(void *data, int dir)
910{
911	struct sb_chinfo *ch = data;
912	ch->dir = dir;
913	return 0;
914}
915
916static int
917esschan_setformat(void *data, u_int32_t format)
918{
919	struct sb_chinfo *ch = data;
920	ess_format(ch, format);
921	return 0;
922}
923
924static int
925esschan_setspeed(void *data, u_int32_t speed)
926{
927	struct sb_chinfo *ch = data;
928	return ess_speed(ch, speed);
929}
930
931static int
932esschan_setblocksize(void *data, u_int32_t blocksize)
933{
934	return blocksize;
935}
936
937static int
938esschan_trigger(void *data, int go)
939{
940	struct sb_chinfo *ch = data;
941	if (go == PCMTRIG_EMLDMAWR) return 0;
942	switch (go) {
943	case PCMTRIG_START:
944		if (!ch->ess_dma_started)
945			buf_isadma(ch->buffer, go);
946		ch->ess_dma_started = 1;
947		ess_start(ch);
948		break;
949	case PCMTRIG_STOP:
950		if (ch->buffer->dl >= 0) {
951			buf_isadma(ch->buffer, go);
952			ch->ess_dma_started = 0;
953			ess_stop(ch);
954		}
955		break;
956	case PCMTRIG_ABORT:
957	default:
958		ch->ess_dma_started = 0;
959		ess_abort(ch);
960		buf_isadma(ch->buffer, go);
961		break;
962	}
963	return 0;
964}
965
966static int
967esschan_getptr(void *data)
968{
969	struct sb_chinfo *ch = data;
970	return buf_isadmaptr(ch->buffer);
971}
972
973static pcmchan_caps *
974esschan_getcaps(void *data)
975{
976	struct sb_chinfo *ch = data;
977	return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
978}
979
980/************************************************************/
981
982static int
983sbmix_init(snd_mixer *m)
984{
985    	struct sb_info *sb = mix_getdevinfo(m);
986
987    	switch (sb->bd_flags & BD_F_MIX_MASK) {
988    	case BD_F_MIX_CT1345: /* SB 3.0 has 1345 mixer */
989		mix_setdevs(m, SBPRO_MIXER_DEVICES);
990		mix_setrecdevs(m, SBPRO_RECORDING_DEVICES);
991		sb_setmixer(sb, 0, 1); /* reset mixer */
992		if (!(sb->bd_flags & BD_F_ESS))
993			sb_setmixer(sb, MIC_VOL, 0x6); /* mic volume max */
994		sb_setmixer(sb, RECORD_SRC, 0x0); /* mic source */
995		sb_setmixer(sb, FM_VOL, 0x0); /* no midi */
996		break;
997
998    	case BD_F_MIX_CT1745: /* SB16 mixer ... */
999		mix_setdevs(m, SB16_MIXER_DEVICES);
1000		mix_setrecdevs(m, SB16_RECORDING_DEVICES);
1001		sb_setmixer(sb, 0x3c, 0x1f); /* make all output active */
1002		sb_setmixer(sb, 0x3d, 0); /* make all inputs-l off */
1003		sb_setmixer(sb, 0x3e, 0); /* make all inputs-r off */
1004    	}
1005    	return 0;
1006}
1007
1008static int
1009sbmix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right)
1010{
1011    	struct sb_info *sb = mix_getdevinfo(m);
1012    	int regoffs;
1013    	u_char   val;
1014    	mixer_tab *iomap;
1015
1016    	switch (sb->bd_flags & BD_F_MIX_MASK) {
1017    	case BD_F_MIX_CT1345:
1018		if (sb->bd_flags & BD_F_ESS)
1019			iomap = &ess_mix;
1020		else
1021			iomap = &sbpro_mix;
1022		break;
1023
1024    	case BD_F_MIX_CT1745:
1025		iomap = &sb16_mix;
1026		break;
1027
1028    	default:
1029        	return -1;
1030    	}
1031
1032	/* Change left channel */
1033    	regoffs = (*iomap)[dev][LEFT_CHN].regno;
1034    	if (regoffs != 0) {
1035		val = sb_getmixer(sb, regoffs);
1036		change_bits(iomap, &val, dev, LEFT_CHN, left);
1037		sb_setmixer(sb, regoffs, val);
1038	}
1039
1040	/* Change right channel */
1041	regoffs = (*iomap)[dev][RIGHT_CHN].regno;
1042	if (regoffs != 0) {
1043		val = sb_getmixer(sb, regoffs); /* Read the new one */
1044		change_bits(iomap, &val, dev, RIGHT_CHN, right);
1045		sb_setmixer(sb, regoffs, val);
1046	} else
1047		right = left;
1048
1049    	return left | (right << 8);
1050}
1051
1052static int
1053sbmix_setrecsrc(snd_mixer *m, u_int32_t src)
1054{
1055    	struct sb_info *sb = mix_getdevinfo(m);
1056    	u_char recdev;
1057
1058    	switch (sb->bd_flags & BD_F_MIX_MASK) {
1059    	case BD_F_MIX_CT1345:
1060		if      (src == SOUND_MASK_LINE) 	recdev = 0x06;
1061		else if (src == SOUND_MASK_CD) 		recdev = 0x02;
1062		else { /* default: mic */
1063	    		src = SOUND_MASK_MIC;
1064	    		recdev = 0;
1065		}
1066		sb_setmixer(sb, RECORD_SRC, recdev |
1067			    (sb_getmixer(sb, RECORD_SRC) & ~0x07));
1068		break;
1069
1070    	case BD_F_MIX_CT1745: /* sb16 */
1071		recdev = 0;
1072		if (src & SOUND_MASK_MIC)   recdev |= 0x01; /* mono mic */
1073		if (src & SOUND_MASK_CD)    recdev |= 0x06; /* l+r cd */
1074		if (src & SOUND_MASK_LINE)  recdev |= 0x18; /* l+r line */
1075		if (src & SOUND_MASK_SYNTH) recdev |= 0x60; /* l+r midi */
1076		sb_setmixer(sb, SB16_IMASK_L, recdev);
1077		sb_setmixer(sb, SB16_IMASK_R, recdev);
1078		/*
1079	 	* since the same volume controls apply to the input and
1080	 	* output sections, the best approach to have a consistent
1081	 	* behaviour among cards would be to disable the output path
1082	 	* on devices which are used to record.
1083	 	* However, since users like to have feedback, we only disable
1084	 	* the mic -- permanently.
1085	 	*/
1086        	sb_setmixer(sb, SB16_OMASK, 0x1f & ~1);
1087		break;
1088       	}
1089    	return src;
1090}
1091
1092static int
1093sbsbc_probe(device_t dev)
1094{
1095    	char buf[64];
1096	uintptr_t func, ver, r, f;
1097
1098	/* The parent device has already been probed. */
1099	r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
1100	if (func != SCF_PCM)
1101		return (ENXIO);
1102
1103	r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
1104	f = (ver & 0xffff0000) >> 16;
1105	ver &= 0x0000ffff;
1106	snprintf(buf, sizeof buf, "SB DSP %d.%02d%s%s", (int) ver >> 8, (int) ver & 0xff,
1107		(f & BD_F_ESS)? " (ESS mode)" : "",
1108		(f & BD_F_SB16X)? " (ViBRA16X)" : "");
1109    	device_set_desc_copy(dev, buf);
1110
1111	return 0;
1112}
1113
1114static int
1115sbsbc_attach(device_t dev)
1116{
1117    	struct sb_info *sb;
1118	uintptr_t ver;
1119
1120    	sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT);
1121    	if (!sb) return ENXIO;
1122    	bzero(sb, sizeof *sb);
1123
1124	BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
1125	sb->bd_id = ver & 0x0000ffff;
1126	sb->bd_flags = (ver & 0xffff0000) >> 16;
1127
1128    	return sb_doattach(dev, sb);
1129}
1130
1131static device_method_t sbsbc_methods[] = {
1132	/* Device interface */
1133	DEVMETHOD(device_probe,		sbsbc_probe),
1134	DEVMETHOD(device_attach,	sbsbc_attach),
1135
1136	{ 0, 0 }
1137};
1138
1139static driver_t sbsbc_driver = {
1140	"pcm",
1141	sbsbc_methods,
1142	sizeof(snddev_info),
1143};
1144
1145DRIVER_MODULE(sbsbc, sbc, sbsbc_driver, pcm_devclass, 0, 0);
1146
1147
1148
1149