xref: /netbsd-src/sys/dev/isa/aria.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: aria.c,v 1.21 2004/10/29 12:57:17 yamt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1996, 1998 Roland C. Dowdeswell.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Roland C. Dowdeswell.
17  * 4. The name of the authors may not be used to endorse or promote products
18  *      derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * TODO:
34  *  o   Test the driver on cards other than a single
35  *      Prometheus Aria 16.
36  *  o   Look into where aria_prometheus_kludge() belongs.
37  *  o   Add some DMA code.  It accomplishes its goal by
38  *      direct IO at the moment.
39  *  o   Different programs should be able to open the device
40  *      with O_RDONLY and O_WRONLY at the same time.  But I
41  *      do not see support for this in /sys/dev/audio.c, so
42  *	I cannot effectively code it.
43  *  o   We should nicely deal with the cards that can do mu-law
44  *      and A-law output.
45  *  o   Rework the mixer interface.
46  *       o   Deal with the lvls better.  We need to do better mapping
47  *           between logarithmic scales and the one byte that
48  *           we are passed.
49  *       o   Deal better with cards that have no mixer.
50  */
51 
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: aria.c,v 1.21 2004/10/29 12:57:17 yamt Exp $");
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/errno.h>
58 #include <sys/ioctl.h>
59 #include <sys/syslog.h>
60 #include <sys/device.h>
61 #include <sys/proc.h>
62 #include <sys/buf.h>
63 #include <sys/fcntl.h>
64 
65 #include <machine/cpu.h>
66 #include <machine/bus.h>
67 
68 #include <sys/audioio.h>
69 #include <dev/audio_if.h>
70 #include <dev/auconv.h>
71 
72 #include <dev/mulaw.h>
73 #include <dev/isa/isavar.h>
74 
75 #include <dev/isa/ariareg.h>
76 
77 #ifdef AUDIO_DEBUG
78 #define DPRINTF(x)	printf x
79 int	ariadebug = 0;
80 #else
81 #define DPRINTF(x)
82 #endif
83 
84 struct aria_mixdev_info {
85 	u_char	num_channels;
86 	u_char	level[2];
87 	u_char	mute;
88 };
89 
90 struct aria_mixmaster {
91 	u_char num_channels;
92 	u_char level[2];
93 	u_char treble[2];
94 	u_char bass[2];
95 };
96 
97 struct aria_softc {
98 	struct	device sc_dev;		/* base device */
99 	void	*sc_ih;			/* interrupt vectoring */
100 	bus_space_tag_t sc_iot;		/* Tag on 'da bus. */
101 	bus_space_handle_t sc_ioh;	/* Handle of iospace */
102 	isa_chipset_tag_t sc_ic;	/* ISA chipset info */
103 
104 	u_short	sc_open;		/* reference count of open calls */
105 	u_short sc_play;		/* non-paused play chans 2**chan */
106 	u_short sc_record;		/* non-paused record chans 2**chan */
107 /* XXX -- keep this? */
108 	u_short sc_gain[2];		/* left/right gain (play) */
109 
110 	u_long	sc_rate;		/* Sample rate for input and output */
111 	u_int	sc_encoding;		/* audio encoding -- mu-law/linear */
112 	int	sc_chans;		/* # of channels */
113 	int	sc_precision;		/* # bits per sample */
114 
115 	u_long	sc_interrupts;		/* number of interrupts taken */
116 	void	(*sc_rintr)(void*);	/* record transfer completion intr handler */
117 	void	(*sc_pintr)(void*);	/* play transfer completion intr handler */
118 	void	*sc_rarg;		/* arg for sc_rintr() */
119 	void	*sc_parg;		/* arg for sc_pintr() */
120 
121 	int	sc_blocksize;		/* literal dio block size */
122 	void	*sc_rdiobuffer;		/* record: where the next samples should be */
123 	void	*sc_pdiobuffer;		/* play:   where the next samples are */
124 
125 	u_short sc_hardware;		/* bit field of hardware present */
126 #define ARIA_TELEPHONE	0x0001		/* has telephone input */
127 #define ARIA_MIXER	0x0002		/* has SC18075 digital mixer */
128 #define ARIA_MODEL	0x0004		/* is SC18025 (=0) or SC18026 (=1) */
129 
130 	struct aria_mixdev_info aria_mix[6];
131 	struct aria_mixmaster ariamix_master;
132 	u_char	aria_mix_source;
133 
134 	int	sc_sendcmd_err;
135 };
136 
137 int	ariaprobe __P((struct device *, struct cfdata *, void *));
138 void	ariaattach __P((struct device *, struct device *, void *));
139 void	ariaclose __P((void *));
140 int	ariaopen __P((void *, int));
141 int	ariareset __P((bus_space_tag_t, bus_space_handle_t));
142 int	aria_reset __P((struct aria_softc *));
143 int	aria_getdev __P((void *, struct audio_device *));
144 
145 void	aria_do_kludge __P((bus_space_tag_t, bus_space_handle_t,
146 			    bus_space_handle_t,
147 			    u_short, u_short, u_short, u_short));
148 void	aria_prometheus_kludge __P((struct isa_attach_args *,
149 				    bus_space_handle_t));
150 
151 int	aria_query_encoding __P((void *, struct audio_encoding *));
152 int	aria_round_blocksize __P((void *, int));
153 int	aria_speaker_ctl __P((void *, int));
154 int	aria_commit_settings __P((void *));
155 int	aria_set_params __P((void *, int, int,
156 			     struct audio_params *, struct audio_params *));
157 int	aria_get_props __P((void *));
158 
159 int	aria_start_output __P((void *, void *, int,
160 			       void (*) __P((void *)), void*));
161 int	aria_start_input __P((void *, void *, int,
162 			      void (*) __P((void *)), void*));
163 
164 int	aria_halt_input __P((void *));
165 int	aria_halt_output __P((void *));
166 
167 int	aria_sendcmd __P((struct aria_softc *, u_short, int, int, int));
168 
169 u_short	aria_getdspmem __P((struct aria_softc *, u_short));
170 void	aria_putdspmem __P((struct aria_softc *, u_short, u_short));
171 
172 int	aria_intr __P((void *));
173 short	ariaversion __P((struct aria_softc *));
174 
175 void	aria_set_mixer __P((struct aria_softc *, int));
176 
177 void	aria_mix_write __P((struct aria_softc *, int, int));
178 int	aria_mix_read __P((struct aria_softc *, int));
179 
180 int	aria_mixer_set_port __P((void *, mixer_ctrl_t *));
181 int	aria_mixer_get_port __P((void *, mixer_ctrl_t *));
182 int	aria_mixer_query_devinfo __P((void *, mixer_devinfo_t *));
183 
184 CFATTACH_DECL(aria, sizeof(struct aria_softc),
185     ariaprobe, ariaattach, NULL, NULL);
186 
187 /* XXX temporary test for 1.3 */
188 #ifndef AudioNaux
189 /* 1.3 */
190 struct cfdriver aria_cd = {
191 	NULL, "aria", DV_DULL
192 };
193 #endif
194 
195 struct audio_device aria_device = {
196 	"Aria 16(se)",
197 	"x",
198 	"aria"
199 };
200 
201 /*
202  * Define our interface to the higher level audio driver.
203  */
204 
205 const struct audio_hw_if aria_hw_if = {
206 	ariaopen,
207 	ariaclose,
208 	NULL,
209 	aria_query_encoding,
210 	aria_set_params,
211 	aria_round_blocksize,
212 	aria_commit_settings,
213 	NULL,
214 	NULL,
215 	aria_start_output,
216 	aria_start_input,
217 	aria_halt_input,
218 	aria_halt_output,
219 	NULL,
220 	aria_getdev,
221 	NULL,
222 	aria_mixer_set_port,
223 	aria_mixer_get_port,
224 	aria_mixer_query_devinfo,
225 	NULL,
226 	NULL,
227 	NULL,
228 	NULL,
229 	aria_get_props,
230 	NULL,
231 	NULL,
232 	NULL,
233 };
234 
235 /*
236  * Probe / attach routines.
237  */
238 
239 /*
240  * Probe for the aria hardware.
241  */
242 int
243 ariaprobe(parent, cf, aux)
244 	struct device *parent;
245 	struct cfdata *cf;
246 	void *aux;
247 {
248 	bus_space_handle_t ioh;
249 	struct isa_attach_args *ia = aux;
250 
251 	if (ia->ia_nio < 1)
252 		return (0);
253 	if (ia->ia_nirq < 1)
254 		return (0);
255 
256 	if (ISA_DIRECT_CONFIG(ia))
257 		return (0);
258 
259 	if (!ARIA_BASE_VALID(ia->ia_io[0].ir_addr)) {
260 		printf("aria: configured iobase %d invalid\n",
261 		    ia->ia_io[0].ir_addr);
262 		return 0;
263 	}
264 
265 	if (!ARIA_IRQ_VALID(ia->ia_irq[0].ir_irq)) {
266 		printf("aria: configured irq %d invalid\n",
267 		    ia->ia_irq[0].ir_irq);
268 		return 0;
269 	}
270 
271 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, ARIADSP_NPORT,
272 	    0, &ioh)) {
273 		DPRINTF(("aria: aria probe failed\n"));
274 		return 0;
275 	}
276 
277 	if (cf->cf_flags & 1)
278 		aria_prometheus_kludge(ia, ioh);
279 
280 	if (ariareset(ia->ia_iot, ioh) != 0) {
281 		DPRINTF(("aria: aria probe failed\n"));
282 		bus_space_unmap(ia->ia_iot, ioh,  ARIADSP_NPORT);
283 		return 0;
284 	}
285 
286 	bus_space_unmap(ia->ia_iot, ioh, ARIADSP_NPORT);
287 
288 	ia->ia_nio = 1;
289 	ia->ia_io[0].ir_size = ARIADSP_NPORT;
290 
291 	ia->ia_nirq = 1;
292 
293 	ia->ia_niomem = 0;
294 	ia->ia_ndrq = 0;
295 
296 	DPRINTF(("aria: aria probe succeeded\n"));
297 	return 1;
298 }
299 
300 /*
301  * I didn't call this a kludge for
302  * nothing.  This is cribbed from
303  * ariainit, the author of that
304  * disassembled some code to discover
305  * how to set up the initial values of
306  * the card.  Without this, the card
307  * is dead. (It will not respond to _any_
308  * input at all.)
309  *
310  * ariainit can be found (ftp) at:
311  * ftp://ftp.wi.leidenuniv.nl/pub/audio/aria/programming/contrib/ariainit.zip
312  * currently.
313  */
314 
315 void
316 aria_prometheus_kludge(ia, ioh1)
317 	struct isa_attach_args *ia;
318 	bus_space_handle_t ioh1;
319 {
320 	bus_space_tag_t iot;
321 	bus_space_handle_t ioh;
322 	u_short	end;
323 
324 	DPRINTF(("aria: begin aria_prometheus_kludge\n"));
325 
326 /* Begin Config Sequence */
327 
328 	iot = ia->ia_iot;
329 	bus_space_map(iot, 0x200, 8, 0, &ioh);
330 
331         bus_space_write_1(iot, ioh, 4, 0x4c);
332         bus_space_write_1(iot, ioh, 5, 0x42);
333         bus_space_write_1(iot, ioh, 6, 0x00);
334         bus_space_write_2(iot, ioh, 0, 0x0f);
335         bus_space_write_1(iot, ioh, 1, 0x00);
336         bus_space_write_2(iot, ioh, 0, 0x02);
337         bus_space_write_1(iot, ioh, 1, ia->ia_io[0].ir_addr>>2);
338 
339 /*
340  * These next three lines set up the iobase
341  * and the irq; and disable the drq.
342  */
343 
344 	aria_do_kludge(iot, ioh, ioh1, 0x111,
345 	    ((ia->ia_io[0].ir_addr-0x280)>>2)+0xA0, 0xbf, 0xa0);
346 	aria_do_kludge(iot, ioh, ioh1, 0x011,
347 	    ia->ia_irq[0].ir_irq-6, 0xf8, 0x00);
348 	aria_do_kludge(iot, ioh, ioh1, 0x011, 0x00, 0xef, 0x00);
349 
350 /* The rest of these lines just disable everything else */
351 
352 	aria_do_kludge(iot, ioh, ioh1, 0x113, 0x00, 0x88, 0x00);
353 	aria_do_kludge(iot, ioh, ioh1, 0x013, 0x00, 0xf8, 0x00);
354 	aria_do_kludge(iot, ioh, ioh1, 0x013, 0x00, 0xef, 0x00);
355 	aria_do_kludge(iot, ioh, ioh1, 0x117, 0x00, 0x88, 0x00);
356 	aria_do_kludge(iot, ioh, ioh1, 0x017, 0x00, 0xff, 0x00);
357 
358 /* End Sequence */
359 
360 	bus_space_write_1(iot, ioh, 0, 0x0f);
361 	end = bus_space_read_1(iot, ioh1, 0);
362 	bus_space_write_2(iot, ioh, 0, 0x0f);
363 	bus_space_write_1(iot, ioh, 1, end|0x80);
364 	bus_space_read_1(iot, ioh, 0);
365 
366 	bus_space_unmap(iot, ioh, 8);
367 /*
368  * This delay is necessary for some reason,
369  * at least it would crash, and sometimes not
370  * probe properly if it did not exist.
371  */
372 	delay(1000000);
373 }
374 
375 void
376 aria_do_kludge(iot, ioh, ioh1, func, bits, and, or)
377 	bus_space_tag_t iot;
378 	bus_space_handle_t ioh;
379 	bus_space_handle_t ioh1;
380 	u_short func;
381 	u_short bits;
382 	u_short and;
383 	u_short or;
384 {
385 	u_int i;
386 	if (func & 0x100) {
387 		func &= ~0x100;
388 		if (bits) {
389 			bus_space_write_2(iot, ioh, 0, func-1);
390 			bus_space_write_1(iot, ioh, 1, bits);
391 		}
392 	} else
393 		or |= bits;
394 
395 	bus_space_write_1(iot, ioh, 0, func);
396 	i = bus_space_read_1(iot, ioh1, 0);
397 	bus_space_write_2(iot, ioh, 0, func);
398 	bus_space_write_1(iot, ioh, 1, (i&and) | or);
399 }
400 
401 /*
402  * Attach hardware to driver, attach hardware driver to audio
403  * pseudo-device driver.
404  */
405 void
406 ariaattach(parent, self, aux)
407 	struct device *parent, *self;
408 	void *aux;
409 {
410 	bus_space_handle_t ioh;
411 	struct aria_softc *sc = (void *)self;
412 	struct isa_attach_args *ia = aux;
413 	u_short i;
414 
415 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, ARIADSP_NPORT,
416 	    0, &ioh))
417 		panic("%s: can map io port range", self->dv_xname);
418 
419 	sc->sc_iot = ia->ia_iot;
420 	sc->sc_ioh = ioh;
421 	sc->sc_ic = ia->ia_ic;
422 
423 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
424 	    IST_EDGE, IPL_AUDIO, aria_intr, sc);
425 
426 	DPRINTF(("isa_intr_establish() returns (%x)\n", (unsigned) sc->sc_ih));
427 
428 	i = aria_getdspmem(sc, ARIAA_HARDWARE_A);
429 
430 	sc->sc_hardware  = 0;
431 	sc->sc_hardware |= ((i>>13)&0x01)==1 ? ARIA_TELEPHONE:0;
432 	sc->sc_hardware |= (((i>>5)&0x07))==0x04 ? ARIA_MIXER:0;
433 	sc->sc_hardware |= (aria_getdspmem(sc, ARIAA_MODEL_A)>=1)?ARIA_MODEL:0;
434 
435 	sc->sc_open       = 0;
436 	sc->sc_play       = 0;
437 	sc->sc_record     = 0;
438 	sc->sc_rate       = 7875;
439 	sc->sc_chans      = 1;
440 	sc->sc_blocksize  = 1024;
441 	sc->sc_precision  = 8;
442         sc->sc_rintr      = 0;
443         sc->sc_rarg       = 0;
444         sc->sc_pintr      = 0;
445         sc->sc_parg       = 0;
446 	sc->sc_gain[0]       = 127;
447 	sc->sc_gain[1]       = 127;
448 
449 	for (i=0; i<6; i++) {
450 		if (i == ARIAMIX_TEL_LVL)
451 			sc->aria_mix[i].num_channels = 1;
452 		else
453 			sc->aria_mix[i].num_channels = 2;
454 		sc->aria_mix[i].level[0] = 127;
455 		sc->aria_mix[i].level[1] = 127;
456 	}
457 
458 	sc->ariamix_master.num_channels = 2;
459 	sc->ariamix_master.level[0] = 222;
460 	sc->ariamix_master.level[1] = 222;
461 	sc->ariamix_master.bass[0] = 127;
462 	sc->ariamix_master.bass[1] = 127;
463 	sc->ariamix_master.treble[0] = 127;
464 	sc->ariamix_master.treble[1] = 127;
465 	sc->aria_mix_source = 0;
466 
467 	aria_commit_settings(sc);
468 
469 	printf(": dsp %s", (ARIA_MODEL&sc->sc_hardware)?"SC18026":"SC18025");
470 	if (ARIA_TELEPHONE&sc->sc_hardware)
471 		printf(", tel");
472 	if (ARIA_MIXER&sc->sc_hardware)
473 		printf(", SC18075 mixer");
474 	printf("\n");
475 
476 	snprintf(aria_device.version, sizeof(aria_device.version), "%s",
477 		ARIA_MODEL & sc->sc_hardware ? "SC18026" : "SC18025");
478 
479 	audio_attach_mi(&aria_hw_if, (void *)sc, &sc->sc_dev);
480 }
481 
482 /*
483  * Various routines to interface to higher level audio driver
484  */
485 
486 int
487 ariaopen(addr, flags)
488 	void *addr;
489 	int flags;
490 {
491 	struct aria_softc *sc = addr;
492 
493 	DPRINTF(("ariaopen() called\n"));
494 
495 	if (!sc)
496 		return ENXIO;
497 
498 	if (flags&FREAD)
499 		sc->sc_open |= ARIAR_OPEN_RECORD;
500 	if (flags&FWRITE)
501 		sc->sc_open |= ARIAR_OPEN_PLAY;
502 
503 	return 0;
504 }
505 
506 int
507 aria_getdev(addr, retp)
508 	void *addr;
509 	struct audio_device *retp;
510 {
511 	*retp = aria_device;
512 	return 0;
513 }
514 
515 /*
516  * Various routines to interface to higher level audio driver
517  */
518 
519 int
520 aria_query_encoding(addr, fp)
521     void *addr;
522     struct audio_encoding *fp;
523 {
524 	struct aria_softc *sc = addr;
525 
526 	switch (fp->index) {
527 		case 0:
528 			strcpy(fp->name, AudioEmulaw);
529 			fp->encoding = AUDIO_ENCODING_ULAW;
530 			fp->precision = 8;
531 			if ((ARIA_MODEL&sc->sc_hardware) == 0)
532 				fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
533 			break;
534 		case 1:
535 			strcpy(fp->name, AudioEalaw);
536 			fp->encoding = AUDIO_ENCODING_ALAW;
537 			fp->precision = 8;
538 			if ((ARIA_MODEL&sc->sc_hardware) == 0)
539 				fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
540 			break;
541 		case 2:
542 			strcpy(fp->name, AudioEslinear);
543 			fp->encoding = AUDIO_ENCODING_SLINEAR;
544 			fp->precision = 8;
545 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
546 			break;
547 		case 3:
548 			strcpy(fp->name, AudioEslinear_le);
549 			fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
550 			fp->precision = 16;
551 			fp->flags = 0;
552 			break;
553 		case 4:
554 			strcpy(fp->name, AudioEslinear_be);
555 			fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
556 			fp->precision = 16;
557 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
558 			break;
559 		case 5:
560 			strcpy(fp->name, AudioEulinear);
561 			fp->encoding = AUDIO_ENCODING_ULINEAR;
562 			fp->precision = 8;
563 			fp->flags = 0;
564 			break;
565 		case 6:
566 			strcpy(fp->name, AudioEulinear_le);
567 			fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
568 			fp->precision = 16;
569 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
570 			break;
571 		case 7:
572 			strcpy(fp->name, AudioEulinear_be);
573 			fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
574 			fp->precision = 16;
575 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
576 			break;
577 		default:
578 			return(EINVAL);
579 		/*NOTREACHED*/
580 	}
581 
582 	return (0);
583 }
584 
585 /*
586  * Store blocksize in bytes.
587  */
588 
589 int
590 aria_round_blocksize(addr, blk)
591 	void *addr;
592 	int blk;
593 {
594 	int i;
595 #if 0 /* XXX -- this is being a tad bit of a problem... */
596 	for (i=64; i<1024; i*=2)
597 		if (blk <= i)
598 			break;
599 #else
600 	i = 1024;
601 #endif
602 	return(i);
603 }
604 
605 int
606 aria_get_props(addr)
607 	void *addr;
608 {
609 	return AUDIO_PROP_FULLDUPLEX;
610 }
611 
612 int
613 aria_set_params(addr, setmode, usemode, p, r)
614 	void *addr;
615 	int setmode, usemode;
616 	struct audio_params *p, *r;
617 {
618 	struct aria_softc *sc = addr;
619 
620 	switch(p->encoding) {
621 	case AUDIO_ENCODING_ULAW:
622 	case AUDIO_ENCODING_ALAW:
623 	case AUDIO_ENCODING_SLINEAR:
624 	case AUDIO_ENCODING_SLINEAR_LE:
625 	case AUDIO_ENCODING_SLINEAR_BE:
626 	case AUDIO_ENCODING_ULINEAR:
627 	case AUDIO_ENCODING_ULINEAR_LE:
628 	case AUDIO_ENCODING_ULINEAR_BE:
629 		break;
630 	default:
631 		return (EINVAL);
632 	}
633 
634 	if (p->sample_rate <= 9450)
635 		p->sample_rate = 7875;
636 	else if (p->sample_rate <= 13387)
637 		p->sample_rate = 11025;
638 	else if (p->sample_rate <= 18900)
639 		p->sample_rate = 15750;
640 	else if (p->sample_rate <= 26775)
641 		p->sample_rate = 22050;
642 	else if (p->sample_rate <= 37800)
643 		p->sample_rate = 31500;
644 	else
645 		p->sample_rate = 44100;
646 
647 	sc->sc_encoding = p->encoding;
648 	sc->sc_precision = p->precision;
649 	sc->sc_chans = p->channels;
650 	sc->sc_rate = p->sample_rate;
651 
652 	switch(p->encoding) {
653 	case AUDIO_ENCODING_ULAW:
654 		if ((ARIA_MODEL&sc->sc_hardware) == 0) {
655 			p->sw_code = mulaw_to_ulinear8;
656 			r->sw_code = ulinear8_to_mulaw;
657 		}
658 		break;
659 	case AUDIO_ENCODING_ALAW:
660 		if ((ARIA_MODEL&sc->sc_hardware) == 0) {
661 			p->sw_code = alaw_to_ulinear8;
662 			r->sw_code = ulinear8_to_alaw;
663 		}
664 		break;
665 	case AUDIO_ENCODING_SLINEAR:
666 		p->sw_code = r->sw_code = change_sign8;
667 		break;
668 	case AUDIO_ENCODING_ULINEAR_LE:
669 		p->sw_code = r->sw_code = change_sign16_le;
670 		break;
671 	case AUDIO_ENCODING_SLINEAR_BE:
672 		p->sw_code = r->sw_code = swap_bytes;
673 		break;
674 	case AUDIO_ENCODING_ULINEAR_BE:
675 		p->sw_code = r->sw_code = swap_bytes_change_sign16_le;
676 		break;
677 	}
678 
679 	return 0;
680 }
681 
682 /*
683  * This is where all of the twiddling goes on.
684  */
685 
686 int
687 aria_commit_settings(addr)
688 	void *addr;
689 {
690         struct aria_softc *sc = addr;
691 	bus_space_tag_t iot = sc->sc_iot;
692 	bus_space_handle_t ioh = sc->sc_ioh;
693 	static u_char tones[16] =
694 	    { 7, 6, 5, 4, 3, 2, 1, 0, 8, 9, 10, 11, 12, 13, 14, 15 };
695 	u_short format;
696 	u_short left, right;
697 	u_short samp;
698 	u_char i;
699 
700 	DPRINTF(("aria_commit_settings\n"));
701 
702 	switch (sc->sc_rate) {
703 	case  7875: format = 0x00; samp = 0x60; break;
704 	case 11025: format = 0x00; samp = 0x40; break;
705 	case 15750: format = 0x10; samp = 0x60; break;
706 	case 22050: format = 0x10; samp = 0x40; break;
707 	case 31500: format = 0x10; samp = 0x20; break;
708 	case 44100: format = 0x20; samp = 0x00; break;
709 	default:    format = 0x00; samp = 0x40; break;/* XXX can we get here? */
710 	}
711 
712 	if ((ARIA_MODEL&sc->sc_hardware) != 0) {
713 		format |= (sc->sc_encoding==AUDIO_ENCODING_ULAW)?0x06:0x00;
714 		format |= (sc->sc_encoding==AUDIO_ENCODING_ALAW)?0x08:0x00;
715 	}
716 
717 	format |= (sc->sc_precision==16)?0x02:0x00;
718 	format |= (sc->sc_chans==2)?1:0;
719 	samp |= bus_space_read_2(iot, ioh, ARIADSP_STATUS) & ~0x60;
720 
721 	aria_sendcmd(sc, ARIADSPC_FORMAT, format, -1, -1);
722 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL, samp);
723 
724 	if (sc->sc_hardware&ARIA_MIXER) {
725 		for (i = 0; i < 6; i++)
726 			aria_set_mixer(sc, i);
727 
728 		if (sc->sc_chans==2) {
729 			aria_sendcmd(sc, ARIADSPC_CHAN_VOL, ARIAR_PLAY_CHAN,
730 				     ((sc->sc_gain[0]+sc->sc_gain[1])/2)<<7,
731 				     -1);
732 			aria_sendcmd(sc, ARIADSPC_CHAN_PAN, ARIAR_PLAY_CHAN,
733 				     (sc->sc_gain[0]-sc->sc_gain[1])/4+0x40,
734 				     -1);
735 		} else {
736 			aria_sendcmd(sc, ARIADSPC_CHAN_VOL, ARIAR_PLAY_CHAN,
737 				     sc->sc_gain[0]<<7, -1);
738 			aria_sendcmd(sc, ARIADSPC_CHAN_PAN, ARIAR_PLAY_CHAN,
739 				     0x40, -1);
740 		}
741 
742 		aria_sendcmd(sc, ARIADSPC_MASMONMODE,
743 			     sc->ariamix_master.num_channels != 2, -1, -1);
744 
745 		aria_sendcmd(sc, ARIADSPC_MIXERVOL, 0x0004,
746 			     sc->ariamix_master.level[0] << 7,
747 			     sc->ariamix_master.level[1] << 7);
748 
749 		/* Convert treble/bass from byte to soundcard style */
750 
751 		left  = (tones[(sc->ariamix_master.treble[0]>>4)&0x0f]<<8) |
752 			 tones[(sc->ariamix_master.bass[0]>>4)&0x0f];
753 		right = (tones[(sc->ariamix_master.treble[1]>>4)&0x0f]<<8) |
754 			 tones[(sc->ariamix_master.bass[1]>>4)&0x0f];
755 
756 		aria_sendcmd(sc, ARIADSPC_TONE, left, right, -1);
757 	}
758 
759 	aria_sendcmd(sc, ARIADSPC_BLOCKSIZE, sc->sc_blocksize/2, -1, -1);
760 
761 /*
762  * If we think that the card is recording or playing, start it up again here.
763  * Some of the previous commands turn the channels off.
764  */
765 
766 	if (sc->sc_record&(1<<ARIAR_RECORD_CHAN))
767 		aria_sendcmd(sc, ARIADSPC_START_REC, ARIAR_RECORD_CHAN, -1,-1);
768 
769 	if (sc->sc_play&(1<<ARIAR_PLAY_CHAN))
770 		aria_sendcmd(sc, ARIADSPC_START_PLAY, ARIAR_PLAY_CHAN, -1, -1);
771 
772 	return(0);
773 }
774 
775 void
776 aria_set_mixer(sc, i)
777         struct aria_softc *sc;
778 	int i;
779 {
780 	u_char source;
781 	switch(i) {
782 	case ARIAMIX_MIC_LVL:     source = 0x0001; break;
783 	case ARIAMIX_CD_LVL:      source = 0x0002; break;
784 	case ARIAMIX_LINE_IN_LVL: source = 0x0008; break;
785 	case ARIAMIX_TEL_LVL:     source = 0x0020; break;
786 	case ARIAMIX_AUX_LVL:     source = 0x0010; break;
787 	case ARIAMIX_DAC_LVL:     source = 0x0004; break;
788 	default:                  source = 0x0000; break;
789 	}
790 
791 	if (source != 0x0000 && source != 0x0004) {
792 		if (sc->aria_mix[i].mute == 1)
793 			aria_sendcmd(sc, ARIADSPC_INPMONMODE, source, 3, -1);
794 		else
795 			aria_sendcmd(sc, ARIADSPC_INPMONMODE, source,
796 				     sc->aria_mix[i].num_channels != 2, -1);
797 
798 		aria_sendcmd(sc, ARIADSPC_INPMONMODE, 0x8000|source,
799 			     sc->aria_mix[i].num_channels != 2, -1);
800 		aria_sendcmd(sc, ARIADSPC_MIXERVOL, source,
801 			     sc->aria_mix[i].level[0] << 7,
802 			     sc->aria_mix[i].level[1] << 7);
803 	}
804 
805 	if (sc->aria_mix_source == i) {
806 		aria_sendcmd(sc, ARIADSPC_ADCSOURCE, source, -1, -1);
807 
808 		if (sc->sc_open & ARIAR_OPEN_RECORD)
809 			aria_sendcmd(sc, ARIADSPC_ADCCONTROL, 1, -1, -1);
810 		else
811 			aria_sendcmd(sc, ARIADSPC_ADCCONTROL, 0, -1, -1);
812 	}
813 }
814 
815 void
816 ariaclose(addr)
817 	void *addr;
818 {
819         struct aria_softc *sc = addr;
820 
821 	DPRINTF(("aria_close sc=0x%x\n", (unsigned) sc));
822 
823 	sc->sc_open = 0;
824 
825 	if (aria_reset(sc) != 0) {
826 		delay(500);
827 		aria_reset(sc);
828 	}
829 }
830 
831 /*
832  * Reset the hardware.
833  */
834 
835 int ariareset(iot, ioh)
836 	bus_space_tag_t iot;
837 	bus_space_handle_t ioh;
838 {
839 	struct aria_softc tmp, *sc = &tmp;
840 
841 	sc->sc_iot = iot;
842 	sc->sc_ioh = ioh;
843 	return aria_reset(sc);
844 }
845 
846 int
847 aria_reset(sc)
848 	struct aria_softc *sc;
849 {
850 	bus_space_tag_t iot = sc->sc_iot;
851 	bus_space_handle_t ioh = sc->sc_ioh;
852 	int fail=0;
853 	int i;
854 
855 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL,
856 			  ARIAR_ARIA_SYNTH | ARIAR_SR22K|ARIAR_DSPINTWR);
857 	aria_putdspmem(sc, 0x6102, 0);
858 
859 	fail |= aria_sendcmd(sc, ARIADSPC_SYSINIT, 0x0000, 0x0000, 0x0000);
860 
861 	for (i=0; i < ARIAR_NPOLL; i++)
862 		if (aria_getdspmem(sc, ARIAA_TASK_A) == 1)
863 			break;
864 
865 	bus_space_write_2(iot, ioh, ARIADSP_CONTROL,
866 			  ARIAR_ARIA_SYNTH|ARIAR_SR22K | ARIAR_DSPINTWR |
867 			  ARIAR_PCINTWR);
868 	fail |= aria_sendcmd(sc, ARIADSPC_MODE, ARIAV_MODE_NO_SYNTH,-1,-1);
869 
870 	return (fail);
871 }
872 
873 /*
874  * Lower-level routines
875  */
876 
877 void
878 aria_putdspmem(sc, loc, val)
879 	struct aria_softc *sc;
880 	u_short loc;
881 	u_short val;
882 {
883 	bus_space_tag_t iot = sc->sc_iot;
884 	bus_space_handle_t ioh = sc->sc_ioh;
885 	bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, loc);
886 	bus_space_write_2(iot, ioh, ARIADSP_DMADATA, val);
887 }
888 
889 u_short
890 aria_getdspmem(sc, loc)
891 	struct aria_softc *sc;
892 	u_short loc;
893 {
894 	bus_space_tag_t iot = sc->sc_iot;
895 	bus_space_handle_t ioh = sc->sc_ioh;
896 	bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, loc);
897 	return bus_space_read_2(iot, ioh, ARIADSP_DMADATA);
898 }
899 
900 /*
901  * aria_sendcmd()
902  *  each full DSP command is unified into this
903  *  function.
904  */
905 
906 #define ARIASEND(data, flag) \
907 	for (i = ARIAR_NPOLL; \
908 	     (bus_space_read_2(iot, ioh, ARIADSP_STATUS) & ARIAR_BUSY) && i>0; \
909 	     i--) \
910 		; \
911 	if (bus_space_read_2(iot, ioh, ARIADSP_STATUS) & ARIAR_BUSY) \
912 		fail |= flag; \
913 	bus_space_write_2(iot, ioh, ARIADSP_WRITE, (u_short)data)
914 
915 int
916 aria_sendcmd(sc, command, arg1, arg2, arg3)
917 	struct aria_softc *sc;
918 	u_short command;
919 	int arg1;
920 	int arg2;
921 	int arg3;
922 {
923 	bus_space_tag_t iot = sc->sc_iot;
924 	bus_space_handle_t ioh = sc->sc_ioh;
925 	int i, fail = 0;
926 
927 	ARIASEND(command, 1);
928 	if (arg1 != -1) {
929 		ARIASEND(arg1, 2);
930 	}
931 	if (arg2 != -1) {
932 		ARIASEND(arg2, 4);
933 	}
934 	if (arg3 != -1) {
935 		ARIASEND(arg3, 8);
936 	}
937 	ARIASEND(ARIADSPC_TERM, 16);
938 
939 	if (fail) {
940 		sc->sc_sendcmd_err++;
941 #ifdef AUDIO_DEBUG
942 		DPRINTF(("aria_sendcmd: failure=(%d) cmd=(0x%x) fail=(0x%x)\n",
943 			 sc->sc_sendcmd_err, command, fail));
944 #endif
945 		return -1;
946 	}
947 
948 	return 0;
949 }
950 #undef ARIASEND
951 
952 int
953 aria_halt_input(addr)
954 	void *addr;
955 {
956 	struct aria_softc *sc = addr;
957 
958 	DPRINTF(("aria_halt_input\n"));
959 
960 	if (sc->sc_record & (1<<0)) {
961 		aria_sendcmd(sc, ARIADSPC_STOP_REC, 0, -1, -1);
962 		sc->sc_record &= ~(1<<0);
963 		sc->sc_rdiobuffer = 0;
964 	}
965 
966 	return(0);
967 }
968 
969 int
970 aria_halt_output(addr)
971 	void *addr;
972 {
973 	struct aria_softc *sc = addr;
974 
975 	DPRINTF(("aria_halt_output\n"));
976 
977 	if (sc->sc_play & (1<<1)) {
978 		aria_sendcmd(sc, ARIADSPC_STOP_PLAY, 1, -1, -1);
979 		sc->sc_play &= ~(1<<1);
980 		sc->sc_pdiobuffer = 0;
981 	}
982 
983 	return(0);
984 }
985 
986 /*
987  * Here we just set up the buffers.  If we receive
988  * an interrupt without these set, it is ignored.
989  */
990 
991 int
992 aria_start_input(addr, p, cc, intr, arg)
993 	void *addr;
994 	void *p;
995 	int cc;
996 	void (*intr) __P((void *));
997 	void *arg;
998 {
999 	struct aria_softc *sc = addr;
1000 
1001 	DPRINTF(("aria_start_input %d @ %x\n", cc, (unsigned) p));
1002 
1003 	if (cc != sc->sc_blocksize) {
1004 		DPRINTF(("aria_start_input reqsize %d not sc_blocksize %d\n",
1005 			cc, sc->sc_blocksize));
1006 		return EINVAL;
1007 	}
1008 
1009 	sc->sc_rarg = arg;
1010 	sc->sc_rintr = intr;
1011 	sc->sc_rdiobuffer = p;
1012 
1013 	if (!(sc->sc_record&(1<<ARIAR_RECORD_CHAN))) {
1014 		aria_sendcmd(sc, ARIADSPC_START_REC, ARIAR_RECORD_CHAN, -1,-1);
1015 		sc->sc_record |= (1<<ARIAR_RECORD_CHAN);
1016 	}
1017 
1018 	return 0;
1019 }
1020 
1021 int
1022 aria_start_output(addr, p, cc, intr, arg)
1023 	void *addr;
1024 	void *p;
1025 	int cc;
1026 	void (*intr) __P((void *));
1027 	void *arg;
1028 {
1029 	struct aria_softc *sc = addr;
1030 
1031 	DPRINTF(("aria_start_output %d @ %x\n", cc, (unsigned) p));
1032 
1033 	if (cc != sc->sc_blocksize) {
1034 		DPRINTF(("aria_start_output reqsize %d not sc_blocksize %d\n",
1035 			cc, sc->sc_blocksize));
1036 		return EINVAL;
1037 	}
1038 
1039 	sc->sc_parg = arg;
1040 	sc->sc_pintr = intr;
1041 	sc->sc_pdiobuffer = p;
1042 
1043 	if (!(sc->sc_play&(1<<ARIAR_PLAY_CHAN))) {
1044 		aria_sendcmd(sc, ARIADSPC_START_PLAY, ARIAR_PLAY_CHAN, -1, -1);
1045 		sc->sc_play |= (1<<ARIAR_PLAY_CHAN);
1046 	}
1047 
1048 	return 0;
1049 }
1050 
1051 /*
1052  * Process an interrupt.  This should be a
1053  * request (from the card) to write or read
1054  * samples.
1055  */
1056 int
1057 aria_intr(arg)
1058 	void *arg;
1059 {
1060 	struct  aria_softc *sc = arg;
1061 	bus_space_tag_t iot = sc->sc_iot;
1062 	bus_space_handle_t ioh = sc->sc_ioh;
1063 	u_short *pdata = sc->sc_pdiobuffer;
1064 	u_short *rdata = sc->sc_rdiobuffer;
1065 	u_short address;
1066 
1067 #if 0 /*  XXX --  BAD BAD BAD (That this is #define'd out */
1068 	DPRINTF(("Checking to see if this is our intr\n"));
1069 
1070 	if ((inw(iobase) & 1) != 0x1)
1071 		return 0;  /* not for us */
1072 #endif
1073 
1074 	sc->sc_interrupts++;
1075 
1076 	DPRINTF(("aria_intr\n"));
1077 
1078 	if ((sc->sc_open & ARIAR_OPEN_PLAY) && (pdata!=NULL)) {
1079 		DPRINTF(("aria_intr play=(%x)\n", (unsigned) pdata));
1080 		address = 0x8000 - 2*(sc->sc_blocksize);
1081 		address+= aria_getdspmem(sc, ARIAA_PLAY_FIFO_A);
1082 		bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, address);
1083 		bus_space_write_multi_2(iot, ioh, ARIADSP_DMADATA, pdata,
1084 					sc->sc_blocksize / 2);
1085 		if (sc->sc_pintr != NULL)
1086 			(*sc->sc_pintr)(sc->sc_parg);
1087 	}
1088 
1089 	if ((sc->sc_open & ARIAR_OPEN_RECORD) && (rdata!=NULL)) {
1090 		DPRINTF(("aria_intr record=(%x)\n", (unsigned) rdata));
1091 		address = 0x8000 - (sc->sc_blocksize);
1092 		address+= aria_getdspmem(sc, ARIAA_REC_FIFO_A);
1093 		bus_space_write_2(iot, ioh, ARIADSP_DMAADDRESS, address);
1094 		bus_space_read_multi_2(iot, ioh, ARIADSP_DMADATA, rdata,
1095 				       sc->sc_blocksize / 2);
1096 		if (sc->sc_rintr != NULL)
1097 			(*sc->sc_rintr)(sc->sc_rarg);
1098 	}
1099 
1100 	aria_sendcmd(sc, ARIADSPC_TRANSCOMPLETE, -1, -1, -1);
1101 
1102 	return 1;
1103 }
1104 
1105 int
1106 aria_mixer_set_port(addr, cp)
1107 	void *addr;
1108 	mixer_ctrl_t *cp;
1109 {
1110 	struct aria_softc *sc = addr;
1111 	int error = EINVAL;
1112 
1113 	DPRINTF(("aria_mixer_set_port\n"));
1114 
1115 	/* This could be done better, no mixer still has some controls. */
1116 	if (!(ARIA_MIXER & sc->sc_hardware))
1117 		return ENXIO;
1118 
1119 	if (cp->type == AUDIO_MIXER_VALUE) {
1120 		mixer_level_t *mv = &cp->un.value;
1121 		switch (cp->dev) {
1122 		case ARIAMIX_MIC_LVL:
1123 			if (mv->num_channels == 1 || mv->num_channels == 2) {
1124 				sc->aria_mix[ARIAMIX_MIC_LVL].num_channels =
1125 					mv->num_channels;
1126 				sc->aria_mix[ARIAMIX_MIC_LVL].level[0] =
1127 					mv->level[0];
1128 				sc->aria_mix[ARIAMIX_MIC_LVL].level[1] =
1129 					mv->level[1];
1130 				error = 0;
1131 			}
1132 			break;
1133 
1134 		case ARIAMIX_LINE_IN_LVL:
1135 			if (mv->num_channels == 1 || mv->num_channels == 2) {
1136 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].num_channels=
1137 					mv->num_channels;
1138 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[0] =
1139 					mv->level[0];
1140 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[1] =
1141 					mv->level[1];
1142 				error = 0;
1143 			}
1144 			break;
1145 
1146 		case ARIAMIX_CD_LVL:
1147 			if (mv->num_channels == 1 || mv->num_channels == 2) {
1148 				sc->aria_mix[ARIAMIX_CD_LVL].num_channels =
1149 					mv->num_channels;
1150 				sc->aria_mix[ARIAMIX_CD_LVL].level[0] =
1151 					mv->level[0];
1152 				sc->aria_mix[ARIAMIX_CD_LVL].level[1] =
1153 					mv->level[1];
1154 				error = 0;
1155 			}
1156 			break;
1157 
1158 		case ARIAMIX_TEL_LVL:
1159 			if (mv->num_channels == 1) {
1160 				sc->aria_mix[ARIAMIX_TEL_LVL].num_channels =
1161 					mv->num_channels;
1162 				sc->aria_mix[ARIAMIX_TEL_LVL].level[0] =
1163 					mv->level[0];
1164 				error = 0;
1165 			}
1166 			break;
1167 
1168 		case ARIAMIX_DAC_LVL:
1169 			if (mv->num_channels == 1 || mv->num_channels == 2) {
1170 				sc->aria_mix[ARIAMIX_DAC_LVL].num_channels =
1171 					mv->num_channels;
1172 				sc->aria_mix[ARIAMIX_DAC_LVL].level[0] =
1173 					mv->level[0];
1174 				sc->aria_mix[ARIAMIX_DAC_LVL].level[1] =
1175 					mv->level[1];
1176 				error = 0;
1177 			}
1178 			break;
1179 
1180 		case ARIAMIX_AUX_LVL:
1181 			if (mv->num_channels == 1 || mv->num_channels == 2) {
1182 				sc->aria_mix[ARIAMIX_AUX_LVL].num_channels =
1183 					mv->num_channels;
1184 				sc->aria_mix[ARIAMIX_AUX_LVL].level[0] =
1185 					mv->level[0];
1186 				sc->aria_mix[ARIAMIX_AUX_LVL].level[1] =
1187 					mv->level[1];
1188 				error = 0;
1189 			}
1190 			break;
1191 
1192 		case ARIAMIX_MASTER_LVL:
1193 			if (mv->num_channels == 1 || mv->num_channels == 2) {
1194 				sc->ariamix_master.num_channels =
1195 					mv->num_channels;
1196 				sc->ariamix_master.level[0] = mv->level[0];
1197 				sc->ariamix_master.level[1] = mv->level[1];
1198 				error = 0;
1199 			}
1200 			break;
1201 
1202 		case ARIAMIX_MASTER_TREBLE:
1203 			if (mv->num_channels == 2) {
1204 				sc->ariamix_master.treble[0] =
1205 					mv->level[0] == 0 ? 1 : mv->level[0];
1206 				sc->ariamix_master.treble[1] =
1207 					mv->level[1] == 0 ? 1 : mv->level[1];
1208 				error = 0;
1209 			}
1210 			break;
1211 		case ARIAMIX_MASTER_BASS:
1212 			if (mv->num_channels == 2) {
1213 				sc->ariamix_master.bass[0] =
1214 					mv->level[0] == 0 ? 1 : mv->level[0];
1215 				sc->ariamix_master.bass[1] =
1216 					mv->level[1] == 0 ? 1 : mv->level[1];
1217 				error = 0;
1218 			}
1219 			break;
1220 		case ARIAMIX_OUT_LVL:
1221 			if (mv->num_channels == 1 || mv->num_channels == 2) {
1222 				sc->sc_gain[0] = mv->level[0];
1223 				sc->sc_gain[1] = mv->level[1];
1224 				error = 0;
1225 			}
1226 			break;
1227 		default:
1228 		}
1229 	}
1230 
1231 	if (cp->type == AUDIO_MIXER_ENUM)
1232 		switch(cp->dev) {
1233 		case ARIAMIX_RECORD_SOURCE:
1234 			if (cp->un.ord>=0 && cp->un.ord<=6) {
1235 				sc->aria_mix_source = cp->un.ord;
1236 				error = 0;
1237 			}
1238 			break;
1239 
1240 		case ARIAMIX_MIC_MUTE:
1241 			if (cp->un.ord == 0 || cp->un.ord == 1) {
1242 				sc->aria_mix[ARIAMIX_MIC_LVL].mute =cp->un.ord;
1243 				error = 0;
1244 			}
1245 			break;
1246 
1247 		case ARIAMIX_LINE_IN_MUTE:
1248 			if (cp->un.ord == 0 || cp->un.ord == 1) {
1249 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].mute =
1250 					cp->un.ord;
1251 				error = 0;
1252 			}
1253 			break;
1254 
1255 		case ARIAMIX_CD_MUTE:
1256 			if (cp->un.ord == 0 || cp->un.ord == 1) {
1257 				sc->aria_mix[ARIAMIX_CD_LVL].mute = cp->un.ord;
1258 				error = 0;
1259 			}
1260 			break;
1261 
1262 		case ARIAMIX_DAC_MUTE:
1263 			if (cp->un.ord == 0 || cp->un.ord == 1) {
1264 				sc->aria_mix[ARIAMIX_DAC_LVL].mute =cp->un.ord;
1265 				error = 0;
1266 			}
1267 			break;
1268 
1269 		case ARIAMIX_AUX_MUTE:
1270 			if (cp->un.ord == 0 || cp->un.ord == 1) {
1271 				sc->aria_mix[ARIAMIX_AUX_LVL].mute =cp->un.ord;
1272 				error = 0;
1273 			}
1274 			break;
1275 
1276 		case ARIAMIX_TEL_MUTE:
1277 			if (cp->un.ord == 0 || cp->un.ord == 1) {
1278 				sc->aria_mix[ARIAMIX_TEL_LVL].mute =cp->un.ord;
1279 				error = 0;
1280 			}
1281 			break;
1282 
1283 		default:
1284 			/* NOTREACHED */
1285 			return ENXIO;
1286 		}
1287 
1288 	return(error);
1289 }
1290 
1291 int
1292 aria_mixer_get_port(addr, cp)
1293     void *addr;
1294     mixer_ctrl_t *cp;
1295 {
1296 	struct aria_softc *sc = addr;
1297 	int error = EINVAL;
1298 
1299 	DPRINTF(("aria_mixer_get_port\n"));
1300 
1301 	/* This could be done better, no mixer still has some controls. */
1302 	if (!(ARIA_MIXER&sc->sc_hardware))
1303 		return ENXIO;
1304 
1305 	switch (cp->dev) {
1306 	case ARIAMIX_MIC_LVL:
1307 		if (cp->type == AUDIO_MIXER_VALUE) {
1308 			cp->un.value.num_channels =
1309 				sc->aria_mix[ARIAMIX_MIC_LVL].num_channels;
1310 			cp->un.value.level[0] =
1311 				sc->aria_mix[ARIAMIX_MIC_LVL].level[0];
1312 			cp->un.value.level[1] =
1313 				sc->aria_mix[ARIAMIX_MIC_LVL].level[1];
1314 			error = 0;
1315 		}
1316 		break;
1317 
1318 	case ARIAMIX_LINE_IN_LVL:
1319 		if (cp->type == AUDIO_MIXER_VALUE) {
1320 			cp->un.value.num_channels =
1321 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].num_channels;
1322 			cp->un.value.level[0] =
1323 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[0];
1324 			cp->un.value.level[1] =
1325 				sc->aria_mix[ARIAMIX_LINE_IN_LVL].level[1];
1326 			error = 0;
1327 		}
1328 		break;
1329 
1330 	case ARIAMIX_CD_LVL:
1331 		if (cp->type == AUDIO_MIXER_VALUE) {
1332 			cp->un.value.num_channels =
1333 				sc->aria_mix[ARIAMIX_CD_LVL].num_channels;
1334 			cp->un.value.level[0] =
1335 				sc->aria_mix[ARIAMIX_CD_LVL].level[0];
1336 			cp->un.value.level[1] =
1337 				sc->aria_mix[ARIAMIX_CD_LVL].level[1];
1338 			error = 0;
1339 		}
1340 		break;
1341 
1342 	case ARIAMIX_TEL_LVL:
1343 		if (cp->type == AUDIO_MIXER_VALUE) {
1344 			cp->un.value.num_channels =
1345 				sc->aria_mix[ARIAMIX_TEL_LVL].num_channels;
1346 			cp->un.value.level[0] =
1347 				sc->aria_mix[ARIAMIX_TEL_LVL].level[0];
1348 			error = 0;
1349 		}
1350 		break;
1351 	case ARIAMIX_DAC_LVL:
1352 		if (cp->type == AUDIO_MIXER_VALUE) {
1353 			cp->un.value.num_channels =
1354 				sc->aria_mix[ARIAMIX_DAC_LVL].num_channels;
1355 			cp->un.value.level[0] =
1356 				sc->aria_mix[ARIAMIX_DAC_LVL].level[0];
1357 			cp->un.value.level[1] =
1358 				sc->aria_mix[ARIAMIX_DAC_LVL].level[1];
1359 			error = 0;
1360 		}
1361 		break;
1362 
1363 	case ARIAMIX_AUX_LVL:
1364 		if (cp->type == AUDIO_MIXER_VALUE) {
1365 			cp->un.value.num_channels =
1366 				sc->aria_mix[ARIAMIX_AUX_LVL].num_channels;
1367 			cp->un.value.level[0] =
1368 				sc->aria_mix[ARIAMIX_AUX_LVL].level[0];
1369 			cp->un.value.level[1] =
1370 				sc->aria_mix[ARIAMIX_AUX_LVL].level[1];
1371 			error = 0;
1372 		}
1373 		break;
1374 
1375 	case ARIAMIX_MIC_MUTE:
1376 		if (cp->type == AUDIO_MIXER_ENUM) {
1377 			cp->un.ord = sc->aria_mix[ARIAMIX_MIC_LVL].mute;
1378 			error = 0;
1379 		}
1380 		break;
1381 
1382 	case ARIAMIX_LINE_IN_MUTE:
1383 		if (cp->type == AUDIO_MIXER_ENUM) {
1384 			cp->un.ord = sc->aria_mix[ARIAMIX_LINE_IN_LVL].mute;
1385 			error = 0;
1386 		}
1387 		break;
1388 
1389 	case ARIAMIX_CD_MUTE:
1390 		if (cp->type == AUDIO_MIXER_ENUM) {
1391 			cp->un.ord = sc->aria_mix[ARIAMIX_CD_LVL].mute;
1392 			error = 0;
1393 		}
1394 		break;
1395 
1396 	case ARIAMIX_DAC_MUTE:
1397 		if (cp->type == AUDIO_MIXER_ENUM) {
1398 			cp->un.ord = sc->aria_mix[ARIAMIX_DAC_LVL].mute;
1399 			error = 0;
1400 		}
1401 		break;
1402 
1403 	case ARIAMIX_AUX_MUTE:
1404 		if (cp->type == AUDIO_MIXER_ENUM) {
1405 			cp->un.ord = sc->aria_mix[ARIAMIX_AUX_LVL].mute;
1406 			error = 0;
1407 		}
1408 		break;
1409 
1410 	case ARIAMIX_TEL_MUTE:
1411 		if (cp->type == AUDIO_MIXER_ENUM) {
1412 			cp->un.ord = sc->aria_mix[ARIAMIX_TEL_LVL].mute;
1413 			error = 0;
1414 		}
1415 		break;
1416 
1417 	case ARIAMIX_MASTER_LVL:
1418 		if (cp->type == AUDIO_MIXER_VALUE) {
1419 			cp->un.value.num_channels =
1420 				sc->ariamix_master.num_channels;
1421 			cp->un.value.level[0] = sc->ariamix_master.level[0];
1422 			cp->un.value.level[1] = sc->ariamix_master.level[1];
1423 			error = 0;
1424 		}
1425 		break;
1426 
1427 	case ARIAMIX_MASTER_TREBLE:
1428 		if (cp->type == AUDIO_MIXER_VALUE) {
1429 			cp->un.value.num_channels = 2;
1430 			cp->un.value.level[0] = sc->ariamix_master.treble[0];
1431 			cp->un.value.level[1] = sc->ariamix_master.treble[1];
1432 			error = 0;
1433 		}
1434 		break;
1435 
1436 	case ARIAMIX_MASTER_BASS:
1437 		if (cp->type == AUDIO_MIXER_VALUE) {
1438 			cp->un.value.num_channels = 2;
1439 			cp->un.value.level[0] = sc->ariamix_master.bass[0];
1440 			cp->un.value.level[1] = sc->ariamix_master.bass[1];
1441 			error = 0;
1442 		}
1443 		break;
1444 
1445 	case ARIAMIX_OUT_LVL:
1446 		if (cp->type == AUDIO_MIXER_VALUE) {
1447 			cp->un.value.num_channels = sc->sc_chans;
1448 			cp->un.value.level[0] = sc->sc_gain[0];
1449 			cp->un.value.level[1] = sc->sc_gain[1];
1450 			error = 0;
1451 		}
1452 		break;
1453 	case ARIAMIX_RECORD_SOURCE:
1454 		if (cp->type == AUDIO_MIXER_ENUM) {
1455 			cp->un.ord = sc->aria_mix_source;
1456 			error = 0;
1457 		}
1458 		break;
1459 
1460 	default:
1461 		return ENXIO;
1462 		/* NOT REACHED */
1463 	}
1464 
1465 	return(error);
1466 }
1467 
1468 int
1469 aria_mixer_query_devinfo(addr, dip)
1470 	   void *addr;
1471 	   mixer_devinfo_t *dip;
1472 {
1473 
1474 	struct aria_softc *sc = addr;
1475 
1476 	DPRINTF(("aria_mixer_query_devinfo\n"));
1477 
1478 	/* This could be done better, no mixer still has some controls. */
1479 	if (!(ARIA_MIXER & sc->sc_hardware))
1480 		return ENXIO;
1481 
1482 	dip->prev = dip->next = AUDIO_MIXER_LAST;
1483 
1484 	switch(dip->index) {
1485 	case ARIAMIX_MIC_LVL:
1486 		dip->type = AUDIO_MIXER_VALUE;
1487 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
1488 		dip->next = ARIAMIX_MIC_MUTE;
1489 		strcpy(dip->label.name, AudioNmicrophone);
1490 		dip->un.v.num_channels = 2;
1491 		strcpy(dip->un.v.units.name, AudioNvolume);
1492 		break;
1493 
1494 	case ARIAMIX_LINE_IN_LVL:
1495 		dip->type = AUDIO_MIXER_VALUE;
1496 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
1497 		dip->next = ARIAMIX_LINE_IN_MUTE;
1498 		strcpy(dip->label.name, AudioNline);
1499 		dip->un.v.num_channels = 2;
1500 		strcpy(dip->un.v.units.name, AudioNvolume);
1501 		break;
1502 
1503 	case ARIAMIX_CD_LVL:
1504 		dip->type = AUDIO_MIXER_VALUE;
1505 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
1506 		dip->next = ARIAMIX_CD_MUTE;
1507 		strcpy(dip->label.name, AudioNcd);
1508 		dip->un.v.num_channels = 2;
1509 		strcpy(dip->un.v.units.name, AudioNvolume);
1510 		break;
1511 
1512 	case ARIAMIX_TEL_LVL:
1513 		dip->type = AUDIO_MIXER_VALUE;
1514 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
1515 		dip->next = ARIAMIX_TEL_MUTE;
1516 		strcpy(dip->label.name, "telephone");
1517 		dip->un.v.num_channels = 1;
1518 		strcpy(dip->un.v.units.name, AudioNvolume);
1519 		break;
1520 
1521 	case ARIAMIX_DAC_LVL:
1522 		dip->type = AUDIO_MIXER_VALUE;
1523 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
1524 		dip->next = ARIAMIX_DAC_MUTE;
1525 		strcpy(dip->label.name, AudioNdac);
1526 		dip->un.v.num_channels = 1;
1527 		strcpy(dip->un.v.units.name, AudioNvolume);
1528 		break;
1529 
1530 	case ARIAMIX_AUX_LVL:
1531 		dip->type = AUDIO_MIXER_VALUE;
1532 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
1533 		dip->next = ARIAMIX_AUX_MUTE;
1534 		strcpy(dip->label.name, AudioNoutput);
1535 		dip->un.v.num_channels = 1;
1536 		strcpy(dip->un.v.units.name, AudioNvolume);
1537 		break;
1538 
1539 	case ARIAMIX_MIC_MUTE:
1540 		dip->prev = ARIAMIX_MIC_LVL;
1541 		goto mute;
1542 
1543 	case ARIAMIX_LINE_IN_MUTE:
1544 		dip->prev = ARIAMIX_LINE_IN_LVL;
1545 		goto mute;
1546 
1547 	case ARIAMIX_CD_MUTE:
1548 		dip->prev = ARIAMIX_CD_LVL;
1549 		goto mute;
1550 
1551 	case ARIAMIX_DAC_MUTE:
1552 		dip->prev = ARIAMIX_DAC_LVL;
1553 		goto mute;
1554 
1555 	case ARIAMIX_AUX_MUTE:
1556 		dip->prev = ARIAMIX_AUX_LVL;
1557 		goto mute;
1558 
1559 	case ARIAMIX_TEL_MUTE:
1560 		dip->prev = ARIAMIX_TEL_LVL;
1561 		goto mute;
1562 
1563 mute:
1564 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
1565 		dip->type = AUDIO_MIXER_ENUM;
1566 		strcpy(dip->label.name, AudioNmute);
1567 		dip->un.e.num_mem = 2;
1568 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
1569 		dip->un.e.member[0].ord = 0;
1570 		strcpy(dip->un.e.member[1].label.name, AudioNon);
1571 		dip->un.e.member[1].ord = 1;
1572 		break;
1573 
1574 	case ARIAMIX_MASTER_LVL:
1575 		dip->type = AUDIO_MIXER_VALUE;
1576 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
1577 		dip->next = AUDIO_MIXER_LAST;
1578 		strcpy(dip->label.name, AudioNvolume);
1579 		dip->un.v.num_channels = 2;
1580 		strcpy(dip->un.v.units.name, AudioNvolume);
1581 		break;
1582 
1583 	case ARIAMIX_MASTER_TREBLE:
1584 		dip->type = AUDIO_MIXER_VALUE;
1585 		dip->mixer_class = ARIAMIX_EQ_CLASS;
1586 		strcpy(dip->label.name, AudioNtreble);
1587 		dip->un.v.num_channels = 2;
1588 		strcpy(dip->un.v.units.name, AudioNtreble);
1589 		break;
1590 
1591 	case ARIAMIX_MASTER_BASS:
1592 		dip->type = AUDIO_MIXER_VALUE;
1593 		dip->mixer_class = ARIAMIX_EQ_CLASS;
1594 		strcpy(dip->label.name, AudioNbass);
1595 		dip->un.v.num_channels = 2;
1596 		strcpy(dip->un.v.units.name, AudioNbass);
1597 		break;
1598 
1599 	case ARIAMIX_OUT_LVL:
1600 		dip->type = AUDIO_MIXER_VALUE;
1601 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
1602 		strcpy(dip->label.name, AudioNoutput);
1603 		dip->un.v.num_channels = 2;
1604 		strcpy(dip->un.v.units.name, AudioNvolume);
1605 		break;
1606 
1607 	case ARIAMIX_RECORD_SOURCE:
1608 		dip->mixer_class = ARIAMIX_RECORD_CLASS;
1609 		dip->type = AUDIO_MIXER_ENUM;
1610 		strcpy(dip->label.name, AudioNsource);
1611 		dip->un.e.num_mem = 6;
1612 		strcpy(dip->un.e.member[0].label.name, AudioNoutput);
1613 		dip->un.e.member[0].ord = ARIAMIX_AUX_LVL;
1614 		strcpy(dip->un.e.member[1].label.name, AudioNmicrophone);
1615 		dip->un.e.member[1].ord = ARIAMIX_MIC_LVL;
1616 		strcpy(dip->un.e.member[2].label.name, AudioNdac);
1617 		dip->un.e.member[2].ord = ARIAMIX_DAC_LVL;
1618 		strcpy(dip->un.e.member[3].label.name, AudioNline);
1619 		dip->un.e.member[3].ord = ARIAMIX_LINE_IN_LVL;
1620 		strcpy(dip->un.e.member[4].label.name, AudioNcd);
1621 		dip->un.e.member[4].ord = ARIAMIX_CD_LVL;
1622 		strcpy(dip->un.e.member[5].label.name, "telephone");
1623 		dip->un.e.member[5].ord = ARIAMIX_TEL_LVL;
1624 		break;
1625 
1626 	case ARIAMIX_INPUT_CLASS:
1627 		dip->type = AUDIO_MIXER_CLASS;
1628 		dip->mixer_class = ARIAMIX_INPUT_CLASS;
1629 		strcpy(dip->label.name, AudioCinputs);
1630 		break;
1631 
1632 	case ARIAMIX_OUTPUT_CLASS:
1633 		dip->type = AUDIO_MIXER_CLASS;
1634 		dip->mixer_class = ARIAMIX_OUTPUT_CLASS;
1635 		strcpy(dip->label.name, AudioCoutputs);
1636 		break;
1637 
1638 	case ARIAMIX_RECORD_CLASS:
1639 		dip->type = AUDIO_MIXER_CLASS;
1640 		dip->mixer_class = ARIAMIX_RECORD_CLASS;
1641 		strcpy(dip->label.name, AudioCrecord);
1642 		break;
1643 
1644 	case ARIAMIX_EQ_CLASS:
1645 		dip->type = AUDIO_MIXER_CLASS;
1646 		dip->mixer_class = ARIAMIX_EQ_CLASS;
1647 		strcpy(dip->label.name, AudioCequalization);
1648 		break;
1649 
1650 	default:
1651 		return ENXIO;
1652 		/*NOTREACHED*/
1653 	}
1654 	return 0;
1655 }
1656