2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Copyright (c) 2001 Cameron Grant <cg@FreeBSD.org>
6 * Copyright (c) 2020 The FreeBSD Foundation
9 * Portions of this software were developed by Ka Ho Ng
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #ifdef HAVE_KERNEL_OPTION_HEADERS
38 #include <sys/param.h>
40 #include <sys/malloc.h>
44 #ifdef COMPAT_FREEBSD32
45 #include <sys/sysent.h>
48 #include <dev/sound/pcm/sound.h>
49 #include <dev/sound/pcm/pcm.h>
50 #include <dev/sound/version.h>
53 SND_DECLARE_FILE("$FreeBSD$");
55 #define SS_TYPE_MODULE 0
57 #define SS_TYPE_MIDI 2
58 #define SS_TYPE_SEQUENCER 3
60 static d_open_t sndstat_open;
61 static void sndstat_close(void *);
62 static d_read_t sndstat_read;
63 static d_write_t sndstat_write;
64 static d_ioctl_t sndstat_ioctl;
66 static struct cdevsw sndstat_cdevsw = {
67 .d_version = D_VERSION,
68 .d_open = sndstat_open,
69 .d_read = sndstat_read,
70 .d_write = sndstat_write,
71 .d_ioctl = sndstat_ioctl,
73 .d_flags = D_TRACKCLOSE,
76 struct sndstat_entry {
77 TAILQ_ENTRY(sndstat_entry) link;
80 sndstat_handler handler;
84 struct sndstat_userdev {
85 TAILQ_ENTRY(sndstat_userdev) link;
98 } info_play, info_rec;
99 nvlist_t *provider_nvl;
102 struct sndstat_file {
103 TAILQ_ENTRY(sndstat_file) entry;
106 void *devs_nvlbuf; /* (l) */
107 size_t devs_nbytes; /* (l) */
108 TAILQ_HEAD(, sndstat_userdev) userdev_list; /* (l) */
114 static struct sx sndstat_lock;
115 static struct cdev *sndstat_dev;
117 #define SNDSTAT_LOCK() sx_xlock(&sndstat_lock)
118 #define SNDSTAT_UNLOCK() sx_xunlock(&sndstat_lock)
120 static TAILQ_HEAD(, sndstat_entry) sndstat_devlist = TAILQ_HEAD_INITIALIZER(sndstat_devlist);
121 static TAILQ_HEAD(, sndstat_file) sndstat_filelist = TAILQ_HEAD_INITIALIZER(sndstat_filelist);
125 static int sndstat_prepare(struct sndstat_file *);
126 static struct sndstat_userdev *
127 sndstat_line2userdev(struct sndstat_file *, const char *, int);
130 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
134 verbose = snd_verbose;
135 error = sysctl_handle_int(oidp, &verbose, 0, req);
136 if (error == 0 && req->newptr != NULL) {
137 if (verbose < 0 || verbose > 4)
140 snd_verbose = verbose;
144 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose,
145 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int),
146 sysctl_hw_sndverbose, "I",
150 sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
152 struct sndstat_file *pf;
154 pf = malloc(sizeof(*pf), M_DEVBUF, M_WAITOK | M_ZERO);
156 if (sbuf_new(&pf->sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
162 TAILQ_INIT(&pf->userdev_list);
163 sx_init(&pf->lock, "sndstat_file");
166 TAILQ_INSERT_TAIL(&sndstat_filelist, pf, entry);
169 devfs_set_cdevpriv(pf, &sndstat_close);
175 * Should only be called either when:
180 sndstat_remove_all_userdevs(struct sndstat_file *pf)
182 struct sndstat_userdev *ud;
185 sx_xlocked(&pf->lock), ("%s: Called without pf->lock", __func__));
186 while ((ud = TAILQ_FIRST(&pf->userdev_list)) != NULL) {
187 TAILQ_REMOVE(&pf->userdev_list, ud, link);
188 free(ud->provider, M_DEVBUF);
189 free(ud->desc, M_DEVBUF);
190 free(ud->devnode, M_DEVBUF);
191 free(ud->nameunit, M_DEVBUF);
192 nvlist_destroy(ud->provider_nvl);
198 sndstat_close(void *sndstat_file)
200 struct sndstat_file *pf = (struct sndstat_file *)sndstat_file;
203 sbuf_delete(&pf->sbuf);
204 TAILQ_REMOVE(&sndstat_filelist, pf, entry);
207 free(pf->devs_nvlbuf, M_NVLIST);
209 sndstat_remove_all_userdevs(pf);
210 sx_xunlock(&pf->lock);
211 sx_destroy(&pf->lock);
217 sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
219 struct sndstat_file *pf;
223 err = devfs_get_cdevpriv((void **)&pf);
227 /* skip zero-length reads */
228 if (buf->uio_resid == 0)
232 if (pf->out_offset != 0) {
233 /* don't allow both reading and writing */
236 } else if (pf->in_offset == 0) {
237 err = sndstat_prepare(pf);
243 len = sbuf_len(&pf->sbuf) - pf->in_offset;
244 if (len > buf->uio_resid)
245 len = buf->uio_resid;
247 err = uiomove(sbuf_data(&pf->sbuf) + pf->in_offset, len, buf);
248 pf->in_offset += len;
255 sndstat_write(struct cdev *i_dev, struct uio *buf, int flag)
257 struct sndstat_file *pf;
262 err = devfs_get_cdevpriv((void **)&pf);
266 /* skip zero-length writes */
267 if (buf->uio_resid == 0)
270 /* don't allow writing more than 64Kbytes */
271 if (buf->uio_resid > 65536)
275 if (pf->in_offset != 0) {
276 /* don't allow both reading and writing */
279 /* only remember the last write - allows for updates */
281 sndstat_remove_all_userdevs(pf);
282 sx_xunlock(&pf->lock);
286 if (len > buf->uio_resid)
287 len = buf->uio_resid;
289 err = uiomove(temp, len, buf);
295 if (sbuf_bcat(&pf->sbuf, temp, len) < 0) {
300 sbuf_finish(&pf->sbuf);
305 str = sbuf_data(&pf->sbuf);
306 while ((line = strsep(&str, "\n")) != NULL) {
307 struct sndstat_userdev *ud;
309 ud = sndstat_line2userdev(pf, line, strlen(line));
314 TAILQ_INSERT_TAIL(&pf->userdev_list, ud, link);
315 sx_xunlock(&pf->lock);
318 pf->out_offset = sbuf_len(&pf->sbuf);
322 sbuf_clear(&pf->sbuf);
329 sndstat_get_caps(struct snddev_info *d, bool play, uint32_t *min_rate,
330 uint32_t *max_rate, uint32_t *fmts, uint32_t *minchn, uint32_t *maxchn)
332 struct pcm_channel *c;
333 unsigned int encoding;
336 dir = play ? PCMDIR_PLAY : PCMDIR_REC;
338 if (play && d->pvchancount > 0) {
339 *min_rate = *max_rate = d->pvchanrate;
340 *fmts = AFMT_ENCODING(d->pvchanformat);
341 *minchn = *maxchn = AFMT_CHANNEL(d->pvchanformat);
343 } else if (!play && d->rvchancount > 0) {
344 *min_rate = *max_rate = d->rvchanrate;
345 *fmts = AFMT_ENCODING(d->rvchanformat);
346 *minchn = *maxchn = AFMT_CHANNEL(d->rvchanformat);
350 *min_rate = UINT32_MAX;
352 *minchn = UINT32_MAX;
355 CHN_FOREACH(c, d, channels.pcm) {
356 struct pcmchan_caps *caps;
359 if (c->direction != dir || (c->flags & CHN_F_VIRTUAL) != 0)
363 caps = chn_getcaps(c);
364 *min_rate = min(caps->minspeed, *min_rate);
365 *max_rate = max(caps->maxspeed, *max_rate);
366 for (i = 0; caps->fmtlist[i]; i++) {
367 encoding |= AFMT_ENCODING(caps->fmtlist[i]);
368 *minchn = min(AFMT_CHANNEL(encoding), *minchn);
369 *maxchn = max(AFMT_CHANNEL(encoding), *maxchn);
373 if (*min_rate == UINT32_MAX)
375 if (*minchn == UINT32_MAX)
380 sndstat_create_diinfo_nv(uint32_t min_rate, uint32_t max_rate, uint32_t formats,
381 uint32_t min_chn, uint32_t max_chn)
385 nv = nvlist_create(0);
388 nvlist_add_number(nv, SNDST_DSPS_INFO_MIN_RATE, min_rate);
389 nvlist_add_number(nv, SNDST_DSPS_INFO_MAX_RATE, max_rate);
390 nvlist_add_number(nv, SNDST_DSPS_INFO_FORMATS, formats);
391 nvlist_add_number(nv, SNDST_DSPS_INFO_MIN_CHN, min_chn);
392 nvlist_add_number(nv, SNDST_DSPS_INFO_MAX_CHN, max_chn);
397 sndstat_build_sound4_nvlist(struct snddev_info *d, nvlist_t **dip)
399 uint32_t maxrate, minrate, fmts, minchn, maxchn;
400 nvlist_t *di = NULL, *sound4di = NULL, *diinfo = NULL;
403 di = nvlist_create(0);
408 sound4di = nvlist_create(0);
409 if (sound4di == NULL) {
414 nvlist_add_bool(di, SNDST_DSPS_FROM_USER, false);
415 nvlist_add_stringf(di, SNDST_DSPS_NAMEUNIT, "%s",
416 device_get_nameunit(d->dev));
417 nvlist_add_stringf(di, SNDST_DSPS_DEVNODE, "dsp%d",
418 device_get_unit(d->dev));
420 di, SNDST_DSPS_DESC, device_get_desc(d->dev));
422 PCM_ACQUIRE_QUICK(d);
423 nvlist_add_number(di, SNDST_DSPS_PCHAN, d->playcount);
424 nvlist_add_number(di, SNDST_DSPS_RCHAN, d->reccount);
425 if (d->playcount > 0) {
426 sndstat_get_caps(d, true, &minrate, &maxrate, &fmts, &minchn,
428 nvlist_add_number(di, "pminrate", minrate);
429 nvlist_add_number(di, "pmaxrate", maxrate);
430 nvlist_add_number(di, "pfmts", fmts);
431 diinfo = sndstat_create_diinfo_nv(minrate, maxrate, fmts,
434 nvlist_set_error(di, ENOMEM);
436 nvlist_move_nvlist(di, SNDST_DSPS_INFO_PLAY, diinfo);
438 if (d->reccount > 0) {
439 sndstat_get_caps(d, false, &minrate, &maxrate, &fmts, &minchn,
441 nvlist_add_number(di, "rminrate", minrate);
442 nvlist_add_number(di, "rmaxrate", maxrate);
443 nvlist_add_number(di, "rfmts", fmts);
444 diinfo = sndstat_create_diinfo_nv(minrate, maxrate, fmts,
447 nvlist_set_error(di, ENOMEM);
449 nvlist_move_nvlist(di, SNDST_DSPS_INFO_REC, diinfo);
452 nvlist_add_number(sound4di, SNDST_DSPS_SOUND4_UNIT,
453 device_get_unit(d->dev)); // XXX: I want signed integer here
455 sound4di, SNDST_DSPS_SOUND4_BITPERFECT, d->flags & SD_F_BITPERFECT);
456 nvlist_add_number(sound4di, SNDST_DSPS_SOUND4_PVCHAN, d->pvchancount);
457 nvlist_add_number(sound4di, SNDST_DSPS_SOUND4_RVCHAN, d->rvchancount);
458 nvlist_move_nvlist(di, SNDST_DSPS_PROVIDER_INFO, sound4di);
460 PCM_RELEASE_QUICK(d);
461 nvlist_add_string(di, SNDST_DSPS_PROVIDER, SNDST_DSPS_SOUND4_PROVIDER);
463 err = nvlist_error(di);
471 nvlist_destroy(sound4di);
478 sndstat_build_userland_nvlist(struct sndstat_userdev *ud, nvlist_t **dip)
480 nvlist_t *di, *diinfo;
483 di = nvlist_create(0);
489 nvlist_add_bool(di, SNDST_DSPS_FROM_USER, true);
490 nvlist_add_number(di, SNDST_DSPS_PCHAN, ud->pchan);
491 nvlist_add_number(di, SNDST_DSPS_RCHAN, ud->rchan);
492 nvlist_add_string(di, SNDST_DSPS_NAMEUNIT, ud->nameunit);
494 di, SNDST_DSPS_DEVNODE, ud->devnode);
495 nvlist_add_string(di, SNDST_DSPS_DESC, ud->desc);
496 if (ud->pchan != 0) {
497 nvlist_add_number(di, "pminrate",
498 ud->info_play.min_rate);
499 nvlist_add_number(di, "pmaxrate",
500 ud->info_play.max_rate);
501 nvlist_add_number(di, "pfmts",
502 ud->info_play.formats);
503 diinfo = sndstat_create_diinfo_nv(ud->info_play.min_rate,
504 ud->info_play.max_rate, ud->info_play.formats,
505 ud->info_play.min_chn, ud->info_play.max_chn);
507 nvlist_set_error(di, ENOMEM);
509 nvlist_move_nvlist(di, SNDST_DSPS_INFO_PLAY, diinfo);
511 if (ud->rchan != 0) {
512 nvlist_add_number(di, "rminrate",
513 ud->info_rec.min_rate);
514 nvlist_add_number(di, "rmaxrate",
515 ud->info_rec.max_rate);
516 nvlist_add_number(di, "rfmts",
517 ud->info_rec.formats);
518 diinfo = sndstat_create_diinfo_nv(ud->info_rec.min_rate,
519 ud->info_rec.max_rate, ud->info_rec.formats,
520 ud->info_rec.min_chn, ud->info_rec.max_chn);
522 nvlist_set_error(di, ENOMEM);
524 nvlist_move_nvlist(di, SNDST_DSPS_INFO_REC, diinfo);
526 nvlist_add_string(di, SNDST_DSPS_PROVIDER,
527 (ud->provider != NULL) ? ud->provider : "");
528 if (ud->provider_nvl != NULL)
530 di, SNDST_DSPS_PROVIDER_INFO, ud->provider_nvl);
532 err = nvlist_error(di);
545 * Should only be called with the following locks held:
549 sndstat_create_devs_nvlist(nvlist_t **nvlp)
553 struct sndstat_entry *ent;
554 struct sndstat_file *pf;
556 nvl = nvlist_create(0);
560 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
561 struct snddev_info *d;
564 if (ent->dev == NULL)
566 d = device_get_softc(ent->dev);
567 if (!PCM_REGISTERED(d))
570 err = sndstat_build_sound4_nvlist(d, &di);
574 nvlist_append_nvlist_array(nvl, SNDST_DSPS, di);
576 err = nvlist_error(nvl);
581 TAILQ_FOREACH(pf, &sndstat_filelist, entry) {
582 struct sndstat_userdev *ud;
586 TAILQ_FOREACH(ud, &pf->userdev_list, link) {
589 err = sndstat_build_userland_nvlist(ud, &di);
591 sx_xunlock(&pf->lock);
594 nvlist_append_nvlist_array(nvl, SNDST_DSPS, di);
597 err = nvlist_error(nvl);
599 sx_xunlock(&pf->lock);
604 sx_xunlock(&pf->lock);
616 sndstat_refresh_devs(struct sndstat_file *pf)
619 free(pf->devs_nvlbuf, M_NVLIST);
620 pf->devs_nvlbuf = NULL;
622 sx_unlock(&pf->lock);
628 sndstat_get_devs(struct sndstat_file *pf, caddr_t data)
631 struct sndstioc_nv_arg *arg = (struct sndstioc_nv_arg *)data;
636 if (pf->devs_nvlbuf == NULL) {
642 sx_xunlock(&pf->lock);
644 err = sndstat_create_devs_nvlist(&nvl);
652 nvlbuf = nvlist_pack(nvl, &nbytes);
653 err = nvlist_error(nvl);
655 if (nvlbuf == NULL || err != 0) {
657 sx_xunlock(&pf->lock);
663 free(pf->devs_nvlbuf, M_NVLIST);
664 pf->devs_nvlbuf = nvlbuf;
665 pf->devs_nbytes = nbytes;
671 arg->nbytes = pf->devs_nbytes;
675 if (arg->nbytes < pf->devs_nbytes) {
681 err = copyout(pf->devs_nvlbuf, arg->buf, pf->devs_nbytes);
685 arg->nbytes = pf->devs_nbytes;
687 free(pf->devs_nvlbuf, M_NVLIST);
688 pf->devs_nvlbuf = NULL;
692 sx_unlock(&pf->lock);
697 sndstat_unpack_user_nvlbuf(const void *unvlbuf, size_t nbytes, nvlist_t **nvl)
702 nvlbuf = malloc(nbytes, M_DEVBUF, M_WAITOK);
703 err = copyin(unvlbuf, nvlbuf, nbytes);
705 free(nvlbuf, M_DEVBUF);
708 *nvl = nvlist_unpack(nvlbuf, nbytes, 0);
709 free(nvlbuf, M_DEVBUF);
718 sndstat_diinfo_is_sane(const nvlist_t *diinfo)
720 if (!(nvlist_exists_number(diinfo, SNDST_DSPS_INFO_MIN_RATE) &&
721 nvlist_exists_number(diinfo, SNDST_DSPS_INFO_MAX_RATE) &&
722 nvlist_exists_number(diinfo, SNDST_DSPS_INFO_FORMATS) &&
723 nvlist_exists_number(diinfo, SNDST_DSPS_INFO_MIN_CHN) &&
724 nvlist_exists_number(diinfo, SNDST_DSPS_INFO_MAX_CHN)))
730 sndstat_dsp_nvlist_is_sane(const nvlist_t *nvlist)
732 if (!(nvlist_exists_string(nvlist, SNDST_DSPS_DEVNODE) &&
733 nvlist_exists_string(nvlist, SNDST_DSPS_DESC) &&
734 nvlist_exists_number(nvlist, SNDST_DSPS_PCHAN) &&
735 nvlist_exists_number(nvlist, SNDST_DSPS_RCHAN)))
738 if (nvlist_get_number(nvlist, SNDST_DSPS_PCHAN) > 0) {
739 if (nvlist_exists_nvlist(nvlist, SNDST_DSPS_INFO_PLAY)) {
740 if (!sndstat_diinfo_is_sane(nvlist_get_nvlist(nvlist,
741 SNDST_DSPS_INFO_PLAY)))
743 } else if (!(nvlist_exists_number(nvlist, "pminrate") &&
744 nvlist_exists_number(nvlist, "pmaxrate") &&
745 nvlist_exists_number(nvlist, "pfmts")))
749 if (nvlist_get_number(nvlist, SNDST_DSPS_RCHAN) > 0) {
750 if (nvlist_exists_nvlist(nvlist, SNDST_DSPS_INFO_REC)) {
751 if (!sndstat_diinfo_is_sane(nvlist_get_nvlist(nvlist,
752 SNDST_DSPS_INFO_REC)))
754 } else if (!(nvlist_exists_number(nvlist, "rminrate") &&
755 nvlist_exists_number(nvlist, "rmaxrate") &&
756 nvlist_exists_number(nvlist, "rfmts")))
765 sndstat_get_diinfo_nv(const nvlist_t *nv, uint32_t *min_rate,
766 uint32_t *max_rate, uint32_t *formats, uint32_t *min_chn,
769 *min_rate = nvlist_get_number(nv, SNDST_DSPS_INFO_MIN_RATE);
770 *max_rate = nvlist_get_number(nv, SNDST_DSPS_INFO_MAX_RATE);
771 *formats = nvlist_get_number(nv, SNDST_DSPS_INFO_FORMATS);
772 *min_chn = nvlist_get_number(nv, SNDST_DSPS_INFO_MIN_CHN);
773 *max_chn = nvlist_get_number(nv, SNDST_DSPS_INFO_MAX_CHN);
777 sndstat_dsp_unpack_nvlist(const nvlist_t *nvlist, struct sndstat_userdev *ud)
779 const char *nameunit, *devnode, *desc;
780 unsigned int pchan, rchan;
781 uint32_t pminrate = 0, pmaxrate = 0;
782 uint32_t rminrate = 0, rmaxrate = 0;
783 uint32_t pfmts = 0, rfmts = 0;
784 uint32_t pminchn = 0, pmaxchn = 0;
785 uint32_t rminchn = 0, rmaxchn = 0;
786 nvlist_t *provider_nvl = NULL;
787 const nvlist_t *diinfo;
788 const char *provider;
790 devnode = nvlist_get_string(nvlist, SNDST_DSPS_DEVNODE);
791 if (nvlist_exists_string(nvlist, SNDST_DSPS_NAMEUNIT))
792 nameunit = nvlist_get_string(nvlist, SNDST_DSPS_NAMEUNIT);
795 desc = nvlist_get_string(nvlist, SNDST_DSPS_DESC);
796 pchan = nvlist_get_number(nvlist, SNDST_DSPS_PCHAN);
797 rchan = nvlist_get_number(nvlist, SNDST_DSPS_RCHAN);
799 if (nvlist_exists_nvlist(nvlist, SNDST_DSPS_INFO_PLAY)) {
800 diinfo = nvlist_get_nvlist(nvlist,
801 SNDST_DSPS_INFO_PLAY);
802 sndstat_get_diinfo_nv(diinfo, &pminrate, &pmaxrate,
803 &pfmts, &pminchn, &pmaxchn);
805 pminrate = nvlist_get_number(nvlist, "pminrate");
806 pmaxrate = nvlist_get_number(nvlist, "pmaxrate");
807 pfmts = nvlist_get_number(nvlist, "pfmts");
811 if (nvlist_exists_nvlist(nvlist, SNDST_DSPS_INFO_REC)) {
812 diinfo = nvlist_get_nvlist(nvlist,
813 SNDST_DSPS_INFO_REC);
814 sndstat_get_diinfo_nv(diinfo, &rminrate, &rmaxrate,
815 &rfmts, &rminchn, &rmaxchn);
817 rminrate = nvlist_get_number(nvlist, "rminrate");
818 rmaxrate = nvlist_get_number(nvlist, "rmaxrate");
819 rfmts = nvlist_get_number(nvlist, "rfmts");
823 provider = dnvlist_get_string(nvlist, SNDST_DSPS_PROVIDER, "");
824 if (provider[0] == '\0')
827 if (provider != NULL &&
828 nvlist_exists_nvlist(nvlist, SNDST_DSPS_PROVIDER_INFO)) {
829 provider_nvl = nvlist_clone(
830 nvlist_get_nvlist(nvlist, SNDST_DSPS_PROVIDER_INFO));
831 if (provider_nvl == NULL)
835 ud->provider = (provider != NULL) ? strdup(provider, M_DEVBUF) : NULL;
836 ud->devnode = strdup(devnode, M_DEVBUF);
837 ud->nameunit = strdup(nameunit, M_DEVBUF);
838 ud->desc = strdup(desc, M_DEVBUF);
841 ud->info_play.min_rate = pminrate;
842 ud->info_play.max_rate = pmaxrate;
843 ud->info_play.formats = pfmts;
844 ud->info_play.min_chn = pminchn;
845 ud->info_play.max_chn = pmaxchn;
846 ud->info_rec.min_rate = rminrate;
847 ud->info_rec.max_rate = rmaxrate;
848 ud->info_rec.formats = rfmts;
849 ud->info_rec.min_chn = rminchn;
850 ud->info_rec.max_chn = rmaxchn;
851 ud->provider_nvl = provider_nvl;
856 sndstat_add_user_devs(struct sndstat_file *pf, caddr_t data)
859 nvlist_t *nvl = NULL;
860 const nvlist_t * const *dsps;
862 struct sndstioc_nv_arg *arg = (struct sndstioc_nv_arg *)data;
864 if ((pf->fflags & FWRITE) == 0) {
869 err = sndstat_unpack_user_nvlbuf(arg->buf, arg->nbytes, &nvl);
873 if (!nvlist_exists_nvlist_array(nvl, SNDST_DSPS)) {
877 dsps = nvlist_get_nvlist_array(nvl, SNDST_DSPS, &ndsps);
878 for (i = 0; i < ndsps; i++) {
879 if (!sndstat_dsp_nvlist_is_sane(dsps[i])) {
885 for (i = 0; i < ndsps; i++) {
886 struct sndstat_userdev *ud =
887 malloc(sizeof(*ud), M_DEVBUF, M_WAITOK);
888 err = sndstat_dsp_unpack_nvlist(dsps[i], ud);
890 sx_unlock(&pf->lock);
893 TAILQ_INSERT_TAIL(&pf->userdev_list, ud, link);
895 sx_unlock(&pf->lock);
903 sndstat_flush_user_devs(struct sndstat_file *pf)
905 if ((pf->fflags & FWRITE) == 0)
909 sndstat_remove_all_userdevs(pf);
910 sx_xunlock(&pf->lock);
915 #ifdef COMPAT_FREEBSD32
917 compat_sndstat_get_devs32(struct sndstat_file *pf, caddr_t data)
919 struct sndstioc_nv_arg32 *arg32 = (struct sndstioc_nv_arg32 *)data;
920 struct sndstioc_nv_arg arg;
923 arg.buf = (void *)(uintptr_t)arg32->buf;
924 arg.nbytes = arg32->nbytes;
926 err = sndstat_get_devs(pf, (caddr_t)&arg);
928 arg32->buf = (uint32_t)(uintptr_t)arg.buf;
929 arg32->nbytes = arg.nbytes;
936 compat_sndstat_add_user_devs32(struct sndstat_file *pf, caddr_t data)
938 struct sndstioc_nv_arg32 *arg32 = (struct sndstioc_nv_arg32 *)data;
939 struct sndstioc_nv_arg arg;
942 arg.buf = (void *)(uintptr_t)arg32->buf;
943 arg.nbytes = arg32->nbytes;
945 err = sndstat_add_user_devs(pf, (caddr_t)&arg);
947 arg32->buf = (uint32_t)(uintptr_t)arg.buf;
948 arg32->nbytes = arg.nbytes;
957 struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
960 struct sndstat_file *pf;
962 err = devfs_get_cdevpriv((void **)&pf);
967 case SNDSTIOC_GET_DEVS:
968 err = sndstat_get_devs(pf, data);
970 #ifdef COMPAT_FREEBSD32
971 case SNDSTIOC_GET_DEVS32:
972 if (!SV_CURPROC_FLAG(SV_ILP32)) {
976 err = compat_sndstat_get_devs32(pf, data);
979 case SNDSTIOC_ADD_USER_DEVS:
980 err = sndstat_add_user_devs(pf, data);
982 #ifdef COMPAT_FREEBSD32
983 case SNDSTIOC_ADD_USER_DEVS32:
984 if (!SV_CURPROC_FLAG(SV_ILP32)) {
988 err = compat_sndstat_add_user_devs32(pf, data);
991 case SNDSTIOC_REFRESH_DEVS:
992 err = sndstat_refresh_devs(pf);
994 case SNDSTIOC_FLUSH_USER_DEVS:
995 err = sndstat_flush_user_devs(pf);
1004 static struct sndstat_userdev *
1005 sndstat_line2userdev(struct sndstat_file *pf, const char *line, int n)
1007 struct sndstat_userdev *ud;
1010 ud = malloc(sizeof(*ud), M_DEVBUF, M_WAITOK|M_ZERO);
1012 ud->provider = NULL;
1013 ud->provider_nvl = NULL;
1014 e = strchr(line, ':');
1017 ud->nameunit = strndup(line, e - line, M_DEVBUF);
1018 ud->devnode = (char *)malloc(e - line + 1, M_DEVBUF, M_WAITOK | M_ZERO);
1019 strlcat(ud->devnode, ud->nameunit, e - line + 1);
1022 e = strchr(line, '<');
1026 e = strrchr(line, '>');
1029 ud->desc = strndup(line, e - line, M_DEVBUF);
1032 e = strchr(line, '(');
1036 e = strrchr(line, ')');
1039 m = strstr(line, "play");
1040 if (m != NULL && m < e)
1042 m = strstr(line, "rec");
1043 if (m != NULL && m < e)
1049 free(ud->nameunit, M_DEVBUF);
1050 free(ud->devnode, M_DEVBUF);
1051 free(ud->desc, M_DEVBUF);
1056 /************************************************************************/
1059 sndstat_register(device_t dev, char *str, sndstat_handler handler)
1061 struct sndstat_entry *ent;
1062 struct sndstat_entry *pre;
1063 const char *devtype;
1067 unit = device_get_unit(dev);
1068 devtype = device_get_name(dev);
1069 if (!strcmp(devtype, "pcm"))
1071 else if (!strcmp(devtype, "midi"))
1072 type = SS_TYPE_MIDI;
1073 else if (!strcmp(devtype, "sequencer"))
1074 type = SS_TYPE_SEQUENCER;
1078 type = SS_TYPE_MODULE;
1082 ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
1087 ent->handler = handler;
1090 /* sorted list insertion */
1091 TAILQ_FOREACH(pre, &sndstat_devlist, link) {
1092 if (pre->unit > unit)
1094 else if (pre->unit < unit)
1096 if (pre->type > type)
1098 else if (pre->type < unit)
1102 TAILQ_INSERT_TAIL(&sndstat_devlist, ent, link);
1104 TAILQ_INSERT_BEFORE(pre, ent, link);
1112 sndstat_registerfile(char *str)
1114 return (sndstat_register(NULL, str, NULL));
1118 sndstat_unregister(device_t dev)
1120 struct sndstat_entry *ent;
1124 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
1125 if (ent->dev == dev) {
1126 TAILQ_REMOVE(&sndstat_devlist, ent, link);
1127 free(ent, M_DEVBUF);
1138 sndstat_unregisterfile(char *str)
1140 struct sndstat_entry *ent;
1144 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
1145 if (ent->dev == NULL && ent->str == str) {
1146 TAILQ_REMOVE(&sndstat_devlist, ent, link);
1147 free(ent, M_DEVBUF);
1157 /************************************************************************/
1160 sndstat_prepare(struct sndstat_file *pf_self)
1162 struct sbuf *s = &pf_self->sbuf;
1163 struct sndstat_entry *ent;
1164 struct snddev_info *d;
1165 struct sndstat_file *pf;
1168 /* make sure buffer is reset */
1171 if (snd_verbose > 0) {
1172 sbuf_printf(s, "FreeBSD Audio Driver (%ubit %d/%s)\n",
1173 (u_int)sizeof(intpcm32_t) << 3, SND_DRV_VERSION,
1177 /* generate list of installed devices */
1179 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
1180 if (ent->dev == NULL)
1182 d = device_get_softc(ent->dev);
1183 if (!PCM_REGISTERED(d))
1186 sbuf_printf(s, "Installed devices:\n");
1187 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
1188 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
1189 if (snd_verbose > 0)
1190 sbuf_printf(s, " %s", ent->str);
1192 /* XXX Need Giant magic entry ??? */
1193 PCM_ACQUIRE_QUICK(d);
1194 ent->handler(s, ent->dev, snd_verbose);
1195 PCM_RELEASE_QUICK(d);
1197 sbuf_printf(s, "\n");
1200 sbuf_printf(s, "No devices installed.\n");
1202 /* append any input from userspace */
1204 TAILQ_FOREACH(pf, &sndstat_filelist, entry) {
1205 struct sndstat_userdev *ud;
1209 sx_xlock(&pf->lock);
1210 if (TAILQ_EMPTY(&pf->userdev_list)) {
1211 sx_unlock(&pf->lock);
1215 sbuf_printf(s, "Installed devices from userspace:\n");
1216 TAILQ_FOREACH(ud, &pf->userdev_list, link) {
1217 const char *caps = (ud->pchan && ud->rchan) ?
1219 (ud->pchan ? "play" : (ud->rchan ? "rec" : ""));
1220 sbuf_printf(s, "%s: <%s>", ud->nameunit, ud->desc);
1221 sbuf_printf(s, " (%s)", caps);
1222 sbuf_printf(s, "\n");
1224 sx_unlock(&pf->lock);
1227 sbuf_printf(s, "No devices installed from userspace.\n");
1229 /* append any file versions */
1230 if (snd_verbose >= 3) {
1232 TAILQ_FOREACH(ent, &sndstat_devlist, link) {
1233 if (ent->dev == NULL && ent->str != NULL) {
1235 sbuf_printf(s, "\nFile Versions:\n");
1236 sbuf_printf(s, "%s\n", ent->str);
1240 sbuf_printf(s, "\nNo file versions.\n");
1243 return (sbuf_len(s));
1247 sndstat_sysinit(void *p)
1249 sx_init(&sndstat_lock, "sndstat lock");
1250 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
1251 UID_ROOT, GID_WHEEL, 0644, "sndstat");
1253 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
1256 sndstat_sysuninit(void *p)
1258 if (sndstat_dev != NULL) {
1259 /* destroy_dev() will wait for all references to go away */
1260 destroy_dev(sndstat_dev);
1262 sx_destroy(&sndstat_lock);
1264 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);