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 = -1;
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 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))
450 /* XXX: do we need a way to let the user change the default unit? */
451 SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit, CTLTYPE_INT | CTLFLAG_RW,
452 0, sizeof(int), sysctl_hw_snd_default_unit, "I", "default sound device");
455 sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS)
457 struct snddev_info *d;
460 v = snd_maxautovchans;
461 error = sysctl_handle_int(oidp, &v, 0, req);
462 if (error == 0 && req->newptr != NULL) {
465 if (v > SND_MAXVCHANS)
467 snd_maxautovchans = v;
468 for (i = 0; pcm_devclass != NULL &&
469 i < devclass_get_maxunit(pcm_devclass); i++) {
470 d = devclass_get_softc(pcm_devclass, i);
471 if (!PCM_REGISTERED(d))
473 PCM_ACQUIRE_QUICK(d);
474 pcm_setmaxautovchans(d, v);
475 PCM_RELEASE_QUICK(d);
480 SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans, CTLTYPE_INT | CTLFLAG_RW,
481 0, sizeof(int), sysctl_hw_snd_maxautovchans, "I", "maximum virtual channel");
484 pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo)
486 struct pcm_channel *ch;
487 int direction, err, rpnum, *pnum, max;
488 int udc, device, chan;
489 char *dirs, *devname, buf[CHN_NAMELEN];
493 KASSERT(num >= -1, ("invalid num=%d", num));
499 direction = PCMDIR_PLAY;
500 pnum = &d->playcount;
501 device = SND_DEV_DSPHW_PLAY;
504 case PCMDIR_PLAY_VIRTUAL:
506 direction = PCMDIR_PLAY;
507 pnum = &d->pvchancount;
508 device = SND_DEV_DSPHW_VPLAY;
513 direction = PCMDIR_REC;
515 device = SND_DEV_DSPHW_REC;
518 case PCMDIR_REC_VIRTUAL:
520 direction = PCMDIR_REC;
521 pnum = &d->rvchancount;
522 device = SND_DEV_DSPHW_VREC;
529 chan = (num == -1) ? 0 : num;
531 if (*pnum >= max || chan >= max)
536 CHN_FOREACH(ch, d, channels.pcm) {
537 if (CHN_DEV(ch) != device)
539 if (chan == CHN_CHAN(ch)) {
541 device_printf(d->dev,
542 "channel num=%d allocated!\n", chan);
547 device_printf(d->dev,
548 "chan=%d > %d\n", chan, max);
555 if (*pnum != rpnum) {
556 device_printf(d->dev,
557 "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n",
558 __func__, dirs, *pnum, rpnum);
562 udc = snd_mkunit(device_get_unit(d->dev), device, chan);
563 devname = dsp_unit2name(buf, sizeof(buf), udc);
565 if (devname == NULL) {
566 device_printf(d->dev,
567 "Failed to query device name udc=0x%08x\n", udc);
572 ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
573 ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
576 strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm));
577 ch->parentsnddev = d;
578 ch->parentchannel = parent;
580 ch->trigger = PCMTRIG_STOP;
581 snprintf(ch->name, sizeof(ch->name), "%s:%s:%s",
582 device_get_nameunit(ch->dev), dirs, devname);
584 err = chn_init(ch, devinfo, dir, direction);
587 device_printf(d->dev, "chn_init(%s) failed: err = %d\n",
589 kobj_delete(ch->methods, M_DEVBUF);
598 pcm_chn_destroy(struct pcm_channel *ch)
600 struct snddev_info *d;
603 d = ch->parentsnddev;
608 device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n",
613 kobj_delete(ch->methods, M_DEVBUF);
620 pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch)
624 KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY ||
625 ch->direction == PCMDIR_REC), ("Invalid pcm channel"));
627 CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm);
629 switch (CHN_DEV(ch)) {
630 case SND_DEV_DSPHW_PLAY:
633 case SND_DEV_DSPHW_VPLAY:
636 case SND_DEV_DSPHW_REC:
639 case SND_DEV_DSPHW_VREC:
652 pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch)
654 struct pcm_channel *tmp;
661 CHN_FOREACH(tmp, d, channels.pcm) {
669 CHN_REMOVE(d, ch, channels.pcm);
671 switch (CHN_DEV(ch)) {
672 case SND_DEV_DSPHW_PLAY:
675 case SND_DEV_DSPHW_VPLAY:
678 case SND_DEV_DSPHW_REC:
681 case SND_DEV_DSPHW_VREC:
694 pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
696 struct snddev_info *d = device_get_softc(dev);
697 struct pcm_channel *ch;
703 ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo);
705 device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n",
706 cls->name, dir, devinfo);
711 err = pcm_chn_add(d, ch);
714 device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n",
723 pcm_killchan(device_t dev)
725 struct snddev_info *d = device_get_softc(dev);
726 struct pcm_channel *ch;
731 ch = CHN_FIRST(d, channels.pcm);
734 error = pcm_chn_remove(d, ch);
738 return (pcm_chn_destroy(ch));
742 pcm_best_unit(int old)
744 struct snddev_info *d;
745 int i, best, bestprio, prio;
749 for (i = 0; pcm_devclass != NULL &&
750 i < devclass_get_maxunit(pcm_devclass); i++) {
751 d = devclass_get_softc(pcm_devclass, i);
752 if (!PCM_REGISTERED(d))
755 if (d->playcount == 0)
757 if (d->reccount == 0)
759 if (prio > bestprio || (prio == bestprio && i == old)) {
768 pcm_setstatus(device_t dev, char *str)
770 struct snddev_info *d = device_get_softc(dev);
774 if (d->playcount == 0 || d->reccount == 0)
775 d->flags |= SD_F_SIMPLEX;
777 if ((d->playcount > 0 || d->reccount > 0) &&
778 !(d->flags & SD_F_AUTOVCHAN)) {
779 d->flags |= SD_F_AUTOVCHAN;
783 pcm_setmaxautovchans(d, snd_maxautovchans);
785 strlcpy(d->status, str, SND_STATUSLEN);
789 /* Last stage, enable cloning. */
790 if (d->clones != NULL)
791 (void)snd_clone_enable(d->clones);
793 /* Done, we're ready.. */
794 d->flags |= SD_F_REGISTERED;
800 if (snd_unit_auto < 0)
801 snd_unit_auto = (snd_unit < 0) ? 1 : 0;
802 if (snd_unit < 0 || snd_unit_auto > 1)
803 snd_unit = device_get_unit(dev);
804 else if (snd_unit_auto == 1)
805 snd_unit = pcm_best_unit(snd_unit);
811 pcm_getflags(device_t dev)
813 struct snddev_info *d = device_get_softc(dev);
819 pcm_setflags(device_t dev, uint32_t val)
821 struct snddev_info *d = device_get_softc(dev);
827 pcm_getdevinfo(device_t dev)
829 struct snddev_info *d = device_get_softc(dev);
835 pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
837 struct snddev_info *d = device_get_softc(dev);
841 if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
843 RANGE(sz, minbufsz, maxbufsz);
845 device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
852 device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
865 sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
867 struct snddev_info *d;
871 if (!PCM_REGISTERED(d))
876 val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
880 err = sysctl_handle_int(oidp, &val, 0, req);
882 if (err == 0 && req->newptr != NULL) {
883 if (!(val == 0 || val == 1)) {
884 PCM_RELEASE_QUICK(d);
890 d->flags &= ~SD_F_BITPERFECT;
891 d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
896 PCM_RELEASE_QUICK(d);
903 sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS)
905 struct snddev_info *d;
910 if (!PCM_REGISTERED(d) || d->clones == NULL)
913 PCM_ACQUIRE_QUICK(d);
915 flags = snd_clone_getflags(d->clones);
916 err = sysctl_handle_int(oidp, &flags, 0, req);
918 if (err == 0 && req->newptr != NULL) {
919 if (flags & ~SND_CLONE_MASK)
922 (void)snd_clone_setflags(d->clones, flags);
925 PCM_RELEASE_QUICK(d);
931 sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS)
933 struct snddev_info *d;
937 if (!PCM_REGISTERED(d) || d->clones == NULL)
940 PCM_ACQUIRE_QUICK(d);
942 deadline = snd_clone_getdeadline(d->clones);
943 err = sysctl_handle_int(oidp, &deadline, 0, req);
945 if (err == 0 && req->newptr != NULL) {
949 (void)snd_clone_setdeadline(d->clones, deadline);
952 PCM_RELEASE_QUICK(d);
958 sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS)
960 struct snddev_info *d;
964 if (!PCM_REGISTERED(d) || d->clones == NULL)
968 err = sysctl_handle_int(oidp, &val, 0, req);
970 if (err == 0 && req->newptr != NULL && val != 0) {
971 PCM_ACQUIRE_QUICK(d);
972 val = snd_clone_gc(d->clones);
973 PCM_RELEASE_QUICK(d);
974 if (bootverbose != 0 || snd_verbose > 3)
975 device_printf(d->dev, "clone gc: pruned=%d\n", val);
982 sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS)
984 struct snddev_info *d;
988 err = sysctl_handle_int(oidp, &val, 0, req);
990 if (err == 0 && req->newptr != NULL && val != 0) {
991 for (i = 0; pcm_devclass != NULL &&
992 i < devclass_get_maxunit(pcm_devclass); i++) {
993 d = devclass_get_softc(pcm_devclass, i);
994 if (!PCM_REGISTERED(d) || d->clones == NULL)
996 PCM_ACQUIRE_QUICK(d);
997 val = snd_clone_gc(d->clones);
998 PCM_RELEASE_QUICK(d);
999 if (bootverbose != 0 || snd_verbose > 3)
1000 device_printf(d->dev, "clone gc: pruned=%d\n",
1007 SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc, CTLTYPE_INT | CTLFLAG_RW,
1008 0, sizeof(int), sysctl_hw_snd_clone_gc, "I",
1009 "global clone garbage collector");
1013 pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
1015 struct snddev_info *d;
1018 if (pcm_veto_load) {
1019 device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load);
1024 if (device_get_unit(dev) > PCMMAXUNIT) {
1025 device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n",
1026 device_get_unit(dev), PCMMAXUNIT);
1028 "Use 'hw.snd.maxunit' tunable to raise the limit.\n");
1032 d = device_get_softc(dev);
1034 d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
1035 cv_init(&d->cv, device_get_nameunit(dev));
1036 PCM_ACQUIRE_QUICK(d);
1037 dsp_cdevinfo_init(d);
1040 * d->flags should be cleared by the allocator of the softc.
1041 * We cannot clear this field here because several devices set
1042 * this flag before calling pcm_register().
1047 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1048 "vpc", &i) != 0 || i != 0)
1049 d->flags |= SD_F_VPC;
1051 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1052 "bitperfect", &i) == 0 && i != 0)
1053 d->flags |= SD_F_BITPERFECT;
1055 d->devinfo = devinfo;
1062 d->pvchanformat = 0;
1064 d->rvchanformat = 0;
1068 * Create clone manager, disabled by default. Cloning will be
1069 * enabled during final stage of driver initialization through
1072 d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE,
1073 SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK |
1074 SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF |
1075 SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED);
1077 CHN_INIT(d, channels.pcm);
1078 CHN_INIT(d, channels.pcm.busy);
1079 CHN_INIT(d, channels.pcm.opened);
1081 /* XXX This is incorrect, but lets play along for now. */
1082 if ((numplay == 0 || numrec == 0) && numplay != numrec)
1083 d->flags |= SD_F_SIMPLEX;
1085 sysctl_ctx_init(&d->play_sysctl_ctx);
1086 d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
1087 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
1088 CTLFLAG_RD, 0, "playback channels node");
1089 sysctl_ctx_init(&d->rec_sysctl_ctx);
1090 d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
1091 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
1092 CTLFLAG_RD, 0, "record channels node");
1093 /* XXX: an user should be able to set this with a control tool, the
1094 sysadmin then needs min+max sysctls for this */
1095 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
1096 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1097 OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size");
1098 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1099 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1100 "bitperfect", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1101 sysctl_dev_pcm_bitperfect, "I",
1102 "bit-perfect playback/recording (0=disable, 1=enable)");
1104 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1105 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1106 "clone_flags", CTLTYPE_UINT | CTLFLAG_RW, d, sizeof(d),
1107 sysctl_dev_pcm_clone_flags, "IU",
1109 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1110 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1111 "clone_deadline", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1112 sysctl_dev_pcm_clone_deadline, "I",
1113 "clone expiration deadline (ms)");
1114 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1115 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1116 "clone_gc", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1117 sysctl_dev_pcm_clone_gc, "I",
1118 "clone garbage collector");
1121 if (numplay > 0 || numrec > 0) {
1122 d->flags |= SD_F_AUTOVCHAN;
1126 if (d->flags & SD_F_EQ)
1127 feeder_eq_initsys(dev);
1129 sndstat_register(dev, d->status, sndstat_prepare_pcm);
1135 pcm_unregister(device_t dev)
1137 struct snddev_info *d;
1138 struct pcm_channel *ch;
1142 d = device_get_softc(dev);
1144 if (!PCM_ALIVE(d)) {
1145 device_printf(dev, "unregister: device not configured\n");
1149 if (sndstat_acquire(td) != 0) {
1150 device_printf(dev, "unregister: sndstat busy\n");
1157 if (d->inprog != 0) {
1158 device_printf(dev, "unregister: operation in progress\n");
1160 sndstat_release(td);
1167 CHN_FOREACH(ch, d, channels.pcm) {
1169 if (ch->refcount > 0) {
1171 "unregister: channel %s busy (pid %d)\n",
1174 PCM_RELEASE_QUICK(d);
1175 sndstat_release(td);
1181 if (d->clones != NULL) {
1182 if (snd_clone_busy(d->clones) != 0) {
1183 device_printf(dev, "unregister: clone busy\n");
1184 PCM_RELEASE_QUICK(d);
1185 sndstat_release(td);
1189 (void)snd_clone_disable(d->clones);
1194 if (mixer_uninit(dev) == EBUSY) {
1195 device_printf(dev, "unregister: mixer busy\n");
1197 if (d->clones != NULL)
1198 (void)snd_clone_enable(d->clones);
1201 sndstat_release(td);
1206 d->flags |= SD_F_DYING;
1207 d->flags &= ~SD_F_REGISTERED;
1211 * No lock being held, so this thing can be flushed without
1212 * stucking into devdrn oblivion.
1214 if (d->clones != NULL) {
1215 snd_clone_destroy(d->clones);
1219 if (d->play_sysctl_tree != NULL) {
1220 sysctl_ctx_free(&d->play_sysctl_ctx);
1221 d->play_sysctl_tree = NULL;
1223 if (d->rec_sysctl_tree != NULL) {
1224 sysctl_ctx_free(&d->rec_sysctl_ctx);
1225 d->rec_sysctl_tree = NULL;
1228 while (!CHN_EMPTY(d, channels.pcm))
1231 dsp_cdevinfo_flush(d);
1237 snd_mtxfree(d->lock);
1238 sndstat_unregister(dev);
1239 sndstat_release(td);
1241 if (snd_unit == device_get_unit(dev)) {
1242 snd_unit = pcm_best_unit(-1);
1243 if (snd_unit_auto == 0)
1250 /************************************************************************/
1253 * @brief Handle OSSv4 SNDCTL_SYSINFO ioctl.
1255 * @param si Pointer to oss_sysinfo struct where information about the
1256 * sound subsystem will be written/copied.
1258 * This routine returns information about the sound system, such as the
1259 * current OSS version, number of audio, MIDI, and mixer drivers, etc.
1260 * Also includes a bitmask showing which of the above types of devices
1264 * Calling threads must not hold any snddev_info or pcm_channel locks.
1266 * @author Ryan Beasley <ryanb@FreeBSD.org>
1269 sound_oss_sysinfo(oss_sysinfo *si)
1271 static char si_product[] = "FreeBSD native OSS ABI";
1272 static char si_version[] = __XSTRING(__FreeBSD_version);
1273 static char si_license[] = "BSD";
1274 static int intnbits = sizeof(int) * 8; /* Better suited as macro?
1275 Must pester a C guru. */
1277 struct snddev_info *d;
1278 struct pcm_channel *c;
1283 strlcpy(si->product, si_product, sizeof(si->product));
1284 strlcpy(si->version, si_version, sizeof(si->version));
1285 si->versionnum = SOUND_VERSION;
1286 strlcpy(si->license, si_license, sizeof(si->license));
1289 * Iterate over PCM devices and their channels, gathering up data
1290 * for the numaudios, ncards, and openedaudio fields.
1293 bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
1297 for (i = 0; pcm_devclass != NULL &&
1298 i < devclass_get_maxunit(pcm_devclass); i++) {
1299 d = devclass_get_softc(pcm_devclass, i);
1300 if (!PCM_REGISTERED(d))
1303 /* XXX Need Giant magic entry ??? */
1305 /* See note in function's docblock */
1306 PCM_UNLOCKASSERT(d);
1309 si->numaudios += d->devcount;
1312 CHN_FOREACH(c, d, channels.pcm) {
1313 CHN_UNLOCKASSERT(c);
1315 if (c->flags & CHN_F_BUSY)
1316 si->openedaudio[j / intnbits] |=
1317 (1 << (j % intnbits));
1324 si->numaudioengines = si->numaudios;
1326 si->numsynths = 0; /* OSSv4 docs: this field is obsolete */
1328 * @todo Collect num{midis,timers}.
1330 * Need access to sound/midi/midi.c::midistat_lock in order
1331 * to safely touch midi_devices and get a head count of, well,
1332 * MIDI devices. midistat_lock is a global static (i.e., local to
1333 * midi.c), but midi_devices is a regular global; should the mutex
1334 * be publicized, or is there another way to get this information?
1336 * NB: MIDI/sequencer stuff is currently on hold.
1340 si->nummixers = mixer_count;
1341 si->numcards = ncards;
1342 /* OSSv4 docs: Intended only for test apps; API doesn't
1343 really have much of a concept of cards. Shouldn't be
1344 used by applications. */
1347 * @todo Fill in "busy devices" fields.
1349 * si->openedmidi = " MIDI devices
1351 bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
1354 * Si->filler is a reserved array, but according to docs each
1355 * element should be set to -1.
1357 for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++)
1362 sound_oss_card_info(oss_card_info *si)
1364 struct snddev_info *d;
1369 for (i = 0; pcm_devclass != NULL &&
1370 i < devclass_get_maxunit(pcm_devclass); i++) {
1371 d = devclass_get_softc(pcm_devclass, i);
1372 if (!PCM_REGISTERED(d))
1375 if (ncards++ != si->card)
1378 PCM_UNLOCKASSERT(d);
1381 strlcpy(si->shortname, device_get_nameunit(d->dev),
1382 sizeof(si->shortname));
1383 strlcpy(si->longname, device_get_desc(d->dev),
1384 sizeof(si->longname));
1385 strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
1386 si->intr_count = si->ack_count = 0;
1395 /************************************************************************/
1398 sound_modevent(module_t mod, int type, void *data)
1402 return (midi_modevent(mod, type, data));
1408 pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
1411 ret = sndstat_acquire(curthread);
1414 if (pcmsg_unrhdr != NULL) {
1415 delete_unrhdr(pcmsg_unrhdr);
1416 pcmsg_unrhdr = NULL;
1429 DEV_MODULE(sound, sound_modevent, NULL);
1430 MODULE_VERSION(sound, SOUND_MODVER);