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