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