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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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/6.4/sys/dev/sound/isa/ess.c 154545 2006-01-19 01:17:00Z ariff $");
   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 = {6000, 48000, 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 = {6000, 48000, 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                         ess_unlock(sc);
  366                         chn_intr(sc->pch.channel);
  367                         ess_lock(sc);
  368                 }
  369                 if (sc->pch.stopping) {
  370                         sc->pch.run = 0;
  371                         sndbuf_dma(sc->pch.buffer, PCMTRIG_STOP);
  372                         sc->pch.stopping = 0;
  373                         if (sc->pch.hwch == 1)
  374                                 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
  375                         else
  376                                 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03);
  377                 }
  378         }
  379 
  380         if (rirq) {
  381                 if (sc->rch.run) {
  382                         ess_unlock(sc);
  383                         chn_intr(sc->rch.channel);
  384                         ess_lock(sc);
  385                 }
  386                 if (sc->rch.stopping) {
  387                         sc->rch.run = 0;
  388                         sndbuf_dma(sc->rch.buffer, PCMTRIG_STOP);
  389                         sc->rch.stopping = 0;
  390                         /* XXX: will this stop audio2? */
  391                         ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
  392                 }
  393         }
  394 
  395         if (src & 2)
  396                 ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80);
  397         if (src & 1)
  398                 ess_rd(sc, DSP_DATA_AVAIL);
  399         ess_unlock(sc);
  400 }
  401 
  402 /* utility functions for ESS */
  403 static u_int8_t
  404 ess_calcspeed8(int *spd)
  405 {
  406         int speed = *spd;
  407         u_int32_t t;
  408 
  409         if (speed > 22000) {
  410                 t = (795500 + speed / 2) / speed;
  411                 speed = (795500 + t / 2) / t;
  412                 t = (256 - t) | 0x80;
  413         } else {
  414                 t = (397700 + speed / 2) / speed;
  415                 speed = (397700 + t / 2) / t;
  416                 t = 128 - t;
  417         }
  418         *spd = speed;
  419         return t & 0x000000ff;
  420 }
  421 
  422 static u_int8_t
  423 ess_calcspeed9(int *spd)
  424 {
  425         int speed, s0, s1, use0;
  426         u_int8_t t0, t1;
  427 
  428         /* rate = source / (256 - divisor) */
  429         /* divisor = 256 - (source / rate) */
  430         speed = *spd;
  431         t0 = 128 - (793800 / speed);
  432         s0 = 793800 / (128 - t0);
  433 
  434         t1 = 128 - (768000 / speed);
  435         s1 = 768000 / (128 - t1);
  436         t1 |= 0x80;
  437 
  438         use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0;
  439 
  440         *spd = use0? s0 : s1;
  441         return use0? t0 : t1;
  442 }
  443 
  444 static u_int8_t
  445 ess_calcfilter(int spd)
  446 {
  447         int cutoff;
  448 
  449         /* cutoff = 7160000 / (256 - divisor) */
  450         /* divisor = 256 - (7160000 / cutoff) */
  451         cutoff = (spd * 9 * 82) / 20;
  452         return (256 - (7160000 / cutoff));
  453 }
  454 
  455 static int
  456 ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
  457 {
  458         int play = (dir == PCMDIR_PLAY)? 1 : 0;
  459         int b16 = (fmt & AFMT_16BIT)? 1 : 0;
  460         int stereo = (fmt & AFMT_STEREO)? 1 : 0;
  461         int unsign = (fmt == AFMT_U8 || fmt == AFMT_U16_LE)? 1 : 0;
  462         u_int8_t spdval, fmtval;
  463 
  464 
  465         spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd);
  466         len = -len;
  467 
  468         if (ch == 1) {
  469                 KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad"));
  470                 /* transfer length low */
  471                 ess_write(sc, 0xa4, len & 0x00ff);
  472                 /* transfer length high */
  473                 ess_write(sc, 0xa5, (len & 0xff00) >> 8);
  474                 /* autoinit, dma dir */
  475                 ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a));
  476                 /* mono/stereo */
  477                 ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02));
  478                 /* demand mode, 4 bytes/xfer */
  479                 ess_write(sc, 0xb9, 0x02);
  480                 /* sample rate */
  481                 ess_write(sc, 0xa1, spdval);
  482                 /* filter cutoff */
  483                 ess_write(sc, 0xa2, ess_calcfilter(spd));
  484                 /* setup dac/adc */
  485                 if (play)
  486                         ess_write(sc, 0xb6, unsign? 0x80 : 0x00);
  487                 /* mono, b16: signed, load signal */
  488                 ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20));
  489                 /* setup fifo */
  490                 ess_write(sc, 0xb7, 0x90 | (unsign? 0x00 : 0x20) |
  491                                            (b16? 0x04 : 0x00) |
  492                                            (stereo? 0x08 : 0x40));
  493                 /* irq control */
  494                 ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50);
  495                 /* drq control */
  496                 ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50);
  497         } else if (ch == 2) {
  498                 KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad"));
  499                 /* transfer length low */
  500                 ess_setmixer(sc, 0x74, len & 0x00ff);
  501                 /* transfer length high */
  502                 ess_setmixer(sc, 0x76, (len & 0xff00) >> 8);
  503                 /* autoinit, 4 bytes/req */
  504                 ess_setmixer(sc, 0x78, 0x90);
  505                 fmtval = b16 | (stereo << 1) | (unsign << 2);
  506                 /* enable irq, set format */
  507                 ess_setmixer(sc, 0x7a, 0x40 | fmtval);
  508                 if (sc->newspeed) {
  509                         /* sample rate */
  510                         ess_setmixer(sc, 0x70, spdval);
  511                         /* filter cutoff */
  512                         ess_setmixer(sc, 0x72, ess_calcfilter(spd));
  513                 }
  514         }
  515 
  516         return 0;
  517 }
  518 static int
  519 ess_start(struct ess_chinfo *ch)
  520 {
  521         struct ess_info *sc = ch->parent;
  522         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
  523 
  524         ess_lock(sc);
  525         ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz);
  526         ch->stopping = 0;
  527         if (ch->hwch == 1)
  528                 ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01);
  529         else
  530                 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03);
  531         if (play)
  532                 ess_cmd(sc, DSP_CMD_SPKON);
  533         ess_unlock(sc);
  534         return 0;
  535 }
  536 
  537 static int
  538 ess_stop(struct ess_chinfo *ch)
  539 {
  540         struct ess_info *sc = ch->parent;
  541         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
  542 
  543         ess_lock(sc);
  544         ch->stopping = 1;
  545         if (ch->hwch == 1)
  546                 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04);
  547         else
  548                 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10);
  549         if (play)
  550                 ess_cmd(sc, DSP_CMD_SPKOFF);
  551         ess_unlock(sc);
  552         return 0;
  553 }
  554 
  555 /* -------------------------------------------------------------------- */
  556 /* channel interface for ESS18xx */
  557 static void *
  558 esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
  559 {
  560         struct ess_info *sc = devinfo;
  561         struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
  562 
  563         ch->parent = sc;
  564         ch->channel = c;
  565         ch->buffer = b;
  566         if (sndbuf_alloc(ch->buffer, sc->parent_dmat, sc->bufsize) != 0)
  567                 return NULL;
  568         ch->dir = dir;
  569         ch->hwch = 1;
  570         if ((dir == PCMDIR_PLAY) && (sc->duplex))
  571                 ch->hwch = 2;
  572         sndbuf_dmasetup(ch->buffer, (ch->hwch == 1)? sc->drq1 : sc->drq2);
  573         return ch;
  574 }
  575 
  576 static int
  577 esschan_setformat(kobj_t obj, void *data, u_int32_t format)
  578 {
  579         struct ess_chinfo *ch = data;
  580 
  581         ch->fmt = format;
  582         return 0;
  583 }
  584 
  585 static int
  586 esschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
  587 {
  588         struct ess_chinfo *ch = data;
  589         struct ess_info *sc = ch->parent;
  590 
  591         ch->spd = speed;
  592         if (sc->newspeed)
  593                 ess_calcspeed9(&ch->spd);
  594         else
  595                 ess_calcspeed8(&ch->spd);
  596         return ch->spd;
  597 }
  598 
  599 static int
  600 esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
  601 {
  602         struct ess_chinfo *ch = data;
  603 
  604         ch->blksz = blocksize;
  605         return ch->blksz;
  606 }
  607 
  608 static int
  609 esschan_trigger(kobj_t obj, void *data, int go)
  610 {
  611         struct ess_chinfo *ch = data;
  612 
  613         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
  614                 return 0;
  615 
  616         switch (go) {
  617         case PCMTRIG_START:
  618                 ch->run = 1;
  619                 sndbuf_dma(ch->buffer, go);
  620                 ess_start(ch);
  621                 break;
  622 
  623         case PCMTRIG_STOP:
  624         case PCMTRIG_ABORT:
  625         default:
  626                 ess_stop(ch);
  627                 break;
  628         }
  629         return 0;
  630 }
  631 
  632 static int
  633 esschan_getptr(kobj_t obj, void *data)
  634 {
  635         struct ess_chinfo *ch = data;
  636 
  637         return sndbuf_dmaptr(ch->buffer);
  638 }
  639 
  640 static struct pcmchan_caps *
  641 esschan_getcaps(kobj_t obj, void *data)
  642 {
  643         struct ess_chinfo *ch = data;
  644 
  645         return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
  646 }
  647 
  648 static kobj_method_t esschan_methods[] = {
  649         KOBJMETHOD(channel_init,                esschan_init),
  650         KOBJMETHOD(channel_setformat,           esschan_setformat),
  651         KOBJMETHOD(channel_setspeed,            esschan_setspeed),
  652         KOBJMETHOD(channel_setblocksize,        esschan_setblocksize),
  653         KOBJMETHOD(channel_trigger,             esschan_trigger),
  654         KOBJMETHOD(channel_getptr,              esschan_getptr),
  655         KOBJMETHOD(channel_getcaps,             esschan_getcaps),
  656         { 0, 0 }
  657 };
  658 CHANNEL_DECLARE(esschan);
  659 
  660 /************************************************************/
  661 
  662 static int
  663 essmix_init(struct snd_mixer *m)
  664 {
  665         struct ess_info *sc = mix_getdevinfo(m);
  666 
  667         mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE |
  668                           SOUND_MASK_IMIX);
  669 
  670         mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
  671                        SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME |
  672                        SOUND_MASK_LINE1 | SOUND_MASK_SPEAKER);
  673 
  674         ess_setmixer(sc, 0, 0); /* reset */
  675 
  676         return 0;
  677 }
  678 
  679 static int
  680 essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
  681 {
  682         struct ess_info *sc = mix_getdevinfo(m);
  683         int preg = 0, rreg = 0, l, r;
  684 
  685         l = (left * 15) / 100;
  686         r = (right * 15) / 100;
  687         switch (dev) {
  688         case SOUND_MIXER_SYNTH:
  689                 preg = 0x36;
  690                 rreg = 0x6b;
  691                 break;
  692 
  693         case SOUND_MIXER_PCM:
  694                 preg = 0x14;
  695                 rreg = 0x7c;
  696                 break;
  697 
  698         case SOUND_MIXER_LINE:
  699                 preg = 0x3e;
  700                 rreg = 0x6e;
  701                 break;
  702 
  703         case SOUND_MIXER_MIC:
  704                 preg = 0x1a;
  705                 rreg = 0x68;
  706                 break;
  707 
  708         case SOUND_MIXER_LINE1:
  709                 preg = 0x3a;
  710                 rreg = 0x6c;
  711                 break;
  712 
  713         case SOUND_MIXER_CD:
  714                 preg = 0x38;
  715                 rreg = 0x6a;
  716                 break;
  717 
  718         case SOUND_MIXER_SPEAKER:
  719                 preg = 0x3c;
  720                 break;
  721 
  722         case SOUND_MIXER_VOLUME:
  723                 l = left? (left * 63) / 100 : 64;
  724                 r = right? (right * 63) / 100 : 64;
  725                 ess_setmixer(sc, 0x60, l);
  726                 ess_setmixer(sc, 0x62, r);
  727                 left = (l == 64)? 0 : (l * 100) / 63;
  728                 right = (r == 64)? 0 : (r * 100) / 63;
  729                 return left | (right << 8);
  730         }
  731 
  732         if (preg)
  733                 ess_setmixer(sc, preg, (l << 4) | r);
  734         if (rreg)
  735                 ess_setmixer(sc, rreg, (l << 4) | r);
  736 
  737         left = (l * 100) / 15;
  738         right = (r * 100) / 15;
  739 
  740         return left | (right << 8);
  741 }
  742 
  743 static int
  744 essmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
  745 {
  746         struct ess_info *sc = mix_getdevinfo(m);
  747         u_char recdev;
  748 
  749         switch (src) {
  750         case SOUND_MASK_CD:
  751                 recdev = 0x02;
  752                 break;
  753 
  754         case SOUND_MASK_LINE:
  755                 recdev = 0x06;
  756                 break;
  757 
  758         case SOUND_MASK_IMIX:
  759                 recdev = 0x05;
  760                 break;
  761 
  762         case SOUND_MASK_MIC:
  763         default:
  764                 recdev = 0x00;
  765                 src = SOUND_MASK_MIC;
  766                 break;
  767         }
  768 
  769         ess_setmixer(sc, 0x1c, recdev);
  770 
  771         return src;
  772 }
  773 
  774 static kobj_method_t essmixer_methods[] = {
  775         KOBJMETHOD(mixer_init,          essmix_init),
  776         KOBJMETHOD(mixer_set,           essmix_set),
  777         KOBJMETHOD(mixer_setrecsrc,     essmix_setrecsrc),
  778         { 0, 0 }
  779 };
  780 MIXER_DECLARE(essmixer);
  781 
  782 /************************************************************/
  783 
  784 static int
  785 ess_probe(device_t dev)
  786 {
  787         uintptr_t func, ver, r, f;
  788 
  789         /* The parent device has already been probed. */
  790         r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
  791         if (func != SCF_PCM)
  792                 return (ENXIO);
  793 
  794         r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
  795         f = (ver & 0xffff0000) >> 16;
  796         if (!(f & BD_F_ESS))
  797                 return (ENXIO);
  798 
  799         device_set_desc(dev, "ESS 18xx DSP");
  800 
  801         return 0;
  802 }
  803 
  804 static int
  805 ess_attach(device_t dev)
  806 {
  807         struct ess_info *sc;
  808         char status[SND_STATUSLEN], buf[64];
  809         int ver;
  810 
  811         sc = (struct ess_info *)malloc(sizeof *sc, M_DEVBUF, M_NOWAIT | M_ZERO);
  812         if (!sc)
  813                 return ENXIO;
  814 
  815         sc->parent_dev = device_get_parent(dev);
  816         sc->bufsize = pcm_getbuffersize(dev, 4096, ESS_BUFFSIZE, 65536);
  817         if (ess_alloc_resources(sc, dev))
  818                 goto no;
  819         if (ess_reset_dsp(sc))
  820                 goto no;
  821         if (mixer_init(dev, &essmixer_class, sc))
  822                 goto no;
  823 
  824         sc->duplex = 0;
  825         sc->newspeed = 0;
  826         ver = (ess_getmixer(sc, 0x40) << 8) | ess_rd(sc, SB_MIX_DATA);
  827         snprintf(buf, sizeof buf, "ESS %x DSP", ver);
  828         device_set_desc_copy(dev, buf);
  829         if (bootverbose)
  830                 device_printf(dev, "ESS%x detected", ver);
  831 
  832         switch (ver) {
  833         case 0x1869:
  834         case 0x1879:
  835 #ifdef ESS18XX_DUPLEX
  836                 sc->duplex = sc->drq2? 1 : 0;
  837 #endif
  838 #ifdef ESS18XX_NEWSPEED
  839                 sc->newspeed = 1;
  840 #endif
  841                 break;
  842         }
  843         if (bootverbose)
  844                 printf("%s%s\n", sc->duplex? ", duplex" : "",
  845                                  sc->newspeed? ", newspeed" : "");
  846 
  847         if (sc->newspeed)
  848                 ess_setmixer(sc, 0x71, 0x22);
  849 
  850         snd_setup_intr(dev, sc->irq, 0, ess_intr, sc, &sc->ih);
  851         if (!sc->duplex)
  852                 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
  853 
  854         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
  855                         /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
  856                         /*highaddr*/BUS_SPACE_MAXADDR,
  857                         /*filter*/NULL, /*filterarg*/NULL,
  858                         /*maxsize*/sc->bufsize, /*nsegments*/1,
  859                         /*maxsegz*/0x3ffff,
  860                         /*flags*/0, /*lockfunc*/busdma_lock_mutex,
  861                         /*lockarg*/&Giant, &sc->parent_dmat) != 0) {
  862                 device_printf(dev, "unable to create dma tag\n");
  863                 goto no;
  864         }
  865 
  866         if (sc->drq2)
  867                 snprintf(buf, SND_STATUSLEN, ":%ld", rman_get_start(sc->drq2));
  868         else
  869                 buf[0] = '\0';
  870 
  871         snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld%s bufsz %u %s",
  872                 rman_get_start(sc->io_base), rman_get_start(sc->irq),
  873                 rman_get_start(sc->drq1), buf, sc->bufsize,
  874                 PCM_KLDSTRING(snd_ess));
  875 
  876         if (pcm_register(dev, sc, 1, 1))
  877                 goto no;
  878         pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc);
  879         pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc);
  880         pcm_setstatus(dev, status);
  881 
  882         return 0;
  883 
  884 no:
  885         ess_release_resources(sc, dev);
  886         return ENXIO;
  887 }
  888 
  889 static int
  890 ess_detach(device_t dev)
  891 {
  892         int r;
  893         struct ess_info *sc;
  894 
  895         r = pcm_unregister(dev);
  896         if (r)
  897                 return r;
  898 
  899         sc = pcm_getdevinfo(dev);
  900         ess_release_resources(sc, dev);
  901         return 0;
  902 }
  903 
  904 static int
  905 ess_resume(device_t dev)
  906 {
  907         struct ess_info *sc;
  908 
  909         sc = pcm_getdevinfo(dev);
  910 
  911         if (ess_reset_dsp(sc)) {
  912                 device_printf(dev, "unable to reset DSP at resume\n");
  913                 return ENXIO;
  914         }
  915 
  916         if (mixer_reinit(dev)) {
  917                 device_printf(dev, "unable to reinitialize mixer at resume\n");
  918                 return ENXIO;
  919         }
  920 
  921         return 0;
  922 }
  923 
  924 static device_method_t ess_methods[] = {
  925         /* Device interface */
  926         DEVMETHOD(device_probe,         ess_probe),
  927         DEVMETHOD(device_attach,        ess_attach),
  928         DEVMETHOD(device_detach,        ess_detach),
  929         DEVMETHOD(device_resume,        ess_resume),
  930 
  931         { 0, 0 }
  932 };
  933 
  934 static driver_t ess_driver = {
  935         "pcm",
  936         ess_methods,
  937         PCM_SOFTC_SIZE,
  938 };
  939 
  940 DRIVER_MODULE(snd_ess, sbc, ess_driver, pcm_devclass, 0, 0);
  941 MODULE_DEPEND(snd_ess, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
  942 MODULE_DEPEND(snd_ess, snd_sbc, 1, 1, 1);
  943 MODULE_VERSION(snd_ess, 1);
  944 
  945 /************************************************************/
  946 
  947 static devclass_t esscontrol_devclass;
  948 
  949 static struct isa_pnp_id essc_ids[] = {
  950         {0x06007316, "ESS Control"},
  951         {0}
  952 };
  953 
  954 static int
  955 esscontrol_probe(device_t dev)
  956 {
  957         int i;
  958 
  959         i = ISA_PNP_PROBE(device_get_parent(dev), dev, essc_ids);
  960         if (i == 0)
  961                 device_quiet(dev);
  962         return i;
  963 }
  964 
  965 static int
  966 esscontrol_attach(device_t dev)
  967 {
  968 #ifdef notyet
  969         struct resource *io;
  970         int rid, i, x;
  971 
  972         rid = 0;
  973         io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
  974         x = 0;
  975         for (i = 0; i < 0x100; i++) {
  976                 port_wr(io, 0, i);
  977                 x = port_rd(io, 1);
  978                 if ((i & 0x0f) == 0)
  979                         printf("%3.3x: ", i);
  980                 printf("%2.2x ", x);
  981                 if ((i & 0x0f) == 0x0f)
  982                         printf("\n");
  983         }
  984         bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
  985         io = NULL;
  986 #endif
  987 
  988         return 0;
  989 }
  990 
  991 static int
  992 esscontrol_detach(device_t dev)
  993 {
  994         return 0;
  995 }
  996 
  997 static device_method_t esscontrol_methods[] = {
  998         /* Device interface */
  999         DEVMETHOD(device_probe,         esscontrol_probe),
 1000         DEVMETHOD(device_attach,        esscontrol_attach),
 1001         DEVMETHOD(device_detach,        esscontrol_detach),
 1002 
 1003         { 0, 0 }
 1004 };
 1005 
 1006 static driver_t esscontrol_driver = {
 1007         "esscontrol",
 1008         esscontrol_methods,
 1009         1,
 1010 };
 1011 
 1012 DRIVER_MODULE(esscontrol, isa, esscontrol_driver, esscontrol_devclass, 0, 0);
 1013 DRIVER_MODULE(esscontrol, acpi, esscontrol_driver, esscontrol_devclass, 0, 0);

Cache object: 47456ef6d7ef77b83b5cfb92b244d719


[ 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.