xref: /netbsd-src/sys/arch/dreamcast/dev/g2/aica.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: aica.c,v 1.16 2007/10/17 19:54:10 garbled Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 SHIMIZU Ryo <ryo@misakimix.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: aica.c,v 1.16 2007/10/17 19:54:10 garbled Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/audioio.h>
39 
40 #include <dev/audio_if.h>
41 #include <dev/mulaw.h>
42 #include <dev/auconv.h>
43 
44 #include <machine/bus.h>
45 #include <machine/sysasicvar.h>
46 
47 #include <dreamcast/dev/g2/g2busvar.h>
48 #include <dreamcast/dev/g2/aicavar.h>
49 #include <dreamcast/dev/microcode/aica_armcode.h>
50 
51 #define	AICA_REG_ADDR	0x00700000
52 #define	AICA_RAM_START	0x00800000
53 #define	AICA_RAM_SIZE	0x00200000
54 #define	AICA_NCHAN	64
55 #define	AICA_TIMEOUT	0x1800
56 
57 struct aica_softc {
58 	struct device		sc_dev;		/* base device */
59 	bus_space_tag_t		sc_memt;
60 	bus_space_handle_t	sc_aica_regh;
61 	bus_space_handle_t	sc_aica_memh;
62 
63 	/* audio property */
64 	int			sc_open;
65 	int			sc_encodings;
66 	int			sc_precision;
67 	int			sc_channels;
68 	int			sc_rate;
69 	void			(*sc_intr)(void *);
70 	void			*sc_intr_arg;
71 
72 	int			sc_output_master;
73 	int			sc_output_gain[2];
74 #define	AICA_VOLUME_LEFT	0
75 #define	AICA_VOLUME_RIGHT	1
76 
77 	/* work for output */
78 	void			*sc_buffer;
79 	void			*sc_buffer_start;
80 	void			*sc_buffer_end;
81 	int			sc_blksize;
82 	int			sc_nextfill;
83 };
84 
85 const struct {
86 	const char *name;
87 	int	encoding;
88 	int	precision;
89 } aica_encodings[] = {
90 	{AudioEadpcm,		AUDIO_ENCODING_ADPCM,		4},
91 	{AudioEslinear,		AUDIO_ENCODING_SLINEAR,		8},
92 	{AudioEulinear,		AUDIO_ENCODING_ULINEAR,		8},
93 	{AudioEmulaw,		AUDIO_ENCODING_ULAW,		8},
94 	{AudioEalaw,		AUDIO_ENCODING_ALAW,		8},
95 	{AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE,	16},
96 	{AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE,	16},
97 	{AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE,	16},
98 	{AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE,	16},
99 };
100 
101 #define AICA_NFORMATS	5
102 static const struct audio_format aica_formats[AICA_NFORMATS] = {
103 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_ADPCM, 4, 4,
104 	 1, AUFMT_MONAURAL, 0, {1, 65536}},
105 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
106 	 1, AUFMT_MONAURAL, 0, {1, 65536}},
107 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
108 	 2, AUFMT_STEREO, 0, {1, 65536}},
109 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
110 	 1, AUFMT_MONAURAL, 0, {1, 65536}},
111 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
112 	 2, AUFMT_STEREO, 0, {1, 65536}},
113 };
114 
115 int aica_match(struct device *, struct cfdata *, void *);
116 void aica_attach(struct device *, struct device *, void *);
117 int aica_print(void *, const char *);
118 
119 CFATTACH_DECL(aica, sizeof(struct aica_softc), aica_match, aica_attach,
120     NULL, NULL);
121 
122 const struct audio_device aica_device = {
123 	"Dreamcast Sound",
124 	"",
125 	"aica"
126 };
127 
128 inline static void aica_g2fifo_wait(void);
129 void aica_enable(struct aica_softc *);
130 void aica_disable(struct aica_softc *);
131 void aica_memwrite(struct aica_softc *, bus_size_t, uint32_t *, int);
132 void aica_ch2p16write(struct aica_softc *, bus_size_t, uint16_t *, int);
133 void aica_ch2p8write(struct aica_softc *, bus_size_t, uint8_t *, int);
134 void aica_command(struct aica_softc *, uint32_t);
135 void aica_sendparam(struct aica_softc *, uint32_t, int, int);
136 void aica_play(struct aica_softc *, int, int, int, int);
137 void aica_fillbuffer(struct aica_softc *);
138 
139 /* intr */
140 int aica_intr(void *);
141 
142 /* for audio */
143 int aica_open(void *, int);
144 void aica_close(void *);
145 int aica_query_encoding(void *, struct audio_encoding *);
146 int aica_set_params(void *, int, int, audio_params_t *,
147     audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
148 int aica_round_blocksize(void *, int, int, const audio_params_t *);
149 size_t aica_round_buffersize(void *, int, size_t);
150 int aica_trigger_output(void *, void *, void *, int, void (*)(void *), void *,
151     const audio_params_t *);
152 int aica_trigger_input(void *, void *, void *, int, void (*)(void *), void *,
153     const audio_params_t *);
154 int aica_halt_output(void *);
155 int aica_halt_input(void *);
156 int aica_getdev(void *, struct audio_device *);
157 int aica_set_port(void *, mixer_ctrl_t *);
158 int aica_get_port(void *, mixer_ctrl_t *);
159 int aica_query_devinfo(void *, mixer_devinfo_t *);
160 void aica_encode(int, int, int, int, u_char *, u_short **);
161 int aica_get_props(void *);
162 
163 const struct audio_hw_if aica_hw_if = {
164 	aica_open,
165 	aica_close,
166 	NULL,				/* aica_drain */
167 	aica_query_encoding,
168 	aica_set_params,
169 	aica_round_blocksize,
170 	NULL,				/* aica_commit_setting */
171 	NULL,				/* aica_init_output */
172 	NULL,				/* aica_init_input */
173 	NULL,				/* aica_start_output */
174 	NULL,				/* aica_start_input */
175 	aica_halt_output,
176 	aica_halt_input,
177 	NULL,				/* aica_speaker_ctl */
178 	aica_getdev,
179 	NULL,				/* aica_setfd */
180 	aica_set_port,
181 	aica_get_port,
182 	aica_query_devinfo,
183 	NULL,				/* aica_allocm */
184 	NULL,				/* aica_freem */
185 
186 	aica_round_buffersize,		/* aica_round_buffersize */
187 
188 	NULL,				/* aica_mappage */
189 	aica_get_props,
190 	aica_trigger_output,
191 	aica_trigger_input,
192 	NULL,				/* aica_dev_ioctl */
193 };
194 
195 int
196 aica_match(struct device *parent, struct cfdata *cf, void *aux)
197 {
198 	static int aica_matched = 0;
199 
200 	if (aica_matched)
201 		return 0;
202 
203 	aica_matched = 1;
204 	return 1;
205 }
206 
207 void
208 aica_attach(struct device *parent, struct device *self, void *aux)
209 {
210 	struct aica_softc *sc;
211 	struct g2bus_attach_args *ga;
212 	int i;
213 
214 	sc = (struct aica_softc *)self;
215 	ga = aux;
216 	sc->sc_memt = ga->ga_memt;
217 
218 	if (bus_space_map(sc->sc_memt, AICA_REG_ADDR, 0x3000, 0,
219 	    &sc->sc_aica_regh) != 0) {
220 		printf(": can't map AICA register space\n");
221 		return;
222 	}
223 
224 	if (bus_space_map(sc->sc_memt, AICA_RAM_START, AICA_RAM_SIZE, 0,
225 	    &sc->sc_aica_memh) != 0) {
226 		printf(": can't map AICA memory space\n");
227 		return;
228 	}
229 
230 	printf(": ARM7 Sound Processing Unit\n");
231 
232 	aica_disable(sc);
233 
234 	for (i = 0; i < AICA_NCHAN; i++)
235 		bus_space_write_4(sc->sc_memt,sc->sc_aica_regh, i << 7,
236 		    ((bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, i << 7)
237 		    & ~0x4000) | 0x8000));
238 
239 	/* load microcode, and clear memory */
240 	bus_space_set_region_4(sc->sc_memt, sc->sc_aica_memh,
241 	    0, 0, AICA_RAM_SIZE / 4);
242 
243 	aica_memwrite(sc, 0, aica_armcode, sizeof(aica_armcode));
244 
245 	aica_enable(sc);
246 
247 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
248 	    sysasic_intr_string(IPL_BIO));
249 	sysasic_intr_establish(SYSASIC_EVENT_AICA, IPL_BIO, SYSASIC_IRL9,
250 	    aica_intr, sc);
251 
252 	audio_attach_mi(&aica_hw_if, sc, &sc->sc_dev);
253 
254 	/* init parameters */
255 	sc->sc_output_master = 255;
256 	sc->sc_output_gain[AICA_VOLUME_LEFT]  = 255;
257 	sc->sc_output_gain[AICA_VOLUME_RIGHT] = 255;
258 }
259 
260 void
261 aica_enable(struct aica_softc *sc)
262 {
263 
264 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x28a8, 24);
265 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00,
266 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00) & ~1);
267 }
268 
269 void
270 aica_disable(struct aica_softc *sc)
271 {
272 
273 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00,
274 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00) | 1);
275 }
276 
277 inline static void
278 aica_g2fifo_wait(void)
279 {
280 	int i;
281 
282 	i = AICA_TIMEOUT;
283 	while (--i > 0)
284 		if ((*(volatile uint32_t *)0xa05f688c) & 0x11)
285 			break;
286 }
287 
288 void
289 aica_memwrite(struct aica_softc *sc, bus_size_t offset, uint32_t *src, int len)
290 {
291 	int n;
292 
293 	KASSERT((offset & 3) == 0);
294 	n = (len + 3) / 4;	/* uint32_t * n (aligned) */
295 
296 	aica_g2fifo_wait();
297 	bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
298 	    offset, src, n);
299 }
300 
301 void
302 aica_ch2p16write(struct aica_softc *sc, bus_size_t offset, uint16_t *src,
303     int len)
304 {
305 	union {
306 		uint32_t w[8];
307 		uint16_t s[16];
308 	} buf;
309 	uint16_t *p;
310 	int i;
311 
312 	KASSERT((offset & 3) == 0);
313 
314 	while (len >= 32) {
315 		p = buf.s;
316 		*p++ = *src++; src++;
317 		*p++ = *src++; src++;
318 		*p++ = *src++; src++;
319 		*p++ = *src++; src++;
320 		*p++ = *src++; src++;
321 		*p++ = *src++; src++;
322 		*p++ = *src++; src++;
323 		*p++ = *src++; src++;
324 		*p++ = *src++; src++;
325 		*p++ = *src++; src++;
326 		*p++ = *src++; src++;
327 		*p++ = *src++; src++;
328 		*p++ = *src++; src++;
329 		*p++ = *src++; src++;
330 		*p++ = *src++; src++;
331 		*p++ = *src++; src++;
332 
333 		aica_g2fifo_wait();
334 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
335 		    offset, buf.w , 32 / 4);
336 
337 		offset += sizeof(uint16_t) * 16;
338 		len -= 32;
339 	}
340 
341 	if (len / 2 > 0) {
342 		p = buf.s;
343 		for (i = 0; i < len / 2; i++) {
344 			*p++ = *src++; src++;
345 		}
346 
347 		aica_g2fifo_wait();
348 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
349 		    offset, buf.w, len / 4);
350 	}
351 }
352 
353 void
354 aica_ch2p8write(struct aica_softc *sc, bus_size_t offset, uint8_t *src,
355     int len)
356 {
357 	uint32_t buf[8];
358 	uint8_t *p;
359 	int i;
360 
361 	KASSERT((offset & 3) == 0);
362 	while (len >= 32) {
363 		p = (uint8_t *)buf;
364 
365 		*p++ = *src++; src++;
366 		*p++ = *src++; src++;
367 		*p++ = *src++; src++;
368 		*p++ = *src++; src++;
369 		*p++ = *src++; src++;
370 		*p++ = *src++; src++;
371 		*p++ = *src++; src++;
372 		*p++ = *src++; src++;
373 		*p++ = *src++; src++;
374 		*p++ = *src++; src++;
375 		*p++ = *src++; src++;
376 		*p++ = *src++; src++;
377 		*p++ = *src++; src++;
378 		*p++ = *src++; src++;
379 		*p++ = *src++; src++;
380 		*p++ = *src++; src++;
381 		*p++ = *src++; src++;
382 		*p++ = *src++; src++;
383 		*p++ = *src++; src++;
384 		*p++ = *src++; src++;
385 		*p++ = *src++; src++;
386 		*p++ = *src++; src++;
387 		*p++ = *src++; src++;
388 		*p++ = *src++; src++;
389 		*p++ = *src++; src++;
390 		*p++ = *src++; src++;
391 		*p++ = *src++; src++;
392 		*p++ = *src++; src++;
393 		*p++ = *src++; src++;
394 		*p++ = *src++; src++;
395 		*p++ = *src++; src++;
396 		*p++ = *src++; src++;
397 
398 		aica_g2fifo_wait();
399 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
400 		    offset, buf, 32 / 4);
401 
402 		offset += 32;
403 		len -= 32;
404 	}
405 
406 	if (len) {
407 		p = (uint8_t *)buf;
408 		for (i = 0; i < len; i++)
409 			*p++ = *src++; src++;
410 
411 		aica_g2fifo_wait();
412 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
413 		    offset, buf, len / 4);
414 	}
415 }
416 
417 int
418 aica_open(void *addr, int flags)
419 {
420 	struct aica_softc *sc;
421 
422 	sc = addr;
423 	if (sc->sc_open)
424 		return EBUSY;
425 
426 	sc->sc_intr = NULL;
427 	sc->sc_open = 1;
428 
429 	return 0;
430 }
431 
432 void
433 aica_close(void *addr)
434 {
435 	struct aica_softc *sc;
436 
437 	sc = addr;
438 	sc->sc_open = 0;
439 	sc->sc_intr = NULL;
440 }
441 
442 int
443 aica_query_encoding(void *addr, struct audio_encoding *fp)
444 {
445 	if (fp->index >= sizeof(aica_encodings) / sizeof(aica_encodings[0]))
446 		return EINVAL;
447 
448 	strcpy(fp->name, aica_encodings[fp->index].name);
449 	fp->encoding = aica_encodings[fp->index].encoding;
450 	fp->precision = aica_encodings[fp->index].precision;
451 	fp->flags = 0;
452 
453 	return 0;
454 }
455 
456 int
457 aica_set_params(void *addr, int setmode, int usemode,
458     audio_params_t *play, audio_params_t *rec,
459     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
460 {
461 	struct aica_softc *sc;
462 	const audio_params_t *hw;
463 	int i;
464 
465 	i = auconv_set_converter(aica_formats, AICA_NFORMATS,
466 				 AUMODE_PLAY, play, false, pfil);
467 	if (i < 0)
468 		return EINVAL;
469 	hw = pfil->req_size > 0 ? &pfil->filters[0].param : play;
470 	sc = addr;
471 	sc->sc_precision = hw->precision;
472 	sc->sc_channels = hw->channels;
473 	sc->sc_rate = hw->sample_rate;
474 	sc->sc_encodings = hw->encoding;
475 	return 0;
476 }
477 
478 int
479 aica_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
480 {
481 	struct aica_softc *sc;
482 
483 	sc = addr;
484 	switch (sc->sc_precision) {
485 	case 4:
486 		if (sc->sc_channels == 1)
487 			return AICA_DMABUF_SIZE / 4;
488 		else
489 			return AICA_DMABUF_SIZE / 2;
490 		break;
491 	case 8:
492 		if (sc->sc_channels == 1)
493 			return AICA_DMABUF_SIZE / 2;
494 		else
495 			return AICA_DMABUF_SIZE;
496 		break;
497 	case 16:
498 		if (sc->sc_channels == 1)
499 			return AICA_DMABUF_SIZE;
500 		else
501 			return AICA_DMABUF_SIZE * 2;
502 		break;
503 	default:
504 		break;
505 	}
506 
507 	return AICA_DMABUF_SIZE / 4;
508 }
509 
510 size_t
511 aica_round_buffersize(void *addr, int dir, size_t bufsize)
512 {
513 
514 	if (dir == AUMODE_PLAY)
515 		return 65536;
516 
517 	return 512;	/* XXX: AUMINBUF */
518 }
519 
520 void
521 aica_command(struct aica_softc *sc, uint32_t command)
522 {
523 
524 	bus_space_write_4(sc->sc_memt, sc->sc_aica_memh,
525 	    AICA_ARM_CMD_COMMAND, command);
526 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh, AICA_ARM_CMD_SERIAL,
527 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_memh,
528 	    AICA_ARM_CMD_SERIAL) + 1);
529 }
530 
531 void
532 aica_sendparam(struct aica_softc *sc, uint32_t command,
533     int32_t lparam, int32_t rparam)
534 {
535 
536 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
537 	    AICA_ARM_CMD_LPARAM, lparam);
538 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
539 	    AICA_ARM_CMD_RPARAM, rparam);
540 
541 	aica_command(sc, command);
542 }
543 
544 void
545 aica_play(struct aica_softc *sc, int blksize, int channel, int rate, int prec)
546 {
547 
548 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
549 	    AICA_ARM_CMD_BLOCKSIZE, blksize);
550 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
551 	    AICA_ARM_CMD_CHANNEL, channel);
552 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
553 	    AICA_ARM_CMD_RATE, rate);
554 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
555 	    AICA_ARM_CMD_PRECISION, prec);
556 
557 	aica_command(sc, AICA_COMMAND_PLAY);
558 }
559 
560 void
561 aica_fillbuffer(struct aica_softc *sc)
562 {
563 
564 	if (sc->sc_channels == 2) {
565 		if (sc->sc_precision == 16) {
566 			aica_ch2p16write(sc,
567 			    AICA_DMABUF_LEFT + sc->sc_nextfill,
568 			    (uint16_t *)sc->sc_buffer + 0, sc->sc_blksize / 2);
569 			aica_ch2p16write(sc,
570 			    AICA_DMABUF_RIGHT + sc->sc_nextfill,
571 			    (uint16_t *)sc->sc_buffer + 1, sc->sc_blksize / 2);
572 		} else if (sc->sc_precision == 8) {
573 			aica_ch2p8write(sc, AICA_DMABUF_LEFT + sc->sc_nextfill,
574 			    (uint8_t *)sc->sc_buffer + 0, sc->sc_blksize / 2);
575 			aica_ch2p8write(sc, AICA_DMABUF_RIGHT + sc->sc_nextfill,
576 			    (uint8_t *)sc->sc_buffer + 1, sc->sc_blksize / 2);
577 		}
578 	} else {
579 		aica_memwrite(sc, AICA_DMABUF_MONO + sc->sc_nextfill,
580 		    sc->sc_buffer, sc->sc_blksize);
581 	}
582 
583 	sc->sc_buffer = (int8_t *)sc->sc_buffer + sc->sc_blksize;
584 	if (sc->sc_buffer >= sc->sc_buffer_end)
585 		sc->sc_buffer = sc->sc_buffer_start;
586 
587 	sc->sc_nextfill ^= sc->sc_blksize / sc->sc_channels;
588 }
589 
590 int
591 aica_intr(void *arg)
592 {
593 	struct aica_softc *sc;
594 
595 	sc = arg;
596 	aica_fillbuffer(sc);
597 
598 	/* call audio interrupt handler (audio_pint()) */
599 	if (sc->sc_open && sc->sc_intr != NULL) {
600 		(*(sc->sc_intr))(sc->sc_intr_arg);
601 	}
602 
603 	/* clear SPU interrupt */
604 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x28bc, 0x20);
605 	return 1;
606 }
607 
608 int
609 aica_trigger_output(void *addr, void *start, void *end, int blksize,
610     void (*intr)(void *), void *arg, const audio_params_t *param)
611 {
612 	struct aica_softc *sc;
613 
614 	sc = addr;
615 	aica_command(sc, AICA_COMMAND_INIT);
616 	tsleep(aica_trigger_output, PWAIT, "aicawait", hz / 20);
617 
618 	sc->sc_buffer_start = sc->sc_buffer = start;
619 	sc->sc_buffer_end = end;
620 	sc->sc_blksize = blksize;
621 	sc->sc_nextfill = 0;
622 
623 	sc->sc_intr = intr;
624 	sc->sc_intr_arg = arg;
625 
626 	/* fill buffers in advance */
627 	aica_intr(sc);
628 	aica_intr(sc);
629 
630 	/* ...and start playing */
631 	aica_play(sc, blksize / sc->sc_channels, sc->sc_channels, sc->sc_rate,
632 	    sc->sc_precision);
633 
634 	return 0;
635 }
636 
637 int
638 aica_trigger_input(void *addr, void *start, void *end, int blksize,
639     void (*intr)(void *), void *arg, const audio_params_t *param)
640 {
641 
642 	return ENODEV;
643 }
644 
645 int
646 aica_halt_output(void *addr)
647 {
648 	struct aica_softc *sc;
649 
650 	sc = addr;
651 	aica_command(sc, AICA_COMMAND_STOP);
652 	return 0;
653 }
654 
655 int
656 aica_halt_input(void *addr)
657 {
658 
659 	return ENODEV;
660 }
661 
662 int
663 aica_getdev(void *addr, struct audio_device *ret)
664 {
665 
666 	*ret = aica_device;
667 	return 0;
668 }
669 
670 int
671 aica_set_port(void *addr, mixer_ctrl_t *mc)
672 {
673 	struct aica_softc *sc;
674 
675 	sc = addr;
676 	switch (mc->dev) {
677 	case AICA_MASTER_VOL:
678 		sc->sc_output_master =
679 		    mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] & 0xff;
680 		aica_sendparam(sc, AICA_COMMAND_MVOL,
681 		    sc->sc_output_master, sc->sc_output_master);
682 		break;
683 	case AICA_OUTPUT_GAIN:
684 		sc->sc_output_gain[AICA_VOLUME_LEFT] =
685 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  & 0xff;
686 		sc->sc_output_gain[AICA_VOLUME_RIGHT] =
687 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] & 0xff;
688 		aica_sendparam(sc, AICA_COMMAND_VOL,
689 		    sc->sc_output_gain[AICA_VOLUME_LEFT],
690 		    sc->sc_output_gain[AICA_VOLUME_RIGHT]);
691 		break;
692 	default:
693 		return EINVAL;
694 	}
695 
696 	return 0;
697 }
698 
699 int
700 aica_get_port(void *addr, mixer_ctrl_t *mc)
701 {
702 	struct aica_softc *sc;
703 
704 	sc = addr;
705 	switch (mc->dev) {
706 	case AICA_MASTER_VOL:
707 		if (mc->un.value.num_channels != 1)
708 			return EINVAL;
709 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
710 		    L16TO256(L256TO16(sc->sc_output_master));
711 		break;
712 	case AICA_OUTPUT_GAIN:
713 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
714 		    sc->sc_output_gain[AICA_VOLUME_LEFT];
715 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
716 		    sc->sc_output_gain[AICA_VOLUME_RIGHT];
717 		break;
718 	default:
719 		return EINVAL;
720 	}
721 	return 0;
722 }
723 
724 int
725 aica_query_devinfo(void *addr, mixer_devinfo_t *md)
726 {
727 
728 	switch (md->index) {
729 	case AICA_MASTER_VOL:
730 		md->type = AUDIO_MIXER_VALUE;
731 		md->mixer_class = AICA_OUTPUT_CLASS;
732 		md->prev = md->next = AUDIO_MIXER_LAST;
733 		strcpy(md->label.name, AudioNmaster);
734 		md->un.v.num_channels = 1;
735 		strcpy(md->un.v.units.name, AudioNvolume);
736 		return 0;
737 	case AICA_OUTPUT_GAIN:
738 		md->type = AUDIO_MIXER_VALUE;
739 		md->mixer_class = AICA_OUTPUT_CLASS;
740 		md->prev = md->next = AUDIO_MIXER_LAST;
741 		strcpy(md->label.name, AudioNoutput);
742 		md->un.v.num_channels = 2;
743 		strcpy(md->label.name, AudioNvolume);
744 		return 0;
745 	case AICA_OUTPUT_CLASS:
746 		md->type = AUDIO_MIXER_CLASS;
747 		md->mixer_class = AICA_OUTPUT_CLASS;
748 		md->next = md->prev = AUDIO_MIXER_LAST;
749 		strcpy(md->label.name, AudioCoutputs);
750 		return 0;
751 	}
752 
753 	return ENXIO;
754 }
755 
756 int
757 aica_get_props(void *addr)
758 {
759 
760 	return 0;
761 }
762