sb16.c revision 74364
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 74364 2001-03-16 21:04:14Z cg $
32 */
33
34#include <dev/sound/pcm/sound.h>
35
36#include  <dev/sound/isa/sb.h>
37#include  <dev/sound/chip.h>
38
39#include "mixer_if.h"
40
41#define SB16_BUFFSIZE	4096
42#define PLAIN_SB16(x) ((((x)->bd_flags) & (BD_F_SB16|BD_F_SB16X)) == BD_F_SB16)
43
44static u_int32_t sb16_fmt8[] = {
45	AFMT_U8,
46	AFMT_STEREO | AFMT_U8,
47	0
48};
49static pcmchan_caps sb16_caps8 = {5000, 45000, sb16_fmt8, 0};
50
51static u_int32_t sb16_fmt16[] = {
52	AFMT_S16_LE,
53	AFMT_STEREO | AFMT_S16_LE,
54	0
55};
56static pcmchan_caps sb16_caps16 = {5000, 45000, sb16_fmt16, 0};
57
58static u_int32_t sb16x_fmt[] = {
59	AFMT_U8,
60	AFMT_STEREO | AFMT_U8,
61	AFMT_S16_LE,
62	AFMT_STEREO | AFMT_S16_LE,
63	0
64};
65static pcmchan_caps sb16x_caps = {5000, 49000, sb16x_fmt, 0};
66
67struct sb_info;
68
69struct sb_chinfo {
70	struct sb_info *parent;
71	pcm_channel *channel;
72	snd_dbuf *buffer;
73	int dir, run, dch;
74	u_int32_t fmt, spd, blksz;
75};
76
77struct sb_info {
78    	struct resource *io_base;	/* I/O address for the board */
79    	struct resource *irq;
80   	struct resource *drq1;
81    	struct resource *drq2;
82    	void *ih;
83    	bus_dma_tag_t parent_dmat;
84
85    	int bd_id;
86    	u_long bd_flags;       /* board-specific flags */
87	int prio, prio16;
88    	struct sb_chinfo pch, rch;
89};
90
91static int sb_rd(struct sb_info *sb, int reg);
92static void sb_wr(struct sb_info *sb, int reg, u_int8_t val);
93static int sb_cmd(struct sb_info *sb, u_char val);
94/* static int sb_cmd1(struct sb_info *sb, u_char cmd, int val); */
95static int sb_cmd2(struct sb_info *sb, u_char cmd, int val);
96static u_int sb_get_byte(struct sb_info *sb);
97static void sb_setmixer(struct sb_info *sb, u_int port, u_int value);
98static int sb_getmixer(struct sb_info *sb, u_int port);
99static int sb_reset_dsp(struct sb_info *sb);
100
101static void sb_intr(void *arg);
102
103static devclass_t pcm_devclass;
104
105/*
106 * Common code for the midi and pcm functions
107 *
108 * sb_cmd write a single byte to the CMD port.
109 * sb_cmd1 write a CMD + 1 byte arg
110 * sb_cmd2 write a CMD + 2 byte arg
111 * sb_get_byte returns a single byte from the DSP data port
112 */
113
114static int
115port_rd(struct resource *port, int off)
116{
117	return bus_space_read_1(rman_get_bustag(port), rman_get_bushandle(port), off);
118}
119
120static void
121port_wr(struct resource *port, int off, u_int8_t data)
122{
123	return bus_space_write_1(rman_get_bustag(port), rman_get_bushandle(port), off, data);
124}
125
126static int
127sb_rd(struct sb_info *sb, int reg)
128{
129	return port_rd(sb->io_base, reg);
130}
131
132static void
133sb_wr(struct sb_info *sb, int reg, u_int8_t val)
134{
135	port_wr(sb->io_base, reg, val);
136}
137
138static int
139sb_dspwr(struct sb_info *sb, u_char val)
140{
141    	int  i;
142
143    	for (i = 0; i < 1000; i++) {
144		if ((sb_rd(sb, SBDSP_STATUS) & 0x80))
145	    		DELAY((i > 100)? 1000 : 10);
146	    	else {
147			sb_wr(sb, SBDSP_CMD, val);
148			return 1;
149		}
150    	}
151    	printf("sb_dspwr(0x%02x) timed out.\n", val);
152    	return 0;
153}
154
155static int
156sb_cmd(struct sb_info *sb, u_char val)
157{
158#if 0
159	printf("sb_cmd: %x\n", val);
160#endif
161    	return sb_dspwr(sb, val);
162}
163
164/*
165static int
166sb_cmd1(struct sb_info *sb, u_char cmd, int val)
167{
168#if 0
169    	printf("sb_cmd1: %x, %x\n", cmd, val);
170#endif
171    	if (sb_dspwr(sb, cmd)) {
172		return sb_dspwr(sb, val & 0xff);
173    	} else return 0;
174}
175*/
176
177static int
178sb_cmd2(struct sb_info *sb, u_char cmd, int val)
179{
180#if 0
181    	printf("sb_cmd2: %x, %x\n", cmd, val);
182#endif
183    	if (sb_dspwr(sb, cmd)) {
184		return sb_dspwr(sb, val & 0xff) &&
185		       sb_dspwr(sb, (val >> 8) & 0xff);
186    	} else return 0;
187}
188
189/*
190 * in the SB, there is a set of indirect "mixer" registers with
191 * address at offset 4, data at offset 5
192 */
193static void
194sb_setmixer(struct sb_info *sb, u_int port, u_int value)
195{
196    	u_long   flags;
197
198    	flags = spltty();
199    	sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
200    	DELAY(10);
201    	sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff));
202    	DELAY(10);
203    	splx(flags);
204}
205
206static int
207sb_getmixer(struct sb_info *sb, u_int port)
208{
209    	int val;
210    	u_long flags;
211
212    	flags = spltty();
213    	sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
214    	DELAY(10);
215    	val = sb_rd(sb, SB_MIX_DATA);
216    	DELAY(10);
217    	splx(flags);
218
219    	return val;
220}
221
222static u_int
223sb_get_byte(struct sb_info *sb)
224{
225    	int i;
226
227    	for (i = 1000; i > 0; i--) {
228		if (sb_rd(sb, DSP_DATA_AVAIL) & 0x80)
229			return sb_rd(sb, DSP_READ);
230		else
231			DELAY(20);
232    	}
233    	return 0xffff;
234}
235
236static int
237sb_reset_dsp(struct sb_info *sb)
238{
239    	sb_wr(sb, SBDSP_RST, 3);
240    	DELAY(100);
241    	sb_wr(sb, SBDSP_RST, 0);
242    	if (sb_get_byte(sb) != 0xAA) {
243        	DEB(printf("sb_reset_dsp 0x%lx failed\n",
244			   rman_get_start(d->io_base)));
245		return ENXIO;	/* Sorry */
246    	}
247    	return 0;
248}
249
250/************************************************************/
251
252struct sb16_mixent {
253	int reg;
254	int bits;
255	int ofs;
256	int stereo;
257};
258
259static const struct sb16_mixent sb16_mixtab[32] = {
260    	[SOUND_MIXER_VOLUME]	= { 0x30, 5, 3, 1 },
261    	[SOUND_MIXER_PCM]	= { 0x32, 5, 3, 1 },
262    	[SOUND_MIXER_SYNTH]	= { 0x34, 5, 3, 1 },
263    	[SOUND_MIXER_CD]	= { 0x36, 5, 3, 1 },
264    	[SOUND_MIXER_LINE]	= { 0x38, 5, 3, 1 },
265    	[SOUND_MIXER_MIC]	= { 0x3a, 5, 3, 0 },
266       	[SOUND_MIXER_SPEAKER]	= { 0x3b, 5, 3, 0 },
267    	[SOUND_MIXER_IGAIN]	= { 0x3f, 2, 6, 1 },
268    	[SOUND_MIXER_OGAIN]	= { 0x41, 2, 6, 1 },
269	[SOUND_MIXER_TREBLE]	= { 0x44, 4, 4, 1 },
270    	[SOUND_MIXER_BASS]	= { 0x46, 4, 4, 1 },
271};
272
273static int
274sb16mix_init(snd_mixer *m)
275{
276    	struct sb_info *sb = mix_getdevinfo(m);
277
278	mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
279     		       SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD |
280     		       SOUND_MASK_IGAIN | SOUND_MASK_OGAIN |
281     		       SOUND_MASK_VOLUME | SOUND_MASK_BASS | SOUND_MASK_TREBLE);
282
283	mix_setrecdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_LINE |
284			  SOUND_MASK_MIC | SOUND_MASK_CD);
285
286	sb_setmixer(sb, 0x3c, 0x1f); /* make all output active */
287
288	sb_setmixer(sb, 0x3d, 0); /* make all inputs-l off */
289	sb_setmixer(sb, 0x3e, 0); /* make all inputs-r off */
290
291	return 0;
292}
293
294static int
295sb16mix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right)
296{
297    	struct sb_info *sb = mix_getdevinfo(m);
298    	const struct sb16_mixent *e;
299    	int max;
300
301	e = &sb16_mixtab[dev];
302	max = (1 << e->bits) - 1;
303
304	left = (left * max) / 100;
305	right = (right * max) / 100;
306
307	sb_setmixer(sb, e->reg, left << e->ofs);
308	if (e->stereo)
309		sb_setmixer(sb, e->reg + 1, right << e->ofs);
310	else
311		right = left;
312
313	left = (left * 100) / max;
314	right = (right * 100) / max;
315
316    	return left | (right << 8);
317}
318
319static int
320sb16mix_setrecsrc(snd_mixer *m, u_int32_t src)
321{
322    	struct sb_info *sb = mix_getdevinfo(m);
323    	u_char recdev;
324
325	recdev = 0;
326	if (src & SOUND_MASK_MIC)
327		recdev |= 0x01; /* mono mic */
328
329	if (src & SOUND_MASK_CD)
330		recdev |= 0x06; /* l+r cd */
331
332	if (src & SOUND_MASK_LINE)
333		recdev |= 0x18; /* l+r line */
334
335	if (src & SOUND_MASK_SYNTH)
336		recdev |= 0x60; /* l+r midi */
337
338	sb_setmixer(sb, SB16_IMASK_L, recdev);
339	sb_setmixer(sb, SB16_IMASK_R, recdev);
340
341	/*
342	 * since the same volume controls apply to the input and
343	 * output sections, the best approach to have a consistent
344	 * behaviour among cards would be to disable the output path
345	 * on devices which are used to record.
346	 * However, since users like to have feedback, we only disable
347	 * the mic -- permanently.
348	 */
349        sb_setmixer(sb, SB16_OMASK, 0x1f & ~1);
350
351	return src;
352}
353
354static kobj_method_t sb16mix_mixer_methods[] = {
355    	KOBJMETHOD(mixer_init,		sb16mix_init),
356    	KOBJMETHOD(mixer_set,		sb16mix_set),
357    	KOBJMETHOD(mixer_setrecsrc,	sb16mix_setrecsrc),
358	{ 0, 0 }
359};
360MIXER_DECLARE(sb16mix_mixer);
361
362/************************************************************/
363
364static void
365sb16_release_resources(struct sb_info *sb, device_t dev)
366{
367    	if (sb->irq) {
368    		if (sb->ih)
369			bus_teardown_intr(dev, sb->irq, sb->ih);
370 		bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq);
371		sb->irq = 0;
372    	}
373    	if (sb->drq2 && (sb->drq2 != sb->drq1)) {
374		isa_dma_release(rman_get_start(sb->drq2));
375		bus_release_resource(dev, SYS_RES_DRQ, 1, sb->drq2);
376		sb->drq2 = 0;
377    	}
378     	if (sb->drq1) {
379		isa_dma_release(rman_get_start(sb->drq1));
380		bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq1);
381		sb->drq1 = 0;
382    	}
383   	if (sb->io_base) {
384		bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base);
385		sb->io_base = 0;
386    	}
387    	if (sb->parent_dmat) {
388		bus_dma_tag_destroy(sb->parent_dmat);
389		sb->parent_dmat = 0;
390    	}
391     	free(sb, M_DEVBUF);
392}
393
394static int
395sb16_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, &rid, 0, ~0, 1, RF_ACTIVE);
402
403	rid = 0;
404	if (!sb->irq)
405    		sb->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
406
407	rid = 0;
408	if (!sb->drq1)
409    		sb->drq1 = bus_alloc_resource(dev, SYS_RES_DRQ, &rid, 0, ~0, 1, RF_ACTIVE);
410
411	rid = 1;
412	if (!sb->drq2)
413        	sb->drq2 = bus_alloc_resource(dev, SYS_RES_DRQ, &rid, 0, ~0, 1, RF_ACTIVE);
414
415    	if (sb->io_base && sb->drq1 && sb->irq) {
416		int bs = SB16_BUFFSIZE;
417
418		isa_dma_acquire(rman_get_start(sb->drq1));
419		isa_dmainit(rman_get_start(sb->drq1), bs);
420
421		if (sb->drq2) {
422			isa_dma_acquire(rman_get_start(sb->drq2));
423			isa_dmainit(rman_get_start(sb->drq2), bs);
424		} else {
425			sb->drq2 = sb->drq1;
426			pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
427		}
428		return 0;
429	} else return ENXIO;
430}
431
432static void
433sb_intr(void *arg)
434{
435    	struct sb_info *sb = (struct sb_info *)arg;
436    	int reason = 3, c;
437
438    	/*
439     	 * The Vibra16X has separate flags for 8 and 16 bit transfers, but
440     	 * I have no idea how to tell capture from playback interrupts...
441     	 */
442
443	reason = 0;
444    	c = sb_getmixer(sb, IRQ_STAT);
445
446	/*
447	 * this tells us if the source is 8-bit or 16-bit dma. We
448     	 * have to check the io channel to map it to read or write...
449     	 */
450
451	if (sb->bd_flags & BD_F_SB16X) {
452    		if (c & 1) { /* 8-bit format */
453			if (sb->pch.fmt & AFMT_8BIT)
454				reason |= 1;
455			if (sb->rch.fmt & AFMT_8BIT)
456				reason |= 2;
457    		}
458    		if (c & 2) { /* 16-bit format */
459			if (sb->pch.fmt & AFMT_16BIT)
460				reason |= 1;
461			if (sb->rch.fmt & AFMT_16BIT)
462				reason |= 2;
463    		}
464	} else {
465    		if (c & 1) { /* 8-bit dma */
466			if (sb->pch.dch == 1)
467				reason |= 1;
468			if (sb->rch.dch == 1)
469				reason |= 2;
470    		}
471    		if (c & 2) { /* 16-bit dma */
472			if (sb->pch.dch == 2)
473				reason |= 1;
474			if (sb->rch.dch == 2)
475				reason |= 2;
476    		}
477	}
478#if 0
479    	printf("sb_intr: reason=%d c=0x%x\n", reason, c);
480#endif
481    	if ((reason & 1) && (sb->pch.run))
482		chn_intr(sb->pch.channel);
483
484    	if ((reason & 2) && (sb->rch.run))
485		chn_intr(sb->rch.channel);
486
487    	if (c & 1)
488		sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */
489
490    	if (c & 2)
491		sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */
492}
493
494static int
495sb_setup(struct sb_info *sb)
496{
497	struct sb_chinfo *ch;
498	u_int8_t v;
499	int l, pprio;
500
501	if (sb->bd_flags & BD_F_DMARUN)
502		sndbuf_isadma(sb->pch.buffer, PCMTRIG_STOP);
503	if (sb->bd_flags & BD_F_DMARUN2)
504		sndbuf_isadma(sb->rch.buffer, PCMTRIG_STOP);
505	sb->bd_flags &= ~(BD_F_DMARUN | BD_F_DMARUN2);
506
507	sb_reset_dsp(sb);
508
509	if (sb->bd_flags & BD_F_SB16X) {
510		pprio = sb->pch.run? 1 : 0;
511		sndbuf_isadmasetup(sb->pch.buffer, pprio? sb->drq1 : NULL);
512		sb->pch.dch = pprio? 1 : 0;
513		sndbuf_isadmasetup(sb->rch.buffer, pprio? sb->drq2 : sb->drq1);
514		sb->rch.dch = pprio? 2 : 1;
515	} else {
516		if (sb->pch.run && sb->rch.run) {
517			pprio = (sb->rch.fmt & AFMT_16BIT)? 0 : 1;
518			sndbuf_isadmasetup(sb->pch.buffer, pprio? sb->drq2 : sb->drq1);
519			sb->pch.dch = pprio? 2 : 1;
520			sndbuf_isadmasetup(sb->rch.buffer, pprio? sb->drq1 : sb->drq2);
521			sb->rch.dch = pprio? 1 : 2;
522		} else {
523			if (sb->pch.run) {
524				sndbuf_isadmasetup(sb->pch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1);
525				sb->pch.dch = (sb->pch.fmt & AFMT_16BIT)? 2 : 1;
526				sndbuf_isadmasetup(sb->rch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2);
527				sb->rch.dch = (sb->pch.fmt & AFMT_16BIT)? 1 : 2;
528			} else if (sb->rch.run) {
529				sndbuf_isadmasetup(sb->pch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2);
530				sb->pch.dch = (sb->rch.fmt & AFMT_16BIT)? 1 : 2;
531				sndbuf_isadmasetup(sb->rch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1);
532				sb->rch.dch = (sb->rch.fmt & AFMT_16BIT)? 2 : 1;
533			}
534		}
535	}
536
537	sndbuf_isadmasetdir(sb->pch.buffer, PCMDIR_PLAY);
538	sndbuf_isadmasetdir(sb->rch.buffer, PCMDIR_REC);
539
540	/*
541	printf("setup: [pch = %d, pfmt = %d, pgo = %d] [rch = %d, rfmt = %d, rgo = %d]\n",
542	       sb->pch.dch, sb->pch.fmt, sb->pch.run, sb->rch.dch, sb->rch.fmt, sb->rch.run);
543	*/
544
545	ch = &sb->pch;
546	if (ch->run) {
547		l = ch->blksz;
548		if (ch->fmt & AFMT_16BIT)
549			l >>= 1;
550		l--;
551
552		/* play speed */
553		RANGE(ch->spd, 5000, 45000);
554		sb_cmd(sb, DSP_CMD_OUT16);
555    		sb_cmd(sb, ch->spd >> 8);
556		sb_cmd(sb, ch->spd & 0xff);
557
558		/* play format, length */
559		v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_DAC;
560		v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8;
561		sb_cmd(sb, v);
562
563		v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0;
564		v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0;
565		sb_cmd2(sb, v, l);
566		sndbuf_isadma(ch->buffer, PCMTRIG_START);
567		sb->bd_flags |= BD_F_DMARUN;
568	}
569
570	ch = &sb->rch;
571	if (ch->run) {
572		l = ch->blksz;
573		if (ch->fmt & AFMT_16BIT)
574			l >>= 1;
575		l--;
576
577		/* record speed */
578		RANGE(ch->spd, 5000, 45000);
579		sb_cmd(sb, DSP_CMD_IN16);
580    		sb_cmd(sb, ch->spd >> 8);
581		sb_cmd(sb, ch->spd & 0xff);
582
583		/* record format, length */
584		v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_ADC;
585		v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8;
586		sb_cmd(sb, v);
587
588		v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0;
589		v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0;
590		sb_cmd2(sb, v, l);
591		sndbuf_isadma(ch->buffer, PCMTRIG_START);
592		sb->bd_flags |= BD_F_DMARUN2;
593	}
594
595    	return 0;
596}
597
598/* channel interface */
599static void *
600sb16chan_init(kobj_t obj, void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
601{
602	struct sb_info *sb = devinfo;
603	struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
604
605	ch->parent = sb;
606	ch->channel = c;
607	ch->buffer = b;
608	ch->dir = dir;
609
610	if (sndbuf_alloc(ch->buffer, sb->parent_dmat, SB16_BUFFSIZE) == -1)
611		return NULL;
612
613	return ch;
614}
615
616static int
617sb16chan_setformat(kobj_t obj, void *data, u_int32_t format)
618{
619	struct sb_chinfo *ch = data;
620	struct sb_info *sb = ch->parent;
621
622	ch->fmt = format;
623	sb->prio = ch->dir;
624	sb->prio16 = (ch->fmt & AFMT_16BIT)? 1 : 0;
625
626	return 0;
627}
628
629static int
630sb16chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
631{
632	struct sb_chinfo *ch = data;
633
634	ch->spd = speed;
635	return speed;
636}
637
638static int
639sb16chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
640{
641	struct sb_chinfo *ch = data;
642
643	ch->blksz = blocksize;
644	return ch->blksz;
645}
646
647static int
648sb16chan_trigger(kobj_t obj, void *data, int go)
649{
650	struct sb_chinfo *ch = data;
651	struct sb_info *sb = ch->parent;
652
653	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
654		return 0;
655
656	if (go == PCMTRIG_START)
657		ch->run = 1;
658	else
659		ch->run = 0;
660
661	sb_setup(sb);
662
663	return 0;
664}
665
666static int
667sb16chan_getptr(kobj_t obj, void *data)
668{
669	struct sb_chinfo *ch = data;
670
671	return sndbuf_isadmaptr(ch->buffer);
672}
673
674static pcmchan_caps *
675sb16chan_getcaps(kobj_t obj, void *data)
676{
677	struct sb_chinfo *ch = data;
678	struct sb_info *sb = ch->parent;
679
680	if ((sb->prio == 0) || (sb->prio == ch->dir))
681		return &sb16x_caps;
682	else
683		return sb->prio16? &sb16_caps8 : &sb16_caps16;
684}
685
686static int
687sb16chan_resetdone(kobj_t obj, void *data)
688{
689	struct sb_chinfo *ch = data;
690	struct sb_info *sb = ch->parent;
691
692	sb->prio = 0;
693
694	return 0;
695}
696
697static kobj_method_t sb16chan_methods[] = {
698    	KOBJMETHOD(channel_init,		sb16chan_init),
699    	KOBJMETHOD(channel_resetdone,		sb16chan_resetdone),
700    	KOBJMETHOD(channel_setformat,		sb16chan_setformat),
701    	KOBJMETHOD(channel_setspeed,		sb16chan_setspeed),
702    	KOBJMETHOD(channel_setblocksize,	sb16chan_setblocksize),
703    	KOBJMETHOD(channel_trigger,		sb16chan_trigger),
704    	KOBJMETHOD(channel_getptr,		sb16chan_getptr),
705    	KOBJMETHOD(channel_getcaps,		sb16chan_getcaps),
706	{ 0, 0 }
707};
708CHANNEL_DECLARE(sb16chan);
709
710/************************************************************/
711
712static int
713sb16_probe(device_t dev)
714{
715    	char buf[64];
716	uintptr_t func, ver, r, f;
717
718	/* The parent device has already been probed. */
719	r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
720	if (func != SCF_PCM)
721		return (ENXIO);
722
723	r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
724	f = (ver & 0xffff0000) >> 16;
725	ver &= 0x0000ffff;
726	if (f & BD_F_SB16) {
727		snprintf(buf, sizeof buf, "SB16 DSP %d.%02d%s", (int) ver >> 8, (int) ver & 0xff,
728			 (f & BD_F_SB16X)? " (ViBRA16X)" : "");
729    		device_set_desc_copy(dev, buf);
730		return 0;
731	} else
732		return (ENXIO);
733}
734
735static int
736sb16_attach(device_t dev)
737{
738    	struct sb_info *sb;
739	uintptr_t ver;
740    	char status[SND_STATUSLEN];
741	int bs = SB16_BUFFSIZE;
742
743    	sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT);
744    	if (!sb)
745		return ENXIO;
746    	bzero(sb, sizeof *sb);
747
748	BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
749	sb->bd_id = ver & 0x0000ffff;
750	sb->bd_flags = (ver & 0xffff0000) >> 16;
751
752    	if (sb16_alloc_resources(sb, dev))
753		goto no;
754    	if (sb_reset_dsp(sb))
755		goto no;
756	if (mixer_init(dev, &sb16mix_mixer_class, sb))
757		goto no;
758	if (bus_setup_intr(dev, sb->irq, INTR_TYPE_TTY, sb_intr, sb, &sb->ih))
759		goto no;
760
761	if (sb->bd_flags & BD_F_SB16X)
762		pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
763
764	sb->prio = 0;
765
766    	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
767			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
768			/*highaddr*/BUS_SPACE_MAXADDR,
769			/*filter*/NULL, /*filterarg*/NULL,
770			/*maxsize*/bs, /*nsegments*/1,
771			/*maxsegz*/0x3ffff,
772			/*flags*/0, &sb->parent_dmat) != 0) {
773		device_printf(dev, "unable to create dma tag\n");
774		goto no;
775    	}
776
777    	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld",
778    	     	rman_get_start(sb->io_base), rman_get_start(sb->irq),
779		rman_get_start(sb->drq1));
780    	if (!(pcm_getflags(dev) & SD_F_SIMPLEX))
781		snprintf(status + strlen(status), SND_STATUSLEN - strlen(status),
782			":%ld", rman_get_start(sb->drq2));
783
784    	if (pcm_register(dev, sb, 1, 1))
785		goto no;
786	pcm_addchan(dev, PCMDIR_REC, &sb16chan_class, sb);
787	pcm_addchan(dev, PCMDIR_PLAY, &sb16chan_class, sb);
788
789    	pcm_setstatus(dev, status);
790
791    	return 0;
792
793no:
794    	sb16_release_resources(sb, dev);
795    	return ENXIO;
796}
797
798static int
799sb16_detach(device_t dev)
800{
801	int r;
802	struct sb_info *sb;
803
804	r = pcm_unregister(dev);
805	if (r)
806		return r;
807
808	sb = pcm_getdevinfo(dev);
809    	sb16_release_resources(sb, dev);
810	return 0;
811}
812
813static device_method_t sb16_methods[] = {
814	/* Device interface */
815	DEVMETHOD(device_probe,		sb16_probe),
816	DEVMETHOD(device_attach,	sb16_attach),
817	DEVMETHOD(device_detach,	sb16_detach),
818
819	{ 0, 0 }
820};
821
822static driver_t sb16_driver = {
823	"pcm",
824	sb16_methods,
825	sizeof(snddev_info),
826};
827
828DRIVER_MODULE(snd_sb16, sbc, sb16_driver, pcm_devclass, 0, 0);
829MODULE_DEPEND(snd_sb16, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
830MODULE_VERSION(snd_sb16, 1);
831
832
833
834
835