2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
4 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
5 * Copyright (c) 1997 Luigi Rizzo
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #ifdef HAVE_KERNEL_OPTION_HEADERS
34 #include <dev/sound/pcm/sound.h>
35 #include <dev/sound/pcm/ac97.h>
36 #include <dev/sound/pcm/vchan.h>
37 #include <dev/sound/pcm/dsp.h>
38 #include <dev/sound/pcm/sndstat.h>
39 #include <dev/sound/version.h>
40 #include <sys/limits.h>
41 #include <sys/sysctl.h>
43 #include "feeder_if.h"
45 SND_DECLARE_FILE("$FreeBSD$");
47 devclass_t pcm_devclass;
49 int pcm_veto_load = 1;
52 TUNABLE_INT("hw.snd.default_unit", &snd_unit);
54 static int snd_unit_auto = 0;
55 TUNABLE_INT("hw.snd.default_auto", &snd_unit_auto);
56 SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RW,
57 &snd_unit_auto, 0, "assign default unit to a newly attached device");
59 int snd_maxautovchans = 16;
60 /* XXX: a tunable implies that we may need more than one sound channel before
61 the system can change a sysctl (/etc/sysctl.conf), do we really need
63 TUNABLE_INT("hw.snd.maxautovchans", &snd_maxautovchans);
65 SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD, 0, "Sound driver");
68 * XXX I've had enough with people not telling proper version/arch
69 * while reporting problems, not after 387397913213th questions/requests.
71 static const char snd_driver_version[] =
72 __XSTRING(SND_DRV_VERSION)"/"MACHINE_ARCH;
73 SYSCTL_STRING(_hw_snd, OID_AUTO, version, CTLFLAG_RD, &snd_driver_version,
74 0, "driver version/arch");
77 * @brief Unit number allocator for syncgroup IDs
79 struct unrhdr *pcmsg_unrhdr = NULL;
82 sndstat_prepare_pcm(SNDSTAT_PREPARE_PCM_ARGS)
84 SNDSTAT_PREPARE_PCM_BEGIN();
85 SNDSTAT_PREPARE_PCM_END();
89 snd_mtxcreate(const char *desc, const char *type)
93 m = malloc(sizeof(*m), M_DEVBUF, M_WAITOK | M_ZERO);
94 mtx_init(m, desc, type, MTX_DEF);
108 snd_mtxassert(void *m)
113 mtx_assert(mtx, MA_OWNED);
118 snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
120 struct snddev_info *d;
122 flags &= INTR_MPSAFE;
123 flags |= INTR_TYPE_AV;
124 d = device_get_softc(dev);
125 if (d != NULL && (flags & INTR_MPSAFE))
126 d->flags |= SD_F_MPSAFE;
128 return bus_setup_intr(dev, res, flags,
129 #if __FreeBSD_version >= 700031
132 hand, param, cookiep);
136 pcm_clonereset(struct snddev_info *d)
142 cmax = d->playcount + d->reccount - 1;
143 if (d->pvchancount > 0)
144 cmax += max(d->pvchancount, snd_maxautovchans) - 1;
145 if (d->rvchancount > 0)
146 cmax += max(d->rvchancount, snd_maxautovchans) - 1;
147 if (cmax > PCMMAXCLONE)
149 (void)snd_clone_gc(d->clones);
150 (void)snd_clone_setmaxunit(d->clones, cmax);
154 pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num)
156 struct pcm_channel *c, *ch, *nch;
157 struct pcmchan_caps *caps;
162 if ((direction == PCMDIR_PLAY && d->playcount < 1) ||
163 (direction == PCMDIR_REC && d->reccount < 1))
166 if (!(d->flags & SD_F_AUTOVCHAN))
169 if (newcnt < 0 || newcnt > SND_MAXVCHANS)
172 if (direction == PCMDIR_PLAY)
173 vcnt = d->pvchancount;
174 else if (direction == PCMDIR_REC)
175 vcnt = d->rvchancount;
181 (num >= 0 && num < SND_MAXVCHANS && (newcnt - 1) == vcnt),
182 ("bogus vchan_create() request num=%d newcnt=%d vcnt=%d",
184 /* add new vchans - find a parent channel first */
186 CHN_FOREACH(c, d, channels.pcm) {
188 if (c->direction == direction &&
189 ((c->flags & CHN_F_HAS_VCHAN) || (vcnt == 0 &&
191 !(c->flags & (CHN_F_BUSY | CHN_F_VIRTUAL))))) {
193 * Reuse hw channel with vchans already
196 if (c->flags & CHN_F_HAS_VCHAN) {
201 * No vchans ever created, look for
202 * channels with supported formats.
204 caps = chn_getcaps(c);
209 for (i = 0; caps->fmtlist[i] != 0; i++) {
210 if (caps->fmtlist[i] & AFMT_CONVERTIBLE)
213 if (caps->fmtlist[i] != 0) {
222 ch->flags |= CHN_F_BUSY;
224 while (err == 0 && newcnt > vcnt) {
225 err = vchan_create(ch, num);
228 else if (err == E2BIG && newcnt > vcnt)
229 device_printf(d->dev,
230 "%s: err=%d Maximum channel reached.\n",
234 ch->flags &= ~CHN_F_BUSY;
240 } else if (newcnt < vcnt) {
242 ("bogus vchan_destroy() request num=%d", num));
243 CHN_FOREACH(c, d, channels.pcm) {
245 if (c->direction != direction ||
246 CHN_EMPTY(c, children) ||
247 !(c->flags & CHN_F_HAS_VCHAN)) {
251 CHN_FOREACH_SAFE(ch, c, nch, children) {
253 if (vcnt == 1 && c->refcount > 0) {
257 if (!(ch->flags & CHN_F_BUSY) &&
259 err = vchan_destroy(ch);
276 /* return error status and a locked channel */
278 pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
279 pid_t pid, char *comm, int devunit)
281 struct pcm_channel *c;
282 int err, vchancount, vchan_num;
284 KASSERT(d != NULL && ch != NULL && (devunit == -1 ||
285 !(devunit & ~(SND_U_MASK | SND_D_MASK | SND_C_MASK))) &&
286 (direction == PCMDIR_PLAY || direction == PCMDIR_REC),
287 ("%s(): invalid d=%p ch=%p direction=%d pid=%d devunit=%d",
288 __func__, d, ch, direction, pid, devunit));
291 /* Double check again. */
293 switch (snd_unit2d(devunit)) {
294 case SND_DEV_DSPHW_PLAY:
295 case SND_DEV_DSPHW_VPLAY:
296 if (direction != PCMDIR_PLAY)
299 case SND_DEV_DSPHW_REC:
300 case SND_DEV_DSPHW_VREC:
301 if (direction != PCMDIR_REC)
305 if (!(direction == PCMDIR_PLAY ||
306 direction == PCMDIR_REC))
314 vchancount = (direction == PCMDIR_PLAY) ? d->pvchancount :
319 /* scan for a free channel */
320 CHN_FOREACH(c, d, channels.pcm) {
322 if (devunit == -1 && c->direction == direction &&
323 (c->flags & CHN_F_VIRTUAL)) {
324 if (vchancount < snd_maxautovchans &&
325 vchan_num < CHN_CHAN(c)) {
331 if (c->direction == direction && !(c->flags & CHN_F_BUSY) &&
332 (devunit == -1 || devunit == -2 || c->unit == devunit)) {
333 c->flags |= CHN_F_BUSY;
335 strlcpy(c->comm, (comm != NULL) ? comm :
336 CHN_COMM_UNKNOWN, sizeof(c->comm));
339 } else if (c->unit == devunit) {
340 if (c->direction != direction)
342 else if (c->flags & CHN_F_BUSY)
348 } else if ((devunit == -1 || devunit == -2) &&
349 c->direction == direction && (c->flags & CHN_F_BUSY))
358 /* no channel available */
359 if (devunit == -1 || snd_unit2d(devunit) == SND_DEV_DSPHW_VPLAY ||
360 snd_unit2d(devunit) == SND_DEV_DSPHW_VREC) {
361 if (!(vchancount > 0 && vchancount < snd_maxautovchans) &&
362 (devunit == -1 || snd_unit2c(devunit) < snd_maxautovchans))
364 err = pcm_setvchans(d, direction, vchancount + 1,
365 (devunit == -1) ? -1 : snd_unit2c(devunit));
376 /* release a locked channel and unlock it */
378 pcm_chnrelease(struct pcm_channel *c)
380 PCM_BUSYASSERT(c->parentsnddev);
383 c->flags &= ~CHN_F_BUSY;
385 strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm));
392 pcm_chnref(struct pcm_channel *c, int ref)
394 PCM_BUSYASSERT(c->parentsnddev);
399 return (c->refcount);
403 pcm_inprog(struct snddev_info *d, int delta)
413 pcm_setmaxautovchans(struct snddev_info *d, int num)
420 if (num >= 0 && d->pvchancount > num)
421 (void)pcm_setvchans(d, PCMDIR_PLAY, num, -1);
422 else if (num > 0 && d->pvchancount == 0)
423 (void)pcm_setvchans(d, PCMDIR_PLAY, 1, -1);
425 if (num >= 0 && d->rvchancount > num)
426 (void)pcm_setvchans(d, PCMDIR_REC, num, -1);
427 else if (num > 0 && d->rvchancount == 0)
428 (void)pcm_setvchans(d, PCMDIR_REC, 1, -1);
434 sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS)
436 struct snddev_info *d;
440 error = sysctl_handle_int(oidp, &unit, 0, req);
441 if (error == 0 && req->newptr != NULL) {
442 d = devclass_get_softc(pcm_devclass, unit);
443 if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm))
449 /* XXX: do we need a way to let the user change the default unit? */
450 SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit, CTLTYPE_INT | CTLFLAG_RW,
451 0, sizeof(int), sysctl_hw_snd_default_unit, "I", "default sound device");
454 sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS)
456 struct snddev_info *d;
459 v = snd_maxautovchans;
460 error = sysctl_handle_int(oidp, &v, 0, req);
461 if (error == 0 && req->newptr != NULL) {
464 if (v > SND_MAXVCHANS)
466 snd_maxautovchans = v;
467 for (i = 0; pcm_devclass != NULL &&
468 i < devclass_get_maxunit(pcm_devclass); i++) {
469 d = devclass_get_softc(pcm_devclass, i);
470 if (!PCM_REGISTERED(d))
472 PCM_ACQUIRE_QUICK(d);
473 pcm_setmaxautovchans(d, v);
474 PCM_RELEASE_QUICK(d);
479 SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans, CTLTYPE_INT | CTLFLAG_RW,
480 0, sizeof(int), sysctl_hw_snd_maxautovchans, "I", "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));
498 direction = PCMDIR_PLAY;
499 pnum = &d->playcount;
500 device = SND_DEV_DSPHW_PLAY;
503 case PCMDIR_PLAY_VIRTUAL:
505 direction = PCMDIR_PLAY;
506 pnum = &d->pvchancount;
507 device = SND_DEV_DSPHW_VPLAY;
512 direction = PCMDIR_REC;
514 device = SND_DEV_DSPHW_REC;
517 case PCMDIR_REC_VIRTUAL:
519 direction = PCMDIR_REC;
520 pnum = &d->rvchancount;
521 device = SND_DEV_DSPHW_VREC;
528 chan = (num == -1) ? 0 : num;
530 if (*pnum >= max || chan >= max)
535 CHN_FOREACH(ch, d, channels.pcm) {
536 if (CHN_DEV(ch) != device)
538 if (chan == CHN_CHAN(ch)) {
540 device_printf(d->dev,
541 "channel num=%d allocated!\n", chan);
546 device_printf(d->dev,
547 "chan=%d > %d\n", chan, max);
554 if (*pnum != rpnum) {
555 device_printf(d->dev,
556 "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n",
557 __func__, dirs, *pnum, rpnum);
561 udc = snd_mkunit(device_get_unit(d->dev), device, chan);
562 devname = dsp_unit2name(buf, sizeof(buf), udc);
564 if (devname == NULL) {
565 device_printf(d->dev,
566 "Failed to query device name udc=0x%08x\n", udc);
571 ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
572 ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
575 strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm));
576 ch->parentsnddev = d;
577 ch->parentchannel = parent;
579 ch->trigger = PCMTRIG_STOP;
580 snprintf(ch->name, sizeof(ch->name), "%s:%s:%s",
581 device_get_nameunit(ch->dev), dirs, devname);
583 err = chn_init(ch, devinfo, dir, direction);
586 device_printf(d->dev, "chn_init(%s) failed: err = %d\n",
588 kobj_delete(ch->methods, M_DEVBUF);
597 pcm_chn_destroy(struct pcm_channel *ch)
599 struct snddev_info *d;
602 d = ch->parentsnddev;
607 device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n",
612 kobj_delete(ch->methods, M_DEVBUF);
619 pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch)
623 KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY ||
624 ch->direction == PCMDIR_REC), ("Invalid pcm channel"));
626 CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm);
628 switch (CHN_DEV(ch)) {
629 case SND_DEV_DSPHW_PLAY:
632 case SND_DEV_DSPHW_VPLAY:
635 case SND_DEV_DSPHW_REC:
638 case SND_DEV_DSPHW_VREC:
651 pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch)
653 struct pcm_channel *tmp;
660 CHN_FOREACH(tmp, d, channels.pcm) {
668 CHN_REMOVE(d, ch, channels.pcm);
670 switch (CHN_DEV(ch)) {
671 case SND_DEV_DSPHW_PLAY:
674 case SND_DEV_DSPHW_VPLAY:
677 case SND_DEV_DSPHW_REC:
680 case SND_DEV_DSPHW_VREC:
693 pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
695 struct snddev_info *d = device_get_softc(dev);
696 struct pcm_channel *ch;
702 ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo);
704 device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n",
705 cls->name, dir, devinfo);
710 err = pcm_chn_add(d, ch);
713 device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n",
722 pcm_killchan(device_t dev)
724 struct snddev_info *d = device_get_softc(dev);
725 struct pcm_channel *ch;
730 ch = CHN_FIRST(d, channels.pcm);
733 error = pcm_chn_remove(d, ch);
737 return (pcm_chn_destroy(ch));
741 pcm_setstatus(device_t dev, char *str)
743 struct snddev_info *d = device_get_softc(dev);
747 if (d->playcount == 0 || d->reccount == 0)
748 d->flags |= SD_F_SIMPLEX;
750 if ((d->playcount > 0 || d->reccount > 0) &&
751 !(d->flags & SD_F_AUTOVCHAN)) {
752 d->flags |= SD_F_AUTOVCHAN;
756 pcm_setmaxautovchans(d, snd_maxautovchans);
758 strlcpy(d->status, str, SND_STATUSLEN);
762 /* Last stage, enable cloning. */
763 if (d->clones != NULL)
764 (void)snd_clone_enable(d->clones);
766 /* Done, we're ready.. */
767 d->flags |= SD_F_REGISTERED;
773 if (snd_unit < 0 || snd_unit_auto != 0)
774 snd_unit = device_get_unit(dev);
780 pcm_getflags(device_t dev)
782 struct snddev_info *d = device_get_softc(dev);
788 pcm_setflags(device_t dev, uint32_t val)
790 struct snddev_info *d = device_get_softc(dev);
796 pcm_getdevinfo(device_t dev)
798 struct snddev_info *d = device_get_softc(dev);
804 pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
806 struct snddev_info *d = device_get_softc(dev);
810 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
812 RANGE(sz, minbufsz, maxbufsz);
814 device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
821 device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
834 sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
836 struct snddev_info *d;
840 if (!PCM_REGISTERED(d))
845 val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
849 err = sysctl_handle_int(oidp, &val, 0, req);
851 if (err == 0 && req->newptr != NULL) {
852 if (!(val == 0 || val == 1)) {
853 PCM_RELEASE_QUICK(d);
859 d->flags &= ~SD_F_BITPERFECT;
860 d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
865 PCM_RELEASE_QUICK(d);
872 sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS)
874 struct snddev_info *d;
879 if (!PCM_REGISTERED(d) || d->clones == NULL)
882 PCM_ACQUIRE_QUICK(d);
884 flags = snd_clone_getflags(d->clones);
885 err = sysctl_handle_int(oidp, &flags, 0, req);
887 if (err == 0 && req->newptr != NULL) {
888 if (flags & ~SND_CLONE_MASK)
891 (void)snd_clone_setflags(d->clones, flags);
894 PCM_RELEASE_QUICK(d);
900 sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS)
902 struct snddev_info *d;
906 if (!PCM_REGISTERED(d) || d->clones == NULL)
909 PCM_ACQUIRE_QUICK(d);
911 deadline = snd_clone_getdeadline(d->clones);
912 err = sysctl_handle_int(oidp, &deadline, 0, req);
914 if (err == 0 && req->newptr != NULL) {
918 (void)snd_clone_setdeadline(d->clones, deadline);
921 PCM_RELEASE_QUICK(d);
927 sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS)
929 struct snddev_info *d;
933 if (!PCM_REGISTERED(d) || d->clones == NULL)
937 err = sysctl_handle_int(oidp, &val, 0, req);
939 if (err == 0 && req->newptr != NULL && val != 0) {
940 PCM_ACQUIRE_QUICK(d);
941 val = snd_clone_gc(d->clones);
942 PCM_RELEASE_QUICK(d);
943 if (bootverbose != 0 || snd_verbose > 3)
944 device_printf(d->dev, "clone gc: pruned=%d\n", val);
951 sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS)
953 struct snddev_info *d;
957 err = sysctl_handle_int(oidp, &val, 0, req);
959 if (err == 0 && req->newptr != NULL && val != 0) {
960 for (i = 0; pcm_devclass != NULL &&
961 i < devclass_get_maxunit(pcm_devclass); i++) {
962 d = devclass_get_softc(pcm_devclass, i);
963 if (!PCM_REGISTERED(d) || d->clones == NULL)
965 PCM_ACQUIRE_QUICK(d);
966 val = snd_clone_gc(d->clones);
967 PCM_RELEASE_QUICK(d);
968 if (bootverbose != 0 || snd_verbose > 3)
969 device_printf(d->dev, "clone gc: pruned=%d\n",
976 SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc, CTLTYPE_INT | CTLFLAG_RW,
977 0, sizeof(int), sysctl_hw_snd_clone_gc, "I",
978 "global clone garbage collector");
982 pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
984 struct snddev_info *d;
988 device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load);
993 if (device_get_unit(dev) > PCMMAXUNIT) {
994 device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n",
995 device_get_unit(dev), PCMMAXUNIT);
997 "Use 'hw.snd.maxunit' tunable to raise the limit.\n");
1001 d = device_get_softc(dev);
1003 d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
1004 cv_init(&d->cv, device_get_nameunit(dev));
1005 PCM_ACQUIRE_QUICK(d);
1006 dsp_cdevinfo_init(d);
1009 * d->flags should be cleared by the allocator of the softc.
1010 * We cannot clear this field here because several devices set
1011 * this flag before calling pcm_register().
1016 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1017 "vpc", &i) != 0 || i != 0)
1018 d->flags |= SD_F_VPC;
1020 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1021 "bitperfect", &i) == 0 && i != 0)
1022 d->flags |= SD_F_BITPERFECT;
1024 d->devinfo = devinfo;
1031 d->pvchanformat = 0;
1033 d->rvchanformat = 0;
1037 * Create clone manager, disabled by default. Cloning will be
1038 * enabled during final stage of driver initialization through
1041 d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE,
1042 SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK |
1043 SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF |
1044 SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED);
1046 if (bootverbose != 0 || snd_verbose > 3) {
1048 "clone manager: deadline=%dms flags=0x%08x\n",
1049 snd_clone_getdeadline(d->clones),
1050 snd_clone_getflags(d->clones));
1053 CHN_INIT(d, channels.pcm);
1054 CHN_INIT(d, channels.pcm.busy);
1055 CHN_INIT(d, channels.pcm.opened);
1057 /* XXX This is incorrect, but lets play along for now. */
1058 if ((numplay == 0 || numrec == 0) && numplay != numrec)
1059 d->flags |= SD_F_SIMPLEX;
1061 sysctl_ctx_init(&d->play_sysctl_ctx);
1062 d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
1063 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
1064 CTLFLAG_RD, 0, "playback channels node");
1065 sysctl_ctx_init(&d->rec_sysctl_ctx);
1066 d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
1067 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
1068 CTLFLAG_RD, 0, "record channels node");
1069 /* XXX: an user should be able to set this with a control tool, the
1070 sysadmin then needs min+max sysctls for this */
1071 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
1072 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1073 OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size");
1074 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1075 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1076 "bitperfect", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1077 sysctl_dev_pcm_bitperfect, "I",
1078 "bit-perfect playback/recording (0=disable, 1=enable)");
1080 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1081 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1082 "clone_flags", CTLTYPE_UINT | CTLFLAG_RW, d, sizeof(d),
1083 sysctl_dev_pcm_clone_flags, "IU",
1085 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1086 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1087 "clone_deadline", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1088 sysctl_dev_pcm_clone_deadline, "I",
1089 "clone expiration deadline (ms)");
1090 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1091 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1092 "clone_gc", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1093 sysctl_dev_pcm_clone_gc, "I",
1094 "clone garbage collector");
1097 if (numplay > 0 || numrec > 0) {
1098 d->flags |= SD_F_AUTOVCHAN;
1102 if (d->flags & SD_F_EQ)
1103 feeder_eq_initsys(dev);
1105 sndstat_register(dev, d->status, sndstat_prepare_pcm);
1111 pcm_unregister(device_t dev)
1113 struct snddev_info *d;
1114 struct pcm_channel *ch;
1119 d = device_get_softc(dev);
1121 if (!PCM_ALIVE(d)) {
1122 device_printf(dev, "unregister: device not configured\n");
1126 if (sndstat_acquire(td) != 0) {
1127 device_printf(dev, "unregister: sndstat busy\n");
1134 if (d->inprog != 0) {
1135 device_printf(dev, "unregister: operation in progress\n");
1137 sndstat_release(td);
1144 CHN_FOREACH(ch, d, channels.pcm) {
1146 if (ch->refcount > 0) {
1148 "unregister: channel %s busy (pid %d)\n",
1151 PCM_RELEASE_QUICK(d);
1152 sndstat_release(td);
1158 if (d->clones != NULL) {
1159 if (snd_clone_busy(d->clones) != 0) {
1160 device_printf(dev, "unregister: clone busy\n");
1161 PCM_RELEASE_QUICK(d);
1162 sndstat_release(td);
1166 (void)snd_clone_disable(d->clones);
1171 if (mixer_uninit(dev) == EBUSY) {
1172 device_printf(dev, "unregister: mixer busy\n");
1174 if (d->clones != NULL)
1175 (void)snd_clone_enable(d->clones);
1178 sndstat_release(td);
1183 d->flags |= SD_F_DYING;
1184 d->flags &= ~SD_F_REGISTERED;
1188 * No lock being held, so this thing can be flushed without
1189 * stucking into devdrn oblivion.
1191 if (d->clones != NULL) {
1192 snd_clone_destroy(d->clones);
1196 if (d->play_sysctl_tree != NULL) {
1197 sysctl_ctx_free(&d->play_sysctl_ctx);
1198 d->play_sysctl_tree = NULL;
1200 if (d->rec_sysctl_tree != NULL) {
1201 sysctl_ctx_free(&d->rec_sysctl_ctx);
1202 d->rec_sysctl_tree = NULL;
1205 while (!CHN_EMPTY(d, channels.pcm))
1208 dsp_cdevinfo_flush(d);
1214 snd_mtxfree(d->lock);
1215 sndstat_unregister(dev);
1216 sndstat_release(td);
1218 if (snd_unit == device_get_unit(dev)) {
1220 * Reassign default unit to the next available dev, but
1221 * first, reset snd_unit to something ridiculous.
1224 for (i = 0; pcm_devclass != NULL &&
1225 i < devclass_get_maxunit(pcm_devclass); i++) {
1226 if (device_get_unit(dev) == i)
1228 d = devclass_get_softc(pcm_devclass, i);
1229 if (PCM_REGISTERED(d)) {
1239 /************************************************************************/
1242 * @brief Handle OSSv4 SNDCTL_SYSINFO ioctl.
1244 * @param si Pointer to oss_sysinfo struct where information about the
1245 * sound subsystem will be written/copied.
1247 * This routine returns information about the sound system, such as the
1248 * current OSS version, number of audio, MIDI, and mixer drivers, etc.
1249 * Also includes a bitmask showing which of the above types of devices
1253 * Calling threads must not hold any snddev_info or pcm_channel locks.
1255 * @author Ryan Beasley <ryanb@FreeBSD.org>
1258 sound_oss_sysinfo(oss_sysinfo *si)
1260 static char si_product[] = "FreeBSD native OSS ABI";
1261 static char si_version[] = __XSTRING(__FreeBSD_version);
1262 static char si_license[] = "BSD";
1263 static int intnbits = sizeof(int) * 8; /* Better suited as macro?
1264 Must pester a C guru. */
1266 struct snddev_info *d;
1267 struct pcm_channel *c;
1272 strlcpy(si->product, si_product, sizeof(si->product));
1273 strlcpy(si->version, si_version, sizeof(si->version));
1274 si->versionnum = SOUND_VERSION;
1275 strlcpy(si->license, si_license, sizeof(si->license));
1278 * Iterate over PCM devices and their channels, gathering up data
1279 * for the numaudios, ncards, and openedaudio fields.
1282 bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
1286 for (i = 0; pcm_devclass != NULL &&
1287 i < devclass_get_maxunit(pcm_devclass); i++) {
1288 d = devclass_get_softc(pcm_devclass, i);
1289 if (!PCM_REGISTERED(d))
1292 /* XXX Need Giant magic entry ??? */
1294 /* See note in function's docblock */
1295 PCM_UNLOCKASSERT(d);
1298 si->numaudios += d->devcount;
1301 CHN_FOREACH(c, d, channels.pcm) {
1302 CHN_UNLOCKASSERT(c);
1304 if (c->flags & CHN_F_BUSY)
1305 si->openedaudio[j / intnbits] |=
1306 (1 << (j % intnbits));
1313 si->numaudioengines = si->numaudios;
1315 si->numsynths = 0; /* OSSv4 docs: this field is obsolete */
1317 * @todo Collect num{midis,timers}.
1319 * Need access to sound/midi/midi.c::midistat_lock in order
1320 * to safely touch midi_devices and get a head count of, well,
1321 * MIDI devices. midistat_lock is a global static (i.e., local to
1322 * midi.c), but midi_devices is a regular global; should the mutex
1323 * be publicized, or is there another way to get this information?
1325 * NB: MIDI/sequencer stuff is currently on hold.
1329 si->nummixers = mixer_count;
1330 si->numcards = ncards;
1331 /* OSSv4 docs: Intended only for test apps; API doesn't
1332 really have much of a concept of cards. Shouldn't be
1333 used by applications. */
1336 * @todo Fill in "busy devices" fields.
1338 * si->openedmidi = " MIDI devices
1340 bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
1343 * Si->filler is a reserved array, but according to docs each
1344 * element should be set to -1.
1346 for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++)
1351 sound_oss_card_info(oss_card_info *si)
1353 struct snddev_info *d;
1358 for (i = 0; pcm_devclass != NULL &&
1359 i < devclass_get_maxunit(pcm_devclass); i++) {
1360 d = devclass_get_softc(pcm_devclass, i);
1361 if (!PCM_REGISTERED(d))
1364 if (ncards++ != si->card)
1367 PCM_UNLOCKASSERT(d);
1370 strlcpy(si->shortname, device_get_nameunit(d->dev),
1371 sizeof(si->shortname));
1372 strlcpy(si->longname, device_get_desc(d->dev),
1373 sizeof(si->longname));
1374 strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
1375 si->intr_count = si->ack_count = 0;
1384 /************************************************************************/
1387 sound_modevent(module_t mod, int type, void *data)
1391 return (midi_modevent(mod, type, data));
1397 pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
1401 ret = sndstat_acquire(curthread);
1404 if (pcmsg_unrhdr != NULL) {
1405 delete_unrhdr(pcmsg_unrhdr);
1406 pcmsg_unrhdr = NULL;
1417 DEV_MODULE(sound, sound_modevent, NULL);
1418 MODULE_VERSION(sound, SOUND_MODVER);