2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-4-Clause
4 * Copyright (c) 1999 Russell Cattelan <cattelan@thebarn.com>
5 * Copyright (c) 1998 Joachim Kuebart <joachim.kuebart@gmx.net>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
46 * 3. All advertising materials mentioning features or use of this
47 * software must display the following acknowledgement:
48 * This product includes software developed by Joachim Kuebart.
50 * 4. The name of the author may not be used to endorse or promote
51 * products derived from this software without specific prior
54 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
55 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
57 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
64 * OF THE POSSIBILITY OF SUCH DAMAGE.
68 * Support the ENSONIQ AudioPCI board and Creative Labs SoundBlaster PCI
69 * boards based on the ES1370, ES1371 and ES1373 chips.
71 * Part of this code was heavily inspired by the linux driver from
72 * Thomas Sailer (sailer@ife.ee.ethz.ch)
73 * Just about everything has been touched and reworked in some way but
74 * the all the underlying sequences/timing/register values are from
78 #ifdef HAVE_KERNEL_OPTION_HEADERS
82 #include <dev/sound/pcm/sound.h>
83 #include <dev/sound/pcm/ac97.h>
84 #include <dev/sound/pci/es137x.h>
86 #include <dev/pci/pcireg.h>
87 #include <dev/pci/pcivar.h>
89 #include <sys/sysctl.h>
93 SND_DECLARE_FILE("$FreeBSD$");
95 #define MEM_MAP_REG 0x14
97 /* PCI IDs of supported chips */
98 #define ES1370_PCI_ID 0x50001274
99 #define ES1371_PCI_ID 0x13711274
100 #define ES1371_PCI_ID2 0x13713274
101 #define CT5880_PCI_ID 0x58801274
102 #define CT4730_PCI_ID 0x89381102
104 #define ES1371REV_ES1371_A 0x02
105 #define ES1371REV_ES1371_B 0x09
107 #define ES1371REV_ES1373_8 0x08
108 #define ES1371REV_ES1373_A 0x04
109 #define ES1371REV_ES1373_B 0x06
111 #define ES1371REV_CT5880_A 0x07
113 #define CT5880REV_CT5880_C 0x02
114 #define CT5880REV_CT5880_D 0x03
115 #define CT5880REV_CT5880_E 0x04
117 #define CT4730REV_CT4730_A 0x00
119 #define ES_DEFAULT_BUFSZ 4096
121 /* 2 DAC for playback, 1 ADC for record */
127 #define ES_DMA_SEGS_MIN 2
128 #define ES_DMA_SEGS_MAX 256
129 #define ES_BLK_MIN 64
130 #define ES_BLK_ALIGN (~(ES_BLK_MIN - 1))
132 #define ES1370_DAC1_MINSPEED 5512
133 #define ES1370_DAC1_MAXSPEED 44100
135 /* device private data */
139 struct es_info *parent;
140 struct pcm_channel *channel;
141 struct snd_dbuf *buffer;
142 struct pcmchan_caps caps;
144 uint32_t fmt, blksz, blkcnt, bufsz;
145 uint32_t ptr, prevptr;
150 * 32bit Ensoniq Configuration (es->escfg).
151 * ----------------------------------------
153 * +-------+--------+------+------+---------+--------+---------+---------+
154 * len | 16 | 1 | 1 | 1 | 2 | 2 | 1 | 8 |
155 * +-------+--------+------+------+---------+--------+---------+---------+
156 * | fixed | single | | | | | is | general |
157 * | rate | pcm | DACx | DACy | numplay | numrec | es1370? | purpose |
158 * | | mixer | | | | | | |
159 * +-------+--------+------+------+---------+--------+---------+---------+
161 #define ES_FIXED_RATE(cfgv) \
162 (((cfgv) & 0xffff0000) >> 16)
163 #define ES_SET_FIXED_RATE(cfgv, nv) \
164 (((cfgv) & ~0xffff0000) | (((nv) & 0xffff) << 16))
165 #define ES_SINGLE_PCM_MIX(cfgv) \
166 (((cfgv) & 0x8000) >> 15)
167 #define ES_SET_SINGLE_PCM_MIX(cfgv, nv) \
168 (((cfgv) & ~0x8000) | (((nv) ? 1 : 0) << 15))
169 #define ES_DAC_FIRST(cfgv) \
170 (((cfgv) & 0x4000) >> 14)
171 #define ES_SET_DAC_FIRST(cfgv, nv) \
172 (((cfgv) & ~0x4000) | (((nv) & 0x1) << 14))
173 #define ES_DAC_SECOND(cfgv) \
174 (((cfgv) & 0x2000) >> 13)
175 #define ES_SET_DAC_SECOND(cfgv, nv) \
176 (((cfgv) & ~0x2000) | (((nv) & 0x1) << 13))
177 #define ES_NUMPLAY(cfgv) \
178 (((cfgv) & 0x1800) >> 11)
179 #define ES_SET_NUMPLAY(cfgv, nv) \
180 (((cfgv) & ~0x1800) | (((nv) & 0x3) << 11))
181 #define ES_NUMREC(cfgv) \
182 (((cfgv) & 0x600) >> 9)
183 #define ES_SET_NUMREC(cfgv, nv) \
184 (((cfgv) & ~0x600) | (((nv) & 0x3) << 9))
185 #define ES_IS_ES1370(cfgv) \
186 (((cfgv) & 0x100) >> 8)
187 #define ES_SET_IS_ES1370(cfgv, nv) \
188 (((cfgv) & ~0x100) | (((nv) ? 1 : 0) << 8))
189 #define ES_GP(cfgv) \
191 #define ES_SET_GP(cfgv, nv) \
192 (((cfgv) & ~0xff) | ((nv) & 0xff))
194 #define ES_DAC1_ENABLED(cfgv) \
195 (ES_NUMPLAY(cfgv) > 1 || \
196 (ES_NUMPLAY(cfgv) == 1 && ES_DAC_FIRST(cfgv) == ES_DAC1))
197 #define ES_DAC2_ENABLED(cfgv) \
198 (ES_NUMPLAY(cfgv) > 1 || \
199 (ES_NUMPLAY(cfgv) == 1 && ES_DAC_FIRST(cfgv) == ES_DAC2))
202 * DAC 1/2 configuration through kernel hint - hint.pcm.<unit>.dac="val"
204 * 0 = Enable both DACs - Default
205 * 1 = Enable single DAC (DAC1)
206 * 2 = Enable single DAC (DAC2)
207 * 3 = Enable both DACs, swap position (DAC2 comes first instead of DAC1)
209 #define ES_DEFAULT_DAC_CFG 0
213 bus_space_handle_t sh;
214 bus_dma_tag_t parent_dmat;
216 struct resource *reg, *irq;
217 int regtype, regid, irqid;
222 unsigned int bufsz, blkcnt;
224 /* Contents of board's registers */
228 struct es_chinfo ch[ES_NCHANS];
230 struct callout poll_timer;
231 int poll_ticks, polling;
234 #define ES_LOCK(sc) snd_mtxlock((sc)->lock)
235 #define ES_UNLOCK(sc) snd_mtxunlock((sc)->lock)
236 #define ES_LOCK_ASSERT(sc) snd_mtxassert((sc)->lock)
239 static void es_intr(void *);
240 static uint32_t es1371_wait_src_ready(struct es_info *);
241 static void es1371_src_write(struct es_info *,
242 unsigned short, unsigned short);
243 static unsigned int es1371_adc_rate(struct es_info *, unsigned int, int);
244 static unsigned int es1371_dac_rate(struct es_info *, unsigned int, int);
245 static int es1371_init(struct es_info *);
246 static int es1370_init(struct es_info *);
247 static int es1370_wrcodec(struct es_info *, unsigned char, unsigned char);
249 static uint32_t es_fmt[] = {
250 SND_FORMAT(AFMT_U8, 1, 0),
251 SND_FORMAT(AFMT_U8, 2, 0),
252 SND_FORMAT(AFMT_S16_LE, 1, 0),
253 SND_FORMAT(AFMT_S16_LE, 2, 0),
256 static struct pcmchan_caps es_caps = {4000, 48000, es_fmt, 0};
258 static const struct {
265 } mixtable[SOUND_MIXER_NRDEVICES] = {
266 [SOUND_MIXER_VOLUME] = { 0, 0x0, 0x1, 1, 0x1f7f, 1 },
267 [SOUND_MIXER_PCM] = { 1, 0x2, 0x3, 1, 0x0400, 1 },
268 [SOUND_MIXER_SYNTH] = { 2, 0x4, 0x5, 1, 0x0060, 1 },
269 [SOUND_MIXER_CD] = { 3, 0x6, 0x7, 1, 0x0006, 1 },
270 [SOUND_MIXER_LINE] = { 4, 0x8, 0x9, 1, 0x0018, 1 },
271 [SOUND_MIXER_LINE1] = { 5, 0xa, 0xb, 1, 0x1800, 1 },
272 [SOUND_MIXER_LINE2] = { 6, 0xc, 0x0, 0, 0x0100, 1 },
273 [SOUND_MIXER_LINE3] = { 7, 0xd, 0x0, 0, 0x0200, 1 },
274 [SOUND_MIXER_MIC] = { 8, 0xe, 0x0, 0, 0x0001, 1 },
275 [SOUND_MIXER_OGAIN] = { 9, 0xf, 0x0, 0, 0x0000, 1 }
278 static __inline uint32_t
279 es_rd(struct es_info *es, int regno, int size)
283 return (bus_space_read_1(es->st, es->sh, regno));
285 return (bus_space_read_2(es->st, es->sh, regno));
287 return (bus_space_read_4(es->st, es->sh, regno));
294 es_wr(struct es_info *es, int regno, uint32_t data, int size)
299 bus_space_write_1(es->st, es->sh, regno, data);
302 bus_space_write_2(es->st, es->sh, regno, data);
305 bus_space_write_4(es->st, es->sh, regno, data);
310 /* -------------------------------------------------------------------- */
311 /* The es1370 mixer interface */
314 es1370_mixinit(struct snd_mixer *m)
320 es = mix_getdevinfo(m);
322 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
323 if (mixtable[i].avail)
327 * Each DAC1/2 for ES1370 can be controlled independently
328 * DAC1 = controlled by synth
329 * DAC2 = controlled by pcm
330 * This is indeed can confuse user if DAC1 become primary playback
331 * channel. Try to be smart and combine both if necessary.
333 if (ES_SINGLE_PCM_MIX(es->escfg))
334 v &= ~(1 << SOUND_MIXER_SYNTH);
337 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
338 if (mixtable[i].recmask)
341 if (ES_SINGLE_PCM_MIX(es->escfg)) /* ditto */
342 v &= ~(1 << SOUND_MIXER_SYNTH);
343 mix_setrecdevs(m, v);
348 es1370_mixset(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
351 int l, r, rl, rr, set_dac1;
353 if (!mixtable[dev].avail)
356 r = (mixtable[dev].stereo) ? right : l;
357 if (mixtable[dev].left == 0xf)
358 rl = (l < 2) ? 0x80 : 7 - (l - 2) / 14;
360 rl = (l < 7) ? 0x80 : 31 - (l - 7) / 3;
361 es = mix_getdevinfo(m);
363 if (dev == SOUND_MIXER_PCM && (ES_SINGLE_PCM_MIX(es->escfg)) &&
364 ES_DAC1_ENABLED(es->escfg))
368 if (mixtable[dev].stereo) {
369 rr = (r < 7) ? 0x80 : 31 - (r - 7) / 3;
370 es1370_wrcodec(es, mixtable[dev].right, rr);
371 if (set_dac1 && mixtable[SOUND_MIXER_SYNTH].stereo)
373 mixtable[SOUND_MIXER_SYNTH].right, rr);
375 es1370_wrcodec(es, mixtable[dev].left, rl);
377 es1370_wrcodec(es, mixtable[SOUND_MIXER_SYNTH].left, rl);
380 return (l | (r << 8));
384 es1370_mixsetrecsrc(struct snd_mixer *m, uint32_t src)
389 es = mix_getdevinfo(m);
390 if (src == 0) src = 1 << SOUND_MIXER_MIC;
391 src &= mix_getrecdevs(m);
392 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
393 if ((src & (1 << i)) != 0) j |= mixtable[i].recmask;
396 if ((src & (1 << SOUND_MIXER_PCM)) && ES_SINGLE_PCM_MIX(es->escfg) &&
397 ES_DAC1_ENABLED(es->escfg))
398 j |= mixtable[SOUND_MIXER_SYNTH].recmask;
399 es1370_wrcodec(es, CODEC_LIMIX1, j & 0x55);
400 es1370_wrcodec(es, CODEC_RIMIX1, j & 0xaa);
401 es1370_wrcodec(es, CODEC_LIMIX2, (j >> 8) & 0x17);
402 es1370_wrcodec(es, CODEC_RIMIX2, (j >> 8) & 0x0f);
403 es1370_wrcodec(es, CODEC_OMIX1, 0x7f);
404 es1370_wrcodec(es, CODEC_OMIX2, 0x3f);
410 static kobj_method_t es1370_mixer_methods[] = {
411 KOBJMETHOD(mixer_init, es1370_mixinit),
412 KOBJMETHOD(mixer_set, es1370_mixset),
413 KOBJMETHOD(mixer_setrecsrc, es1370_mixsetrecsrc),
416 MIXER_DECLARE(es1370_mixer);
418 /* -------------------------------------------------------------------- */
421 es1370_wrcodec(struct es_info *es, unsigned char i, unsigned char data)
427 for (t = 0; t < 0x1000; t++) {
428 if ((es_rd(es, ES1370_REG_STATUS, 4) &
430 es_wr(es, ES1370_REG_CODEC,
431 ((unsigned short)i << CODEC_INDEX_SHIFT) | data, 2);
436 device_printf(es->dev, "%s: timed out\n", __func__);
440 /* -------------------------------------------------------------------- */
442 /* channel interface */
444 eschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
445 struct pcm_channel *c, int dir)
447 struct es_info *es = devinfo;
448 struct es_chinfo *ch;
453 if (dir == PCMDIR_PLAY) {
454 index = ES_GP(es->escfg);
455 es->escfg = ES_SET_GP(es->escfg, index + 1);
457 index = ES_DAC_FIRST(es->escfg);
459 index = ES_DAC_SECOND(es->escfg);
461 device_printf(es->dev,
462 "Invalid ES_GP index: %d\n", index);
466 if (!(index == ES_DAC1 || index == ES_DAC2)) {
467 device_printf(es->dev, "Unknown DAC: %d\n", index + 1);
471 if (es->ch[index].channel != NULL) {
472 device_printf(es->dev, "DAC%d already initialized!\n",
484 if (ES_IS_ES1370(es->escfg)) {
485 if (ch->index == ES_DAC1) {
486 ch->caps.maxspeed = ES1370_DAC1_MAXSPEED;
487 ch->caps.minspeed = ES1370_DAC1_MINSPEED;
489 uint32_t fixed_rate = ES_FIXED_RATE(es->escfg);
490 if (!(fixed_rate < es_caps.minspeed ||
491 fixed_rate > es_caps.maxspeed)) {
492 ch->caps.maxspeed = fixed_rate;
493 ch->caps.minspeed = fixed_rate;
500 ch->bufsz = es->bufsz;
501 ch->blkcnt = es->blkcnt;
502 ch->blksz = ch->bufsz / ch->blkcnt;
505 if (sndbuf_alloc(ch->buffer, es->parent_dmat, 0, ch->bufsz) != 0)
508 if (dir == PCMDIR_PLAY) {
509 if (ch->index == ES_DAC1) {
510 es_wr(es, ES1370_REG_MEMPAGE,
511 ES1370_REG_DAC1_FRAMEADR >> 8, 1);
512 es_wr(es, ES1370_REG_DAC1_FRAMEADR & 0xff,
513 sndbuf_getbufaddr(ch->buffer), 4);
514 es_wr(es, ES1370_REG_DAC1_FRAMECNT & 0xff,
515 (ch->bufsz >> 2) - 1, 4);
517 es_wr(es, ES1370_REG_MEMPAGE,
518 ES1370_REG_DAC2_FRAMEADR >> 8, 1);
519 es_wr(es, ES1370_REG_DAC2_FRAMEADR & 0xff,
520 sndbuf_getbufaddr(ch->buffer), 4);
521 es_wr(es, ES1370_REG_DAC2_FRAMECNT & 0xff,
522 (ch->bufsz >> 2) - 1, 4);
525 es_wr(es, ES1370_REG_MEMPAGE, ES1370_REG_ADC_FRAMEADR >> 8, 1);
526 es_wr(es, ES1370_REG_ADC_FRAMEADR & 0xff,
527 sndbuf_getbufaddr(ch->buffer), 4);
528 es_wr(es, ES1370_REG_ADC_FRAMECNT & 0xff,
529 (ch->bufsz >> 2) - 1, 4);
536 eschan_setformat(kobj_t obj, void *data, uint32_t format)
538 struct es_chinfo *ch = data;
539 struct es_info *es = ch->parent;
542 if (ch->dir == PCMDIR_PLAY) {
543 if (ch->index == ES_DAC1) {
544 es->sctrl &= ~SCTRL_P1FMT;
545 if (format & AFMT_S16_LE)
546 es->sctrl |= SCTRL_P1SEB;
547 if (AFMT_CHANNEL(format) > 1)
548 es->sctrl |= SCTRL_P1SMB;
550 es->sctrl &= ~SCTRL_P2FMT;
551 if (format & AFMT_S16_LE)
552 es->sctrl |= SCTRL_P2SEB;
553 if (AFMT_CHANNEL(format) > 1)
554 es->sctrl |= SCTRL_P2SMB;
557 es->sctrl &= ~SCTRL_R1FMT;
558 if (format & AFMT_S16_LE)
559 es->sctrl |= SCTRL_R1SEB;
560 if (AFMT_CHANNEL(format) > 1)
561 es->sctrl |= SCTRL_R1SMB;
563 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
570 eschan1370_setspeed(kobj_t obj, void *data, uint32_t speed)
572 struct es_chinfo *ch = data;
573 struct es_info *es = ch->parent;
576 /* Fixed rate , do nothing. */
577 if (ch->caps.minspeed == ch->caps.maxspeed) {
579 return (ch->caps.maxspeed);
581 if (speed < ch->caps.minspeed)
582 speed = ch->caps.minspeed;
583 if (speed > ch->caps.maxspeed)
584 speed = ch->caps.maxspeed;
585 if (ch->index == ES_DAC1) {
587 * DAC1 does not support continuous rate settings.
588 * Pick the nearest and use it since FEEDER_RATE will
589 * do the proper conversion for us.
591 es->ctrl &= ~CTRL_WTSRSEL;
594 es->ctrl |= 0 << CTRL_SH_WTSRSEL;
595 } else if (speed < 16537) {
597 es->ctrl |= 1 << CTRL_SH_WTSRSEL;
598 } else if (speed < 33075) {
600 es->ctrl |= 2 << CTRL_SH_WTSRSEL;
603 es->ctrl |= 3 << CTRL_SH_WTSRSEL;
606 es->ctrl &= ~CTRL_PCLKDIV;
607 es->ctrl |= DAC2_SRTODIV(speed) << CTRL_SH_PCLKDIV;
609 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
615 eschan1371_setspeed(kobj_t obj, void *data, uint32_t speed)
617 struct es_chinfo *ch = data;
618 struct es_info *es = ch->parent;
623 if (ch->dir == PCMDIR_PLAY)
624 i = es1371_dac_rate(es, speed, ch->index); /* play */
626 i = es1371_adc_rate(es, speed, ch->index); /* record */
628 delta = (speed > i) ? (speed - i) : (i - speed);
635 eschan_setfragments(kobj_t obj, void *data, uint32_t blksz, uint32_t blkcnt)
637 struct es_chinfo *ch = data;
638 struct es_info *es = ch->parent;
640 blksz &= ES_BLK_ALIGN;
642 if (blksz > (sndbuf_getmaxsize(ch->buffer) / ES_DMA_SEGS_MIN))
643 blksz = sndbuf_getmaxsize(ch->buffer) / ES_DMA_SEGS_MIN;
644 if (blksz < ES_BLK_MIN)
646 if (blkcnt > ES_DMA_SEGS_MAX)
647 blkcnt = ES_DMA_SEGS_MAX;
648 if (blkcnt < ES_DMA_SEGS_MIN)
649 blkcnt = ES_DMA_SEGS_MIN;
651 while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->buffer)) {
652 if ((blkcnt >> 1) >= ES_DMA_SEGS_MIN)
654 else if ((blksz >> 1) >= ES_BLK_MIN)
660 if ((sndbuf_getblksz(ch->buffer) != blksz ||
661 sndbuf_getblkcnt(ch->buffer) != blkcnt) &&
662 sndbuf_resize(ch->buffer, blkcnt, blksz) != 0)
663 device_printf(es->dev, "%s: failed blksz=%u blkcnt=%u\n",
664 __func__, blksz, blkcnt);
666 ch->bufsz = sndbuf_getsize(ch->buffer);
667 ch->blksz = sndbuf_getblksz(ch->buffer);
668 ch->blkcnt = sndbuf_getblkcnt(ch->buffer);
674 eschan_setblocksize(kobj_t obj, void *data, uint32_t blksz)
676 struct es_chinfo *ch = data;
677 struct es_info *es = ch->parent;
679 eschan_setfragments(obj, data, blksz, es->blkcnt);
684 #define es_chan_active(es) ((es)->ch[ES_DAC1].active + \
685 (es)->ch[ES_DAC2].active + \
686 (es)->ch[ES_ADC].active)
689 es_poll_channel(struct es_chinfo *ch)
695 if (ch == NULL || ch->channel == NULL || ch->active == 0)
699 if (ch->dir == PCMDIR_PLAY) {
700 if (ch->index == ES_DAC1)
701 reg = ES1370_REG_DAC1_FRAMECNT;
703 reg = ES1370_REG_DAC2_FRAMECNT;
705 reg = ES1370_REG_ADC_FRAMECNT;
706 sz = ch->blksz * ch->blkcnt;
707 es_wr(es, ES1370_REG_MEMPAGE, reg >> 8, 4);
708 ptr = es_rd(es, reg & 0x000000ff, 4) >> 16;
712 ptr &= ~(ch->blksz - 1);
713 delta = (sz + ptr - ch->prevptr) % sz;
715 if (delta < ch->blksz)
724 es_poll_callback(void *arg)
726 struct es_info *es = arg;
727 uint32_t trigger = 0;
734 if (es->polling == 0 || es_chan_active(es) == 0) {
739 for (i = 0; i < ES_NCHANS; i++) {
740 if (es_poll_channel(&es->ch[i]) != 0)
745 callout_reset(&es->poll_timer, 1/*es->poll_ticks*/,
746 es_poll_callback, es);
750 for (i = 0; i < ES_NCHANS; i++) {
751 if (trigger & (1 << i))
752 chn_intr(es->ch[i].channel);
757 eschan_trigger(kobj_t obj, void *data, int go)
759 struct es_chinfo *ch = data;
760 struct es_info *es = ch->parent;
763 if (!PCMTRIG_COMMON(go))
767 cnt = (ch->blksz / sndbuf_getalign(ch->buffer)) - 1;
768 if (ch->fmt & AFMT_16BIT)
770 if (AFMT_CHANNEL(ch->fmt) > 1)
772 if (ch->dir == PCMDIR_PLAY) {
773 if (go == PCMTRIG_START) {
774 if (ch->index == ES_DAC1) {
775 es->ctrl |= CTRL_DAC1_EN;
776 es->sctrl &= ~(SCTRL_P1LOOPSEL |
777 SCTRL_P1PAUSE | SCTRL_P1SCTRLD);
778 if (es->polling == 0)
779 es->sctrl |= SCTRL_P1INTEN;
781 es->sctrl &= ~SCTRL_P1INTEN;
783 es_wr(es, ES1370_REG_DAC1_SCOUNT, cnt, 4);
784 /* start at beginning of buffer */
785 es_wr(es, ES1370_REG_MEMPAGE,
786 ES1370_REG_DAC1_FRAMECNT >> 8, 4);
787 es_wr(es, ES1370_REG_DAC1_FRAMECNT & 0xff,
788 (ch->bufsz >> 2) - 1, 4);
790 es->ctrl |= CTRL_DAC2_EN;
791 es->sctrl &= ~(SCTRL_P2ENDINC | SCTRL_P2STINC |
792 SCTRL_P2LOOPSEL | SCTRL_P2PAUSE |
794 if (es->polling == 0)
795 es->sctrl |= SCTRL_P2INTEN;
797 es->sctrl &= ~SCTRL_P2INTEN;
798 es->sctrl |= (b << 2) |
799 ((((b >> 1) & 1) + 1) << SCTRL_SH_P2ENDINC);
800 es_wr(es, ES1370_REG_DAC2_SCOUNT, cnt, 4);
801 /* start at beginning of buffer */
802 es_wr(es, ES1370_REG_MEMPAGE,
803 ES1370_REG_DAC2_FRAMECNT >> 8, 4);
804 es_wr(es, ES1370_REG_DAC2_FRAMECNT & 0xff,
805 (ch->bufsz >> 2) - 1, 4);
808 es->ctrl &= ~((ch->index == ES_DAC1) ?
809 CTRL_DAC1_EN : CTRL_DAC2_EN);
811 if (go == PCMTRIG_START) {
812 es->ctrl |= CTRL_ADC_EN;
813 es->sctrl &= ~SCTRL_R1LOOPSEL;
814 if (es->polling == 0)
815 es->sctrl |= SCTRL_R1INTEN;
817 es->sctrl &= ~SCTRL_R1INTEN;
819 es_wr(es, ES1370_REG_ADC_SCOUNT, cnt, 4);
820 /* start at beginning of buffer */
821 es_wr(es, ES1370_REG_MEMPAGE,
822 ES1370_REG_ADC_FRAMECNT >> 8, 4);
823 es_wr(es, ES1370_REG_ADC_FRAMECNT & 0xff,
824 (ch->bufsz >> 2) - 1, 4);
826 es->ctrl &= ~CTRL_ADC_EN;
828 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
829 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
830 if (go == PCMTRIG_START) {
831 if (es->polling != 0) {
834 if (es_chan_active(es) == 0) {
836 callout_reset(&es->poll_timer, 1,
837 es_poll_callback, es);
843 if (es->polling != 0) {
844 if (es_chan_active(es) == 0) {
845 callout_stop(&es->poll_timer);
855 eschan_getptr(kobj_t obj, void *data)
857 struct es_chinfo *ch = data;
858 struct es_info *es = ch->parent;
862 if (es->polling != 0)
865 if (ch->dir == PCMDIR_PLAY) {
866 if (ch->index == ES_DAC1)
867 reg = ES1370_REG_DAC1_FRAMECNT;
869 reg = ES1370_REG_DAC2_FRAMECNT;
871 reg = ES1370_REG_ADC_FRAMECNT;
872 es_wr(es, ES1370_REG_MEMPAGE, reg >> 8, 4);
873 cnt = es_rd(es, reg & 0x000000ff, 4) >> 16;
874 /* cnt is longwords */
884 static struct pcmchan_caps *
885 eschan_getcaps(kobj_t obj, void *data)
887 struct es_chinfo *ch = data;
892 static kobj_method_t eschan1370_methods[] = {
893 KOBJMETHOD(channel_init, eschan_init),
894 KOBJMETHOD(channel_setformat, eschan_setformat),
895 KOBJMETHOD(channel_setspeed, eschan1370_setspeed),
896 KOBJMETHOD(channel_setblocksize, eschan_setblocksize),
897 KOBJMETHOD(channel_setfragments, eschan_setfragments),
898 KOBJMETHOD(channel_trigger, eschan_trigger),
899 KOBJMETHOD(channel_getptr, eschan_getptr),
900 KOBJMETHOD(channel_getcaps, eschan_getcaps),
903 CHANNEL_DECLARE(eschan1370);
905 static kobj_method_t eschan1371_methods[] = {
906 KOBJMETHOD(channel_init, eschan_init),
907 KOBJMETHOD(channel_setformat, eschan_setformat),
908 KOBJMETHOD(channel_setspeed, eschan1371_setspeed),
909 KOBJMETHOD(channel_setblocksize, eschan_setblocksize),
910 KOBJMETHOD(channel_setfragments, eschan_setfragments),
911 KOBJMETHOD(channel_trigger, eschan_trigger),
912 KOBJMETHOD(channel_getptr, eschan_getptr),
913 KOBJMETHOD(channel_getcaps, eschan_getcaps),
916 CHANNEL_DECLARE(eschan1371);
918 /* -------------------------------------------------------------------- */
919 /* The interrupt handler */
923 struct es_info *es = p;
924 uint32_t intsrc, sctrl;
927 if (es->polling != 0) {
931 intsrc = es_rd(es, ES1370_REG_STATUS, 4);
932 if ((intsrc & STAT_INTR) == 0) {
938 if (intsrc & STAT_ADC)
939 sctrl &= ~SCTRL_R1INTEN;
940 if (intsrc & STAT_DAC1)
941 sctrl &= ~SCTRL_P1INTEN;
942 if (intsrc & STAT_DAC2)
943 sctrl &= ~SCTRL_P2INTEN;
945 es_wr(es, ES1370_REG_SERIAL_CONTROL, sctrl, 4);
946 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
949 if (intsrc & STAT_ADC)
950 chn_intr(es->ch[ES_ADC].channel);
951 if (intsrc & STAT_DAC1)
952 chn_intr(es->ch[ES_DAC1].channel);
953 if (intsrc & STAT_DAC2)
954 chn_intr(es->ch[ES_DAC2].channel);
957 /* ES1370 specific */
959 es1370_init(struct es_info *es)
964 /* ES1370 default to fixed rate operation */
965 if (resource_int_value(device_get_name(es->dev),
966 device_get_unit(es->dev), "fixed_rate", &r) == 0) {
969 if (fixed_rate < es_caps.minspeed)
970 fixed_rate = es_caps.minspeed;
971 if (fixed_rate > es_caps.maxspeed)
972 fixed_rate = es_caps.maxspeed;
975 fixed_rate = es_caps.maxspeed;
977 if (resource_int_value(device_get_name(es->dev),
978 device_get_unit(es->dev), "single_pcm_mixer", &r) == 0)
979 single_pcm = (r != 0) ? 1 : 0;
984 if (ES_NUMPLAY(es->escfg) == 1)
987 es->escfg = ES_SET_IS_ES1370(es->escfg, 1);
989 es->escfg = ES_SET_FIXED_RATE(es->escfg, fixed_rate);
991 es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
992 fixed_rate = DSP_DEFAULT_SPEED;
995 es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 1);
997 es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 0);
998 es->ctrl = CTRL_CDC_EN | CTRL_JYSTK_EN | CTRL_SERR_DIS |
999 (DAC2_SRTODIV(fixed_rate) << CTRL_SH_PCLKDIV);
1000 es->ctrl |= 3 << CTRL_SH_WTSRSEL;
1001 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1004 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
1007 es1370_wrcodec(es, CODEC_RES_PD, 3);
1009 * CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off the LRCLK2 PLL;
1010 * program DAC_SYNC=0!
1012 es1370_wrcodec(es, CODEC_CSEL, 0);
1013 /* Recording source is mixer */
1014 es1370_wrcodec(es, CODEC_ADSEL, 0);
1015 /* MIC amp is 0db */
1016 es1370_wrcodec(es, CODEC_MGAIN, 0);
1022 /* ES1371 specific */
1024 es1371_init(struct es_info *es)
1026 uint32_t cssr, devid, revid, subdev;
1030 /* This is NOT ES1370 */
1031 es->escfg = ES_SET_IS_ES1370(es->escfg, 0);
1035 devid = pci_get_devid(es->dev);
1036 revid = pci_get_revid(es->dev);
1037 subdev = (pci_get_subdevice(es->dev) << 16) |
1038 pci_get_subvendor(es->dev);
1040 * Joyport blacklist. Either we're facing with broken hardware
1041 * or because this hardware need special (unknown) initialization
1045 case 0x20001274: /* old Ensoniq */
1049 es->ctrl = CTRL_JYSTK_EN;
1052 if (devid == CT4730_PCI_ID) {
1053 /* XXX amplifier hack? */
1054 es->ctrl |= (1 << 16);
1056 /* initialize the chips */
1057 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1058 es_wr(es, ES1370_REG_SERIAL_CONTROL, es->sctrl, 4);
1059 es_wr(es, ES1371_REG_LEGACY, 0, 4);
1060 if ((devid == ES1371_PCI_ID && revid == ES1371REV_ES1373_8) ||
1061 (devid == ES1371_PCI_ID && revid == ES1371REV_CT5880_A) ||
1062 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_C) ||
1063 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_D) ||
1064 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_E)) {
1066 es_wr(es, ES1370_REG_STATUS, cssr, 4);
1069 /* AC'97 warm reset to start the bitclk */
1070 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1071 es_wr(es, ES1371_REG_LEGACY, ES1371_SYNC_RES, 4);
1073 es_wr(es, ES1370_REG_CONTROL, es->sctrl, 4);
1074 es1371_wait_src_ready(es);
1075 /* Init the sample rate converter */
1076 es_wr(es, ES1371_REG_SMPRATE, ES1371_DIS_SRC, 4);
1077 for (idx = 0; idx < 0x80; idx++)
1078 es1371_src_write(es, idx, 0);
1079 es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
1080 es1371_src_write(es, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
1081 es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
1082 es1371_src_write(es, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
1083 es1371_src_write(es, ES_SMPREG_VOL_ADC, 1 << 12);
1084 es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, 1 << 12);
1085 es1371_src_write(es, ES_SMPREG_VOL_DAC1, 1 << 12);
1086 es1371_src_write(es, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
1087 es1371_src_write(es, ES_SMPREG_VOL_DAC2, 1 << 12);
1088 es1371_src_write(es, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
1089 es1371_adc_rate(es, 22050, ES_ADC);
1090 es1371_dac_rate(es, 22050, ES_DAC1);
1091 es1371_dac_rate(es, 22050, ES_DAC2);
1094 * enabling the sample rate converter without properly programming
1095 * its parameters causes the chip to lock up (the SRC busy bit will
1096 * be stuck high, and I've found no way to rectify this other than
1099 es1371_wait_src_ready(es);
1100 es_wr(es, ES1371_REG_SMPRATE, 0, 4);
1101 /* try to reset codec directly */
1102 es_wr(es, ES1371_REG_CODEC, 0, 4);
1103 es_wr(es, ES1370_REG_STATUS, cssr, 4);
1109 /* -------------------------------------------------------------------- */
1112 es1371_wrcd(kobj_t obj, void *s, int addr, uint32_t data)
1114 uint32_t t, x, orig;
1115 struct es_info *es = (struct es_info*)s;
1117 for (t = 0; t < 0x1000; t++) {
1118 if (!es_rd(es, ES1371_REG_CODEC & CODEC_WIP, 4))
1121 /* save the current state for later */
1122 x = orig = es_rd(es, ES1371_REG_SMPRATE, 4);
1123 /* enable SRC state data in SRC mux */
1124 es_wr(es, ES1371_REG_SMPRATE, (x & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1125 ES1371_DIS_P2 | ES1371_DIS_R1)) | 0x00010000, 4);
1127 for (t = 0; t < 0x1000; t++) {
1128 if ((es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1132 /* wait for a SAFE time to write addr/data and then do it, dammit */
1133 for (t = 0; t < 0x1000; t++) {
1134 if ((es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1139 es_wr(es, ES1371_REG_CODEC, ((addr << CODEC_POADD_SHIFT) &
1140 CODEC_POADD_MASK) | ((data << CODEC_PODAT_SHIFT) &
1141 CODEC_PODAT_MASK), 4);
1142 /* restore SRC reg */
1143 es1371_wait_src_ready(s);
1144 es_wr(es, ES1371_REG_SMPRATE, orig, 4);
1150 es1371_rdcd(kobj_t obj, void *s, int addr)
1152 uint32_t t, x, orig;
1153 struct es_info *es = (struct es_info *)s;
1155 for (t = 0; t < 0x1000; t++) {
1156 if (!(x = es_rd(es, ES1371_REG_CODEC, 4) & CODEC_WIP))
1160 /* save the current state for later */
1161 x = orig = es_rd(es, ES1371_REG_SMPRATE, 4);
1162 /* enable SRC state data in SRC mux */
1163 es_wr(es, ES1371_REG_SMPRATE, (x & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1164 ES1371_DIS_P2 | ES1371_DIS_R1)) | 0x00010000, 4);
1166 for (t = 0; t < 0x1000; t++) {
1167 if ((x = es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1171 /* wait for a SAFE time to write addr/data and then do it, dammit */
1172 for (t = 0; t < 0x1000; t++) {
1173 if ((x = es_rd(es, ES1371_REG_SMPRATE, 4) & 0x00870000) ==
1178 es_wr(es, ES1371_REG_CODEC, ((addr << CODEC_POADD_SHIFT) &
1179 CODEC_POADD_MASK) | CODEC_PORD, 4);
1181 /* restore SRC reg */
1182 es1371_wait_src_ready(s);
1183 es_wr(es, ES1371_REG_SMPRATE, orig, 4);
1185 /* now wait for the stinkin' data (RDY) */
1186 for (t = 0; t < 0x1000; t++) {
1187 if ((x = es_rd(es, ES1371_REG_CODEC, 4)) & CODEC_RDY)
1191 return ((x & CODEC_PIDAT_MASK) >> CODEC_PIDAT_SHIFT);
1194 static kobj_method_t es1371_ac97_methods[] = {
1195 KOBJMETHOD(ac97_read, es1371_rdcd),
1196 KOBJMETHOD(ac97_write, es1371_wrcd),
1199 AC97_DECLARE(es1371_ac97);
1201 /* -------------------------------------------------------------------- */
1204 es1371_src_read(struct es_info *es, unsigned short reg)
1208 r = es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1209 ES1371_DIS_P2 | ES1371_DIS_R1);
1210 r |= ES1371_SRC_RAM_ADDRO(reg);
1211 es_wr(es, ES1371_REG_SMPRATE, r, 4);
1212 return (ES1371_SRC_RAM_DATAI(es1371_wait_src_ready(es)));
1216 es1371_src_write(struct es_info *es, unsigned short reg, unsigned short data)
1220 r = es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1221 ES1371_DIS_P2 | ES1371_DIS_R1);
1222 r |= ES1371_SRC_RAM_ADDRO(reg) | ES1371_SRC_RAM_DATAO(data);
1223 es_wr(es, ES1371_REG_SMPRATE, r | ES1371_SRC_RAM_WE, 4);
1227 es1371_adc_rate(struct es_info *es, unsigned int rate, int set)
1229 unsigned int n, truncm, freq, result;
1238 if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
1240 truncm = (21 * n - 1) | 1;
1241 freq = ((48000UL << 15) / rate) * n;
1242 result = (48000UL << 15) / (freq / n);
1244 if (rate >= 24000) {
1247 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
1248 (((239 - truncm) >> 1) << 9) | (n << 4));
1252 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
1253 0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
1255 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
1256 (es1371_src_read(es, ES_SMPREG_ADC + ES_SMPREG_INT_REGS) &
1257 0x00ff) | ((freq >> 5) & 0xfc00));
1258 es1371_src_write(es, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC,
1260 es1371_src_write(es, ES_SMPREG_VOL_ADC, n << 8);
1261 es1371_src_write(es, ES_SMPREG_VOL_ADC + 1, n << 8);
1267 es1371_dac_rate(struct es_info *es, unsigned int rate, int set)
1269 unsigned int freq, r, result, dac, dis;
1277 freq = ((rate << 15) + 1500) / 3000;
1278 result = (freq * 3000) >> 15;
1280 dac = (set == ES_DAC1) ? ES_SMPREG_DAC1 : ES_SMPREG_DAC2;
1281 dis = (set == ES_DAC1) ? ES1371_DIS_P2 : ES1371_DIS_P1;
1282 r = (es1371_wait_src_ready(es) & (ES1371_DIS_SRC | ES1371_DIS_P1 |
1283 ES1371_DIS_P2 | ES1371_DIS_R1));
1284 es_wr(es, ES1371_REG_SMPRATE, r, 4);
1285 es1371_src_write(es, dac + ES_SMPREG_INT_REGS,
1286 (es1371_src_read(es, dac + ES_SMPREG_INT_REGS) & 0x00ff) |
1287 ((freq >> 5) & 0xfc00));
1288 es1371_src_write(es, dac + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
1289 r = (es1371_wait_src_ready(es) &
1290 (ES1371_DIS_SRC | dis | ES1371_DIS_R1));
1291 es_wr(es, ES1371_REG_SMPRATE, r, 4);
1296 es1371_wait_src_ready(struct es_info *es)
1300 for (t = 0; t < 0x1000; t++) {
1301 if (!((r = es_rd(es, ES1371_REG_SMPRATE, 4)) &
1302 ES1371_SRC_RAM_BUSY))
1306 device_printf(es->dev, "%s: timed out 0x%x [0x%x]\n", __func__,
1307 ES1371_REG_SMPRATE, r);
1311 /* -------------------------------------------------------------------- */
1314 * Probe and attach the card
1318 es_pci_probe(device_t dev)
1320 switch(pci_get_devid(dev)) {
1322 device_set_desc(dev, "AudioPCI ES1370");
1323 return (BUS_PROBE_DEFAULT);
1325 switch(pci_get_revid(dev)) {
1326 case ES1371REV_ES1371_A:
1327 device_set_desc(dev, "AudioPCI ES1371-A");
1328 return (BUS_PROBE_DEFAULT);
1329 case ES1371REV_ES1371_B:
1330 device_set_desc(dev, "AudioPCI ES1371-B");
1331 return (BUS_PROBE_DEFAULT);
1332 case ES1371REV_ES1373_A:
1333 device_set_desc(dev, "AudioPCI ES1373-A");
1334 return (BUS_PROBE_DEFAULT);
1335 case ES1371REV_ES1373_B:
1336 device_set_desc(dev, "AudioPCI ES1373-B");
1337 return (BUS_PROBE_DEFAULT);
1338 case ES1371REV_ES1373_8:
1339 device_set_desc(dev, "AudioPCI ES1373-8");
1340 return (BUS_PROBE_DEFAULT);
1341 case ES1371REV_CT5880_A:
1342 device_set_desc(dev, "Creative CT5880-A");
1343 return (BUS_PROBE_DEFAULT);
1345 device_set_desc(dev, "AudioPCI ES1371-?");
1347 "unknown revision %d -- please report to "
1348 "freebsd-multimedia@freebsd.org\n",
1349 pci_get_revid(dev));
1350 return (BUS_PROBE_DEFAULT);
1352 case ES1371_PCI_ID2:
1353 device_set_desc(dev, "Strange AudioPCI ES1371-? (vid=3274)");
1355 "unknown revision %d -- please report to "
1356 "freebsd-multimedia@freebsd.org\n", pci_get_revid(dev));
1357 return (BUS_PROBE_DEFAULT);
1359 switch(pci_get_revid(dev)) {
1360 case CT4730REV_CT4730_A:
1361 device_set_desc(dev,
1362 "Creative SB AudioPCI CT4730/EV1938");
1363 return (BUS_PROBE_DEFAULT);
1365 device_set_desc(dev, "Creative SB AudioPCI CT4730-?");
1367 "unknown revision %d -- please report to "
1368 "freebsd-multimedia@freebsd.org\n",
1369 pci_get_revid(dev));
1370 return (BUS_PROBE_DEFAULT);
1373 switch(pci_get_revid(dev)) {
1374 case CT5880REV_CT5880_C:
1375 device_set_desc(dev, "Creative CT5880-C");
1376 return (BUS_PROBE_DEFAULT);
1377 case CT5880REV_CT5880_D:
1378 device_set_desc(dev, "Creative CT5880-D");
1379 return (BUS_PROBE_DEFAULT);
1380 case CT5880REV_CT5880_E:
1381 device_set_desc(dev, "Creative CT5880-E");
1382 return (BUS_PROBE_DEFAULT);
1384 device_set_desc(dev, "Creative CT5880-?");
1386 "unknown revision %d -- please report to "
1387 "freebsd-multimedia@freebsd.org\n",
1388 pci_get_revid(dev));
1389 return (BUS_PROBE_DEFAULT);
1397 sysctl_es137x_spdif_enable(SYSCTL_HANDLER_ARGS)
1404 dev = oidp->oid_arg1;
1405 es = pcm_getdevinfo(dev);
1407 r = es_rd(es, ES1370_REG_STATUS, 4);
1409 new_en = (r & ENABLE_SPDIF) ? 1 : 0;
1410 err = sysctl_handle_int(oidp, &new_en, 0, req);
1412 if (err || req->newptr == NULL)
1414 if (new_en < 0 || new_en > 1)
1420 es->ctrl |= SPDIFEN_B;
1421 es->ctrl |= RECEN_B;
1424 es->ctrl &= ~SPDIFEN_B;
1425 es->ctrl &= ~RECEN_B;
1427 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1428 es_wr(es, ES1370_REG_STATUS, r, 4);
1435 sysctl_es137x_latency_timer(SYSCTL_HANDLER_ARGS)
1442 dev = oidp->oid_arg1;
1443 es = pcm_getdevinfo(dev);
1445 val = pci_read_config(dev, PCIR_LATTIMER, 1);
1447 err = sysctl_handle_int(oidp, &val, 0, req);
1449 if (err || req->newptr == NULL)
1455 pci_write_config(dev, PCIR_LATTIMER, val, 1);
1462 sysctl_es137x_fixed_rate(SYSCTL_HANDLER_ARGS)
1469 dev = oidp->oid_arg1;
1470 es = pcm_getdevinfo(dev);
1472 val = ES_FIXED_RATE(es->escfg);
1473 if (val < es_caps.minspeed)
1476 err = sysctl_handle_int(oidp, &val, 0, req);
1478 if (err || req->newptr == NULL)
1480 if (val != 0 && (val < es_caps.minspeed || val > es_caps.maxspeed))
1484 if (es->ctrl & (CTRL_DAC2_EN|CTRL_ADC_EN)) {
1489 if (val != ES_FIXED_RATE(es->escfg)) {
1490 es->escfg = ES_SET_FIXED_RATE(es->escfg, val);
1491 es->ch[ES_DAC2].caps.maxspeed = val;
1492 es->ch[ES_DAC2].caps.minspeed = val;
1493 es->ch[ES_ADC].caps.maxspeed = val;
1494 es->ch[ES_ADC].caps.minspeed = val;
1495 es->ctrl &= ~CTRL_PCLKDIV;
1496 es->ctrl |= DAC2_SRTODIV(val) << CTRL_SH_PCLKDIV;
1497 es_wr(es, ES1370_REG_CONTROL, es->ctrl, 4);
1500 es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
1501 es->ch[ES_DAC2].caps = es_caps;
1502 es->ch[ES_ADC].caps = es_caps;
1510 sysctl_es137x_single_pcm_mixer(SYSCTL_HANDLER_ARGS)
1513 struct snddev_info *d;
1514 struct snd_mixer *m;
1517 int recsrc, level, err;
1519 dev = oidp->oid_arg1;
1520 d = device_get_softc(dev);
1521 if (!PCM_REGISTERED(d) || d->mixer_dev == NULL ||
1522 d->mixer_dev->si_drv1 == NULL)
1528 set = ES_SINGLE_PCM_MIX(es->escfg);
1531 err = sysctl_handle_int(oidp, &val, 0, req);
1533 if (err || req->newptr == NULL)
1535 if (!(val == 0 || val == 1))
1539 PCM_ACQUIRE_QUICK(d);
1540 m = (d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 : NULL;
1542 PCM_RELEASE_QUICK(d);
1545 if (mixer_busy(m) != 0) {
1546 PCM_RELEASE_QUICK(d);
1549 level = mix_get(m, SOUND_MIXER_PCM);
1550 recsrc = mix_getrecsrc(m);
1551 if (level < 0 || recsrc < 0) {
1552 PCM_RELEASE_QUICK(d);
1557 if (es->ctrl & (CTRL_ADC_EN | CTRL_DAC1_EN | CTRL_DAC2_EN)) {
1559 PCM_RELEASE_QUICK(d);
1563 es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 1);
1565 es->escfg = ES_SET_SINGLE_PCM_MIX(es->escfg, 0);
1568 mix_setdevs(m, mix_getdevs(m) | (1 << SOUND_MIXER_SYNTH));
1569 mix_setrecdevs(m, mix_getrecdevs(m) | (1 << SOUND_MIXER_SYNTH));
1570 err = mix_set(m, SOUND_MIXER_SYNTH, level & 0x7f,
1571 (level >> 8) & 0x7f);
1573 err = mix_set(m, SOUND_MIXER_SYNTH, level & 0x7f,
1574 (level >> 8) & 0x7f);
1575 mix_setdevs(m, mix_getdevs(m) & ~(1 << SOUND_MIXER_SYNTH));
1576 mix_setrecdevs(m, mix_getrecdevs(m) &
1577 ~(1 << SOUND_MIXER_SYNTH));
1581 if (recsrc & (1 << SOUND_MIXER_PCM))
1582 recsrc |= 1 << SOUND_MIXER_SYNTH;
1583 else if (recsrc & (1 << SOUND_MIXER_SYNTH))
1584 recsrc |= 1 << SOUND_MIXER_PCM;
1585 if (level != recsrc)
1586 err = mix_setrecsrc(m, recsrc);
1589 PCM_RELEASE_QUICK(d);
1595 sysctl_es_polling(SYSCTL_HANDLER_ARGS)
1601 dev = oidp->oid_arg1;
1602 es = pcm_getdevinfo(dev);
1608 err = sysctl_handle_int(oidp, &val, 0, req);
1610 if (err || req->newptr == NULL)
1612 if (val < 0 || val > 1)
1616 if (val != es->polling) {
1617 if (es_chan_active(es) != 0)
1630 es_init_sysctls(device_t dev)
1633 int r, devid, revid;
1635 devid = pci_get_devid(dev);
1636 revid = pci_get_revid(dev);
1637 es = pcm_getdevinfo(dev);
1638 if ((devid == ES1371_PCI_ID && revid == ES1371REV_ES1373_8) ||
1639 (devid == ES1371_PCI_ID && revid == ES1371REV_CT5880_A) ||
1640 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_C) ||
1641 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_D) ||
1642 (devid == CT5880_PCI_ID && revid == CT5880REV_CT5880_E)) {
1643 /* XXX: an user should be able to set this with a control tool,
1644 if not done before 7.0-RELEASE, this needs to be converted
1645 to a device specific sysctl "dev.pcm.X.yyy" via
1646 device_get_sysctl_*() as discussed on multimedia@ in msg-id
1647 <861wujij2q.fsf@xps.des.no> */
1648 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1649 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1650 "spdif_enabled", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1651 dev, sizeof(dev), sysctl_es137x_spdif_enable, "I",
1652 "Enable S/PDIF output on primary playback channel");
1653 } else if (devid == ES1370_PCI_ID) {
1655 * Enable fixed rate sysctl if both DAC2 / ADC enabled.
1657 if (es->ch[ES_DAC2].channel != NULL &&
1658 es->ch[ES_ADC].channel != NULL) {
1659 /* XXX: an user should be able to set this with a control tool,
1660 if not done before 7.0-RELEASE, this needs to be converted
1661 to a device specific sysctl "dev.pcm.X.yyy" via
1662 device_get_sysctl_*() as discussed on multimedia@ in msg-id
1663 <861wujij2q.fsf@xps.des.no> */
1664 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1665 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1666 OID_AUTO, "fixed_rate",
1667 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1668 dev, sizeof(dev), sysctl_es137x_fixed_rate, "I",
1669 "Enable fixed rate playback/recording");
1672 * Enable single pcm mixer sysctl if both DAC1/2 enabled.
1674 if (es->ch[ES_DAC1].channel != NULL &&
1675 es->ch[ES_DAC2].channel != NULL) {
1676 /* XXX: an user should be able to set this with a control tool,
1677 if not done before 7.0-RELEASE, this needs to be converted
1678 to a device specific sysctl "dev.pcm.X.yyy" via
1679 device_get_sysctl_*() as discussed on multimedia@ in msg-id
1680 <861wujij2q.fsf@xps.des.no> */
1681 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1682 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1683 OID_AUTO, "single_pcm_mixer",
1684 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev,
1685 sizeof(dev), sysctl_es137x_single_pcm_mixer, "I",
1686 "Single PCM mixer controller for both DAC1/DAC2");
1689 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1690 "latency_timer", &r) == 0 && !(r < 0 || r > 255))
1691 pci_write_config(dev, PCIR_LATTIMER, r, 1);
1692 /* XXX: this needs to be converted to a device specific sysctl
1693 "dev.pcm.X.yyy" via device_get_sysctl_*() as discussed on
1694 multimedia@ in msg-id <861wujij2q.fsf@xps.des.no> */
1695 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1696 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1697 "latency_timer", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1698 dev, sizeof(dev), sysctl_es137x_latency_timer, "I",
1699 "PCI Latency Timer configuration");
1700 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1701 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1702 "polling", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev,
1703 sizeof(dev), sysctl_es_polling, "I", "Enable polling mode");
1707 es_pci_attach(device_t dev)
1709 struct es_info *es = NULL;
1710 int mapped, i, numplay, dac_cfg;
1711 char status[SND_STATUSLEN];
1712 struct ac97_info *codec = NULL;
1713 kobj_class_t ct = NULL;
1716 es = malloc(sizeof *es, M_DEVBUF, M_WAITOK | M_ZERO);
1717 es->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_es137x softc");
1722 pci_enable_busmaster(dev);
1724 es->regid = MEM_MAP_REG;
1725 es->regtype = SYS_RES_MEMORY;
1726 es->reg = bus_alloc_resource_any(dev, es->regtype, &es->regid,
1732 es->regid = PCIR_BAR(0);
1733 es->regtype = SYS_RES_IOPORT;
1734 es->reg = bus_alloc_resource_any(dev, es->regtype, &es->regid,
1740 device_printf(dev, "unable to map register space\n");
1744 es->st = rman_get_bustag(es->reg);
1745 es->sh = rman_get_bushandle(es->reg);
1746 callout_init(&es->poll_timer, 1);
1749 if (resource_int_value(device_get_name(dev),
1750 device_get_unit(dev), "polling", &i) == 0 && i != 0)
1755 es->bufsz = pcm_getbuffersize(dev, 4096, ES_DEFAULT_BUFSZ, 65536);
1756 if (resource_int_value(device_get_name(dev),
1757 device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
1761 es->blkcnt = es->bufsz / i;
1763 while (es->blkcnt >> i)
1765 es->blkcnt = 1 << (i - 1);
1766 if (es->blkcnt < ES_DMA_SEGS_MIN)
1767 es->blkcnt = ES_DMA_SEGS_MIN;
1768 else if (es->blkcnt > ES_DMA_SEGS_MAX)
1769 es->blkcnt = ES_DMA_SEGS_MAX;
1774 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1775 "dac", &dac_cfg) == 0) {
1776 if (dac_cfg < 0 || dac_cfg > 3)
1777 dac_cfg = ES_DEFAULT_DAC_CFG;
1779 dac_cfg = ES_DEFAULT_DAC_CFG;
1782 case 0: /* Enable all DAC: DAC1, DAC2 */
1784 es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC1);
1785 es->escfg = ES_SET_DAC_SECOND(es->escfg, ES_DAC2);
1787 case 1: /* Only DAC1 */
1789 es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC1);
1791 case 3: /* Enable all DAC / swap position: DAC2, DAC1 */
1793 es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC2);
1794 es->escfg = ES_SET_DAC_SECOND(es->escfg, ES_DAC1);
1796 case 2: /* Only DAC2 */
1799 es->escfg = ES_SET_DAC_FIRST(es->escfg, ES_DAC2);
1802 es->escfg = ES_SET_NUMPLAY(es->escfg, numplay);
1803 es->escfg = ES_SET_NUMREC(es->escfg, 1);
1805 devid = pci_get_devid(dev);
1808 case ES1371_PCI_ID2:
1812 codec = AC97_CREATE(dev, es, es1371_ac97);
1815 /* our init routine does everything for us */
1816 /* set to NULL; flag mixer_init not to run the ac97_init */
1817 /* ac97_mixer.init = NULL; */
1818 if (mixer_init(dev, ac97_getmixerclass(), codec))
1820 ct = &eschan1371_class;
1825 * Disable fixed rate operation if DAC2 disabled.
1826 * This is a special case for es1370 only, where the
1827 * speed of both ADC and DAC2 locked together.
1829 if (!ES_DAC2_ENABLED(es->escfg))
1830 es->escfg = ES_SET_FIXED_RATE(es->escfg, 0);
1831 if (mixer_init(dev, &es1370_mixer_class, es))
1833 ct = &eschan1370_class;
1841 es->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &es->irqid,
1842 RF_ACTIVE | RF_SHAREABLE);
1843 if (!es->irq || snd_setup_intr(dev, es->irq, INTR_MPSAFE, es_intr,
1845 device_printf(dev, "unable to map interrupt\n");
1849 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev),
1850 /*alignment*/2, /*boundary*/0,
1851 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1852 /*highaddr*/BUS_SPACE_MAXADDR,
1853 /*filter*/NULL, /*filterarg*/NULL,
1854 /*maxsize*/es->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
1855 /*flags*/0, /*lockfunc*/NULL,
1856 /*lockarg*/NULL, &es->parent_dmat) != 0) {
1857 device_printf(dev, "unable to create dma tag\n");
1861 snprintf(status, SND_STATUSLEN, "at %s 0x%jx irq %jd %s",
1862 (es->regtype == SYS_RES_IOPORT)? "io" : "memory",
1863 rman_get_start(es->reg), rman_get_start(es->irq),
1864 PCM_KLDSTRING(snd_es137x));
1866 if (pcm_register(dev, es, numplay, 1))
1868 for (i = 0; i < numplay; i++)
1869 pcm_addchan(dev, PCMDIR_PLAY, ct, es);
1870 pcm_addchan(dev, PCMDIR_REC, ct, es);
1871 es_init_sysctls(dev);
1872 pcm_setstatus(dev, status);
1873 es->escfg = ES_SET_GP(es->escfg, 0);
1875 device_printf(dev, "<Playback: DAC%d / Record: ADC>\n",
1876 ES_DAC_FIRST(es->escfg) + 1);
1877 else if (numplay == 2)
1878 device_printf(dev, "<Playback: DAC%d,DAC%d / Record: ADC>\n",
1879 ES_DAC_FIRST(es->escfg) + 1, ES_DAC_SECOND(es->escfg) + 1);
1883 if (es->parent_dmat)
1884 bus_dma_tag_destroy(es->parent_dmat);
1886 bus_teardown_intr(dev, es->irq, es->ih);
1888 bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq);
1890 ac97_destroy(codec);
1892 bus_release_resource(dev, es->regtype, es->regid, es->reg);
1894 snd_mtxfree(es->lock);
1901 es_pci_detach(device_t dev)
1906 r = pcm_unregister(dev);
1910 es = pcm_getdevinfo(dev);
1912 if (es != NULL && es->num != 0) {
1915 callout_stop(&es->poll_timer);
1917 callout_drain(&es->poll_timer);
1920 bus_teardown_intr(dev, es->irq, es->ih);
1921 bus_release_resource(dev, SYS_RES_IRQ, es->irqid, es->irq);
1922 bus_release_resource(dev, es->regtype, es->regid, es->reg);
1923 bus_dma_tag_destroy(es->parent_dmat);
1924 snd_mtxfree(es->lock);
1930 static device_method_t es_methods[] = {
1931 /* Device interface */
1932 DEVMETHOD(device_probe, es_pci_probe),
1933 DEVMETHOD(device_attach, es_pci_attach),
1934 DEVMETHOD(device_detach, es_pci_detach),
1938 static driver_t es_driver = {
1944 DRIVER_MODULE(snd_es137x, pci, es_driver, pcm_devclass, 0, 0);
1945 MODULE_DEPEND(snd_es137x, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1946 MODULE_VERSION(snd_es137x, 1);