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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include <dev/sound/pcm/sound.h>
34 #include <sys/ctype.h>
36 #include <sys/rwlock.h>
37 #include <sys/sysent.h>
40 #include <vm/vm_object.h>
41 #include <vm/vm_page.h>
42 #include <vm/vm_pager.h>
44 SND_DECLARE_FILE("$FreeBSD$");
46 static int dsp_mmap_allow_prot_exec = 0;
47 SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RW,
48 &dsp_mmap_allow_prot_exec, 0,
49 "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
52 struct pcm_channel *rdch, *wrch;
53 struct pcm_channel *volch;
55 TAILQ_ENTRY(dsp_cdevinfo) link;
58 #define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
59 #define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
60 #define PCM_VOLCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
61 #define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
63 #define DSP_CDEVINFO_CACHESIZE 8
65 #define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \
66 (y) != NULL && (y)->si_drv1 != NULL)
70 static d_open_t dsp_open;
71 static d_close_t dsp_close;
72 static d_read_t dsp_read;
73 static d_write_t dsp_write;
74 static d_ioctl_t dsp_ioctl;
75 static d_poll_t dsp_poll;
76 static d_mmap_t dsp_mmap;
77 static d_mmap_single_t dsp_mmap_single;
79 struct cdevsw dsp_cdevsw = {
80 .d_version = D_VERSION,
88 .d_mmap_single = dsp_mmap_single,
92 static eventhandler_tag dsp_ehtag = NULL;
93 static int dsp_umax = -1;
94 static int dsp_cmax = -1;
96 static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
97 static int dsp_oss_syncstart(int sg_id);
98 static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
99 static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
100 static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
101 static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
102 static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
103 #ifdef OSSV4_EXPERIMENT
104 static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
105 static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
106 static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
107 static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
108 static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
111 static struct snddev_info *
112 dsp_get_info(struct cdev *dev)
114 return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
118 dsp_get_flags(struct cdev *dev)
122 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
124 return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
128 dsp_set_flags(struct cdev *dev, uint32_t flags)
132 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
135 pcm_setflags(bdev, flags);
139 * return the channels associated with an open device instance.
140 * lock channels specified.
143 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
146 struct snddev_info *d;
147 struct pcm_channel *ch;
150 if (PCM_SIMPLEX(dev) != 0) {
151 d = dsp_get_info(dev);
152 if (!PCM_REGISTERED(d))
158 * Note: order is important -
159 * pcm flags -> prio query flags -> wild guess
162 flags = dsp_get_flags(dev);
163 if (flags & SD_F_PRIO_WR) {
165 PCM_RDCH(dev) = NULL;
166 } else if (flags & SD_F_PRIO_RD) {
168 PCM_WRCH(dev) = NULL;
169 } else if (prio & SD_F_PRIO_WR) {
171 PCM_RDCH(dev) = NULL;
172 flags |= SD_F_PRIO_WR;
173 } else if (prio & SD_F_PRIO_RD) {
175 PCM_WRCH(dev) = NULL;
176 flags |= SD_F_PRIO_RD;
177 } else if (PCM_WRCH(dev) != NULL) {
179 PCM_RDCH(dev) = NULL;
180 flags |= SD_F_PRIO_WR;
181 } else if (PCM_RDCH(dev) != NULL) {
183 PCM_WRCH(dev) = NULL;
184 flags |= SD_F_PRIO_RD;
186 PCM_SIMPLEX(dev) = 0;
187 dsp_set_flags(dev, flags);
197 *rdch = PCM_RDCH(dev);
198 *wrch = PCM_WRCH(dev);
200 if (*rdch != NULL && (prio & SD_F_PRIO_RD))
202 if (*wrch != NULL && (prio & SD_F_PRIO_WR))
208 /* unlock specified channels */
210 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
213 if (wrch != NULL && (prio & SD_F_PRIO_WR))
215 if (rdch != NULL && (prio & SD_F_PRIO_RD))
220 dsp_cdevinfo_alloc(struct cdev *dev,
221 struct pcm_channel *rdch, struct pcm_channel *wrch,
222 struct pcm_channel *volch)
224 struct snddev_info *d;
225 struct dsp_cdevinfo *cdi;
228 d = dsp_get_info(dev);
230 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
231 ((rdch == NULL && wrch == NULL) || rdch != wrch),
232 ("bogus %s(), what are you trying to accomplish here?", __func__));
236 simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
239 * Scan for free instance entry and put it into the end of list.
240 * Create new one if necessary.
242 TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
248 cdi->simplex = simplex;
250 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
251 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
256 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
261 cdi->simplex = simplex;
263 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
268 dsp_cdevinfo_free(struct cdev *dev)
270 struct snddev_info *d;
271 struct dsp_cdevinfo *cdi, *tmp;
275 d = dsp_get_info(dev);
277 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
278 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
279 PCM_VOLCH(dev) == NULL,
280 ("bogus %s(), what are you trying to accomplish here?", __func__));
293 * Once it is free, move it back to the beginning of list for
294 * faster new entry allocation.
296 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
297 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
300 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
301 * Reset simplex flags.
303 flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
304 i = DSP_CDEVINFO_CACHESIZE;
305 TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
306 if (cdi->busy != 0) {
307 if (cdi->simplex == 0) {
308 if (cdi->rdch != NULL)
309 flags |= SD_F_PRIO_RD;
310 if (cdi->wrch != NULL)
311 flags |= SD_F_PRIO_WR;
315 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
321 dsp_set_flags(dev, flags);
325 dsp_cdevinfo_init(struct snddev_info *d)
327 struct dsp_cdevinfo *cdi;
330 KASSERT(d != NULL, ("NULL snddev_info"));
334 TAILQ_INIT(&d->dsp_cdevinfo_pool);
335 for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
336 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
337 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
342 dsp_cdevinfo_flush(struct snddev_info *d)
344 struct dsp_cdevinfo *cdi, *tmp;
346 KASSERT(d != NULL, ("NULL snddev_info"));
350 cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
351 while (cdi != NULL) {
352 tmp = TAILQ_NEXT(cdi, link);
356 TAILQ_INIT(&d->dsp_cdevinfo_pool);
359 /* duplex / simplex cdev type */
361 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
362 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
363 DSP_CDEV_TYPE_RDWR /* duplex read, write, or both */
367 DSP_CDEV_VOLCTL_NONE,
368 DSP_CDEV_VOLCTL_READ,
369 DSP_CDEV_VOLCTL_WRITE
372 #define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
373 #define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
374 #define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
375 #define DSP_F_READ(x) ((x) & FREAD)
376 #define DSP_F_WRITE(x) ((x) & FWRITE)
378 static const struct {
390 { SND_DEV_DSP, "dsp", ".", NULL, 0, 0, 0, 0,
391 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
392 DSP_CDEV_TYPE_RDWR },
393 { SND_DEV_AUDIO, "audio", ".", NULL, 0, 0, 0, 0,
394 SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
395 DSP_CDEV_TYPE_RDWR },
396 { SND_DEV_DSP16, "dspW", ".", NULL, 0, 0, 0, 0,
397 SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
398 DSP_CDEV_TYPE_RDWR },
399 { SND_DEV_DSPHW_PLAY, "dsp", ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
400 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
401 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
402 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
403 { SND_DEV_DSPHW_REC, "dsp", ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
404 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
405 { SND_DEV_DSPHW_VREC, "dsp", ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
406 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
407 { SND_DEV_DSPHW_CD, "dspcd", ".", NULL, 0, 0, 0, 0,
408 SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR },
409 /* Low priority, OSSv4 aliases. */
410 { SND_DEV_DSP, "dsp_ac3", ".", "dsp", 0, 0, 0, 0,
411 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
412 DSP_CDEV_TYPE_RDWR },
413 { SND_DEV_DSP, "dsp_mmap", ".", "dsp", 0, 0, 0, 0,
414 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
415 DSP_CDEV_TYPE_RDWR },
416 { SND_DEV_DSP, "dsp_multich", ".", "dsp", 0, 0, 0, 0,
417 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
418 DSP_CDEV_TYPE_RDWR },
419 { SND_DEV_DSP, "dsp_spdifout", ".", "dsp", 0, 0, 0, 0,
420 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
421 DSP_CDEV_TYPE_RDWR },
422 { SND_DEV_DSP, "dsp_spdifin", ".", "dsp", 0, 0, 0, 0,
423 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
424 DSP_CDEV_TYPE_RDWR },
427 #define DSP_FIXUP_ERROR() do { \
428 prio = dsp_get_flags(i_dev); \
429 if (!DSP_F_VALID(flags)) \
431 if (!DSP_F_DUPLEX(flags) && \
432 ((DSP_F_READ(flags) && d->reccount == 0) || \
433 (DSP_F_WRITE(flags) && d->playcount == 0))) \
435 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
436 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
437 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
439 else if (DSP_REGISTERED(d, i_dev)) \
444 dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
446 struct pcm_channel *rdch, *wrch;
447 struct snddev_info *d;
448 uint32_t fmt, spd, prio, volctl;
449 int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
451 /* Kind of impossible.. */
452 if (i_dev == NULL || td == NULL)
455 d = dsp_get_info(i_dev);
456 if (!PCM_REGISTERED(d))
461 /* Lock snddev so nobody else can monkey with it. */
466 * Try to acquire cloned device before someone else pick it.
467 * ENODEV means this is not a cloned droids.
469 error = snd_clone_acquire(i_dev);
470 if (!(error == 0 || error == ENODEV)) {
481 (void)snd_clone_release(i_dev);
488 * That is just enough. Acquire and unlock pcm lock so
489 * the other will just have to wait until we finish doing
495 devtype = PCMDEV(i_dev);
500 volctl = DSP_CDEV_VOLCTL_NONE;
502 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
503 if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
506 * Volume control only valid for DSPHW devices,
507 * and it must be opened in opposite direction be it
508 * simplex or duplex. Anything else will be handled
511 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) {
512 if (dsp_cdevs[i].volctl != 0 &&
514 volctl = DSP_CDEV_VOLCTL_WRITE;
518 if (DSP_F_READ(flags)) {
519 (void)snd_clone_release(i_dev);
520 PCM_RELEASE_QUICK(d);
524 wdevunit = dev2unit(i_dev);
525 } else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
526 if (dsp_cdevs[i].volctl != 0 &&
527 DSP_F_WRITE(flags)) {
528 volctl = DSP_CDEV_VOLCTL_READ;
532 if (DSP_F_WRITE(flags)) {
533 (void)snd_clone_release(i_dev);
534 PCM_RELEASE_QUICK(d);
538 rdevunit = dev2unit(i_dev);
540 fmt = dsp_cdevs[i].fmt;
541 spd = dsp_cdevs[i].spd;
545 /* No matching devtype? */
546 if (fmt == 0 || spd == 0)
547 panic("impossible devtype %d", devtype);
555 * if we get here, the open request is valid- either:
556 * * we were previously not open
557 * * we were open for play xor record and the opener wants
558 * the non-open direction
560 if (DSP_F_READ(flags)) {
562 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
563 td->td_proc->p_pid, td->td_proc->p_comm, rdevunit);
565 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
568 if (volctl == DSP_CDEV_VOLCTL_READ)
573 pcm_chnrelease(rdch);
574 if (!DSP_F_DUPLEX(flags)) {
575 (void)snd_clone_release(i_dev);
576 PCM_RELEASE_QUICK(d);
581 } else if (volctl == DSP_CDEV_VOLCTL_READ) {
584 pcm_chnrelease(rdch);
587 if (flags & O_NONBLOCK)
588 rdch->flags |= CHN_F_NBIO;
590 rdch->flags |= CHN_F_EXCLUSIVE;
592 if (volctl == DSP_CDEV_VOLCTL_NONE)
593 chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
598 if (DSP_F_WRITE(flags)) {
600 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
601 td->td_proc->p_pid, td->td_proc->p_comm, wdevunit);
603 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
606 if (volctl == DSP_CDEV_VOLCTL_WRITE)
611 pcm_chnrelease(wrch);
612 if (!DSP_F_DUPLEX(flags)) {
615 * Lock, deref and release previously
616 * created record channel
619 pcm_chnref(rdch, -1);
620 pcm_chnrelease(rdch);
622 (void)snd_clone_release(i_dev);
623 PCM_RELEASE_QUICK(d);
628 } else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
631 pcm_chnrelease(wrch);
634 if (flags & O_NONBLOCK)
635 wrch->flags |= CHN_F_NBIO;
637 wrch->flags |= CHN_F_EXCLUSIVE;
639 if (volctl == DSP_CDEV_VOLCTL_NONE)
640 chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
649 * We're done. Allocate channels information for this cdev.
652 case DSP_CDEV_VOLCTL_READ:
653 KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
654 dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
656 case DSP_CDEV_VOLCTL_WRITE:
657 KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
658 dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
660 case DSP_CDEV_VOLCTL_NONE:
662 if (wrch == NULL && rdch == NULL) {
663 (void)snd_clone_release(i_dev);
673 dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
675 CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
677 CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
682 * Increase clone refcount for its automatic garbage collector.
684 (void)snd_clone_ref(i_dev);
695 dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
697 struct pcm_channel *rdch, *wrch, *volch;
698 struct snddev_info *d;
699 int sg_ids, rdref, wdref;
701 d = dsp_get_info(i_dev);
702 if (!DSP_REGISTERED(d, i_dev))
711 rdch = PCM_RDCH(i_dev);
712 wrch = PCM_WRCH(i_dev);
713 volch = PCM_VOLCH(i_dev);
715 PCM_RDCH(i_dev) = NULL;
716 PCM_WRCH(i_dev) = NULL;
717 PCM_VOLCH(i_dev) = NULL;
725 else if (volch == wrch)
729 pcm_chnref(volch, -1);
735 CHN_REMOVE(d, rdch, channels.pcm.opened);
737 CHN_REMOVE(d, wrch, channels.pcm.opened);
739 if (rdch != NULL || wrch != NULL) {
743 * The channel itself need not be locked because:
744 * a) Adding a channel to a syncgroup happens only
745 * in dsp_ioctl(), which cannot run concurrently
747 * b) The syncmember pointer (sm) is protected by
748 * the global syncgroup list lock.
749 * c) A channel can't just disappear, invalidating
750 * pointers, unless it's closed/dereferenced
754 sg_ids = chn_syncdestroy(rdch);
757 free_unr(pcmsg_unrhdr, sg_ids);
760 pcm_chnref(rdch, rdref);
761 chn_abort(rdch); /* won't sleep */
762 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
763 CHN_F_DEAD | CHN_F_EXCLUSIVE);
764 chn_reset(rdch, 0, 0);
765 pcm_chnrelease(rdch);
769 * Please see block above.
772 sg_ids = chn_syncdestroy(wrch);
775 free_unr(pcmsg_unrhdr, sg_ids);
778 pcm_chnref(wrch, wdref);
779 chn_flush(wrch); /* may sleep */
780 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
781 CHN_F_DEAD | CHN_F_EXCLUSIVE);
782 chn_reset(wrch, 0, 0);
783 pcm_chnrelease(wrch);
788 dsp_cdevinfo_free(i_dev);
790 * Release clone busy state and unref it so the automatic
791 * garbage collector will get the hint and do the remaining
794 (void)snd_clone_release(i_dev);
797 * destroy_dev() might sleep, so release pcm lock
798 * here and rely on pcm cv serialization.
801 (void)snd_clone_unref(i_dev);
813 dsp_io_ops(struct cdev *i_dev, struct uio *buf)
815 struct snddev_info *d;
816 struct pcm_channel **ch, *rdch, *wrch;
817 int (*chn_io)(struct pcm_channel *, struct uio *);
821 KASSERT(i_dev != NULL && buf != NULL &&
822 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
823 ("%s(): io train wreck!", __func__));
825 d = dsp_get_info(i_dev);
826 if (!DSP_REGISTERED(d, i_dev))
831 switch (buf->uio_rw) {
843 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
849 runpid = buf->uio_td->td_proc->p_pid;
851 getchns(i_dev, &rdch, &wrch, prio);
853 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
858 if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
859 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
860 relchns(i_dev, rdch, wrch, prio);
863 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
864 (*ch)->flags |= CHN_F_RUNNING;
869 * chn_read/write must give up channel lock in order to copy bytes
870 * from/to userland, so up the "in progress" counter to make sure
871 * someone else doesn't come along and muss up the buffer.
874 ret = chn_io(*ch, buf);
877 CHN_BROADCAST(&(*ch)->cv);
879 relchns(i_dev, rdch, wrch, prio);
887 dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
889 return (dsp_io_ops(i_dev, buf));
893 dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
895 return (dsp_io_ops(i_dev, buf));
899 dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
901 struct snddev_info *d;
902 struct pcm_channel *c;
905 KASSERT(dev != NULL && volch != NULL,
906 ("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
908 d = dsp_get_info(dev);
909 if (!PCM_REGISTERED(d)) {
920 if (!(c->feederflags & (1 << FEEDER_VOLUME)))
930 unit = dev2unit(dev);
932 CHN_FOREACH(c, d, channels.pcm) {
934 if (c->unit != unit) {
944 return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
954 dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
957 struct snddev_info *d;
958 struct pcm_channel *rdch, *wrch;
961 d = dsp_get_info(dev);
962 if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
969 rdch = PCM_RDCH(dev);
970 wrch = PCM_WRCH(dev);
972 /* No specific channel, look into cache */
974 volch = PCM_VOLCH(dev);
978 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
980 else if (j == SOUND_MIXER_PCM && wrch != NULL)
984 devtype = PCMDEV(dev);
986 /* Look super harder */
988 (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
989 devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
990 ret = dsp_get_volume_channel(dev, &volch);
997 /* Final validation */
1000 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
1004 if (volch->direction == PCMDIR_PLAY)
1012 if (volch != NULL &&
1013 ((j == SOUND_MIXER_PCM && volch->direction == PCMDIR_PLAY) ||
1014 (j == SOUND_MIXER_RECLEV && volch->direction == PCMDIR_REC))) {
1015 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1016 int left, right, center;
1018 left = *(int *)arg & 0x7f;
1019 right = ((*(int *)arg) >> 8) & 0x7f;
1020 center = (left + right) >> 1;
1021 chn_setvolume_multi(volch, SND_VOL_C_PCM, left, right,
1023 } else if ((cmd & ~0xff) == MIXER_READ(0)) {
1024 *(int *)arg = CHN_GETVOLUME(volch,
1025 SND_VOL_C_PCM, SND_CHN_T_FL);
1026 *(int *)arg |= CHN_GETVOLUME(volch,
1027 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1030 } else if (rdch != NULL || wrch != NULL) {
1032 case SOUND_MIXER_DEVMASK:
1033 case SOUND_MIXER_CAPS:
1034 case SOUND_MIXER_STEREODEVS:
1035 if ((cmd & ~0xff) == MIXER_READ(0)) {
1038 *(int *)arg |= SOUND_MASK_RECLEV;
1040 *(int *)arg |= SOUND_MASK_PCM;
1044 case SOUND_MIXER_RECMASK:
1045 case SOUND_MIXER_RECSRC:
1046 if ((cmd & ~0xff) == MIXER_READ(0))
1062 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1065 struct pcm_channel *chn, *rdch, *wrch;
1066 struct snddev_info *d;
1068 int *arg_i, ret, tmp;
1070 d = dsp_get_info(i_dev);
1071 if (!DSP_REGISTERED(d, i_dev))
1081 if (IOCGROUP(cmd) == 'M') {
1082 if (cmd == OSS_GETVERSION) {
1083 *arg_i = SOUND_VERSION;
1087 ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
1093 if (d->mixer_dev != NULL) {
1094 PCM_ACQUIRE_QUICK(d);
1095 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1097 PCM_RELEASE_QUICK(d);
1107 * Certain ioctls may be made on any type of device (audio, mixer,
1108 * and MIDI). Handle those special cases here.
1110 if (IOCGROUP(cmd) == 'X') {
1111 PCM_ACQUIRE_QUICK(d);
1113 case SNDCTL_SYSINFO:
1114 sound_oss_sysinfo((oss_sysinfo *)arg);
1116 case SNDCTL_CARDINFO:
1117 ret = sound_oss_card_info((oss_card_info *)arg);
1119 case SNDCTL_AUDIOINFO:
1120 case SNDCTL_AUDIOINFO_EX:
1121 case SNDCTL_ENGINEINFO:
1122 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
1124 case SNDCTL_MIXERINFO:
1125 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
1130 PCM_RELEASE_QUICK(d);
1135 getchns(i_dev, &rdch, &wrch, 0);
1137 if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
1139 if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
1142 if (wrch == NULL && rdch == NULL) {
1150 * we start with the new ioctl interface.
1152 case AIONWRITE: /* how many bytes can write ? */
1156 if (wrch && wrch->bufhard.dl)
1157 while (chn_wrfeed(wrch) == 0);
1159 *arg_i = sndbuf_getfree(wrch->bufsoft);
1167 case AIOSSIZE: /* set the current blocksize */
1169 struct snd_size *p = (struct snd_size *)arg;
1173 PCM_ACQUIRE_QUICK(d);
1176 chn_setblocksize(wrch, 2, p->play_size);
1177 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1182 chn_setblocksize(rdch, 2, p->rec_size);
1183 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1186 PCM_RELEASE_QUICK(d);
1189 case AIOGSIZE: /* get the current blocksize */
1191 struct snd_size *p = (struct snd_size *)arg;
1195 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1200 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1209 snd_chan_param *p = (snd_chan_param *)arg;
1211 if (cmd == AIOSFMT &&
1212 ((p->play_format != 0 && p->play_rate == 0) ||
1213 (p->rec_format != 0 && p->rec_rate == 0))) {
1217 PCM_ACQUIRE_QUICK(d);
1220 if (cmd == AIOSFMT && p->play_format != 0) {
1222 SND_FORMAT(p->play_format,
1223 AFMT_CHANNEL(wrch->format),
1224 AFMT_EXTCHANNEL(wrch->format)));
1225 chn_setspeed(wrch, p->play_rate);
1227 p->play_rate = wrch->speed;
1228 p->play_format = AFMT_ENCODING(wrch->format);
1236 if (cmd == AIOSFMT && p->rec_format != 0) {
1238 SND_FORMAT(p->rec_format,
1239 AFMT_CHANNEL(rdch->format),
1240 AFMT_EXTCHANNEL(rdch->format)));
1241 chn_setspeed(rdch, p->rec_rate);
1243 p->rec_rate = rdch->speed;
1244 p->rec_format = AFMT_ENCODING(rdch->format);
1250 PCM_RELEASE_QUICK(d);
1254 case AIOGCAP: /* get capabilities */
1256 snd_capabilities *p = (snd_capabilities *)arg;
1257 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
1263 rcaps = chn_getcaps(rdch);
1267 pcaps = chn_getcaps(wrch);
1269 p->rate_min = max(rcaps? rcaps->minspeed : 0,
1270 pcaps? pcaps->minspeed : 0);
1271 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
1272 pcaps? pcaps->maxspeed : 1000000);
1273 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
1274 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
1275 /* XXX bad on sb16 */
1276 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
1277 (wrch? chn_getformats(wrch) : 0xffffffff);
1279 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
1280 pdev = d->mixer_dev;
1281 p->mixers = 1; /* default: one mixer */
1282 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
1283 p->left = p->right = 100;
1293 if (*arg_i == AIOSYNC_PLAY && wrch) {
1295 *arg_i = chn_abort(wrch);
1297 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
1299 *arg_i = chn_abort(rdch);
1302 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
1308 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
1309 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
1313 * here follow the standard ioctls (filio.h etc.)
1315 case FIONREAD: /* get # bytes to read */
1318 /* if (rdch && rdch->bufhard.dl)
1319 while (chn_rdfeed(rdch) == 0);
1321 *arg_i = sndbuf_getready(rdch->bufsoft);
1329 case FIOASYNC: /*set/clear async i/o */
1330 DEB( printf("FIOASYNC\n") ; )
1333 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1334 case FIONBIO: /* set/clear non-blocking i/o */
1337 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1338 rdch->flags |= CHN_F_NBIO;
1340 rdch->flags &= ~CHN_F_NBIO;
1345 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1346 wrch->flags |= CHN_F_NBIO;
1348 wrch->flags &= ~CHN_F_NBIO;
1354 * Finally, here is the linux-compatible ioctl interface
1356 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1357 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
1358 case SNDCTL_DSP_GETBLKSIZE:
1359 chn = wrch ? wrch : rdch;
1362 *arg_i = sndbuf_getblksz(chn->bufsoft);
1370 case SNDCTL_DSP_SETBLKSIZE:
1371 RANGE(*arg_i, 16, 65536);
1372 PCM_ACQUIRE_QUICK(d);
1375 chn_setblocksize(wrch, 2, *arg_i);
1380 chn_setblocksize(rdch, 2, *arg_i);
1383 PCM_RELEASE_QUICK(d);
1386 case SNDCTL_DSP_RESET:
1387 DEB(printf("dsp reset\n"));
1402 case SNDCTL_DSP_SYNC:
1403 DEB(printf("dsp sync\n"));
1404 /* chn_sync may sleep */
1412 case SNDCTL_DSP_SPEED:
1413 /* chn_setspeed may sleep */
1415 PCM_ACQUIRE_QUICK(d);
1418 ret = chn_setspeed(wrch, *arg_i);
1422 if (rdch && ret == 0) {
1424 ret = chn_setspeed(rdch, *arg_i);
1429 PCM_RELEASE_QUICK(d);
1433 case SOUND_PCM_READ_RATE:
1434 chn = wrch ? wrch : rdch;
1437 *arg_i = chn->speed;
1445 case SNDCTL_DSP_STEREO:
1447 *arg_i = (*arg_i)? 2 : 1;
1448 PCM_ACQUIRE_QUICK(d);
1451 ret = chn_setformat(wrch,
1452 SND_FORMAT(wrch->format, *arg_i, 0));
1453 tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1456 if (rdch && ret == 0) {
1458 ret = chn_setformat(rdch,
1459 SND_FORMAT(rdch->format, *arg_i, 0));
1461 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1464 PCM_RELEASE_QUICK(d);
1468 case SOUND_PCM_WRITE_CHANNELS:
1469 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1476 struct pcmchan_matrix *m;
1480 if (*arg_i > SND_CHN_MAX)
1481 *arg_i = SND_CHN_MAX;
1483 m = feeder_matrix_default_channel_map(*arg_i);
1489 PCM_ACQUIRE_QUICK(d);
1492 ret = chn_setformat(wrch,
1493 SND_FORMAT(wrch->format, *arg_i, ext));
1494 tmp = AFMT_CHANNEL(wrch->format);
1497 if (rdch && ret == 0) {
1499 ret = chn_setformat(rdch,
1500 SND_FORMAT(rdch->format, *arg_i, ext));
1502 tmp = AFMT_CHANNEL(rdch->format);
1505 PCM_RELEASE_QUICK(d);
1508 chn = wrch ? wrch : rdch;
1510 *arg_i = AFMT_CHANNEL(chn->format);
1515 case SOUND_PCM_READ_CHANNELS:
1516 chn = wrch ? wrch : rdch;
1519 *arg_i = AFMT_CHANNEL(chn->format);
1527 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
1528 chn = wrch ? wrch : rdch;
1531 *arg_i = chn_getformats(chn);
1539 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1540 if (*arg_i != AFMT_QUERY) {
1542 PCM_ACQUIRE_QUICK(d);
1545 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1546 AFMT_CHANNEL(wrch->format),
1547 AFMT_EXTCHANNEL(wrch->format)));
1551 if (rdch && ret == 0) {
1553 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1554 AFMT_CHANNEL(rdch->format),
1555 AFMT_EXTCHANNEL(rdch->format)));
1560 PCM_RELEASE_QUICK(d);
1561 *arg_i = AFMT_ENCODING(tmp);
1563 chn = wrch ? wrch : rdch;
1565 *arg_i = AFMT_ENCODING(chn->format);
1570 case SNDCTL_DSP_SETFRAGMENT:
1571 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1573 uint32_t fragln = (*arg_i) & 0x0000ffff;
1574 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1576 uint32_t r_maxfrags, r_fragsz;
1578 RANGE(fragln, 4, 16);
1579 fragsz = 1 << fragln;
1582 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1585 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1586 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1588 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1589 PCM_ACQUIRE_QUICK(d);
1592 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1593 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1594 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1597 r_maxfrags = maxfrags;
1600 if (wrch && ret == 0) {
1602 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1603 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1604 fragsz = sndbuf_getblksz(wrch->bufsoft);
1606 } else { /* use whatever came from the read channel */
1607 maxfrags = r_maxfrags;
1610 PCM_RELEASE_QUICK(d);
1613 while (fragsz > 1) {
1617 *arg_i = (maxfrags << 16) | fragln;
1621 case SNDCTL_DSP_GETISPACE:
1622 /* return the size of data available in the input queue */
1624 audio_buf_info *a = (audio_buf_info *)arg;
1626 struct snd_dbuf *bs = rdch->bufsoft;
1629 a->bytes = sndbuf_getready(bs);
1630 a->fragments = a->bytes / sndbuf_getblksz(bs);
1631 a->fragstotal = sndbuf_getblkcnt(bs);
1632 a->fragsize = sndbuf_getblksz(bs);
1639 case SNDCTL_DSP_GETOSPACE:
1640 /* return space available in the output queue */
1642 audio_buf_info *a = (audio_buf_info *)arg;
1644 struct snd_dbuf *bs = wrch->bufsoft;
1647 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1648 a->bytes = sndbuf_getfree(bs);
1649 a->fragments = a->bytes / sndbuf_getblksz(bs);
1650 a->fragstotal = sndbuf_getblkcnt(bs);
1651 a->fragsize = sndbuf_getblksz(bs);
1658 case SNDCTL_DSP_GETIPTR:
1660 count_info *a = (count_info *)arg;
1662 struct snd_dbuf *bs = rdch->bufsoft;
1665 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1666 a->bytes = sndbuf_gettotal(bs);
1667 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1668 a->ptr = sndbuf_getfreeptr(bs);
1669 rdch->blocks = sndbuf_getblocks(bs);
1676 case SNDCTL_DSP_GETOPTR:
1678 count_info *a = (count_info *)arg;
1680 struct snd_dbuf *bs = wrch->bufsoft;
1683 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1684 a->bytes = sndbuf_gettotal(bs);
1685 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1686 a->ptr = sndbuf_getreadyptr(bs);
1687 wrch->blocks = sndbuf_getblocks(bs);
1694 case SNDCTL_DSP_GETCAPS:
1696 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1697 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1698 *arg_i |= PCM_CAP_DUPLEX;
1702 case SOUND_PCM_READ_BITS:
1703 chn = wrch ? wrch : rdch;
1706 if (chn->format & AFMT_8BIT)
1708 else if (chn->format & AFMT_16BIT)
1710 else if (chn->format & AFMT_24BIT)
1712 else if (chn->format & AFMT_32BIT)
1723 case SNDCTL_DSP_SETTRIGGER:
1726 rdch->flags &= ~CHN_F_NOTRIGGER;
1727 if (*arg_i & PCM_ENABLE_INPUT)
1732 rdch->flags |= CHN_F_NOTRIGGER;
1738 wrch->flags &= ~CHN_F_NOTRIGGER;
1739 if (*arg_i & PCM_ENABLE_OUTPUT)
1744 wrch->flags |= CHN_F_NOTRIGGER;
1750 case SNDCTL_DSP_GETTRIGGER:
1754 if (wrch->flags & CHN_F_TRIGGERED)
1755 *arg_i |= PCM_ENABLE_OUTPUT;
1760 if (rdch->flags & CHN_F_TRIGGERED)
1761 *arg_i |= PCM_ENABLE_INPUT;
1766 case SNDCTL_DSP_GETODELAY:
1768 struct snd_dbuf *bs = wrch->bufsoft;
1771 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1772 *arg_i = sndbuf_getready(bs);
1778 case SNDCTL_DSP_POST:
1781 wrch->flags &= ~CHN_F_NOTRIGGER;
1787 case SNDCTL_DSP_SETDUPLEX:
1789 * switch to full-duplex mode if card is in half-duplex
1790 * mode and is able to work in full-duplex mode
1793 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1794 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1799 * The following four ioctls are simple wrappers around mixer_ioctl
1800 * with no further processing. xcmd is short for "translated
1803 case SNDCTL_DSP_GETRECVOL:
1805 xcmd = SOUND_MIXER_READ_RECLEV;
1809 case SNDCTL_DSP_SETRECVOL:
1811 xcmd = SOUND_MIXER_WRITE_RECLEV;
1815 case SNDCTL_DSP_GETPLAYVOL:
1817 xcmd = SOUND_MIXER_READ_PCM;
1821 case SNDCTL_DSP_SETPLAYVOL:
1823 xcmd = SOUND_MIXER_WRITE_PCM;
1827 ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
1833 if (d->mixer_dev != NULL) {
1834 PCM_ACQUIRE_QUICK(d);
1835 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
1837 PCM_RELEASE_QUICK(d);
1843 case SNDCTL_DSP_GET_RECSRC_NAMES:
1844 case SNDCTL_DSP_GET_RECSRC:
1845 case SNDCTL_DSP_SET_RECSRC:
1846 if (d->mixer_dev != NULL) {
1847 PCM_ACQUIRE_QUICK(d);
1848 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1850 PCM_RELEASE_QUICK(d);
1856 * The following 3 ioctls aren't very useful at the moment. For
1857 * now, only a single channel is associated with a cdev (/dev/dspN
1858 * instance), so there's only a single output routing to use (i.e.,
1859 * the wrch bound to this cdev).
1861 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1863 oss_mixer_enuminfo *ei;
1864 ei = (oss_mixer_enuminfo *)arg;
1867 ei->version = 0; /* static for now */
1868 ei->strindex[0] = 0;
1872 strlcpy(ei->strings, wrch->name,
1873 sizeof(ei->strings));
1876 ei->strings[0] = '\0';
1880 case SNDCTL_DSP_GET_PLAYTGT:
1881 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1884 * OSSv4: "The value that was accepted by the device will
1885 * be returned back in the variable pointed by the
1894 case SNDCTL_DSP_SILENCE:
1896 * Flush the software (pre-feed) buffer, but try to minimize playback
1897 * interruption. (I.e., record unplayed samples with intent to
1898 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1904 struct snd_dbuf *bs;
1906 while (wrch->inprog != 0)
1907 cv_wait(&wrch->cv, wrch->lock);
1909 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1910 bs->sl = sndbuf_getready(bs);
1911 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1912 sndbuf_fillsilence(bs);
1919 case SNDCTL_DSP_SKIP:
1921 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1922 * playback buffer by moving the current write position immediately
1923 * before the point where the device is currently reading the samples."
1928 struct snd_dbuf *bs;
1930 while (wrch->inprog != 0)
1931 cv_wait(&wrch->cv, wrch->lock);
1933 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1934 sndbuf_softreset(bs);
1935 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1943 case SNDCTL_DSP_CURRENT_OPTR:
1944 case SNDCTL_DSP_CURRENT_IPTR:
1946 * @note Changing formats resets the buffer counters, which differs
1947 * from the 4Front drivers. However, I don't expect this to be
1948 * much of a problem.
1950 * @note In a test where @c CURRENT_OPTR is called immediately after write
1951 * returns, this driver is about 32K samples behind whereas
1952 * 4Front's is about 8K samples behind. Should determine source
1953 * of discrepancy, even if only out of curiosity.
1955 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1957 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1961 struct snd_dbuf *bs;
1964 oss_count_t *oc = (oss_count_t *)arg;
1969 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1970 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
1971 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
1973 oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
1974 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
1980 case SNDCTL_DSP_HALT_OUTPUT:
1981 case SNDCTL_DSP_HALT_INPUT:
1982 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
1992 case SNDCTL_DSP_LOW_WATER:
1994 * Set the number of bytes required to attract attention by
1999 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
2004 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
2009 case SNDCTL_DSP_GETERROR:
2011 * OSSv4 docs: "All errors and counters will automatically be
2012 * cleared to zeroes after the call so each call will return only
2013 * the errors that occurred after the previous invocation. ... The
2014 * play_underruns and rec_overrun fields are the only useful fields
2015 * returned by OSS 4.0."
2018 audio_errinfo *ei = (audio_errinfo *)arg;
2020 bzero((void *)ei, sizeof(*ei));
2024 ei->play_underruns = wrch->xruns;
2030 ei->rec_overruns = rdch->xruns;
2037 case SNDCTL_DSP_SYNCGROUP:
2038 PCM_ACQUIRE_QUICK(d);
2039 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
2040 PCM_RELEASE_QUICK(d);
2043 case SNDCTL_DSP_SYNCSTART:
2044 PCM_ACQUIRE_QUICK(d);
2045 ret = dsp_oss_syncstart(*arg_i);
2046 PCM_RELEASE_QUICK(d);
2049 case SNDCTL_DSP_POLICY:
2050 PCM_ACQUIRE_QUICK(d);
2051 ret = dsp_oss_policy(wrch, rdch, *arg_i);
2052 PCM_RELEASE_QUICK(d);
2055 case SNDCTL_DSP_COOKEDMODE:
2056 PCM_ACQUIRE_QUICK(d);
2057 if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
2058 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
2059 PCM_RELEASE_QUICK(d);
2061 case SNDCTL_DSP_GET_CHNORDER:
2062 PCM_ACQUIRE_QUICK(d);
2063 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
2064 PCM_RELEASE_QUICK(d);
2066 case SNDCTL_DSP_SET_CHNORDER:
2067 PCM_ACQUIRE_QUICK(d);
2068 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
2069 PCM_RELEASE_QUICK(d);
2071 case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
2072 PCM_ACQUIRE_QUICK(d);
2073 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
2074 PCM_RELEASE_QUICK(d);
2076 case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
2079 #ifdef OSSV4_EXPERIMENT
2081 * XXX The following ioctls are not yet supported and just return
2084 case SNDCTL_DSP_GETOPEAKS:
2085 case SNDCTL_DSP_GETIPEAKS:
2086 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
2090 oss_peaks_t *op = (oss_peaks_t *)arg;
2094 ret = chn_getpeaks(chn, &lpeak, &rpeak);
2106 * XXX Once implemented, revisit this for proper cv protection
2109 case SNDCTL_GETLABEL:
2110 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
2112 case SNDCTL_SETLABEL:
2113 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
2115 case SNDCTL_GETSONG:
2116 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
2118 case SNDCTL_SETSONG:
2119 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
2121 case SNDCTL_SETNAME:
2122 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
2126 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
2127 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
2128 * 4Front Technologies.
2130 case SNDCTL_DSP_READCTL:
2131 case SNDCTL_DSP_WRITECTL:
2134 #endif /* !0 (explicitly omitted ioctls) */
2136 #endif /* !OSSV4_EXPERIMENT */
2137 case SNDCTL_DSP_MAPINBUF:
2138 case SNDCTL_DSP_MAPOUTBUF:
2139 case SNDCTL_DSP_SETSYNCRO:
2142 case SNDCTL_DSP_SUBDIVIDE:
2143 case SOUND_PCM_WRITE_FILTER:
2144 case SOUND_PCM_READ_FILTER:
2145 /* dunno what these do, don't sound important */
2148 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
2159 dsp_poll(struct cdev *i_dev, int events, struct thread *td)
2161 struct snddev_info *d;
2162 struct pcm_channel *wrch, *rdch;
2165 d = dsp_get_info(i_dev);
2166 if (!DSP_REGISTERED(d, i_dev))
2175 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2177 if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) {
2178 e = (events & (POLLOUT | POLLWRNORM));
2180 ret |= chn_poll(wrch, e, td);
2183 if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) {
2184 e = (events & (POLLIN | POLLRDNORM));
2186 ret |= chn_poll(rdch, e, td);
2189 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2197 dsp_mmap(struct cdev *i_dev, vm_ooffset_t offset, vm_paddr_t *paddr,
2198 int nprot, vm_memattr_t *memattr)
2201 /* XXX memattr is not honored */
2202 *paddr = vtophys(offset);
2207 dsp_mmap_single(struct cdev *i_dev, vm_ooffset_t *offset,
2208 vm_size_t size, struct vm_object **object, int nprot)
2210 struct snddev_info *d;
2211 struct pcm_channel *wrch, *rdch, *c;
2214 * Reject PROT_EXEC by default. It just doesn't makes sense.
2215 * Unfortunately, we have to give up this one due to linux_mmap
2218 * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
2222 if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
2223 (dsp_mmap_allow_prot_exec == 0 &&
2224 SV_CURPROC_ABI() != SV_ABI_LINUX)))
2226 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
2231 * PROT_READ (alone) selects the input buffer.
2232 * PROT_WRITE (alone) selects the output buffer.
2233 * PROT_WRITE|PROT_READ together select the output buffer.
2235 if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
2238 d = dsp_get_info(i_dev);
2239 if (!DSP_REGISTERED(d, i_dev))
2244 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2246 c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
2247 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
2248 (*offset + size) > sndbuf_getsize(c->bufsoft) ||
2249 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
2250 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
2251 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2257 wrch->flags |= CHN_F_MMAP;
2259 rdch->flags |= CHN_F_MMAP;
2261 *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
2262 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2263 *object = vm_pager_allocate(OBJT_DEVICE, i_dev,
2264 size, nprot, *offset, curthread->td_ucred);
2268 if (*object == NULL)
2273 /* So much for dev_stdclone() */
2275 dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
2279 len = strlen(namep);
2281 if (bcmp(name, namep, len) != 0)
2286 if (isdigit(*name) == 0)
2291 if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
2294 for (*u = 0; isdigit(*name) != 0; name++) {
2302 return ((use_sep == 0) ? 0 : ENODEV);
2304 if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
2309 if (*name == '0' && name[1] != '\0')
2312 for (*c = 0; isdigit(*name) != 0; name++) {
2326 dsp_clone(void *arg,
2327 #if __FreeBSD_version >= 600034
2330 char *name, int namelen, struct cdev **dev)
2332 struct snddev_info *d;
2333 struct snd_clone_entry *ce;
2334 struct pcm_channel *c;
2335 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
2336 char *devname, *devcmp, *devsep;
2338 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
2352 for (i = 0; unit == -1 &&
2353 i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2354 devtype = dsp_cdevs[i].type;
2355 devcmp = dsp_cdevs[i].name;
2356 devsep = dsp_cdevs[i].sep;
2357 devname = dsp_cdevs[i].alias;
2358 if (devname == NULL)
2360 devhw = dsp_cdevs[i].hw;
2361 devcmax = dsp_cdevs[i].max - 1;
2362 if (strcmp(name, devcmp) == 0)
2364 else if (dsp_stdclone(name, devcmp, devsep,
2365 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
2371 d = devclass_get_softc(pcm_devclass, unit);
2372 if (!PCM_REGISTERED(d) || d->clones == NULL)
2375 /* XXX Need Giant magic entry ??? */
2378 if (snd_clone_disabled(d->clones)) {
2387 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2390 KASSERT(devcmax <= dsp_cmax,
2391 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2392 if (cunit > devcmax) {
2393 PCM_RELEASE_QUICK(d);
2396 udcmask |= snd_c2unit(cunit);
2397 CHN_FOREACH(c, d, channels.pcm) {
2399 if (c->unit != udcmask) {
2404 udcmask &= ~snd_c2unit(cunit);
2406 * Temporarily increase clone maxunit to overcome
2407 * vchan flexibility.
2409 * # sysctl dev.pcm.0.play.vchans=256
2410 * dev.pcm.0.play.vchans: 1 -> 256
2411 * # cat /dev/zero > /dev/dsp0.vp255 &
2413 * # sysctl dev.pcm.0.play.vchans=0
2414 * dev.pcm.0.play.vchans: 256 -> 1
2416 * [1] + running cat /dev/zero > /dev/dsp0.vp255
2418 * # cat /dev/zero > /dev/dsp0.vp255
2419 * zsh: operation not supported: /dev/dsp0.vp255
2421 tumax = snd_clone_getmaxunit(d->clones);
2423 snd_clone_setmaxunit(d->clones, cunit);
2426 goto dsp_clone_alloc;
2429 * Ok, so we're requesting unallocated vchan, but still
2430 * within maximum vchan limit.
2432 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
2433 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
2434 cunit < snd_maxautovchans) {
2435 udcmask &= ~snd_c2unit(cunit);
2436 tumax = snd_clone_getmaxunit(d->clones);
2438 snd_clone_setmaxunit(d->clones, cunit);
2441 goto dsp_clone_alloc;
2443 PCM_RELEASE_QUICK(d);
2448 ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
2450 snd_clone_setmaxunit(d->clones, tumax);
2452 udcmask |= snd_c2unit(cunit);
2453 *dev = make_dev(&dsp_cdevsw, PCMMINOR(udcmask),
2454 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
2455 devname, unit, devsep, cunit);
2456 snd_clone_register(ce, *dev);
2459 PCM_RELEASE_QUICK(d);
2466 dsp_sysinit(void *p)
2468 if (dsp_ehtag != NULL)
2470 /* initialize unit numbering */
2472 dsp_umax = PCMMAXUNIT;
2473 dsp_cmax = PCMMAXCHAN;
2474 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2478 dsp_sysuninit(void *p)
2480 if (dsp_ehtag == NULL)
2482 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2486 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2487 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2490 dsp_unit2name(char *buf, size_t len, int unit)
2494 KASSERT(buf != NULL && len != 0,
2495 ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2497 dtype = snd_unit2d(unit);
2499 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2500 if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2502 snprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
2503 snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
2511 * @brief Handler for SNDCTL_AUDIOINFO.
2513 * Gathers information about the audio device specified in ai->dev. If
2514 * ai->dev == -1, then this function gathers information about the current
2515 * device. If the call comes in on a non-audio device and ai->dev == -1,
2518 * This routine is supposed to go practically straight to the hardware,
2519 * getting capabilities directly from the sound card driver, side-stepping
2520 * the intermediate channel interface.
2522 * Note, however, that the usefulness of this command is significantly
2523 * decreased when requesting info about any device other than the one serving
2524 * the request. While each snddev_channel refers to a specific device node,
2525 * the converse is *not* true. Currently, when a sound device node is opened,
2526 * the sound subsystem scans for an available audio channel (or channels, if
2527 * opened in read+write) and then assigns them to the si_drv[12] private
2528 * data fields. As a result, any information returned linking a channel to
2529 * a specific character device isn't necessarily accurate.
2532 * Calling threads must not hold any snddev_info or pcm_channel locks.
2534 * @param dev device on which the ioctl was issued
2535 * @param ai ioctl request data container
2538 * @retval EINVAL ai->dev specifies an invalid device
2540 * @todo Verify correctness of Doxygen tags. ;)
2543 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2545 struct pcmchan_caps *caps;
2546 struct pcm_channel *ch;
2547 struct snddev_info *d;
2549 int i, nchan, *rates, minch, maxch;
2550 char *devname, buf[CHN_NAMELEN];
2553 * If probing the device that received the ioctl, make sure it's a
2554 * DSP device. (Users may use this ioctl with /dev/mixer and
2557 if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw)
2563 bzero(buf, sizeof(buf));
2566 * Search for the requested audio device (channel). Start by
2567 * iterating over pcm devices.
2569 for (i = 0; pcm_devclass != NULL &&
2570 i < devclass_get_maxunit(pcm_devclass); i++) {
2571 d = devclass_get_softc(pcm_devclass, i);
2572 if (!PCM_REGISTERED(d))
2575 /* XXX Need Giant magic entry ??? */
2577 /* See the note in function docblock */
2578 PCM_UNLOCKASSERT(d);
2581 CHN_FOREACH(ch, d, channels.pcm) {
2582 CHN_UNLOCKASSERT(ch);
2584 if (ai->dev == -1) {
2585 if (DSP_REGISTERED(d, i_dev) &&
2586 (ch == PCM_RDCH(i_dev) || /* record ch */
2587 ch == PCM_WRCH(i_dev))) { /* playback ch */
2588 devname = dsp_unit2name(buf,
2589 sizeof(buf), ch->unit);
2591 } else if (ai->dev == nchan) {
2592 devname = dsp_unit2name(buf, sizeof(buf),
2595 if (devname != NULL)
2601 if (devname != NULL) {
2603 * At this point, the following synchronization stuff
2605 * - a specific PCM device is locked.
2606 * - a specific audio channel has been locked, so be
2607 * sure to unlock when exiting;
2610 caps = chn_getcaps(ch);
2613 * With all handles collected, zero out the user's
2614 * container and begin filling in its fields.
2616 bzero((void *)ai, sizeof(oss_audioinfo));
2619 strlcpy(ai->name, ch->name, sizeof(ai->name));
2621 if ((ch->flags & CHN_F_BUSY) == 0)
2624 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2628 * @c cmd - OSSv4 docs: "Only supported under Linux at
2629 * this moment." Cop-out, I know, but I'll save
2630 * running around in the process table for later.
2631 * Is there a risk of leaking information?
2636 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2637 * Note, however, that a single channel operates in
2638 * only one direction, so PCM_CAP_DUPLEX is out.
2641 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2642 * these in pcmchan::caps?
2644 ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2645 ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2648 * Collect formats supported @b natively by the
2649 * device. Also determine min/max channels. (I.e.,
2650 * mono, stereo, or both?)
2652 * If any channel is stereo, maxch = 2;
2653 * if all channels are stereo, minch = 2, too;
2654 * if any channel is mono, minch = 1;
2655 * and if all channels are mono, maxch = 1.
2660 for (i = 0; caps->fmtlist[i]; i++) {
2661 fmts |= caps->fmtlist[i];
2662 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2663 minch = (minch == 0) ? 2 : minch;
2667 maxch = (maxch == 0) ? 1 : maxch;
2671 if (ch->direction == PCMDIR_PLAY)
2672 ai->oformats = fmts;
2674 ai->iformats = fmts;
2678 * @c magic - OSSv4 docs: "Reserved for internal use
2682 * @c card_number - OSSv4 docs: "Number of the sound
2683 * card where this device belongs or -1 if this
2684 * information is not available. Applications
2685 * should normally not use this field for any
2688 ai->card_number = -1;
2690 * @todo @c song_name - depends first on
2691 * SNDCTL_[GS]ETSONG @todo @c label - depends
2692 * on SNDCTL_[GS]ETLABEL
2693 * @todo @c port_number - routing information?
2695 ai->port_number = -1;
2696 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2699 * @c real_device - OSSv4 docs: "Obsolete."
2701 ai->real_device = -1;
2702 strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
2703 strlcat(ai->devnode, devname, sizeof(ai->devnode));
2704 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2707 * @c flags - OSSv4 docs: "Reserved for future use."
2710 * @c binding - OSSv4 docs: "Reserved for future use."
2712 * @todo @c handle - haven't decided how to generate
2713 * this yet; bus, vendor, device IDs?
2715 ai->min_rate = caps->minspeed;
2716 ai->max_rate = caps->maxspeed;
2718 ai->min_channels = minch;
2719 ai->max_channels = maxch;
2721 ai->nrates = chn_getrates(ch, &rates);
2722 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2723 ai->nrates = OSS_MAX_SAMPLE_RATES;
2725 for (i = 0; i < ai->nrates; i++)
2726 ai->rates[i] = rates[i];
2728 ai->next_play_engine = 0;
2729 ai->next_rec_engine = 0;
2736 if (devname != NULL)
2740 /* Exhausted the search -- nothing is locked, so return. */
2745 * @brief Assigns a PCM channel to a sync group.
2747 * Sync groups are used to enable audio operations on multiple devices
2748 * simultaneously. They may be used with any number of devices and may
2749 * span across applications. Devices are added to groups with
2750 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2751 * SNDCTL_DSP_SYNCSTART ioctl.
2753 * If the @c id field of the @c group parameter is set to zero, then a new
2754 * sync group is created. Otherwise, wrch and rdch (if set) are added to
2755 * the group specified.
2757 * @todo As far as memory allocation, should we assume that things are
2758 * okay and allocate with M_WAITOK before acquiring channel locks,
2759 * freeing later if not?
2761 * @param wrch output channel associated w/ device (if any)
2762 * @param rdch input channel associated w/ device (if any)
2763 * @param group Sync group parameters
2766 * @retval non-zero error to be propagated upstream
2769 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2771 struct pcmchan_syncmember *smrd, *smwr;
2772 struct pcmchan_syncgroup *sg;
2781 * Free_unr() may sleep, so store released syncgroup IDs until after
2782 * all locks are released.
2784 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2789 * - Insert channel(s) into group's member list.
2790 * - Set CHN_F_NOTRIGGER on channel(s).
2791 * - Stop channel(s).
2795 * If device's channels are already mapped to a group, unmap them.
2799 sg_ids[0] = chn_syncdestroy(wrch);
2804 sg_ids[1] = chn_syncdestroy(rdch);
2808 * Verify that mode matches character device properites.
2809 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2810 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2812 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2813 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2819 * An id of zero indicates the user wants to create a new
2822 if (group->id == 0) {
2823 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2825 SLIST_INIT(&sg->members);
2826 sg->id = alloc_unr(pcmsg_unrhdr);
2829 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2833 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2834 if (sg->id == group->id)
2841 /* Couldn't create or find a syncgroup. Fail. */
2846 * Allocate a syncmember, assign it and a channel together, and
2847 * insert into syncgroup.
2849 if (group->mode & PCM_ENABLE_INPUT) {
2850 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2856 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2861 rdch->flags |= CHN_F_NOTRIGGER;
2865 if (group->mode & PCM_ENABLE_OUTPUT) {
2866 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2872 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2877 wrch->flags |= CHN_F_NOTRIGGER;
2885 free(smrd, M_DEVBUF);
2886 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2888 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2906 free_unr(pcmsg_unrhdr, sg_ids[0]);
2908 free_unr(pcmsg_unrhdr, sg_ids[1]);
2910 free_unr(pcmsg_unrhdr, sg_ids[2]);
2916 * @brief Launch a sync group into action
2918 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
2919 * iterates over all members, triggering them along the way.
2921 * @note Caller must not hold any channel locks.
2923 * @param sg_id sync group identifier
2926 * @retval non-zero error worthy of propagating upstream to user
2929 dsp_oss_syncstart(int sg_id)
2931 struct pcmchan_syncmember *sm, *sm_tmp;
2932 struct pcmchan_syncgroup *sg;
2933 struct pcm_channel *c;
2936 /* Get the synclists lock */
2943 /* Search for syncgroup by ID */
2944 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2945 if (sg->id == sg_id)
2949 /* Return EINVAL if not found */
2955 /* Any removals resulting in an empty group should've handled this */
2956 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
2959 * Attempt to lock all member channels - if any are already
2960 * locked, unlock those acquired, sleep for a bit, and try
2963 SLIST_FOREACH(sm, &sg->members, link) {
2964 if (CHN_TRYLOCK(sm->ch) == 0) {
2965 int timo = hz * 5/1000;
2969 /* Release all locked channels so far, retry */
2970 SLIST_FOREACH(sm_tmp, &sg->members, link) {
2971 /* sm is the member already locked */
2974 CHN_UNLOCK(sm_tmp->ch);
2977 /** @todo Is PRIBIO correct/ */
2978 ret = msleep(sm, &snd_pcm_syncgroups_mtx,
2979 PRIBIO | PCATCH, "pcmsg", timo);
2980 if (ret == EINTR || ret == ERESTART)
2984 ret = 0; /* Assumes ret == EAGAIN... */
2987 } while (needlocks && ret == 0);
2989 /* Proceed only if no errors encountered. */
2991 /* Launch channels */
2992 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
2993 SLIST_REMOVE_HEAD(&sg->members, link);
2998 c->flags &= ~CHN_F_NOTRIGGER;
3004 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
3011 * Free_unr() may sleep, so be sure to give up the syncgroup lock
3015 free_unr(pcmsg_unrhdr, sg_id);
3021 * @brief Handler for SNDCTL_DSP_POLICY
3023 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
3024 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
3025 * specifying those two parameters, s/he simply selects a number from 0..10
3026 * which corresponds to a buffer size. Smaller numbers request smaller
3027 * buffers with lower latencies (at greater overhead from more frequent
3028 * interrupts), while greater numbers behave in the opposite manner.
3030 * The 4Front spec states that a value of 5 should be the default. However,
3031 * this implementation deviates slightly by using a linear scale without
3032 * consulting drivers. I.e., even though drivers may have different default
3033 * buffer sizes, a policy argument of 5 will have the same result across
3036 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
3039 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
3040 * work with hardware drivers directly.
3042 * @note PCM channel arguments must not be locked by caller.
3044 * @param wrch Pointer to opened playback channel (optional; may be NULL)
3045 * @param rdch " recording channel (optional; may be NULL)
3046 * @param policy Integer from [0:10]
3048 * @retval 0 constant (for now)
3051 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
3055 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
3058 /* Default: success */
3063 ret = chn_setlatency(rdch, policy);
3067 if (wrch && ret == 0) {
3069 ret = chn_setlatency(wrch, policy);
3080 * @brief Enable or disable "cooked" mode
3082 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
3083 * is the default, the sound system handles rate and format conversions
3084 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
3085 * operates with 44100Hz/16bit/signed samples).
3087 * Disabling cooked mode is intended for applications wanting to mmap()
3088 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
3089 * feeder architecture, presumably to gain as much control over audio
3090 * hardware as possible.
3092 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
3095 * @param wrch playback channel (optional; may be NULL)
3096 * @param rdch recording channel (optional; may be NULL)
3097 * @param enabled 0 = raw mode, 1 = cooked mode
3099 * @retval EINVAL Operation not yet supported.
3102 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
3106 * XXX I just don't get it. Why don't they call it
3107 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
3108 * This is just plain so confusing, incoherent,
3109 * <insert any non-printable characters here>.
3111 if (!(enabled == 1 || enabled == 0))
3115 * I won't give in. I'm inverting its logic here and now.
3116 * Brag all you want, but "BITPERFECT" should be the better
3119 enabled ^= 0x00000001;
3123 wrch->flags &= ~CHN_F_BITPERFECT;
3124 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3130 rdch->flags &= ~CHN_F_BITPERFECT;
3131 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3139 * @brief Retrieve channel interleaving order
3141 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
3143 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
3146 * @note As the ioctl definition is still under construction, FreeBSD
3147 * does not currently support SNDCTL_DSP_GET_CHNORDER.
3149 * @param wrch playback channel (optional; may be NULL)
3150 * @param rdch recording channel (optional; may be NULL)
3151 * @param map channel map (result will be stored there)
3153 * @retval EINVAL Operation not yet supported.
3156 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3158 struct pcm_channel *ch;
3161 ch = (wrch != NULL) ? wrch : rdch;
3164 ret = chn_oss_getorder(ch, map);
3173 * @brief Specify channel interleaving order
3175 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
3177 * @note As the ioctl definition is still under construction, FreeBSD
3178 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
3180 * @param wrch playback channel (optional; may be NULL)
3181 * @param rdch recording channel (optional; may be NULL)
3182 * @param map channel map
3184 * @retval EINVAL Operation not yet supported.
3187 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3195 ret = chn_oss_setorder(wrch, map);
3199 if (ret == 0 && rdch != NULL) {
3201 ret = chn_oss_setorder(rdch, map);
3209 dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
3212 struct pcm_channel *ch;
3217 ch = (wrch != NULL) ? wrch : rdch;
3221 ret = chn_oss_getmask(ch, &chnmask);
3232 #ifdef OSSV4_EXPERIMENT
3234 * @brief Retrieve an audio device's label
3236 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
3238 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3241 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
3242 * consoles) can show variable "labels" for certain controls. By default
3243 * the application name (say quake) is shown as the label but
3244 * applications may change the labels themselves."
3246 * @note As the ioctl definition is still under construction, FreeBSD
3247 * does not currently support @c SNDCTL_GETLABEL.
3249 * @param wrch playback channel (optional; may be NULL)
3250 * @param rdch recording channel (optional; may be NULL)
3251 * @param label label gets copied here
3253 * @retval EINVAL Operation not yet supported.
3256 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3262 * @brief Specify an audio device's label
3264 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
3265 * comments for @c dsp_oss_getlabel immediately above.
3267 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3270 * @note As the ioctl definition is still under construction, FreeBSD
3271 * does not currently support SNDCTL_SETLABEL.
3273 * @param wrch playback channel (optional; may be NULL)
3274 * @param rdch recording channel (optional; may be NULL)
3275 * @param label label gets copied from here
3277 * @retval EINVAL Operation not yet supported.
3280 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3286 * @brief Retrieve name of currently played song
3288 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
3289 * tell the system the name of the currently playing song, which would be
3290 * visible in @c /dev/sndstat.
3292 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
3295 * @note As the ioctl definition is still under construction, FreeBSD
3296 * does not currently support SNDCTL_GETSONG.
3298 * @param wrch playback channel (optional; may be NULL)
3299 * @param rdch recording channel (optional; may be NULL)
3300 * @param song song name gets copied here
3302 * @retval EINVAL Operation not yet supported.
3305 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3311 * @brief Retrieve name of currently played song
3313 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
3314 * tell the system the name of the currently playing song, which would be
3315 * visible in @c /dev/sndstat.
3317 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
3320 * @note As the ioctl definition is still under construction, FreeBSD
3321 * does not currently support SNDCTL_SETSONG.
3323 * @param wrch playback channel (optional; may be NULL)
3324 * @param rdch recording channel (optional; may be NULL)
3325 * @param song song name gets copied from here
3327 * @retval EINVAL Operation not yet supported.
3330 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3336 * @brief Rename a device
3338 * This is a handler for the @c SNDCTL_SETNAME ioctl.
3340 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
3343 * From Hannu@4Front: "This call is used to change the device name
3344 * reported in /dev/sndstat and ossinfo. So instead of using some generic
3345 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
3346 * name depending on the current context (for example 'OSS virtual wave table
3347 * synth' or 'VoIP link to London')."
3349 * @note As the ioctl definition is still under construction, FreeBSD
3350 * does not currently support SNDCTL_SETNAME.
3352 * @param wrch playback channel (optional; may be NULL)
3353 * @param rdch recording channel (optional; may be NULL)
3354 * @param name new device name gets copied from here
3356 * @retval EINVAL Operation not yet supported.
3359 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
3363 #endif /* !OSSV4_EXPERIMENT */