Home | History | Annotate | Line # | Download | only in pci
yds.c revision 1.37
      1 /*	$NetBSD: yds.c,v 1.37 2006/11/16 01:33:10 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000, 2001 Kazuki Sakamoto and Minoura Makoto.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Yamaha YMF724[B-F]/740[B-C]/744/754
     30  *
     31  * Documentation links:
     32  * - ftp://ftp.alsa-project.org/pub/manuals/yamaha/
     33  * - ftp://ftp.alsa-project.org/pub/manuals/yamaha/pci/
     34  *
     35  * TODO:
     36  * - FM synth volume (difficult: mixed before ac97)
     37  * - Digital in/out (SPDIF) support
     38  * - Effect??
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: yds.c,v 1.37 2006/11/16 01:33:10 christos Exp $");
     43 
     44 #include "mpu.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/fcntl.h>
     50 #include <sys/malloc.h>
     51 #include <sys/device.h>
     52 #include <sys/proc.h>
     53 
     54 #include <dev/pci/pcidevs.h>
     55 #include <dev/pci/pcireg.h>
     56 #include <dev/pci/pcivar.h>
     57 
     58 #include <sys/audioio.h>
     59 #include <dev/audio_if.h>
     60 #include <dev/mulaw.h>
     61 #include <dev/auconv.h>
     62 #include <dev/ic/ac97reg.h>
     63 #include <dev/ic/ac97var.h>
     64 #include <dev/ic/mpuvar.h>
     65 
     66 #include <machine/bus.h>
     67 #include <machine/intr.h>
     68 
     69 #include <dev/microcode/yds/yds_hwmcode.h>
     70 #include <dev/pci/ydsreg.h>
     71 #include <dev/pci/ydsvar.h>
     72 
     73 /* Debug */
     74 #undef YDS_USE_REC_SLOT
     75 #define YDS_USE_P44
     76 
     77 #ifdef AUDIO_DEBUG
     78 # define DPRINTF(x)	if (ydsdebug) printf x
     79 # define DPRINTFN(n,x)	if (ydsdebug>(n)) printf x
     80 int	ydsdebug = 0;
     81 #else
     82 # define DPRINTF(x)
     83 # define DPRINTFN(n,x)
     84 #endif
     85 #ifdef YDS_USE_REC_SLOT
     86 # define YDS_INPUT_SLOT 0	/* REC slot = ADC + loopbacks */
     87 #else
     88 # define YDS_INPUT_SLOT 1	/* ADC slot */
     89 #endif
     90 
     91 static int	yds_match(struct device *, struct cfdata *, void *);
     92 static void	yds_attach(struct device *, struct device *, void *);
     93 static int	yds_intr(void *);
     94 
     95 #define DMAADDR(p)	((p)->map->dm_segs[0].ds_addr)
     96 #define KERNADDR(p)	((void *)((p)->addr))
     97 
     98 static int	yds_allocmem(struct yds_softc *, size_t, size_t,
     99 			     struct yds_dma *);
    100 static int	yds_freemem(struct yds_softc *, struct yds_dma *);
    101 
    102 #ifndef AUDIO_DEBUG
    103 #define YWRITE1(sc, r, x) bus_space_write_1((sc)->memt, (sc)->memh, (r), (x))
    104 #define YWRITE2(sc, r, x) bus_space_write_2((sc)->memt, (sc)->memh, (r), (x))
    105 #define YWRITE4(sc, r, x) bus_space_write_4((sc)->memt, (sc)->memh, (r), (x))
    106 #define YREAD1(sc, r)	bus_space_read_1((sc)->memt, (sc)->memh, (r))
    107 #define YREAD2(sc, r)	bus_space_read_2((sc)->memt, (sc)->memh, (r))
    108 #define YREAD4(sc, r)	bus_space_read_4((sc)->memt, (sc)->memh, (r))
    109 #else
    110 static uint16_t YREAD2(struct yds_softc *sc, bus_size_t r)
    111 {
    112 	DPRINTFN(5, (" YREAD2(0x%lX)\n", (unsigned long)r));
    113 	return bus_space_read_2(sc->memt, sc->memh, r);
    114 }
    115 
    116 static uint32_t YREAD4(struct yds_softc *sc, bus_size_t r)
    117 {
    118 	DPRINTFN(5, (" YREAD4(0x%lX)\n", (unsigned long)r));
    119 	return bus_space_read_4(sc->memt, sc->memh, r);
    120 }
    121 
    122 #ifdef notdef
    123 static void YWRITE1(struct yds_softc *sc, bus_size_t r, uint8_t x)
    124 {
    125 	DPRINTFN(5, (" YWRITE1(0x%lX,0x%lX)\n", (unsigned long)r,
    126 		     (unsigned long)x));
    127 	bus_space_write_1(sc->memt, sc->memh, r, x);
    128 }
    129 #endif
    130 
    131 static void YWRITE2(struct yds_softc *sc, bus_size_t r, uint16_t x)
    132 {
    133 	DPRINTFN(5, (" YWRITE2(0x%lX,0x%lX)\n", (unsigned long)r,
    134 		     (unsigned long)x));
    135 	bus_space_write_2(sc->memt, sc->memh, r, x);
    136 }
    137 
    138 static void YWRITE4(struct yds_softc *sc, bus_size_t r, uint32_t x)
    139 {
    140 	DPRINTFN(5, (" YWRITE4(0x%lX,0x%lX)\n", (unsigned long)r,
    141 		     (unsigned long)x));
    142 	bus_space_write_4(sc->memt, sc->memh, r, x);
    143 }
    144 #endif
    145 
    146 #define	YWRITEREGION4(sc, r, x, c)	\
    147 	bus_space_write_region_4((sc)->memt, (sc)->memh, (r), (x), (c) / 4)
    148 
    149 CFATTACH_DECL(yds, sizeof(struct yds_softc),
    150     yds_match, yds_attach, NULL, NULL);
    151 
    152 static int	yds_open(void *, int);
    153 static void	yds_close(void *);
    154 static int	yds_query_encoding(void *, struct audio_encoding *);
    155 static int	yds_set_params(void *, int, int, audio_params_t *,
    156 			       audio_params_t *, stream_filter_list_t *,
    157 			       stream_filter_list_t *);
    158 static int	yds_round_blocksize(void *, int, int, const audio_params_t *);
    159 static int	yds_trigger_output(void *, void *, void *, int,
    160 				   void (*)(void *), void *,
    161 				   const audio_params_t *);
    162 static int	yds_trigger_input(void *, void *, void *, int,
    163 				  void (*)(void *), void *,
    164 				  const audio_params_t *);
    165 static int	yds_halt_output(void *);
    166 static int	yds_halt_input(void *);
    167 static int	yds_getdev(void *, struct audio_device *);
    168 static int	yds_mixer_set_port(void *, mixer_ctrl_t *);
    169 static int	yds_mixer_get_port(void *, mixer_ctrl_t *);
    170 static void   *yds_malloc(void *, int, size_t, struct malloc_type *, int);
    171 static void	yds_free(void *, void *, struct malloc_type *);
    172 static size_t	yds_round_buffersize(void *, int, size_t);
    173 static paddr_t yds_mappage(void *, void *, off_t, int);
    174 static int	yds_get_props(void *);
    175 static int	yds_query_devinfo(void *, mixer_devinfo_t *);
    176 
    177 static int     yds_attach_codec(void *, struct ac97_codec_if *);
    178 static int	yds_read_codec(void *, uint8_t, uint16_t *);
    179 static int	yds_write_codec(void *, uint8_t, uint16_t);
    180 static int     yds_reset_codec(void *);
    181 
    182 static u_int	yds_get_dstype(int);
    183 static int	yds_download_mcode(struct yds_softc *);
    184 static int	yds_allocate_slots(struct yds_softc *);
    185 static void	yds_configure_legacy(struct device *);
    186 static void	yds_enable_dsp(struct yds_softc *);
    187 static int	yds_disable_dsp(struct yds_softc *);
    188 static int	yds_ready_codec(struct yds_codec_softc *);
    189 static int	yds_halt(struct yds_softc *);
    190 static uint32_t yds_get_lpfq(u_int);
    191 static uint32_t yds_get_lpfk(u_int);
    192 static struct yds_dma *yds_find_dma(struct yds_softc *, void *);
    193 
    194 static int	yds_init(struct yds_softc *);
    195 static void	yds_powerhook(int, void *);
    196 
    197 #ifdef AUDIO_DEBUG
    198 static void	yds_dump_play_slot(struct yds_softc *, int);
    199 #define	YDS_DUMP_PLAY_SLOT(n, sc, bank) \
    200 	if (ydsdebug > (n)) yds_dump_play_slot(sc, bank)
    201 #else
    202 #define	YDS_DUMP_PLAY_SLOT(n, sc, bank)
    203 #endif /* AUDIO_DEBUG */
    204 
    205 static const struct audio_hw_if yds_hw_if = {
    206 	yds_open,
    207 	yds_close,
    208 	NULL,
    209 	yds_query_encoding,
    210 	yds_set_params,
    211 	yds_round_blocksize,
    212 	NULL,
    213 	NULL,
    214 	NULL,
    215 	NULL,
    216 	NULL,
    217 	yds_halt_output,
    218 	yds_halt_input,
    219 	NULL,
    220 	yds_getdev,
    221 	NULL,
    222 	yds_mixer_set_port,
    223 	yds_mixer_get_port,
    224 	yds_query_devinfo,
    225 	yds_malloc,
    226 	yds_free,
    227 	yds_round_buffersize,
    228 	yds_mappage,
    229 	yds_get_props,
    230 	yds_trigger_output,
    231 	yds_trigger_input,
    232 	NULL,
    233 	NULL,	/* powerstate */
    234 };
    235 
    236 static const struct audio_device yds_device = {
    237 	"Yamaha DS-1",
    238 	"",
    239 	"yds"
    240 };
    241 
    242 static const struct {
    243 	uint	id;
    244 	u_int	flags;
    245 #define YDS_CAP_MCODE_1			0x0001
    246 #define YDS_CAP_MCODE_1E		0x0002
    247 #define YDS_CAP_LEGACY_SELECTABLE	0x0004
    248 #define YDS_CAP_LEGACY_FLEXIBLE		0x0008
    249 #define YDS_CAP_HAS_P44			0x0010
    250 } yds_chip_capabliity_list[] = {
    251 	{ PCI_PRODUCT_YAMAHA_YMF724,
    252 	  YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE },
    253 	/* 740[C] has only 32 slots.  But anyway we use only 2 */
    254 	{ PCI_PRODUCT_YAMAHA_YMF740,
    255 	  YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE },	/* XXX NOT TESTED */
    256 	{ PCI_PRODUCT_YAMAHA_YMF740C,
    257 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
    258 	{ PCI_PRODUCT_YAMAHA_YMF724F,
    259 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
    260 	{ PCI_PRODUCT_YAMAHA_YMF744B,
    261 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE },
    262 	{ PCI_PRODUCT_YAMAHA_YMF754,
    263 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE|YDS_CAP_HAS_P44 },
    264 	{ 0, 0 }
    265 };
    266 #ifdef AUDIO_DEBUG
    267 #define YDS_CAP_BITS	"\020\005P44\004LEGFLEX\003LEGSEL\002MCODE1E\001MCODE1"
    268 #endif
    269 
    270 static const struct audio_format yds_formats[] = {
    271 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    272 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    273 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    274 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    275 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    276 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    277 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    278 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    279 };
    280 #define	YDS_NFORMATS	(sizeof(yds_formats) / sizeof(struct audio_format))
    281 
    282 #ifdef AUDIO_DEBUG
    283 static void
    284 yds_dump_play_slot(struct yds_softc *sc, int bank)
    285 {
    286 	int i, j;
    287 	uint32_t *p;
    288 	uint32_t num;
    289 	char *pa;
    290 
    291 	for (i = 0; i < N_PLAY_SLOTS; i++) {
    292 		printf("pbankp[%d] = %p,", i*2, sc->pbankp[i*2]);
    293 		printf("pbankp[%d] = %p\n", i*2+1, sc->pbankp[i*2+1]);
    294 	}
    295 
    296 	pa = (char *)DMAADDR(&sc->sc_ctrldata) + sc->pbankoff;
    297 	p = (uint32_t *)sc->ptbl;
    298 	printf("ptbl + 0: %d\n", *p++);
    299 	for (i = 0; i < N_PLAY_SLOTS; i++) {
    300 		printf("ptbl + %d: 0x%x, should be %p\n",
    301 		       i+1, *p,
    302 		       pa + i * sizeof(struct play_slot_ctrl_bank) *
    303 				N_PLAY_SLOT_CTRL_BANK);
    304 		p++;
    305 	}
    306 
    307 	num = le32toh(*(uint32_t*)sc->ptbl);
    308 	printf("numofplay = %d\n", num);
    309 
    310 	for (i = 0; i < num; i++) {
    311 		p = (uint32_t *)sc->pbankp[i*2];
    312 
    313 		printf("  pbankp[%d], bank 0 : %p\n", i*2, p);
    314 		for (j = 0;
    315 		     j < sizeof(struct play_slot_ctrl_bank) / sizeof(uint32_t);
    316 		     j++) {
    317 			printf("    0x%02x: 0x%08x\n",
    318 			       (unsigned)(j * sizeof(uint32_t)),
    319 			       (unsigned)*p++);
    320 		}
    321 
    322 		p = (uint32_t *)sc->pbankp[i*2 + 1];
    323 		printf("  pbankp[%d], bank 1 : %p\n", i*2 + 1, p);
    324 		for (j = 0;
    325 		     j < sizeof(struct play_slot_ctrl_bank) / sizeof(uint32_t);
    326 		     j++) {
    327 			printf("    0x%02x: 0x%08x\n",
    328 			       (unsigned)(j * sizeof(uint32_t)),
    329 			       (unsigned)*p++);
    330 		}
    331 	}
    332 }
    333 #endif /* AUDIO_DEBUG */
    334 
    335 static u_int
    336 yds_get_dstype(int id)
    337 {
    338 	int i;
    339 
    340 	for (i = 0; yds_chip_capabliity_list[i].id; i++) {
    341 		if (PCI_PRODUCT(id) == yds_chip_capabliity_list[i].id)
    342 			return yds_chip_capabliity_list[i].flags;
    343 	}
    344 
    345 	return -1;
    346 }
    347 
    348 static int
    349 yds_download_mcode(struct yds_softc *sc)
    350 {
    351 	static struct {
    352 		const uint32_t *mcode;
    353 		size_t size;
    354 	} ctrls[] = {
    355 		{yds_ds1_ctrl_mcode, sizeof(yds_ds1_ctrl_mcode)},
    356 		{yds_ds1e_ctrl_mcode, sizeof(yds_ds1e_ctrl_mcode)},
    357 	};
    358 	u_int ctrl;
    359 	const uint32_t *p;
    360 	size_t size;
    361 	int dstype;
    362 
    363 	if (sc->sc_flags & YDS_CAP_MCODE_1)
    364 		dstype = YDS_DS_1;
    365 	else if (sc->sc_flags & YDS_CAP_MCODE_1E)
    366 		dstype = YDS_DS_1E;
    367 	else
    368 		return 1;	/* unknown */
    369 
    370 	if (yds_disable_dsp(sc))
    371 		return 1;
    372 
    373 	/* Software reset */
    374 	YWRITE4(sc, YDS_MODE, YDS_MODE_RESET);
    375 	YWRITE4(sc, YDS_MODE, 0);
    376 
    377 	YWRITE4(sc, YDS_MAPOF_REC, 0);
    378 	YWRITE4(sc, YDS_MAPOF_EFFECT, 0);
    379 	YWRITE4(sc, YDS_PLAY_CTRLBASE, 0);
    380 	YWRITE4(sc, YDS_REC_CTRLBASE, 0);
    381 	YWRITE4(sc, YDS_EFFECT_CTRLBASE, 0);
    382 	YWRITE4(sc, YDS_WORK_BASE, 0);
    383 
    384 	ctrl = YREAD2(sc, YDS_GLOBAL_CONTROL);
    385 	YWRITE2(sc, YDS_GLOBAL_CONTROL, ctrl & ~0x0007);
    386 
    387 	/* Download DSP microcode. */
    388 	p = yds_dsp_mcode;
    389 	size = sizeof(yds_dsp_mcode);
    390 	YWRITEREGION4(sc, YDS_DSP_INSTRAM, p, size);
    391 
    392 	/* Download CONTROL microcode. */
    393 	p = ctrls[dstype].mcode;
    394 	size = ctrls[dstype].size;
    395 	YWRITEREGION4(sc, YDS_CTRL_INSTRAM, p, size);
    396 
    397 	yds_enable_dsp(sc);
    398 	delay(10 * 1000);		/* nessesary on my 724F (??) */
    399 
    400 	return 0;
    401 }
    402 
    403 static int
    404 yds_allocate_slots(struct yds_softc *sc)
    405 {
    406 	size_t pcs, rcs, ecs, ws, memsize;
    407 	void *mp;
    408 	uint32_t da;		/* DMA address */
    409 	char *va;		/* KVA */
    410 	off_t cb;
    411 	int i;
    412 	struct yds_dma *p;
    413 
    414 	/* Alloc DSP Control Data */
    415 	pcs = YREAD4(sc, YDS_PLAY_CTRLSIZE) * sizeof(uint32_t);
    416 	rcs = YREAD4(sc, YDS_REC_CTRLSIZE) * sizeof(uint32_t);
    417 	ecs = YREAD4(sc, YDS_EFFECT_CTRLSIZE) * sizeof(uint32_t);
    418 	ws = WORK_SIZE;
    419 	YWRITE4(sc, YDS_WORK_SIZE, ws / sizeof(uint32_t));
    420 
    421 	DPRINTF(("play control size : %d\n", (unsigned int)pcs));
    422 	DPRINTF(("rec control size : %d\n", (unsigned int)rcs));
    423 	DPRINTF(("eff control size : %d\n", (unsigned int)ecs));
    424 	DPRINTF(("work size : %d\n", (unsigned int)ws));
    425 #ifdef DIAGNOSTIC
    426 	if (pcs != sizeof(struct play_slot_ctrl_bank)) {
    427 		printf("%s: invalid play slot ctrldata %d != %d\n",
    428 		       sc->sc_dev.dv_xname, (unsigned int)pcs,
    429 		       (unsigned int)sizeof(struct play_slot_ctrl_bank));
    430 	if (rcs != sizeof(struct rec_slot_ctrl_bank))
    431 		printf("%s: invalid rec slot ctrldata %d != %d\n",
    432 		       sc->sc_dev.dv_xname, (unsigned int)rcs,
    433 		       (unsigned int)sizeof(struct rec_slot_ctrl_bank));
    434 	}
    435 #endif
    436 
    437 	memsize = N_PLAY_SLOTS*N_PLAY_SLOT_CTRL_BANK*pcs +
    438 		  N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK*rcs + ws;
    439 	memsize += (N_PLAY_SLOTS+1)*sizeof(uint32_t);
    440 
    441 	p = &sc->sc_ctrldata;
    442 	if (KERNADDR(p) == NULL) {
    443 		i = yds_allocmem(sc, memsize, 16, p);
    444 		if (i) {
    445 			printf("%s: couldn't alloc/map DSP DMA buffer, reason %d\n",
    446 				sc->sc_dev.dv_xname, i);
    447 			free(p, M_DEVBUF);
    448 			return 1;
    449 		}
    450 	}
    451 	mp = KERNADDR(p);
    452 	da = DMAADDR(p);
    453 
    454 	DPRINTF(("mp:%p, DMA addr:%p\n",
    455 		 mp, (void *)sc->sc_ctrldata.map->dm_segs[0].ds_addr));
    456 
    457 	memset(mp, 0, memsize);
    458 
    459 	/* Work space */
    460 	cb = 0;
    461 	va = (uint8_t *)mp;
    462 	YWRITE4(sc, YDS_WORK_BASE, da + cb);
    463 	cb += ws;
    464 
    465 	/* Play control data table */
    466 	sc->ptbl = (uint32_t *)(va + cb);
    467 	sc->ptbloff = cb;
    468 	YWRITE4(sc, YDS_PLAY_CTRLBASE, da + cb);
    469 	cb += (N_PLAY_SLOT_CTRL + 1) * sizeof(uint32_t);
    470 
    471 	/* Record slot control data */
    472 	sc->rbank = (struct rec_slot_ctrl_bank *)(va + cb);
    473 	YWRITE4(sc, YDS_REC_CTRLBASE, da + cb);
    474 	sc->rbankoff = cb;
    475 	cb += N_REC_SLOT_CTRL * N_REC_SLOT_CTRL_BANK * rcs;
    476 
    477 #if 0
    478 	/* Effect slot control data -- unused */
    479 	YWRITE4(sc, YDS_EFFECT_CTRLBASE, da + cb);
    480 	cb += N_EFFECT_SLOT_CTRL * N_EFFECT_SLOT_CTRL_BANK * ecs;
    481 #endif
    482 
    483 	/* Play slot control data */
    484 	sc->pbankoff = cb;
    485 	for (i=0; i < N_PLAY_SLOT_CTRL; i++) {
    486 		sc->pbankp[i*2] = (struct play_slot_ctrl_bank *)(va + cb);
    487 		*(sc->ptbl + i+1) = htole32(da + cb);
    488 		cb += pcs;
    489 
    490 		sc->pbankp[i*2+1] = (struct play_slot_ctrl_bank *)(va + cb);
    491 		cb += pcs;
    492 	}
    493 	/* Sync play control data table */
    494 	bus_dmamap_sync(sc->sc_dmatag, p->map,
    495 			sc->ptbloff, (N_PLAY_SLOT_CTRL+1) * sizeof(uint32_t),
    496 			BUS_DMASYNC_PREWRITE);
    497 
    498 	return 0;
    499 }
    500 
    501 static void
    502 yds_enable_dsp(struct yds_softc *sc)
    503 {
    504 
    505 	YWRITE4(sc, YDS_CONFIG, YDS_DSP_SETUP);
    506 }
    507 
    508 static int
    509 yds_disable_dsp(struct yds_softc *sc)
    510 {
    511 	int to;
    512 	uint32_t data;
    513 
    514 	data = YREAD4(sc, YDS_CONFIG);
    515 	if (data)
    516 		YWRITE4(sc, YDS_CONFIG, YDS_DSP_DISABLE);
    517 
    518 	for (to = 0; to < YDS_WORK_TIMEOUT; to++) {
    519 		if ((YREAD4(sc, YDS_STATUS) & YDS_STAT_WORK) == 0)
    520 			return 0;
    521 		delay(1);
    522 	}
    523 
    524 	return 1;
    525 }
    526 
    527 static int
    528 yds_match(struct device *parent, struct cfdata *match,
    529     void *aux)
    530 {
    531 	struct pci_attach_args *pa;
    532 
    533 	pa = (struct pci_attach_args *)aux;
    534 	switch (PCI_VENDOR(pa->pa_id)) {
    535 	case PCI_VENDOR_YAMAHA:
    536 		switch (PCI_PRODUCT(pa->pa_id)) {
    537 		case PCI_PRODUCT_YAMAHA_YMF724:
    538 		case PCI_PRODUCT_YAMAHA_YMF740:
    539 		case PCI_PRODUCT_YAMAHA_YMF740C:
    540 		case PCI_PRODUCT_YAMAHA_YMF724F:
    541 		case PCI_PRODUCT_YAMAHA_YMF744B:
    542 		case PCI_PRODUCT_YAMAHA_YMF754:
    543 			return 1;
    544 		}
    545 		break;
    546 	}
    547 
    548 	return 0;
    549 }
    550 
    551 /*
    552  * This routine is called after all the ISA devices are configured,
    553  * to avoid conflict.
    554  */
    555 static void
    556 yds_configure_legacy(struct device *arg)
    557 #define FLEXIBLE	(sc->sc_flags & YDS_CAP_LEGACY_FLEXIBLE)
    558 #define SELECTABLE	(sc->sc_flags & YDS_CAP_LEGACY_SELECTABLE)
    559 {
    560 	static const bus_addr_t opl_addrs[] = {0x388, 0x398, 0x3A0, 0x3A8};
    561 	static const bus_addr_t mpu_addrs[] = {0x330, 0x300, 0x332, 0x334};
    562 	struct yds_softc *sc;
    563 	pcireg_t reg;
    564 	struct device *dev;
    565 	int i;
    566 
    567 	sc = (struct yds_softc*) arg;
    568 	if (!FLEXIBLE && !SELECTABLE)
    569 		return;
    570 
    571 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY);
    572 	reg &= ~0x8133c03f;	/* these bits are out of interest */
    573 	reg |= ((YDS_PCI_EX_LEGACY_IMOD) |
    574 		(YDS_PCI_LEGACY_FMEN |
    575 		 YDS_PCI_LEGACY_MEN /*| YDS_PCI_LEGACY_MIEN*/));
    576 	reg |= YDS_PCI_EX_LEGACY_SMOD_DISABLE;
    577 	if (FLEXIBLE) {
    578 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
    579 		delay(100*1000);
    580 	}
    581 
    582 	/* Look for OPL */
    583 	dev = 0;
    584 	for (i = 0; i < sizeof(opl_addrs) / sizeof(bus_addr_t); i++) {
    585 		if (SELECTABLE) {
    586 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    587 				       YDS_PCI_LEGACY, reg | (i << (0+16)));
    588 			delay(100*1000);	/* wait 100ms */
    589 		} else
    590 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    591 				       YDS_PCI_FM_BA, opl_addrs[i]);
    592 		if (bus_space_map(sc->sc_opl_iot,
    593 				  opl_addrs[i], 4, 0, &sc->sc_opl_ioh) == 0) {
    594 			struct audio_attach_args aa;
    595 
    596 			aa.type = AUDIODEV_TYPE_OPL;
    597 			aa.hwif = aa.hdl = NULL;
    598 			dev = config_found(&sc->sc_dev, &aa, audioprint);
    599 			if (dev == 0)
    600 				bus_space_unmap(sc->sc_opl_iot,
    601 						sc->sc_opl_ioh, 4);
    602 			else {
    603 				if (SELECTABLE)
    604 					reg |= (i << (0+16));
    605 				break;
    606 			}
    607 		}
    608 	}
    609 	if (dev == 0) {
    610 		reg &= ~YDS_PCI_LEGACY_FMEN;
    611 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    612 			       YDS_PCI_LEGACY, reg);
    613 	} else {
    614 		/* Max. volume */
    615 		YWRITE4(sc, YDS_LEGACY_OUT_VOLUME, 0x3fff3fff);
    616 		YWRITE4(sc, YDS_LEGACY_REC_VOLUME, 0x3fff3fff);
    617 	}
    618 
    619 	/* Look for MPU */
    620 	dev = 0;
    621 	for (i = 0; i < sizeof(mpu_addrs) / sizeof(bus_addr_t); i++) {
    622 		if (SELECTABLE)
    623 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    624 				       YDS_PCI_LEGACY, reg | (i << (4+16)));
    625 		else
    626 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    627 				       YDS_PCI_MPU_BA, mpu_addrs[i]);
    628 		if (bus_space_map(sc->sc_mpu_iot,
    629 				  mpu_addrs[i], 2, 0, &sc->sc_mpu_ioh) == 0) {
    630 			struct audio_attach_args aa;
    631 
    632 			aa.type = AUDIODEV_TYPE_MPU;
    633 			aa.hwif = aa.hdl = NULL;
    634 			dev = config_found(&sc->sc_dev, &aa, audioprint);
    635 			if (dev == 0)
    636 				bus_space_unmap(sc->sc_mpu_iot,
    637 						sc->sc_mpu_ioh, 2);
    638 			else {
    639 				if (SELECTABLE)
    640 					reg |= (i << (4+16));
    641 				break;
    642 			}
    643 		}
    644 	}
    645 	if (dev == 0) {
    646 		reg &= ~(YDS_PCI_LEGACY_MEN | YDS_PCI_LEGACY_MIEN);
    647 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
    648 	}
    649 	sc->sc_mpu = dev;
    650 }
    651 #undef FLEXIBLE
    652 #undef SELECTABLE
    653 
    654 static int
    655 yds_init(struct yds_softc *sc)
    656 {
    657 	uint32_t reg;
    658 
    659 	DPRINTF(("yds_init()\n"));
    660 
    661 	/* Download microcode */
    662 	if (yds_download_mcode(sc)) {
    663 		printf("%s: download microcode failed\n", sc->sc_dev.dv_xname);
    664 		return 1;
    665 	}
    666 
    667 	/* Allocate DMA buffers */
    668 	if (yds_allocate_slots(sc)) {
    669 		printf("%s: could not allocate slots\n", sc->sc_dev.dv_xname);
    670 		return 1;
    671 	}
    672 
    673 	/* Warm reset */
    674 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
    675 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL,
    676 		reg | YDS_DSCTRL_WRST);
    677 	delay(50000);
    678 
    679 	return 0;
    680 }
    681 
    682 static void
    683 yds_powerhook(int why, void *addr)
    684 {
    685 	struct yds_softc *sc;
    686 	pci_chipset_tag_t pc;
    687 	pcitag_t tag;
    688 	pcireg_t reg;
    689 	int s;
    690 
    691 	sc = (struct yds_softc *)addr;
    692 	pc = sc->sc_pc;
    693 	tag = sc->sc_pcitag;
    694 
    695 	s = splaudio();
    696 	switch (why) {
    697 	case PWR_SUSPEND:
    698 		pci_conf_capture(pc, tag, &sc->sc_pciconf);
    699 
    700 		sc->sc_dsctrl = pci_conf_read(pc, tag, YDS_PCI_DSCTRL);
    701 		sc->sc_legacy = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
    702 		sc->sc_ba[0] = pci_conf_read(pc, tag, YDS_PCI_FM_BA);
    703 		sc->sc_ba[1] = pci_conf_read(pc, tag, YDS_PCI_MPU_BA);
    704 		break;
    705 	case PWR_RESUME:
    706 		pci_conf_restore(pc, tag, &sc->sc_pciconf);
    707 
    708 		/* Disable legacy mode */
    709 		reg = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
    710 		pci_conf_write(pc, tag, YDS_PCI_LEGACY,
    711 		    reg & YDS_PCI_LEGACY_LAD);
    712 
    713 		/* Enable the device. */
    714 		reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    715 		reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    716 			PCI_COMMAND_MASTER_ENABLE);
    717 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, reg);
    718 		reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    719 		if (yds_init(sc)) {
    720 			printf("%s: reinitialize failed\n",
    721 				sc->sc_dev.dv_xname);
    722 			splx(s);
    723 			return;
    724 		}
    725 		pci_conf_write(pc, tag, YDS_PCI_DSCTRL, sc->sc_dsctrl);
    726 		sc->sc_codec[0].codec_if->vtbl->restore_ports(sc->sc_codec[0].codec_if);
    727 		break;
    728 	case PWR_SOFTRESUME:
    729 		pci_conf_write(pc, tag, YDS_PCI_LEGACY, sc->sc_legacy);
    730 		pci_conf_write(pc, tag, YDS_PCI_FM_BA, sc->sc_ba[0]);
    731 		pci_conf_write(pc, tag, YDS_PCI_MPU_BA, sc->sc_ba[1]);
    732 #if notyet
    733 		yds_configure_legacy(addr);
    734 #endif
    735 		break;
    736 	}
    737 	splx(s);
    738 
    739 	return;
    740 }
    741 
    742 static void
    743 yds_attach(struct device *parent, struct device *self, void *aux)
    744 {
    745 	struct yds_softc *sc;
    746 	struct pci_attach_args *pa;
    747 	pci_chipset_tag_t pc;
    748 	char const *intrstr;
    749 	pci_intr_handle_t ih;
    750 	pcireg_t reg;
    751 	struct yds_codec_softc *codec;
    752 	char devinfo[256];
    753 	int i, r, to;
    754 	int revision;
    755 	int ac97_id2;
    756 
    757 	sc = (struct yds_softc *)self;
    758 	pa = (struct pci_attach_args *)aux;
    759 	pc = pa->pa_pc;
    760 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    761 	revision = PCI_REVISION(pa->pa_class);
    762 	printf(": %s (rev. 0x%02x)\n", devinfo, revision);
    763 
    764 	/* Map register to memory */
    765 	if (pci_mapreg_map(pa, YDS_PCI_MBA, PCI_MAPREG_TYPE_MEM, 0,
    766 			   &sc->memt, &sc->memh, NULL, NULL)) {
    767 		printf("%s: can't map memory space\n", sc->sc_dev.dv_xname);
    768 		return;
    769 	}
    770 
    771 	/* Map and establish the interrupt. */
    772 	if (pci_intr_map(pa, &ih)) {
    773 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    774 		return;
    775 	}
    776 	intrstr = pci_intr_string(pc, ih);
    777 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, yds_intr, sc);
    778 	if (sc->sc_ih == NULL) {
    779 		printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
    780 		if (intrstr != NULL)
    781 			printf(" at %s", intrstr);
    782 		printf("\n");
    783 		return;
    784 	}
    785 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    786 
    787 	sc->sc_dmatag = pa->pa_dmat;
    788 	sc->sc_pc = pc;
    789 	sc->sc_pcitag = pa->pa_tag;
    790 	sc->sc_id = pa->pa_id;
    791 	sc->sc_revision = revision;
    792 	sc->sc_flags = yds_get_dstype(sc->sc_id);
    793 #ifdef AUDIO_DEBUG
    794 	if (ydsdebug) {
    795 		char bits[80];
    796 
    797 		printf("%s: chip has %s\n", sc->sc_dev.dv_xname,
    798 		       bitmask_snprintf(sc->sc_flags, YDS_CAP_BITS, bits,
    799 					sizeof(bits)));
    800 	}
    801 #endif
    802 
    803 	/* Disable legacy mode */
    804 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_LEGACY);
    805 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_LEGACY,
    806 		       reg & YDS_PCI_LEGACY_LAD);
    807 
    808 	/* Enable the device. */
    809 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    810 	reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    811 		PCI_COMMAND_MASTER_ENABLE);
    812 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    813 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    814 
    815 	/* Mute all volumes */
    816 	for (i = 0x80; i < 0xc0; i += 2)
    817 		YWRITE2(sc, i, 0);
    818 
    819 	/* Initialize the device */
    820 	if (yds_init(sc)) {
    821 		printf("%s: initialize failed\n", sc->sc_dev.dv_xname);
    822 		return;
    823 	}
    824 
    825 	/*
    826 	 * Detect primary/secondary AC97
    827 	 *	YMF754 Hardware Specification Rev 1.01 page 24
    828 	 */
    829 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_DSCTRL);
    830 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
    831 	delay(400000);		/* Needed for 740C. */
    832 
    833 	/* Primary */
    834 	for (to = 0; to < AC97_TIMEOUT; to++) {
    835 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
    836 			break;
    837 		delay(1);
    838 	}
    839 	if (to == AC97_TIMEOUT) {
    840 		printf("%s: no AC97 available\n", sc->sc_dev.dv_xname);
    841 		return;
    842 	}
    843 
    844 	/* Secondary */
    845 	/* Secondary AC97 is used for 4ch audio. Currently unused. */
    846 	ac97_id2 = -1;
    847 	if ((YREAD2(sc, YDS_ACTIVITY) & YDS_ACTIVITY_DOCKA) == 0)
    848 		goto detected;
    849 #if 0				/* reset secondary... */
    850 	YWRITE2(sc, YDS_GPIO_OCTRL,
    851 		YREAD2(sc, YDS_GPIO_OCTRL) & ~YDS_GPIO_GPO2);
    852 	YWRITE2(sc, YDS_GPIO_FUNCE,
    853 		(YREAD2(sc, YDS_GPIO_FUNCE)&(~YDS_GPIO_GPC2))|YDS_GPIO_GPE2);
    854 #endif
    855 	for (to = 0; to < AC97_TIMEOUT; to++) {
    856 		if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY) == 0)
    857 			break;
    858 		delay(1);
    859 	}
    860 	if (to < AC97_TIMEOUT) {
    861 		/* detect id */
    862 		for (ac97_id2 = 1; ac97_id2 < 4; ac97_id2++) {
    863 			YWRITE2(sc, AC97_CMD_ADDR,
    864 				AC97_CMD_READ | AC97_ID(ac97_id2) | 0x28);
    865 
    866 			for (to = 0; to < AC97_TIMEOUT; to++) {
    867 				if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY)
    868 				    == 0)
    869 					goto detected;
    870 				delay(1);
    871 			}
    872 		}
    873 		if (ac97_id2 == 4)
    874 			ac97_id2 = -1;
    875 detected:
    876 		;
    877 	}
    878 
    879 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg | YDS_DSCTRL_CRST);
    880 	delay (20);
    881 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
    882 	delay (400000);
    883 	for (to = 0; to < AC97_TIMEOUT; to++) {
    884 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
    885 			break;
    886 		delay(1);
    887 	}
    888 
    889 	/*
    890 	 * Attach ac97 codec
    891 	 */
    892 	for (i = 0; i < 2; i++) {
    893 		static struct {
    894 			int data;
    895 			int addr;
    896 		} statregs[] = {
    897 			{AC97_STAT_DATA1, AC97_STAT_ADDR1},
    898 			{AC97_STAT_DATA2, AC97_STAT_ADDR2},
    899 		};
    900 
    901 		if (i == 1 && ac97_id2 == -1)
    902 			break;		/* secondary ac97 not available */
    903 
    904 		codec = &sc->sc_codec[i];
    905 		memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
    906 		codec->sc = sc;
    907 		codec->id = i == 1 ? ac97_id2 : 0;
    908 		codec->status_data = statregs[i].data;
    909 		codec->status_addr = statregs[i].addr;
    910 		codec->host_if.arg = codec;
    911 		codec->host_if.attach = yds_attach_codec;
    912 		codec->host_if.read = yds_read_codec;
    913 		codec->host_if.write = yds_write_codec;
    914 		codec->host_if.reset = yds_reset_codec;
    915 
    916 		if ((r = ac97_attach(&codec->host_if, self)) != 0) {
    917 			printf("%s: can't attach codec (error 0x%X)\n",
    918 			       sc->sc_dev.dv_xname, r);
    919 			return;
    920 		}
    921 	}
    922 
    923 	if (0 != auconv_create_encodings(yds_formats, YDS_NFORMATS,
    924 					 &sc->sc_encodings))
    925 		return;
    926 
    927 	audio_attach_mi(&yds_hw_if, sc, &sc->sc_dev);
    928 
    929 	sc->sc_legacy_iot = pa->pa_iot;
    930 	config_defer((struct device*) sc, yds_configure_legacy);
    931 
    932 	powerhook_establish(sc->sc_dev.dv_xname, yds_powerhook, sc);
    933 }
    934 
    935 static int
    936 yds_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    937 {
    938 	struct yds_codec_softc *sc;
    939 
    940 	sc = sc_;
    941 	sc->codec_if = codec_if;
    942 	return 0;
    943 }
    944 
    945 static int
    946 yds_ready_codec(struct yds_codec_softc *sc)
    947 {
    948 	int to;
    949 
    950 	for (to = 0; to < AC97_TIMEOUT; to++) {
    951 		if ((YREAD2(sc->sc, sc->status_addr) & AC97_BUSY) == 0)
    952 			return 0;
    953 		delay(1);
    954 	}
    955 
    956 	return 1;
    957 }
    958 
    959 static int
    960 yds_read_codec(void *sc_, uint8_t reg, uint16_t *data)
    961 {
    962 	struct yds_codec_softc *sc;
    963 
    964 	sc = sc_;
    965 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_READ | AC97_ID(sc->id) | reg);
    966 
    967 	if (yds_ready_codec(sc)) {
    968 		printf("%s: yds_read_codec timeout\n",
    969 		       sc->sc->sc_dev.dv_xname);
    970 		return EIO;
    971 	}
    972 
    973 	if (PCI_PRODUCT(sc->sc->sc_id) == PCI_PRODUCT_YAMAHA_YMF744B &&
    974 	    sc->sc->sc_revision < 2) {
    975 		int i;
    976 		for (i=0; i<600; i++)
    977 			(void)YREAD2(sc->sc, sc->status_data);
    978 	}
    979 
    980 	*data = YREAD2(sc->sc, sc->status_data);
    981 
    982 	return 0;
    983 }
    984 
    985 static int
    986 yds_write_codec(void *sc_, uint8_t reg, uint16_t data)
    987 {
    988 	struct yds_codec_softc *sc;
    989 
    990 	sc = sc_;
    991 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_WRITE | AC97_ID(sc->id) | reg);
    992 	YWRITE2(sc->sc, AC97_CMD_DATA, data);
    993 
    994 	if (yds_ready_codec(sc)) {
    995 		printf("%s: yds_write_codec timeout\n",
    996 			sc->sc->sc_dev.dv_xname);
    997 		return EIO;
    998 	}
    999 
   1000 	return 0;
   1001 }
   1002 
   1003 /*
   1004  * XXX: Must handle the secondary differntly!!
   1005  */
   1006 static int
   1007 yds_reset_codec(void *sc_)
   1008 {
   1009 	struct yds_codec_softc *codec;
   1010 	struct yds_softc *sc;
   1011 	pcireg_t reg;
   1012 
   1013 	codec = sc_;
   1014 	sc = codec->sc;
   1015 	/* reset AC97 codec */
   1016 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
   1017 	if (reg & 0x03) {
   1018 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   1019 			       YDS_PCI_DSCTRL, reg & ~0x03);
   1020 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   1021 			       YDS_PCI_DSCTRL, reg | 0x03);
   1022 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   1023 			       YDS_PCI_DSCTRL, reg & ~0x03);
   1024 		delay(50000);
   1025 	}
   1026 
   1027 	yds_ready_codec(sc_);
   1028 	return 0;
   1029 }
   1030 
   1031 static int
   1032 yds_intr(void *p)
   1033 {
   1034 	struct yds_softc *sc;
   1035 	u_int status;
   1036 
   1037 	sc = p;
   1038 	status = YREAD4(sc, YDS_STATUS);
   1039 	DPRINTFN(1, ("yds_intr: status=%08x\n", status));
   1040 	if ((status & (YDS_STAT_INT|YDS_STAT_TINT)) == 0) {
   1041 #if NMPU > 0
   1042 		if (sc->sc_mpu)
   1043 			return mpu_intr(sc->sc_mpu);
   1044 #endif
   1045 		return 0;
   1046 	}
   1047 
   1048 	if (status & YDS_STAT_TINT) {
   1049 		YWRITE4(sc, YDS_STATUS, YDS_STAT_TINT);
   1050 		printf ("yds_intr: timeout!\n");
   1051 	}
   1052 
   1053 	if (status & YDS_STAT_INT) {
   1054 		int nbank;
   1055 
   1056 		nbank = (YREAD4(sc, YDS_CONTROL_SELECT) == 0);
   1057 		/* Clear interrupt flag */
   1058 		YWRITE4(sc, YDS_STATUS, YDS_STAT_INT);
   1059 
   1060 		/* Buffer for the next frame is always ready. */
   1061 		YWRITE4(sc, YDS_MODE, YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV2);
   1062 
   1063 		if (sc->sc_play.intr) {
   1064 			u_int dma, ccpu, blk, len;
   1065 
   1066 			/* Sync play slot control data */
   1067 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1068 					sc->pbankoff,
   1069 					sizeof(struct play_slot_ctrl_bank)*
   1070 					    le32toh(*sc->ptbl)*
   1071 					    N_PLAY_SLOT_CTRL_BANK,
   1072 					BUS_DMASYNC_POSTWRITE|
   1073 					BUS_DMASYNC_POSTREAD);
   1074 			dma = le32toh(sc->pbankp[nbank]->pgstart) * sc->sc_play.factor;
   1075 			ccpu = sc->sc_play.offset;
   1076 			blk = sc->sc_play.blksize;
   1077 			len = sc->sc_play.length;
   1078 
   1079 			if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
   1080 			    ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
   1081 				/* We can fill the next block */
   1082 				/* Sync ring buffer for previous write */
   1083 				bus_dmamap_sync(sc->sc_dmatag,
   1084 						sc->sc_play.dma->map,
   1085 						ccpu, blk,
   1086 						BUS_DMASYNC_POSTWRITE);
   1087 				sc->sc_play.intr(sc->sc_play.intr_arg);
   1088 				sc->sc_play.offset += blk;
   1089 				if (sc->sc_play.offset >= len) {
   1090 					sc->sc_play.offset -= len;
   1091 #ifdef DIAGNOSTIC
   1092 					if (sc->sc_play.offset != 0)
   1093 						printf ("Audio ringbuffer botch\n");
   1094 #endif
   1095 				}
   1096 				/* Sync ring buffer for next write */
   1097 				bus_dmamap_sync(sc->sc_dmatag,
   1098 						sc->sc_play.dma->map,
   1099 						ccpu, blk,
   1100 						BUS_DMASYNC_PREWRITE);
   1101 			}
   1102 		}
   1103 		if (sc->sc_rec.intr) {
   1104 			u_int dma, ccpu, blk, len;
   1105 
   1106 			/* Sync rec slot control data */
   1107 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1108 					sc->rbankoff,
   1109 					sizeof(struct rec_slot_ctrl_bank)*
   1110 					    N_REC_SLOT_CTRL*
   1111 					    N_REC_SLOT_CTRL_BANK,
   1112 					BUS_DMASYNC_POSTWRITE|
   1113 					BUS_DMASYNC_POSTREAD);
   1114 			dma = le32toh(sc->rbank[YDS_INPUT_SLOT*2 + nbank].pgstartadr);
   1115 			ccpu = sc->sc_rec.offset;
   1116 			blk = sc->sc_rec.blksize;
   1117 			len = sc->sc_rec.length;
   1118 
   1119 			if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
   1120 			    ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
   1121 				/* We can drain the current block */
   1122 				/* Sync ring buffer first */
   1123 				bus_dmamap_sync(sc->sc_dmatag,
   1124 						sc->sc_rec.dma->map,
   1125 						ccpu, blk,
   1126 						BUS_DMASYNC_POSTREAD);
   1127 				sc->sc_rec.intr(sc->sc_rec.intr_arg);
   1128 				sc->sc_rec.offset += blk;
   1129 				if (sc->sc_rec.offset >= len) {
   1130 					sc->sc_rec.offset -= len;
   1131 #ifdef DIAGNOSTIC
   1132 					if (sc->sc_rec.offset != 0)
   1133 						printf ("Audio ringbuffer botch\n");
   1134 #endif
   1135 				}
   1136 				/* Sync ring buffer for next read */
   1137 				bus_dmamap_sync(sc->sc_dmatag,
   1138 						sc->sc_rec.dma->map,
   1139 						ccpu, blk,
   1140 						BUS_DMASYNC_PREREAD);
   1141 			}
   1142 		}
   1143 	}
   1144 
   1145 	return 1;
   1146 }
   1147 
   1148 static int
   1149 yds_allocmem(struct yds_softc *sc, size_t size, size_t align, struct yds_dma *p)
   1150 {
   1151 	int error;
   1152 
   1153 	p->size = size;
   1154 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
   1155 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
   1156 				 &p->nsegs, BUS_DMA_NOWAIT);
   1157 	if (error)
   1158 		return error;
   1159 
   1160 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
   1161 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
   1162 	if (error)
   1163 		goto free;
   1164 
   1165 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
   1166 				  0, BUS_DMA_NOWAIT, &p->map);
   1167 	if (error)
   1168 		goto unmap;
   1169 
   1170 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
   1171 				BUS_DMA_NOWAIT);
   1172 	if (error)
   1173 		goto destroy;
   1174 	return 0;
   1175 
   1176 destroy:
   1177 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1178 unmap:
   1179 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1180 free:
   1181 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1182 	return error;
   1183 }
   1184 
   1185 static int
   1186 yds_freemem(struct yds_softc *sc, struct yds_dma *p)
   1187 {
   1188 
   1189 	bus_dmamap_unload(sc->sc_dmatag, p->map);
   1190 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1191 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1192 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1193 	return 0;
   1194 }
   1195 
   1196 static int
   1197 yds_open(void *addr, int flags)
   1198 {
   1199 	struct yds_softc *sc;
   1200 	uint32_t mode;
   1201 
   1202 	sc = addr;
   1203 	/* Select bank 0. */
   1204 	YWRITE4(sc, YDS_CONTROL_SELECT, 0);
   1205 
   1206 	/* Start the DSP operation. */
   1207 	mode = YREAD4(sc, YDS_MODE);
   1208 	mode |= YDS_MODE_ACTV;
   1209 	mode &= ~YDS_MODE_ACTV2;
   1210 	YWRITE4(sc, YDS_MODE, mode);
   1211 
   1212 	return 0;
   1213 }
   1214 
   1215 /*
   1216  * Close function is called at splaudio().
   1217  */
   1218 static void
   1219 yds_close(void *addr)
   1220 {
   1221 
   1222 	yds_halt(addr);
   1223 }
   1224 
   1225 static int
   1226 yds_query_encoding(void *addr, struct audio_encoding *fp)
   1227 {
   1228 	struct yds_softc *sc;
   1229 
   1230 	sc = addr;
   1231 	return auconv_query_encoding(sc->sc_encodings, fp);
   1232 }
   1233 
   1234 static int
   1235 yds_set_params(void *addr, int setmode, int usemode,
   1236 	       audio_params_t *play, audio_params_t* rec,
   1237 	       stream_filter_list_t *pfil, stream_filter_list_t *rfil)
   1238 {
   1239 	if (setmode & AUMODE_RECORD) {
   1240 		if (auconv_set_converter(yds_formats, YDS_NFORMATS,
   1241 					 AUMODE_RECORD, rec, FALSE, rfil) < 0)
   1242 			return EINVAL;
   1243 	}
   1244 	if (setmode & AUMODE_PLAY) {
   1245 		if (auconv_set_converter(yds_formats, YDS_NFORMATS,
   1246 					 AUMODE_PLAY, play, FALSE, pfil) < 0)
   1247 			return EINVAL;
   1248 	}
   1249 	return 0;
   1250 }
   1251 
   1252 static int
   1253 yds_round_blocksize(void *addr, int blk, int mode,
   1254     const audio_params_t *param)
   1255 {
   1256 
   1257 	/*
   1258 	 * Block size must be bigger than a frame.
   1259 	 * That is 1024bytes at most, i.e. for 48000Hz, 16bit, 2ch.
   1260 	 */
   1261 	if (blk < 1024)
   1262 		blk = 1024;
   1263 
   1264 	return blk & ~4;
   1265 }
   1266 
   1267 static uint32_t
   1268 yds_get_lpfq(u_int sample_rate)
   1269 {
   1270 	int i;
   1271 	static struct lpfqt {
   1272 		u_int rate;
   1273 		uint32_t lpfq;
   1274 	} lpfqt[] = {
   1275 		{8000,  0x32020000},
   1276 		{11025, 0x31770000},
   1277 		{16000, 0x31390000},
   1278 		{22050, 0x31c90000},
   1279 		{32000, 0x33d00000},
   1280 		{48000, 0x40000000},
   1281 		{0, 0}
   1282 	};
   1283 
   1284 	if (sample_rate == 44100)		/* for P44 slot? */
   1285 		return 0x370A0000;
   1286 
   1287 	for (i = 0; lpfqt[i].rate != 0; i++)
   1288 		if (sample_rate <= lpfqt[i].rate)
   1289 			break;
   1290 
   1291 	return lpfqt[i].lpfq;
   1292 }
   1293 
   1294 static uint32_t
   1295 yds_get_lpfk(u_int sample_rate)
   1296 {
   1297 	int i;
   1298 	static struct lpfkt {
   1299 		u_int rate;
   1300 		uint32_t lpfk;
   1301 	} lpfkt[] = {
   1302 		{8000,  0x18b20000},
   1303 		{11025, 0x20930000},
   1304 		{16000, 0x2b9a0000},
   1305 		{22050, 0x35a10000},
   1306 		{32000, 0x3eaa0000},
   1307 		{48000, 0x40000000},
   1308 		{0, 0}
   1309 	};
   1310 
   1311 	if (sample_rate == 44100)		/* for P44 slot? */
   1312 		return 0x46460000;
   1313 
   1314 	for (i = 0; lpfkt[i].rate != 0; i++)
   1315 		if (sample_rate <= lpfkt[i].rate)
   1316 			break;
   1317 
   1318 	return lpfkt[i].lpfk;
   1319 }
   1320 
   1321 static int
   1322 yds_trigger_output(void *addr, void *start, void *end, int blksize,
   1323 		   void (*intr)(void *), void *arg, const audio_params_t *param)
   1324 #define P44		(sc->sc_flags & YDS_CAP_HAS_P44)
   1325 {
   1326 	struct yds_softc *sc;
   1327 	struct yds_dma *p;
   1328 	struct play_slot_ctrl_bank *psb;
   1329 	const u_int gain = 0x40000000;
   1330 	bus_addr_t s;
   1331 	size_t l;
   1332 	int i;
   1333 	int p44, channels;
   1334 	uint32_t format;
   1335 
   1336 	sc = addr;
   1337 #ifdef DIAGNOSTIC
   1338 	if (sc->sc_play.intr)
   1339 		panic("yds_trigger_output: already running");
   1340 #endif
   1341 
   1342 	sc->sc_play.intr = intr;
   1343 	sc->sc_play.intr_arg = arg;
   1344 	sc->sc_play.offset = 0;
   1345 	sc->sc_play.blksize = blksize;
   1346 
   1347 	DPRINTFN(1, ("yds_trigger_output: sc=%p start=%p end=%p "
   1348 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1349 
   1350 	p = yds_find_dma(sc, start);
   1351 	if (!p) {
   1352 		printf("yds_trigger_output: bad addr %p\n", start);
   1353 		return EINVAL;
   1354 	}
   1355 	sc->sc_play.dma = p;
   1356 
   1357 #ifdef YDS_USE_P44
   1358 	/* The document says the P44 SRC supports only stereo, 16bit PCM. */
   1359 	if (P44)
   1360 		p44 = ((param->sample_rate == 44100) &&
   1361 		       (param->channels == 2) &&
   1362 		       (param->precision == 16));
   1363 	else
   1364 #endif
   1365 		p44 = 0;
   1366 	channels = p44 ? 1 : param->channels;
   1367 
   1368 	s = DMAADDR(p);
   1369 	l = ((char *)end - (char *)start);
   1370 	sc->sc_play.length = l;
   1371 
   1372 	*sc->ptbl = htole32(channels);	/* Num of play */
   1373 
   1374 	sc->sc_play.factor = 1;
   1375 	if (param->channels == 2)
   1376 		sc->sc_play.factor *= 2;
   1377 	if (param->precision != 8)
   1378 		sc->sc_play.factor *= 2;
   1379 	l /= sc->sc_play.factor;
   1380 
   1381 	format = ((channels == 2 ? PSLT_FORMAT_STEREO : 0) |
   1382 		  (param->precision == 8 ? PSLT_FORMAT_8BIT : 0) |
   1383 		  (p44 ? PSLT_FORMAT_SRC441 : 0));
   1384 
   1385 	psb = sc->pbankp[0];
   1386 	memset(psb, 0, sizeof(*psb));
   1387 	psb->format = htole32(format);
   1388 	psb->pgbase = htole32(s);
   1389 	psb->pgloopend = htole32(l);
   1390 	if (!p44) {
   1391 		psb->pgdeltaend = htole32((param->sample_rate * 65536 / 48000) << 12);
   1392 		psb->lpfkend = htole32(yds_get_lpfk(param->sample_rate));
   1393 		psb->eggainend = htole32(gain);
   1394 		psb->lpfq = htole32(yds_get_lpfq(param->sample_rate));
   1395 		psb->pgdelta = htole32(psb->pgdeltaend);
   1396 		psb->lpfk = htole32(yds_get_lpfk(param->sample_rate));
   1397 		psb->eggain = htole32(gain);
   1398 	}
   1399 
   1400 	for (i = 0; i < channels; i++) {
   1401 		/* i == 0: left or mono, i == 1: right */
   1402 		psb = sc->pbankp[i*2];
   1403 		if (i)
   1404 			/* copy from left */
   1405 			*psb = *(sc->pbankp[0]);
   1406 		if (channels == 2) {
   1407 			/* stereo */
   1408 			if (i == 0) {
   1409 				psb->lchgain = psb->lchgainend = htole32(gain);
   1410 			} else {
   1411 				psb->lchgain = psb->lchgainend = 0;
   1412 				psb->rchgain = psb->rchgainend = htole32(gain);
   1413 				psb->format |= htole32(PSLT_FORMAT_RCH);
   1414 			}
   1415 		} else if (!p44) {
   1416 			/* mono */
   1417 			psb->lchgain = psb->rchgain = htole32(gain);
   1418 			psb->lchgainend = psb->rchgainend = htole32(gain);
   1419 		}
   1420 		/* copy to the other bank */
   1421 		*(sc->pbankp[i*2+1]) = *psb;
   1422 	}
   1423 
   1424 	YDS_DUMP_PLAY_SLOT(5, sc, 0);
   1425 	YDS_DUMP_PLAY_SLOT(5, sc, 1);
   1426 
   1427 	if (p44)
   1428 		YWRITE4(sc, YDS_P44_OUT_VOLUME, 0x3fff3fff);
   1429 	else
   1430 		YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0x3fff3fff);
   1431 
   1432 	/* Now the play slot for the next frame is set up!! */
   1433 	/* Sync play slot control data for both directions */
   1434 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1435 			sc->ptbloff,
   1436 			sizeof(struct play_slot_ctrl_bank) *
   1437 			    channels * N_PLAY_SLOT_CTRL_BANK,
   1438 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   1439 	/* Sync ring buffer */
   1440 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
   1441 			BUS_DMASYNC_PREWRITE);
   1442 	/* HERE WE GO!! */
   1443 	YWRITE4(sc, YDS_MODE,
   1444 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
   1445 
   1446 	return 0;
   1447 }
   1448 #undef P44
   1449 
   1450 static int
   1451 yds_trigger_input(void *addr, void *start, void *end, int blksize,
   1452 		  void (*intr)(void *), void *arg, const audio_params_t *param)
   1453 {
   1454 	struct yds_softc *sc;
   1455 	struct yds_dma *p;
   1456 	u_int srate, format;
   1457 	struct rec_slot_ctrl_bank *rsb;
   1458 	bus_addr_t s;
   1459 	size_t l;
   1460 
   1461 	sc = addr;
   1462 #ifdef DIAGNOSTIC
   1463 	if (sc->sc_rec.intr)
   1464 		panic("yds_trigger_input: already running");
   1465 #endif
   1466 	sc->sc_rec.intr = intr;
   1467 	sc->sc_rec.intr_arg = arg;
   1468 	sc->sc_rec.offset = 0;
   1469 	sc->sc_rec.blksize = blksize;
   1470 
   1471 	DPRINTFN(1, ("yds_trigger_input: "
   1472 	    "sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1473 	    addr, start, end, blksize, intr, arg));
   1474 	DPRINTFN(1, (" parameters: rate=%u, precision=%u, channels=%u\n",
   1475 	    param->sample_rate, param->precision, param->channels));
   1476 
   1477 	p = yds_find_dma(sc, start);
   1478 	if (!p) {
   1479 		printf("yds_trigger_input: bad addr %p\n", start);
   1480 		return EINVAL;
   1481 	}
   1482 	sc->sc_rec.dma = p;
   1483 
   1484 	s = DMAADDR(p);
   1485 	l = ((char *)end - (char *)start);
   1486 	sc->sc_rec.length = l;
   1487 
   1488 	sc->sc_rec.factor = 1;
   1489 	if (param->channels == 2)
   1490 		sc->sc_rec.factor *= 2;
   1491 	if (param->precision != 8)
   1492 		sc->sc_rec.factor *= 2;
   1493 
   1494 	rsb = &sc->rbank[0];
   1495 	memset(rsb, 0, sizeof(*rsb));
   1496 	rsb->pgbase = htole32(s);
   1497 	rsb->pgloopendadr = htole32(l);
   1498 	/* Seems all 4 banks must be set up... */
   1499 	sc->rbank[1] = *rsb;
   1500 	sc->rbank[2] = *rsb;
   1501 	sc->rbank[3] = *rsb;
   1502 
   1503 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0x3fff3fff);
   1504 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0x3fff3fff);
   1505 	srate = 48000 * 4096 / param->sample_rate - 1;
   1506 	format = ((param->precision == 8 ? YDS_FORMAT_8BIT : 0) |
   1507 		  (param->channels == 2 ? YDS_FORMAT_STEREO : 0));
   1508 	DPRINTF(("srate=%d, format=%08x\n", srate, format));
   1509 #ifdef YDS_USE_REC_SLOT
   1510 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0x3fff3fff);
   1511 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0x3fff3fff);
   1512 	YWRITE4(sc, YDS_MAPOF_REC, YDS_RECSLOT_VALID);
   1513 	YWRITE4(sc, YDS_REC_SAMPLE_RATE, srate);
   1514 	YWRITE4(sc, YDS_REC_FORMAT, format);
   1515 #else
   1516 	YWRITE4(sc, YDS_MAPOF_REC, YDS_ADCSLOT_VALID);
   1517 	YWRITE4(sc, YDS_ADC_SAMPLE_RATE, srate);
   1518 	YWRITE4(sc, YDS_ADC_FORMAT, format);
   1519 #endif
   1520 	/* Now the rec slot for the next frame is set up!! */
   1521 	/* Sync record slot control data */
   1522 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1523 			sc->rbankoff,
   1524 			sizeof(struct rec_slot_ctrl_bank)*
   1525 			    N_REC_SLOT_CTRL*
   1526 			    N_REC_SLOT_CTRL_BANK,
   1527 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   1528 	/* Sync ring buffer */
   1529 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
   1530 			BUS_DMASYNC_PREREAD);
   1531 	/* HERE WE GO!! */
   1532 	YWRITE4(sc, YDS_MODE,
   1533 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
   1534 
   1535 	return 0;
   1536 }
   1537 
   1538 static int
   1539 yds_halt(struct yds_softc *sc)
   1540 {
   1541 	uint32_t mode;
   1542 
   1543 	/* Stop the DSP operation. */
   1544 	mode = YREAD4(sc, YDS_MODE);
   1545 	YWRITE4(sc, YDS_MODE, mode & ~(YDS_MODE_ACTV|YDS_MODE_ACTV2));
   1546 
   1547 	/* Paranoia...  mute all */
   1548 	YWRITE4(sc, YDS_P44_OUT_VOLUME, 0);
   1549 	YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0);
   1550 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0);
   1551 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0);
   1552 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0);
   1553 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0);
   1554 
   1555 	return 0;
   1556 }
   1557 
   1558 static int
   1559 yds_halt_output(void *addr)
   1560 {
   1561 	struct yds_softc *sc;
   1562 
   1563 	DPRINTF(("yds: yds_halt_output\n"));
   1564 	sc = addr;
   1565 	if (sc->sc_play.intr) {
   1566 		sc->sc_play.intr = 0;
   1567 		/* Sync play slot control data */
   1568 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1569 				sc->pbankoff,
   1570 				sizeof(struct play_slot_ctrl_bank)*
   1571 				    (*sc->ptbl)*N_PLAY_SLOT_CTRL_BANK,
   1572 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
   1573 		/* Stop the play slot operation */
   1574 		sc->pbankp[0]->status =
   1575 		sc->pbankp[1]->status =
   1576 		sc->pbankp[2]->status =
   1577 		sc->pbankp[3]->status = 1;
   1578 		/* Sync ring buffer */
   1579 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_play.dma->map,
   1580 				0, sc->sc_play.length, BUS_DMASYNC_POSTWRITE);
   1581 	}
   1582 
   1583 	return 0;
   1584 }
   1585 
   1586 static int
   1587 yds_halt_input(void *addr)
   1588 {
   1589 	struct yds_softc *sc;
   1590 
   1591 	DPRINTF(("yds: yds_halt_input\n"));
   1592 	sc = addr;
   1593 	sc->sc_rec.intr = NULL;
   1594 	if (sc->sc_rec.intr) {
   1595 		/* Stop the rec slot operation */
   1596 		YWRITE4(sc, YDS_MAPOF_REC, 0);
   1597 		sc->sc_rec.intr = 0;
   1598 		/* Sync rec slot control data */
   1599 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1600 				sc->rbankoff,
   1601 				sizeof(struct rec_slot_ctrl_bank)*
   1602 				    N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK,
   1603 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
   1604 		/* Sync ring buffer */
   1605 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_rec.dma->map,
   1606 				0, sc->sc_rec.length, BUS_DMASYNC_POSTREAD);
   1607 	}
   1608 
   1609 	return 0;
   1610 }
   1611 
   1612 static int
   1613 yds_getdev(void *addr, struct audio_device *retp)
   1614 {
   1615 
   1616 	*retp = yds_device;
   1617 	return 0;
   1618 }
   1619 
   1620 static int
   1621 yds_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1622 {
   1623 	struct yds_softc *sc;
   1624 
   1625 	sc = addr;
   1626 	return sc->sc_codec[0].codec_if->vtbl->mixer_set_port(
   1627 	    sc->sc_codec[0].codec_if, cp);
   1628 }
   1629 
   1630 static int
   1631 yds_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1632 {
   1633 	struct yds_softc *sc;
   1634 
   1635 	sc = addr;
   1636 	return sc->sc_codec[0].codec_if->vtbl->mixer_get_port(
   1637 	    sc->sc_codec[0].codec_if, cp);
   1638 }
   1639 
   1640 static int
   1641 yds_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1642 {
   1643 	struct yds_softc *sc;
   1644 
   1645 	sc = addr;
   1646 	return sc->sc_codec[0].codec_if->vtbl->query_devinfo(
   1647 	    sc->sc_codec[0].codec_if, dip);
   1648 }
   1649 
   1650 static void *
   1651 yds_malloc(void *addr, int direction, size_t size,
   1652 	   struct malloc_type *pool, int flags)
   1653 {
   1654 	struct yds_softc *sc;
   1655 	struct yds_dma *p;
   1656 	int error;
   1657 
   1658 	p = malloc(sizeof(*p), pool, flags);
   1659 	if (p == NULL)
   1660 		return NULL;
   1661 	sc = addr;
   1662 	error = yds_allocmem(sc, size, 16, p);
   1663 	if (error) {
   1664 		free(p, pool);
   1665 		return NULL;
   1666 	}
   1667 	p->next = sc->sc_dmas;
   1668 	sc->sc_dmas = p;
   1669 	return KERNADDR(p);
   1670 }
   1671 
   1672 static void
   1673 yds_free(void *addr, void *ptr, struct malloc_type *pool)
   1674 {
   1675 	struct yds_softc *sc;
   1676 	struct yds_dma **pp, *p;
   1677 
   1678 	sc = addr;
   1679 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1680 		if (KERNADDR(p) == ptr) {
   1681 			yds_freemem(sc, p);
   1682 			*pp = p->next;
   1683 			free(p, pool);
   1684 			return;
   1685 		}
   1686 	}
   1687 }
   1688 
   1689 static struct yds_dma *
   1690 yds_find_dma(struct yds_softc *sc, void *addr)
   1691 {
   1692 	struct yds_dma *p;
   1693 
   1694 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
   1695 		continue;
   1696 
   1697 	return p;
   1698 }
   1699 
   1700 static size_t
   1701 yds_round_buffersize(void *addr, int direction, size_t size)
   1702 {
   1703 
   1704 	/*
   1705 	 * Buffer size should be at least twice as bigger as a frame.
   1706 	 */
   1707 	if (size < 1024 * 3)
   1708 		size = 1024 * 3;
   1709 	return size;
   1710 }
   1711 
   1712 static paddr_t
   1713 yds_mappage(void *addr, void *mem, off_t off, int prot)
   1714 {
   1715 	struct yds_softc *sc;
   1716 	struct yds_dma *p;
   1717 
   1718 	if (off < 0)
   1719 		return -1;
   1720 	sc = addr;
   1721 	p = yds_find_dma(sc, mem);
   1722 	if (p == NULL)
   1723 		return -1;
   1724 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1725 	    off, prot, BUS_DMA_WAITOK);
   1726 }
   1727 
   1728 static int
   1729 yds_get_props(void *addr)
   1730 {
   1731 
   1732 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1733 	    AUDIO_PROP_FULLDUPLEX;
   1734 }
   1735