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