xref: /netbsd-src/sys/dev/pci/emuxki.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: emuxki.c,v 1.76 2022/09/07 03:34:43 khorben Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Yannick Montulet, and by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * EMU10K1 single voice driver
34  * o. only 1 voice playback, 1 recording
35  * o. only s16le 2ch 48k
36  * This makes it simple to control buffers and interrupts
37  * while satisfying playback and recording quality.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: emuxki.c,v 1.76 2022/09/07 03:34:43 khorben Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/device.h>
45 #include <sys/module.h>
46 #include <sys/errno.h>
47 #include <sys/systm.h>
48 #include <sys/audioio.h>
49 #include <sys/mutex.h>
50 #include <sys/kmem.h>
51 #include <sys/malloc.h>
52 #include <sys/fcntl.h>
53 
54 #include <sys/bus.h>
55 #include <sys/intr.h>
56 
57 #include <dev/pci/emuxkireg.h>
58 #include <dev/pci/emuxkivar.h>
59 #include <dev/pci/emuxki_boards.h>
60 
61 /* #define EMUXKI_DEBUG 1 */
62 #ifdef EMUXKI_DEBUG
63 #define emudebug EMUXKI_DEBUG
64 # define DPRINTF(fmt...)	do { if (emudebug) printf(fmt); } while (0)
65 # define DPRINTFN(n,fmt...)	do { if (emudebug>=(n)) printf(fmt); } while (0)
66 #else
67 # define DPRINTF(fmt...)	__nothing
68 # define DPRINTFN(n,fmt...)	__nothing
69 #endif
70 
71 /*
72  * PCI
73  * Note: emuxki's page table entry uses only 31bit addressing.
74  *       (Maybe, later chip has 32bit mode, but it isn't used now.)
75  */
76 
77 #define EMU_PCI_CBIO		(0x10)
78 
79 /* blackmagic */
80 #define X1(x)		((sc->sc_type & EMUXKI_AUDIGY) ? EMU_A_##x : EMU_##x)
81 #define X2(x, y)	((sc->sc_type & EMUXKI_AUDIGY) \
82     ? EMU_A_##x(EMU_A_##y) : EMU_##x(EMU_##y))
83 #define EMU_A_DSP_FX		EMU_DSP_FX
84 #define EMU_A_DSP_IN_AC97	EMU_DSP_IN_AC97
85 
86 /* prototypes */
87 static struct dmamem *dmamem_alloc(struct emuxki_softc *, size_t);
88 static void	dmamem_free(struct dmamem *);
89 static void	dmamem_sync(struct dmamem *, int);
90 static uint8_t	emuxki_readio_1(struct emuxki_softc *, int) __unused;
91 static uint16_t	emuxki_readio_2(struct emuxki_softc *, int);
92 static uint32_t	emuxki_readio_4(struct emuxki_softc *, int);
93 static void	emuxki_writeio_1(struct emuxki_softc *, int, uint8_t);
94 static void	emuxki_writeio_2(struct emuxki_softc *, int, uint16_t);
95 static void	emuxki_writeio_4(struct emuxki_softc *, int, uint32_t);
96 static uint32_t	emuxki_readptr(struct emuxki_softc *, int, int, int);
97 static void	emuxki_writeptr(struct emuxki_softc *, int, int, int, uint32_t);
98 static uint32_t	emuxki_read(struct emuxki_softc *, int, int);
99 static void	emuxki_write(struct emuxki_softc *, int, int, uint32_t);
100 static int	emuxki_match(device_t, cfdata_t, void *);
101 static void	emuxki_attach(device_t, device_t, void *);
102 static int	emuxki_detach(device_t, int);
103 static int	emuxki_init(struct emuxki_softc *);
104 static void	emuxki_dsp_addop(struct emuxki_softc *, uint16_t *, uint8_t,
105 		    uint16_t, uint16_t, uint16_t, uint16_t);
106 static void	emuxki_initfx(struct emuxki_softc *);
107 static void	emuxki_play_start(struct emuxki_softc *, int, uint32_t,
108 		    uint32_t);
109 static void	emuxki_play_stop(struct emuxki_softc *, int);
110 
111 static int	emuxki_query_format(void *, audio_format_query_t *);
112 static int	emuxki_set_format(void *, int,
113 		    const audio_params_t *, const audio_params_t *,
114 		    audio_filter_reg_t *, audio_filter_reg_t *);
115 static int	emuxki_halt_output(void *);
116 static int	emuxki_halt_input(void *);
117 static int	emuxki_intr(void *);
118 static int	emuxki_getdev(void *, struct audio_device *);
119 static int	emuxki_set_port(void *, mixer_ctrl_t *);
120 static int	emuxki_get_port(void *, mixer_ctrl_t *);
121 static int	emuxki_query_devinfo(void *, mixer_devinfo_t *);
122 static void	*emuxki_allocm(void *, int, size_t);
123 static void	emuxki_freem(void *, void *, size_t);
124 static int	emuxki_round_blocksize(void *, int, int,
125 		    const audio_params_t *);
126 static size_t	emuxki_round_buffersize(void *, int, size_t);
127 static int	emuxki_get_props(void *);
128 static int	emuxki_trigger_output(void *, void *, void *, int,
129 		    void (*)(void *), void *, const audio_params_t *);
130 static int	emuxki_trigger_input(void *, void *, void *, int,
131 		    void (*)(void *), void *, const audio_params_t *);
132 static void	emuxki_get_locks(void *, kmutex_t **, kmutex_t **);
133 
134 static int	emuxki_ac97_init(struct emuxki_softc *);
135 static int	emuxki_ac97_attach(void *, struct ac97_codec_if *);
136 static int	emuxki_ac97_read(void *, uint8_t, uint16_t *);
137 static int	emuxki_ac97_write(void *, uint8_t, uint16_t);
138 static int	emuxki_ac97_reset(void *);
139 static enum ac97_host_flags	emuxki_ac97_flags(void *);
140 
141 
142 CFATTACH_DECL_NEW(emuxki, sizeof(struct emuxki_softc),
143     emuxki_match, emuxki_attach, emuxki_detach, NULL);
144 
145 static const struct audio_hw_if emuxki_hw_if = {
146 	.query_format		= emuxki_query_format,
147 	.set_format		= emuxki_set_format,
148 	.round_blocksize	= emuxki_round_blocksize,
149 	.halt_output		= emuxki_halt_output,
150 	.halt_input		= emuxki_halt_input,
151 	.getdev			= emuxki_getdev,
152 	.set_port		= emuxki_set_port,
153 	.get_port		= emuxki_get_port,
154 	.query_devinfo		= emuxki_query_devinfo,
155 	.allocm			= emuxki_allocm,
156 	.freem			= emuxki_freem,
157 	.round_buffersize	= emuxki_round_buffersize,
158 	.get_props		= emuxki_get_props,
159 	.trigger_output		= emuxki_trigger_output,
160 	.trigger_input		= emuxki_trigger_input,
161 	.get_locks		= emuxki_get_locks,
162 };
163 
164 static const struct audio_format emuxki_formats[] = {
165 	{
166 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
167 		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
168 		.validbits	= 16,
169 		.precision	= 16,
170 		.channels	= 2,
171 		.channel_mask	= AUFMT_STEREO,
172 		.frequency_type	= 1,
173 		.frequency	= { 48000 },
174 	}
175 };
176 #define EMUXKI_NFORMATS	__arraycount(emuxki_formats)
177 
178 /*
179  * dma memory
180  */
181 
182 static struct dmamem *
183 dmamem_alloc(struct emuxki_softc *sc, size_t size)
184 {
185 	struct dmamem *mem;
186 
187 	KASSERT(!mutex_owned(&sc->sc_intr_lock));
188 
189 	/* Allocate memory for structure */
190 	mem = kmem_alloc(sizeof(*mem), KM_SLEEP);
191 	mem->dmat = sc->sc_dmat;
192 	mem->size = size;
193 	mem->align = EMU_DMA_ALIGN;
194 	mem->nsegs = EMU_DMA_NSEGS;
195 	mem->bound = 0;
196 
197 	mem->segs = kmem_alloc(mem->nsegs * sizeof(*(mem->segs)), KM_SLEEP);
198 
199 	if (bus_dmamem_alloc(mem->dmat, mem->size, mem->align, mem->bound,
200 	    mem->segs, mem->nsegs, &mem->rsegs, BUS_DMA_WAITOK)) {
201 		device_printf(sc->sc_dev,
202 		    "%s bus_dmamem_alloc failed\n", __func__);
203 		goto memfree;
204 	}
205 
206 	if (bus_dmamem_map(mem->dmat, mem->segs, mem->nsegs, mem->size,
207 	    &mem->kaddr, BUS_DMA_WAITOK | BUS_DMA_COHERENT)) {
208 		device_printf(sc->sc_dev,
209 		    "%s bus_dmamem_map failed\n", __func__);
210 		goto free;
211 	}
212 
213 	if (bus_dmamap_create(mem->dmat, mem->size, mem->nsegs, mem->size,
214 	    mem->bound, BUS_DMA_WAITOK, &mem->map)) {
215 		device_printf(sc->sc_dev,
216 		    "%s bus_dmamap_create failed\n", __func__);
217 		goto unmap;
218 	}
219 
220 	if (bus_dmamap_load(mem->dmat, mem->map, mem->kaddr,
221 	    mem->size, NULL, BUS_DMA_WAITOK)) {
222 		device_printf(sc->sc_dev,
223 		    "%s bus_dmamap_load failed\n", __func__);
224 		goto destroy;
225 	}
226 
227 	return mem;
228 
229 destroy:
230 	bus_dmamap_destroy(mem->dmat, mem->map);
231 unmap:
232 	bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
233 free:
234 	bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
235 memfree:
236 	kmem_free(mem->segs, mem->nsegs * sizeof(*(mem->segs)));
237 	kmem_free(mem, sizeof(*mem));
238 
239 	return NULL;
240 }
241 
242 static void
243 dmamem_free(struct dmamem *mem)
244 {
245 
246 	bus_dmamap_unload(mem->dmat, mem->map);
247 	bus_dmamap_destroy(mem->dmat, mem->map);
248 	bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
249 	bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
250 
251 	kmem_free(mem->segs, mem->nsegs * sizeof(*(mem->segs)));
252 	kmem_free(mem, sizeof(*mem));
253 }
254 
255 static void
256 dmamem_sync(struct dmamem *mem, int ops)
257 {
258 
259 	bus_dmamap_sync(mem->dmat, mem->map, 0, mem->size, ops);
260 }
261 
262 
263 /*
264  * I/O register access
265  */
266 
267 static uint8_t
268 emuxki_readio_1(struct emuxki_softc *sc, int addr)
269 {
270 
271 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, addr);
272 }
273 
274 static void
275 emuxki_writeio_1(struct emuxki_softc *sc, int addr, uint8_t data)
276 {
277 
278 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, addr, data);
279 }
280 
281 static uint16_t
282 emuxki_readio_2(struct emuxki_softc *sc, int addr)
283 {
284 
285 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, addr);
286 }
287 
288 static void
289 emuxki_writeio_2(struct emuxki_softc *sc, int addr, uint16_t data)
290 {
291 
292 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, addr, data);
293 }
294 
295 static uint32_t
296 emuxki_readio_4(struct emuxki_softc *sc, int addr)
297 {
298 
299 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, addr);
300 }
301 
302 static void
303 emuxki_writeio_4(struct emuxki_softc *sc, int addr, uint32_t data)
304 {
305 
306 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, addr, data);
307 }
308 
309 static uint32_t
310 emuxki_readptr(struct emuxki_softc *sc, int aptr, int dptr, int addr)
311 {
312 	uint32_t data;
313 
314 	mutex_spin_enter(&sc->sc_index_lock);
315 	emuxki_writeio_4(sc, aptr, addr);
316 	data = emuxki_readio_4(sc, dptr);
317 	mutex_spin_exit(&sc->sc_index_lock);
318 	return data;
319 }
320 
321 static void
322 emuxki_writeptr(struct emuxki_softc *sc, int aptr, int dptr, int addr,
323     uint32_t data)
324 {
325 
326 	mutex_spin_enter(&sc->sc_index_lock);
327 	emuxki_writeio_4(sc, aptr, addr);
328 	emuxki_writeio_4(sc, dptr, data);
329 	mutex_spin_exit(&sc->sc_index_lock);
330 }
331 
332 static uint32_t
333 emuxki_read(struct emuxki_softc *sc, int ch, int addr)
334 {
335 
336 	/* Original HENTAI addressing is never supported. */
337 	KASSERT((addr & 0xff000000) == 0);
338 
339 	return emuxki_readptr(sc, EMU_PTR, EMU_DATA, (addr << 16) + ch);
340 }
341 
342 static void
343 emuxki_write(struct emuxki_softc *sc, int ch, int addr, uint32_t data)
344 {
345 
346 	/* Original HENTAI addressing is never supported. */
347 	KASSERT((addr & 0xff000000) == 0);
348 
349 	emuxki_writeptr(sc, EMU_PTR, EMU_DATA, (addr << 16) + ch, data);
350 }
351 
352 /*
353  * MD driver
354  */
355 
356 static int
357 emuxki_match(device_t parent, cfdata_t match, void *aux)
358 {
359 	struct pci_attach_args *pa;
360 	pcireg_t reg;
361 
362 	pa = aux;
363 
364 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
365 	if (emuxki_board_lookup(PCI_VENDOR(pa->pa_id),
366 				PCI_PRODUCT(pa->pa_id), reg,
367 				PCI_REVISION(pa->pa_class)) != NULL)
368 		return 1;
369 
370 	return 0;
371 }
372 
373 static void
374 emuxki_attach(device_t parent, device_t self, void *aux)
375 {
376 	struct emuxki_softc *sc;
377 	struct pci_attach_args *pa;
378 	const struct emuxki_board *sb;
379 	pci_intr_handle_t ih;
380 	const char *intrstr;
381 	char intrbuf[PCI_INTRSTR_LEN];
382 	pcireg_t reg;
383 
384 	sc = device_private(self);
385 	sc->sc_dev = self;
386 	pa = aux;
387 
388 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
389 	sb = emuxki_board_lookup(PCI_VENDOR(pa->pa_id),
390 				      PCI_PRODUCT(pa->pa_id), reg,
391 				      PCI_REVISION(pa->pa_class));
392 	KASSERT(sb != NULL);
393 
394 	pci_aprint_devinfo(pa, "Audio controller");
395 	aprint_normal_dev(self, "%s [%s]\n", sb->sb_name, sb->sb_board);
396 	DPRINTF("dmat=%p\n", (char *)pa->pa_dmat);
397 
398 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
399 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
400 	mutex_init(&sc->sc_index_lock, MUTEX_DEFAULT, IPL_AUDIO);
401 
402 	sc->sc_pc   = pa->pa_pc;
403 
404 	/* EMU10K1 can only address 31 bits (2GB) */
405 	if (bus_dmatag_subregion(pa->pa_dmat, 0, ((uint32_t)1 << 31) - 1,
406 	    &(sc->sc_dmat), BUS_DMA_NOWAIT) != 0) {
407 		aprint_error_dev(self,
408 		    "WARNING: failed to restrict dma range,"
409 		    " falling back to parent bus dma range\n");
410 		sc->sc_dmat = pa->pa_dmat;
411 	}
412 
413 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
414 	reg |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE |
415 	    PCI_COMMAND_MEM_ENABLE;
416 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
417 
418 	if (pci_mapreg_map(pa, EMU_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
419 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
420 		aprint_error(": can't map iospace\n");
421 		return;
422 	}
423 
424 	if (pci_intr_map(pa, &ih)) {
425 		aprint_error_dev(self, "couldn't map interrupt\n");
426 		goto unmap;
427 	}
428 
429 	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
430 	sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_AUDIO,
431 	    emuxki_intr, sc, device_xname(self));
432 	if (sc->sc_ih == NULL) {
433 		aprint_error_dev(self, "couldn't establish interrupt");
434 		if (intrstr != NULL)
435 			aprint_error(" at %s", intrstr);
436 		aprint_error("\n");
437 		goto unmap;
438 	}
439 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
440 
441 	/* XXX it's unknown whether APS is made from Audigy as well */
442 	sc->sc_type = sb->sb_flags;
443 	if (sc->sc_type & EMUXKI_AUDIGY2_CA0108) {
444 		strlcpy(sc->sc_audv.name, "Audigy2+CA0108",
445 		    sizeof(sc->sc_audv.name));
446 	} else if (sc->sc_type & EMUXKI_AUDIGY2) {
447 		strlcpy(sc->sc_audv.name, "Audigy2", sizeof(sc->sc_audv.name));
448 	} else if (sc->sc_type & EMUXKI_AUDIGY) {
449 		strlcpy(sc->sc_audv.name, "Audigy", sizeof(sc->sc_audv.name));
450 	} else if (sc->sc_type & EMUXKI_APS) {
451 		strlcpy(sc->sc_audv.name, "E-mu APS", sizeof(sc->sc_audv.name));
452 	} else {
453 		strlcpy(sc->sc_audv.name, "SB Live!", sizeof(sc->sc_audv.name));
454 	}
455 	snprintf(sc->sc_audv.version, sizeof(sc->sc_audv.version), "0x%02x",
456 	    PCI_REVISION(pa->pa_class));
457 	strlcpy(sc->sc_audv.config, "emuxki", sizeof(sc->sc_audv.config));
458 
459 	if (emuxki_init(sc)) {
460 		aprint_error("emuxki_init error\n");
461 		goto intrdis;
462 	}
463 	if (emuxki_ac97_init(sc)) {
464 		aprint_error("emuxki_ac97_init error\n");
465 		goto intrdis;
466 	}
467 
468 	sc->sc_audev = audio_attach_mi(&emuxki_hw_if, sc, self);
469 	if (sc->sc_audev == NULL) {
470 		aprint_error("audio_attach_mi error\n");
471 		goto intrdis;
472 	}
473 
474 	return;
475 
476 intrdis:
477 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
478 unmap:
479 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
480 	return;
481 }
482 
483 static int
484 emuxki_detach(device_t self, int flags)
485 {
486 	struct emuxki_softc *sc;
487 
488 	sc = device_private(self);
489 	if (sc->sc_audev != NULL) /* Test in case audio didn't attach */
490 		config_detach(sc->sc_audev, 0);
491 
492 	/* All voices should be stopped now but add some code here if not */
493 	emuxki_writeio_4(sc, EMU_HCFG,
494 	    EMU_HCFG_LOCKSOUNDCACHE |
495 	    EMU_HCFG_LOCKTANKCACHE_MASK |
496 	    EMU_HCFG_MUTEBUTTONENABLE);
497 	emuxki_writeio_4(sc, EMU_INTE, 0);
498 
499 	/* Disable any Channels interrupts */
500 	emuxki_write(sc, 0, EMU_CLIEL, 0);
501 	emuxki_write(sc, 0, EMU_CLIEH, 0);
502 	emuxki_write(sc, 0, EMU_SOLEL, 0);
503 	emuxki_write(sc, 0, EMU_SOLEH, 0);
504 
505 	/* stop DSP */
506 	emuxki_write(sc, 0, X1(DBG), X1(DBG_SINGLE_STEP));
507 
508 	dmamem_free(sc->ptb);
509 
510 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
511 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
512 
513 	mutex_destroy(&sc->sc_lock);
514 	mutex_destroy(&sc->sc_intr_lock);
515 	mutex_destroy(&sc->sc_index_lock);
516 
517 	return 0;
518 }
519 
520 static int
521 emuxki_init(struct emuxki_softc *sc)
522 {
523 	int i;
524 	uint32_t spcs;
525 	uint32_t hcfg;
526 
527 	/* clear AUDIO bit */
528 	emuxki_writeio_4(sc, EMU_HCFG,
529 	    EMU_HCFG_LOCKSOUNDCACHE |
530 	    EMU_HCFG_LOCKTANKCACHE_MASK |
531 	    EMU_HCFG_MUTEBUTTONENABLE);
532 
533 	/* mask interrupt without PCIERR */
534 	emuxki_writeio_4(sc, EMU_INTE,
535 	    EMU_INTE_SAMPLERATER | /* always on this bit */
536 	    EMU_INTE_PCIERRENABLE);
537 
538 	/* disable all channel interrupt */
539 	emuxki_write(sc, 0, EMU_CLIEL, 0);
540 	emuxki_write(sc, 0, EMU_CLIEH, 0);
541 	emuxki_write(sc, 0, EMU_SOLEL, 0);
542 	emuxki_write(sc, 0, EMU_SOLEH, 0);
543 
544 	/* Set recording buffers sizes to zero */
545 	emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
546 	emuxki_write(sc, 0, EMU_MICBA, 0);
547 	emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
548 	emuxki_write(sc, 0, EMU_FXBA, 0);
549 	emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
550 	emuxki_write(sc, 0, EMU_ADCBA, 0);
551 
552 	if(sc->sc_type & EMUXKI_AUDIGY) {
553 		emuxki_write(sc, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS);
554 		emuxki_write(sc, 0, EMU_AC97SLOT,
555 		    EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
556 	}
557 
558 	/* Initialize all channels to stopped and no effects */
559 	for (i = 0; i < EMU_NUMCHAN; i++) {
560 		emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0x7f7f);
561 		emuxki_write(sc, i, EMU_CHAN_IP, EMU_CHAN_IP_UNITY);
562 		emuxki_write(sc, i, EMU_CHAN_VTFT, 0xffff);
563 		emuxki_write(sc, i, EMU_CHAN_CVCF, 0xffff);
564 		emuxki_write(sc, i, EMU_CHAN_PTRX, 0);
565 		emuxki_write(sc, i, EMU_CHAN_CPF, 0);
566 		emuxki_write(sc, i, EMU_CHAN_CCR, 0);
567 		emuxki_write(sc, i, EMU_CHAN_PSST, 0);
568 		emuxki_write(sc, i, EMU_CHAN_DSL, 0);
569 		emuxki_write(sc, i, EMU_CHAN_CCCA, EMU_CHAN_CCCA_INTERPROM_1);
570 		emuxki_write(sc, i, EMU_CHAN_Z1, 0);
571 		emuxki_write(sc, i, EMU_CHAN_Z2, 0);
572 		emuxki_write(sc, i, EMU_CHAN_MAPA, 0xffffffff);
573 		emuxki_write(sc, i, EMU_CHAN_MAPB, 0xffffffff);
574 		emuxki_write(sc, i, EMU_CHAN_FXRT, 0x32100000);
575 		emuxki_write(sc, i, EMU_CHAN_ATKHLDM, 0);
576 		emuxki_write(sc, i, EMU_CHAN_DCYSUSM, 0);
577 		emuxki_write(sc, i, EMU_CHAN_IFATN, 0xffff);
578 		emuxki_write(sc, i, EMU_CHAN_PEFE, 0x007f);
579 		emuxki_write(sc, i, EMU_CHAN_FMMOD, 0);
580 		emuxki_write(sc, i, EMU_CHAN_TREMFRQ, 0);
581 		emuxki_write(sc, i, EMU_CHAN_FM2FRQ2, 0);
582 		emuxki_write(sc, i, EMU_CHAN_TEMPENV, 0);
583 
584 		/* these are last so OFF prevents writing */
585 		emuxki_write(sc, i, EMU_CHAN_LFOVAL2, 0x8000);
586 		emuxki_write(sc, i, EMU_CHAN_LFOVAL1, 0x8000);
587 		emuxki_write(sc, i, EMU_CHAN_ATKHLDV, 0x7f7f);
588 		emuxki_write(sc, i, EMU_CHAN_ENVVOL, 0);
589 		emuxki_write(sc, i, EMU_CHAN_ENVVAL, 0x8000);
590 	}
591 
592 	/* set digital outputs format */
593 	spcs = EMU_SPCS_CLKACCY_1000PPM |
594 	       EMU_SPCS_SAMPLERATE_48 |
595 	       EMU_SPCS_CHANNELNUM_LEFT |
596 	       EMU_SPCS_SOURCENUM_UNSPEC |
597 	       EMU_SPCS_GENERATIONSTATUS |
598 	       0x00001200 /* Cat code. */ |
599 	       0x00000000 /* IEC-958 Mode */ |
600 	       EMU_SPCS_EMPHASIS_NONE |
601 	       EMU_SPCS_COPYRIGHT;
602 	emuxki_write(sc, 0, EMU_SPCS0, spcs);
603 	emuxki_write(sc, 0, EMU_SPCS1, spcs);
604 	emuxki_write(sc, 0, EMU_SPCS2, spcs);
605 
606 	if (sc->sc_type & EMUXKI_AUDIGY2_CA0108) {
607 		/* Setup SRCMulti_I2S SamplingRate */
608 		emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE,
609 		    emuxki_read(sc, 0, EMU_A2_SPDIF_SAMPLERATE) & 0xfffff1ff);
610 
611 		/* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */
612 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCSEL,
613 		    EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
614 
615 		/* Setup SRCMulti Input Audio Enable */
616 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA,
617 		    0x7b0000, 0xff000000);
618 
619 		/* Setup SPDIF Out Audio Enable
620 		 * The Audigy 2 Value has a separate SPDIF out,
621 		 * so no need for a mixer switch */
622 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA,
623 		    0x7a0000, 0xff000000);
624 		emuxki_writeio_4(sc, EMU_A_IOCFG,
625 		    emuxki_readio_4(sc, EMU_A_IOCFG) & ~0x8); /* clear bit 3 */
626 	} else if (sc->sc_type & EMUXKI_AUDIGY2) {
627 		emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE,
628 		    EMU_A2_SPDIF_UNKNOWN);
629 
630 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCSEL,
631 		    EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
632 
633 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCMULTI,
634 		    EMU_A2_SRCMULTI_ENABLE_INPUT);
635 	}
636 
637 	/* page table */
638 	sc->ptb = dmamem_alloc(sc, EMU_MAXPTE * sizeof(uint32_t));
639 	if (sc->ptb == NULL) {
640 		device_printf(sc->sc_dev, "ptb allocation error\n");
641 		return ENOMEM;
642 	}
643 	emuxki_write(sc, 0, EMU_PTB, DMAADDR(sc->ptb));
644 
645 	emuxki_write(sc, 0, EMU_TCBS, 0);	/* This means 16K TCB */
646 	emuxki_write(sc, 0, EMU_TCB, 0);	/* No TCB use for now */
647 
648 	/* Let's play with sound processor */
649 	emuxki_initfx(sc);
650 
651 	/* enable interrupt */
652 	emuxki_writeio_4(sc, EMU_INTE,
653 	    emuxki_readio_4(sc, EMU_INTE) |
654 	    EMU_INTE_VOLINCRENABLE |
655 	    EMU_INTE_VOLDECRENABLE |
656 	    EMU_INTE_MUTEENABLE);
657 
658 	if (sc->sc_type & EMUXKI_AUDIGY2_CA0108) {
659 		emuxki_writeio_4(sc, EMU_A_IOCFG,
660 		    0x0060 | emuxki_readio_4(sc, EMU_A_IOCFG));
661 	} else if (sc->sc_type & EMUXKI_AUDIGY2) {
662 		emuxki_writeio_4(sc, EMU_A_IOCFG,
663 		    EMU_A_IOCFG_GPOUT0 | emuxki_readio_4(sc, EMU_A_IOCFG));
664 	}
665 
666 	/* enable AUDIO bit */
667 	hcfg = EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE;
668 
669 	if (sc->sc_type & EMUXKI_AUDIGY2) {
670 		hcfg |= EMU_HCFG_AC3ENABLE_CDSPDIF |
671 		        EMU_HCFG_AC3ENABLE_GPSPDIF;
672 	} else if (sc->sc_type & EMUXKI_AUDIGY) {
673 	} else {
674 		hcfg |= EMU_HCFG_LOCKTANKCACHE_MASK;
675 	}
676 	/* joystick not supported now */
677 	emuxki_writeio_4(sc, EMU_HCFG, hcfg);
678 
679 	return 0;
680 }
681 
682 /*
683  * dsp programming
684  */
685 
686 static void
687 emuxki_dsp_addop(struct emuxki_softc *sc, uint16_t *pc, uint8_t op,
688     uint16_t r, uint16_t a, uint16_t x, uint16_t y)
689 {
690 	uint32_t loword;
691 	uint32_t hiword;
692 	int reg;
693 
694 	if (sc->sc_type & EMUXKI_AUDIGY) {
695 		reg = EMU_A_MICROCODEBASE;
696 		loword = (x << 12) & EMU_A_DSP_LOWORD_OPX_MASK;
697 		loword |= y & EMU_A_DSP_LOWORD_OPY_MASK;
698 		hiword = (op << 24) & EMU_A_DSP_HIWORD_OPCODE_MASK;
699 		hiword |= (r << 12) & EMU_A_DSP_HIWORD_RESULT_MASK;
700 		hiword |= a & EMU_A_DSP_HIWORD_OPA_MASK;
701 	} else {
702 		reg = EMU_MICROCODEBASE;
703 		loword = (x << 10) & EMU_DSP_LOWORD_OPX_MASK;
704 		loword |= y & EMU_DSP_LOWORD_OPY_MASK;
705 		hiword = (op << 20) & EMU_DSP_HIWORD_OPCODE_MASK;
706 		hiword |= (r << 10) & EMU_DSP_HIWORD_RESULT_MASK;
707 		hiword |= a & EMU_DSP_HIWORD_OPA_MASK;
708 	}
709 
710 	reg += (*pc) * 2;
711 	/* must ordering; lo, hi */
712 	emuxki_write(sc, 0, reg, loword);
713 	emuxki_write(sc, 0, reg + 1, hiword);
714 
715 	(*pc)++;
716 }
717 
718 static void
719 emuxki_initfx(struct emuxki_softc *sc)
720 {
721 	uint16_t pc;
722 
723 	/* Set all GPRs to 0 */
724 	for (pc = 0; pc < 256; pc++)
725 		emuxki_write(sc, 0, EMU_DSP_GPR(pc), 0);
726 	for (pc = 0; pc < 160; pc++) {
727 		emuxki_write(sc, 0, EMU_TANKMEMDATAREGBASE + pc, 0);
728 		emuxki_write(sc, 0, EMU_TANKMEMADDRREGBASE + pc, 0);
729 	}
730 
731 	/* stop DSP, single step mode */
732 	emuxki_write(sc, 0, X1(DBG), X1(DBG_SINGLE_STEP));
733 
734 	/* XXX: delay (48kHz equiv. 21us) if needed */
735 
736 	/* start DSP programming */
737 	pc = 0;
738 
739 	/* OUT[L/R] = 0 + FX[L/R] * 1 */
740 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
741 	    X2(DSP_OUTL, DSP_OUT_A_FRONT),
742 	    X1(DSP_CST(0)),
743 	    X1(DSP_FX(0)),
744 	    X1(DSP_CST(1)));
745 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
746 	    X2(DSP_OUTR, DSP_OUT_A_FRONT),
747 	    X1(DSP_CST(0)),
748 	    X1(DSP_FX(1)),
749 	    X1(DSP_CST(1)));
750 #if 0
751 	/* XXX: rear feature??? */
752 	/* Rear OUT[L/R] = 0 + FX[L/R] * 1 */
753 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
754 	    X2(DSP_OUTL, DSP_OUT_A_REAR),
755 	    X1(DSP_CST(0)),
756 	    X1(DSP_FX(0)),
757 	    X1(DSP_CST(1)));
758 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
759 	    X2(DSP_OUTR, DSP_OUT_A_REAR),
760 	    X1(DSP_CST(0)),
761 	    X1(DSP_FX(1)),
762 	    X1(DSP_CST(1)));
763 #endif
764 	/* ADC recording[L/R] = AC97 In[L/R] */
765 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
766 	    X2(DSP_OUTL, DSP_OUT_ADC),
767 	    X2(DSP_INL, DSP_IN_AC97),
768 	    X1(DSP_CST(0)),
769 	    X1(DSP_CST(0)));
770 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
771 	    X2(DSP_OUTR, DSP_OUT_ADC),
772 	    X2(DSP_INR, DSP_IN_AC97),
773 	    X1(DSP_CST(0)),
774 	    X1(DSP_CST(0)));
775 
776 	/* fill NOP the rest of the microcode */
777 	while (pc < 512) {
778 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
779 		    X1(DSP_CST(0)),
780 		    X1(DSP_CST(0)),
781 		    X1(DSP_CST(0)),
782 		    X1(DSP_CST(0)));
783 	}
784 
785 	/* clear single step flag, run DSP */
786 	emuxki_write(sc, 0, X1(DBG), 0);
787 }
788 
789 /*
790  * operations
791  */
792 
793 static void
794 emuxki_play_start(struct emuxki_softc *sc, int ch, uint32_t start, uint32_t end)
795 {
796 	uint32_t pitch;
797 	uint32_t volume;
798 
799 	/* 48kHz:16384 = 128/375 */
800 	pitch = sc->play.sample_rate * 128 / 375;
801 	volume = 32767;
802 
803 	emuxki_write(sc, ch, EMU_CHAN_DSL,
804 	    (0 << 24) |	/* send amound D = 0 */
805 	    end);
806 
807 	emuxki_write(sc, ch, EMU_CHAN_PSST,
808 	    (0 << 24) |	/* send amount C = 0 */
809 	    start);
810 
811 	emuxki_write(sc, ch, EMU_CHAN_VTFT,
812 	    (volume << 16) |
813 	    (0xffff));	/* cutoff filter = none */
814 
815 	emuxki_write(sc, ch, EMU_CHAN_CVCF,
816 	    (volume << 16) |
817 	    (0xffff));	/* cutoff filter = none */
818 
819 	emuxki_write(sc, ch, EMU_CHAN_PTRX,
820 	    (pitch << 16) |
821 	    ((ch == 0 ? 0x7f : 0) << 8) |	/* send amount A = 255,0(L) */
822 	    ((ch == 0 ? 0 : 0x7f)));		/* send amount B = 0,255(R) */
823 
824 	/* set the pitch to start */
825 	emuxki_write(sc, ch, EMU_CHAN_CPF,
826 	    (pitch << 16) |
827 	    EMU_CHAN_CPF_STEREO_MASK);	/* stereo only */
828 }
829 
830 static void
831 emuxki_play_stop(struct emuxki_softc *sc, int ch)
832 {
833 
834 	/* pitch = 0 to stop playing */
835 	emuxki_write(sc, ch, EMU_CHAN_CPF, EMU_CHAN_CPF_STOP_MASK);
836 	/* volume = 0 */
837 	emuxki_write(sc, ch, EMU_CHAN_CVCF, 0);
838 }
839 
840 static void
841 emuxki_timer_start(struct emuxki_softc *sc)
842 {
843 	uint32_t timer;
844 
845 	/* frame count of half PTE at 16bit, 2ch, 48kHz */
846 	timer = EMU_PTESIZE / 4 / 2;
847 
848 	/* EMU_TIMER is 16bit register */
849 	emuxki_writeio_2(sc, EMU_TIMER, timer);
850 	emuxki_writeio_4(sc, EMU_INTE,
851 	    emuxki_readio_4(sc, EMU_INTE) |
852 	        EMU_INTE_INTERTIMERENB);
853 	DPRINTF("timer start\n");
854 }
855 
856 static void
857 emuxki_timer_stop(struct emuxki_softc *sc)
858 {
859 
860 	emuxki_writeio_4(sc, EMU_INTE,
861 	    emuxki_readio_4(sc, EMU_INTE) &
862 	        ~EMU_INTE_INTERTIMERENB);
863 	/* EMU_TIMER is 16bit register */
864 	emuxki_writeio_2(sc, EMU_TIMER, 0);
865 	DPRINTF("timer stop\n");
866 }
867 
868 /*
869  * audio interface
870  */
871 
872 static int
873 emuxki_query_format(void *hdl, audio_format_query_t *afp)
874 {
875 
876 	return audio_query_format(emuxki_formats, EMUXKI_NFORMATS, afp);
877 }
878 
879 static int
880 emuxki_set_format(void *hdl, int setmode,
881     const audio_params_t *play, const audio_params_t *rec,
882     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
883 {
884 	struct emuxki_softc *sc = hdl;
885 
886 	if ((setmode & AUMODE_PLAY))
887 		sc->play = *play;
888 	if ((setmode & AUMODE_RECORD))
889 		sc->rec = *rec;
890 	return 0;
891 }
892 
893 static int
894 emuxki_halt_output(void *hdl)
895 {
896 	struct emuxki_softc *sc = hdl;
897 
898 	emuxki_timer_stop(sc);
899 	emuxki_play_stop(sc, 0);
900 	emuxki_play_stop(sc, 1);
901 	return 0;
902 }
903 
904 static int
905 emuxki_halt_input(void *hdl)
906 {
907 	struct emuxki_softc *sc = hdl;
908 
909 	/* stop ADC */
910 	emuxki_write(sc, 0, EMU_ADCCR, 0);
911 
912 	/* disable interrupt */
913 	emuxki_writeio_4(sc, EMU_INTE,
914 	    emuxki_readio_4(sc, EMU_INTE) &
915 	        ~EMU_INTE_ADCBUFENABLE);
916 
917 	return 0;
918 }
919 
920 static int
921 emuxki_intr(void *hdl)
922 {
923 	struct emuxki_softc *sc = hdl;
924 	uint32_t ipr;
925 	uint32_t curaddr;
926 	int handled = 0;
927 
928 	mutex_spin_enter(&sc->sc_intr_lock);
929 
930 	ipr = emuxki_readio_4(sc, EMU_IPR);
931 	DPRINTFN(3, "emuxki: ipr=%08x\n", ipr);
932 	if (sc->pintr && (ipr & EMU_IPR_INTERVALTIMER)) {
933 		/* read ch 0 */
934 		curaddr = emuxki_read(sc, 0, EMU_CHAN_CCCA) &
935 		    EMU_CHAN_CCCA_CURRADDR_MASK;
936 		DPRINTFN(3, "curaddr=%08x\n", curaddr);
937 		curaddr *= sc->pframesize;
938 
939 		if (curaddr < sc->poffset)
940 			curaddr += sc->plength;
941 		if (curaddr >= sc->poffset + sc->pblksize) {
942 			dmamem_sync(sc->pmem, BUS_DMASYNC_POSTWRITE);
943 			sc->pintr(sc->pintrarg);
944 			sc->poffset += sc->pblksize;
945 			if (sc->poffset >= sc->plength) {
946 				sc->poffset -= sc->plength;
947 			}
948 			dmamem_sync(sc->pmem, BUS_DMASYNC_PREWRITE);
949 		}
950 		handled = 1;
951 	}
952 
953 	if (sc->rintr &&
954 	    (ipr & (EMU_IPR_ADCBUFHALFFULL | EMU_IPR_ADCBUFFULL))) {
955 		char *src;
956 		char *dst;
957 
958 		/* Record DMA buffer has just 2 blocks */
959 		src = KERNADDR(sc->rmem);
960 		if (ipr & EMU_IPR_ADCBUFFULL) {
961 			/* 2nd block */
962 			src += EMU_REC_DMABLKSIZE;
963 		}
964 		dst = (char *)sc->rptr + sc->rcurrent;
965 
966 		dmamem_sync(sc->rmem, BUS_DMASYNC_POSTREAD);
967 		memcpy(dst, src, EMU_REC_DMABLKSIZE);
968 		/* for next trans */
969 		dmamem_sync(sc->rmem, BUS_DMASYNC_PREREAD);
970 		sc->rcurrent += EMU_REC_DMABLKSIZE;
971 
972 		if (sc->rcurrent >= sc->roffset + sc->rblksize) {
973 			sc->rintr(sc->rintrarg);
974 			sc->roffset += sc->rblksize;
975 			if (sc->roffset >= sc->rlength) {
976 				sc->roffset = 0;
977 				sc->rcurrent = 0;
978 			}
979 		}
980 
981 		handled = 1;
982 	}
983 
984 #if defined(EMUXKI_DEBUG)
985 	if (!handled) {
986 		char buf[1024];
987 		snprintb(buf, sizeof(buf),
988 		    "\20"
989 		    "\x19""RATETRCHANGE"
990 		    "\x18""FXDSP"
991 		    "\x17""FORCEINT"
992 		    "\x16""PCIERROR"
993 		    "\x15""VOLINCR"
994 		    "\x14""VOLDECR"
995 		    "\x13""MUTE"
996 		    "\x12""MICBUFFULL"
997 		    "\x11""MICBUFHALFFULL"
998 		    "\x10""ADCBUFFULL"
999 		    "\x0f""ADCBUFHALFFULL"
1000 		    "\x0e""EFXBUFFULL"
1001 		    "\x0d""EFXBUFHALFFULL"
1002 		    "\x0c""GPSPDIFSTCHANGE"
1003 		    "\x0b""CDROMSTCHANGE"
1004 		    /*     INTERVALTIMER */
1005 		    "\x09""MIDITRANSBUFE"
1006 		    "\x08""MIDIRECVBUFE"
1007 		    "\x07""CHANNELLOOP"
1008 		    , ipr);
1009 		DPRINTF("unexpected intr: %s\n", buf);
1010 
1011 		/* for debugging (must not handle if !DEBUG) */
1012 		handled = 1;
1013 	}
1014 #endif
1015 
1016 	/* Reset interrupt bit */
1017 	emuxki_writeio_4(sc, EMU_IPR, ipr);
1018 
1019 	mutex_spin_exit(&sc->sc_intr_lock);
1020 
1021 	/* Interrupt handler must return !=0 if handled */
1022 	return handled;
1023 }
1024 
1025 static int
1026 emuxki_getdev(void *hdl, struct audio_device *dev)
1027 {
1028 	struct emuxki_softc *sc = hdl;
1029 
1030 	*dev = sc->sc_audv;
1031 	return 0;
1032 }
1033 
1034 static int
1035 emuxki_set_port(void *hdl, mixer_ctrl_t *mctl)
1036 {
1037 	struct emuxki_softc *sc = hdl;
1038 
1039 	return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl);
1040 }
1041 
1042 static int
1043 emuxki_get_port(void *hdl, mixer_ctrl_t *mctl)
1044 {
1045 	struct emuxki_softc *sc = hdl;
1046 
1047 	return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl);
1048 }
1049 
1050 static int
1051 emuxki_query_devinfo(void *hdl, mixer_devinfo_t *minfo)
1052 {
1053 	struct emuxki_softc *sc = hdl;
1054 
1055 	return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo);
1056 }
1057 
1058 static void *
1059 emuxki_allocm(void *hdl, int direction, size_t size)
1060 {
1061 	struct emuxki_softc *sc = hdl;
1062 
1063 	if (direction == AUMODE_PLAY) {
1064 		if (sc->pmem) {
1065 			panic("pmem already allocated\n");
1066 			return NULL;
1067 		}
1068 		sc->pmem = dmamem_alloc(sc, size);
1069 		return KERNADDR(sc->pmem);
1070 	} else {
1071 		/* rmem is fixed size internal DMA buffer */
1072 		if (sc->rmem) {
1073 			panic("rmem already allocated\n");
1074 			return NULL;
1075 		}
1076 		/* rmem fixed size */
1077 		sc->rmem = dmamem_alloc(sc, EMU_REC_DMASIZE);
1078 
1079 		/* recording MI buffer is normal kmem, software trans. */
1080 		sc->rptr = kmem_alloc(size, KM_SLEEP);
1081 		return sc->rptr;
1082 	}
1083 }
1084 
1085 static void
1086 emuxki_freem(void *hdl, void *ptr, size_t size)
1087 {
1088 	struct emuxki_softc *sc = hdl;
1089 
1090 	if (sc->pmem && ptr == KERNADDR(sc->pmem)) {
1091 		dmamem_free(sc->pmem);
1092 		sc->pmem = NULL;
1093 	}
1094 	if (sc->rmem && ptr == sc->rptr) {
1095 		dmamem_free(sc->rmem);
1096 		sc->rmem = NULL;
1097 		kmem_free(sc->rptr, size);
1098 		sc->rptr = NULL;
1099 	}
1100 }
1101 
1102 /*
1103  * blocksize rounding to EMU_PTESIZE. It is for easy to drive.
1104  */
1105 static int
1106 emuxki_round_blocksize(void *hdl, int blksize,
1107     int mode, const audio_params_t* param)
1108 {
1109 
1110 	/*
1111 	 * This is not necessary for recording, but symmetric for easy.
1112 	 * For recording buffer/block size requirements of hardware,
1113 	 * see EMU_RECBS_BUFSIZE_*
1114 	 */
1115 	if (blksize < EMU_PTESIZE)
1116 		blksize = EMU_PTESIZE;
1117 	return rounddown(blksize, EMU_PTESIZE);
1118 }
1119 
1120 static size_t
1121 emuxki_round_buffersize(void *hdl, int direction, size_t bsize)
1122 {
1123 
1124 	/* This is not necessary for recording, but symmetric for easy */
1125 	if (bsize < EMU_MINPTE * EMU_PTESIZE) {
1126 		bsize = EMU_MINPTE * EMU_PTESIZE;
1127 	} else if (bsize > EMU_MAXPTE * EMU_PTESIZE) {
1128 		bsize = EMU_MAXPTE * EMU_PTESIZE;
1129 	}
1130 	return roundup(bsize, EMU_PTESIZE);
1131 }
1132 
1133 static int
1134 emuxki_get_props(void *hdl)
1135 {
1136 
1137 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
1138 	    AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
1139 }
1140 
1141 static int
1142 emuxki_trigger_output(void *hdl, void *start, void *end, int blksize,
1143     void (*intr)(void *), void *arg, const audio_params_t *params)
1144 {
1145 	struct emuxki_softc *sc = hdl;
1146 	int npage;
1147 	uint32_t *kptb;
1148 	bus_addr_t dpmem;
1149 	int i;
1150 	uint32_t hwstart;
1151 	uint32_t hwend;
1152 
1153 	if (sc->pmem == NULL)
1154 		panic("pmem == NULL\n");
1155 	if (start != KERNADDR(sc->pmem))
1156 		panic("start != KERNADDR(sc->pmem)\n");
1157 
1158 	sc->pframesize = 4;	/* channels * bit / 8 = 2*16/8=4 */
1159 	sc->pblksize = blksize;
1160 	sc->plength = (char *)end - (char *)start;
1161 	sc->poffset = 0;
1162 	npage = roundup(sc->plength, EMU_PTESIZE);
1163 
1164 	kptb = KERNADDR(sc->ptb);
1165 	dpmem = DMAADDR(sc->pmem);
1166 	for (i = 0; i < npage; i++) {
1167 		kptb[i] = htole32(dpmem << 1);
1168 		dpmem += EMU_PTESIZE;
1169 	}
1170 	dmamem_sync(sc->ptb, BUS_DMASYNC_PREWRITE);
1171 
1172 	hwstart = 0;
1173 	hwend = hwstart + sc->plength / sc->pframesize;
1174 
1175 	sc->pintr = intr;
1176 	sc->pintrarg = arg;
1177 
1178 	dmamem_sync(sc->pmem, BUS_DMASYNC_PREWRITE);
1179 
1180 	emuxki_play_start(sc, 0, hwstart, hwend);
1181 	emuxki_play_start(sc, 1, hwstart, hwend);
1182 
1183 	emuxki_timer_start(sc);
1184 
1185 	return 0;
1186 }
1187 
1188 /*
1189  * Recording uses temporary buffer.  Because it can use ADC_HALF/FULL
1190  * interrupts and this method doesn't conflict with playback.
1191  */
1192 
1193 static int
1194 emuxki_trigger_input(void *hdl, void *start, void *end, int blksize,
1195     void (*intr)(void *), void *arg, const audio_params_t *params)
1196 {
1197 	struct emuxki_softc *sc = hdl;
1198 
1199 	if (sc->rmem == NULL)
1200 		panic("rmem == NULL\n");
1201 	if (start != sc->rptr)
1202 		panic("start != sc->rptr\n");
1203 
1204 	sc->rframesize = 4;	/* channels * bit / 8 = 2*16/8=4 */
1205 	sc->rblksize = blksize;
1206 	sc->rlength = (char *)end - (char *)start;
1207 	sc->roffset = 0;
1208 	sc->rcurrent = 0;
1209 
1210 	sc->rintr = intr;
1211 	sc->rintrarg = arg;
1212 
1213 	/*
1214 	 * Memo:
1215 	 *  recording source is selected by AC97
1216 	 *  AC97 input source routes to ADC by FX(DSP)
1217 	 *
1218 	 * Must keep following sequence order
1219 	 */
1220 
1221 	/* first, stop ADC */
1222 	emuxki_write(sc, 0, EMU_ADCCR, 0);
1223 	emuxki_write(sc, 0, EMU_ADCBA, 0);
1224 	emuxki_write(sc, 0, EMU_ADCBS, 0);
1225 
1226 	dmamem_sync(sc->rmem, BUS_DMASYNC_PREREAD);
1227 
1228 	/* ADC interrupt enable */
1229 	emuxki_writeio_4(sc, EMU_INTE,
1230 	    emuxki_readio_4(sc, EMU_INTE) |
1231 	        EMU_INTE_ADCBUFENABLE);
1232 
1233 	/* ADC Enable */
1234 	/* stereo, 48kHz, enable */
1235 	emuxki_write(sc, 0, EMU_ADCCR,
1236 	    X1(ADCCR_LCHANENABLE) | X1(ADCCR_RCHANENABLE));
1237 
1238 	/* ADC buffer address */
1239 	emuxki_write(sc, 0, X1(ADCIDX), 0);
1240 	emuxki_write(sc, 0, EMU_ADCBA, DMAADDR(sc->rmem));
1241 
1242 	/* ADC buffer size, to start */
1243 	emuxki_write(sc, 0, EMU_ADCBS, EMU_REC_BUFSIZE_RECBS);
1244 
1245 	return 0;
1246 }
1247 
1248 static void
1249 emuxki_get_locks(void *hdl, kmutex_t **intr, kmutex_t **proc)
1250 {
1251 	struct emuxki_softc *sc = hdl;
1252 
1253 	*intr = &sc->sc_intr_lock;
1254 	*proc = &sc->sc_lock;
1255 }
1256 
1257 /*
1258  * AC97
1259  */
1260 
1261 static int
1262 emuxki_ac97_init(struct emuxki_softc *sc)
1263 {
1264 
1265 	sc->hostif.arg = sc;
1266 	sc->hostif.attach = emuxki_ac97_attach;
1267 	sc->hostif.read = emuxki_ac97_read;
1268 	sc->hostif.write = emuxki_ac97_write;
1269 	sc->hostif.reset = emuxki_ac97_reset;
1270 	sc->hostif.flags = emuxki_ac97_flags;
1271 	return ac97_attach(&sc->hostif, sc->sc_dev, &sc->sc_lock);
1272 }
1273 
1274 /*
1275  * AC97 callbacks
1276  */
1277 
1278 static int
1279 emuxki_ac97_attach(void *hdl, struct ac97_codec_if *codecif)
1280 {
1281 	struct emuxki_softc *sc = hdl;
1282 
1283 	sc->codecif = codecif;
1284 	return 0;
1285 }
1286 
1287 static int
1288 emuxki_ac97_read(void *hdl, uint8_t reg, uint16_t *val)
1289 {
1290 	struct emuxki_softc *sc = hdl;
1291 
1292 	mutex_spin_enter(&sc->sc_index_lock);
1293 	emuxki_writeio_1(sc, EMU_AC97ADDR, reg);
1294 	*val = emuxki_readio_2(sc, EMU_AC97DATA);
1295 	mutex_spin_exit(&sc->sc_index_lock);
1296 
1297 	return 0;
1298 }
1299 
1300 static int
1301 emuxki_ac97_write(void *hdl, uint8_t reg, uint16_t val)
1302 {
1303 	struct emuxki_softc *sc = hdl;
1304 
1305 	mutex_spin_enter(&sc->sc_index_lock);
1306 	emuxki_writeio_1(sc, EMU_AC97ADDR, reg);
1307 	emuxki_writeio_2(sc, EMU_AC97DATA, val);
1308 	mutex_spin_exit(&sc->sc_index_lock);
1309 
1310 	return 0;
1311 }
1312 
1313 static int
1314 emuxki_ac97_reset(void *hdl)
1315 {
1316 
1317 	return 0;
1318 }
1319 
1320 static enum ac97_host_flags
1321 emuxki_ac97_flags(void *hdl)
1322 {
1323 
1324 	return AC97_HOST_SWAPPED_CHANNELS;
1325 }
1326 
1327 MODULE(MODULE_CLASS_DRIVER, emuxki, "pci,audio");
1328 
1329 #ifdef _MODULE
1330 #include "ioconf.c"
1331 #endif
1332 
1333 static int
1334 emuxki_modcmd(modcmd_t cmd, void *opaque)
1335 {
1336 	int error = 0;
1337 
1338 	switch (cmd) {
1339 	case MODULE_CMD_INIT:
1340 #ifdef _MODULE
1341 		error = config_init_component(cfdriver_ioconf_emuxki,
1342 		    cfattach_ioconf_emuxki, cfdata_ioconf_emuxki);
1343 #endif
1344 		return error;
1345 	case MODULE_CMD_FINI:
1346 #ifdef _MODULE
1347 		error = config_fini_component(cfdriver_ioconf_emuxki,
1348 		    cfattach_ioconf_emuxki, cfdata_ioconf_emuxki);
1349 #endif
1350 		return error;
1351 	default:
1352 		return ENOTTY;
1353 	}
1354 }
1355