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