fms.c revision 1.41 1 /* $NetBSD: fms.c,v 1.41 2012/10/27 17:18:32 chs 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.41 2012/10/27 17:18:32 chs 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
236 pa = aux;
237 sc = device_private(self);
238 sc->sc_dev = self;
239 intrstr = NULL;
240 pc = pa->pa_pc;
241 pt = pa->pa_tag;
242 aprint_naive(": Audio controller\n");
243 aprint_normal(": Forte Media FM-801\n");
244
245 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
246 &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
247 aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
248 return;
249 }
250 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2,
251 &sc->sc_mpu_ioh))
252 panic("fms_attach: can't get mpu subregion handle");
253 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4,
254 &sc->sc_opl_ioh))
255 panic("fms_attach: can't get opl subregion handle");
256
257 if (pci_intr_map(pa, &ih)) {
258 aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
259 return;
260 }
261 intrstr = pci_intr_string(pc, ih);
262
263 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
264 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
265
266 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, fms_intr, sc);
267 if (sc->sc_ih == NULL) {
268 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
269 if (intrstr != NULL)
270 aprint_error(" at %s", intrstr);
271 aprint_error("\n");
272 mutex_destroy(&sc->sc_lock);
273 mutex_destroy(&sc->sc_intr_lock);
274 return;
275 }
276
277 sc->sc_dmat = pa->pa_dmat;
278
279 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
280
281 /* Disable legacy audio (SBPro compatibility) */
282 pci_conf_write(pc, pt, 0x40, 0);
283
284 /* Reset codec and AC'97 */
285 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
286 delay(2); /* > 1us according to AC'97 documentation */
287 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
288 delay(1); /* > 168.2ns according to AC'97 documentation */
289
290 /* Set up volume */
291 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808);
292 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808);
293 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808);
294
295 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000);
296
297 /* Unmask playback, record and mpu interrupts, mask the rest */
298 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK);
299 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK,
300 (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
301 FM_INTMASK_VOL);
302 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
303 FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
304 FM_INTSTATUS_VOL);
305
306 sc->host_if.arg = sc;
307 sc->host_if.attach = fms_attach_codec;
308 sc->host_if.read = fms_read_codec;
309 sc->host_if.write = fms_write_codec;
310 sc->host_if.reset = fms_reset_codec;
311
312 if (ac97_attach(&sc->host_if, self, &sc->sc_lock) != 0) {
313 mutex_destroy(&sc->sc_intr_lock);
314 mutex_destroy(&sc->sc_lock);
315 return;
316 }
317
318 audio_attach_mi(&fms_hw_if, sc, sc->sc_dev);
319
320 aa.type = AUDIODEV_TYPE_OPL;
321 aa.hwif = NULL;
322 aa.hdl = NULL;
323 config_found(sc->sc_dev, &aa, audioprint);
324
325 aa.type = AUDIODEV_TYPE_MPU;
326 aa.hwif = NULL;
327 aa.hdl = NULL;
328 sc->sc_mpu_dev = config_found(sc->sc_dev, &aa, audioprint);
329 }
330
331 /*
332 * Each AC-link frame takes 20.8us, data should be ready in next frame,
333 * we allow more than two.
334 */
335 #define TIMO 50
336 static int
337 fms_read_codec(void *addr, uint8_t reg, uint16_t *val)
338 {
339 struct fms_softc *sc;
340 int i;
341
342 sc = addr;
343 /* Poll until codec is ready */
344 for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
345 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
346 delay(1);
347 if (i >= TIMO) {
348 printf("fms: codec busy\n");
349 return 1;
350 }
351
352 /* Write register index, read access */
353 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD,
354 reg | FM_CODEC_CMD_READ);
355
356 /* Poll until we have valid data */
357 for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh,
358 FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++)
359 delay(1);
360 if (i >= TIMO) {
361 printf("fms: no data from codec\n");
362 return 1;
363 }
364
365 /* Read data */
366 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA);
367 return 0;
368 }
369
370 static int
371 fms_write_codec(void *addr, uint8_t reg, uint16_t val)
372 {
373 struct fms_softc *sc = addr;
374 int i;
375
376 /* Poll until codec is ready */
377 for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
378 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
379 delay(1);
380 if (i >= TIMO) {
381 printf("fms: codec busy\n");
382 return 1;
383 }
384
385 /* Write data */
386 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val);
387 /* Write index register, write access */
388 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg);
389 return 0;
390 }
391 #undef TIMO
392
393 static int
394 fms_attach_codec(void *addr, struct ac97_codec_if *cif)
395 {
396 struct fms_softc *sc;
397
398 sc = addr;
399 sc->codec_if = cif;
400 return 0;
401 }
402
403 /* Cold Reset */
404 static int
405 fms_reset_codec(void *addr)
406 {
407 struct fms_softc *sc;
408
409 sc = addr;
410 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
411 delay(2);
412 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
413 delay(1);
414 return 0;
415 }
416
417 static int
418 fms_intr(void *arg)
419 {
420 struct fms_softc *sc = arg;
421 #if NMPU > 0
422 struct mpu_softc *sc_mpu = device_private(sc->sc_mpu_dev);
423 #endif
424 uint16_t istat;
425
426 mutex_spin_enter(&sc->sc_intr_lock);
427
428 istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS);
429
430 if (istat & FM_INTSTATUS_PLAY) {
431 if ((sc->sc_play_nextblk += sc->sc_play_blksize) >=
432 sc->sc_play_end)
433 sc->sc_play_nextblk = sc->sc_play_start;
434
435 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
436 sc->sc_play_flip++ & 1 ?
437 FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk);
438
439 if (sc->sc_pintr)
440 sc->sc_pintr(sc->sc_parg);
441 else
442 printf("unexpected play intr\n");
443 }
444
445 if (istat & FM_INTSTATUS_REC) {
446 if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >=
447 sc->sc_rec_end)
448 sc->sc_rec_nextblk = sc->sc_rec_start;
449
450 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
451 sc->sc_rec_flip++ & 1 ?
452 FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk);
453
454 if (sc->sc_rintr)
455 sc->sc_rintr(sc->sc_rarg);
456 else
457 printf("unexpected rec intr\n");
458 }
459
460 #if NMPU > 0
461 if (istat & FM_INTSTATUS_MPU)
462 mpu_intr(sc_mpu);
463 #endif
464
465 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
466 istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC));
467
468 mutex_spin_exit(&sc->sc_intr_lock);
469
470 return 1;
471 }
472
473 static int
474 fms_query_encoding(void *addr, struct audio_encoding *fp)
475 {
476
477 switch (fp->index) {
478 case 0:
479 strcpy(fp->name, AudioEmulaw);
480 fp->encoding = AUDIO_ENCODING_ULAW;
481 fp->precision = 8;
482 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
483 return 0;
484 case 1:
485 strcpy(fp->name, AudioEslinear_le);
486 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
487 fp->precision = 16;
488 fp->flags = 0;
489 return 0;
490 case 2:
491 strcpy(fp->name, AudioEulinear);
492 fp->encoding = AUDIO_ENCODING_ULINEAR;
493 fp->precision = 8;
494 fp->flags = 0;
495 return 0;
496 case 3:
497 strcpy(fp->name, AudioEalaw);
498 fp->encoding = AUDIO_ENCODING_ALAW;
499 fp->precision = 8;
500 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
501 return 0;
502 case 4:
503 strcpy(fp->name, AudioEulinear_le);
504 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
505 fp->precision = 16;
506 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
507 return 0;
508 case 5:
509 strcpy(fp->name, AudioEslinear);
510 fp->encoding = AUDIO_ENCODING_SLINEAR;
511 fp->precision = 8;
512 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
513 return 0;
514 case 6:
515 strcpy(fp->name, AudioEulinear_be);
516 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
517 fp->precision = 16;
518 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
519 return 0;
520 case 7:
521 strcpy(fp->name, AudioEslinear_be);
522 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
523 fp->precision = 16;
524 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
525 return 0;
526 default:
527 return EINVAL;
528 }
529 }
530
531 /*
532 * Range below -limit- is set to -rate-
533 * What a pity FM801 does not have 24000
534 * 24000 -> 22050 sounds rather poor
535 */
536 static struct {
537 int limit;
538 int rate;
539 } const fms_rates[11] = {
540 { 6600, 5500 },
541 { 8750, 8000 },
542 { 10250, 9600 },
543 { 13200, 11025 },
544 { 17500, 16000 },
545 { 20500, 19200 },
546 { 26500, 22050 },
547 { 35000, 32000 },
548 { 41000, 38400 },
549 { 46000, 44100 },
550 { 48000, 48000 },
551 /* anything above -> 48000 */
552 };
553
554 #define FMS_NFORMATS 4
555 static const struct audio_format fms_formats[FMS_NFORMATS] = {
556 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
557 2, AUFMT_STEREO, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
558 32000, 38400, 44100, 48000}},
559 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
560 1, AUFMT_MONAURAL, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
561 32000, 38400, 44100, 48000}},
562 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
563 2, AUFMT_STEREO, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
564 32000, 38400, 44100, 48000}},
565 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
566 1, AUFMT_MONAURAL, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
567 32000, 38400, 44100, 48000}},
568 };
569
570 static int
571 fms_set_params(void *addr, int setmode, int usemode,
572 audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
573 stream_filter_list_t *rfil)
574 {
575 struct fms_softc *sc;
576 int i, index;
577
578 sc = addr;
579 if (setmode & AUMODE_PLAY) {
580 for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit;
581 i++)
582 continue;
583 play->sample_rate = fms_rates[i].rate;
584 index = auconv_set_converter(fms_formats, FMS_NFORMATS,
585 AUMODE_PLAY, play, FALSE, pfil);
586 if (index < 0)
587 return EINVAL;
588 sc->sc_play_reg = i << 8;
589 if (fms_formats[index].channels == 2)
590 sc->sc_play_reg |= FM_PLAY_STEREO;
591 if (fms_formats[index].precision == 16)
592 sc->sc_play_reg |= FM_PLAY_16BIT;
593 }
594
595 if (setmode & AUMODE_RECORD) {
596 for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit;
597 i++)
598 continue;
599 rec->sample_rate = fms_rates[i].rate;
600 index = auconv_set_converter(fms_formats, FMS_NFORMATS,
601 AUMODE_RECORD, rec, FALSE, rfil);
602 if (index < 0)
603 return EINVAL;
604 sc->sc_rec_reg = i << 8;
605 if (fms_formats[index].channels == 2)
606 sc->sc_rec_reg |= FM_REC_STEREO;
607 if (fms_formats[index].precision == 16)
608 sc->sc_rec_reg |= FM_REC_16BIT;
609 }
610
611 return 0;
612 }
613
614 static int
615 fms_round_blocksize(void *addr, int blk, int mode,
616 const audio_params_t *param)
617 {
618
619 return blk & ~0xf;
620 }
621
622 static int
623 fms_halt_output(void *addr)
624 {
625 struct fms_softc *sc;
626 uint16_t k1;
627
628 sc = addr;
629 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL);
630 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
631 (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
632 FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST);
633
634 return 0;
635 }
636
637 static int
638 fms_halt_input(void *addr)
639 {
640 struct fms_softc *sc;
641 uint16_t k1;
642
643 sc = addr;
644 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL);
645 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
646 (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
647 FM_REC_BUF1_LAST | FM_REC_BUF2_LAST);
648
649 return 0;
650 }
651
652 static int
653 fms_getdev(void *addr, struct audio_device *retp)
654 {
655
656 *retp = fms_device;
657 return 0;
658 }
659
660 static int
661 fms_set_port(void *addr, mixer_ctrl_t *cp)
662 {
663 struct fms_softc *sc;
664
665 sc = addr;
666 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
667 }
668
669 static int
670 fms_get_port(void *addr, mixer_ctrl_t *cp)
671 {
672 struct fms_softc *sc;
673
674 sc = addr;
675 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
676 }
677
678 static void *
679 fms_malloc(void *addr, int direction, size_t size)
680 {
681 struct fms_softc *sc;
682 struct fms_dma *p;
683 int error;
684 int rseg;
685
686 sc = addr;
687 p = kmem_alloc(sizeof(*p), KM_SLEEP);
688 if (p == NULL)
689 return NULL;
690
691 p->size = size;
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