xref: /netbsd/sys/dev/pci/fms.c (revision beecddb6)
1 /*	$NetBSD: fms.c,v 1.49 2021/08/07 16:19:14 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Witold J. Wnuk.
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  * Forte Media FM801 Audio Device Driver
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: fms.c,v 1.49 2021/08/07 16:19:14 thorpej Exp $");
38 
39 #include "mpu.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/kmem.h>
45 #include <sys/device.h>
46 #include <sys/audioio.h>
47 
48 #include <sys/bus.h>
49 #include <sys/cpu.h>
50 
51 #include <dev/pci/pcidevs.h>
52 #include <dev/pci/pcivar.h>
53 
54 #include <dev/audio/audio_if.h>
55 
56 #include <dev/ic/ac97var.h>
57 #include <dev/ic/mpuvar.h>
58 
59 #include <dev/pci/fmsvar.h>
60 
61 
62 struct fms_dma {
63 	struct fms_dma *next;
64 	void *addr;
65 	size_t size;
66 	bus_dmamap_t map;
67 	bus_dma_segment_t seg;
68 };
69 
70 
71 
72 static int	fms_match(device_t, cfdata_t, void *);
73 static void	fms_attach(device_t, device_t, void *);
74 static int	fms_intr(void *);
75 
76 static int	fms_query_format(void *, audio_format_query_t *);
77 static int	fms_set_format(void *, int,
78 			       const audio_params_t *, const audio_params_t *,
79 			       audio_filter_reg_t *, audio_filter_reg_t *);
80 static int	fms_round_blocksize(void *, int, int, const audio_params_t *);
81 static int	fms_halt_output(void *);
82 static int	fms_halt_input(void *);
83 static int	fms_getdev(void *, struct audio_device *);
84 static int	fms_set_port(void *, mixer_ctrl_t *);
85 static int	fms_get_port(void *, mixer_ctrl_t *);
86 static int	fms_query_devinfo(void *, mixer_devinfo_t *);
87 static void	*fms_malloc(void *, int, size_t);
88 static void	fms_free(void *, void *, size_t);
89 static int	fms_get_props(void *);
90 static int	fms_trigger_output(void *, void *, void *, int,
91 				   void (*)(void *), void *,
92 				   const audio_params_t *);
93 static int	fms_trigger_input(void *, void *, void *, int,
94 				  void (*)(void *), void *,
95 				  const audio_params_t *);
96 static void	fms_get_locks(void *, kmutex_t **, kmutex_t **);
97 
98 CFATTACH_DECL_NEW(fms, sizeof (struct fms_softc),
99     fms_match, fms_attach, NULL, NULL);
100 
101 static struct audio_device fms_device = {
102 	"Forte Media 801",
103 	"1.0",
104 	"fms"
105 };
106 
107 /*
108  * The frequency list in this format is also referred from fms_rate2index().
109  * So don't rearrange or delete entries.
110  */
111 static const struct audio_format fms_formats[] = {
112 	{
113 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
114 		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
115 		.validbits	= 16,
116 		.precision	= 16,
117 		.channels	= 2,
118 		.channel_mask	= AUFMT_STEREO,
119 		.frequency_type	= 11,
120 		.frequency	= { 5500, 8000, 9600, 11025, 16000, 19200,
121 		                    22050, 32000, 38400, 44100, 48000},
122 	},
123 };
124 #define FMS_NFORMATS	__arraycount(fms_formats)
125 
126 
127 static const struct audio_hw_if fms_hw_if = {
128 	.query_format		= fms_query_format,
129 	.set_format		= fms_set_format,
130 	.round_blocksize	= fms_round_blocksize,
131 	.halt_output		= fms_halt_output,
132 	.halt_input		= fms_halt_input,
133 	.getdev			= fms_getdev,
134 	.set_port		= fms_set_port,
135 	.get_port		= fms_get_port,
136 	.query_devinfo		= fms_query_devinfo,
137 	.allocm			= fms_malloc,
138 	.freem			= fms_free,
139 	.get_props		= fms_get_props,
140 	.trigger_output		= fms_trigger_output,
141 	.trigger_input		= fms_trigger_input,
142 	.get_locks		= fms_get_locks,
143 };
144 
145 static int	fms_attach_codec(void *, struct ac97_codec_if *);
146 static int	fms_read_codec(void *, uint8_t, uint16_t *);
147 static int	fms_write_codec(void *, uint8_t, uint16_t);
148 static int	fms_reset_codec(void *);
149 static int	fms_rate2index(u_int);
150 
151 #define FM_PCM_VOLUME		0x00
152 #define FM_FM_VOLUME		0x02
153 #define FM_I2S_VOLUME		0x04
154 #define FM_RECORD_SOURCE	0x06
155 
156 #define FM_PLAY_CTL		0x08
157 #define  FM_PLAY_RATE_MASK		0x0f00
158 #define  FM_PLAY_BUF1_LAST		0x0001
159 #define  FM_PLAY_BUF2_LAST		0x0002
160 #define  FM_PLAY_START			0x0020
161 #define  FM_PLAY_PAUSE			0x0040
162 #define  FM_PLAY_STOPNOW		0x0080
163 #define  FM_PLAY_16BIT			0x4000
164 #define  FM_PLAY_STEREO			0x8000
165 
166 #define FM_PLAY_DMALEN		0x0a
167 #define FM_PLAY_DMABUF1		0x0c
168 #define FM_PLAY_DMABUF2		0x10
169 
170 
171 #define FM_REC_CTL		0x14
172 #define  FM_REC_RATE_MASK		0x0f00
173 #define  FM_REC_BUF1_LAST		0x0001
174 #define  FM_REC_BUF2_LAST		0x0002
175 #define  FM_REC_START			0x0020
176 #define  FM_REC_PAUSE			0x0040
177 #define  FM_REC_STOPNOW			0x0080
178 #define  FM_REC_16BIT			0x4000
179 #define  FM_REC_STEREO			0x8000
180 
181 
182 #define FM_REC_DMALEN		0x16
183 #define FM_REC_DMABUF1		0x18
184 #define FM_REC_DMABUF2		0x1c
185 
186 #define FM_CODEC_CTL		0x22
187 #define FM_VOLUME		0x26
188 #define  FM_VOLUME_MUTE			0x8000
189 
190 #define FM_CODEC_CMD		0x2a
191 #define  FM_CODEC_CMD_READ		0x0080
192 #define  FM_CODEC_CMD_VALID		0x0100
193 #define  FM_CODEC_CMD_BUSY		0x0200
194 
195 #define FM_CODEC_DATA		0x2c
196 
197 #define FM_IO_CTL		0x52
198 #define FM_CARD_CTL		0x54
199 
200 #define FM_INTMASK		0x56
201 #define  FM_INTMASK_PLAY		0x0001
202 #define  FM_INTMASK_REC			0x0002
203 #define  FM_INTMASK_VOL			0x0040
204 #define  FM_INTMASK_MPU			0x0080
205 
206 #define FM_INTSTATUS		0x5a
207 #define  FM_INTSTATUS_PLAY		0x0100
208 #define  FM_INTSTATUS_REC		0x0200
209 #define  FM_INTSTATUS_VOL		0x4000
210 #define  FM_INTSTATUS_MPU		0x8000
211 
212 
213 static int
fms_match(device_t parent,cfdata_t match,void * aux)214 fms_match(device_t parent, cfdata_t match, void *aux)
215 {
216 	struct pci_attach_args *pa;
217 
218 	pa = (struct pci_attach_args *)aux;
219 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_FORTEMEDIA)
220 		return 0;
221 	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_FORTEMEDIA_FM801)
222 		return 0;
223 
224 	return 1;
225 }
226 
227 static void
fms_attach(device_t parent,device_t self,void * aux)228 fms_attach(device_t parent, device_t self, void *aux)
229 {
230 	struct pci_attach_args *pa;
231 	struct fms_softc *sc;
232 	struct audio_attach_args aa;
233 	const char *intrstr;
234 	pci_chipset_tag_t pc;
235 	pcitag_t pt;
236 	pci_intr_handle_t ih;
237 	uint16_t k1;
238 	char intrbuf[PCI_INTRSTR_LEN];
239 
240 	pa = aux;
241 	sc = device_private(self);
242 	sc->sc_dev = self;
243 	intrstr = NULL;
244 	pc = pa->pa_pc;
245 	pt = pa->pa_tag;
246 	aprint_naive(": Audio controller\n");
247 	aprint_normal(": Forte Media FM-801\n");
248 
249 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
250 			   &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
251 		aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
252 		return;
253 	}
254 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2,
255 				&sc->sc_mpu_ioh))
256 		panic("fms_attach: can't get mpu subregion handle");
257 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4,
258 				&sc->sc_opl_ioh))
259 		panic("fms_attach: can't get opl subregion handle");
260 
261 	if (pci_intr_map(pa, &ih)) {
262 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
263 		return;
264 	}
265 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
266 
267 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
268 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
269 
270 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, fms_intr, sc,
271 	    device_xname(self));
272 	if (sc->sc_ih == NULL) {
273 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
274 		if (intrstr != NULL)
275 			aprint_error(" at %s", intrstr);
276 		aprint_error("\n");
277 		mutex_destroy(&sc->sc_lock);
278 		mutex_destroy(&sc->sc_intr_lock);
279 		return;
280 	}
281 
282 	sc->sc_dmat = pa->pa_dmat;
283 
284 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
285 
286 	/* Disable legacy audio (SBPro compatibility) */
287 	pci_conf_write(pc, pt, 0x40, 0);
288 
289 	/* Reset codec and AC'97 */
290 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
291 	delay(2);		/* > 1us according to AC'97 documentation */
292 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
293 	delay(1);		/* > 168.2ns according to AC'97 documentation */
294 
295 	/* Set up volume */
296 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808);
297 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808);
298 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808);
299 
300 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000);
301 
302 	/* Unmask playback, record and mpu interrupts, mask the rest */
303 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK);
304 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK,
305 	    (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
306 	     FM_INTMASK_VOL);
307 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
308 	    FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
309 	    FM_INTSTATUS_VOL);
310 
311 	sc->host_if.arg = sc;
312 	sc->host_if.attach = fms_attach_codec;
313 	sc->host_if.read = fms_read_codec;
314 	sc->host_if.write = fms_write_codec;
315 	sc->host_if.reset = fms_reset_codec;
316 
317 	if (ac97_attach(&sc->host_if, self, &sc->sc_lock) != 0) {
318 		mutex_destroy(&sc->sc_intr_lock);
319 		mutex_destroy(&sc->sc_lock);
320 		return;
321 	}
322 
323 	audio_attach_mi(&fms_hw_if, sc, sc->sc_dev);
324 
325 	aa.type = AUDIODEV_TYPE_OPL;
326 	aa.hwif = NULL;
327 	aa.hdl = NULL;
328 	config_found(sc->sc_dev, &aa, audioprint, CFARGS_NONE);
329 
330 	aa.type = AUDIODEV_TYPE_MPU;
331 	aa.hwif = NULL;
332 	aa.hdl = NULL;
333 	sc->sc_mpu_dev = config_found(sc->sc_dev, &aa, audioprint, CFARGS_NONE);
334 }
335 
336 /*
337  * Each AC-link frame takes 20.8us, data should be ready in next frame,
338  * we allow more than two.
339  */
340 #define TIMO 50
341 static int
fms_read_codec(void * addr,uint8_t reg,uint16_t * val)342 fms_read_codec(void *addr, uint8_t reg, uint16_t *val)
343 {
344 	struct fms_softc *sc;
345 	int i;
346 
347 	sc = addr;
348 	/* Poll until codec is ready */
349 	for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
350 		 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
351 		delay(1);
352 	if (i >= TIMO) {
353 		printf("fms: codec busy\n");
354 		return 1;
355 	}
356 
357 	/* Write register index, read access */
358 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD,
359 			  reg | FM_CODEC_CMD_READ);
360 
361 	/* Poll until we have valid data */
362 	for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh,
363 		 FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++)
364 		delay(1);
365 	if (i >= TIMO) {
366 		printf("fms: no data from codec\n");
367 		return 1;
368 	}
369 
370 	/* Read data */
371 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA);
372 	return 0;
373 }
374 
375 static int
fms_write_codec(void * addr,uint8_t reg,uint16_t val)376 fms_write_codec(void *addr, uint8_t reg, uint16_t val)
377 {
378 	struct fms_softc *sc = addr;
379 	int i;
380 
381 	/* Poll until codec is ready */
382 	for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
383 		 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
384 		delay(1);
385 	if (i >= TIMO) {
386 		printf("fms: codec busy\n");
387 		return 1;
388 	}
389 
390 	/* Write data */
391 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val);
392 	/* Write index register, write access */
393 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg);
394 	return 0;
395 }
396 #undef TIMO
397 
398 static int
fms_attach_codec(void * addr,struct ac97_codec_if * cif)399 fms_attach_codec(void *addr, struct ac97_codec_if *cif)
400 {
401 	struct fms_softc *sc;
402 
403 	sc = addr;
404 	sc->codec_if = cif;
405 	return 0;
406 }
407 
408 /* Cold Reset */
409 static int
fms_reset_codec(void * addr)410 fms_reset_codec(void *addr)
411 {
412 	struct fms_softc *sc;
413 
414 	sc = addr;
415 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
416 	delay(2);
417 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
418 	delay(1);
419 	return 0;
420 }
421 
422 static int
fms_intr(void * arg)423 fms_intr(void *arg)
424 {
425 	struct fms_softc *sc = arg;
426 #if NMPU > 0
427 	struct mpu_softc *sc_mpu = device_private(sc->sc_mpu_dev);
428 #endif
429 	uint16_t istat;
430 
431 	mutex_spin_enter(&sc->sc_intr_lock);
432 
433 	istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS);
434 
435 	if (istat & FM_INTSTATUS_PLAY) {
436 		if ((sc->sc_play_nextblk += sc->sc_play_blksize) >=
437 		     sc->sc_play_end)
438 			sc->sc_play_nextblk = sc->sc_play_start;
439 
440 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
441 		    sc->sc_play_flip++ & 1 ?
442 		    FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk);
443 
444 		if (sc->sc_pintr)
445 			sc->sc_pintr(sc->sc_parg);
446 		else
447 			printf("unexpected play intr\n");
448 	}
449 
450 	if (istat & FM_INTSTATUS_REC) {
451 		if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >=
452 		     sc->sc_rec_end)
453 			sc->sc_rec_nextblk = sc->sc_rec_start;
454 
455 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
456 		    sc->sc_rec_flip++ & 1 ?
457 		    FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk);
458 
459 		if (sc->sc_rintr)
460 			sc->sc_rintr(sc->sc_rarg);
461 		else
462 			printf("unexpected rec intr\n");
463 	}
464 
465 #if NMPU > 0
466 	if (istat & FM_INTSTATUS_MPU)
467 		mpu_intr(sc_mpu);
468 #endif
469 
470 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
471 			  istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC));
472 
473 	mutex_spin_exit(&sc->sc_intr_lock);
474 
475 	return 1;
476 }
477 
478 static int
fms_query_format(void * addr,audio_format_query_t * afp)479 fms_query_format(void *addr, audio_format_query_t *afp)
480 {
481 
482 	return audio_query_format(fms_formats, FMS_NFORMATS, afp);
483 }
484 
485 /* Return index number of sample_rate */
486 static int
fms_rate2index(u_int sample_rate)487 fms_rate2index(u_int sample_rate)
488 {
489 	int i;
490 
491 	for (i = 0; i < fms_formats[0].frequency_type; i++) {
492 		if (sample_rate == fms_formats[0].frequency[i])
493 			return i;
494 	}
495 
496 	/* NOTREACHED */
497 	panic("fms_format.frequency mismatch?\n");
498 }
499 
500 static int
fms_set_format(void * addr,int setmode,const audio_params_t * play,const audio_params_t * rec,audio_filter_reg_t * pfil,audio_filter_reg_t * rfil)501 fms_set_format(void *addr, int setmode,
502     const audio_params_t *play, const audio_params_t *rec,
503     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
504 {
505 	struct fms_softc *sc;
506 
507 	sc = addr;
508 	if (setmode & AUMODE_PLAY) {
509 		sc->sc_play_reg = fms_rate2index(play->sample_rate) << 8;
510 		sc->sc_play_reg |= FM_PLAY_STEREO;
511 		sc->sc_play_reg |= FM_PLAY_16BIT;
512 	}
513 
514 	if (setmode & AUMODE_RECORD) {
515 		sc->sc_rec_reg = fms_rate2index(rec->sample_rate) << 8;
516 		sc->sc_rec_reg |= FM_REC_STEREO;
517 		sc->sc_rec_reg |= FM_REC_16BIT;
518 	}
519 
520 	return 0;
521 }
522 
523 static int
fms_round_blocksize(void * addr,int blk,int mode,const audio_params_t * param)524 fms_round_blocksize(void *addr, int blk, int mode,
525     const audio_params_t *param)
526 {
527 
528 	return blk & ~0xf;
529 }
530 
531 static int
fms_halt_output(void * addr)532 fms_halt_output(void *addr)
533 {
534 	struct fms_softc *sc;
535 	uint16_t k1;
536 
537 	sc = addr;
538 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL);
539 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
540 			  (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
541 			  FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST);
542 
543 	return 0;
544 }
545 
546 static int
fms_halt_input(void * addr)547 fms_halt_input(void *addr)
548 {
549 	struct fms_softc *sc;
550 	uint16_t k1;
551 
552 	sc = addr;
553 	k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL);
554 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
555 			  (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
556 			  FM_REC_BUF1_LAST | FM_REC_BUF2_LAST);
557 
558 	return 0;
559 }
560 
561 static int
fms_getdev(void * addr,struct audio_device * retp)562 fms_getdev(void *addr, struct audio_device *retp)
563 {
564 
565 	*retp = fms_device;
566 	return 0;
567 }
568 
569 static int
fms_set_port(void * addr,mixer_ctrl_t * cp)570 fms_set_port(void *addr, mixer_ctrl_t *cp)
571 {
572 	struct fms_softc *sc;
573 
574 	sc = addr;
575 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
576 }
577 
578 static int
fms_get_port(void * addr,mixer_ctrl_t * cp)579 fms_get_port(void *addr, mixer_ctrl_t *cp)
580 {
581 	struct fms_softc *sc;
582 
583 	sc = addr;
584 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
585 }
586 
587 static void *
fms_malloc(void * addr,int direction,size_t size)588 fms_malloc(void *addr, int direction, size_t size)
589 {
590 	struct fms_softc *sc;
591 	struct fms_dma *p;
592 	int error;
593 	int rseg;
594 
595 	sc = addr;
596 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
597 	p->size = size;
598 
599 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
600 				      1, &rseg, BUS_DMA_WAITOK)) != 0) {
601 		aprint_error_dev(sc->sc_dev, "unable to allocate DMA, error = %d\n", error);
602 		goto fail_alloc;
603 	}
604 
605 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
606 				    BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
607 		aprint_error_dev(sc->sc_dev, "unable to map DMA, error = %d\n",
608 		       error);
609 		goto fail_map;
610 	}
611 
612 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
613 				       BUS_DMA_WAITOK, &p->map)) != 0) {
614 		aprint_error_dev(sc->sc_dev, "unable to create DMA map, error = %d\n",
615 		       error);
616 		goto fail_create;
617 	}
618 
619 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
620 				     BUS_DMA_WAITOK)) != 0) {
621 		aprint_error_dev(sc->sc_dev, "unable to load DMA map, error = %d\n",
622 		       error);
623 		goto fail_load;
624 	}
625 
626 	p->next = sc->sc_dmas;
627 	sc->sc_dmas = p;
628 
629 	return p->addr;
630 
631 
632 fail_load:
633 	bus_dmamap_destroy(sc->sc_dmat, p->map);
634 fail_create:
635 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
636 fail_map:
637 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
638 fail_alloc:
639 	kmem_free(p, sizeof(*p));
640 	return NULL;
641 }
642 
643 static void
fms_free(void * addr,void * ptr,size_t size)644 fms_free(void *addr, void *ptr, size_t size)
645 {
646 	struct fms_softc *sc;
647 	struct fms_dma **pp, *p;
648 
649 	sc = addr;
650 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
651 		if (p->addr == ptr) {
652 			bus_dmamap_unload(sc->sc_dmat, p->map);
653 			bus_dmamap_destroy(sc->sc_dmat, p->map);
654 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
655 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
656 
657 			*pp = p->next;
658 			kmem_free(p, sizeof(*p));
659 			return;
660 		}
661 
662 	panic("fms_free: trying to free unallocated memory");
663 }
664 
665 static int
fms_get_props(void * addr)666 fms_get_props(void *addr)
667 {
668 
669 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
670 	    AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
671 }
672 
673 static int
fms_query_devinfo(void * addr,mixer_devinfo_t * dip)674 fms_query_devinfo(void *addr, mixer_devinfo_t *dip)
675 {
676 	struct fms_softc *sc;
677 
678 	sc = addr;
679 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
680 }
681 
682 static int
fms_trigger_output(void * addr,void * start,void * end,int blksize,void (* intr)(void *),void * arg,const audio_params_t * param)683 fms_trigger_output(void *addr, void *start, void *end, int blksize,
684     void (*intr)(void *), void *arg, const audio_params_t *param)
685 {
686 	struct fms_softc *sc;
687 	struct fms_dma *p;
688 
689 	sc = addr;
690 	sc->sc_pintr = intr;
691 	sc->sc_parg = arg;
692 
693 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
694 		continue;
695 
696 	if (p == NULL)
697 		panic("fms_trigger_output: request with bad start "
698 		      "address (%p)", start);
699 
700 	sc->sc_play_start = p->map->dm_segs[0].ds_addr;
701 	sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start);
702 	sc->sc_play_blksize = blksize;
703 	sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize;
704 	sc->sc_play_flip = 0;
705 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1);
706 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1,
707 			  sc->sc_play_start);
708 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2,
709 			  sc->sc_play_nextblk);
710 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
711 			  FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg);
712 	return 0;
713 }
714 
715 
716 static int
fms_trigger_input(void * addr,void * start,void * end,int blksize,void (* intr)(void *),void * arg,const audio_params_t * param)717 fms_trigger_input(void *addr, void *start, void *end, int blksize,
718     void (*intr)(void *), void *arg, const audio_params_t *param)
719 {
720 	struct fms_softc *sc;
721 	struct fms_dma *p;
722 
723 	sc = addr;
724 	sc->sc_rintr = intr;
725 	sc->sc_rarg = arg;
726 
727 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
728 		continue;
729 
730 	if (p == NULL)
731 		panic("fms_trigger_input: request with bad start "
732 		      "address (%p)", start);
733 
734 	sc->sc_rec_start = p->map->dm_segs[0].ds_addr;
735 	sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start);
736 	sc->sc_rec_blksize = blksize;
737 	sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize;
738 	sc->sc_rec_flip = 0;
739 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1);
740 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1,
741 			  sc->sc_rec_start);
742 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2,
743 			  sc->sc_rec_nextblk);
744 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
745 			  FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg);
746 	return 0;
747 }
748 
749 static void
fms_get_locks(void * addr,kmutex_t ** intr,kmutex_t ** thread)750 fms_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
751 {
752 	struct fms_softc *sc;
753 
754 	sc = addr;
755 	*intr = &sc->sc_intr_lock;
756 	*thread = &sc->sc_lock;
757 }
758