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