FreeBSD kernel sound device code
ess.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5 * Copyright (c) 1997,1998 Luigi Rizzo
6 *
7 * Derived from files in the Voxware 3.5 distribution,
8 * Copyright by Hannu Savolainen 1994, under the same copyright
9 * conditions.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifdef HAVE_KERNEL_OPTION_HEADERS
35#include "opt_snd.h"
36#endif
37
38#include <dev/sound/pcm/sound.h>
39
40#include <dev/sound/isa/sb.h>
41#include <dev/sound/chip.h>
42
43#include <isa/isavar.h>
44
45#include "mixer_if.h"
46
47SND_DECLARE_FILE("$FreeBSD$");
48
49#define ESS_BUFFSIZE (4096)
50#define ABS(x) (((x) < 0)? -(x) : (x))
51
52/* audio2 never generates irqs and sounds very noisy */
53#undef ESS18XX_DUPLEX
54
55/* more accurate clocks and split audio1/audio2 rates */
56#define ESS18XX_NEWSPEED
57
58static u_int32_t ess_pfmt[] = {
59 SND_FORMAT(AFMT_U8, 1, 0),
60 SND_FORMAT(AFMT_U8, 2, 0),
61 SND_FORMAT(AFMT_S8, 1, 0),
62 SND_FORMAT(AFMT_S8, 2, 0),
63 SND_FORMAT(AFMT_S16_LE, 1, 0),
64 SND_FORMAT(AFMT_S16_LE, 2, 0),
65 SND_FORMAT(AFMT_U16_LE, 1, 0),
66 SND_FORMAT(AFMT_U16_LE, 2, 0),
67 0
68};
69
70static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_pfmt, 0};
71
72static u_int32_t ess_rfmt[] = {
73 SND_FORMAT(AFMT_U8, 1, 0),
74 SND_FORMAT(AFMT_U8, 2, 0),
75 SND_FORMAT(AFMT_S8, 1, 0),
76 SND_FORMAT(AFMT_S8, 2, 0),
77 SND_FORMAT(AFMT_S16_LE, 1, 0),
78 SND_FORMAT(AFMT_S16_LE, 2, 0),
79 SND_FORMAT(AFMT_U16_LE, 1, 0),
80 SND_FORMAT(AFMT_U16_LE, 2, 0),
81 0
82};
83
84static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_rfmt, 0};
85
86struct ess_info;
87
88struct ess_chinfo {
93 u_int32_t fmt, spd, blksz;
94};
95
96struct ess_info {
97 device_t parent_dev;
98 struct resource *io_base; /* I/O address for the board */
99 struct resource *irq;
100 struct resource *drq1;
101 struct resource *drq2;
102 void *ih;
103 bus_dma_tag_t parent_dmat;
104
105 unsigned int bufsize;
106 int type;
107 unsigned int duplex:1, newspeed:1;
108 u_long bd_flags; /* board-specific flags */
109 struct ess_chinfo pch, rch;
110};
111
112#if 0
113static int ess_rd(struct ess_info *sc, int reg);
114static void ess_wr(struct ess_info *sc, int reg, u_int8_t val);
115static int ess_dspready(struct ess_info *sc);
116static int ess_cmd(struct ess_info *sc, u_char val);
117static int ess_cmd1(struct ess_info *sc, u_char cmd, int val);
118static int ess_get_byte(struct ess_info *sc);
119static void ess_setmixer(struct ess_info *sc, u_int port, u_int value);
120static int ess_getmixer(struct ess_info *sc, u_int port);
121static int ess_reset_dsp(struct ess_info *sc);
122
123static int ess_write(struct ess_info *sc, u_char reg, int val);
124static int ess_read(struct ess_info *sc, u_char reg);
125
126static void ess_intr(void *arg);
127static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len);
128static int ess_start(struct ess_chinfo *ch);
129static int ess_stop(struct ess_chinfo *ch);
130#endif
131
132/*
133 * Common code for the midi and pcm functions
134 *
135 * ess_cmd write a single byte to the CMD port.
136 * ess_cmd1 write a CMD + 1 byte arg
137 * ess_cmd2 write a CMD + 2 byte arg
138 * ess_get_byte returns a single byte from the DSP data port
139 *
140 * ess_write is actually ess_cmd1
141 * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte
142 */
143
144static void
145ess_lock(struct ess_info *sc) {
146 sbc_lock(device_get_softc(sc->parent_dev));
147}
148
149static void
150ess_unlock(struct ess_info *sc) {
151 sbc_unlock(device_get_softc(sc->parent_dev));
152}
153
154static int
155port_rd(struct resource *port, int off)
156{
157 return bus_space_read_1(rman_get_bustag(port),
158 rman_get_bushandle(port),
159 off);
160}
161
162static void
163port_wr(struct resource *port, int off, u_int8_t data)
164{
165 bus_space_write_1(rman_get_bustag(port),
166 rman_get_bushandle(port),
167 off, data);
168}
169
170static int
171ess_rd(struct ess_info *sc, int reg)
172{
173 return port_rd(sc->io_base, reg);
174}
175
176static void
177ess_wr(struct ess_info *sc, int reg, u_int8_t val)
178{
179 port_wr(sc->io_base, reg, val);
180}
181
182static int
184{
185 return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0);
186}
187
188static int
189ess_dspwr(struct ess_info *sc, u_char val)
190{
191 int i;
192
193 for (i = 0; i < 1000; i++) {
194 if (ess_dspready(sc)) {
195 ess_wr(sc, SBDSP_CMD, val);
196 return 1;
197 }
198 if (i > 10) DELAY((i > 100)? 1000 : 10);
199 }
200 printf("ess_dspwr(0x%02x) timed out.\n", val);
201 return 0;
202}
203
204static int
205ess_cmd(struct ess_info *sc, u_char val)
206{
207#if 0
208 printf("ess_cmd: %x\n", val);
209#endif
210 return ess_dspwr(sc, val);
211}
212
213static int
214ess_cmd1(struct ess_info *sc, u_char cmd, int val)
215{
216#if 0
217 printf("ess_cmd1: %x, %x\n", cmd, val);
218#endif
219 if (ess_dspwr(sc, cmd)) {
220 return ess_dspwr(sc, val & 0xff);
221 } else return 0;
222}
223
224static void
225ess_setmixer(struct ess_info *sc, u_int port, u_int value)
226{
227 DEB(printf("ess_setmixer: reg=%x, val=%x\n", port, value);)
228 ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
229 DELAY(10);
230 ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff));
231 DELAY(10);
232}
233
234static int
235ess_getmixer(struct ess_info *sc, u_int port)
236{
237 int val;
238 ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
239 DELAY(10);
240 val = ess_rd(sc, SB_MIX_DATA);
241 DELAY(10);
242
243 return val;
244}
245
246static int
248{
249 int i;
250
251 for (i = 1000; i > 0; i--) {
252 if (ess_rd(sc, DSP_DATA_AVAIL) & 0x80)
253 return ess_rd(sc, DSP_READ);
254 else
255 DELAY(20);
256 }
257 return -1;
258}
259
260static int
261ess_write(struct ess_info *sc, u_char reg, int val)
262{
263 return ess_cmd1(sc, reg, val);
264}
265
266static int
267ess_read(struct ess_info *sc, u_char reg)
268{
269 return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1;
270}
271
272static int
274{
275 ess_wr(sc, SBDSP_RST, 3);
276 DELAY(100);
277 ess_wr(sc, SBDSP_RST, 0);
278 if (ess_get_byte(sc) != 0xAA) {
279 DEB(printf("ess_reset_dsp 0x%lx failed\n",
280 rman_get_start(sc->io_base)));
281 return ENXIO; /* Sorry */
282 }
283 ess_cmd(sc, 0xc6);
284 return 0;
285}
286
287static void
288ess_release_resources(struct ess_info *sc, device_t dev)
289{
290 if (sc->irq) {
291 if (sc->ih)
292 bus_teardown_intr(dev, sc->irq, sc->ih);
293 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
294 sc->irq = NULL;
295 }
296 if (sc->drq1) {
297 isa_dma_release(rman_get_start(sc->drq1));
298 bus_release_resource(dev, SYS_RES_DRQ, 0, sc->drq1);
299 sc->drq1 = NULL;
300 }
301 if (sc->drq2) {
302 isa_dma_release(rman_get_start(sc->drq2));
303 bus_release_resource(dev, SYS_RES_DRQ, 1, sc->drq2);
304 sc->drq2 = NULL;
305 }
306 if (sc->io_base) {
307 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->io_base);
308 sc->io_base = NULL;
309 }
310 if (sc->parent_dmat) {
311 bus_dma_tag_destroy(sc->parent_dmat);
312 sc->parent_dmat = 0;
313 }
314 free(sc, M_DEVBUF);
315}
316
317static int
318ess_alloc_resources(struct ess_info *sc, device_t dev)
319{
320 int rid;
321
322 rid = 0;
323 if (!sc->io_base)
324 sc->io_base = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
325 &rid, RF_ACTIVE);
326 rid = 0;
327 if (!sc->irq)
328 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
329 &rid, RF_ACTIVE);
330 rid = 0;
331 if (!sc->drq1)
332 sc->drq1 = bus_alloc_resource_any(dev, SYS_RES_DRQ,
333 &rid, RF_ACTIVE);
334 rid = 1;
335 if (!sc->drq2)
336 sc->drq2 = bus_alloc_resource_any(dev, SYS_RES_DRQ,
337 &rid, RF_ACTIVE);
338
339 if (sc->io_base && sc->drq1 && sc->irq) {
340 isa_dma_acquire(rman_get_start(sc->drq1));
341 isa_dmainit(rman_get_start(sc->drq1), sc->bufsize);
342
343 if (sc->drq2) {
344 isa_dma_acquire(rman_get_start(sc->drq2));
345 isa_dmainit(rman_get_start(sc->drq2), sc->bufsize);
346 }
347
348 return 0;
349 } else return ENXIO;
350}
351
352static void
353ess_intr(void *arg)
354{
355 struct ess_info *sc = (struct ess_info *)arg;
356 int src, pirq, rirq;
357
358 ess_lock(sc);
359 src = 0;
360 if (ess_getmixer(sc, 0x7a) & 0x80)
361 src |= 2;
362 if (ess_rd(sc, 0x0c) & 0x01)
363 src |= 1;
364
365 pirq = (src & sc->pch.hwch)? 1 : 0;
366 rirq = (src & sc->rch.hwch)? 1 : 0;
367
368 if (pirq) {
369 if (sc->pch.run) {
370 ess_unlock(sc);
371 chn_intr(sc->pch.channel);
372 ess_lock(sc);
373 }
374 if (sc->pch.stopping) {
375 sc->pch.run = 0;
376 sndbuf_dma(sc->pch.buffer, PCMTRIG_STOP);
377 sc->pch.stopping = 0;
378 if (sc->pch.hwch == 1)
379 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
380 else
381 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03);
382 }
383 }
384
385 if (rirq) {
386 if (sc->rch.run) {
387 ess_unlock(sc);
388 chn_intr(sc->rch.channel);
389 ess_lock(sc);
390 }
391 if (sc->rch.stopping) {
392 sc->rch.run = 0;
394 sc->rch.stopping = 0;
395 /* XXX: will this stop audio2? */
396 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
397 }
398 }
399
400 if (src & 2)
401 ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80);
402 if (src & 1)
404 ess_unlock(sc);
405}
406
407/* utility functions for ESS */
408static u_int8_t
410{
411 int speed = *spd;
412 u_int32_t t;
413
414 if (speed > 22000) {
415 t = (795500 + speed / 2) / speed;
416 speed = (795500 + t / 2) / t;
417 t = (256 - t) | 0x80;
418 } else {
419 t = (397700 + speed / 2) / speed;
420 speed = (397700 + t / 2) / t;
421 t = 128 - t;
422 }
423 *spd = speed;
424 return t & 0x000000ff;
425}
426
427static u_int8_t
429{
430 int speed, s0, s1, use0;
431 u_int8_t t0, t1;
432
433 /* rate = source / (256 - divisor) */
434 /* divisor = 256 - (source / rate) */
435 speed = *spd;
436 t0 = 128 - (793800 / speed);
437 s0 = 793800 / (128 - t0);
438
439 t1 = 128 - (768000 / speed);
440 s1 = 768000 / (128 - t1);
441 t1 |= 0x80;
442
443 use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0;
444
445 *spd = use0? s0 : s1;
446 return use0? t0 : t1;
447}
448
449static u_int8_t
451{
452 int cutoff;
453
454 /* cutoff = 7160000 / (256 - divisor) */
455 /* divisor = 256 - (7160000 / cutoff) */
456 cutoff = (spd * 9 * 82) / 20;
457 return (256 - (7160000 / cutoff));
458}
459
460static int
461ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
462{
463 int play = (dir == PCMDIR_PLAY)? 1 : 0;
464 int b16 = (fmt & AFMT_16BIT)? 1 : 0;
465 int stereo = (AFMT_CHANNEL(fmt) > 1)? 1 : 0;
466 int unsign = (fmt == AFMT_U8 || fmt == AFMT_U16_LE)? 1 : 0;
467 u_int8_t spdval, fmtval;
468
469 spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd);
470 len = -len;
471
472 if (ch == 1) {
473 KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad"));
474 /* transfer length low */
475 ess_write(sc, 0xa4, len & 0x00ff);
476 /* transfer length high */
477 ess_write(sc, 0xa5, (len & 0xff00) >> 8);
478 /* autoinit, dma dir */
479 ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a));
480 /* mono/stereo */
481 ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02));
482 /* demand mode, 4 bytes/xfer */
483 ess_write(sc, 0xb9, 0x02);
484 /* sample rate */
485 ess_write(sc, 0xa1, spdval);
486 /* filter cutoff */
487 ess_write(sc, 0xa2, ess_calcfilter(spd));
488 /* setup dac/adc */
489 if (play)
490 ess_write(sc, 0xb6, unsign? 0x80 : 0x00);
491 /* mono, b16: signed, load signal */
492 ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20));
493 /* setup fifo */
494 ess_write(sc, 0xb7, 0x90 | (unsign? 0x00 : 0x20) |
495 (b16? 0x04 : 0x00) |
496 (stereo? 0x08 : 0x40));
497 /* irq control */
498 ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50);
499 /* drq control */
500 ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50);
501 } else if (ch == 2) {
502 KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad"));
503 /* transfer length low */
504 ess_setmixer(sc, 0x74, len & 0x00ff);
505 /* transfer length high */
506 ess_setmixer(sc, 0x76, (len & 0xff00) >> 8);
507 /* autoinit, 4 bytes/req */
508 ess_setmixer(sc, 0x78, 0x90);
509 fmtval = b16 | (stereo << 1) | (unsign << 2);
510 /* enable irq, set format */
511 ess_setmixer(sc, 0x7a, 0x40 | fmtval);
512 if (sc->newspeed) {
513 /* sample rate */
514 ess_setmixer(sc, 0x70, spdval);
515 /* filter cutoff */
516 ess_setmixer(sc, 0x72, ess_calcfilter(spd));
517 }
518 }
519
520 return 0;
521}
522static int
524{
525 struct ess_info *sc = ch->parent;
526 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
527
528 ess_lock(sc);
529 ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz);
530 ch->stopping = 0;
531 if (ch->hwch == 1)
532 ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01);
533 else
534 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03);
535 if (play)
537 ess_unlock(sc);
538 return 0;
539}
540
541static int
543{
544 struct ess_info *sc = ch->parent;
545 int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
546
547 ess_lock(sc);
548 ch->stopping = 1;
549 if (ch->hwch == 1)
550 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04);
551 else
552 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10);
553 if (play)
555 ess_unlock(sc);
556 return 0;
557}
558
559/* -------------------------------------------------------------------- */
560/* channel interface for ESS18xx */
561static void *
562esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
563{
564 struct ess_info *sc = devinfo;
565 struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
566
567 ch->parent = sc;
568 ch->channel = c;
569 ch->buffer = b;
570 if (sndbuf_alloc(ch->buffer, sc->parent_dmat, 0, sc->bufsize) != 0)
571 return NULL;
572 ch->dir = dir;
573 ch->hwch = 1;
574 if ((dir == PCMDIR_PLAY) && (sc->duplex))
575 ch->hwch = 2;
576 sndbuf_dmasetup(ch->buffer, (ch->hwch == 1)? sc->drq1 : sc->drq2);
577 return ch;
578}
579
580static int
581esschan_setformat(kobj_t obj, void *data, u_int32_t format)
582{
583 struct ess_chinfo *ch = data;
584
585 ch->fmt = format;
586 return 0;
587}
588
589static u_int32_t
590esschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
591{
592 struct ess_chinfo *ch = data;
593 struct ess_info *sc = ch->parent;
594
595 ch->spd = speed;
596 if (sc->newspeed)
597 ess_calcspeed9(&ch->spd);
598 else
599 ess_calcspeed8(&ch->spd);
600 return ch->spd;
601}
602
603static u_int32_t
604esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
605{
606 struct ess_chinfo *ch = data;
607
608 ch->blksz = blocksize;
609 return ch->blksz;
610}
611
612static int
613esschan_trigger(kobj_t obj, void *data, int go)
614{
615 struct ess_chinfo *ch = data;
616
617 if (!PCMTRIG_COMMON(go))
618 return 0;
619
620 switch (go) {
621 case PCMTRIG_START:
622 ch->run = 1;
623 sndbuf_dma(ch->buffer, go);
624 ess_start(ch);
625 break;
626
627 case PCMTRIG_STOP:
628 case PCMTRIG_ABORT:
629 default:
630 ess_stop(ch);
631 break;
632 }
633 return 0;
634}
635
636static u_int32_t
637esschan_getptr(kobj_t obj, void *data)
638{
639 struct ess_chinfo *ch = data;
640
641 return sndbuf_dmaptr(ch->buffer);
642}
643
644static struct pcmchan_caps *
645esschan_getcaps(kobj_t obj, void *data)
646{
647 struct ess_chinfo *ch = data;
648
649 return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
650}
651
652static kobj_method_t esschan_methods[] = {
653 KOBJMETHOD(channel_init, esschan_init),
654 KOBJMETHOD(channel_setformat, esschan_setformat),
655 KOBJMETHOD(channel_setspeed, esschan_setspeed),
656 KOBJMETHOD(channel_setblocksize, esschan_setblocksize),
657 KOBJMETHOD(channel_trigger, esschan_trigger),
658 KOBJMETHOD(channel_getptr, esschan_getptr),
659 KOBJMETHOD(channel_getcaps, esschan_getcaps),
661};
663
664/************************************************************/
665
666static int
668{
669 struct ess_info *sc = mix_getdevinfo(m);
670
671 mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE |
672 SOUND_MASK_IMIX);
673
674 mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
675 SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME |
676 SOUND_MASK_LINE1 | SOUND_MASK_SPEAKER);
677
678 ess_setmixer(sc, 0, 0); /* reset */
679
680 return 0;
681}
682
683static int
684essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
685{
686 struct ess_info *sc = mix_getdevinfo(m);
687 int preg = 0, rreg = 0, l, r;
688
689 l = (left * 15) / 100;
690 r = (right * 15) / 100;
691 switch (dev) {
692 case SOUND_MIXER_SYNTH:
693 preg = 0x36;
694 rreg = 0x6b;
695 break;
696
697 case SOUND_MIXER_PCM:
698 preg = 0x14;
699 rreg = 0x7c;
700 break;
701
702 case SOUND_MIXER_LINE:
703 preg = 0x3e;
704 rreg = 0x6e;
705 break;
706
707 case SOUND_MIXER_MIC:
708 preg = 0x1a;
709 rreg = 0x68;
710 break;
711
712 case SOUND_MIXER_LINE1:
713 preg = 0x3a;
714 rreg = 0x6c;
715 break;
716
717 case SOUND_MIXER_CD:
718 preg = 0x38;
719 rreg = 0x6a;
720 break;
721
722 case SOUND_MIXER_SPEAKER:
723 preg = 0x3c;
724 break;
725
726 case SOUND_MIXER_VOLUME:
727 l = left? (left * 63) / 100 : 64;
728 r = right? (right * 63) / 100 : 64;
729 ess_setmixer(sc, 0x60, l);
730 ess_setmixer(sc, 0x62, r);
731 left = (l == 64)? 0 : (l * 100) / 63;
732 right = (r == 64)? 0 : (r * 100) / 63;
733 return left | (right << 8);
734 }
735
736 if (preg)
737 ess_setmixer(sc, preg, (l << 4) | r);
738 if (rreg)
739 ess_setmixer(sc, rreg, (l << 4) | r);
740
741 left = (l * 100) / 15;
742 right = (r * 100) / 15;
743
744 return left | (right << 8);
745}
746
747static u_int32_t
748essmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
749{
750 struct ess_info *sc = mix_getdevinfo(m);
751 u_char recdev;
752
753 switch (src) {
754 case SOUND_MASK_CD:
755 recdev = 0x02;
756 break;
757
758 case SOUND_MASK_LINE:
759 recdev = 0x06;
760 break;
761
762 case SOUND_MASK_IMIX:
763 recdev = 0x05;
764 break;
765
766 case SOUND_MASK_MIC:
767 default:
768 recdev = 0x00;
769 src = SOUND_MASK_MIC;
770 break;
771 }
772
773 ess_setmixer(sc, 0x1c, recdev);
774
775 return src;
776}
777
778static kobj_method_t essmixer_methods[] = {
779 KOBJMETHOD(mixer_init, essmix_init),
780 KOBJMETHOD(mixer_set, essmix_set),
783};
785
786/************************************************************/
787
788static int
789ess_probe(device_t dev)
790{
791 uintptr_t func, ver, r, f;
792
793 /* The parent device has already been probed. */
794 r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
795 if (func != SCF_PCM)
796 return (ENXIO);
797
798 r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
799 f = (ver & 0xffff0000) >> 16;
800 if (!(f & BD_F_ESS))
801 return (ENXIO);
802
803 device_set_desc(dev, "ESS 18xx DSP");
804
805 return 0;
806}
807
808static int
810{
811 struct ess_info *sc;
812 char status[SND_STATUSLEN], buf[64];
813 int ver;
814
815 gone_in_dev(dev, 14, "ISA sound driver");
816 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
817 sc->parent_dev = device_get_parent(dev);
818 sc->bufsize = pcm_getbuffersize(dev, 4096, ESS_BUFFSIZE, 65536);
819 if (ess_alloc_resources(sc, dev))
820 goto no;
821 if (ess_reset_dsp(sc))
822 goto no;
823 if (mixer_init(dev, &essmixer_class, sc))
824 goto no;
825
826 sc->duplex = 0;
827 sc->newspeed = 0;
828 ver = (ess_getmixer(sc, 0x40) << 8) | ess_rd(sc, SB_MIX_DATA);
829 snprintf(buf, sizeof buf, "ESS %x DSP", ver);
830 device_set_desc_copy(dev, buf);
831 if (bootverbose)
832 device_printf(dev, "ESS%x detected", ver);
833
834 switch (ver) {
835 case 0x1869:
836 case 0x1879:
837#ifdef ESS18XX_DUPLEX
838 sc->duplex = sc->drq2? 1 : 0;
839#endif
840#ifdef ESS18XX_NEWSPEED
841 sc->newspeed = 1;
842#endif
843 break;
844 }
845 if (bootverbose)
846 printf("%s%s\n", sc->duplex? ", duplex" : "",
847 sc->newspeed? ", newspeed" : "");
848
849 if (sc->newspeed)
850 ess_setmixer(sc, 0x71, 0x22);
851
852 snd_setup_intr(dev, sc->irq, 0, ess_intr, sc, &sc->ih);
853 if (!sc->duplex)
855
856 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
857 /*boundary*/0,
858 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
859 /*highaddr*/BUS_SPACE_MAXADDR,
860 /*filter*/NULL, /*filterarg*/NULL,
861 /*maxsize*/sc->bufsize, /*nsegments*/1,
862 /*maxsegz*/0x3ffff,
863 /*flags*/0, /*lockfunc*/NULL, /*lockarg*/NULL,
864 &sc->parent_dmat) != 0) {
865 device_printf(dev, "unable to create dma tag\n");
866 goto no;
867 }
868
869 if (sc->drq2)
870 snprintf(buf, SND_STATUSLEN, ":%jd", rman_get_start(sc->drq2));
871 else
872 buf[0] = '\0';
873
874 snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd drq %jd%s bufsz %u %s",
875 rman_get_start(sc->io_base), rman_get_start(sc->irq),
876 rman_get_start(sc->drq1), buf, sc->bufsize,
877 PCM_KLDSTRING(snd_ess));
878
879 if (pcm_register(dev, sc, 1, 1))
880 goto no;
881 pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc);
882 pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc);
884
885 return 0;
886
887no:
889 return ENXIO;
890}
891
892static int
894{
895 int r;
896 struct ess_info *sc;
897
899 if (r)
900 return r;
901
902 sc = pcm_getdevinfo(dev);
904 return 0;
905}
906
907static int
909{
910 struct ess_info *sc;
911
912 sc = pcm_getdevinfo(dev);
913
914 if (ess_reset_dsp(sc)) {
915 device_printf(dev, "unable to reset DSP at resume\n");
916 return ENXIO;
917 }
918
919 if (mixer_reinit(dev)) {
920 device_printf(dev, "unable to reinitialize mixer at resume\n");
921 return ENXIO;
922 }
923
924 return 0;
925}
926
927static device_method_t ess_methods[] = {
928 /* Device interface */
929 DEVMETHOD(device_probe, ess_probe),
930 DEVMETHOD(device_attach, ess_attach),
931 DEVMETHOD(device_detach, ess_detach),
932 DEVMETHOD(device_resume, ess_resume),
933 { 0, 0 }
934};
935
936static driver_t ess_driver = {
937 "pcm",
940};
941
944MODULE_DEPEND(snd_ess, snd_sbc, 1, 1, 1);
945MODULE_VERSION(snd_ess, 1);
946
947/************************************************************/
948
949static devclass_t esscontrol_devclass;
950
951static struct isa_pnp_id essc_ids[] = {
952 {0x06007316, "ESS Control"},
953 {0}
954};
955
956static int
958{
959 int i;
960
961 i = ISA_PNP_PROBE(device_get_parent(dev), dev, essc_ids);
962 if (i == 0)
963 device_quiet(dev);
964 return i;
965}
966
967static int
969{
970#ifdef notyet
971 struct resource *io;
972 int rid, i, x;
973
974 rid = 0;
975 io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
976 x = 0;
977 for (i = 0; i < 0x100; i++) {
978 port_wr(io, 0, i);
979 x = port_rd(io, 1);
980 if ((i & 0x0f) == 0)
981 printf("%3.3x: ", i);
982 printf("%2.2x ", x);
983 if ((i & 0x0f) == 0x0f)
984 printf("\n");
985 }
986 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
987 io = NULL;
988#endif
989
990 return 0;
991}
992
993static int
995{
996 return 0;
997}
998
999static device_method_t esscontrol_methods[] = {
1000 /* Device interface */
1001 DEVMETHOD(device_probe, esscontrol_probe),
1002 DEVMETHOD(device_attach, esscontrol_attach),
1003 DEVMETHOD(device_detach, esscontrol_detach),
1004 { 0, 0 }
1005};
1006
1007static driver_t esscontrol_driver = {
1008 "esscontrol",
1010 1,
1011};
1012
u_int32_t data
Definition: ac97_if.m:60
void * devinfo
Definition: ac97_if.m:47
#define DEB(x)
Definition: als4000.c:56
uint32_t format
Definition: audio_dai_if.m:39
uint32_t speed
Definition: audio_dai_if.m:86
int go
Definition: audio_dai_if.m:64
unsigned int fmt
Definition: audio_soc.c:91
int sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags, unsigned int size)
Definition: buffer.c:93
void chn_intr(struct pcm_channel *c)
Definition: channel.c:660
#define PCMDIR_PLAY
Definition: channel.h:339
#define PCMTRIG_START
Definition: channel.h:344
#define PCMTRIG_STOP
Definition: channel.h:347
#define PCMDIR_REC
Definition: channel.h:341
#define PCMTRIG_COMMON(x)
Definition: channel.h:350
#define PCMTRIG_ABORT
Definition: channel.h:348
struct pcm_channel * c
Definition: channel_if.m:106
METHOD int free
Definition: channel_if.m:110
u_int32_t blocksize
Definition: channel_if.m:140
struct pcmchan_matrix * m
Definition: channel_if.m:232
struct snd_dbuf * b
Definition: channel_if.m:105
@ SCF_PCM
Definition: chip.h:36
uint32_t spd
Definition: dsp.c:394
uint16_t len
unsigned stereo
Definition: es137x.c:262
unsigned right
Definition: es137x.c:261
unsigned left
Definition: es137x.c:260
static int ess_resume(device_t dev)
Definition: ess.c:908
static kobj_method_t esschan_methods[]
Definition: ess.c:652
static int ess_attach(device_t dev)
Definition: ess.c:809
static void * esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
Definition: ess.c:562
static int essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
Definition: ess.c:684
static int ess_reset_dsp(struct ess_info *sc)
Definition: ess.c:273
CHANNEL_DECLARE(esschan)
static int esscontrol_detach(device_t dev)
Definition: ess.c:994
static int ess_start(struct ess_chinfo *ch)
Definition: ess.c:523
static int ess_probe(device_t dev)
Definition: ess.c:789
static struct pcmchan_caps * esschan_getcaps(kobj_t obj, void *data)
Definition: ess.c:645
static u_int8_t ess_calcspeed9(int *spd)
Definition: ess.c:428
static int ess_detach(device_t dev)
Definition: ess.c:893
static int ess_getmixer(struct ess_info *sc, u_int port)
Definition: ess.c:235
#define ESS_BUFFSIZE
Definition: ess.c:49
static int ess_stop(struct ess_chinfo *ch)
Definition: ess.c:542
static void ess_intr(void *arg)
Definition: ess.c:353
static int esschan_setformat(kobj_t obj, void *data, u_int32_t format)
Definition: ess.c:581
static int port_rd(struct resource *port, int off)
Definition: ess.c:155
static int ess_write(struct ess_info *sc, u_char reg, int val)
Definition: ess.c:261
static u_int32_t essmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
Definition: ess.c:748
static void ess_release_resources(struct ess_info *sc, device_t dev)
Definition: ess.c:288
static u_int32_t esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
Definition: ess.c:604
MODULE_VERSION(snd_ess, 1)
static u_int32_t ess_pfmt[]
Definition: ess.c:58
static devclass_t esscontrol_devclass
Definition: ess.c:949
static int ess_dspwr(struct ess_info *sc, u_char val)
Definition: ess.c:189
static u_int8_t ess_calcfilter(int spd)
Definition: ess.c:450
static device_method_t ess_methods[]
Definition: ess.c:927
MIXER_DECLARE(essmixer)
static int ess_read(struct ess_info *sc, u_char reg)
Definition: ess.c:267
static void ess_lock(struct ess_info *sc)
Definition: ess.c:145
static int esschan_trigger(kobj_t obj, void *data, int go)
Definition: ess.c:613
SND_DECLARE_FILE("$FreeBSD$")
static void ess_unlock(struct ess_info *sc)
Definition: ess.c:150
static kobj_method_t essmixer_methods[]
Definition: ess.c:778
static int esscontrol_probe(device_t dev)
Definition: ess.c:957
static void port_wr(struct resource *port, int off, u_int8_t data)
Definition: ess.c:163
static int ess_cmd(struct ess_info *sc, u_char val)
Definition: ess.c:205
DRIVER_MODULE(snd_ess, sbc, ess_driver, pcm_devclass, 0, 0)
#define ABS(x)
Definition: ess.c:50
static int ess_get_byte(struct ess_info *sc)
Definition: ess.c:247
static u_int32_t esschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
Definition: ess.c:590
static u_int32_t esschan_getptr(kobj_t obj, void *data)
Definition: ess.c:637
static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
Definition: ess.c:461
static struct pcmchan_caps ess_reccaps
Definition: ess.c:84
static int ess_cmd1(struct ess_info *sc, u_char cmd, int val)
Definition: ess.c:214
static int ess_rd(struct ess_info *sc, int reg)
Definition: ess.c:171
static driver_t ess_driver
Definition: ess.c:936
static u_int32_t ess_rfmt[]
Definition: ess.c:72
static device_method_t esscontrol_methods[]
Definition: ess.c:999
static int ess_dspready(struct ess_info *sc)
Definition: ess.c:183
static driver_t esscontrol_driver
Definition: ess.c:1007
static int essmix_init(struct snd_mixer *m)
Definition: ess.c:667
static int esscontrol_attach(device_t dev)
Definition: ess.c:968
static u_int8_t ess_calcspeed8(int *spd)
Definition: ess.c:409
static struct isa_pnp_id essc_ids[]
Definition: ess.c:951
static struct pcmchan_caps ess_playcaps
Definition: ess.c:70
static int ess_alloc_resources(struct ess_info *sc, device_t dev)
Definition: ess.c:318
static void ess_wr(struct ess_info *sc, int reg, u_int8_t val)
Definition: ess.c:177
MODULE_DEPEND(snd_ess, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER)
static void ess_setmixer(struct ess_info *sc, u_int port, u_int value)
Definition: ess.c:225
ISA_PNP_INFO(essc_ids)
uint32_t value
Definition: hdaa.c:58
uint8_t reg
Definition: hdac.c:211
int dir
Definition: hdac_if.m:45
bus_addr_t buf
Definition: hdac_if.m:63
uint8_t r
#define KOBJMETHOD_END
Definition: midi.c:76
static int mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
Definition: mixer.c:373
int mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
Definition: mixer.c:725
static int mixer_set(struct snd_mixer *m, u_int dev, u_int32_t muted, u_int lev)
Definition: mixer.c:247
void mix_setdevs(struct snd_mixer *m, u_int32_t v)
Definition: mixer.c:489
int mixer_reinit(device_t dev)
Definition: mixer.c:860
void * mix_getdevinfo(struct snd_mixer *m)
Definition: mixer.c:645
void mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
Record mask of available recording devices.
Definition: mixer.c:529
unsigned dev
Definition: mixer_if.m:59
u_int32_t src
Definition: mixer_if.m:66
bool * status
uint16_t rid
u_int32_t val
u_int func
void sbc_unlock(struct sbc_softc *)
Definition: sbc.c:136
#define DSP_READ
Definition: sb.h:44
void sbc_lock(struct sbc_softc *)
Definition: sbc.c:124
#define SBDSP_RST
Definition: sb.h:43
#define SB_MIX_ADDR
Definition: sb.h:51
#define SBDSP_STATUS
Definition: sb.h:47
#define BD_F_ESS
Definition: sb.h:174
#define SBDSP_CMD
Definition: sb.h:46
#define SB_MIX_DATA
Definition: sb.h:52
#define DSP_CMD_SPKON
Definition: sb.h:100
#define DSP_CMD_SPKOFF
Definition: sb.h:101
#define DSP_DATA_AVAIL
Definition: sb.h:48
int sndbuf_dmasetup(struct snd_dbuf *b, struct resource *drq)
Definition: sndbuf_dma.c:40
void sndbuf_dma(struct snd_dbuf *b, int go)
Definition: sndbuf_dma.c:63
int sndbuf_dmaptr(struct snd_dbuf *b)
Definition: sndbuf_dma.c:88
void pcm_setflags(device_t dev, uint32_t val)
Definition: sound.c:824
void * pcm_getdevinfo(device_t dev)
Definition: sound.c:832
uint32_t pcm_getflags(device_t dev)
Definition: sound.c:816
int pcm_setstatus(device_t dev, char *str)
Definition: sound.c:766
devclass_t pcm_devclass
Definition: sound.c:49
int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
Definition: sound.c:692
int pcm_unregister(device_t dev)
Definition: sound.c:1170
int pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
Definition: sound.c:1080
int snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
Definition: sound.c:117
unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
Definition: sound.c:840
#define PCM_KLDSTRING(a)
Definition: sound.h:619
#define SND_FORMAT(f, c, e)
Definition: sound.h:238
#define SOUND_PREFVER
Definition: sound.h:103
#define SOUND_MAXVER
Definition: sound.h:104
#define AFMT_CHANNEL(v)
Definition: sound.h:227
#define SD_F_SIMPLEX
Definition: sound.h:132
#define SOUND_MINVER
Definition: sound.h:102
#define PCM_SOFTC_SIZE
Definition: sound.h:96
#define SND_STATUSLEN
Definition: sound.h:98
#define AFMT_16BIT
Definition: sound.h:191
Definition: ess.c:88
struct snd_dbuf * buffer
Definition: ess.c:91
u_int32_t blksz
Definition: ess.c:93
struct ess_info * parent
Definition: ess.c:89
struct pcm_channel * channel
Definition: ess.c:90
int run
Definition: ess.c:92
u_int32_t fmt
Definition: ess.c:93
u_int32_t spd
Definition: ess.c:93
int stopping
Definition: ess.c:92
int dir
Definition: ess.c:92
int hwch
Definition: ess.c:92
Definition: ess.c:96
struct resource * drq1
Definition: ess.c:100
struct resource * io_base
Definition: ess.c:98
int type
Definition: ess.c:106
unsigned int newspeed
Definition: ess.c:107
bus_dma_tag_t parent_dmat
Definition: ess.c:103
void * ih
Definition: ess.c:102
unsigned int bufsize
Definition: ess.c:105
unsigned int duplex
Definition: ess.c:107
struct resource * irq
Definition: ess.c:99
struct resource * drq2
Definition: ess.c:101
device_t parent_dev
Definition: ess.c:97
u_long bd_flags
Definition: ess.c:108
struct ess_chinfo pch rch
Definition: ess.c:109