The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/sound/isa/ess.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: d5e77abd1718b0fd42935ccef43cb2db


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.