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