Home | History | Annotate | Line # | Download | only in pci
yds.c revision 1.24
      1 /*	$NetBSD: yds.c,v 1.24 2004/11/09 11:12:54 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.24 2004/11/09 11:12:54 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 	int i, r, to;
    691 	int revision;
    692 	int ac97_id2;
    693 
    694 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    695 	revision = PCI_REVISION(pa->pa_class);
    696 	printf(": %s (rev. 0x%02x)\n", devinfo, revision);
    697 
    698 	/* Map register to memory */
    699 	if (pci_mapreg_map(pa, YDS_PCI_MBA, PCI_MAPREG_TYPE_MEM, 0,
    700 			   &sc->memt, &sc->memh, NULL, NULL)) {
    701 		printf("%s: can't map memory space\n", sc->sc_dev.dv_xname);
    702 		return;
    703 	}
    704 
    705 	/* Map and establish the interrupt. */
    706 	if (pci_intr_map(pa, &ih)) {
    707 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    708 		return;
    709 	}
    710 	intrstr = pci_intr_string(pc, ih);
    711 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, yds_intr, sc);
    712 	if (sc->sc_ih == NULL) {
    713 		printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
    714 		if (intrstr != NULL)
    715 			printf(" at %s", intrstr);
    716 		printf("\n");
    717 		return;
    718 	}
    719 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    720 
    721 	sc->sc_dmatag = pa->pa_dmat;
    722 	sc->sc_pc = pc;
    723 	sc->sc_pcitag = pa->pa_tag;
    724 	sc->sc_id = pa->pa_id;
    725 	sc->sc_revision = revision;
    726 	sc->sc_flags = yds_get_dstype(sc->sc_id);
    727 #ifdef AUDIO_DEBUG
    728 	if (ydsdebug) {
    729 		char bits[80];
    730 
    731 		printf("%s: chip has %s\n", sc->sc_dev.dv_xname,
    732 		       bitmask_snprintf(sc->sc_flags, YDS_CAP_BITS, bits,
    733 					sizeof(bits)));
    734 	}
    735 #endif
    736 
    737 	/* Disable legacy mode */
    738 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_LEGACY);
    739 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_LEGACY,
    740 		       reg & YDS_PCI_LEGACY_LAD);
    741 
    742 	/* Enable the device. */
    743 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    744 	reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    745 		PCI_COMMAND_MASTER_ENABLE);
    746 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    747 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    748 
    749 	/* Mute all volumes */
    750 	for (i = 0x80; i < 0xc0; i += 2)
    751 		YWRITE2(sc, i, 0);
    752 
    753 	/* Initialize the device */
    754 	if (yds_init(sc)) {
    755 		printf("%s: initialize failed\n", sc->sc_dev.dv_xname);
    756 		return;
    757 	}
    758 
    759 	/*
    760 	 * Detect primary/secondary AC97
    761 	 *	YMF754 Hardware Specification Rev 1.01 page 24
    762 	 */
    763 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_DSCTRL);
    764 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
    765 	delay(400000);		/* Needed for 740C. */
    766 
    767 	/* Primary */
    768 	for (to = 0; to < AC97_TIMEOUT; to++) {
    769 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
    770 			break;
    771 		delay(1);
    772 	}
    773 	if (to == AC97_TIMEOUT) {
    774 		printf("%s: no AC97 available\n", sc->sc_dev.dv_xname);
    775 		return;
    776 	}
    777 
    778 	/* Secondary */
    779 	/* Secondary AC97 is used for 4ch audio. Currently unused. */
    780 	ac97_id2 = -1;
    781 	if ((YREAD2(sc, YDS_ACTIVITY) & YDS_ACTIVITY_DOCKA) == 0)
    782 		goto detected;
    783 #if 0				/* reset secondary... */
    784 	YWRITE2(sc, YDS_GPIO_OCTRL,
    785 		YREAD2(sc, YDS_GPIO_OCTRL) & ~YDS_GPIO_GPO2);
    786 	YWRITE2(sc, YDS_GPIO_FUNCE,
    787 		(YREAD2(sc, YDS_GPIO_FUNCE)&(~YDS_GPIO_GPC2))|YDS_GPIO_GPE2);
    788 #endif
    789 	for (to = 0; to < AC97_TIMEOUT; to++) {
    790 		if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY) == 0)
    791 			break;
    792 		delay(1);
    793 	}
    794 	if (to < AC97_TIMEOUT) {
    795 		/* detect id */
    796 		for (ac97_id2 = 1; ac97_id2 < 4; ac97_id2++) {
    797 			YWRITE2(sc, AC97_CMD_ADDR,
    798 				AC97_CMD_READ | AC97_ID(ac97_id2) | 0x28);
    799 
    800 			for (to = 0; to < AC97_TIMEOUT; to++) {
    801 				if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY)
    802 				    == 0)
    803 					goto detected;
    804 				delay(1);
    805 			}
    806 		}
    807 		if (ac97_id2 == 4)
    808 			ac97_id2 = -1;
    809 detected:
    810 		;
    811 	}
    812 
    813 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg | YDS_DSCTRL_CRST);
    814 	delay (20);
    815 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
    816 	delay (400000);
    817 	for (to = 0; to < AC97_TIMEOUT; to++) {
    818 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
    819 			break;
    820 		delay(1);
    821 	}
    822 
    823 	/*
    824 	 * Attach ac97 codec
    825 	 */
    826 	for (i = 0; i < 2; i++) {
    827 		static struct {
    828 			int data;
    829 			int addr;
    830 		} statregs[] = {
    831 			{AC97_STAT_DATA1, AC97_STAT_ADDR1},
    832 			{AC97_STAT_DATA2, AC97_STAT_ADDR2},
    833 		};
    834 
    835 		if (i == 1 && ac97_id2 == -1)
    836 			break;		/* secondary ac97 not available */
    837 
    838 		codec = &sc->sc_codec[i];
    839 		memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
    840 		codec->sc = sc;
    841 		codec->id = i == 1 ? ac97_id2 : 0;
    842 		codec->status_data = statregs[i].data;
    843 		codec->status_addr = statregs[i].addr;
    844 		codec->host_if.arg = codec;
    845 		codec->host_if.attach = yds_attach_codec;
    846 		codec->host_if.read = yds_read_codec;
    847 		codec->host_if.write = yds_write_codec;
    848 		codec->host_if.reset = yds_reset_codec;
    849 
    850 		if ((r = ac97_attach(&codec->host_if)) != 0) {
    851 			printf("%s: can't attach codec (error 0x%X)\n",
    852 			       sc->sc_dev.dv_xname, r);
    853 			return;
    854 		}
    855 	}
    856 
    857 	audio_attach_mi(&yds_hw_if, sc, &sc->sc_dev);
    858 
    859 	sc->sc_legacy_iot = pa->pa_iot;
    860 	config_defer((struct device*) sc, yds_configure_legacy);
    861 
    862 	powerhook_establish(yds_powerhook, sc);
    863 }
    864 
    865 int
    866 yds_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    867 {
    868 	struct yds_codec_softc *sc = sc_;
    869 
    870 	sc->codec_if = codec_if;
    871 	return 0;
    872 }
    873 
    874 static int
    875 yds_ready_codec(struct yds_codec_softc *sc)
    876 {
    877 	int to;
    878 
    879 	for (to = 0; to < AC97_TIMEOUT; to++) {
    880 		if ((YREAD2(sc->sc, sc->status_addr) & AC97_BUSY) == 0)
    881 			return 0;
    882 		delay(1);
    883 	}
    884 
    885 	return 1;
    886 }
    887 
    888 int
    889 yds_read_codec(void *sc_, u_int8_t reg, u_int16_t *data)
    890 {
    891 	struct yds_codec_softc *sc = sc_;
    892 
    893 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_READ | AC97_ID(sc->id) | reg);
    894 
    895 	if (yds_ready_codec(sc)) {
    896 		printf("%s: yds_read_codec timeout\n",
    897 		       sc->sc->sc_dev.dv_xname);
    898 		return EIO;
    899 	}
    900 
    901 	if (PCI_PRODUCT(sc->sc->sc_id) == PCI_PRODUCT_YAMAHA_YMF744B &&
    902 	    sc->sc->sc_revision < 2) {
    903 		int i;
    904 		for (i=0; i<600; i++)
    905 			YREAD2(sc->sc, sc->status_data);
    906 	}
    907 
    908 	*data = YREAD2(sc->sc, sc->status_data);
    909 
    910 	return 0;
    911 }
    912 
    913 int
    914 yds_write_codec(void *sc_, u_int8_t reg, u_int16_t data)
    915 {
    916 	struct yds_codec_softc *sc = sc_;
    917 
    918 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_WRITE | AC97_ID(sc->id) | reg);
    919 	YWRITE2(sc->sc, AC97_CMD_DATA, data);
    920 
    921 	if (yds_ready_codec(sc)) {
    922 		printf("%s: yds_write_codec timeout\n",
    923 			sc->sc->sc_dev.dv_xname);
    924 		return EIO;
    925 	}
    926 
    927 	return 0;
    928 }
    929 
    930 /*
    931  * XXX: Must handle the secondary differntly!!
    932  */
    933 int
    934 yds_reset_codec(void *sc_)
    935 {
    936 	struct yds_codec_softc *codec = sc_;
    937 	struct yds_softc *sc = codec->sc;
    938 	pcireg_t reg;
    939 
    940 	/* reset AC97 codec */
    941 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
    942 	if (reg & 0x03) {
    943 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    944 			       YDS_PCI_DSCTRL, reg & ~0x03);
    945 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    946 			       YDS_PCI_DSCTRL, reg | 0x03);
    947 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    948 			       YDS_PCI_DSCTRL, reg & ~0x03);
    949 		delay(50000);
    950 	}
    951 
    952 	yds_ready_codec(sc_);
    953 	return 0;
    954 }
    955 
    956 int
    957 yds_intr(void *p)
    958 {
    959 	struct yds_softc *sc = p;
    960 	u_int status;
    961 
    962 	status = YREAD4(sc, YDS_STATUS);
    963 	DPRINTFN(1, ("yds_intr: status=%08x\n", status));
    964 	if ((status & (YDS_STAT_INT|YDS_STAT_TINT)) == 0) {
    965 #if NMPU > 0
    966 		if (sc->sc_mpu)
    967 			return mpu_intr(sc->sc_mpu);
    968 #endif
    969 		return 0;
    970 	}
    971 
    972 	if (status & YDS_STAT_TINT) {
    973 		YWRITE4(sc, YDS_STATUS, YDS_STAT_TINT);
    974 		printf ("yds_intr: timeout!\n");
    975 	}
    976 
    977 	if (status & YDS_STAT_INT) {
    978 		int nbank = (YREAD4(sc, YDS_CONTROL_SELECT) == 0);
    979 
    980 		/* Clear interrupt flag */
    981 		YWRITE4(sc, YDS_STATUS, YDS_STAT_INT);
    982 
    983 		/* Buffer for the next frame is always ready. */
    984 		YWRITE4(sc, YDS_MODE, YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV2);
    985 
    986 		if (sc->sc_play.intr) {
    987 			u_int dma, cpu, blk, len;
    988 
    989 			/* Sync play slot control data */
    990 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
    991 					sc->pbankoff,
    992 					sizeof(struct play_slot_ctrl_bank)*
    993 					    le32toh(*sc->ptbl)*
    994 					    N_PLAY_SLOT_CTRL_BANK,
    995 					BUS_DMASYNC_POSTWRITE|
    996 					BUS_DMASYNC_POSTREAD);
    997 			dma = le32toh(sc->pbankp[nbank]->pgstart) * sc->sc_play.factor;
    998 			cpu = sc->sc_play.offset;
    999 			blk = sc->sc_play.blksize;
   1000 			len = sc->sc_play.length;
   1001 
   1002 			if (((dma > cpu) && (dma - cpu > blk * 2)) ||
   1003 			    ((cpu > dma) && (dma + len - cpu > blk * 2))) {
   1004 				/* We can fill the next block */
   1005 				/* Sync ring buffer for previous write */
   1006 				bus_dmamap_sync(sc->sc_dmatag,
   1007 						sc->sc_play.dma->map,
   1008 						cpu, blk,
   1009 						BUS_DMASYNC_POSTWRITE);
   1010 				sc->sc_play.intr(sc->sc_play.intr_arg);
   1011 				sc->sc_play.offset += blk;
   1012 				if (sc->sc_play.offset >= len) {
   1013 					sc->sc_play.offset -= len;
   1014 #ifdef DIAGNOSTIC
   1015 					if (sc->sc_play.offset != 0)
   1016 						printf ("Audio ringbuffer botch\n");
   1017 #endif
   1018 				}
   1019 				/* Sync ring buffer for next write */
   1020 				bus_dmamap_sync(sc->sc_dmatag,
   1021 						sc->sc_play.dma->map,
   1022 						cpu, blk,
   1023 						BUS_DMASYNC_PREWRITE);
   1024 			}
   1025 		}
   1026 		if (sc->sc_rec.intr) {
   1027 			u_int dma, cpu, blk, len;
   1028 
   1029 			/* Sync rec slot control data */
   1030 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1031 					sc->rbankoff,
   1032 					sizeof(struct rec_slot_ctrl_bank)*
   1033 					    N_REC_SLOT_CTRL*
   1034 					    N_REC_SLOT_CTRL_BANK,
   1035 					BUS_DMASYNC_POSTWRITE|
   1036 					BUS_DMASYNC_POSTREAD);
   1037 			dma = le32toh(sc->rbank[YDS_INPUT_SLOT*2 + nbank].pgstartadr);
   1038 			cpu = sc->sc_rec.offset;
   1039 			blk = sc->sc_rec.blksize;
   1040 			len = sc->sc_rec.length;
   1041 
   1042 			if (((dma > cpu) && (dma - cpu > blk * 2)) ||
   1043 			    ((cpu > dma) && (dma + len - cpu > blk * 2))) {
   1044 				/* We can drain the current block */
   1045 				/* Sync ring buffer first */
   1046 				bus_dmamap_sync(sc->sc_dmatag,
   1047 						sc->sc_rec.dma->map,
   1048 						cpu, blk,
   1049 						BUS_DMASYNC_POSTREAD);
   1050 				sc->sc_rec.intr(sc->sc_rec.intr_arg);
   1051 				sc->sc_rec.offset += blk;
   1052 				if (sc->sc_rec.offset >= len) {
   1053 					sc->sc_rec.offset -= len;
   1054 #ifdef DIAGNOSTIC
   1055 					if (sc->sc_rec.offset != 0)
   1056 						printf ("Audio ringbuffer botch\n");
   1057 #endif
   1058 				}
   1059 				/* Sync ring buffer for next read */
   1060 				bus_dmamap_sync(sc->sc_dmatag,
   1061 						sc->sc_rec.dma->map,
   1062 						cpu, blk,
   1063 						BUS_DMASYNC_PREREAD);
   1064 			}
   1065 		}
   1066 	}
   1067 
   1068 	return 1;
   1069 }
   1070 
   1071 int
   1072 yds_allocmem(struct yds_softc *sc, size_t size, size_t align, struct yds_dma *p)
   1073 {
   1074 	int error;
   1075 
   1076 	p->size = size;
   1077 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
   1078 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
   1079 				 &p->nsegs, BUS_DMA_NOWAIT);
   1080 	if (error)
   1081 		return (error);
   1082 
   1083 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
   1084 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
   1085 	if (error)
   1086 		goto free;
   1087 
   1088 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
   1089 				  0, BUS_DMA_NOWAIT, &p->map);
   1090 	if (error)
   1091 		goto unmap;
   1092 
   1093 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
   1094 				BUS_DMA_NOWAIT);
   1095 	if (error)
   1096 		goto destroy;
   1097 	return (0);
   1098 
   1099 destroy:
   1100 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1101 unmap:
   1102 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1103 free:
   1104 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1105 	return (error);
   1106 }
   1107 
   1108 int
   1109 yds_freemem(struct yds_softc *sc, struct yds_dma *p)
   1110 {
   1111 	bus_dmamap_unload(sc->sc_dmatag, p->map);
   1112 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1113 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1114 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1115 	return 0;
   1116 }
   1117 
   1118 int
   1119 yds_open(void *addr, int flags)
   1120 {
   1121 	struct yds_softc *sc = addr;
   1122 	u_int32_t mode;
   1123 
   1124 	/* Select bank 0. */
   1125 	YWRITE4(sc, YDS_CONTROL_SELECT, 0);
   1126 
   1127 	/* Start the DSP operation. */
   1128 	mode = YREAD4(sc, YDS_MODE);
   1129 	mode |= YDS_MODE_ACTV;
   1130 	mode &= ~YDS_MODE_ACTV2;
   1131 	YWRITE4(sc, YDS_MODE, mode);
   1132 
   1133 	return 0;
   1134 }
   1135 
   1136 /*
   1137  * Close function is called at splaudio().
   1138  */
   1139 void
   1140 yds_close(void *addr)
   1141 {
   1142 	struct yds_softc *sc = addr;
   1143 
   1144 	yds_halt(sc);
   1145 }
   1146 
   1147 int
   1148 yds_query_encoding(void *addr, struct audio_encoding *fp)
   1149 {
   1150 	switch (fp->index) {
   1151 	case 0:
   1152 		strcpy(fp->name, AudioEulinear);
   1153 		fp->encoding = AUDIO_ENCODING_ULINEAR;
   1154 		fp->precision = 8;
   1155 		fp->flags = 0;
   1156 		return (0);
   1157 	case 1:
   1158 		strcpy(fp->name, AudioEmulaw);
   1159 		fp->encoding = AUDIO_ENCODING_ULAW;
   1160 		fp->precision = 8;
   1161 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1162 		return (0);
   1163 	case 2:
   1164 		strcpy(fp->name, AudioEalaw);
   1165 		fp->encoding = AUDIO_ENCODING_ALAW;
   1166 		fp->precision = 8;
   1167 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1168 		return (0);
   1169 	case 3:
   1170 		strcpy(fp->name, AudioEslinear);
   1171 		fp->encoding = AUDIO_ENCODING_SLINEAR;
   1172 		fp->precision = 8;
   1173 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1174 		return (0);
   1175 	case 4:
   1176 		strcpy(fp->name, AudioEslinear_le);
   1177 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
   1178 		fp->precision = 16;
   1179 		fp->flags = 0;
   1180 		return (0);
   1181 	case 5:
   1182 		strcpy(fp->name, AudioEulinear_le);
   1183 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
   1184 		fp->precision = 16;
   1185 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1186 		return (0);
   1187 	case 6:
   1188 		strcpy(fp->name, AudioEslinear_be);
   1189 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
   1190 		fp->precision = 16;
   1191 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1192 		return (0);
   1193 	case 7:
   1194 		strcpy(fp->name, AudioEulinear_be);
   1195 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
   1196 		fp->precision = 16;
   1197 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1198 		return (0);
   1199 	default:
   1200 		return (EINVAL);
   1201 	}
   1202 }
   1203 
   1204 int
   1205 yds_set_params(void *addr, int setmode, int usemode,
   1206 	       struct audio_params *play, struct audio_params* rec)
   1207 {
   1208 	struct audio_params *p;
   1209 	int mode;
   1210 
   1211 	for (mode = AUMODE_RECORD; mode != -1;
   1212 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1213 		if ((setmode & mode) == 0)
   1214 			continue;
   1215 
   1216 		p = mode == AUMODE_PLAY ? play : rec;
   1217 
   1218 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
   1219 		    (p->precision != 8 && p->precision != 16) ||
   1220 		    (p->channels != 1 && p->channels != 2))
   1221 			return (EINVAL);
   1222 
   1223 		p->factor = 1;
   1224 		p->sw_code = 0;
   1225 		switch (p->encoding) {
   1226 		case AUDIO_ENCODING_SLINEAR_BE:
   1227 			if (p->precision == 16)
   1228 				p->sw_code = swap_bytes;
   1229 			else
   1230 				p->sw_code = change_sign8;
   1231 			break;
   1232 		case AUDIO_ENCODING_SLINEAR_LE:
   1233 			if (p->precision != 16)
   1234 				p->sw_code = change_sign8;
   1235 			break;
   1236 		case AUDIO_ENCODING_ULINEAR_BE:
   1237 			if (p->precision == 16) {
   1238 				if (mode == AUMODE_PLAY)
   1239 					p->sw_code = swap_bytes_change_sign16_le;
   1240 				else
   1241 					p->sw_code = change_sign16_swap_bytes_le;
   1242 			}
   1243 			break;
   1244 		case AUDIO_ENCODING_ULINEAR_LE:
   1245 			if (p->precision == 16)
   1246 				p->sw_code = change_sign16_le;
   1247 			break;
   1248 		case AUDIO_ENCODING_ULAW:
   1249 			if (mode == AUMODE_PLAY) {
   1250 				p->factor = 2;
   1251 				p->precision = 16;
   1252 				p->sw_code = mulaw_to_slinear16_le;
   1253 			} else
   1254 				p->sw_code = ulinear8_to_mulaw;
   1255 			break;
   1256 		case AUDIO_ENCODING_ALAW:
   1257 			if (mode == AUMODE_PLAY) {
   1258 				p->factor = 2;
   1259 				p->precision = 16;
   1260 				p->sw_code = alaw_to_slinear16_le;
   1261 			} else
   1262 				p->sw_code = ulinear8_to_alaw;
   1263 			break;
   1264 		default:
   1265 			return (EINVAL);
   1266 		}
   1267 	}
   1268 
   1269 	return 0;
   1270 }
   1271 
   1272 int
   1273 yds_round_blocksize(void *addr, int blk)
   1274 {
   1275 	/*
   1276 	 * Block size must be bigger than a frame.
   1277 	 * That is 1024bytes at most, i.e. for 48000Hz, 16bit, 2ch.
   1278 	 */
   1279 	if (blk < 1024)
   1280 		blk = 1024;
   1281 
   1282 	return blk & ~4;
   1283 }
   1284 
   1285 static u_int32_t
   1286 yds_get_lpfq(u_int sample_rate)
   1287 {
   1288 	int i;
   1289 	static struct lpfqt {
   1290 		u_int rate;
   1291 		u_int32_t lpfq;
   1292 	} lpfqt[] = {
   1293 		{8000,  0x32020000},
   1294 		{11025, 0x31770000},
   1295 		{16000, 0x31390000},
   1296 		{22050, 0x31c90000},
   1297 		{32000, 0x33d00000},
   1298 		{48000, 0x40000000},
   1299 		{0, 0}
   1300 	};
   1301 
   1302 	if (sample_rate == 44100)		/* for P44 slot? */
   1303 		return 0x370A0000;
   1304 
   1305 	for (i = 0; lpfqt[i].rate != 0; i++)
   1306 		if (sample_rate <= lpfqt[i].rate)
   1307 			break;
   1308 
   1309 	return lpfqt[i].lpfq;
   1310 }
   1311 
   1312 static u_int32_t
   1313 yds_get_lpfk(u_int sample_rate)
   1314 {
   1315 	int i;
   1316 	static struct lpfkt {
   1317 		u_int rate;
   1318 		u_int32_t lpfk;
   1319 	} lpfkt[] = {
   1320 		{8000,  0x18b20000},
   1321 		{11025, 0x20930000},
   1322 		{16000, 0x2b9a0000},
   1323 		{22050, 0x35a10000},
   1324 		{32000, 0x3eaa0000},
   1325 		{48000, 0x40000000},
   1326 		{0, 0}
   1327 	};
   1328 
   1329 	if (sample_rate == 44100)		/* for P44 slot? */
   1330 		return 0x46460000;
   1331 
   1332 	for (i = 0; lpfkt[i].rate != 0; i++)
   1333 		if (sample_rate <= lpfkt[i].rate)
   1334 			break;
   1335 
   1336 	return lpfkt[i].lpfk;
   1337 }
   1338 
   1339 int
   1340 yds_trigger_output(void *addr, void *start, void *end, int blksize,
   1341 		   void (*intr)(void *), void *arg, struct audio_params *param)
   1342 #define P44		(sc->sc_flags & YDS_CAP_HAS_P44)
   1343 {
   1344 	struct yds_softc *sc = addr;
   1345 	struct yds_dma *p;
   1346 	struct play_slot_ctrl_bank *psb;
   1347 	const u_int gain = 0x40000000;
   1348 	bus_addr_t s;
   1349 	size_t l;
   1350 	int i;
   1351 	int p44, channels;
   1352 	u_int32_t format;
   1353 
   1354 #ifdef DIAGNOSTIC
   1355 	if (sc->sc_play.intr)
   1356 		panic("yds_trigger_output: already running");
   1357 #endif
   1358 
   1359 	sc->sc_play.intr = intr;
   1360 	sc->sc_play.intr_arg = arg;
   1361 	sc->sc_play.offset = 0;
   1362 	sc->sc_play.blksize = blksize;
   1363 
   1364 	DPRINTFN(1, ("yds_trigger_output: sc=%p start=%p end=%p "
   1365 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1366 
   1367 	p = yds_find_dma(sc, start);
   1368 	if (!p) {
   1369 		printf("yds_trigger_output: bad addr %p\n", start);
   1370 		return (EINVAL);
   1371 	}
   1372 	sc->sc_play.dma = p;
   1373 
   1374 #ifdef YDS_USE_P44
   1375 	/* The document says the P44 SRC supports only stereo, 16bit PCM. */
   1376 	if (P44)
   1377 		p44 = ((param->sample_rate == 44100) &&
   1378 		       (param->channels == 2) &&
   1379 		       (param->precision == 16));
   1380 	else
   1381 #endif
   1382 		p44 = 0;
   1383 	channels = p44 ? 1 : param->channels;
   1384 
   1385 	s = DMAADDR(p);
   1386 	l = ((char *)end - (char *)start);
   1387 	sc->sc_play.length = l;
   1388 
   1389 	*sc->ptbl = htole32(channels);	/* Num of play */
   1390 
   1391 	sc->sc_play.factor = 1;
   1392 	if (param->channels == 2)
   1393 		sc->sc_play.factor *= 2;
   1394 	if (param->precision != 8)
   1395 		sc->sc_play.factor *= 2;
   1396 	l /= sc->sc_play.factor;
   1397 
   1398 	format = ((channels == 2 ? PSLT_FORMAT_STEREO : 0) |
   1399 		  (param->precision == 8 ? PSLT_FORMAT_8BIT : 0) |
   1400 		  (p44 ? PSLT_FORMAT_SRC441 : 0));
   1401 
   1402 	psb = sc->pbankp[0];
   1403 	memset(psb, 0, sizeof(*psb));
   1404 	psb->format = htole32(format);
   1405 	psb->pgbase = htole32(s);
   1406 	psb->pgloopend = htole32(l);
   1407 	if (!p44) {
   1408 		psb->pgdeltaend = htole32((param->sample_rate * 65536 / 48000) << 12);
   1409 		psb->lpfkend = htole32(yds_get_lpfk(param->sample_rate));
   1410 		psb->eggainend = htole32(gain);
   1411 		psb->lpfq = htole32(yds_get_lpfq(param->sample_rate));
   1412 		psb->pgdelta = htole32(psb->pgdeltaend);
   1413 		psb->lpfk = htole32(yds_get_lpfk(param->sample_rate));
   1414 		psb->eggain = htole32(gain);
   1415 	}
   1416 
   1417 	for (i = 0; i < channels; i++) {
   1418 		/* i == 0: left or mono, i == 1: right */
   1419 		psb = sc->pbankp[i*2];
   1420 		if (i)
   1421 			/* copy from left */
   1422 			*psb = *(sc->pbankp[0]);
   1423 		if (channels == 2) {
   1424 			/* stereo */
   1425 			if (i == 0) {
   1426 				psb->lchgain = psb->lchgainend = htole32(gain);
   1427 			} else {
   1428 				psb->lchgain = psb->lchgainend = 0;
   1429 				psb->rchgain = psb->rchgainend = htole32(gain);
   1430 				psb->format |= htole32(PSLT_FORMAT_RCH);
   1431 			}
   1432 		} else if (!p44) {
   1433 			/* mono */
   1434 			psb->lchgain = psb->rchgain = htole32(gain);
   1435 			psb->lchgainend = psb->rchgainend = htole32(gain);
   1436 		}
   1437 		/* copy to the other bank */
   1438 		*(sc->pbankp[i*2+1]) = *psb;
   1439 	}
   1440 
   1441 	YDS_DUMP_PLAY_SLOT(5, sc, 0);
   1442 	YDS_DUMP_PLAY_SLOT(5, sc, 1);
   1443 
   1444 	if (p44)
   1445 		YWRITE4(sc, YDS_P44_OUT_VOLUME, 0x3fff3fff);
   1446 	else
   1447 		YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0x3fff3fff);
   1448 
   1449 	/* Now the play slot for the next frame is set up!! */
   1450 	/* Sync play slot control data for both directions */
   1451 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1452 			sc->ptbloff,
   1453 			sizeof(struct play_slot_ctrl_bank) *
   1454 			    channels * N_PLAY_SLOT_CTRL_BANK,
   1455 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   1456 	/* Sync ring buffer */
   1457 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
   1458 			BUS_DMASYNC_PREWRITE);
   1459 	/* HERE WE GO!! */
   1460 	YWRITE4(sc, YDS_MODE,
   1461 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
   1462 
   1463 	return 0;
   1464 }
   1465 #undef P44
   1466 
   1467 int
   1468 yds_trigger_input(void *addr, void *start, void *end, int blksize,
   1469 		  void (*intr)(void *), void *arg, struct audio_params *param)
   1470 {
   1471 	struct yds_softc *sc = addr;
   1472 	struct yds_dma *p;
   1473 	u_int srate, format;
   1474 	struct rec_slot_ctrl_bank *rsb;
   1475 	bus_addr_t s;
   1476 	size_t l;
   1477 
   1478 #ifdef DIAGNOSTIC
   1479 	if (sc->sc_rec.intr)
   1480 		panic("yds_trigger_input: already running");
   1481 #endif
   1482 	sc->sc_rec.intr = intr;
   1483 	sc->sc_rec.intr_arg = arg;
   1484 	sc->sc_rec.offset = 0;
   1485 	sc->sc_rec.blksize = blksize;
   1486 
   1487 	DPRINTFN(1, ("yds_trigger_input: "
   1488 	    "sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1489 	    addr, start, end, blksize, intr, arg));
   1490 	DPRINTFN(1, (" parameters: rate=%lu, precision=%u, channels=%u\n",
   1491 	    param->sample_rate, param->precision, param->channels));
   1492 
   1493 	p = yds_find_dma(sc, start);
   1494 	if (!p) {
   1495 		printf("yds_trigger_input: bad addr %p\n", start);
   1496 		return (EINVAL);
   1497 	}
   1498 	sc->sc_rec.dma = p;
   1499 
   1500 	s = DMAADDR(p);
   1501 	l = ((char *)end - (char *)start);
   1502 	sc->sc_rec.length = l;
   1503 
   1504 	sc->sc_rec.factor = 1;
   1505 	if (param->channels == 2)
   1506 		sc->sc_rec.factor *= 2;
   1507 	if (param->precision != 8)
   1508 		sc->sc_rec.factor *= 2;
   1509 
   1510 	rsb = &sc->rbank[0];
   1511 	memset(rsb, 0, sizeof(*rsb));
   1512 	rsb->pgbase = htole32(s);
   1513 	rsb->pgloopendadr = htole32(l);
   1514 	/* Seems all 4 banks must be set up... */
   1515 	sc->rbank[1] = *rsb;
   1516 	sc->rbank[2] = *rsb;
   1517 	sc->rbank[3] = *rsb;
   1518 
   1519 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0x3fff3fff);
   1520 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0x3fff3fff);
   1521 	srate = 48000 * 4096 / param->sample_rate - 1;
   1522 	format = ((param->precision == 8 ? YDS_FORMAT_8BIT : 0) |
   1523 		  (param->channels == 2 ? YDS_FORMAT_STEREO : 0));
   1524 	DPRINTF(("srate=%d, format=%08x\n", srate, format));
   1525 #ifdef YDS_USE_REC_SLOT
   1526 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0x3fff3fff);
   1527 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0x3fff3fff);
   1528 	YWRITE4(sc, YDS_MAPOF_REC, YDS_RECSLOT_VALID);
   1529 	YWRITE4(sc, YDS_REC_SAMPLE_RATE, srate);
   1530 	YWRITE4(sc, YDS_REC_FORMAT, format);
   1531 #else
   1532 	YWRITE4(sc, YDS_MAPOF_REC, YDS_ADCSLOT_VALID);
   1533 	YWRITE4(sc, YDS_ADC_SAMPLE_RATE, srate);
   1534 	YWRITE4(sc, YDS_ADC_FORMAT, format);
   1535 #endif
   1536 	/* Now the rec slot for the next frame is set up!! */
   1537 	/* Sync record slot control data */
   1538 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1539 			sc->rbankoff,
   1540 			sizeof(struct rec_slot_ctrl_bank)*
   1541 			    N_REC_SLOT_CTRL*
   1542 			    N_REC_SLOT_CTRL_BANK,
   1543 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   1544 	/* Sync ring buffer */
   1545 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
   1546 			BUS_DMASYNC_PREREAD);
   1547 	/* HERE WE GO!! */
   1548 	YWRITE4(sc, YDS_MODE,
   1549 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
   1550 
   1551 	return 0;
   1552 }
   1553 
   1554 static int
   1555 yds_halt(struct yds_softc *sc)
   1556 {
   1557 	u_int32_t mode;
   1558 
   1559 	/* Stop the DSP operation. */
   1560 	mode = YREAD4(sc, YDS_MODE);
   1561 	YWRITE4(sc, YDS_MODE, mode & ~(YDS_MODE_ACTV|YDS_MODE_ACTV2));
   1562 
   1563 	/* Paranoia...  mute all */
   1564 	YWRITE4(sc, YDS_P44_OUT_VOLUME, 0);
   1565 	YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0);
   1566 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0);
   1567 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0);
   1568 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0);
   1569 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0);
   1570 
   1571 	return 0;
   1572 }
   1573 
   1574 int
   1575 yds_halt_output(void *addr)
   1576 {
   1577 	struct yds_softc *sc = addr;
   1578 
   1579 	DPRINTF(("yds: yds_halt_output\n"));
   1580 	if (sc->sc_play.intr) {
   1581 		sc->sc_play.intr = 0;
   1582 		/* Sync play slot control data */
   1583 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1584 				sc->pbankoff,
   1585 				sizeof(struct play_slot_ctrl_bank)*
   1586 				    (*sc->ptbl)*N_PLAY_SLOT_CTRL_BANK,
   1587 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
   1588 		/* Stop the play slot operation */
   1589 		sc->pbankp[0]->status =
   1590 		sc->pbankp[1]->status =
   1591 		sc->pbankp[2]->status =
   1592 		sc->pbankp[3]->status = 1;
   1593 		/* Sync ring buffer */
   1594 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_play.dma->map,
   1595 				0, sc->sc_play.length, BUS_DMASYNC_POSTWRITE);
   1596 	}
   1597 
   1598 	return 0;
   1599 }
   1600 
   1601 int
   1602 yds_halt_input(void *addr)
   1603 {
   1604 	struct yds_softc *sc = addr;
   1605 
   1606 	DPRINTF(("yds: yds_halt_input\n"));
   1607 	sc->sc_rec.intr = NULL;
   1608 	if (sc->sc_rec.intr) {
   1609 		/* Stop the rec slot operation */
   1610 		YWRITE4(sc, YDS_MAPOF_REC, 0);
   1611 		sc->sc_rec.intr = 0;
   1612 		/* Sync rec slot control data */
   1613 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1614 				sc->rbankoff,
   1615 				sizeof(struct rec_slot_ctrl_bank)*
   1616 				    N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK,
   1617 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
   1618 		/* Sync ring buffer */
   1619 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_rec.dma->map,
   1620 				0, sc->sc_rec.length, BUS_DMASYNC_POSTREAD);
   1621 	}
   1622 
   1623 	return 0;
   1624 }
   1625 
   1626 int
   1627 yds_getdev(void *addr, struct audio_device *retp)
   1628 {
   1629 	*retp = yds_device;
   1630 
   1631 	return 0;
   1632 }
   1633 
   1634 int
   1635 yds_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1636 {
   1637 	struct yds_softc *sc = addr;
   1638 
   1639 	return (sc->sc_codec[0].codec_if->vtbl->mixer_set_port(
   1640 	    sc->sc_codec[0].codec_if, cp));
   1641 }
   1642 
   1643 int
   1644 yds_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1645 {
   1646 	struct yds_softc *sc = addr;
   1647 
   1648 	return (sc->sc_codec[0].codec_if->vtbl->mixer_get_port(
   1649 	    sc->sc_codec[0].codec_if, cp));
   1650 }
   1651 
   1652 int
   1653 yds_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1654 {
   1655 	struct yds_softc *sc = addr;
   1656 
   1657 	return (sc->sc_codec[0].codec_if->vtbl->query_devinfo(
   1658 	    sc->sc_codec[0].codec_if, dip));
   1659 }
   1660 
   1661 int
   1662 yds_get_portnum_by_name(struct yds_softc *sc, char *class, char *device, char *qualifier)
   1663 {
   1664 	return (sc->sc_codec[0].codec_if->vtbl->get_portnum_by_name(
   1665 	    sc->sc_codec[0].codec_if, class, device, qualifier));
   1666 }
   1667 
   1668 void *
   1669 yds_malloc(void *addr, int direction, size_t size,
   1670 	   struct malloc_type *pool, int flags)
   1671 {
   1672 	struct yds_softc *sc = addr;
   1673 	struct yds_dma *p;
   1674 	int error;
   1675 
   1676 	p = malloc(sizeof(*p), pool, flags);
   1677 	if (!p)
   1678 		return (0);
   1679 	error = yds_allocmem(sc, size, 16, p);
   1680 	if (error) {
   1681 		free(p, pool);
   1682 		return (0);
   1683 	}
   1684 	p->next = sc->sc_dmas;
   1685 	sc->sc_dmas = p;
   1686 	return (KERNADDR(p));
   1687 }
   1688 
   1689 void
   1690 yds_free(void *addr, void *ptr, struct malloc_type *pool)
   1691 {
   1692 	struct yds_softc *sc = addr;
   1693 	struct yds_dma **pp, *p;
   1694 
   1695 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1696 		if (KERNADDR(p) == ptr) {
   1697 			yds_freemem(sc, p);
   1698 			*pp = p->next;
   1699 			free(p, pool);
   1700 			return;
   1701 		}
   1702 	}
   1703 }
   1704 
   1705 static struct yds_dma *
   1706 yds_find_dma(struct yds_softc *sc, void *addr)
   1707 {
   1708 	struct yds_dma *p;
   1709 
   1710 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
   1711 		;
   1712 
   1713 	return p;
   1714 }
   1715 
   1716 size_t
   1717 yds_round_buffersize(void *addr, int direction, size_t size)
   1718 {
   1719 	/*
   1720 	 * Buffer size should be at least twice as bigger as a frame.
   1721 	 */
   1722 	if (size < 1024 * 3)
   1723 		size = 1024 * 3;
   1724 	return (size);
   1725 }
   1726 
   1727 paddr_t
   1728 yds_mappage(void *addr, void *mem, off_t off, int prot)
   1729 {
   1730 	struct yds_softc *sc = addr;
   1731 	struct yds_dma *p;
   1732 
   1733 	if (off < 0)
   1734 		return (-1);
   1735 	p = yds_find_dma(sc, mem);
   1736 	if (!p)
   1737 		return (-1);
   1738 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1739 				off, prot, BUS_DMA_WAITOK));
   1740 }
   1741 
   1742 int
   1743 yds_get_props(void *addr)
   1744 {
   1745 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1746 		AUDIO_PROP_FULLDUPLEX);
   1747 }
   1748