[BACK]Return to dev_sioctl.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sndiod

Annotation of src/usr.bin/sndiod/dev_sioctl.c, Revision 1.3

1.3     ! ratchov     1: /*     $OpenBSD: dev_sioctl.c,v 1.2 2020/03/08 14:52:20 ratchov Exp $  */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2014-2020 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <sys/time.h>
                     18: #include <sys/types.h>
                     19:
                     20: #include <poll.h>
                     21: #include <sndio.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include "abuf.h"
                     26: #include "defs.h"
                     27: #include "dev.h"
                     28: #include "dsp.h"
                     29: #include "file.h"
                     30: #include "dev_sioctl.h"
                     31: #include "utils.h"
                     32:
                     33: void dev_sioctl_ondesc(void *, struct sioctl_desc *, int);
                     34: void dev_sioctl_onval(void *, unsigned int, unsigned int);
                     35: int dev_sioctl_pollfd(void *, struct pollfd *);
                     36: int dev_sioctl_revents(void *, struct pollfd *);
                     37: void dev_sioctl_in(void *);
                     38: void dev_sioctl_out(void *);
                     39: void dev_sioctl_hup(void *);
                     40:
                     41: struct fileops dev_sioctl_ops = {
                     42:        "sioctl",
                     43:        dev_sioctl_pollfd,
                     44:        dev_sioctl_revents,
                     45:        dev_sioctl_in,
                     46:        dev_sioctl_out,
                     47:        dev_sioctl_hup
                     48: };
                     49:
                     50: void
                     51: dev_sioctl_ondesc(void *arg, struct sioctl_desc *desc, int val)
                     52: {
                     53: #define GROUP_PREFIX           "hw"
                     54:        char group_buf[CTL_NAMEMAX], *group;
                     55:        struct dev *d = arg;
                     56:        int addr;
                     57:
1.2       ratchov    58:        if (desc == NULL) {
                     59:                dev_ctlsync(d);
1.1       ratchov    60:                return;
1.2       ratchov    61:        }
                     62:
1.1       ratchov    63:        addr = CTLADDR_END + desc->addr;
                     64:        dev_rmctl(d, addr);
                     65:
                     66:        /*
                     67:         * prefix group names we use (top-level and "app") with "hw."
                     68:         * to ensure that all controls have unique names when multiple
                     69:         * sndiod's are chained
                     70:         */
                     71:        if (desc->group[0] == 0)
                     72:                group = GROUP_PREFIX;
                     73:        else {
                     74:                group = group_buf;
                     75:                if (snprintf(group_buf, CTL_NAMEMAX, GROUP_PREFIX "/%s",
                     76:                    desc->group) >= CTL_NAMEMAX)
                     77:                        return;
                     78:        }
                     79:
                     80:        dev_addctl(d, group, desc->type, addr,
                     81:            desc->node0.name, desc->node0.unit, desc->func,
                     82:            desc->node1.name, desc->node1.unit, desc->maxval, val);
                     83: }
                     84:
                     85: void
                     86: dev_sioctl_onval(void *arg, unsigned int addr, unsigned int val)
                     87: {
                     88:        struct dev *d = arg;
                     89:        struct ctl *c;
                     90:
                     91:        addr += CTLADDR_END;
                     92:
                     93:        dev_log(d);
                     94:        log_puts(": onctl: addr = ");
                     95:        log_putu(addr);
                     96:        log_puts(", val = ");
                     97:        log_putu(val);
                     98:        log_puts("\n");
                     99:
                    100:        for (c = d->ctl_list; c != NULL; c = c->next) {
                    101:                if (c->addr != addr)
                    102:                        continue;
                    103:                ctl_log(c);
                    104:                log_puts(": new value -> ");
                    105:                log_putu(val);
                    106:                log_puts("\n");
                    107:                c->val_mask = ~0U;
                    108:                c->curval = val;
                    109:        }
                    110: }
                    111:
                    112: /*
                    113:  * open the control device.
                    114:  */
                    115: void
                    116: dev_sioctl_open(struct dev *d)
                    117: {
1.3     ! ratchov   118:        if (d->sioctl.hdl == NULL) {
        !           119:                /*
        !           120:                 * At this point there are clients, for instance if we're
        !           121:                 * called by dev_reopen() but the control device couldn't
        !           122:                 * be opened. In this case controls have changed (thoseof
        !           123:                 * old device are just removed) so we need to notify clients.
        !           124:                 */
        !           125:                dev_ctlsync(d);
1.1       ratchov   126:                return;
1.3     ! ratchov   127:        }
1.1       ratchov   128:        sioctl_ondesc(d->sioctl.hdl, dev_sioctl_ondesc, d);
                    129:        sioctl_onval(d->sioctl.hdl, dev_sioctl_onval, d);
                    130:        d->sioctl.file = file_new(&dev_sioctl_ops, d, "mix",
                    131:            sioctl_nfds(d->sioctl.hdl));
                    132: }
                    133:
                    134: /*
                    135:  * close the control device.
                    136:  */
                    137: void
                    138: dev_sioctl_close(struct dev *d)
                    139: {
                    140:        if (d->sioctl.hdl == NULL)
                    141:                return;
                    142:        file_del(d->sioctl.file);
                    143: }
                    144:
                    145: int
                    146: dev_sioctl_pollfd(void *arg, struct pollfd *pfd)
                    147: {
                    148:        struct dev *d = arg;
                    149:        struct ctl *c;
                    150:        int events = 0;
                    151:
                    152:        for (c = d->ctl_list; c != NULL; c = c->next) {
                    153:                if (c->dirty)
                    154:                        events |= POLLOUT;
                    155:        }
                    156:        return sioctl_pollfd(d->sioctl.hdl, pfd, events);
                    157: }
                    158:
                    159: int
                    160: dev_sioctl_revents(void *arg, struct pollfd *pfd)
                    161: {
                    162:        struct dev *d = arg;
                    163:
                    164:        return sioctl_revents(d->sioctl.hdl, pfd);
                    165: }
                    166:
                    167: void
                    168: dev_sioctl_in(void *arg)
                    169: {
                    170: }
                    171:
                    172: void
                    173: dev_sioctl_out(void *arg)
                    174: {
                    175:        struct dev *d = arg;
                    176:        struct ctl *c;
                    177:        int cnt;
                    178:
                    179:        /*
                    180:         * for each dirty ctl, call sioctl_setval() and dev_unref(). As
                    181:         * dev_unref() may destroy the ctl_list, we must call it after
                    182:         * we've finished iterating on it.
                    183:         */
                    184:        cnt = 0;
                    185:        for (c = d->ctl_list; c != NULL; c = c->next) {
                    186:                if (!c->dirty)
                    187:                        continue;
                    188:                if (!sioctl_setval(d->sioctl.hdl,
                    189:                        c->addr - CTLADDR_END, c->curval)) {
                    190:                        ctl_log(c);
                    191:                        log_puts(": set failed\n");
                    192:                        break;
                    193:                }
                    194:                if (log_level >= 2) {
                    195:                        ctl_log(c);
                    196:                        log_puts(": changed\n");
                    197:                }
                    198:                c->dirty = 0;
                    199:                cnt++;
                    200:        }
                    201:        while (cnt-- > 0)
                    202:                dev_unref(d);
                    203: }
                    204:
                    205: void
                    206: dev_sioctl_hup(void *arg)
                    207: {
                    208:        struct dev *d = arg;
                    209:
                    210:        dev_sioctl_close(d);
                    211: }