1098ca2bdSWarner Losh /*- 290da2b28SAriff Abdullah * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org> 390da2b28SAriff Abdullah * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006 490da2b28SAriff Abdullah * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org> 540ac6e17SJoel Dahl * Copyright (c) 1997 Luigi Rizzo 6987e5972SCameron Grant * All rights reserved. 7987e5972SCameron Grant * 8987e5972SCameron Grant * Redistribution and use in source and binary forms, with or without 9987e5972SCameron Grant * modification, are permitted provided that the following conditions 10987e5972SCameron Grant * are met: 11987e5972SCameron Grant * 1. Redistributions of source code must retain the above copyright 12987e5972SCameron Grant * notice, this list of conditions and the following disclaimer. 13987e5972SCameron Grant * 2. Redistributions in binary form must reproduce the above copyright 14987e5972SCameron Grant * notice, this list of conditions and the following disclaimer in the 15987e5972SCameron Grant * documentation and/or other materials provided with the distribution. 16987e5972SCameron Grant * 17987e5972SCameron Grant * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18987e5972SCameron Grant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19987e5972SCameron Grant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20987e5972SCameron Grant * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21987e5972SCameron Grant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22987e5972SCameron Grant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23987e5972SCameron Grant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24987e5972SCameron Grant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25987e5972SCameron Grant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26987e5972SCameron Grant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27987e5972SCameron Grant * SUCH DAMAGE. 28987e5972SCameron Grant */ 29987e5972SCameron Grant 3090da2b28SAriff Abdullah #ifdef HAVE_KERNEL_OPTION_HEADERS 3190da2b28SAriff Abdullah #include "opt_snd.h" 3290da2b28SAriff Abdullah #endif 3390da2b28SAriff Abdullah 34ef9308b1SCameron Grant #include <dev/sound/pcm/sound.h> 35a580b31aSAriff Abdullah #include <dev/sound/pcm/ac97.h> 36285648f9SCameron Grant #include <dev/sound/pcm/vchan.h> 375ee30e27SMathew Kanner #include <dev/sound/pcm/dsp.h> 3890da2b28SAriff Abdullah #include <dev/sound/pcm/sndstat.h> 39bba4862cSAriff Abdullah #include <dev/sound/version.h> 40b611c801SAlexander Leidinger #include <sys/limits.h> 417c438dbeSCameron Grant #include <sys/sysctl.h> 42285648f9SCameron Grant 4367b1dce3SCameron Grant #include "feeder_if.h" 4467b1dce3SCameron Grant 4567b1dce3SCameron Grant SND_DECLARE_FILE("$FreeBSD$"); 4667b1dce3SCameron Grant 47d95502a8SCameron Grant devclass_t pcm_devclass; 4882db23e2SCameron Grant 49b8a36395SCameron Grant int pcm_veto_load = 1; 50b8a36395SCameron Grant 51f3685841SAriff Abdullah int snd_unit = -1; 52851a904aSAlexander Leidinger TUNABLE_INT("hw.snd.default_unit", &snd_unit); 53cbe7d6a3SCameron Grant 54*cbebc90dSAlexander Motin static int snd_unit_auto = -1; 55ad8612b9SAriff Abdullah TUNABLE_INT("hw.snd.default_auto", &snd_unit_auto); 56ad8612b9SAriff Abdullah SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RW, 57838d3589SAriff Abdullah &snd_unit_auto, 0, "assign default unit to a newly attached device"); 58ad8612b9SAriff Abdullah 59bba4862cSAriff Abdullah int snd_maxautovchans = 16; 60851a904aSAlexander Leidinger /* XXX: a tunable implies that we may need more than one sound channel before 61851a904aSAlexander Leidinger the system can change a sysctl (/etc/sysctl.conf), do we really need 62851a904aSAlexander Leidinger this? */ 6367b1dce3SCameron Grant TUNABLE_INT("hw.snd.maxautovchans", &snd_maxautovchans); 64987e5972SCameron Grant 6582db23e2SCameron Grant SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD, 0, "Sound driver"); 6682db23e2SCameron Grant 67bba4862cSAriff Abdullah /* 68bba4862cSAriff Abdullah * XXX I've had enough with people not telling proper version/arch 69bba4862cSAriff Abdullah * while reporting problems, not after 387397913213th questions/requests. 70bba4862cSAriff Abdullah */ 71a9bdb5d3SAndriy Gapon static char snd_driver_version[] = 72bba4862cSAriff Abdullah __XSTRING(SND_DRV_VERSION)"/"MACHINE_ARCH; 73bba4862cSAriff Abdullah SYSCTL_STRING(_hw_snd, OID_AUTO, version, CTLFLAG_RD, &snd_driver_version, 7490da2b28SAriff Abdullah 0, "driver version/arch"); 75bba4862cSAriff Abdullah 76b611c801SAlexander Leidinger /** 77b611c801SAlexander Leidinger * @brief Unit number allocator for syncgroup IDs 78b611c801SAlexander Leidinger */ 79b611c801SAlexander Leidinger struct unrhdr *pcmsg_unrhdr = NULL; 80b611c801SAlexander Leidinger 8190da2b28SAriff Abdullah static int 8290da2b28SAriff Abdullah sndstat_prepare_pcm(SNDSTAT_PREPARE_PCM_ARGS) 8390da2b28SAriff Abdullah { 8490da2b28SAriff Abdullah SNDSTAT_PREPARE_PCM_BEGIN(); 8590da2b28SAriff Abdullah SNDSTAT_PREPARE_PCM_END(); 8690da2b28SAriff Abdullah } 8767b1dce3SCameron Grant 8837209180SCameron Grant void * 892c69ba87SJohn Baldwin snd_mtxcreate(const char *desc, const char *type) 9037209180SCameron Grant { 9137209180SCameron Grant struct mtx *m; 9237209180SCameron Grant 93a163d034SWarner Losh m = malloc(sizeof(*m), M_DEVBUF, M_WAITOK | M_ZERO); 9412e524a2SDon Lewis mtx_init(m, desc, type, MTX_DEF); 9512e524a2SDon Lewis return m; 9612e524a2SDon Lewis } 9712e524a2SDon Lewis 9837209180SCameron Grant void 9937209180SCameron Grant snd_mtxfree(void *m) 10037209180SCameron Grant { 10137209180SCameron Grant struct mtx *mtx = m; 10237209180SCameron Grant 10337209180SCameron Grant mtx_destroy(mtx); 10437209180SCameron Grant free(mtx, M_DEVBUF); 10537209180SCameron Grant } 10637209180SCameron Grant 10737209180SCameron Grant void 10837209180SCameron Grant snd_mtxassert(void *m) 10937209180SCameron Grant { 110f00f162aSCameron Grant #ifdef INVARIANTS 11137209180SCameron Grant struct mtx *mtx = m; 11237209180SCameron Grant 11337209180SCameron Grant mtx_assert(mtx, MA_OWNED); 11437209180SCameron Grant #endif 11537209180SCameron Grant } 11637209180SCameron Grant 11737209180SCameron Grant int 11837209180SCameron Grant snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep) 11937209180SCameron Grant { 120e4e61333SAriff Abdullah struct snddev_info *d; 12190da2b28SAriff Abdullah 12237209180SCameron Grant flags &= INTR_MPSAFE; 12346700f12SPeter Wemm flags |= INTR_TYPE_AV; 124e4e61333SAriff Abdullah d = device_get_softc(dev); 125e4e61333SAriff Abdullah if (d != NULL && (flags & INTR_MPSAFE)) 126e4e61333SAriff Abdullah d->flags |= SD_F_MPSAFE; 127e4e61333SAriff Abdullah 1282cc08b74SAriff Abdullah return bus_setup_intr(dev, res, flags, 1292cc08b74SAriff Abdullah #if __FreeBSD_version >= 700031 1302cc08b74SAriff Abdullah NULL, 1312cc08b74SAriff Abdullah #endif 1322cc08b74SAriff Abdullah hand, param, cookiep); 13337209180SCameron Grant } 13437209180SCameron Grant 135bba4862cSAriff Abdullah static void 136bba4862cSAriff Abdullah pcm_clonereset(struct snddev_info *d) 137a1d444e1SAriff Abdullah { 138bba4862cSAriff Abdullah int cmax; 139bba4862cSAriff Abdullah 140e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 141bba4862cSAriff Abdullah 142bba4862cSAriff Abdullah cmax = d->playcount + d->reccount - 1; 143bba4862cSAriff Abdullah if (d->pvchancount > 0) 14490da2b28SAriff Abdullah cmax += max(d->pvchancount, snd_maxautovchans) - 1; 145bba4862cSAriff Abdullah if (d->rvchancount > 0) 14690da2b28SAriff Abdullah cmax += max(d->rvchancount, snd_maxautovchans) - 1; 147bba4862cSAriff Abdullah if (cmax > PCMMAXCLONE) 148bba4862cSAriff Abdullah cmax = PCMMAXCLONE; 149bba4862cSAriff Abdullah (void)snd_clone_gc(d->clones); 150bba4862cSAriff Abdullah (void)snd_clone_setmaxunit(d->clones, cmax); 151bba4862cSAriff Abdullah } 152bba4862cSAriff Abdullah 15390da2b28SAriff Abdullah int 154bba4862cSAriff Abdullah pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num) 155bba4862cSAriff Abdullah { 156bba4862cSAriff Abdullah struct pcm_channel *c, *ch, *nch; 15790da2b28SAriff Abdullah struct pcmchan_caps *caps; 15890da2b28SAriff Abdullah int i, err, vcnt; 159bba4862cSAriff Abdullah 160e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 161a1d444e1SAriff Abdullah 162bba4862cSAriff Abdullah if ((direction == PCMDIR_PLAY && d->playcount < 1) || 163e4e61333SAriff Abdullah (direction == PCMDIR_REC && d->reccount < 1)) 164e4e61333SAriff Abdullah return (ENODEV); 165a580b31aSAriff Abdullah 166e4e61333SAriff Abdullah if (!(d->flags & SD_F_AUTOVCHAN)) 167e4e61333SAriff Abdullah return (EINVAL); 168a1d444e1SAriff Abdullah 169e4e61333SAriff Abdullah if (newcnt < 0 || newcnt > SND_MAXVCHANS) 170e4e61333SAriff Abdullah return (E2BIG); 171a1d444e1SAriff Abdullah 172bba4862cSAriff Abdullah if (direction == PCMDIR_PLAY) 173bba4862cSAriff Abdullah vcnt = d->pvchancount; 174bba4862cSAriff Abdullah else if (direction == PCMDIR_REC) 175bba4862cSAriff Abdullah vcnt = d->rvchancount; 176e4e61333SAriff Abdullah else 177e4e61333SAriff Abdullah return (EINVAL); 178a1d444e1SAriff Abdullah 179a1d444e1SAriff Abdullah if (newcnt > vcnt) { 180bba4862cSAriff Abdullah KASSERT(num == -1 || 181bba4862cSAriff Abdullah (num >= 0 && num < SND_MAXVCHANS && (newcnt - 1) == vcnt), 182bba4862cSAriff Abdullah ("bogus vchan_create() request num=%d newcnt=%d vcnt=%d", 183bba4862cSAriff Abdullah num, newcnt, vcnt)); 184a1d444e1SAriff Abdullah /* add new vchans - find a parent channel first */ 185e4e61333SAriff Abdullah ch = NULL; 186bba4862cSAriff Abdullah CHN_FOREACH(c, d, channels.pcm) { 187a1d444e1SAriff Abdullah CHN_LOCK(c); 188bba4862cSAriff Abdullah if (c->direction == direction && 189bba4862cSAriff Abdullah ((c->flags & CHN_F_HAS_VCHAN) || (vcnt == 0 && 19090da2b28SAriff Abdullah c->refcount < 1 && 191e4e61333SAriff Abdullah !(c->flags & (CHN_F_BUSY | CHN_F_VIRTUAL))))) { 19290da2b28SAriff Abdullah /* 19390da2b28SAriff Abdullah * Reuse hw channel with vchans already 19490da2b28SAriff Abdullah * created. 19590da2b28SAriff Abdullah */ 19690da2b28SAriff Abdullah if (c->flags & CHN_F_HAS_VCHAN) { 197e4e61333SAriff Abdullah ch = c; 198e4e61333SAriff Abdullah break; 199e4e61333SAriff Abdullah } 20090da2b28SAriff Abdullah /* 20190da2b28SAriff Abdullah * No vchans ever created, look for 20290da2b28SAriff Abdullah * channels with supported formats. 20390da2b28SAriff Abdullah */ 20490da2b28SAriff Abdullah caps = chn_getcaps(c); 20590da2b28SAriff Abdullah if (caps == NULL) { 20690da2b28SAriff Abdullah CHN_UNLOCK(c); 20790da2b28SAriff Abdullah continue; 20890da2b28SAriff Abdullah } 20990da2b28SAriff Abdullah for (i = 0; caps->fmtlist[i] != 0; i++) { 21090da2b28SAriff Abdullah if (caps->fmtlist[i] & AFMT_CONVERTIBLE) 21190da2b28SAriff Abdullah break; 21290da2b28SAriff Abdullah } 21390da2b28SAriff Abdullah if (caps->fmtlist[i] != 0) { 21490da2b28SAriff Abdullah ch = c; 21590da2b28SAriff Abdullah break; 21690da2b28SAriff Abdullah } 21790da2b28SAriff Abdullah } 218a1d444e1SAriff Abdullah CHN_UNLOCK(c); 219a1d444e1SAriff Abdullah } 220e4e61333SAriff Abdullah if (ch == NULL) 221e4e61333SAriff Abdullah return (EBUSY); 222e4e61333SAriff Abdullah ch->flags |= CHN_F_BUSY; 223e4e61333SAriff Abdullah err = 0; 224a1d444e1SAriff Abdullah while (err == 0 && newcnt > vcnt) { 225e4e61333SAriff Abdullah err = vchan_create(ch, num); 226bba4862cSAriff Abdullah if (err == 0) 227a1d444e1SAriff Abdullah vcnt++; 228bba4862cSAriff Abdullah else if (err == E2BIG && newcnt > vcnt) 229bba4862cSAriff Abdullah device_printf(d->dev, 230bba4862cSAriff Abdullah "%s: err=%d Maximum channel reached.\n", 231bba4862cSAriff Abdullah __func__, err); 232a1d444e1SAriff Abdullah } 233a1d444e1SAriff Abdullah if (vcnt == 0) 234e4e61333SAriff Abdullah ch->flags &= ~CHN_F_BUSY; 235e4e61333SAriff Abdullah CHN_UNLOCK(ch); 236e4e61333SAriff Abdullah if (err != 0) 237e4e61333SAriff Abdullah return (err); 238e4e61333SAriff Abdullah else 239bba4862cSAriff Abdullah pcm_clonereset(d); 240a1d444e1SAriff Abdullah } else if (newcnt < vcnt) { 241bba4862cSAriff Abdullah KASSERT(num == -1, 242bba4862cSAriff Abdullah ("bogus vchan_destroy() request num=%d", num)); 243bba4862cSAriff Abdullah CHN_FOREACH(c, d, channels.pcm) { 244a1d444e1SAriff Abdullah CHN_LOCK(c); 245bba4862cSAriff Abdullah if (c->direction != direction || 246bba4862cSAriff Abdullah CHN_EMPTY(c, children) || 247bba4862cSAriff Abdullah !(c->flags & CHN_F_HAS_VCHAN)) { 248a1d444e1SAriff Abdullah CHN_UNLOCK(c); 249bba4862cSAriff Abdullah continue; 250a1d444e1SAriff Abdullah } 251bba4862cSAriff Abdullah CHN_FOREACH_SAFE(ch, c, nch, children) { 252bba4862cSAriff Abdullah CHN_LOCK(ch); 25390da2b28SAriff Abdullah if (vcnt == 1 && c->refcount > 0) { 254bba4862cSAriff Abdullah CHN_UNLOCK(ch); 25590da2b28SAriff Abdullah break; 25690da2b28SAriff Abdullah } 25790da2b28SAriff Abdullah if (!(ch->flags & CHN_F_BUSY) && 25890da2b28SAriff Abdullah ch->refcount < 1) { 259bba4862cSAriff Abdullah err = vchan_destroy(ch); 260a1d444e1SAriff Abdullah if (err == 0) 261a1d444e1SAriff Abdullah vcnt--; 262bba4862cSAriff Abdullah } else 263bba4862cSAriff Abdullah CHN_UNLOCK(ch); 264e4e61333SAriff Abdullah if (vcnt == newcnt) 265bba4862cSAriff Abdullah break; 266a1d444e1SAriff Abdullah } 267bba4862cSAriff Abdullah CHN_UNLOCK(c); 268bba4862cSAriff Abdullah break; 269bba4862cSAriff Abdullah } 270bba4862cSAriff Abdullah pcm_clonereset(d); 271bba4862cSAriff Abdullah } 272a1d444e1SAriff Abdullah 273e4e61333SAriff Abdullah return (0); 274a1d444e1SAriff Abdullah } 275a1d444e1SAriff Abdullah 2763fdb3676SAriff Abdullah /* return error status and a locked channel */ 2773fdb3676SAriff Abdullah int 2783fdb3676SAriff Abdullah pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction, 27990da2b28SAriff Abdullah pid_t pid, char *comm, int devunit) 280285648f9SCameron Grant { 281285648f9SCameron Grant struct pcm_channel *c; 28290da2b28SAriff Abdullah int err, vchancount, vchan_num; 283bba4862cSAriff Abdullah 284bba4862cSAriff Abdullah KASSERT(d != NULL && ch != NULL && (devunit == -1 || 285bba4862cSAriff Abdullah !(devunit & ~(SND_U_MASK | SND_D_MASK | SND_C_MASK))) && 286bba4862cSAriff Abdullah (direction == PCMDIR_PLAY || direction == PCMDIR_REC), 287e4e61333SAriff Abdullah ("%s(): invalid d=%p ch=%p direction=%d pid=%d devunit=%d", 288bba4862cSAriff Abdullah __func__, d, ch, direction, pid, devunit)); 289e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 290bba4862cSAriff Abdullah 291bba4862cSAriff Abdullah /* Double check again. */ 292bba4862cSAriff Abdullah if (devunit != -1) { 293bba4862cSAriff Abdullah switch (snd_unit2d(devunit)) { 294bba4862cSAriff Abdullah case SND_DEV_DSPHW_PLAY: 295bba4862cSAriff Abdullah case SND_DEV_DSPHW_VPLAY: 296bba4862cSAriff Abdullah if (direction != PCMDIR_PLAY) 29790da2b28SAriff Abdullah return (ENOTSUP); 298bba4862cSAriff Abdullah break; 299bba4862cSAriff Abdullah case SND_DEV_DSPHW_REC: 300bba4862cSAriff Abdullah case SND_DEV_DSPHW_VREC: 301bba4862cSAriff Abdullah if (direction != PCMDIR_REC) 30290da2b28SAriff Abdullah return (ENOTSUP); 303bba4862cSAriff Abdullah break; 304bba4862cSAriff Abdullah default: 305bba4862cSAriff Abdullah if (!(direction == PCMDIR_PLAY || 306bba4862cSAriff Abdullah direction == PCMDIR_REC)) 30790da2b28SAriff Abdullah return (ENOTSUP); 308bba4862cSAriff Abdullah break; 309bba4862cSAriff Abdullah } 310bba4862cSAriff Abdullah } 311285648f9SCameron Grant 31290da2b28SAriff Abdullah *ch = NULL; 31390da2b28SAriff Abdullah vchan_num = 0; 31490da2b28SAriff Abdullah vchancount = (direction == PCMDIR_PLAY) ? d->pvchancount : 31590da2b28SAriff Abdullah d->rvchancount; 31690da2b28SAriff Abdullah 3173fdb3676SAriff Abdullah retry_chnalloc: 31890da2b28SAriff Abdullah err = ENOTSUP; 319f637a36cSCameron Grant /* scan for a free channel */ 320bba4862cSAriff Abdullah CHN_FOREACH(c, d, channels.pcm) { 32149c5e6e2SCameron Grant CHN_LOCK(c); 32290da2b28SAriff Abdullah if (devunit == -1 && c->direction == direction && 32390da2b28SAriff Abdullah (c->flags & CHN_F_VIRTUAL)) { 32490da2b28SAriff Abdullah if (vchancount < snd_maxautovchans && 32590da2b28SAriff Abdullah vchan_num < CHN_CHAN(c)) { 32690da2b28SAriff Abdullah CHN_UNLOCK(c); 32790da2b28SAriff Abdullah goto vchan_alloc; 32890da2b28SAriff Abdullah } 32990da2b28SAriff Abdullah vchan_num++; 33090da2b28SAriff Abdullah } 331bba4862cSAriff Abdullah if (c->direction == direction && !(c->flags & CHN_F_BUSY) && 332bba4862cSAriff Abdullah (devunit == -1 || devunit == -2 || c->unit == devunit)) { 333285648f9SCameron Grant c->flags |= CHN_F_BUSY; 334b8f0d9e0SCameron Grant c->pid = pid; 33590da2b28SAriff Abdullah strlcpy(c->comm, (comm != NULL) ? comm : 33690da2b28SAriff Abdullah CHN_COMM_UNKNOWN, sizeof(c->comm)); 3373fdb3676SAriff Abdullah *ch = c; 338bba4862cSAriff Abdullah return (0); 339bba4862cSAriff Abdullah } else if (c->unit == devunit) { 3403fdb3676SAriff Abdullah if (c->direction != direction) 34190da2b28SAriff Abdullah err = ENOTSUP; 3423fdb3676SAriff Abdullah else if (c->flags & CHN_F_BUSY) 3433fdb3676SAriff Abdullah err = EBUSY; 3443fdb3676SAriff Abdullah else 3453fdb3676SAriff Abdullah err = EINVAL; 3463fdb3676SAriff Abdullah CHN_UNLOCK(c); 347bba4862cSAriff Abdullah return (err); 348bba4862cSAriff Abdullah } else if ((devunit == -1 || devunit == -2) && 349bba4862cSAriff Abdullah c->direction == direction && (c->flags & CHN_F_BUSY)) 350a1d444e1SAriff Abdullah err = EBUSY; 35149c5e6e2SCameron Grant CHN_UNLOCK(c); 352285648f9SCameron Grant } 353f637a36cSCameron Grant 354e4e61333SAriff Abdullah if (devunit == -2) 355e4e61333SAriff Abdullah return (err); 356e4e61333SAriff Abdullah 35790da2b28SAriff Abdullah vchan_alloc: 358f637a36cSCameron Grant /* no channel available */ 359e4e61333SAriff Abdullah if (devunit == -1 || snd_unit2d(devunit) == SND_DEV_DSPHW_VPLAY || 360e4e61333SAriff Abdullah snd_unit2d(devunit) == SND_DEV_DSPHW_VREC) { 361bba4862cSAriff Abdullah if (!(vchancount > 0 && vchancount < snd_maxautovchans) && 362bba4862cSAriff Abdullah (devunit == -1 || snd_unit2c(devunit) < snd_maxautovchans)) 363bba4862cSAriff Abdullah return (err); 364bba4862cSAriff Abdullah err = pcm_setvchans(d, direction, vchancount + 1, 365bba4862cSAriff Abdullah (devunit == -1) ? -1 : snd_unit2c(devunit)); 366a1d444e1SAriff Abdullah if (err == 0) { 367bba4862cSAriff Abdullah if (devunit == -1) 368bba4862cSAriff Abdullah devunit = -2; 3693fdb3676SAriff Abdullah goto retry_chnalloc; 370f637a36cSCameron Grant } 371f637a36cSCameron Grant } 372f637a36cSCameron Grant 373bba4862cSAriff Abdullah return (err); 374285648f9SCameron Grant } 375285648f9SCameron Grant 376b8f0d9e0SCameron Grant /* release a locked channel and unlock it */ 377285648f9SCameron Grant int 378b8f0d9e0SCameron Grant pcm_chnrelease(struct pcm_channel *c) 379285648f9SCameron Grant { 380e4e61333SAriff Abdullah PCM_BUSYASSERT(c->parentsnddev); 381b8f0d9e0SCameron Grant CHN_LOCKASSERT(c); 382e4e61333SAriff Abdullah 383285648f9SCameron Grant c->flags &= ~CHN_F_BUSY; 384b8f0d9e0SCameron Grant c->pid = -1; 38590da2b28SAriff Abdullah strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm)); 38649c5e6e2SCameron Grant CHN_UNLOCK(c); 387e4e61333SAriff Abdullah 388e4e61333SAriff Abdullah return (0); 389285648f9SCameron Grant } 390285648f9SCameron Grant 391285648f9SCameron Grant int 392285648f9SCameron Grant pcm_chnref(struct pcm_channel *c, int ref) 393285648f9SCameron Grant { 394e4e61333SAriff Abdullah PCM_BUSYASSERT(c->parentsnddev); 395b8f0d9e0SCameron Grant CHN_LOCKASSERT(c); 396e4e61333SAriff Abdullah 397285648f9SCameron Grant c->refcount += ref; 398e4e61333SAriff Abdullah 399e4e61333SAriff Abdullah return (c->refcount); 400285648f9SCameron Grant } 401285648f9SCameron Grant 40267b1dce3SCameron Grant int 40367b1dce3SCameron Grant pcm_inprog(struct snddev_info *d, int delta) 40467b1dce3SCameron Grant { 40590da2b28SAriff Abdullah PCM_LOCKASSERT(d); 406a527dbc7SCameron Grant 407a527dbc7SCameron Grant d->inprog += delta; 408e4e61333SAriff Abdullah 409e4e61333SAriff Abdullah return (d->inprog); 41067b1dce3SCameron Grant } 41167b1dce3SCameron Grant 41267b1dce3SCameron Grant static void 41367b1dce3SCameron Grant pcm_setmaxautovchans(struct snddev_info *d, int num) 41467b1dce3SCameron Grant { 415e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 416e4e61333SAriff Abdullah 417bba4862cSAriff Abdullah if (num < 0) 418bba4862cSAriff Abdullah return; 419bba4862cSAriff Abdullah 420bba4862cSAriff Abdullah if (num >= 0 && d->pvchancount > num) 421bba4862cSAriff Abdullah (void)pcm_setvchans(d, PCMDIR_PLAY, num, -1); 422bba4862cSAriff Abdullah else if (num > 0 && d->pvchancount == 0) 423bba4862cSAriff Abdullah (void)pcm_setvchans(d, PCMDIR_PLAY, 1, -1); 424bba4862cSAriff Abdullah 425bba4862cSAriff Abdullah if (num >= 0 && d->rvchancount > num) 426bba4862cSAriff Abdullah (void)pcm_setvchans(d, PCMDIR_REC, num, -1); 427bba4862cSAriff Abdullah else if (num > 0 && d->rvchancount == 0) 428bba4862cSAriff Abdullah (void)pcm_setvchans(d, PCMDIR_REC, 1, -1); 429bba4862cSAriff Abdullah 430bba4862cSAriff Abdullah pcm_clonereset(d); 43167b1dce3SCameron Grant } 43267b1dce3SCameron Grant 43333dbf14aSCameron Grant static int 434851a904aSAlexander Leidinger sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS) 43533dbf14aSCameron Grant { 436b8f0d9e0SCameron Grant struct snddev_info *d; 43733dbf14aSCameron Grant int error, unit; 43833dbf14aSCameron Grant 43933dbf14aSCameron Grant unit = snd_unit; 440041b706bSDavid Malone error = sysctl_handle_int(oidp, &unit, 0, req); 44133dbf14aSCameron Grant if (error == 0 && req->newptr != NULL) { 442b8f0d9e0SCameron Grant d = devclass_get_softc(pcm_devclass, unit); 443e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm)) 444b8f0d9e0SCameron Grant return EINVAL; 44533dbf14aSCameron Grant snd_unit = unit; 446*cbebc90dSAlexander Motin snd_unit_auto = 0; 44733dbf14aSCameron Grant } 44833dbf14aSCameron Grant return (error); 44933dbf14aSCameron Grant } 450851a904aSAlexander Leidinger /* XXX: do we need a way to let the user change the default unit? */ 451851a904aSAlexander Leidinger SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit, CTLTYPE_INT | CTLFLAG_RW, 452a580b31aSAriff Abdullah 0, sizeof(int), sysctl_hw_snd_default_unit, "I", "default sound device"); 453987e5972SCameron Grant 454cd9766c5SCameron Grant static int 45567b1dce3SCameron Grant sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS) 456cd9766c5SCameron Grant { 45767b1dce3SCameron Grant struct snddev_info *d; 45867b1dce3SCameron Grant int i, v, error; 459cd9766c5SCameron Grant 46067b1dce3SCameron Grant v = snd_maxautovchans; 461041b706bSDavid Malone error = sysctl_handle_int(oidp, &v, 0, req); 462cd9766c5SCameron Grant if (error == 0 && req->newptr != NULL) { 463bba4862cSAriff Abdullah if (v < 0) 464bba4862cSAriff Abdullah v = 0; 465bba4862cSAriff Abdullah if (v > SND_MAXVCHANS) 466bba4862cSAriff Abdullah v = SND_MAXVCHANS; 467bba4862cSAriff Abdullah snd_maxautovchans = v; 4689c271f79SAriff Abdullah for (i = 0; pcm_devclass != NULL && 4699c271f79SAriff Abdullah i < devclass_get_maxunit(pcm_devclass); i++) { 47067b1dce3SCameron Grant d = devclass_get_softc(pcm_devclass, i); 471e4e61333SAriff Abdullah if (!PCM_REGISTERED(d)) 47267b1dce3SCameron Grant continue; 473e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 47467b1dce3SCameron Grant pcm_setmaxautovchans(d, v); 475e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 47667b1dce3SCameron Grant } 47767b1dce3SCameron Grant } 478cd9766c5SCameron Grant return (error); 479cd9766c5SCameron Grant } 48067b1dce3SCameron Grant SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans, CTLTYPE_INT | CTLFLAG_RW, 481a580b31aSAriff Abdullah 0, sizeof(int), sysctl_hw_snd_maxautovchans, "I", "maximum virtual channel"); 482f637a36cSCameron Grant 483285648f9SCameron Grant struct pcm_channel * 484bba4862cSAriff Abdullah pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo) 485987e5972SCameron Grant { 486bba4862cSAriff Abdullah struct pcm_channel *ch; 487bba4862cSAriff Abdullah int direction, err, rpnum, *pnum, max; 488bba4862cSAriff Abdullah int udc, device, chan; 489bba4862cSAriff Abdullah char *dirs, *devname, buf[CHN_NAMELEN]; 490bba4862cSAriff Abdullah 491e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 49290da2b28SAriff Abdullah PCM_LOCKASSERT(d); 493bba4862cSAriff Abdullah KASSERT(num >= -1, ("invalid num=%d", num)); 494bba4862cSAriff Abdullah 495987e5972SCameron Grant 496285648f9SCameron Grant switch (dir) { 497285648f9SCameron Grant case PCMDIR_PLAY: 498285648f9SCameron Grant dirs = "play"; 499a3193a9cSDon Lewis direction = PCMDIR_PLAY; 500a67fe5c1SCameron Grant pnum = &d->playcount; 501bba4862cSAriff Abdullah device = SND_DEV_DSPHW_PLAY; 502bba4862cSAriff Abdullah max = SND_MAXHWCHAN; 503285648f9SCameron Grant break; 504bba4862cSAriff Abdullah case PCMDIR_PLAY_VIRTUAL: 505bba4862cSAriff Abdullah dirs = "virtual"; 506bba4862cSAriff Abdullah direction = PCMDIR_PLAY; 507bba4862cSAriff Abdullah pnum = &d->pvchancount; 508bba4862cSAriff Abdullah device = SND_DEV_DSPHW_VPLAY; 509bba4862cSAriff Abdullah max = SND_MAXVCHANS; 510bba4862cSAriff Abdullah break; 511285648f9SCameron Grant case PCMDIR_REC: 512285648f9SCameron Grant dirs = "record"; 513a3193a9cSDon Lewis direction = PCMDIR_REC; 514a67fe5c1SCameron Grant pnum = &d->reccount; 515bba4862cSAriff Abdullah device = SND_DEV_DSPHW_REC; 516bba4862cSAriff Abdullah max = SND_MAXHWCHAN; 517285648f9SCameron Grant break; 518bba4862cSAriff Abdullah case PCMDIR_REC_VIRTUAL: 519285648f9SCameron Grant dirs = "virtual"; 520bba4862cSAriff Abdullah direction = PCMDIR_REC; 521bba4862cSAriff Abdullah pnum = &d->rvchancount; 522bba4862cSAriff Abdullah device = SND_DEV_DSPHW_VREC; 523bba4862cSAriff Abdullah max = SND_MAXVCHANS; 524285648f9SCameron Grant break; 525285648f9SCameron Grant default: 526e4e61333SAriff Abdullah return (NULL); 5279c326820SCameron Grant } 528285648f9SCameron Grant 529bba4862cSAriff Abdullah chan = (num == -1) ? 0 : num; 530285648f9SCameron Grant 531e4e61333SAriff Abdullah if (*pnum >= max || chan >= max) 532e4e61333SAriff Abdullah return (NULL); 533bba4862cSAriff Abdullah 5343fdb3676SAriff Abdullah rpnum = 0; 535bba4862cSAriff Abdullah 536bba4862cSAriff Abdullah CHN_FOREACH(ch, d, channels.pcm) { 537bba4862cSAriff Abdullah if (CHN_DEV(ch) != device) 5383fdb3676SAriff Abdullah continue; 539bba4862cSAriff Abdullah if (chan == CHN_CHAN(ch)) { 540bba4862cSAriff Abdullah if (num != -1) { 5413fdb3676SAriff Abdullah device_printf(d->dev, 542bba4862cSAriff Abdullah "channel num=%d allocated!\n", chan); 543e4e61333SAriff Abdullah return (NULL); 544bba4862cSAriff Abdullah } 545bba4862cSAriff Abdullah chan++; 546bba4862cSAriff Abdullah if (chan >= max) { 547bba4862cSAriff Abdullah device_printf(d->dev, 548bba4862cSAriff Abdullah "chan=%d > %d\n", chan, max); 549e4e61333SAriff Abdullah return (NULL); 550bba4862cSAriff Abdullah } 5513fdb3676SAriff Abdullah } 5523fdb3676SAriff Abdullah rpnum++; 5533fdb3676SAriff Abdullah } 554bba4862cSAriff Abdullah 5553fdb3676SAriff Abdullah if (*pnum != rpnum) { 5563fdb3676SAriff Abdullah device_printf(d->dev, 557bba4862cSAriff Abdullah "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n", 5583fdb3676SAriff Abdullah __func__, dirs, *pnum, rpnum); 559e4e61333SAriff Abdullah return (NULL); 5603fdb3676SAriff Abdullah } 561a67fe5c1SCameron Grant 562bba4862cSAriff Abdullah udc = snd_mkunit(device_get_unit(d->dev), device, chan); 563bba4862cSAriff Abdullah devname = dsp_unit2name(buf, sizeof(buf), udc); 564bba4862cSAriff Abdullah 565bba4862cSAriff Abdullah if (devname == NULL) { 566bba4862cSAriff Abdullah device_printf(d->dev, 567bba4862cSAriff Abdullah "Failed to query device name udc=0x%08x\n", udc); 568e4e61333SAriff Abdullah return (NULL); 569bba4862cSAriff Abdullah } 570bba4862cSAriff Abdullah 57190da2b28SAriff Abdullah PCM_UNLOCK(d); 572bba4862cSAriff Abdullah ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO); 573bba4862cSAriff Abdullah ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO); 574bba4862cSAriff Abdullah ch->unit = udc; 575285648f9SCameron Grant ch->pid = -1; 57690da2b28SAriff Abdullah strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm)); 577285648f9SCameron Grant ch->parentsnddev = d; 578285648f9SCameron Grant ch->parentchannel = parent; 579436c9b65SScott Long ch->dev = d->dev; 580bba4862cSAriff Abdullah ch->trigger = PCMTRIG_STOP; 581bba4862cSAriff Abdullah snprintf(ch->name, sizeof(ch->name), "%s:%s:%s", 582bba4862cSAriff Abdullah device_get_nameunit(ch->dev), dirs, devname); 583285648f9SCameron Grant 584a3193a9cSDon Lewis err = chn_init(ch, devinfo, dir, direction); 58590da2b28SAriff Abdullah PCM_LOCK(d); 5860f55ac6cSCameron Grant if (err) { 587bba4862cSAriff Abdullah device_printf(d->dev, "chn_init(%s) failed: err = %d\n", 588bba4862cSAriff Abdullah ch->name, err); 589285648f9SCameron Grant kobj_delete(ch->methods, M_DEVBUF); 590285648f9SCameron Grant free(ch, M_DEVBUF); 591e4e61333SAriff Abdullah return (NULL); 592bbb5bf3dSCameron Grant } 593285648f9SCameron Grant 594e4e61333SAriff Abdullah return (ch); 595285648f9SCameron Grant } 596285648f9SCameron Grant 597285648f9SCameron Grant int 598285648f9SCameron Grant pcm_chn_destroy(struct pcm_channel *ch) 599285648f9SCameron Grant { 600a67fe5c1SCameron Grant struct snddev_info *d; 601285648f9SCameron Grant int err; 602285648f9SCameron Grant 603a67fe5c1SCameron Grant d = ch->parentsnddev; 604e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 605e4e61333SAriff Abdullah 606285648f9SCameron Grant err = chn_kill(ch); 607285648f9SCameron Grant if (err) { 608e4e61333SAriff Abdullah device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n", 609e4e61333SAriff Abdullah ch->name, err); 610e4e61333SAriff Abdullah return (err); 611285648f9SCameron Grant } 612285648f9SCameron Grant 613285648f9SCameron Grant kobj_delete(ch->methods, M_DEVBUF); 614285648f9SCameron Grant free(ch, M_DEVBUF); 615285648f9SCameron Grant 616e4e61333SAriff Abdullah return (0); 617285648f9SCameron Grant } 618285648f9SCameron Grant 619285648f9SCameron Grant int 6205ee30e27SMathew Kanner pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch) 621285648f9SCameron Grant { 622e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 62390da2b28SAriff Abdullah PCM_LOCKASSERT(d); 624bba4862cSAriff Abdullah KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY || 625bba4862cSAriff Abdullah ch->direction == PCMDIR_REC), ("Invalid pcm channel")); 626285648f9SCameron Grant 62790da2b28SAriff Abdullah CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm); 6283fdb3676SAriff Abdullah 629e4e61333SAriff Abdullah switch (CHN_DEV(ch)) { 630e4e61333SAriff Abdullah case SND_DEV_DSPHW_PLAY: 631e4e61333SAriff Abdullah d->playcount++; 632e4e61333SAriff Abdullah break; 633e4e61333SAriff Abdullah case SND_DEV_DSPHW_VPLAY: 634e4e61333SAriff Abdullah d->pvchancount++; 635e4e61333SAriff Abdullah break; 636e4e61333SAriff Abdullah case SND_DEV_DSPHW_REC: 637e4e61333SAriff Abdullah d->reccount++; 638e4e61333SAriff Abdullah break; 639e4e61333SAriff Abdullah case SND_DEV_DSPHW_VREC: 640e4e61333SAriff Abdullah d->rvchancount++; 641e4e61333SAriff Abdullah break; 642e4e61333SAriff Abdullah default: 643e4e61333SAriff Abdullah break; 644e4e61333SAriff Abdullah } 645b8f0d9e0SCameron Grant 646e4e61333SAriff Abdullah d->devcount++; 647e4e61333SAriff Abdullah 648e4e61333SAriff Abdullah return (0); 64933dbf14aSCameron Grant } 65033dbf14aSCameron Grant 651285648f9SCameron Grant int 6525ee30e27SMathew Kanner pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch) 65333dbf14aSCameron Grant { 654bba4862cSAriff Abdullah struct pcm_channel *tmp; 65533dbf14aSCameron Grant 656e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 65790da2b28SAriff Abdullah PCM_LOCKASSERT(d); 658e4e61333SAriff Abdullah 659bba4862cSAriff Abdullah tmp = NULL; 660a527dbc7SCameron Grant 661bba4862cSAriff Abdullah CHN_FOREACH(tmp, d, channels.pcm) { 662bba4862cSAriff Abdullah if (tmp == ch) 663bba4862cSAriff Abdullah break; 66433dbf14aSCameron Grant } 665bba4862cSAriff Abdullah 666bba4862cSAriff Abdullah if (tmp != ch) 667e4e61333SAriff Abdullah return (EINVAL); 66867beb5a5SCameron Grant 669bba4862cSAriff Abdullah CHN_REMOVE(d, ch, channels.pcm); 670e4e61333SAriff Abdullah 671bba4862cSAriff Abdullah switch (CHN_DEV(ch)) { 672bba4862cSAriff Abdullah case SND_DEV_DSPHW_PLAY: 67367beb5a5SCameron Grant d->playcount--; 674bba4862cSAriff Abdullah break; 675bba4862cSAriff Abdullah case SND_DEV_DSPHW_VPLAY: 676bba4862cSAriff Abdullah d->pvchancount--; 677bba4862cSAriff Abdullah break; 678bba4862cSAriff Abdullah case SND_DEV_DSPHW_REC: 679bba4862cSAriff Abdullah d->reccount--; 680bba4862cSAriff Abdullah break; 681bba4862cSAriff Abdullah case SND_DEV_DSPHW_VREC: 682bba4862cSAriff Abdullah d->rvchancount--; 683bba4862cSAriff Abdullah break; 684bba4862cSAriff Abdullah default: 685bba4862cSAriff Abdullah break; 686bba4862cSAriff Abdullah } 687285648f9SCameron Grant 688e4e61333SAriff Abdullah d->devcount--; 689e4e61333SAriff Abdullah 690e4e61333SAriff Abdullah return (0); 691987e5972SCameron Grant } 692987e5972SCameron Grant 693987e5972SCameron Grant int 694285648f9SCameron Grant pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo) 695285648f9SCameron Grant { 696285648f9SCameron Grant struct snddev_info *d = device_get_softc(dev); 69767b1dce3SCameron Grant struct pcm_channel *ch; 69867b1dce3SCameron Grant int err; 699285648f9SCameron Grant 700e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 701e4e61333SAriff Abdullah 70290da2b28SAriff Abdullah PCM_LOCK(d); 703bba4862cSAriff Abdullah ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo); 704285648f9SCameron Grant if (!ch) { 705e4e61333SAriff Abdullah device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n", 706e4e61333SAriff Abdullah cls->name, dir, devinfo); 70790da2b28SAriff Abdullah PCM_UNLOCK(d); 708e4e61333SAriff Abdullah return (ENODEV); 709285648f9SCameron Grant } 710cd9766c5SCameron Grant 7115ee30e27SMathew Kanner err = pcm_chn_add(d, ch); 71290da2b28SAriff Abdullah PCM_UNLOCK(d); 713285648f9SCameron Grant if (err) { 714e4e61333SAriff Abdullah device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n", 715e4e61333SAriff Abdullah ch->name, err); 716285648f9SCameron Grant pcm_chn_destroy(ch); 717cd9766c5SCameron Grant } 718cd9766c5SCameron Grant 719e4e61333SAriff Abdullah return (err); 720285648f9SCameron Grant } 721285648f9SCameron Grant 722285648f9SCameron Grant static int 723285648f9SCameron Grant pcm_killchan(device_t dev) 724285648f9SCameron Grant { 725285648f9SCameron Grant struct snddev_info *d = device_get_softc(dev); 726e33bee07SOlivier Houchard struct pcm_channel *ch; 727e4e61333SAriff Abdullah int error; 728e4e61333SAriff Abdullah 729e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 730285648f9SCameron Grant 731bba4862cSAriff Abdullah ch = CHN_FIRST(d, channels.pcm); 732285648f9SCameron Grant 73390da2b28SAriff Abdullah PCM_LOCK(d); 734bba4862cSAriff Abdullah error = pcm_chn_remove(d, ch); 73590da2b28SAriff Abdullah PCM_UNLOCK(d); 736e33bee07SOlivier Houchard if (error) 737e33bee07SOlivier Houchard return (error); 738e33bee07SOlivier Houchard return (pcm_chn_destroy(ch)); 739285648f9SCameron Grant } 740285648f9SCameron Grant 741*cbebc90dSAlexander Motin static int 742*cbebc90dSAlexander Motin pcm_best_unit(int old) 743*cbebc90dSAlexander Motin { 744*cbebc90dSAlexander Motin struct snddev_info *d; 745*cbebc90dSAlexander Motin int i, best, bestprio, prio; 746*cbebc90dSAlexander Motin 747*cbebc90dSAlexander Motin best = -1; 748*cbebc90dSAlexander Motin bestprio = -100; 749*cbebc90dSAlexander Motin for (i = 0; pcm_devclass != NULL && 750*cbebc90dSAlexander Motin i < devclass_get_maxunit(pcm_devclass); i++) { 751*cbebc90dSAlexander Motin d = devclass_get_softc(pcm_devclass, i); 752*cbebc90dSAlexander Motin if (!PCM_REGISTERED(d)) 753*cbebc90dSAlexander Motin continue; 754*cbebc90dSAlexander Motin prio = 0; 755*cbebc90dSAlexander Motin if (d->playcount == 0) 756*cbebc90dSAlexander Motin prio -= 10; 757*cbebc90dSAlexander Motin if (d->reccount == 0) 758*cbebc90dSAlexander Motin prio -= 2; 759*cbebc90dSAlexander Motin if (prio > bestprio || (prio == bestprio && i == old)) { 760*cbebc90dSAlexander Motin best = i; 761*cbebc90dSAlexander Motin bestprio = prio; 762*cbebc90dSAlexander Motin } 763*cbebc90dSAlexander Motin } 764*cbebc90dSAlexander Motin return (best); 765*cbebc90dSAlexander Motin } 766*cbebc90dSAlexander Motin 767285648f9SCameron Grant int 768987e5972SCameron Grant pcm_setstatus(device_t dev, char *str) 769987e5972SCameron Grant { 77066ef8af5SCameron Grant struct snddev_info *d = device_get_softc(dev); 77149c5e6e2SCameron Grant 772e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 773bba4862cSAriff Abdullah 774e4e61333SAriff Abdullah if (d->playcount == 0 || d->reccount == 0) 775e4e61333SAriff Abdullah d->flags |= SD_F_SIMPLEX; 776e4e61333SAriff Abdullah 777e4e61333SAriff Abdullah if ((d->playcount > 0 || d->reccount > 0) && 778e4e61333SAriff Abdullah !(d->flags & SD_F_AUTOVCHAN)) { 779e4e61333SAriff Abdullah d->flags |= SD_F_AUTOVCHAN; 780e4e61333SAriff Abdullah vchan_initsys(dev); 781e4e61333SAriff Abdullah } 782e4e61333SAriff Abdullah 783e4e61333SAriff Abdullah pcm_setmaxautovchans(d, snd_maxautovchans); 784bba4862cSAriff Abdullah 785a580b31aSAriff Abdullah strlcpy(d->status, str, SND_STATUSLEN); 786bba4862cSAriff Abdullah 78790da2b28SAriff Abdullah PCM_LOCK(d); 788e4e61333SAriff Abdullah 789bba4862cSAriff Abdullah /* Last stage, enable cloning. */ 790e4e61333SAriff Abdullah if (d->clones != NULL) 791bba4862cSAriff Abdullah (void)snd_clone_enable(d->clones); 792e4e61333SAriff Abdullah 793e4e61333SAriff Abdullah /* Done, we're ready.. */ 794e4e61333SAriff Abdullah d->flags |= SD_F_REGISTERED; 795e4e61333SAriff Abdullah 796e4e61333SAriff Abdullah PCM_RELEASE(d); 797bba4862cSAriff Abdullah 79890da2b28SAriff Abdullah PCM_UNLOCK(d); 799bba4862cSAriff Abdullah 800*cbebc90dSAlexander Motin if (snd_unit_auto < 0) 801*cbebc90dSAlexander Motin snd_unit_auto = (snd_unit < 0) ? 1 : 0; 802*cbebc90dSAlexander Motin if (snd_unit < 0 || snd_unit_auto > 1) 803f3685841SAriff Abdullah snd_unit = device_get_unit(dev); 804*cbebc90dSAlexander Motin else if (snd_unit_auto == 1) 805*cbebc90dSAlexander Motin snd_unit = pcm_best_unit(snd_unit); 806f3685841SAriff Abdullah 807e4e61333SAriff Abdullah return (0); 808987e5972SCameron Grant } 809987e5972SCameron Grant 810a1d444e1SAriff Abdullah uint32_t 811987e5972SCameron Grant pcm_getflags(device_t dev) 812987e5972SCameron Grant { 81366ef8af5SCameron Grant struct snddev_info *d = device_get_softc(dev); 81449c5e6e2SCameron Grant 815987e5972SCameron Grant return d->flags; 816987e5972SCameron Grant } 817987e5972SCameron Grant 818987e5972SCameron Grant void 819a1d444e1SAriff Abdullah pcm_setflags(device_t dev, uint32_t val) 820987e5972SCameron Grant { 82166ef8af5SCameron Grant struct snddev_info *d = device_get_softc(dev); 822d95502a8SCameron Grant 823987e5972SCameron Grant d->flags = val; 824987e5972SCameron Grant } 825987e5972SCameron Grant 82639004e69SCameron Grant void * 82739004e69SCameron Grant pcm_getdevinfo(device_t dev) 82839004e69SCameron Grant { 82966ef8af5SCameron Grant struct snddev_info *d = device_get_softc(dev); 83049c5e6e2SCameron Grant 83139004e69SCameron Grant return d->devinfo; 83239004e69SCameron Grant } 83339004e69SCameron Grant 834a67fe5c1SCameron Grant unsigned int 835d55d96f6SAlexander Leidinger pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz) 836a67fe5c1SCameron Grant { 837a67fe5c1SCameron Grant struct snddev_info *d = device_get_softc(dev); 8384e60be34SCameron Grant int sz, x; 839a67fe5c1SCameron Grant 840a67fe5c1SCameron Grant sz = 0; 8414e60be34SCameron Grant if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) { 8424e60be34SCameron Grant x = sz; 843d55d96f6SAlexander Leidinger RANGE(sz, minbufsz, maxbufsz); 8444e60be34SCameron Grant if (x != sz) 845d55d96f6SAlexander Leidinger device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz); 846d55d96f6SAlexander Leidinger x = minbufsz; 8474e60be34SCameron Grant while (x < sz) 8484e60be34SCameron Grant x <<= 1; 8494e60be34SCameron Grant if (x > sz) 8504e60be34SCameron Grant x >>= 1; 8514e60be34SCameron Grant if (x != sz) { 8524e60be34SCameron Grant device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x); 8534e60be34SCameron Grant sz = x; 8544e60be34SCameron Grant } 8554e60be34SCameron Grant } else { 856a67fe5c1SCameron Grant sz = deflt; 8574e60be34SCameron Grant } 8584e60be34SCameron Grant 859a67fe5c1SCameron Grant d->bufsz = sz; 860a67fe5c1SCameron Grant 861a67fe5c1SCameron Grant return sz; 862a67fe5c1SCameron Grant } 863a67fe5c1SCameron Grant 86490da2b28SAriff Abdullah static int 86590da2b28SAriff Abdullah sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS) 86690da2b28SAriff Abdullah { 86790da2b28SAriff Abdullah struct snddev_info *d; 86890da2b28SAriff Abdullah int err, val; 86990da2b28SAriff Abdullah 87090da2b28SAriff Abdullah d = oidp->oid_arg1; 87190da2b28SAriff Abdullah if (!PCM_REGISTERED(d)) 87290da2b28SAriff Abdullah return (ENODEV); 87390da2b28SAriff Abdullah 87490da2b28SAriff Abdullah PCM_LOCK(d); 87590da2b28SAriff Abdullah PCM_WAIT(d); 87690da2b28SAriff Abdullah val = (d->flags & SD_F_BITPERFECT) ? 1 : 0; 87790da2b28SAriff Abdullah PCM_ACQUIRE(d); 87890da2b28SAriff Abdullah PCM_UNLOCK(d); 87990da2b28SAriff Abdullah 88090da2b28SAriff Abdullah err = sysctl_handle_int(oidp, &val, 0, req); 88190da2b28SAriff Abdullah 88290da2b28SAriff Abdullah if (err == 0 && req->newptr != NULL) { 88390da2b28SAriff Abdullah if (!(val == 0 || val == 1)) { 88490da2b28SAriff Abdullah PCM_RELEASE_QUICK(d); 88590da2b28SAriff Abdullah return (EINVAL); 88690da2b28SAriff Abdullah } 88790da2b28SAriff Abdullah 88890da2b28SAriff Abdullah PCM_LOCK(d); 88990da2b28SAriff Abdullah 89090da2b28SAriff Abdullah d->flags &= ~SD_F_BITPERFECT; 89190da2b28SAriff Abdullah d->flags |= (val != 0) ? SD_F_BITPERFECT : 0; 89290da2b28SAriff Abdullah 89390da2b28SAriff Abdullah PCM_RELEASE(d); 89490da2b28SAriff Abdullah PCM_UNLOCK(d); 89590da2b28SAriff Abdullah } else 89690da2b28SAriff Abdullah PCM_RELEASE_QUICK(d); 89790da2b28SAriff Abdullah 89890da2b28SAriff Abdullah return (err); 89990da2b28SAriff Abdullah } 90090da2b28SAriff Abdullah 90190da2b28SAriff Abdullah #ifdef SND_DEBUG 902bba4862cSAriff Abdullah static int 903bba4862cSAriff Abdullah sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS) 904bba4862cSAriff Abdullah { 905bba4862cSAriff Abdullah struct snddev_info *d; 906bba4862cSAriff Abdullah uint32_t flags; 907bba4862cSAriff Abdullah int err; 908bba4862cSAriff Abdullah 909bba4862cSAriff Abdullah d = oidp->oid_arg1; 910e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || d->clones == NULL) 911bba4862cSAriff Abdullah return (ENODEV); 912bba4862cSAriff Abdullah 913e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 914e4e61333SAriff Abdullah 915bba4862cSAriff Abdullah flags = snd_clone_getflags(d->clones); 916041b706bSDavid Malone err = sysctl_handle_int(oidp, &flags, 0, req); 917bba4862cSAriff Abdullah 918bba4862cSAriff Abdullah if (err == 0 && req->newptr != NULL) { 919e4e61333SAriff Abdullah if (flags & ~SND_CLONE_MASK) 920bba4862cSAriff Abdullah err = EINVAL; 921e4e61333SAriff Abdullah else 922bba4862cSAriff Abdullah (void)snd_clone_setflags(d->clones, flags); 923bba4862cSAriff Abdullah } 924e4e61333SAriff Abdullah 925e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 926bba4862cSAriff Abdullah 927bba4862cSAriff Abdullah return (err); 928bba4862cSAriff Abdullah } 929bba4862cSAriff Abdullah 930bba4862cSAriff Abdullah static int 931bba4862cSAriff Abdullah sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS) 932bba4862cSAriff Abdullah { 933bba4862cSAriff Abdullah struct snddev_info *d; 934bba4862cSAriff Abdullah int err, deadline; 935bba4862cSAriff Abdullah 936bba4862cSAriff Abdullah d = oidp->oid_arg1; 937e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || d->clones == NULL) 938bba4862cSAriff Abdullah return (ENODEV); 939bba4862cSAriff Abdullah 940e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 941e4e61333SAriff Abdullah 942bba4862cSAriff Abdullah deadline = snd_clone_getdeadline(d->clones); 943041b706bSDavid Malone err = sysctl_handle_int(oidp, &deadline, 0, req); 944bba4862cSAriff Abdullah 945bba4862cSAriff Abdullah if (err == 0 && req->newptr != NULL) { 946bba4862cSAriff Abdullah if (deadline < 0) 947bba4862cSAriff Abdullah err = EINVAL; 948e4e61333SAriff Abdullah else 949bba4862cSAriff Abdullah (void)snd_clone_setdeadline(d->clones, deadline); 950bba4862cSAriff Abdullah } 951e4e61333SAriff Abdullah 952e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 953bba4862cSAriff Abdullah 954bba4862cSAriff Abdullah return (err); 955bba4862cSAriff Abdullah } 956bba4862cSAriff Abdullah 957bba4862cSAriff Abdullah static int 958bba4862cSAriff Abdullah sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS) 959bba4862cSAriff Abdullah { 960bba4862cSAriff Abdullah struct snddev_info *d; 961bba4862cSAriff Abdullah int err, val; 962bba4862cSAriff Abdullah 963bba4862cSAriff Abdullah d = oidp->oid_arg1; 964e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || d->clones == NULL) 965bba4862cSAriff Abdullah return (ENODEV); 966bba4862cSAriff Abdullah 967bba4862cSAriff Abdullah val = 0; 968041b706bSDavid Malone err = sysctl_handle_int(oidp, &val, 0, req); 969bba4862cSAriff Abdullah 970bba4862cSAriff Abdullah if (err == 0 && req->newptr != NULL && val != 0) { 971e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 972e4e61333SAriff Abdullah val = snd_clone_gc(d->clones); 973e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 974e4e61333SAriff Abdullah if (bootverbose != 0 || snd_verbose > 3) 975e4e61333SAriff Abdullah device_printf(d->dev, "clone gc: pruned=%d\n", val); 976bba4862cSAriff Abdullah } 977bba4862cSAriff Abdullah 978bba4862cSAriff Abdullah return (err); 979bba4862cSAriff Abdullah } 980bba4862cSAriff Abdullah 981bba4862cSAriff Abdullah static int 982bba4862cSAriff Abdullah sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS) 983bba4862cSAriff Abdullah { 984bba4862cSAriff Abdullah struct snddev_info *d; 985bba4862cSAriff Abdullah int i, err, val; 986bba4862cSAriff Abdullah 987bba4862cSAriff Abdullah val = 0; 988041b706bSDavid Malone err = sysctl_handle_int(oidp, &val, 0, req); 989bba4862cSAriff Abdullah 990bba4862cSAriff Abdullah if (err == 0 && req->newptr != NULL && val != 0) { 9919c271f79SAriff Abdullah for (i = 0; pcm_devclass != NULL && 9929c271f79SAriff Abdullah i < devclass_get_maxunit(pcm_devclass); i++) { 993bba4862cSAriff Abdullah d = devclass_get_softc(pcm_devclass, i); 994e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || d->clones == NULL) 995bba4862cSAriff Abdullah continue; 996e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 997e4e61333SAriff Abdullah val = snd_clone_gc(d->clones); 998e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 999e4e61333SAriff Abdullah if (bootverbose != 0 || snd_verbose > 3) 1000e4e61333SAriff Abdullah device_printf(d->dev, "clone gc: pruned=%d\n", 1001e4e61333SAriff Abdullah val); 1002bba4862cSAriff Abdullah } 1003bba4862cSAriff Abdullah } 1004bba4862cSAriff Abdullah 1005bba4862cSAriff Abdullah return (err); 1006bba4862cSAriff Abdullah } 1007bba4862cSAriff Abdullah SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc, CTLTYPE_INT | CTLFLAG_RW, 1008bba4862cSAriff Abdullah 0, sizeof(int), sysctl_hw_snd_clone_gc, "I", 1009bba4862cSAriff Abdullah "global clone garbage collector"); 1010bba4862cSAriff Abdullah #endif 1011bba4862cSAriff Abdullah 1012987e5972SCameron Grant int 1013987e5972SCameron Grant pcm_register(device_t dev, void *devinfo, int numplay, int numrec) 1014987e5972SCameron Grant { 1015bba4862cSAriff Abdullah struct snddev_info *d; 101690da2b28SAriff Abdullah int i; 1017987e5972SCameron Grant 1018b8a36395SCameron Grant if (pcm_veto_load) { 1019b8a36395SCameron Grant device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load); 1020b8a36395SCameron Grant 1021b8a36395SCameron Grant return EINVAL; 1022b8a36395SCameron Grant } 1023b8a36395SCameron Grant 1024bba4862cSAriff Abdullah if (device_get_unit(dev) > PCMMAXUNIT) { 1025bba4862cSAriff Abdullah device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n", 1026bba4862cSAriff Abdullah device_get_unit(dev), PCMMAXUNIT); 1027bba4862cSAriff Abdullah device_printf(dev, 1028bba4862cSAriff Abdullah "Use 'hw.snd.maxunit' tunable to raise the limit.\n"); 1029bba4862cSAriff Abdullah return ENODEV; 1030bba4862cSAriff Abdullah } 1031bba4862cSAriff Abdullah 1032bba4862cSAriff Abdullah d = device_get_softc(dev); 1033e4e61333SAriff Abdullah d->dev = dev; 10342c69ba87SJohn Baldwin d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev"); 1035e4e61333SAriff Abdullah cv_init(&d->cv, device_get_nameunit(dev)); 1036e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 1037e4e61333SAriff Abdullah dsp_cdevinfo_init(d); 10387233ababSAlexander Leidinger #if 0 10397233ababSAlexander Leidinger /* 10407233ababSAlexander Leidinger * d->flags should be cleared by the allocator of the softc. 10417233ababSAlexander Leidinger * We cannot clear this field here because several devices set 10427233ababSAlexander Leidinger * this flag before calling pcm_register(). 10437233ababSAlexander Leidinger */ 1044cd9766c5SCameron Grant d->flags = 0; 10457233ababSAlexander Leidinger #endif 104690da2b28SAriff Abdullah i = 0; 104790da2b28SAriff Abdullah if (resource_int_value(device_get_name(dev), device_get_unit(dev), 104890da2b28SAriff Abdullah "vpc", &i) != 0 || i != 0) 104990da2b28SAriff Abdullah d->flags |= SD_F_VPC; 105090da2b28SAriff Abdullah 105190da2b28SAriff Abdullah if (resource_int_value(device_get_name(dev), device_get_unit(dev), 105290da2b28SAriff Abdullah "bitperfect", &i) == 0 && i != 0) 105390da2b28SAriff Abdullah d->flags |= SD_F_BITPERFECT; 105490da2b28SAriff Abdullah 1055987e5972SCameron Grant d->devinfo = devinfo; 1056f637a36cSCameron Grant d->devcount = 0; 1057506a5308SCameron Grant d->reccount = 0; 1058a67fe5c1SCameron Grant d->playcount = 0; 1059bba4862cSAriff Abdullah d->pvchancount = 0; 1060bba4862cSAriff Abdullah d->rvchancount = 0; 1061bba4862cSAriff Abdullah d->pvchanrate = 0; 1062bba4862cSAriff Abdullah d->pvchanformat = 0; 1063bba4862cSAriff Abdullah d->rvchanrate = 0; 1064bba4862cSAriff Abdullah d->rvchanformat = 0; 1065d95502a8SCameron Grant d->inprog = 0; 1066833f7023SCameron Grant 1067bba4862cSAriff Abdullah /* 1068bba4862cSAriff Abdullah * Create clone manager, disabled by default. Cloning will be 106990da2b28SAriff Abdullah * enabled during final stage of driver initialization through 1070bba4862cSAriff Abdullah * pcm_setstatus(). 1071bba4862cSAriff Abdullah */ 1072e4e61333SAriff Abdullah d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE, 1073e4e61333SAriff Abdullah SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK | 1074bba4862cSAriff Abdullah SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF | 1075bba4862cSAriff Abdullah SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED); 1076bba4862cSAriff Abdullah 1077bba4862cSAriff Abdullah if (bootverbose != 0 || snd_verbose > 3) { 1078bba4862cSAriff Abdullah device_printf(dev, 1079bba4862cSAriff Abdullah "clone manager: deadline=%dms flags=0x%08x\n", 1080bba4862cSAriff Abdullah snd_clone_getdeadline(d->clones), 1081bba4862cSAriff Abdullah snd_clone_getflags(d->clones)); 1082bba4862cSAriff Abdullah } 1083bba4862cSAriff Abdullah 1084bba4862cSAriff Abdullah CHN_INIT(d, channels.pcm); 1085bba4862cSAriff Abdullah CHN_INIT(d, channels.pcm.busy); 108690da2b28SAriff Abdullah CHN_INIT(d, channels.pcm.opened); 108745550658SPoul-Henning Kamp 1088e4e61333SAriff Abdullah /* XXX This is incorrect, but lets play along for now. */ 1089a1d444e1SAriff Abdullah if ((numplay == 0 || numrec == 0) && numplay != numrec) 1090285648f9SCameron Grant d->flags |= SD_F_SIMPLEX; 1091d95502a8SCameron Grant 1092bba4862cSAriff Abdullah sysctl_ctx_init(&d->play_sysctl_ctx); 1093bba4862cSAriff Abdullah d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx, 1094bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play", 1095bba4862cSAriff Abdullah CTLFLAG_RD, 0, "playback channels node"); 1096bba4862cSAriff Abdullah sysctl_ctx_init(&d->rec_sysctl_ctx); 1097bba4862cSAriff Abdullah d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx, 1098bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec", 1099bba4862cSAriff Abdullah CTLFLAG_RD, 0, "record channels node"); 1100851a904aSAlexander Leidinger /* XXX: an user should be able to set this with a control tool, the 1101851a904aSAlexander Leidinger sysadmin then needs min+max sysctls for this */ 11026dc7dc9aSMatthew D Fleming SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev), 1103a580b31aSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 1104a580b31aSAriff Abdullah OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size"); 110590da2b28SAriff Abdullah SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 110690da2b28SAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 110790da2b28SAriff Abdullah "bitperfect", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d), 110890da2b28SAriff Abdullah sysctl_dev_pcm_bitperfect, "I", 110990da2b28SAriff Abdullah "bit-perfect playback/recording (0=disable, 1=enable)"); 1110bba4862cSAriff Abdullah #ifdef SND_DEBUG 1111bba4862cSAriff Abdullah SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1112bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1113bba4862cSAriff Abdullah "clone_flags", CTLTYPE_UINT | CTLFLAG_RW, d, sizeof(d), 1114bba4862cSAriff Abdullah sysctl_dev_pcm_clone_flags, "IU", 1115bba4862cSAriff Abdullah "clone flags"); 1116bba4862cSAriff Abdullah SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1117bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1118bba4862cSAriff Abdullah "clone_deadline", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d), 1119bba4862cSAriff Abdullah sysctl_dev_pcm_clone_deadline, "I", 1120bba4862cSAriff Abdullah "clone expiration deadline (ms)"); 1121bba4862cSAriff Abdullah SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1122bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1123bba4862cSAriff Abdullah "clone_gc", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d), 1124bba4862cSAriff Abdullah sysctl_dev_pcm_clone_gc, "I", 1125bba4862cSAriff Abdullah "clone garbage collector"); 112682db23e2SCameron Grant #endif 1127e4e61333SAriff Abdullah 1128bba4862cSAriff Abdullah if (numplay > 0 || numrec > 0) { 1129cd9766c5SCameron Grant d->flags |= SD_F_AUTOVCHAN; 11303fdb3676SAriff Abdullah vchan_initsys(dev); 113197d69a96SAlexander Leidinger } 1132cd9766c5SCameron Grant 113390da2b28SAriff Abdullah if (d->flags & SD_F_EQ) 113490da2b28SAriff Abdullah feeder_eq_initsys(dev); 113590da2b28SAriff Abdullah 113667b1dce3SCameron Grant sndstat_register(dev, d->status, sndstat_prepare_pcm); 1137e4e61333SAriff Abdullah 1138987e5972SCameron Grant return 0; 1139987e5972SCameron Grant } 1140987e5972SCameron Grant 114133dbf14aSCameron Grant int 114233dbf14aSCameron Grant pcm_unregister(device_t dev) 11437c438dbeSCameron Grant { 1144e4e61333SAriff Abdullah struct snddev_info *d; 1145a67fe5c1SCameron Grant struct pcm_channel *ch; 1146bba4862cSAriff Abdullah struct thread *td; 11477c438dbeSCameron Grant 1148bba4862cSAriff Abdullah td = curthread; 1149e4e61333SAriff Abdullah d = device_get_softc(dev); 1150e4e61333SAriff Abdullah 1151e4e61333SAriff Abdullah if (!PCM_ALIVE(d)) { 1152e4e61333SAriff Abdullah device_printf(dev, "unregister: device not configured\n"); 1153e4e61333SAriff Abdullah return (0); 1154e4e61333SAriff Abdullah } 1155bba4862cSAriff Abdullah 1156bba4862cSAriff Abdullah if (sndstat_acquire(td) != 0) { 115728ef3fb0SAlexander Leidinger device_printf(dev, "unregister: sndstat busy\n"); 1158e4e61333SAriff Abdullah return (EBUSY); 115928ef3fb0SAlexander Leidinger } 116028ef3fb0SAlexander Leidinger 116190da2b28SAriff Abdullah PCM_LOCK(d); 1162e4e61333SAriff Abdullah PCM_WAIT(d); 1163e4e61333SAriff Abdullah 1164e4e61333SAriff Abdullah if (d->inprog != 0) { 11655c25132aSGeorge C A Reid device_printf(dev, "unregister: operation in progress\n"); 116690da2b28SAriff Abdullah PCM_UNLOCK(d); 1167bba4862cSAriff Abdullah sndstat_release(td); 1168e4e61333SAriff Abdullah return (EBUSY); 11695c25132aSGeorge C A Reid } 11705ee30e27SMathew Kanner 1171e4e61333SAriff Abdullah PCM_ACQUIRE(d); 117290da2b28SAriff Abdullah PCM_UNLOCK(d); 1173e4e61333SAriff Abdullah 1174e4e61333SAriff Abdullah CHN_FOREACH(ch, d, channels.pcm) { 1175e4e61333SAriff Abdullah CHN_LOCK(ch); 1176e4e61333SAriff Abdullah if (ch->refcount > 0) { 1177e4e61333SAriff Abdullah device_printf(dev, 1178e4e61333SAriff Abdullah "unregister: channel %s busy (pid %d)\n", 1179e4e61333SAriff Abdullah ch->name, ch->pid); 1180e4e61333SAriff Abdullah CHN_UNLOCK(ch); 1181e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 1182bba4862cSAriff Abdullah sndstat_release(td); 1183e4e61333SAriff Abdullah return (EBUSY); 1184285648f9SCameron Grant } 1185e4e61333SAriff Abdullah CHN_UNLOCK(ch); 1186c9b53085SCameron Grant } 11875ee30e27SMathew Kanner 1188bba4862cSAriff Abdullah if (d->clones != NULL) { 1189bba4862cSAriff Abdullah if (snd_clone_busy(d->clones) != 0) { 1190bba4862cSAriff Abdullah device_printf(dev, "unregister: clone busy\n"); 1191e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 1192bba4862cSAriff Abdullah sndstat_release(td); 1193e4e61333SAriff Abdullah return (EBUSY); 1194e4e61333SAriff Abdullah } else { 119590da2b28SAriff Abdullah PCM_LOCK(d); 1196bba4862cSAriff Abdullah (void)snd_clone_disable(d->clones); 119790da2b28SAriff Abdullah PCM_UNLOCK(d); 1198e4e61333SAriff Abdullah } 1199bba4862cSAriff Abdullah } 1200bba4862cSAriff Abdullah 1201b4221868SAriff Abdullah if (mixer_uninit(dev) == EBUSY) { 12027233ababSAlexander Leidinger device_printf(dev, "unregister: mixer busy\n"); 120390da2b28SAriff Abdullah PCM_LOCK(d); 1204bba4862cSAriff Abdullah if (d->clones != NULL) 1205bba4862cSAriff Abdullah (void)snd_clone_enable(d->clones); 1206e4e61333SAriff Abdullah PCM_RELEASE(d); 120790da2b28SAriff Abdullah PCM_UNLOCK(d); 1208bba4862cSAriff Abdullah sndstat_release(td); 1209e4e61333SAriff Abdullah return (EBUSY); 12107233ababSAlexander Leidinger } 12117233ababSAlexander Leidinger 121290da2b28SAriff Abdullah PCM_LOCK(d); 1213e4e61333SAriff Abdullah d->flags |= SD_F_DYING; 1214e4e61333SAriff Abdullah d->flags &= ~SD_F_REGISTERED; 121590da2b28SAriff Abdullah PCM_UNLOCK(d); 1216e4e61333SAriff Abdullah 1217e4e61333SAriff Abdullah /* 1218e4e61333SAriff Abdullah * No lock being held, so this thing can be flushed without 1219e4e61333SAriff Abdullah * stucking into devdrn oblivion. 1220e4e61333SAriff Abdullah */ 1221bba4862cSAriff Abdullah if (d->clones != NULL) { 1222bba4862cSAriff Abdullah snd_clone_destroy(d->clones); 1223bba4862cSAriff Abdullah d->clones = NULL; 12247982e7a4SAriff Abdullah } 1225bba4862cSAriff Abdullah 1226bba4862cSAriff Abdullah if (d->play_sysctl_tree != NULL) { 1227bba4862cSAriff Abdullah sysctl_ctx_free(&d->play_sysctl_ctx); 1228bba4862cSAriff Abdullah d->play_sysctl_tree = NULL; 1229bba4862cSAriff Abdullah } 1230bba4862cSAriff Abdullah if (d->rec_sysctl_tree != NULL) { 1231bba4862cSAriff Abdullah sysctl_ctx_free(&d->rec_sysctl_ctx); 1232bba4862cSAriff Abdullah d->rec_sysctl_tree = NULL; 1233a1d444e1SAriff Abdullah } 1234bba4862cSAriff Abdullah 1235bba4862cSAriff Abdullah while (!CHN_EMPTY(d, channels.pcm)) 1236285648f9SCameron Grant pcm_killchan(dev); 12377c438dbeSCameron Grant 1238e4e61333SAriff Abdullah dsp_cdevinfo_flush(d); 1239e4e61333SAriff Abdullah 124090da2b28SAriff Abdullah PCM_LOCK(d); 1241e4e61333SAriff Abdullah PCM_RELEASE(d); 1242e4e61333SAriff Abdullah cv_destroy(&d->cv); 124390da2b28SAriff Abdullah PCM_UNLOCK(d); 124449c5e6e2SCameron Grant snd_mtxfree(d->lock); 124528ef3fb0SAlexander Leidinger sndstat_unregister(dev); 1246bba4862cSAriff Abdullah sndstat_release(td); 1247bba4862cSAriff Abdullah 1248bba4862cSAriff Abdullah if (snd_unit == device_get_unit(dev)) { 1249*cbebc90dSAlexander Motin snd_unit = pcm_best_unit(-1); 1250*cbebc90dSAlexander Motin if (snd_unit_auto == 0) 1251*cbebc90dSAlexander Motin snd_unit_auto = 1; 1252e4e61333SAriff Abdullah } 1253bba4862cSAriff Abdullah 1254e4e61333SAriff Abdullah return (0); 125533dbf14aSCameron Grant } 12567c438dbeSCameron Grant 125767b1dce3SCameron Grant /************************************************************************/ 125867b1dce3SCameron Grant 1259b611c801SAlexander Leidinger /** 1260b611c801SAlexander Leidinger * @brief Handle OSSv4 SNDCTL_SYSINFO ioctl. 1261b611c801SAlexander Leidinger * 1262b611c801SAlexander Leidinger * @param si Pointer to oss_sysinfo struct where information about the 1263b611c801SAlexander Leidinger * sound subsystem will be written/copied. 1264b611c801SAlexander Leidinger * 1265b611c801SAlexander Leidinger * This routine returns information about the sound system, such as the 1266b611c801SAlexander Leidinger * current OSS version, number of audio, MIDI, and mixer drivers, etc. 1267b611c801SAlexander Leidinger * Also includes a bitmask showing which of the above types of devices 1268b611c801SAlexander Leidinger * are open (busy). 1269b611c801SAlexander Leidinger * 1270b611c801SAlexander Leidinger * @note 1271b611c801SAlexander Leidinger * Calling threads must not hold any snddev_info or pcm_channel locks. 1272b611c801SAlexander Leidinger * 1273b611c801SAlexander Leidinger * @author Ryan Beasley <ryanb@FreeBSD.org> 1274b611c801SAlexander Leidinger */ 1275b611c801SAlexander Leidinger void 1276b611c801SAlexander Leidinger sound_oss_sysinfo(oss_sysinfo *si) 1277b611c801SAlexander Leidinger { 1278b611c801SAlexander Leidinger static char si_product[] = "FreeBSD native OSS ABI"; 1279b611c801SAlexander Leidinger static char si_version[] = __XSTRING(__FreeBSD_version); 1280c412d503SAlexander Motin static char si_license[] = "BSD"; 1281b611c801SAlexander Leidinger static int intnbits = sizeof(int) * 8; /* Better suited as macro? 1282b611c801SAlexander Leidinger Must pester a C guru. */ 1283b611c801SAlexander Leidinger 1284b611c801SAlexander Leidinger struct snddev_info *d; 1285b611c801SAlexander Leidinger struct pcm_channel *c; 1286b611c801SAlexander Leidinger int i, j, ncards; 1287b611c801SAlexander Leidinger 1288b611c801SAlexander Leidinger ncards = 0; 1289b611c801SAlexander Leidinger 1290b611c801SAlexander Leidinger strlcpy(si->product, si_product, sizeof(si->product)); 1291b611c801SAlexander Leidinger strlcpy(si->version, si_version, sizeof(si->version)); 1292b611c801SAlexander Leidinger si->versionnum = SOUND_VERSION; 1293c412d503SAlexander Motin strlcpy(si->license, si_license, sizeof(si->license)); 1294b611c801SAlexander Leidinger 1295b611c801SAlexander Leidinger /* 1296b611c801SAlexander Leidinger * Iterate over PCM devices and their channels, gathering up data 1297b611c801SAlexander Leidinger * for the numaudios, ncards, and openedaudio fields. 1298b611c801SAlexander Leidinger */ 1299b611c801SAlexander Leidinger si->numaudios = 0; 1300b611c801SAlexander Leidinger bzero((void *)&si->openedaudio, sizeof(si->openedaudio)); 1301b611c801SAlexander Leidinger 1302b611c801SAlexander Leidinger j = 0; 1303b611c801SAlexander Leidinger 13049c271f79SAriff Abdullah for (i = 0; pcm_devclass != NULL && 13059c271f79SAriff Abdullah i < devclass_get_maxunit(pcm_devclass); i++) { 1306b611c801SAlexander Leidinger d = devclass_get_softc(pcm_devclass, i); 1307e4e61333SAriff Abdullah if (!PCM_REGISTERED(d)) 1308b611c801SAlexander Leidinger continue; 1309b611c801SAlexander Leidinger 1310e4e61333SAriff Abdullah /* XXX Need Giant magic entry ??? */ 1311e4e61333SAriff Abdullah 1312b611c801SAlexander Leidinger /* See note in function's docblock */ 131390da2b28SAriff Abdullah PCM_UNLOCKASSERT(d); 131490da2b28SAriff Abdullah PCM_LOCK(d); 1315b611c801SAlexander Leidinger 1316b611c801SAlexander Leidinger si->numaudios += d->devcount; 1317b611c801SAlexander Leidinger ++ncards; 1318b611c801SAlexander Leidinger 1319bba4862cSAriff Abdullah CHN_FOREACH(c, d, channels.pcm) { 132090da2b28SAriff Abdullah CHN_UNLOCKASSERT(c); 1321b611c801SAlexander Leidinger CHN_LOCK(c); 1322b611c801SAlexander Leidinger if (c->flags & CHN_F_BUSY) 1323b611c801SAlexander Leidinger si->openedaudio[j / intnbits] |= 1324b611c801SAlexander Leidinger (1 << (j % intnbits)); 1325b611c801SAlexander Leidinger CHN_UNLOCK(c); 1326b611c801SAlexander Leidinger j++; 1327b611c801SAlexander Leidinger } 1328b611c801SAlexander Leidinger 132990da2b28SAriff Abdullah PCM_UNLOCK(d); 1330b611c801SAlexander Leidinger } 1331c412d503SAlexander Motin si->numaudioengines = si->numaudios; 1332b611c801SAlexander Leidinger 1333b611c801SAlexander Leidinger si->numsynths = 0; /* OSSv4 docs: this field is obsolete */ 1334b611c801SAlexander Leidinger /** 1335b611c801SAlexander Leidinger * @todo Collect num{midis,timers}. 1336b611c801SAlexander Leidinger * 1337b611c801SAlexander Leidinger * Need access to sound/midi/midi.c::midistat_lock in order 1338b611c801SAlexander Leidinger * to safely touch midi_devices and get a head count of, well, 1339b611c801SAlexander Leidinger * MIDI devices. midistat_lock is a global static (i.e., local to 1340b611c801SAlexander Leidinger * midi.c), but midi_devices is a regular global; should the mutex 1341b611c801SAlexander Leidinger * be publicized, or is there another way to get this information? 1342b611c801SAlexander Leidinger * 1343b611c801SAlexander Leidinger * NB: MIDI/sequencer stuff is currently on hold. 1344b611c801SAlexander Leidinger */ 1345b611c801SAlexander Leidinger si->nummidis = 0; 1346b611c801SAlexander Leidinger si->numtimers = 0; 1347b611c801SAlexander Leidinger si->nummixers = mixer_count; 1348b611c801SAlexander Leidinger si->numcards = ncards; 1349b611c801SAlexander Leidinger /* OSSv4 docs: Intended only for test apps; API doesn't 1350b611c801SAlexander Leidinger really have much of a concept of cards. Shouldn't be 1351b611c801SAlexander Leidinger used by applications. */ 1352b611c801SAlexander Leidinger 1353b611c801SAlexander Leidinger /** 1354b611c801SAlexander Leidinger * @todo Fill in "busy devices" fields. 1355b611c801SAlexander Leidinger * 1356b611c801SAlexander Leidinger * si->openedmidi = " MIDI devices 1357b611c801SAlexander Leidinger */ 1358b611c801SAlexander Leidinger bzero((void *)&si->openedmidi, sizeof(si->openedmidi)); 1359b611c801SAlexander Leidinger 1360b611c801SAlexander Leidinger /* 1361b611c801SAlexander Leidinger * Si->filler is a reserved array, but according to docs each 1362b611c801SAlexander Leidinger * element should be set to -1. 1363b611c801SAlexander Leidinger */ 1364b611c801SAlexander Leidinger for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++) 1365b611c801SAlexander Leidinger si->filler[i] = -1; 1366b611c801SAlexander Leidinger } 1367b611c801SAlexander Leidinger 136852f6e09eSAlexander Motin int 136952f6e09eSAlexander Motin sound_oss_card_info(oss_card_info *si) 137052f6e09eSAlexander Motin { 137152f6e09eSAlexander Motin struct snddev_info *d; 137252f6e09eSAlexander Motin int i, ncards; 137352f6e09eSAlexander Motin 137452f6e09eSAlexander Motin ncards = 0; 137552f6e09eSAlexander Motin 137652f6e09eSAlexander Motin for (i = 0; pcm_devclass != NULL && 137752f6e09eSAlexander Motin i < devclass_get_maxunit(pcm_devclass); i++) { 137852f6e09eSAlexander Motin d = devclass_get_softc(pcm_devclass, i); 137952f6e09eSAlexander Motin if (!PCM_REGISTERED(d)) 138052f6e09eSAlexander Motin continue; 138152f6e09eSAlexander Motin 138252f6e09eSAlexander Motin if (ncards++ != si->card) 138352f6e09eSAlexander Motin continue; 138452f6e09eSAlexander Motin 138590da2b28SAriff Abdullah PCM_UNLOCKASSERT(d); 138690da2b28SAriff Abdullah PCM_LOCK(d); 138752f6e09eSAlexander Motin 138852f6e09eSAlexander Motin strlcpy(si->shortname, device_get_nameunit(d->dev), 138952f6e09eSAlexander Motin sizeof(si->shortname)); 139052f6e09eSAlexander Motin strlcpy(si->longname, device_get_desc(d->dev), 139152f6e09eSAlexander Motin sizeof(si->longname)); 139252f6e09eSAlexander Motin strlcpy(si->hw_info, d->status, sizeof(si->hw_info)); 139352f6e09eSAlexander Motin si->intr_count = si->ack_count = 0; 139490da2b28SAriff Abdullah 139590da2b28SAriff Abdullah PCM_UNLOCK(d); 139690da2b28SAriff Abdullah 139752f6e09eSAlexander Motin return (0); 139852f6e09eSAlexander Motin } 139952f6e09eSAlexander Motin return (ENXIO); 140052f6e09eSAlexander Motin } 140152f6e09eSAlexander Motin 1402b611c801SAlexander Leidinger /************************************************************************/ 1403b611c801SAlexander Leidinger 14040739ea1dSSeigo Tanimura static int 14050739ea1dSSeigo Tanimura sound_modevent(module_t mod, int type, void *data) 14060739ea1dSSeigo Tanimura { 1407b611c801SAlexander Leidinger int ret; 14087233ababSAlexander Leidinger #if 0 14090739ea1dSSeigo Tanimura return (midi_modevent(mod, type, data)); 14107233ababSAlexander Leidinger #else 1411b611c801SAlexander Leidinger ret = 0; 1412b611c801SAlexander Leidinger 1413b611c801SAlexander Leidinger switch(type) { 1414b611c801SAlexander Leidinger case MOD_LOAD: 1415b611c801SAlexander Leidinger pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL); 1416b611c801SAlexander Leidinger break; 1417b611c801SAlexander Leidinger case MOD_UNLOAD: 1418b611c801SAlexander Leidinger case MOD_SHUTDOWN: 1419bba4862cSAriff Abdullah ret = sndstat_acquire(curthread); 1420bba4862cSAriff Abdullah if (ret != 0) 1421bba4862cSAriff Abdullah break; 1422b611c801SAlexander Leidinger if (pcmsg_unrhdr != NULL) { 1423b611c801SAlexander Leidinger delete_unrhdr(pcmsg_unrhdr); 1424b611c801SAlexander Leidinger pcmsg_unrhdr = NULL; 1425b611c801SAlexander Leidinger } 1426b611c801SAlexander Leidinger break; 1427b611c801SAlexander Leidinger default: 142890da2b28SAriff Abdullah ret = ENOTSUP; 1429b611c801SAlexander Leidinger } 1430b611c801SAlexander Leidinger 1431b611c801SAlexander Leidinger return ret; 14327233ababSAlexander Leidinger #endif 14330739ea1dSSeigo Tanimura } 14340739ea1dSSeigo Tanimura 14350739ea1dSSeigo Tanimura DEV_MODULE(sound, sound_modevent, NULL); 14360739ea1dSSeigo Tanimura MODULE_VERSION(sound, SOUND_MODVER); 1437