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