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/sbc.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 Seigo Tanimura
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <dev/sound/chip.h>
   28 #include <dev/sound/pcm/sound.h>
   29 #include <dev/sound/isa/sb.h>
   30 
   31 #include <isa/isavar.h>
   32 
   33 SND_DECLARE_FILE("$FreeBSD: releng/5.1/sys/dev/sound/isa/sbc.c 110499 2003-02-07 14:05:34Z nyan $");
   34 
   35 #define IO_MAX  3
   36 #define IRQ_MAX 1
   37 #define DRQ_MAX 2
   38 #define INTR_MAX        2
   39 
   40 struct sbc_softc;
   41 
   42 struct sbc_ihl {
   43         driver_intr_t *intr[INTR_MAX];
   44         void *intr_arg[INTR_MAX];
   45         struct sbc_softc *parent;
   46 };
   47 
   48 /* Here is the parameter structure per a device. */
   49 struct sbc_softc {
   50         device_t dev; /* device */
   51         device_t child_pcm, child_midi1, child_midi2;
   52 
   53         int io_rid[IO_MAX]; /* io port rids */
   54         struct resource *io[IO_MAX]; /* io port resources */
   55         int io_alloced[IO_MAX]; /* io port alloc flag */
   56 
   57         int irq_rid[IRQ_MAX]; /* irq rids */
   58         struct resource *irq[IRQ_MAX]; /* irq resources */
   59         int irq_alloced[IRQ_MAX]; /* irq alloc flag */
   60 
   61         int drq_rid[DRQ_MAX]; /* drq rids */
   62         struct resource *drq[DRQ_MAX]; /* drq resources */
   63         int drq_alloced[DRQ_MAX]; /* drq alloc flag */
   64 
   65         struct sbc_ihl ihl[IRQ_MAX];
   66 
   67         void *ih[IRQ_MAX];
   68 
   69         struct mtx *lock;
   70 
   71         u_int32_t bd_ver;
   72 };
   73 
   74 static int sbc_probe(device_t dev);
   75 static int sbc_attach(device_t dev);
   76 static void sbc_intr(void *p);
   77 
   78 static struct resource *sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
   79                                            u_long start, u_long end, u_long count, u_int flags);
   80 static int sbc_release_resource(device_t bus, device_t child, int type, int rid,
   81                                 struct resource *r);
   82 static int sbc_setup_intr(device_t dev, device_t child, struct resource *irq,
   83                int flags, driver_intr_t *intr, void *arg,
   84                void **cookiep);
   85 static int sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
   86                   void *cookie);
   87 
   88 static int alloc_resource(struct sbc_softc *scp);
   89 static int release_resource(struct sbc_softc *scp);
   90 
   91 static devclass_t sbc_devclass;
   92 
   93 static int io_range[3] = {0x10, 0x2, 0x4};
   94 
   95 #ifdef PC98 /* I/O address table for PC98 */
   96 static bus_addr_t pcm_iat[] = {
   97         0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700,
   98         0x800, 0x900, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00
   99 };
  100 static bus_addr_t midi_iat[] = {
  101         0x000, 0x100
  102 };
  103 static bus_addr_t opl_iat[] = {
  104         0x000, 0x100, 0x200, 0x300
  105 };
  106 static bus_addr_t *sb_iat[] = { pcm_iat, midi_iat, opl_iat };
  107 #endif
  108 
  109 static int sb_rd(struct resource *io, int reg);
  110 static void sb_wr(struct resource *io, int reg, u_int8_t val);
  111 static int sb_dspready(struct resource *io);
  112 static int sb_cmd(struct resource *io, u_char val);
  113 static u_int sb_get_byte(struct resource *io);
  114 static void sb_setmixer(struct resource *io, u_int port, u_int value);
  115 
  116 static void
  117 sbc_lockinit(struct sbc_softc *scp)
  118 {
  119         scp->lock = snd_mtxcreate(device_get_nameunit(scp->dev), "sound softc");
  120 }
  121 
  122 static void
  123 sbc_lockdestroy(struct sbc_softc *scp)
  124 {
  125         snd_mtxfree(scp->lock);
  126 }
  127 
  128 void
  129 sbc_lock(struct sbc_softc *scp)
  130 {
  131         snd_mtxlock(scp->lock);
  132 }
  133 
  134 void
  135 sbc_unlock(struct sbc_softc *scp)
  136 {
  137         snd_mtxunlock(scp->lock);
  138 }
  139 
  140 static int
  141 sb_rd(struct resource *io, int reg)
  142 {
  143         return bus_space_read_1(rman_get_bustag(io),
  144                                 rman_get_bushandle(io),
  145                                 reg);
  146 }
  147 
  148 static void
  149 sb_wr(struct resource *io, int reg, u_int8_t val)
  150 {
  151         bus_space_write_1(rman_get_bustag(io),
  152                           rman_get_bushandle(io),
  153                           reg, val);
  154 }
  155 
  156 static int
  157 sb_dspready(struct resource *io)
  158 {
  159         return ((sb_rd(io, SBDSP_STATUS) & 0x80) == 0);
  160 }
  161 
  162 static int
  163 sb_dspwr(struct resource *io, u_char val)
  164 {
  165         int  i;
  166 
  167         for (i = 0; i < 1000; i++) {
  168                 if (sb_dspready(io)) {
  169                         sb_wr(io, SBDSP_CMD, val);
  170                         return 1;
  171                 }
  172                 if (i > 10) DELAY((i > 100)? 1000 : 10);
  173         }
  174         printf("sb_dspwr(0x%02x) timed out.\n", val);
  175         return 0;
  176 }
  177 
  178 static int
  179 sb_cmd(struct resource *io, u_char val)
  180 {
  181         return sb_dspwr(io, val);
  182 }
  183 
  184 static void
  185 sb_setmixer(struct resource *io, u_int port, u_int value)
  186 {
  187         u_long   flags;
  188 
  189         flags = spltty();
  190         sb_wr(io, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
  191         DELAY(10);
  192         sb_wr(io, SB_MIX_DATA, (u_char) (value & 0xff));
  193         DELAY(10);
  194         splx(flags);
  195 }
  196 
  197 static u_int
  198 sb_get_byte(struct resource *io)
  199 {
  200         int i;
  201 
  202         for (i = 1000; i > 0; i--) {
  203                 if (sb_rd(io, DSP_DATA_AVAIL) & 0x80)
  204                         return sb_rd(io, DSP_READ);
  205                 else
  206                         DELAY(20);
  207         }
  208         return 0xffff;
  209 }
  210 
  211 static int
  212 sb_reset_dsp(struct resource *io)
  213 {
  214         sb_wr(io, SBDSP_RST, 3);
  215         DELAY(100);
  216         sb_wr(io, SBDSP_RST, 0);
  217         return (sb_get_byte(io) == 0xAA)? 0 : ENXIO;
  218 }
  219 
  220 static int
  221 sb_identify_board(struct resource *io)
  222 {
  223         int ver, essver, rev;
  224 
  225         sb_cmd(io, DSP_CMD_GETVER);     /* Get version */
  226         ver = (sb_get_byte(io) << 8) | sb_get_byte(io);
  227         if (ver < 0x100 || ver > 0x4ff) return 0;
  228         if (ver == 0x0301) {
  229                 /* Try to detect ESS chips. */
  230                 sb_cmd(io, DSP_CMD_GETID); /* Return ident. bytes. */
  231                 essver = (sb_get_byte(io) << 8) | sb_get_byte(io);
  232                 rev = essver & 0x000f;
  233                 essver &= 0xfff0;
  234                 if (essver == 0x4880) ver |= 0x1000;
  235                 else if (essver == 0x6880) ver = 0x0500 | rev;
  236         }
  237         return ver;
  238 }
  239 
  240 static struct isa_pnp_id sbc_ids[] = {
  241         {0x01008c0e, "Creative ViBRA16C"},              /* CTL0001 */
  242         {0x31008c0e, "Creative SB16/SB32"},             /* CTL0031 */
  243         {0x41008c0e, "Creative SB16/SB32"},             /* CTL0041 */
  244         {0x42008c0e, "Creative SB AWE64"},              /* CTL0042 */
  245         {0x43008c0e, "Creative ViBRA16X"},              /* CTL0043 */
  246         {0x44008c0e, "Creative SB AWE64 Gold"},         /* CTL0044 */
  247         {0x45008c0e, "Creative SB AWE64"},              /* CTL0045 */
  248         {0x46008c0e, "Creative SB AWE64"},              /* CTL0046 */
  249 
  250         {0x01000000, "Avance Logic ALS100+"},           /* @@@0001 - ViBRA16X clone */
  251         {0x01100000, "Avance Asound 110"},              /* @@@1001 */
  252         {0x01200000, "Avance Logic ALS120"},            /* @@@2001 - ViBRA16X clone */
  253 
  254         {0x81167316, "ESS ES1681"},                     /* ESS1681 */
  255         {0x02017316, "ESS ES1688"},                     /* ESS1688 */
  256         {0x68187316, "ESS ES1868"},                     /* ESS1868 */
  257         {0x03007316, "ESS ES1869"},                     /* ESS1869 */
  258         {0x69187316, "ESS ES1869"},                     /* ESS1869 */
  259         {0xabb0110e, "ESS ES1869 (Compaq OEM)"},        /* CPQb0ab */
  260         {0xacb0110e, "ESS ES1869 (Compaq OEM)"},        /* CPQb0ac */
  261         {0x78187316, "ESS ES1878"},                     /* ESS1878 */
  262         {0x79187316, "ESS ES1879"},                     /* ESS1879 */
  263         {0x88187316, "ESS ES1888"},                     /* ESS1888 */
  264         {0x07017316, "ESS ES1888 (DEC OEM)"},           /* ESS0107 */
  265         {0x06017316, "ESS ES1888 (Dell OEM)"},          /* ESS0106 */
  266         {0}
  267 };
  268 
  269 static int
  270 sbc_probe(device_t dev)
  271 {
  272         char *s = NULL;
  273         u_int32_t lid, vid;
  274 
  275         lid = isa_get_logicalid(dev);
  276         vid = isa_get_vendorid(dev);
  277         if (lid) {
  278                 if (lid == 0x01000000 && vid != 0x01009305) /* ALS0001 */
  279                         return ENXIO;
  280                 /* Check pnp ids */
  281                 return ISA_PNP_PROBE(device_get_parent(dev), dev, sbc_ids);
  282         } else {
  283                 int rid = 0, ver;
  284                 struct resource *io;
  285 
  286 #ifdef PC98
  287                 io = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid,
  288                                          pcm_iat, 16, RF_ACTIVE);
  289 #else
  290                 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
  291                                         0, ~0, 16, RF_ACTIVE);
  292 #endif
  293                 if (!io) goto bad;
  294 #ifdef PC98
  295                 isa_load_resourcev(io, pcm_iat, 16);
  296 #endif
  297                 if (sb_reset_dsp(io)) goto bad2;
  298                 ver = sb_identify_board(io);
  299                 if (ver == 0) goto bad2;
  300                 switch ((ver & 0x00000f00) >> 8) {
  301                 case 1:
  302                         device_set_desc(dev, "SoundBlaster 1.0 (not supported)");
  303                         s = NULL;
  304                         break;
  305 
  306                 case 2:
  307                         s = "SoundBlaster 2.0";
  308                         break;
  309 
  310                 case 3:
  311                         s = (ver & 0x0000f000)? "ESS 488" : "SoundBlaster Pro";
  312                         break;
  313 
  314                 case 4:
  315                         s = "SoundBlaster 16";
  316                         break;
  317 
  318                 case 5:
  319                         s = (ver & 0x00000008)? "ESS 688" : "ESS 1688";
  320                         break;
  321                 }
  322                 if (s) device_set_desc(dev, s);
  323 bad2:           bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
  324 bad:            return s? 0 : ENXIO;
  325         }
  326 }
  327 
  328 static int
  329 sbc_attach(device_t dev)
  330 {
  331         char *err = NULL;
  332         struct sbc_softc *scp;
  333         struct sndcard_func *func;
  334         u_int32_t logical_id = isa_get_logicalid(dev);
  335         int flags = device_get_flags(dev);
  336         int f, dh, dl, x, irq, i;
  337 
  338         if (!logical_id && (flags & DV_F_DUAL_DMA)) {
  339                 bus_set_resource(dev, SYS_RES_DRQ, 1,
  340                                  flags & DV_F_DRQ_MASK, 1);
  341         }
  342 
  343         scp = device_get_softc(dev);
  344         bzero(scp, sizeof(*scp));
  345         scp->dev = dev;
  346         sbc_lockinit(scp);
  347         err = "alloc_resource";
  348         if (alloc_resource(scp)) goto bad;
  349 
  350         err = "sb_reset_dsp";
  351         if (sb_reset_dsp(scp->io[0])) goto bad;
  352         err = "sb_identify_board";
  353         scp->bd_ver = sb_identify_board(scp->io[0]) & 0x00000fff;
  354         if (scp->bd_ver == 0) goto bad;
  355         f = 0;
  356         if (logical_id == 0x01200000 && scp->bd_ver < 0x0400) scp->bd_ver = 0x0499;
  357         switch ((scp->bd_ver & 0x0f00) >> 8) {
  358         case 1: /* old sound blaster has nothing... */
  359                 break;
  360 
  361         case 2:
  362                 f |= BD_F_DUP_MIDI;
  363                 if (scp->bd_ver > 0x200) f |= BD_F_MIX_CT1335;
  364                 break;
  365 
  366         case 5:
  367                 f |= BD_F_ESS;
  368                 scp->bd_ver = 0x0301;
  369         case 3:
  370                 f |= BD_F_DUP_MIDI | BD_F_MIX_CT1345;
  371                 break;
  372 
  373         case 4:
  374                 f |= BD_F_SB16 | BD_F_MIX_CT1745;
  375                 if (scp->drq[0]) dl = rman_get_start(scp->drq[0]); else dl = -1;
  376                 if (scp->drq[1]) dh = rman_get_start(scp->drq[1]); else dh = dl;
  377                 if (!logical_id && (dh < dl)) {
  378                         struct resource *r;
  379                         r = scp->drq[0];
  380                         scp->drq[0] = scp->drq[1];
  381                         scp->drq[1] = r;
  382                         dl = rman_get_start(scp->drq[0]);
  383                         dh = rman_get_start(scp->drq[1]);
  384                 }
  385                 /* soft irq/dma configuration */
  386                 x = -1;
  387                 irq = rman_get_start(scp->irq[0]);
  388 #ifdef PC98
  389                 /* SB16 in PC98 use different IRQ table */
  390                 if      (irq == 3) x = 1;
  391                 else if (irq == 5) x = 8;
  392                 else if (irq == 10) x = 2;
  393                 else if (irq == 12) x = 4;
  394                 if (x == -1) {
  395                         err = "bad irq (3/5/10/12 valid)";
  396                         goto bad;
  397                 }
  398                 else sb_setmixer(scp->io[0], IRQ_NR, x);
  399                 /* SB16 in PC98 use different dma setting */
  400                 sb_setmixer(scp->io[0], DMA_NR, dh == 0 ? 1 : 2);
  401 #else
  402                 if      (irq == 5) x = 2;
  403                 else if (irq == 7) x = 4;
  404                 else if (irq == 9) x = 1;
  405                 else if (irq == 10) x = 8;
  406                 if (x == -1) {
  407                         err = "bad irq (5/7/9/10 valid)";
  408                         goto bad;
  409                 }
  410                 else sb_setmixer(scp->io[0], IRQ_NR, x);
  411                 sb_setmixer(scp->io[0], DMA_NR, (1 << dh) | (1 << dl));
  412 #endif
  413                 if (bootverbose) {
  414                         device_printf(dev, "setting card to irq %d, drq %d", irq, dl);
  415                         if (dl != dh) printf(", %d", dh);
  416                         printf("\n");
  417                 }
  418                 break;
  419         }
  420 
  421         switch (logical_id) {
  422         case 0x43008c0e:        /* CTL0043 */
  423         case 0x01200000:
  424         case 0x01000000:
  425                 f |= BD_F_SB16X;
  426                 break;
  427         }
  428         scp->bd_ver |= f << 16;
  429 
  430         err = "setup_intr";
  431         for (i = 0; i < IRQ_MAX; i++) {
  432                 scp->ihl[i].parent = scp;
  433                 if (snd_setup_intr(dev, scp->irq[i], INTR_MPSAFE, sbc_intr, &scp->ihl[i], &scp->ih[i]))
  434                         goto bad;
  435         }
  436 
  437         /* PCM Audio */
  438         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
  439         if (func == NULL) goto bad;
  440         func->func = SCF_PCM;
  441         scp->child_pcm = device_add_child(dev, "pcm", -1);
  442         device_set_ivars(scp->child_pcm, func);
  443 
  444         /* Midi Interface */
  445         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
  446         if (func == NULL) goto bad;
  447         func->func = SCF_MIDI;
  448         scp->child_midi1 = device_add_child(dev, "midi", -1);
  449         device_set_ivars(scp->child_midi1, func);
  450 
  451         /* OPL FM Synthesizer */
  452         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
  453         if (func == NULL) goto bad;
  454         func->func = SCF_SYNTH;
  455         scp->child_midi2 = device_add_child(dev, "midi", -1);
  456         device_set_ivars(scp->child_midi2, func);
  457 
  458         /* probe/attach kids */
  459         bus_generic_attach(dev);
  460 
  461         return (0);
  462 
  463 bad:    if (err) device_printf(dev, "%s\n", err);
  464         release_resource(scp);
  465         return (ENXIO);
  466 }
  467 
  468 static int
  469 sbc_detach(device_t dev)
  470 {
  471         struct sbc_softc *scp = device_get_softc(dev);
  472 
  473         sbc_lock(scp);
  474         device_delete_child(dev, scp->child_midi2);
  475         device_delete_child(dev, scp->child_midi1);
  476         device_delete_child(dev, scp->child_pcm);
  477         release_resource(scp);
  478         sbc_lockdestroy(scp);
  479         return bus_generic_detach(dev);
  480 }
  481 
  482 static void
  483 sbc_intr(void *p)
  484 {
  485         struct sbc_ihl *ihl = p;
  486         int i;
  487 
  488         /* sbc_lock(ihl->parent); */
  489         i = 0;
  490         while (i < INTR_MAX) {
  491                 if (ihl->intr[i] != NULL) ihl->intr[i](ihl->intr_arg[i]);
  492                 i++;
  493         }
  494         /* sbc_unlock(ihl->parent); */
  495 }
  496 
  497 static int
  498 sbc_setup_intr(device_t dev, device_t child, struct resource *irq,
  499                int flags, driver_intr_t *intr, void *arg,
  500                void **cookiep)
  501 {
  502         struct sbc_softc *scp = device_get_softc(dev);
  503         struct sbc_ihl *ihl = NULL;
  504         int i, ret;
  505 
  506         sbc_lock(scp);
  507         i = 0;
  508         while (i < IRQ_MAX) {
  509                 if (irq == scp->irq[i]) ihl = &scp->ihl[i];
  510                 i++;
  511         }
  512         ret = 0;
  513         if (ihl == NULL) ret = EINVAL;
  514         i = 0;
  515         while ((ret == 0) && (i < INTR_MAX)) {
  516                 if (ihl->intr[i] == NULL) {
  517                         ihl->intr[i] = intr;
  518                         ihl->intr_arg[i] = arg;
  519                         *cookiep = &ihl->intr[i];
  520                         ret = -1;
  521                 } else i++;
  522         }
  523         sbc_unlock(scp);
  524         return (ret > 0)? EINVAL : 0;
  525 }
  526 
  527 static int
  528 sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
  529                   void *cookie)
  530 {
  531         struct sbc_softc *scp = device_get_softc(dev);
  532         struct sbc_ihl *ihl = NULL;
  533         int i, ret;
  534 
  535         sbc_lock(scp);
  536         i = 0;
  537         while (i < IRQ_MAX) {
  538                 if (irq == scp->irq[i]) ihl = &scp->ihl[i];
  539                 i++;
  540         }
  541         ret = 0;
  542         if (ihl == NULL) ret = EINVAL;
  543         i = 0;
  544         while ((ret == 0) && (i < INTR_MAX)) {
  545                 if (cookie == &ihl->intr[i]) {
  546                         ihl->intr[i] = NULL;
  547                         ihl->intr_arg[i] = NULL;
  548                         return 0;
  549                 } else i++;
  550         }
  551         sbc_unlock(scp);
  552         return (ret > 0)? EINVAL : 0;
  553 }
  554 
  555 static struct resource *
  556 sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
  557                       u_long start, u_long end, u_long count, u_int flags)
  558 {
  559         struct sbc_softc *scp;
  560         int *alloced, rid_max, alloced_max;
  561         struct resource **res;
  562 #ifdef PC98
  563         int i;
  564 #endif
  565 
  566         scp = device_get_softc(bus);
  567         switch (type) {
  568         case SYS_RES_IOPORT:
  569                 alloced = scp->io_alloced;
  570                 res = scp->io;
  571 #ifdef PC98
  572                 rid_max = 0;
  573                 for (i = 0; i < IO_MAX; i++)
  574                         rid_max += io_range[i];
  575 #else
  576                 rid_max = IO_MAX - 1;
  577 #endif
  578                 alloced_max = 1;
  579                 break;
  580         case SYS_RES_DRQ:
  581                 alloced = scp->drq_alloced;
  582                 res = scp->drq;
  583                 rid_max = DRQ_MAX - 1;
  584                 alloced_max = 1;
  585                 break;
  586         case SYS_RES_IRQ:
  587                 alloced = scp->irq_alloced;
  588                 res = scp->irq;
  589                 rid_max = IRQ_MAX - 1;
  590                 alloced_max = INTR_MAX; /* pcm and mpu may share the irq. */
  591                 break;
  592         default:
  593                 return (NULL);
  594         }
  595 
  596         if (*rid > rid_max || alloced[*rid] == alloced_max)
  597                 return (NULL);
  598 
  599         alloced[*rid]++;
  600         return (res[*rid]);
  601 }
  602 
  603 static int
  604 sbc_release_resource(device_t bus, device_t child, int type, int rid,
  605                         struct resource *r)
  606 {
  607         struct sbc_softc *scp;
  608         int *alloced, rid_max;
  609 
  610         scp = device_get_softc(bus);
  611         switch (type) {
  612         case SYS_RES_IOPORT:
  613                 alloced = scp->io_alloced;
  614                 rid_max = IO_MAX - 1;
  615                 break;
  616         case SYS_RES_DRQ:
  617                 alloced = scp->drq_alloced;
  618                 rid_max = DRQ_MAX - 1;
  619                 break;
  620         case SYS_RES_IRQ:
  621                 alloced = scp->irq_alloced;
  622                 rid_max = IRQ_MAX - 1;
  623                 break;
  624         default:
  625                 return (1);
  626         }
  627 
  628         if (rid > rid_max || alloced[rid] == 0)
  629                 return (1);
  630 
  631         alloced[rid]--;
  632         return (0);
  633 }
  634 
  635 static int
  636 sbc_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
  637 {
  638         struct sbc_softc *scp = device_get_softc(bus);
  639         struct sndcard_func *func = device_get_ivars(dev);
  640 
  641         switch (index) {
  642         case 0:
  643                 *result = func->func;
  644                 break;
  645 
  646         case 1:
  647                 *result = scp->bd_ver;
  648                 break;
  649 
  650         default:
  651                 return ENOENT;
  652         }
  653 
  654         return 0;
  655 }
  656 
  657 static int
  658 sbc_write_ivar(device_t bus, device_t dev,
  659                int index, uintptr_t value)
  660 {
  661         switch (index) {
  662         case 0:
  663         case 1:
  664                 return EINVAL;
  665 
  666         default:
  667                 return (ENOENT);
  668         }
  669 }
  670 
  671 static int
  672 alloc_resource(struct sbc_softc *scp)
  673 {
  674         int i;
  675 
  676         for (i = 0 ; i < IO_MAX ; i++) {
  677                 if (scp->io[i] == NULL) {
  678 #ifdef PC98
  679                         scp->io_rid[i] = i > 0 ?
  680                                 scp->io_rid[i - 1] + io_range[i - 1] : 0;
  681                         scp->io[i] = isa_alloc_resourcev(scp->dev,
  682                                                          SYS_RES_IOPORT,
  683                                                          &scp->io_rid[i],
  684                                                          sb_iat[i],
  685                                                          io_range[i],
  686                                                          RF_ACTIVE);
  687                         if (scp->io[i] != NULL)
  688                                 isa_load_resourcev(scp->io[i], sb_iat[i],
  689                                                    io_range[i]);
  690 #else
  691                         scp->io_rid[i] = i;
  692                         scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
  693                                                         0, ~0, io_range[i], RF_ACTIVE);
  694 #endif
  695                         if (i == 0 && scp->io[i] == NULL)
  696                                 return (1);
  697                         scp->io_alloced[i] = 0;
  698                 }
  699         }
  700         for (i = 0 ; i < DRQ_MAX ; i++) {
  701                 if (scp->drq[i] == NULL) {
  702                         scp->drq_rid[i] = i;
  703                         scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i],
  704                                                          0, ~0, 1, RF_ACTIVE);
  705                         if (i == 0 && scp->drq[i] == NULL)
  706                                 return (1);
  707                         scp->drq_alloced[i] = 0;
  708                 }
  709         }
  710         for (i = 0 ; i < IRQ_MAX ; i++) {
  711                 if (scp->irq[i] == NULL) {
  712                         scp->irq_rid[i] = i;
  713                         scp->irq[i] = bus_alloc_resource(scp->dev, SYS_RES_IRQ, &scp->irq_rid[i],
  714                                                          0, ~0, 1, RF_ACTIVE);
  715                         if (i == 0 && scp->irq[i] == NULL)
  716                                 return (1);
  717                         scp->irq_alloced[i] = 0;
  718                 }
  719         }
  720         return (0);
  721 }
  722 
  723 static int
  724 release_resource(struct sbc_softc *scp)
  725 {
  726         int i;
  727 
  728         for (i = 0 ; i < IO_MAX ; i++) {
  729                 if (scp->io[i] != NULL) {
  730                         bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
  731                         scp->io[i] = NULL;
  732                 }
  733         }
  734         for (i = 0 ; i < DRQ_MAX ; i++) {
  735                 if (scp->drq[i] != NULL) {
  736                         bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
  737                         scp->drq[i] = NULL;
  738                 }
  739         }
  740         for (i = 0 ; i < IRQ_MAX ; i++) {
  741                 if (scp->irq[i] != NULL) {
  742                         if (scp->ih[i] != NULL)
  743                                 bus_teardown_intr(scp->dev, scp->irq[i], scp->ih[i]);
  744                         scp->ih[i] = NULL;
  745                         bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid[i], scp->irq[i]);
  746                         scp->irq[i] = NULL;
  747                 }
  748         }
  749         return (0);
  750 }
  751 
  752 static device_method_t sbc_methods[] = {
  753         /* Device interface */
  754         DEVMETHOD(device_probe,         sbc_probe),
  755         DEVMETHOD(device_attach,        sbc_attach),
  756         DEVMETHOD(device_detach,        sbc_detach),
  757         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
  758         DEVMETHOD(device_suspend,       bus_generic_suspend),
  759         DEVMETHOD(device_resume,        bus_generic_resume),
  760 
  761         /* Bus interface */
  762         DEVMETHOD(bus_read_ivar,        sbc_read_ivar),
  763         DEVMETHOD(bus_write_ivar,       sbc_write_ivar),
  764         DEVMETHOD(bus_print_child,      bus_generic_print_child),
  765         DEVMETHOD(bus_alloc_resource,   sbc_alloc_resource),
  766         DEVMETHOD(bus_release_resource, sbc_release_resource),
  767         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
  768         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
  769         DEVMETHOD(bus_setup_intr,       sbc_setup_intr),
  770         DEVMETHOD(bus_teardown_intr,    sbc_teardown_intr),
  771 
  772         { 0, 0 }
  773 };
  774 
  775 static driver_t sbc_driver = {
  776         "sbc",
  777         sbc_methods,
  778         sizeof(struct sbc_softc),
  779 };
  780 
  781 /* sbc can be attached to an isa bus. */
  782 DRIVER_MODULE(snd_sbc, isa, sbc_driver, sbc_devclass, 0, 0);
  783 MODULE_DEPEND(snd_sbc, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
  784 MODULE_VERSION(snd_sbc, 1);

Cache object: 91f75b31650cece84337d633691c9968


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