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