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/pcm/vchan.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) 2001 Cameron Grant <cg@freebsd.org>
    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/pcm/sound.h>
   28 #include <dev/sound/pcm/vchan.h>
   29 #include "feeder_if.h"
   30 
   31 SND_DECLARE_FILE("$FreeBSD: src/sys/dev/sound/pcm/vchan.c,v 1.16.2.1 2005/01/30 01:00:05 imp Exp $");
   32 
   33 struct vchinfo {
   34         u_int32_t spd, fmt, blksz, bps, run;
   35         struct pcm_channel *channel, *parent;
   36         struct pcmchan_caps caps;
   37 };
   38 
   39 static u_int32_t vchan_fmt[] = {
   40         AFMT_STEREO | AFMT_S16_LE,
   41         0
   42 };
   43 
   44 static int
   45 vchan_mix_s16(int16_t *to, int16_t *tmp, unsigned int count)
   46 {
   47         /*
   48          * to is the output buffer, tmp is the input buffer
   49          * count is the number of 16bit samples to mix
   50          */
   51         int i;
   52         int x;
   53 
   54         for(i = 0; i < count; i++) {
   55                 x = to[i];
   56                 x += tmp[i];
   57                 if (x < -32768) {
   58                         /* printf("%d + %d = %d (u)\n", to[i], tmp[i], x); */
   59                         x = -32768;
   60                 }
   61                 if (x > 32767) {
   62                         /* printf("%d + %d = %d (o)\n", to[i], tmp[i], x); */
   63                         x = 32767;
   64                 }
   65                 to[i] = x & 0x0000ffff;
   66         }
   67         return 0;
   68 }
   69 
   70 static int
   71 feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32_t count, void *source)
   72 {
   73         /* we're going to abuse things a bit */
   74         struct snd_dbuf *src = source;
   75         struct pcmchan_children *cce;
   76         struct pcm_channel *ch;
   77         int16_t *tmp, *dst;
   78         unsigned int cnt;
   79 
   80         if (sndbuf_getsize(src) < count)
   81                 panic("feed_vchan_s16(%s): tmp buffer size %d < count %d, flags = 0x%x",
   82                     c->name, sndbuf_getsize(src), count, c->flags);
   83         count &= ~1;
   84         bzero(b, count);
   85 
   86         /*
   87          * we are going to use our source as a temporary buffer since it's
   88          * got no other purpose.  we obtain our data by traversing the channel
   89          * list of children and calling vchan_mix_* to mix count bytes from each
   90          * into our destination buffer, b
   91          */
   92         dst = (int16_t *)b;
   93         tmp = (int16_t *)sndbuf_getbuf(src);
   94         bzero(tmp, count);
   95         SLIST_FOREACH(cce, &c->children, link) {
   96                 ch = cce->channel;
   97                 CHN_LOCK(ch);
   98                 if (ch->flags & CHN_F_TRIGGERED) {
   99                         if (ch->flags & CHN_F_MAPPED)
  100                                 sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft));
  101                         cnt = FEEDER_FEED(ch->feeder, ch, (u_int8_t *)tmp, count, ch->bufsoft);
  102                         vchan_mix_s16(dst, tmp, cnt / 2);
  103                 }
  104                 CHN_UNLOCK(ch);
  105         }
  106 
  107         return count;
  108 }
  109 
  110 static struct pcm_feederdesc feeder_vchan_s16_desc[] = {
  111         {FEEDER_MIXER, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
  112         {0},
  113 };
  114 static kobj_method_t feeder_vchan_s16_methods[] = {
  115         KOBJMETHOD(feeder_feed,         feed_vchan_s16),
  116         { 0, 0 }
  117 };
  118 FEEDER_DECLARE(feeder_vchan_s16, 2, NULL);
  119 
  120 /************************************************************/
  121 
  122 static void *
  123 vchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
  124 {
  125         struct vchinfo *ch;
  126         struct pcm_channel *parent = devinfo;
  127 
  128         KASSERT(dir == PCMDIR_PLAY, ("vchan_init: bad direction"));
  129         ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
  130         ch->parent = parent;
  131         ch->channel = c;
  132         ch->fmt = AFMT_U8;
  133         ch->spd = DSP_DEFAULT_SPEED;
  134         ch->blksz = 2048;
  135 
  136         c->flags |= CHN_F_VIRTUAL;
  137 
  138         return ch;
  139 }
  140 
  141 static int
  142 vchan_free(kobj_t obj, void *data)
  143 {
  144         return 0;
  145 }
  146 
  147 static int
  148 vchan_setformat(kobj_t obj, void *data, u_int32_t format)
  149 {
  150         struct vchinfo *ch = data;
  151         struct pcm_channel *parent = ch->parent;
  152         struct pcm_channel *channel = ch->channel;
  153 
  154         ch->fmt = format;
  155         ch->bps = 1;
  156         ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
  157         ch->bps <<= (ch->fmt & AFMT_16BIT)? 1 : 0;
  158         ch->bps <<= (ch->fmt & AFMT_32BIT)? 2 : 0;
  159         CHN_UNLOCK(channel);
  160         chn_notify(parent, CHN_N_FORMAT);
  161         CHN_LOCK(channel);
  162         return 0;
  163 }
  164 
  165 static int
  166 vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
  167 {
  168         struct vchinfo *ch = data;
  169         struct pcm_channel *parent = ch->parent;
  170         struct pcm_channel *channel = ch->channel;
  171 
  172         ch->spd = speed;
  173         CHN_UNLOCK(channel);
  174         chn_notify(parent, CHN_N_RATE);
  175         CHN_LOCK(channel);
  176         return speed;
  177 }
  178 
  179 static int
  180 vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
  181 {
  182         struct vchinfo *ch = data;
  183         struct pcm_channel *parent = ch->parent;
  184         /* struct pcm_channel *channel = ch->channel; */
  185         int prate, crate;
  186 
  187         ch->blksz = blocksize;
  188         /* CHN_UNLOCK(channel); */
  189         chn_notify(parent, CHN_N_BLOCKSIZE);
  190         CHN_LOCK(parent);
  191         /* CHN_LOCK(channel); */
  192 
  193         crate = ch->spd * ch->bps;
  194         prate = sndbuf_getspd(parent->bufhard) * sndbuf_getbps(parent->bufhard);
  195         blocksize = sndbuf_getblksz(parent->bufhard);
  196         CHN_UNLOCK(parent);
  197         blocksize *= prate;
  198         blocksize /= crate;
  199 
  200         return blocksize;
  201 }
  202 
  203 static int
  204 vchan_trigger(kobj_t obj, void *data, int go)
  205 {
  206         struct vchinfo *ch = data;
  207         struct pcm_channel *parent = ch->parent;
  208         struct pcm_channel *channel = ch->channel;
  209 
  210         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
  211                 return 0;
  212 
  213         ch->run = (go == PCMTRIG_START)? 1 : 0;
  214         CHN_UNLOCK(channel);
  215         chn_notify(parent, CHN_N_TRIGGER);
  216         CHN_LOCK(channel);
  217 
  218         return 0;
  219 }
  220 
  221 static struct pcmchan_caps *
  222 vchan_getcaps(kobj_t obj, void *data)
  223 {
  224         struct vchinfo *ch = data;
  225 
  226         ch->caps.minspeed = sndbuf_getspd(ch->parent->bufhard);
  227         ch->caps.maxspeed = ch->caps.minspeed;
  228         ch->caps.fmtlist = vchan_fmt;
  229         ch->caps.caps = 0;
  230 
  231         return &ch->caps;
  232 }
  233 
  234 static kobj_method_t vchan_methods[] = {
  235         KOBJMETHOD(channel_init,                vchan_init),
  236         KOBJMETHOD(channel_free,                vchan_free),
  237         KOBJMETHOD(channel_setformat,           vchan_setformat),
  238         KOBJMETHOD(channel_setspeed,            vchan_setspeed),
  239         KOBJMETHOD(channel_setblocksize,        vchan_setblocksize),
  240         KOBJMETHOD(channel_trigger,             vchan_trigger),
  241         KOBJMETHOD(channel_getcaps,             vchan_getcaps),
  242         { 0, 0 }
  243 };
  244 CHANNEL_DECLARE(vchan);
  245 
  246 /* virtual channel interface */
  247 
  248 int
  249 vchan_create(struct pcm_channel *parent)
  250 {
  251         struct snddev_info *d = parent->parentsnddev;
  252         struct pcmchan_children *pce;
  253         struct pcm_channel *child;
  254         int err, first;
  255 
  256         CHN_UNLOCK(parent);
  257 
  258         pce = malloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
  259         if (!pce) {
  260                 CHN_LOCK(parent);
  261                 return ENOMEM;
  262         }
  263 
  264         /* create a new playback channel */
  265         child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
  266         if (!child) {
  267                 free(pce, M_DEVBUF);
  268                 CHN_LOCK(parent);
  269                 return ENODEV;
  270         }
  271 
  272         CHN_LOCK(parent);
  273         if (!(parent->flags & CHN_F_BUSY))
  274                 return EBUSY;
  275 
  276         first = SLIST_EMPTY(&parent->children);
  277         /* add us to our parent channel's children */
  278         pce->channel = child;
  279         SLIST_INSERT_HEAD(&parent->children, pce, link);
  280         CHN_UNLOCK(parent);
  281 
  282         /* add us to our grandparent's channel list */
  283         /*
  284          * XXX maybe we shouldn't always add the dev_t
  285          */
  286         err = pcm_chn_add(d, child);
  287         if (err) {
  288                 pcm_chn_destroy(child);
  289                 free(pce, M_DEVBUF);
  290         }
  291 
  292         CHN_LOCK(parent);
  293         /* XXX gross ugly hack, murder death kill */
  294         if (first && !err) {
  295                 err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
  296                 if (err)
  297                         printf("chn_reset: %d\n", err);
  298                 err = chn_setspeed(parent, 44100);
  299                 if (err)
  300                         printf("chn_setspeed: %d\n", err);
  301         }
  302 
  303         return err;
  304 }
  305 
  306 int
  307 vchan_destroy(struct pcm_channel *c)
  308 {
  309         struct pcm_channel *parent = c->parentchannel;
  310         struct snddev_info *d = parent->parentsnddev;
  311         struct pcmchan_children *pce;
  312         int err, last;
  313 
  314         CHN_LOCK(parent);
  315         if (!(parent->flags & CHN_F_BUSY)) {
  316                 CHN_UNLOCK(parent);
  317                 return EBUSY;
  318         }
  319         if (SLIST_EMPTY(&parent->children)) {
  320                 CHN_UNLOCK(parent);
  321                 return EINVAL;
  322         }
  323 
  324         /* remove us from our parent's children list */
  325         SLIST_FOREACH(pce, &parent->children, link) {
  326                 if (pce->channel == c)
  327                         goto gotch;
  328         }
  329         CHN_UNLOCK(parent);
  330         return EINVAL;
  331 gotch:
  332         SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
  333         free(pce, M_DEVBUF);
  334 
  335         last = SLIST_EMPTY(&parent->children);
  336         if (last)
  337                 parent->flags &= ~CHN_F_BUSY;
  338 
  339         /* remove us from our grandparent's channel list */
  340         err = pcm_chn_remove(d, c);
  341         if (err)
  342                 return err;
  343 
  344         CHN_UNLOCK(parent);
  345         /* destroy ourselves */
  346         err = pcm_chn_destroy(c);
  347 
  348         return err;
  349 }
  350 
  351 int
  352 vchan_initsys(device_t dev)
  353 {
  354 #ifdef SND_DYNSYSCTL
  355         struct snddev_info *d;
  356 
  357         d = device_get_softc(dev);
  358         SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
  359             OID_AUTO, "vchans", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
  360             sysctl_hw_snd_vchans, "I", "");
  361 #endif
  362 
  363         return 0;
  364 }
  365 
  366 

Cache object: 652a42f8b03b6d7b333943f6e7a26646


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