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