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