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-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 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/6.4/sys/dev/sound/isa/sbc.c 153901 2005-12-30 19:55:55Z netchild $");
   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_lockassert(struct sbc_softc *scp)
  136 {
  137         snd_mtxassert(scp->lock);
  138 }
  139 
  140 void
  141 sbc_unlock(struct sbc_softc *scp)
  142 {
  143         snd_mtxunlock(scp->lock);
  144 }
  145 
  146 static int
  147 sb_rd(struct resource *io, int reg)
  148 {
  149         return bus_space_read_1(rman_get_bustag(io),
  150                                 rman_get_bushandle(io),
  151                                 reg);
  152 }
  153 
  154 static void
  155 sb_wr(struct resource *io, int reg, u_int8_t val)
  156 {
  157         bus_space_write_1(rman_get_bustag(io),
  158                           rman_get_bushandle(io),
  159                           reg, val);
  160 }
  161 
  162 static int
  163 sb_dspready(struct resource *io)
  164 {
  165         return ((sb_rd(io, SBDSP_STATUS) & 0x80) == 0);
  166 }
  167 
  168 static int
  169 sb_dspwr(struct resource *io, u_char val)
  170 {
  171         int  i;
  172 
  173         for (i = 0; i < 1000; i++) {
  174                 if (sb_dspready(io)) {
  175                         sb_wr(io, SBDSP_CMD, val);
  176                         return 1;
  177                 }
  178                 if (i > 10) DELAY((i > 100)? 1000 : 10);
  179         }
  180         printf("sb_dspwr(0x%02x) timed out.\n", val);
  181         return 0;
  182 }
  183 
  184 static int
  185 sb_cmd(struct resource *io, u_char val)
  186 {
  187         return sb_dspwr(io, val);
  188 }
  189 
  190 static void
  191 sb_setmixer(struct resource *io, u_int port, u_int value)
  192 {
  193         u_long   flags;
  194 
  195         flags = spltty();
  196         sb_wr(io, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
  197         DELAY(10);
  198         sb_wr(io, SB_MIX_DATA, (u_char) (value & 0xff));
  199         DELAY(10);
  200         splx(flags);
  201 }
  202 
  203 static u_int
  204 sb_get_byte(struct resource *io)
  205 {
  206         int i;
  207 
  208         for (i = 1000; i > 0; i--) {
  209                 if (sb_rd(io, DSP_DATA_AVAIL) & 0x80)
  210                         return sb_rd(io, DSP_READ);
  211                 else
  212                         DELAY(20);
  213         }
  214         return 0xffff;
  215 }
  216 
  217 static int
  218 sb_reset_dsp(struct resource *io)
  219 {
  220         sb_wr(io, SBDSP_RST, 3);
  221         DELAY(100);
  222         sb_wr(io, SBDSP_RST, 0);
  223         return (sb_get_byte(io) == 0xAA)? 0 : ENXIO;
  224 }
  225 
  226 static int
  227 sb_identify_board(struct resource *io)
  228 {
  229         int ver, essver, rev;
  230 
  231         sb_cmd(io, DSP_CMD_GETVER);     /* Get version */
  232         ver = (sb_get_byte(io) << 8) | sb_get_byte(io);
  233         if (ver < 0x100 || ver > 0x4ff) return 0;
  234         if (ver == 0x0301) {
  235                 /* Try to detect ESS chips. */
  236                 sb_cmd(io, DSP_CMD_GETID); /* Return ident. bytes. */
  237                 essver = (sb_get_byte(io) << 8) | sb_get_byte(io);
  238                 rev = essver & 0x000f;
  239                 essver &= 0xfff0;
  240                 if (essver == 0x4880) ver |= 0x1000;
  241                 else if (essver == 0x6880) ver = 0x0500 | rev;
  242         }
  243         return ver;
  244 }
  245 
  246 static struct isa_pnp_id sbc_ids[] = {
  247         {0x01008c0e, "Creative ViBRA16C"},              /* CTL0001 */
  248         {0x31008c0e, "Creative SB16/SB32"},             /* CTL0031 */
  249         {0x41008c0e, "Creative SB16/SB32"},             /* CTL0041 */
  250         {0x42008c0e, "Creative SB AWE64"},              /* CTL0042 */
  251         {0x43008c0e, "Creative ViBRA16X"},              /* CTL0043 */
  252         {0x44008c0e, "Creative SB AWE64 Gold"},         /* CTL0044 */
  253         {0x45008c0e, "Creative SB AWE64"},              /* CTL0045 */
  254         {0x46008c0e, "Creative SB AWE64"},              /* CTL0046 */
  255 
  256         {0x01000000, "Avance Logic ALS100+"},           /* @@@0001 - ViBRA16X clone */
  257         {0x01100000, "Avance Asound 110"},              /* @@@1001 */
  258         {0x01200000, "Avance Logic ALS120"},            /* @@@2001 - ViBRA16X clone */
  259 
  260         {0x81167316, "ESS ES1681"},                     /* ESS1681 */
  261         {0x02017316, "ESS ES1688"},                     /* ESS1688 */
  262         {0x68097316, "ESS ES1688"},                     /* ESS1688 */
  263         {0x68187316, "ESS ES1868"},                     /* ESS1868 */
  264         {0x03007316, "ESS ES1869"},                     /* ESS1869 */
  265         {0x69187316, "ESS ES1869"},                     /* ESS1869 */
  266         {0xabb0110e, "ESS ES1869 (Compaq OEM)"},        /* CPQb0ab */
  267         {0xacb0110e, "ESS ES1869 (Compaq OEM)"},        /* CPQb0ac */
  268         {0x78187316, "ESS ES1878"},                     /* ESS1878 */
  269         {0x79187316, "ESS ES1879"},                     /* ESS1879 */
  270         {0x88187316, "ESS ES1888"},                     /* ESS1888 */
  271         {0x07017316, "ESS ES1888 (DEC OEM)"},           /* ESS0107 */
  272         {0x06017316, "ESS ES1888 (Dell OEM)"},          /* ESS0106 */
  273         {0}
  274 };
  275 
  276 static int
  277 sbc_probe(device_t dev)
  278 {
  279         char *s = NULL;
  280         u_int32_t lid, vid;
  281 
  282         lid = isa_get_logicalid(dev);
  283         vid = isa_get_vendorid(dev);
  284         if (lid) {
  285                 if (lid == 0x01000000 && vid != 0x01009305) /* ALS0001 */
  286                         return ENXIO;
  287                 /* Check pnp ids */
  288                 return ISA_PNP_PROBE(device_get_parent(dev), dev, sbc_ids);
  289         } else {
  290                 int rid = 0, ver;
  291                 struct resource *io;
  292 
  293 #ifdef PC98
  294                 io = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid,
  295                                          pcm_iat, 16, RF_ACTIVE);
  296 #else
  297                 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
  298                                         0, ~0, 16, RF_ACTIVE);
  299 #endif
  300                 if (!io) goto bad;
  301 #ifdef PC98
  302                 isa_load_resourcev(io, pcm_iat, 16);
  303 #endif
  304                 if (sb_reset_dsp(io)) goto bad2;
  305                 ver = sb_identify_board(io);
  306                 if (ver == 0) goto bad2;
  307                 switch ((ver & 0x00000f00) >> 8) {
  308                 case 1:
  309                         device_set_desc(dev, "SoundBlaster 1.0 (not supported)");
  310                         s = NULL;
  311                         break;
  312 
  313                 case 2:
  314                         s = "SoundBlaster 2.0";
  315                         break;
  316 
  317                 case 3:
  318                         s = (ver & 0x0000f000)? "ESS 488" : "SoundBlaster Pro";
  319                         break;
  320 
  321                 case 4:
  322                         s = "SoundBlaster 16";
  323                         break;
  324 
  325                 case 5:
  326                         s = (ver & 0x00000008)? "ESS 688" : "ESS 1688";
  327                         break;
  328                 }
  329                 if (s) device_set_desc(dev, s);
  330 bad2:           bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
  331 bad:            return s? 0 : ENXIO;
  332         }
  333 }
  334 
  335 static int
  336 sbc_attach(device_t dev)
  337 {
  338         char *err = NULL;
  339         struct sbc_softc *scp;
  340         struct sndcard_func *func;
  341         u_int32_t logical_id = isa_get_logicalid(dev);
  342         int flags = device_get_flags(dev);
  343         int f, dh, dl, x, irq, i;
  344 
  345         if (!logical_id && (flags & DV_F_DUAL_DMA)) {
  346                 bus_set_resource(dev, SYS_RES_DRQ, 1,
  347                                  flags & DV_F_DRQ_MASK, 1);
  348         }
  349 
  350         scp = device_get_softc(dev);
  351         bzero(scp, sizeof(*scp));
  352         scp->dev = dev;
  353         sbc_lockinit(scp);
  354         err = "alloc_resource";
  355         if (alloc_resource(scp)) goto bad;
  356 
  357         err = "sb_reset_dsp";
  358         if (sb_reset_dsp(scp->io[0])) goto bad;
  359         err = "sb_identify_board";
  360         scp->bd_ver = sb_identify_board(scp->io[0]) & 0x00000fff;
  361         if (scp->bd_ver == 0) goto bad;
  362         f = 0;
  363         if (logical_id == 0x01200000 && scp->bd_ver < 0x0400) scp->bd_ver = 0x0499;
  364         switch ((scp->bd_ver & 0x0f00) >> 8) {
  365         case 1: /* old sound blaster has nothing... */
  366                 break;
  367 
  368         case 2:
  369                 f |= BD_F_DUP_MIDI;
  370                 if (scp->bd_ver > 0x200) f |= BD_F_MIX_CT1335;
  371                 break;
  372 
  373         case 5:
  374                 f |= BD_F_ESS;
  375                 scp->bd_ver = 0x0301;
  376         case 3:
  377                 f |= BD_F_DUP_MIDI | BD_F_MIX_CT1345;
  378                 break;
  379 
  380         case 4:
  381                 f |= BD_F_SB16 | BD_F_MIX_CT1745;
  382                 if (scp->drq[0]) dl = rman_get_start(scp->drq[0]); else dl = -1;
  383                 if (scp->drq[1]) dh = rman_get_start(scp->drq[1]); else dh = dl;
  384                 if (!logical_id && (dh < dl)) {
  385                         struct resource *r;
  386                         r = scp->drq[0];
  387                         scp->drq[0] = scp->drq[1];
  388                         scp->drq[1] = r;
  389                         dl = rman_get_start(scp->drq[0]);
  390                         dh = rman_get_start(scp->drq[1]);
  391                 }
  392                 /* soft irq/dma configuration */
  393                 x = -1;
  394                 irq = rman_get_start(scp->irq[0]);
  395 #ifdef PC98
  396                 /* SB16 in PC98 use different IRQ table */
  397                 if      (irq == 3) x = 1;
  398                 else if (irq == 5) x = 8;
  399                 else if (irq == 10) x = 2;
  400                 else if (irq == 12) x = 4;
  401                 if (x == -1) {
  402                         err = "bad irq (3/5/10/12 valid)";
  403                         goto bad;
  404                 }
  405                 else sb_setmixer(scp->io[0], IRQ_NR, x);
  406                 /* SB16 in PC98 use different dma setting */
  407                 sb_setmixer(scp->io[0], DMA_NR, dh == 0 ? 1 : 2);
  408 #else
  409                 if      (irq == 5) x = 2;
  410                 else if (irq == 7) x = 4;
  411                 else if (irq == 9) x = 1;
  412                 else if (irq == 10) x = 8;
  413                 if (x == -1) {
  414                         err = "bad irq (5/7/9/10 valid)";
  415                         goto bad;
  416                 }
  417                 else sb_setmixer(scp->io[0], IRQ_NR, x);
  418                 sb_setmixer(scp->io[0], DMA_NR, (1 << dh) | (1 << dl));
  419 #endif
  420                 if (bootverbose) {
  421                         device_printf(dev, "setting card to irq %d, drq %d", irq, dl);
  422                         if (dl != dh) printf(", %d", dh);
  423                         printf("\n");
  424                 }
  425                 break;
  426         }
  427 
  428         switch (logical_id) {
  429         case 0x43008c0e:        /* CTL0043 */
  430         case 0x01200000:
  431         case 0x01000000:
  432                 f |= BD_F_SB16X;
  433                 break;
  434         }
  435         scp->bd_ver |= f << 16;
  436 
  437         err = "setup_intr";
  438         for (i = 0; i < IRQ_MAX; i++) {
  439                 scp->ihl[i].parent = scp;
  440                 if (snd_setup_intr(dev, scp->irq[i], 0, sbc_intr, &scp->ihl[i], &scp->ih[i]))
  441                         goto bad;
  442         }
  443 
  444         /* PCM Audio */
  445         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
  446         if (func == NULL) goto bad;
  447         func->func = SCF_PCM;
  448         scp->child_pcm = device_add_child(dev, "pcm", -1);
  449         device_set_ivars(scp->child_pcm, func);
  450 
  451         /* Midi Interface */
  452         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
  453         if (func == NULL) goto bad;
  454         func->func = SCF_MIDI;
  455         scp->child_midi1 = device_add_child(dev, "midi", -1);
  456         device_set_ivars(scp->child_midi1, func);
  457 
  458         /* OPL FM Synthesizer */
  459         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
  460         if (func == NULL) goto bad;
  461         func->func = SCF_SYNTH;
  462         scp->child_midi2 = device_add_child(dev, "midi", -1);
  463         device_set_ivars(scp->child_midi2, func);
  464 
  465         /* probe/attach kids */
  466         bus_generic_attach(dev);
  467 
  468         return (0);
  469 
  470 bad:    if (err) device_printf(dev, "%s\n", err);
  471         release_resource(scp);
  472         return (ENXIO);
  473 }
  474 
  475 static int
  476 sbc_detach(device_t dev)
  477 {
  478         struct sbc_softc *scp = device_get_softc(dev);
  479 
  480         sbc_lock(scp);
  481         device_delete_child(dev, scp->child_midi2);
  482         device_delete_child(dev, scp->child_midi1);
  483         device_delete_child(dev, scp->child_pcm);
  484         release_resource(scp);
  485         sbc_lockdestroy(scp);
  486         return bus_generic_detach(dev);
  487 }
  488 
  489 static void
  490 sbc_intr(void *p)
  491 {
  492         struct sbc_ihl *ihl = p;
  493         int i;
  494 
  495         /* sbc_lock(ihl->parent); */
  496         i = 0;
  497         while (i < INTR_MAX) {
  498                 if (ihl->intr[i] != NULL) ihl->intr[i](ihl->intr_arg[i]);
  499                 i++;
  500         }
  501         /* sbc_unlock(ihl->parent); */
  502 }
  503 
  504 static int
  505 sbc_setup_intr(device_t dev, device_t child, struct resource *irq,
  506                int flags, driver_intr_t *intr, void *arg,
  507                void **cookiep)
  508 {
  509         struct sbc_softc *scp = device_get_softc(dev);
  510         struct sbc_ihl *ihl = NULL;
  511         int i, ret;
  512 
  513         sbc_lock(scp);
  514         i = 0;
  515         while (i < IRQ_MAX) {
  516                 if (irq == scp->irq[i]) ihl = &scp->ihl[i];
  517                 i++;
  518         }
  519         ret = 0;
  520         if (ihl == NULL) ret = EINVAL;
  521         i = 0;
  522         while ((ret == 0) && (i < INTR_MAX)) {
  523                 if (ihl->intr[i] == NULL) {
  524                         ihl->intr[i] = intr;
  525                         ihl->intr_arg[i] = arg;
  526                         *cookiep = &ihl->intr[i];
  527                         ret = -1;
  528                 } else i++;
  529         }
  530         sbc_unlock(scp);
  531         return (ret > 0)? EINVAL : 0;
  532 }
  533 
  534 static int
  535 sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
  536                   void *cookie)
  537 {
  538         struct sbc_softc *scp = device_get_softc(dev);
  539         struct sbc_ihl *ihl = NULL;
  540         int i, ret;
  541 
  542         sbc_lock(scp);
  543         i = 0;
  544         while (i < IRQ_MAX) {
  545                 if (irq == scp->irq[i]) ihl = &scp->ihl[i];
  546                 i++;
  547         }
  548         ret = 0;
  549         if (ihl == NULL) ret = EINVAL;
  550         i = 0;
  551         while ((ret == 0) && (i < INTR_MAX)) {
  552                 if (cookie == &ihl->intr[i]) {
  553                         ihl->intr[i] = NULL;
  554                         ihl->intr_arg[i] = NULL;
  555                         return 0;
  556                 } else i++;
  557         }
  558         sbc_unlock(scp);
  559         return (ret > 0)? EINVAL : 0;
  560 }
  561 
  562 static struct resource *
  563 sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
  564                       u_long start, u_long end, u_long count, u_int flags)
  565 {
  566         struct sbc_softc *scp;
  567         int *alloced, rid_max, alloced_max;
  568         struct resource **res;
  569 #ifdef PC98
  570         int i;
  571 #endif
  572 
  573         scp = device_get_softc(bus);
  574         switch (type) {
  575         case SYS_RES_IOPORT:
  576                 alloced = scp->io_alloced;
  577                 res = scp->io;
  578 #ifdef PC98
  579                 rid_max = 0;
  580                 for (i = 0; i < IO_MAX; i++)
  581                         rid_max += io_range[i];
  582 #else
  583                 rid_max = IO_MAX - 1;
  584 #endif
  585                 alloced_max = 1;
  586                 break;
  587         case SYS_RES_DRQ:
  588                 alloced = scp->drq_alloced;
  589                 res = scp->drq;
  590                 rid_max = DRQ_MAX - 1;
  591                 alloced_max = 1;
  592                 break;
  593         case SYS_RES_IRQ:
  594                 alloced = scp->irq_alloced;
  595                 res = scp->irq;
  596                 rid_max = IRQ_MAX - 1;
  597                 alloced_max = INTR_MAX; /* pcm and mpu may share the irq. */
  598                 break;
  599         default:
  600                 return (NULL);
  601         }
  602 
  603         if (*rid > rid_max || alloced[*rid] == alloced_max)
  604                 return (NULL);
  605 
  606         alloced[*rid]++;
  607         return (res[*rid]);
  608 }
  609 
  610 static int
  611 sbc_release_resource(device_t bus, device_t child, int type, int rid,
  612                         struct resource *r)
  613 {
  614         struct sbc_softc *scp;
  615         int *alloced, rid_max;
  616 
  617         scp = device_get_softc(bus);
  618         switch (type) {
  619         case SYS_RES_IOPORT:
  620                 alloced = scp->io_alloced;
  621                 rid_max = IO_MAX - 1;
  622                 break;
  623         case SYS_RES_DRQ:
  624                 alloced = scp->drq_alloced;
  625                 rid_max = DRQ_MAX - 1;
  626                 break;
  627         case SYS_RES_IRQ:
  628                 alloced = scp->irq_alloced;
  629                 rid_max = IRQ_MAX - 1;
  630                 break;
  631         default:
  632                 return (1);
  633         }
  634 
  635         if (rid > rid_max || alloced[rid] == 0)
  636                 return (1);
  637 
  638         alloced[rid]--;
  639         return (0);
  640 }
  641 
  642 static int
  643 sbc_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
  644 {
  645         struct sbc_softc *scp = device_get_softc(bus);
  646         struct sndcard_func *func = device_get_ivars(dev);
  647 
  648         switch (index) {
  649         case 0:
  650                 *result = func->func;
  651                 break;
  652 
  653         case 1:
  654                 *result = scp->bd_ver;
  655                 break;
  656 
  657         default:
  658                 return ENOENT;
  659         }
  660 
  661         return 0;
  662 }
  663 
  664 static int
  665 sbc_write_ivar(device_t bus, device_t dev,
  666                int index, uintptr_t value)
  667 {
  668         switch (index) {
  669         case 0:
  670         case 1:
  671                 return EINVAL;
  672 
  673         default:
  674                 return (ENOENT);
  675         }
  676 }
  677 
  678 static int
  679 alloc_resource(struct sbc_softc *scp)
  680 {
  681         int i;
  682 
  683         for (i = 0 ; i < IO_MAX ; i++) {
  684                 if (scp->io[i] == NULL) {
  685 #ifdef PC98
  686                         scp->io_rid[i] = i > 0 ?
  687                                 scp->io_rid[i - 1] + io_range[i - 1] : 0;
  688                         scp->io[i] = isa_alloc_resourcev(scp->dev,
  689                                                          SYS_RES_IOPORT,
  690                                                          &scp->io_rid[i],
  691                                                          sb_iat[i],
  692                                                          io_range[i],
  693                                                          RF_ACTIVE);
  694                         if (scp->io[i] != NULL)
  695                                 isa_load_resourcev(scp->io[i], sb_iat[i],
  696                                                    io_range[i]);
  697 #else
  698                         scp->io_rid[i] = i;
  699                         scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
  700                                                         0, ~0, io_range[i], RF_ACTIVE);
  701 #endif
  702                         if (i == 0 && scp->io[i] == NULL)
  703                                 return (1);
  704                         scp->io_alloced[i] = 0;
  705                 }
  706         }
  707         for (i = 0 ; i < DRQ_MAX ; i++) {
  708                 if (scp->drq[i] == NULL) {
  709                         scp->drq_rid[i] = i;
  710                         scp->drq[i] = bus_alloc_resource_any(scp->dev,
  711                                                              SYS_RES_DRQ,
  712                                                              &scp->drq_rid[i],
  713                                                              RF_ACTIVE);
  714                         if (i == 0 && scp->drq[i] == NULL)
  715                                 return (1);
  716                         scp->drq_alloced[i] = 0;
  717                 }
  718         }
  719         for (i = 0 ; i < IRQ_MAX ; i++) {
  720                 if (scp->irq[i] == NULL) {
  721                         scp->irq_rid[i] = i;
  722                         scp->irq[i] = bus_alloc_resource_any(scp->dev,
  723                                                              SYS_RES_IRQ,
  724                                                              &scp->irq_rid[i],
  725                                                              RF_ACTIVE);
  726                         if (i == 0 && scp->irq[i] == NULL)
  727                                 return (1);
  728                         scp->irq_alloced[i] = 0;
  729                 }
  730         }
  731         return (0);
  732 }
  733 
  734 static int
  735 release_resource(struct sbc_softc *scp)
  736 {
  737         int i;
  738 
  739         for (i = 0 ; i < IO_MAX ; i++) {
  740                 if (scp->io[i] != NULL) {
  741                         bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
  742                         scp->io[i] = NULL;
  743                 }
  744         }
  745         for (i = 0 ; i < DRQ_MAX ; i++) {
  746                 if (scp->drq[i] != NULL) {
  747                         bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
  748                         scp->drq[i] = NULL;
  749                 }
  750         }
  751         for (i = 0 ; i < IRQ_MAX ; i++) {
  752                 if (scp->irq[i] != NULL) {
  753                         if (scp->ih[i] != NULL)
  754                                 bus_teardown_intr(scp->dev, scp->irq[i], scp->ih[i]);
  755                         scp->ih[i] = NULL;
  756                         bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid[i], scp->irq[i]);
  757                         scp->irq[i] = NULL;
  758                 }
  759         }
  760         return (0);
  761 }
  762 
  763 static device_method_t sbc_methods[] = {
  764         /* Device interface */
  765         DEVMETHOD(device_probe,         sbc_probe),
  766         DEVMETHOD(device_attach,        sbc_attach),
  767         DEVMETHOD(device_detach,        sbc_detach),
  768         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
  769         DEVMETHOD(device_suspend,       bus_generic_suspend),
  770         DEVMETHOD(device_resume,        bus_generic_resume),
  771 
  772         /* Bus interface */
  773         DEVMETHOD(bus_read_ivar,        sbc_read_ivar),
  774         DEVMETHOD(bus_write_ivar,       sbc_write_ivar),
  775         DEVMETHOD(bus_print_child,      bus_generic_print_child),
  776         DEVMETHOD(bus_alloc_resource,   sbc_alloc_resource),
  777         DEVMETHOD(bus_release_resource, sbc_release_resource),
  778         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
  779         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
  780         DEVMETHOD(bus_setup_intr,       sbc_setup_intr),
  781         DEVMETHOD(bus_teardown_intr,    sbc_teardown_intr),
  782 
  783         { 0, 0 }
  784 };
  785 
  786 static driver_t sbc_driver = {
  787         "sbc",
  788         sbc_methods,
  789         sizeof(struct sbc_softc),
  790 };
  791 
  792 /* sbc can be attached to an isa bus. */
  793 DRIVER_MODULE(snd_sbc, isa, sbc_driver, sbc_devclass, 0, 0);
  794 DRIVER_MODULE(snd_sbc, acpi, sbc_driver, sbc_devclass, 0, 0);
  795 MODULE_DEPEND(snd_sbc, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
  796 MODULE_VERSION(snd_sbc, 1);

Cache object: c782063250e702b58123294f1899d755


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