xref: /netbsd-src/sys/dev/pci/emuxki.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: emuxki.c,v 1.77 2023/05/10 00:11:41 riastradh 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.77 2023/05/10 00:11:41 riastradh 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 = device_private(self);
487 	int error;
488 
489 	error = config_detach_children(self, flags);
490 	if (error)
491 		return error;
492 
493 	/* All voices should be stopped now but add some code here if not */
494 	emuxki_writeio_4(sc, EMU_HCFG,
495 	    EMU_HCFG_LOCKSOUNDCACHE |
496 	    EMU_HCFG_LOCKTANKCACHE_MASK |
497 	    EMU_HCFG_MUTEBUTTONENABLE);
498 	emuxki_writeio_4(sc, EMU_INTE, 0);
499 
500 	/* Disable any Channels interrupts */
501 	emuxki_write(sc, 0, EMU_CLIEL, 0);
502 	emuxki_write(sc, 0, EMU_CLIEH, 0);
503 	emuxki_write(sc, 0, EMU_SOLEL, 0);
504 	emuxki_write(sc, 0, EMU_SOLEH, 0);
505 
506 	/* stop DSP */
507 	emuxki_write(sc, 0, X1(DBG), X1(DBG_SINGLE_STEP));
508 
509 	dmamem_free(sc->ptb);
510 
511 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
512 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
513 
514 	mutex_destroy(&sc->sc_lock);
515 	mutex_destroy(&sc->sc_intr_lock);
516 	mutex_destroy(&sc->sc_index_lock);
517 
518 	return 0;
519 }
520 
521 static int
522 emuxki_init(struct emuxki_softc *sc)
523 {
524 	int i;
525 	uint32_t spcs;
526 	uint32_t hcfg;
527 
528 	/* clear AUDIO bit */
529 	emuxki_writeio_4(sc, EMU_HCFG,
530 	    EMU_HCFG_LOCKSOUNDCACHE |
531 	    EMU_HCFG_LOCKTANKCACHE_MASK |
532 	    EMU_HCFG_MUTEBUTTONENABLE);
533 
534 	/* mask interrupt without PCIERR */
535 	emuxki_writeio_4(sc, EMU_INTE,
536 	    EMU_INTE_SAMPLERATER | /* always on this bit */
537 	    EMU_INTE_PCIERRENABLE);
538 
539 	/* disable all channel interrupt */
540 	emuxki_write(sc, 0, EMU_CLIEL, 0);
541 	emuxki_write(sc, 0, EMU_CLIEH, 0);
542 	emuxki_write(sc, 0, EMU_SOLEL, 0);
543 	emuxki_write(sc, 0, EMU_SOLEH, 0);
544 
545 	/* Set recording buffers sizes to zero */
546 	emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
547 	emuxki_write(sc, 0, EMU_MICBA, 0);
548 	emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
549 	emuxki_write(sc, 0, EMU_FXBA, 0);
550 	emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
551 	emuxki_write(sc, 0, EMU_ADCBA, 0);
552 
553 	if(sc->sc_type & EMUXKI_AUDIGY) {
554 		emuxki_write(sc, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS);
555 		emuxki_write(sc, 0, EMU_AC97SLOT,
556 		    EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
557 	}
558 
559 	/* Initialize all channels to stopped and no effects */
560 	for (i = 0; i < EMU_NUMCHAN; i++) {
561 		emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0x7f7f);
562 		emuxki_write(sc, i, EMU_CHAN_IP, EMU_CHAN_IP_UNITY);
563 		emuxki_write(sc, i, EMU_CHAN_VTFT, 0xffff);
564 		emuxki_write(sc, i, EMU_CHAN_CVCF, 0xffff);
565 		emuxki_write(sc, i, EMU_CHAN_PTRX, 0);
566 		emuxki_write(sc, i, EMU_CHAN_CPF, 0);
567 		emuxki_write(sc, i, EMU_CHAN_CCR, 0);
568 		emuxki_write(sc, i, EMU_CHAN_PSST, 0);
569 		emuxki_write(sc, i, EMU_CHAN_DSL, 0);
570 		emuxki_write(sc, i, EMU_CHAN_CCCA, EMU_CHAN_CCCA_INTERPROM_1);
571 		emuxki_write(sc, i, EMU_CHAN_Z1, 0);
572 		emuxki_write(sc, i, EMU_CHAN_Z2, 0);
573 		emuxki_write(sc, i, EMU_CHAN_MAPA, 0xffffffff);
574 		emuxki_write(sc, i, EMU_CHAN_MAPB, 0xffffffff);
575 		emuxki_write(sc, i, EMU_CHAN_FXRT, 0x32100000);
576 		emuxki_write(sc, i, EMU_CHAN_ATKHLDM, 0);
577 		emuxki_write(sc, i, EMU_CHAN_DCYSUSM, 0);
578 		emuxki_write(sc, i, EMU_CHAN_IFATN, 0xffff);
579 		emuxki_write(sc, i, EMU_CHAN_PEFE, 0x007f);
580 		emuxki_write(sc, i, EMU_CHAN_FMMOD, 0);
581 		emuxki_write(sc, i, EMU_CHAN_TREMFRQ, 0);
582 		emuxki_write(sc, i, EMU_CHAN_FM2FRQ2, 0);
583 		emuxki_write(sc, i, EMU_CHAN_TEMPENV, 0);
584 
585 		/* these are last so OFF prevents writing */
586 		emuxki_write(sc, i, EMU_CHAN_LFOVAL2, 0x8000);
587 		emuxki_write(sc, i, EMU_CHAN_LFOVAL1, 0x8000);
588 		emuxki_write(sc, i, EMU_CHAN_ATKHLDV, 0x7f7f);
589 		emuxki_write(sc, i, EMU_CHAN_ENVVOL, 0);
590 		emuxki_write(sc, i, EMU_CHAN_ENVVAL, 0x8000);
591 	}
592 
593 	/* set digital outputs format */
594 	spcs = EMU_SPCS_CLKACCY_1000PPM |
595 	       EMU_SPCS_SAMPLERATE_48 |
596 	       EMU_SPCS_CHANNELNUM_LEFT |
597 	       EMU_SPCS_SOURCENUM_UNSPEC |
598 	       EMU_SPCS_GENERATIONSTATUS |
599 	       0x00001200 /* Cat code. */ |
600 	       0x00000000 /* IEC-958 Mode */ |
601 	       EMU_SPCS_EMPHASIS_NONE |
602 	       EMU_SPCS_COPYRIGHT;
603 	emuxki_write(sc, 0, EMU_SPCS0, spcs);
604 	emuxki_write(sc, 0, EMU_SPCS1, spcs);
605 	emuxki_write(sc, 0, EMU_SPCS2, spcs);
606 
607 	if (sc->sc_type & EMUXKI_AUDIGY2_CA0108) {
608 		/* Setup SRCMulti_I2S SamplingRate */
609 		emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE,
610 		    emuxki_read(sc, 0, EMU_A2_SPDIF_SAMPLERATE) & 0xfffff1ff);
611 
612 		/* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */
613 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCSEL,
614 		    EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
615 
616 		/* Setup SRCMulti Input Audio Enable */
617 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA,
618 		    0x7b0000, 0xff000000);
619 
620 		/* Setup SPDIF Out Audio Enable
621 		 * The Audigy 2 Value has a separate SPDIF out,
622 		 * so no need for a mixer switch */
623 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA,
624 		    0x7a0000, 0xff000000);
625 		emuxki_writeio_4(sc, EMU_A_IOCFG,
626 		    emuxki_readio_4(sc, EMU_A_IOCFG) & ~0x8); /* clear bit 3 */
627 	} else if (sc->sc_type & EMUXKI_AUDIGY2) {
628 		emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE,
629 		    EMU_A2_SPDIF_UNKNOWN);
630 
631 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCSEL,
632 		    EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
633 
634 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCMULTI,
635 		    EMU_A2_SRCMULTI_ENABLE_INPUT);
636 	}
637 
638 	/* page table */
639 	sc->ptb = dmamem_alloc(sc, EMU_MAXPTE * sizeof(uint32_t));
640 	if (sc->ptb == NULL) {
641 		device_printf(sc->sc_dev, "ptb allocation error\n");
642 		return ENOMEM;
643 	}
644 	emuxki_write(sc, 0, EMU_PTB, DMAADDR(sc->ptb));
645 
646 	emuxki_write(sc, 0, EMU_TCBS, 0);	/* This means 16K TCB */
647 	emuxki_write(sc, 0, EMU_TCB, 0);	/* No TCB use for now */
648 
649 	/* Let's play with sound processor */
650 	emuxki_initfx(sc);
651 
652 	/* enable interrupt */
653 	emuxki_writeio_4(sc, EMU_INTE,
654 	    emuxki_readio_4(sc, EMU_INTE) |
655 	    EMU_INTE_VOLINCRENABLE |
656 	    EMU_INTE_VOLDECRENABLE |
657 	    EMU_INTE_MUTEENABLE);
658 
659 	if (sc->sc_type & EMUXKI_AUDIGY2_CA0108) {
660 		emuxki_writeio_4(sc, EMU_A_IOCFG,
661 		    0x0060 | emuxki_readio_4(sc, EMU_A_IOCFG));
662 	} else if (sc->sc_type & EMUXKI_AUDIGY2) {
663 		emuxki_writeio_4(sc, EMU_A_IOCFG,
664 		    EMU_A_IOCFG_GPOUT0 | emuxki_readio_4(sc, EMU_A_IOCFG));
665 	}
666 
667 	/* enable AUDIO bit */
668 	hcfg = EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE;
669 
670 	if (sc->sc_type & EMUXKI_AUDIGY2) {
671 		hcfg |= EMU_HCFG_AC3ENABLE_CDSPDIF |
672 		        EMU_HCFG_AC3ENABLE_GPSPDIF;
673 	} else if (sc->sc_type & EMUXKI_AUDIGY) {
674 	} else {
675 		hcfg |= EMU_HCFG_LOCKTANKCACHE_MASK;
676 	}
677 	/* joystick not supported now */
678 	emuxki_writeio_4(sc, EMU_HCFG, hcfg);
679 
680 	return 0;
681 }
682 
683 /*
684  * dsp programming
685  */
686 
687 static void
688 emuxki_dsp_addop(struct emuxki_softc *sc, uint16_t *pc, uint8_t op,
689     uint16_t r, uint16_t a, uint16_t x, uint16_t y)
690 {
691 	uint32_t loword;
692 	uint32_t hiword;
693 	int reg;
694 
695 	if (sc->sc_type & EMUXKI_AUDIGY) {
696 		reg = EMU_A_MICROCODEBASE;
697 		loword = (x << 12) & EMU_A_DSP_LOWORD_OPX_MASK;
698 		loword |= y & EMU_A_DSP_LOWORD_OPY_MASK;
699 		hiword = (op << 24) & EMU_A_DSP_HIWORD_OPCODE_MASK;
700 		hiword |= (r << 12) & EMU_A_DSP_HIWORD_RESULT_MASK;
701 		hiword |= a & EMU_A_DSP_HIWORD_OPA_MASK;
702 	} else {
703 		reg = EMU_MICROCODEBASE;
704 		loword = (x << 10) & EMU_DSP_LOWORD_OPX_MASK;
705 		loword |= y & EMU_DSP_LOWORD_OPY_MASK;
706 		hiword = (op << 20) & EMU_DSP_HIWORD_OPCODE_MASK;
707 		hiword |= (r << 10) & EMU_DSP_HIWORD_RESULT_MASK;
708 		hiword |= a & EMU_DSP_HIWORD_OPA_MASK;
709 	}
710 
711 	reg += (*pc) * 2;
712 	/* must ordering; lo, hi */
713 	emuxki_write(sc, 0, reg, loword);
714 	emuxki_write(sc, 0, reg + 1, hiword);
715 
716 	(*pc)++;
717 }
718 
719 static void
720 emuxki_initfx(struct emuxki_softc *sc)
721 {
722 	uint16_t pc;
723 
724 	/* Set all GPRs to 0 */
725 	for (pc = 0; pc < 256; pc++)
726 		emuxki_write(sc, 0, EMU_DSP_GPR(pc), 0);
727 	for (pc = 0; pc < 160; pc++) {
728 		emuxki_write(sc, 0, EMU_TANKMEMDATAREGBASE + pc, 0);
729 		emuxki_write(sc, 0, EMU_TANKMEMADDRREGBASE + pc, 0);
730 	}
731 
732 	/* stop DSP, single step mode */
733 	emuxki_write(sc, 0, X1(DBG), X1(DBG_SINGLE_STEP));
734 
735 	/* XXX: delay (48kHz equiv. 21us) if needed */
736 
737 	/* start DSP programming */
738 	pc = 0;
739 
740 	/* OUT[L/R] = 0 + FX[L/R] * 1 */
741 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
742 	    X2(DSP_OUTL, DSP_OUT_A_FRONT),
743 	    X1(DSP_CST(0)),
744 	    X1(DSP_FX(0)),
745 	    X1(DSP_CST(1)));
746 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
747 	    X2(DSP_OUTR, DSP_OUT_A_FRONT),
748 	    X1(DSP_CST(0)),
749 	    X1(DSP_FX(1)),
750 	    X1(DSP_CST(1)));
751 #if 0
752 	/* XXX: rear feature??? */
753 	/* Rear OUT[L/R] = 0 + FX[L/R] * 1 */
754 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
755 	    X2(DSP_OUTL, DSP_OUT_A_REAR),
756 	    X1(DSP_CST(0)),
757 	    X1(DSP_FX(0)),
758 	    X1(DSP_CST(1)));
759 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
760 	    X2(DSP_OUTR, DSP_OUT_A_REAR),
761 	    X1(DSP_CST(0)),
762 	    X1(DSP_FX(1)),
763 	    X1(DSP_CST(1)));
764 #endif
765 	/* ADC recording[L/R] = AC97 In[L/R] */
766 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
767 	    X2(DSP_OUTL, DSP_OUT_ADC),
768 	    X2(DSP_INL, DSP_IN_AC97),
769 	    X1(DSP_CST(0)),
770 	    X1(DSP_CST(0)));
771 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
772 	    X2(DSP_OUTR, DSP_OUT_ADC),
773 	    X2(DSP_INR, DSP_IN_AC97),
774 	    X1(DSP_CST(0)),
775 	    X1(DSP_CST(0)));
776 
777 	/* fill NOP the rest of the microcode */
778 	while (pc < 512) {
779 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
780 		    X1(DSP_CST(0)),
781 		    X1(DSP_CST(0)),
782 		    X1(DSP_CST(0)),
783 		    X1(DSP_CST(0)));
784 	}
785 
786 	/* clear single step flag, run DSP */
787 	emuxki_write(sc, 0, X1(DBG), 0);
788 }
789 
790 /*
791  * operations
792  */
793 
794 static void
795 emuxki_play_start(struct emuxki_softc *sc, int ch, uint32_t start, uint32_t end)
796 {
797 	uint32_t pitch;
798 	uint32_t volume;
799 
800 	/* 48kHz:16384 = 128/375 */
801 	pitch = sc->play.sample_rate * 128 / 375;
802 	volume = 32767;
803 
804 	emuxki_write(sc, ch, EMU_CHAN_DSL,
805 	    (0 << 24) |	/* send amound D = 0 */
806 	    end);
807 
808 	emuxki_write(sc, ch, EMU_CHAN_PSST,
809 	    (0 << 24) |	/* send amount C = 0 */
810 	    start);
811 
812 	emuxki_write(sc, ch, EMU_CHAN_VTFT,
813 	    (volume << 16) |
814 	    (0xffff));	/* cutoff filter = none */
815 
816 	emuxki_write(sc, ch, EMU_CHAN_CVCF,
817 	    (volume << 16) |
818 	    (0xffff));	/* cutoff filter = none */
819 
820 	emuxki_write(sc, ch, EMU_CHAN_PTRX,
821 	    (pitch << 16) |
822 	    ((ch == 0 ? 0x7f : 0) << 8) |	/* send amount A = 255,0(L) */
823 	    ((ch == 0 ? 0 : 0x7f)));		/* send amount B = 0,255(R) */
824 
825 	/* set the pitch to start */
826 	emuxki_write(sc, ch, EMU_CHAN_CPF,
827 	    (pitch << 16) |
828 	    EMU_CHAN_CPF_STEREO_MASK);	/* stereo only */
829 }
830 
831 static void
832 emuxki_play_stop(struct emuxki_softc *sc, int ch)
833 {
834 
835 	/* pitch = 0 to stop playing */
836 	emuxki_write(sc, ch, EMU_CHAN_CPF, EMU_CHAN_CPF_STOP_MASK);
837 	/* volume = 0 */
838 	emuxki_write(sc, ch, EMU_CHAN_CVCF, 0);
839 }
840 
841 static void
842 emuxki_timer_start(struct emuxki_softc *sc)
843 {
844 	uint32_t timer;
845 
846 	/* frame count of half PTE at 16bit, 2ch, 48kHz */
847 	timer = EMU_PTESIZE / 4 / 2;
848 
849 	/* EMU_TIMER is 16bit register */
850 	emuxki_writeio_2(sc, EMU_TIMER, timer);
851 	emuxki_writeio_4(sc, EMU_INTE,
852 	    emuxki_readio_4(sc, EMU_INTE) |
853 	        EMU_INTE_INTERTIMERENB);
854 	DPRINTF("timer start\n");
855 }
856 
857 static void
858 emuxki_timer_stop(struct emuxki_softc *sc)
859 {
860 
861 	emuxki_writeio_4(sc, EMU_INTE,
862 	    emuxki_readio_4(sc, EMU_INTE) &
863 	        ~EMU_INTE_INTERTIMERENB);
864 	/* EMU_TIMER is 16bit register */
865 	emuxki_writeio_2(sc, EMU_TIMER, 0);
866 	DPRINTF("timer stop\n");
867 }
868 
869 /*
870  * audio interface
871  */
872 
873 static int
874 emuxki_query_format(void *hdl, audio_format_query_t *afp)
875 {
876 
877 	return audio_query_format(emuxki_formats, EMUXKI_NFORMATS, afp);
878 }
879 
880 static int
881 emuxki_set_format(void *hdl, int setmode,
882     const audio_params_t *play, const audio_params_t *rec,
883     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
884 {
885 	struct emuxki_softc *sc = hdl;
886 
887 	if ((setmode & AUMODE_PLAY))
888 		sc->play = *play;
889 	if ((setmode & AUMODE_RECORD))
890 		sc->rec = *rec;
891 	return 0;
892 }
893 
894 static int
895 emuxki_halt_output(void *hdl)
896 {
897 	struct emuxki_softc *sc = hdl;
898 
899 	emuxki_timer_stop(sc);
900 	emuxki_play_stop(sc, 0);
901 	emuxki_play_stop(sc, 1);
902 	return 0;
903 }
904 
905 static int
906 emuxki_halt_input(void *hdl)
907 {
908 	struct emuxki_softc *sc = hdl;
909 
910 	/* stop ADC */
911 	emuxki_write(sc, 0, EMU_ADCCR, 0);
912 
913 	/* disable interrupt */
914 	emuxki_writeio_4(sc, EMU_INTE,
915 	    emuxki_readio_4(sc, EMU_INTE) &
916 	        ~EMU_INTE_ADCBUFENABLE);
917 
918 	return 0;
919 }
920 
921 static int
922 emuxki_intr(void *hdl)
923 {
924 	struct emuxki_softc *sc = hdl;
925 	uint32_t ipr;
926 	uint32_t curaddr;
927 	int handled = 0;
928 
929 	mutex_spin_enter(&sc->sc_intr_lock);
930 
931 	ipr = emuxki_readio_4(sc, EMU_IPR);
932 	DPRINTFN(3, "emuxki: ipr=%08x\n", ipr);
933 	if (sc->pintr && (ipr & EMU_IPR_INTERVALTIMER)) {
934 		/* read ch 0 */
935 		curaddr = emuxki_read(sc, 0, EMU_CHAN_CCCA) &
936 		    EMU_CHAN_CCCA_CURRADDR_MASK;
937 		DPRINTFN(3, "curaddr=%08x\n", curaddr);
938 		curaddr *= sc->pframesize;
939 
940 		if (curaddr < sc->poffset)
941 			curaddr += sc->plength;
942 		if (curaddr >= sc->poffset + sc->pblksize) {
943 			dmamem_sync(sc->pmem, BUS_DMASYNC_POSTWRITE);
944 			sc->pintr(sc->pintrarg);
945 			sc->poffset += sc->pblksize;
946 			if (sc->poffset >= sc->plength) {
947 				sc->poffset -= sc->plength;
948 			}
949 			dmamem_sync(sc->pmem, BUS_DMASYNC_PREWRITE);
950 		}
951 		handled = 1;
952 	}
953 
954 	if (sc->rintr &&
955 	    (ipr & (EMU_IPR_ADCBUFHALFFULL | EMU_IPR_ADCBUFFULL))) {
956 		char *src;
957 		char *dst;
958 
959 		/* Record DMA buffer has just 2 blocks */
960 		src = KERNADDR(sc->rmem);
961 		if (ipr & EMU_IPR_ADCBUFFULL) {
962 			/* 2nd block */
963 			src += EMU_REC_DMABLKSIZE;
964 		}
965 		dst = (char *)sc->rptr + sc->rcurrent;
966 
967 		dmamem_sync(sc->rmem, BUS_DMASYNC_POSTREAD);
968 		memcpy(dst, src, EMU_REC_DMABLKSIZE);
969 		/* for next trans */
970 		dmamem_sync(sc->rmem, BUS_DMASYNC_PREREAD);
971 		sc->rcurrent += EMU_REC_DMABLKSIZE;
972 
973 		if (sc->rcurrent >= sc->roffset + sc->rblksize) {
974 			sc->rintr(sc->rintrarg);
975 			sc->roffset += sc->rblksize;
976 			if (sc->roffset >= sc->rlength) {
977 				sc->roffset = 0;
978 				sc->rcurrent = 0;
979 			}
980 		}
981 
982 		handled = 1;
983 	}
984 
985 #if defined(EMUXKI_DEBUG)
986 	if (!handled) {
987 		char buf[1024];
988 		snprintb(buf, sizeof(buf),
989 		    "\20"
990 		    "\x19""RATETRCHANGE"
991 		    "\x18""FXDSP"
992 		    "\x17""FORCEINT"
993 		    "\x16""PCIERROR"
994 		    "\x15""VOLINCR"
995 		    "\x14""VOLDECR"
996 		    "\x13""MUTE"
997 		    "\x12""MICBUFFULL"
998 		    "\x11""MICBUFHALFFULL"
999 		    "\x10""ADCBUFFULL"
1000 		    "\x0f""ADCBUFHALFFULL"
1001 		    "\x0e""EFXBUFFULL"
1002 		    "\x0d""EFXBUFHALFFULL"
1003 		    "\x0c""GPSPDIFSTCHANGE"
1004 		    "\x0b""CDROMSTCHANGE"
1005 		    /*     INTERVALTIMER */
1006 		    "\x09""MIDITRANSBUFE"
1007 		    "\x08""MIDIRECVBUFE"
1008 		    "\x07""CHANNELLOOP"
1009 		    , ipr);
1010 		DPRINTF("unexpected intr: %s\n", buf);
1011 
1012 		/* for debugging (must not handle if !DEBUG) */
1013 		handled = 1;
1014 	}
1015 #endif
1016 
1017 	/* Reset interrupt bit */
1018 	emuxki_writeio_4(sc, EMU_IPR, ipr);
1019 
1020 	mutex_spin_exit(&sc->sc_intr_lock);
1021 
1022 	/* Interrupt handler must return !=0 if handled */
1023 	return handled;
1024 }
1025 
1026 static int
1027 emuxki_getdev(void *hdl, struct audio_device *dev)
1028 {
1029 	struct emuxki_softc *sc = hdl;
1030 
1031 	*dev = sc->sc_audv;
1032 	return 0;
1033 }
1034 
1035 static int
1036 emuxki_set_port(void *hdl, mixer_ctrl_t *mctl)
1037 {
1038 	struct emuxki_softc *sc = hdl;
1039 
1040 	return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl);
1041 }
1042 
1043 static int
1044 emuxki_get_port(void *hdl, mixer_ctrl_t *mctl)
1045 {
1046 	struct emuxki_softc *sc = hdl;
1047 
1048 	return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl);
1049 }
1050 
1051 static int
1052 emuxki_query_devinfo(void *hdl, mixer_devinfo_t *minfo)
1053 {
1054 	struct emuxki_softc *sc = hdl;
1055 
1056 	return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo);
1057 }
1058 
1059 static void *
1060 emuxki_allocm(void *hdl, int direction, size_t size)
1061 {
1062 	struct emuxki_softc *sc = hdl;
1063 
1064 	if (direction == AUMODE_PLAY) {
1065 		if (sc->pmem) {
1066 			panic("pmem already allocated\n");
1067 			return NULL;
1068 		}
1069 		sc->pmem = dmamem_alloc(sc, size);
1070 		return KERNADDR(sc->pmem);
1071 	} else {
1072 		/* rmem is fixed size internal DMA buffer */
1073 		if (sc->rmem) {
1074 			panic("rmem already allocated\n");
1075 			return NULL;
1076 		}
1077 		/* rmem fixed size */
1078 		sc->rmem = dmamem_alloc(sc, EMU_REC_DMASIZE);
1079 
1080 		/* recording MI buffer is normal kmem, software trans. */
1081 		sc->rptr = kmem_alloc(size, KM_SLEEP);
1082 		return sc->rptr;
1083 	}
1084 }
1085 
1086 static void
1087 emuxki_freem(void *hdl, void *ptr, size_t size)
1088 {
1089 	struct emuxki_softc *sc = hdl;
1090 
1091 	if (sc->pmem && ptr == KERNADDR(sc->pmem)) {
1092 		dmamem_free(sc->pmem);
1093 		sc->pmem = NULL;
1094 	}
1095 	if (sc->rmem && ptr == sc->rptr) {
1096 		dmamem_free(sc->rmem);
1097 		sc->rmem = NULL;
1098 		kmem_free(sc->rptr, size);
1099 		sc->rptr = NULL;
1100 	}
1101 }
1102 
1103 /*
1104  * blocksize rounding to EMU_PTESIZE. It is for easy to drive.
1105  */
1106 static int
1107 emuxki_round_blocksize(void *hdl, int blksize,
1108     int mode, const audio_params_t* param)
1109 {
1110 
1111 	/*
1112 	 * This is not necessary for recording, but symmetric for easy.
1113 	 * For recording buffer/block size requirements of hardware,
1114 	 * see EMU_RECBS_BUFSIZE_*
1115 	 */
1116 	if (blksize < EMU_PTESIZE)
1117 		blksize = EMU_PTESIZE;
1118 	return rounddown(blksize, EMU_PTESIZE);
1119 }
1120 
1121 static size_t
1122 emuxki_round_buffersize(void *hdl, int direction, size_t bsize)
1123 {
1124 
1125 	/* This is not necessary for recording, but symmetric for easy */
1126 	if (bsize < EMU_MINPTE * EMU_PTESIZE) {
1127 		bsize = EMU_MINPTE * EMU_PTESIZE;
1128 	} else if (bsize > EMU_MAXPTE * EMU_PTESIZE) {
1129 		bsize = EMU_MAXPTE * EMU_PTESIZE;
1130 	}
1131 	return roundup(bsize, EMU_PTESIZE);
1132 }
1133 
1134 static int
1135 emuxki_get_props(void *hdl)
1136 {
1137 
1138 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
1139 	    AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
1140 }
1141 
1142 static int
1143 emuxki_trigger_output(void *hdl, void *start, void *end, int blksize,
1144     void (*intr)(void *), void *arg, const audio_params_t *params)
1145 {
1146 	struct emuxki_softc *sc = hdl;
1147 	int npage;
1148 	uint32_t *kptb;
1149 	bus_addr_t dpmem;
1150 	int i;
1151 	uint32_t hwstart;
1152 	uint32_t hwend;
1153 
1154 	if (sc->pmem == NULL)
1155 		panic("pmem == NULL\n");
1156 	if (start != KERNADDR(sc->pmem))
1157 		panic("start != KERNADDR(sc->pmem)\n");
1158 
1159 	sc->pframesize = 4;	/* channels * bit / 8 = 2*16/8=4 */
1160 	sc->pblksize = blksize;
1161 	sc->plength = (char *)end - (char *)start;
1162 	sc->poffset = 0;
1163 	npage = roundup(sc->plength, EMU_PTESIZE);
1164 
1165 	kptb = KERNADDR(sc->ptb);
1166 	dpmem = DMAADDR(sc->pmem);
1167 	for (i = 0; i < npage; i++) {
1168 		kptb[i] = htole32(dpmem << 1);
1169 		dpmem += EMU_PTESIZE;
1170 	}
1171 	dmamem_sync(sc->ptb, BUS_DMASYNC_PREWRITE);
1172 
1173 	hwstart = 0;
1174 	hwend = hwstart + sc->plength / sc->pframesize;
1175 
1176 	sc->pintr = intr;
1177 	sc->pintrarg = arg;
1178 
1179 	dmamem_sync(sc->pmem, BUS_DMASYNC_PREWRITE);
1180 
1181 	emuxki_play_start(sc, 0, hwstart, hwend);
1182 	emuxki_play_start(sc, 1, hwstart, hwend);
1183 
1184 	emuxki_timer_start(sc);
1185 
1186 	return 0;
1187 }
1188 
1189 /*
1190  * Recording uses temporary buffer.  Because it can use ADC_HALF/FULL
1191  * interrupts and this method doesn't conflict with playback.
1192  */
1193 
1194 static int
1195 emuxki_trigger_input(void *hdl, void *start, void *end, int blksize,
1196     void (*intr)(void *), void *arg, const audio_params_t *params)
1197 {
1198 	struct emuxki_softc *sc = hdl;
1199 
1200 	if (sc->rmem == NULL)
1201 		panic("rmem == NULL\n");
1202 	if (start != sc->rptr)
1203 		panic("start != sc->rptr\n");
1204 
1205 	sc->rframesize = 4;	/* channels * bit / 8 = 2*16/8=4 */
1206 	sc->rblksize = blksize;
1207 	sc->rlength = (char *)end - (char *)start;
1208 	sc->roffset = 0;
1209 	sc->rcurrent = 0;
1210 
1211 	sc->rintr = intr;
1212 	sc->rintrarg = arg;
1213 
1214 	/*
1215 	 * Memo:
1216 	 *  recording source is selected by AC97
1217 	 *  AC97 input source routes to ADC by FX(DSP)
1218 	 *
1219 	 * Must keep following sequence order
1220 	 */
1221 
1222 	/* first, stop ADC */
1223 	emuxki_write(sc, 0, EMU_ADCCR, 0);
1224 	emuxki_write(sc, 0, EMU_ADCBA, 0);
1225 	emuxki_write(sc, 0, EMU_ADCBS, 0);
1226 
1227 	dmamem_sync(sc->rmem, BUS_DMASYNC_PREREAD);
1228 
1229 	/* ADC interrupt enable */
1230 	emuxki_writeio_4(sc, EMU_INTE,
1231 	    emuxki_readio_4(sc, EMU_INTE) |
1232 	        EMU_INTE_ADCBUFENABLE);
1233 
1234 	/* ADC Enable */
1235 	/* stereo, 48kHz, enable */
1236 	emuxki_write(sc, 0, EMU_ADCCR,
1237 	    X1(ADCCR_LCHANENABLE) | X1(ADCCR_RCHANENABLE));
1238 
1239 	/* ADC buffer address */
1240 	emuxki_write(sc, 0, X1(ADCIDX), 0);
1241 	emuxki_write(sc, 0, EMU_ADCBA, DMAADDR(sc->rmem));
1242 
1243 	/* ADC buffer size, to start */
1244 	emuxki_write(sc, 0, EMU_ADCBS, EMU_REC_BUFSIZE_RECBS);
1245 
1246 	return 0;
1247 }
1248 
1249 static void
1250 emuxki_get_locks(void *hdl, kmutex_t **intr, kmutex_t **proc)
1251 {
1252 	struct emuxki_softc *sc = hdl;
1253 
1254 	*intr = &sc->sc_intr_lock;
1255 	*proc = &sc->sc_lock;
1256 }
1257 
1258 /*
1259  * AC97
1260  */
1261 
1262 static int
1263 emuxki_ac97_init(struct emuxki_softc *sc)
1264 {
1265 
1266 	sc->hostif.arg = sc;
1267 	sc->hostif.attach = emuxki_ac97_attach;
1268 	sc->hostif.read = emuxki_ac97_read;
1269 	sc->hostif.write = emuxki_ac97_write;
1270 	sc->hostif.reset = emuxki_ac97_reset;
1271 	sc->hostif.flags = emuxki_ac97_flags;
1272 	return ac97_attach(&sc->hostif, sc->sc_dev, &sc->sc_lock);
1273 }
1274 
1275 /*
1276  * AC97 callbacks
1277  */
1278 
1279 static int
1280 emuxki_ac97_attach(void *hdl, struct ac97_codec_if *codecif)
1281 {
1282 	struct emuxki_softc *sc = hdl;
1283 
1284 	sc->codecif = codecif;
1285 	return 0;
1286 }
1287 
1288 static int
1289 emuxki_ac97_read(void *hdl, uint8_t reg, uint16_t *val)
1290 {
1291 	struct emuxki_softc *sc = hdl;
1292 
1293 	mutex_spin_enter(&sc->sc_index_lock);
1294 	emuxki_writeio_1(sc, EMU_AC97ADDR, reg);
1295 	*val = emuxki_readio_2(sc, EMU_AC97DATA);
1296 	mutex_spin_exit(&sc->sc_index_lock);
1297 
1298 	return 0;
1299 }
1300 
1301 static int
1302 emuxki_ac97_write(void *hdl, uint8_t reg, uint16_t val)
1303 {
1304 	struct emuxki_softc *sc = hdl;
1305 
1306 	mutex_spin_enter(&sc->sc_index_lock);
1307 	emuxki_writeio_1(sc, EMU_AC97ADDR, reg);
1308 	emuxki_writeio_2(sc, EMU_AC97DATA, val);
1309 	mutex_spin_exit(&sc->sc_index_lock);
1310 
1311 	return 0;
1312 }
1313 
1314 static int
1315 emuxki_ac97_reset(void *hdl)
1316 {
1317 
1318 	return 0;
1319 }
1320 
1321 static enum ac97_host_flags
1322 emuxki_ac97_flags(void *hdl)
1323 {
1324 
1325 	return AC97_HOST_SWAPPED_CHANNELS;
1326 }
1327 
1328 MODULE(MODULE_CLASS_DRIVER, emuxki, "pci,audio");
1329 
1330 #ifdef _MODULE
1331 #include "ioconf.c"
1332 #endif
1333 
1334 static int
1335 emuxki_modcmd(modcmd_t cmd, void *opaque)
1336 {
1337 	int error = 0;
1338 
1339 	switch (cmd) {
1340 	case MODULE_CMD_INIT:
1341 #ifdef _MODULE
1342 		error = config_init_component(cfdriver_ioconf_emuxki,
1343 		    cfattach_ioconf_emuxki, cfdata_ioconf_emuxki);
1344 #endif
1345 		return error;
1346 	case MODULE_CMD_FINI:
1347 #ifdef _MODULE
1348 		error = config_fini_component(cfdriver_ioconf_emuxki,
1349 		    cfattach_ioconf_emuxki, cfdata_ioconf_emuxki);
1350 #endif
1351 		return error;
1352 	default:
1353 		return ENOTTY;
1354 	}
1355 }
1356