Home | History | Annotate | Line # | Download | only in pci
fms.c revision 1.20
      1 /*	$NetBSD: fms.c,v 1.20 2004/10/29 12:57:18 yamt 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.20 2004/10/29 12:57:18 yamt 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, struct audio_params *,
     91 			    struct audio_params *));
     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 *, struct audio_params *));
    106 int	fms_trigger_input __P((void *, void *, void *, int, void (*)(void *),
    107 			       void *, struct audio_params *));
    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 	int i;
    251 
    252 	u_int16_t k1;
    253 
    254 	aprint_naive(": Audio controller\n");
    255 	aprint_normal(": Forte Media FM-801\n");
    256 
    257 	if (pci_intr_map(pa, &ih)) {
    258 		aprint_error("%s: couldn't map interrupt\n",
    259 		    sc->sc_dev.dv_xname);
    260 		return;
    261 	}
    262 	intrstr = pci_intr_string(pc, ih);
    263 
    264 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, fms_intr, sc);
    265 	if (sc->sc_ih == NULL) {
    266 		aprint_error("%s: couldn't establish interrupt",
    267 		    sc->sc_dev.dv_xname);
    268 		if (intrstr != NULL)
    269 			aprint_normal(" at %s", intrstr);
    270 		aprint_normal("\n");
    271 		return;
    272 	}
    273 
    274 	sc->sc_dmat = pa->pa_dmat;
    275 
    276 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    277 
    278 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
    279 			   &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
    280 		aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    281 		return;
    282 	}
    283 
    284 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2,
    285 				&sc->sc_mpu_ioh))
    286 		panic("fms_attach: can't get mpu subregion handle");
    287 
    288 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4,
    289 				&sc->sc_opl_ioh))
    290 		panic("fms_attach: can't get opl subregion handle");
    291 
    292 	/* Disable legacy audio (SBPro compatibility) */
    293 	pci_conf_write(pc, pt, 0x40, 0);
    294 
    295 	/* Reset codec and AC'97 */
    296 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
    297 	delay(2);		/* > 1us according to AC'97 documentation */
    298 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
    299 	delay(1);		/* > 168.2ns according to AC'97 documentation */
    300 
    301 	/* Set up volume */
    302 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808);
    303 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808);
    304 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808);
    305 
    306 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000);
    307 
    308 	/* Unmask playback, record and mpu interrupts, mask the rest */
    309 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK);
    310 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK,
    311 	    (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
    312 	     FM_INTMASK_VOL);
    313 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
    314 	    FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
    315 	    FM_INTSTATUS_VOL);
    316 
    317 	sc->host_if.arg = sc;
    318 	sc->host_if.attach = fms_attach_codec;
    319 	sc->host_if.read = fms_read_codec;
    320 	sc->host_if.write = fms_write_codec;
    321 	sc->host_if.reset = fms_reset_codec;
    322 
    323 	if (ac97_attach(&sc->host_if) != 0)
    324 		return;
    325 
    326 	/* Turn mute off */
    327 	for (i = 0; i < 3; i++) {
    328 		static struct {
    329 			char *class, *device;
    330 		} d[] = {
    331 			{ AudioCoutputs, AudioNmaster },
    332 			{ AudioCinputs, AudioNdac },
    333 			{ AudioCrecord, AudioNvolume }
    334 		};
    335 		struct mixer_ctrl ctl;
    336 
    337 		ctl.type = AUDIO_MIXER_ENUM;
    338 		ctl.un.ord = 0;
    339 		ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
    340 			d[i].class, d[i].device, AudioNmute);
    341 		fms_set_port(sc, &ctl);
    342 	}
    343 
    344 	audio_attach_mi(&fms_hw_if, sc, &sc->sc_dev);
    345 
    346 	aa.type = AUDIODEV_TYPE_OPL;
    347 	aa.hwif = NULL;
    348 	aa.hdl = NULL;
    349 	config_found(&sc->sc_dev, &aa, audioprint);
    350 
    351 	aa.type = AUDIODEV_TYPE_MPU;
    352 	aa.hwif = NULL;
    353 	aa.hdl = NULL;
    354 	sc->sc_mpu_dev = config_found(&sc->sc_dev, &aa, audioprint);
    355 }
    356 
    357 /*
    358  * Each AC-link frame takes 20.8us, data should be ready in next frame,
    359  * we allow more than two.
    360  */
    361 #define TIMO 50
    362 int
    363 fms_read_codec(addr, reg, val)
    364 	void *addr;
    365 	u_int8_t reg;
    366 	u_int16_t *val;
    367 {
    368 	struct fms_softc *sc = addr;
    369 	int i;
    370 
    371 	/* Poll until codec is ready */
    372 	for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    373 		 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
    374 		delay(1);
    375 	if (i >= TIMO) {
    376 		printf("fms: codec busy\n");
    377 		return 1;
    378 	}
    379 
    380 	/* Write register index, read access */
    381 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD,
    382 			  reg | FM_CODEC_CMD_READ);
    383 
    384 	/* Poll until we have valid data */
    385 	for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    386 		 FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++)
    387 		delay(1);
    388 	if (i >= TIMO) {
    389 		printf("fms: no data from codec\n");
    390 		return 1;
    391 	}
    392 
    393 	/* Read data */
    394 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA);
    395 	return 0;
    396 }
    397 
    398 int
    399 fms_write_codec(addr, reg, val)
    400 	void *addr;
    401 	u_int8_t reg;
    402 	u_int16_t val;
    403 {
    404 	struct fms_softc *sc = addr;
    405 	int i;
    406 
    407 	/* Poll until codec is ready */
    408 	for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    409 		 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
    410 		delay(1);
    411 	if (i >= TIMO) {
    412 		printf("fms: codec busy\n");
    413 		return 1;
    414 	}
    415 
    416 	/* Write data */
    417 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val);
    418 	/* Write index register, write access */
    419 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg);
    420 	return 0;
    421 }
    422 #undef TIMO
    423 
    424 int
    425 fms_attach_codec(addr, cif)
    426 	void *addr;
    427 	struct ac97_codec_if *cif;
    428 {
    429 	struct fms_softc *sc = addr;
    430 
    431 	sc->codec_if = cif;
    432 	return 0;
    433 }
    434 
    435 /* Cold Reset */
    436 int
    437 fms_reset_codec(addr)
    438 	void *addr;
    439 {
    440 	struct fms_softc *sc = addr;
    441 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
    442 	delay(2);
    443 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
    444 	delay(1);
    445 	return 0;
    446 }
    447 
    448 int
    449 fms_intr(arg)
    450 	void *arg;
    451 {
    452 	struct fms_softc *sc = arg;
    453 	u_int16_t istat;
    454 
    455 	istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS);
    456 
    457 	if (istat & FM_INTSTATUS_PLAY) {
    458 		if ((sc->sc_play_nextblk += sc->sc_play_blksize) >=
    459 		     sc->sc_play_end)
    460 			sc->sc_play_nextblk = sc->sc_play_start;
    461 
    462 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    463 		    sc->sc_play_flip++ & 1 ?
    464 		    FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk);
    465 
    466 		if (sc->sc_pintr)
    467 			sc->sc_pintr(sc->sc_parg);
    468 		else
    469 			printf("unexpected play intr\n");
    470 	}
    471 
    472 	if (istat & FM_INTSTATUS_REC) {
    473 		if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >=
    474 		     sc->sc_rec_end)
    475 			sc->sc_rec_nextblk = sc->sc_rec_start;
    476 
    477 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    478 		    sc->sc_rec_flip++ & 1 ?
    479 		    FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk);
    480 
    481 		if (sc->sc_rintr)
    482 			sc->sc_rintr(sc->sc_rarg);
    483 		else
    484 			printf("unexpected rec intr\n");
    485 	}
    486 
    487 #if NMPU > 0
    488 	if (istat & FM_INTSTATUS_MPU)
    489 		mpu_intr(sc->sc_mpu_dev);
    490 #endif
    491 
    492 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
    493 			  istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC));
    494 
    495 	return 1;
    496 }
    497 
    498 int
    499 fms_open(addr, flags)
    500 	void *addr;
    501 	int flags;
    502 {
    503 	/* UNUSED struct fms_softc *sc = addr;*/
    504 
    505 	return 0;
    506 }
    507 
    508 void
    509 fms_close(addr)
    510 	void *addr;
    511 {
    512 	/* UNUSED struct fms_softc *sc = addr;*/
    513 }
    514 
    515 int
    516 fms_query_encoding(addr, fp)
    517 	void *addr;
    518 	struct audio_encoding *fp;
    519 {
    520 
    521 	switch (fp->index) {
    522 	case 0:
    523 		strcpy(fp->name, AudioEmulaw);
    524 		fp->encoding = AUDIO_ENCODING_ULAW;
    525 		fp->precision = 8;
    526 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    527 		return 0;
    528 	case 1:
    529 		strcpy(fp->name, AudioEslinear_le);
    530 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    531 		fp->precision = 16;
    532 		fp->flags = 0;
    533 		return 0;
    534 	case 2:
    535 		strcpy(fp->name, AudioEulinear);
    536 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    537 		fp->precision = 8;
    538 		fp->flags = 0;
    539 		return 0;
    540 	case 3:
    541 		strcpy(fp->name, AudioEalaw);
    542 		fp->encoding = AUDIO_ENCODING_ALAW;
    543 		fp->precision = 8;
    544 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    545 		return 0;
    546 	case 4:
    547 		strcpy(fp->name, AudioEulinear_le);
    548 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    549 		fp->precision = 16;
    550 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    551 		return 0;
    552 	case 5:
    553 		strcpy(fp->name, AudioEslinear);
    554 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    555 		fp->precision = 8;
    556 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    557 		return 0;
    558 	case 6:
    559 		strcpy(fp->name, AudioEulinear_be);
    560 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    561 		fp->precision = 16;
    562 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    563 		return 0;
    564 	case 7:
    565 		strcpy(fp->name, AudioEslinear_be);
    566 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    567 		fp->precision = 16;
    568 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    569 		return 0;
    570 	default:
    571 		return EINVAL;
    572 	}
    573 }
    574 
    575 /*
    576  * Range below -limit- is set to -rate-
    577  * What a pity FM801 does not have 24000
    578  * 24000 -> 22050 sounds rather poor
    579  */
    580 struct {
    581 	int limit;
    582 	int rate;
    583 } fms_rates[11] = {
    584 	{  6600,  5500 },
    585 	{  8750,  8000 },
    586 	{ 10250,  9600 },
    587 	{ 13200, 11025 },
    588 	{ 17500, 16000 },
    589 	{ 20500, 19200 },
    590 	{ 26500, 22050 },
    591 	{ 35000, 32000 },
    592 	{ 41000, 38400 },
    593 	{ 46000, 44100 },
    594 	{ 48000, 48000 },
    595 	/* anything above -> 48000 */
    596 };
    597 
    598 int
    599 fms_set_params(addr, setmode, usemode, play, rec)
    600 	void *addr;
    601 	int setmode, usemode;
    602 	struct audio_params *play, *rec;
    603 {
    604 	struct fms_softc *sc = addr;
    605 	int i;
    606 
    607 	if (setmode & AUMODE_PLAY) {
    608 		play->factor = 1;
    609 		play->sw_code = 0;
    610 		switch(play->encoding) {
    611 		case AUDIO_ENCODING_ULAW:
    612 			play->factor = 2;
    613 			play->sw_code = mulaw_to_slinear16_le;
    614 			break;
    615 		case AUDIO_ENCODING_SLINEAR_LE:
    616 			if (play->precision == 8)
    617 				play->sw_code = change_sign8;
    618 			break;
    619 		case AUDIO_ENCODING_ULINEAR_LE:
    620 			if (play->precision == 16)
    621 				play->sw_code = change_sign16_le;
    622 			break;
    623 		case AUDIO_ENCODING_ALAW:
    624 			play->factor = 2;
    625 			play->sw_code = alaw_to_slinear16_le;
    626 			break;
    627 		case AUDIO_ENCODING_SLINEAR_BE:
    628 			if (play->precision == 16)
    629 				play->sw_code = swap_bytes;
    630 			else
    631 				play->sw_code = change_sign8;
    632 			break;
    633 		case AUDIO_ENCODING_ULINEAR_BE:
    634 			if (play->precision == 16)
    635 				play->sw_code = change_sign16_swap_bytes_le;
    636 			break;
    637 		default:
    638 			return EINVAL;
    639 		}
    640 		for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit;
    641 		     i++)
    642 			;
    643 		play->sample_rate = fms_rates[i].rate;
    644 		sc->sc_play_reg = (play->channels == 2 ? FM_PLAY_STEREO : 0) |
    645 		    (play->precision * play->factor == 16 ? FM_PLAY_16BIT : 0) |
    646 		    (i << 8);
    647 	}
    648 
    649 	if (setmode & AUMODE_RECORD) {
    650 
    651 		rec->factor = 1;
    652 		rec->sw_code = 0;
    653 		switch(rec->encoding) {
    654 		case AUDIO_ENCODING_ULAW:
    655 			rec->sw_code = ulinear8_to_mulaw;
    656 			break;
    657 		case AUDIO_ENCODING_SLINEAR_LE:
    658 			if (rec->precision == 8)
    659 				rec->sw_code = change_sign8;
    660 			break;
    661 		case AUDIO_ENCODING_ULINEAR_LE:
    662 			if (rec->precision == 16)
    663 				rec->sw_code = change_sign16_le;
    664 			break;
    665 		case AUDIO_ENCODING_ALAW:
    666 			rec->sw_code = ulinear8_to_alaw;
    667 			break;
    668 		case AUDIO_ENCODING_SLINEAR_BE:
    669 			if (play->precision == 16)
    670 				play->sw_code = swap_bytes;
    671 			else
    672 				play->sw_code = change_sign8;
    673 			break;
    674 		case AUDIO_ENCODING_ULINEAR_BE:
    675 			if (play->precision == 16)
    676 				play->sw_code = swap_bytes_change_sign16_le;
    677 			break;
    678 		default:
    679 			return EINVAL;
    680 		}
    681 		for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit;
    682 		     i++)
    683 			;
    684 		rec->sample_rate = fms_rates[i].rate;
    685 		sc->sc_rec_reg =
    686 		    (rec->channels == 2 ? FM_REC_STEREO : 0) |
    687 		    (rec->precision * rec->factor == 16 ? FM_REC_16BIT : 0) |
    688 		    (i << 8);
    689 	}
    690 
    691 	return 0;
    692 }
    693 
    694 int
    695 fms_round_blocksize(addr, blk)
    696 	void *addr;
    697 	int blk;
    698 {
    699 	return blk & ~0xf;
    700 }
    701 
    702 int
    703 fms_halt_output(addr)
    704 	void *addr;
    705 {
    706 	struct fms_softc *sc = addr;
    707 	u_int16_t k1;
    708 
    709 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL);
    710 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
    711 			  (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
    712 			  FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST);
    713 
    714 	return 0;
    715 }
    716 
    717 int
    718 fms_halt_input(addr)
    719 	void *addr;
    720 {
    721 	struct fms_softc *sc = addr;
    722 	u_int16_t k1;
    723 
    724 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL);
    725 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
    726 			  (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
    727 			  FM_REC_BUF1_LAST | FM_REC_BUF2_LAST);
    728 
    729 	return 0;
    730 }
    731 
    732 int
    733 fms_getdev(addr, retp)
    734 	void *addr;
    735 	struct audio_device *retp;
    736 {
    737 	*retp = fms_device;
    738 	return 0;
    739 }
    740 
    741 int
    742 fms_set_port(addr, cp)
    743 	void *addr;
    744 	mixer_ctrl_t *cp;
    745 {
    746 	struct fms_softc *sc = addr;
    747 
    748 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
    749 }
    750 
    751 int
    752 fms_get_port(addr, cp)
    753 	void *addr;
    754 	mixer_ctrl_t *cp;
    755 {
    756 	struct fms_softc *sc = addr;
    757 
    758 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
    759 }
    760 
    761 void *
    762 fms_malloc(addr, direction, size, pool, flags)
    763 	void *addr;
    764 	int direction;
    765 	size_t size;
    766 	struct malloc_type *pool;
    767 	int flags;
    768 {
    769 	struct fms_softc *sc = addr;
    770 	struct fms_dma *p;
    771 	int error;
    772 	int rseg;
    773 
    774 	p = malloc(sizeof(*p), pool, flags);
    775 	if (!p)
    776 		return 0;
    777 
    778 	p->size = size;
    779 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
    780 				      1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    781 		printf("%s: unable to allocate DMA, error = %d\n",
    782 		       sc->sc_dev.dv_xname, error);
    783 		goto fail_alloc;
    784 	}
    785 
    786 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
    787 				    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    788 		printf("%s: unable to map DMA, error = %d\n",
    789 		       sc->sc_dev.dv_xname, error);
    790 		goto fail_map;
    791 	}
    792 
    793 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    794 				       BUS_DMA_NOWAIT, &p->map)) != 0) {
    795 		printf("%s: unable to create DMA map, error = %d\n",
    796 		       sc->sc_dev.dv_xname, error);
    797 		goto fail_create;
    798 	}
    799 
    800 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
    801 				     BUS_DMA_NOWAIT)) != 0) {
    802 		printf("%s: unable to load DMA map, error = %d\n",
    803 		       sc->sc_dev.dv_xname, error);
    804 		goto fail_load;
    805 	}
    806 
    807 	p->next = sc->sc_dmas;
    808 	sc->sc_dmas = p;
    809 
    810 	return p->addr;
    811 
    812 
    813 fail_load:
    814 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    815 fail_create:
    816 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
    817 fail_map:
    818 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    819 fail_alloc:
    820 	free(p, pool);
    821 	return 0;
    822 }
    823 
    824 void
    825 fms_free(addr, ptr, pool)
    826 	void *addr;
    827 	void *ptr;
    828 	struct malloc_type *pool;
    829 {
    830 	struct fms_softc *sc = addr;
    831 	struct fms_dma **pp, *p;
    832 
    833 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
    834 		if (p->addr == ptr) {
    835 			bus_dmamap_unload(sc->sc_dmat, p->map);
    836 			bus_dmamap_destroy(sc->sc_dmat, p->map);
    837 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    838 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    839 
    840 			*pp = p->next;
    841 			free(p, pool);
    842 			return;
    843 		}
    844 
    845 	panic("fms_free: trying to free unallocated memory");
    846 }
    847 
    848 size_t
    849 fms_round_buffersize(addr, direction, size)
    850 	void *addr;
    851 	int direction;
    852 	size_t size;
    853 {
    854 	return size;
    855 }
    856 
    857 paddr_t
    858 fms_mappage(addr, mem, off, prot)
    859 	void *addr;
    860 	void *mem;
    861 	off_t off;
    862 	int prot;
    863 {
    864 	struct fms_softc *sc = addr;
    865 	struct fms_dma *p;
    866 
    867 	if (off < 0)
    868 		return -1;
    869 
    870 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
    871 		;
    872 	if (!p)
    873 		return -1;
    874 
    875 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
    876 			       BUS_DMA_WAITOK);
    877 }
    878 
    879 int
    880 fms_get_props(addr)
    881 	void *addr;
    882 {
    883 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
    884 	       AUDIO_PROP_FULLDUPLEX;
    885 }
    886 
    887 int
    888 fms_query_devinfo(addr, dip)
    889 	void *addr;
    890 	mixer_devinfo_t *dip;
    891 {
    892 	struct fms_softc *sc = addr;
    893 
    894 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
    895 }
    896 
    897 int
    898 fms_trigger_output(addr, start, end, blksize, intr, arg, param)
    899 	void *addr;
    900 	void *start, *end;
    901 	int blksize;
    902 	void (*intr) __P((void *));
    903 	void *arg;
    904 	struct audio_params *param;
    905 {
    906 	struct fms_softc *sc = addr;
    907 	struct fms_dma *p;
    908 
    909 	sc->sc_pintr = intr;
    910 	sc->sc_parg = arg;
    911 
    912 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
    913 		;
    914 
    915 	if (!p)
    916 		panic("fms_trigger_output: request with bad start "
    917 		      "address (%p)", start);
    918 
    919 	sc->sc_play_start = p->map->dm_segs[0].ds_addr;
    920 	sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start);
    921 	sc->sc_play_blksize = blksize;
    922 	sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize;
    923 	sc->sc_play_flip = 0;
    924 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1);
    925 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1,
    926 			  sc->sc_play_start);
    927 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2,
    928 			  sc->sc_play_nextblk);
    929 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
    930 			  FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg);
    931 	return 0;
    932 }
    933 
    934 
    935 int
    936 fms_trigger_input(addr, start, end, blksize, intr, arg, param)
    937 	void *addr;
    938 	void *start, *end;
    939 	int blksize;
    940 	void (*intr) __P((void *));
    941 	void *arg;
    942 	struct audio_params *param;
    943 {
    944 	struct fms_softc *sc = addr;
    945 	struct fms_dma *p;
    946 
    947 	sc->sc_rintr = intr;
    948 	sc->sc_rarg = arg;
    949 
    950 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
    951 		;
    952 
    953 	if (!p)
    954 		panic("fms_trigger_input: request with bad start "
    955 		      "address (%p)", start);
    956 
    957 	sc->sc_rec_start = p->map->dm_segs[0].ds_addr;
    958 	sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start);
    959 	sc->sc_rec_blksize = blksize;
    960 	sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize;
    961 	sc->sc_rec_flip = 0;
    962 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1);
    963 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1,
    964 			  sc->sc_rec_start);
    965 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2,
    966 			  sc->sc_rec_nextblk);
    967 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
    968 			  FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg);
    969 	return 0;
    970 }
    971 
    972 
    973