2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <dev/sound/pcm/sound.h>
29 #include <dev/sound/pcm/ac97.h>
30 #include <dev/sound/pcm/vchan.h>
31 #include <dev/sound/pcm/dsp.h>
32 #include <dev/sound/version.h>
33 #include <sys/limits.h>
34 #include <sys/sysctl.h>
36 #include "feeder_if.h"
38 SND_DECLARE_FILE("$FreeBSD$");
40 devclass_t pcm_devclass;
42 int pcm_veto_load = 1;
46 TUNABLE_INT("hw.snd.default_unit", &snd_unit);
49 static int snd_unit_auto = 0;
50 TUNABLE_INT("hw.snd.default_auto", &snd_unit_auto);
51 SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RW,
52 &snd_unit_auto, 0, "assign default unit to a newly attached device");
54 int snd_maxautovchans = 16;
55 /* XXX: a tunable implies that we may need more than one sound channel before
56 the system can change a sysctl (/etc/sysctl.conf), do we really need
58 TUNABLE_INT("hw.snd.maxautovchans", &snd_maxautovchans);
60 SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD, 0, "Sound driver");
63 * XXX I've had enough with people not telling proper version/arch
64 * while reporting problems, not after 387397913213th questions/requests.
66 static const char snd_driver_version[] =
67 __XSTRING(SND_DRV_VERSION)"/"MACHINE_ARCH;
68 SYSCTL_STRING(_hw_snd, OID_AUTO, version, CTLFLAG_RD, &snd_driver_version,
69 0, "Driver version/arch");
72 * @brief Unit number allocator for syncgroup IDs
74 struct unrhdr *pcmsg_unrhdr = NULL;
76 static int sndstat_prepare_pcm(struct sbuf *s, device_t dev, int verbose);
79 snd_mtxcreate(const char *desc, const char *type)
84 m = malloc(sizeof(*m), M_DEVBUF, M_WAITOK | M_ZERO);
85 mtx_init(m, desc, type, MTX_DEF);
88 return (void *)0xcafebabe;
98 /* mtx_assert(mtx, MA_OWNED); */
105 snd_mtxassert(void *m)
111 mtx_assert(mtx, MA_OWNED);
127 snd_mtxunlock(void *m)
137 snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
139 struct snddev_info *d;
141 flags &= INTR_MPSAFE;
142 flags |= INTR_TYPE_AV;
144 flags = INTR_TYPE_AV;
146 d = device_get_softc(dev);
147 if (d != NULL && (flags & INTR_MPSAFE))
148 d->flags |= SD_F_MPSAFE;
150 return bus_setup_intr(dev, res, flags,
151 #if __FreeBSD_version >= 700031
154 hand, param, cookiep);
157 #ifndef PCM_DEBUG_MTX
159 pcm_lock(struct snddev_info *d)
161 snd_mtxlock(d->lock);
165 pcm_unlock(struct snddev_info *d)
167 snd_mtxunlock(d->lock);
172 pcm_getfakechan(struct snddev_info *d)
178 pcm_clonereset(struct snddev_info *d)
184 cmax = d->playcount + d->reccount - 1;
185 if (d->pvchancount > 0)
186 cmax += MAX(d->pvchancount, snd_maxautovchans) - 1;
187 if (d->rvchancount > 0)
188 cmax += MAX(d->rvchancount, snd_maxautovchans) - 1;
189 if (cmax > PCMMAXCLONE)
191 (void)snd_clone_gc(d->clones);
192 (void)snd_clone_setmaxunit(d->clones, cmax);
196 pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num)
198 struct pcm_channel *c, *ch, *nch;
203 if ((direction == PCMDIR_PLAY && d->playcount < 1) ||
204 (direction == PCMDIR_REC && d->reccount < 1))
207 if (!(d->flags & SD_F_AUTOVCHAN))
210 if (newcnt < 0 || newcnt > SND_MAXVCHANS)
213 if (direction == PCMDIR_PLAY)
214 vcnt = d->pvchancount;
215 else if (direction == PCMDIR_REC)
216 vcnt = d->rvchancount;
222 (num >= 0 && num < SND_MAXVCHANS && (newcnt - 1) == vcnt),
223 ("bogus vchan_create() request num=%d newcnt=%d vcnt=%d",
225 /* add new vchans - find a parent channel first */
227 CHN_FOREACH(c, d, channels.pcm) {
229 if (c->direction == direction &&
230 ((c->flags & CHN_F_HAS_VCHAN) || (vcnt == 0 &&
231 !(c->flags & (CHN_F_BUSY | CHN_F_VIRTUAL))))) {
239 ch->flags |= CHN_F_BUSY;
241 while (err == 0 && newcnt > vcnt) {
242 err = vchan_create(ch, num);
245 else if (err == E2BIG && newcnt > vcnt)
246 device_printf(d->dev,
247 "%s: err=%d Maximum channel reached.\n",
251 ch->flags &= ~CHN_F_BUSY;
257 } else if (newcnt < vcnt) {
259 ("bogus vchan_destroy() request num=%d", num));
260 CHN_FOREACH(c, d, channels.pcm) {
262 if (c->direction != direction ||
263 CHN_EMPTY(c, children) ||
264 !(c->flags & CHN_F_HAS_VCHAN)) {
268 CHN_FOREACH_SAFE(ch, c, nch, children) {
270 if (!(ch->flags & CHN_F_BUSY)) {
273 err = vchan_destroy(ch);
291 /* return error status and a locked channel */
293 pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
294 pid_t pid, int devunit)
296 struct pcm_channel *c;
299 KASSERT(d != NULL && ch != NULL && (devunit == -1 ||
300 !(devunit & ~(SND_U_MASK | SND_D_MASK | SND_C_MASK))) &&
301 (direction == PCMDIR_PLAY || direction == PCMDIR_REC),
302 ("%s(): invalid d=%p ch=%p direction=%d pid=%d devunit=%d",
303 __func__, d, ch, direction, pid, devunit));
306 /* Double check again. */
308 switch (snd_unit2d(devunit)) {
309 case SND_DEV_DSPHW_PLAY:
310 case SND_DEV_DSPHW_VPLAY:
311 if (direction != PCMDIR_PLAY)
314 case SND_DEV_DSPHW_REC:
315 case SND_DEV_DSPHW_VREC:
316 if (direction != PCMDIR_REC)
320 if (!(direction == PCMDIR_PLAY ||
321 direction == PCMDIR_REC))
329 /* scan for a free channel */
330 CHN_FOREACH(c, d, channels.pcm) {
332 if (c->direction == direction && !(c->flags & CHN_F_BUSY) &&
333 (devunit == -1 || devunit == -2 || c->unit == devunit)) {
334 c->flags |= CHN_F_BUSY;
338 } else if (c->unit == devunit) {
339 if (c->direction != direction)
341 else if (c->flags & CHN_F_BUSY)
347 } else if ((devunit == -1 || devunit == -2) &&
348 c->direction == direction && (c->flags & CHN_F_BUSY))
356 /* no channel available */
357 if (devunit == -1 || snd_unit2d(devunit) == SND_DEV_DSPHW_VPLAY ||
358 snd_unit2d(devunit) == SND_DEV_DSPHW_VREC) {
359 if (direction == PCMDIR_PLAY)
360 vchancount = d->pvchancount;
362 vchancount = d->rvchancount;
363 if (!(vchancount > 0 && vchancount < snd_maxautovchans) &&
364 (devunit == -1 || snd_unit2c(devunit) < snd_maxautovchans))
366 err = pcm_setvchans(d, direction, vchancount + 1,
367 (devunit == -1) ? -1 : snd_unit2c(devunit));
378 /* release a locked channel and unlock it */
380 pcm_chnrelease(struct pcm_channel *c)
382 PCM_BUSYASSERT(c->parentsnddev);
385 c->flags &= ~CHN_F_BUSY;
393 pcm_chnref(struct pcm_channel *c, int ref)
395 PCM_BUSYASSERT(c->parentsnddev);
400 return (c->refcount);
404 pcm_inprog(struct snddev_info *d, int delta)
406 snd_mtxassert(d->lock);
414 pcm_setmaxautovchans(struct snddev_info *d, int num)
421 if (num >= 0 && d->pvchancount > num)
422 (void)pcm_setvchans(d, PCMDIR_PLAY, num, -1);
423 else if (num > 0 && d->pvchancount == 0)
424 (void)pcm_setvchans(d, PCMDIR_PLAY, 1, -1);
426 if (num >= 0 && d->rvchancount > num)
427 (void)pcm_setvchans(d, PCMDIR_REC, num, -1);
428 else if (num > 0 && d->rvchancount == 0)
429 (void)pcm_setvchans(d, PCMDIR_REC, 1, -1);
436 sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS)
438 struct snddev_info *d;
442 error = sysctl_handle_int(oidp, &unit, 0, req);
443 if (error == 0 && req->newptr != NULL) {
444 d = devclass_get_softc(pcm_devclass, unit);
445 if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm))
451 /* XXX: do we need a way to let the user change the default unit? */
452 SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit, CTLTYPE_INT | CTLFLAG_RW,
453 0, sizeof(int), sysctl_hw_snd_default_unit, "I", "default sound device");
457 sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS)
459 struct snddev_info *d;
462 v = snd_maxautovchans;
463 error = sysctl_handle_int(oidp, &v, 0, req);
464 if (error == 0 && req->newptr != NULL) {
467 if (v > SND_MAXVCHANS)
469 snd_maxautovchans = v;
470 for (i = 0; pcm_devclass != NULL &&
471 i < devclass_get_maxunit(pcm_devclass); i++) {
472 d = devclass_get_softc(pcm_devclass, i);
473 if (!PCM_REGISTERED(d))
475 PCM_ACQUIRE_QUICK(d);
476 pcm_setmaxautovchans(d, v);
477 PCM_RELEASE_QUICK(d);
482 SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans, CTLTYPE_INT | CTLFLAG_RW,
483 0, sizeof(int), sysctl_hw_snd_maxautovchans, "I", "maximum virtual channel");
486 pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo)
488 struct pcm_channel *ch;
489 int direction, err, rpnum, *pnum, max;
490 int udc, device, chan;
491 char *dirs, *devname, buf[CHN_NAMELEN];
494 snd_mtxassert(d->lock);
495 KASSERT(num >= -1, ("invalid num=%d", num));
501 direction = PCMDIR_PLAY;
502 pnum = &d->playcount;
503 device = SND_DEV_DSPHW_PLAY;
506 case PCMDIR_PLAY_VIRTUAL:
508 direction = PCMDIR_PLAY;
509 pnum = &d->pvchancount;
510 device = SND_DEV_DSPHW_VPLAY;
515 direction = PCMDIR_REC;
517 device = SND_DEV_DSPHW_REC;
520 case PCMDIR_REC_VIRTUAL:
522 direction = PCMDIR_REC;
523 pnum = &d->rvchancount;
524 device = SND_DEV_DSPHW_VREC;
531 chan = (num == -1) ? 0 : num;
533 if (*pnum >= max || chan >= max)
538 CHN_FOREACH(ch, d, channels.pcm) {
539 if (CHN_DEV(ch) != device)
541 if (chan == CHN_CHAN(ch)) {
543 device_printf(d->dev,
544 "channel num=%d allocated!\n", chan);
549 device_printf(d->dev,
550 "chan=%d > %d\n", chan, max);
557 if (*pnum != rpnum) {
558 device_printf(d->dev,
559 "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n",
560 __func__, dirs, *pnum, rpnum);
564 udc = snd_mkunit(device_get_unit(d->dev), device, chan);
565 devname = dsp_unit2name(buf, sizeof(buf), udc);
567 if (devname == NULL) {
568 device_printf(d->dev,
569 "Failed to query device name udc=0x%08x\n", udc);
574 ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
575 ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
578 ch->parentsnddev = d;
579 ch->parentchannel = parent;
581 ch->trigger = PCMTRIG_STOP;
582 snprintf(ch->name, sizeof(ch->name), "%s:%s:%s",
583 device_get_nameunit(ch->dev), dirs, devname);
585 err = chn_init(ch, devinfo, dir, direction);
588 device_printf(d->dev, "chn_init(%s) failed: err = %d\n",
590 kobj_delete(ch->methods, M_DEVBUF);
599 pcm_chn_destroy(struct pcm_channel *ch)
601 struct snddev_info *d;
604 d = ch->parentsnddev;
609 device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n",
614 kobj_delete(ch->methods, M_DEVBUF);
621 pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch)
623 struct pcm_channel *tmp, *after;
627 snd_mtxassert(d->lock);
628 KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY ||
629 ch->direction == PCMDIR_REC), ("Invalid pcm channel"));
636 * Look for possible device collision.
638 CHN_FOREACH(tmp, d, channels.pcm) {
639 if (tmp->unit == ch->unit) {
640 device_printf(d->dev, "%s(): Device collision "
641 "old=%p new=%p devunit=0x%08x\n",
642 __func__, tmp, ch, ch->unit);
645 if (CHN_DEV(tmp) < CHN_DEV(ch)) {
649 } else if (CHN_DEV(tmp) > CHN_DEV(ch))
652 if (CHN_CHAN(tmp) < CHN_CHAN(ch))
654 else if (CHN_CHAN(tmp) > CHN_CHAN(ch))
659 CHN_INSERT_AFTER(after, ch, channels.pcm);
661 CHN_INSERT_HEAD(d, ch, channels.pcm);
664 switch (CHN_DEV(ch)) {
665 case SND_DEV_DSPHW_PLAY:
668 case SND_DEV_DSPHW_VPLAY:
671 case SND_DEV_DSPHW_REC:
674 case SND_DEV_DSPHW_VREC:
687 pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch)
689 struct pcm_channel *tmp;
692 snd_mtxassert(d->lock);
696 CHN_FOREACH(tmp, d, channels.pcm) {
704 CHN_REMOVE(d, ch, channels.pcm);
706 switch (CHN_DEV(ch)) {
707 case SND_DEV_DSPHW_PLAY:
710 case SND_DEV_DSPHW_VPLAY:
713 case SND_DEV_DSPHW_REC:
716 case SND_DEV_DSPHW_VREC:
729 pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
731 struct snddev_info *d = device_get_softc(dev);
732 struct pcm_channel *ch;
738 ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo);
740 device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n",
741 cls->name, dir, devinfo);
746 err = pcm_chn_add(d, ch);
749 device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n",
758 pcm_killchan(device_t dev)
760 struct snddev_info *d = device_get_softc(dev);
761 struct pcm_channel *ch;
766 ch = CHN_FIRST(d, channels.pcm);
769 error = pcm_chn_remove(d, ch);
773 return (pcm_chn_destroy(ch));
777 pcm_setstatus(device_t dev, char *str)
779 struct snddev_info *d = device_get_softc(dev);
783 if (d->playcount == 0 || d->reccount == 0)
784 d->flags |= SD_F_SIMPLEX;
786 if ((d->playcount > 0 || d->reccount > 0) &&
787 !(d->flags & SD_F_AUTOVCHAN)) {
788 d->flags |= SD_F_AUTOVCHAN;
792 pcm_setmaxautovchans(d, snd_maxautovchans);
794 strlcpy(d->status, str, SND_STATUSLEN);
798 /* Last stage, enable cloning. */
799 if (d->clones != NULL)
800 (void)snd_clone_enable(d->clones);
802 /* Done, we're ready.. */
803 d->flags |= SD_F_REGISTERED;
809 if (snd_unit < 0 || snd_unit_auto != 0)
810 snd_unit = device_get_unit(dev);
816 pcm_getflags(device_t dev)
818 struct snddev_info *d = device_get_softc(dev);
824 pcm_setflags(device_t dev, uint32_t val)
826 struct snddev_info *d = device_get_softc(dev);
832 pcm_getdevinfo(device_t dev)
834 struct snddev_info *d = device_get_softc(dev);
840 pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
842 struct snddev_info *d = device_get_softc(dev);
846 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
848 RANGE(sz, minbufsz, maxbufsz);
850 device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
857 device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
869 #if defined(SND_DYNSYSCTL) && defined(SND_DEBUG)
871 sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS)
873 struct snddev_info *d;
878 if (!PCM_REGISTERED(d) || d->clones == NULL)
881 PCM_ACQUIRE_QUICK(d);
883 flags = snd_clone_getflags(d->clones);
884 err = sysctl_handle_int(oidp, &flags, 0, req);
886 if (err == 0 && req->newptr != NULL) {
887 if (flags & ~SND_CLONE_MASK)
890 (void)snd_clone_setflags(d->clones, flags);
893 PCM_RELEASE_QUICK(d);
899 sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS)
901 struct snddev_info *d;
905 if (!PCM_REGISTERED(d) || d->clones == NULL)
908 PCM_ACQUIRE_QUICK(d);
910 deadline = snd_clone_getdeadline(d->clones);
911 err = sysctl_handle_int(oidp, &deadline, 0, req);
913 if (err == 0 && req->newptr != NULL) {
917 (void)snd_clone_setdeadline(d->clones, deadline);
920 PCM_RELEASE_QUICK(d);
926 sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS)
928 struct snddev_info *d;
932 if (!PCM_REGISTERED(d) || d->clones == NULL)
936 err = sysctl_handle_int(oidp, &val, 0, req);
938 if (err == 0 && req->newptr != NULL && val != 0) {
939 PCM_ACQUIRE_QUICK(d);
940 val = snd_clone_gc(d->clones);
941 PCM_RELEASE_QUICK(d);
942 if (bootverbose != 0 || snd_verbose > 3)
943 device_printf(d->dev, "clone gc: pruned=%d\n", val);
950 sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS)
952 struct snddev_info *d;
956 err = sysctl_handle_int(oidp, &val, 0, req);
958 if (err == 0 && req->newptr != NULL && val != 0) {
959 for (i = 0; pcm_devclass != NULL &&
960 i < devclass_get_maxunit(pcm_devclass); i++) {
961 d = devclass_get_softc(pcm_devclass, i);
962 if (!PCM_REGISTERED(d) || d->clones == NULL)
964 PCM_ACQUIRE_QUICK(d);
965 val = snd_clone_gc(d->clones);
966 PCM_RELEASE_QUICK(d);
967 if (bootverbose != 0 || snd_verbose > 3)
968 device_printf(d->dev, "clone gc: pruned=%d\n",
975 SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc, CTLTYPE_INT | CTLFLAG_RW,
976 0, sizeof(int), sysctl_hw_snd_clone_gc, "I",
977 "global clone garbage collector");
981 pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
983 struct snddev_info *d;
986 device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load);
991 if (device_get_unit(dev) > PCMMAXUNIT) {
992 device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n",
993 device_get_unit(dev), PCMMAXUNIT);
995 "Use 'hw.snd.maxunit' tunable to raise the limit.\n");
999 d = device_get_softc(dev);
1001 d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
1002 cv_init(&d->cv, device_get_nameunit(dev));
1003 PCM_ACQUIRE_QUICK(d);
1004 dsp_cdevinfo_init(d);
1007 * d->flags should be cleared by the allocator of the softc.
1008 * We cannot clear this field here because several devices set
1009 * this flag before calling pcm_register().
1013 d->devinfo = devinfo;
1020 d->pvchanformat = 0;
1022 d->rvchanformat = 0;
1026 * Create clone manager, disabled by default. Cloning will be
1027 * enabled during final stage of driver iniialization through
1030 d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE,
1031 SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK |
1032 SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF |
1033 SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED);
1035 if (bootverbose != 0 || snd_verbose > 3) {
1037 "clone manager: deadline=%dms flags=0x%08x\n",
1038 snd_clone_getdeadline(d->clones),
1039 snd_clone_getflags(d->clones));
1042 CHN_INIT(d, channels.pcm);
1043 CHN_INIT(d, channels.pcm.busy);
1045 /* XXX This is incorrect, but lets play along for now. */
1046 if ((numplay == 0 || numrec == 0) && numplay != numrec)
1047 d->flags |= SD_F_SIMPLEX;
1049 d->fakechan = fkchan_setup(dev);
1050 chn_init(d->fakechan, NULL, 0, 0);
1052 #ifdef SND_DYNSYSCTL
1053 sysctl_ctx_init(&d->play_sysctl_ctx);
1054 d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
1055 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
1056 CTLFLAG_RD, 0, "playback channels node");
1057 sysctl_ctx_init(&d->rec_sysctl_ctx);
1058 d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
1059 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
1060 CTLFLAG_RD, 0, "record channels node");
1061 /* XXX: an user should be able to set this with a control tool, the
1062 sysadmin then needs min+max sysctls for this */
1063 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
1064 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1065 OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size");
1067 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1068 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1069 "clone_flags", CTLTYPE_UINT | CTLFLAG_RW, d, sizeof(d),
1070 sysctl_dev_pcm_clone_flags, "IU",
1072 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1073 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1074 "clone_deadline", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1075 sysctl_dev_pcm_clone_deadline, "I",
1076 "clone expiration deadline (ms)");
1077 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1078 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1079 "clone_gc", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1080 sysctl_dev_pcm_clone_gc, "I",
1081 "clone garbage collector");
1085 if (numplay > 0 || numrec > 0) {
1086 d->flags |= SD_F_AUTOVCHAN;
1090 sndstat_register(dev, d->status, sndstat_prepare_pcm);
1096 pcm_unregister(device_t dev)
1098 struct snddev_info *d;
1099 struct pcm_channel *ch;
1104 d = device_get_softc(dev);
1106 if (!PCM_ALIVE(d)) {
1107 device_printf(dev, "unregister: device not configured\n");
1111 if (sndstat_acquire(td) != 0) {
1112 device_printf(dev, "unregister: sndstat busy\n");
1119 if (d->inprog != 0) {
1120 device_printf(dev, "unregister: operation in progress\n");
1122 sndstat_release(td);
1129 CHN_FOREACH(ch, d, channels.pcm) {
1131 if (ch->refcount > 0) {
1133 "unregister: channel %s busy (pid %d)\n",
1136 PCM_RELEASE_QUICK(d);
1137 sndstat_release(td);
1143 if (d->clones != NULL) {
1144 if (snd_clone_busy(d->clones) != 0) {
1145 device_printf(dev, "unregister: clone busy\n");
1146 PCM_RELEASE_QUICK(d);
1147 sndstat_release(td);
1151 (void)snd_clone_disable(d->clones);
1156 if (mixer_uninit(dev) == EBUSY) {
1157 device_printf(dev, "unregister: mixer busy\n");
1159 if (d->clones != NULL)
1160 (void)snd_clone_enable(d->clones);
1163 sndstat_release(td);
1168 d->flags |= SD_F_DYING;
1169 d->flags &= ~SD_F_REGISTERED;
1173 * No lock being held, so this thing can be flushed without
1174 * stucking into devdrn oblivion.
1176 if (d->clones != NULL) {
1177 snd_clone_destroy(d->clones);
1181 #ifdef SND_DYNSYSCTL
1182 if (d->play_sysctl_tree != NULL) {
1183 sysctl_ctx_free(&d->play_sysctl_ctx);
1184 d->play_sysctl_tree = NULL;
1186 if (d->rec_sysctl_tree != NULL) {
1187 sysctl_ctx_free(&d->rec_sysctl_ctx);
1188 d->rec_sysctl_tree = NULL;
1192 while (!CHN_EMPTY(d, channels.pcm))
1195 chn_kill(d->fakechan);
1196 fkchan_kill(d->fakechan);
1198 dsp_cdevinfo_flush(d);
1204 snd_mtxfree(d->lock);
1205 sndstat_unregister(dev);
1206 sndstat_release(td);
1208 if (snd_unit == device_get_unit(dev)) {
1210 * Reassign default unit to the next available dev, but
1211 * first, reset snd_unit to something ridiculous.
1214 for (i = 0; pcm_devclass != NULL &&
1215 i < devclass_get_maxunit(pcm_devclass); i++) {
1216 if (device_get_unit(dev) == i)
1218 d = devclass_get_softc(pcm_devclass, i);
1219 if (PCM_REGISTERED(d)) {
1229 /************************************************************************/
1232 sndstat_prepare_pcm(struct sbuf *s, device_t dev, int verbose)
1234 struct snddev_info *d;
1235 struct pcm_channel *c;
1236 struct pcm_feeder *f;
1241 d = device_get_softc(dev);
1247 if (CHN_EMPTY(d, channels.pcm)) {
1248 sbuf_printf(s, " (mixer only)");
1252 sbuf_printf(s, " (%dp:%dv/%dr:%dv channels%s%s)",
1253 d->playcount, d->pvchancount,
1254 d->reccount, d->rvchancount,
1255 (d->flags & SD_F_SIMPLEX)? "" : " duplex",
1257 (device_get_unit(dev) == snd_unit)? " default" : ""
1266 CHN_FOREACH(c, d, channels.pcm) {
1268 KASSERT(c->bufhard != NULL && c->bufsoft != NULL,
1269 ("hosed pcm channel setup"));
1271 sbuf_printf(s, "\n\t");
1273 /* it would be better to indent child channels */
1274 sbuf_printf(s, "%s[%s]: ", c->parentchannel? c->parentchannel->name : "", c->name);
1275 sbuf_printf(s, "spd %d", c->speed);
1276 if (c->speed != sndbuf_getspd(c->bufhard))
1277 sbuf_printf(s, "/%d", sndbuf_getspd(c->bufhard));
1278 sbuf_printf(s, ", fmt 0x%08x", c->format);
1279 if (c->format != sndbuf_getfmt(c->bufhard))
1280 sbuf_printf(s, "/0x%08x", sndbuf_getfmt(c->bufhard));
1281 sbuf_printf(s, ", flags 0x%08x, 0x%08x", c->flags, c->feederflags);
1283 sbuf_printf(s, ", pid %d", c->pid);
1284 sbuf_printf(s, "\n\t");
1286 sbuf_printf(s, "interrupts %d, ", c->interrupts);
1287 if (c->direction == PCMDIR_REC)
1288 sbuf_printf(s, "overruns %d, feed %u, hfree %d, sfree %d [b:%d/%d/%d|bs:%d/%d/%d]",
1289 c->xruns, c->feedcount, sndbuf_getfree(c->bufhard), sndbuf_getfree(c->bufsoft),
1290 sndbuf_getsize(c->bufhard), sndbuf_getblksz(c->bufhard),
1291 sndbuf_getblkcnt(c->bufhard),
1292 sndbuf_getsize(c->bufsoft), sndbuf_getblksz(c->bufsoft),
1293 sndbuf_getblkcnt(c->bufsoft));
1295 sbuf_printf(s, "underruns %d, feed %u, ready %d [b:%d/%d/%d|bs:%d/%d/%d]",
1296 c->xruns, c->feedcount, sndbuf_getready(c->bufsoft),
1297 sndbuf_getsize(c->bufhard), sndbuf_getblksz(c->bufhard),
1298 sndbuf_getblkcnt(c->bufhard),
1299 sndbuf_getsize(c->bufsoft), sndbuf_getblksz(c->bufsoft),
1300 sndbuf_getblkcnt(c->bufsoft));
1301 sbuf_printf(s, "\n\t");
1303 sbuf_printf(s, "{%s}", (c->direction == PCMDIR_REC)? "hardware" : "userland");
1304 sbuf_printf(s, " -> ");
1306 while (f->source != NULL)
1309 sbuf_printf(s, "%s", f->class->name);
1310 if (f->desc->type == FEEDER_FMT)
1311 sbuf_printf(s, "(0x%08x -> 0x%08x)", f->desc->in, f->desc->out);
1312 if (f->desc->type == FEEDER_RATE)
1313 sbuf_printf(s, "(%d -> %d)", FEEDER_GET(f, FEEDRATE_SRC), FEEDER_GET(f, FEEDRATE_DST));
1314 if (f->desc->type == FEEDER_ROOT || f->desc->type == FEEDER_MIXER ||
1315 f->desc->type == FEEDER_VOLUME)
1316 sbuf_printf(s, "(0x%08x)", f->desc->out);
1317 sbuf_printf(s, " -> ");
1320 sbuf_printf(s, "{%s}", (c->direction == PCMDIR_REC)? "userland" : "hardware");
1326 /************************************************************************/
1328 #ifdef SND_DYNSYSCTL
1330 sysctl_hw_snd_vchans(SYSCTL_HANDLER_ARGS)
1332 struct snddev_info *d;
1333 int direction, vchancount;
1336 d = devclass_get_softc(pcm_devclass, VCHAN_SYSCTL_UNIT(oidp->oid_arg1));
1337 if (!PCM_REGISTERED(d) || !(d->flags & SD_F_AUTOVCHAN))
1343 switch (VCHAN_SYSCTL_DIR(oidp->oid_arg1)) {
1345 direction = PCMDIR_PLAY;
1346 vchancount = d->pvchancount;
1350 direction = PCMDIR_REC;
1351 vchancount = d->rvchancount;
1369 err = sysctl_handle_int(oidp, &cnt, 0, req);
1371 if (err == 0 && req->newptr != NULL && vchancount != cnt) {
1374 if (cnt > SND_MAXVCHANS)
1375 cnt = SND_MAXVCHANS;
1376 err = pcm_setvchans(d, direction, cnt, -1);
1379 PCM_RELEASE_QUICK(d);
1385 /************************************************************************/
1388 * @brief Handle OSSv4 SNDCTL_SYSINFO ioctl.
1390 * @param si Pointer to oss_sysinfo struct where information about the
1391 * sound subsystem will be written/copied.
1393 * This routine returns information about the sound system, such as the
1394 * current OSS version, number of audio, MIDI, and mixer drivers, etc.
1395 * Also includes a bitmask showing which of the above types of devices
1399 * Calling threads must not hold any snddev_info or pcm_channel locks.
1401 * @author Ryan Beasley <ryanb@FreeBSD.org>
1404 sound_oss_sysinfo(oss_sysinfo *si)
1406 static char si_product[] = "FreeBSD native OSS ABI";
1407 static char si_version[] = __XSTRING(__FreeBSD_version);
1408 static int intnbits = sizeof(int) * 8; /* Better suited as macro?
1409 Must pester a C guru. */
1411 struct snddev_info *d;
1412 struct pcm_channel *c;
1417 strlcpy(si->product, si_product, sizeof(si->product));
1418 strlcpy(si->version, si_version, sizeof(si->version));
1419 si->versionnum = SOUND_VERSION;
1422 * Iterate over PCM devices and their channels, gathering up data
1423 * for the numaudios, ncards, and openedaudio fields.
1426 bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
1430 for (i = 0; pcm_devclass != NULL &&
1431 i < devclass_get_maxunit(pcm_devclass); i++) {
1432 d = devclass_get_softc(pcm_devclass, i);
1433 if (!PCM_REGISTERED(d))
1436 /* XXX Need Giant magic entry ??? */
1438 /* See note in function's docblock */
1439 mtx_assert(d->lock, MA_NOTOWNED);
1442 si->numaudios += d->devcount;
1445 CHN_FOREACH(c, d, channels.pcm) {
1446 mtx_assert(c->lock, MA_NOTOWNED);
1448 if (c->flags & CHN_F_BUSY)
1449 si->openedaudio[j / intnbits] |=
1450 (1 << (j % intnbits));
1458 si->numsynths = 0; /* OSSv4 docs: this field is obsolete */
1460 * @todo Collect num{midis,timers}.
1462 * Need access to sound/midi/midi.c::midistat_lock in order
1463 * to safely touch midi_devices and get a head count of, well,
1464 * MIDI devices. midistat_lock is a global static (i.e., local to
1465 * midi.c), but midi_devices is a regular global; should the mutex
1466 * be publicized, or is there another way to get this information?
1468 * NB: MIDI/sequencer stuff is currently on hold.
1472 si->nummixers = mixer_count;
1473 si->numcards = ncards;
1474 /* OSSv4 docs: Intended only for test apps; API doesn't
1475 really have much of a concept of cards. Shouldn't be
1476 used by applications. */
1479 * @todo Fill in "busy devices" fields.
1481 * si->openedmidi = " MIDI devices
1483 bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
1486 * Si->filler is a reserved array, but according to docs each
1487 * element should be set to -1.
1489 for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++)
1493 /************************************************************************/
1496 sound_modevent(module_t mod, int type, void *data)
1500 return (midi_modevent(mod, type, data));
1506 pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
1510 ret = sndstat_acquire(curthread);
1513 if (pcmsg_unrhdr != NULL) {
1514 delete_unrhdr(pcmsg_unrhdr);
1515 pcmsg_unrhdr = NULL;
1526 DEV_MODULE(sound, sound_modevent, NULL);
1527 MODULE_VERSION(sound, SOUND_MODVER);