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