sb16.c revision 108064
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 108064 2002-12-18 22:53:24Z semenu $");
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	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	[SOUND_MIXER_LINE1]	= { 0x52, 5, 3, 1 }
303};
304
305static int
306sb16mix_init(struct snd_mixer *m)
307{
308    	struct sb_info *sb = mix_getdevinfo(m);
309
310	mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
311     		       SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD |
312     		       SOUND_MASK_IGAIN | SOUND_MASK_OGAIN | SOUND_MASK_LINE1 |
313     		       SOUND_MASK_VOLUME | SOUND_MASK_BASS | SOUND_MASK_TREBLE);
314
315	mix_setrecdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_LINE |
316			  SOUND_MASK_LINE1 | SOUND_MASK_MIC | SOUND_MASK_CD);
317
318	sb_setmixer(sb, 0x3c, 0x1f); /* make all output active */
319
320	sb_setmixer(sb, 0x3d, 0); /* make all inputs-l off */
321	sb_setmixer(sb, 0x3e, 0); /* make all inputs-r off */
322
323	return 0;
324}
325
326static int
327sb16mix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
328{
329    	struct sb_info *sb = mix_getdevinfo(m);
330    	const struct sb16_mixent *e;
331    	int max;
332
333	e = &sb16_mixtab[dev];
334	max = (1 << e->bits) - 1;
335
336	left = (left * max) / 100;
337	right = (right * max) / 100;
338
339	sb_setmixer(sb, e->reg, left << e->ofs);
340	if (e->stereo)
341		sb_setmixer(sb, e->reg + 1, right << e->ofs);
342	else
343		right = left;
344
345	left = (left * 100) / max;
346	right = (right * 100) / max;
347
348    	return left | (right << 8);
349}
350
351static int
352sb16mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
353{
354    	struct sb_info *sb = mix_getdevinfo(m);
355    	u_char recdev;
356
357	recdev = 0;
358	if (src & SOUND_MASK_MIC)
359		recdev |= 0x01; /* mono mic */
360
361	if (src & SOUND_MASK_CD)
362		recdev |= 0x06; /* l+r cd */
363
364	if (src & SOUND_MASK_LINE)
365		recdev |= 0x18; /* l+r line */
366
367	if (src & SOUND_MASK_SYNTH)
368		recdev |= 0x60; /* l+r midi */
369
370	sb_setmixer(sb, SB16_IMASK_L, recdev);
371	sb_setmixer(sb, SB16_IMASK_R, recdev);
372
373	/* Switch on/off FM tuner source */
374	if (src & SOUND_MASK_LINE1)
375		sb_setmixer(sb, 0x4a, 0x0c);
376	else
377		sb_setmixer(sb, 0x4a, 0x00);
378
379	/*
380	 * since the same volume controls apply to the input and
381	 * output sections, the best approach to have a consistent
382	 * behaviour among cards would be to disable the output path
383	 * on devices which are used to record.
384	 * However, since users like to have feedback, we only disable
385	 * the mic -- permanently.
386	 */
387        sb_setmixer(sb, SB16_OMASK, 0x1f & ~1);
388
389	return src;
390}
391
392static kobj_method_t sb16mix_mixer_methods[] = {
393    	KOBJMETHOD(mixer_init,		sb16mix_init),
394    	KOBJMETHOD(mixer_set,		sb16mix_set),
395    	KOBJMETHOD(mixer_setrecsrc,	sb16mix_setrecsrc),
396	{ 0, 0 }
397};
398MIXER_DECLARE(sb16mix_mixer);
399
400/************************************************************/
401
402static void
403sb16_release_resources(struct sb_info *sb, device_t dev)
404{
405    	if (sb->irq) {
406    		if (sb->ih)
407			bus_teardown_intr(dev, sb->irq, sb->ih);
408 		bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq);
409		sb->irq = 0;
410    	}
411    	if (sb->drq2) {
412		if (sb->drq2 != sb->drq1) {
413			isa_dma_release(rman_get_start(sb->drq2));
414			bus_release_resource(dev, SYS_RES_DRQ, 1, sb->drq2);
415		}
416		sb->drq2 = 0;
417    	}
418     	if (sb->drq1) {
419		isa_dma_release(rman_get_start(sb->drq1));
420		bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq1);
421		sb->drq1 = 0;
422    	}
423   	if (sb->io_base) {
424		bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base);
425		sb->io_base = 0;
426    	}
427    	if (sb->parent_dmat) {
428		bus_dma_tag_destroy(sb->parent_dmat);
429		sb->parent_dmat = 0;
430    	}
431     	free(sb, M_DEVBUF);
432}
433
434static int
435sb16_alloc_resources(struct sb_info *sb, device_t dev)
436{
437	int rid;
438
439	rid = 0;
440	if (!sb->io_base)
441    		sb->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
442
443	rid = 0;
444	if (!sb->irq)
445    		sb->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
446
447	rid = 0;
448	if (!sb->drq1)
449    		sb->drq1 = bus_alloc_resource(dev, SYS_RES_DRQ, &rid, 0, ~0, 1, RF_ACTIVE);
450
451	rid = 1;
452	if (!sb->drq2)
453        	sb->drq2 = bus_alloc_resource(dev, SYS_RES_DRQ, &rid, 0, ~0, 1, RF_ACTIVE);
454
455    	if (sb->io_base && sb->drq1 && sb->irq) {
456		isa_dma_acquire(rman_get_start(sb->drq1));
457		isa_dmainit(rman_get_start(sb->drq1), sb->bufsize);
458
459		if (sb->drq2) {
460			isa_dma_acquire(rman_get_start(sb->drq2));
461			isa_dmainit(rman_get_start(sb->drq2), sb->bufsize);
462		} else {
463			sb->drq2 = sb->drq1;
464			pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
465		}
466		return 0;
467	} else return ENXIO;
468}
469
470/* sbc does locking for us */
471static void
472sb_intr(void *arg)
473{
474    	struct sb_info *sb = (struct sb_info *)arg;
475    	int reason = 3, c;
476
477    	/*
478     	 * The Vibra16X has separate flags for 8 and 16 bit transfers, but
479     	 * I have no idea how to tell capture from playback interrupts...
480     	 */
481
482	reason = 0;
483	sb_lock(sb);
484    	c = sb_getmixer(sb, IRQ_STAT);
485    	if (c & 1)
486		sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */
487
488    	if (c & 2)
489		sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */
490	sb_unlock(sb);
491
492	/*
493	 * this tells us if the source is 8-bit or 16-bit dma. We
494     	 * have to check the io channel to map it to read or write...
495     	 */
496
497	if (sb->bd_flags & BD_F_SB16X) {
498    		if (c & 1) { /* 8-bit format */
499			if (sb->pch.fmt & AFMT_8BIT)
500				reason |= 1;
501			if (sb->rch.fmt & AFMT_8BIT)
502				reason |= 2;
503    		}
504    		if (c & 2) { /* 16-bit format */
505			if (sb->pch.fmt & AFMT_16BIT)
506				reason |= 1;
507			if (sb->rch.fmt & AFMT_16BIT)
508				reason |= 2;
509    		}
510	} else {
511    		if (c & 1) { /* 8-bit dma */
512			if (sb->pch.dch == 1)
513				reason |= 1;
514			if (sb->rch.dch == 1)
515				reason |= 2;
516    		}
517    		if (c & 2) { /* 16-bit dma */
518			if (sb->pch.dch == 2)
519				reason |= 1;
520			if (sb->rch.dch == 2)
521				reason |= 2;
522    		}
523	}
524#if 0
525    	printf("sb_intr: reason=%d c=0x%x\n", reason, c);
526#endif
527    	if ((reason & 1) && (sb->pch.run))
528		chn_intr(sb->pch.channel);
529
530    	if ((reason & 2) && (sb->rch.run))
531		chn_intr(sb->rch.channel);
532}
533
534static int
535sb_setup(struct sb_info *sb)
536{
537	struct sb_chinfo *ch;
538	u_int8_t v;
539	int l, pprio;
540
541	sb_lock(sb);
542	if (sb->bd_flags & BD_F_DMARUN)
543		sndbuf_isadma(sb->pch.buffer, PCMTRIG_STOP);
544	if (sb->bd_flags & BD_F_DMARUN2)
545		sndbuf_isadma(sb->rch.buffer, PCMTRIG_STOP);
546	sb->bd_flags &= ~(BD_F_DMARUN | BD_F_DMARUN2);
547
548	sb_reset_dsp(sb);
549
550	if (sb->bd_flags & BD_F_SB16X) {
551		pprio = sb->pch.run? 1 : 0;
552		sndbuf_isadmasetup(sb->pch.buffer, pprio? sb->drq1 : NULL);
553		sb->pch.dch = pprio? 1 : 0;
554		sndbuf_isadmasetup(sb->rch.buffer, pprio? sb->drq2 : sb->drq1);
555		sb->rch.dch = pprio? 2 : 1;
556	} else {
557		if (sb->pch.run && sb->rch.run) {
558			pprio = (sb->rch.fmt & AFMT_16BIT)? 0 : 1;
559			sndbuf_isadmasetup(sb->pch.buffer, pprio? sb->drq2 : sb->drq1);
560			sb->pch.dch = pprio? 2 : 1;
561			sndbuf_isadmasetup(sb->rch.buffer, pprio? sb->drq1 : sb->drq2);
562			sb->rch.dch = pprio? 1 : 2;
563		} else {
564			if (sb->pch.run) {
565				sndbuf_isadmasetup(sb->pch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1);
566				sb->pch.dch = (sb->pch.fmt & AFMT_16BIT)? 2 : 1;
567				sndbuf_isadmasetup(sb->rch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2);
568				sb->rch.dch = (sb->pch.fmt & AFMT_16BIT)? 1 : 2;
569			} else if (sb->rch.run) {
570				sndbuf_isadmasetup(sb->pch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2);
571				sb->pch.dch = (sb->rch.fmt & AFMT_16BIT)? 1 : 2;
572				sndbuf_isadmasetup(sb->rch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1);
573				sb->rch.dch = (sb->rch.fmt & AFMT_16BIT)? 2 : 1;
574			}
575		}
576	}
577
578	sndbuf_isadmasetdir(sb->pch.buffer, PCMDIR_PLAY);
579	sndbuf_isadmasetdir(sb->rch.buffer, PCMDIR_REC);
580
581	/*
582	printf("setup: [pch = %d, pfmt = %d, pgo = %d] [rch = %d, rfmt = %d, rgo = %d]\n",
583	       sb->pch.dch, sb->pch.fmt, sb->pch.run, sb->rch.dch, sb->rch.fmt, sb->rch.run);
584	*/
585
586	ch = &sb->pch;
587	if (ch->run) {
588		l = ch->blksz;
589		if (ch->fmt & AFMT_16BIT)
590			l >>= 1;
591		l--;
592
593		/* play speed */
594		RANGE(ch->spd, 5000, 45000);
595		sb_cmd(sb, DSP_CMD_OUT16);
596    		sb_cmd(sb, ch->spd >> 8);
597		sb_cmd(sb, ch->spd & 0xff);
598
599		/* play format, length */
600		v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_DAC;
601		v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8;
602		sb_cmd(sb, v);
603
604		v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0;
605		v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0;
606		sb_cmd2(sb, v, l);
607		sndbuf_isadma(ch->buffer, PCMTRIG_START);
608		sb->bd_flags |= BD_F_DMARUN;
609	}
610
611	ch = &sb->rch;
612	if (ch->run) {
613		l = ch->blksz;
614		if (ch->fmt & AFMT_16BIT)
615			l >>= 1;
616		l--;
617
618		/* record speed */
619		RANGE(ch->spd, 5000, 45000);
620		sb_cmd(sb, DSP_CMD_IN16);
621    		sb_cmd(sb, ch->spd >> 8);
622		sb_cmd(sb, ch->spd & 0xff);
623
624		/* record format, length */
625		v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_ADC;
626		v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8;
627		sb_cmd(sb, v);
628
629		v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0;
630		v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0;
631		sb_cmd2(sb, v, l);
632		sndbuf_isadma(ch->buffer, PCMTRIG_START);
633		sb->bd_flags |= BD_F_DMARUN2;
634	}
635	sb_unlock(sb);
636
637    	return 0;
638}
639
640/* channel interface */
641static void *
642sb16chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
643{
644	struct sb_info *sb = devinfo;
645	struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
646
647	ch->parent = sb;
648	ch->channel = c;
649	ch->buffer = b;
650	ch->dir = dir;
651
652	if (sndbuf_alloc(ch->buffer, sb->parent_dmat, sb->bufsize) == -1)
653		return NULL;
654
655	return ch;
656}
657
658static int
659sb16chan_setformat(kobj_t obj, void *data, u_int32_t format)
660{
661	struct sb_chinfo *ch = data;
662	struct sb_info *sb = ch->parent;
663
664	ch->fmt = format;
665	sb->prio = ch->dir;
666	sb->prio16 = (ch->fmt & AFMT_16BIT)? 1 : 0;
667
668	return 0;
669}
670
671static int
672sb16chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
673{
674	struct sb_chinfo *ch = data;
675
676	ch->spd = speed;
677	return speed;
678}
679
680static int
681sb16chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
682{
683	struct sb_chinfo *ch = data;
684
685	ch->blksz = blocksize;
686	return ch->blksz;
687}
688
689static int
690sb16chan_trigger(kobj_t obj, void *data, int go)
691{
692	struct sb_chinfo *ch = data;
693	struct sb_info *sb = ch->parent;
694
695	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
696		return 0;
697
698	if (go == PCMTRIG_START)
699		ch->run = 1;
700	else
701		ch->run = 0;
702
703	sb_setup(sb);
704
705	return 0;
706}
707
708static int
709sb16chan_getptr(kobj_t obj, void *data)
710{
711	struct sb_chinfo *ch = data;
712
713	return sndbuf_isadmaptr(ch->buffer);
714}
715
716static struct pcmchan_caps *
717sb16chan_getcaps(kobj_t obj, void *data)
718{
719	struct sb_chinfo *ch = data;
720	struct sb_info *sb = ch->parent;
721
722	if ((sb->prio == 0) || (sb->prio == ch->dir))
723		return &sb16x_caps;
724	else
725		return sb->prio16? &sb16_caps8 : &sb16_caps16;
726}
727
728static int
729sb16chan_resetdone(kobj_t obj, void *data)
730{
731	struct sb_chinfo *ch = data;
732	struct sb_info *sb = ch->parent;
733
734	sb->prio = 0;
735
736	return 0;
737}
738
739static kobj_method_t sb16chan_methods[] = {
740    	KOBJMETHOD(channel_init,		sb16chan_init),
741    	KOBJMETHOD(channel_resetdone,		sb16chan_resetdone),
742    	KOBJMETHOD(channel_setformat,		sb16chan_setformat),
743    	KOBJMETHOD(channel_setspeed,		sb16chan_setspeed),
744    	KOBJMETHOD(channel_setblocksize,	sb16chan_setblocksize),
745    	KOBJMETHOD(channel_trigger,		sb16chan_trigger),
746    	KOBJMETHOD(channel_getptr,		sb16chan_getptr),
747    	KOBJMETHOD(channel_getcaps,		sb16chan_getcaps),
748	{ 0, 0 }
749};
750CHANNEL_DECLARE(sb16chan);
751
752/************************************************************/
753
754static int
755sb16_probe(device_t dev)
756{
757    	char buf[64];
758	uintptr_t func, ver, r, f;
759
760	/* The parent device has already been probed. */
761	r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
762	if (func != SCF_PCM)
763		return (ENXIO);
764
765	r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
766	f = (ver & 0xffff0000) >> 16;
767	ver &= 0x0000ffff;
768	if (f & BD_F_SB16) {
769		snprintf(buf, sizeof buf, "SB16 DSP %d.%02d%s", (int) ver >> 8, (int) ver & 0xff,
770			 (f & BD_F_SB16X)? " (ViBRA16X)" : "");
771    		device_set_desc_copy(dev, buf);
772		return 0;
773	} else
774		return (ENXIO);
775}
776
777static int
778sb16_attach(device_t dev)
779{
780    	struct sb_info *sb;
781	uintptr_t ver;
782    	char status[SND_STATUSLEN], status2[SND_STATUSLEN];
783
784    	sb = (struct sb_info *)malloc(sizeof *sb, M_DEVBUF, M_NOWAIT | M_ZERO);
785    	if (!sb)
786		return ENXIO;
787
788	sb->parent_dev = device_get_parent(dev);
789	BUS_READ_IVAR(sb->parent_dev, dev, 1, &ver);
790	sb->bd_id = ver & 0x0000ffff;
791	sb->bd_flags = (ver & 0xffff0000) >> 16;
792	sb->bufsize = pcm_getbuffersize(dev, 4096, SB16_BUFFSIZE, 65536);
793
794    	if (sb16_alloc_resources(sb, dev))
795		goto no;
796    	if (sb_reset_dsp(sb))
797		goto no;
798	if (mixer_init(dev, &sb16mix_mixer_class, sb))
799		goto no;
800	if (snd_setup_intr(dev, sb->irq, INTR_MPSAFE, sb_intr, sb, &sb->ih))
801		goto no;
802
803	if (sb->bd_flags & BD_F_SB16X)
804		pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
805
806	sb->prio = 0;
807
808    	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
809			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
810			/*highaddr*/BUS_SPACE_MAXADDR,
811			/*filter*/NULL, /*filterarg*/NULL,
812			/*maxsize*/sb->bufsize, /*nsegments*/1,
813			/*maxsegz*/0x3ffff,
814			/*flags*/0, &sb->parent_dmat) != 0) {
815		device_printf(dev, "unable to create dma tag\n");
816		goto no;
817    	}
818
819    	if (!(pcm_getflags(dev) & SD_F_SIMPLEX))
820		snprintf(status2, SND_STATUSLEN, ":%ld", rman_get_start(sb->drq2));
821	else
822		status2[0] = '\0';
823
824    	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld%s bufsz %ud",
825    	     	rman_get_start(sb->io_base), rman_get_start(sb->irq),
826		rman_get_start(sb->drq1), status2, sb->bufsize);
827
828    	if (pcm_register(dev, sb, 1, 1))
829		goto no;
830	pcm_addchan(dev, PCMDIR_REC, &sb16chan_class, sb);
831	pcm_addchan(dev, PCMDIR_PLAY, &sb16chan_class, sb);
832
833    	pcm_setstatus(dev, status);
834
835    	return 0;
836
837no:
838    	sb16_release_resources(sb, dev);
839    	return ENXIO;
840}
841
842static int
843sb16_detach(device_t dev)
844{
845	int r;
846	struct sb_info *sb;
847
848	r = pcm_unregister(dev);
849	if (r)
850		return r;
851
852	sb = pcm_getdevinfo(dev);
853    	sb16_release_resources(sb, dev);
854	return 0;
855}
856
857static device_method_t sb16_methods[] = {
858	/* Device interface */
859	DEVMETHOD(device_probe,		sb16_probe),
860	DEVMETHOD(device_attach,	sb16_attach),
861	DEVMETHOD(device_detach,	sb16_detach),
862
863	{ 0, 0 }
864};
865
866static driver_t sb16_driver = {
867	"pcm",
868	sb16_methods,
869	PCM_SOFTC_SIZE,
870};
871
872DRIVER_MODULE(snd_sb16, sbc, sb16_driver, pcm_devclass, 0, 0);
873MODULE_DEPEND(snd_sb16, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
874MODULE_DEPEND(snd_sb16, snd_sbc, 1, 1, 1);
875MODULE_VERSION(snd_sb16, 1);
876