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