xref: /openbsd-src/sys/dev/pci/fms.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: fms.c,v 1.3 2001/06/12 15:40:30 niklas Exp $ */
2 /*	$NetBSD: fms.c,v 1.5.4.1 2000/06/30 16:27:50 simonb Exp $	*/
3 
4 /*-
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Witold J. Wnuk.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Forte Media FM801 Audio Device Driver
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/device.h>
49 #include <sys/audioio.h>
50 
51 #include <machine/bus.h>
52 #include <machine/cpu.h>
53 
54 #include <dev/pci/pcidevs.h>
55 #include <dev/pci/pcivar.h>
56 
57 #include <dev/audio_if.h>
58 #include <dev/mulaw.h>
59 #include <dev/auconv.h>
60 
61 #include <dev/ic/ac97.h>
62 #if 0
63 #include <dev/ic/mpuvar.h>
64 #endif
65 
66 #include <dev/pci/fmsvar.h>
67 
68 
69 struct fms_dma {
70 	struct fms_dma *next;
71 	caddr_t addr;
72 	size_t size;
73 	bus_dmamap_t map;
74 	bus_dma_segment_t seg;
75 };
76 
77 
78 
79 int	fms_match __P((struct device *, void *, void *));
80 void	fms_attach __P((struct device *, struct device *, void *));
81 int	fms_intr __P((void *));
82 
83 int	fms_open __P((void *, int));
84 void	fms_close __P((void *));
85 int	fms_query_encoding __P((void *, struct audio_encoding *));
86 int	fms_set_params __P((void *, int, int, struct audio_params *,
87 			    struct audio_params *));
88 int	fms_round_blocksize __P((void *, int));
89 int	fms_halt_output __P((void *));
90 int	fms_halt_input __P((void *));
91 int	fms_getdev __P((void *, struct audio_device *));
92 int	fms_set_port __P((void *, mixer_ctrl_t *));
93 int	fms_get_port __P((void *, mixer_ctrl_t *));
94 int	fms_query_devinfo __P((void *, mixer_devinfo_t *));
95 void	*fms_malloc __P((void *, int, size_t, int, int));
96 void	fms_free __P((void *, void *, int));
97 size_t	fms_round_buffersize __P((void *, int, size_t));
98 int	fms_mappage __P((void *, void *, int, int));
99 int	fms_get_props __P((void *));
100 int	fms_trigger_output __P((void *, void *, void *, int, void (*)(void *),
101 				void *, struct audio_params *));
102 int	fms_trigger_input __P((void *, void *, void *, int, void (*)(void *),
103 			       void *, struct audio_params *));
104 
105 struct  cfdriver fms_cd = {
106 	NULL, "fms", DV_DULL
107 };
108 
109 struct cfattach fms_ca = {
110 	sizeof (struct fms_softc), fms_match, fms_attach
111 };
112 
113 struct audio_device fms_device = {
114 	"Forte Media 801",
115 	"1.0",
116 	"fms"
117 };
118 
119 
120 struct audio_hw_if fms_hw_if = {
121 	fms_open,
122 	fms_close,
123 	NULL,
124 	fms_query_encoding,
125 	fms_set_params,
126 	fms_round_blocksize,
127 	NULL,
128 	NULL,
129 	NULL,
130 	NULL,
131 	NULL,
132 	fms_halt_output,
133 	fms_halt_input,
134 	NULL,
135 	fms_getdev,
136 	NULL,
137 	fms_set_port,
138 	fms_get_port,
139 	fms_query_devinfo,
140 	NULL,
141 	fms_free,
142 	NULL,
143 	fms_mappage,
144 	fms_get_props,
145 	fms_trigger_output,
146 	fms_trigger_input,
147 	fms_malloc,
148 	fms_round_buffersize,
149 };
150 
151 int	fms_attach_codec __P((void *, struct ac97_codec_if *));
152 int	fms_read_codec __P((void *, u_int8_t, u_int16_t *));
153 int	fms_write_codec __P((void *, u_int8_t, u_int16_t));
154 void	fms_reset_codec __P((void *));
155 
156 int	fms_allocmem __P((struct fms_softc *, size_t, size_t,
157 			  struct fms_dma *));
158 int	fms_freemem __P((struct fms_softc *, struct fms_dma *));
159 
160 #define FM_PCM_VOLUME		0x00
161 #define FM_FM_VOLUME		0x02
162 #define FM_I2S_VOLUME		0x04
163 #define FM_RECORD_SOURCE	0x06
164 
165 #define FM_PLAY_CTL		0x08
166 #define  FM_PLAY_RATE_MASK		0x0f00
167 #define  FM_PLAY_BUF1_LAST		0x0001
168 #define  FM_PLAY_BUF2_LAST		0x0002
169 #define  FM_PLAY_START			0x0020
170 #define  FM_PLAY_PAUSE			0x0040
171 #define  FM_PLAY_STOPNOW		0x0080
172 #define  FM_PLAY_16BIT			0x4000
173 #define  FM_PLAY_STEREO			0x8000
174 
175 #define FM_PLAY_DMALEN		0x0a
176 #define FM_PLAY_DMABUF1		0x0c
177 #define FM_PLAY_DMABUF2		0x10
178 
179 
180 #define FM_REC_CTL		0x14
181 #define  FM_REC_RATE_MASK		0x0f00
182 #define  FM_REC_BUF1_LAST		0x0001
183 #define  FM_REC_BUF2_LAST		0x0002
184 #define  FM_REC_START			0x0020
185 #define  FM_REC_PAUSE			0x0040
186 #define  FM_REC_STOPNOW			0x0080
187 #define  FM_REC_16BIT			0x4000
188 #define  FM_REC_STEREO			0x8000
189 
190 
191 #define FM_REC_DMALEN		0x16
192 #define FM_REC_DMABUF1		0x18
193 #define FM_REC_DMABUF2		0x1c
194 
195 #define FM_CODEC_CTL		0x22
196 #define FM_VOLUME		0x26
197 #define  FM_VOLUME_MUTE			0x8000
198 
199 #define FM_CODEC_CMD		0x2a
200 #define  FM_CODEC_CMD_READ		0x0080
201 #define  FM_CODEC_CMD_VALID		0x0100
202 #define  FM_CODEC_CMD_BUSY		0x0200
203 
204 #define FM_CODEC_DATA		0x2c
205 
206 #define FM_IO_CTL		0x52
207 #define FM_CARD_CTL		0x54
208 
209 #define FM_INTMASK		0x56
210 #define  FM_INTMASK_PLAY		0x0001
211 #define  FM_INTMASK_REC			0x0002
212 #define  FM_INTMASK_VOL			0x0040
213 #define  FM_INTMASK_MPU			0x0080
214 
215 #define FM_INTSTATUS		0x5a
216 #define  FM_INTSTATUS_PLAY		0x0100
217 #define  FM_INTSTATUS_REC		0x0200
218 #define  FM_INTSTATUS_VOL		0x4000
219 #define  FM_INTSTATUS_MPU		0x8000
220 
221 
222 
223 int
224 fms_match(parent, match, aux)
225 	struct device *parent;
226 	void *match;
227 	void *aux;
228 {
229 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
230 
231 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_FORTEMEDIA)
232 		return 0;
233 	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_FORTEMEDIA_FM801)
234 		return 0;
235 
236 	return 1;
237 }
238 
239 void
240 fms_attach(parent, self, aux)
241 	struct device *parent;
242 	struct device *self;
243 	void *aux;
244 {
245 	struct pci_attach_args *pa = aux;
246 	struct fms_softc *sc = (struct fms_softc *) self;
247 	struct audio_attach_args aa;
248 	const char *intrstr = NULL;
249 	pci_chipset_tag_t pc = pa->pa_pc;
250 	pcitag_t pt = pa->pa_tag;
251 	pci_intr_handle_t ih;
252 	int i;
253 
254 	u_int16_t k1;
255 
256 	printf(": Forte Media FM-801\n");
257 
258 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline,
259 			 &ih)) {
260 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
261 		return;
262 	}
263 	intrstr = pci_intr_string(pc, ih);
264 
265 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, fms_intr, sc,
266 	    sc->sc_dev.dv_xname);
267 	if (sc->sc_ih == NULL) {
268 		printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
269 		if (intrstr != NULL)
270 			printf(" at %s", intrstr);
271 		printf("\n");
272 		return;
273 	}
274 
275 	sc->sc_dmat = pa->pa_dmat;
276 
277 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
278 
279 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
280 			   &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize, 0)) {
281 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
282 		return;
283 	}
284 
285 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2,
286 				&sc->sc_mpu_ioh))
287 		panic("fms_attach: can't get mpu subregion handle");
288 
289 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4,
290 				&sc->sc_opl_ioh))
291 		panic("fms_attach: can't get opl subregion handle");
292 
293 	/* Disable legacy audio (SBPro compatibility) */
294 	pci_conf_write(pc, pt, 0x40, 0);
295 
296 	/* Reset codec and AC'97 */
297 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
298 	delay(2);		/* > 1us according to AC'97 documentation */
299 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
300 	delay(1);		/* > 168.2ns according to AC'97 documentation */
301 
302 	/* Set up volume */
303 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808);
304 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808);
305 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808);
306 
307 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000);
308 
309 	/* Unmask playback, record and mpu interrupts, mask the rest */
310 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK);
311 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK,
312 	    (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
313 	     FM_INTMASK_VOL);
314 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
315 	    FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
316 	    FM_INTSTATUS_VOL);
317 
318 	sc->host_if.arg = sc;
319 	sc->host_if.attach = fms_attach_codec;
320 	sc->host_if.read = fms_read_codec;
321 	sc->host_if.write = fms_write_codec;
322 	sc->host_if.reset = fms_reset_codec;
323 
324 	if (ac97_attach(&sc->host_if) != 0)
325 		return;
326 
327 	/* Turn mute off */
328 	for (i = 0; i < 3; i++) {
329 		static struct {
330 			char *class, *device;
331 		} d[] = {
332 			{ AudioCoutputs, AudioNmaster },
333 			{ AudioCinputs, AudioNdac },
334 			{ AudioCrecord, AudioNvolume }
335 		};
336 		struct mixer_ctrl ctl;
337 
338 		ctl.type = AUDIO_MIXER_ENUM;
339 		ctl.un.ord = 0;
340 		ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
341 			d[i].class, d[i].device, AudioNmute);
342 		fms_set_port(sc, &ctl);
343 	}
344 
345 	audio_attach_mi(&fms_hw_if, sc, &sc->sc_dev);
346 
347 	aa.type = AUDIODEV_TYPE_OPL;
348 	aa.hwif = NULL;
349 	aa.hdl = NULL;
350 	config_found(&sc->sc_dev, &aa, audioprint);
351 
352 	aa.type = AUDIODEV_TYPE_MPU;
353 	aa.hwif = NULL;
354 	aa.hdl = NULL;
355 	sc->sc_mpu_dev = config_found(&sc->sc_dev, &aa, audioprint);
356 }
357 
358 /*
359  * Each AC-link frame takes 20.8us, data should be ready in next frame,
360  * we allow more than two.
361  */
362 #define TIMO 50
363 int
364 fms_read_codec(addr, reg, val)
365 	void *addr;
366 	u_int8_t reg;
367 	u_int16_t *val;
368 {
369 	struct fms_softc *sc = addr;
370 	int i;
371 
372 	/* Poll until codec is ready */
373 	for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
374 		 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
375 		delay(1);
376 	if (i >= TIMO) {
377 		printf("fms: codec busy\n");
378 		return 1;
379 	}
380 
381 	/* Write register index, read access */
382 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD,
383 			  reg | FM_CODEC_CMD_READ);
384 
385 	/* Poll until we have valid data */
386 	for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh,
387 		 FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++)
388 		delay(1);
389 	if (i >= TIMO) {
390 		printf("fms: no data from codec\n");
391 		return 1;
392 	}
393 
394 	/* Read data */
395 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA);
396 	return 0;
397 }
398 
399 int
400 fms_write_codec(addr, reg, val)
401 	void *addr;
402 	u_int8_t reg;
403 	u_int16_t val;
404 {
405 	struct fms_softc *sc = addr;
406 	int i;
407 
408 	/* Poll until codec is ready */
409 	for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
410 		 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
411 		delay(1);
412 	if (i >= TIMO) {
413 		printf("fms: codec busy\n");
414 		return 1;
415 	}
416 
417 	/* Write data */
418 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val);
419 	/* Write index register, write access */
420 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg);
421 	return 0;
422 }
423 #undef TIMO
424 
425 int
426 fms_attach_codec(addr, cif)
427 	void *addr;
428 	struct ac97_codec_if *cif;
429 {
430 	struct fms_softc *sc = addr;
431 
432 	sc->codec_if = cif;
433 	return 0;
434 }
435 
436 /* Cold Reset */
437 void
438 fms_reset_codec(addr)
439 	void *addr;
440 {
441 	struct fms_softc *sc = addr;
442 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
443 	delay(2);
444 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
445 	delay(1);
446 }
447 
448 int
449 fms_intr(arg)
450 	void *arg;
451 {
452 	struct fms_softc *sc = arg;
453 	u_int16_t istat;
454 
455 	istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS);
456 
457 	if (istat & FM_INTSTATUS_PLAY) {
458 		if ((sc->sc_play_nextblk += sc->sc_play_blksize) >=
459 		     sc->sc_play_end)
460 			sc->sc_play_nextblk = sc->sc_play_start;
461 
462 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
463 		    sc->sc_play_flip++ & 1 ?
464 		    FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk);
465 
466 		if (sc->sc_pintr)
467 			sc->sc_pintr(sc->sc_parg);
468 		else
469 			printf("unexpected play intr\n");
470 	}
471 
472 	if (istat & FM_INTSTATUS_REC) {
473 		if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >=
474 		     sc->sc_rec_end)
475 			sc->sc_rec_nextblk = sc->sc_rec_start;
476 
477 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
478 		    sc->sc_rec_flip++ & 1 ?
479 		    FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk);
480 
481 		if (sc->sc_rintr)
482 			sc->sc_rintr(sc->sc_rarg);
483 		else
484 			printf("unexpected rec intr\n");
485 	}
486 
487 #if 0
488 	if (istat & FM_INTSTATUS_MPU)
489 		mpu_intr(sc->sc_mpu_dev);
490 #endif
491 
492 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
493 			  istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC));
494 
495 	return 1;
496 }
497 
498 int
499 fms_open(addr, flags)
500 	void *addr;
501 	int flags;
502 {
503 	/* UNUSED struct fms_softc *sc = addr;*/
504 
505 	return 0;
506 }
507 
508 void
509 fms_close(addr)
510 	void *addr;
511 {
512 	/* UNUSED struct fms_softc *sc = addr;*/
513 }
514 
515 int
516 fms_query_encoding(addr, fp)
517 	void *addr;
518 	struct audio_encoding *fp;
519 {
520 
521 	switch (fp->index) {
522 	case 0:
523 		strcpy(fp->name, AudioEmulaw);
524 		fp->encoding = AUDIO_ENCODING_ULAW;
525 		fp->precision = 8;
526 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
527 		return 0;
528 	case 1:
529 		strcpy(fp->name, AudioEslinear_le);
530 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
531 		fp->precision = 16;
532 		fp->flags = 0;
533 		return 0;
534 	case 2:
535 		strcpy(fp->name, AudioEulinear);
536 		fp->encoding = AUDIO_ENCODING_ULINEAR;
537 		fp->precision = 8;
538 		fp->flags = 0;
539 		return 0;
540 	case 3:
541 		strcpy(fp->name, AudioEalaw);
542 		fp->encoding = AUDIO_ENCODING_ALAW;
543 		fp->precision = 8;
544 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
545 		return 0;
546 	case 4:
547 		strcpy(fp->name, AudioEulinear_le);
548 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
549 		fp->precision = 16;
550 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
551 		return 0;
552 	case 5:
553 		strcpy(fp->name, AudioEslinear);
554 		fp->encoding = AUDIO_ENCODING_SLINEAR;
555 		fp->precision = 8;
556 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
557 		return 0;
558 	case 6:
559 		strcpy(fp->name, AudioEulinear_be);
560 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
561 		fp->precision = 16;
562 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
563 		return 0;
564 	case 7:
565 		strcpy(fp->name, AudioEslinear_be);
566 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
567 		fp->precision = 16;
568 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
569 		return 0;
570 	default:
571 		return EINVAL;
572 	}
573 }
574 
575 /*
576  * Range below -limit- is set to -rate-
577  * What a pity FM801 does not have 24000
578  * 24000 -> 22050 sounds rather poor
579  */
580 struct {
581 	int limit;
582 	int rate;
583 } fms_rates[11] = {
584 	{  6600,  5500 },
585 	{  8750,  8000 },
586 	{ 10250,  9600 },
587 	{ 13200, 11025 },
588 	{ 17500, 16000 },
589 	{ 20500, 19200 },
590 	{ 26500, 22050 },
591 	{ 35000, 32000 },
592 	{ 41000, 38400 },
593 	{ 46000, 44100 },
594 	{ 48000, 48000 },
595 	/* anything above -> 48000 */
596 };
597 
598 int
599 fms_set_params(addr, setmode, usemode, play, rec)
600 	void *addr;
601 	int setmode, usemode;
602 	struct audio_params *play, *rec;
603 {
604 	struct fms_softc *sc = addr;
605 	int i;
606 
607 	if (setmode & AUMODE_PLAY) {
608 		play->factor = 1;
609 		play->sw_code = 0;
610 		switch(play->encoding) {
611 		case AUDIO_ENCODING_ULAW:
612 			play->factor = 2;
613 			play->sw_code = mulaw_to_slinear16_le;
614 			break;
615 		case AUDIO_ENCODING_SLINEAR_LE:
616 			if (play->precision == 8)
617 				play->sw_code = change_sign8;
618 			break;
619 		case AUDIO_ENCODING_ULINEAR_LE:
620 			if (play->precision == 16)
621 				play->sw_code = change_sign16_le;
622 			break;
623 		case AUDIO_ENCODING_ALAW:
624 			play->factor = 2;
625 			play->sw_code = alaw_to_slinear16_le;
626 			break;
627 		case AUDIO_ENCODING_SLINEAR_BE:
628 			if (play->precision == 16)
629 				play->sw_code = swap_bytes;
630 			else
631 				play->sw_code = change_sign8;
632 			break;
633 		case AUDIO_ENCODING_ULINEAR_BE:
634 			if (play->precision == 16)
635 				play->sw_code = change_sign16_swap_bytes_le;
636 			break;
637 		default:
638 			return EINVAL;
639 		}
640 		for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit;
641 		     i++)
642 			;
643 		play->sample_rate = fms_rates[i].rate;
644 		sc->sc_play_reg = (play->channels == 2 ? FM_PLAY_STEREO : 0) |
645 		    (play->precision * play->factor == 16 ? FM_PLAY_16BIT : 0) |
646 		    (i << 8);
647 	}
648 
649 	if (setmode & AUMODE_RECORD) {
650 
651 		rec->factor = 1;
652 		rec->sw_code = 0;
653 		switch(rec->encoding) {
654 		case AUDIO_ENCODING_ULAW:
655 			rec->sw_code = ulinear8_to_mulaw;
656 			break;
657 		case AUDIO_ENCODING_SLINEAR_LE:
658 			if (rec->precision == 8)
659 				rec->sw_code = change_sign8;
660 			break;
661 		case AUDIO_ENCODING_ULINEAR_LE:
662 			if (rec->precision == 16)
663 				rec->sw_code = change_sign16_le;
664 			break;
665 		case AUDIO_ENCODING_ALAW:
666 			rec->sw_code = ulinear8_to_alaw;
667 			break;
668 		case AUDIO_ENCODING_SLINEAR_BE:
669 			if (play->precision == 16)
670 				play->sw_code = swap_bytes;
671 			else
672 				play->sw_code = change_sign8;
673 			break;
674 		case AUDIO_ENCODING_ULINEAR_BE:
675 			if (play->precision == 16)
676 				play->sw_code = swap_bytes_change_sign16_le;
677 			break;
678 		default:
679 			return EINVAL;
680 		}
681 		for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit;
682 		     i++)
683 			;
684 		rec->sample_rate = fms_rates[i].rate;
685 		sc->sc_rec_reg =
686 		    (rec->channels == 2 ? FM_REC_STEREO : 0) |
687 		    (rec->precision * rec->factor == 16 ? FM_REC_16BIT : 0) |
688 		    (i << 8);
689 	}
690 
691 	return 0;
692 }
693 
694 int
695 fms_round_blocksize(addr, blk)
696 	void *addr;
697 	int blk;
698 {
699 	return blk & ~0xf;
700 }
701 
702 int
703 fms_halt_output(addr)
704 	void *addr;
705 {
706 	struct fms_softc *sc = addr;
707 	u_int16_t k1;
708 
709 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL);
710 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
711 			  (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
712 			  FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST);
713 
714 	return 0;
715 }
716 
717 int
718 fms_halt_input(addr)
719 	void *addr;
720 {
721 	struct fms_softc *sc = addr;
722 	u_int16_t k1;
723 
724 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL);
725 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
726 			  (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
727 			  FM_REC_BUF1_LAST | FM_REC_BUF2_LAST);
728 
729 	return 0;
730 }
731 
732 int
733 fms_getdev(addr, retp)
734 	void *addr;
735 	struct audio_device *retp;
736 {
737 	*retp = fms_device;
738 	return 0;
739 }
740 
741 int
742 fms_set_port(addr, cp)
743 	void *addr;
744 	mixer_ctrl_t *cp;
745 {
746 	struct fms_softc *sc = addr;
747 
748 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
749 }
750 
751 int
752 fms_get_port(addr, cp)
753 	void *addr;
754 	mixer_ctrl_t *cp;
755 {
756 	struct fms_softc *sc = addr;
757 
758 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
759 }
760 
761 void *
762 fms_malloc(addr, direction, size, pool, flags)
763 	void *addr;
764 	int direction;
765 	size_t size;
766 	int pool, flags;
767 {
768 	struct fms_softc *sc = addr;
769 	struct fms_dma *p;
770 	int error;
771 	int rseg;
772 
773 	p = malloc(sizeof(*p), pool, flags);
774 	if (!p)
775 		return 0;
776 
777 	p->size = size;
778 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &p->seg, 1,
779 				      &rseg, BUS_DMA_NOWAIT)) != 0) {
780 		printf("%s: unable to allocate dma, error = %d\n",
781 		       sc->sc_dev.dv_xname, error);
782 		goto fail_alloc;
783 	}
784 
785 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
786 				    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
787 		printf("%s: unable to map dma, error = %d\n",
788 		       sc->sc_dev.dv_xname, error);
789 		goto fail_map;
790 	}
791 
792 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
793 				       BUS_DMA_NOWAIT, &p->map)) != 0) {
794 		printf("%s: unable to create dma map, error = %d\n",
795 		       sc->sc_dev.dv_xname, error);
796 		goto fail_create;
797 	}
798 
799 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
800 				     BUS_DMA_NOWAIT)) != 0) {
801 		printf("%s: unable to load dma map, error = %d\n",
802 		       sc->sc_dev.dv_xname, error);
803 		goto fail_load;
804 	}
805 
806 	p->next = sc->sc_dmas;
807 	sc->sc_dmas = p;
808 
809 	return p->addr;
810 
811 
812 fail_load:
813 	bus_dmamap_destroy(sc->sc_dmat, p->map);
814 fail_create:
815 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
816 fail_map:
817 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
818 fail_alloc:
819 	free(p, pool);
820 	return 0;
821 }
822 
823 void
824 fms_free(addr, ptr, pool)
825 	void *addr;
826 	void *ptr;
827 	int pool;
828 {
829 	struct fms_softc *sc = addr;
830 	struct fms_dma **pp, *p;
831 
832 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
833 		if (p->addr == ptr) {
834 			bus_dmamap_unload(sc->sc_dmat, p->map);
835 			bus_dmamap_destroy(sc->sc_dmat, p->map);
836 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
837 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
838 
839 			*pp = p->next;
840 			free(p, pool);
841 			return;
842 		}
843 
844 	panic("fms_free: trying to free unallocated memory");
845 }
846 
847 size_t
848 fms_round_buffersize(addr, direction, size)
849 	void *addr;
850 	int direction;
851 	size_t size;
852 {
853 	return size;
854 }
855 
856 int
857 fms_mappage(addr, mem, off, prot)
858 	void *addr;
859 	void *mem;
860 	int off;
861 	int prot;
862 {
863 	struct fms_softc *sc = addr;
864 	struct fms_dma *p;
865 
866 	if (off < 0)
867 		return -1;
868 
869 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
870 		;
871 	if (!p)
872 		return -1;
873 
874 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
875 			       BUS_DMA_WAITOK);
876 }
877 
878 int
879 fms_get_props(addr)
880 	void *addr;
881 {
882 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
883 	       AUDIO_PROP_FULLDUPLEX;
884 }
885 
886 int
887 fms_query_devinfo(addr, dip)
888 	void *addr;
889 	mixer_devinfo_t *dip;
890 {
891 	struct fms_softc *sc = addr;
892 
893 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
894 }
895 
896 int
897 fms_trigger_output(addr, start, end, blksize, intr, arg, param)
898 	void *addr;
899 	void *start, *end;
900 	int blksize;
901 	void (*intr) __P((void *));
902 	void *arg;
903 	struct audio_params *param;
904 {
905 	struct fms_softc *sc = addr;
906 	struct fms_dma *p;
907 
908 	sc->sc_pintr = intr;
909 	sc->sc_parg = arg;
910 
911 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
912 		;
913 
914 	if (!p)
915 		panic("fms_trigger_output: request with bad start "
916 		      "address (%p)\n", start);
917 
918 	sc->sc_play_start = p->map->dm_segs[0].ds_addr;
919 	sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start);
920 	sc->sc_play_blksize = blksize;
921 	sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize;
922 	sc->sc_play_flip = 0;
923 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1);
924 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1,
925 			  sc->sc_play_start);
926 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2,
927 			  sc->sc_play_nextblk);
928 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
929 			  FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg);
930 	return 0;
931 }
932 
933 
934 int
935 fms_trigger_input(addr, start, end, blksize, intr, arg, param)
936 	void *addr;
937 	void *start, *end;
938 	int blksize;
939 	void (*intr) __P((void *));
940 	void *arg;
941 	struct audio_params *param;
942 {
943 	struct fms_softc *sc = addr;
944 	struct fms_dma *p;
945 
946 	sc->sc_rintr = intr;
947 	sc->sc_rarg = arg;
948 
949 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
950 		;
951 
952 	if (!p)
953 		panic("fms_trigger_input: request with bad start "
954 		      "address (%p)\n", start);
955 
956 	sc->sc_rec_start = p->map->dm_segs[0].ds_addr;
957 	sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start);
958 	sc->sc_rec_blksize = blksize;
959 	sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize;
960 	sc->sc_rec_flip = 0;
961 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1);
962 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1,
963 			  sc->sc_rec_start);
964 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2,
965 			  sc->sc_rec_nextblk);
966 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
967 			  FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg);
968 	return 0;
969 }
970 
971 
972