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>
35 #include <sys/sysent.h>
38 #include <vm/vm_object.h>
39 #include <vm/vm_page.h>
40 #include <vm/vm_pager.h>
42 SND_DECLARE_FILE("$FreeBSD$");
44 static int dsp_mmap_allow_prot_exec = 0;
45 SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RW,
46 &dsp_mmap_allow_prot_exec, 0,
47 "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
50 struct pcm_channel *rdch, *wrch;
51 struct pcm_channel *volch;
53 TAILQ_ENTRY(dsp_cdevinfo) link;
56 #define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
57 #define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
58 #define PCM_VOLCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
59 #define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
61 #define DSP_CDEVINFO_CACHESIZE 8
63 #define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \
64 (y) != NULL && (y)->si_drv1 != NULL)
68 static d_open_t dsp_open;
69 static d_close_t dsp_close;
70 static d_read_t dsp_read;
71 static d_write_t dsp_write;
72 static d_ioctl_t dsp_ioctl;
73 static d_poll_t dsp_poll;
74 static d_mmap_t dsp_mmap;
75 static d_mmap_single_t dsp_mmap_single;
77 struct cdevsw dsp_cdevsw = {
78 .d_version = D_VERSION,
86 .d_mmap_single = dsp_mmap_single,
90 static eventhandler_tag dsp_ehtag = NULL;
91 static int dsp_umax = -1;
92 static int dsp_cmax = -1;
94 static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
95 static int dsp_oss_syncstart(int sg_id);
96 static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
97 static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
98 static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
99 static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
100 static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
101 #ifdef OSSV4_EXPERIMENT
102 static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
103 static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
104 static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
105 static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
106 static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
109 static struct snddev_info *
110 dsp_get_info(struct cdev *dev)
112 return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
116 dsp_get_flags(struct cdev *dev)
120 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
122 return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
126 dsp_set_flags(struct cdev *dev, uint32_t flags)
130 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
133 pcm_setflags(bdev, flags);
137 * return the channels associated with an open device instance.
138 * lock channels specified.
141 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
144 struct snddev_info *d;
145 struct pcm_channel *ch;
148 if (PCM_SIMPLEX(dev) != 0) {
149 d = dsp_get_info(dev);
150 if (!PCM_REGISTERED(d))
156 * Note: order is important -
157 * pcm flags -> prio query flags -> wild guess
160 flags = dsp_get_flags(dev);
161 if (flags & SD_F_PRIO_WR) {
163 PCM_RDCH(dev) = NULL;
164 } else if (flags & SD_F_PRIO_RD) {
166 PCM_WRCH(dev) = NULL;
167 } else if (prio & SD_F_PRIO_WR) {
169 PCM_RDCH(dev) = NULL;
170 flags |= SD_F_PRIO_WR;
171 } else if (prio & SD_F_PRIO_RD) {
173 PCM_WRCH(dev) = NULL;
174 flags |= SD_F_PRIO_RD;
175 } else if (PCM_WRCH(dev) != NULL) {
177 PCM_RDCH(dev) = NULL;
178 flags |= SD_F_PRIO_WR;
179 } else if (PCM_RDCH(dev) != NULL) {
181 PCM_WRCH(dev) = NULL;
182 flags |= SD_F_PRIO_RD;
184 PCM_SIMPLEX(dev) = 0;
185 dsp_set_flags(dev, flags);
195 *rdch = PCM_RDCH(dev);
196 *wrch = PCM_WRCH(dev);
198 if (*rdch != NULL && (prio & SD_F_PRIO_RD))
200 if (*wrch != NULL && (prio & SD_F_PRIO_WR))
206 /* unlock specified channels */
208 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
211 if (wrch != NULL && (prio & SD_F_PRIO_WR))
213 if (rdch != NULL && (prio & SD_F_PRIO_RD))
218 dsp_cdevinfo_alloc(struct cdev *dev,
219 struct pcm_channel *rdch, struct pcm_channel *wrch,
220 struct pcm_channel *volch)
222 struct snddev_info *d;
223 struct dsp_cdevinfo *cdi;
226 d = dsp_get_info(dev);
228 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
229 ((rdch == NULL && wrch == NULL) || rdch != wrch),
230 ("bogus %s(), what are you trying to accomplish here?", __func__));
234 simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
237 * Scan for free instance entry and put it into the end of list.
238 * Create new one if necessary.
240 TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
246 cdi->simplex = simplex;
248 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
249 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
254 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
259 cdi->simplex = simplex;
261 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
266 dsp_cdevinfo_free(struct cdev *dev)
268 struct snddev_info *d;
269 struct dsp_cdevinfo *cdi, *tmp;
273 d = dsp_get_info(dev);
275 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
276 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
277 PCM_VOLCH(dev) == NULL,
278 ("bogus %s(), what are you trying to accomplish here?", __func__));
291 * Once it is free, move it back to the beginning of list for
292 * faster new entry allocation.
294 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
295 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
298 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
299 * Reset simplex flags.
301 flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
302 i = DSP_CDEVINFO_CACHESIZE;
303 TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
304 if (cdi->busy != 0) {
305 if (cdi->simplex == 0) {
306 if (cdi->rdch != NULL)
307 flags |= SD_F_PRIO_RD;
308 if (cdi->wrch != NULL)
309 flags |= SD_F_PRIO_WR;
313 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
319 dsp_set_flags(dev, flags);
323 dsp_cdevinfo_init(struct snddev_info *d)
325 struct dsp_cdevinfo *cdi;
328 KASSERT(d != NULL, ("NULL snddev_info"));
332 TAILQ_INIT(&d->dsp_cdevinfo_pool);
333 for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
334 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
335 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
340 dsp_cdevinfo_flush(struct snddev_info *d)
342 struct dsp_cdevinfo *cdi, *tmp;
344 KASSERT(d != NULL, ("NULL snddev_info"));
348 cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
349 while (cdi != NULL) {
350 tmp = TAILQ_NEXT(cdi, link);
354 TAILQ_INIT(&d->dsp_cdevinfo_pool);
357 /* duplex / simplex cdev type */
359 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
360 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
361 DSP_CDEV_TYPE_RDWR /* duplex read, write, or both */
365 DSP_CDEV_VOLCTL_NONE,
366 DSP_CDEV_VOLCTL_READ,
367 DSP_CDEV_VOLCTL_WRITE
370 #define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
371 #define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
372 #define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
373 #define DSP_F_READ(x) ((x) & FREAD)
374 #define DSP_F_WRITE(x) ((x) & FWRITE)
376 static const struct {
388 { SND_DEV_DSP, "dsp", ".", NULL, 0, 0, 0, 0,
389 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
390 DSP_CDEV_TYPE_RDWR },
391 { SND_DEV_AUDIO, "audio", ".", NULL, 0, 0, 0, 0,
392 SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
393 DSP_CDEV_TYPE_RDWR },
394 { SND_DEV_DSP16, "dspW", ".", NULL, 0, 0, 0, 0,
395 SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
396 DSP_CDEV_TYPE_RDWR },
397 { SND_DEV_DSPHW_PLAY, "dsp", ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
398 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
399 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
400 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
401 { SND_DEV_DSPHW_REC, "dsp", ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
402 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
403 { SND_DEV_DSPHW_VREC, "dsp", ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
404 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
405 { SND_DEV_DSPHW_CD, "dspcd", ".", NULL, 0, 0, 0, 0,
406 SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR },
407 /* Low priority, OSSv4 aliases. */
408 { SND_DEV_DSP, "dsp_ac3", ".", "dsp", 0, 0, 0, 0,
409 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
410 DSP_CDEV_TYPE_RDWR },
411 { SND_DEV_DSP, "dsp_mmap", ".", "dsp", 0, 0, 0, 0,
412 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
413 DSP_CDEV_TYPE_RDWR },
414 { SND_DEV_DSP, "dsp_multich", ".", "dsp", 0, 0, 0, 0,
415 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
416 DSP_CDEV_TYPE_RDWR },
417 { SND_DEV_DSP, "dsp_spdifout", ".", "dsp", 0, 0, 0, 0,
418 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
419 DSP_CDEV_TYPE_RDWR },
420 { SND_DEV_DSP, "dsp_spdifin", ".", "dsp", 0, 0, 0, 0,
421 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
422 DSP_CDEV_TYPE_RDWR },
425 #define DSP_FIXUP_ERROR() do { \
426 prio = dsp_get_flags(i_dev); \
427 if (!DSP_F_VALID(flags)) \
429 if (!DSP_F_DUPLEX(flags) && \
430 ((DSP_F_READ(flags) && d->reccount == 0) || \
431 (DSP_F_WRITE(flags) && d->playcount == 0))) \
433 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
434 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
435 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
437 else if (DSP_REGISTERED(d, i_dev)) \
442 dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
444 struct pcm_channel *rdch, *wrch;
445 struct snddev_info *d;
446 uint32_t fmt, spd, prio, volctl;
447 int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
449 /* Kind of impossible.. */
450 if (i_dev == NULL || td == NULL)
453 d = dsp_get_info(i_dev);
454 if (!PCM_REGISTERED(d))
459 /* Lock snddev so nobody else can monkey with it. */
464 * Try to acquire cloned device before someone else pick it.
465 * ENODEV means this is not a cloned droids.
467 error = snd_clone_acquire(i_dev);
468 if (!(error == 0 || error == ENODEV)) {
479 (void)snd_clone_release(i_dev);
486 * That is just enough. Acquire and unlock pcm lock so
487 * the other will just have to wait until we finish doing
493 devtype = PCMDEV(i_dev);
498 volctl = DSP_CDEV_VOLCTL_NONE;
500 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
501 if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
504 * Volume control only valid for DSPHW devices,
505 * and it must be opened in opposite direction be it
506 * simplex or duplex. Anything else will be handled
509 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) {
510 if (dsp_cdevs[i].volctl != 0 &&
512 volctl = DSP_CDEV_VOLCTL_WRITE;
516 if (DSP_F_READ(flags)) {
517 (void)snd_clone_release(i_dev);
518 PCM_RELEASE_QUICK(d);
522 wdevunit = dev2unit(i_dev);
523 } else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
524 if (dsp_cdevs[i].volctl != 0 &&
525 DSP_F_WRITE(flags)) {
526 volctl = DSP_CDEV_VOLCTL_READ;
530 if (DSP_F_WRITE(flags)) {
531 (void)snd_clone_release(i_dev);
532 PCM_RELEASE_QUICK(d);
536 rdevunit = dev2unit(i_dev);
538 fmt = dsp_cdevs[i].fmt;
539 spd = dsp_cdevs[i].spd;
543 /* No matching devtype? */
544 if (fmt == 0 || spd == 0)
545 panic("impossible devtype %d", devtype);
553 * if we get here, the open request is valid- either:
554 * * we were previously not open
555 * * we were open for play xor record and the opener wants
556 * the non-open direction
558 if (DSP_F_READ(flags)) {
560 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
561 td->td_proc->p_pid, td->td_proc->p_comm, rdevunit);
563 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
566 if (volctl == DSP_CDEV_VOLCTL_READ)
571 pcm_chnrelease(rdch);
572 if (!DSP_F_DUPLEX(flags)) {
573 (void)snd_clone_release(i_dev);
574 PCM_RELEASE_QUICK(d);
579 } else if (volctl == DSP_CDEV_VOLCTL_READ) {
582 pcm_chnrelease(rdch);
585 if (flags & O_NONBLOCK)
586 rdch->flags |= CHN_F_NBIO;
588 rdch->flags |= CHN_F_EXCLUSIVE;
590 if (volctl == DSP_CDEV_VOLCTL_NONE)
591 chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
596 if (DSP_F_WRITE(flags)) {
598 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
599 td->td_proc->p_pid, td->td_proc->p_comm, wdevunit);
601 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
604 if (volctl == DSP_CDEV_VOLCTL_WRITE)
609 pcm_chnrelease(wrch);
610 if (!DSP_F_DUPLEX(flags)) {
613 * Lock, deref and release previously
614 * created record channel
617 pcm_chnref(rdch, -1);
618 pcm_chnrelease(rdch);
620 (void)snd_clone_release(i_dev);
621 PCM_RELEASE_QUICK(d);
626 } else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
629 pcm_chnrelease(wrch);
632 if (flags & O_NONBLOCK)
633 wrch->flags |= CHN_F_NBIO;
635 wrch->flags |= CHN_F_EXCLUSIVE;
637 if (volctl == DSP_CDEV_VOLCTL_NONE)
638 chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
647 * We're done. Allocate channels information for this cdev.
650 case DSP_CDEV_VOLCTL_READ:
651 KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
652 dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
654 case DSP_CDEV_VOLCTL_WRITE:
655 KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
656 dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
658 case DSP_CDEV_VOLCTL_NONE:
660 if (wrch == NULL && rdch == NULL) {
661 (void)snd_clone_release(i_dev);
671 dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
673 CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
675 CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
680 * Increase clone refcount for its automatic garbage collector.
682 (void)snd_clone_ref(i_dev);
693 dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
695 struct pcm_channel *rdch, *wrch, *volch;
696 struct snddev_info *d;
697 int sg_ids, rdref, wdref;
699 d = dsp_get_info(i_dev);
700 if (!DSP_REGISTERED(d, i_dev))
709 rdch = PCM_RDCH(i_dev);
710 wrch = PCM_WRCH(i_dev);
711 volch = PCM_VOLCH(i_dev);
713 PCM_RDCH(i_dev) = NULL;
714 PCM_WRCH(i_dev) = NULL;
715 PCM_VOLCH(i_dev) = NULL;
723 else if (volch == wrch)
727 pcm_chnref(volch, -1);
733 CHN_REMOVE(d, rdch, channels.pcm.opened);
735 CHN_REMOVE(d, wrch, channels.pcm.opened);
737 if (rdch != NULL || wrch != NULL) {
741 * The channel itself need not be locked because:
742 * a) Adding a channel to a syncgroup happens only
743 * in dsp_ioctl(), which cannot run concurrently
745 * b) The syncmember pointer (sm) is protected by
746 * the global syncgroup list lock.
747 * c) A channel can't just disappear, invalidating
748 * pointers, unless it's closed/dereferenced
752 sg_ids = chn_syncdestroy(rdch);
755 free_unr(pcmsg_unrhdr, sg_ids);
758 pcm_chnref(rdch, rdref);
759 chn_abort(rdch); /* won't sleep */
760 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
761 CHN_F_DEAD | CHN_F_EXCLUSIVE);
762 chn_reset(rdch, 0, 0);
763 pcm_chnrelease(rdch);
767 * Please see block above.
770 sg_ids = chn_syncdestroy(wrch);
773 free_unr(pcmsg_unrhdr, sg_ids);
776 pcm_chnref(wrch, wdref);
777 chn_flush(wrch); /* may sleep */
778 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
779 CHN_F_DEAD | CHN_F_EXCLUSIVE);
780 chn_reset(wrch, 0, 0);
781 pcm_chnrelease(wrch);
786 dsp_cdevinfo_free(i_dev);
788 * Release clone busy state and unref it so the automatic
789 * garbage collector will get the hint and do the remaining
792 (void)snd_clone_release(i_dev);
795 * destroy_dev() might sleep, so release pcm lock
796 * here and rely on pcm cv serialization.
799 (void)snd_clone_unref(i_dev);
811 dsp_io_ops(struct cdev *i_dev, struct uio *buf)
813 struct snddev_info *d;
814 struct pcm_channel **ch, *rdch, *wrch;
815 int (*chn_io)(struct pcm_channel *, struct uio *);
819 KASSERT(i_dev != NULL && buf != NULL &&
820 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
821 ("%s(): io train wreck!", __func__));
823 d = dsp_get_info(i_dev);
824 if (!DSP_REGISTERED(d, i_dev))
829 switch (buf->uio_rw) {
841 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
847 runpid = buf->uio_td->td_proc->p_pid;
849 getchns(i_dev, &rdch, &wrch, prio);
851 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
856 if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
857 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
858 relchns(i_dev, rdch, wrch, prio);
861 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
862 (*ch)->flags |= CHN_F_RUNNING;
867 * chn_read/write must give up channel lock in order to copy bytes
868 * from/to userland, so up the "in progress" counter to make sure
869 * someone else doesn't come along and muss up the buffer.
872 ret = chn_io(*ch, buf);
875 CHN_BROADCAST(&(*ch)->cv);
877 relchns(i_dev, rdch, wrch, prio);
885 dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
887 return (dsp_io_ops(i_dev, buf));
891 dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
893 return (dsp_io_ops(i_dev, buf));
897 dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
899 struct snddev_info *d;
900 struct pcm_channel *c;
903 KASSERT(dev != NULL && volch != NULL,
904 ("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
906 d = dsp_get_info(dev);
907 if (!PCM_REGISTERED(d)) {
918 if (!(c->feederflags & (1 << FEEDER_VOLUME)))
928 unit = dev2unit(dev);
930 CHN_FOREACH(c, d, channels.pcm) {
932 if (c->unit != unit) {
942 return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
952 dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
955 struct snddev_info *d;
956 struct pcm_channel *rdch, *wrch;
959 d = dsp_get_info(dev);
960 if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
967 rdch = PCM_RDCH(dev);
968 wrch = PCM_WRCH(dev);
970 /* No specific channel, look into cache */
972 volch = PCM_VOLCH(dev);
976 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
978 else if (j == SOUND_MIXER_PCM && wrch != NULL)
982 devtype = PCMDEV(dev);
984 /* Look super harder */
986 (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
987 devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
988 ret = dsp_get_volume_channel(dev, &volch);
995 /* Final validation */
998 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
1002 if (volch->direction == PCMDIR_PLAY)
1010 if (volch != NULL &&
1011 ((j == SOUND_MIXER_PCM && volch->direction == PCMDIR_PLAY) ||
1012 (j == SOUND_MIXER_RECLEV && volch->direction == PCMDIR_REC))) {
1013 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1014 int left, right, center;
1016 left = *(int *)arg & 0x7f;
1017 right = ((*(int *)arg) >> 8) & 0x7f;
1018 center = (left + right) >> 1;
1019 chn_setvolume_multi(volch, SND_VOL_C_PCM, left, right,
1021 } else if ((cmd & ~0xff) == MIXER_READ(0)) {
1022 *(int *)arg = CHN_GETVOLUME(volch,
1023 SND_VOL_C_PCM, SND_CHN_T_FL);
1024 *(int *)arg |= CHN_GETVOLUME(volch,
1025 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1028 } else if (rdch != NULL || wrch != NULL) {
1030 case SOUND_MIXER_DEVMASK:
1031 case SOUND_MIXER_CAPS:
1032 case SOUND_MIXER_STEREODEVS:
1033 if ((cmd & ~0xff) == MIXER_READ(0)) {
1036 *(int *)arg |= SOUND_MASK_RECLEV;
1038 *(int *)arg |= SOUND_MASK_PCM;
1042 case SOUND_MIXER_RECMASK:
1043 case SOUND_MIXER_RECSRC:
1044 if ((cmd & ~0xff) == MIXER_READ(0))
1060 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1063 struct pcm_channel *chn, *rdch, *wrch;
1064 struct snddev_info *d;
1066 int *arg_i, ret, tmp;
1068 d = dsp_get_info(i_dev);
1069 if (!DSP_REGISTERED(d, i_dev))
1079 if (IOCGROUP(cmd) == 'M') {
1080 if (cmd == OSS_GETVERSION) {
1081 *arg_i = SOUND_VERSION;
1085 ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
1091 if (d->mixer_dev != NULL) {
1092 PCM_ACQUIRE_QUICK(d);
1093 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1095 PCM_RELEASE_QUICK(d);
1105 * Certain ioctls may be made on any type of device (audio, mixer,
1106 * and MIDI). Handle those special cases here.
1108 if (IOCGROUP(cmd) == 'X') {
1109 PCM_ACQUIRE_QUICK(d);
1111 case SNDCTL_SYSINFO:
1112 sound_oss_sysinfo((oss_sysinfo *)arg);
1114 case SNDCTL_CARDINFO:
1115 ret = sound_oss_card_info((oss_card_info *)arg);
1117 case SNDCTL_AUDIOINFO:
1118 case SNDCTL_AUDIOINFO_EX:
1119 case SNDCTL_ENGINEINFO:
1120 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
1122 case SNDCTL_MIXERINFO:
1123 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
1128 PCM_RELEASE_QUICK(d);
1133 getchns(i_dev, &rdch, &wrch, 0);
1135 if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
1137 if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
1140 if (wrch == NULL && rdch == NULL) {
1148 * we start with the new ioctl interface.
1150 case AIONWRITE: /* how many bytes can write ? */
1154 if (wrch && wrch->bufhard.dl)
1155 while (chn_wrfeed(wrch) == 0);
1157 *arg_i = sndbuf_getfree(wrch->bufsoft);
1165 case AIOSSIZE: /* set the current blocksize */
1167 struct snd_size *p = (struct snd_size *)arg;
1171 PCM_ACQUIRE_QUICK(d);
1174 chn_setblocksize(wrch, 2, p->play_size);
1175 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1180 chn_setblocksize(rdch, 2, p->rec_size);
1181 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1184 PCM_RELEASE_QUICK(d);
1187 case AIOGSIZE: /* get the current blocksize */
1189 struct snd_size *p = (struct snd_size *)arg;
1193 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1198 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1207 snd_chan_param *p = (snd_chan_param *)arg;
1209 if (cmd == AIOSFMT &&
1210 ((p->play_format != 0 && p->play_rate == 0) ||
1211 (p->rec_format != 0 && p->rec_rate == 0))) {
1215 PCM_ACQUIRE_QUICK(d);
1218 if (cmd == AIOSFMT && p->play_format != 0) {
1220 SND_FORMAT(p->play_format,
1221 AFMT_CHANNEL(wrch->format),
1222 AFMT_EXTCHANNEL(wrch->format)));
1223 chn_setspeed(wrch, p->play_rate);
1225 p->play_rate = wrch->speed;
1226 p->play_format = AFMT_ENCODING(wrch->format);
1234 if (cmd == AIOSFMT && p->rec_format != 0) {
1236 SND_FORMAT(p->rec_format,
1237 AFMT_CHANNEL(rdch->format),
1238 AFMT_EXTCHANNEL(rdch->format)));
1239 chn_setspeed(rdch, p->rec_rate);
1241 p->rec_rate = rdch->speed;
1242 p->rec_format = AFMT_ENCODING(rdch->format);
1248 PCM_RELEASE_QUICK(d);
1252 case AIOGCAP: /* get capabilities */
1254 snd_capabilities *p = (snd_capabilities *)arg;
1255 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
1261 rcaps = chn_getcaps(rdch);
1265 pcaps = chn_getcaps(wrch);
1267 p->rate_min = max(rcaps? rcaps->minspeed : 0,
1268 pcaps? pcaps->minspeed : 0);
1269 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
1270 pcaps? pcaps->maxspeed : 1000000);
1271 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
1272 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
1273 /* XXX bad on sb16 */
1274 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
1275 (wrch? chn_getformats(wrch) : 0xffffffff);
1277 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
1278 pdev = d->mixer_dev;
1279 p->mixers = 1; /* default: one mixer */
1280 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
1281 p->left = p->right = 100;
1291 if (*arg_i == AIOSYNC_PLAY && wrch) {
1293 *arg_i = chn_abort(wrch);
1295 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
1297 *arg_i = chn_abort(rdch);
1300 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
1306 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
1307 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
1311 * here follow the standard ioctls (filio.h etc.)
1313 case FIONREAD: /* get # bytes to read */
1316 /* if (rdch && rdch->bufhard.dl)
1317 while (chn_rdfeed(rdch) == 0);
1319 *arg_i = sndbuf_getready(rdch->bufsoft);
1327 case FIOASYNC: /*set/clear async i/o */
1328 DEB( printf("FIOASYNC\n") ; )
1331 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1332 case FIONBIO: /* set/clear non-blocking i/o */
1335 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1336 rdch->flags |= CHN_F_NBIO;
1338 rdch->flags &= ~CHN_F_NBIO;
1343 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1344 wrch->flags |= CHN_F_NBIO;
1346 wrch->flags &= ~CHN_F_NBIO;
1352 * Finally, here is the linux-compatible ioctl interface
1354 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1355 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
1356 case SNDCTL_DSP_GETBLKSIZE:
1357 chn = wrch ? wrch : rdch;
1360 *arg_i = sndbuf_getblksz(chn->bufsoft);
1368 case SNDCTL_DSP_SETBLKSIZE:
1369 RANGE(*arg_i, 16, 65536);
1370 PCM_ACQUIRE_QUICK(d);
1373 chn_setblocksize(wrch, 2, *arg_i);
1378 chn_setblocksize(rdch, 2, *arg_i);
1381 PCM_RELEASE_QUICK(d);
1384 case SNDCTL_DSP_RESET:
1385 DEB(printf("dsp reset\n"));
1400 case SNDCTL_DSP_SYNC:
1401 DEB(printf("dsp sync\n"));
1402 /* chn_sync may sleep */
1410 case SNDCTL_DSP_SPEED:
1411 /* chn_setspeed may sleep */
1413 PCM_ACQUIRE_QUICK(d);
1416 ret = chn_setspeed(wrch, *arg_i);
1420 if (rdch && ret == 0) {
1422 ret = chn_setspeed(rdch, *arg_i);
1427 PCM_RELEASE_QUICK(d);
1431 case SOUND_PCM_READ_RATE:
1432 chn = wrch ? wrch : rdch;
1435 *arg_i = chn->speed;
1443 case SNDCTL_DSP_STEREO:
1445 *arg_i = (*arg_i)? 2 : 1;
1446 PCM_ACQUIRE_QUICK(d);
1449 ret = chn_setformat(wrch,
1450 SND_FORMAT(wrch->format, *arg_i, 0));
1451 tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1454 if (rdch && ret == 0) {
1456 ret = chn_setformat(rdch,
1457 SND_FORMAT(rdch->format, *arg_i, 0));
1459 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1462 PCM_RELEASE_QUICK(d);
1466 case SOUND_PCM_WRITE_CHANNELS:
1467 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1474 struct pcmchan_matrix *m;
1478 if (*arg_i > SND_CHN_MAX)
1479 *arg_i = SND_CHN_MAX;
1481 m = feeder_matrix_default_channel_map(*arg_i);
1487 PCM_ACQUIRE_QUICK(d);
1490 ret = chn_setformat(wrch,
1491 SND_FORMAT(wrch->format, *arg_i, ext));
1492 tmp = AFMT_CHANNEL(wrch->format);
1495 if (rdch && ret == 0) {
1497 ret = chn_setformat(rdch,
1498 SND_FORMAT(rdch->format, *arg_i, ext));
1500 tmp = AFMT_CHANNEL(rdch->format);
1503 PCM_RELEASE_QUICK(d);
1506 chn = wrch ? wrch : rdch;
1508 *arg_i = AFMT_CHANNEL(chn->format);
1513 case SOUND_PCM_READ_CHANNELS:
1514 chn = wrch ? wrch : rdch;
1517 *arg_i = AFMT_CHANNEL(chn->format);
1525 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
1526 chn = wrch ? wrch : rdch;
1529 *arg_i = chn_getformats(chn);
1537 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1538 if (*arg_i != AFMT_QUERY) {
1540 PCM_ACQUIRE_QUICK(d);
1543 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1544 AFMT_CHANNEL(wrch->format),
1545 AFMT_EXTCHANNEL(wrch->format)));
1549 if (rdch && ret == 0) {
1551 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1552 AFMT_CHANNEL(rdch->format),
1553 AFMT_EXTCHANNEL(rdch->format)));
1558 PCM_RELEASE_QUICK(d);
1559 *arg_i = AFMT_ENCODING(tmp);
1561 chn = wrch ? wrch : rdch;
1563 *arg_i = AFMT_ENCODING(chn->format);
1568 case SNDCTL_DSP_SETFRAGMENT:
1569 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1571 uint32_t fragln = (*arg_i) & 0x0000ffff;
1572 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1574 uint32_t r_maxfrags, r_fragsz;
1576 RANGE(fragln, 4, 16);
1577 fragsz = 1 << fragln;
1580 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1583 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1584 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1586 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1587 PCM_ACQUIRE_QUICK(d);
1590 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1591 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1592 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1595 r_maxfrags = maxfrags;
1598 if (wrch && ret == 0) {
1600 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1601 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1602 fragsz = sndbuf_getblksz(wrch->bufsoft);
1604 } else { /* use whatever came from the read channel */
1605 maxfrags = r_maxfrags;
1608 PCM_RELEASE_QUICK(d);
1611 while (fragsz > 1) {
1615 *arg_i = (maxfrags << 16) | fragln;
1619 case SNDCTL_DSP_GETISPACE:
1620 /* return the size of data available in the input queue */
1622 audio_buf_info *a = (audio_buf_info *)arg;
1624 struct snd_dbuf *bs = rdch->bufsoft;
1627 a->bytes = sndbuf_getready(bs);
1628 a->fragments = a->bytes / sndbuf_getblksz(bs);
1629 a->fragstotal = sndbuf_getblkcnt(bs);
1630 a->fragsize = sndbuf_getblksz(bs);
1637 case SNDCTL_DSP_GETOSPACE:
1638 /* return space available in the output queue */
1640 audio_buf_info *a = (audio_buf_info *)arg;
1642 struct snd_dbuf *bs = wrch->bufsoft;
1645 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1646 a->bytes = sndbuf_getfree(bs);
1647 a->fragments = a->bytes / sndbuf_getblksz(bs);
1648 a->fragstotal = sndbuf_getblkcnt(bs);
1649 a->fragsize = sndbuf_getblksz(bs);
1656 case SNDCTL_DSP_GETIPTR:
1658 count_info *a = (count_info *)arg;
1660 struct snd_dbuf *bs = rdch->bufsoft;
1663 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1664 a->bytes = sndbuf_gettotal(bs);
1665 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1666 a->ptr = sndbuf_getfreeptr(bs);
1667 rdch->blocks = sndbuf_getblocks(bs);
1674 case SNDCTL_DSP_GETOPTR:
1676 count_info *a = (count_info *)arg;
1678 struct snd_dbuf *bs = wrch->bufsoft;
1681 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1682 a->bytes = sndbuf_gettotal(bs);
1683 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1684 a->ptr = sndbuf_getreadyptr(bs);
1685 wrch->blocks = sndbuf_getblocks(bs);
1692 case SNDCTL_DSP_GETCAPS:
1694 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1695 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1696 *arg_i |= PCM_CAP_DUPLEX;
1700 case SOUND_PCM_READ_BITS:
1701 chn = wrch ? wrch : rdch;
1704 if (chn->format & AFMT_8BIT)
1706 else if (chn->format & AFMT_16BIT)
1708 else if (chn->format & AFMT_24BIT)
1710 else if (chn->format & AFMT_32BIT)
1721 case SNDCTL_DSP_SETTRIGGER:
1724 rdch->flags &= ~CHN_F_NOTRIGGER;
1725 if (*arg_i & PCM_ENABLE_INPUT)
1730 rdch->flags |= CHN_F_NOTRIGGER;
1736 wrch->flags &= ~CHN_F_NOTRIGGER;
1737 if (*arg_i & PCM_ENABLE_OUTPUT)
1742 wrch->flags |= CHN_F_NOTRIGGER;
1748 case SNDCTL_DSP_GETTRIGGER:
1752 if (wrch->flags & CHN_F_TRIGGERED)
1753 *arg_i |= PCM_ENABLE_OUTPUT;
1758 if (rdch->flags & CHN_F_TRIGGERED)
1759 *arg_i |= PCM_ENABLE_INPUT;
1764 case SNDCTL_DSP_GETODELAY:
1766 struct snd_dbuf *bs = wrch->bufsoft;
1769 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1770 *arg_i = sndbuf_getready(bs);
1776 case SNDCTL_DSP_POST:
1779 wrch->flags &= ~CHN_F_NOTRIGGER;
1785 case SNDCTL_DSP_SETDUPLEX:
1787 * switch to full-duplex mode if card is in half-duplex
1788 * mode and is able to work in full-duplex mode
1791 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1792 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1797 * The following four ioctls are simple wrappers around mixer_ioctl
1798 * with no further processing. xcmd is short for "translated
1801 case SNDCTL_DSP_GETRECVOL:
1803 xcmd = SOUND_MIXER_READ_RECLEV;
1807 case SNDCTL_DSP_SETRECVOL:
1809 xcmd = SOUND_MIXER_WRITE_RECLEV;
1813 case SNDCTL_DSP_GETPLAYVOL:
1815 xcmd = SOUND_MIXER_READ_PCM;
1819 case SNDCTL_DSP_SETPLAYVOL:
1821 xcmd = SOUND_MIXER_WRITE_PCM;
1825 ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
1831 if (d->mixer_dev != NULL) {
1832 PCM_ACQUIRE_QUICK(d);
1833 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
1835 PCM_RELEASE_QUICK(d);
1841 case SNDCTL_DSP_GET_RECSRC_NAMES:
1842 case SNDCTL_DSP_GET_RECSRC:
1843 case SNDCTL_DSP_SET_RECSRC:
1844 if (d->mixer_dev != NULL) {
1845 PCM_ACQUIRE_QUICK(d);
1846 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1848 PCM_RELEASE_QUICK(d);
1854 * The following 3 ioctls aren't very useful at the moment. For
1855 * now, only a single channel is associated with a cdev (/dev/dspN
1856 * instance), so there's only a single output routing to use (i.e.,
1857 * the wrch bound to this cdev).
1859 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1861 oss_mixer_enuminfo *ei;
1862 ei = (oss_mixer_enuminfo *)arg;
1865 ei->version = 0; /* static for now */
1866 ei->strindex[0] = 0;
1870 strlcpy(ei->strings, wrch->name,
1871 sizeof(ei->strings));
1874 ei->strings[0] = '\0';
1878 case SNDCTL_DSP_GET_PLAYTGT:
1879 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1882 * OSSv4: "The value that was accepted by the device will
1883 * be returned back in the variable pointed by the
1892 case SNDCTL_DSP_SILENCE:
1894 * Flush the software (pre-feed) buffer, but try to minimize playback
1895 * interruption. (I.e., record unplayed samples with intent to
1896 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1902 struct snd_dbuf *bs;
1904 while (wrch->inprog != 0)
1905 cv_wait(&wrch->cv, wrch->lock);
1907 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1908 bs->sl = sndbuf_getready(bs);
1909 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1910 sndbuf_fillsilence(bs);
1917 case SNDCTL_DSP_SKIP:
1919 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1920 * playback buffer by moving the current write position immediately
1921 * before the point where the device is currently reading the samples."
1926 struct snd_dbuf *bs;
1928 while (wrch->inprog != 0)
1929 cv_wait(&wrch->cv, wrch->lock);
1931 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1932 sndbuf_softreset(bs);
1933 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1941 case SNDCTL_DSP_CURRENT_OPTR:
1942 case SNDCTL_DSP_CURRENT_IPTR:
1944 * @note Changing formats resets the buffer counters, which differs
1945 * from the 4Front drivers. However, I don't expect this to be
1946 * much of a problem.
1948 * @note In a test where @c CURRENT_OPTR is called immediately after write
1949 * returns, this driver is about 32K samples behind whereas
1950 * 4Front's is about 8K samples behind. Should determine source
1951 * of discrepancy, even if only out of curiosity.
1953 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1955 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1959 struct snd_dbuf *bs;
1962 oss_count_t *oc = (oss_count_t *)arg;
1967 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1968 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
1969 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
1971 oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
1972 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
1978 case SNDCTL_DSP_HALT_OUTPUT:
1979 case SNDCTL_DSP_HALT_INPUT:
1980 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
1990 case SNDCTL_DSP_LOW_WATER:
1992 * Set the number of bytes required to attract attention by
1997 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
2002 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
2007 case SNDCTL_DSP_GETERROR:
2009 * OSSv4 docs: "All errors and counters will automatically be
2010 * cleared to zeroes after the call so each call will return only
2011 * the errors that occurred after the previous invocation. ... The
2012 * play_underruns and rec_overrun fields are the only usefull fields
2013 * returned by OSS 4.0."
2016 audio_errinfo *ei = (audio_errinfo *)arg;
2018 bzero((void *)ei, sizeof(*ei));
2022 ei->play_underruns = wrch->xruns;
2028 ei->rec_overruns = rdch->xruns;
2035 case SNDCTL_DSP_SYNCGROUP:
2036 PCM_ACQUIRE_QUICK(d);
2037 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
2038 PCM_RELEASE_QUICK(d);
2041 case SNDCTL_DSP_SYNCSTART:
2042 PCM_ACQUIRE_QUICK(d);
2043 ret = dsp_oss_syncstart(*arg_i);
2044 PCM_RELEASE_QUICK(d);
2047 case SNDCTL_DSP_POLICY:
2048 PCM_ACQUIRE_QUICK(d);
2049 ret = dsp_oss_policy(wrch, rdch, *arg_i);
2050 PCM_RELEASE_QUICK(d);
2053 case SNDCTL_DSP_COOKEDMODE:
2054 PCM_ACQUIRE_QUICK(d);
2055 if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
2056 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
2057 PCM_RELEASE_QUICK(d);
2059 case SNDCTL_DSP_GET_CHNORDER:
2060 PCM_ACQUIRE_QUICK(d);
2061 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
2062 PCM_RELEASE_QUICK(d);
2064 case SNDCTL_DSP_SET_CHNORDER:
2065 PCM_ACQUIRE_QUICK(d);
2066 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
2067 PCM_RELEASE_QUICK(d);
2069 case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
2070 PCM_ACQUIRE_QUICK(d);
2071 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
2072 PCM_RELEASE_QUICK(d);
2074 case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
2077 #ifdef OSSV4_EXPERIMENT
2079 * XXX The following ioctls are not yet supported and just return
2082 case SNDCTL_DSP_GETOPEAKS:
2083 case SNDCTL_DSP_GETIPEAKS:
2084 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
2088 oss_peaks_t *op = (oss_peaks_t *)arg;
2092 ret = chn_getpeaks(chn, &lpeak, &rpeak);
2104 * XXX Once implemented, revisit this for proper cv protection
2107 case SNDCTL_GETLABEL:
2108 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
2110 case SNDCTL_SETLABEL:
2111 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
2113 case SNDCTL_GETSONG:
2114 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
2116 case SNDCTL_SETSONG:
2117 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
2119 case SNDCTL_SETNAME:
2120 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
2124 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
2125 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
2126 * 4Front Technologies.
2128 case SNDCTL_DSP_READCTL:
2129 case SNDCTL_DSP_WRITECTL:
2132 #endif /* !0 (explicitly omitted ioctls) */
2134 #endif /* !OSSV4_EXPERIMENT */
2135 case SNDCTL_DSP_MAPINBUF:
2136 case SNDCTL_DSP_MAPOUTBUF:
2137 case SNDCTL_DSP_SETSYNCRO:
2140 case SNDCTL_DSP_SUBDIVIDE:
2141 case SOUND_PCM_WRITE_FILTER:
2142 case SOUND_PCM_READ_FILTER:
2143 /* dunno what these do, don't sound important */
2146 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
2157 dsp_poll(struct cdev *i_dev, int events, struct thread *td)
2159 struct snddev_info *d;
2160 struct pcm_channel *wrch, *rdch;
2163 d = dsp_get_info(i_dev);
2164 if (!DSP_REGISTERED(d, i_dev))
2173 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2175 if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) {
2176 e = (events & (POLLOUT | POLLWRNORM));
2178 ret |= chn_poll(wrch, e, td);
2181 if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) {
2182 e = (events & (POLLIN | POLLRDNORM));
2184 ret |= chn_poll(rdch, e, td);
2187 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2195 dsp_mmap(struct cdev *i_dev, vm_ooffset_t offset, vm_paddr_t *paddr,
2196 int nprot, vm_memattr_t *memattr)
2199 /* XXX memattr is not honored */
2200 *paddr = vtophys(offset);
2205 dsp_mmap_single(struct cdev *i_dev, vm_ooffset_t *offset,
2206 vm_size_t size, struct vm_object **object, int nprot)
2208 struct snddev_info *d;
2209 struct pcm_channel *wrch, *rdch, *c;
2212 * Reject PROT_EXEC by default. It just doesn't makes sense.
2213 * Unfortunately, we have to give up this one due to linux_mmap
2216 * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
2220 if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
2221 (dsp_mmap_allow_prot_exec == 0 &&
2222 SV_CURPROC_ABI() != SV_ABI_LINUX)))
2224 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
2229 * PROT_READ (alone) selects the input buffer.
2230 * PROT_WRITE (alone) selects the output buffer.
2231 * PROT_WRITE|PROT_READ together select the output buffer.
2233 if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
2236 d = dsp_get_info(i_dev);
2237 if (!DSP_REGISTERED(d, i_dev))
2242 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2244 c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
2245 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
2246 (*offset + size) > sndbuf_getsize(c->bufsoft) ||
2247 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
2248 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
2249 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2255 wrch->flags |= CHN_F_MMAP;
2257 rdch->flags |= CHN_F_MMAP;
2259 *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
2260 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2261 *object = vm_pager_allocate(OBJT_DEVICE, i_dev,
2262 size, nprot, *offset, curthread->td_ucred);
2266 if (*object == NULL)
2271 /* So much for dev_stdclone() */
2273 dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
2277 len = strlen(namep);
2279 if (bcmp(name, namep, len) != 0)
2284 if (isdigit(*name) == 0)
2289 if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
2292 for (*u = 0; isdigit(*name) != 0; name++) {
2300 return ((use_sep == 0) ? 0 : ENODEV);
2302 if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
2307 if (*name == '0' && name[1] != '\0')
2310 for (*c = 0; isdigit(*name) != 0; name++) {
2324 dsp_clone(void *arg,
2325 #if __FreeBSD_version >= 600034
2328 char *name, int namelen, struct cdev **dev)
2330 struct snddev_info *d;
2331 struct snd_clone_entry *ce;
2332 struct pcm_channel *c;
2333 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
2334 char *devname, *devcmp, *devsep;
2336 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
2350 for (i = 0; unit == -1 &&
2351 i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2352 devtype = dsp_cdevs[i].type;
2353 devcmp = dsp_cdevs[i].name;
2354 devsep = dsp_cdevs[i].sep;
2355 devname = dsp_cdevs[i].alias;
2356 if (devname == NULL)
2358 devhw = dsp_cdevs[i].hw;
2359 devcmax = dsp_cdevs[i].max - 1;
2360 if (strcmp(name, devcmp) == 0)
2362 else if (dsp_stdclone(name, devcmp, devsep,
2363 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
2369 d = devclass_get_softc(pcm_devclass, unit);
2370 if (!PCM_REGISTERED(d) || d->clones == NULL)
2373 /* XXX Need Giant magic entry ??? */
2376 if (snd_clone_disabled(d->clones)) {
2385 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2388 KASSERT(devcmax <= dsp_cmax,
2389 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2390 if (cunit > devcmax) {
2391 PCM_RELEASE_QUICK(d);
2394 udcmask |= snd_c2unit(cunit);
2395 CHN_FOREACH(c, d, channels.pcm) {
2397 if (c->unit != udcmask) {
2402 udcmask &= ~snd_c2unit(cunit);
2404 * Temporarily increase clone maxunit to overcome
2405 * vchan flexibility.
2407 * # sysctl dev.pcm.0.play.vchans=256
2408 * dev.pcm.0.play.vchans: 1 -> 256
2409 * # cat /dev/zero > /dev/dsp0.vp255 &
2411 * # sysctl dev.pcm.0.play.vchans=0
2412 * dev.pcm.0.play.vchans: 256 -> 1
2414 * [1] + running cat /dev/zero > /dev/dsp0.vp255
2416 * # cat /dev/zero > /dev/dsp0.vp255
2417 * zsh: operation not supported: /dev/dsp0.vp255
2419 tumax = snd_clone_getmaxunit(d->clones);
2421 snd_clone_setmaxunit(d->clones, cunit);
2424 goto dsp_clone_alloc;
2427 * Ok, so we're requesting unallocated vchan, but still
2428 * within maximum vchan limit.
2430 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
2431 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
2432 cunit < snd_maxautovchans) {
2433 udcmask &= ~snd_c2unit(cunit);
2434 tumax = snd_clone_getmaxunit(d->clones);
2436 snd_clone_setmaxunit(d->clones, cunit);
2439 goto dsp_clone_alloc;
2441 PCM_RELEASE_QUICK(d);
2446 ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
2448 snd_clone_setmaxunit(d->clones, tumax);
2450 udcmask |= snd_c2unit(cunit);
2451 *dev = make_dev(&dsp_cdevsw, PCMMINOR(udcmask),
2452 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
2453 devname, unit, devsep, cunit);
2454 snd_clone_register(ce, *dev);
2457 PCM_RELEASE_QUICK(d);
2464 dsp_sysinit(void *p)
2466 if (dsp_ehtag != NULL)
2468 /* initialize unit numbering */
2470 dsp_umax = PCMMAXUNIT;
2471 dsp_cmax = PCMMAXCHAN;
2472 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2476 dsp_sysuninit(void *p)
2478 if (dsp_ehtag == NULL)
2480 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2484 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2485 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2488 dsp_unit2name(char *buf, size_t len, int unit)
2492 KASSERT(buf != NULL && len != 0,
2493 ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2495 dtype = snd_unit2d(unit);
2497 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2498 if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2500 snprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
2501 snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
2509 * @brief Handler for SNDCTL_AUDIOINFO.
2511 * Gathers information about the audio device specified in ai->dev. If
2512 * ai->dev == -1, then this function gathers information about the current
2513 * device. If the call comes in on a non-audio device and ai->dev == -1,
2516 * This routine is supposed to go practically straight to the hardware,
2517 * getting capabilities directly from the sound card driver, side-stepping
2518 * the intermediate channel interface.
2520 * Note, however, that the usefulness of this command is significantly
2521 * decreased when requesting info about any device other than the one serving
2522 * the request. While each snddev_channel refers to a specific device node,
2523 * the converse is *not* true. Currently, when a sound device node is opened,
2524 * the sound subsystem scans for an available audio channel (or channels, if
2525 * opened in read+write) and then assigns them to the si_drv[12] private
2526 * data fields. As a result, any information returned linking a channel to
2527 * a specific character device isn't necessarily accurate.
2530 * Calling threads must not hold any snddev_info or pcm_channel locks.
2532 * @param dev device on which the ioctl was issued
2533 * @param ai ioctl request data container
2536 * @retval EINVAL ai->dev specifies an invalid device
2538 * @todo Verify correctness of Doxygen tags. ;)
2541 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2543 struct pcmchan_caps *caps;
2544 struct pcm_channel *ch;
2545 struct snddev_info *d;
2547 int i, nchan, *rates, minch, maxch;
2548 char *devname, buf[CHN_NAMELEN];
2551 * If probing the device that received the ioctl, make sure it's a
2552 * DSP device. (Users may use this ioctl with /dev/mixer and
2555 if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw)
2561 bzero(buf, sizeof(buf));
2564 * Search for the requested audio device (channel). Start by
2565 * iterating over pcm devices.
2567 for (i = 0; pcm_devclass != NULL &&
2568 i < devclass_get_maxunit(pcm_devclass); i++) {
2569 d = devclass_get_softc(pcm_devclass, i);
2570 if (!PCM_REGISTERED(d))
2573 /* XXX Need Giant magic entry ??? */
2575 /* See the note in function docblock */
2576 PCM_UNLOCKASSERT(d);
2579 CHN_FOREACH(ch, d, channels.pcm) {
2580 CHN_UNLOCKASSERT(ch);
2582 if (ai->dev == -1) {
2583 if (DSP_REGISTERED(d, i_dev) &&
2584 (ch == PCM_RDCH(i_dev) || /* record ch */
2585 ch == PCM_WRCH(i_dev))) { /* playback ch */
2586 devname = dsp_unit2name(buf,
2587 sizeof(buf), ch->unit);
2589 } else if (ai->dev == nchan) {
2590 devname = dsp_unit2name(buf, sizeof(buf),
2593 if (devname != NULL)
2599 if (devname != NULL) {
2601 * At this point, the following synchronization stuff
2603 * - a specific PCM device is locked.
2604 * - a specific audio channel has been locked, so be
2605 * sure to unlock when exiting;
2608 caps = chn_getcaps(ch);
2611 * With all handles collected, zero out the user's
2612 * container and begin filling in its fields.
2614 bzero((void *)ai, sizeof(oss_audioinfo));
2617 strlcpy(ai->name, ch->name, sizeof(ai->name));
2619 if ((ch->flags & CHN_F_BUSY) == 0)
2622 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2626 * @c cmd - OSSv4 docs: "Only supported under Linux at
2627 * this moment." Cop-out, I know, but I'll save
2628 * running around in the process table for later.
2629 * Is there a risk of leaking information?
2634 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2635 * Note, however, that a single channel operates in
2636 * only one direction, so PCM_CAP_DUPLEX is out.
2639 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2640 * these in pcmchan::caps?
2642 ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2643 ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2646 * Collect formats supported @b natively by the
2647 * device. Also determine min/max channels. (I.e.,
2648 * mono, stereo, or both?)
2650 * If any channel is stereo, maxch = 2;
2651 * if all channels are stereo, minch = 2, too;
2652 * if any channel is mono, minch = 1;
2653 * and if all channels are mono, maxch = 1.
2658 for (i = 0; caps->fmtlist[i]; i++) {
2659 fmts |= caps->fmtlist[i];
2660 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2661 minch = (minch == 0) ? 2 : minch;
2665 maxch = (maxch == 0) ? 1 : maxch;
2669 if (ch->direction == PCMDIR_PLAY)
2670 ai->oformats = fmts;
2672 ai->iformats = fmts;
2676 * @c magic - OSSv4 docs: "Reserved for internal use
2680 * @c card_number - OSSv4 docs: "Number of the sound
2681 * card where this device belongs or -1 if this
2682 * information is not available. Applications
2683 * should normally not use this field for any
2686 ai->card_number = -1;
2688 * @todo @c song_name - depends first on
2689 * SNDCTL_[GS]ETSONG @todo @c label - depends
2690 * on SNDCTL_[GS]ETLABEL
2691 * @todo @c port_number - routing information?
2693 ai->port_number = -1;
2694 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2697 * @c real_device - OSSv4 docs: "Obsolete."
2699 ai->real_device = -1;
2700 strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
2701 strlcat(ai->devnode, devname, sizeof(ai->devnode));
2702 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2705 * @c flags - OSSv4 docs: "Reserved for future use."
2708 * @c binding - OSSv4 docs: "Reserved for future use."
2710 * @todo @c handle - haven't decided how to generate
2711 * this yet; bus, vendor, device IDs?
2713 ai->min_rate = caps->minspeed;
2714 ai->max_rate = caps->maxspeed;
2716 ai->min_channels = minch;
2717 ai->max_channels = maxch;
2719 ai->nrates = chn_getrates(ch, &rates);
2720 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2721 ai->nrates = OSS_MAX_SAMPLE_RATES;
2723 for (i = 0; i < ai->nrates; i++)
2724 ai->rates[i] = rates[i];
2726 ai->next_play_engine = 0;
2727 ai->next_rec_engine = 0;
2734 if (devname != NULL)
2738 /* Exhausted the search -- nothing is locked, so return. */
2743 * @brief Assigns a PCM channel to a sync group.
2745 * Sync groups are used to enable audio operations on multiple devices
2746 * simultaneously. They may be used with any number of devices and may
2747 * span across applications. Devices are added to groups with
2748 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2749 * SNDCTL_DSP_SYNCSTART ioctl.
2751 * If the @c id field of the @c group parameter is set to zero, then a new
2752 * sync group is created. Otherwise, wrch and rdch (if set) are added to
2753 * the group specified.
2755 * @todo As far as memory allocation, should we assume that things are
2756 * okay and allocate with M_WAITOK before acquiring channel locks,
2757 * freeing later if not?
2759 * @param wrch output channel associated w/ device (if any)
2760 * @param rdch input channel associated w/ device (if any)
2761 * @param group Sync group parameters
2764 * @retval non-zero error to be propagated upstream
2767 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2769 struct pcmchan_syncmember *smrd, *smwr;
2770 struct pcmchan_syncgroup *sg;
2779 * Free_unr() may sleep, so store released syncgroup IDs until after
2780 * all locks are released.
2782 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2787 * - Insert channel(s) into group's member list.
2788 * - Set CHN_F_NOTRIGGER on channel(s).
2789 * - Stop channel(s).
2793 * If device's channels are already mapped to a group, unmap them.
2797 sg_ids[0] = chn_syncdestroy(wrch);
2802 sg_ids[1] = chn_syncdestroy(rdch);
2806 * Verify that mode matches character device properites.
2807 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2808 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2810 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2811 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2817 * An id of zero indicates the user wants to create a new
2820 if (group->id == 0) {
2821 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2823 SLIST_INIT(&sg->members);
2824 sg->id = alloc_unr(pcmsg_unrhdr);
2827 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2831 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2832 if (sg->id == group->id)
2839 /* Couldn't create or find a syncgroup. Fail. */
2844 * Allocate a syncmember, assign it and a channel together, and
2845 * insert into syncgroup.
2847 if (group->mode & PCM_ENABLE_INPUT) {
2848 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2854 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2859 rdch->flags |= CHN_F_NOTRIGGER;
2863 if (group->mode & PCM_ENABLE_OUTPUT) {
2864 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2870 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2875 wrch->flags |= CHN_F_NOTRIGGER;
2883 free(smrd, M_DEVBUF);
2884 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2886 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2904 free_unr(pcmsg_unrhdr, sg_ids[0]);
2906 free_unr(pcmsg_unrhdr, sg_ids[1]);
2908 free_unr(pcmsg_unrhdr, sg_ids[2]);
2914 * @brief Launch a sync group into action
2916 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
2917 * iterates over all members, triggering them along the way.
2919 * @note Caller must not hold any channel locks.
2921 * @param sg_id sync group identifier
2924 * @retval non-zero error worthy of propagating upstream to user
2927 dsp_oss_syncstart(int sg_id)
2929 struct pcmchan_syncmember *sm, *sm_tmp;
2930 struct pcmchan_syncgroup *sg;
2931 struct pcm_channel *c;
2934 /* Get the synclists lock */
2941 /* Search for syncgroup by ID */
2942 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2943 if (sg->id == sg_id)
2947 /* Return EINVAL if not found */
2953 /* Any removals resulting in an empty group should've handled this */
2954 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
2957 * Attempt to lock all member channels - if any are already
2958 * locked, unlock those acquired, sleep for a bit, and try
2961 SLIST_FOREACH(sm, &sg->members, link) {
2962 if (CHN_TRYLOCK(sm->ch) == 0) {
2963 int timo = hz * 5/1000;
2967 /* Release all locked channels so far, retry */
2968 SLIST_FOREACH(sm_tmp, &sg->members, link) {
2969 /* sm is the member already locked */
2972 CHN_UNLOCK(sm_tmp->ch);
2975 /** @todo Is PRIBIO correct/ */
2976 ret = msleep(sm, &snd_pcm_syncgroups_mtx,
2977 PRIBIO | PCATCH, "pcmsg", timo);
2978 if (ret == EINTR || ret == ERESTART)
2982 ret = 0; /* Assumes ret == EAGAIN... */
2985 } while (needlocks && ret == 0);
2987 /* Proceed only if no errors encountered. */
2989 /* Launch channels */
2990 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
2991 SLIST_REMOVE_HEAD(&sg->members, link);
2996 c->flags &= ~CHN_F_NOTRIGGER;
3002 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
3009 * Free_unr() may sleep, so be sure to give up the syncgroup lock
3013 free_unr(pcmsg_unrhdr, sg_id);
3019 * @brief Handler for SNDCTL_DSP_POLICY
3021 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
3022 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
3023 * specifying those two parameters, s/he simply selects a number from 0..10
3024 * which corresponds to a buffer size. Smaller numbers request smaller
3025 * buffers with lower latencies (at greater overhead from more frequent
3026 * interrupts), while greater numbers behave in the opposite manner.
3028 * The 4Front spec states that a value of 5 should be the default. However,
3029 * this implementation deviates slightly by using a linear scale without
3030 * consulting drivers. I.e., even though drivers may have different default
3031 * buffer sizes, a policy argument of 5 will have the same result across
3034 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
3037 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
3038 * work with hardware drivers directly.
3040 * @note PCM channel arguments must not be locked by caller.
3042 * @param wrch Pointer to opened playback channel (optional; may be NULL)
3043 * @param rdch " recording channel (optional; may be NULL)
3044 * @param policy Integer from [0:10]
3046 * @retval 0 constant (for now)
3049 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
3053 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
3056 /* Default: success */
3061 ret = chn_setlatency(rdch, policy);
3065 if (wrch && ret == 0) {
3067 ret = chn_setlatency(wrch, policy);
3078 * @brief Enable or disable "cooked" mode
3080 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
3081 * is the default, the sound system handles rate and format conversions
3082 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
3083 * operates with 44100Hz/16bit/signed samples).
3085 * Disabling cooked mode is intended for applications wanting to mmap()
3086 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
3087 * feeder architecture, presumably to gain as much control over audio
3088 * hardware as possible.
3090 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
3093 * @param wrch playback channel (optional; may be NULL)
3094 * @param rdch recording channel (optional; may be NULL)
3095 * @param enabled 0 = raw mode, 1 = cooked mode
3097 * @retval EINVAL Operation not yet supported.
3100 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
3104 * XXX I just don't get it. Why don't they call it
3105 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
3106 * This is just plain so confusing, incoherent,
3107 * <insert any non-printable characters here>.
3109 if (!(enabled == 1 || enabled == 0))
3113 * I won't give in. I'm inverting its logic here and now.
3114 * Brag all you want, but "BITPERFECT" should be the better
3117 enabled ^= 0x00000001;
3121 wrch->flags &= ~CHN_F_BITPERFECT;
3122 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3128 rdch->flags &= ~CHN_F_BITPERFECT;
3129 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3137 * @brief Retrieve channel interleaving order
3139 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
3141 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
3144 * @note As the ioctl definition is still under construction, FreeBSD
3145 * does not currently support SNDCTL_DSP_GET_CHNORDER.
3147 * @param wrch playback channel (optional; may be NULL)
3148 * @param rdch recording channel (optional; may be NULL)
3149 * @param map channel map (result will be stored there)
3151 * @retval EINVAL Operation not yet supported.
3154 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3156 struct pcm_channel *ch;
3159 ch = (wrch != NULL) ? wrch : rdch;
3162 ret = chn_oss_getorder(ch, map);
3171 * @brief Specify channel interleaving order
3173 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
3175 * @note As the ioctl definition is still under construction, FreeBSD
3176 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
3178 * @param wrch playback channel (optional; may be NULL)
3179 * @param rdch recording channel (optional; may be NULL)
3180 * @param map channel map
3182 * @retval EINVAL Operation not yet supported.
3185 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3193 ret = chn_oss_setorder(wrch, map);
3197 if (ret == 0 && rdch != NULL) {
3199 ret = chn_oss_setorder(rdch, map);
3207 dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
3210 struct pcm_channel *ch;
3215 ch = (wrch != NULL) ? wrch : rdch;
3219 ret = chn_oss_getmask(ch, &chnmask);
3230 #ifdef OSSV4_EXPERIMENT
3232 * @brief Retrieve an audio device's label
3234 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
3236 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3239 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
3240 * consoles) can show variable "labels" for certain controls. By default
3241 * the application name (say quake) is shown as the label but
3242 * applications may change the labels themselves."
3244 * @note As the ioctl definition is still under construction, FreeBSD
3245 * does not currently support @c SNDCTL_GETLABEL.
3247 * @param wrch playback channel (optional; may be NULL)
3248 * @param rdch recording channel (optional; may be NULL)
3249 * @param label label gets copied here
3251 * @retval EINVAL Operation not yet supported.
3254 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3260 * @brief Specify an audio device's label
3262 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
3263 * comments for @c dsp_oss_getlabel immediately above.
3265 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3268 * @note As the ioctl definition is still under construction, FreeBSD
3269 * does not currently support SNDCTL_SETLABEL.
3271 * @param wrch playback channel (optional; may be NULL)
3272 * @param rdch recording channel (optional; may be NULL)
3273 * @param label label gets copied from here
3275 * @retval EINVAL Operation not yet supported.
3278 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3284 * @brief Retrieve name of currently played song
3286 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
3287 * tell the system the name of the currently playing song, which would be
3288 * visible in @c /dev/sndstat.
3290 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
3293 * @note As the ioctl definition is still under construction, FreeBSD
3294 * does not currently support SNDCTL_GETSONG.
3296 * @param wrch playback channel (optional; may be NULL)
3297 * @param rdch recording channel (optional; may be NULL)
3298 * @param song song name gets copied here
3300 * @retval EINVAL Operation not yet supported.
3303 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3309 * @brief Retrieve name of currently played song
3311 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
3312 * tell the system the name of the currently playing song, which would be
3313 * visible in @c /dev/sndstat.
3315 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
3318 * @note As the ioctl definition is still under construction, FreeBSD
3319 * does not currently support SNDCTL_SETSONG.
3321 * @param wrch playback channel (optional; may be NULL)
3322 * @param rdch recording channel (optional; may be NULL)
3323 * @param song song name gets copied from here
3325 * @retval EINVAL Operation not yet supported.
3328 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3334 * @brief Rename a device
3336 * This is a handler for the @c SNDCTL_SETNAME ioctl.
3338 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
3341 * From Hannu@4Front: "This call is used to change the device name
3342 * reported in /dev/sndstat and ossinfo. So instead of using some generic
3343 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
3344 * name depending on the current context (for example 'OSS virtual wave table
3345 * synth' or 'VoIP link to London')."
3347 * @note As the ioctl definition is still under construction, FreeBSD
3348 * does not currently support SNDCTL_SETNAME.
3350 * @param wrch playback channel (optional; may be NULL)
3351 * @param rdch recording channel (optional; may be NULL)
3352 * @param name new device name gets copied from here
3354 * @retval EINVAL Operation not yet supported.
3357 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
3361 #endif /* !OSSV4_EXPERIMENT */