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 54cbebc90dSAlexander Motin static int snd_unit_auto = -1; 55*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RWTUN, 56838d3589SAriff Abdullah &snd_unit_auto, 0, "assign default unit to a newly attached device"); 57ad8612b9SAriff Abdullah 58bba4862cSAriff Abdullah int snd_maxautovchans = 16; 59851a904aSAlexander Leidinger /* XXX: a tunable implies that we may need more than one sound channel before 60851a904aSAlexander Leidinger the system can change a sysctl (/etc/sysctl.conf), do we really need 61851a904aSAlexander Leidinger this? */ 6267b1dce3SCameron Grant TUNABLE_INT("hw.snd.maxautovchans", &snd_maxautovchans); 63987e5972SCameron Grant 6482db23e2SCameron Grant SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD, 0, "Sound driver"); 6582db23e2SCameron Grant 66bba4862cSAriff Abdullah /* 67bba4862cSAriff Abdullah * XXX I've had enough with people not telling proper version/arch 68bba4862cSAriff Abdullah * while reporting problems, not after 387397913213th questions/requests. 69bba4862cSAriff Abdullah */ 70a9bdb5d3SAndriy Gapon static char snd_driver_version[] = 71bba4862cSAriff Abdullah __XSTRING(SND_DRV_VERSION)"/"MACHINE_ARCH; 72bba4862cSAriff Abdullah SYSCTL_STRING(_hw_snd, OID_AUTO, version, CTLFLAG_RD, &snd_driver_version, 7390da2b28SAriff Abdullah 0, "driver version/arch"); 74bba4862cSAriff Abdullah 75b611c801SAlexander Leidinger /** 76b611c801SAlexander Leidinger * @brief Unit number allocator for syncgroup IDs 77b611c801SAlexander Leidinger */ 78b611c801SAlexander Leidinger struct unrhdr *pcmsg_unrhdr = NULL; 79b611c801SAlexander Leidinger 8090da2b28SAriff Abdullah static int 8190da2b28SAriff Abdullah sndstat_prepare_pcm(SNDSTAT_PREPARE_PCM_ARGS) 8290da2b28SAriff Abdullah { 8390da2b28SAriff Abdullah SNDSTAT_PREPARE_PCM_BEGIN(); 8490da2b28SAriff Abdullah SNDSTAT_PREPARE_PCM_END(); 8590da2b28SAriff Abdullah } 8667b1dce3SCameron Grant 8737209180SCameron Grant void * 882c69ba87SJohn Baldwin snd_mtxcreate(const char *desc, const char *type) 8937209180SCameron Grant { 9037209180SCameron Grant struct mtx *m; 9137209180SCameron Grant 92a163d034SWarner Losh m = malloc(sizeof(*m), M_DEVBUF, M_WAITOK | M_ZERO); 9312e524a2SDon Lewis mtx_init(m, desc, type, MTX_DEF); 9412e524a2SDon Lewis return m; 9512e524a2SDon Lewis } 9612e524a2SDon Lewis 9737209180SCameron Grant void 9837209180SCameron Grant snd_mtxfree(void *m) 9937209180SCameron Grant { 10037209180SCameron Grant struct mtx *mtx = m; 10137209180SCameron Grant 10237209180SCameron Grant mtx_destroy(mtx); 10337209180SCameron Grant free(mtx, M_DEVBUF); 10437209180SCameron Grant } 10537209180SCameron Grant 10637209180SCameron Grant void 10737209180SCameron Grant snd_mtxassert(void *m) 10837209180SCameron Grant { 109f00f162aSCameron Grant #ifdef INVARIANTS 11037209180SCameron Grant struct mtx *mtx = m; 11137209180SCameron Grant 11237209180SCameron Grant mtx_assert(mtx, MA_OWNED); 11337209180SCameron Grant #endif 11437209180SCameron Grant } 11537209180SCameron Grant 11637209180SCameron Grant int 11737209180SCameron Grant snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep) 11837209180SCameron Grant { 119e4e61333SAriff Abdullah struct snddev_info *d; 12090da2b28SAriff Abdullah 12137209180SCameron Grant flags &= INTR_MPSAFE; 12246700f12SPeter Wemm flags |= INTR_TYPE_AV; 123e4e61333SAriff Abdullah d = device_get_softc(dev); 124e4e61333SAriff Abdullah if (d != NULL && (flags & INTR_MPSAFE)) 125e4e61333SAriff Abdullah d->flags |= SD_F_MPSAFE; 126e4e61333SAriff Abdullah 1272cc08b74SAriff Abdullah return bus_setup_intr(dev, res, flags, 1282cc08b74SAriff Abdullah #if __FreeBSD_version >= 700031 1292cc08b74SAriff Abdullah NULL, 1302cc08b74SAriff Abdullah #endif 1312cc08b74SAriff Abdullah hand, param, cookiep); 13237209180SCameron Grant } 13337209180SCameron Grant 134bba4862cSAriff Abdullah static void 135bba4862cSAriff Abdullah pcm_clonereset(struct snddev_info *d) 136a1d444e1SAriff Abdullah { 137bba4862cSAriff Abdullah int cmax; 138bba4862cSAriff Abdullah 139e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 140bba4862cSAriff Abdullah 141bba4862cSAriff Abdullah cmax = d->playcount + d->reccount - 1; 142bba4862cSAriff Abdullah if (d->pvchancount > 0) 14390da2b28SAriff Abdullah cmax += max(d->pvchancount, snd_maxautovchans) - 1; 144bba4862cSAriff Abdullah if (d->rvchancount > 0) 14590da2b28SAriff Abdullah cmax += max(d->rvchancount, snd_maxautovchans) - 1; 146bba4862cSAriff Abdullah if (cmax > PCMMAXCLONE) 147bba4862cSAriff Abdullah cmax = PCMMAXCLONE; 148bba4862cSAriff Abdullah (void)snd_clone_gc(d->clones); 149bba4862cSAriff Abdullah (void)snd_clone_setmaxunit(d->clones, cmax); 150bba4862cSAriff Abdullah } 151bba4862cSAriff Abdullah 15290da2b28SAriff Abdullah int 153bba4862cSAriff Abdullah pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num) 154bba4862cSAriff Abdullah { 155bba4862cSAriff Abdullah struct pcm_channel *c, *ch, *nch; 15690da2b28SAriff Abdullah struct pcmchan_caps *caps; 15790da2b28SAriff Abdullah int i, err, vcnt; 158bba4862cSAriff Abdullah 159e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 160a1d444e1SAriff Abdullah 161bba4862cSAriff Abdullah if ((direction == PCMDIR_PLAY && d->playcount < 1) || 162e4e61333SAriff Abdullah (direction == PCMDIR_REC && d->reccount < 1)) 163e4e61333SAriff Abdullah return (ENODEV); 164a580b31aSAriff Abdullah 165e4e61333SAriff Abdullah if (!(d->flags & SD_F_AUTOVCHAN)) 166e4e61333SAriff Abdullah return (EINVAL); 167a1d444e1SAriff Abdullah 168e4e61333SAriff Abdullah if (newcnt < 0 || newcnt > SND_MAXVCHANS) 169e4e61333SAriff Abdullah return (E2BIG); 170a1d444e1SAriff Abdullah 171bba4862cSAriff Abdullah if (direction == PCMDIR_PLAY) 172bba4862cSAriff Abdullah vcnt = d->pvchancount; 173bba4862cSAriff Abdullah else if (direction == PCMDIR_REC) 174bba4862cSAriff Abdullah vcnt = d->rvchancount; 175e4e61333SAriff Abdullah else 176e4e61333SAriff Abdullah return (EINVAL); 177a1d444e1SAriff Abdullah 178a1d444e1SAriff Abdullah if (newcnt > vcnt) { 179bba4862cSAriff Abdullah KASSERT(num == -1 || 180bba4862cSAriff Abdullah (num >= 0 && num < SND_MAXVCHANS && (newcnt - 1) == vcnt), 181bba4862cSAriff Abdullah ("bogus vchan_create() request num=%d newcnt=%d vcnt=%d", 182bba4862cSAriff Abdullah num, newcnt, vcnt)); 183a1d444e1SAriff Abdullah /* add new vchans - find a parent channel first */ 184e4e61333SAriff Abdullah ch = NULL; 185bba4862cSAriff Abdullah CHN_FOREACH(c, d, channels.pcm) { 186a1d444e1SAriff Abdullah CHN_LOCK(c); 187bba4862cSAriff Abdullah if (c->direction == direction && 188bba4862cSAriff Abdullah ((c->flags & CHN_F_HAS_VCHAN) || (vcnt == 0 && 18990da2b28SAriff Abdullah c->refcount < 1 && 190e4e61333SAriff Abdullah !(c->flags & (CHN_F_BUSY | CHN_F_VIRTUAL))))) { 19190da2b28SAriff Abdullah /* 19290da2b28SAriff Abdullah * Reuse hw channel with vchans already 19390da2b28SAriff Abdullah * created. 19490da2b28SAriff Abdullah */ 19590da2b28SAriff Abdullah if (c->flags & CHN_F_HAS_VCHAN) { 196e4e61333SAriff Abdullah ch = c; 197e4e61333SAriff Abdullah break; 198e4e61333SAriff Abdullah } 19990da2b28SAriff Abdullah /* 20090da2b28SAriff Abdullah * No vchans ever created, look for 20190da2b28SAriff Abdullah * channels with supported formats. 20290da2b28SAriff Abdullah */ 20390da2b28SAriff Abdullah caps = chn_getcaps(c); 20490da2b28SAriff Abdullah if (caps == NULL) { 20590da2b28SAriff Abdullah CHN_UNLOCK(c); 20690da2b28SAriff Abdullah continue; 20790da2b28SAriff Abdullah } 20890da2b28SAriff Abdullah for (i = 0; caps->fmtlist[i] != 0; i++) { 20990da2b28SAriff Abdullah if (caps->fmtlist[i] & AFMT_CONVERTIBLE) 21090da2b28SAriff Abdullah break; 21190da2b28SAriff Abdullah } 21290da2b28SAriff Abdullah if (caps->fmtlist[i] != 0) { 21390da2b28SAriff Abdullah ch = c; 21490da2b28SAriff Abdullah break; 21590da2b28SAriff Abdullah } 21690da2b28SAriff Abdullah } 217a1d444e1SAriff Abdullah CHN_UNLOCK(c); 218a1d444e1SAriff Abdullah } 219e4e61333SAriff Abdullah if (ch == NULL) 220e4e61333SAriff Abdullah return (EBUSY); 221e4e61333SAriff Abdullah ch->flags |= CHN_F_BUSY; 222e4e61333SAriff Abdullah err = 0; 223a1d444e1SAriff Abdullah while (err == 0 && newcnt > vcnt) { 224e4e61333SAriff Abdullah err = vchan_create(ch, num); 225bba4862cSAriff Abdullah if (err == 0) 226a1d444e1SAriff Abdullah vcnt++; 227bba4862cSAriff Abdullah else if (err == E2BIG && newcnt > vcnt) 228bba4862cSAriff Abdullah device_printf(d->dev, 229bba4862cSAriff Abdullah "%s: err=%d Maximum channel reached.\n", 230bba4862cSAriff Abdullah __func__, err); 231a1d444e1SAriff Abdullah } 232a1d444e1SAriff Abdullah if (vcnt == 0) 233e4e61333SAriff Abdullah ch->flags &= ~CHN_F_BUSY; 234e4e61333SAriff Abdullah CHN_UNLOCK(ch); 235e4e61333SAriff Abdullah if (err != 0) 236e4e61333SAriff Abdullah return (err); 237e4e61333SAriff Abdullah else 238bba4862cSAriff Abdullah pcm_clonereset(d); 239a1d444e1SAriff Abdullah } else if (newcnt < vcnt) { 240bba4862cSAriff Abdullah KASSERT(num == -1, 241bba4862cSAriff Abdullah ("bogus vchan_destroy() request num=%d", num)); 242bba4862cSAriff Abdullah CHN_FOREACH(c, d, channels.pcm) { 243a1d444e1SAriff Abdullah CHN_LOCK(c); 244bba4862cSAriff Abdullah if (c->direction != direction || 245bba4862cSAriff Abdullah CHN_EMPTY(c, children) || 246bba4862cSAriff Abdullah !(c->flags & CHN_F_HAS_VCHAN)) { 247a1d444e1SAriff Abdullah CHN_UNLOCK(c); 248bba4862cSAriff Abdullah continue; 249a1d444e1SAriff Abdullah } 250bba4862cSAriff Abdullah CHN_FOREACH_SAFE(ch, c, nch, children) { 251bba4862cSAriff Abdullah CHN_LOCK(ch); 25290da2b28SAriff Abdullah if (vcnt == 1 && c->refcount > 0) { 253bba4862cSAriff Abdullah CHN_UNLOCK(ch); 25490da2b28SAriff Abdullah break; 25590da2b28SAriff Abdullah } 25690da2b28SAriff Abdullah if (!(ch->flags & CHN_F_BUSY) && 25790da2b28SAriff Abdullah ch->refcount < 1) { 258bba4862cSAriff Abdullah err = vchan_destroy(ch); 259a1d444e1SAriff Abdullah if (err == 0) 260a1d444e1SAriff Abdullah vcnt--; 261bba4862cSAriff Abdullah } else 262bba4862cSAriff Abdullah CHN_UNLOCK(ch); 263e4e61333SAriff Abdullah if (vcnt == newcnt) 264bba4862cSAriff Abdullah break; 265a1d444e1SAriff Abdullah } 266bba4862cSAriff Abdullah CHN_UNLOCK(c); 267bba4862cSAriff Abdullah break; 268bba4862cSAriff Abdullah } 269bba4862cSAriff Abdullah pcm_clonereset(d); 270bba4862cSAriff Abdullah } 271a1d444e1SAriff Abdullah 272e4e61333SAriff Abdullah return (0); 273a1d444e1SAriff Abdullah } 274a1d444e1SAriff Abdullah 2753fdb3676SAriff Abdullah /* return error status and a locked channel */ 2763fdb3676SAriff Abdullah int 2773fdb3676SAriff Abdullah pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction, 27890da2b28SAriff Abdullah pid_t pid, char *comm, int devunit) 279285648f9SCameron Grant { 280285648f9SCameron Grant struct pcm_channel *c; 28190da2b28SAriff Abdullah int err, vchancount, vchan_num; 282bba4862cSAriff Abdullah 283bba4862cSAriff Abdullah KASSERT(d != NULL && ch != NULL && (devunit == -1 || 284bba4862cSAriff Abdullah !(devunit & ~(SND_U_MASK | SND_D_MASK | SND_C_MASK))) && 285bba4862cSAriff Abdullah (direction == PCMDIR_PLAY || direction == PCMDIR_REC), 286e4e61333SAriff Abdullah ("%s(): invalid d=%p ch=%p direction=%d pid=%d devunit=%d", 287bba4862cSAriff Abdullah __func__, d, ch, direction, pid, devunit)); 288e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 289bba4862cSAriff Abdullah 290bba4862cSAriff Abdullah /* Double check again. */ 291bba4862cSAriff Abdullah if (devunit != -1) { 292bba4862cSAriff Abdullah switch (snd_unit2d(devunit)) { 293bba4862cSAriff Abdullah case SND_DEV_DSPHW_PLAY: 294bba4862cSAriff Abdullah case SND_DEV_DSPHW_VPLAY: 295bba4862cSAriff Abdullah if (direction != PCMDIR_PLAY) 29690da2b28SAriff Abdullah return (ENOTSUP); 297bba4862cSAriff Abdullah break; 298bba4862cSAriff Abdullah case SND_DEV_DSPHW_REC: 299bba4862cSAriff Abdullah case SND_DEV_DSPHW_VREC: 300bba4862cSAriff Abdullah if (direction != PCMDIR_REC) 30190da2b28SAriff Abdullah return (ENOTSUP); 302bba4862cSAriff Abdullah break; 303bba4862cSAriff Abdullah default: 304bba4862cSAriff Abdullah if (!(direction == PCMDIR_PLAY || 305bba4862cSAriff Abdullah direction == PCMDIR_REC)) 30690da2b28SAriff Abdullah return (ENOTSUP); 307bba4862cSAriff Abdullah break; 308bba4862cSAriff Abdullah } 309bba4862cSAriff Abdullah } 310285648f9SCameron Grant 31190da2b28SAriff Abdullah *ch = NULL; 31290da2b28SAriff Abdullah vchan_num = 0; 31390da2b28SAriff Abdullah vchancount = (direction == PCMDIR_PLAY) ? d->pvchancount : 31490da2b28SAriff Abdullah d->rvchancount; 31590da2b28SAriff Abdullah 3163fdb3676SAriff Abdullah retry_chnalloc: 31790da2b28SAriff Abdullah err = ENOTSUP; 318f637a36cSCameron Grant /* scan for a free channel */ 319bba4862cSAriff Abdullah CHN_FOREACH(c, d, channels.pcm) { 32049c5e6e2SCameron Grant CHN_LOCK(c); 32190da2b28SAriff Abdullah if (devunit == -1 && c->direction == direction && 32290da2b28SAriff Abdullah (c->flags & CHN_F_VIRTUAL)) { 32390da2b28SAriff Abdullah if (vchancount < snd_maxautovchans && 32490da2b28SAriff Abdullah vchan_num < CHN_CHAN(c)) { 32590da2b28SAriff Abdullah CHN_UNLOCK(c); 32690da2b28SAriff Abdullah goto vchan_alloc; 32790da2b28SAriff Abdullah } 32890da2b28SAriff Abdullah vchan_num++; 32990da2b28SAriff Abdullah } 330bba4862cSAriff Abdullah if (c->direction == direction && !(c->flags & CHN_F_BUSY) && 331bba4862cSAriff Abdullah (devunit == -1 || devunit == -2 || c->unit == devunit)) { 332285648f9SCameron Grant c->flags |= CHN_F_BUSY; 333b8f0d9e0SCameron Grant c->pid = pid; 33490da2b28SAriff Abdullah strlcpy(c->comm, (comm != NULL) ? comm : 33590da2b28SAriff Abdullah CHN_COMM_UNKNOWN, sizeof(c->comm)); 3363fdb3676SAriff Abdullah *ch = c; 337bba4862cSAriff Abdullah return (0); 338bba4862cSAriff Abdullah } else if (c->unit == devunit) { 3393fdb3676SAriff Abdullah if (c->direction != direction) 34090da2b28SAriff Abdullah err = ENOTSUP; 3413fdb3676SAriff Abdullah else if (c->flags & CHN_F_BUSY) 3423fdb3676SAriff Abdullah err = EBUSY; 3433fdb3676SAriff Abdullah else 3443fdb3676SAriff Abdullah err = EINVAL; 3453fdb3676SAriff Abdullah CHN_UNLOCK(c); 346bba4862cSAriff Abdullah return (err); 347bba4862cSAriff Abdullah } else if ((devunit == -1 || devunit == -2) && 348bba4862cSAriff Abdullah c->direction == direction && (c->flags & CHN_F_BUSY)) 349a1d444e1SAriff Abdullah err = EBUSY; 35049c5e6e2SCameron Grant CHN_UNLOCK(c); 351285648f9SCameron Grant } 352f637a36cSCameron Grant 353e4e61333SAriff Abdullah if (devunit == -2) 354e4e61333SAriff Abdullah return (err); 355e4e61333SAriff Abdullah 35690da2b28SAriff Abdullah vchan_alloc: 357f637a36cSCameron Grant /* no channel available */ 358e4e61333SAriff Abdullah if (devunit == -1 || snd_unit2d(devunit) == SND_DEV_DSPHW_VPLAY || 359e4e61333SAriff Abdullah snd_unit2d(devunit) == SND_DEV_DSPHW_VREC) { 360bba4862cSAriff Abdullah if (!(vchancount > 0 && vchancount < snd_maxautovchans) && 361bba4862cSAriff Abdullah (devunit == -1 || snd_unit2c(devunit) < snd_maxautovchans)) 362bba4862cSAriff Abdullah return (err); 363bba4862cSAriff Abdullah err = pcm_setvchans(d, direction, vchancount + 1, 364bba4862cSAriff Abdullah (devunit == -1) ? -1 : snd_unit2c(devunit)); 365a1d444e1SAriff Abdullah if (err == 0) { 366bba4862cSAriff Abdullah if (devunit == -1) 367bba4862cSAriff Abdullah devunit = -2; 3683fdb3676SAriff Abdullah goto retry_chnalloc; 369f637a36cSCameron Grant } 370f637a36cSCameron Grant } 371f637a36cSCameron Grant 372bba4862cSAriff Abdullah return (err); 373285648f9SCameron Grant } 374285648f9SCameron Grant 375b8f0d9e0SCameron Grant /* release a locked channel and unlock it */ 376285648f9SCameron Grant int 377b8f0d9e0SCameron Grant pcm_chnrelease(struct pcm_channel *c) 378285648f9SCameron Grant { 379e4e61333SAriff Abdullah PCM_BUSYASSERT(c->parentsnddev); 380b8f0d9e0SCameron Grant CHN_LOCKASSERT(c); 381e4e61333SAriff Abdullah 382285648f9SCameron Grant c->flags &= ~CHN_F_BUSY; 383b8f0d9e0SCameron Grant c->pid = -1; 38490da2b28SAriff Abdullah strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm)); 38549c5e6e2SCameron Grant CHN_UNLOCK(c); 386e4e61333SAriff Abdullah 387e4e61333SAriff Abdullah return (0); 388285648f9SCameron Grant } 389285648f9SCameron Grant 390285648f9SCameron Grant int 391285648f9SCameron Grant pcm_chnref(struct pcm_channel *c, int ref) 392285648f9SCameron Grant { 393e4e61333SAriff Abdullah PCM_BUSYASSERT(c->parentsnddev); 394b8f0d9e0SCameron Grant CHN_LOCKASSERT(c); 395e4e61333SAriff Abdullah 396285648f9SCameron Grant c->refcount += ref; 397e4e61333SAriff Abdullah 398e4e61333SAriff Abdullah return (c->refcount); 399285648f9SCameron Grant } 400285648f9SCameron Grant 40167b1dce3SCameron Grant int 40267b1dce3SCameron Grant pcm_inprog(struct snddev_info *d, int delta) 40367b1dce3SCameron Grant { 40490da2b28SAriff Abdullah PCM_LOCKASSERT(d); 405a527dbc7SCameron Grant 406a527dbc7SCameron Grant d->inprog += delta; 407e4e61333SAriff Abdullah 408e4e61333SAriff Abdullah return (d->inprog); 40967b1dce3SCameron Grant } 41067b1dce3SCameron Grant 41167b1dce3SCameron Grant static void 41267b1dce3SCameron Grant pcm_setmaxautovchans(struct snddev_info *d, int num) 41367b1dce3SCameron Grant { 414e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 415e4e61333SAriff Abdullah 416bba4862cSAriff Abdullah if (num < 0) 417bba4862cSAriff Abdullah return; 418bba4862cSAriff Abdullah 419bba4862cSAriff Abdullah if (num >= 0 && d->pvchancount > num) 420bba4862cSAriff Abdullah (void)pcm_setvchans(d, PCMDIR_PLAY, num, -1); 421bba4862cSAriff Abdullah else if (num > 0 && d->pvchancount == 0) 422bba4862cSAriff Abdullah (void)pcm_setvchans(d, PCMDIR_PLAY, 1, -1); 423bba4862cSAriff Abdullah 424bba4862cSAriff Abdullah if (num >= 0 && d->rvchancount > num) 425bba4862cSAriff Abdullah (void)pcm_setvchans(d, PCMDIR_REC, num, -1); 426bba4862cSAriff Abdullah else if (num > 0 && d->rvchancount == 0) 427bba4862cSAriff Abdullah (void)pcm_setvchans(d, PCMDIR_REC, 1, -1); 428bba4862cSAriff Abdullah 429bba4862cSAriff Abdullah pcm_clonereset(d); 43067b1dce3SCameron Grant } 43167b1dce3SCameron Grant 43233dbf14aSCameron Grant static int 433851a904aSAlexander Leidinger sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS) 43433dbf14aSCameron Grant { 435b8f0d9e0SCameron Grant struct snddev_info *d; 43633dbf14aSCameron Grant int error, unit; 43733dbf14aSCameron Grant 43833dbf14aSCameron Grant unit = snd_unit; 439041b706bSDavid Malone error = sysctl_handle_int(oidp, &unit, 0, req); 44033dbf14aSCameron Grant if (error == 0 && req->newptr != NULL) { 441b8f0d9e0SCameron Grant d = devclass_get_softc(pcm_devclass, unit); 442e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm)) 443b8f0d9e0SCameron Grant return EINVAL; 44433dbf14aSCameron Grant snd_unit = unit; 445cbebc90dSAlexander Motin snd_unit_auto = 0; 44633dbf14aSCameron Grant } 44733dbf14aSCameron Grant return (error); 44833dbf14aSCameron Grant } 449851a904aSAlexander Leidinger /* XXX: do we need a way to let the user change the default unit? */ 450b7d6c6b5SEitan Adler SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit, 451b7d6c6b5SEitan Adler CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY, 452b7d6c6b5SEitan Adler 0, sizeof(int), sysctl_hw_snd_default_unit, "I", 453b7d6c6b5SEitan Adler "default sound device"); 454987e5972SCameron Grant 455cd9766c5SCameron Grant static int 45667b1dce3SCameron Grant sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS) 457cd9766c5SCameron Grant { 45867b1dce3SCameron Grant struct snddev_info *d; 45967b1dce3SCameron Grant int i, v, error; 460cd9766c5SCameron Grant 46167b1dce3SCameron Grant v = snd_maxautovchans; 462041b706bSDavid Malone error = sysctl_handle_int(oidp, &v, 0, req); 463cd9766c5SCameron Grant if (error == 0 && req->newptr != NULL) { 464bba4862cSAriff Abdullah if (v < 0) 465bba4862cSAriff Abdullah v = 0; 466bba4862cSAriff Abdullah if (v > SND_MAXVCHANS) 467bba4862cSAriff Abdullah v = SND_MAXVCHANS; 468bba4862cSAriff Abdullah snd_maxautovchans = v; 4699c271f79SAriff Abdullah for (i = 0; pcm_devclass != NULL && 4709c271f79SAriff Abdullah i < devclass_get_maxunit(pcm_devclass); i++) { 47167b1dce3SCameron Grant d = devclass_get_softc(pcm_devclass, i); 472e4e61333SAriff Abdullah if (!PCM_REGISTERED(d)) 47367b1dce3SCameron Grant continue; 474e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 47567b1dce3SCameron Grant pcm_setmaxautovchans(d, v); 476e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 47767b1dce3SCameron Grant } 47867b1dce3SCameron Grant } 479cd9766c5SCameron Grant return (error); 480cd9766c5SCameron Grant } 48167b1dce3SCameron Grant SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans, CTLTYPE_INT | CTLFLAG_RW, 482a580b31aSAriff Abdullah 0, sizeof(int), sysctl_hw_snd_maxautovchans, "I", "maximum virtual channel"); 483f637a36cSCameron Grant 484285648f9SCameron Grant struct pcm_channel * 485bba4862cSAriff Abdullah pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo) 486987e5972SCameron Grant { 487bba4862cSAriff Abdullah struct pcm_channel *ch; 488bba4862cSAriff Abdullah int direction, err, rpnum, *pnum, max; 489bba4862cSAriff Abdullah int udc, device, chan; 490bba4862cSAriff Abdullah char *dirs, *devname, buf[CHN_NAMELEN]; 491bba4862cSAriff Abdullah 492e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 49390da2b28SAriff Abdullah PCM_LOCKASSERT(d); 494bba4862cSAriff Abdullah KASSERT(num >= -1, ("invalid num=%d", num)); 495bba4862cSAriff Abdullah 496987e5972SCameron Grant 497285648f9SCameron Grant switch (dir) { 498285648f9SCameron Grant case PCMDIR_PLAY: 499285648f9SCameron Grant dirs = "play"; 500a3193a9cSDon Lewis direction = PCMDIR_PLAY; 501a67fe5c1SCameron Grant pnum = &d->playcount; 502bba4862cSAriff Abdullah device = SND_DEV_DSPHW_PLAY; 503bba4862cSAriff Abdullah max = SND_MAXHWCHAN; 504285648f9SCameron Grant break; 505bba4862cSAriff Abdullah case PCMDIR_PLAY_VIRTUAL: 506bba4862cSAriff Abdullah dirs = "virtual"; 507bba4862cSAriff Abdullah direction = PCMDIR_PLAY; 508bba4862cSAriff Abdullah pnum = &d->pvchancount; 509bba4862cSAriff Abdullah device = SND_DEV_DSPHW_VPLAY; 510bba4862cSAriff Abdullah max = SND_MAXVCHANS; 511bba4862cSAriff Abdullah break; 512285648f9SCameron Grant case PCMDIR_REC: 513285648f9SCameron Grant dirs = "record"; 514a3193a9cSDon Lewis direction = PCMDIR_REC; 515a67fe5c1SCameron Grant pnum = &d->reccount; 516bba4862cSAriff Abdullah device = SND_DEV_DSPHW_REC; 517bba4862cSAriff Abdullah max = SND_MAXHWCHAN; 518285648f9SCameron Grant break; 519bba4862cSAriff Abdullah case PCMDIR_REC_VIRTUAL: 520285648f9SCameron Grant dirs = "virtual"; 521bba4862cSAriff Abdullah direction = PCMDIR_REC; 522bba4862cSAriff Abdullah pnum = &d->rvchancount; 523bba4862cSAriff Abdullah device = SND_DEV_DSPHW_VREC; 524bba4862cSAriff Abdullah max = SND_MAXVCHANS; 525285648f9SCameron Grant break; 526285648f9SCameron Grant default: 527e4e61333SAriff Abdullah return (NULL); 5289c326820SCameron Grant } 529285648f9SCameron Grant 530bba4862cSAriff Abdullah chan = (num == -1) ? 0 : num; 531285648f9SCameron Grant 532e4e61333SAriff Abdullah if (*pnum >= max || chan >= max) 533e4e61333SAriff Abdullah return (NULL); 534bba4862cSAriff Abdullah 5353fdb3676SAriff Abdullah rpnum = 0; 536bba4862cSAriff Abdullah 537bba4862cSAriff Abdullah CHN_FOREACH(ch, d, channels.pcm) { 538bba4862cSAriff Abdullah if (CHN_DEV(ch) != device) 5393fdb3676SAriff Abdullah continue; 540bba4862cSAriff Abdullah if (chan == CHN_CHAN(ch)) { 541bba4862cSAriff Abdullah if (num != -1) { 5423fdb3676SAriff Abdullah device_printf(d->dev, 543bba4862cSAriff Abdullah "channel num=%d allocated!\n", chan); 544e4e61333SAriff Abdullah return (NULL); 545bba4862cSAriff Abdullah } 546bba4862cSAriff Abdullah chan++; 547bba4862cSAriff Abdullah if (chan >= max) { 548bba4862cSAriff Abdullah device_printf(d->dev, 549bba4862cSAriff Abdullah "chan=%d > %d\n", chan, max); 550e4e61333SAriff Abdullah return (NULL); 551bba4862cSAriff Abdullah } 5523fdb3676SAriff Abdullah } 5533fdb3676SAriff Abdullah rpnum++; 5543fdb3676SAriff Abdullah } 555bba4862cSAriff Abdullah 5563fdb3676SAriff Abdullah if (*pnum != rpnum) { 5573fdb3676SAriff Abdullah device_printf(d->dev, 558bba4862cSAriff Abdullah "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n", 5593fdb3676SAriff Abdullah __func__, dirs, *pnum, rpnum); 560e4e61333SAriff Abdullah return (NULL); 5613fdb3676SAriff Abdullah } 562a67fe5c1SCameron Grant 563bba4862cSAriff Abdullah udc = snd_mkunit(device_get_unit(d->dev), device, chan); 564bba4862cSAriff Abdullah devname = dsp_unit2name(buf, sizeof(buf), udc); 565bba4862cSAriff Abdullah 566bba4862cSAriff Abdullah if (devname == NULL) { 567bba4862cSAriff Abdullah device_printf(d->dev, 568bba4862cSAriff Abdullah "Failed to query device name udc=0x%08x\n", udc); 569e4e61333SAriff Abdullah return (NULL); 570bba4862cSAriff Abdullah } 571bba4862cSAriff Abdullah 57290da2b28SAriff Abdullah PCM_UNLOCK(d); 573bba4862cSAriff Abdullah ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO); 574bba4862cSAriff Abdullah ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO); 575bba4862cSAriff Abdullah ch->unit = udc; 576285648f9SCameron Grant ch->pid = -1; 57790da2b28SAriff Abdullah strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm)); 578285648f9SCameron Grant ch->parentsnddev = d; 579285648f9SCameron Grant ch->parentchannel = parent; 580436c9b65SScott Long ch->dev = d->dev; 581bba4862cSAriff Abdullah ch->trigger = PCMTRIG_STOP; 582bba4862cSAriff Abdullah snprintf(ch->name, sizeof(ch->name), "%s:%s:%s", 583bba4862cSAriff Abdullah device_get_nameunit(ch->dev), dirs, devname); 584285648f9SCameron Grant 585a3193a9cSDon Lewis err = chn_init(ch, devinfo, dir, direction); 58690da2b28SAriff Abdullah PCM_LOCK(d); 5870f55ac6cSCameron Grant if (err) { 588bba4862cSAriff Abdullah device_printf(d->dev, "chn_init(%s) failed: err = %d\n", 589bba4862cSAriff Abdullah ch->name, err); 590285648f9SCameron Grant kobj_delete(ch->methods, M_DEVBUF); 591285648f9SCameron Grant free(ch, M_DEVBUF); 592e4e61333SAriff Abdullah return (NULL); 593bbb5bf3dSCameron Grant } 594285648f9SCameron Grant 595e4e61333SAriff Abdullah return (ch); 596285648f9SCameron Grant } 597285648f9SCameron Grant 598285648f9SCameron Grant int 599285648f9SCameron Grant pcm_chn_destroy(struct pcm_channel *ch) 600285648f9SCameron Grant { 601a67fe5c1SCameron Grant struct snddev_info *d; 602285648f9SCameron Grant int err; 603285648f9SCameron Grant 604a67fe5c1SCameron Grant d = ch->parentsnddev; 605e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 606e4e61333SAriff Abdullah 607285648f9SCameron Grant err = chn_kill(ch); 608285648f9SCameron Grant if (err) { 609e4e61333SAriff Abdullah device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n", 610e4e61333SAriff Abdullah ch->name, err); 611e4e61333SAriff Abdullah return (err); 612285648f9SCameron Grant } 613285648f9SCameron Grant 614285648f9SCameron Grant kobj_delete(ch->methods, M_DEVBUF); 615285648f9SCameron Grant free(ch, M_DEVBUF); 616285648f9SCameron Grant 617e4e61333SAriff Abdullah return (0); 618285648f9SCameron Grant } 619285648f9SCameron Grant 620285648f9SCameron Grant int 6215ee30e27SMathew Kanner pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch) 622285648f9SCameron Grant { 623e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 62490da2b28SAriff Abdullah PCM_LOCKASSERT(d); 625bba4862cSAriff Abdullah KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY || 626bba4862cSAriff Abdullah ch->direction == PCMDIR_REC), ("Invalid pcm channel")); 627285648f9SCameron Grant 62890da2b28SAriff Abdullah CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm); 6293fdb3676SAriff Abdullah 630e4e61333SAriff Abdullah switch (CHN_DEV(ch)) { 631e4e61333SAriff Abdullah case SND_DEV_DSPHW_PLAY: 632e4e61333SAriff Abdullah d->playcount++; 633e4e61333SAriff Abdullah break; 634e4e61333SAriff Abdullah case SND_DEV_DSPHW_VPLAY: 635e4e61333SAriff Abdullah d->pvchancount++; 636e4e61333SAriff Abdullah break; 637e4e61333SAriff Abdullah case SND_DEV_DSPHW_REC: 638e4e61333SAriff Abdullah d->reccount++; 639e4e61333SAriff Abdullah break; 640e4e61333SAriff Abdullah case SND_DEV_DSPHW_VREC: 641e4e61333SAriff Abdullah d->rvchancount++; 642e4e61333SAriff Abdullah break; 643e4e61333SAriff Abdullah default: 644e4e61333SAriff Abdullah break; 645e4e61333SAriff Abdullah } 646b8f0d9e0SCameron Grant 647e4e61333SAriff Abdullah d->devcount++; 648e4e61333SAriff Abdullah 649e4e61333SAriff Abdullah return (0); 65033dbf14aSCameron Grant } 65133dbf14aSCameron Grant 652285648f9SCameron Grant int 6535ee30e27SMathew Kanner pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch) 65433dbf14aSCameron Grant { 655bba4862cSAriff Abdullah struct pcm_channel *tmp; 65633dbf14aSCameron Grant 657e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 65890da2b28SAriff Abdullah PCM_LOCKASSERT(d); 659e4e61333SAriff Abdullah 660bba4862cSAriff Abdullah tmp = NULL; 661a527dbc7SCameron Grant 662bba4862cSAriff Abdullah CHN_FOREACH(tmp, d, channels.pcm) { 663bba4862cSAriff Abdullah if (tmp == ch) 664bba4862cSAriff Abdullah break; 66533dbf14aSCameron Grant } 666bba4862cSAriff Abdullah 667bba4862cSAriff Abdullah if (tmp != ch) 668e4e61333SAriff Abdullah return (EINVAL); 66967beb5a5SCameron Grant 670bba4862cSAriff Abdullah CHN_REMOVE(d, ch, channels.pcm); 671e4e61333SAriff Abdullah 672bba4862cSAriff Abdullah switch (CHN_DEV(ch)) { 673bba4862cSAriff Abdullah case SND_DEV_DSPHW_PLAY: 67467beb5a5SCameron Grant d->playcount--; 675bba4862cSAriff Abdullah break; 676bba4862cSAriff Abdullah case SND_DEV_DSPHW_VPLAY: 677bba4862cSAriff Abdullah d->pvchancount--; 678bba4862cSAriff Abdullah break; 679bba4862cSAriff Abdullah case SND_DEV_DSPHW_REC: 680bba4862cSAriff Abdullah d->reccount--; 681bba4862cSAriff Abdullah break; 682bba4862cSAriff Abdullah case SND_DEV_DSPHW_VREC: 683bba4862cSAriff Abdullah d->rvchancount--; 684bba4862cSAriff Abdullah break; 685bba4862cSAriff Abdullah default: 686bba4862cSAriff Abdullah break; 687bba4862cSAriff Abdullah } 688285648f9SCameron Grant 689e4e61333SAriff Abdullah d->devcount--; 690e4e61333SAriff Abdullah 691e4e61333SAriff Abdullah return (0); 692987e5972SCameron Grant } 693987e5972SCameron Grant 694987e5972SCameron Grant int 695285648f9SCameron Grant pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo) 696285648f9SCameron Grant { 697285648f9SCameron Grant struct snddev_info *d = device_get_softc(dev); 69867b1dce3SCameron Grant struct pcm_channel *ch; 69967b1dce3SCameron Grant int err; 700285648f9SCameron Grant 701e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 702e4e61333SAriff Abdullah 70390da2b28SAriff Abdullah PCM_LOCK(d); 704bba4862cSAriff Abdullah ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo); 705285648f9SCameron Grant if (!ch) { 706e4e61333SAriff Abdullah device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n", 707e4e61333SAriff Abdullah cls->name, dir, devinfo); 70890da2b28SAriff Abdullah PCM_UNLOCK(d); 709e4e61333SAriff Abdullah return (ENODEV); 710285648f9SCameron Grant } 711cd9766c5SCameron Grant 7125ee30e27SMathew Kanner err = pcm_chn_add(d, ch); 71390da2b28SAriff Abdullah PCM_UNLOCK(d); 714285648f9SCameron Grant if (err) { 715e4e61333SAriff Abdullah device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n", 716e4e61333SAriff Abdullah ch->name, err); 717285648f9SCameron Grant pcm_chn_destroy(ch); 718cd9766c5SCameron Grant } 719cd9766c5SCameron Grant 720e4e61333SAriff Abdullah return (err); 721285648f9SCameron Grant } 722285648f9SCameron Grant 723285648f9SCameron Grant static int 724285648f9SCameron Grant pcm_killchan(device_t dev) 725285648f9SCameron Grant { 726285648f9SCameron Grant struct snddev_info *d = device_get_softc(dev); 727e33bee07SOlivier Houchard struct pcm_channel *ch; 728e4e61333SAriff Abdullah int error; 729e4e61333SAriff Abdullah 730e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 731285648f9SCameron Grant 732bba4862cSAriff Abdullah ch = CHN_FIRST(d, channels.pcm); 733285648f9SCameron Grant 73490da2b28SAriff Abdullah PCM_LOCK(d); 735bba4862cSAriff Abdullah error = pcm_chn_remove(d, ch); 73690da2b28SAriff Abdullah PCM_UNLOCK(d); 737e33bee07SOlivier Houchard if (error) 738e33bee07SOlivier Houchard return (error); 739e33bee07SOlivier Houchard return (pcm_chn_destroy(ch)); 740285648f9SCameron Grant } 741285648f9SCameron Grant 742cbebc90dSAlexander Motin static int 743cbebc90dSAlexander Motin pcm_best_unit(int old) 744cbebc90dSAlexander Motin { 745cbebc90dSAlexander Motin struct snddev_info *d; 746cbebc90dSAlexander Motin int i, best, bestprio, prio; 747cbebc90dSAlexander Motin 748cbebc90dSAlexander Motin best = -1; 749cbebc90dSAlexander Motin bestprio = -100; 750cbebc90dSAlexander Motin for (i = 0; pcm_devclass != NULL && 751cbebc90dSAlexander Motin i < devclass_get_maxunit(pcm_devclass); i++) { 752cbebc90dSAlexander Motin d = devclass_get_softc(pcm_devclass, i); 753cbebc90dSAlexander Motin if (!PCM_REGISTERED(d)) 754cbebc90dSAlexander Motin continue; 755cbebc90dSAlexander Motin prio = 0; 756cbebc90dSAlexander Motin if (d->playcount == 0) 757cbebc90dSAlexander Motin prio -= 10; 758cbebc90dSAlexander Motin if (d->reccount == 0) 759cbebc90dSAlexander Motin prio -= 2; 760cbebc90dSAlexander Motin if (prio > bestprio || (prio == bestprio && i == old)) { 761cbebc90dSAlexander Motin best = i; 762cbebc90dSAlexander Motin bestprio = prio; 763cbebc90dSAlexander Motin } 764cbebc90dSAlexander Motin } 765cbebc90dSAlexander Motin return (best); 766cbebc90dSAlexander Motin } 767cbebc90dSAlexander Motin 768285648f9SCameron Grant int 769987e5972SCameron Grant pcm_setstatus(device_t dev, char *str) 770987e5972SCameron Grant { 77166ef8af5SCameron Grant struct snddev_info *d = device_get_softc(dev); 77249c5e6e2SCameron Grant 773e4e61333SAriff Abdullah PCM_BUSYASSERT(d); 774bba4862cSAriff Abdullah 775e4e61333SAriff Abdullah if (d->playcount == 0 || d->reccount == 0) 776e4e61333SAriff Abdullah d->flags |= SD_F_SIMPLEX; 777e4e61333SAriff Abdullah 778e4e61333SAriff Abdullah if ((d->playcount > 0 || d->reccount > 0) && 779e4e61333SAriff Abdullah !(d->flags & SD_F_AUTOVCHAN)) { 780e4e61333SAriff Abdullah d->flags |= SD_F_AUTOVCHAN; 781e4e61333SAriff Abdullah vchan_initsys(dev); 782e4e61333SAriff Abdullah } 783e4e61333SAriff Abdullah 784e4e61333SAriff Abdullah pcm_setmaxautovchans(d, snd_maxautovchans); 785bba4862cSAriff Abdullah 786a580b31aSAriff Abdullah strlcpy(d->status, str, SND_STATUSLEN); 787bba4862cSAriff Abdullah 78890da2b28SAriff Abdullah PCM_LOCK(d); 789e4e61333SAriff Abdullah 790bba4862cSAriff Abdullah /* Last stage, enable cloning. */ 791e4e61333SAriff Abdullah if (d->clones != NULL) 792bba4862cSAriff Abdullah (void)snd_clone_enable(d->clones); 793e4e61333SAriff Abdullah 794e4e61333SAriff Abdullah /* Done, we're ready.. */ 795e4e61333SAriff Abdullah d->flags |= SD_F_REGISTERED; 796e4e61333SAriff Abdullah 797e4e61333SAriff Abdullah PCM_RELEASE(d); 798bba4862cSAriff Abdullah 79990da2b28SAriff Abdullah PCM_UNLOCK(d); 800bba4862cSAriff Abdullah 801cbebc90dSAlexander Motin if (snd_unit_auto < 0) 802cbebc90dSAlexander Motin snd_unit_auto = (snd_unit < 0) ? 1 : 0; 803cbebc90dSAlexander Motin if (snd_unit < 0 || snd_unit_auto > 1) 804f3685841SAriff Abdullah snd_unit = device_get_unit(dev); 805cbebc90dSAlexander Motin else if (snd_unit_auto == 1) 806cbebc90dSAlexander Motin snd_unit = pcm_best_unit(snd_unit); 807f3685841SAriff Abdullah 808e4e61333SAriff Abdullah return (0); 809987e5972SCameron Grant } 810987e5972SCameron Grant 811a1d444e1SAriff Abdullah uint32_t 812987e5972SCameron Grant pcm_getflags(device_t dev) 813987e5972SCameron Grant { 81466ef8af5SCameron Grant struct snddev_info *d = device_get_softc(dev); 81549c5e6e2SCameron Grant 816987e5972SCameron Grant return d->flags; 817987e5972SCameron Grant } 818987e5972SCameron Grant 819987e5972SCameron Grant void 820a1d444e1SAriff Abdullah pcm_setflags(device_t dev, uint32_t val) 821987e5972SCameron Grant { 82266ef8af5SCameron Grant struct snddev_info *d = device_get_softc(dev); 823d95502a8SCameron Grant 824987e5972SCameron Grant d->flags = val; 825987e5972SCameron Grant } 826987e5972SCameron Grant 82739004e69SCameron Grant void * 82839004e69SCameron Grant pcm_getdevinfo(device_t dev) 82939004e69SCameron Grant { 83066ef8af5SCameron Grant struct snddev_info *d = device_get_softc(dev); 83149c5e6e2SCameron Grant 83239004e69SCameron Grant return d->devinfo; 83339004e69SCameron Grant } 83439004e69SCameron Grant 835a67fe5c1SCameron Grant unsigned int 836d55d96f6SAlexander Leidinger pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz) 837a67fe5c1SCameron Grant { 838a67fe5c1SCameron Grant struct snddev_info *d = device_get_softc(dev); 8394e60be34SCameron Grant int sz, x; 840a67fe5c1SCameron Grant 841a67fe5c1SCameron Grant sz = 0; 8424e60be34SCameron Grant if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) { 8434e60be34SCameron Grant x = sz; 844d55d96f6SAlexander Leidinger RANGE(sz, minbufsz, maxbufsz); 8454e60be34SCameron Grant if (x != sz) 846d55d96f6SAlexander Leidinger device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz); 847d55d96f6SAlexander Leidinger x = minbufsz; 8484e60be34SCameron Grant while (x < sz) 8494e60be34SCameron Grant x <<= 1; 8504e60be34SCameron Grant if (x > sz) 8514e60be34SCameron Grant x >>= 1; 8524e60be34SCameron Grant if (x != sz) { 8534e60be34SCameron Grant device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x); 8544e60be34SCameron Grant sz = x; 8554e60be34SCameron Grant } 8564e60be34SCameron Grant } else { 857a67fe5c1SCameron Grant sz = deflt; 8584e60be34SCameron Grant } 8594e60be34SCameron Grant 860a67fe5c1SCameron Grant d->bufsz = sz; 861a67fe5c1SCameron Grant 862a67fe5c1SCameron Grant return sz; 863a67fe5c1SCameron Grant } 864a67fe5c1SCameron Grant 86590da2b28SAriff Abdullah static int 86690da2b28SAriff Abdullah sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS) 86790da2b28SAriff Abdullah { 86890da2b28SAriff Abdullah struct snddev_info *d; 86990da2b28SAriff Abdullah int err, val; 87090da2b28SAriff Abdullah 87190da2b28SAriff Abdullah d = oidp->oid_arg1; 87290da2b28SAriff Abdullah if (!PCM_REGISTERED(d)) 87390da2b28SAriff Abdullah return (ENODEV); 87490da2b28SAriff Abdullah 87590da2b28SAriff Abdullah PCM_LOCK(d); 87690da2b28SAriff Abdullah PCM_WAIT(d); 87790da2b28SAriff Abdullah val = (d->flags & SD_F_BITPERFECT) ? 1 : 0; 87890da2b28SAriff Abdullah PCM_ACQUIRE(d); 87990da2b28SAriff Abdullah PCM_UNLOCK(d); 88090da2b28SAriff Abdullah 88190da2b28SAriff Abdullah err = sysctl_handle_int(oidp, &val, 0, req); 88290da2b28SAriff Abdullah 88390da2b28SAriff Abdullah if (err == 0 && req->newptr != NULL) { 88490da2b28SAriff Abdullah if (!(val == 0 || val == 1)) { 88590da2b28SAriff Abdullah PCM_RELEASE_QUICK(d); 88690da2b28SAriff Abdullah return (EINVAL); 88790da2b28SAriff Abdullah } 88890da2b28SAriff Abdullah 88990da2b28SAriff Abdullah PCM_LOCK(d); 89090da2b28SAriff Abdullah 89190da2b28SAriff Abdullah d->flags &= ~SD_F_BITPERFECT; 89290da2b28SAriff Abdullah d->flags |= (val != 0) ? SD_F_BITPERFECT : 0; 89390da2b28SAriff Abdullah 89490da2b28SAriff Abdullah PCM_RELEASE(d); 89590da2b28SAriff Abdullah PCM_UNLOCK(d); 89690da2b28SAriff Abdullah } else 89790da2b28SAriff Abdullah PCM_RELEASE_QUICK(d); 89890da2b28SAriff Abdullah 89990da2b28SAriff Abdullah return (err); 90090da2b28SAriff Abdullah } 90190da2b28SAriff Abdullah 90290da2b28SAriff Abdullah #ifdef SND_DEBUG 903bba4862cSAriff Abdullah static int 904bba4862cSAriff Abdullah sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS) 905bba4862cSAriff Abdullah { 906bba4862cSAriff Abdullah struct snddev_info *d; 907bba4862cSAriff Abdullah uint32_t flags; 908bba4862cSAriff Abdullah int err; 909bba4862cSAriff Abdullah 910bba4862cSAriff Abdullah d = oidp->oid_arg1; 911e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || d->clones == NULL) 912bba4862cSAriff Abdullah return (ENODEV); 913bba4862cSAriff Abdullah 914e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 915e4e61333SAriff Abdullah 916bba4862cSAriff Abdullah flags = snd_clone_getflags(d->clones); 917041b706bSDavid Malone err = sysctl_handle_int(oidp, &flags, 0, req); 918bba4862cSAriff Abdullah 919bba4862cSAriff Abdullah if (err == 0 && req->newptr != NULL) { 920e4e61333SAriff Abdullah if (flags & ~SND_CLONE_MASK) 921bba4862cSAriff Abdullah err = EINVAL; 922e4e61333SAriff Abdullah else 923bba4862cSAriff Abdullah (void)snd_clone_setflags(d->clones, flags); 924bba4862cSAriff Abdullah } 925e4e61333SAriff Abdullah 926e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 927bba4862cSAriff Abdullah 928bba4862cSAriff Abdullah return (err); 929bba4862cSAriff Abdullah } 930bba4862cSAriff Abdullah 931bba4862cSAriff Abdullah static int 932bba4862cSAriff Abdullah sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS) 933bba4862cSAriff Abdullah { 934bba4862cSAriff Abdullah struct snddev_info *d; 935bba4862cSAriff Abdullah int err, deadline; 936bba4862cSAriff Abdullah 937bba4862cSAriff Abdullah d = oidp->oid_arg1; 938e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || d->clones == NULL) 939bba4862cSAriff Abdullah return (ENODEV); 940bba4862cSAriff Abdullah 941e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 942e4e61333SAriff Abdullah 943bba4862cSAriff Abdullah deadline = snd_clone_getdeadline(d->clones); 944041b706bSDavid Malone err = sysctl_handle_int(oidp, &deadline, 0, req); 945bba4862cSAriff Abdullah 946bba4862cSAriff Abdullah if (err == 0 && req->newptr != NULL) { 947bba4862cSAriff Abdullah if (deadline < 0) 948bba4862cSAriff Abdullah err = EINVAL; 949e4e61333SAriff Abdullah else 950bba4862cSAriff Abdullah (void)snd_clone_setdeadline(d->clones, deadline); 951bba4862cSAriff Abdullah } 952e4e61333SAriff Abdullah 953e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 954bba4862cSAriff Abdullah 955bba4862cSAriff Abdullah return (err); 956bba4862cSAriff Abdullah } 957bba4862cSAriff Abdullah 958bba4862cSAriff Abdullah static int 959bba4862cSAriff Abdullah sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS) 960bba4862cSAriff Abdullah { 961bba4862cSAriff Abdullah struct snddev_info *d; 962bba4862cSAriff Abdullah int err, val; 963bba4862cSAriff Abdullah 964bba4862cSAriff Abdullah d = oidp->oid_arg1; 965e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || d->clones == NULL) 966bba4862cSAriff Abdullah return (ENODEV); 967bba4862cSAriff Abdullah 968bba4862cSAriff Abdullah val = 0; 969041b706bSDavid Malone err = sysctl_handle_int(oidp, &val, 0, req); 970bba4862cSAriff Abdullah 971bba4862cSAriff Abdullah if (err == 0 && req->newptr != NULL && val != 0) { 972e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 973e4e61333SAriff Abdullah val = snd_clone_gc(d->clones); 974e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 975e4e61333SAriff Abdullah if (bootverbose != 0 || snd_verbose > 3) 976e4e61333SAriff Abdullah device_printf(d->dev, "clone gc: pruned=%d\n", val); 977bba4862cSAriff Abdullah } 978bba4862cSAriff Abdullah 979bba4862cSAriff Abdullah return (err); 980bba4862cSAriff Abdullah } 981bba4862cSAriff Abdullah 982bba4862cSAriff Abdullah static int 983bba4862cSAriff Abdullah sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS) 984bba4862cSAriff Abdullah { 985bba4862cSAriff Abdullah struct snddev_info *d; 986bba4862cSAriff Abdullah int i, err, val; 987bba4862cSAriff Abdullah 988bba4862cSAriff Abdullah val = 0; 989041b706bSDavid Malone err = sysctl_handle_int(oidp, &val, 0, req); 990bba4862cSAriff Abdullah 991bba4862cSAriff Abdullah if (err == 0 && req->newptr != NULL && val != 0) { 9929c271f79SAriff Abdullah for (i = 0; pcm_devclass != NULL && 9939c271f79SAriff Abdullah i < devclass_get_maxunit(pcm_devclass); i++) { 994bba4862cSAriff Abdullah d = devclass_get_softc(pcm_devclass, i); 995e4e61333SAriff Abdullah if (!PCM_REGISTERED(d) || d->clones == NULL) 996bba4862cSAriff Abdullah continue; 997e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 998e4e61333SAriff Abdullah val = snd_clone_gc(d->clones); 999e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 1000e4e61333SAriff Abdullah if (bootverbose != 0 || snd_verbose > 3) 1001e4e61333SAriff Abdullah device_printf(d->dev, "clone gc: pruned=%d\n", 1002e4e61333SAriff Abdullah val); 1003bba4862cSAriff Abdullah } 1004bba4862cSAriff Abdullah } 1005bba4862cSAriff Abdullah 1006bba4862cSAriff Abdullah return (err); 1007bba4862cSAriff Abdullah } 1008bba4862cSAriff Abdullah SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc, CTLTYPE_INT | CTLFLAG_RW, 1009bba4862cSAriff Abdullah 0, sizeof(int), sysctl_hw_snd_clone_gc, "I", 1010bba4862cSAriff Abdullah "global clone garbage collector"); 1011bba4862cSAriff Abdullah #endif 1012bba4862cSAriff Abdullah 1013987e5972SCameron Grant int 1014987e5972SCameron Grant pcm_register(device_t dev, void *devinfo, int numplay, int numrec) 1015987e5972SCameron Grant { 1016bba4862cSAriff Abdullah struct snddev_info *d; 101790da2b28SAriff Abdullah int i; 1018987e5972SCameron Grant 1019b8a36395SCameron Grant if (pcm_veto_load) { 1020b8a36395SCameron Grant device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load); 1021b8a36395SCameron Grant 1022b8a36395SCameron Grant return EINVAL; 1023b8a36395SCameron Grant } 1024b8a36395SCameron Grant 1025bba4862cSAriff Abdullah if (device_get_unit(dev) > PCMMAXUNIT) { 1026bba4862cSAriff Abdullah device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n", 1027bba4862cSAriff Abdullah device_get_unit(dev), PCMMAXUNIT); 1028bba4862cSAriff Abdullah device_printf(dev, 1029bba4862cSAriff Abdullah "Use 'hw.snd.maxunit' tunable to raise the limit.\n"); 1030bba4862cSAriff Abdullah return ENODEV; 1031bba4862cSAriff Abdullah } 1032bba4862cSAriff Abdullah 1033bba4862cSAriff Abdullah d = device_get_softc(dev); 1034e4e61333SAriff Abdullah d->dev = dev; 10352c69ba87SJohn Baldwin d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev"); 1036e4e61333SAriff Abdullah cv_init(&d->cv, device_get_nameunit(dev)); 1037e4e61333SAriff Abdullah PCM_ACQUIRE_QUICK(d); 1038e4e61333SAriff Abdullah dsp_cdevinfo_init(d); 10397233ababSAlexander Leidinger #if 0 10407233ababSAlexander Leidinger /* 10417233ababSAlexander Leidinger * d->flags should be cleared by the allocator of the softc. 10427233ababSAlexander Leidinger * We cannot clear this field here because several devices set 10437233ababSAlexander Leidinger * this flag before calling pcm_register(). 10447233ababSAlexander Leidinger */ 1045cd9766c5SCameron Grant d->flags = 0; 10467233ababSAlexander Leidinger #endif 104790da2b28SAriff Abdullah i = 0; 104890da2b28SAriff Abdullah if (resource_int_value(device_get_name(dev), device_get_unit(dev), 104990da2b28SAriff Abdullah "vpc", &i) != 0 || i != 0) 105090da2b28SAriff Abdullah d->flags |= SD_F_VPC; 105190da2b28SAriff Abdullah 105290da2b28SAriff Abdullah if (resource_int_value(device_get_name(dev), device_get_unit(dev), 105390da2b28SAriff Abdullah "bitperfect", &i) == 0 && i != 0) 105490da2b28SAriff Abdullah d->flags |= SD_F_BITPERFECT; 105590da2b28SAriff Abdullah 1056987e5972SCameron Grant d->devinfo = devinfo; 1057f637a36cSCameron Grant d->devcount = 0; 1058506a5308SCameron Grant d->reccount = 0; 1059a67fe5c1SCameron Grant d->playcount = 0; 1060bba4862cSAriff Abdullah d->pvchancount = 0; 1061bba4862cSAriff Abdullah d->rvchancount = 0; 1062bba4862cSAriff Abdullah d->pvchanrate = 0; 1063bba4862cSAriff Abdullah d->pvchanformat = 0; 1064bba4862cSAriff Abdullah d->rvchanrate = 0; 1065bba4862cSAriff Abdullah d->rvchanformat = 0; 1066d95502a8SCameron Grant d->inprog = 0; 1067833f7023SCameron Grant 1068bba4862cSAriff Abdullah /* 1069bba4862cSAriff Abdullah * Create clone manager, disabled by default. Cloning will be 107090da2b28SAriff Abdullah * enabled during final stage of driver initialization through 1071bba4862cSAriff Abdullah * pcm_setstatus(). 1072bba4862cSAriff Abdullah */ 1073e4e61333SAriff Abdullah d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE, 1074e4e61333SAriff Abdullah SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK | 1075bba4862cSAriff Abdullah SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF | 1076bba4862cSAriff Abdullah SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED); 1077bba4862cSAriff Abdullah 1078bba4862cSAriff Abdullah CHN_INIT(d, channels.pcm); 1079bba4862cSAriff Abdullah CHN_INIT(d, channels.pcm.busy); 108090da2b28SAriff Abdullah CHN_INIT(d, channels.pcm.opened); 108145550658SPoul-Henning Kamp 1082e4e61333SAriff Abdullah /* XXX This is incorrect, but lets play along for now. */ 1083a1d444e1SAriff Abdullah if ((numplay == 0 || numrec == 0) && numplay != numrec) 1084285648f9SCameron Grant d->flags |= SD_F_SIMPLEX; 1085d95502a8SCameron Grant 1086bba4862cSAriff Abdullah sysctl_ctx_init(&d->play_sysctl_ctx); 1087bba4862cSAriff Abdullah d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx, 1088bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play", 1089bba4862cSAriff Abdullah CTLFLAG_RD, 0, "playback channels node"); 1090bba4862cSAriff Abdullah sysctl_ctx_init(&d->rec_sysctl_ctx); 1091bba4862cSAriff Abdullah d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx, 1092bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec", 1093bba4862cSAriff Abdullah CTLFLAG_RD, 0, "record channels node"); 1094851a904aSAlexander Leidinger /* XXX: an user should be able to set this with a control tool, the 1095851a904aSAlexander Leidinger sysadmin then needs min+max sysctls for this */ 10966dc7dc9aSMatthew D Fleming SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev), 1097a580b31aSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 1098a580b31aSAriff Abdullah OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size"); 109990da2b28SAriff Abdullah SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 110090da2b28SAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 110190da2b28SAriff Abdullah "bitperfect", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d), 110290da2b28SAriff Abdullah sysctl_dev_pcm_bitperfect, "I", 110390da2b28SAriff Abdullah "bit-perfect playback/recording (0=disable, 1=enable)"); 1104bba4862cSAriff Abdullah #ifdef SND_DEBUG 1105bba4862cSAriff Abdullah SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1106bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1107bba4862cSAriff Abdullah "clone_flags", CTLTYPE_UINT | CTLFLAG_RW, d, sizeof(d), 1108bba4862cSAriff Abdullah sysctl_dev_pcm_clone_flags, "IU", 1109bba4862cSAriff Abdullah "clone flags"); 1110bba4862cSAriff Abdullah SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1111bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1112bba4862cSAriff Abdullah "clone_deadline", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d), 1113bba4862cSAriff Abdullah sysctl_dev_pcm_clone_deadline, "I", 1114bba4862cSAriff Abdullah "clone expiration deadline (ms)"); 1115bba4862cSAriff Abdullah SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1116bba4862cSAriff Abdullah SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1117bba4862cSAriff Abdullah "clone_gc", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d), 1118bba4862cSAriff Abdullah sysctl_dev_pcm_clone_gc, "I", 1119bba4862cSAriff Abdullah "clone garbage collector"); 112082db23e2SCameron Grant #endif 1121e4e61333SAriff Abdullah 1122bba4862cSAriff Abdullah if (numplay > 0 || numrec > 0) { 1123cd9766c5SCameron Grant d->flags |= SD_F_AUTOVCHAN; 11243fdb3676SAriff Abdullah vchan_initsys(dev); 112597d69a96SAlexander Leidinger } 1126cd9766c5SCameron Grant 112790da2b28SAriff Abdullah if (d->flags & SD_F_EQ) 112890da2b28SAriff Abdullah feeder_eq_initsys(dev); 112990da2b28SAriff Abdullah 113067b1dce3SCameron Grant sndstat_register(dev, d->status, sndstat_prepare_pcm); 1131e4e61333SAriff Abdullah 1132987e5972SCameron Grant return 0; 1133987e5972SCameron Grant } 1134987e5972SCameron Grant 113533dbf14aSCameron Grant int 113633dbf14aSCameron Grant pcm_unregister(device_t dev) 11377c438dbeSCameron Grant { 1138e4e61333SAriff Abdullah struct snddev_info *d; 1139a67fe5c1SCameron Grant struct pcm_channel *ch; 1140bba4862cSAriff Abdullah struct thread *td; 11417c438dbeSCameron Grant 1142bba4862cSAriff Abdullah td = curthread; 1143e4e61333SAriff Abdullah d = device_get_softc(dev); 1144e4e61333SAriff Abdullah 1145e4e61333SAriff Abdullah if (!PCM_ALIVE(d)) { 1146e4e61333SAriff Abdullah device_printf(dev, "unregister: device not configured\n"); 1147e4e61333SAriff Abdullah return (0); 1148e4e61333SAriff Abdullah } 1149bba4862cSAriff Abdullah 1150bba4862cSAriff Abdullah if (sndstat_acquire(td) != 0) { 115128ef3fb0SAlexander Leidinger device_printf(dev, "unregister: sndstat busy\n"); 1152e4e61333SAriff Abdullah return (EBUSY); 115328ef3fb0SAlexander Leidinger } 115428ef3fb0SAlexander Leidinger 115590da2b28SAriff Abdullah PCM_LOCK(d); 1156e4e61333SAriff Abdullah PCM_WAIT(d); 1157e4e61333SAriff Abdullah 1158e4e61333SAriff Abdullah if (d->inprog != 0) { 11595c25132aSGeorge C A Reid device_printf(dev, "unregister: operation in progress\n"); 116090da2b28SAriff Abdullah PCM_UNLOCK(d); 1161bba4862cSAriff Abdullah sndstat_release(td); 1162e4e61333SAriff Abdullah return (EBUSY); 11635c25132aSGeorge C A Reid } 11645ee30e27SMathew Kanner 1165e4e61333SAriff Abdullah PCM_ACQUIRE(d); 116690da2b28SAriff Abdullah PCM_UNLOCK(d); 1167e4e61333SAriff Abdullah 1168e4e61333SAriff Abdullah CHN_FOREACH(ch, d, channels.pcm) { 1169e4e61333SAriff Abdullah CHN_LOCK(ch); 1170e4e61333SAriff Abdullah if (ch->refcount > 0) { 1171e4e61333SAriff Abdullah device_printf(dev, 1172e4e61333SAriff Abdullah "unregister: channel %s busy (pid %d)\n", 1173e4e61333SAriff Abdullah ch->name, ch->pid); 1174e4e61333SAriff Abdullah CHN_UNLOCK(ch); 1175e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 1176bba4862cSAriff Abdullah sndstat_release(td); 1177e4e61333SAriff Abdullah return (EBUSY); 1178285648f9SCameron Grant } 1179e4e61333SAriff Abdullah CHN_UNLOCK(ch); 1180c9b53085SCameron Grant } 11815ee30e27SMathew Kanner 1182bba4862cSAriff Abdullah if (d->clones != NULL) { 1183bba4862cSAriff Abdullah if (snd_clone_busy(d->clones) != 0) { 1184bba4862cSAriff Abdullah device_printf(dev, "unregister: clone busy\n"); 1185e4e61333SAriff Abdullah PCM_RELEASE_QUICK(d); 1186bba4862cSAriff Abdullah sndstat_release(td); 1187e4e61333SAriff Abdullah return (EBUSY); 1188e4e61333SAriff Abdullah } else { 118990da2b28SAriff Abdullah PCM_LOCK(d); 1190bba4862cSAriff Abdullah (void)snd_clone_disable(d->clones); 119190da2b28SAriff Abdullah PCM_UNLOCK(d); 1192e4e61333SAriff Abdullah } 1193bba4862cSAriff Abdullah } 1194bba4862cSAriff Abdullah 1195b4221868SAriff Abdullah if (mixer_uninit(dev) == EBUSY) { 11967233ababSAlexander Leidinger device_printf(dev, "unregister: mixer busy\n"); 119790da2b28SAriff Abdullah PCM_LOCK(d); 1198bba4862cSAriff Abdullah if (d->clones != NULL) 1199bba4862cSAriff Abdullah (void)snd_clone_enable(d->clones); 1200e4e61333SAriff Abdullah PCM_RELEASE(d); 120190da2b28SAriff Abdullah PCM_UNLOCK(d); 1202bba4862cSAriff Abdullah sndstat_release(td); 1203e4e61333SAriff Abdullah return (EBUSY); 12047233ababSAlexander Leidinger } 12057233ababSAlexander Leidinger 120690da2b28SAriff Abdullah PCM_LOCK(d); 1207e4e61333SAriff Abdullah d->flags |= SD_F_DYING; 1208e4e61333SAriff Abdullah d->flags &= ~SD_F_REGISTERED; 120990da2b28SAriff Abdullah PCM_UNLOCK(d); 1210e4e61333SAriff Abdullah 1211e4e61333SAriff Abdullah /* 1212e4e61333SAriff Abdullah * No lock being held, so this thing can be flushed without 1213e4e61333SAriff Abdullah * stucking into devdrn oblivion. 1214e4e61333SAriff Abdullah */ 1215bba4862cSAriff Abdullah if (d->clones != NULL) { 1216bba4862cSAriff Abdullah snd_clone_destroy(d->clones); 1217bba4862cSAriff Abdullah d->clones = NULL; 12187982e7a4SAriff Abdullah } 1219bba4862cSAriff Abdullah 1220bba4862cSAriff Abdullah if (d->play_sysctl_tree != NULL) { 1221bba4862cSAriff Abdullah sysctl_ctx_free(&d->play_sysctl_ctx); 1222bba4862cSAriff Abdullah d->play_sysctl_tree = NULL; 1223bba4862cSAriff Abdullah } 1224bba4862cSAriff Abdullah if (d->rec_sysctl_tree != NULL) { 1225bba4862cSAriff Abdullah sysctl_ctx_free(&d->rec_sysctl_ctx); 1226bba4862cSAriff Abdullah d->rec_sysctl_tree = NULL; 1227a1d444e1SAriff Abdullah } 1228bba4862cSAriff Abdullah 1229bba4862cSAriff Abdullah while (!CHN_EMPTY(d, channels.pcm)) 1230285648f9SCameron Grant pcm_killchan(dev); 12317c438dbeSCameron Grant 1232e4e61333SAriff Abdullah dsp_cdevinfo_flush(d); 1233e4e61333SAriff Abdullah 123490da2b28SAriff Abdullah PCM_LOCK(d); 1235e4e61333SAriff Abdullah PCM_RELEASE(d); 1236e4e61333SAriff Abdullah cv_destroy(&d->cv); 123790da2b28SAriff Abdullah PCM_UNLOCK(d); 123849c5e6e2SCameron Grant snd_mtxfree(d->lock); 123928ef3fb0SAlexander Leidinger sndstat_unregister(dev); 1240bba4862cSAriff Abdullah sndstat_release(td); 1241bba4862cSAriff Abdullah 1242bba4862cSAriff Abdullah if (snd_unit == device_get_unit(dev)) { 1243cbebc90dSAlexander Motin snd_unit = pcm_best_unit(-1); 1244cbebc90dSAlexander Motin if (snd_unit_auto == 0) 1245cbebc90dSAlexander Motin snd_unit_auto = 1; 1246e4e61333SAriff Abdullah } 1247bba4862cSAriff Abdullah 1248e4e61333SAriff Abdullah return (0); 124933dbf14aSCameron Grant } 12507c438dbeSCameron Grant 125167b1dce3SCameron Grant /************************************************************************/ 125267b1dce3SCameron Grant 1253b611c801SAlexander Leidinger /** 1254b611c801SAlexander Leidinger * @brief Handle OSSv4 SNDCTL_SYSINFO ioctl. 1255b611c801SAlexander Leidinger * 1256b611c801SAlexander Leidinger * @param si Pointer to oss_sysinfo struct where information about the 1257b611c801SAlexander Leidinger * sound subsystem will be written/copied. 1258b611c801SAlexander Leidinger * 1259b611c801SAlexander Leidinger * This routine returns information about the sound system, such as the 1260b611c801SAlexander Leidinger * current OSS version, number of audio, MIDI, and mixer drivers, etc. 1261b611c801SAlexander Leidinger * Also includes a bitmask showing which of the above types of devices 1262b611c801SAlexander Leidinger * are open (busy). 1263b611c801SAlexander Leidinger * 1264b611c801SAlexander Leidinger * @note 1265b611c801SAlexander Leidinger * Calling threads must not hold any snddev_info or pcm_channel locks. 1266b611c801SAlexander Leidinger * 1267b611c801SAlexander Leidinger * @author Ryan Beasley <ryanb@FreeBSD.org> 1268b611c801SAlexander Leidinger */ 1269b611c801SAlexander Leidinger void 1270b611c801SAlexander Leidinger sound_oss_sysinfo(oss_sysinfo *si) 1271b611c801SAlexander Leidinger { 1272b611c801SAlexander Leidinger static char si_product[] = "FreeBSD native OSS ABI"; 1273b611c801SAlexander Leidinger static char si_version[] = __XSTRING(__FreeBSD_version); 1274c412d503SAlexander Motin static char si_license[] = "BSD"; 1275b611c801SAlexander Leidinger static int intnbits = sizeof(int) * 8; /* Better suited as macro? 1276b611c801SAlexander Leidinger Must pester a C guru. */ 1277b611c801SAlexander Leidinger 1278b611c801SAlexander Leidinger struct snddev_info *d; 1279b611c801SAlexander Leidinger struct pcm_channel *c; 1280b611c801SAlexander Leidinger int i, j, ncards; 1281b611c801SAlexander Leidinger 1282b611c801SAlexander Leidinger ncards = 0; 1283b611c801SAlexander Leidinger 1284b611c801SAlexander Leidinger strlcpy(si->product, si_product, sizeof(si->product)); 1285b611c801SAlexander Leidinger strlcpy(si->version, si_version, sizeof(si->version)); 1286b611c801SAlexander Leidinger si->versionnum = SOUND_VERSION; 1287c412d503SAlexander Motin strlcpy(si->license, si_license, sizeof(si->license)); 1288b611c801SAlexander Leidinger 1289b611c801SAlexander Leidinger /* 1290b611c801SAlexander Leidinger * Iterate over PCM devices and their channels, gathering up data 1291b611c801SAlexander Leidinger * for the numaudios, ncards, and openedaudio fields. 1292b611c801SAlexander Leidinger */ 1293b611c801SAlexander Leidinger si->numaudios = 0; 1294b611c801SAlexander Leidinger bzero((void *)&si->openedaudio, sizeof(si->openedaudio)); 1295b611c801SAlexander Leidinger 1296b611c801SAlexander Leidinger j = 0; 1297b611c801SAlexander Leidinger 12989c271f79SAriff Abdullah for (i = 0; pcm_devclass != NULL && 12999c271f79SAriff Abdullah i < devclass_get_maxunit(pcm_devclass); i++) { 1300b611c801SAlexander Leidinger d = devclass_get_softc(pcm_devclass, i); 1301e4e61333SAriff Abdullah if (!PCM_REGISTERED(d)) 1302b611c801SAlexander Leidinger continue; 1303b611c801SAlexander Leidinger 1304e4e61333SAriff Abdullah /* XXX Need Giant magic entry ??? */ 1305e4e61333SAriff Abdullah 1306b611c801SAlexander Leidinger /* See note in function's docblock */ 130790da2b28SAriff Abdullah PCM_UNLOCKASSERT(d); 130890da2b28SAriff Abdullah PCM_LOCK(d); 1309b611c801SAlexander Leidinger 1310b611c801SAlexander Leidinger si->numaudios += d->devcount; 1311b611c801SAlexander Leidinger ++ncards; 1312b611c801SAlexander Leidinger 1313bba4862cSAriff Abdullah CHN_FOREACH(c, d, channels.pcm) { 131490da2b28SAriff Abdullah CHN_UNLOCKASSERT(c); 1315b611c801SAlexander Leidinger CHN_LOCK(c); 1316b611c801SAlexander Leidinger if (c->flags & CHN_F_BUSY) 1317b611c801SAlexander Leidinger si->openedaudio[j / intnbits] |= 1318b611c801SAlexander Leidinger (1 << (j % intnbits)); 1319b611c801SAlexander Leidinger CHN_UNLOCK(c); 1320b611c801SAlexander Leidinger j++; 1321b611c801SAlexander Leidinger } 1322b611c801SAlexander Leidinger 132390da2b28SAriff Abdullah PCM_UNLOCK(d); 1324b611c801SAlexander Leidinger } 1325c412d503SAlexander Motin si->numaudioengines = si->numaudios; 1326b611c801SAlexander Leidinger 1327b611c801SAlexander Leidinger si->numsynths = 0; /* OSSv4 docs: this field is obsolete */ 1328b611c801SAlexander Leidinger /** 1329b611c801SAlexander Leidinger * @todo Collect num{midis,timers}. 1330b611c801SAlexander Leidinger * 1331b611c801SAlexander Leidinger * Need access to sound/midi/midi.c::midistat_lock in order 1332b611c801SAlexander Leidinger * to safely touch midi_devices and get a head count of, well, 1333b611c801SAlexander Leidinger * MIDI devices. midistat_lock is a global static (i.e., local to 1334b611c801SAlexander Leidinger * midi.c), but midi_devices is a regular global; should the mutex 1335b611c801SAlexander Leidinger * be publicized, or is there another way to get this information? 1336b611c801SAlexander Leidinger * 1337b611c801SAlexander Leidinger * NB: MIDI/sequencer stuff is currently on hold. 1338b611c801SAlexander Leidinger */ 1339b611c801SAlexander Leidinger si->nummidis = 0; 1340b611c801SAlexander Leidinger si->numtimers = 0; 1341b611c801SAlexander Leidinger si->nummixers = mixer_count; 1342b611c801SAlexander Leidinger si->numcards = ncards; 1343b611c801SAlexander Leidinger /* OSSv4 docs: Intended only for test apps; API doesn't 1344b611c801SAlexander Leidinger really have much of a concept of cards. Shouldn't be 1345b611c801SAlexander Leidinger used by applications. */ 1346b611c801SAlexander Leidinger 1347b611c801SAlexander Leidinger /** 1348b611c801SAlexander Leidinger * @todo Fill in "busy devices" fields. 1349b611c801SAlexander Leidinger * 1350b611c801SAlexander Leidinger * si->openedmidi = " MIDI devices 1351b611c801SAlexander Leidinger */ 1352b611c801SAlexander Leidinger bzero((void *)&si->openedmidi, sizeof(si->openedmidi)); 1353b611c801SAlexander Leidinger 1354b611c801SAlexander Leidinger /* 1355b611c801SAlexander Leidinger * Si->filler is a reserved array, but according to docs each 1356b611c801SAlexander Leidinger * element should be set to -1. 1357b611c801SAlexander Leidinger */ 1358b611c801SAlexander Leidinger for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++) 1359b611c801SAlexander Leidinger si->filler[i] = -1; 1360b611c801SAlexander Leidinger } 1361b611c801SAlexander Leidinger 136252f6e09eSAlexander Motin int 136352f6e09eSAlexander Motin sound_oss_card_info(oss_card_info *si) 136452f6e09eSAlexander Motin { 136552f6e09eSAlexander Motin struct snddev_info *d; 136652f6e09eSAlexander Motin int i, ncards; 136752f6e09eSAlexander Motin 136852f6e09eSAlexander Motin ncards = 0; 136952f6e09eSAlexander Motin 137052f6e09eSAlexander Motin for (i = 0; pcm_devclass != NULL && 137152f6e09eSAlexander Motin i < devclass_get_maxunit(pcm_devclass); i++) { 137252f6e09eSAlexander Motin d = devclass_get_softc(pcm_devclass, i); 137352f6e09eSAlexander Motin if (!PCM_REGISTERED(d)) 137452f6e09eSAlexander Motin continue; 137552f6e09eSAlexander Motin 137652f6e09eSAlexander Motin if (ncards++ != si->card) 137752f6e09eSAlexander Motin continue; 137852f6e09eSAlexander Motin 137990da2b28SAriff Abdullah PCM_UNLOCKASSERT(d); 138090da2b28SAriff Abdullah PCM_LOCK(d); 138152f6e09eSAlexander Motin 138252f6e09eSAlexander Motin strlcpy(si->shortname, device_get_nameunit(d->dev), 138352f6e09eSAlexander Motin sizeof(si->shortname)); 138452f6e09eSAlexander Motin strlcpy(si->longname, device_get_desc(d->dev), 138552f6e09eSAlexander Motin sizeof(si->longname)); 138652f6e09eSAlexander Motin strlcpy(si->hw_info, d->status, sizeof(si->hw_info)); 138752f6e09eSAlexander Motin si->intr_count = si->ack_count = 0; 138890da2b28SAriff Abdullah 138990da2b28SAriff Abdullah PCM_UNLOCK(d); 139090da2b28SAriff Abdullah 139152f6e09eSAlexander Motin return (0); 139252f6e09eSAlexander Motin } 139352f6e09eSAlexander Motin return (ENXIO); 139452f6e09eSAlexander Motin } 139552f6e09eSAlexander Motin 1396b611c801SAlexander Leidinger /************************************************************************/ 1397b611c801SAlexander Leidinger 13980739ea1dSSeigo Tanimura static int 13990739ea1dSSeigo Tanimura sound_modevent(module_t mod, int type, void *data) 14000739ea1dSSeigo Tanimura { 1401b611c801SAlexander Leidinger int ret; 14027233ababSAlexander Leidinger #if 0 14030739ea1dSSeigo Tanimura return (midi_modevent(mod, type, data)); 14047233ababSAlexander Leidinger #else 1405b611c801SAlexander Leidinger ret = 0; 1406b611c801SAlexander Leidinger 1407b611c801SAlexander Leidinger switch(type) { 1408b611c801SAlexander Leidinger case MOD_LOAD: 1409b611c801SAlexander Leidinger pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL); 1410b611c801SAlexander Leidinger break; 1411b611c801SAlexander Leidinger case MOD_UNLOAD: 1412bba4862cSAriff Abdullah ret = sndstat_acquire(curthread); 1413bba4862cSAriff Abdullah if (ret != 0) 1414bba4862cSAriff Abdullah break; 1415b611c801SAlexander Leidinger if (pcmsg_unrhdr != NULL) { 1416b611c801SAlexander Leidinger delete_unrhdr(pcmsg_unrhdr); 1417b611c801SAlexander Leidinger pcmsg_unrhdr = NULL; 1418b611c801SAlexander Leidinger } 1419b611c801SAlexander Leidinger break; 14205525b53dSAlexander Motin case MOD_SHUTDOWN: 14215525b53dSAlexander Motin break; 1422b611c801SAlexander Leidinger default: 142390da2b28SAriff Abdullah ret = ENOTSUP; 1424b611c801SAlexander Leidinger } 1425b611c801SAlexander Leidinger 1426b611c801SAlexander Leidinger return ret; 14277233ababSAlexander Leidinger #endif 14280739ea1dSSeigo Tanimura } 14290739ea1dSSeigo Tanimura 14300739ea1dSSeigo Tanimura DEV_MODULE(sound, sound_modevent, NULL); 14310739ea1dSSeigo Tanimura MODULE_VERSION(sound, SOUND_MODVER); 1432