sb16.c revision 70134
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 70134 2000-12-18 01:36:41Z 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 dl, dh, 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->drq1) {
374		bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq1);
375		sb->drq1 = 0;
376    	}
377    	if (sb->drq2) {
378		bus_release_resource(dev, SYS_RES_DRQ, 1, sb->drq2);
379		sb->drq2 = 0;
380    	}
381    	if (sb->io_base) {
382		bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base);
383		sb->io_base = 0;
384    	}
385    	if (sb->parent_dmat) {
386		bus_dma_tag_destroy(sb->parent_dmat);
387		sb->parent_dmat = 0;
388    	}
389     	free(sb, M_DEVBUF);
390}
391
392static int
393sb16_alloc_resources(struct sb_info *sb, device_t dev)
394{
395	int rid;
396
397	rid = 0;
398	if (!sb->io_base)
399    		sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
400
401	rid = 0;
402	if (!sb->irq)
403    		sb->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
404
405	rid = 0;
406	if (!sb->drq1)
407    		sb->drq1 = bus_alloc_resource(dev, SYS_RES_DRQ, &rid, 0, ~0, 1, RF_ACTIVE);
408
409	rid = 1;
410	if (!sb->drq2)
411        	sb->drq2 = bus_alloc_resource(dev, SYS_RES_DRQ, &rid, 0, ~0, 1, RF_ACTIVE);
412
413    	if (sb->io_base && sb->drq1 && sb->irq) {
414		int bs = SB16_BUFFSIZE;
415
416		isa_dma_acquire(rman_get_start(sb->drq1));
417		isa_dmainit(rman_get_start(sb->drq1), bs);
418
419		if (sb->drq2) {
420			isa_dma_acquire(rman_get_start(sb->drq2));
421			isa_dmainit(rman_get_start(sb->drq2), bs);
422		}
423		return 0;
424	} else return ENXIO;
425}
426
427static void
428sb_intr(void *arg)
429{
430    	struct sb_info *sb = (struct sb_info *)arg;
431    	int reason = 3, c;
432
433    	/*
434     	 * The Vibra16X has separate flags for 8 and 16 bit transfers, but
435     	 * I have no idea how to tell capture from playback interrupts...
436     	 */
437
438	reason = 0;
439    	c = sb_getmixer(sb, IRQ_STAT);
440
441	/*
442	 * this tells us if the source is 8-bit or 16-bit dma. We
443     	 * have to check the io channel to map it to read or write...
444     	 */
445
446	if (sb->bd_flags & BD_F_SB16X) {
447    		if (c & 1) { /* 8-bit format */
448			if (sb->pch.fmt & AFMT_8BIT)
449				reason |= 1;
450			if (sb->rch.fmt & AFMT_8BIT)
451				reason |= 2;
452    		}
453    		if (c & 2) { /* 16-bit format */
454			if (sb->pch.fmt & AFMT_16BIT)
455				reason |= 1;
456			if (sb->rch.fmt & AFMT_16BIT)
457				reason |= 2;
458    		}
459	} else {
460    		if (c & 1) { /* 8-bit dma */
461			if (sb->pch.dch == sb->dl)
462				reason |= 1;
463			if (sb->rch.dch == sb->dl)
464				reason |= 2;
465    		}
466    		if (c & 2) { /* 16-bit dma */
467			if (sb->pch.dch == sb->dh)
468				reason |= 1;
469			if (sb->rch.dch == sb->dh)
470				reason |= 2;
471    		}
472	}
473#if 0
474    	printf("sb_intr: reason=%d c=0x%x\n", reason, c);
475#endif
476    	if ((reason & 1) && (sb->pch.run))
477		chn_intr(sb->pch.channel);
478
479    	if ((reason & 2) && (sb->rch.run))
480		chn_intr(sb->rch.channel);
481
482    	if (c & 1)
483		sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */
484
485    	if (c & 2)
486		sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */
487}
488
489static int
490sb_setup(struct sb_info *sb)
491{
492	struct sb_chinfo *ch;
493	u_int8_t v;
494	int l, pprio;
495
496	if (sb->bd_flags & BD_F_DMARUN)
497		buf_isadma(sb->pch.buffer, PCMTRIG_STOP);
498	if (sb->bd_flags & BD_F_DMARUN2)
499		buf_isadma(sb->rch.buffer, PCMTRIG_STOP);
500	sb->bd_flags &= ~(BD_F_DMARUN | BD_F_DMARUN2);
501
502	sb_reset_dsp(sb);
503
504	if (sb->bd_flags & BD_F_SB16X) {
505		pprio = sb->pch.run? 1 : 0;
506		sb->pch.buffer->chan = pprio? sb->dl : -1;
507		sb->rch.buffer->chan = pprio? sb->dh : sb->dl;
508	} else {
509		if (sb->pch.run && sb->rch.run) {
510			pprio = (sb->rch.fmt & AFMT_16BIT)? 0 : 1;
511			sb->pch.buffer->chan = pprio? sb->dh : sb->dl;
512			sb->rch.buffer->chan = pprio? sb->dl : sb->dh;
513		} else {
514			if (sb->pch.run) {
515				sb->pch.buffer->chan = (sb->pch.fmt & AFMT_16BIT)? sb->dh : sb->dl;
516				sb->rch.buffer->chan = (sb->pch.fmt & AFMT_16BIT)? sb->dl : sb->dh;
517			} else if (sb->rch.run) {
518				sb->pch.buffer->chan = (sb->rch.fmt & AFMT_16BIT)? sb->dl : sb->dh;
519				sb->rch.buffer->chan = (sb->rch.fmt & AFMT_16BIT)? sb->dh : sb->dl;
520			}
521		}
522	}
523
524	sb->pch.dch = sb->pch.buffer->chan;
525	sb->rch.dch = sb->rch.buffer->chan;
526
527	sb->pch.buffer->dir = ISADMA_WRITE;
528	sb->rch.buffer->dir = ISADMA_READ;
529
530	/*
531	printf("setup: [pch = %d, pfmt = %d, pgo = %d] [rch = %d, rfmt = %d, rgo = %d]\n",
532	       sb->pch.dch, sb->pch.fmt, sb->pch.run, sb->rch.dch, sb->rch.fmt, sb->rch.run);
533	*/
534
535	ch = &sb->pch;
536	if (ch->run) {
537		l = ch->blksz;
538		if (ch->fmt & AFMT_16BIT)
539			l >>= 1;
540		l--;
541
542		/* play speed */
543		RANGE(ch->spd, 5000, 45000);
544		sb_cmd(sb, DSP_CMD_OUT16);
545    		sb_cmd(sb, ch->spd >> 8);
546		sb_cmd(sb, ch->spd & 0xff);
547
548		/* play format, length */
549		v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_DAC;
550		v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8;
551		sb_cmd(sb, v);
552
553		v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0;
554		v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0;
555		sb_cmd2(sb, v, l);
556		buf_isadma(ch->buffer, PCMTRIG_START);
557		sb->bd_flags |= BD_F_DMARUN;
558	}
559
560	ch = &sb->rch;
561	if (ch->run) {
562		l = ch->blksz;
563		if (ch->fmt & AFMT_16BIT)
564			l >>= 1;
565		l--;
566
567		/* record speed */
568		RANGE(ch->spd, 5000, 45000);
569		sb_cmd(sb, DSP_CMD_IN16);
570    		sb_cmd(sb, ch->spd >> 8);
571		sb_cmd(sb, ch->spd & 0xff);
572
573		/* record format, length */
574		v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_ADC;
575		v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8;
576		sb_cmd(sb, v);
577
578		v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0;
579		v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0;
580		sb_cmd2(sb, v, l);
581		buf_isadma(ch->buffer, PCMTRIG_START);
582		sb->bd_flags |= BD_F_DMARUN2;
583	}
584
585    	return 0;
586}
587
588/* channel interface */
589static void *
590sb16chan_init(kobj_t obj, void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
591{
592	struct sb_info *sb = devinfo;
593	struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
594
595	ch->parent = sb;
596	ch->channel = c;
597	ch->buffer = b;
598	ch->buffer->bufsize = SB16_BUFFSIZE;
599	ch->dir = dir;
600
601	if (chn_allocbuf(ch->buffer, sb->parent_dmat) == -1)
602		return NULL;
603
604	return ch;
605}
606
607static int
608sb16chan_setformat(kobj_t obj, void *data, u_int32_t format)
609{
610	struct sb_chinfo *ch = data;
611	struct sb_info *sb = ch->parent;
612
613	ch->fmt = format;
614	sb->prio = ch->dir;
615	sb->prio16 = (ch->fmt & AFMT_16BIT)? 1 : 0;
616
617	return 0;
618}
619
620static int
621sb16chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
622{
623	struct sb_chinfo *ch = data;
624
625	ch->spd = speed;
626	return speed;
627}
628
629static int
630sb16chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
631{
632	struct sb_chinfo *ch = data;
633
634	ch->blksz = blocksize;
635	return blocksize;
636}
637
638static int
639sb16chan_trigger(kobj_t obj, void *data, int go)
640{
641	struct sb_chinfo *ch = data;
642	struct sb_info *sb = ch->parent;
643
644	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
645		return 0;
646
647	if (go == PCMTRIG_START)
648		ch->run = 1;
649	else
650		ch->run = 0;
651
652	sb_setup(sb);
653
654	return 0;
655}
656
657static int
658sb16chan_getptr(kobj_t obj, void *data)
659{
660	struct sb_chinfo *ch = data;
661
662	return buf_isadmaptr(ch->buffer);
663}
664
665static pcmchan_caps *
666sb16chan_getcaps(kobj_t obj, void *data)
667{
668	struct sb_chinfo *ch = data;
669	struct sb_info *sb = ch->parent;
670
671	if ((sb->prio == 0) || (sb->prio == ch->dir))
672		return &sb16x_caps;
673	else
674		return sb->prio16? &sb16_caps8 : &sb16_caps16;
675}
676
677static int
678sb16chan_resetdone(kobj_t obj, void *data)
679{
680	struct sb_chinfo *ch = data;
681	struct sb_info *sb = ch->parent;
682
683	sb->prio = 0;
684
685	return 0;
686}
687
688static kobj_method_t sb16chan_methods[] = {
689    	KOBJMETHOD(channel_init,		sb16chan_init),
690    	KOBJMETHOD(channel_resetdone,		sb16chan_resetdone),
691    	KOBJMETHOD(channel_setformat,		sb16chan_setformat),
692    	KOBJMETHOD(channel_setspeed,		sb16chan_setspeed),
693    	KOBJMETHOD(channel_setblocksize,	sb16chan_setblocksize),
694    	KOBJMETHOD(channel_trigger,		sb16chan_trigger),
695    	KOBJMETHOD(channel_getptr,		sb16chan_getptr),
696    	KOBJMETHOD(channel_getcaps,		sb16chan_getcaps),
697	{ 0, 0 }
698};
699CHANNEL_DECLARE(sb16chan);
700
701/************************************************************/
702
703static int
704sb16_probe(device_t dev)
705{
706    	char buf[64];
707	uintptr_t func, ver, r, f;
708
709	/* The parent device has already been probed. */
710	r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
711	if (func != SCF_PCM)
712		return (ENXIO);
713
714	r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
715	f = (ver & 0xffff0000) >> 16;
716	ver &= 0x0000ffff;
717	if (f & BD_F_SB16) {
718		snprintf(buf, sizeof buf, "SB16 DSP %d.%02d%s", (int) ver >> 8, (int) ver & 0xff,
719			 (f & BD_F_SB16X)? " (ViBRA16X)" : "");
720    		device_set_desc_copy(dev, buf);
721		return 0;
722	} else
723		return (ENXIO);
724}
725
726static int
727sb16_attach(device_t dev)
728{
729    	struct sb_info *sb;
730	uintptr_t ver;
731    	char status[SND_STATUSLEN];
732	int bs = SB16_BUFFSIZE;
733
734    	sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT);
735    	if (!sb)
736		return ENXIO;
737    	bzero(sb, sizeof *sb);
738
739	BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
740	sb->bd_id = ver & 0x0000ffff;
741	sb->bd_flags = (ver & 0xffff0000) >> 16;
742
743    	if (sb16_alloc_resources(sb, dev))
744		goto no;
745    	if (sb_reset_dsp(sb))
746		goto no;
747	if (mixer_init(dev, &sb16mix_mixer_class, sb))
748		goto no;
749	if (bus_setup_intr(dev, sb->irq, INTR_TYPE_TTY, sb_intr, sb, &sb->ih))
750		goto no;
751
752	if (!sb->drq2 || (sb->bd_flags & BD_F_SB16X))
753		pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
754
755	sb->dl = rman_get_start(sb->drq1);
756	sb->dh = sb->drq2? rman_get_start(sb->drq2) : sb->dl;
757	sb->prio = 0;
758
759    	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
760			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
761			/*highaddr*/BUS_SPACE_MAXADDR,
762			/*filter*/NULL, /*filterarg*/NULL,
763			/*maxsize*/bs, /*nsegments*/1,
764			/*maxsegz*/0x3ffff,
765			/*flags*/0, &sb->parent_dmat) != 0) {
766		device_printf(dev, "unable to create dma tag\n");
767		goto no;
768    	}
769
770    	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld",
771    	     	rman_get_start(sb->io_base), rman_get_start(sb->irq),
772		rman_get_start(sb->drq1));
773    	if (sb->drq2)
774		snprintf(status + strlen(status), SND_STATUSLEN - strlen(status),
775			":%ld", rman_get_start(sb->drq2));
776
777    	if (pcm_register(dev, sb, 1, 1))
778		goto no;
779	pcm_addchan(dev, PCMDIR_REC, &sb16chan_class, sb);
780	pcm_addchan(dev, PCMDIR_PLAY, &sb16chan_class, sb);
781
782    	pcm_setstatus(dev, status);
783
784    	return 0;
785
786no:
787    	sb16_release_resources(sb, dev);
788    	return ENXIO;
789}
790
791static int
792sb16_detach(device_t dev)
793{
794	int r;
795	struct sb_info *sb;
796
797	r = pcm_unregister(dev);
798	if (r)
799		return r;
800
801	sb = pcm_getdevinfo(dev);
802    	sb16_release_resources(sb, dev);
803	return 0;
804}
805
806static device_method_t sb16_methods[] = {
807	/* Device interface */
808	DEVMETHOD(device_probe,		sb16_probe),
809	DEVMETHOD(device_attach,	sb16_attach),
810	DEVMETHOD(device_detach,	sb16_detach),
811
812	{ 0, 0 }
813};
814
815static driver_t sb16_driver = {
816	"pcm",
817	sb16_methods,
818	sizeof(snddev_info),
819};
820
821DRIVER_MODULE(snd_sb16, sbc, sb16_driver, pcm_devclass, 0, 0);
822MODULE_DEPEND(snd_sb16, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
823MODULE_VERSION(snd_sb16, 1);
824
825
826
827
828