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