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