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