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