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