xref: /netbsd-src/sys/dev/pci/fms.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: fms.c,v 1.40 2011/11/24 03:35:59 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Witold J. Wnuk.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Forte Media FM801 Audio Device Driver
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: fms.c,v 1.40 2011/11/24 03:35:59 mrg Exp $");
38 
39 #include "mpu.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/kmem.h>
45 #include <sys/device.h>
46 #include <sys/audioio.h>
47 
48 #include <sys/bus.h>
49 #include <sys/cpu.h>
50 
51 #include <dev/pci/pcidevs.h>
52 #include <dev/pci/pcivar.h>
53 
54 #include <dev/audio_if.h>
55 #include <dev/mulaw.h>
56 #include <dev/auconv.h>
57 
58 #include <dev/ic/ac97var.h>
59 #include <dev/ic/mpuvar.h>
60 
61 #include <dev/pci/fmsvar.h>
62 
63 
64 struct fms_dma {
65 	struct fms_dma *next;
66 	void *addr;
67 	size_t size;
68 	bus_dmamap_t map;
69 	bus_dma_segment_t seg;
70 };
71 
72 
73 
74 static int	fms_match(device_t, cfdata_t, void *);
75 static void	fms_attach(device_t, device_t, void *);
76 static int	fms_intr(void *);
77 
78 static int	fms_query_encoding(void *, struct audio_encoding *);
79 static int	fms_set_params(void *, int, int, audio_params_t *,
80 			       audio_params_t *, stream_filter_list_t *,
81 			       stream_filter_list_t *);
82 static int	fms_round_blocksize(void *, int, int, const audio_params_t *);
83 static int	fms_halt_output(void *);
84 static int	fms_halt_input(void *);
85 static int	fms_getdev(void *, struct audio_device *);
86 static int	fms_set_port(void *, mixer_ctrl_t *);
87 static int	fms_get_port(void *, mixer_ctrl_t *);
88 static int	fms_query_devinfo(void *, mixer_devinfo_t *);
89 static void	*fms_malloc(void *, int, size_t);
90 static void	fms_free(void *, void *, size_t);
91 static size_t	fms_round_buffersize(void *, int, size_t);
92 static paddr_t	fms_mappage(void *, void *, off_t, int);
93 static int	fms_get_props(void *);
94 static int	fms_trigger_output(void *, void *, void *, int,
95 				   void (*)(void *), void *,
96 				   const audio_params_t *);
97 static int	fms_trigger_input(void *, void *, void *, int,
98 				  void (*)(void *), void *,
99 				  const audio_params_t *);
100 static void	fms_get_locks(void *, kmutex_t **, kmutex_t **);
101 
102 CFATTACH_DECL(fms, sizeof (struct fms_softc),
103     fms_match, fms_attach, NULL, NULL);
104 
105 static struct audio_device fms_device = {
106 	"Forte Media 801",
107 	"1.0",
108 	"fms"
109 };
110 
111 
112 static const struct audio_hw_if fms_hw_if = {
113 	NULL,			/* open */
114 	NULL,			/* close */
115 	NULL,
116 	fms_query_encoding,
117 	fms_set_params,
118 	fms_round_blocksize,
119 	NULL,
120 	NULL,
121 	NULL,
122 	NULL,
123 	NULL,
124 	fms_halt_output,
125 	fms_halt_input,
126 	NULL,
127 	fms_getdev,
128 	NULL,
129 	fms_set_port,
130 	fms_get_port,
131 	fms_query_devinfo,
132 	fms_malloc,
133 	fms_free,
134 	fms_round_buffersize,
135 	fms_mappage,
136 	fms_get_props,
137 	fms_trigger_output,
138 	fms_trigger_input,
139 	NULL,
140 	fms_get_locks,
141 };
142 
143 static int	fms_attach_codec(void *, struct ac97_codec_if *);
144 static int	fms_read_codec(void *, uint8_t, uint16_t *);
145 static int	fms_write_codec(void *, uint8_t, uint16_t);
146 static int	fms_reset_codec(void *);
147 
148 #define FM_PCM_VOLUME		0x00
149 #define FM_FM_VOLUME		0x02
150 #define FM_I2S_VOLUME		0x04
151 #define FM_RECORD_SOURCE	0x06
152 
153 #define FM_PLAY_CTL		0x08
154 #define  FM_PLAY_RATE_MASK		0x0f00
155 #define  FM_PLAY_BUF1_LAST		0x0001
156 #define  FM_PLAY_BUF2_LAST		0x0002
157 #define  FM_PLAY_START			0x0020
158 #define  FM_PLAY_PAUSE			0x0040
159 #define  FM_PLAY_STOPNOW		0x0080
160 #define  FM_PLAY_16BIT			0x4000
161 #define  FM_PLAY_STEREO			0x8000
162 
163 #define FM_PLAY_DMALEN		0x0a
164 #define FM_PLAY_DMABUF1		0x0c
165 #define FM_PLAY_DMABUF2		0x10
166 
167 
168 #define FM_REC_CTL		0x14
169 #define  FM_REC_RATE_MASK		0x0f00
170 #define  FM_REC_BUF1_LAST		0x0001
171 #define  FM_REC_BUF2_LAST		0x0002
172 #define  FM_REC_START			0x0020
173 #define  FM_REC_PAUSE			0x0040
174 #define  FM_REC_STOPNOW			0x0080
175 #define  FM_REC_16BIT			0x4000
176 #define  FM_REC_STEREO			0x8000
177 
178 
179 #define FM_REC_DMALEN		0x16
180 #define FM_REC_DMABUF1		0x18
181 #define FM_REC_DMABUF2		0x1c
182 
183 #define FM_CODEC_CTL		0x22
184 #define FM_VOLUME		0x26
185 #define  FM_VOLUME_MUTE			0x8000
186 
187 #define FM_CODEC_CMD		0x2a
188 #define  FM_CODEC_CMD_READ		0x0080
189 #define  FM_CODEC_CMD_VALID		0x0100
190 #define  FM_CODEC_CMD_BUSY		0x0200
191 
192 #define FM_CODEC_DATA		0x2c
193 
194 #define FM_IO_CTL		0x52
195 #define FM_CARD_CTL		0x54
196 
197 #define FM_INTMASK		0x56
198 #define  FM_INTMASK_PLAY		0x0001
199 #define  FM_INTMASK_REC			0x0002
200 #define  FM_INTMASK_VOL			0x0040
201 #define  FM_INTMASK_MPU			0x0080
202 
203 #define FM_INTSTATUS		0x5a
204 #define  FM_INTSTATUS_PLAY		0x0100
205 #define  FM_INTSTATUS_REC		0x0200
206 #define  FM_INTSTATUS_VOL		0x4000
207 #define  FM_INTSTATUS_MPU		0x8000
208 
209 
210 static int
211 fms_match(device_t parent, cfdata_t match, void *aux)
212 {
213 	struct pci_attach_args *pa;
214 
215 	pa = (struct pci_attach_args *)aux;
216 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_FORTEMEDIA)
217 		return 0;
218 	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_FORTEMEDIA_FM801)
219 		return 0;
220 
221 	return 1;
222 }
223 
224 static void
225 fms_attach(device_t parent, device_t self, void *aux)
226 {
227 	struct pci_attach_args *pa;
228 	struct fms_softc *sc;
229 	struct audio_attach_args aa;
230 	const char *intrstr;
231 	pci_chipset_tag_t pc;
232 	pcitag_t pt;
233 	pci_intr_handle_t ih;
234 	uint16_t k1;
235 
236 	pa = aux;
237 	sc = device_private(self);
238 	intrstr = NULL;
239 	pc = pa->pa_pc;
240 	pt = pa->pa_tag;
241 	aprint_naive(": Audio controller\n");
242 	aprint_normal(": Forte Media FM-801\n");
243 
244 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
245 			   &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
246 		aprint_error_dev(&sc->sc_dev, "can't map i/o space\n");
247 		return;
248 	}
249 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2,
250 				&sc->sc_mpu_ioh))
251 		panic("fms_attach: can't get mpu subregion handle");
252 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4,
253 				&sc->sc_opl_ioh))
254 		panic("fms_attach: can't get opl subregion handle");
255 
256 	if (pci_intr_map(pa, &ih)) {
257 		aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
258 		return;
259 	}
260 	intrstr = pci_intr_string(pc, ih);
261 
262 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
263 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
264 
265 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, fms_intr, sc);
266 	if (sc->sc_ih == NULL) {
267 		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
268 		if (intrstr != NULL)
269 			aprint_error(" at %s", intrstr);
270 		aprint_error("\n");
271 		mutex_destroy(&sc->sc_lock);
272 		mutex_destroy(&sc->sc_intr_lock);
273 		return;
274 	}
275 
276 	sc->sc_dmat = pa->pa_dmat;
277 
278 	aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n", intrstr);
279 
280 	/* Disable legacy audio (SBPro compatibility) */
281 	pci_conf_write(pc, pt, 0x40, 0);
282 
283 	/* Reset codec and AC'97 */
284 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
285 	delay(2);		/* > 1us according to AC'97 documentation */
286 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
287 	delay(1);		/* > 168.2ns according to AC'97 documentation */
288 
289 	/* Set up volume */
290 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808);
291 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808);
292 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808);
293 
294 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000);
295 
296 	/* Unmask playback, record and mpu interrupts, mask the rest */
297 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK);
298 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK,
299 	    (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
300 	     FM_INTMASK_VOL);
301 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
302 	    FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
303 	    FM_INTSTATUS_VOL);
304 
305 	sc->host_if.arg = sc;
306 	sc->host_if.attach = fms_attach_codec;
307 	sc->host_if.read = fms_read_codec;
308 	sc->host_if.write = fms_write_codec;
309 	sc->host_if.reset = fms_reset_codec;
310 
311 	if (ac97_attach(&sc->host_if, self, &sc->sc_lock) != 0) {
312 		mutex_destroy(&sc->sc_intr_lock);
313 		mutex_destroy(&sc->sc_lock);
314 		return;
315 	}
316 
317 	audio_attach_mi(&fms_hw_if, sc, &sc->sc_dev);
318 
319 	aa.type = AUDIODEV_TYPE_OPL;
320 	aa.hwif = NULL;
321 	aa.hdl = NULL;
322 	config_found(&sc->sc_dev, &aa, audioprint);
323 
324 	aa.type = AUDIODEV_TYPE_MPU;
325 	aa.hwif = NULL;
326 	aa.hdl = NULL;
327 	sc->sc_mpu_dev = config_found(&sc->sc_dev, &aa, audioprint);
328 }
329 
330 /*
331  * Each AC-link frame takes 20.8us, data should be ready in next frame,
332  * we allow more than two.
333  */
334 #define TIMO 50
335 static int
336 fms_read_codec(void *addr, uint8_t reg, uint16_t *val)
337 {
338 	struct fms_softc *sc;
339 	int i;
340 
341 	sc = addr;
342 	/* Poll until codec is ready */
343 	for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
344 		 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
345 		delay(1);
346 	if (i >= TIMO) {
347 		printf("fms: codec busy\n");
348 		return 1;
349 	}
350 
351 	/* Write register index, read access */
352 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD,
353 			  reg | FM_CODEC_CMD_READ);
354 
355 	/* Poll until we have valid data */
356 	for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh,
357 		 FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++)
358 		delay(1);
359 	if (i >= TIMO) {
360 		printf("fms: no data from codec\n");
361 		return 1;
362 	}
363 
364 	/* Read data */
365 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA);
366 	return 0;
367 }
368 
369 static int
370 fms_write_codec(void *addr, uint8_t reg, uint16_t val)
371 {
372 	struct fms_softc *sc = addr;
373 	int i;
374 
375 	/* Poll until codec is ready */
376 	for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
377 		 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
378 		delay(1);
379 	if (i >= TIMO) {
380 		printf("fms: codec busy\n");
381 		return 1;
382 	}
383 
384 	/* Write data */
385 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val);
386 	/* Write index register, write access */
387 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg);
388 	return 0;
389 }
390 #undef TIMO
391 
392 static int
393 fms_attach_codec(void *addr, struct ac97_codec_if *cif)
394 {
395 	struct fms_softc *sc;
396 
397 	sc = addr;
398 	sc->codec_if = cif;
399 	return 0;
400 }
401 
402 /* Cold Reset */
403 static int
404 fms_reset_codec(void *addr)
405 {
406 	struct fms_softc *sc;
407 
408 	sc = addr;
409 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
410 	delay(2);
411 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
412 	delay(1);
413 	return 0;
414 }
415 
416 static int
417 fms_intr(void *arg)
418 {
419 	struct fms_softc *sc = arg;
420 #if NMPU > 0
421 	struct mpu_softc *sc_mpu = device_private(sc->sc_mpu_dev);
422 #endif
423 	uint16_t istat;
424 
425 	mutex_spin_enter(&sc->sc_intr_lock);
426 
427 	istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS);
428 
429 	if (istat & FM_INTSTATUS_PLAY) {
430 		if ((sc->sc_play_nextblk += sc->sc_play_blksize) >=
431 		     sc->sc_play_end)
432 			sc->sc_play_nextblk = sc->sc_play_start;
433 
434 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
435 		    sc->sc_play_flip++ & 1 ?
436 		    FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk);
437 
438 		if (sc->sc_pintr)
439 			sc->sc_pintr(sc->sc_parg);
440 		else
441 			printf("unexpected play intr\n");
442 	}
443 
444 	if (istat & FM_INTSTATUS_REC) {
445 		if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >=
446 		     sc->sc_rec_end)
447 			sc->sc_rec_nextblk = sc->sc_rec_start;
448 
449 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
450 		    sc->sc_rec_flip++ & 1 ?
451 		    FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk);
452 
453 		if (sc->sc_rintr)
454 			sc->sc_rintr(sc->sc_rarg);
455 		else
456 			printf("unexpected rec intr\n");
457 	}
458 
459 #if NMPU > 0
460 	if (istat & FM_INTSTATUS_MPU)
461 		mpu_intr(sc_mpu);
462 #endif
463 
464 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
465 			  istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC));
466 
467 	mutex_spin_exit(&sc->sc_intr_lock);
468 
469 	return 1;
470 }
471 
472 static int
473 fms_query_encoding(void *addr, struct audio_encoding *fp)
474 {
475 
476 	switch (fp->index) {
477 	case 0:
478 		strcpy(fp->name, AudioEmulaw);
479 		fp->encoding = AUDIO_ENCODING_ULAW;
480 		fp->precision = 8;
481 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
482 		return 0;
483 	case 1:
484 		strcpy(fp->name, AudioEslinear_le);
485 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
486 		fp->precision = 16;
487 		fp->flags = 0;
488 		return 0;
489 	case 2:
490 		strcpy(fp->name, AudioEulinear);
491 		fp->encoding = AUDIO_ENCODING_ULINEAR;
492 		fp->precision = 8;
493 		fp->flags = 0;
494 		return 0;
495 	case 3:
496 		strcpy(fp->name, AudioEalaw);
497 		fp->encoding = AUDIO_ENCODING_ALAW;
498 		fp->precision = 8;
499 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
500 		return 0;
501 	case 4:
502 		strcpy(fp->name, AudioEulinear_le);
503 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
504 		fp->precision = 16;
505 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
506 		return 0;
507 	case 5:
508 		strcpy(fp->name, AudioEslinear);
509 		fp->encoding = AUDIO_ENCODING_SLINEAR;
510 		fp->precision = 8;
511 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
512 		return 0;
513 	case 6:
514 		strcpy(fp->name, AudioEulinear_be);
515 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
516 		fp->precision = 16;
517 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
518 		return 0;
519 	case 7:
520 		strcpy(fp->name, AudioEslinear_be);
521 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
522 		fp->precision = 16;
523 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
524 		return 0;
525 	default:
526 		return EINVAL;
527 	}
528 }
529 
530 /*
531  * Range below -limit- is set to -rate-
532  * What a pity FM801 does not have 24000
533  * 24000 -> 22050 sounds rather poor
534  */
535 static struct {
536 	int limit;
537 	int rate;
538 } const fms_rates[11] = {
539 	{  6600,  5500 },
540 	{  8750,  8000 },
541 	{ 10250,  9600 },
542 	{ 13200, 11025 },
543 	{ 17500, 16000 },
544 	{ 20500, 19200 },
545 	{ 26500, 22050 },
546 	{ 35000, 32000 },
547 	{ 41000, 38400 },
548 	{ 46000, 44100 },
549 	{ 48000, 48000 },
550 	/* anything above -> 48000 */
551 };
552 
553 #define FMS_NFORMATS	4
554 static const struct audio_format fms_formats[FMS_NFORMATS] = {
555 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
556 	 2, AUFMT_STEREO, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
557 			       32000, 38400, 44100, 48000}},
558 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
559 	 1, AUFMT_MONAURAL, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
560 				 32000, 38400, 44100, 48000}},
561 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
562 	 2, AUFMT_STEREO, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
563 			       32000, 38400, 44100, 48000}},
564 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
565 	 1, AUFMT_MONAURAL, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
566 				 32000, 38400, 44100, 48000}},
567 };
568 
569 static int
570 fms_set_params(void *addr, int setmode, int usemode,
571     audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
572     stream_filter_list_t *rfil)
573 {
574 	struct fms_softc *sc;
575 	int i, index;
576 
577 	sc = addr;
578 	if (setmode & AUMODE_PLAY) {
579 		for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit;
580 		     i++)
581 			continue;
582 		play->sample_rate = fms_rates[i].rate;
583 		index = auconv_set_converter(fms_formats, FMS_NFORMATS,
584 					     AUMODE_PLAY, play, FALSE, pfil);
585 		if (index < 0)
586 			return EINVAL;
587 		sc->sc_play_reg = i << 8;
588 		if (fms_formats[index].channels == 2)
589 			sc->sc_play_reg |= FM_PLAY_STEREO;
590 		if (fms_formats[index].precision == 16)
591 			sc->sc_play_reg |= FM_PLAY_16BIT;
592 	}
593 
594 	if (setmode & AUMODE_RECORD) {
595 		for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit;
596 		     i++)
597 			continue;
598 		rec->sample_rate = fms_rates[i].rate;
599 		index = auconv_set_converter(fms_formats, FMS_NFORMATS,
600 					     AUMODE_RECORD, rec, FALSE, rfil);
601 		if (index < 0)
602 			return EINVAL;
603 		sc->sc_rec_reg = i << 8;
604 		if (fms_formats[index].channels == 2)
605 			sc->sc_rec_reg |= FM_REC_STEREO;
606 		if (fms_formats[index].precision == 16)
607 			sc->sc_rec_reg |= FM_REC_16BIT;
608 	}
609 
610 	return 0;
611 }
612 
613 static int
614 fms_round_blocksize(void *addr, int blk, int mode,
615     const audio_params_t *param)
616 {
617 
618 	return blk & ~0xf;
619 }
620 
621 static int
622 fms_halt_output(void *addr)
623 {
624 	struct fms_softc *sc;
625 	uint16_t k1;
626 
627 	sc = addr;
628 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL);
629 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
630 			  (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
631 			  FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST);
632 
633 	return 0;
634 }
635 
636 static int
637 fms_halt_input(void *addr)
638 {
639 	struct fms_softc *sc;
640 	uint16_t k1;
641 
642 	sc = addr;
643 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL);
644 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
645 			  (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
646 			  FM_REC_BUF1_LAST | FM_REC_BUF2_LAST);
647 
648 	return 0;
649 }
650 
651 static int
652 fms_getdev(void *addr, struct audio_device *retp)
653 {
654 
655 	*retp = fms_device;
656 	return 0;
657 }
658 
659 static int
660 fms_set_port(void *addr, mixer_ctrl_t *cp)
661 {
662 	struct fms_softc *sc;
663 
664 	sc = addr;
665 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
666 }
667 
668 static int
669 fms_get_port(void *addr, mixer_ctrl_t *cp)
670 {
671 	struct fms_softc *sc;
672 
673 	sc = addr;
674 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
675 }
676 
677 static void *
678 fms_malloc(void *addr, int direction, size_t size)
679 {
680 	struct fms_softc *sc;
681 	struct fms_dma *p;
682 	int error;
683 	int rseg;
684 
685 	sc = addr;
686 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
687 	if (p == NULL)
688 		return NULL;
689 
690 	p->size = size;
691 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
692 				      1, &rseg, BUS_DMA_WAITOK)) != 0) {
693 		aprint_error_dev(&sc->sc_dev, "unable to allocate DMA, error = %d\n", error);
694 		goto fail_alloc;
695 	}
696 
697 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
698 				    BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
699 		aprint_error_dev(&sc->sc_dev, "unable to map DMA, error = %d\n",
700 		       error);
701 		goto fail_map;
702 	}
703 
704 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
705 				       BUS_DMA_WAITOK, &p->map)) != 0) {
706 		aprint_error_dev(&sc->sc_dev, "unable to create DMA map, error = %d\n",
707 		       error);
708 		goto fail_create;
709 	}
710 
711 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
712 				     BUS_DMA_WAITOK)) != 0) {
713 		aprint_error_dev(&sc->sc_dev, "unable to load DMA map, error = %d\n",
714 		       error);
715 		goto fail_load;
716 	}
717 
718 	p->next = sc->sc_dmas;
719 	sc->sc_dmas = p;
720 
721 	return p->addr;
722 
723 
724 fail_load:
725 	bus_dmamap_destroy(sc->sc_dmat, p->map);
726 fail_create:
727 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
728 fail_map:
729 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
730 fail_alloc:
731 	kmem_free(p, sizeof(*p));
732 	return NULL;
733 }
734 
735 static void
736 fms_free(void *addr, void *ptr, size_t size)
737 {
738 	struct fms_softc *sc;
739 	struct fms_dma **pp, *p;
740 
741 	sc = addr;
742 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
743 		if (p->addr == ptr) {
744 			bus_dmamap_unload(sc->sc_dmat, p->map);
745 			bus_dmamap_destroy(sc->sc_dmat, p->map);
746 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
747 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
748 
749 			*pp = p->next;
750 			kmem_free(p, sizeof(*p));
751 			return;
752 		}
753 
754 	panic("fms_free: trying to free unallocated memory");
755 }
756 
757 static size_t
758 fms_round_buffersize(void *addr, int direction, size_t size)
759 {
760 
761 	return size;
762 }
763 
764 static paddr_t
765 fms_mappage(void *addr, void *mem, off_t off, int prot)
766 {
767 	struct fms_softc *sc;
768 	struct fms_dma *p;
769 
770 	sc = addr;
771 	if (off < 0)
772 		return -1;
773 
774 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
775 		continue;
776 	if (p == NULL)
777 		return -1;
778 
779 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
780 			       BUS_DMA_WAITOK);
781 }
782 
783 static int
784 fms_get_props(void *addr)
785 {
786 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
787 	       AUDIO_PROP_FULLDUPLEX;
788 }
789 
790 static int
791 fms_query_devinfo(void *addr, mixer_devinfo_t *dip)
792 {
793 	struct fms_softc *sc;
794 
795 	sc = addr;
796 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
797 }
798 
799 static int
800 fms_trigger_output(void *addr, void *start, void *end, int blksize,
801     void (*intr)(void *), void *arg, const audio_params_t *param)
802 {
803 	struct fms_softc *sc;
804 	struct fms_dma *p;
805 
806 	sc = addr;
807 	sc->sc_pintr = intr;
808 	sc->sc_parg = arg;
809 
810 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
811 		continue;
812 
813 	if (p == NULL)
814 		panic("fms_trigger_output: request with bad start "
815 		      "address (%p)", start);
816 
817 	sc->sc_play_start = p->map->dm_segs[0].ds_addr;
818 	sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start);
819 	sc->sc_play_blksize = blksize;
820 	sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize;
821 	sc->sc_play_flip = 0;
822 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1);
823 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1,
824 			  sc->sc_play_start);
825 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2,
826 			  sc->sc_play_nextblk);
827 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
828 			  FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg);
829 	return 0;
830 }
831 
832 
833 static int
834 fms_trigger_input(void *addr, void *start, void *end, int blksize,
835     void (*intr)(void *), void *arg, const audio_params_t *param)
836 {
837 	struct fms_softc *sc;
838 	struct fms_dma *p;
839 
840 	sc = addr;
841 	sc->sc_rintr = intr;
842 	sc->sc_rarg = arg;
843 
844 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
845 		continue;
846 
847 	if (p == NULL)
848 		panic("fms_trigger_input: request with bad start "
849 		      "address (%p)", start);
850 
851 	sc->sc_rec_start = p->map->dm_segs[0].ds_addr;
852 	sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start);
853 	sc->sc_rec_blksize = blksize;
854 	sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize;
855 	sc->sc_rec_flip = 0;
856 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1);
857 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1,
858 			  sc->sc_rec_start);
859 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2,
860 			  sc->sc_rec_nextblk);
861 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
862 			  FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg);
863 	return 0;
864 }
865 
866 static void
867 fms_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
868 {
869 	struct fms_softc *sc;
870 
871 	sc = addr;
872 	*intr = &sc->sc_intr_lock;
873 	*thread = &sc->sc_lock;
874 }
875