2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7 * Copyright (c) 1997 Luigi Rizzo
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #ifdef HAVE_KERNEL_OPTION_HEADERS
36 #include <dev/sound/pcm/sound.h>
37 #include <dev/sound/pcm/ac97.h>
38 #include <dev/sound/pcm/vchan.h>
39 #include <dev/sound/pcm/dsp.h>
40 #include <dev/sound/pcm/sndstat.h>
41 #include <dev/sound/version.h>
42 #include <sys/limits.h>
43 #include <sys/sysctl.h>
45 #include "feeder_if.h"
47 SND_DECLARE_FILE("$FreeBSD$");
49 devclass_t pcm_devclass;
51 int pcm_veto_load = 1;
55 static int snd_unit_auto = -1;
56 SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RWTUN,
57 &snd_unit_auto, 0, "assign default unit to a newly attached device");
59 int snd_maxautovchans = 16;
61 SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
64 static void pcm_sysinit(device_t);
67 * XXX I've had enough with people not telling proper version/arch
68 * while reporting problems, not after 387397913213th questions/requests.
70 static char snd_driver_version[] =
71 __XSTRING(SND_DRV_VERSION)"/"MACHINE_ARCH;
72 SYSCTL_STRING(_hw_snd, OID_AUTO, version, CTLFLAG_RD, &snd_driver_version,
73 0, "driver version/arch");
76 * @brief Unit number allocator for syncgroup IDs
78 struct unrhdr *pcmsg_unrhdr = NULL;
81 sndstat_prepare_pcm(SNDSTAT_PREPARE_PCM_ARGS)
83 SNDSTAT_PREPARE_PCM_BEGIN();
84 SNDSTAT_PREPARE_PCM_END();
88 snd_mtxcreate(const char *desc, const char *type)
92 m = malloc(sizeof(*m), M_DEVBUF, M_WAITOK | M_ZERO);
93 mtx_init(m, desc, type, MTX_DEF);
107 snd_mtxassert(void *m)
112 mtx_assert(mtx, MA_OWNED);
117 snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
119 struct snddev_info *d;
121 flags &= INTR_MPSAFE;
122 flags |= INTR_TYPE_AV;
123 d = device_get_softc(dev);
124 if (d != NULL && (flags & INTR_MPSAFE))
125 d->flags |= SD_F_MPSAFE;
127 return bus_setup_intr(dev, res, flags, NULL, hand, param, cookiep);
131 pcm_clonereset(struct snddev_info *d)
137 cmax = d->playcount + d->reccount - 1;
138 if (d->pvchancount > 0)
139 cmax += max(d->pvchancount, snd_maxautovchans) - 1;
140 if (d->rvchancount > 0)
141 cmax += max(d->rvchancount, snd_maxautovchans) - 1;
142 if (cmax > PCMMAXCLONE)
144 (void)snd_clone_gc(d->clones);
145 (void)snd_clone_setmaxunit(d->clones, cmax);
149 pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num)
151 struct pcm_channel *c, *ch, *nch;
152 struct pcmchan_caps *caps;
157 if ((direction == PCMDIR_PLAY && d->playcount < 1) ||
158 (direction == PCMDIR_REC && d->reccount < 1))
161 if (!(d->flags & SD_F_AUTOVCHAN))
164 if (newcnt < 0 || newcnt > SND_MAXVCHANS)
167 if (direction == PCMDIR_PLAY)
168 vcnt = d->pvchancount;
169 else if (direction == PCMDIR_REC)
170 vcnt = d->rvchancount;
176 (num >= 0 && num < SND_MAXVCHANS && (newcnt - 1) == vcnt),
177 ("bogus vchan_create() request num=%d newcnt=%d vcnt=%d",
179 /* add new vchans - find a parent channel first */
181 CHN_FOREACH(c, d, channels.pcm) {
183 if (c->direction == direction &&
184 ((c->flags & CHN_F_HAS_VCHAN) || (vcnt == 0 &&
186 !(c->flags & (CHN_F_BUSY | CHN_F_VIRTUAL))))) {
188 * Reuse hw channel with vchans already
191 if (c->flags & CHN_F_HAS_VCHAN) {
196 * No vchans ever created, look for
197 * channels with supported formats.
199 caps = chn_getcaps(c);
204 for (i = 0; caps->fmtlist[i] != 0; i++) {
205 if (caps->fmtlist[i] & AFMT_CONVERTIBLE)
208 if (caps->fmtlist[i] != 0) {
217 ch->flags |= CHN_F_BUSY;
219 while (err == 0 && newcnt > vcnt) {
220 err = vchan_create(ch, num);
223 else if (err == E2BIG && newcnt > vcnt)
224 device_printf(d->dev,
225 "%s: err=%d Maximum channel reached.\n",
229 ch->flags &= ~CHN_F_BUSY;
235 } else if (newcnt < vcnt) {
237 ("bogus vchan_destroy() request num=%d", num));
238 CHN_FOREACH(c, d, channels.pcm) {
240 if (c->direction != direction ||
241 CHN_EMPTY(c, children) ||
242 !(c->flags & CHN_F_HAS_VCHAN)) {
246 CHN_FOREACH_SAFE(ch, c, nch, children) {
248 if (vcnt == 1 && c->refcount > 0) {
252 if (!(ch->flags & CHN_F_BUSY) &&
254 err = vchan_destroy(ch);
271 /* return error status and a locked channel */
273 pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
274 pid_t pid, char *comm, int devunit)
276 struct pcm_channel *c;
277 int err, vchancount, vchan_num;
279 KASSERT(d != NULL && ch != NULL && (devunit == -1 ||
280 !(devunit & ~(SND_U_MASK | SND_D_MASK | SND_C_MASK))) &&
281 (direction == PCMDIR_PLAY || direction == PCMDIR_REC),
282 ("%s(): invalid d=%p ch=%p direction=%d pid=%d devunit=%d",
283 __func__, d, ch, direction, pid, devunit));
286 /* Double check again. */
288 switch (snd_unit2d(devunit)) {
289 case SND_DEV_DSPHW_PLAY:
290 case SND_DEV_DSPHW_VPLAY:
291 if (direction != PCMDIR_PLAY)
294 case SND_DEV_DSPHW_REC:
295 case SND_DEV_DSPHW_VREC:
296 if (direction != PCMDIR_REC)
300 if (!(direction == PCMDIR_PLAY ||
301 direction == PCMDIR_REC))
309 vchancount = (direction == PCMDIR_PLAY) ? d->pvchancount :
314 /* scan for a free channel */
315 CHN_FOREACH(c, d, channels.pcm) {
317 if (devunit == -1 && c->direction == direction &&
318 (c->flags & CHN_F_VIRTUAL)) {
319 if (vchancount < snd_maxautovchans &&
320 vchan_num < CHN_CHAN(c)) {
326 if (c->direction == direction && !(c->flags & CHN_F_BUSY) &&
327 (devunit == -1 || devunit == -2 || c->unit == devunit)) {
328 c->flags |= CHN_F_BUSY;
330 strlcpy(c->comm, (comm != NULL) ? comm :
331 CHN_COMM_UNKNOWN, sizeof(c->comm));
334 } else if (c->unit == devunit) {
335 if (c->direction != direction)
337 else if (c->flags & CHN_F_BUSY)
343 } else if ((devunit == -1 || devunit == -2) &&
344 c->direction == direction && (c->flags & CHN_F_BUSY))
353 /* no channel available */
354 if (devunit == -1 || snd_unit2d(devunit) == SND_DEV_DSPHW_VPLAY ||
355 snd_unit2d(devunit) == SND_DEV_DSPHW_VREC) {
356 if (!(vchancount > 0 && vchancount < snd_maxautovchans) &&
357 (devunit == -1 || snd_unit2c(devunit) < snd_maxautovchans))
359 err = pcm_setvchans(d, direction, vchancount + 1,
360 (devunit == -1) ? -1 : snd_unit2c(devunit));
371 /* release a locked channel and unlock it */
373 pcm_chnrelease(struct pcm_channel *c)
375 PCM_BUSYASSERT(c->parentsnddev);
378 c->flags &= ~CHN_F_BUSY;
380 strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm));
387 pcm_chnref(struct pcm_channel *c, int ref)
389 PCM_BUSYASSERT(c->parentsnddev);
394 return (c->refcount);
398 pcm_inprog(struct snddev_info *d, int delta)
408 pcm_setmaxautovchans(struct snddev_info *d, int num)
415 if (num >= 0 && d->pvchancount > num)
416 (void)pcm_setvchans(d, PCMDIR_PLAY, num, -1);
417 else if (num > 0 && d->pvchancount == 0)
418 (void)pcm_setvchans(d, PCMDIR_PLAY, 1, -1);
420 if (num >= 0 && d->rvchancount > num)
421 (void)pcm_setvchans(d, PCMDIR_REC, num, -1);
422 else if (num > 0 && d->rvchancount == 0)
423 (void)pcm_setvchans(d, PCMDIR_REC, 1, -1);
429 sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS)
431 struct snddev_info *d;
435 error = sysctl_handle_int(oidp, &unit, 0, req);
436 if (error == 0 && req->newptr != NULL) {
437 d = devclass_get_softc(pcm_devclass, unit);
438 if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm))
445 /* XXX: do we need a way to let the user change the default unit? */
446 SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit,
447 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT, 0,
448 sizeof(int), sysctl_hw_snd_default_unit, "I",
449 "default sound device");
452 sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS)
454 struct snddev_info *d;
457 v = snd_maxautovchans;
458 error = sysctl_handle_int(oidp, &v, 0, req);
459 if (error == 0 && req->newptr != NULL) {
462 if (v > SND_MAXVCHANS)
464 snd_maxautovchans = v;
465 for (i = 0; pcm_devclass != NULL &&
466 i < devclass_get_maxunit(pcm_devclass); i++) {
467 d = devclass_get_softc(pcm_devclass, i);
468 if (!PCM_REGISTERED(d))
470 PCM_ACQUIRE_QUICK(d);
471 pcm_setmaxautovchans(d, v);
472 PCM_RELEASE_QUICK(d);
477 SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans,
478 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
479 sysctl_hw_snd_maxautovchans, "I",
480 "maximum virtual channel");
483 pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo)
485 struct pcm_channel *ch;
486 int direction, err, rpnum, *pnum, max;
487 int udc, device, chan;
488 char *dirs, *devname, buf[CHN_NAMELEN];
492 KASSERT(num >= -1, ("invalid num=%d", num));
497 direction = PCMDIR_PLAY;
498 pnum = &d->playcount;
499 device = SND_DEV_DSPHW_PLAY;
502 case PCMDIR_PLAY_VIRTUAL:
504 direction = PCMDIR_PLAY;
505 pnum = &d->pvchancount;
506 device = SND_DEV_DSPHW_VPLAY;
511 direction = PCMDIR_REC;
513 device = SND_DEV_DSPHW_REC;
516 case PCMDIR_REC_VIRTUAL:
518 direction = PCMDIR_REC;
519 pnum = &d->rvchancount;
520 device = SND_DEV_DSPHW_VREC;
527 chan = (num == -1) ? 0 : num;
529 if (*pnum >= max || chan >= max)
534 CHN_FOREACH(ch, d, channels.pcm) {
535 if (CHN_DEV(ch) != device)
537 if (chan == CHN_CHAN(ch)) {
539 device_printf(d->dev,
540 "channel num=%d allocated!\n", chan);
545 device_printf(d->dev,
546 "chan=%d > %d\n", chan, max);
553 if (*pnum != rpnum) {
554 device_printf(d->dev,
555 "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n",
556 __func__, dirs, *pnum, rpnum);
560 udc = snd_mkunit(device_get_unit(d->dev), device, chan);
561 devname = dsp_unit2name(buf, sizeof(buf), udc);
563 if (devname == NULL) {
564 device_printf(d->dev,
565 "Failed to query device name udc=0x%08x\n", udc);
570 ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
571 ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
574 strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm));
575 ch->parentsnddev = d;
576 ch->parentchannel = parent;
578 ch->trigger = PCMTRIG_STOP;
579 snprintf(ch->name, sizeof(ch->name), "%s:%s:%s",
580 device_get_nameunit(ch->dev), dirs, devname);
582 err = chn_init(ch, devinfo, dir, direction);
585 device_printf(d->dev, "chn_init(%s) failed: err = %d\n",
587 kobj_delete(ch->methods, M_DEVBUF);
596 pcm_chn_destroy(struct pcm_channel *ch)
598 struct snddev_info *d;
601 d = ch->parentsnddev;
606 device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n",
611 kobj_delete(ch->methods, M_DEVBUF);
618 pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch)
622 KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY ||
623 ch->direction == PCMDIR_REC), ("Invalid pcm channel"));
625 CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm);
627 switch (CHN_DEV(ch)) {
628 case SND_DEV_DSPHW_PLAY:
631 case SND_DEV_DSPHW_VPLAY:
634 case SND_DEV_DSPHW_REC:
637 case SND_DEV_DSPHW_VREC:
650 pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch)
652 struct pcm_channel *tmp;
659 CHN_FOREACH(tmp, d, channels.pcm) {
667 CHN_REMOVE(d, ch, channels.pcm);
669 switch (CHN_DEV(ch)) {
670 case SND_DEV_DSPHW_PLAY:
673 case SND_DEV_DSPHW_VPLAY:
676 case SND_DEV_DSPHW_REC:
679 case SND_DEV_DSPHW_VREC:
692 pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
694 struct snddev_info *d = device_get_softc(dev);
695 struct pcm_channel *ch;
701 ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo);
703 device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n",
704 cls->name, dir, devinfo);
709 err = pcm_chn_add(d, ch);
712 device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n",
721 pcm_killchan(device_t dev)
723 struct snddev_info *d = device_get_softc(dev);
724 struct pcm_channel *ch;
729 ch = CHN_FIRST(d, channels.pcm);
732 error = pcm_chn_remove(d, ch);
736 return (pcm_chn_destroy(ch));
740 pcm_best_unit(int old)
742 struct snddev_info *d;
743 int i, best, bestprio, prio;
747 for (i = 0; pcm_devclass != NULL &&
748 i < devclass_get_maxunit(pcm_devclass); i++) {
749 d = devclass_get_softc(pcm_devclass, i);
750 if (!PCM_REGISTERED(d))
753 if (d->playcount == 0)
755 if (d->reccount == 0)
757 if (prio > bestprio || (prio == bestprio && i == old)) {
766 pcm_setstatus(device_t dev, char *str)
768 struct snddev_info *d = device_get_softc(dev);
770 /* should only be called once */
771 if (d->flags & SD_F_REGISTERED)
776 if (d->playcount == 0 || d->reccount == 0)
777 d->flags |= SD_F_SIMPLEX;
779 if (d->playcount > 0 || d->reccount > 0)
780 d->flags |= SD_F_AUTOVCHAN;
782 pcm_setmaxautovchans(d, snd_maxautovchans);
784 strlcpy(d->status, str, SND_STATUSLEN);
788 /* Last stage, enable cloning. */
789 if (d->clones != NULL)
790 (void)snd_clone_enable(d->clones);
792 /* Done, we're ready.. */
793 d->flags |= SD_F_REGISTERED;
800 * Create all sysctls once SD_F_REGISTERED is set else
801 * tunable sysctls won't work:
805 if (snd_unit_auto < 0)
806 snd_unit_auto = (snd_unit < 0) ? 1 : 0;
807 if (snd_unit < 0 || snd_unit_auto > 1)
808 snd_unit = device_get_unit(dev);
809 else if (snd_unit_auto == 1)
810 snd_unit = pcm_best_unit(snd_unit);
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);
870 sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
872 struct snddev_info *d;
876 if (!PCM_REGISTERED(d))
881 val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
885 err = sysctl_handle_int(oidp, &val, 0, req);
887 if (err == 0 && req->newptr != NULL) {
888 if (!(val == 0 || val == 1)) {
889 PCM_RELEASE_QUICK(d);
895 d->flags &= ~SD_F_BITPERFECT;
896 d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
901 PCM_RELEASE_QUICK(d);
908 sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS)
910 struct snddev_info *d;
915 if (!PCM_REGISTERED(d) || d->clones == NULL)
918 PCM_ACQUIRE_QUICK(d);
920 flags = snd_clone_getflags(d->clones);
921 err = sysctl_handle_int(oidp, &flags, 0, req);
923 if (err == 0 && req->newptr != NULL) {
924 if (flags & ~SND_CLONE_MASK)
927 (void)snd_clone_setflags(d->clones, flags);
930 PCM_RELEASE_QUICK(d);
936 sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS)
938 struct snddev_info *d;
942 if (!PCM_REGISTERED(d) || d->clones == NULL)
945 PCM_ACQUIRE_QUICK(d);
947 deadline = snd_clone_getdeadline(d->clones);
948 err = sysctl_handle_int(oidp, &deadline, 0, req);
950 if (err == 0 && req->newptr != NULL) {
954 (void)snd_clone_setdeadline(d->clones, deadline);
957 PCM_RELEASE_QUICK(d);
963 sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS)
965 struct snddev_info *d;
969 if (!PCM_REGISTERED(d) || d->clones == NULL)
973 err = sysctl_handle_int(oidp, &val, 0, req);
975 if (err == 0 && req->newptr != NULL && val != 0) {
976 PCM_ACQUIRE_QUICK(d);
977 val = snd_clone_gc(d->clones);
978 PCM_RELEASE_QUICK(d);
979 if (bootverbose != 0 || snd_verbose > 3)
980 device_printf(d->dev, "clone gc: pruned=%d\n", val);
987 sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS)
989 struct snddev_info *d;
993 err = sysctl_handle_int(oidp, &val, 0, req);
995 if (err == 0 && req->newptr != NULL && val != 0) {
996 for (i = 0; pcm_devclass != NULL &&
997 i < devclass_get_maxunit(pcm_devclass); i++) {
998 d = devclass_get_softc(pcm_devclass, i);
999 if (!PCM_REGISTERED(d) || d->clones == NULL)
1001 PCM_ACQUIRE_QUICK(d);
1002 val = snd_clone_gc(d->clones);
1003 PCM_RELEASE_QUICK(d);
1004 if (bootverbose != 0 || snd_verbose > 3)
1005 device_printf(d->dev, "clone gc: pruned=%d\n",
1012 SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc,
1013 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
1014 sysctl_hw_snd_clone_gc, "I",
1015 "global clone garbage collector");
1019 pcm_sysinit(device_t dev)
1021 struct snddev_info *d = device_get_softc(dev);
1023 /* XXX: an user should be able to set this with a control tool, the
1024 sysadmin then needs min+max sysctls for this */
1025 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
1026 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1027 OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size");
1028 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1029 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1030 "bitperfect", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, d,
1031 sizeof(d), sysctl_dev_pcm_bitperfect, "I",
1032 "bit-perfect playback/recording (0=disable, 1=enable)");
1034 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1035 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1036 "clone_flags", CTLTYPE_UINT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
1037 d, sizeof(d), sysctl_dev_pcm_clone_flags, "IU",
1039 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1040 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1041 "clone_deadline", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
1042 d, sizeof(d), sysctl_dev_pcm_clone_deadline, "I",
1043 "clone expiration deadline (ms)");
1044 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1045 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1047 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, d, sizeof(d),
1048 sysctl_dev_pcm_clone_gc, "I", "clone garbage collector");
1050 if (d->flags & SD_F_AUTOVCHAN)
1052 if (d->flags & SD_F_EQ)
1053 feeder_eq_initsys(dev);
1057 pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
1059 struct snddev_info *d;
1062 if (pcm_veto_load) {
1063 device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load);
1068 if (device_get_unit(dev) > PCMMAXUNIT) {
1069 device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n",
1070 device_get_unit(dev), PCMMAXUNIT);
1072 "Use 'hw.snd.maxunit' tunable to raise the limit.\n");
1076 d = device_get_softc(dev);
1078 d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
1079 cv_init(&d->cv, device_get_nameunit(dev));
1080 PCM_ACQUIRE_QUICK(d);
1081 dsp_cdevinfo_init(d);
1084 * d->flags should be cleared by the allocator of the softc.
1085 * We cannot clear this field here because several devices set
1086 * this flag before calling pcm_register().
1091 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1092 "vpc", &i) != 0 || i != 0)
1093 d->flags |= SD_F_VPC;
1095 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1096 "bitperfect", &i) == 0 && i != 0)
1097 d->flags |= SD_F_BITPERFECT;
1099 d->devinfo = devinfo;
1106 d->pvchanformat = 0;
1108 d->rvchanformat = 0;
1112 * Create clone manager, disabled by default. Cloning will be
1113 * enabled during final stage of driver initialization through
1116 d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE,
1117 SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK |
1118 SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF |
1119 SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED);
1121 CHN_INIT(d, channels.pcm);
1122 CHN_INIT(d, channels.pcm.busy);
1123 CHN_INIT(d, channels.pcm.opened);
1125 /* XXX This is incorrect, but lets play along for now. */
1126 if ((numplay == 0 || numrec == 0) && numplay != numrec)
1127 d->flags |= SD_F_SIMPLEX;
1129 sysctl_ctx_init(&d->play_sysctl_ctx);
1130 d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
1131 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
1132 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "playback channels node");
1133 sysctl_ctx_init(&d->rec_sysctl_ctx);
1134 d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
1135 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
1136 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "record channels node");
1138 if (numplay > 0 || numrec > 0)
1139 d->flags |= SD_F_AUTOVCHAN;
1141 sndstat_register(dev, d->status, sndstat_prepare_pcm);
1147 pcm_unregister(device_t dev)
1149 struct snddev_info *d;
1150 struct pcm_channel *ch;
1154 d = device_get_softc(dev);
1156 if (!PCM_ALIVE(d)) {
1157 device_printf(dev, "unregister: device not configured\n");
1164 d->flags |= SD_F_DETACHING;
1166 if (d->inprog != 0) {
1167 device_printf(dev, "unregister: operation in progress\n");
1175 CHN_FOREACH(ch, d, channels.pcm) {
1177 if (ch->refcount > 0) {
1179 "unregister: channel %s busy (pid %d)\n",
1182 PCM_RELEASE_QUICK(d);
1188 if (d->clones != NULL) {
1189 if (snd_clone_busy(d->clones) != 0) {
1190 device_printf(dev, "unregister: clone busy\n");
1191 PCM_RELEASE_QUICK(d);
1195 (void)snd_clone_disable(d->clones);
1200 if (mixer_uninit(dev) == EBUSY) {
1201 device_printf(dev, "unregister: mixer busy\n");
1203 if (d->clones != NULL)
1204 (void)snd_clone_enable(d->clones);
1210 /* remove /dev/sndstat entry first */
1211 sndstat_unregister(dev);
1214 d->flags |= SD_F_DYING;
1215 d->flags &= ~SD_F_REGISTERED;
1219 * No lock being held, so this thing can be flushed without
1220 * stucking into devdrn oblivion.
1222 if (d->clones != NULL) {
1223 snd_clone_destroy(d->clones);
1227 if (d->play_sysctl_tree != NULL) {
1228 sysctl_ctx_free(&d->play_sysctl_ctx);
1229 d->play_sysctl_tree = NULL;
1231 if (d->rec_sysctl_tree != NULL) {
1232 sysctl_ctx_free(&d->rec_sysctl_ctx);
1233 d->rec_sysctl_tree = NULL;
1236 while (!CHN_EMPTY(d, channels.pcm))
1239 dsp_cdevinfo_flush(d);
1245 snd_mtxfree(d->lock);
1247 if (snd_unit == device_get_unit(dev)) {
1248 snd_unit = pcm_best_unit(-1);
1249 if (snd_unit_auto == 0)
1256 /************************************************************************/
1259 * @brief Handle OSSv4 SNDCTL_SYSINFO ioctl.
1261 * @param si Pointer to oss_sysinfo struct where information about the
1262 * sound subsystem will be written/copied.
1264 * This routine returns information about the sound system, such as the
1265 * current OSS version, number of audio, MIDI, and mixer drivers, etc.
1266 * Also includes a bitmask showing which of the above types of devices
1270 * Calling threads must not hold any snddev_info or pcm_channel locks.
1272 * @author Ryan Beasley <ryanb@FreeBSD.org>
1275 sound_oss_sysinfo(oss_sysinfo *si)
1277 static char si_product[] = "FreeBSD native OSS ABI";
1278 static char si_version[] = __XSTRING(__FreeBSD_version);
1279 static char si_license[] = "BSD";
1280 static int intnbits = sizeof(int) * 8; /* Better suited as macro?
1281 Must pester a C guru. */
1283 struct snddev_info *d;
1284 struct pcm_channel *c;
1289 strlcpy(si->product, si_product, sizeof(si->product));
1290 strlcpy(si->version, si_version, sizeof(si->version));
1291 si->versionnum = SOUND_VERSION;
1292 strlcpy(si->license, si_license, sizeof(si->license));
1295 * Iterate over PCM devices and their channels, gathering up data
1296 * for the numaudios, ncards, and openedaudio fields.
1299 bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
1303 for (i = 0; pcm_devclass != NULL &&
1304 i < devclass_get_maxunit(pcm_devclass); i++) {
1305 d = devclass_get_softc(pcm_devclass, i);
1306 if (!PCM_REGISTERED(d))
1309 /* XXX Need Giant magic entry ??? */
1311 /* See note in function's docblock */
1312 PCM_UNLOCKASSERT(d);
1315 si->numaudios += d->devcount;
1318 CHN_FOREACH(c, d, channels.pcm) {
1319 CHN_UNLOCKASSERT(c);
1321 if (c->flags & CHN_F_BUSY)
1322 si->openedaudio[j / intnbits] |=
1323 (1 << (j % intnbits));
1330 si->numaudioengines = si->numaudios;
1332 si->numsynths = 0; /* OSSv4 docs: this field is obsolete */
1334 * @todo Collect num{midis,timers}.
1336 * Need access to sound/midi/midi.c::midistat_lock in order
1337 * to safely touch midi_devices and get a head count of, well,
1338 * MIDI devices. midistat_lock is a global static (i.e., local to
1339 * midi.c), but midi_devices is a regular global; should the mutex
1340 * be publicized, or is there another way to get this information?
1342 * NB: MIDI/sequencer stuff is currently on hold.
1346 si->nummixers = mixer_count;
1347 si->numcards = ncards;
1348 /* OSSv4 docs: Intended only for test apps; API doesn't
1349 really have much of a concept of cards. Shouldn't be
1350 used by applications. */
1353 * @todo Fill in "busy devices" fields.
1355 * si->openedmidi = " MIDI devices
1357 bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
1360 * Si->filler is a reserved array, but according to docs each
1361 * element should be set to -1.
1363 for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++)
1368 sound_oss_card_info(oss_card_info *si)
1370 struct snddev_info *d;
1375 for (i = 0; pcm_devclass != NULL &&
1376 i < devclass_get_maxunit(pcm_devclass); i++) {
1377 d = devclass_get_softc(pcm_devclass, i);
1378 if (!PCM_REGISTERED(d))
1381 if (ncards++ != si->card)
1384 PCM_UNLOCKASSERT(d);
1387 strlcpy(si->shortname, device_get_nameunit(d->dev),
1388 sizeof(si->shortname));
1389 strlcpy(si->longname, device_get_desc(d->dev),
1390 sizeof(si->longname));
1391 strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
1392 si->intr_count = si->ack_count = 0;
1401 /************************************************************************/
1404 sound_modevent(module_t mod, int type, void *data)
1408 return (midi_modevent(mod, type, data));
1414 pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
1417 if (pcmsg_unrhdr != NULL) {
1418 delete_unrhdr(pcmsg_unrhdr);
1419 pcmsg_unrhdr = NULL;
1432 DEV_MODULE(sound, sound_modevent, NULL);
1433 MODULE_VERSION(sound, SOUND_MODVER);