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