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