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