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